]> git.dujemihanovic.xyz Git - nameless-os.git/blobdiff - kernel/kernel.c
(VERY BROKEN) Enable paging
[nameless-os.git] / kernel / kernel.c
index 7ac2ad72d79127aa2c942ca62a3561b3a8da9cb6..b274340cd2605ca7a905579cf06f0b32adbf8d99 100644 (file)
@@ -1,17 +1,65 @@
 #include <tty.h>
+#include <io.h>
+#include <irq/idt.h>
+#include <irq/i8259a.h>
+#include <stdint.h>
+#include <mm/paging.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";
+extern void double_fault(struct abort_frame *frame);
+extern void keyb_handler(struct interrupt_frame *frame);
+extern void pf_handler(struct fault_frame *frame);
+static struct idt_descriptor idt[256] __attribute__((aligned(0x10)));
+static struct idtr idtr __attribute__((aligned(0x10)));
 
-void _start(void)
+static struct page_directory_entry page_dir[1024] __attribute__((aligned(0x1000)));
+static struct page_table_entry page_table[1024] __attribute__((aligned(0x1000)));
+
+void kmain(void)
 {
        screen_clear();
-       kprint(string);
+       kprint("Welcome to Nameless OS!\nRunning revision: ", 0);
+       kprint(GIT_COMMIT, 0);
+       kprint("\nEnabling paging...\n", 0);
+       for (int i=0xb8; i < 0xc0; i++) {
+               page_table[i].p = 1;
+               page_table[i].rw = 0;
+               page_table[i].us = 0;
+               page_table[i].page_frame_addr = i;
+       }
+       for (int i=0x100; i < 0x102; i++) {
+               page_table[i].p = 1;
+               page_table[i].rw = 0;
+               page_table[i].us = 0;
+               page_table[i].page_frame_addr = i;
+       }
+       for (int i=0x102; i < 0x110; i++) {
+               page_table[i].p = 1;
+               page_table[i].rw = 1;
+               page_table[i].us = 0;
+               page_table[i].page_frame_addr = i;
+       }
+       page_dir[0].p = 1;
+       page_dir[0].page_table_addr = (int) &page_table >> 12;
+       enable_paging(page_dir);
+       kprint("Preparing IDT...\n", 0);
+       idt_set_descriptor(idt, 8, 0x8, (uint32_t) double_fault, IDT_INTERRUPT_GATE, 0x0);
+       idt_set_descriptor(idt, 14, 0x8, (uint32_t) pf_handler, IDT_INTERRUPT_GATE, 0x0);
+       idt_set_descriptor(idt, 33, 0x8, (uint32_t) keyb_handler, IDT_INTERRUPT_GATE, 0x0);
+       kprint("IDT prepared, loading...\n", 0);
+       populate_idtr(&idtr, idt);
+       load_idt(idtr);
+       kprint("IDT loaded, enabling interrupts...\n", 0);
+       pic_init(0x20, 0x28);
+       pic_mask_all();
+       pic_unmask(1);
+       asm volatile ("sti");
+       kprint("All done\n", 0);
+       struct idtr curr_idtr;
+       asm ("sidt %0": "=m" (curr_idtr));
+       kprintw(curr_idtr.limit);
+       kprintd(curr_idtr.base);
+       //kprint("Gonna force a double fault\n", 0);
+       //int test = 1/0;
+       //int test2 = *(int *) (0xa0000000);
+       while(1);
 }