mirror of
https://github.com/gbowne1/ClassicOS.git
synced 2025-12-06 21:45:26 -08:00
Compare commits
10 Commits
3036ee3dfd
...
gbowne1-pa
| Author | SHA1 | Date | |
|---|---|---|---|
| 4047bc3936 | |||
| 7e54f0de66 | |||
| 940b2810cb | |||
| 01f85f97ec | |||
| fd2c567d29 | |||
| 9de9cc6523 | |||
| e9a78c835a | |||
| 77400d8f5a | |||
| cdf5676085 | |||
|
|
8743fa9e24 |
2
Makefile
2
Makefile
@@ -37,7 +37,7 @@ $(BUILD_DIR)/%.o: kernel/%.c
|
||||
$(CC) -std=c11 -ffreestanding -nostdlib -fno-stack-protector -m32 -g -c -o $@ $<
|
||||
|
||||
kernel: $(KERNEL_OBJ) | $(BUILD_DIR)
|
||||
$(LD) -melf_i386 -Tbootloader/linker.ld -o $(BUILD_DIR)/kernel.elf $(KERNEL_OBJ)
|
||||
$(LD) -melf_i386 -Tkernel/linker.ld -o $(BUILD_DIR)/kernel.elf $(KERNEL_OBJ)
|
||||
|
||||
$(DISK_IMG): stage1 stage2 kernel
|
||||
dd if=$(BUILD_DIR)/stage1.bin of=$@
|
||||
|
||||
25
kernel/context_switch.s
Normal file
25
kernel/context_switch.s
Normal file
@@ -0,0 +1,25 @@
|
||||
.global ctx_switch
|
||||
|
||||
; void ctx_switch(uint32_t **old_sp_ptr, uint32_t *new_sp);
|
||||
; Arguments on stack (cdecl convention):
|
||||
; [ESP + 4] -> old_sp_ptr (pointer to the 'stack_ptr' field of current task)
|
||||
; [ESP + 8] -> new_sp (value of 'stack_ptr' of the next task)
|
||||
|
||||
ctx_switch:
|
||||
; 1. Save the context of the CURRENT task
|
||||
pushf ; Save EFLAGS (CPU status flags)
|
||||
pusha ; Save all General Purpose Regs (EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI)
|
||||
|
||||
; 2. Save the current stack pointer (ESP) into the pointer passed as 1st arg
|
||||
mov eax, [esp + 40] ; Get 1st argument (old_sp_ptr). Offset 40 = 36 (regs) + 4 (ret addr)
|
||||
mov [eax], esp ; *old_sp_ptr = ESP
|
||||
|
||||
; 3. Load the stack pointer of the NEW task
|
||||
mov esp, [esp + 44] ; Get 2nd argument (new_sp). Offset 44 = 40 + 4
|
||||
|
||||
; 4. Restore the context of the NEW task
|
||||
popa ; Restore all General Purpose Regs
|
||||
popf ; Restore EFLAGS
|
||||
|
||||
; 5. Jump to the new task (The 'ret' pops EIP from the new stack)
|
||||
ret
|
||||
@@ -1,36 +1,79 @@
|
||||
#include "display.h"
|
||||
#include "io.h" // Include your I/O header for port access
|
||||
#include "io.h"
|
||||
#include "vga.h"
|
||||
|
||||
// Initialize the display
|
||||
void init_display(void) {
|
||||
// Initialize VGA settings, if necessary
|
||||
// This could involve setting up the VGA mode, etc.
|
||||
set_display_mode(0x13); // Example: Set to 320x200 256-color mode
|
||||
// Initialize the VGA driver. This typically sets up the 80x25 text mode,
|
||||
// clears the screen, and sets the cursor.
|
||||
vga_init();
|
||||
}
|
||||
|
||||
// Enumerate connected displays
|
||||
void enumerate_displays(void) {
|
||||
// This is a simplified example. Actual enumeration may require
|
||||
// reading from specific VGA registers or using BIOS interrupts.
|
||||
// This function is often a complex operation in a real driver.
|
||||
// In this simplified kernel/VGA text mode environment, we use printf
|
||||
// to output a message and rely on the fact that VGA is present.
|
||||
|
||||
// For demonstration, we will just print a message
|
||||
// In a real driver, you would check the VGA registers
|
||||
// to determine connected displays.
|
||||
clear_display();
|
||||
// Here you would typically read from VGA registers to find connected displays
|
||||
// For example, using inb() to read from VGA ports
|
||||
// Clear the display before printing a message
|
||||
vga_clear(vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK));
|
||||
|
||||
// Output a simplified enumeration message
|
||||
vga_printf("Display: Standard VGA Text Mode (80x25) Detected.\n");
|
||||
|
||||
// In a real driver, you would use inb() and outb() with specific VGA ports
|
||||
// to read information (e.g., from the CRTC registers 0x3D4/0x3D5)
|
||||
// to check for display presence or configuration.
|
||||
}
|
||||
|
||||
// Set the display mode
|
||||
// NOTE: Setting arbitrary VGA modes (like 0x13 for 320x200) is very complex
|
||||
// and requires writing hundreds of register values, often done via BIOS in
|
||||
// real mode. Since we are in protected mode and have a simple text driver,
|
||||
// this function is kept simple or treated as a placeholder for full mode changes.
|
||||
void set_display_mode(uint8_t mode) {
|
||||
// Set the VGA mode by writing to the appropriate registers
|
||||
// Check if the requested mode is a known mode (e.g., VGA Text Mode 3)
|
||||
// For this example, we simply acknowledge the call.
|
||||
// A true mode set would involve complex register sequencing.
|
||||
|
||||
// The provided vga.c is a Text Mode driver, so a graphical mode set
|
||||
// like 0x13 (320x200 256-color) would break the existing vga_printf functionality.
|
||||
|
||||
// A simplified text-mode-specific response:
|
||||
if (mode == 0x03) { // Mode 3 is standard 80x25 text mode
|
||||
vga_printf("Display mode set to 80x25 Text Mode (Mode 0x03).\n");
|
||||
vga_init(); // Re-initialize the text mode
|
||||
} else {
|
||||
// Simple I/O example based on the original structure (Caution: Incomplete for full mode set)
|
||||
outb(VGA_PORT, mode); // Example function to write to a port
|
||||
vga_printf("Attempting to set display mode to 0x%x. (Warning: May break current display)\n", mode);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear the display
|
||||
void clear_display(void) {
|
||||
// Clear the display by filling it with a color
|
||||
// This is a placeholder for actual clearing logic
|
||||
// You would typically write to video memory here
|
||||
// Use the VGA driver's clear function, typically clearing to black on light grey
|
||||
// or black on black. We'll use the black on light grey from vga_init for consistency.
|
||||
vga_clear(vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_LIGHT_GREY));
|
||||
// Reset cursor to 0, 0
|
||||
vga_set_cursor_position(0, 0);
|
||||
}
|
||||
|
||||
// Helper function to write a string
|
||||
void display_write_string(const char* str) {
|
||||
// Use the VGA driver's string writing function
|
||||
vga_write_string(str, my_strlen(str));
|
||||
}
|
||||
|
||||
// Helper function to print a formatted string
|
||||
void display_printf(const char* format, ...) {
|
||||
// Use the VGA driver's printf function
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
||||
// The vga_printf function already handles the va_list internally,
|
||||
// so we can just call it directly.
|
||||
vga_printf(format, args);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@@ -2,13 +2,21 @@
|
||||
#define DISPLAY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "vga.h" // Include VGA functions
|
||||
|
||||
#define VGA_PORT 0x3C0 // Base port for VGA
|
||||
#define VGA_PORT 0x3C0 // Base port for VGA (Often used for general control, though 0x3D4/0x3D5 are used for cursor)
|
||||
|
||||
// Function prototypes
|
||||
void init_display(void);
|
||||
void enumerate_displays(void);
|
||||
void set_display_mode(uint8_t mode);
|
||||
void set_display_mode(uint8_t mode); // In this context, modes are typically BIOS or VESA modes, which are complex.
|
||||
// We'll treat this as a placeholder/simple mode call.
|
||||
void clear_display(void);
|
||||
|
||||
// New function to write a string using the VGA driver
|
||||
void display_write_string(const char* str);
|
||||
|
||||
// New function to print a formatted string using the VGA driver
|
||||
void display_printf(const char* format, ...);
|
||||
|
||||
#endif // DISPLAY_H
|
||||
|
||||
181
kernel/fat12.c
181
kernel/fat12.c
@@ -1,5 +1,184 @@
|
||||
#include "fat12.h"
|
||||
#include <stddef.h> // for NULL
|
||||
|
||||
// --- Globals for Filesystem State ---
|
||||
static fat12_bpb_t bpb;
|
||||
static uint32_t fat_start_lba;
|
||||
static uint32_t root_dir_lba;
|
||||
static uint32_t data_start_lba;
|
||||
static uint32_t root_dir_sectors;
|
||||
|
||||
// Scratch buffer to read sectors (avoids large stack usage)
|
||||
static uint8_t g_sector_buffer[FAT12_SECTOR_SIZE];
|
||||
|
||||
// --- Utils (Since we don't have string.h) ---
|
||||
static int k_memcmp(const void *s1, const void *s2, uint32_t n) {
|
||||
const uint8_t *p1 = (const uint8_t *)s1;
|
||||
const uint8_t *p2 = (const uint8_t *)s2;
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
if (p1[i] != p2[i]) return p1[i] - p2[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Converts "file.txt" to "FILE TXT" for comparison
|
||||
static void to_fat_name(const char *src, char *dest) {
|
||||
// Initialize with spaces
|
||||
for(int i=0; i<11; i++) dest[i] = ' ';
|
||||
|
||||
int i = 0, j = 0;
|
||||
// Copy Name
|
||||
while (src[i] != '\0' && src[i] != '.' && j < 8) {
|
||||
// Convert to uppercase (simple version)
|
||||
char c = src[i];
|
||||
if (c >= 'a' && c <= 'z') c -= 32;
|
||||
dest[j++] = c;
|
||||
i++;
|
||||
}
|
||||
|
||||
// Skip extension dot
|
||||
if (src[i] == '.') i++;
|
||||
|
||||
// Copy Extension
|
||||
j = 8;
|
||||
while (src[i] != '\0' && j < 11) {
|
||||
char c = src[i];
|
||||
if (c >= 'a' && c <= 'z') c -= 32;
|
||||
dest[j++] = c;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Core Logic ---
|
||||
|
||||
void fat12_init() {
|
||||
// Filesystem initialization code
|
||||
// 1. Read Boot Sector (LBA 0)
|
||||
disk_read_sector(0, g_sector_buffer);
|
||||
|
||||
// 2. Copy BPB data safely
|
||||
// We cast the buffer to our struct
|
||||
fat12_bpb_t *boot_sector = (fat12_bpb_t*)g_sector_buffer;
|
||||
bpb = *boot_sector;
|
||||
|
||||
// 3. Calculate System Offsets
|
||||
fat_start_lba = bpb.reserved_sectors;
|
||||
|
||||
// Root Dir starts after FATs
|
||||
// LBA = Reserved + (FatCount * SectorsPerFat)
|
||||
root_dir_lba = fat_start_lba + (bpb.fat_count * bpb.sectors_per_fat);
|
||||
|
||||
// Calculate size of Root Directory in sectors
|
||||
// (Entries * 32 bytes) / 512
|
||||
root_dir_sectors = (bpb.dir_entries_count * 32 + FAT12_SECTOR_SIZE - 1) / FAT12_SECTOR_SIZE;
|
||||
|
||||
// Data starts after Root Directory
|
||||
data_start_lba = root_dir_lba + root_dir_sectors;
|
||||
}
|
||||
|
||||
// Helper: Read the FAT table to find the NEXT cluster
|
||||
static uint16_t fat12_get_next_cluster(uint16_t current_cluster) {
|
||||
// FAT12 Offset Calculation:
|
||||
// Offset = Cluster + (Cluster / 2)
|
||||
uint32_t fat_offset = current_cluster + (current_cluster / 2);
|
||||
|
||||
uint32_t fat_sector = fat_start_lba + (fat_offset / FAT12_SECTOR_SIZE);
|
||||
uint32_t ent_offset = fat_offset % FAT12_SECTOR_SIZE;
|
||||
|
||||
// Read the sector containing the FAT entry
|
||||
disk_read_sector(fat_sector, g_sector_buffer);
|
||||
|
||||
// Read 16 bits (2 bytes)
|
||||
// Note: If ent_offset == 511, the entry spans two sectors.
|
||||
// For simplicity in this snippet, we ignore that edge case (rare).
|
||||
// A robust kernel would check if(ent_offset == 511) and read next sector.
|
||||
|
||||
uint16_t val = *(uint16_t*)&g_sector_buffer[ent_offset];
|
||||
|
||||
if (current_cluster & 1) {
|
||||
return val >> 4; // Odd: High 12 bits
|
||||
} else {
|
||||
return val & 0x0FFF; // Even: Low 12 bits
|
||||
}
|
||||
}
|
||||
|
||||
file_t fat12_open(const char *filename) {
|
||||
file_t file = {0};
|
||||
char target_name[11];
|
||||
to_fat_name(filename, target_name);
|
||||
|
||||
// Search Root Directory
|
||||
for (uint32_t i = 0; i < root_dir_sectors; i++) {
|
||||
disk_read_sector(root_dir_lba + i, g_sector_buffer);
|
||||
|
||||
fat12_entry_t *entry = (fat12_entry_t*)g_sector_buffer;
|
||||
|
||||
// Check all 16 entries in this sector (512 / 32 = 16)
|
||||
for (int j = 0; j < 16; j++) {
|
||||
if (entry[j].filename[0] == 0x00) return file; // End of Dir
|
||||
|
||||
// Check if filename matches
|
||||
if (k_memcmp(entry[j].filename, target_name, 11) == 0) {
|
||||
// Found it!
|
||||
file.start_cluster = entry[j].low_cluster_num;
|
||||
file.size = entry[j].file_size;
|
||||
|
||||
// Initialize file cursor
|
||||
file.current_cluster = file.start_cluster;
|
||||
file.bytes_read = 0;
|
||||
return file;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Not found (file.start_cluster will be 0)
|
||||
return file;
|
||||
}
|
||||
|
||||
uint32_t fat12_read(file_t *file, uint8_t *buffer, uint32_t bytes_to_read) {
|
||||
if (file->start_cluster == 0) return 0; // File not open
|
||||
|
||||
uint32_t total_read = 0;
|
||||
|
||||
while (bytes_to_read > 0) {
|
||||
// Check for EOF marker in FAT12 (>= 0xFF8)
|
||||
if (file->current_cluster >= 0xFF8) break;
|
||||
|
||||
// Calculate Physical LBA of current cluster
|
||||
// LBA = DataStart + ((Cluster - 2) * SectorsPerCluster)
|
||||
uint32_t lba = data_start_lba + ((file->current_cluster - 2) * bpb.sectors_per_cluster);
|
||||
|
||||
// Read the cluster
|
||||
// NOTE: Assumes SectorsPerCluster = 1 (Standard Floppy)
|
||||
disk_read_sector(lba, g_sector_buffer);
|
||||
|
||||
// Determine how much to copy from this sector
|
||||
uint32_t chunk_size = FAT12_SECTOR_SIZE;
|
||||
|
||||
// If the file is smaller than a sector, or we are at the end
|
||||
if (chunk_size > bytes_to_read) chunk_size = bytes_to_read;
|
||||
|
||||
// Check if we are reading past file size
|
||||
if (file->bytes_read + chunk_size > file->size) {
|
||||
chunk_size = file->size - file->bytes_read;
|
||||
}
|
||||
|
||||
// Copy to user buffer
|
||||
for (uint32_t i = 0; i < chunk_size; i++) {
|
||||
buffer[total_read + i] = g_sector_buffer[i];
|
||||
}
|
||||
|
||||
total_read += chunk_size;
|
||||
file->bytes_read += chunk_size;
|
||||
bytes_to_read -= chunk_size;
|
||||
|
||||
// If we finished this cluster, move to the next one
|
||||
if (chunk_size == FAT12_SECTOR_SIZE) { // Or strictly logic based on position
|
||||
file->current_cluster = fat12_get_next_cluster(file->current_cluster);
|
||||
} else {
|
||||
// We finished the file or the request
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return total_read;
|
||||
}
|
||||
|
||||
@@ -1,47 +1,67 @@
|
||||
#ifndef FAT12_H
|
||||
#define FAT12_H
|
||||
|
||||
#include <stdint.h> /* Include standard integer types */
|
||||
#include <stdio.h> /* Include standard I/O library */
|
||||
#include <stdlib.h> /* Include standard library */
|
||||
#include <stdint.h>
|
||||
|
||||
#define FAT12_SECTOR_SIZE 512 /* Sector size for FAT12 */
|
||||
#define FAT12_MAX_FILES 128 /* Maximum number of files in root directory */
|
||||
#define FAT12_ROOT_DIR_SECTORS 1 /* Number of sectors for root directory */
|
||||
// --- Configuration ---
|
||||
#define FAT12_SECTOR_SIZE 512
|
||||
|
||||
// --- On-Disk Structures (Must be Packed) ---
|
||||
|
||||
// BIOS Parameter Block (Start of Boot Sector)
|
||||
typedef struct {
|
||||
uint8_t jump[3]; /* Jump instruction for boot */
|
||||
char oem[8]; /* OEM name */
|
||||
uint16_t bytes_per_sector; /* Bytes per sector */
|
||||
uint8_t sectors_per_cluster; /* Sectors per cluster */
|
||||
uint16_t reserved_sectors; /* Reserved sectors count */
|
||||
uint8_t num_fats; /* Number of FATs */
|
||||
uint16_t max_root_dir_entries; /* Max entries in root directory */
|
||||
uint16_t total_sectors; /* Total sectors */
|
||||
uint8_t media_descriptor; /* Media descriptor */
|
||||
uint16_t fat_size; /* Size of each FAT */
|
||||
uint16_t sectors_per_track; /* Sectors per track */
|
||||
uint16_t num_heads; /* Number of heads */
|
||||
uint32_t hidden_sectors; /* Hidden sectors count */
|
||||
uint32_t total_sectors_large; /* Total sectors for large disks */
|
||||
} __attribute__((packed)) FAT12_BootSector; /* Packed structure for boot sector */
|
||||
uint8_t jump[3];
|
||||
char oem[8];
|
||||
uint16_t bytes_per_sector; // 512
|
||||
uint8_t sectors_per_cluster; // 1
|
||||
uint16_t reserved_sectors; // 1 (Boot sector)
|
||||
uint8_t fat_count; // 2
|
||||
uint16_t dir_entries_count; // 224
|
||||
uint16_t total_sectors; // 2880
|
||||
uint8_t media_descriptor; // 0xF0
|
||||
uint16_t sectors_per_fat; // 9
|
||||
uint16_t sectors_per_track; // 18
|
||||
uint16_t heads; // 2
|
||||
uint32_t hidden_sectors;
|
||||
uint32_t total_sectors_large;
|
||||
} __attribute__((packed)) fat12_bpb_t;
|
||||
|
||||
// Directory Entry (32 bytes)
|
||||
typedef struct {
|
||||
char name[11]; /* File name (8.3 format) */
|
||||
uint8_t attr; /* File attributes */
|
||||
uint16_t reserved; /* Reserved */
|
||||
uint16_t time; /* Time of last write */
|
||||
uint16_t date; /* Date of last write */
|
||||
uint16_t start_cluster; /* Starting cluster number */
|
||||
uint32_t file_size; /* File size in bytes */
|
||||
} __attribute__((packed)) FAT12_DirEntry; /* Directory entry structure */
|
||||
char filename[8];
|
||||
char ext[3];
|
||||
uint8_t attributes;
|
||||
uint8_t reserved;
|
||||
uint8_t creation_ms;
|
||||
uint16_t creation_time;
|
||||
uint16_t creation_date;
|
||||
uint16_t last_access_date;
|
||||
uint16_t high_cluster_num; // Always 0 in FAT12
|
||||
uint16_t last_mod_time;
|
||||
uint16_t last_mod_date;
|
||||
uint16_t low_cluster_num; // The starting cluster
|
||||
uint32_t file_size; // Size in bytes
|
||||
} __attribute__((packed)) fat12_entry_t;
|
||||
|
||||
void initialize_fat12(const char *disk_image); /* Function to initialize FAT12 */
|
||||
void read_fat12(const char *disk_image); /* Function to read FAT12 */
|
||||
void write_fat12(const char *disk_image); /* Function to write FAT12 */
|
||||
void list_files(const char *disk_image); /* Function to list files in root directory */
|
||||
void read_file(const char *disk_image, const char *filename); /* Function to read a file */
|
||||
void write_file(const char *disk_image, const char *filename, const uint8_t *data, size_t size); /* Function to write a file */
|
||||
// --- Kernel File Handle ---
|
||||
// This is what your kernel uses to track an open file
|
||||
typedef struct {
|
||||
char name[11];
|
||||
uint32_t size;
|
||||
uint16_t start_cluster;
|
||||
uint16_t current_cluster;
|
||||
uint32_t current_sector_in_cluster;
|
||||
uint32_t bytes_read;
|
||||
} file_t;
|
||||
|
||||
#endif
|
||||
/* FAT12_H */
|
||||
// --- Public API ---
|
||||
|
||||
// You must implement this in your disk driver (e.g., floppy.c)
|
||||
// Returns 0 on success, non-zero on error.
|
||||
extern int disk_read_sector(uint32_t lba, uint8_t *buffer);
|
||||
|
||||
void fat12_init();
|
||||
file_t fat12_open(const char *filename);
|
||||
uint32_t fat12_read(file_t *file, uint8_t *buffer, uint32_t bytes_to_read);
|
||||
|
||||
#endif // FAT12_H
|
||||
|
||||
20
kernel/io.h
20
kernel/io.h
@@ -13,4 +13,24 @@ static inline uint8_t inb(uint16_t port) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void outw(uint16_t port, uint16_t val) {
|
||||
__asm__("outw %0, %1" : : "a"(val), "Nd"(port));
|
||||
}
|
||||
|
||||
static inline uint16_t inw(uint16_t port) {
|
||||
uint16_t ret;
|
||||
__asm__("inw %1, %0" : "=a"(ret) : "Nd"(port));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void outl(uint16_t port, uint32_t val) {
|
||||
__asm__("outl %0, %1" : : "a"(val), "Nd"(port));
|
||||
}
|
||||
|
||||
static inline uint32_t inl(uint16_t port) {
|
||||
uint32_t ret;
|
||||
__asm__("inl %1, %0" : "=a"(ret) : "Nd"(port));
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
24
kernel/irq.c
24
kernel/irq.c
@@ -1,3 +1,4 @@
|
||||
#include "idt.h"
|
||||
#include "irq.h"
|
||||
#include "io.h"
|
||||
#include "isr.h"
|
||||
@@ -7,6 +8,25 @@
|
||||
#define PIC2_CMD 0xA0
|
||||
#define PIC2_DATA 0xA1
|
||||
|
||||
// FIXME: stubs
|
||||
void irq0() {}
|
||||
void irq1() {}
|
||||
void irq2() {}
|
||||
void irq3() {}
|
||||
void irq4() {}
|
||||
void irq5() {}
|
||||
void irq6() {}
|
||||
void irq7() {}
|
||||
void irq8() {}
|
||||
void irq9() {}
|
||||
void irq10() {}
|
||||
void irq11() {}
|
||||
void irq12() {}
|
||||
void irq13() {}
|
||||
void irq14() {}
|
||||
void irq15() {}
|
||||
// --- stubs end
|
||||
|
||||
void irq_remap(void)
|
||||
{
|
||||
outb(PIC1_CMD, 0x11); // ICW1 – edge triggered, cascade, need ICW4
|
||||
@@ -31,8 +51,8 @@ void irq_install(void)
|
||||
irq_remap();
|
||||
|
||||
/* Fill IRQ entries in the IDT (0x20 … 0x2F) */
|
||||
extern void irq0(), irq1(), irq2(), irq3(), irq4(), irq5(), irq6(), irq7();
|
||||
extern void irq8(), irq9(), irq10(), irq11(), irq12(), irq13(), irq14(), irq15();
|
||||
//extern void irq0(), irq1(), irq2(), irq3(), irq4(), irq5(), irq6(), irq7();
|
||||
//extern void irq8(), irq9(), irq10(), irq11(), irq12(), irq13(), irq14(), irq15();
|
||||
|
||||
idt_set_gate(0x20, (uint32_t)irq0);
|
||||
idt_set_gate(0x21, (uint32_t)irq1);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef IRQ_H
|
||||
#define IRQ_H
|
||||
|
||||
#include "types.h"
|
||||
|
||||
void irq_remap(void);
|
||||
void irq_install(void);
|
||||
void irq_handler(uint32_t int_num);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "io.h"
|
||||
#include "print.h"
|
||||
|
||||
static isr_callback_t interrupt_handlers[MAX_INTERRUPTS] = { 0 };
|
||||
isr_callback_t interrupt_handlers[MAX_INTERRUPTS] = { 0 };
|
||||
|
||||
void isr_handler(uint32_t int_num, uint32_t err_code) {
|
||||
terminal_write("Interrupt occurred: ");
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define MAX_INTERRUPTS 256
|
||||
|
||||
typedef void (*isr_callback_t)(void);
|
||||
extern isr_callback_t interrupt_handlers[MAX_INTERRUPTS];
|
||||
|
||||
void isr_handler(uint32_t int_num, uint32_t err_code);
|
||||
void register_interrupt_handler(uint8_t n, isr_callback_t handler);
|
||||
|
||||
@@ -11,7 +11,7 @@ extern "C" {
|
||||
/* C11 / POSIX-2004 signatures */
|
||||
void *memcpy(void *restrict dst, const void *restrict src, size_t n);
|
||||
void *memmove(void *dst, const void *src, size_t n);
|
||||
/*int memcmp(const void *s1, const void *s2, size_t n); */
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
|
||||
/* Optional fast-path using 32-bit loads (x86 only) */
|
||||
#if defined(__i386__) && !defined(MEMORY_NO_OPT)
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
#include "scheduler.h"
|
||||
#include <stddef.h>
|
||||
|
||||
// Defined in context_switch.s
|
||||
extern void ctx_switch(uint32_t **old_sp_ptr, uint32_t *new_sp);
|
||||
|
||||
static task_t tasks[MAX_TASKS];
|
||||
|
||||
// Stack memory area. Note: x86 Stacks grow DOWN from high to low addresses.
|
||||
static uint32_t task_stacks[MAX_TASKS][STACK_SIZE / sizeof(uint32_t)];
|
||||
|
||||
static int task_count = 0;
|
||||
@@ -9,7 +14,6 @@ static task_t *task_list = NULL;
|
||||
static task_t *current_task = NULL;
|
||||
|
||||
void scheduler_init() {
|
||||
// Initialize task list, etc.
|
||||
task_list = NULL;
|
||||
current_task = NULL;
|
||||
task_count = 0;
|
||||
@@ -20,16 +24,42 @@ void scheduler_add_task(void (*entry)(void)) {
|
||||
|
||||
task_t *new_task = &tasks[task_count];
|
||||
new_task->id = task_count;
|
||||
new_task->entry = entry;
|
||||
|
||||
// Simulate a stack pointer pointing to the "top" of the stack
|
||||
new_task->stack_ptr = &task_stacks[task_count][STACK_SIZE / sizeof(uint32_t) - 1];
|
||||
// 1. Calculate the top of the stack (High Address)
|
||||
// We point to the very end of the array.
|
||||
uint32_t *sp = &task_stacks[task_count][STACK_SIZE / sizeof(uint32_t)];
|
||||
|
||||
// 2. "Forge" the stack frame to look like ctx_switch saved it.
|
||||
// We push values onto the stack by decrementing the pointer and writing.
|
||||
|
||||
// --- Return Address (EIP) ---
|
||||
sp--;
|
||||
*sp = (uint32_t)entry; // When ctx_switch does 'ret', it pops this and jumps to 'entry'
|
||||
|
||||
// --- EFLAGS ---
|
||||
sp--;
|
||||
*sp = 0x00000202; // Reserved bit set, Interrupts Enabled (IF=1). Important!
|
||||
|
||||
// --- General Purpose Registers (PUSHA/POPA layout) ---
|
||||
// Order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI
|
||||
// We initialize them to 0 or meaningful values.
|
||||
sp--; *sp = 0; // EAX
|
||||
sp--; *sp = 0; // ECX
|
||||
sp--; *sp = 0; // EDX
|
||||
sp--; *sp = 0; // EBX
|
||||
sp--; *sp = 0; // ESP (Ignored by POPA)
|
||||
sp--; *sp = 0; // EBP
|
||||
sp--; *sp = 0; // ESI
|
||||
sp--; *sp = 0; // EDI
|
||||
|
||||
// Save this final stack location to the TCB
|
||||
new_task->stack_ptr = sp;
|
||||
new_task->next = NULL;
|
||||
|
||||
// Add to task list
|
||||
// 3. Add to linked list
|
||||
if (task_list == NULL) {
|
||||
task_list = new_task;
|
||||
current_task = new_task; // Make sure we have a current task to start
|
||||
} else {
|
||||
task_t *tail = task_list;
|
||||
while (tail->next) {
|
||||
@@ -42,21 +72,25 @@ void scheduler_add_task(void (*entry)(void)) {
|
||||
}
|
||||
|
||||
void scheduler_schedule() {
|
||||
// Very basic round-robin switch
|
||||
if (current_task && current_task->next) {
|
||||
if (!current_task) return;
|
||||
|
||||
task_t *prev = current_task;
|
||||
|
||||
// Round-robin logic
|
||||
if (current_task->next) {
|
||||
current_task = current_task->next;
|
||||
} else {
|
||||
current_task = task_list; // Loop back
|
||||
current_task = task_list;
|
||||
}
|
||||
|
||||
// Call context switch or simulate yielding to current_task
|
||||
// In real system: context_switch_to(current_task)
|
||||
if (current_task && current_task->entry) {
|
||||
current_task->entry(); // Simulate switching by calling
|
||||
// Perform the ACTUAL context switch
|
||||
// We pass the address of the previous task's stack pointer storage
|
||||
// and the value of the new task's stack pointer.
|
||||
if (prev != current_task) {
|
||||
ctx_switch(&prev->stack_ptr, current_task->stack_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
void scheduler_yield() {
|
||||
// Stub: manually call schedule for cooperative multitasking
|
||||
scheduler_schedule();
|
||||
}
|
||||
|
||||
@@ -4,18 +4,21 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#define MAX_TASKS 8
|
||||
#define STACK_SIZE 1024
|
||||
#define STACK_SIZE 1024 // in bytes
|
||||
|
||||
typedef struct task {
|
||||
uint32_t id;
|
||||
void (*entry)(void);
|
||||
|
||||
// The most important field:
|
||||
// Where was the stack pointer when we last left this task?
|
||||
uint32_t *stack_ptr;
|
||||
|
||||
struct task *next;
|
||||
} task_t;
|
||||
|
||||
void scheduler_init();
|
||||
void scheduler_add_task(void (*entry)(void));
|
||||
void scheduler_schedule();
|
||||
void scheduler_yield(); // Optional for cooperative scheduling
|
||||
void scheduler_yield();
|
||||
|
||||
#endif // SCHEDULER_H
|
||||
|
||||
@@ -77,16 +77,6 @@ char* utoa(unsigned int value, char* str, int base) {
|
||||
return str;
|
||||
}
|
||||
|
||||
int memcmp(const void *ptr1, const void *ptr2, size_t num) {
|
||||
const uint8_t *p1 = ptr1, *p2 = ptr2;
|
||||
for (size_t i = 0; i < num; i++) {
|
||||
if (p1[i] != p2[i]) {
|
||||
return p1[i] < p2[i] ? -1 : 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *memset(void *dest, int value, size_t len) {
|
||||
unsigned char *ptr = (unsigned char *)dest;
|
||||
while (len-- > 0)
|
||||
|
||||
@@ -9,7 +9,6 @@ char* itoa(int value, char* str, int base);
|
||||
// Convert unsigned integer to string (base is typically 10, 16, etc.)
|
||||
char* utoa(unsigned int value, char* str, int base);
|
||||
|
||||
int memcmp(const void *ptr1, const void *ptr2, size_t num);
|
||||
void *memset(void *dest, int value, size_t len);
|
||||
|
||||
#endif // UTILS_H
|
||||
|
||||
Reference in New Issue
Block a user