Working on 64-bit IDT
[tpg/acess2.git] / Kernel / arch / x86_64 / desctab.asm
1 ;
2 ;
3 ;
4 [BITS 64]
5
6 %define NUM_IRQ_CALLBACKS       4
7
8 MM_LOCALAPIC    equ     0xFFFFFD0000000000
9
10 %macro PUSH_GPR 0
11         mov [rsp-0x60], rsp
12         mov [rsp-0x08], r15
13         mov [rsp-0x10], r14
14         mov [rsp-0x18], r13
15         mov [rsp-0x20], r12
16         mov [rsp-0x28], r11
17         mov [rsp-0x30], r10
18         mov [rsp-0x38], r9
19         mov [rsp-0x40], r8
20         mov [rsp-0x48], rdi
21         mov [rsp-0x50], rsi
22         mov [rsp-0x58], rbp
23         mov [rsp-0x68], rbx
24         mov [rsp-0x70], rdx
25         mov [rsp-0x78], rcx
26         mov [rsp-0x80], rax
27         sub rsp, 0x80
28 %endmacro
29 %macro POP_GPR  0
30         add rsp, 0x80
31         mov r15, [rsp-0x08]
32         mov r14, [rsp-0x10]
33         mov r13, [rsp-0x18]
34         mov r12, [rsp-0x20]
35         mov r11, [rsp-0x28]
36         mov r10, [rsp-0x30]
37         mov r9,  [rsp-0x38]
38         mov r8,  [rsp-0x40]
39         mov rdi, [rsp-0x48]
40         mov rsi, [rsp-0x50]
41         mov rbp, [rsp-0x58]
42         ;mov rsp, [rsp-0x60]
43         mov rbx, [rsp-0x68]
44         mov rdx, [rsp-0x70]
45         mov rcx, [rsp-0x78]
46         mov rax, [rsp-0x80]
47 %endmacro
48
49 [section .text]
50 [global Desctab_Init]
51 Desctab_Init:
52         ; Install IDT
53         mov rax, gIDTPtr
54         lidt [rax]
55         
56         ; Save to make following instructions smaller
57         mov rdi, gIDT
58         
59         ; Set an IDT entry to a callback
60         %macro SETIDT 2
61         mov rax, %2
62         mov     WORD [rdi + %1*16], ax
63         shr rax, 16
64         mov     WORD [rdi + %1*16+6], ax
65         shr rax, 16
66         mov DWORD [rdi+%1*16+8], eax
67         ; Enable
68         mov     ax, WORD [rdi + %1*16 + 4]
69         or ax, 0x8000
70         mov     WORD [rdi + %1*16 + 4], ax
71         %endmacro
72         
73         ; Install error handlers
74         %macro SETISR 1
75         SETIDT %1, Isr%1
76         %endmacro
77         
78         %assign i 0
79         %rep 32
80         SETISR i
81         %assign i i+1
82         %endrep
83         
84         ; Install IRQs
85         %macro SETIRQ 2
86         SETIDT %2, Irq%1
87         %endmacro
88         
89         %assign i 0
90         %rep 16
91         SETIRQ i, 0xF0+i
92         %assign i i+1
93         %endrep
94         
95         ; Remap PIC
96         
97         ret
98
99 ; int IRQ_AddHandler(int IRQ, void (*Handler)(int IRQ))
100 ; Return Values:
101 ;  0 on Success
102 ; -1 on an invalid IRQ Number
103 ; -2 when no slots are avaliable
104 [global IRQ_AddHandler]
105 IRQ_AddHandler:
106         ; RDI - IRQ Number
107         ; RSI - Callback
108         
109         cmp rdi, 16
110         jb .numOK
111         xor rax, rax
112         dec rax
113         jmp .ret
114 .numOK:
115
116         mov rax, rdi
117         shr rax, 3+2
118         mov rcx, gaIRQ_Handlers
119         add rax, rcx
120         
121         ; Find a free callback slot
122         %rep NUM_IRQ_CALLBACKS
123         mov rdx, [rax]
124         test rdx, rdx
125         jz .assign
126         add rax, 8
127         %endrep
128         ; None found, return -2
129         xor rax, rax
130         dec rax
131         dec rax
132         jmp .ret
133         
134         ; Assign the IRQ Callback
135 .assign:
136         mov [rax], rsi
137         xor rax, rax
138
139 .ret:
140         ret
141
142 %macro ISR_NOERRNO      1
143 Isr%1:
144         push    0
145         push    %1
146         jmp     ErrorCommon
147 %endmacro
148 %macro ISR_ERRNO        1
149 Isr%1:
150         push    %1
151         jmp     ErrorCommon
152 %endmacro
153
154 ISR_NOERRNO     0;  0: Divide By Zero Exception
155 ISR_NOERRNO     1;  1: Debug Exception
156 ISR_NOERRNO     2;  2: Non Maskable Interrupt Exception
157 ISR_NOERRNO     3;  3: Int 3 Exception
158 ISR_NOERRNO     4;  4: INTO Exception
159 ISR_NOERRNO     5;  5: Out of Bounds Exception
160 ISR_NOERRNO     6;  6: Invalid Opcode Exception
161 ISR_NOERRNO     7;  7: Coprocessor Not Available Exception
162 ISR_ERRNO       8;  8: Double Fault Exception (With Error Code!)
163 ISR_NOERRNO     9;  9: Coprocessor Segment Overrun Exception
164 ISR_ERRNO       10; 10: Bad TSS Exception (With Error Code!)
165 ISR_ERRNO       11; 11: Segment Not Present Exception (With Error Code!)
166 ISR_ERRNO       12; 12: Stack Fault Exception (With Error Code!)
167 ISR_ERRNO       13; 13: General Protection Fault Exception (With Error Code!)
168 ISR_ERRNO       14; 14: Page Fault Exception (With Error Code!)
169 ISR_NOERRNO     15; 15: Reserved Exception
170 ISR_NOERRNO     16; 16: Floating Point Exception
171 ISR_NOERRNO     17; 17: Alignment Check Exception
172 ISR_NOERRNO     18; 18: Machine Check Exception
173 ISR_NOERRNO     19; 19: Reserved
174 ISR_NOERRNO     20; 20: Reserved
175 ISR_NOERRNO     21; 21: Reserved
176 ISR_NOERRNO     22; 22: Reserved
177 ISR_NOERRNO     23; 23: Reserved
178 ISR_NOERRNO     24; 24: Reserved
179 ISR_NOERRNO     25; 25: Reserved
180 ISR_NOERRNO     26; 26: Reserved
181 ISR_NOERRNO     27; 27: Reserved
182 ISR_NOERRNO     28; 28: Reserved
183 ISR_NOERRNO     29; 29: Reserved
184 ISR_NOERRNO     30; 30: Reserved
185 ISR_NOERRNO     31; 31: Reserved
186
187 ErrorCommon:
188         add rsp, 2*8
189         iret
190
191 %macro DEFIRQ   1
192 Irq%1:
193         push    0
194         push    %1
195         jmp     IrqCommon
196 %endmacro
197
198 %assign i 0
199 %rep 16
200 DEFIRQ  i
201 %assign i i+1
202 %endrep
203
204 IrqCommon:
205         PUSH_GPR
206         
207         mov rbx, [rsp+16*8]     ; Calculate address
208         shr rbx, 3+2    ; *8*4
209         mov rax, gaIRQ_Handlers
210         add rbx, rax
211         
212         %assign i 0
213         %rep NUM_IRQ_CALLBACKS
214         mov rax, [rbx]
215         test rax, rax
216         mov rdi, [rsp+16*8]     ; Get IRQ number
217         jz .skip.%[i]
218         call rax        ; 2 Bytes (Op and Mod/RM)
219 .skip.%[i]:
220         add rbx, 8
221         %assign i i+1
222         %endrep
223         
224         mov rdi, [rsp+16*8]     ; Get IRQ number
225         cmp rdi, 8
226         mov al, 0x20
227         jb .skipAckSecondary
228         mov dx, 0x00A0
229         out dx, al
230 .skipAckSecondary:
231         mov dx, 0x0020
232         out dx, al
233         
234         POP_GPR
235         add rsp, 16
236         iret
237
238 [extern Proc_Scheduler]
239 SchedulerIRQ:
240         ; TODO: Find Current CPU
241         PUSH_GPR
242         ;PUSH_FPU
243         ;PUSH_XMM
244         
245         xor rsi, rsi
246         mov rdi, MM_LOCALAPIC+0x20
247         mov esi, [rdi]
248         call Proc_Scheduler
249         
250         ;POP_XMM
251         ;POP_FPU
252         POP_GPR
253         iret
254
255 [section .data]
256 gIDT:
257         times 256       dd      0x00080000, 0x00008E00, 0, 0    ; 64-bit Interrupt Gate, CS = 0x8, IST0
258 gIDTPtr:
259         dw      256*16-1
260         dq      gIDT
261
262 gaIRQ_Handlers:
263         times   16*NUM_IRQ_CALLBACKS    dq      0

UCC git Repository :: git.ucc.asn.au