4 %include "arch/x86_64/include/common.inc.asm"
11 %define NUM_IRQ_CALLBACKS 4
13 MM_LOCALAPIC equ 0xFFFFFD0000000000
18 ; Save to make following instructions smaller
21 ; Set an IDT entry to a callback
24 mov WORD [rdi + %1*16], ax
26 mov WORD [rdi + %1*16 + 6], ax
28 mov DWORD [rdi + %1*16 + 8], eax
30 mov ax, WORD [rdi + %1*16 + 4]
32 mov WORD [rdi + %1*16 + 4], ax
35 ; Install error handlers
68 out dx, al ; Init Command
71 out dx, al ; Offset (Start of IDT Range)
73 out dx, al ; IRQ connected to Slave (00000100b) = IRQ2
81 out dx, al ; Init Command
84 out dx, al ; Offset (Start of IDT Range)
86 out dx, al ; IRQ Line connected to master
98 ; Re-install GDT (in higher address space)
101 mov QWORD [rax+2], rcx
107 ; Initialise System Calls (SYSCALL/SYSRET)
113 ; Set IA32_LSTAR (RIP of handler)
114 mov ecx, 0xC0000082 ; IA32_LSTAR
115 mov eax, SyscallStub - 0xFFFFFFFF00000000
118 ; Set IA32_FMASK (flags mask)
123 ; Set IA32_STAR (Kernel/User CS)
126 mov edx, 0x8 | (0x1B << 16) ; Kernel CS (and Kernel DS/SS - 8), User CS
131 ; int IRQ_AddHandler(int IRQ, void (*Handler)(int IRQ), void *Ptr)
134 ; -1 on an invalid IRQ Number
135 ; -2 when no slots are avaliable
136 [global IRQ_AddHandler]
142 ; Check for RDI >= 16
150 ; Get handler base into RAX
152 mov rcx, gaIRQ_Handlers
155 ; Find a free callback slot
156 %rep NUM_IRQ_CALLBACKS
162 ; None found, return -2
168 ; Assign the IRQ Callback
170 ; A little bit of debug
176 mov rcx, rdi ; IRQ Number
177 mov rdx, rsi ; Callback
178 mov rsi, rax ; Pointer
179 mov rdi, csIRQ_Assigned
189 add rax, gaIRQ_DataPtrs - gaIRQ_Handlers
198 db "IRQ %p := %p (IRQ %i)",0
215 ISR_NOERRNO 0; 0: Divide By Zero Exception
216 ISR_NOERRNO 1; 1: Debug Exception
217 ISR_NOERRNO 2; 2: Non Maskable Interrupt Exception
218 ISR_NOERRNO 3; 3: Int 3 Exception
219 ISR_NOERRNO 4; 4: INTO Exception
220 ISR_NOERRNO 5; 5: Out of Bounds Exception
221 ISR_NOERRNO 6; 6: Invalid Opcode Exception
222 ISR_NOERRNO 7; 7: Coprocessor Not Available Exception
223 ISR_ERRNO 8; 8: Double Fault Exception (With Error Code!)
224 ISR_NOERRNO 9; 9: Coprocessor Segment Overrun Exception
225 ISR_ERRNO 10; 10: Bad TSS Exception (With Error Code!)
226 ISR_ERRNO 11; 11: Segment Not Present Exception (With Error Code!)
227 ISR_ERRNO 12; 12: Stack Fault Exception (With Error Code!)
228 ISR_ERRNO 13; 13: General Protection Fault Exception (With Error Code!)
229 ISR_ERRNO 14; 14: Page Fault Exception (With Error Code!)
230 ISR_NOERRNO 15; 15: Reserved Exception
231 ISR_NOERRNO 16; 16: Floating Point Exception
232 ISR_NOERRNO 17; 17: Alignment Check Exception
233 ISR_NOERRNO 18; 18: Machine Check Exception
234 ISR_NOERRNO 19; 19: Reserved
235 ISR_NOERRNO 20; 20: Reserved
236 ISR_NOERRNO 21; 21: Reserved
237 ISR_NOERRNO 22; 22: Reserved
238 ISR_NOERRNO 23; 23: Reserved
239 ISR_NOERRNO 24; 24: Reserved
240 ISR_NOERRNO 25; 25: Reserved
241 ISR_NOERRNO 26; 26: Reserved
242 ISR_NOERRNO 27; 27: Reserved
243 ISR_NOERRNO 28; 28: Reserved
244 ISR_NOERRNO 29; 29: Reserved
245 ISR_NOERRNO 30; 30: Reserved
246 ISR_NOERRNO 31; 31: Reserved
248 [extern Error_Handler]
288 ; mov rdi, csIRQ_Fired
289 ; mov rsi, [rsp+(16+2)*8]
292 mov ebx, [rsp+(16+2)*8] ; Get interrupt number (16 GPRS + 2 SRs)
294 mov rax, gaIRQ_Handlers
297 ; Check all callbacks
298 sub rsp, 8 ; Shadow of argument
300 %rep NUM_IRQ_CALLBACKS
301 ; Get callback address
303 test rax, rax ; Check if it exists
305 ; Set RDI to IRQ number
306 mov rdi, [rsp+(16+2+1)*8] ; Get IRQ number
307 mov rsi, [rbx-gaIRQ_Handlers+gaIRQ_DataPtrs]
317 mov rdi, [rsp+(16+2)*8] ; Get IRQ number
330 [extern Time_UpdateTimestamp]
333 [global APIC_Timer_IRQ]
341 mov eax, DWORD [gpMP_LocalAPIC]
342 mov DWORD [eax+0x0B0], 0
356 call Time_UpdateTimestamp
360 csUserSS: db "User SS: 0x%x",0
363 mov rsi, [rsp+0x80+0x20]
369 out 0x20, al ; ACK IRQ
376 [extern ci_offsetof_tThread_KernelStack]
377 [extern SyscallHandler]
381 mov ebx, [rel ci_offsetof_tThread_KernelStack]
382 mov rbp, [rbp+rbx] ; Get kernel stack
383 xchg rbp, rsp ; Swap stacks
385 push rbp ; Save User RSP
392 ; R10 (RCX for non syscall)
396 mov [rsp+0x00], rax ; Number
397 ; mov [rsp+0x08], rax ; Errno (don't care really)
398 mov [rsp+0x10], rdi ; Arg1
399 mov [rsp+0x18], rsi ; Arg2
400 mov [rsp+0x20], rdx ; Arg3
401 mov [rsp+0x28], r10 ; Arg4
402 mov [rsp+0x30], r8 ; Arg5
403 mov [rsp+0x38], r9 ; Arg6
411 csSyscallReturn: db "Syscall Return: 0x%x",0
413 mov rdi, csSyscallReturn
419 mov ebx, [rsp+8] ; Get errno
420 mov rax, [rsp+0] ; Get return
425 pop rsp ; Change back to user stack
426 ; TODO: Determine if user is 64 or 32 bit
428 db 0x48 ; REX, nasm doesn't have a sysretq opcode
433 ; 64-bit Interrupt Gate, CS = 0x8, IST0 (Disabled)
434 times 256 dd 0x00080000, 0x00000E00, 0, 0
440 times 16*NUM_IRQ_CALLBACKS dq 0
442 times 16*NUM_IRQ_CALLBACKS dq 0