hazel/kernel/kernel.c

59 lines
1.9 KiB
C
Raw Normal View History

2024-06-27 18:36:32 -04:00
#include <kernel/kernel.h>
2024-06-25 22:47:21 -04:00
#include <kernel/multiboot.h>
2024-07-02 18:44:36 -04:00
#include <kernel/mem.h>
2024-06-27 18:36:32 -04:00
#include <kernel/serial.h>
2024-07-01 19:28:45 -04:00
#include <kernel/idt.h>
#include <kernel/pic.h>
#include <kernel/pit.h>
2024-06-27 18:36:32 -04:00
kernel_ctx_t ctx = {0};
2024-07-01 19:28:45 -04:00
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);
}
2024-06-25 22:47:21 -04:00
2024-07-02 00:46:29 -04:00
void sleep(int delay) {
uint64_t end = ctx.ticks + delay;
while (ctx.ticks < end)
cpu_relax;
}
2024-06-25 22:47:21 -04:00
void kernel(multiboot_info_t *info) {
2024-06-30 21:27:33 -04:00
if (!CHECK_FLAG(info->flags, 6)) goto halt; // Memory map
if (!CHECK_FLAG(info->flags, 12)) goto halt; // VBE data
2024-07-02 18:44:36 -04:00
ctx.multi_mmap = (multi_mmap_t *)info->memmapaddress;
ctx.multi_mmap_size = info->memmaplength;
2024-06-27 18:36:32 -04:00
2024-06-30 21:27:33 -04:00
if (serial_port_init(COM1)) ctx.log_method = LOG_COM1;
2024-06-27 18:36:32 -04:00
LOG("Kernel log being sent to COM1\n");
2024-07-01 19:28:45 -04:00
2024-07-02 18:44:36 -04:00
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);
2024-07-01 19:28:45 -04:00
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" ::);
2024-07-02 00:46:29 -04:00
ctx.ticks = 0;
2024-07-01 19:28:45 -04:00
pit_init();
2024-07-02 00:46:29 -04:00
2024-06-27 18:36:32 -04:00
halt:
2024-06-25 15:28:44 -04:00
for (;;) {}
}