blob: b2d229f09c0777d3dedcf5e6cfc7fbe4ea19addc [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>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050026
27/********************************************************************
28 * Hypervisor initialization
29 * - should be called with:
Marc Zyngier5a677ce2013-04-12 19:12:06 +010030 * r0 = top of Hyp stack (kernel VA)
31 * r1 = pointer to hyp vectors
32 * r2,r3 = Hypervisor pgd pointer
33 *
34 * The init scenario is:
35 * - We jump in HYP with four parameters: boot HYP pgd, runtime HYP pgd,
36 * runtime stack, runtime vectors
37 * - Enable the MMU with the boot pgd
38 * - Jump to a target into the trampoline page (remember, this is the same
39 * physical page!)
40 * - Now switch to the runtime pgd (same VA, and still the same physical
41 * page!)
42 * - Invalidate TLBs
43 * - Set stack and vectors
44 * - Profit! (or eret, if you only care about the code).
45 *
46 * As we only have four registers available to pass parameters (and we
47 * need six), we split the init in two phases:
48 * - Phase 1: r0 = 0, r1 = 0, r2,r3 contain the boot PGD.
49 * Provides the basic HYP init, and enable the MMU.
50 * - Phase 2: r0 = ToS, r1 = vectors, r2,r3 contain the runtime PGD.
51 * Switches to the runtime PGD, set stack and vectors.
Christoffer Dall342cd0a2013-01-20 18:28:06 -050052 */
53
54 .text
55 .pushsection .hyp.idmap.text,"ax"
56 .align 5
57__kvm_hyp_init:
58 .globl __kvm_hyp_init
59
60 @ Hyp-mode exception vector
61 W(b) .
62 W(b) .
63 W(b) .
64 W(b) .
65 W(b) .
66 W(b) __do_hyp_init
67 W(b) .
68 W(b) .
69
70__do_hyp_init:
Marc Zyngier5a677ce2013-04-12 19:12:06 +010071 cmp r0, #0 @ We have a SP?
72 bne phase2 @ Yes, second stage init
73
Christoffer Dall342cd0a2013-01-20 18:28:06 -050074 @ Set the HTTBR to point to the hypervisor PGD pointer passed
Marc Zyngier5a677ce2013-04-12 19:12:06 +010075 mcrr p15, 4, r2, r3, c2
Christoffer Dall342cd0a2013-01-20 18:28:06 -050076
77 @ Set the HTCR and VTCR to the same shareability and cacheability
78 @ settings as the non-secure TTBCR and with T0SZ == 0.
79 mrc p15, 4, r0, c2, c0, 2 @ HTCR
Marc Zyngier5a677ce2013-04-12 19:12:06 +010080 ldr r2, =HTCR_MASK
81 bic r0, r0, r2
Christoffer Dall342cd0a2013-01-20 18:28:06 -050082 mrc p15, 0, r1, c2, c0, 2 @ TTBCR
83 and r1, r1, #(HTCR_MASK & ~TTBCR_T0SZ)
84 orr r0, r0, r1
85 mcr p15, 4, r0, c2, c0, 2 @ HTCR
86
87 mrc p15, 4, r1, c2, c1, 2 @ VTCR
Marc Zyngier5a677ce2013-04-12 19:12:06 +010088 ldr r2, =VTCR_MASK
89 bic r1, r1, r2
Christoffer Dall342cd0a2013-01-20 18:28:06 -050090 bic r0, r0, #(~VTCR_HTCR_SH) @ clear non-reusable HTCR bits
91 orr r1, r0, r1
92 orr r1, r1, #(KVM_VTCR_SL0 | KVM_VTCR_T0SZ | KVM_VTCR_S)
93 mcr p15, 4, r1, c2, c1, 2 @ VTCR
94
95 @ Use the same memory attributes for hyp. accesses as the kernel
96 @ (copy MAIRx ro HMAIRx).
97 mrc p15, 0, r0, c10, c2, 0
98 mcr p15, 4, r0, c10, c2, 0
99 mrc p15, 0, r0, c10, c2, 1
100 mcr p15, 4, r0, c10, c2, 1
101
102 @ Set the HSCTLR to:
103 @ - ARM/THUMB exceptions: Kernel config (Thumb-2 kernel)
104 @ - Endianness: Kernel config
105 @ - Fast Interrupt Features: Kernel config
106 @ - Write permission implies XN: disabled
107 @ - Instruction cache: enabled
108 @ - Data/Unified cache: enabled
109 @ - Memory alignment checks: enabled
110 @ - MMU: enabled (this code must be run from an identity mapping)
111 mrc p15, 4, r0, c1, c0, 0 @ HSCR
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100112 ldr r2, =HSCTLR_MASK
113 bic r0, r0, r2
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500114 mrc p15, 0, r1, c1, c0, 0 @ SCTLR
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100115 ldr r2, =(HSCTLR_EE | HSCTLR_FI | HSCTLR_I | HSCTLR_C)
116 and r1, r1, r2
117 ARM( ldr r2, =(HSCTLR_M | HSCTLR_A) )
118 THUMB( ldr r2, =(HSCTLR_M | HSCTLR_A | HSCTLR_TE) )
119 orr r1, r1, r2
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500120 orr r0, r0, r1
121 isb
122 mcr p15, 4, r0, c1, c0, 0 @ HSCR
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500123
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100124 @ End of init phase-1
125 eret
126
127phase2:
128 @ Set stack pointer
129 mov sp, r0
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500130
131 @ Set HVBAR to point to the HYP vectors
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100132 mcr p15, 4, r1, c12, c0, 0 @ HVBAR
133
134 @ Jump to the trampoline page
135 ldr r0, =TRAMPOLINE_VA
136 adr r1, target
137 bfi r0, r1, #0, #PAGE_SHIFT
Russell King6ebbf2c2014-06-30 16:29:12 +0100138 ret r0
Marc Zyngier5a677ce2013-04-12 19:12:06 +0100139
140target: @ We're now in the trampoline code, switch page tables
141 mcrr p15, 4, r2, r3, c2
142 isb
143
144 @ Invalidate the old TLBs
145 mcr p15, 4, r0, c8, c7, 0 @ TLBIALLH
Will Deacone3ab5472013-05-13 12:08:06 +0100146 dsb ish
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500147
148 eret
149
150 .ltorg
151
152 .globl __kvm_hyp_init_end
153__kvm_hyp_init_end:
154
155 .popsection