hazel/kernel/kernel.c
2024-07-02 18:44:36 -04:00

59 lines
1.9 KiB
C

#include <kernel/kernel.h>
#include <kernel/multiboot.h>
#include <kernel/mem.h>
#include <kernel/serial.h>
#include <kernel/idt.h>
#include <kernel/pic.h>
#include <kernel/pit.h>
kernel_ctx_t ctx = {0};
struct idt_entry idt[256] = {0};
struct idtr idtr = {0};
extern uint32_t isr_stub_table[32];
extern void pit_isr(void);
void exception_handler(int_stack_frame_t r) {
LOG("Exception 0x%X (0x%X) has occurred\nEAX: 0x%08X\nECX: 0x%08X\nEDX: 0x%08X\nEBX: 0x%08X\nESI: 0x%08X\nEDI: 0x%08X\nEBP: 0x%08X\nESP: 0x%08X\n",
r.int_no, r.err_code, r.eax, r.ecx, r.edx, r.ebx, r.esi, r.edi, r.ebp, r.esp+24);
LOG("\nEIP: 0x%08X\nCS: 0x%08X\nEFLAGS: 0x%08X\n",
r.eip, r.cs, r.eflags);
}
void sleep(int delay) {
uint64_t end = ctx.ticks + delay;
while (ctx.ticks < end)
cpu_relax;
}
void kernel(multiboot_info_t *info) {
if (!CHECK_FLAG(info->flags, 6)) goto halt; // Memory map
if (!CHECK_FLAG(info->flags, 12)) goto halt; // VBE data
ctx.multi_mmap = (multi_mmap_t *)info->memmapaddress;
ctx.multi_mmap_size = info->memmaplength;
if (serial_port_init(COM1)) ctx.log_method = LOG_COM1;
LOG("Kernel log being sent to COM1\n");
if (!mmap_init()) goto halt;
LOG("%d bytes of RAM detected\nCreated a %d byte large physical memory map at 0x%08X\n", ctx.mmap_size*BLOCK_SIZE*8, ctx.mmap_size, ctx.mmap);
for (int i = 0; i < 32; i++) {
idt_encode_entry(idt, i, isr_stub_table[i], GDT_SEGMENT_SELECTOR(1, 0), TRAP_GATE_32 | INT_RING0 | INT_PRESENT);
}
idt_encode_entry(idt, 32, (uint32_t)pit_isr, GDT_SEGMENT_SELECTOR(1, 0), TRAP_GATE_32 | INT_RING0 | INT_PRESENT);
idtr.size = (sizeof(struct idt_entry) * 256) - 1;
idtr.base = (uint32_t) idt;
asm volatile ("lidt %0" :: "m"(idtr));
pic_remap(PIC_1_START, PIC_2_START);
asm volatile ("sti" ::);
ctx.ticks = 0;
pit_init();
halt:
for (;;) {}
}