swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 1 | #include "descriptor_tables.h" |
| 2 | #include "vga.h" |
swissChili | 9b3584b | 2021-02-18 13:57:27 -0800 | [diff] [blame^] | 3 | #include "io.h" |
| 4 | #include "pic.h" |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 5 | |
| 6 | extern void gdt_flush(uint gdt); |
| 7 | extern void idt_flush(uint idt); |
| 8 | |
| 9 | static void gdt_set_gate(uint i, uint base, uint limit, uchar access, uchar gran); |
| 10 | static void idt_set_gate(uchar num, uint base, ushort selector, uchar flags); |
| 11 | |
| 12 | struct gdt_entry gdt_entries[5]; |
| 13 | struct gdt_pointer gdt_pointer; |
| 14 | |
| 15 | struct idt_entry idt_entries[256]; |
| 16 | struct idt_pointer idt_pointer; |
| 17 | |
swissChili | 9b3584b | 2021-02-18 13:57:27 -0800 | [diff] [blame^] | 18 | static void (*isrs[32])() = { |
| 19 | isr0, isr1, isr2, isr3, isr4, isr5, isr6, isr7, isr8, isr9, isr10, |
| 20 | isr11, isr12, isr13, isr14, isr15, isr16, isr17, isr18, isr19, isr20, isr21, |
| 21 | isr22, isr23, isr24, isr25, isr26, isr27, isr28, isr29, isr30, isr31}; |
| 22 | |
| 23 | static void (*irqs[16])() = {irq0, irq1, irq2, irq3, irq4, irq5, |
| 24 | irq6, irq7, irq8, irq9, irq10, irq11, |
| 25 | irq12, irq13, irq14, irq15}; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 26 | |
| 27 | void init_gdt() |
| 28 | { |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 29 | vga_write("Initializing GDT...\n"); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 30 | gdt_pointer.limit = sizeof(struct gdt_entry) * 5 - 1; |
| 31 | gdt_pointer.base = (uint)&gdt_entries; |
| 32 | |
| 33 | gdt_set_gate(0, 0, 0, 0, 0); // Null segment |
| 34 | gdt_set_gate(1, 0, ~0, 0x9a, 0xcf); // Code segment |
| 35 | gdt_set_gate(2, 0, ~0, 0x92, 0xcf); // Data segment |
| 36 | gdt_set_gate(3, 0, ~0, 0xfa, 0xcf); // User mode code segment |
| 37 | gdt_set_gate(4, 0, ~0, 0xf2, 0xcf); // User mode data segment |
| 38 | |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 39 | for (volatile uint i = 0; i < 0x1000; i++) |
| 40 | {} // waste some time, for some reason this helps |
| 41 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 42 | gdt_flush((uint) &gdt_pointer); |
| 43 | |
| 44 | vga_write("GDT Initialized\n"); |
| 45 | } |
| 46 | |
| 47 | static void gdt_set_gate(uint i, uint base, uint limit, uchar access, uchar gran) |
| 48 | { |
| 49 | struct gdt_entry *e = &gdt_entries[i]; |
| 50 | |
| 51 | e->base_low = base & 0xffff; |
| 52 | e->base_middle = (base >> 16) & 0xff; |
| 53 | e->base_high = (base >> 24) & 0xff; |
| 54 | |
| 55 | e->limit_low = limit & 0xffff; |
| 56 | e->granularity = ((limit >> 16) & 0x0f) | (gran & 0xf0); |
| 57 | |
| 58 | e->access = access; |
| 59 | } |
| 60 | |
| 61 | void init_idt() |
| 62 | { |
| 63 | idt_pointer.limit = sizeof(struct idt_entry) * 256 - 1; |
| 64 | idt_pointer.base = (uint)&idt_entries; |
| 65 | |
| 66 | memset(&idt_entries, 0, sizeof(idt_entries)); |
| 67 | |
| 68 | vga_write("sizeof(idt_entries) = "); |
| 69 | vga_putx(sizeof(idt_entries)); |
| 70 | vga_put('\n'); |
| 71 | |
| 72 | vga_write("isr0 = "); |
| 73 | vga_putx((uint)isrs[0]); |
| 74 | vga_put('\n'); |
| 75 | |
| 76 | for (int i = 0; i < 32; i++) |
| 77 | { |
| 78 | idt_set_gate(i, (uint)isrs[i], 0x08, 0x8e); |
| 79 | } |
| 80 | |
| 81 | idt_flush((uint)&idt_pointer); |
| 82 | |
swissChili | 9b3584b | 2021-02-18 13:57:27 -0800 | [diff] [blame^] | 83 | // Remap PIC |
| 84 | |
| 85 | outb(PIC1_COMMAND, 0x11); |
| 86 | outb(PIC2_COMMAND, 0x11); |
| 87 | |
| 88 | outb(PIC1_DATA, 0x20); |
| 89 | outb(PIC2_DATA, 0x28); |
| 90 | outb(PIC1_DATA, 0x04); |
| 91 | outb(PIC2_DATA, 0x02); |
| 92 | outb(PIC1_DATA, 0x01); |
| 93 | outb(PIC2_DATA, 0x01); |
| 94 | outb(PIC1_DATA, 0); |
| 95 | outb(PIC2_DATA, 0); |
| 96 | |
| 97 | for (int i = 0; i < 16; i++) |
| 98 | { |
| 99 | idt_set_gate(i + 32, (uint)irqs[i], 0x08, 0x8e); |
| 100 | } |
| 101 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 102 | vga_write("IDT Initialized!\n"); |
| 103 | } |
| 104 | |
| 105 | static void idt_set_gate(uchar num, uint base, ushort selector, uchar flags) |
| 106 | { |
| 107 | struct idt_entry *e = &idt_entries[num]; |
| 108 | |
| 109 | e->base_low = base & 0xffff; |
| 110 | e->base_high = (base >> 16) & 0xffff; |
| 111 | |
| 112 | e->selector = selector; |
| 113 | e->zero = 0; |
| 114 | e->flags = flags /* | 0x60 */; |
| 115 | } |
| 116 | |
| 117 | void init_descriptor_tables() |
| 118 | { |
| 119 | init_gdt(); |
| 120 | init_idt(); |
| 121 | } |