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