hyp-init.S 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2012,2013 - 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/assembler.h>
  19. #include <asm/kvm_arm.h>
  20. #include <asm/kvm_mmu.h>
  21. #include <asm/pgtable-hwdef.h>
  22. #include <asm/sysreg.h>
  23. #include <asm/virt.h>
  24. .text
  25. .pushsection .hyp.idmap.text, "ax"
  26. .align 11
  27. ENTRY(__kvm_hyp_init)
  28. ventry __invalid // Synchronous EL2t
  29. ventry __invalid // IRQ EL2t
  30. ventry __invalid // FIQ EL2t
  31. ventry __invalid // Error EL2t
  32. ventry __invalid // Synchronous EL2h
  33. ventry __invalid // IRQ EL2h
  34. ventry __invalid // FIQ EL2h
  35. ventry __invalid // Error EL2h
  36. ventry __do_hyp_init // Synchronous 64-bit EL1
  37. ventry __invalid // IRQ 64-bit EL1
  38. ventry __invalid // FIQ 64-bit EL1
  39. ventry __invalid // Error 64-bit EL1
  40. ventry __invalid // Synchronous 32-bit EL1
  41. ventry __invalid // IRQ 32-bit EL1
  42. ventry __invalid // FIQ 32-bit EL1
  43. ventry __invalid // Error 32-bit EL1
  44. __invalid:
  45. b .
  46. /*
  47. * x0: HYP pgd
  48. * x1: HYP stack
  49. * x2: HYP vectors
  50. */
  51. __do_hyp_init:
  52. /* Check for a stub HVC call */
  53. cmp x0, #HVC_STUB_HCALL_NR
  54. b.lo __kvm_handle_stub_hvc
  55. msr ttbr0_el2, x0
  56. mrs x4, tcr_el1
  57. ldr x5, =TCR_EL2_MASK
  58. and x4, x4, x5
  59. mov x5, #TCR_EL2_RES1
  60. orr x4, x4, x5
  61. #ifndef CONFIG_ARM64_VA_BITS_48
  62. /*
  63. * If we are running with VA_BITS < 48, we may be running with an extra
  64. * level of translation in the ID map. This is only the case if system
  65. * RAM is out of range for the currently configured page size and number
  66. * of translation levels, in which case we will also need the extra
  67. * level for the HYP ID map, or we won't be able to enable the EL2 MMU.
  68. *
  69. * However, at EL2, there is only one TTBR register, and we can't switch
  70. * between translation tables *and* update TCR_EL2.T0SZ at the same
  71. * time. Bottom line: we need the extra level in *both* our translation
  72. * tables.
  73. *
  74. * So use the same T0SZ value we use for the ID map.
  75. */
  76. ldr_l x5, idmap_t0sz
  77. bfi x4, x5, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
  78. #endif
  79. /*
  80. * Read the PARange bits from ID_AA64MMFR0_EL1 and set the PS bits in
  81. * TCR_EL2.
  82. */
  83. mrs x5, ID_AA64MMFR0_EL1
  84. bfi x4, x5, #16, #3
  85. msr tcr_el2, x4
  86. mrs x4, mair_el1
  87. msr mair_el2, x4
  88. isb
  89. /* Invalidate the stale TLBs from Bootloader */
  90. tlbi alle2
  91. dsb sy
  92. /*
  93. * Preserve all the RES1 bits while setting the default flags,
  94. * as well as the EE bit on BE. Drop the A flag since the compiler
  95. * is allowed to generate unaligned accesses.
  96. */
  97. ldr x4, =(SCTLR_EL2_RES1 | (SCTLR_ELx_FLAGS & ~SCTLR_ELx_A))
  98. CPU_BE( orr x4, x4, #SCTLR_ELx_EE)
  99. msr sctlr_el2, x4
  100. isb
  101. /* Set the stack and new vectors */
  102. kern_hyp_va x1
  103. mov sp, x1
  104. kern_hyp_va x2
  105. msr vbar_el2, x2
  106. /* copy tpidr_el1 into tpidr_el2 for use by HYP */
  107. mrs x1, tpidr_el1
  108. msr tpidr_el2, x1
  109. /* Hello, World! */
  110. eret
  111. ENDPROC(__kvm_hyp_init)
  112. ENTRY(__kvm_handle_stub_hvc)
  113. cmp x0, #HVC_SOFT_RESTART
  114. b.ne 1f
  115. /* This is where we're about to jump, staying at EL2 */
  116. msr elr_el2, x1
  117. mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT | PSR_MODE_EL2h)
  118. msr spsr_el2, x0
  119. /* Shuffle the arguments, and don't come back */
  120. mov x0, x2
  121. mov x1, x3
  122. mov x2, x4
  123. b reset
  124. 1: cmp x0, #HVC_RESET_VECTORS
  125. b.ne 1f
  126. /*
  127. * Set the HVC_RESET_VECTORS return code before entering the common
  128. * path so that we do not clobber x0-x2 in case we are coming via
  129. * HVC_SOFT_RESTART.
  130. */
  131. mov x0, xzr
  132. reset:
  133. /* Reset kvm back to the hyp stub. */
  134. mrs x5, sctlr_el2
  135. ldr x6, =SCTLR_ELx_FLAGS
  136. bic x5, x5, x6 // Clear SCTL_M and etc
  137. pre_disable_mmu_workaround
  138. msr sctlr_el2, x5
  139. isb
  140. /* Install stub vectors */
  141. adr_l x5, __hyp_stub_vectors
  142. msr vbar_el2, x5
  143. eret
  144. 1: /* Bad stub call */
  145. ldr x0, =HVC_STUB_ERR
  146. eret
  147. ENDPROC(__kvm_handle_stub_hvc)
  148. .ltorg
  149. .popsection