hyp-init.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. .text
  24. .pushsection .hyp.idmap.text, "ax"
  25. .align 11
  26. ENTRY(__kvm_hyp_init)
  27. ventry __invalid // Synchronous EL2t
  28. ventry __invalid // IRQ EL2t
  29. ventry __invalid // FIQ EL2t
  30. ventry __invalid // Error EL2t
  31. ventry __invalid // Synchronous EL2h
  32. ventry __invalid // IRQ EL2h
  33. ventry __invalid // FIQ EL2h
  34. ventry __invalid // Error EL2h
  35. ventry __do_hyp_init // Synchronous 64-bit EL1
  36. ventry __invalid // IRQ 64-bit EL1
  37. ventry __invalid // FIQ 64-bit EL1
  38. ventry __invalid // Error 64-bit EL1
  39. ventry __invalid // Synchronous 32-bit EL1
  40. ventry __invalid // IRQ 32-bit EL1
  41. ventry __invalid // FIQ 32-bit EL1
  42. ventry __invalid // Error 32-bit EL1
  43. __invalid:
  44. b .
  45. /*
  46. * x0: HYP pgd
  47. * x1: HYP stack
  48. * x2: HYP vectors
  49. */
  50. __do_hyp_init:
  51. msr ttbr0_el2, x0
  52. mrs x4, tcr_el1
  53. ldr x5, =TCR_EL2_MASK
  54. and x4, x4, x5
  55. mov x5, #TCR_EL2_RES1
  56. orr x4, x4, x5
  57. #ifndef CONFIG_ARM64_VA_BITS_48
  58. /*
  59. * If we are running with VA_BITS < 48, we may be running with an extra
  60. * level of translation in the ID map. This is only the case if system
  61. * RAM is out of range for the currently configured page size and number
  62. * of translation levels, in which case we will also need the extra
  63. * level for the HYP ID map, or we won't be able to enable the EL2 MMU.
  64. *
  65. * However, at EL2, there is only one TTBR register, and we can't switch
  66. * between translation tables *and* update TCR_EL2.T0SZ at the same
  67. * time. Bottom line: we need the extra level in *both* our translation
  68. * tables.
  69. *
  70. * So use the same T0SZ value we use for the ID map.
  71. */
  72. ldr_l x5, idmap_t0sz
  73. bfi x4, x5, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH
  74. #endif
  75. /*
  76. * Read the PARange bits from ID_AA64MMFR0_EL1 and set the PS bits in
  77. * TCR_EL2.
  78. */
  79. mrs x5, ID_AA64MMFR0_EL1
  80. bfi x4, x5, #16, #3
  81. msr tcr_el2, x4
  82. mrs x4, mair_el1
  83. msr mair_el2, x4
  84. isb
  85. /* Invalidate the stale TLBs from Bootloader */
  86. tlbi alle2
  87. dsb sy
  88. /*
  89. * Preserve all the RES1 bits while setting the default flags,
  90. * as well as the EE bit on BE. Drop the A flag since the compiler
  91. * is allowed to generate unaligned accesses.
  92. */
  93. ldr x4, =(SCTLR_EL2_RES1 | (SCTLR_ELx_FLAGS & ~SCTLR_ELx_A))
  94. CPU_BE( orr x4, x4, #SCTLR_ELx_EE)
  95. msr sctlr_el2, x4
  96. isb
  97. /* Set the stack and new vectors */
  98. kern_hyp_va x1
  99. mov sp, x1
  100. kern_hyp_va x2
  101. msr vbar_el2, x2
  102. /* copy tpidr_el1 into tpidr_el2 for use by HYP */
  103. mrs x1, tpidr_el1
  104. msr tpidr_el2, x1
  105. /* Hello, World! */
  106. eret
  107. ENDPROC(__kvm_hyp_init)
  108. /*
  109. * Reset kvm back to the hyp stub.
  110. */
  111. ENTRY(__kvm_hyp_reset)
  112. /* We're now in idmap, disable MMU */
  113. mrs x0, sctlr_el2
  114. ldr x1, =SCTLR_ELx_FLAGS
  115. bic x0, x0, x1 // Clear SCTL_M and etc
  116. msr sctlr_el2, x0
  117. isb
  118. /* Install stub vectors */
  119. adr_l x0, __hyp_stub_vectors
  120. msr vbar_el2, x0
  121. eret
  122. ENDPROC(__kvm_hyp_reset)
  123. .ltorg
  124. .popsection