]>
Commit | Line | Data |
---|---|---|
749cf76c CD |
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 | */ | |
342cd0ab CD |
18 | |
19 | #include <linux/linkage.h> | |
6ebbf2ce | 20 | #include <asm/assembler.h> |
342cd0ab | 21 | #include <asm/unified.h> |
749cf76c CD |
22 | #include <asm/asm-offsets.h> |
23 | #include <asm/kvm_asm.h> | |
342cd0ab | 24 | #include <asm/kvm_arm.h> |
5a677ce0 | 25 | #include <asm/kvm_mmu.h> |
bc845e4f | 26 | #include <asm/virt.h> |
342cd0ab CD |
27 | |
28 | /******************************************************************** | |
29 | * Hypervisor initialization | |
30 | * - should be called with: | |
5a677ce0 MZ |
31 | * r0 = top of Hyp stack (kernel VA) |
32 | * r1 = pointer to hyp vectors | |
33 | * r2,r3 = Hypervisor pgd pointer | |
34 | * | |
35 | * The init scenario is: | |
cd602a37 MZ |
36 | * - We jump in HYP with 3 parameters: runtime HYP pgd, runtime stack, |
37 | * runtime vectors | |
5a677ce0 MZ |
38 | * - Invalidate TLBs |
39 | * - Set stack and vectors | |
cd602a37 MZ |
40 | * - Setup the page tables |
41 | * - Enable the MMU | |
5a677ce0 | 42 | * - Profit! (or eret, if you only care about the code). |
bc845e4f MZ |
43 | * |
44 | * Another possibility is to get a HYP stub hypercall. | |
45 | * We discriminate between the two by checking if r0 contains a value | |
46 | * that is less than HVC_STUB_HCALL_NR. | |
342cd0ab CD |
47 | */ |
48 | ||
49 | .text | |
50 | .pushsection .hyp.idmap.text,"ax" | |
51 | .align 5 | |
52 | __kvm_hyp_init: | |
53 | .globl __kvm_hyp_init | |
54 | ||
55 | @ Hyp-mode exception vector | |
56 | W(b) . | |
57 | W(b) . | |
58 | W(b) . | |
59 | W(b) . | |
60 | W(b) . | |
61 | W(b) __do_hyp_init | |
62 | W(b) . | |
63 | W(b) . | |
64 | ||
65 | __do_hyp_init: | |
bc845e4f MZ |
66 | @ Check for a stub hypercall |
67 | cmp r0, #HVC_STUB_HCALL_NR | |
68 | blo __kvm_handle_stub_hvc | |
69 | ||
cd602a37 MZ |
70 | @ Set stack pointer |
71 | mov sp, r0 | |
72 | ||
73 | @ Set HVBAR to point to the HYP vectors | |
74 | mcr p15, 4, r1, c12, c0, 0 @ HVBAR | |
5a677ce0 | 75 | |
342cd0ab | 76 | @ Set the HTTBR to point to the hypervisor PGD pointer passed |
19b0e60a | 77 | mcrr p15, 4, rr_lo_hi(r2, r3), c2 |
342cd0ab CD |
78 | |
79 | @ Set the HTCR and VTCR to the same shareability and cacheability | |
80 | @ settings as the non-secure TTBCR and with T0SZ == 0. | |
81 | mrc p15, 4, r0, c2, c0, 2 @ HTCR | |
5a677ce0 MZ |
82 | ldr r2, =HTCR_MASK |
83 | bic r0, r0, r2 | |
342cd0ab CD |
84 | mrc p15, 0, r1, c2, c0, 2 @ TTBCR |
85 | and r1, r1, #(HTCR_MASK & ~TTBCR_T0SZ) | |
86 | orr r0, r0, r1 | |
87 | mcr p15, 4, r0, c2, c0, 2 @ HTCR | |
88 | ||
342cd0ab CD |
89 | @ Use the same memory attributes for hyp. accesses as the kernel |
90 | @ (copy MAIRx ro HMAIRx). | |
91 | mrc p15, 0, r0, c10, c2, 0 | |
92 | mcr p15, 4, r0, c10, c2, 0 | |
93 | mrc p15, 0, r0, c10, c2, 1 | |
94 | mcr p15, 4, r0, c10, c2, 1 | |
95 | ||
f6edbbf3 PS |
96 | @ Invalidate the stale TLBs from Bootloader |
97 | mcr p15, 4, r0, c8, c7, 0 @ TLBIALLH | |
98 | dsb ish | |
99 | ||
342cd0ab CD |
100 | @ Set the HSCTLR to: |
101 | @ - ARM/THUMB exceptions: Kernel config (Thumb-2 kernel) | |
102 | @ - Endianness: Kernel config | |
103 | @ - Fast Interrupt Features: Kernel config | |
104 | @ - Write permission implies XN: disabled | |
105 | @ - Instruction cache: enabled | |
106 | @ - Data/Unified cache: enabled | |
342cd0ab CD |
107 | @ - MMU: enabled (this code must be run from an identity mapping) |
108 | mrc p15, 4, r0, c1, c0, 0 @ HSCR | |
5a677ce0 MZ |
109 | ldr r2, =HSCTLR_MASK |
110 | bic r0, r0, r2 | |
342cd0ab | 111 | mrc p15, 0, r1, c1, c0, 0 @ SCTLR |
5a677ce0 MZ |
112 | ldr r2, =(HSCTLR_EE | HSCTLR_FI | HSCTLR_I | HSCTLR_C) |
113 | and r1, r1, r2 | |
33b5c388 MZ |
114 | ARM( ldr r2, =(HSCTLR_M) ) |
115 | THUMB( ldr r2, =(HSCTLR_M | HSCTLR_TE) ) | |
5a677ce0 | 116 | orr r1, r1, r2 |
342cd0ab | 117 | orr r0, r0, r1 |
342cd0ab | 118 | mcr p15, 4, r0, c1, c0, 0 @ HSCR |
5a677ce0 MZ |
119 | isb |
120 | ||
342cd0ab CD |
121 | eret |
122 | ||
bc845e4f | 123 | ENTRY(__kvm_handle_stub_hvc) |
ecb5d61d | 124 | cmp r0, #HVC_SOFT_RESTART |
4d5f9c14 MZ |
125 | bne 1f |
126 | ||
127 | /* The target is expected in r1 */ | |
128 | msr ELR_hyp, r1 | |
129 | mrs r0, cpsr | |
130 | bic r0, r0, #MODE_MASK | |
131 | orr r0, r0, #HYP_MODE | |
132 | THUMB( orr r0, r0, #PSR_T_BIT ) | |
133 | msr spsr_cxsf, r0 | |
134 | b reset | |
135 | ||
5d224aa7 | 136 | 1: cmp r0, #HVC_RESET_VECTORS |
bc845e4f | 137 | bne 1f |
4d5f9c14 MZ |
138 | |
139 | reset: | |
e537ecd7 MZ |
140 | /* We're now in idmap, disable MMU */ |
141 | mrc p15, 4, r1, c1, c0, 0 @ HSCTLR | |
bc845e4f MZ |
142 | ldr r0, =(HSCTLR_M | HSCTLR_A | HSCTLR_C | HSCTLR_I) |
143 | bic r1, r1, r0 | |
e537ecd7 MZ |
144 | mcr p15, 4, r1, c1, c0, 0 @ HSCTLR |
145 | ||
bc845e4f MZ |
146 | /* |
147 | * Install stub vectors, using ardb's VA->PA trick. | |
148 | */ | |
149 | 0: adr r0, 0b @ PA(0) | |
150 | movw r1, #:lower16:__hyp_stub_vectors - 0b @ VA(stub) - VA(0) | |
151 | movt r1, #:upper16:__hyp_stub_vectors - 0b | |
152 | add r1, r1, r0 @ PA(stub) | |
153 | mcr p15, 4, r1, c12, c0, 0 @ HVBAR | |
154 | b exit | |
155 | ||
156 | 1: ldr r0, =HVC_STUB_ERR | |
d9118c87 | 157 | eret |
e537ecd7 | 158 | |
bc845e4f | 159 | exit: |
d9118c87 | 160 | mov r0, #0 |
e537ecd7 | 161 | eret |
bc845e4f | 162 | ENDPROC(__kvm_handle_stub_hvc) |
e537ecd7 | 163 | |
342cd0ab CD |
164 | .ltorg |
165 | ||
166 | .globl __kvm_hyp_init_end | |
167 | __kvm_hyp_init_end: | |
168 | ||
169 | .popsection |