]> git.dujemihanovic.xyz Git - nameless-os.git/blob - kernel/kernel.c
c82bd4d7bc085d4f36ed4dbad94df65dbf304250
[nameless-os.git] / kernel / kernel.c
1 #include <stdint.h>
2 #include <tty.h>
3 #include <io.h>
4 #include <irq/idt.h>
5 #include <irq/interrupt.h>
6 #include <irq/i8259a.h>
7 #include <input/ps2.h>
8
9 struct idt_descriptor idt[256] __attribute__((aligned(0x10)));
10 struct idtr idtr __attribute__((aligned(0x10)));
11 extern void (*_int_handler_table[48])(void);
12
13 /* Small handler for double faults. Will be put elsewhere eventually. */
14 int double_fault_handler(struct interrupt_frame *frame)
15 {
16 kprint("PANIC: Double fault occurred!\n", VGA_COLOR_BRIGHT_RED);
17 kprint("EAX: ", VGA_COLOR_BRIGHT_RED);
18 kprintd(frame->eax);
19 kprint("\n", VGA_COLOR_BRIGHT_RED);
20 kprint("EBX: ", VGA_COLOR_BRIGHT_RED);
21 kprintd(frame->ebx);
22 kprint("\n", VGA_COLOR_BRIGHT_RED);
23 kprint("ECX: ", VGA_COLOR_BRIGHT_RED);
24 kprintd(frame->ecx);
25 kprint("\n", VGA_COLOR_BRIGHT_RED);
26 kprint("EDX: ", VGA_COLOR_BRIGHT_RED);
27 kprintd(frame->edx);
28 kprint("\n", VGA_COLOR_BRIGHT_RED);
29 kprint("EBP: ", VGA_COLOR_BRIGHT_RED);
30 kprintd(frame->ebp);
31 kprint("\n", VGA_COLOR_BRIGHT_RED);
32 kprint("ESP: ", VGA_COLOR_BRIGHT_RED);
33 kprintd(frame->esp);
34 kprint("\n", VGA_COLOR_BRIGHT_RED);
35 kprint("ESI: ", VGA_COLOR_BRIGHT_RED);
36 kprintd(frame->esi);
37 kprint("\n", VGA_COLOR_BRIGHT_RED);
38 kprint("EDI: ", VGA_COLOR_BRIGHT_RED);
39 kprintd(frame->edi);
40 kprint("\n", VGA_COLOR_BRIGHT_RED);
41 kprint("EIP: ", VGA_COLOR_BRIGHT_RED);
42 kprintd(frame->eip);
43 kprint("\n", VGA_COLOR_BRIGHT_RED);
44 kprint("EFLAGS: ", VGA_COLOR_BRIGHT_RED);
45 kprintd(frame->eflags);
46 kprint("\n", VGA_COLOR_BRIGHT_RED);
47
48 /* IF has already been cleared for us */
49 halt:
50 asm("hlt");
51 goto halt;
52 }
53
54 void kmain(void)
55 {
56 int ps2_success;
57 screen_clear();
58 kprint("Welcome to Nameless OS!\nRunning revision: ", 0);
59 kprint(GIT_COMMIT, 0);
60 kprint("\n", 0);
61 kprint("Preparing IDT...\n", 0);
62 for (int i=0; i<48; i++) {
63 idt_set_descriptor(idt, i, 0x8, _int_handler_table[i], IDT_INTERRUPT_GATE, 0);
64 }
65 kprint("IDT prepared, loading...\n", 0);
66 populate_idtr(&idtr, idt);
67 load_idt(idtr);
68 kprint("Setting up interrupts...\n", 0);
69 register_interrupt(8, &double_fault_handler);
70 pic_init(0x20, 0x28);
71 pic_mask_all();
72 ps2_success = ps2_initialize();
73 switch (ps2_success) {
74 case -1:
75 kprint("No PS/2 devices found or working, we will have no input\n", 0);
76 break;
77 case 0:
78 kprint("Found one working PS/2 device\n", 0);
79 break;
80 case 1:
81 kprint("Found two working PS/2 devices\n", 0);
82 }
83 asm volatile ("sti");
84 kprint("All done\n", 0);
85 while(1);
86 }