My Project
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
37 /* 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) */
38 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);
39 /* 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) */
40 set_idt_gate(0, (unsigned long)isr0);
41 set_idt_gate(1, (unsigned long)isr1);
42 set_idt_gate(2, (unsigned long)isr2);
43 set_idt_gate(3, (unsigned long)isr3);
44 set_idt_gate(4, (unsigned long)isr4);
45 set_idt_gate(5, (unsigned long)isr5);
46 set_idt_gate(6, (unsigned long)isr6);
47 set_idt_gate(7, (unsigned long)isr7);
48 set_idt_gate(8, (unsigned long)isr8);
49 set_idt_gate(9, (unsigned long)isr9);
50 set_idt_gate(10, (unsigned long)isr10);
51 set_idt_gate(11, (unsigned long)isr11);
52 set_idt_gate(12, (unsigned long)isr12);
53 set_idt_gate(13, (unsigned long)isr13);
54 set_idt_gate(14, (unsigned long)isr14);
55 set_idt_gate(15, (unsigned long)isr15);
56 set_idt_gate(16, (unsigned long)isr16);
57 set_idt_gate(17, (unsigned long)isr17);
58 set_idt_gate(18, (unsigned long)isr18);
59 set_idt_gate(19, (unsigned long)isr19);
60 set_idt_gate(20, (unsigned long)isr20);
61 set_idt_gate(21, (unsigned long)isr21);
62 set_idt_gate(22, (unsigned long)isr22);
63 set_idt_gate(23, (unsigned long)isr23);
64 set_idt_gate(24, (unsigned long)isr24);
65 set_idt_gate(25, (unsigned long)isr25);
66 set_idt_gate(26, (unsigned long)isr26);
67 set_idt_gate(27, (unsigned long)isr27);
68 set_idt_gate(28, (unsigned long)isr28);
69 set_idt_gate(29, (unsigned long)isr29);
70 set_idt_gate(30, (unsigned long)isr30);
71 set_idt_gate(31, (unsigned long)isr31);
72
73 /* Fill IDT Gates for IRQs (32-47) */
74 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);
75 set_idt_gate(32, (unsigned long)irq0);
76 set_idt_gate(33, (unsigned long)irq1);
77 set_idt_gate(34, (unsigned long)irq2);
78 set_idt_gate(35, (unsigned long)irq3);
79 set_idt_gate(36, (unsigned long)irq4);
80 set_idt_gate(37, (unsigned long)irq5);
81 set_idt_gate(38, (unsigned long)irq6);
82 set_idt_gate(39, (unsigned long)irq7);
83 set_idt_gate(40, (unsigned long)irq8);
84 set_idt_gate(41, (unsigned long)irq9);
85 set_idt_gate(42, (unsigned long)irq10);
86 set_idt_gate(43, (unsigned long)irq11);
87 set_idt_gate(44, (unsigned long)irq12);
88 set_idt_gate(45, (unsigned long)irq13);
89 set_idt_gate(46, (unsigned long)irq14);
90 set_idt_gate(47, (unsigned long)irq15);
91 /* For LAPIC */
92 extern void isr239(void); // LAPIC ISR.
93 set_idt_gate(LAPIC_TIMER_VECTOR, (unsigned long)isr239);
94#define LAPIC_SPURIOUS_VECTOR 254
95 /* For SIV LAPIC */
96 extern void isr254(void); // SIV ISR
97 set_idt_gate(LAPIC_SPURIOUS_VECTOR, (unsigned long)isr254);
98 /* For LAPIC CPU Action */
99 extern void isr_ipi(void);
100 set_idt_gate(VECTOR_IPI, (unsigned long)isr_ipi);
101
102 extern void isr_dpc(void); // DPC Handler.
103 set_idt_gate(VECTOR_DPC, (unsigned long)isr_dpc);
104
105 extern void isr_apc(void); // APC Handler.
106 set_idt_gate(VECTOR_APC, (unsigned long)isr_apc);
107
108 /* ISTs are reloaded in MeInitProcessor */
109
110 /* Finally, Load IDT. */
111 PIDT.limit = sizeof(IDT_ENTRY64) * IDT_ENTRIES - 1; // Max limit is the amount of IDT_ENTRIES structs (0-255)
112 PIDT.base = (unsigned long)&IDT;
113 __lidt(&PIDT);
114}
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:179
FORCEINLINE void __lidt(void *idt_ptr)
Definition intrin.h:135
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_IPI
Definition mh.h:49
#define VECTOR_DPC
Definition mh.h:47
#define LAPIC_TIMER_VECTOR
Definition mh.h:50
#define VECTOR_APC
Definition mh.h:48