Skip to content

Instantly share code, notes, and snippets.

@gsuberland
Created April 10, 2021 21:49
Show Gist options
  • Save gsuberland/88d4eaf1f35190a283b5276763f5a010 to your computer and use it in GitHub Desktop.
Save gsuberland/88d4eaf1f35190a283b5276763f5a010 to your computer and use it in GitHub Desktop.
ISR auto-generation with default handlers, in GCC AS
.intel_syntax noprefix
.altmacro
.section .data
.align 4
.globl KernelInterruptContext
KernelInterruptContext:
.long 0 // eax
.long 0 // ebx
.long 0 // ecx
.long 0 // edx
.long 0 // esi
.long 0 // edi
.long 0 // esp
.long 0 // ebp
.word 0 // cs
.word 0 // ds
.word 0 // es
.word 0 // fs
.word 0 // gs
.word 0 // ss
.byte 0 // interrupt number
.section .text
.align 4
/*.globl dispatch_default_interrupt_handler
.globl dispatch_exception_div0_handler
.type dispatch_default_interrupt_handler, @function
.type dispatch_exception_div0_handler, @function*/
capture_KernelInterruptContext:
mov dword ptr [KernelInterruptContext+0x00], eax
mov dword ptr [KernelInterruptContext+0x04], ebx
mov dword ptr [KernelInterruptContext+0x08], ecx
mov dword ptr [KernelInterruptContext+0x0c], edx
mov dword ptr [KernelInterruptContext+0x10], esi
mov dword ptr [KernelInterruptContext+0x14], edi
mov dword ptr [KernelInterruptContext+0x18], esp
mov dword ptr [KernelInterruptContext+0x1c], ebp
mov word ptr [KernelInterruptContext+0x20], cs
mov word ptr [KernelInterruptContext+0x22], ds
mov word ptr [KernelInterruptContext+0x24], es
mov word ptr [KernelInterruptContext+0x26], fs
mov word ptr [KernelInterruptContext+0x28], gs
mov word ptr [KernelInterruptContext+0x2a], ss
mov eax, [esp + 4]
mov byte ptr [KernelInterruptContext+0x2c], al
mov eax, offset KernelInterruptContext
ret 4
/* define ISR_HANDLER_# as the C function to be called for that particular ISR */
ISR_HANDLER_0=exception_div0_handler
dispatch_interrupt_BEGIN:
/* macro for generating ISR dispatchers */
.macro make_isr_dispatch number
.globl dispatch_interrupt_\number
.type dispatch_interrupt_\number, @function
dispatch_interrupt_\number:
push \number
call capture_KernelInterruptContext
.ifdef ISR_HANDLER_\number
jmp ISR_HANDLER_\number
.else
jmp default_interrupt_handler
.endif
.endm
/* loop to generate all 256 ISR dispatchers */
.set i,0
.rept 256
make_isr_dispatch %i
.set i, i+1
.endr
dispatch_interrupt_END:
.section .data
.align 4
/* macro for generating ISR table entries */
.macro make_isr_dispatch_ptr number
.long dispatch_interrupt_\number
.endm
/* loop to generate all 256 ISR table entries */
.globl isr_handlers_table
isr_handlers_table:
.set i,0
.rept 256
make_isr_dispatch_ptr %i
.set i, i+1
.endr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment