entry.S 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (C) 2015 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/linkage.h>
  18. #include <asm/asm-offsets.h>
  19. #include <asm/assembler.h>
  20. #include <asm/fpsimdmacros.h>
  21. #include <asm/kvm.h>
  22. #include <asm/kvm_arm.h>
  23. #include <asm/kvm_asm.h>
  24. #include <asm/kvm_mmu.h>
  25. #define CPU_GP_REG_OFFSET(x) (CPU_GP_REGS + x)
  26. #define CPU_XREG_OFFSET(x) CPU_GP_REG_OFFSET(CPU_USER_PT_REGS + 8*x)
  27. .text
  28. .pushsection .hyp.text, "ax"
  29. .macro save_callee_saved_regs ctxt
  30. stp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
  31. stp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
  32. stp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
  33. stp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)]
  34. stp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)]
  35. stp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)]
  36. .endm
  37. .macro restore_callee_saved_regs ctxt
  38. ldp x19, x20, [\ctxt, #CPU_XREG_OFFSET(19)]
  39. ldp x21, x22, [\ctxt, #CPU_XREG_OFFSET(21)]
  40. ldp x23, x24, [\ctxt, #CPU_XREG_OFFSET(23)]
  41. ldp x25, x26, [\ctxt, #CPU_XREG_OFFSET(25)]
  42. ldp x27, x28, [\ctxt, #CPU_XREG_OFFSET(27)]
  43. ldp x29, lr, [\ctxt, #CPU_XREG_OFFSET(29)]
  44. .endm
  45. /*
  46. * u64 __guest_enter(struct kvm_vcpu *vcpu,
  47. * struct kvm_cpu_context *host_ctxt);
  48. */
  49. ENTRY(__guest_enter)
  50. // x0: vcpu
  51. // x1: host context
  52. // x2-x17: clobbered by macros
  53. // x18: guest context
  54. // Store the host regs
  55. save_callee_saved_regs x1
  56. // Store the host_ctxt for use at exit time
  57. str x1, [sp, #-16]!
  58. add x18, x0, #VCPU_CONTEXT
  59. // Restore guest regs x0-x17
  60. ldp x0, x1, [x18, #CPU_XREG_OFFSET(0)]
  61. ldp x2, x3, [x18, #CPU_XREG_OFFSET(2)]
  62. ldp x4, x5, [x18, #CPU_XREG_OFFSET(4)]
  63. ldp x6, x7, [x18, #CPU_XREG_OFFSET(6)]
  64. ldp x8, x9, [x18, #CPU_XREG_OFFSET(8)]
  65. ldp x10, x11, [x18, #CPU_XREG_OFFSET(10)]
  66. ldp x12, x13, [x18, #CPU_XREG_OFFSET(12)]
  67. ldp x14, x15, [x18, #CPU_XREG_OFFSET(14)]
  68. ldp x16, x17, [x18, #CPU_XREG_OFFSET(16)]
  69. // Restore guest regs x19-x29, lr
  70. restore_callee_saved_regs x18
  71. // Restore guest reg x18
  72. ldr x18, [x18, #CPU_XREG_OFFSET(18)]
  73. // Do not touch any register after this!
  74. eret
  75. ENDPROC(__guest_enter)
  76. ENTRY(__guest_exit)
  77. // x0: return code
  78. // x1: vcpu
  79. // x2-x29,lr: vcpu regs
  80. // vcpu x0-x1 on the stack
  81. add x1, x1, #VCPU_CONTEXT
  82. ALTERNATIVE(nop, SET_PSTATE_PAN(1), ARM64_HAS_PAN, CONFIG_ARM64_PAN)
  83. // Store the guest regs x2 and x3
  84. stp x2, x3, [x1, #CPU_XREG_OFFSET(2)]
  85. // Retrieve the guest regs x0-x1 from the stack
  86. ldp x2, x3, [sp], #16 // x0, x1
  87. // Store the guest regs x0-x1 and x4-x18
  88. stp x2, x3, [x1, #CPU_XREG_OFFSET(0)]
  89. stp x4, x5, [x1, #CPU_XREG_OFFSET(4)]
  90. stp x6, x7, [x1, #CPU_XREG_OFFSET(6)]
  91. stp x8, x9, [x1, #CPU_XREG_OFFSET(8)]
  92. stp x10, x11, [x1, #CPU_XREG_OFFSET(10)]
  93. stp x12, x13, [x1, #CPU_XREG_OFFSET(12)]
  94. stp x14, x15, [x1, #CPU_XREG_OFFSET(14)]
  95. stp x16, x17, [x1, #CPU_XREG_OFFSET(16)]
  96. str x18, [x1, #CPU_XREG_OFFSET(18)]
  97. // Store the guest regs x19-x29, lr
  98. save_callee_saved_regs x1
  99. // Restore the host_ctxt from the stack
  100. ldr x2, [sp], #16
  101. // Now restore the host regs
  102. restore_callee_saved_regs x2
  103. // If we have a pending asynchronous abort, now is the
  104. // time to find out. From your VAXorcist book, page 666:
  105. // "Threaten me not, oh Evil one! For I speak with
  106. // the power of DEC, and I command thee to show thyself!"
  107. mrs x2, elr_el2
  108. mrs x3, esr_el2
  109. mrs x4, spsr_el2
  110. mov x5, x0
  111. dsb sy // Synchronize against in-flight ld/st
  112. msr daifclr, #4 // Unmask aborts
  113. // This is our single instruction exception window. A pending
  114. // SError is guaranteed to occur at the earliest when we unmask
  115. // it, and at the latest just after the ISB.
  116. .global abort_guest_exit_start
  117. abort_guest_exit_start:
  118. isb
  119. .global abort_guest_exit_end
  120. abort_guest_exit_end:
  121. // If the exception took place, restore the EL1 exception
  122. // context so that we can report some information.
  123. // Merge the exception code with the SError pending bit.
  124. tbz x0, #ARM_EXIT_WITH_SERROR_BIT, 1f
  125. msr elr_el2, x2
  126. msr esr_el2, x3
  127. msr spsr_el2, x4
  128. orr x0, x0, x5
  129. 1: ret
  130. ENDPROC(__guest_exit)
  131. ENTRY(__fpsimd_guest_restore)
  132. stp x2, x3, [sp, #-16]!
  133. stp x4, lr, [sp, #-16]!
  134. alternative_if_not ARM64_HAS_VIRT_HOST_EXTN
  135. mrs x2, cptr_el2
  136. bic x2, x2, #CPTR_EL2_TFP
  137. msr cptr_el2, x2
  138. alternative_else
  139. mrs x2, cpacr_el1
  140. orr x2, x2, #CPACR_EL1_FPEN
  141. msr cpacr_el1, x2
  142. alternative_endif
  143. isb
  144. mrs x3, tpidr_el2
  145. ldr x0, [x3, #VCPU_HOST_CONTEXT]
  146. kern_hyp_va x0
  147. add x0, x0, #CPU_GP_REG_OFFSET(CPU_FP_REGS)
  148. bl __fpsimd_save_state
  149. add x2, x3, #VCPU_CONTEXT
  150. add x0, x2, #CPU_GP_REG_OFFSET(CPU_FP_REGS)
  151. bl __fpsimd_restore_state
  152. // Skip restoring fpexc32 for AArch64 guests
  153. mrs x1, hcr_el2
  154. tbnz x1, #HCR_RW_SHIFT, 1f
  155. ldr x4, [x3, #VCPU_FPEXC32_EL2]
  156. msr fpexc32_el2, x4
  157. 1:
  158. ldp x4, lr, [sp], #16
  159. ldp x2, x3, [sp], #16
  160. ldp x0, x1, [sp], #16
  161. eret
  162. ENDPROC(__fpsimd_guest_restore)