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