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]
166 mov rcx, gaIRQ_Handlers
169 ; Find a free callback slot
170 %rep NUM_IRQ_CALLBACKS
176 ; None found, return -2
182 ; Assign the IRQ Callback
184 ; A little bit of debug
189 mov rcx, rdi ; IRQ Number
190 mov rdx, rsi ; Callback
191 mov rsi, rax ; Pointer
192 mov rdi, csIRQ_Assigned
207 db "IRQ %p := %p (IRQ %i)",0
222 ISR_NOERRNO 0; 0: Divide By Zero Exception
223 ISR_NOERRNO 1; 1: Debug Exception
224 ISR_NOERRNO 2; 2: Non Maskable Interrupt Exception
225 ISR_NOERRNO 3; 3: Int 3 Exception
226 ISR_NOERRNO 4; 4: INTO Exception
227 ISR_NOERRNO 5; 5: Out of Bounds Exception
228 ISR_NOERRNO 6; 6: Invalid Opcode Exception
229 ISR_NOERRNO 7; 7: Coprocessor Not Available Exception
230 ISR_ERRNO 8; 8: Double Fault Exception (With Error Code!)
231 ISR_NOERRNO 9; 9: Coprocessor Segment Overrun Exception
232 ISR_ERRNO 10; 10: Bad TSS Exception (With Error Code!)
233 ISR_ERRNO 11; 11: Segment Not Present Exception (With Error Code!)
234 ISR_ERRNO 12; 12: Stack Fault Exception (With Error Code!)
235 ISR_ERRNO 13; 13: General Protection Fault Exception (With Error Code!)
236 ISR_ERRNO 14; 14: Page Fault Exception (With Error Code!)
237 ISR_NOERRNO 15; 15: Reserved Exception
238 ISR_NOERRNO 16; 16: Floating Point Exception
239 ISR_NOERRNO 17; 17: Alignment Check Exception
240 ISR_NOERRNO 18; 18: Machine Check Exception
241 ISR_NOERRNO 19; 19: Reserved
242 ISR_NOERRNO 20; 20: Reserved
243 ISR_NOERRNO 21; 21: Reserved
244 ISR_NOERRNO 22; 22: Reserved
245 ISR_NOERRNO 23; 23: Reserved
246 ISR_NOERRNO 24; 24: Reserved
247 ISR_NOERRNO 25; 25: Reserved
248 ISR_NOERRNO 26; 26: Reserved
249 ISR_NOERRNO 27; 27: Reserved
250 ISR_NOERRNO 28; 28: Reserved
251 ISR_NOERRNO 29; 29: Reserved
252 ISR_NOERRNO 30; 30: Reserved
253 ISR_NOERRNO 31; 31: Reserved
255 [extern Error_Handler]
295 mov rbx, [rsp+(16+2)*8] ; Calculate address
297 mov rax, gaIRQ_Handlers
300 ; Check all callbacks
302 %rep NUM_IRQ_CALLBACKS
303 ; Get callback address
305 test rax, rax ; Check if it exists
307 ; Set RDI to IRQ number
308 mov rdi, [rsp+16*8] ; Get IRQ number
316 mov rdi, [rsp+16*8] ; Get IRQ number
333 [extern Proc_Scheduler]
334 [global SchedulerIRQ]
336 ; NOTE: Proc_Scheduler makes assumptions about the stack state when called
347 ; Save Thread Pointer
356 ; Restore Thread Pointer
360 ; Send EOI (To either the APIC or the PIC)
367 out 0x20, al ; ACK IRQ
372 mov eax, DWORD [gpMP_LocalAPIC]
373 mov DWORD [eax+0x0B0], 0
382 add rsp, 2*8 ; Dummy error code and IRQ num
387 ; 64-bit Interrupt Gate, CS = 0x8, IST0 (Disabled)
388 times 256 dd 0x00080000, 0x00000E00, 0, 0
394 times 16*NUM_IRQ_CALLBACKS dq 0