zakhar_azg

Untitled

Nov 9th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. pub(super) unsafe fn switch_to(registers: *const Registers) {
  2. let old_kernel_rsp = rsp();
  3.  
  4. unsafe {
  5. asm!(
  6. "
  7. push rbx
  8. push rbp
  9. push {0}
  10.  
  11. mov gs:[{rsp_offset}], rsp
  12.  
  13. cli
  14.  
  15. mov rsp, {0}
  16.  
  17. pop rax
  18. pop rbx
  19. pop rcx
  20. pop rdx
  21. pop rdi
  22. pop rsi
  23. pop rbp
  24. pop r8
  25. pop r9
  26. pop r10
  27. pop r11
  28. pop r12
  29. pop r13
  30. pop r14
  31. pop r15
  32.  
  33. iretq
  34.  
  35. store_user_mode_context:
  36. mov rsp, gs:[{rsp_offset}]
  37.  
  38. pop rsp
  39. add rsp, {user_registers_size}
  40.  
  41. push r15
  42. push r14
  43. push r13
  44. push r12
  45. push r11
  46. push r10
  47. push r9
  48. push r8
  49. push rbp
  50. push rsi
  51. push rdi
  52. push rdx
  53. push rcx
  54. push rbx
  55. push rax
  56.  
  57. mov rsp, gs:[{rsp_offset}]
  58.  
  59. sti
  60.  
  61. pop rax
  62. pop rbp
  63. pop rbx
  64.  
  65. switch_to_kernel_mode:
  66. ",
  67. in(reg) registers,
  68.  
  69. rsp_offset = const KERNEL_RSP_OFFSET_IN_CPU,
  70. user_registers_size = const mem::size_of::<Registers>() - mem::size_of::<ModeContext>(),
  71.  
  72. lateout("rax") _,
  73. lateout("rcx") _,
  74. lateout("rdx") _,
  75. lateout("rdi") _,
  76. lateout("rsi") _,
  77. lateout("r8") _,
  78. lateout("r9") _,
  79. lateout("r10") _,
  80. lateout("r11") _,
  81. lateout("r12") _,
  82. lateout("r13") _,
  83. lateout("r14") _,
  84. lateout("r15") _,
  85. );
  86. }
  87.  
  88. let new_kernel_rsp = rsp();
  89.  
  90. assert!(
  91. old_kernel_rsp.is_ok() && new_kernel_rsp.is_ok(),
  92. "check that the kernel RSP is saved and restored correctly",
  93. );
  94.  
  95. assert_eq!(
  96. old_kernel_rsp, new_kernel_rsp,
  97. concat!(
  98. "check that the kernel RSP is saved and restored correctly and the code ",
  99. "pushes to the stack the same amount of information as it pops from it",
  100. ),
  101. );
  102. }
Advertisement
Add Comment
Please, Sign In to add comment