blob: 570ed4a9c2618ba7810f96af0b106c8cfbdfd2cc [file] [log] [blame]
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001/*
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 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -050018
19#include <linux/linkage.h>
Russell King6ebbf2c2014-06-30 16:29:12 +010020#include <asm/assembler.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050021#include <asm/unified.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050022#include <asm/asm-offsets.h>
23#include <asm/kvm_asm.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050024#include <asm/kvm_arm.h>
Marc Zyngier5a677ce2013-04-12 19:12:06 +010025#include <asm/kvm_mmu.h>
Marc Zyngierbc845e42017-04-03 19:37:53 +010026#include <asm/virt.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050027
28/********************************************************************
29 * Hypervisor initialization
30 * - should be called with:
Marc Zyngier5a677ce2013-04-12 19:12:06 +010031 * 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:
Marc Zyngiercd602a32016-06-30 18:40:47 +010036 * - We jump in HYP with 3 parameters: runtime HYP pgd, runtime stack,
37 * runtime vectors
Marc Zyngier5a677ce2013-04-12 19:12:06 +010038 * - Invalidate TLBs
39 * - Set stack and vectors
Marc Zyngiercd602a32016-06-30 18:40:47 +010040 * - Setup the page tables
41 * - Enable the MMU
Marc Zyngier5a677ce2013-04-12 19:12:06 +010042 * - Profit! (or eret, if you only care about the code).
Marc Zyngierbc845e42017-04-03 19:37:53 +010043 *
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.
Christoffer Dall342cd0a2013-01-20 18:28:06 -050047 */
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:
Marc Zyngierbc845e42017-04-03 19:37:53 +010066 @ Check for a stub hypercall
67 cmp r0, #HVC_STUB_HCALL_NR
68 blo __kvm_handle_stub_hvc
69
Marc Zyngiercd602a32016-06-30 18:40:47 +010070 @ 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
Marc Zyngier5a677ce2013-04-12 19:12:06 +010075
Christoffer Dall342cd0a2013-01-20 18:28:06 -050076 @ Set the HTTBR to point to the hypervisor PGD pointer passed
Victor Kamensky19b0e602014-06-12 09:30:02 -070077 mcrr p15, 4, rr_lo_hi(r2, r3), c2
Christoffer Dall342cd0a2013-01-20 18:28:06 -050078
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
Marc Zyngier5a677ce2013-04-12 19:12:06 +010082 ldr r2, =HTCR_MASK
83 bic r0, r0, r2
Christoffer Dall342cd0a2013-01-20 18:28:06 -050084 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
Christoffer Dall342cd0a2013-01-20 18:28:06 -050089 @ 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
Pranavkumar Sawargaonkarf6edbbf2014-07-31 12:23:23 +053096 @ Invalidate the stale TLBs from Bootloader
97 mcr p15, 4, r0, c8, c7, 0 @ TLBIALLH
98 dsb ish
99
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500100 @ 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
107 @ - Memory alignment checks: enabled
108 @ - MMU: enabled (this code must be run from an identity mapping)
109 mrc p15, 4, r0, c1, c0, 0 @ HSCR
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100110 ldr r2, =HSCTLR_MASK
111 bic r0, r0, r2
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500112 mrc p15, 0, r1, c1, c0, 0 @ SCTLR
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100113 ldr r2, =(HSCTLR_EE | HSCTLR_FI | HSCTLR_I | HSCTLR_C)
114 and r1, r1, r2
115 ARM( ldr r2, =(HSCTLR_M | HSCTLR_A) )
116 THUMB( ldr r2, =(HSCTLR_M | HSCTLR_A | HSCTLR_TE) )
117 orr r1, r1, r2
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500118 orr r0, r0, r1
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500119 mcr p15, 4, r0, c1, c0, 0 @ HSCR
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100120 isb
121
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500122 eret
123
Marc Zyngierbc845e42017-04-03 19:37:53 +0100124ENTRY(__kvm_handle_stub_hvc)
Marc Zyngierecb5d612017-04-03 19:38:03 +0100125 cmp r0, #HVC_SOFT_RESTART
Marc Zyngier4d5f9c12017-04-03 19:37:57 +0100126 bne 1f
127
128 /* The target is expected in r1 */
129 msr ELR_hyp, r1
130 mrs r0, cpsr
131 bic r0, r0, #MODE_MASK
132 orr r0, r0, #HYP_MODE
133THUMB( orr r0, r0, #PSR_T_BIT )
134 msr spsr_cxsf, r0
135 b reset
136
Marc Zyngier5d224aa2017-04-03 19:37:54 +01001371: cmp r0, #HVC_RESET_VECTORS
Marc Zyngierbc845e42017-04-03 19:37:53 +0100138 bne 1f
Marc Zyngier4d5f9c12017-04-03 19:37:57 +0100139
140reset:
Marc Zyngiere537ecd2016-06-30 18:40:48 +0100141 /* We're now in idmap, disable MMU */
142 mrc p15, 4, r1, c1, c0, 0 @ HSCTLR
Marc Zyngierbc845e42017-04-03 19:37:53 +0100143 ldr r0, =(HSCTLR_M | HSCTLR_A | HSCTLR_C | HSCTLR_I)
144 bic r1, r1, r0
Marc Zyngiere537ecd2016-06-30 18:40:48 +0100145 mcr p15, 4, r1, c1, c0, 0 @ HSCTLR
146
Marc Zyngierbc845e42017-04-03 19:37:53 +0100147 /*
148 * Install stub vectors, using ardb's VA->PA trick.
149 */
1500: adr r0, 0b @ PA(0)
151 movw r1, #:lower16:__hyp_stub_vectors - 0b @ VA(stub) - VA(0)
152 movt r1, #:upper16:__hyp_stub_vectors - 0b
153 add r1, r1, r0 @ PA(stub)
154 mcr p15, 4, r1, c12, c0, 0 @ HVBAR
155 b exit
Marc Zyngiere537ecd2016-06-30 18:40:48 +0100156
Marc Zyngierbc845e42017-04-03 19:37:53 +01001571: ldr r0, =HVC_STUB_ERR
Marc Zyngierd9118c82017-04-03 19:38:06 +0100158 eret
Marc Zyngierbc845e42017-04-03 19:37:53 +0100159
160exit:
Marc Zyngierd9118c82017-04-03 19:38:06 +0100161 mov r0, #0
Marc Zyngiere537ecd2016-06-30 18:40:48 +0100162 eret
Marc Zyngierbc845e42017-04-03 19:37:53 +0100163ENDPROC(__kvm_handle_stub_hvc)
Marc Zyngiere537ecd2016-06-30 18:40:48 +0100164
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500165 .ltorg
166
167 .globl __kvm_hyp_init_end
168__kvm_hyp_init_end:
169
170 .popsection