Skip to content

Commit 4733414

Browse files
jingzhangosoupton
authored andcommitted
KVM: arm64: Save ID registers' sanitized value per guest
Initialize the default ID register values upon the first call to KVM_ARM_VCPU_INIT. The vCPU feature flags are finalized at that point, so it is possible to determine the maximum feature set supported by a particular VM configuration. Do nothing with these values for now, as we need to rework the plumbing of what's already writable to be compatible with the generic infrastructure. Co-developed-by: Reiji Watanabe <reijiw@google.com> Signed-off-by: Reiji Watanabe <reijiw@google.com> Signed-off-by: Jing Zhang <jingzhangos@google.com> Link: https://lore.kernel.org/r/20230609190054.1542113-7-oliver.upton@linux.dev [Oliver: Hoist everything into KVM_ARM_VCPU_INIT time, so the features are final] Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
1 parent d86cde6 commit 4733414

3 files changed

Lines changed: 75 additions & 3 deletions

File tree

arch/arm64/include/asm/kvm_host.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ struct kvm_arch {
225225
#define KVM_ARCH_FLAG_TIMER_PPIS_IMMUTABLE 6
226226
/* SMCCC filter initialized for the VM */
227227
#define KVM_ARCH_FLAG_SMCCC_FILTER_CONFIGURED 7
228+
/* Initial ID reg values loaded */
229+
#define KVM_ARCH_FLAG_ID_REGS_INITIALIZED 8
228230
unsigned long flags;
229231

230232
/* VM-wide vCPU feature set */
@@ -247,6 +249,19 @@ struct kvm_arch {
247249
struct kvm_smccc_features smccc_feat;
248250
struct maple_tree smccc_filter;
249251

252+
/*
253+
* Emulated CPU ID registers per VM
254+
* (Op0, Op1, CRn, CRm, Op2) of the ID registers to be saved in it
255+
* is (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
256+
*
257+
* These emulated idregs are VM-wide, but accessed from the context of a vCPU.
258+
* Atomic access to multiple idregs are guarded by kvm_arch.config_lock.
259+
*/
260+
#define IDREG_IDX(id) (((sys_reg_CRm(id) - 1) << 3) | sys_reg_Op2(id))
261+
#define IDREG(kvm, id) ((kvm)->arch.id_regs[IDREG_IDX(id)])
262+
#define KVM_ARM_ID_REG_NUM (IDREG_IDX(sys_reg(3, 0, 0, 7, 7)) + 1)
263+
u64 id_regs[KVM_ARM_ID_REG_NUM];
264+
250265
/*
251266
* For an untrusted host VM, 'pkvm.handle' is used to lookup
252267
* the associated pKVM instance in the hypervisor.

arch/arm64/kvm/sys_regs.c

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,17 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu, const struct sys_reg_desc *r
13111311
return __kvm_read_sanitised_id_reg(vcpu, r);
13121312
}
13131313

1314+
/*
1315+
* Return true if the register's (Op0, Op1, CRn, CRm, Op2) is
1316+
* (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8.
1317+
*/
1318+
static inline bool is_id_reg(u32 id)
1319+
{
1320+
return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 &&
1321+
sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 &&
1322+
sys_reg_CRm(id) < 8);
1323+
}
1324+
13141325
static unsigned int id_visibility(const struct kvm_vcpu *vcpu,
13151326
const struct sys_reg_desc *r)
13161327
{
@@ -2303,6 +2314,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
23032314
EL2_REG(SP_EL2, NULL, reset_unknown, 0),
23042315
};
23052316

2317+
static const struct sys_reg_desc *first_idreg;
2318+
23062319
static bool trap_dbgdidr(struct kvm_vcpu *vcpu,
23072320
struct sys_reg_params *p,
23082321
const struct sys_reg_desc *r)
@@ -2993,6 +3006,28 @@ static bool emulate_sys_reg(struct kvm_vcpu *vcpu,
29933006
return false;
29943007
}
29953008

3009+
static void kvm_reset_id_regs(struct kvm_vcpu *vcpu)
3010+
{
3011+
const struct sys_reg_desc *idreg = first_idreg;
3012+
u32 id = reg_to_encoding(idreg);
3013+
struct kvm *kvm = vcpu->kvm;
3014+
3015+
if (test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))
3016+
return;
3017+
3018+
lockdep_assert_held(&kvm->arch.config_lock);
3019+
3020+
/* Initialize all idregs */
3021+
while (is_id_reg(id)) {
3022+
IDREG(kvm, id) = idreg->reset(vcpu, idreg);
3023+
3024+
idreg++;
3025+
id = reg_to_encoding(idreg);
3026+
}
3027+
3028+
set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags);
3029+
}
3030+
29963031
/**
29973032
* kvm_reset_sys_regs - sets system registers to reset value
29983033
* @vcpu: The VCPU pointer
@@ -3004,9 +3039,17 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
30043039
{
30053040
unsigned long i;
30063041

3007-
for (i = 0; i < ARRAY_SIZE(sys_reg_descs); i++)
3008-
if (sys_reg_descs[i].reset)
3009-
sys_reg_descs[i].reset(vcpu, &sys_reg_descs[i]);
3042+
kvm_reset_id_regs(vcpu);
3043+
3044+
for (i = 0; i < ARRAY_SIZE(sys_reg_descs); i++) {
3045+
const struct sys_reg_desc *r = &sys_reg_descs[i];
3046+
3047+
if (is_id_reg(reg_to_encoding(r)))
3048+
continue;
3049+
3050+
if (r->reset)
3051+
r->reset(vcpu, r);
3052+
}
30103053
}
30113054

30123055
/**
@@ -3413,6 +3456,7 @@ int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
34133456

34143457
int __init kvm_sys_reg_table_init(void)
34153458
{
3459+
struct sys_reg_params params;
34163460
bool valid = true;
34173461
unsigned int i;
34183462

@@ -3431,5 +3475,11 @@ int __init kvm_sys_reg_table_init(void)
34313475
for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++)
34323476
invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]);
34333477

3478+
/* Find the first idreg (SYS_ID_PFR0_EL1) in sys_reg_descs. */
3479+
params = encoding_to_params(SYS_ID_PFR0_EL1);
3480+
first_idreg = find_reg(&params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
3481+
if (!first_idreg)
3482+
return -EINVAL;
3483+
34343484
return 0;
34353485
}

arch/arm64/kvm/sys_regs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ struct sys_reg_params {
2727
bool is_write;
2828
};
2929

30+
#define encoding_to_params(reg) \
31+
((struct sys_reg_params){ .Op0 = sys_reg_Op0(reg), \
32+
.Op1 = sys_reg_Op1(reg), \
33+
.CRn = sys_reg_CRn(reg), \
34+
.CRm = sys_reg_CRm(reg), \
35+
.Op2 = sys_reg_Op2(reg) })
36+
3037
#define esr_sys64_to_params(esr) \
3138
((struct sys_reg_params){ .Op0 = ((esr) >> 20) & 3, \
3239
.Op1 = ((esr) >> 14) & 0x7, \

0 commit comments

Comments
 (0)