blob: 0d82ae921b9ca4b7d01db2580dfab16af9ed36bd [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
18#include "hyp.h"
19
Marc Zyngier32876222015-10-28 14:15:45 +000020static bool __hyp_text __fpsimd_enabled_nvhe(void)
21{
22 return !(read_sysreg(cptr_el2) & CPTR_EL2_TFP);
23}
24
25static bool __hyp_text __fpsimd_enabled_vhe(void)
26{
27 return !!(read_sysreg(cpacr_el1) & CPACR_EL1_FPEN);
28}
29
30static hyp_alternate_select(__fpsimd_is_enabled,
31 __fpsimd_enabled_nvhe, __fpsimd_enabled_vhe,
32 ARM64_HAS_VIRT_HOST_EXTN);
33
34bool __hyp_text __fpsimd_enabled(void)
35{
36 return __fpsimd_is_enabled()();
37}
38
Marc Zyngierbe901e92015-10-21 09:57:10 +010039static void __hyp_text __activate_traps(struct kvm_vcpu *vcpu)
40{
41 u64 val;
42
43 /*
44 * We are about to set CPTR_EL2.TFP to trap all floating point
45 * register accesses to EL2, however, the ARM ARM clearly states that
46 * traps are only taken to EL2 if the operation would not otherwise
47 * trap to EL1. Therefore, always make sure that for 32-bit guests,
48 * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
49 */
50 val = vcpu->arch.hcr_el2;
51 if (!(val & HCR_RW)) {
52 write_sysreg(1 << 30, fpexc32_el2);
53 isb();
54 }
55 write_sysreg(val, hcr_el2);
56 /* Trap on AArch32 cp15 c15 accesses (EL1 or EL0) */
57 write_sysreg(1 << 15, hstr_el2);
Dave Martina7e0ac22016-01-19 16:20:18 +000058
59 val = CPTR_EL2_DEFAULT;
60 val |= CPTR_EL2_TTA | CPTR_EL2_TFP;
61 write_sysreg(val, cptr_el2);
62
Marc Zyngierbe901e92015-10-21 09:57:10 +010063 write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
64}
65
66static void __hyp_text __deactivate_traps(struct kvm_vcpu *vcpu)
67{
68 write_sysreg(HCR_RW, hcr_el2);
69 write_sysreg(0, hstr_el2);
70 write_sysreg(read_sysreg(mdcr_el2) & MDCR_EL2_HPMN_MASK, mdcr_el2);
Dave Martina7e0ac22016-01-19 16:20:18 +000071 write_sysreg(CPTR_EL2_DEFAULT, cptr_el2);
Marc Zyngierbe901e92015-10-21 09:57:10 +010072}
73
74static void __hyp_text __activate_vm(struct kvm_vcpu *vcpu)
75{
76 struct kvm *kvm = kern_hyp_va(vcpu->kvm);
77 write_sysreg(kvm->arch.vttbr, vttbr_el2);
78}
79
80static void __hyp_text __deactivate_vm(struct kvm_vcpu *vcpu)
81{
82 write_sysreg(0, vttbr_el2);
83}
84
85static hyp_alternate_select(__vgic_call_save_state,
86 __vgic_v2_save_state, __vgic_v3_save_state,
87 ARM64_HAS_SYSREG_GIC_CPUIF);
88
89static hyp_alternate_select(__vgic_call_restore_state,
90 __vgic_v2_restore_state, __vgic_v3_restore_state,
91 ARM64_HAS_SYSREG_GIC_CPUIF);
92
93static void __hyp_text __vgic_save_state(struct kvm_vcpu *vcpu)
94{
95 __vgic_call_save_state()(vcpu);
96 write_sysreg(read_sysreg(hcr_el2) & ~HCR_INT_OVERRIDE, hcr_el2);
97}
98
99static void __hyp_text __vgic_restore_state(struct kvm_vcpu *vcpu)
100{
101 u64 val;
102
103 val = read_sysreg(hcr_el2);
104 val |= HCR_INT_OVERRIDE;
105 val |= vcpu->arch.irq_lines;
106 write_sysreg(val, hcr_el2);
107
108 __vgic_call_restore_state()(vcpu);
109}
110
Marc Zyngier3ffa75c2015-10-26 09:10:07 +0000111static int __hyp_text __guest_run(struct kvm_vcpu *vcpu)
Marc Zyngierbe901e92015-10-21 09:57:10 +0100112{
113 struct kvm_cpu_context *host_ctxt;
114 struct kvm_cpu_context *guest_ctxt;
Marc Zyngierc13d1682015-10-26 08:34:09 +0000115 bool fp_enabled;
Marc Zyngierbe901e92015-10-21 09:57:10 +0100116 u64 exit_code;
117
118 vcpu = kern_hyp_va(vcpu);
119 write_sysreg(vcpu, tpidr_el2);
120
121 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
122 guest_ctxt = &vcpu->arch.ctxt;
123
Marc Zyngieredef5282015-10-28 12:17:35 +0000124 __sysreg_save_host_state(host_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100125 __debug_cond_save_host_state(vcpu);
126
127 __activate_traps(vcpu);
128 __activate_vm(vcpu);
129
130 __vgic_restore_state(vcpu);
131 __timer_restore_state(vcpu);
132
133 /*
134 * We must restore the 32-bit state before the sysregs, thanks
135 * to Cortex-A57 erratum #852523.
136 */
137 __sysreg32_restore_state(vcpu);
Marc Zyngieredef5282015-10-28 12:17:35 +0000138 __sysreg_restore_guest_state(guest_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100139 __debug_restore_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
140
141 /* Jump in the fire! */
142 exit_code = __guest_enter(vcpu, host_ctxt);
143 /* And we're baaack! */
144
Marc Zyngierc13d1682015-10-26 08:34:09 +0000145 fp_enabled = __fpsimd_enabled();
146
Marc Zyngieredef5282015-10-28 12:17:35 +0000147 __sysreg_save_guest_state(guest_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100148 __sysreg32_save_state(vcpu);
149 __timer_save_state(vcpu);
150 __vgic_save_state(vcpu);
151
152 __deactivate_traps(vcpu);
153 __deactivate_vm(vcpu);
154
Marc Zyngieredef5282015-10-28 12:17:35 +0000155 __sysreg_restore_host_state(host_ctxt);
Marc Zyngierbe901e92015-10-21 09:57:10 +0100156
Marc Zyngierc13d1682015-10-26 08:34:09 +0000157 if (fp_enabled) {
158 __fpsimd_save_state(&guest_ctxt->gp_regs.fp_regs);
159 __fpsimd_restore_state(&host_ctxt->gp_regs.fp_regs);
160 }
161
Marc Zyngierbe901e92015-10-21 09:57:10 +0100162 __debug_save_state(vcpu, kern_hyp_va(vcpu->arch.debug_ptr), guest_ctxt);
163 __debug_cond_restore_host_state(vcpu);
164
165 return exit_code;
166}
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000167
Marc Zyngier3ffa75c2015-10-26 09:10:07 +0000168__alias(__guest_run) int __kvm_vcpu_run(struct kvm_vcpu *vcpu);
Marc Zyngier044ac372015-10-25 13:58:00 +0000169
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000170static const char __hyp_panic_string[] = "HYP panic:\nPS:%08llx PC:%016llx ESR:%08llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%p\n";
171
172void __hyp_text __noreturn __hyp_panic(void)
173{
174 unsigned long str_va = (unsigned long)__hyp_panic_string;
175 u64 spsr = read_sysreg(spsr_el2);
176 u64 elr = read_sysreg(elr_el2);
177 u64 par = read_sysreg(par_el1);
178
179 if (read_sysreg(vttbr_el2)) {
180 struct kvm_vcpu *vcpu;
181 struct kvm_cpu_context *host_ctxt;
182
183 vcpu = (struct kvm_vcpu *)read_sysreg(tpidr_el2);
184 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
185 __deactivate_traps(vcpu);
186 __deactivate_vm(vcpu);
Marc Zyngieredef5282015-10-28 12:17:35 +0000187 __sysreg_restore_host_state(host_ctxt);
Marc Zyngier53fd5b62015-10-25 15:21:52 +0000188 }
189
190 /* Call panic for real */
191 __hyp_do_panic(hyp_kern_va(str_va),
192 spsr, elr,
193 read_sysreg(esr_el2), read_sysreg(far_el2),
194 read_sysreg(hpfar_el2), par,
195 (void *)read_sysreg(tpidr_el2));
196
197 unreachable();
198}