init.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Author: Christoffer Dall <c.dall@virtualopensystems.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, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include <linux/linkage.h>
  19. #include <asm/assembler.h>
  20. #include <asm/unified.h>
  21. #include <asm/asm-offsets.h>
  22. #include <asm/kvm_asm.h>
  23. #include <asm/kvm_arm.h>
  24. #include <asm/kvm_mmu.h>
  25. /********************************************************************
  26. * Hypervisor initialization
  27. * - should be called with:
  28. * r0 = top of Hyp stack (kernel VA)
  29. * r1 = pointer to hyp vectors
  30. * r2,r3 = Hypervisor pgd pointer
  31. *
  32. * The init scenario is:
  33. * - We jump in HYP with 3 parameters: runtime HYP pgd, runtime stack,
  34. * runtime vectors
  35. * - Invalidate TLBs
  36. * - Set stack and vectors
  37. * - Setup the page tables
  38. * - Enable the MMU
  39. * - Profit! (or eret, if you only care about the code).
  40. */
  41. .text
  42. .pushsection .hyp.idmap.text,"ax"
  43. .align 5
  44. __kvm_hyp_init:
  45. .globl __kvm_hyp_init
  46. @ Hyp-mode exception vector
  47. W(b) .
  48. W(b) .
  49. W(b) .
  50. W(b) .
  51. W(b) .
  52. W(b) __do_hyp_init
  53. W(b) .
  54. W(b) .
  55. __do_hyp_init:
  56. @ Set stack pointer
  57. mov sp, r0
  58. @ Set HVBAR to point to the HYP vectors
  59. mcr p15, 4, r1, c12, c0, 0 @ HVBAR
  60. @ Set the HTTBR to point to the hypervisor PGD pointer passed
  61. mcrr p15, 4, rr_lo_hi(r2, r3), c2
  62. @ Set the HTCR and VTCR to the same shareability and cacheability
  63. @ settings as the non-secure TTBCR and with T0SZ == 0.
  64. mrc p15, 4, r0, c2, c0, 2 @ HTCR
  65. ldr r2, =HTCR_MASK
  66. bic r0, r0, r2
  67. mrc p15, 0, r1, c2, c0, 2 @ TTBCR
  68. and r1, r1, #(HTCR_MASK & ~TTBCR_T0SZ)
  69. orr r0, r0, r1
  70. mcr p15, 4, r0, c2, c0, 2 @ HTCR
  71. @ Use the same memory attributes for hyp. accesses as the kernel
  72. @ (copy MAIRx ro HMAIRx).
  73. mrc p15, 0, r0, c10, c2, 0
  74. mcr p15, 4, r0, c10, c2, 0
  75. mrc p15, 0, r0, c10, c2, 1
  76. mcr p15, 4, r0, c10, c2, 1
  77. @ Invalidate the stale TLBs from Bootloader
  78. mcr p15, 4, r0, c8, c7, 0 @ TLBIALLH
  79. dsb ish
  80. @ Set the HSCTLR to:
  81. @ - ARM/THUMB exceptions: Kernel config (Thumb-2 kernel)
  82. @ - Endianness: Kernel config
  83. @ - Fast Interrupt Features: Kernel config
  84. @ - Write permission implies XN: disabled
  85. @ - Instruction cache: enabled
  86. @ - Data/Unified cache: enabled
  87. @ - MMU: enabled (this code must be run from an identity mapping)
  88. mrc p15, 4, r0, c1, c0, 0 @ HSCR
  89. ldr r2, =HSCTLR_MASK
  90. bic r0, r0, r2
  91. mrc p15, 0, r1, c1, c0, 0 @ SCTLR
  92. ldr r2, =(HSCTLR_EE | HSCTLR_FI | HSCTLR_I | HSCTLR_C)
  93. and r1, r1, r2
  94. ARM( ldr r2, =(HSCTLR_M) )
  95. THUMB( ldr r2, =(HSCTLR_M | HSCTLR_TE) )
  96. orr r1, r1, r2
  97. orr r0, r0, r1
  98. mcr p15, 4, r0, c1, c0, 0 @ HSCR
  99. isb
  100. eret
  101. @ r0 : stub vectors address
  102. ENTRY(__kvm_hyp_reset)
  103. /* We're now in idmap, disable MMU */
  104. mrc p15, 4, r1, c1, c0, 0 @ HSCTLR
  105. ldr r2, =(HSCTLR_M | HSCTLR_A | HSCTLR_C | HSCTLR_I)
  106. bic r1, r1, r2
  107. mcr p15, 4, r1, c1, c0, 0 @ HSCTLR
  108. /* Install stub vectors */
  109. mcr p15, 4, r0, c12, c0, 0 @ HVBAR
  110. isb
  111. eret
  112. ENDPROC(__kvm_hyp_reset)
  113. .ltorg
  114. .globl __kvm_hyp_init_end
  115. __kvm_hyp_init_end:
  116. .popsection