diff options
Diffstat (limited to 'kc/core/cpu')
| -rw-r--r-- | kc/core/cpu/exceptions.S | 50 | ||||
| -rw-r--r-- | kc/core/cpu/interrupt.h | 70 | ||||
| -rw-r--r-- | kc/core/cpu/irq.h | 30 |
3 files changed, 109 insertions, 41 deletions
diff --git a/kc/core/cpu/exceptions.S b/kc/core/cpu/exceptions.S index 8a19c8c..087cf12 100644 --- a/kc/core/cpu/exceptions.S +++ b/kc/core/cpu/exceptions.S @@ -1,67 +1,35 @@ #include "exceptions.h" +#include "interrupt.h" .macro EXCEPTION_BEGIN vec:req, code=0 // just in case something tried to be clever before cld // push param registers - push %rdi + ISR_STORE_PARAM_REGS // store certain parameters movb $\vec,%dil - push %rsi .if \code - movq 16(%rsp), %rsi + movq ISR_PARAM_REGS_SIZE(%rsp), %rsi .else xor %rsi, %rsi .endif - push %rdx - push %rcx - push %r8 - push %r9 // caller-saved registers - push %rax - push %r10 - push %r11 + ISR_STORE_CALLER_REGS // callee-saved registers - push %rbx - push %rbp - push %r12 - push %r13 - push %r14 - push %r15 + ISR_STORE_CALLEE_REGS .endm .macro EXCEPTION_END vec:req, code=0 // restore all the registers - pop %r15 - pop %r14 - pop %r13 - pop %r12 - pop %rbp - pop %rbx - pop %r11 - pop %r10 - pop %rax - pop %r9 - pop %r8 - pop %rcx - pop %rdx - pop %rsi - pop %rdi + ISR_RESTORE_CALLEE_REGS + ISR_RESTORE_CALLER_REGS + ISR_RESTORE_PARAM_REGS .if \code add $8, %rsp .endif iretq .endm -.macro EXCEPTION_CALL func - // align the stack for the call to the handler function - mov %rsp, %rbp - and $-16, %rsp - call \func - // restore stack pointer, possibly unaligned - mov %rbp, %rsp -.endm - .macro EXCEPTION_DEFINE name:req, vec:req \name\()_isr: .if \vec > 9 && \vec < 15 || \vec == 17 || \vec == 21 || \vec > 28 && \vec < 31 @@ -69,7 +37,7 @@ .else EXCEPTION_BEGIN \vec .endif - EXCEPTION_CALL \name\()_handler + ISR_CALL \name\()_handler .if \vec > 9 && \vec < 15 || \vec == 17 || \vec == 21 || \vec > 28 && \vec < 31 EXCEPTION_END \vec, 1 .else diff --git a/kc/core/cpu/interrupt.h b/kc/core/cpu/interrupt.h new file mode 100644 index 0000000..dff2d99 --- /dev/null +++ b/kc/core/cpu/interrupt.h @@ -0,0 +1,70 @@ +#pragma once + +#ifndef __ASSEMBLER__ +#else + +.macro ISR_STORE_PARAM_REGS +push %rdi +push %rsi +push %rdx +push %rcx +push %r8 +push %r9 +.endm + +#define ISR_PARAM_REGS_SIZE 48 + +.macro ISR_STORE_CALLER_REGS +push %rax +push %r10 +push %r11 +.endm + +#define ISR_CALLER_REGS_SIZE 24 + +.macro ISR_STORE_CALLEE_REGS +push %rbx +push %rbp +push %r12 +push %r13 +push %r14 +push %r15 +.endm + +#define ISR_CALLEE_REGS_SIZE 48 + +.macro ISR_RESTORE_PARAM_REGS +pop %r9 +pop %r8 +pop %rcx +pop %rdx +pop %rsi +pop %rdi +.endm + +.macro ISR_RESTORE_CALLER_REGS +pop %r11 +pop %r10 +pop %rax +.endm + +.macro ISR_RESTORE_CALLEE_REGS +pop %r15 +pop %r14 +pop %r13 +pop %r12 +pop %rbp +pop %rbx +.endm + +.macro ISR_CALL func + // align the stack for the call to the handler function + mov %rsp, %rbp + and $-16, %rsp + call \func + // restore stack pointer, possibly unaligned + mov %rbp, %rsp +.endm + +#endif + diff --git a/kc/core/cpu/irq.h b/kc/core/cpu/irq.h new file mode 100644 index 0000000..9874761 --- /dev/null +++ b/kc/core/cpu/irq.h @@ -0,0 +1,30 @@ +#pragma once + +#include "interrupt.h" + +#ifndef __ASSEMBLER__ +#else + +.macro IRQ_DECLARE name:req handler:req, irq:req, offset:req +.hidden \name\()_irq\()\irq\()_isr +.global \name\()_irq\()\irq\()_isr +.extern \handler +.type \name\()_irq\()\irq\()_isr, @function +.endm + +.macro IRQ_DEFINE name:req handler:req, irq:req +\name\()_irq\()\irq\()_isr: + ISR_STORE_PARAM_REGS + movb \irq, %dil + ISR_STORE_CALLER_REGS + ISR_STORE_CALLEE_REGS + ISR_CALL \handler + ISR_RESTORE_CALLEE_REGS + ISR_RESTORE_CALLER_REGS + ISR_RESTORE_PARAM_REGS + iretq +.size \name\()_irq\()\irq\()_isr, . - \name\()_irq\()\irq\()_isr +.endm + +#endif + |
