Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub(super) unsafe fn switch_to(registers: *const Registers) {
- let old_kernel_rsp = rsp();
- unsafe {
- asm!(
- "
- push rbx
- push rbp
- push {0}
- mov gs:[{rsp_offset}], rsp
- cli
- mov rsp, {0}
- pop rax
- pop rbx
- pop rcx
- pop rdx
- pop rdi
- pop rsi
- pop rbp
- pop r8
- pop r9
- pop r10
- pop r11
- pop r12
- pop r13
- pop r14
- pop r15
- iretq
- store_user_mode_context:
- mov rsp, gs:[{rsp_offset}]
- pop rsp
- add rsp, {user_registers_size}
- push r15
- push r14
- push r13
- push r12
- push r11
- push r10
- push r9
- push r8
- push rbp
- push rsi
- push rdi
- push rdx
- push rcx
- push rbx
- push rax
- mov rsp, gs:[{rsp_offset}]
- sti
- pop rax
- pop rbp
- pop rbx
- switch_to_kernel_mode:
- ",
- in(reg) registers,
- rsp_offset = const KERNEL_RSP_OFFSET_IN_CPU,
- user_registers_size = const mem::size_of::<Registers>() - mem::size_of::<ModeContext>(),
- lateout("rax") _,
- lateout("rcx") _,
- lateout("rdx") _,
- lateout("rdi") _,
- lateout("rsi") _,
- lateout("r8") _,
- lateout("r9") _,
- lateout("r10") _,
- lateout("r11") _,
- lateout("r12") _,
- lateout("r13") _,
- lateout("r14") _,
- lateout("r15") _,
- );
- }
- let new_kernel_rsp = rsp();
- assert!(
- old_kernel_rsp.is_ok() && new_kernel_rsp.is_ok(),
- "check that the kernel RSP is saved and restored correctly",
- );
- assert_eq!(
- old_kernel_rsp, new_kernel_rsp,
- concat!(
- "check that the kernel RSP is saved and restored correctly and the code ",
- "pushes to the stack the same amount of information as it pops from it",
- ),
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment