Unifying External Interrupt System (#51)
[lunaix-os.git] / lunaix-os / arch / x86 / exceptions / isrdef.c
1 /* Generated from i386_isrdef.c.j2. Do NOT modify */
2
3 #include <lunaix/types.h>
4 #include "asm/x86_ivs.h"
5 #include "asm/x86.h"
6
7 #define IDT_INTERRUPT 0x70
8 #define IDT_ATTR(dpl, type) (((type) << 5) | ((dpl & 3) << 13) | (1 << 15))
9 #define IDT_ENTRY 256
10
11 #define __expand_iv(iv) _asm_isr##iv()
12 #define DECLARE_ISR(iv) extern void __expand_iv(iv);
13
14 #ifndef CONFIG_ARCH_X86_64
15
16 static inline void 
17 install_idte(struct x86_sysdesc* idt, int iv, ptr_t isr, int dpl) 
18 {
19     struct x86_sysdesc* idte = &idt[iv];
20
21     idte->hi = ((ptr_t)isr & 0xffff0000) | IDT_ATTR(dpl, IDT_INTERRUPT); 
22     idte->lo = (KCODE_SEG << 16) | ((ptr_t)isr & 0x0000ffff);           
23 }
24 struct x86_sysdesc _idt[IDT_ENTRY];
25 u16_t _idt_limit = sizeof(_idt) - 1;
26
27 #else
28
29 static inline must_inline void 
30 install_idte(struct x86_sysdesc* idt, int iv, ptr_t isr, int dpl) 
31 {
32     struct x86_sysdesc* idte = &idt[iv];
33     
34     idte->hi = isr >> 32;
35     
36     idte->lo = (isr & 0xffff0000UL) | IDT_ATTR(dpl, IDT_INTERRUPT); 
37
38     idte->lo <<= 32;                                                     
39     idte->lo |= (KCODE_SEG << 16) | (isr & 0x0000ffffUL);
40 }
41
42 struct x86_sysdesc _idt[IDT_ENTRY];
43 u16_t _idt_limit = sizeof(_idt) - 1;
44
45 #endif
46
47 DECLARE_ISR(0)
48 DECLARE_ISR(33)
49
50 void
51 exception_install_handler()
52 {
53     ptr_t start = (ptr_t)_asm_isr0;
54     
55     for (int i = 0; i < 256; i++) {
56         install_idte(_idt, i, start, 0);
57         start += 16;
58     }
59
60     install_idte(_idt, 33, (ptr_t)_asm_isr33, 3);
61
62 #ifdef CONFIG_ARCH_X86_64
63     // TODO set different IST to some exception so it can get a
64     //      better and safe stack to work with
65 #endif
66 }