109cf57ebSDavid Brazdil // SPDX-License-Identifier: GPL-2.0-only
209cf57ebSDavid Brazdil /*
309cf57ebSDavid Brazdil * Copyright (C) 2015 - ARM Ltd
409cf57ebSDavid Brazdil * Author: Marc Zyngier <marc.zyngier@arm.com>
509cf57ebSDavid Brazdil */
609cf57ebSDavid Brazdil
709cf57ebSDavid Brazdil #ifndef __ARM64_KVM_HYP_SWITCH_H__
809cf57ebSDavid Brazdil #define __ARM64_KVM_HYP_SWITCH_H__
909cf57ebSDavid Brazdil
10cdb5e02eSMarc Zyngier #include <hyp/adjust_pc.h>
117dd9b5a1SMarc Zyngier #include <hyp/fault.h>
12cdb5e02eSMarc Zyngier
1309cf57ebSDavid Brazdil #include <linux/arm-smccc.h>
1409cf57ebSDavid Brazdil #include <linux/kvm_host.h>
1509cf57ebSDavid Brazdil #include <linux/types.h>
1609cf57ebSDavid Brazdil #include <linux/jump_label.h>
1709cf57ebSDavid Brazdil #include <uapi/linux/psci.h>
1809cf57ebSDavid Brazdil
1909cf57ebSDavid Brazdil #include <kvm/arm_psci.h>
2009cf57ebSDavid Brazdil
2109cf57ebSDavid Brazdil #include <asm/barrier.h>
2209cf57ebSDavid Brazdil #include <asm/cpufeature.h>
23e9ee186bSJames Morse #include <asm/extable.h>
2409cf57ebSDavid Brazdil #include <asm/kprobes.h>
2509cf57ebSDavid Brazdil #include <asm/kvm_asm.h>
2609cf57ebSDavid Brazdil #include <asm/kvm_emulate.h>
2709cf57ebSDavid Brazdil #include <asm/kvm_hyp.h>
2809cf57ebSDavid Brazdil #include <asm/kvm_mmu.h>
29e9adde43SMarc Zyngier #include <asm/kvm_nested.h>
3009cf57ebSDavid Brazdil #include <asm/fpsimd.h>
3109cf57ebSDavid Brazdil #include <asm/debug-monitors.h>
3209cf57ebSDavid Brazdil #include <asm/processor.h>
3309cf57ebSDavid Brazdil
34ae2b2f33SMark Rutland struct kvm_exception_table_entry {
35ae2b2f33SMark Rutland int insn, fixup;
36ae2b2f33SMark Rutland };
37ae2b2f33SMark Rutland
38ae2b2f33SMark Rutland extern struct kvm_exception_table_entry __start___kvm_ex_table;
39ae2b2f33SMark Rutland extern struct kvm_exception_table_entry __stop___kvm_ex_table;
40e9ee186bSJames Morse
41e9ada6c2SMarc Zyngier /* Check whether the FP regs are owned by the guest */
guest_owns_fp_regs(struct kvm_vcpu * vcpu)42e9ada6c2SMarc Zyngier static inline bool guest_owns_fp_regs(struct kvm_vcpu *vcpu)
4309cf57ebSDavid Brazdil {
44f8077b0dSMarc Zyngier return vcpu->arch.fp_state == FP_STATE_GUEST_OWNED;
4509cf57ebSDavid Brazdil }
4609cf57ebSDavid Brazdil
4709cf57ebSDavid Brazdil /* Save the 32-bit only FPSIMD system register state */
__fpsimd_save_fpexc32(struct kvm_vcpu * vcpu)48c50cb043SDavid Brazdil static inline void __fpsimd_save_fpexc32(struct kvm_vcpu *vcpu)
4909cf57ebSDavid Brazdil {
5009cf57ebSDavid Brazdil if (!vcpu_el1_is_32bit(vcpu))
5109cf57ebSDavid Brazdil return;
5209cf57ebSDavid Brazdil
5371071acfSMarc Zyngier __vcpu_sys_reg(vcpu, FPEXC32_EL2) = read_sysreg(fpexc32_el2);
5409cf57ebSDavid Brazdil }
5509cf57ebSDavid Brazdil
__activate_traps_fpsimd32(struct kvm_vcpu * vcpu)56c50cb043SDavid Brazdil static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
5709cf57ebSDavid Brazdil {
5809cf57ebSDavid Brazdil /*
5909cf57ebSDavid Brazdil * We are about to set CPTR_EL2.TFP to trap all floating point
6009cf57ebSDavid Brazdil * register accesses to EL2, however, the ARM ARM clearly states that
6109cf57ebSDavid Brazdil * traps are only taken to EL2 if the operation would not otherwise
6209cf57ebSDavid Brazdil * trap to EL1. Therefore, always make sure that for 32-bit guests,
6309cf57ebSDavid Brazdil * we set FPEXC.EN to prevent traps to EL1, when setting the TFP bit.
6409cf57ebSDavid Brazdil * If FP/ASIMD is not implemented, FPEXC is UNDEFINED and any access to
6509cf57ebSDavid Brazdil * it will cause an exception.
6609cf57ebSDavid Brazdil */
6709cf57ebSDavid Brazdil if (vcpu_el1_is_32bit(vcpu) && system_supports_fpsimd()) {
6809cf57ebSDavid Brazdil write_sysreg(1 << 30, fpexc32_el2);
6909cf57ebSDavid Brazdil isb();
7009cf57ebSDavid Brazdil }
7109cf57ebSDavid Brazdil }
7209cf57ebSDavid Brazdil
73d4d2daccSMarc Zyngier #define compute_clr_set(vcpu, reg, clr, set) \
74d4d2daccSMarc Zyngier do { \
75d4d2daccSMarc Zyngier u64 hfg; \
76d4d2daccSMarc Zyngier hfg = __vcpu_sys_reg(vcpu, reg) & ~__ ## reg ## _RES0; \
77d4d2daccSMarc Zyngier set |= hfg & __ ## reg ## _MASK; \
78d4d2daccSMarc Zyngier clr |= ~hfg & __ ## reg ## _nMASK; \
79d4d2daccSMarc Zyngier } while(0)
80e930694eSMarc Zyngier
81e930694eSMarc Zyngier
__activate_traps_hfgxtr(struct kvm_vcpu * vcpu)82e930694eSMarc Zyngier static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
83ce4a3622SOliver Upton {
84e930694eSMarc Zyngier struct kvm_cpu_context *hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
85ce4a3622SOliver Upton u64 r_clr = 0, w_clr = 0, r_set = 0, w_set = 0, tmp;
86e930694eSMarc Zyngier u64 r_val, w_val;
87e930694eSMarc Zyngier
88e930694eSMarc Zyngier if (!cpus_have_final_cap(ARM64_HAS_FGT))
89e930694eSMarc Zyngier return;
90e930694eSMarc Zyngier
91e930694eSMarc Zyngier ctxt_sys_reg(hctxt, HFGRTR_EL2) = read_sysreg_s(SYS_HFGRTR_EL2);
92e930694eSMarc Zyngier ctxt_sys_reg(hctxt, HFGWTR_EL2) = read_sysreg_s(SYS_HFGWTR_EL2);
93ce4a3622SOliver Upton
94ce4a3622SOliver Upton if (cpus_have_final_cap(ARM64_SME)) {
95ce4a3622SOliver Upton tmp = HFGxTR_EL2_nSMPRI_EL1_MASK | HFGxTR_EL2_nTPIDR2_EL0_MASK;
96ce4a3622SOliver Upton
97ce4a3622SOliver Upton r_clr |= tmp;
98ce4a3622SOliver Upton w_clr |= tmp;
99ce4a3622SOliver Upton }
100ce4a3622SOliver Upton
101082fdfd1SOliver Upton /*
102082fdfd1SOliver Upton * Trap guest writes to TCR_EL1 to prevent it from enabling HA or HD.
103082fdfd1SOliver Upton */
104082fdfd1SOliver Upton if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38))
105082fdfd1SOliver Upton w_set |= HFGxTR_EL2_TCR_EL1_MASK;
106082fdfd1SOliver Upton
107d4d2daccSMarc Zyngier if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
108d4d2daccSMarc Zyngier compute_clr_set(vcpu, HFGRTR_EL2, r_clr, r_set);
109d4d2daccSMarc Zyngier compute_clr_set(vcpu, HFGWTR_EL2, w_clr, w_set);
110ce4a3622SOliver Upton }
111ce4a3622SOliver Upton
112e930694eSMarc Zyngier /* The default is not to trap anything but ACCDATA_EL1 */
113e930694eSMarc Zyngier r_val = __HFGRTR_EL2_nMASK & ~HFGxTR_EL2_nACCDATA_EL1;
114e930694eSMarc Zyngier r_val |= r_set;
115e930694eSMarc Zyngier r_val &= ~r_clr;
116e930694eSMarc Zyngier
117e930694eSMarc Zyngier w_val = __HFGWTR_EL2_nMASK & ~HFGxTR_EL2_nACCDATA_EL1;
118e930694eSMarc Zyngier w_val |= w_set;
119e930694eSMarc Zyngier w_val &= ~w_clr;
120e930694eSMarc Zyngier
121e930694eSMarc Zyngier write_sysreg_s(r_val, SYS_HFGRTR_EL2);
122e930694eSMarc Zyngier write_sysreg_s(w_val, SYS_HFGWTR_EL2);
123d4d2daccSMarc Zyngier
124d4d2daccSMarc Zyngier if (!vcpu_has_nv(vcpu) || is_hyp_ctxt(vcpu))
125d4d2daccSMarc Zyngier return;
126d4d2daccSMarc Zyngier
127d4d2daccSMarc Zyngier ctxt_sys_reg(hctxt, HFGITR_EL2) = read_sysreg_s(SYS_HFGITR_EL2);
128d4d2daccSMarc Zyngier
129d4d2daccSMarc Zyngier r_set = r_clr = 0;
130d4d2daccSMarc Zyngier compute_clr_set(vcpu, HFGITR_EL2, r_clr, r_set);
131d4d2daccSMarc Zyngier r_val = __HFGITR_EL2_nMASK;
132d4d2daccSMarc Zyngier r_val |= r_set;
133d4d2daccSMarc Zyngier r_val &= ~r_clr;
134d4d2daccSMarc Zyngier
135d4d2daccSMarc Zyngier write_sysreg_s(r_val, SYS_HFGITR_EL2);
136d4d2daccSMarc Zyngier
137d4d2daccSMarc Zyngier ctxt_sys_reg(hctxt, HDFGRTR_EL2) = read_sysreg_s(SYS_HDFGRTR_EL2);
138d4d2daccSMarc Zyngier ctxt_sys_reg(hctxt, HDFGWTR_EL2) = read_sysreg_s(SYS_HDFGWTR_EL2);
139d4d2daccSMarc Zyngier
140d4d2daccSMarc Zyngier r_clr = r_set = w_clr = w_set = 0;
141d4d2daccSMarc Zyngier
142d4d2daccSMarc Zyngier compute_clr_set(vcpu, HDFGRTR_EL2, r_clr, r_set);
143d4d2daccSMarc Zyngier compute_clr_set(vcpu, HDFGWTR_EL2, w_clr, w_set);
144d4d2daccSMarc Zyngier
145d4d2daccSMarc Zyngier r_val = __HDFGRTR_EL2_nMASK;
146d4d2daccSMarc Zyngier r_val |= r_set;
147d4d2daccSMarc Zyngier r_val &= ~r_clr;
148d4d2daccSMarc Zyngier
149d4d2daccSMarc Zyngier w_val = __HDFGWTR_EL2_nMASK;
150d4d2daccSMarc Zyngier w_val |= w_set;
151d4d2daccSMarc Zyngier w_val &= ~w_clr;
152d4d2daccSMarc Zyngier
153d4d2daccSMarc Zyngier write_sysreg_s(r_val, SYS_HDFGRTR_EL2);
154d4d2daccSMarc Zyngier write_sysreg_s(w_val, SYS_HDFGWTR_EL2);
155ce4a3622SOliver Upton }
156ce4a3622SOliver Upton
__deactivate_traps_hfgxtr(struct kvm_vcpu * vcpu)157e930694eSMarc Zyngier static inline void __deactivate_traps_hfgxtr(struct kvm_vcpu *vcpu)
158ce4a3622SOliver Upton {
159e930694eSMarc Zyngier struct kvm_cpu_context *hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
160ce4a3622SOliver Upton
161e930694eSMarc Zyngier if (!cpus_have_final_cap(ARM64_HAS_FGT))
162e930694eSMarc Zyngier return;
163ce4a3622SOliver Upton
164e930694eSMarc Zyngier write_sysreg_s(ctxt_sys_reg(hctxt, HFGRTR_EL2), SYS_HFGRTR_EL2);
165e930694eSMarc Zyngier write_sysreg_s(ctxt_sys_reg(hctxt, HFGWTR_EL2), SYS_HFGWTR_EL2);
166ce4a3622SOliver Upton
167d4d2daccSMarc Zyngier if (!vcpu_has_nv(vcpu) || is_hyp_ctxt(vcpu))
168d4d2daccSMarc Zyngier return;
169082fdfd1SOliver Upton
170d4d2daccSMarc Zyngier write_sysreg_s(ctxt_sys_reg(hctxt, HFGITR_EL2), SYS_HFGITR_EL2);
171d4d2daccSMarc Zyngier write_sysreg_s(ctxt_sys_reg(hctxt, HDFGRTR_EL2), SYS_HDFGRTR_EL2);
172d4d2daccSMarc Zyngier write_sysreg_s(ctxt_sys_reg(hctxt, HDFGWTR_EL2), SYS_HDFGWTR_EL2);
173ce4a3622SOliver Upton }
174ce4a3622SOliver Upton
__activate_traps_common(struct kvm_vcpu * vcpu)175c50cb043SDavid Brazdil static inline void __activate_traps_common(struct kvm_vcpu *vcpu)
17609cf57ebSDavid Brazdil {
17709cf57ebSDavid Brazdil /* Trap on AArch32 cp15 c15 (impdef sysregs) accesses (EL1 or EL0) */
17809cf57ebSDavid Brazdil write_sysreg(1 << 15, hstr_el2);
17909cf57ebSDavid Brazdil
18009cf57ebSDavid Brazdil /*
18109cf57ebSDavid Brazdil * Make sure we trap PMU access from EL0 to EL2. Also sanitize
18209cf57ebSDavid Brazdil * PMSELR_EL0 to make sure it never contains the cycle
18309cf57ebSDavid Brazdil * counter, which could make a PMXEVCNTR_EL0 access UNDEF at
18409cf57ebSDavid Brazdil * EL1 instead of being trapped to EL2.
18509cf57ebSDavid Brazdil */
186f27647b5SMarc Zyngier if (kvm_arm_support_pmu_v3()) {
1878681f717SReiji Watanabe struct kvm_cpu_context *hctxt;
1888681f717SReiji Watanabe
18909cf57ebSDavid Brazdil write_sysreg(0, pmselr_el0);
1908681f717SReiji Watanabe
1918681f717SReiji Watanabe hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
1928681f717SReiji Watanabe ctxt_sys_reg(hctxt, PMUSERENR_EL0) = read_sysreg(pmuserenr_el0);
19309cf57ebSDavid Brazdil write_sysreg(ARMV8_PMU_USERENR_MASK, pmuserenr_el0);
1940c2f9acfSReiji Watanabe vcpu_set_flag(vcpu, PMUSERENR_ON_CPU);
195f27647b5SMarc Zyngier }
1961460b4b2SFuad Tabba
1971460b4b2SFuad Tabba vcpu->arch.mdcr_el2_host = read_sysreg(mdcr_el2);
19809cf57ebSDavid Brazdil write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
1994151bb63SMarc Zyngier
20003fb54d0SMarc Zyngier if (cpus_have_final_cap(ARM64_HAS_HCX)) {
20103fb54d0SMarc Zyngier u64 hcrx = HCRX_GUEST_FLAGS;
20203fb54d0SMarc Zyngier if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
20303fb54d0SMarc Zyngier u64 clr = 0, set = 0;
20403fb54d0SMarc Zyngier
20503fb54d0SMarc Zyngier compute_clr_set(vcpu, HCRX_EL2, clr, set);
20603fb54d0SMarc Zyngier
20703fb54d0SMarc Zyngier hcrx |= set;
20803fb54d0SMarc Zyngier hcrx &= ~clr;
20903fb54d0SMarc Zyngier }
21003fb54d0SMarc Zyngier
21103fb54d0SMarc Zyngier write_sysreg_s(hcrx, SYS_HCRX_EL2);
21203fb54d0SMarc Zyngier }
213a63cf311SMarc Zyngier
214e930694eSMarc Zyngier __activate_traps_hfgxtr(vcpu);
21509cf57ebSDavid Brazdil }
21609cf57ebSDavid Brazdil
__deactivate_traps_common(struct kvm_vcpu * vcpu)2171460b4b2SFuad Tabba static inline void __deactivate_traps_common(struct kvm_vcpu *vcpu)
21809cf57ebSDavid Brazdil {
2191460b4b2SFuad Tabba write_sysreg(vcpu->arch.mdcr_el2_host, mdcr_el2);
2201460b4b2SFuad Tabba
22109cf57ebSDavid Brazdil write_sysreg(0, hstr_el2);
2228681f717SReiji Watanabe if (kvm_arm_support_pmu_v3()) {
2238681f717SReiji Watanabe struct kvm_cpu_context *hctxt;
2248681f717SReiji Watanabe
2258681f717SReiji Watanabe hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
2268681f717SReiji Watanabe write_sysreg(ctxt_sys_reg(hctxt, PMUSERENR_EL0), pmuserenr_el0);
2270c2f9acfSReiji Watanabe vcpu_clear_flag(vcpu, PMUSERENR_ON_CPU);
2288681f717SReiji Watanabe }
2294151bb63SMarc Zyngier
230a63cf311SMarc Zyngier if (cpus_have_final_cap(ARM64_HAS_HCX))
231a63cf311SMarc Zyngier write_sysreg_s(HCRX_HOST_FLAGS, SYS_HCRX_EL2);
232a63cf311SMarc Zyngier
233e930694eSMarc Zyngier __deactivate_traps_hfgxtr(vcpu);
23409cf57ebSDavid Brazdil }
23509cf57ebSDavid Brazdil
___activate_traps(struct kvm_vcpu * vcpu)236c50cb043SDavid Brazdil static inline void ___activate_traps(struct kvm_vcpu *vcpu)
23709cf57ebSDavid Brazdil {
23809cf57ebSDavid Brazdil u64 hcr = vcpu->arch.hcr_el2;
23909cf57ebSDavid Brazdil
24009cf57ebSDavid Brazdil if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM))
24109cf57ebSDavid Brazdil hcr |= HCR_TVM;
24209cf57ebSDavid Brazdil
24309cf57ebSDavid Brazdil write_sysreg(hcr, hcr_el2);
24409cf57ebSDavid Brazdil
24509cf57ebSDavid Brazdil if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN) && (hcr & HCR_VSE))
24609cf57ebSDavid Brazdil write_sysreg_s(vcpu->arch.vsesr_el2, SYS_VSESR_EL2);
24709cf57ebSDavid Brazdil }
24809cf57ebSDavid Brazdil
___deactivate_traps(struct kvm_vcpu * vcpu)249c50cb043SDavid Brazdil static inline void ___deactivate_traps(struct kvm_vcpu *vcpu)
25009cf57ebSDavid Brazdil {
25109cf57ebSDavid Brazdil /*
25209cf57ebSDavid Brazdil * If we pended a virtual abort, preserve it until it gets
25309cf57ebSDavid Brazdil * cleared. See D1.14.3 (Virtual Interrupts) for details, but
25409cf57ebSDavid Brazdil * the crucial bit is "On taking a vSError interrupt,
25509cf57ebSDavid Brazdil * HCR_EL2.VSE is cleared to 0."
25609cf57ebSDavid Brazdil */
25709cf57ebSDavid Brazdil if (vcpu->arch.hcr_el2 & HCR_VSE) {
25809cf57ebSDavid Brazdil vcpu->arch.hcr_el2 &= ~HCR_VSE;
25909cf57ebSDavid Brazdil vcpu->arch.hcr_el2 |= read_sysreg(hcr_el2) & HCR_VSE;
26009cf57ebSDavid Brazdil }
26109cf57ebSDavid Brazdil }
26209cf57ebSDavid Brazdil
__populate_fault_info(struct kvm_vcpu * vcpu)263159b859bSQuentin Perret static inline bool __populate_fault_info(struct kvm_vcpu *vcpu)
264159b859bSQuentin Perret {
2658fb20461SMarc Zyngier return __get_fault_info(vcpu->arch.fault.esr_el2, &vcpu->arch.fault);
266159b859bSQuentin Perret }
267159b859bSQuentin Perret
__hyp_sve_restore_guest(struct kvm_vcpu * vcpu)26852029198SMarc Zyngier static inline void __hyp_sve_restore_guest(struct kvm_vcpu *vcpu)
26952029198SMarc Zyngier {
27052029198SMarc Zyngier sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, SYS_ZCR_EL2);
27152029198SMarc Zyngier __sve_restore_state(vcpu_sve_pffr(vcpu),
27252029198SMarc Zyngier &vcpu->arch.ctxt.fp_regs.fpsr);
27352029198SMarc Zyngier write_sysreg_el1(__vcpu_sys_reg(vcpu, ZCR_EL1), SYS_ZCR);
27452029198SMarc Zyngier }
27552029198SMarc Zyngier
fpsimd_lazy_switch_to_guest(struct kvm_vcpu * vcpu)276*7d566962SMark Rutland static inline void fpsimd_lazy_switch_to_guest(struct kvm_vcpu *vcpu)
277*7d566962SMark Rutland {
278*7d566962SMark Rutland u64 zcr_el1, zcr_el2;
279*7d566962SMark Rutland
280*7d566962SMark Rutland if (!guest_owns_fp_regs(vcpu))
281*7d566962SMark Rutland return;
282*7d566962SMark Rutland
283*7d566962SMark Rutland if (vcpu_has_sve(vcpu)) {
284*7d566962SMark Rutland zcr_el2 = vcpu_sve_max_vq(vcpu) - 1;
285*7d566962SMark Rutland
286*7d566962SMark Rutland write_sysreg_el2(zcr_el2, SYS_ZCR);
287*7d566962SMark Rutland
288*7d566962SMark Rutland zcr_el1 = __vcpu_sys_reg(vcpu, ZCR_EL1);
289*7d566962SMark Rutland write_sysreg_el1(zcr_el1, SYS_ZCR);
290*7d566962SMark Rutland }
291*7d566962SMark Rutland }
292*7d566962SMark Rutland
fpsimd_lazy_switch_to_host(struct kvm_vcpu * vcpu)293*7d566962SMark Rutland static inline void fpsimd_lazy_switch_to_host(struct kvm_vcpu *vcpu)
294*7d566962SMark Rutland {
295*7d566962SMark Rutland u64 zcr_el1, zcr_el2;
296*7d566962SMark Rutland
297*7d566962SMark Rutland if (!guest_owns_fp_regs(vcpu))
298*7d566962SMark Rutland return;
299*7d566962SMark Rutland
300*7d566962SMark Rutland /*
301*7d566962SMark Rutland * When the guest owns the FP regs, we know that guest+hyp traps for
302*7d566962SMark Rutland * any FPSIMD/SVE/SME features exposed to the guest have been disabled
303*7d566962SMark Rutland * by either fpsimd_lazy_switch_to_guest() or kvm_hyp_handle_fpsimd()
304*7d566962SMark Rutland * prior to __guest_entry(). As __guest_entry() guarantees a context
305*7d566962SMark Rutland * synchronization event, we don't need an ISB here to avoid taking
306*7d566962SMark Rutland * traps for anything that was exposed to the guest.
307*7d566962SMark Rutland */
308*7d566962SMark Rutland if (vcpu_has_sve(vcpu)) {
309*7d566962SMark Rutland zcr_el1 = read_sysreg_el1(SYS_ZCR);
310*7d566962SMark Rutland __vcpu_sys_reg(vcpu, ZCR_EL1) = zcr_el1;
311*7d566962SMark Rutland
312*7d566962SMark Rutland /*
313*7d566962SMark Rutland * The guest's state is always saved using the guest's max VL.
314*7d566962SMark Rutland * Ensure that the host has the guest's max VL active such that
315*7d566962SMark Rutland * the host can save the guest's state lazily, but don't
316*7d566962SMark Rutland * artificially restrict the host to the guest's max VL.
317*7d566962SMark Rutland */
318*7d566962SMark Rutland if (has_vhe()) {
319*7d566962SMark Rutland zcr_el2 = vcpu_sve_max_vq(vcpu) - 1;
320*7d566962SMark Rutland write_sysreg_el2(zcr_el2, SYS_ZCR);
321*7d566962SMark Rutland } else {
322*7d566962SMark Rutland zcr_el2 = sve_vq_from_vl(kvm_host_sve_max_vl) - 1;
323*7d566962SMark Rutland write_sysreg_el2(zcr_el2, SYS_ZCR);
324*7d566962SMark Rutland
325*7d566962SMark Rutland zcr_el1 = vcpu_sve_max_vq(vcpu) - 1;
326*7d566962SMark Rutland write_sysreg_el1(zcr_el1, SYS_ZCR);
327*7d566962SMark Rutland }
328*7d566962SMark Rutland }
329*7d566962SMark Rutland }
330*7d566962SMark Rutland
3318fb20461SMarc Zyngier /*
3328fb20461SMarc Zyngier * We trap the first access to the FP/SIMD to save the host context and
3338fb20461SMarc Zyngier * restore the guest context lazily.
3348fb20461SMarc Zyngier * If FP/SIMD is not implemented, handle the trap and inject an undefined
3358fb20461SMarc Zyngier * instruction exception to the guest. Similarly for trapped SVE accesses.
3368fb20461SMarc Zyngier */
kvm_hyp_handle_fpsimd(struct kvm_vcpu * vcpu,u64 * exit_code)33793074abeSMark Rutland static inline bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)
33809cf57ebSDavid Brazdil {
3398383741aSMarc Zyngier bool sve_guest;
340a394cf6eSMarc Zyngier u8 esr_ec;
3418c8010d6SMarc Zyngier u64 reg;
34209cf57ebSDavid Brazdil
34309cf57ebSDavid Brazdil if (!system_supports_fpsimd())
34409cf57ebSDavid Brazdil return false;
34509cf57ebSDavid Brazdil
34609cf57ebSDavid Brazdil sve_guest = vcpu_has_sve(vcpu);
347a394cf6eSMarc Zyngier esr_ec = kvm_vcpu_trap_get_class(vcpu);
34809cf57ebSDavid Brazdil
349d071cefdSMark Brown /* Only handle traps the vCPU can support here: */
350d071cefdSMark Brown switch (esr_ec) {
351d071cefdSMark Brown case ESR_ELx_EC_FP_ASIMD:
352d071cefdSMark Brown break;
353d071cefdSMark Brown case ESR_ELx_EC_SVE:
354d071cefdSMark Brown if (!sve_guest)
35509cf57ebSDavid Brazdil return false;
356d071cefdSMark Brown break;
357d071cefdSMark Brown default:
358d071cefdSMark Brown return false;
359d071cefdSMark Brown }
36009cf57ebSDavid Brazdil
36109cf57ebSDavid Brazdil /* Valid trap. Switch the context: */
36201a244deSMark Brown
36301a244deSMark Brown /* First disable enough traps to allow us to update the registers */
36475c76ab5SMarc Zyngier if (has_vhe() || has_hvhe()) {
3653bb72d86SMark Brown reg = CPACR_EL1_FPEN_EL0EN | CPACR_EL1_FPEN_EL1EN;
36609cf57ebSDavid Brazdil if (sve_guest)
3673bb72d86SMark Brown reg |= CPACR_EL1_ZEN_EL0EN | CPACR_EL1_ZEN_EL1EN;
36809cf57ebSDavid Brazdil
3698c8010d6SMarc Zyngier sysreg_clear_set(cpacr_el1, 0, reg);
37009cf57ebSDavid Brazdil } else {
3718c8010d6SMarc Zyngier reg = CPTR_EL2_TFP;
3728c8010d6SMarc Zyngier if (sve_guest)
3738c8010d6SMarc Zyngier reg |= CPTR_EL2_TZ;
37409cf57ebSDavid Brazdil
3758c8010d6SMarc Zyngier sysreg_clear_set(cptr_el2, reg, 0);
3768c8010d6SMarc Zyngier }
37709cf57ebSDavid Brazdil isb();
37809cf57ebSDavid Brazdil
37901a244deSMark Brown /* Restore the guest state */
38052029198SMarc Zyngier if (sve_guest)
38152029198SMarc Zyngier __hyp_sve_restore_guest(vcpu);
38252029198SMarc Zyngier else
383e47c2055SMarc Zyngier __fpsimd_restore_state(&vcpu->arch.ctxt.fp_regs);
38409cf57ebSDavid Brazdil
38509cf57ebSDavid Brazdil /* Skip restoring fpexc32 for AArch64 guests */
38609cf57ebSDavid Brazdil if (!(read_sysreg(hcr_el2) & HCR_RW))
38771071acfSMarc Zyngier write_sysreg(__vcpu_sys_reg(vcpu, FPEXC32_EL2), fpexc32_el2);
38809cf57ebSDavid Brazdil
389f8077b0dSMarc Zyngier vcpu->arch.fp_state = FP_STATE_GUEST_OWNED;
39009cf57ebSDavid Brazdil
39109cf57ebSDavid Brazdil return true;
39209cf57ebSDavid Brazdil }
39309cf57ebSDavid Brazdil
handle_tx2_tvm(struct kvm_vcpu * vcpu)394c50cb043SDavid Brazdil static inline bool handle_tx2_tvm(struct kvm_vcpu *vcpu)
39509cf57ebSDavid Brazdil {
396a394cf6eSMarc Zyngier u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
39709cf57ebSDavid Brazdil int rt = kvm_vcpu_sys_get_rt(vcpu);
39809cf57ebSDavid Brazdil u64 val = vcpu_get_reg(vcpu, rt);
39909cf57ebSDavid Brazdil
40009cf57ebSDavid Brazdil /*
40109cf57ebSDavid Brazdil * The normal sysreg handling code expects to see the traps,
40209cf57ebSDavid Brazdil * let's not do anything here.
40309cf57ebSDavid Brazdil */
40409cf57ebSDavid Brazdil if (vcpu->arch.hcr_el2 & HCR_TVM)
40509cf57ebSDavid Brazdil return false;
40609cf57ebSDavid Brazdil
40709cf57ebSDavid Brazdil switch (sysreg) {
40809cf57ebSDavid Brazdil case SYS_SCTLR_EL1:
40909cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_SCTLR);
41009cf57ebSDavid Brazdil break;
41109cf57ebSDavid Brazdil case SYS_TTBR0_EL1:
41209cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_TTBR0);
41309cf57ebSDavid Brazdil break;
41409cf57ebSDavid Brazdil case SYS_TTBR1_EL1:
41509cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_TTBR1);
41609cf57ebSDavid Brazdil break;
41709cf57ebSDavid Brazdil case SYS_TCR_EL1:
41809cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_TCR);
41909cf57ebSDavid Brazdil break;
42009cf57ebSDavid Brazdil case SYS_ESR_EL1:
42109cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_ESR);
42209cf57ebSDavid Brazdil break;
42309cf57ebSDavid Brazdil case SYS_FAR_EL1:
42409cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_FAR);
42509cf57ebSDavid Brazdil break;
42609cf57ebSDavid Brazdil case SYS_AFSR0_EL1:
42709cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_AFSR0);
42809cf57ebSDavid Brazdil break;
42909cf57ebSDavid Brazdil case SYS_AFSR1_EL1:
43009cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_AFSR1);
43109cf57ebSDavid Brazdil break;
43209cf57ebSDavid Brazdil case SYS_MAIR_EL1:
43309cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_MAIR);
43409cf57ebSDavid Brazdil break;
43509cf57ebSDavid Brazdil case SYS_AMAIR_EL1:
43609cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_AMAIR);
43709cf57ebSDavid Brazdil break;
43809cf57ebSDavid Brazdil case SYS_CONTEXTIDR_EL1:
43909cf57ebSDavid Brazdil write_sysreg_el1(val, SYS_CONTEXTIDR);
44009cf57ebSDavid Brazdil break;
44109cf57ebSDavid Brazdil default:
44209cf57ebSDavid Brazdil return false;
44309cf57ebSDavid Brazdil }
44409cf57ebSDavid Brazdil
44509cf57ebSDavid Brazdil __kvm_skip_instr(vcpu);
44609cf57ebSDavid Brazdil return true;
44709cf57ebSDavid Brazdil }
44809cf57ebSDavid Brazdil
esr_is_ptrauth_trap(u64 esr)4490b12620fSAlexandru Elisei static inline bool esr_is_ptrauth_trap(u64 esr)
45009cf57ebSDavid Brazdil {
45109cf57ebSDavid Brazdil switch (esr_sys64_to_sysreg(esr)) {
45209cf57ebSDavid Brazdil case SYS_APIAKEYLO_EL1:
45309cf57ebSDavid Brazdil case SYS_APIAKEYHI_EL1:
45409cf57ebSDavid Brazdil case SYS_APIBKEYLO_EL1:
45509cf57ebSDavid Brazdil case SYS_APIBKEYHI_EL1:
45609cf57ebSDavid Brazdil case SYS_APDAKEYLO_EL1:
45709cf57ebSDavid Brazdil case SYS_APDAKEYHI_EL1:
45809cf57ebSDavid Brazdil case SYS_APDBKEYLO_EL1:
45909cf57ebSDavid Brazdil case SYS_APDBKEYHI_EL1:
46009cf57ebSDavid Brazdil case SYS_APGAKEYLO_EL1:
46109cf57ebSDavid Brazdil case SYS_APGAKEYHI_EL1:
46209cf57ebSDavid Brazdil return true;
46309cf57ebSDavid Brazdil }
46409cf57ebSDavid Brazdil
46509cf57ebSDavid Brazdil return false;
46609cf57ebSDavid Brazdil }
46709cf57ebSDavid Brazdil
4682c3db77cSMarc Zyngier #define __ptrauth_save_key(ctxt, key) \
4692c3db77cSMarc Zyngier do { \
4702c3db77cSMarc Zyngier u64 __val; \
4712c3db77cSMarc Zyngier __val = read_sysreg_s(SYS_ ## key ## KEYLO_EL1); \
4722c3db77cSMarc Zyngier ctxt_sys_reg(ctxt, key ## KEYLO_EL1) = __val; \
4732c3db77cSMarc Zyngier __val = read_sysreg_s(SYS_ ## key ## KEYHI_EL1); \
4742c3db77cSMarc Zyngier ctxt_sys_reg(ctxt, key ## KEYHI_EL1) = __val; \
4752c3db77cSMarc Zyngier } while(0)
47609cf57ebSDavid Brazdil
4774e3393a9SAndrew Scull DECLARE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
4784e3393a9SAndrew Scull
kvm_hyp_handle_ptrauth(struct kvm_vcpu * vcpu,u64 * exit_code)4798fb20461SMarc Zyngier static bool kvm_hyp_handle_ptrauth(struct kvm_vcpu *vcpu, u64 *exit_code)
48009cf57ebSDavid Brazdil {
48109cf57ebSDavid Brazdil struct kvm_cpu_context *ctxt;
48209cf57ebSDavid Brazdil u64 val;
48309cf57ebSDavid Brazdil
4848a049862SMarc Zyngier if (!vcpu_has_ptrauth(vcpu))
48509cf57ebSDavid Brazdil return false;
48609cf57ebSDavid Brazdil
48714ef9d04SMarc Zyngier ctxt = this_cpu_ptr(&kvm_hyp_ctxt);
4882c3db77cSMarc Zyngier __ptrauth_save_key(ctxt, APIA);
4892c3db77cSMarc Zyngier __ptrauth_save_key(ctxt, APIB);
4902c3db77cSMarc Zyngier __ptrauth_save_key(ctxt, APDA);
4912c3db77cSMarc Zyngier __ptrauth_save_key(ctxt, APDB);
4922c3db77cSMarc Zyngier __ptrauth_save_key(ctxt, APGA);
49309cf57ebSDavid Brazdil
49409cf57ebSDavid Brazdil vcpu_ptrauth_enable(vcpu);
49509cf57ebSDavid Brazdil
49609cf57ebSDavid Brazdil val = read_sysreg(hcr_el2);
49709cf57ebSDavid Brazdil val |= (HCR_API | HCR_APK);
49809cf57ebSDavid Brazdil write_sysreg(val, hcr_el2);
49909cf57ebSDavid Brazdil
50009cf57ebSDavid Brazdil return true;
50109cf57ebSDavid Brazdil }
50209cf57ebSDavid Brazdil
kvm_hyp_handle_cntpct(struct kvm_vcpu * vcpu)503e9adde43SMarc Zyngier static bool kvm_hyp_handle_cntpct(struct kvm_vcpu *vcpu)
504e9adde43SMarc Zyngier {
505e9adde43SMarc Zyngier struct arch_timer_context *ctxt;
506e9adde43SMarc Zyngier u32 sysreg;
507e9adde43SMarc Zyngier u64 val;
508e9adde43SMarc Zyngier
509e9adde43SMarc Zyngier /*
510e9adde43SMarc Zyngier * We only get here for 64bit guests, 32bit guests will hit
511e9adde43SMarc Zyngier * the long and winding road all the way to the standard
512e9adde43SMarc Zyngier * handling. Yes, it sucks to be irrelevant.
513e9adde43SMarc Zyngier */
514e9adde43SMarc Zyngier sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
515e9adde43SMarc Zyngier
516e9adde43SMarc Zyngier switch (sysreg) {
517e9adde43SMarc Zyngier case SYS_CNTPCT_EL0:
518e9adde43SMarc Zyngier case SYS_CNTPCTSS_EL0:
51981dc9504SMarc Zyngier if (vcpu_has_nv(vcpu)) {
52081dc9504SMarc Zyngier if (is_hyp_ctxt(vcpu)) {
52181dc9504SMarc Zyngier ctxt = vcpu_hptimer(vcpu);
52281dc9504SMarc Zyngier break;
52381dc9504SMarc Zyngier }
52481dc9504SMarc Zyngier
52581dc9504SMarc Zyngier /* Check for guest hypervisor trapping */
52681dc9504SMarc Zyngier val = __vcpu_sys_reg(vcpu, CNTHCTL_EL2);
52781dc9504SMarc Zyngier if (!vcpu_el2_e2h_is_set(vcpu))
52881dc9504SMarc Zyngier val = (val & CNTHCTL_EL1PCTEN) << 10;
52981dc9504SMarc Zyngier
53081dc9504SMarc Zyngier if (!(val & (CNTHCTL_EL1PCTEN << 10)))
53181dc9504SMarc Zyngier return false;
53281dc9504SMarc Zyngier }
53381dc9504SMarc Zyngier
534e9adde43SMarc Zyngier ctxt = vcpu_ptimer(vcpu);
535e9adde43SMarc Zyngier break;
536e9adde43SMarc Zyngier default:
537e9adde43SMarc Zyngier return false;
538e9adde43SMarc Zyngier }
539e9adde43SMarc Zyngier
540e9adde43SMarc Zyngier val = arch_timer_read_cntpct_el0();
541e9adde43SMarc Zyngier
542e9adde43SMarc Zyngier if (ctxt->offset.vm_offset)
543e9adde43SMarc Zyngier val -= *kern_hyp_va(ctxt->offset.vm_offset);
5441e0eec09SMarc Zyngier if (ctxt->offset.vcpu_offset)
5451e0eec09SMarc Zyngier val -= *kern_hyp_va(ctxt->offset.vcpu_offset);
546e9adde43SMarc Zyngier
547e9adde43SMarc Zyngier vcpu_set_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu), val);
548e9adde43SMarc Zyngier __kvm_skip_instr(vcpu);
549e9adde43SMarc Zyngier return true;
550e9adde43SMarc Zyngier }
551e9adde43SMarc Zyngier
handle_ampere1_tcr(struct kvm_vcpu * vcpu)552082fdfd1SOliver Upton static bool handle_ampere1_tcr(struct kvm_vcpu *vcpu)
553082fdfd1SOliver Upton {
554082fdfd1SOliver Upton u32 sysreg = esr_sys64_to_sysreg(kvm_vcpu_get_esr(vcpu));
555082fdfd1SOliver Upton int rt = kvm_vcpu_sys_get_rt(vcpu);
556082fdfd1SOliver Upton u64 val = vcpu_get_reg(vcpu, rt);
557082fdfd1SOliver Upton
558082fdfd1SOliver Upton if (sysreg != SYS_TCR_EL1)
559082fdfd1SOliver Upton return false;
560082fdfd1SOliver Upton
561082fdfd1SOliver Upton /*
562082fdfd1SOliver Upton * Affected parts do not advertise support for hardware Access Flag /
563082fdfd1SOliver Upton * Dirty state management in ID_AA64MMFR1_EL1.HAFDBS, but the underlying
564082fdfd1SOliver Upton * control bits are still functional. The architecture requires these be
565082fdfd1SOliver Upton * RES0 on systems that do not implement FEAT_HAFDBS.
566082fdfd1SOliver Upton *
567082fdfd1SOliver Upton * Uphold the requirements of the architecture by masking guest writes
568082fdfd1SOliver Upton * to TCR_EL1.{HA,HD} here.
569082fdfd1SOliver Upton */
570082fdfd1SOliver Upton val &= ~(TCR_HD | TCR_HA);
571082fdfd1SOliver Upton write_sysreg_el1(val, SYS_TCR);
57274158a8cSOliver Upton __kvm_skip_instr(vcpu);
573082fdfd1SOliver Upton return true;
574082fdfd1SOliver Upton }
575082fdfd1SOliver Upton
kvm_hyp_handle_sysreg(struct kvm_vcpu * vcpu,u64 * exit_code)57693074abeSMark Rutland static inline bool kvm_hyp_handle_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code)
5778fb20461SMarc Zyngier {
5788fb20461SMarc Zyngier if (cpus_have_final_cap(ARM64_WORKAROUND_CAVIUM_TX2_219_TVM) &&
5798fb20461SMarc Zyngier handle_tx2_tvm(vcpu))
5808fb20461SMarc Zyngier return true;
5818fb20461SMarc Zyngier
582082fdfd1SOliver Upton if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38) &&
583082fdfd1SOliver Upton handle_ampere1_tcr(vcpu))
584082fdfd1SOliver Upton return true;
585082fdfd1SOliver Upton
5868fb20461SMarc Zyngier if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
5878fb20461SMarc Zyngier __vgic_v3_perform_cpuif_access(vcpu) == 1)
5888fb20461SMarc Zyngier return true;
5898fb20461SMarc Zyngier
5908a049862SMarc Zyngier if (esr_is_ptrauth_trap(kvm_vcpu_get_esr(vcpu)))
5918a049862SMarc Zyngier return kvm_hyp_handle_ptrauth(vcpu, exit_code);
5928a049862SMarc Zyngier
593e9adde43SMarc Zyngier if (kvm_hyp_handle_cntpct(vcpu))
594e9adde43SMarc Zyngier return true;
595e9adde43SMarc Zyngier
5968fb20461SMarc Zyngier return false;
5978fb20461SMarc Zyngier }
5988fb20461SMarc Zyngier
kvm_hyp_handle_cp15_32(struct kvm_vcpu * vcpu,u64 * exit_code)59993074abeSMark Rutland static inline bool kvm_hyp_handle_cp15_32(struct kvm_vcpu *vcpu, u64 *exit_code)
6008fb20461SMarc Zyngier {
6018fb20461SMarc Zyngier if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
6028fb20461SMarc Zyngier __vgic_v3_perform_cpuif_access(vcpu) == 1)
6038fb20461SMarc Zyngier return true;
6048fb20461SMarc Zyngier
6058fb20461SMarc Zyngier return false;
6068fb20461SMarc Zyngier }
6078fb20461SMarc Zyngier
kvm_hyp_handle_memory_fault(struct kvm_vcpu * vcpu,u64 * exit_code)60893074abeSMark Rutland static inline bool kvm_hyp_handle_memory_fault(struct kvm_vcpu *vcpu,
60993074abeSMark Rutland u64 *exit_code)
6108fb20461SMarc Zyngier {
6118fb20461SMarc Zyngier if (!__populate_fault_info(vcpu))
6128fb20461SMarc Zyngier return true;
6138fb20461SMarc Zyngier
6148fb20461SMarc Zyngier return false;
6158fb20461SMarc Zyngier }
61693074abeSMark Rutland #define kvm_hyp_handle_iabt_low kvm_hyp_handle_memory_fault
61793074abeSMark Rutland #define kvm_hyp_handle_watchpt_low kvm_hyp_handle_memory_fault
6188fb20461SMarc Zyngier
kvm_hyp_handle_dabt_low(struct kvm_vcpu * vcpu,u64 * exit_code)61993074abeSMark Rutland static inline bool kvm_hyp_handle_dabt_low(struct kvm_vcpu *vcpu, u64 *exit_code)
6208fb20461SMarc Zyngier {
621811154e2SAkihiko Odaki if (kvm_hyp_handle_memory_fault(vcpu, exit_code))
6228fb20461SMarc Zyngier return true;
6238fb20461SMarc Zyngier
6248fb20461SMarc Zyngier if (static_branch_unlikely(&vgic_v2_cpuif_trap)) {
6258fb20461SMarc Zyngier bool valid;
6268fb20461SMarc Zyngier
627b0803ba7SMarc Zyngier valid = kvm_vcpu_trap_get_fault_type(vcpu) == ESR_ELx_FSC_FAULT &&
6288fb20461SMarc Zyngier kvm_vcpu_dabt_isvalid(vcpu) &&
6298fb20461SMarc Zyngier !kvm_vcpu_abt_issea(vcpu) &&
6308fb20461SMarc Zyngier !kvm_vcpu_abt_iss1tw(vcpu);
6318fb20461SMarc Zyngier
6328fb20461SMarc Zyngier if (valid) {
6338fb20461SMarc Zyngier int ret = __vgic_v2_perform_cpuif_access(vcpu);
6348fb20461SMarc Zyngier
6358fb20461SMarc Zyngier if (ret == 1)
6368fb20461SMarc Zyngier return true;
6378fb20461SMarc Zyngier
6388fb20461SMarc Zyngier /* Promote an illegal access to an SError.*/
6398fb20461SMarc Zyngier if (ret == -1)
6408fb20461SMarc Zyngier *exit_code = ARM_EXCEPTION_EL1_SERROR;
6418fb20461SMarc Zyngier }
6428fb20461SMarc Zyngier }
6438fb20461SMarc Zyngier
6448fb20461SMarc Zyngier return false;
6458fb20461SMarc Zyngier }
6468fb20461SMarc Zyngier
6478fb20461SMarc Zyngier typedef bool (*exit_handler_fn)(struct kvm_vcpu *, u64 *);
6488fb20461SMarc Zyngier
6498fb20461SMarc Zyngier /*
6508fb20461SMarc Zyngier * Allow the hypervisor to handle the exit with an exit handler if it has one.
6518fb20461SMarc Zyngier *
6528fb20461SMarc Zyngier * Returns true if the hypervisor handled the exit, and control should go back
6538fb20461SMarc Zyngier * to the guest, or false if it hasn't.
6548fb20461SMarc Zyngier */
kvm_hyp_handle_exit(struct kvm_vcpu * vcpu,u64 * exit_code,const exit_handler_fn * handlers)6552afe0394SMark Rutland static inline bool kvm_hyp_handle_exit(struct kvm_vcpu *vcpu, u64 *exit_code,
6562afe0394SMark Rutland const exit_handler_fn *handlers)
6578fb20461SMarc Zyngier {
6582afe0394SMark Rutland exit_handler_fn fn = handlers[kvm_vcpu_trap_get_class(vcpu)];
6598fb20461SMarc Zyngier if (fn)
6608fb20461SMarc Zyngier return fn(vcpu, exit_code);
6618fb20461SMarc Zyngier
6628fb20461SMarc Zyngier return false;
6638fb20461SMarc Zyngier }
6648fb20461SMarc Zyngier
synchronize_vcpu_pstate(struct kvm_vcpu * vcpu,u64 * exit_code)6651dd498e5SJames Morse static inline void synchronize_vcpu_pstate(struct kvm_vcpu *vcpu, u64 *exit_code)
6661dd498e5SJames Morse {
6671dd498e5SJames Morse /*
6681dd498e5SJames Morse * Check for the conditions of Cortex-A510's #2077057. When these occur
6691dd498e5SJames Morse * SPSR_EL2 can't be trusted, but isn't needed either as it is
6701dd498e5SJames Morse * unchanged from the value in vcpu_gp_regs(vcpu)->pstate.
6711dd498e5SJames Morse * Are we single-stepping the guest, and took a PAC exception from the
6721dd498e5SJames Morse * active-not-pending state?
6731dd498e5SJames Morse */
6741dd498e5SJames Morse if (cpus_have_final_cap(ARM64_WORKAROUND_2077057) &&
6751dd498e5SJames Morse vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
6761dd498e5SJames Morse *vcpu_cpsr(vcpu) & DBG_SPSR_SS &&
6771dd498e5SJames Morse ESR_ELx_EC(read_sysreg_el2(SYS_ESR)) == ESR_ELx_EC_PAC)
6781dd498e5SJames Morse write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
6791dd498e5SJames Morse
6801dd498e5SJames Morse vcpu->arch.ctxt.regs.pstate = read_sysreg_el2(SYS_SPSR);
6811dd498e5SJames Morse }
6821dd498e5SJames Morse
68309cf57ebSDavid Brazdil /*
68409cf57ebSDavid Brazdil * Return true when we were able to fixup the guest exit and should return to
68509cf57ebSDavid Brazdil * the guest, false when we should restore the host state and return to the
68609cf57ebSDavid Brazdil * main run loop.
68709cf57ebSDavid Brazdil */
__fixup_guest_exit(struct kvm_vcpu * vcpu,u64 * exit_code,const exit_handler_fn * handlers)6882afe0394SMark Rutland static inline bool __fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code,
6892afe0394SMark Rutland const exit_handler_fn *handlers)
69009cf57ebSDavid Brazdil {
69109cf57ebSDavid Brazdil if (ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ)
69209cf57ebSDavid Brazdil vcpu->arch.fault.esr_el2 = read_sysreg_el2(SYS_ESR);
69309cf57ebSDavid Brazdil
6941c71dbc8SJames Morse if (ARM_SERROR_PENDING(*exit_code) &&
6951c71dbc8SJames Morse ARM_EXCEPTION_CODE(*exit_code) != ARM_EXCEPTION_IRQ) {
696defe21f4SMarc Zyngier u8 esr_ec = kvm_vcpu_trap_get_class(vcpu);
697defe21f4SMarc Zyngier
698defe21f4SMarc Zyngier /*
699defe21f4SMarc Zyngier * HVC already have an adjusted PC, which we need to
700defe21f4SMarc Zyngier * correct in order to return to after having injected
701defe21f4SMarc Zyngier * the SError.
702defe21f4SMarc Zyngier *
703defe21f4SMarc Zyngier * SMC, on the other hand, is *trapped*, meaning its
704defe21f4SMarc Zyngier * preferred return address is the SMC itself.
705defe21f4SMarc Zyngier */
706defe21f4SMarc Zyngier if (esr_ec == ESR_ELx_EC_HVC32 || esr_ec == ESR_ELx_EC_HVC64)
707defe21f4SMarc Zyngier write_sysreg_el2(read_sysreg_el2(SYS_ELR) - 4, SYS_ELR);
708defe21f4SMarc Zyngier }
709defe21f4SMarc Zyngier
71009cf57ebSDavid Brazdil /*
71109cf57ebSDavid Brazdil * We're using the raw exception code in order to only process
71209cf57ebSDavid Brazdil * the trap if no SError is pending. We will come back to the
71309cf57ebSDavid Brazdil * same PC once the SError has been injected, and replay the
71409cf57ebSDavid Brazdil * trapping instruction.
71509cf57ebSDavid Brazdil */
71609cf57ebSDavid Brazdil if (*exit_code != ARM_EXCEPTION_TRAP)
71709cf57ebSDavid Brazdil goto exit;
71809cf57ebSDavid Brazdil
7198fb20461SMarc Zyngier /* Check if there's an exit handler and allow it to handle the exit. */
7202afe0394SMark Rutland if (kvm_hyp_handle_exit(vcpu, exit_code, handlers))
72196d389caSRob Herring goto guest;
72209cf57ebSDavid Brazdil exit:
72309cf57ebSDavid Brazdil /* Return to the host kernel and handle the exit */
72409cf57ebSDavid Brazdil return false;
72596d389caSRob Herring
72696d389caSRob Herring guest:
72796d389caSRob Herring /* Re-enter the guest */
72896d389caSRob Herring asm(ALTERNATIVE("nop", "dmb sy", ARM64_WORKAROUND_1508412));
72996d389caSRob Herring return true;
73009cf57ebSDavid Brazdil }
73109cf57ebSDavid Brazdil
__kvm_unexpected_el2_exception(void)732e9ee186bSJames Morse static inline void __kvm_unexpected_el2_exception(void)
733e9ee186bSJames Morse {
7347db21530SAndrew Scull extern char __guest_exit_panic[];
735e9ee186bSJames Morse unsigned long addr, fixup;
736ae2b2f33SMark Rutland struct kvm_exception_table_entry *entry, *end;
737e9ee186bSJames Morse unsigned long elr_el2 = read_sysreg(elr_el2);
738e9ee186bSJames Morse
739247bc166SDavid Brazdil entry = &__start___kvm_ex_table;
740247bc166SDavid Brazdil end = &__stop___kvm_ex_table;
741e9ee186bSJames Morse
742e9ee186bSJames Morse while (entry < end) {
743e9ee186bSJames Morse addr = (unsigned long)&entry->insn + entry->insn;
744e9ee186bSJames Morse fixup = (unsigned long)&entry->fixup + entry->fixup;
745e9ee186bSJames Morse
746e9ee186bSJames Morse if (addr != elr_el2) {
747e9ee186bSJames Morse entry++;
748e9ee186bSJames Morse continue;
749e9ee186bSJames Morse }
750e9ee186bSJames Morse
751e9ee186bSJames Morse write_sysreg(fixup, elr_el2);
752e9ee186bSJames Morse return;
753e9ee186bSJames Morse }
754e9ee186bSJames Morse
7557db21530SAndrew Scull /* Trigger a panic after restoring the hyp context. */
7567db21530SAndrew Scull write_sysreg(__guest_exit_panic, elr_el2);
757e9ee186bSJames Morse }
758e9ee186bSJames Morse
75909cf57ebSDavid Brazdil #endif /* __ARM64_KVM_HYP_SWITCH_H__ */
760