Attempting to fix gdt, isr, idt and memory.c.

This commit is contained in:
Gregory Kenneth Bowne 2023-07-17 23:30:44 -07:00
parent 4b75ea421d
commit 79a27d232c
8 changed files with 72 additions and 66 deletions

View File

@ -6,25 +6,29 @@
#IncludeRegexTransform: #IncludeRegexTransform:
/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/keyboard/keyboard.c /media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/screen/screen.c
stdint.h screen.h
- /media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/screen/screen.h
stdbool.h stdbool.h
- -
stddef.h stddef.h
- -
stdint.h
/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/screen/screen.c -
screen.h stdio.h
/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/drivers/screen/screen.h -
dos.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/drivers/screen/screen.h
stdbool.h
/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/kernel/kernel.c -
kernel.h stddef.h
/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/kernel/kernel.h -
stdint.h
/media/gbowne1/18656299-5992-400a-a7a4-b6ffc4b0612f/Documents/ClassicOS/src/kernel/kernel.h -
stdio.h
-
stdlib.h
-

View File

@ -1 +0,0 @@
empty

View File

@ -1,22 +1,16 @@
#include "gdt.h" #include "gdt.h"
#include "isr.h" #include "isr.h"
#include <inttypes.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <inttypes.h>
// GDT table // GDT table
struct gdt_entry *gdt; struct gdt_entry *gdt;
// GDT pointer
struct gdt_ptr {
uint16_t limit;
void* base;
} gp;
// Initialize a GDT entry // Initialize a GDT entry
void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access, void gdt_set_gate(uint32_t num, uint32_t base, uint32_t limit, uint8_t access,
uint8_t gran) 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; gdt[num].access = access;
} }
// Initialize the GDT
void gdt_init() void gdt_init()
{ {
// Set up GDT pointer // Set up GDT pointer
gp.limit = (sizeof(struct gdt_entry) * 3) - 1; gp.limit = (sizeof(struct gdt_entry) * 3) - 1;
gdt = (struct gdt_entry *)malloc(sizeof(struct gdt_entry) * 3); gdt = (struct gdt_entry *)malloc(sizeof(struct gdt_entry) * 3);
gp.base = gdt; gp.base = (uint32_t)gdt; // Cast gdt to uint32_t
// Clear GDT // Clear GDT
memset(gdt, 0, sizeof(struct gdt_entry) * 3); memset(gdt, 0, sizeof(struct gdt_entry) * 3);

View File

@ -1,22 +1,15 @@
#include "idt.h" #include "idt.h"
#include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
// IDT table // IDT table
struct idt_entry idt[256]; struct idt_entry idt[256];
// IDT pointer // 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; struct idt_ptr idtp;
// Exception handlers // Exception handlers
@ -32,10 +25,11 @@ extern void keyboard_handler();
extern void device_handler(); extern void device_handler();
// Initialize an IDT entry // 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; uint32_t base_addr = (uint32_t)base;
idt[num].base_hi = (base >> 16) & 0xFFFF; idt[num].base_lo = base_addr & 0xFFFF;
idt[num].base_hi = (base_addr >> 16) & 0xFFFF;
idt[num].sel = sel; idt[num].sel = sel;
idt[num].always0 = 0; idt[num].always0 = 0;
idt[num].flags = flags; idt[num].flags = flags;
@ -46,23 +40,23 @@ void idt_init()
{ {
// Set up IDT pointer // Set up IDT pointer
idtp.limit = sizeof(idt) - 1; idtp.limit = sizeof(idt) - 1;
idtp.base = idt; idtp.base = &idt[0];
// Clear IDT // Clear IDT
memset(&idt, 0, sizeof(idt)); memset(&idt, 0, sizeof(idt));
// Set up exception handlers // Set up exception handlers
idt_set_gate(0, (uint32_t)divide_error_handler, 0x08, 0x8E); idt_set_gate(0, divide_error_handler, 0x08, 0x8E);
idt_set_gate(14, (uint32_t)page_fault_handler, 0x08, 0x8E); idt_set_gate(14, page_fault_handler, 0x08, 0x8E);
idt_set_gate(13, (uint32_t)general_protection_fault_handler, 0x08, 0x8E); idt_set_gate(13, general_protection_fault_handler, 0x08, 0x8E);
idt_set_gate(8, (uint32_t)double_fault_handler, 0x08, 0x8E); idt_set_gate(8, double_fault_handler, 0x08, 0x8E);
// Set up interrupt handlers // Set up interrupt handlers
idt_set_gate(0x80, (uint32_t)system_call_handler, 0x08, 0xEE); idt_set_gate(0x80, system_call_handler, 0x08, 0xEE);
idt_set_gate(0x20, (uint32_t)timer_handler, 0x08, 0x8E); idt_set_gate(0x20, timer_handler, 0x08, 0x8E);
idt_set_gate(0x21, (uint32_t)keyboard_handler, 0x08, 0x8E); idt_set_gate(0x21, keyboard_handler, 0x08, 0x8E);
idt_set_gate(0x30, (uint32_t)device_handler, 0x08, 0x8E); idt_set_gate(0x30, device_handler, 0x08, 0x8E);
// Load IDT // Load IDT
_asm volatile("lidt %0" : : "m"(idtp)); __asm__ volatile("lidt %0" : : "m"(idtp));
} }

View File

@ -4,7 +4,8 @@
#include <stdint.h> #include <stdint.h>
// IDT entry structure // IDT entry structure
struct idt_entry { struct idt_entry
{
uint16_t base_lo; // Lower 16 bits of handler function address uint16_t base_lo; // Lower 16 bits of handler function address
uint16_t sel; // Kernel segment selector uint16_t sel; // Kernel segment selector
uint8_t always0; // Always 0 uint8_t always0; // Always 0
@ -13,9 +14,10 @@ struct idt_entry {
} __attribute__((packed)); } __attribute__((packed));
// IDT pointer structure // IDT pointer structure
struct idt_ptr { struct idt_ptr
{
uint16_t limit; // Size of IDT in bytes - 1 uint16_t limit; // Size of IDT in bytes - 1
uint32_t base; // Address of IDT struct idt_entry *base; // Address of IDT
} __attribute__((packed)); } __attribute__((packed));
// Exception handlers // Exception handlers
@ -33,4 +35,4 @@ void device();
// Initialize the IDT // Initialize the IDT
void idt_init(); void idt_init();
#endif #endif /* IDT_H */

View File

@ -1,4 +1,6 @@
#include "isr.h" #include "isr.h"
#include "idt.h"
#include <stdio.h>
// ISR table // ISR table
void (*isr_table[256])(struct isr_regs *regs); void (*isr_table[256])(struct isr_regs *regs);
@ -11,6 +13,12 @@ void isr_register(uint8_t num, void (*handler)(struct isr_regs *regs))
// ISR handler // ISR handler
void isr_handler(struct isr_regs *regs) void isr_handler(struct isr_regs *regs)
{
if (regs->int_no == 0)
{
divide_error();
}
else
{ {
void (*handler)(struct isr_regs *regs); void (*handler)(struct isr_regs *regs);
@ -20,11 +28,12 @@ void isr_handler(struct isr_regs *regs)
handler(regs); handler(regs);
} }
} }
}
// Exception handlers // Exception handlers
void divide_error() void divide_error()
{ {
// Handle divide error exception printf("Divide by zero error!\n");
} }
void page_fault() void page_fault()

View File

@ -46,7 +46,12 @@ void kfree(void *ptr)
if ((uint8_t *)ptr >= heap_start && ptr < (void *)heap_end) if ((uint8_t *)ptr >= heap_start && ptr < (void *)heap_end)
{ {
/* Zero out the memory */ /* 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 */ /* Free the memory by moving the heap position */
heap_pos = (uint8_t *)ptr; heap_pos = (uint8_t *)ptr;