039fe95ed5497afa456f3b3416c5979eec6fa64b
[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         ; Save to make following instructions smaller
53         mov rdi, gIDT
54         
55         ; Set an IDT entry to a callback
56         %macro SETIDT 2
57         mov rax, %2
58         mov     WORD [rdi + %1*16], ax
59         shr rax, 16
60         mov     WORD [rdi + %1*16 + 6], ax
61         shr rax, 16
62         mov DWORD [rdi + %1*16 + 8], eax
63         ; Enable
64         mov     ax, WORD [rdi + %1*16 + 4]
65         or ax, 0x8000
66         mov     WORD [rdi + %1*16 + 4], ax
67         %endmacro
68         
69         ; Install error handlers
70         %macro SETISR 1
71         SETIDT %1, Isr%1
72         %endmacro
73         
74         %assign i 0
75         %rep 32
76         SETISR i
77         %assign i i+1
78         %endrep
79         
80         ; Install IRQs
81         SETIDT  0xF0, Irq0
82         SETIDT  0xF1, Irq1
83         SETIDT  0xF2, Irq2
84         SETIDT  0xF3, Irq3
85         SETIDT  0xF4, Irq4
86         SETIDT  0xF5, Irq5
87         SETIDT  0xF6, Irq6
88         SETIDT  0xF7, Irq7
89         SETIDT  0xF8, Irq8
90         SETIDT  0xF9, Irq9
91         SETIDT  0xFA, Irq10
92         SETIDT  0xFB, Irq11
93         SETIDT  0xFC, Irq12
94         SETIDT  0xFD, Irq13
95         SETIDT  0xFE, Irq14
96         SETIDT  0xFF, Irq15
97
98         ; Remap PIC
99         push rdx        ; Save RDX
100         mov dx, 0x20
101         mov al, 0x11
102         out dx, al      ;       Init Command
103     mov dx, 0x21
104         mov al, 0xF0
105         out dx, al      ;       Offset (Start of IDT Range)
106     mov al, 0x04
107         out dx, al      ;       IRQ connected to Slave (00000100b) = IRQ2
108     mov al, 0x01
109         out dx, al      ;       Set Mode
110     mov al, 0x00
111         out dx, al      ;       Set Mode
112         
113         mov dx, 0xA0
114         mov al, 0x11
115         out dx, al      ;       Init Command
116     mov dx, 0xA1
117         mov al, 0xF8
118         out dx, al      ;       Offset (Start of IDT Range)
119     mov al, 0x02
120         out dx, al      ;       IRQ Line connected to master
121     mov al, 0x01
122         out dx, al      ;       Set Mode
123     mov dl, 0x00
124         out dx, al      ;       Set Mode
125         pop rdx
126         
127         
128         ; Install IDT
129         mov rax, gIDTPtr
130         lidt [rax]
131         
132         ; Start interrupts
133         sti
134         
135         ret
136
137 ; int IRQ_AddHandler(int IRQ, void (*Handler)(int IRQ))
138 ; Return Values:
139 ;  0 on Success
140 ; -1 on an invalid IRQ Number
141 ; -2 when no slots are avaliable
142 [global IRQ_AddHandler]
143 IRQ_AddHandler:
144         ; RDI - IRQ Number
145         ; RSI - Callback
146         
147         cmp rdi, 16
148         jb .numOK
149         xor rax, rax
150         dec rax
151         jmp .ret
152 .numOK:
153
154         mov rax, rdi
155         shr rax, 3+2
156         mov rcx, gaIRQ_Handlers
157         add rax, rcx
158         
159         ; Find a free callback slot
160         %rep NUM_IRQ_CALLBACKS
161         mov rdx, [rax]
162         test rdx, rdx
163         jz .assign
164         add rax, 8
165         %endrep
166         ; None found, return -2
167         xor rax, rax
168         dec rax
169         dec rax
170         jmp .ret
171         
172         ; Assign the IRQ Callback
173 .assign:
174         mov [rax], rsi
175         xor rax, rax
176
177 .ret:
178         ret
179
180 %macro ISR_NOERRNO      1
181 Isr%1:
182         push    QWORD 0
183         push    QWORD %1
184         jmp     ErrorCommon
185 %endmacro
186 %macro ISR_ERRNO        1
187 Isr%1:
188         push    QWORD %1
189         jmp     ErrorCommon
190 %endmacro
191
192 ISR_NOERRNO     0;  0: Divide By Zero Exception
193 ISR_NOERRNO     1;  1: Debug Exception
194 ISR_NOERRNO     2;  2: Non Maskable Interrupt Exception
195 ISR_NOERRNO     3;  3: Int 3 Exception
196 ISR_NOERRNO     4;  4: INTO Exception
197 ISR_NOERRNO     5;  5: Out of Bounds Exception
198 ISR_NOERRNO     6;  6: Invalid Opcode Exception
199 ISR_NOERRNO     7;  7: Coprocessor Not Available Exception
200 ISR_ERRNO       8;  8: Double Fault Exception (With Error Code!)
201 ISR_NOERRNO     9;  9: Coprocessor Segment Overrun Exception
202 ISR_ERRNO       10; 10: Bad TSS Exception (With Error Code!)
203 ISR_ERRNO       11; 11: Segment Not Present Exception (With Error Code!)
204 ISR_ERRNO       12; 12: Stack Fault Exception (With Error Code!)
205 ISR_ERRNO       13; 13: General Protection Fault Exception (With Error Code!)
206 ISR_ERRNO       14; 14: Page Fault Exception (With Error Code!)
207 ISR_NOERRNO     15; 15: Reserved Exception
208 ISR_NOERRNO     16; 16: Floating Point Exception
209 ISR_NOERRNO     17; 17: Alignment Check Exception
210 ISR_NOERRNO     18; 18: Machine Check Exception
211 ISR_NOERRNO     19; 19: Reserved
212 ISR_NOERRNO     20; 20: Reserved
213 ISR_NOERRNO     21; 21: Reserved
214 ISR_NOERRNO     22; 22: Reserved
215 ISR_NOERRNO     23; 23: Reserved
216 ISR_NOERRNO     24; 24: Reserved
217 ISR_NOERRNO     25; 25: Reserved
218 ISR_NOERRNO     26; 26: Reserved
219 ISR_NOERRNO     27; 27: Reserved
220 ISR_NOERRNO     28; 28: Reserved
221 ISR_NOERRNO     29; 29: Reserved
222 ISR_NOERRNO     30; 30: Reserved
223 ISR_NOERRNO     31; 31: Reserved
224
225 [extern Error_Handler]
226 [global ErrorCommon]
227 ErrorCommon:
228         PUSH_GPR
229         push gs
230         push fs
231         ;PUSH_FPU
232         ;PUSH_XMM
233         
234         mov rdi, rsp
235         xchg bx, bx
236         call Error_Handler
237         
238         ;POP_XMM
239         ;POP_FPU
240         pop fs
241         pop gs
242         POP_GPR
243         add rsp, 2*8
244         iretq
245
246 %macro DEFIRQ   1
247 Irq%1:
248         push    0
249         push    %1
250         jmp     IrqCommon
251 %endmacro
252
253 %assign i 0
254 %rep 16
255 DEFIRQ  i
256 %assign i i+1
257 %endrep
258
259 [global IrqCommon]
260 IrqCommon:
261         PUSH_GPR
262         
263         mov rbx, [rsp+16*8]     ; Calculate address
264         shr rbx, 3+2    ; *8*4
265         mov rax, gaIRQ_Handlers
266         add rbx, rax
267         
268         ; Check all callbacks
269         %assign i 0
270         %rep NUM_IRQ_CALLBACKS
271         ; Get callback address
272         mov rax, [rbx]
273         test rax, rax   ; Check if it exists
274         jz .skip.%[i]
275         ; Set RDI to IRQ number
276         mov rdi, [rsp+16*8]     ; Get IRQ number
277         call rax        ; Call
278 .skip.%[i]:
279         add rbx, 8      ; Next!
280         %assign i i+1
281         %endrep
282         
283         ; ACK
284         mov rdi, [rsp+16*8]     ; Get IRQ number
285         cmp rdi, 8
286         mov al, 0x20
287         jb .skipAckSecondary
288         mov dx, 0x00A0
289         out dx, al
290 .skipAckSecondary:
291         mov dx, 0x0020
292         out dx, al
293         
294         POP_GPR
295         add rsp, 8*2
296         ;xchg bx, bx
297         iretq
298
299 [extern Proc_Scheduler]
300 [global SchedulerIRQ]
301 SchedulerIRQ:
302         ; TODO: Find Current CPU
303         PUSH_GPR
304         ;PUSH_FPU
305         ;PUSH_XMM
306         
307         xor rsi, rsi
308         mov rdi, MM_LOCALAPIC+0x20
309         mov esi, [rdi]
310         call Proc_Scheduler
311         
312         ;POP_XMM
313         ;POP_FPU
314         POP_GPR
315         add rsp, 8*2
316         iretq
317
318 [section .data]
319 gIDT:
320         ; 64-bit Interrupt Gate, CS = 0x8, IST0 (Disabled)
321         times 256       dd      0x00080000, 0x00000E00, 0, 0
322 gIDTPtr:
323         dw      256*16-1
324         dq      gIDT
325
326 gaIRQ_Handlers:
327         times   16*NUM_IRQ_CALLBACKS    dq      0

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