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:
/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
-

View File

@ -1 +0,0 @@
empty

View File

@ -1,22 +1,16 @@
#include "gdt.h"
#include "isr.h"
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
// 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);

View File

@ -1,22 +1,15 @@
#include "idt.h"
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 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,10 +25,11 @@ 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;
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;
@ -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));
}

View File

@ -4,7 +4,8 @@
#include <stdint.h>
// IDT entry structure
struct idt_entry {
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
@ -13,9 +14,10 @@ struct idt_entry {
} __attribute__((packed));
// IDT pointer structure
struct idt_ptr {
struct idt_ptr
{
uint16_t limit; // Size of IDT in bytes - 1
uint32_t base; // Address of IDT
struct idt_entry *base; // Address of IDT
} __attribute__((packed));
// Exception handlers
@ -33,4 +35,4 @@ void device();
// Initialize the IDT
void idt_init();
#endif
#endif /* IDT_H */

View File

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

View File

@ -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;