10 %define NUM_IRQ_CALLBACKS 4
12 MM_LOCALAPIC equ 0xFFFFFD0000000000
56 ; Save to make following instructions smaller
59 ; Set an IDT entry to a callback
62 mov WORD [rdi + %1*16], ax
64 mov WORD [rdi + %1*16 + 6], ax
66 mov DWORD [rdi + %1*16 + 8], eax
68 mov ax, WORD [rdi + %1*16 + 4]
70 mov WORD [rdi + %1*16 + 4], ax
73 ; Install error handlers
85 SETIDT 0xF0, SchedulerIRQ
106 out dx, al ; Init Command
109 out dx, al ; Offset (Start of IDT Range)
111 out dx, al ; IRQ connected to Slave (00000100b) = IRQ2
113 out dx, al ; Set Mode
115 out dx, al ; Set Mode
119 out dx, al ; Init Command
122 out dx, al ; Offset (Start of IDT Range)
124 out dx, al ; IRQ Line connected to master
126 out dx, al ; Set Mode
128 out dx, al ; Set Mode
136 ; Re-install GDT (in higher address space)
139 mov QWORD [rax+2], rcx
147 ; int IRQ_AddHandler(int IRQ, void (*Handler)(int IRQ))
150 ; -1 on an invalid IRQ Number
151 ; -2 when no slots are avaliable
152 [global IRQ_AddHandler]
157 ; Check for RDI >= 16
165 ; Get handler base into RAX
167 mov rcx, gaIRQ_Handlers
170 ; Find a free callback slot
171 %rep NUM_IRQ_CALLBACKS
177 ; None found, return -2
183 ; Assign the IRQ Callback
185 ; A little bit of debug
190 mov rcx, rdi ; IRQ Number
191 mov rdx, rsi ; Callback
192 mov rsi, rax ; Pointer
193 mov rdi, csIRQ_Assigned
209 db "IRQ %p := %p (IRQ %i)",0
224 ISR_NOERRNO 0; 0: Divide By Zero Exception
225 ISR_NOERRNO 1; 1: Debug Exception
226 ISR_NOERRNO 2; 2: Non Maskable Interrupt Exception
227 ISR_NOERRNO 3; 3: Int 3 Exception
228 ISR_NOERRNO 4; 4: INTO Exception
229 ISR_NOERRNO 5; 5: Out of Bounds Exception
230 ISR_NOERRNO 6; 6: Invalid Opcode Exception
231 ISR_NOERRNO 7; 7: Coprocessor Not Available Exception
232 ISR_ERRNO 8; 8: Double Fault Exception (With Error Code!)
233 ISR_NOERRNO 9; 9: Coprocessor Segment Overrun Exception
234 ISR_ERRNO 10; 10: Bad TSS Exception (With Error Code!)
235 ISR_ERRNO 11; 11: Segment Not Present Exception (With Error Code!)
236 ISR_ERRNO 12; 12: Stack Fault Exception (With Error Code!)
237 ISR_ERRNO 13; 13: General Protection Fault Exception (With Error Code!)
238 ISR_ERRNO 14; 14: Page Fault Exception (With Error Code!)
239 ISR_NOERRNO 15; 15: Reserved Exception
240 ISR_NOERRNO 16; 16: Floating Point Exception
241 ISR_NOERRNO 17; 17: Alignment Check Exception
242 ISR_NOERRNO 18; 18: Machine Check Exception
243 ISR_NOERRNO 19; 19: Reserved
244 ISR_NOERRNO 20; 20: Reserved
245 ISR_NOERRNO 21; 21: Reserved
246 ISR_NOERRNO 22; 22: Reserved
247 ISR_NOERRNO 23; 23: Reserved
248 ISR_NOERRNO 24; 24: Reserved
249 ISR_NOERRNO 25; 25: Reserved
250 ISR_NOERRNO 26; 26: Reserved
251 ISR_NOERRNO 27; 27: Reserved
252 ISR_NOERRNO 28; 28: Reserved
253 ISR_NOERRNO 29; 29: Reserved
254 ISR_NOERRNO 30; 30: Reserved
255 ISR_NOERRNO 31; 31: Reserved
257 [extern Error_Handler]
297 mov rbx, [rsp+(16+2)*8] ; Get interrupt number (16 GPRS + 2 SRs)
298 ; xchg bx, bx ; Bochs Magic break (NOTE: will clear the high-bits of RBX)
300 mov rax, gaIRQ_Handlers
303 ; Check all callbacks
304 sub rsp, 8 ; Shadow of argument
306 %rep NUM_IRQ_CALLBACKS
307 ; Get callback address
309 test rax, rax ; Check if it exists
311 ; Set RDI to IRQ number
312 mov rdi, [rsp+(16+2+1)*8] ; Get IRQ number
322 mov rdi, [rsp+16*8] ; Get IRQ number
338 [extern Proc_Scheduler]
339 [global SchedulerIRQ]
341 ; NOTE: Proc_Scheduler makes assumptions about the stack state when called
352 ; Save Thread Pointer
361 ; Restore Thread Pointer
365 ; Send EOI (To either the APIC or the PIC)
372 out 0x20, al ; ACK IRQ
377 mov eax, DWORD [gpMP_LocalAPIC]
378 mov DWORD [eax+0x0B0], 0
387 add rsp, 2*8 ; Dummy error code and IRQ num
392 ; 64-bit Interrupt Gate, CS = 0x8, IST0 (Disabled)
393 times 256 dd 0x00080000, 0x00000E00, 0, 0
399 times 16*NUM_IRQ_CALLBACKS dq 0