entry.S 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2015 - ARM Ltd
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. */
  6. #include <linux/linkage.h>
  7. #include <asm/alternative.h>
  8. #include <asm/assembler.h>
  9. #include <asm/fpsimdmacros.h>
  10. #include <asm/kvm.h>
  11. #include <asm/kvm_arm.h>
  12. #include <asm/kvm_asm.h>
  13. #include <asm/kvm_mmu.h>
  14. #include <asm/kvm_mte.h>
  15. #include <asm/kvm_ptrauth.h>
  16. .text
  17. /*
  18. * u64 __guest_enter(struct kvm_vcpu *vcpu);
  19. */
  20. SYM_FUNC_START(__guest_enter)
  21. // x0: vcpu
  22. // x1-x17: clobbered by macros
  23. // x29: guest context
  24. adr_this_cpu x1, kvm_hyp_ctxt, x2
  25. // Store the hyp regs
  26. save_callee_saved_regs x1
  27. // Save hyp's sp_el0
  28. save_sp_el0 x1, x2
  29. // Now the hyp state is stored if we have a pending RAS SError it must
  30. // affect the host or hyp. If any asynchronous exception is pending we
  31. // defer the guest entry. The DSB isn't necessary before v8.2 as any
  32. // SError would be fatal.
  33. alternative_if ARM64_HAS_RAS_EXTN
  34. dsb nshst
  35. isb
  36. alternative_else_nop_endif
  37. mrs x1, isr_el1
  38. cbz x1, 1f
  39. // Ensure that __guest_enter() always provides a context
  40. // synchronization event so that callers don't need ISBs for anything
  41. // that would usually be synchonized by the ERET.
  42. isb
  43. mov x0, #ARM_EXCEPTION_IRQ
  44. ret
  45. 1:
  46. set_loaded_vcpu x0, x1, x2
  47. add x29, x0, #VCPU_CONTEXT
  48. // mte_switch_to_guest(g_ctxt, h_ctxt, tmp1)
  49. mte_switch_to_guest x29, x1, x2
  50. // Macro ptrauth_switch_to_guest format:
  51. // ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3)
  52. // The below macro to restore guest keys is not implemented in C code
  53. // as it may cause Pointer Authentication key signing mismatch errors
  54. // when this feature is enabled for kernel code.
  55. ptrauth_switch_to_guest x29, x0, x1, x2
  56. // Restore the guest's sp_el0
  57. restore_sp_el0 x29, x0
  58. // Restore guest regs x0-x17
  59. ldp x0, x1, [x29, #CPU_XREG_OFFSET(0)]
  60. ldp x2, x3, [x29, #CPU_XREG_OFFSET(2)]
  61. ldp x4, x5, [x29, #CPU_XREG_OFFSET(4)]
  62. ldp x6, x7, [x29, #CPU_XREG_OFFSET(6)]
  63. ldp x8, x9, [x29, #CPU_XREG_OFFSET(8)]
  64. ldp x10, x11, [x29, #CPU_XREG_OFFSET(10)]
  65. ldp x12, x13, [x29, #CPU_XREG_OFFSET(12)]
  66. ldp x14, x15, [x29, #CPU_XREG_OFFSET(14)]
  67. ldp x16, x17, [x29, #CPU_XREG_OFFSET(16)]
  68. // Restore guest regs x18-x29, lr
  69. restore_callee_saved_regs x29
  70. // Do not touch any register after this!
  71. eret
  72. sb
  73. SYM_INNER_LABEL(__guest_exit_restore_elr_and_panic, SYM_L_GLOBAL)
  74. // x2-x29,lr: vcpu regs
  75. // vcpu x0-x1 on the stack
  76. adr_this_cpu x0, kvm_hyp_ctxt, x1
  77. ldr x0, [x0, #CPU_ELR_EL2]
  78. msr elr_el2, x0
  79. SYM_INNER_LABEL(__guest_exit_panic, SYM_L_GLOBAL)
  80. // x2-x29,lr: vcpu regs
  81. // vcpu x0-x1 on the stack
  82. // If the hyp context is loaded, go straight to hyp_panic
  83. get_loaded_vcpu x0, x1
  84. cbnz x0, 1f
  85. b hyp_panic
  86. 1:
  87. // The hyp context is saved so make sure it is restored to allow
  88. // hyp_panic to run at hyp and, subsequently, panic to run in the host.
  89. // This makes use of __guest_exit to avoid duplication but sets the
  90. // return address to tail call into hyp_panic. As a side effect, the
  91. // current state is saved to the guest context but it will only be
  92. // accurate if the guest had been completely restored.
  93. adr_this_cpu x0, kvm_hyp_ctxt, x1
  94. adr_l x1, hyp_panic
  95. str x1, [x0, #CPU_XREG_OFFSET(30)]
  96. get_vcpu_ptr x1, x0
  97. SYM_INNER_LABEL(__guest_exit, SYM_L_GLOBAL)
  98. // x0: return code
  99. // x1: vcpu
  100. // x2-x29,lr: vcpu regs
  101. // vcpu x0-x1 on the stack
  102. add x1, x1, #VCPU_CONTEXT
  103. ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN)
  104. // Store the guest regs x2 and x3
  105. stp x2, x3, [x1, #CPU_XREG_OFFSET(2)]
  106. // Retrieve the guest regs x0-x1 from the stack
  107. ldp x2, x3, [sp], #16 // x0, x1
  108. // Store the guest regs x0-x1 and x4-x17
  109. stp x2, x3, [x1, #CPU_XREG_OFFSET(0)]
  110. stp x4, x5, [x1, #CPU_XREG_OFFSET(4)]
  111. stp x6, x7, [x1, #CPU_XREG_OFFSET(6)]
  112. stp x8, x9, [x1, #CPU_XREG_OFFSET(8)]
  113. stp x10, x11, [x1, #CPU_XREG_OFFSET(10)]
  114. stp x12, x13, [x1, #CPU_XREG_OFFSET(12)]
  115. stp x14, x15, [x1, #CPU_XREG_OFFSET(14)]
  116. stp x16, x17, [x1, #CPU_XREG_OFFSET(16)]
  117. // Store the guest regs x18-x29, lr
  118. save_callee_saved_regs x1
  119. // Store the guest's sp_el0
  120. save_sp_el0 x1, x2
  121. adr_this_cpu x2, kvm_hyp_ctxt, x3
  122. // Macro ptrauth_switch_to_hyp format:
  123. // ptrauth_switch_to_hyp(guest cxt, host cxt, tmp1, tmp2, tmp3)
  124. // The below macro to save/restore keys is not implemented in C code
  125. // as it may cause Pointer Authentication key signing mismatch errors
  126. // when this feature is enabled for kernel code.
  127. ptrauth_switch_to_hyp x1, x2, x3, x4, x5
  128. // mte_switch_to_hyp(g_ctxt, h_ctxt, reg1)
  129. mte_switch_to_hyp x1, x2, x3
  130. // Restore hyp's sp_el0
  131. restore_sp_el0 x2, x3
  132. // Now restore the hyp regs
  133. restore_callee_saved_regs x2
  134. set_loaded_vcpu xzr, x2, x3
  135. alternative_if ARM64_HAS_RAS_EXTN
  136. // If we have the RAS extensions we can consume a pending error
  137. // without an unmask-SError and isb. The ESB-instruction consumed any
  138. // pending guest error when we took the exception from the guest.
  139. mrs_s x2, SYS_DISR_EL1
  140. str x2, [x1, #(VCPU_FAULT_DISR - VCPU_CONTEXT)]
  141. cbz x2, 1f
  142. msr_s SYS_DISR_EL1, xzr
  143. orr x0, x0, #(1<<ARM_EXIT_WITH_SERROR_BIT)
  144. 1: ret
  145. alternative_else
  146. dsb sy // Synchronize against in-flight ld/st
  147. isb // Prevent an early read of side-effect free ISR
  148. mrs x2, isr_el1
  149. tbnz x2, #ISR_EL1_A_SHIFT, 2f
  150. ret
  151. nop
  152. 2:
  153. alternative_endif
  154. // We know we have a pending asynchronous abort, now is the
  155. // time to flush it out. From your VAXorcist book, page 666:
  156. // "Threaten me not, oh Evil one! For I speak with
  157. // the power of DEC, and I command thee to show thyself!"
  158. mrs x2, elr_el2
  159. mrs x3, esr_el2
  160. mrs x4, spsr_el2
  161. mov x5, x0
  162. msr daifclr, #4 // Unmask aborts
  163. // This is our single instruction exception window. A pending
  164. // SError is guaranteed to occur at the earliest when we unmask
  165. // it, and at the latest just after the ISB.
  166. abort_guest_exit_start:
  167. isb
  168. abort_guest_exit_end:
  169. msr daifset, #4 // Mask aborts
  170. ret
  171. _kvm_extable abort_guest_exit_start, 9997f
  172. _kvm_extable abort_guest_exit_end, 9997f
  173. 9997:
  174. msr daifset, #4 // Mask aborts
  175. mov x0, #(1 << ARM_EXIT_WITH_SERROR_BIT)
  176. // restore the EL1 exception context so that we can report some
  177. // information. Merge the exception code with the SError pending bit.
  178. msr elr_el2, x2
  179. msr esr_el2, x3
  180. msr spsr_el2, x4
  181. orr x0, x0, x5
  182. 1: ret
  183. SYM_FUNC_END(__guest_enter)