From 79a27d232c1dad711f7daaa67079923987ef0917 Mon Sep 17 00:00:00 2001 From: Greg Bowne Date: Mon, 17 Jul 2023 23:30:44 -0700 Subject: [PATCH] Attempting to fix gdt, isr, idt and memory.c. --- build/CMakeFiles/ClassicOS.dir/C.includecache | 32 +++++++------- build/CMakeFiles/Progress/2 | 1 - build/CMakeFiles/Progress/{1 => 4} | 0 src/kernel/arch/x86/gdt.c | 13 ++---- src/kernel/arch/x86/idt.c | 42 ++++++++----------- src/kernel/arch/x86/idt.h | 22 +++++----- src/kernel/arch/x86/isr.c | 21 +++++++--- src/kernel/arch/x86/memory/memory.c | 7 +++- 8 files changed, 72 insertions(+), 66 deletions(-) delete mode 100644 build/CMakeFiles/Progress/2 rename build/CMakeFiles/Progress/{1 => 4} (100%) diff --git a/build/CMakeFiles/ClassicOS.dir/C.includecache b/build/CMakeFiles/ClassicOS.dir/C.includecache index 70e4417..8162824 100644 --- a/build/CMakeFiles/ClassicOS.dir/C.includecache +++ b/build/CMakeFiles/ClassicOS.dir/C.includecache @@ -6,25 +6,29 @@ #IncludeRegexTransform: -/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/keyboard/keyboard.c -stdint.h -- +/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/screen/screen.c +screen.h +/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/screen/screen.h stdbool.h - stddef.h - - -/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/screen/screen.c -screen.h -/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/screen/screen.h -dos.h +stdint.h +- +stdio.h +- +stdlib.h - /media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/screen/screen.h - -/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/kernel/kernel.c -kernel.h -/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/kernel/kernel.h - -/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/kernel/kernel.h +stdbool.h +- +stddef.h +- +stdint.h +- +stdio.h +- +stdlib.h +- diff --git a/build/CMakeFiles/Progress/2 b/build/CMakeFiles/Progress/2 deleted file mode 100644 index 7b4d68d..0000000 --- a/build/CMakeFiles/Progress/2 +++ /dev/null @@ -1 +0,0 @@ -empty \ No newline at end of file diff --git a/build/CMakeFiles/Progress/1 b/build/CMakeFiles/Progress/4 similarity index 100% rename from build/CMakeFiles/Progress/1 rename to build/CMakeFiles/Progress/4 diff --git a/src/kernel/arch/x86/gdt.c b/src/kernel/arch/x86/gdt.c index 6c162cf..23d1bf5 100644 --- a/src/kernel/arch/x86/gdt.c +++ b/src/kernel/arch/x86/gdt.c @@ -1,22 +1,16 @@ #include "gdt.h" #include "isr.h" +#include #include #include #include #include #include #include -#include // GDT table struct gdt_entry *gdt; -// GDT pointer -struct gdt_ptr { - uint16_t limit; - void* base; -} gp; - // Initialize a GDT entry void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) @@ -30,13 +24,12 @@ void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access, gdt[num].access = access; } -// Initialize the GDT void gdt_init() { // Set up GDT pointer gp.limit = (sizeof(struct gdt_entry) * 3) - 1; - gdt = (struct gdt_entry*)malloc(sizeof(struct gdt_entry) * 3); - gp.base = gdt; + gdt = (struct gdt_entry *)malloc(sizeof(struct gdt_entry) * 3); + gp.base = (uint32_t)gdt; // Cast gdt to uint32_t // Clear GDT memset(gdt, 0, sizeof(struct gdt_entry) * 3); diff --git a/src/kernel/arch/x86/idt.c b/src/kernel/arch/x86/idt.c index 2b0ea44..c6b3fab 100644 --- a/src/kernel/arch/x86/idt.c +++ b/src/kernel/arch/x86/idt.c @@ -1,22 +1,15 @@ #include "idt.h" -#include - #include #include #include #include #include +#include // IDT table struct idt_entry idt[256]; // IDT pointer -struct idt_ptr -{ - uint16_t limit; // Size of IDT in bytes - 1 - struct idt_entry *base; // Address of IDT -} __attribute__((packed)); - struct idt_ptr idtp; // Exception handlers @@ -32,13 +25,14 @@ extern void keyboard_handler(); extern void device_handler(); // Initialize an IDT entry -void idt_set_gate(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags) +void idt_set_gate(uint8_t num, void *base, uint16_t sel, uint8_t flags) { - idt[num].base_lo = base & 0xFFFF; - idt[num].base_hi = (base >> 16) & 0xFFFF; - idt[num].sel = sel; - idt[num].always0 = 0; - idt[num].flags = flags; + uint32_t base_addr = (uint32_t)base; + idt[num].base_lo = base_addr & 0xFFFF; + idt[num].base_hi = (base_addr >> 16) & 0xFFFF; + idt[num].sel = sel; + idt[num].always0 = 0; + idt[num].flags = flags; } // Initialize the IDT @@ -46,23 +40,23 @@ void idt_init() { // Set up IDT pointer idtp.limit = sizeof(idt) - 1; - idtp.base = idt; + idtp.base = &idt[0]; // Clear IDT memset(&idt, 0, sizeof(idt)); // Set up exception handlers - idt_set_gate(0, (uint32_t)divide_error_handler, 0x08, 0x8E); - idt_set_gate(14, (uint32_t)page_fault_handler, 0x08, 0x8E); - idt_set_gate(13, (uint32_t)general_protection_fault_handler, 0x08, 0x8E); - idt_set_gate(8, (uint32_t)double_fault_handler, 0x08, 0x8E); + idt_set_gate(0, divide_error_handler, 0x08, 0x8E); + idt_set_gate(14, page_fault_handler, 0x08, 0x8E); + idt_set_gate(13, general_protection_fault_handler, 0x08, 0x8E); + idt_set_gate(8, double_fault_handler, 0x08, 0x8E); // Set up interrupt handlers - idt_set_gate(0x80, (uint32_t)system_call_handler, 0x08, 0xEE); - idt_set_gate(0x20, (uint32_t)timer_handler, 0x08, 0x8E); - idt_set_gate(0x21, (uint32_t)keyboard_handler, 0x08, 0x8E); - idt_set_gate(0x30, (uint32_t)device_handler, 0x08, 0x8E); + idt_set_gate(0x80, system_call_handler, 0x08, 0xEE); + idt_set_gate(0x20, timer_handler, 0x08, 0x8E); + idt_set_gate(0x21, keyboard_handler, 0x08, 0x8E); + idt_set_gate(0x30, device_handler, 0x08, 0x8E); // Load IDT - _asm volatile("lidt %0" : : "m"(idtp)); + __asm__ volatile("lidt %0" : : "m"(idtp)); } \ No newline at end of file diff --git a/src/kernel/arch/x86/idt.h b/src/kernel/arch/x86/idt.h index b29e14f..ebc16d4 100644 --- a/src/kernel/arch/x86/idt.h +++ b/src/kernel/arch/x86/idt.h @@ -4,18 +4,20 @@ #include // IDT entry structure -struct idt_entry { - uint16_t base_lo; // Lower 16 bits of handler function address - uint16_t sel; // Kernel segment selector - uint8_t always0; // Always 0 - uint8_t flags; // Flags - uint16_t base_hi; // Upper 16 bits of handler function address +struct idt_entry +{ + uint16_t base_lo; // Lower 16 bits of handler function address + uint16_t sel; // Kernel segment selector + uint8_t always0; // Always 0 + uint8_t flags; // Flags + uint16_t base_hi; // Upper 16 bits of handler function address } __attribute__((packed)); // IDT pointer structure -struct idt_ptr { - uint16_t limit; // Size of IDT in bytes - 1 - uint32_t base; // Address of IDT +struct idt_ptr +{ + uint16_t limit; // Size of IDT in bytes - 1 + struct idt_entry *base; // Address of IDT } __attribute__((packed)); // Exception handlers @@ -33,4 +35,4 @@ void device(); // Initialize the IDT void idt_init(); -#endif \ No newline at end of file +#endif /* IDT_H */ \ No newline at end of file diff --git a/src/kernel/arch/x86/isr.c b/src/kernel/arch/x86/isr.c index f589594..65aeede 100644 --- a/src/kernel/arch/x86/isr.c +++ b/src/kernel/arch/x86/isr.c @@ -1,4 +1,6 @@ #include "isr.h" +#include "idt.h" +#include // ISR table void (*isr_table[256])(struct isr_regs *regs); @@ -12,19 +14,26 @@ void isr_register(uint8_t num, void (*handler)(struct isr_regs *regs)) // ISR handler void isr_handler(struct isr_regs *regs) { - void (*handler)(struct isr_regs *regs); - - handler = isr_table[regs->int_no]; - if (handler) + if (regs->int_no == 0) { - handler(regs); + divide_error(); + } + else + { + void (*handler)(struct isr_regs *regs); + + handler = isr_table[regs->int_no]; + if (handler) + { + handler(regs); + } } } // Exception handlers void divide_error() { - // Handle divide error exception + printf("Divide by zero error!\n"); } void page_fault() diff --git a/src/kernel/arch/x86/memory/memory.c b/src/kernel/arch/x86/memory/memory.c index f762a96..dbf2af9 100644 --- a/src/kernel/arch/x86/memory/memory.c +++ b/src/kernel/arch/x86/memory/memory.c @@ -46,7 +46,12 @@ void kfree(void *ptr) if ((uint8_t *)ptr >= heap_start && ptr < (void *)heap_end) { /* Zero out the memory */ - memset(ptr, 0, (uint8_t *)heap_pos - (uint8_t *)ptr); + volatile uint8_t *volatile_ptr = (volatile uint8_t *)ptr; + size_t size = (uint8_t *)heap_pos - (uint8_t *)ptr; + while (size--) + { + *volatile_ptr++ = 0; + } /* Free the memory by moving the heap position */ heap_pos = (uint8_t *)ptr;