kernel
Loading...
Searching...
No Matches
idt.c
Go to the documentation of this file.
1/*
2 * PROJECT: MatanelOS Kernel
3 * LICENSE: GPLv3
4 * PURPOSE: IMPLEMENTATION To SETUP IDT Entries.
5 */
6
7#include "../../includes/mh.h"
8
11
12/* Set one gate. */
13void set_idt_gate(int n, unsigned long int handler) {
14 IDT[n].offset_low = handler & 0xFFFF;
15 IDT[n].selector = 0x08; // code segment selector
16 IDT[n].ist = 0;
17 IDT[n].type_attr = 0x8E; // interrupt gate, present, ring 0
18 IDT[n].offset_mid = (handler >> 16) & 0xFFFF;
19 IDT[n].offset_high = (handler >> 32) & 0xFFFFFFFF;
20 IDT[n].zero = 0;
21}
22
23/* Populate IDT: exceptions, IRQ, and then finally load it. */
25 /* REMAP the PIC so IRQs start at vector 0x20 */
26 __outbyte(0x20, 0x11); // initialize master PIC
27 __outbyte(0xA0, 0x11); // initialize slave PIC
28 __outbyte(0x21, 0x20); // master PIC vector offset 0x20.
29 __outbyte(0xA1, 0x28); // slave PIC vector offset 0x28
30 __outbyte(0x21, 0x04);
31 __outbyte(0xA1, 0x02);
32 __outbyte(0x21, 0x01);
33 __outbyte(0xA1, 0x01);
34 __outbyte(0x21, 0x0);
35 __outbyte(0xA1, 0x0);
36 __outbyte(0x21, 0xFF); // 0xFF = 11111111 (Mask all 8 lines)
37 __outbyte(0xA1, 0xFF); // 0xFF = 11111111 (Mask all 8 lines)
38
39 /* Fill IDT Entries for CPU Exceptions (0-31) */ /* For clarifications, all of the ISR and IRQ externals live in isr_stub (where it defines the functions and gets linked together, via the global keyword) and isr_common_stub (where it does the routine), where they are linked together via the linker (externs) */
40 extern void isr0(void); extern void isr1(void); extern void isr2(void); extern void isr3(void); extern void isr4(void); extern void isr5(void); extern void isr6(void); extern void isr7(void); extern void isr8(void); extern void isr9(void); extern void isr10(void); extern void isr11(void); extern void isr12(void); extern void isr13(void); extern void isr14(void); extern void isr15(void); extern void isr16(void); extern void isr17(void); extern void isr18(void); extern void isr19(void); extern void isr20(void); extern void isr21(void); extern void isr22(void); extern void isr23(void); extern void isr24(void); extern void isr25(void); extern void isr26(void); extern void isr27(void); extern void isr28(void); extern void isr29(void); extern void isr30(void); extern void isr31(void);
41 /* I forgo t to set n in the set_idt_gate, they were all zeros and I didn't understand why I got IRQ of like 50 thousand and error code of 4 billion. (i copy pasted each line instead of typing manually) */
42 set_idt_gate(0, (unsigned long)isr0);
43 set_idt_gate(1, (unsigned long)isr1);
44 set_idt_gate(2, (unsigned long)isr2);
45 set_idt_gate(3, (unsigned long)isr3);
46 set_idt_gate(4, (unsigned long)isr4);
47 set_idt_gate(5, (unsigned long)isr5);
48 set_idt_gate(6, (unsigned long)isr6);
49 set_idt_gate(7, (unsigned long)isr7);
50 set_idt_gate(8, (unsigned long)isr8);
51 set_idt_gate(9, (unsigned long)isr9);
52 set_idt_gate(10, (unsigned long)isr10);
53 set_idt_gate(11, (unsigned long)isr11);
54 set_idt_gate(12, (unsigned long)isr12);
55 set_idt_gate(13, (unsigned long)isr13);
56 set_idt_gate(14, (unsigned long)isr14);
57 set_idt_gate(15, (unsigned long)isr15);
58 set_idt_gate(16, (unsigned long)isr16);
59 set_idt_gate(17, (unsigned long)isr17);
60 set_idt_gate(18, (unsigned long)isr18);
61 set_idt_gate(19, (unsigned long)isr19);
62 set_idt_gate(20, (unsigned long)isr20);
63 set_idt_gate(21, (unsigned long)isr21);
64 set_idt_gate(22, (unsigned long)isr22);
65 set_idt_gate(23, (unsigned long)isr23);
66 set_idt_gate(24, (unsigned long)isr24);
67 set_idt_gate(25, (unsigned long)isr25);
68 set_idt_gate(26, (unsigned long)isr26);
69 set_idt_gate(27, (unsigned long)isr27);
70 set_idt_gate(28, (unsigned long)isr28);
71 set_idt_gate(29, (unsigned long)isr29);
72 set_idt_gate(30, (unsigned long)isr30);
73 set_idt_gate(31, (unsigned long)isr31);
74
75 /* Fill IDT Gates for IRQs (32-47) */
76 extern void irq0(void); extern void irq1(void); extern void irq2(void); extern void irq3(void); extern void irq4(void); extern void irq5(void); extern void irq6(void); extern void irq7(void); extern void irq8(void); extern void irq9(void); extern void irq10(void); extern void irq11(void); extern void irq12(void); extern void irq13(void); extern void irq14(void); extern void irq15(void);
77 set_idt_gate(32, (unsigned long)irq0);
78 set_idt_gate(33, (unsigned long)irq1);
79 set_idt_gate(34, (unsigned long)irq2);
80 set_idt_gate(35, (unsigned long)irq3);
81 set_idt_gate(36, (unsigned long)irq4);
82 set_idt_gate(37, (unsigned long)irq5);
83 set_idt_gate(38, (unsigned long)irq6);
84 set_idt_gate(39, (unsigned long)irq7);
85 set_idt_gate(40, (unsigned long)irq8);
86 set_idt_gate(41, (unsigned long)irq9);
87 set_idt_gate(42, (unsigned long)irq10);
88 set_idt_gate(43, (unsigned long)irq11);
89 set_idt_gate(44, (unsigned long)irq12);
90 set_idt_gate(45, (unsigned long)irq13);
91 set_idt_gate(46, (unsigned long)irq14);
92 set_idt_gate(47, (unsigned long)irq15);
93
94 /* For LAPIC Timer */
95 extern void isr_clock(void); // LAPIC ISR.
96 set_idt_gate(VECTOR_CLOCK, (unsigned long)isr_clock);
97
98#define LAPIC_SPURIOUS_VECTOR 254
99 /* For SIV LAPIC */
100 extern void isr254(void); // SIV ISR
101 set_idt_gate(LAPIC_SPURIOUS_VECTOR, (unsigned long)isr254);
102
103 /* For LAPIC CPU Action */
104 extern void isr_ipi(void);
105 set_idt_gate(VECTOR_IPI, (unsigned long)isr_ipi);
106
107 extern void isr_dpc(void); // DPC Handler.
108 set_idt_gate(VECTOR_DPC, (unsigned long)isr_dpc);
109
110 extern void isr_apc(void); // APC Handler.
111 set_idt_gate(VECTOR_APC, (unsigned long)isr_apc);
112
113 /* ISTs are reloaded in MeInitProcessor */
114
115 /* Finally, Load IDT. */
116 PIDT.limit = sizeof(IDT_ENTRY64) * IDT_ENTRIES - 1; // Max limit is the amount of IDT_ENTRIES structs (0-255)
117 PIDT.base = (unsigned long)&IDT;
118 __lidt(&PIDT);
119}
void install_idt()
Definition idt.c:24
#define LAPIC_SPURIOUS_VECTOR
void set_idt_gate(int n, unsigned long int handler)
Definition idt.c:13
FORCEINLINE void __outbyte(unsigned short port, unsigned char val)
Definition intrin.h:194
FORCEINLINE void __lidt(void *idt_ptr)
Definition intrin.h:150
IDT_PTR PIDT
Definition idt.c:10
IDT_ENTRY64 IDT[]
Definition idt.c:9
struct _IDT_PTR IDT_PTR
struct _IDT_ENTRY_64 IDT_ENTRY64
#define IDT_ENTRIES
Definition mh.h:26
#define VECTOR_CLOCK
Definition mh.h:37
#define VECTOR_IPI
Definition mh.h:38
#define VECTOR_DPC
Definition mh.h:36
#define VECTOR_APC
Definition mh.h:35