More work on x86_64 build, error handling and 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         ; 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         %macro SETIRQ 1
82         SETIDT 0xF0+%1, Irq%1
83         %endmacro
84         
85         %assign i 0
86         %rep 16
87         SETIRQ i
88         %assign i i+1
89         %endrep
90
91         ; Remap PIC
92         push rdx        ; Save RDX
93         mov dx, 0x20
94         mov al, 0x11
95         out dx, al      ;       Init Command
96     mov dx, 0x21
97         mov al, 0xF0
98         out dx, al      ;       Offset (Start of IDT Range)
99     mov al, 0x04
100         out dx, al      ;       IRQ connected to Slave (00000100b) = IRQ2
101     mov al, 0x01
102         out dx, al      ;       Set Mode
103     mov al, 0x00
104         out dx, al      ;       Set Mode
105         
106         mov dx, 0xA0
107         mov al, 0x11
108         out dx, al      ;       Init Command
109     mov dx, 0xA1
110         mov al, 0xF8
111         out dx, al      ;       Offset (Start of IDT Range)
112     mov al, 0x02
113         out dx, al      ;       IRQ Line connected to master
114     mov al, 0x01
115         out dx, al      ;       Set Mode
116     mov dl, 0x00
117         out dx, al      ;       Set Mode
118         pop rdx
119         
120         
121         ; Install IDT
122         mov rax, gIDTPtr
123         lidt [rax]
124         
125         ; Start interrupts
126         sti
127         
128         ret
129
130 ; int IRQ_AddHandler(int IRQ, void (*Handler)(int IRQ))
131 ; Return Values:
132 ;  0 on Success
133 ; -1 on an invalid IRQ Number
134 ; -2 when no slots are avaliable
135 [global IRQ_AddHandler]
136 IRQ_AddHandler:
137         ; RDI - IRQ Number
138         ; RSI - Callback
139         
140         cmp rdi, 16
141         jb .numOK
142         xor rax, rax
143         dec rax
144         jmp .ret
145 .numOK:
146
147         mov rax, rdi
148         shr rax, 3+2
149         mov rcx, gaIRQ_Handlers
150         add rax, rcx
151         
152         ; Find a free callback slot
153         %rep NUM_IRQ_CALLBACKS
154         mov rdx, [rax]
155         test rdx, rdx
156         jz .assign
157         add rax, 8
158         %endrep
159         ; None found, return -2
160         xor rax, rax
161         dec rax
162         dec rax
163         jmp .ret
164         
165         ; Assign the IRQ Callback
166 .assign:
167         mov [rax], rsi
168         xor rax, rax
169
170 .ret:
171         ret
172
173 %macro ISR_NOERRNO      1
174 Isr%1:
175         push    0
176         push    %1
177         jmp     ErrorCommon
178 %endmacro
179 %macro ISR_ERRNO        1
180 Isr%1:
181         push    %1
182         jmp     ErrorCommon
183 %endmacro
184
185 ISR_NOERRNO     0;  0: Divide By Zero Exception
186 ISR_NOERRNO     1;  1: Debug Exception
187 ISR_NOERRNO     2;  2: Non Maskable Interrupt Exception
188 ISR_NOERRNO     3;  3: Int 3 Exception
189 ISR_NOERRNO     4;  4: INTO Exception
190 ISR_NOERRNO     5;  5: Out of Bounds Exception
191 ISR_NOERRNO     6;  6: Invalid Opcode Exception
192 ISR_NOERRNO     7;  7: Coprocessor Not Available Exception
193 ISR_ERRNO       8;  8: Double Fault Exception (With Error Code!)
194 ISR_NOERRNO     9;  9: Coprocessor Segment Overrun Exception
195 ISR_ERRNO       10; 10: Bad TSS Exception (With Error Code!)
196 ISR_ERRNO       11; 11: Segment Not Present Exception (With Error Code!)
197 ISR_ERRNO       12; 12: Stack Fault Exception (With Error Code!)
198 ISR_ERRNO       13; 13: General Protection Fault Exception (With Error Code!)
199 ISR_ERRNO       14; 14: Page Fault Exception (With Error Code!)
200 ISR_NOERRNO     15; 15: Reserved Exception
201 ISR_NOERRNO     16; 16: Floating Point Exception
202 ISR_NOERRNO     17; 17: Alignment Check Exception
203 ISR_NOERRNO     18; 18: Machine Check Exception
204 ISR_NOERRNO     19; 19: Reserved
205 ISR_NOERRNO     20; 20: Reserved
206 ISR_NOERRNO     21; 21: Reserved
207 ISR_NOERRNO     22; 22: Reserved
208 ISR_NOERRNO     23; 23: Reserved
209 ISR_NOERRNO     24; 24: Reserved
210 ISR_NOERRNO     25; 25: Reserved
211 ISR_NOERRNO     26; 26: Reserved
212 ISR_NOERRNO     27; 27: Reserved
213 ISR_NOERRNO     28; 28: Reserved
214 ISR_NOERRNO     29; 29: Reserved
215 ISR_NOERRNO     30; 30: Reserved
216 ISR_NOERRNO     31; 31: Reserved
217
218 [extern Error_Handler]
219 [global ErrorCommon]
220 ErrorCommon:
221         PUSH_GPR
222         push gs
223         push fs
224         ;PUSH_FPU
225         ;PUSH_XMM
226         
227         mov rsi, rsp
228         call Error_Handler
229         
230         ;POP_XMM
231         ;POP_FPU
232         pop fs
233         pop gs
234         POP_GPR
235         add rsp, 2*8
236         iret
237
238 %macro DEFIRQ   1
239 Irq%1:
240         push    0
241         push    %1
242         jmp     IrqCommon
243 %endmacro
244
245 %assign i 0
246 %rep 16
247 DEFIRQ  i
248 %assign i i+1
249 %endrep
250
251 IrqCommon:
252         PUSH_GPR
253         
254         mov rbx, [rsp+16*8]     ; Calculate address
255         shr rbx, 3+2    ; *8*4
256         mov rax, gaIRQ_Handlers
257         add rbx, rax
258         
259         %assign i 0
260         %rep NUM_IRQ_CALLBACKS
261         mov rax, [rbx]
262         test rax, rax
263         mov rdi, [rsp+16*8]     ; Get IRQ number
264         jz .skip.%[i]
265         call rax        ; 2 Bytes (Op and Mod/RM)
266 .skip.%[i]:
267         add rbx, 8
268         %assign i i+1
269         %endrep
270         
271         mov rdi, [rsp+16*8]     ; Get IRQ number
272         cmp rdi, 8
273         mov al, 0x20
274         jb .skipAckSecondary
275         mov dx, 0x00A0
276         out dx, al
277 .skipAckSecondary:
278         mov dx, 0x0020
279         out dx, al
280         
281         POP_GPR
282         add rsp, 16
283         iret
284
285 [extern Proc_Scheduler]
286 SchedulerIRQ:
287         ; TODO: Find Current CPU
288         PUSH_GPR
289         ;PUSH_FPU
290         ;PUSH_XMM
291         
292         xor rsi, rsi
293         mov rdi, MM_LOCALAPIC+0x20
294         mov esi, [rdi]
295         call Proc_Scheduler
296         
297         ;POP_XMM
298         ;POP_FPU
299         POP_GPR
300         iret
301
302 [section .data]
303 gIDT:
304         times 256       dd      0x00080000, 0x00000E00, 0, 0    ; 64-bit Interrupt Gate, CS = 0x8, IST0
305 gIDTPtr:
306         dw      256*16-1
307         dq      gIDT
308
309 gaIRQ_Handlers:
310         times   16*NUM_IRQ_CALLBACKS    dq      0

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