Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
isr.asm
Go to the documentation of this file.
1%macro pushall 0
2 push rax
3 push rbx
4 push rcx
5 push rdx
6 push rsi
7 push rdi
8 push rbp
9 push r8
10 push r9
11 push r10
12 push r11
13 push r12
14 push r13
15 push r14
16 push r15
17%endmacro
18
19%macro popall 0
20 pop r15
21 pop r14
22 pop r13
23 pop r12
24 pop r11
25 pop r10
26 pop r9
27 pop r8
28 pop rbp
29 pop rdi
30 pop rsi
31 pop rdx
32 pop rcx
33 pop rbx
34 pop rax
35%endmacro
36
37extern vxInterruptHandler
38
39; Stack layout at int_common entry:
40; [rsp+ 0] = error code (pushed by isr macro or CPU)
41; [rsp+ 8] = int number (pushed by isr macro)
42; [rsp+16] = RIP (pushed by CPU)
43; [rsp+24] = CS (pushed by CPU) <-- ring check
44; [rsp+32] = RFLAGS
45; [rsp+40] = RSP (only on privilege change)
46; [rsp+48] = SS (only on privilege change)
47
48int_common:
49 ; CS is at [rsp+24] before pushall
50 test qword [rsp+24], 3
51 jz .skip_swapgs_entry
52 swapgs
53.skip_swapgs_entry:
54
55 pushall
56 ; After pushall: 15 regs * 8 = 120 bytes pushed
57 ; CS is now at [rsp + 120 + 24] = [rsp+144]
58
59 mov rbx, rsp ; rbx = pointer to saved-regs frame (interrupt_stack_frame_t)
60
61 ; Disable x87 EM + TS so fxsave doesn't #NM
62 mov rax, cr0
63 and rax, ~((1 << 2) | (1 << 3))
64 mov cr0, rax
65
66 ; Allocate 512-byte fxsave buffer, 64-byte aligned (safe for fxsave/xsave)
67 sub rsp, 512
68 and rsp, -64
69 fxsave [rsp]
70
71 mov rdi, rbx ; arg1: interrupt_stack_frame_t*
72 mov rsi, rsp ; arg2: fxsave area* (optional, can be ignored in handler)
73 call vxInterruptHandler
74
75 fxrstor [rsp] ; restore FPU — rsp unchanged since fxsave
76
77 mov rsp, rbx ; restore to post-pushall stack position
78
79 popall
80
81 ; CS is at [rsp+24] again (same layout as entry, before pushall)
82 test qword [rsp+24], 3
83 jz .skip_swapgs_exit
84 swapgs
85.skip_swapgs_exit:
86 add rsp, 16 ; discard error code + int number
87 iretq
88
89
90%macro isr 1
91isr_%1:
92%if !(%1 == 8 || (%1 >= 10 && %1 <= 14) || %1 == 17 || %1 == 21 || %1 == 29 || %1 == 30)
93 push 0
94%endif
95 push %1
96 jmp int_common
97%endmacro
98
99%assign i 0
100%rep 256
101 isr i
102%assign i i+1
103%endrep
104
105global int_table
106section .data
107int_table:
108%assign i 0
109%rep 256
110 dq isr_%+i
111%assign i i+1
112%endrep