hyperlight_guest/exceptions/
interrupt_entry.rs

1/*
2Copyright 2024 The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17// Note: this code takes reference from
18// https://github.com/nanvix/nanvix/blob/dev/src/kernel/src/hal/arch/x86/hooks.S
19
20use core::arch::global_asm;
21
22use crate::exceptions::handlers::hl_exception_handler;
23
24extern "C" {
25    // Exception handlers
26    pub(crate) fn _do_excp0();
27    pub(crate) fn _do_excp1();
28    pub(crate) fn _do_excp2();
29    pub(crate) fn _do_excp3();
30    pub(crate) fn _do_excp4();
31    pub(crate) fn _do_excp5();
32    pub(crate) fn _do_excp6();
33    pub(crate) fn _do_excp7();
34    pub(crate) fn _do_excp8();
35    pub(crate) fn _do_excp9();
36    pub(crate) fn _do_excp10();
37    pub(crate) fn _do_excp11();
38    pub(crate) fn _do_excp12();
39    pub(crate) fn _do_excp13();
40    pub(crate) fn _do_excp14();
41    pub(crate) fn _do_excp15();
42    pub(crate) fn _do_excp16();
43    pub(crate) fn _do_excp17();
44    pub(crate) fn _do_excp18();
45    pub(crate) fn _do_excp19();
46    pub(crate) fn _do_excp20();
47    pub(crate) fn _do_excp30();
48}
49
50// Defines `context_save` and `context_restore`
51macro_rules! context_save {
52    () => {
53        concat!(
54            // Save general-purpose registers
55            "    push rax\n",
56            "    push rbx\n",
57            "    push rcx\n",
58            "    push rdx\n",
59            "    push rsi\n",
60            "    push rdi\n",
61            "    push rbp\n",
62            "    push r8\n",
63            "    push r9\n",
64            "    push r10\n",
65            "    push r11\n",
66            "    push r12\n",
67            "    push r13\n",
68            "    push r14\n",
69            "    push r15\n",
70            // Save segment registers
71            "    mov rax, ds\n",
72            "    push rax\n",
73            "    mov rax, es\n",
74            "    push rax\n",
75            "    mov rax, fs\n",
76            "    push rax\n",
77            "    mov rax, gs\n",
78            "    push rax\n",
79        )
80    };
81}
82
83macro_rules! context_restore {
84    () => {
85        concat!(
86            // Restore segment registers
87            "    pop rax\n",
88            "    mov gs, rax\n",
89            "    pop rax\n",
90            "    mov fs, rax\n",
91            "    pop rax\n",
92            "    mov es, rax\n",
93            "    pop rax\n",
94            "    mov ds, rax\n",
95            // Restore general-purpose registers
96            "    pop r15\n",
97            "    pop r14\n",
98            "    pop r13\n",
99            "    pop r12\n",
100            "    pop r11\n",
101            "    pop r10\n",
102            "    pop r9\n",
103            "    pop r8\n",
104            "    pop rbp\n",
105            "    pop rdi\n",
106            "    pop rsi\n",
107            "    pop rdx\n",
108            "    pop rcx\n",
109            "    pop rbx\n",
110            "    pop rax\n",
111        )
112    };
113}
114
115// Generates exception handlers
116macro_rules! generate_exceptions {
117    () => {
118        concat!(
119            // Common exception handler
120            ".global _do_excp_common\n",
121            "_do_excp_common:\n",
122            // rdi is the stack pointer.
123            "    mov rdi, rsp\n",
124            "    call {hl_exception_handler}\n",
125            context_restore!(),
126            "    iretq\n", // iretq is used to return from exception in x86_64
127            generate_excp!(0, pusherrcode),
128            generate_excp!(1, pusherrcode),
129            generate_excp!(2, pusherrcode),
130            generate_excp!(3, pusherrcode),
131            generate_excp!(4, pusherrcode),
132            generate_excp!(5, pusherrcode),
133            generate_excp!(6, pusherrcode),
134            generate_excp!(7, pusherrcode),
135            generate_excp!(8),
136            generate_excp!(9, pusherrcode),
137            generate_excp!(10),
138            generate_excp!(11),
139            generate_excp!(12),
140            generate_excp!(13),
141            generate_excp!(14, pagefault),
142            generate_excp!(15, pusherrcode),
143            generate_excp!(16, pusherrcode),
144            generate_excp!(17),
145            generate_excp!(18, pusherrcode),
146            generate_excp!(19, pusherrcode),
147            generate_excp!(20, pusherrcode),
148            generate_excp!(30),
149        )
150    };
151}
152
153// Macro to generate exception handlers
154// that satisfy the `extern`s at the top of the file.
155//
156// - Example output from this macro for generate_excp!(0) call:
157// ```assembly
158// .global _do_excp0
159// _do_excp0:
160//     context_save!()
161//     mov rsi, 0
162//     mov rdx, 0
163//     jmp _do_excp_common
164// ```
165macro_rules! generate_excp {
166    ($num:expr) => {
167        concat!(
168            ".global _do_excp",
169            stringify!($num),
170            "\n",
171            "_do_excp",
172            stringify!($num),
173            ":\n",
174            context_save!(),
175            // rsi is the exception number.
176            "    mov rsi, ",
177            stringify!($num),
178            "\n",
179            // rdx is only used for pagefault exception and
180            // contains the address that caused the pagefault.
181            "    mov rdx, 0\n",
182            "    jmp _do_excp_common\n"
183        )
184    };
185    ($num:expr, pusherrcode) => {
186        concat!(
187            ".global _do_excp",
188            stringify!($num),
189            "\n",
190            "_do_excp",
191            stringify!($num),
192            ":\n",
193            // Some exceptions push an error code onto the stack.
194            // For the ones that don't, we push a 0 to keep the
195            // stack aligned.
196            "   push 0\n",
197            context_save!(),
198            // rsi is the exception number.
199            "    mov rsi, ",
200            stringify!($num),
201            "\n",
202            // rdx is only used for pagefault exception and
203            // contains the address that caused the pagefault.
204            "    mov rdx, 0\n",
205            "    jmp _do_excp_common\n"
206        )
207    };
208    ($num:expr, pagefault) => {
209        concat!(
210            ".global _do_excp",
211            stringify!($num),
212            "\n",
213            "_do_excp",
214            stringify!($num),
215            ":\n",
216            context_save!(),
217            "    mov rsi, ",
218            stringify!($num),
219            "\n",
220            // In a page fault exception, the cr2 register
221            // contains the address that caused the page fault.
222            "    mov rdx, cr2\n",
223            "    jmp _do_excp_common\n"
224        )
225    };
226}
227
228// Output the assembly code
229global_asm!(
230    generate_exceptions!(),
231    hl_exception_handler = sym hl_exception_handler,
232);