Skip to content

Commit cb6cd8a

Browse files
author
Marc Zyngier
committed
Merge branch kvm-arm64/feat_idst into kvmarm-master/next
* kvm-arm64/feat_idst: : . : Add support for FEAT_IDST, allowing ID registers that are not implemented : to be reported as a normal trap rather than as an UNDEF exception. : . KVM: arm64: selftests: Add a test for FEAT_IDST KVM: arm64: pkvm: Report optional ID register traps with a 0x18 syndrome KVM: arm64: pkvm: Add a generic synchronous exception injection primitive KVM: arm64: Force trap of GMID_EL1 when the guest doesn't have MTE KVM: arm64: Handle CSSIDR2_EL1 and SMIDR_EL1 in a generic way KVM: arm64: Handle FEAT_IDST for sysregs without specific handlers KVM: arm64: Add a generic synchronous exception injection primitive KVM: arm64: Add trap routing for GMID_EL1 arm64: Repaint ID_AA64MMFR2_EL1.IDS description Signed-off-by: Marc Zyngier <maz@kernel.org>
2 parents 7c652c0 + b638a9d commit cb6cd8a

9 files changed

Lines changed: 194 additions & 16 deletions

File tree

arch/arm64/include/asm/kvm_emulate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ bool kvm_condition_valid32(const struct kvm_vcpu *vcpu);
4545
void kvm_skip_instr32(struct kvm_vcpu *vcpu);
4646

4747
void kvm_inject_undefined(struct kvm_vcpu *vcpu);
48+
void kvm_inject_sync(struct kvm_vcpu *vcpu, u64 esr);
4849
int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr);
4950
int kvm_inject_sea(struct kvm_vcpu *vcpu, bool iabt, u64 addr);
5051
int kvm_inject_dabt_excl_atomic(struct kvm_vcpu *vcpu, u64 addr);

arch/arm64/kvm/emulate-nested.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ enum cgt_group_id {
7070
CGT_HCR_ENSCXT,
7171
CGT_HCR_TTLBIS,
7272
CGT_HCR_TTLBOS,
73+
CGT_HCR_TID5,
7374

7475
CGT_MDCR_TPMCR,
7576
CGT_MDCR_TPM,
@@ -308,6 +309,12 @@ static const struct trap_bits coarse_trap_bits[] = {
308309
.mask = HCR_TTLBOS,
309310
.behaviour = BEHAVE_FORWARD_RW,
310311
},
312+
[CGT_HCR_TID5] = {
313+
.index = HCR_EL2,
314+
.value = HCR_TID5,
315+
.mask = HCR_TID5,
316+
.behaviour = BEHAVE_FORWARD_RW,
317+
},
311318
[CGT_MDCR_TPMCR] = {
312319
.index = MDCR_EL2,
313320
.value = MDCR_EL2_TPMCR,
@@ -665,6 +672,7 @@ static const struct encoding_to_trap_config encoding_to_cgt[] __initconst = {
665672
SR_TRAP(SYS_CCSIDR2_EL1, CGT_HCR_TID2_TID4),
666673
SR_TRAP(SYS_CLIDR_EL1, CGT_HCR_TID2_TID4),
667674
SR_TRAP(SYS_CSSELR_EL1, CGT_HCR_TID2_TID4),
675+
SR_TRAP(SYS_GMID_EL1, CGT_HCR_TID5),
668676
SR_RANGE_TRAP(SYS_ID_PFR0_EL1,
669677
sys_reg(3, 0, 0, 7, 7), CGT_HCR_TID3),
670678
SR_TRAP(SYS_ICC_SGI0R_EL1, CGT_HCR_IMO_FMO_ICH_HCR_TC),
@@ -2587,6 +2595,19 @@ bool triage_sysreg_trap(struct kvm_vcpu *vcpu, int *sr_index)
25872595

25882596
params = esr_sys64_to_params(esr);
25892597

2598+
/*
2599+
* This implements the pseudocode UnimplementedIDRegister()
2600+
* helper for the purpose of dealing with FEAT_IDST.
2601+
*/
2602+
if (in_feat_id_space(&params)) {
2603+
if (kvm_has_feat(vcpu->kvm, ID_AA64MMFR2_EL1, IDS, IMP))
2604+
kvm_inject_sync(vcpu, kvm_vcpu_get_esr(vcpu));
2605+
else
2606+
kvm_inject_undefined(vcpu);
2607+
2608+
return true;
2609+
}
2610+
25902611
/*
25912612
* Check for the IMPDEF range, as per DDI0487 J.a,
25922613
* D18.3.2 Reserved encodings for IMPLEMENTATION

arch/arm64/kvm/hyp/nvhe/sys_regs.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static const struct pvm_ftr_bits pvmid_aa64mmfr2[] = {
134134
MAX_FEAT(ID_AA64MMFR2_EL1, UAO, IMP),
135135
MAX_FEAT(ID_AA64MMFR2_EL1, IESB, IMP),
136136
MAX_FEAT(ID_AA64MMFR2_EL1, AT, IMP),
137-
MAX_FEAT_ENUM(ID_AA64MMFR2_EL1, IDS, 0x18),
137+
MAX_FEAT(ID_AA64MMFR2_EL1, IDS, IMP),
138138
MAX_FEAT(ID_AA64MMFR2_EL1, TTL, IMP),
139139
MAX_FEAT(ID_AA64MMFR2_EL1, BBM, 2),
140140
MAX_FEAT(ID_AA64MMFR2_EL1, E0PD, IMP),
@@ -243,16 +243,15 @@ static u64 pvm_calc_id_reg(const struct kvm_vcpu *vcpu, u32 id)
243243
}
244244
}
245245

246-
/*
247-
* Inject an unknown/undefined exception to an AArch64 guest while most of its
248-
* sysregs are live.
249-
*/
250-
static void inject_undef64(struct kvm_vcpu *vcpu)
246+
static void inject_sync64(struct kvm_vcpu *vcpu, u64 esr)
251247
{
252-
u64 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
253-
254248
*vcpu_pc(vcpu) = read_sysreg_el2(SYS_ELR);
255249
*vcpu_cpsr(vcpu) = read_sysreg_el2(SYS_SPSR);
250+
251+
/*
252+
* Make sure we have the latest update to VBAR_EL1, as pKVM
253+
* handles traps very early, before sysregs are resync'ed
254+
*/
256255
__vcpu_assign_sys_reg(vcpu, VBAR_EL1, read_sysreg_el1(SYS_VBAR));
257256

258257
kvm_pend_exception(vcpu, EXCEPT_AA64_EL1_SYNC);
@@ -265,6 +264,15 @@ static void inject_undef64(struct kvm_vcpu *vcpu)
265264
write_sysreg_el2(*vcpu_cpsr(vcpu), SYS_SPSR);
266265
}
267266

267+
/*
268+
* Inject an unknown/undefined exception to an AArch64 guest while most of its
269+
* sysregs are live.
270+
*/
271+
static void inject_undef64(struct kvm_vcpu *vcpu)
272+
{
273+
inject_sync64(vcpu, (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT));
274+
}
275+
268276
static u64 read_id_reg(const struct kvm_vcpu *vcpu,
269277
struct sys_reg_desc const *r)
270278
{
@@ -339,6 +347,18 @@ static bool pvm_gic_read_sre(struct kvm_vcpu *vcpu,
339347
return true;
340348
}
341349

350+
static bool pvm_idst_access(struct kvm_vcpu *vcpu,
351+
struct sys_reg_params *p,
352+
const struct sys_reg_desc *r)
353+
{
354+
if (kvm_has_feat(vcpu->kvm, ID_AA64MMFR2_EL1, IDS, IMP))
355+
inject_sync64(vcpu, kvm_vcpu_get_esr(vcpu));
356+
else
357+
inject_undef64(vcpu);
358+
359+
return false;
360+
}
361+
342362
/* Mark the specified system register as an AArch32 feature id register. */
343363
#define AARCH32(REG) { SYS_DESC(REG), .access = pvm_access_id_aarch32 }
344364

@@ -469,6 +489,9 @@ static const struct sys_reg_desc pvm_sys_reg_descs[] = {
469489

470490
HOST_HANDLED(SYS_CCSIDR_EL1),
471491
HOST_HANDLED(SYS_CLIDR_EL1),
492+
{ SYS_DESC(SYS_CCSIDR2_EL1), .access = pvm_idst_access },
493+
{ SYS_DESC(SYS_GMID_EL1), .access = pvm_idst_access },
494+
{ SYS_DESC(SYS_SMIDR_EL1), .access = pvm_idst_access },
472495
HOST_HANDLED(SYS_AIDR_EL1),
473496
HOST_HANDLED(SYS_CSSELR_EL1),
474497
HOST_HANDLED(SYS_CTR_EL0),

arch/arm64/kvm/inject_fault.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,24 @@ static void inject_abt64(struct kvm_vcpu *vcpu, bool is_iabt, unsigned long addr
162162
vcpu_write_sys_reg(vcpu, esr, exception_esr_elx(vcpu));
163163
}
164164

165+
void kvm_inject_sync(struct kvm_vcpu *vcpu, u64 esr)
166+
{
167+
pend_sync_exception(vcpu);
168+
vcpu_write_sys_reg(vcpu, esr, exception_esr_elx(vcpu));
169+
}
170+
165171
static void inject_undef64(struct kvm_vcpu *vcpu)
166172
{
167173
u64 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT);
168174

169-
pend_sync_exception(vcpu);
170-
171175
/*
172176
* Build an unknown exception, depending on the instruction
173177
* set.
174178
*/
175179
if (kvm_vcpu_trap_il_is32bit(vcpu))
176180
esr |= ESR_ELx_IL;
177181

178-
vcpu_write_sys_reg(vcpu, esr, exception_esr_elx(vcpu));
182+
kvm_inject_sync(vcpu, esr);
179183
}
180184

181185
#define DFSR_FSC_EXTABT_LPAE 0x10

arch/arm64/kvm/sys_regs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,8 +3414,6 @@ static const struct sys_reg_desc sys_reg_descs[] = {
34143414
{ SYS_DESC(SYS_CCSIDR_EL1), access_ccsidr },
34153415
{ SYS_DESC(SYS_CLIDR_EL1), access_clidr, reset_clidr, CLIDR_EL1,
34163416
.set_user = set_clidr, .val = ~CLIDR_EL1_RES0 },
3417-
{ SYS_DESC(SYS_CCSIDR2_EL1), undef_access },
3418-
{ SYS_DESC(SYS_SMIDR_EL1), undef_access },
34193417
IMPLEMENTATION_ID(AIDR_EL1, GENMASK_ULL(63, 0)),
34203418
{ SYS_DESC(SYS_CSSELR_EL1), access_csselr, reset_unknown, CSSELR_EL1 },
34213419
ID_FILTERED(CTR_EL0, ctr_el0,
@@ -5581,6 +5579,8 @@ static void vcpu_set_hcr(struct kvm_vcpu *vcpu)
55815579

55825580
if (kvm_has_mte(vcpu->kvm))
55835581
vcpu->arch.hcr_el2 |= HCR_ATA;
5582+
else
5583+
vcpu->arch.hcr_el2 |= HCR_TID5;
55845584

55855585
/*
55865586
* In the absence of FGT, we cannot independently trap TLBI

arch/arm64/kvm/sys_regs.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ struct sys_reg_params {
4949
.Op2 = ((esr) >> 17) & 0x7, \
5050
.is_write = !((esr) & 1) })
5151

52+
/*
53+
* The Feature ID space is defined as the System register space in AArch64
54+
* with op0==3, op1=={0, 1, 3}, CRn==0, CRm=={0-7}, op2=={0-7}.
55+
*/
56+
static inline bool in_feat_id_space(struct sys_reg_params *p)
57+
{
58+
return (p->Op0 == 3 && !(p->Op1 & 0b100) && p->Op1 != 2 &&
59+
p->CRn == 0 && !(p->CRm & 0b1000));
60+
}
61+
5262
struct sys_reg_desc {
5363
/* Sysreg string for debug */
5464
const char *name;

arch/arm64/tools/sysreg

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,9 +2256,10 @@ UnsignedEnum 43:40 FWB
22562256
0b0000 NI
22572257
0b0001 IMP
22582258
EndEnum
2259-
Enum 39:36 IDS
2260-
0b0000 0x0
2261-
0b0001 0x18
2259+
UnsignedEnum 39:36 IDS
2260+
0b0000 NI
2261+
0b0001 IMP
2262+
0b0010 EL3
22622263
EndEnum
22632264
UnsignedEnum 35:32 AT
22642265
0b0000 NI

tools/testing/selftests/kvm/Makefile.kvm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ TEST_GEN_PROGS_arm64 += arm64/vgic_irq
175175
TEST_GEN_PROGS_arm64 += arm64/vgic_lpi_stress
176176
TEST_GEN_PROGS_arm64 += arm64/vpmu_counter_access
177177
TEST_GEN_PROGS_arm64 += arm64/no-vgic-v3
178+
TEST_GEN_PROGS_arm64 += arm64/idreg-idst
178179
TEST_GEN_PROGS_arm64 += arm64/kvm-uuid
179180
TEST_GEN_PROGS_arm64 += access_tracking_perf_test
180181
TEST_GEN_PROGS_arm64 += arch_timer
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Access all FEAT_IDST-handled registers that depend on more than
5+
* just FEAT_AA64, and fail if we don't get an a trap with an 0x18 EC.
6+
*/
7+
8+
#include <test_util.h>
9+
#include <kvm_util.h>
10+
#include <processor.h>
11+
12+
static volatile bool sys64, undef;
13+
14+
#define __check_sr_read(r) \
15+
({ \
16+
uint64_t val; \
17+
\
18+
sys64 = false; \
19+
undef = false; \
20+
dsb(sy); \
21+
val = read_sysreg_s(SYS_ ## r); \
22+
val; \
23+
})
24+
25+
/* Fatal checks */
26+
#define check_sr_read(r) \
27+
do { \
28+
__check_sr_read(r); \
29+
__GUEST_ASSERT(!undef, #r " unexpected UNDEF"); \
30+
__GUEST_ASSERT(sys64, #r " didn't trap"); \
31+
} while(0)
32+
33+
34+
static void guest_code(void)
35+
{
36+
check_sr_read(CCSIDR2_EL1);
37+
check_sr_read(SMIDR_EL1);
38+
check_sr_read(GMID_EL1);
39+
40+
GUEST_DONE();
41+
}
42+
43+
static void guest_sys64_handler(struct ex_regs *regs)
44+
{
45+
sys64 = true;
46+
undef = false;
47+
regs->pc += 4;
48+
}
49+
50+
static void guest_undef_handler(struct ex_regs *regs)
51+
{
52+
sys64 = false;
53+
undef = true;
54+
regs->pc += 4;
55+
}
56+
57+
static void test_run_vcpu(struct kvm_vcpu *vcpu)
58+
{
59+
struct ucall uc;
60+
61+
do {
62+
vcpu_run(vcpu);
63+
64+
switch (get_ucall(vcpu, &uc)) {
65+
case UCALL_ABORT:
66+
REPORT_GUEST_ASSERT(uc);
67+
break;
68+
case UCALL_PRINTF:
69+
printf("%s", uc.buffer);
70+
break;
71+
case UCALL_DONE:
72+
break;
73+
default:
74+
TEST_FAIL("Unknown ucall %lu", uc.cmd);
75+
}
76+
} while (uc.cmd != UCALL_DONE);
77+
}
78+
79+
static void test_guest_feat_idst(void)
80+
{
81+
struct kvm_vcpu *vcpu;
82+
struct kvm_vm *vm;
83+
84+
/* This VM has no MTE, no SME, no CCIDX */
85+
vm = vm_create_with_one_vcpu(&vcpu, guest_code);
86+
87+
vm_init_descriptor_tables(vm);
88+
vcpu_init_descriptor_tables(vcpu);
89+
90+
vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
91+
ESR_ELx_EC_SYS64, guest_sys64_handler);
92+
vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
93+
ESR_ELx_EC_UNKNOWN, guest_undef_handler);
94+
95+
test_run_vcpu(vcpu);
96+
97+
kvm_vm_free(vm);
98+
}
99+
100+
int main(int argc, char *argv[])
101+
{
102+
struct kvm_vcpu *vcpu;
103+
struct kvm_vm *vm;
104+
uint64_t mmfr2;
105+
106+
test_disable_default_vgic();
107+
108+
vm = vm_create_with_one_vcpu(&vcpu, NULL);
109+
mmfr2 = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64MMFR2_EL1));
110+
__TEST_REQUIRE(FIELD_GET(ID_AA64MMFR2_EL1_IDS, mmfr2) > 0,
111+
"FEAT_IDST not supported");
112+
kvm_vm_free(vm);
113+
114+
test_guest_feat_idst();
115+
116+
return 0;
117+
}

0 commit comments

Comments
 (0)