168 lines
2.8 KiB
NASM
168 lines
2.8 KiB
NASM
|
[BITS 32]
|
||
|
[GLOBAL isr_stub_table]
|
||
|
[GLOBAL irq_stub_table]
|
||
|
extern irq_handler ; C function
|
||
|
|
||
|
section .text
|
||
|
|
||
|
; --------------------------------------------
|
||
|
; ------------------- IRQs -------------------
|
||
|
; --------------------------------------------
|
||
|
|
||
|
%macro irq_stub 1
|
||
|
irq_stub_%+%1:
|
||
|
pusha
|
||
|
push ds
|
||
|
push es
|
||
|
push fs
|
||
|
push gs
|
||
|
|
||
|
mov ax, 0x10
|
||
|
mov ds, ax
|
||
|
mov es, ax
|
||
|
mov fs, ax
|
||
|
mov gs, ax
|
||
|
|
||
|
push dword %1 ; Push IRQ number
|
||
|
call irq_handler
|
||
|
add esp, 4 ; Clean up stack
|
||
|
|
||
|
; Send EOI
|
||
|
mov al, 0x20
|
||
|
mov bl, %1
|
||
|
cmp bl, 8
|
||
|
jb .skip_slave_eoi
|
||
|
out 0xA0, al
|
||
|
.skip_slave_eoi:
|
||
|
out 0x20, al
|
||
|
|
||
|
pop gs
|
||
|
pop fs
|
||
|
pop es
|
||
|
pop ds
|
||
|
popa
|
||
|
iret
|
||
|
%endmacro
|
||
|
|
||
|
|
||
|
; Define 16 IRQ stubs
|
||
|
irq_stub 0
|
||
|
irq_stub 1
|
||
|
irq_stub 2
|
||
|
irq_stub 3
|
||
|
irq_stub 4
|
||
|
irq_stub 5
|
||
|
irq_stub 6
|
||
|
irq_stub 7
|
||
|
irq_stub 8
|
||
|
irq_stub 9
|
||
|
irq_stub 10
|
||
|
irq_stub 11
|
||
|
irq_stub 12
|
||
|
irq_stub 13
|
||
|
irq_stub 14
|
||
|
irq_stub 15
|
||
|
|
||
|
irq_stub_table:
|
||
|
%assign i 0
|
||
|
%rep 16
|
||
|
dd irq_stub_%+i
|
||
|
%assign i i+1
|
||
|
%endrep
|
||
|
|
||
|
; --------------------------------------------
|
||
|
; ------------------- ISRs -------------------
|
||
|
; --------------------------------------------
|
||
|
|
||
|
; Common exception handler entry point
|
||
|
isr_common_handler:
|
||
|
pusha ; Push general-purpose registers
|
||
|
push ds
|
||
|
push es
|
||
|
push fs
|
||
|
push gs
|
||
|
|
||
|
; Load known-good segment selectors
|
||
|
mov ax, 0x10
|
||
|
mov ds, ax
|
||
|
mov es, ax
|
||
|
mov fs, ax
|
||
|
mov gs, ax
|
||
|
|
||
|
; Print exception number and error code
|
||
|
; You can insert direct port writes or call kernel logging function here
|
||
|
; For now, just hang
|
||
|
.halt:
|
||
|
cli
|
||
|
hlt
|
||
|
jmp .halt
|
||
|
|
||
|
; If you want to later restore:
|
||
|
pop gs
|
||
|
pop fs
|
||
|
pop es
|
||
|
pop ds
|
||
|
popa
|
||
|
add esp, 8 ; clean up int_no + err_code
|
||
|
iret
|
||
|
|
||
|
; Macro: for exceptions with error code
|
||
|
%macro isr_err_stub 1
|
||
|
isr_stub_%+%1:
|
||
|
cli
|
||
|
push dword %1 ; interrupt number
|
||
|
jmp isr_common_handler
|
||
|
%endmacro
|
||
|
|
||
|
; Macro: for exceptions without error code
|
||
|
%macro isr_no_err_stub 1
|
||
|
isr_stub_%+%1:
|
||
|
cli
|
||
|
push dword 0 ; fake error code
|
||
|
push dword %1 ; interrupt number
|
||
|
jmp isr_common_handler
|
||
|
%endmacro
|
||
|
|
||
|
; Define all 32 exception stubs
|
||
|
isr_no_err_stub 0
|
||
|
isr_no_err_stub 1
|
||
|
isr_no_err_stub 2
|
||
|
isr_no_err_stub 3
|
||
|
isr_no_err_stub 4
|
||
|
isr_no_err_stub 5
|
||
|
isr_no_err_stub 6
|
||
|
isr_no_err_stub 7
|
||
|
isr_err_stub 8
|
||
|
isr_no_err_stub 9
|
||
|
isr_err_stub 10
|
||
|
isr_err_stub 11
|
||
|
isr_err_stub 12
|
||
|
isr_err_stub 13
|
||
|
isr_err_stub 14
|
||
|
isr_no_err_stub 15
|
||
|
isr_no_err_stub 16
|
||
|
isr_err_stub 17
|
||
|
isr_no_err_stub 18
|
||
|
isr_no_err_stub 19
|
||
|
isr_no_err_stub 20
|
||
|
isr_no_err_stub 21
|
||
|
isr_no_err_stub 22
|
||
|
isr_no_err_stub 23
|
||
|
isr_no_err_stub 24
|
||
|
isr_no_err_stub 25
|
||
|
isr_no_err_stub 26
|
||
|
isr_no_err_stub 27
|
||
|
isr_no_err_stub 28
|
||
|
isr_no_err_stub 29
|
||
|
isr_err_stub 30
|
||
|
isr_no_err_stub 31
|
||
|
|
||
|
; Create jump table
|
||
|
isr_stub_table:
|
||
|
%assign i 0
|
||
|
%rep 32
|
||
|
dd isr_stub_%+i
|
||
|
%assign i i+1
|
||
|
%endrep
|
||
|
|