blob: e609942ef79c8a5a84b5d8593690c2dda6894603 [file] [log] [blame]
Marc Zyngierbe901e92015-10-21 09:57:10 +01001/*
2 * Copyright (C) 2015 - 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
Marc Zyngier68908bf2015-01-29 15:47:55 +000018#include <asm/kvm_asm.h>
19
Marc Zyngierbe901e92015-10-21 09:57:10 +010020#include "hyp.h"
21
Marc Zyngier32876222015-10-28 14:15:45 +000022static bool __hyp_text __fpsimd_enabled_nvhe(void)
23{
24 return !(read_sysreg(cptr_el2) & CPTR_EL2_TFP);
25}
26
27static bool __hyp_text __fpsimd_enabled_vhe(void)
28{
29 return !!(read_sysreg(cpacr_el1) & CPACR_EL1_FPEN);
30}
31
32static hyp_alternate_select(__fpsimd_is_enabled,
33 __fpsimd_enabled_nvhe, __fpsimd_enabled_vhe,
34 ARM64_HAS_VIRT_HOST_EXTN);
35
36bool __hyp_text __fpsimd_enabled(void)
37{
38 return __fpsimd_is_enabled()();
39}
40
Marc Zyngier68908bf2015-01-29 15:47:55 +000041static void __hyp_text __activate_traps_vhe(void)
42{
43 u64 val;
44
45 val = read_sysreg(cpacr_el1);
46 val |= CPACR_EL1_TTA;
47 val &= ~CPACR_EL1_FPEN;
48 write_sysreg(val, cpacr_el1);
49
50 write_sysreg(__kvm_hyp_vector, vbar_el1);
51}
52
53static void __hyp_text __activate_traps_nvhe(void)
54{
55 u64 val;
56
57 val = CPTR_EL2_DEFAULT;
58 val |= CPTR_EL2_TTA | CPTR_EL2_TFP;
59 write_sysreg(val, cptr_el2);
60}
61
62static hyp_alternate_select(__activate_traps_arch,
63 __activate_traps_nvhe, __activate_traps_vhe,
64 ARM64_HAS_VIRT_HOST_EXTN);
65
Marc Zyngierbe901e92015-10-21 09:57:10 +010066static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
67{
68 u64 val;
69
70 /*
71 * We are about to set CPTR_EL2.TFP to trap all floating point
72 * register accesses to EL2, however, the ARM ARM clearly states that
73 * traps are only taken to EL2 if the operation would not otherwise
74 * trap to EL1. Therefore, always make sure that for 32-bit guests,
75 * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
76 */
77 val = vcpu->arch.hcr_el2;
78 if (!(val & HCR_RW)) {
79 write_sysreg(1 << 30, fpexc32_el2);
80 isb();
81 }
82 write_sysreg(val, hcr_el2);
83 /* Trap on AArch32 cp15 c15 accesses (EL1 or EL0) */
84 write_sysreg(1 << 15, hstr_el2);
Marc Zyngierbe901e92015-10-21 09:57:10 +010085 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
Marc Zyngier68908bf2015-01-29 15:47:55 +000086 __activate_traps_arch()();
Marc Zyngierbe901e92015-10-21 09:57:10 +010087}
88
Marc Zyngier68908bf2015-01-29 15:47:55 +000089static void __hyp_text __deactivate_traps_vhe(void)
90{
91 extern char vectors[]; /* kernel exception vectors */
92
93 write_sysreg(HCR_HOST_VHE_FLAGS, hcr_el2);
94 write_sysreg(CPACR_EL1_FPEN, cpacr_el1);
95 write_sysreg(vectors, vbar_el1);
96}
97
98static void __hyp_text __deactivate_traps_nvhe(void)
99{
100 write_sysreg(HCR_RW, hcr_el2);
101 write_sysreg(CPTR_EL2_DEFAULT, cptr_el2);
102}
103
104static hyp_alternate_select(__deactivate_traps_arch,
105 __deactivate_traps_nvhe, __deactivate_traps_vhe,
106 ARM64_HAS_VIRT_HOST_EXTN);
107
Marc Zyngierbe901e92015-10-21 09:57:10 +0100108static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
109{
Marc Zyngier68908bf2015-01-29 15:47:55 +0000110 __deactivate_traps_arch()();
Marc Zyngierbe901e92015-10-21 09:57:10 +0100111 write_sysreg(0, hstr_el2);
112 write_sysreg(read_sysreg(mdcr_el2) & MDCR_EL2_HPMN_MASK, mdcr_el2);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100113}
114
115static void __hyp_text __activate_vm(struct kvm_vcpu *vcpu)
116{
117 struct kvm *kvm = kern_hyp_va(vcpu->kvm);
118 write_sysreg(kvm->arch.vttbr, vttbr_el2);
119}
120
121static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu)
122{
123 write_sysreg(0, vttbr_el2);
124}
125
126static hyp_alternate_select(__vgic_call_save_state,
127 __vgic_v2_save_state, __vgic_v3_save_state,
128 ARM64_HAS_SYSREG_GIC_CPUIF);
129
130static hyp_alternate_select(__vgic_call_restore_state,
131 __vgic_v2_restore_state, __vgic_v3_restore_state,
132 ARM64_HAS_SYSREG_GIC_CPUIF);
133
134static void __hyp_text __vgic_save_state(struct kvm_vcpu *vcpu)
135{
136 __vgic_call_save_state()(vcpu);
137 write_sysreg(read_sysreg(hcr_el2) & ~HCR_INT_OVERRIDE, hcr_el2);
138}
139
140static void __hyp_text __vgic_restore_state(struct kvm_vcpu *vcpu)
141{
142 u64 val;
143
144 val = read_sysreg(hcr_el2);
145 val |= HCR_INT_OVERRIDE;
146 val |= vcpu->arch.irq_lines;
147 write_sysreg(val, hcr_el2);
148
149 __vgic_call_restore_state()(vcpu);
150}
151
Marc Zyngier3ffa75c2015-10-26 09:10:07 +0000152static int __hyp_text __guest_run(struct kvm_vcpu *vcpu)
Marc Zyngierbe901e92015-10-21 09:57:10 +0100153{
154 struct kvm_cpu_context *host_ctxt;
155 struct kvm_cpu_context *guest_ctxt;
Marc Zyngierc13d1682015-10-26 08:34:09 +0000156 bool fp_enabled;
Marc Zyngierbe901e92015-10-21 09:57:10 +0100157 u64 exit_code;
158
159 vcpu = kern_hyp_va(vcpu);
160 write_sysreg(vcpu, tpidr_el2);
161
162 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
163 guest_ctxt = &vcpu->arch.ctxt;
164
Marc Zyngieredef5282015-10-28 12:17:35 +0000165 __sysreg_save_host_state(host_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100166 __debug_cond_save_host_state(vcpu);
167
168 __activate_traps(vcpu);
169 __activate_vm(vcpu);
170
171 __vgic_restore_state(vcpu);
172 __timer_restore_state(vcpu);
173
174 /*
175 * We must restore the 32-bit state before the sysregs, thanks
176 * to Cortex-A57 erratum #852523.
177 */
178 __sysreg32_restore_state(vcpu);
Marc Zyngieredef5282015-10-28 12:17:35 +0000179 __sysreg_restore_guest_state(guest_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100180 __debug_restore_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
181
182 /* Jump in the fire! */
183 exit_code = __guest_enter(vcpu, host_ctxt);
184 /* And we're baaack! */
185
Marc Zyngierc13d1682015-10-26 08:34:09 +0000186 fp_enabled = __fpsimd_enabled();
187
Marc Zyngieredef5282015-10-28 12:17:35 +0000188 __sysreg_save_guest_state(guest_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100189 __sysreg32_save_state(vcpu);
190 __timer_save_state(vcpu);
191 __vgic_save_state(vcpu);
192
193 __deactivate_traps(vcpu);
194 __deactivate_vm(vcpu);
195
Marc Zyngieredef5282015-10-28 12:17:35 +0000196 __sysreg_restore_host_state(host_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100197
Marc Zyngierc13d1682015-10-26 08:34:09 +0000198 if (fp_enabled) {
199 __fpsimd_save_state(&guest_ctxt->gp_regs.fp_regs);
200 __fpsimd_restore_state(&host_ctxt->gp_regs.fp_regs);
201 }
202
Marc Zyngierbe901e92015-10-21 09:57:10 +0100203 __debug_save_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
204 __debug_cond_restore_host_state(vcpu);
205
206 return exit_code;
207}
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000208
Marc Zyngier3ffa75c2015-10-26 09:10:07 +0000209__alias(__guest_run) int __kvm_vcpu_run(struct kvm_vcpu *vcpu);
Marc Zyngier044ac372015-10-25 13:58:00 +0000210
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000211static const char __hyp_panic_string[] = "HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n";
212
213void __hyp_text __noreturn __hyp_panic(void)
214{
215 unsigned long str_va = (unsigned long)__hyp_panic_string;
216 u64 spsr = read_sysreg(spsr_el2);
217 u64 elr = read_sysreg(elr_el2);
218 u64 par = read_sysreg(par_el1);
219
220 if (read_sysreg(vttbr_el2)) {
221 struct kvm_vcpu *vcpu;
222 struct kvm_cpu_context *host_ctxt;
223
224 vcpu = (struct kvm_vcpu *)read_sysreg(tpidr_el2);
225 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
226 __deactivate_traps(vcpu);
227 __deactivate_vm(vcpu);
Marc Zyngieredef5282015-10-28 12:17:35 +0000228 __sysreg_restore_host_state(host_ctxt);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000229 }
230
231 /* Call panic for real */
232 __hyp_do_panic(hyp_kern_va(str_va),
233 spsr, elr,
234 read_sysreg(esr_el2), read_sysreg(far_el2),
235 read_sysreg(hpfar_el2), par,
236 (void *)read_sysreg(tpidr_el2));
237
238 unreachable();
239}