()
| 175 | /// |
| 176 | #[naked] |
| 177 | extern "C" fn handle_syscall() { |
| 178 | unsafe { |
| 179 | asm!( |
| 180 | // Here we switch stack to avoid messing with user stack |
| 181 | // swapgs is a way to do this |
| 182 | // - <https://github.com/redox-os/kernel/blob/master/src/arch/x86_64/interrupt/syscall.rs#L65> |
| 183 | // - <https://www.felixcloutier.com/x86/swapgs> |
| 184 | |
| 185 | "swapgs", // Put the TSS address into GS (stored in syscalls::init) |
| 186 | "mov gs:{tss_temp}, rsp", // Save user stack pointer in TSS entry |
| 187 | |
| 188 | "mov rsp, gs:{tss_timer}", // Get kernel stack pointer |
| 189 | "sub rsp, {ks_offset}", // Use a different location than timer interrupt |
| 190 | |
| 191 | // Create an Exception stack frame |
| 192 | "sub rsp, 8", // To be replaced with SS |
| 193 | "push gs:{tss_temp}", // User stack pointer |
| 194 | "swapgs", // Put TSS address back |
| 195 | |
| 196 | // Could re-enable interrupts here? |
| 197 | |
| 198 | "push r11", // Caller's RFLAGS |
| 199 | "sub rsp, 8", // CS |
| 200 | "push rcx", // Caller's RIP |
| 201 | |
| 202 | // Create the remainder of the Context struct |
| 203 | "push rax", |
| 204 | "push rbx", |
| 205 | "push rcx", |
| 206 | "push rdx", |
| 207 | |
| 208 | "push rdi", |
| 209 | "push rsi", |
| 210 | "push rbp", |
| 211 | "push r8", |
| 212 | |
| 213 | "push r9", |
| 214 | "push r10", |
| 215 | "push r11", |
| 216 | "push r12", |
| 217 | |
| 218 | "push r13", |
| 219 | "push r14", |
| 220 | "push r15", |
| 221 | |
| 222 | // Call the rust dispatch_syscall function |
| 223 | // C calling convention so arguments are in registers |
| 224 | // RDI, RSI, RDX, RCX, R8, R9 |
| 225 | "mov r8, rdx", // Fifth argument <- Syscall third argument |
| 226 | "mov rcx, rsi", // Fourth argument <- Syscall second argument |
| 227 | "mov rdx, rdi", // Third argument <- Syscall first argument |
| 228 | "mov rsi, rax", // Second argument is the syscall number |
| 229 | "mov rdi, rsp", // First argument is the Context address |
| 230 | "call {dispatch_fn}", |
| 231 | |
| 232 | "pop r15", // restore callee-saved registers |
| 233 | "pop r14", |
| 234 | "pop r13", |
nothing calls this directly
no outgoing calls
no test coverage detected