Skip to content

Commit f5a5a40

Browse files
Marc Zyngieroupton
authored andcommitted
KVM: arm64: Propagate and handle Fine-Grained UNDEF bits
In order to correctly honor our FGU bits, they must be converted into a set of FGT bits. They get merged as part of the existing FGT setting. Similarly, the UNDEF injection phase takes place when handling the trap. This results in a bit of rework in the FGT macros in order to help with the code generation, as burying per-CPU accesses in macros results in a lot of expansion, not to mention the vcpu->kvm access on nvhe (kern_hyp_va() is not optimisation-friendly). Reviewed-by: Joey Gouly <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Oliver Upton <[email protected]>
1 parent 2fd8f31 commit f5a5a40

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

arch/arm64/kvm/emulate-nested.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,17 @@ bool triage_sysreg_trap(struct kvm_vcpu *vcpu, int *sr_index)
20172017
if (!tc.val)
20182018
goto local;
20192019

2020+
/*
2021+
* If a sysreg can be trapped using a FGT, first check whether we
2022+
* trap for the purpose of forbidding the feature. In that case,
2023+
* inject an UNDEF.
2024+
*/
2025+
if (tc.fgt != __NO_FGT_GROUP__ &&
2026+
(vcpu->kvm->arch.fgu[tc.fgt] & BIT(tc.bit))) {
2027+
kvm_inject_undefined(vcpu);
2028+
return true;
2029+
}
2030+
20202031
/*
20212032
* If we're not nesting, immediately return to the caller, with the
20222033
* sysreg index, should we have it.

arch/arm64/kvm/hyp/include/hyp/switch.h

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,48 @@ static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
7979
clr |= ~hfg & __ ## reg ## _nMASK; \
8080
} while(0)
8181

82-
#define update_fgt_traps_cs(vcpu, reg, clr, set) \
82+
#define reg_to_fgt_group_id(reg) \
83+
({ \
84+
enum fgt_group_id id; \
85+
switch(reg) { \
86+
case HFGRTR_EL2: \
87+
case HFGWTR_EL2: \
88+
id = HFGxTR_GROUP; \
89+
break; \
90+
case HFGITR_EL2: \
91+
id = HFGITR_GROUP; \
92+
break; \
93+
case HDFGRTR_EL2: \
94+
case HDFGWTR_EL2: \
95+
id = HDFGRTR_GROUP; \
96+
break; \
97+
case HAFGRTR_EL2: \
98+
id = HAFGRTR_GROUP; \
99+
break; \
100+
default: \
101+
BUILD_BUG_ON(1); \
102+
} \
103+
\
104+
id; \
105+
})
106+
107+
#define compute_undef_clr_set(vcpu, kvm, reg, clr, set) \
108+
do { \
109+
u64 hfg = kvm->arch.fgu[reg_to_fgt_group_id(reg)]; \
110+
set |= hfg & __ ## reg ## _MASK; \
111+
clr |= hfg & __ ## reg ## _nMASK; \
112+
} while(0)
113+
114+
#define update_fgt_traps_cs(hctxt, vcpu, kvm, reg, clr, set) \
83115
do { \
84-
struct kvm_cpu_context *hctxt = \
85-
&this_cpu_ptr(&kvm_host_data)->host_ctxt; \
86116
u64 c = 0, s = 0; \
87117
\
88118
ctxt_sys_reg(hctxt, reg) = read_sysreg_s(SYS_ ## reg); \
89-
compute_clr_set(vcpu, reg, c, s); \
119+
if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) \
120+
compute_clr_set(vcpu, reg, c, s); \
121+
\
122+
compute_undef_clr_set(vcpu, kvm, reg, c, s); \
123+
\
90124
s |= set; \
91125
c |= clr; \
92126
if (c || s) { \
@@ -97,8 +131,8 @@ static inline void __activate_traps_fpsimd32(struct kvm_vcpu *vcpu)
97131
} \
98132
} while(0)
99133

100-
#define update_fgt_traps(vcpu, reg) \
101-
update_fgt_traps_cs(vcpu, reg, 0, 0)
134+
#define update_fgt_traps(hctxt, vcpu, kvm, reg) \
135+
update_fgt_traps_cs(hctxt, vcpu, kvm, reg, 0, 0)
102136

103137
/*
104138
* Validate the fine grain trap masks.
@@ -122,6 +156,7 @@ static inline bool cpu_has_amu(void)
122156
static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
123157
{
124158
struct kvm_cpu_context *hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
159+
struct kvm *kvm = kern_hyp_va(vcpu->kvm);
125160
u64 r_clr = 0, w_clr = 0, r_set = 0, w_set = 0, tmp;
126161
u64 r_val, w_val;
127162

@@ -157,6 +192,9 @@ static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
157192
compute_clr_set(vcpu, HFGWTR_EL2, w_clr, w_set);
158193
}
159194

195+
compute_undef_clr_set(vcpu, kvm, HFGRTR_EL2, r_clr, r_set);
196+
compute_undef_clr_set(vcpu, kvm, HFGWTR_EL2, w_clr, w_set);
197+
160198
/* The default to trap everything not handled or supported in KVM. */
161199
tmp = HFGxTR_EL2_nAMAIR2_EL1 | HFGxTR_EL2_nMAIR2_EL1 | HFGxTR_EL2_nS2POR_EL1 |
162200
HFGxTR_EL2_nPOR_EL1 | HFGxTR_EL2_nPOR_EL0 | HFGxTR_EL2_nACCDATA_EL1;
@@ -172,36 +210,39 @@ static inline void __activate_traps_hfgxtr(struct kvm_vcpu *vcpu)
172210
write_sysreg_s(r_val, SYS_HFGRTR_EL2);
173211
write_sysreg_s(w_val, SYS_HFGWTR_EL2);
174212

175-
if (!vcpu_has_nv(vcpu) || is_hyp_ctxt(vcpu))
176-
return;
177-
178-
update_fgt_traps(vcpu, HFGITR_EL2);
179-
update_fgt_traps(vcpu, HDFGRTR_EL2);
180-
update_fgt_traps(vcpu, HDFGWTR_EL2);
213+
update_fgt_traps(hctxt, vcpu, kvm, HFGITR_EL2);
214+
update_fgt_traps(hctxt, vcpu, kvm, HDFGRTR_EL2);
215+
update_fgt_traps(hctxt, vcpu, kvm, HDFGWTR_EL2);
181216

182217
if (cpu_has_amu())
183-
update_fgt_traps(vcpu, HAFGRTR_EL2);
218+
update_fgt_traps(hctxt, vcpu, kvm, HAFGRTR_EL2);
184219
}
185220

221+
#define __deactivate_fgt(htcxt, vcpu, kvm, reg) \
222+
do { \
223+
if ((vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) || \
224+
kvm->arch.fgu[reg_to_fgt_group_id(reg)]) \
225+
write_sysreg_s(ctxt_sys_reg(hctxt, reg), \
226+
SYS_ ## reg); \
227+
} while(0)
228+
186229
static inline void __deactivate_traps_hfgxtr(struct kvm_vcpu *vcpu)
187230
{
188231
struct kvm_cpu_context *hctxt = &this_cpu_ptr(&kvm_host_data)->host_ctxt;
232+
struct kvm *kvm = kern_hyp_va(vcpu->kvm);
189233

190234
if (!cpus_have_final_cap(ARM64_HAS_FGT))
191235
return;
192236

193237
write_sysreg_s(ctxt_sys_reg(hctxt, HFGRTR_EL2), SYS_HFGRTR_EL2);
194238
write_sysreg_s(ctxt_sys_reg(hctxt, HFGWTR_EL2), SYS_HFGWTR_EL2);
195239

196-
if (!vcpu_has_nv(vcpu) || is_hyp_ctxt(vcpu))
197-
return;
198-
199-
write_sysreg_s(ctxt_sys_reg(hctxt, HFGITR_EL2), SYS_HFGITR_EL2);
200-
write_sysreg_s(ctxt_sys_reg(hctxt, HDFGRTR_EL2), SYS_HDFGRTR_EL2);
201-
write_sysreg_s(ctxt_sys_reg(hctxt, HDFGWTR_EL2), SYS_HDFGWTR_EL2);
240+
__deactivate_fgt(hctxt, vcpu, kvm, HFGITR_EL2);
241+
__deactivate_fgt(hctxt, vcpu, kvm, HDFGRTR_EL2);
242+
__deactivate_fgt(hctxt, vcpu, kvm, HDFGWTR_EL2);
202243

203244
if (cpu_has_amu())
204-
write_sysreg_s(ctxt_sys_reg(hctxt, HAFGRTR_EL2), SYS_HAFGRTR_EL2);
245+
__deactivate_fgt(hctxt, vcpu, kvm, HAFGRTR_EL2);
205246
}
206247

207248
static inline void __activate_traps_common(struct kvm_vcpu *vcpu)

0 commit comments

Comments
 (0)