Skip to content

Commit 7ba5b8f

Browse files
Fuad TabbaMarc Zyngier
authored andcommitted
KVM: arm64: Initialize feature id registers for protected VMs
The hypervisor maintains the state of protected VMs. Initialize the values for feature ID registers for protected VMs, to be used when setting traps and when advertising features to protected VMs. Signed-off-by: Fuad Tabba <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Marc Zyngier <[email protected]>
1 parent a3163dc commit 7ba5b8f

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

arch/arm64/kvm/hyp/include/nvhe/fixed_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
u64 pvm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id);
202202
bool kvm_handle_pvm_sysreg(struct kvm_vcpu *vcpu, u64 *exit_code);
203203
bool kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu, u64 *exit_code);
204+
void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu);
204205
int kvm_check_pvm_sysreg_table(void);
205206

206207
#endif /* __ARM64_KVM_FIXED_CONFIG_H__ */

arch/arm64/kvm/hyp/include/nvhe/pkvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ struct pkvm_hyp_vm {
4747
struct pkvm_hyp_vcpu *vcpus[];
4848
};
4949

50+
extern hyp_spinlock_t vm_table_lock;
51+
5052
static inline struct pkvm_hyp_vm *
5153
pkvm_hyp_vcpu_to_hyp_vm(struct pkvm_hyp_vcpu *hyp_vcpu)
5254
{

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,10 @@ static pkvm_handle_t idx_to_vm_handle(unsigned int idx)
257257

258258
/*
259259
* Spinlock for protecting state related to the VM table. Protects writes
260-
* to 'vm_table' and 'nr_table_entries' as well as reads and writes to
261-
* 'last_hyp_vcpu_lookup'.
260+
* to 'vm_table', 'nr_table_entries', and other per-vm state on initialization.
261+
* Also protects reads and writes to 'last_hyp_vcpu_lookup'.
262262
*/
263-
static DEFINE_HYP_SPINLOCK(vm_table_lock);
263+
DEFINE_HYP_SPINLOCK(vm_table_lock);
264264

265265
/*
266266
* The table of VM entries for protected VMs in hyp.
@@ -381,6 +381,7 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm,
381381
hyp_vm->kvm.created_vcpus = nr_vcpus;
382382
hyp_vm->kvm.arch.mmu.vtcr = host_mmu.arch.mmu.vtcr;
383383
hyp_vm->kvm.arch.pkvm.enabled = READ_ONCE(host_kvm->arch.pkvm.enabled);
384+
hyp_vm->kvm.arch.flags = 0;
384385
pkvm_init_features_from_host(hyp_vm, host_kvm);
385386
}
386387

@@ -419,6 +420,9 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu,
419420
hyp_vcpu->vcpu.arch.cflags = READ_ONCE(host_vcpu->arch.cflags);
420421
hyp_vcpu->vcpu.arch.mp_state.mp_state = KVM_MP_STATE_STOPPED;
421422

423+
if (pkvm_hyp_vcpu_is_protected(hyp_vcpu))
424+
kvm_init_pvm_id_regs(&hyp_vcpu->vcpu);
425+
422426
ret = pkvm_vcpu_init_traps(hyp_vcpu);
423427
if (ret)
424428
goto done;

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <hyp/adjust_pc.h>
1313

1414
#include <nvhe/fixed_config.h>
15+
#include <nvhe/pkvm.h>
1516

1617
#include "../../sys_regs.h"
1718

@@ -204,8 +205,7 @@ static u64 get_pvm_id_aa64mmfr2(const struct kvm_vcpu *vcpu)
204205
return id_aa64mmfr2_el1_sys_val & PVM_ID_AA64MMFR2_ALLOW;
205206
}
206207

207-
/* Read a sanitized cpufeature ID register by its encoding */
208-
u64 pvm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
208+
static u64 pvm_calc_id_reg(const struct kvm_vcpu *vcpu, u32 id)
209209
{
210210
switch (id) {
211211
case SYS_ID_AA64PFR0_EL1:
@@ -240,10 +240,25 @@ u64 pvm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
240240
}
241241
}
242242

243+
/* Read a sanitized cpufeature ID register by its encoding */
244+
u64 pvm_read_id_reg(const struct kvm_vcpu *vcpu, u32 id)
245+
{
246+
return pvm_calc_id_reg(vcpu, id);
247+
}
248+
243249
static u64 read_id_reg(const struct kvm_vcpu *vcpu,
244250
struct sys_reg_desc const *r)
245251
{
246-
return pvm_read_id_reg(vcpu, reg_to_encoding(r));
252+
struct kvm *kvm = vcpu->kvm;
253+
u32 reg = reg_to_encoding(r);
254+
255+
if (WARN_ON_ONCE(!test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags)))
256+
return 0;
257+
258+
if (reg >= sys_reg(3, 0, 0, 1, 0) && reg <= sys_reg(3, 0, 0, 7, 7))
259+
return kvm->arch.id_regs[IDREG_IDX(reg)];
260+
261+
return 0;
247262
}
248263

249264
/* Handler to RAZ/WI sysregs */
@@ -448,6 +463,30 @@ static const struct sys_reg_desc pvm_sys_reg_descs[] = {
448463
/* Performance Monitoring Registers are restricted. */
449464
};
450465

466+
/*
467+
* Initializes feature registers for protected vms.
468+
*/
469+
void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu)
470+
{
471+
struct kvm *kvm = vcpu->kvm;
472+
struct kvm_arch *ka = &kvm->arch;
473+
u32 r;
474+
475+
hyp_assert_lock_held(&vm_table_lock);
476+
477+
if (test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags))
478+
return;
479+
480+
/*
481+
* Initialize only AArch64 id registers since AArch32 isn't supported
482+
* for protected VMs.
483+
*/
484+
for (r = sys_reg(3, 0, 0, 4, 0); r <= sys_reg(3, 0, 0, 7, 7); r += sys_reg(0, 0, 0, 0, 1))
485+
ka->id_regs[IDREG_IDX(r)] = pvm_calc_id_reg(vcpu, r);
486+
487+
set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags);
488+
}
489+
451490
/*
452491
* Checks that the sysreg table is unique and in-order.
453492
*

0 commit comments

Comments
 (0)