interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / x86 / trap.S

105 lines · 2639 bytes Raw Blame History
1/* trap.S -- x86_64 descriptor-table loads, the 256 interrupt stubs and the
2 * stack switch for AIENOS-KERNEL-0 (native/kernel/x86). The C side is
3 * compiled -mabi=ms: arguments in rcx, rdx, r8, r9, 32 bytes of shadow space
4 * reserved by the caller, rax/rcx/rdx/r8-r11 caller-saved. */
5
6 .text
7
8/* void ck_x86_load_gdt(const void *gdtr, u16 code_sel, u16 data_sel, u16 tss_sel)
9 * Loads the GDT, reloads CS with a far return and the data segments, then
10 * the task register. */
11 .globl ck_x86_load_gdt
12ck_x86_load_gdt:
13 lgdt (%rcx)
14 xor %eax, %eax
15 lldt %ax /* no firmware LDT/call gates survive */
16 mov %r8w, %ds
17 mov %r8w, %es
18 mov %r8w, %ss
19 mov %r8w, %fs
20 mov %r8w, %gs
21 movzwq %dx, %rdx
22 lea 1f(%rip), %rax
23 push %rdx
24 push %rax
25 lretq
261: test %r9w, %r9w /* tss_sel 0: no task register (secondary cores) */
27 jz 2f
28 ltr %r9w
292: ret
30
31/* noreturn void ck_x86_call_on_stack(void (*fn)(void *), void *arg, u64 sp)
32 * sp is 16-byte aligned; the call leaves the callee at sp - 40 = 8 mod 16,
33 * as a normal call would. */
34 .globl ck_x86_call_on_stack
35ck_x86_call_on_stack:
36 mov %r8, %rsp
37 sub $32, %rsp
38 mov %rcx, %rax
39 mov %rdx, %rcx
40 call *%rax
41 ud2
42
43/* 256 stubs, each at ck_x86_isr_stubs + 16 * vector. Vectors that push an
44 * error code (8, 10-14, 17, 21, 29, 30) skip the dummy push. */
45 .section .text.isr, "ax"
46 .globl ck_x86_isr_stubs
47 .balign 16
48ck_x86_isr_stubs:
49 .set vec, 0
50 .rept 256
51 .org ck_x86_isr_stubs + vec * 16
52 .if (vec == 8) || ((vec >= 10) && (vec <= 14)) || (vec == 17) || (vec == 21) || (vec == 29) || (vec == 30)
53 .else
54 pushq $0
55 .endif
56 pushq $vec
57 jmp isr_common
58 .set vec, vec + 1
59 .endr
60 .org ck_x86_isr_stubs + 256 * 16
61
62isr_common:
63 push %rax
64 push %rbx
65 push %rcx
66 push %rdx
67 push %rsi
68 push %rdi
69 push %rbp
70 push %r8
71 push %r9
72 push %r10
73 push %r11
74 push %r12
75 push %r13
76 push %r14
77 push %r15
78 cld
79 mov %rsp, %rcx /* struct ck_x86_frame * */
80 testb $3, 144(%rsp) /* interrupted CS: only CPL3 takes the tool exit */
81 jnz ck_local_user_exit
82 mov %rsp, %rbx /* callee-saved under both ABIs */
83 and $-16, %rsp
84 sub $32, %rsp
85 call ck_x86_trap
86 mov %rbx, %rsp
87 pop %r15
88 pop %r14
89 pop %r13
90 pop %r12
91 pop %r11
92 pop %r10
93 pop %r9
94 pop %r8
95 pop %rbp
96 pop %rdi
97 pop %rsi
98 pop %rdx
99 pop %rcx
100 pop %rbx
101 pop %rax
102 add $16, %rsp /* vector, error code */
103 iretq
104
105 .section .note.GNU-stack, "", @progbits