]> git.dujemihanovic.xyz Git - nameless-os.git/blobdiff - kernel/kernel.c
Small changes to double fault handler
[nameless-os.git] / kernel / kernel.c
index 7ac2ad72d79127aa2c942ca62a3561b3a8da9cb6..adfdd3f58021c698a9cba474d7870680e5dbf696 100644 (file)
@@ -1,17 +1,97 @@
 #include <tty.h>
+#include <io.h>
+#include <irq/idt.h>
+#include <irq/interrupt.h>
+#include <irq/i8259a.h>
+#include <stdint.h>
+#include <mm/paging.h>
+#include <panic.h>
 
-const char *string = "Hello there!\n\n\
-Hopefully your machine manages to print this text.\n\
-If it did, that's great news because I managed to write a partial VGA driver.\n\n\
-Right now, the short-term roadmap is as follows:\n\n\
-* Complete the text-mode part of the VGA driver.\n\
-* Enable interrupts using the PIC.\n\
-* Write a driver for the Intel 8042 PS/2 controller so the OS can receive keystrokes.\n\
-* Once all that is done, it would be desirable to start the switch to userspace so the OS can actually be remotely usable.\n\n\
-Feel free to mess around with the code, although I doubt it will be very interesting at the moment.\n";
+struct idt_descriptor idt[256] __attribute__((aligned(0x10)));
+struct idtr idtr __attribute__((aligned(0x10)));
+extern void (*_int_handler_table[48])(void);
 
-void _start(void)
+struct e820_map {
+       uint64_t base;
+       uint64_t length;
+       uint32_t type;
+       uint32_t attrib;
+};
+
+void print_e820(struct e820_map *mmap, int mmap_size)
+{
+       kprint("       Base       |      Length      |   Type   |  Attrib  |\n", 0);
+       kprint("------------------------------------------------------------\n", 0);
+       for (int i=0; i<mmap_size; i++) {
+               kprintq(mmap[i].base);
+               kprintc('|', 0);
+               kprintq(mmap[i].length);
+               kprintc('|', 0);
+               kprintd(mmap[i].type);
+               kprintc('|', 0);
+               kprintd(mmap[i].attrib);
+               kprintc('|', 0);
+               kprintc('\n', 0);
+       }
+}
+
+/* Small handler for double faults. Will be put elsewhere eventually. */
+int double_fault_handler(struct interrupt_frame *frame)
+{
+       kprint("Double fault occurred!\n", VGA_COLOR_BRIGHT_RED);
+       kprint("EAX: ", 0);
+       kprintd(frame->eax);
+       kprintc(' ', 0);
+       kprint("EBX: ", 0);
+       kprintd(frame->ebx);
+       kprintc(' ', 0);
+       kprint("ECX: ", 0);
+       kprintd(frame->ecx);
+       kprintc(' ', 0);
+       kprint("EDX: ", 0);
+       kprintd(frame->edx);
+       kprintc('\n', 0);
+       kprint("EBP: ", 0);
+       kprintd(frame->ebp);
+       kprintc(' ', 0);
+       kprint("ESP: ", 0);
+       kprintd(frame->esp);
+       kprintc('\n', 0);
+       kprint("ESI: ", 0);
+       kprintd(frame->esi);
+       kprintc(' ', 0);
+       kprint("EDI: ", 0);
+       kprintd(frame->edi);
+       kprintc('\n', 0);
+       kprint("EIP: ", 0);
+       kprintd(frame->eip);
+       kprintc(' ', 0);
+       kprint("EFLAGS: ", 0);
+       kprintd(frame->eflags);
+       kprintc('\n', 0);
+       PANIC("double fault");
+}
+
+void kmain(struct e820_map *mmap, int mmap_size)
 {
        screen_clear();
-       kprint(string);
+       kprint("Welcome to Nameless OS!\nRunning revision: ", 0);
+       kprint(GIT_COMMIT, 0);
+       kprint("\n", 0);
+       kprint("BIOS memory map:\n", 0);
+       print_e820(mmap, mmap_size);
+       kprint("Preparing IDT...\n", 0);
+       for (int i=0; i<48; i++) {
+               idt_set_descriptor(idt, i, 0x8, _int_handler_table[i], IDT_INTERRUPT_GATE, 0);
+       }
+       kprint("IDT prepared, loading...\n", 0);
+       populate_idtr(&idtr, idt);
+       load_idt(idtr);
+       kprint("Setting up interrupts...\n", 0);
+       register_interrupt(8, &double_fault_handler);
+       pic_init(0x20, 0x28);
+       pic_mask_all();
+       asm volatile ("sti");
+       kprint("All done\n", 0);
+       while(1);
 }