]> git.dujemihanovic.xyz Git - nameless-os.git/blobdiff - kernel/kernel.c
Use interrupt frame and new double fault handler
[nameless-os.git] / kernel / kernel.c
index 7b6d3543c39066879672e1b44acfc357b8a9f2d9..c82bd4d7bc085d4f36ed4dbad94df65dbf304250 100644 (file)
@@ -1,39 +1,86 @@
+#include <stdint.h>
 #include <tty.h>
 #include <io.h>
-#include <stdint.h>
+#include <irq/idt.h>
+#include <irq/interrupt.h>
+#include <irq/i8259a.h>
+#include <input/ps2.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)
+/* Small handler for double faults. Will be put elsewhere eventually. */
+int double_fault_handler(struct interrupt_frame *frame)
 {
+       kprint("PANIC: Double fault occurred!\n", VGA_COLOR_BRIGHT_RED);
+       kprint("EAX: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->eax);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+       kprint("EBX: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->ebx);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+       kprint("ECX: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->ecx);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+       kprint("EDX: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->edx);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+       kprint("EBP: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->ebp);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+       kprint("ESP: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->esp);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+       kprint("ESI: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->esi);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+       kprint("EDI: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->edi);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+       kprint("EIP: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->eip);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+       kprint("EFLAGS: ", VGA_COLOR_BRIGHT_RED);
+       kprintd(frame->eflags);
+       kprint("\n", VGA_COLOR_BRIGHT_RED);
+
+       /* IF has already been cleared for us */
+halt:
+       asm("hlt");
+       goto halt;
+}
+
+void kmain(void)
+{
+       int ps2_success;
        screen_clear();
-       /*kprint(string, 0x07);*/
-       kprint("Color 0x01\n", VGA_COLOR_BLUE);
-       kprint("Color 0x02\n", VGA_COLOR_GREEN);
-       kprint("Color 0x03\n", VGA_COLOR_TEAL);
-       kprint("Color 0x04\n", VGA_COLOR_DARK_RED);
-       kprint("Color 0x05\n", VGA_COLOR_MAGENTA);
-       kprint("Color 0x06\n", VGA_COLOR_BROWN);
-       kprint("Color 0x07\n", VGA_COLOR_LIGHT_GRAY);
-       kprint("Color 0x08\n", VGA_COLOR_DARK_GRAY);
-       kprint("Color 0x09\n", VGA_COLOR_PURPLE);
-       kprint("Color 0x0A\n", VGA_COLOR_LIME);
-       kprint("Color 0x0B\n", VGA_COLOR_CYAN);
-       kprint("Color 0x0C\n", VGA_COLOR_BRIGHT_RED);
-       kprint("Color 0x0D\n", VGA_COLOR_PINK);
-       kprint("Color 0x0E\n", VGA_COLOR_YELLOW);
-       kprint("Color 0x0F\n", VGA_COLOR_WHITE);
-       kprintb(0xAE);
-       kprint("\n", 0);
-       kprintw(0xDEAD);
+       kprint("Welcome to Nameless OS!\nRunning revision: ", 0);
+       kprint(GIT_COMMIT, 0);
        kprint("\n", 0);
-       kprintd(0x89ABCDEF);
+       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();
+       ps2_success = ps2_initialize();
+       switch (ps2_success) {
+               case -1:
+                       kprint("No PS/2 devices found or working, we will have no input\n", 0);
+                       break;
+               case 0:
+                       kprint("Found one working PS/2 device\n", 0);
+                       break;
+               case 1:
+                       kprint("Found two working PS/2 devices\n", 0);
+       }
+       asm volatile ("sti");
+       kprint("All done\n", 0);
+       while(1);
 }