hyp-init.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * x3: per-CPU offset
  51. */
  52. __do_hyp_init:
  53. /* Check for a stub HVC call */
  54. cmp x0, #HVC_STUB_HCALL_NR
  55. b.lo __kvm_handle_stub_hvc
  56. phys_to_ttbr x4, x0
  57. alternative_if ARM64_HAS_CNP
  58. orr x4, x4, #TTBR_CNP_BIT
  59. alternative_else_nop_endif
  60. msr ttbr0_el2, x4
  61. mrs x4, tcr_el1
  62. ldr x5, =TCR_EL2_MASK
  63. and x4, x4, x5
  64. mov x5, #TCR_EL2_RES1
  65. orr x4, x4, x5
  66. /*
  67. * The ID map may be configured to use an extended virtual address
  68. * range. This is only the case if system RAM is out of range for the
  69. * currently configured page size and VA_BITS, in which case we will
  70. * also need the extended virtual range for the HYP ID map, or we won't
  71. * be able to enable the EL2 MMU.
  72. *
  73. * However, at EL2, there is only one TTBR register, and we can't switch
  74. * between translation tables *and* update TCR_EL2.T0SZ at the same
  75. * time. Bottom line: we need to use the extended range with *both* our
  76. * translation tables.
  77. *
  78. * So use the same T0SZ value we use for the ID map.
  79. */
  80. ldr_l x5, idmap_t0sz
  81. bfi x4, x5, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
  82. /*
  83. * Set the PS bits in TCR_EL2.
  84. */
  85. tcr_compute_pa_size x4, #TCR_EL2_PS_SHIFT, x5, x6
  86. msr tcr_el2, x4
  87. mrs x4, mair_el1
  88. msr mair_el2, x4
  89. isb
  90. /* Invalidate the stale TLBs from Bootloader */
  91. tlbi alle2
  92. dsb sy
  93. /*
  94. * Preserve all the RES1 bits while setting the default flags,
  95. * as well as the EE bit on BE. Drop the A flag since the compiler
  96. * is allowed to generate unaligned accesses.
  97. */
  98. ldr x4, =(SCTLR_EL2_RES1 | (SCTLR_ELx_FLAGS & ~SCTLR_ELx_A))
  99. CPU_BE( orr x4, x4, #SCTLR_ELx_EE)
  100. msr sctlr_el2, x4
  101. isb
  102. /* Set the stack and new vectors */
  103. kern_hyp_va x1
  104. mov sp, x1
  105. msr vbar_el2, x2
  106. /* Set tpidr_el2 for use by HYP */
  107. msr tpidr_el2, x3
  108. /* Hello, World! */
  109. eret
  110. ENDPROC(__kvm_hyp_init)
  111. ENTRY(__kvm_handle_stub_hvc)
  112. cmp x0, #HVC_SOFT_RESTART
  113. b.ne 1f
  114. /* This is where we're about to jump, staying at EL2 */
  115. msr elr_el2, x1
  116. mov x0, #(PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT | PSR_MODE_EL2h)
  117. msr spsr_el2, x0
  118. /* Shuffle the arguments, and don't come back */
  119. mov x0, x2
  120. mov x1, x3
  121. mov x2, x4
  122. b reset
  123. 1: cmp x0, #HVC_RESET_VECTORS
  124. b.ne 1f
  125. reset:
  126. /*
  127. * Reset kvm back to the hyp stub. Do not clobber x0-x4 in
  128. * case we coming via HVC_SOFT_RESTART.
  129. */
  130. mrs x5, sctlr_el2
  131. ldr x6, =SCTLR_ELx_FLAGS
  132. bic x5, x5, x6 // Clear SCTL_M and etc
  133. pre_disable_mmu_workaround
  134. msr sctlr_el2, x5
  135. isb
  136. /* Install stub vectors */
  137. adr_l x5, __hyp_stub_vectors
  138. msr vbar_el2, x5
  139. mov x0, xzr
  140. eret
  141. 1: /* Bad stub call */
  142. ldr x0, =HVC_STUB_ERR
  143. eret
  144. ENDPROC(__kvm_handle_stub_hvc)
  145. .ltorg
  146. .popsection