Skip to content

Commit 59da1cb

Browse files
author
Marc Zyngier
committed
KVM: arm64: vgic-v3: Add hook to handle guest GICv3 sysreg accesses at EL2
In order to start handling guest access to GICv3 system registers, let's add a hook that will get called when we trap a system register access. This is gated by a new static key (vgic_v3_cpuif_trap). Tested-by: Alexander Graf <agraf@suse.de> Acked-by: David Daney <david.daney@cavium.com> Reviewed-by: Eric Auger <eric.auger@redhat.com> Reviewed-by: Christoffer Dall <cdall@linaro.org> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Christoffer Dall <cdall@linaro.org>
1 parent 021234e commit 59da1cb

5 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎arch/arm64/include/asm/kvm_hyp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ int __vgic_v2_perform_cpuif_access(struct kvm_vcpu *vcpu);
127127

128128
void __vgic_v3_save_state(struct kvm_vcpu *vcpu);
129129
void __vgic_v3_restore_state(struct kvm_vcpu *vcpu);
130+
int __vgic_v3_perform_cpuif_access(struct kvm_vcpu *vcpu);
130131

131132
void __timer_save_state(struct kvm_vcpu *vcpu);
132133
void __timer_restore_state(struct kvm_vcpu *vcpu);

‎arch/arm64/kvm/hyp/switch.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,20 @@ int __hyp_text __kvm_vcpu_run(struct kvm_vcpu *vcpu)
350350
}
351351
}
352352

353+
if (static_branch_unlikely(&vgic_v3_cpuif_trap) &&
354+
exit_code == ARM_EXCEPTION_TRAP &&
355+
(kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_SYS64 ||
356+
kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_CP15_32)) {
357+
int ret = __vgic_v3_perform_cpuif_access(vcpu);
358+
359+
if (ret == 1) {
360+
__skip_instr(vcpu);
361+
goto again;
362+
}
363+
364+
/* 0 falls through to be handled out of EL2 */
365+
}
366+
353367
fp_enabled = __fpsimd_enabled();
354368

355369
__sysreg_save_guest_state(guest_ctxt);

‎include/kvm/arm_vgic.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ struct vgic_cpu {
292292
};
293293

294294
extern struct static_key_false vgic_v2_cpuif_trap;
295+
extern struct static_key_false vgic_v3_cpuif_trap;
295296

296297
int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write);
297298
void kvm_vgic_early_init(struct kvm *kvm);

‎virt/kvm/arm/hyp/vgic-v3-sr.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/irqchip/arm-gic-v3.h>
2020
#include <linux/kvm_host.h>
2121

22+
#include <asm/kvm_emulate.h>
2223
#include <asm/kvm_hyp.h>
2324

2425
#define vtr_to_max_lr_idx(v) ((v) & 0xf)
@@ -371,3 +372,40 @@ void __hyp_text __vgic_v3_write_vmcr(u32 vmcr)
371372
{
372373
write_gicreg(vmcr, ICH_VMCR_EL2);
373374
}
375+
376+
#ifdef CONFIG_ARM64
377+
378+
int __hyp_text __vgic_v3_perform_cpuif_access(struct kvm_vcpu *vcpu)
379+
{
380+
int rt;
381+
u32 esr;
382+
u32 vmcr;
383+
void (*fn)(struct kvm_vcpu *, u32, int);
384+
bool is_read;
385+
u32 sysreg;
386+
387+
esr = kvm_vcpu_get_hsr(vcpu);
388+
if (vcpu_mode_is_32bit(vcpu)) {
389+
if (!kvm_condition_valid(vcpu))
390+
return 1;
391+
392+
sysreg = esr_cp15_to_sysreg(esr);
393+
} else {
394+
sysreg = esr_sys64_to_sysreg(esr);
395+
}
396+
397+
is_read = (esr & ESR_ELx_SYS64_ISS_DIR_MASK) == ESR_ELx_SYS64_ISS_DIR_READ;
398+
399+
switch (sysreg) {
400+
default:
401+
return 0;
402+
}
403+
404+
vmcr = __vgic_v3_read_vmcr();
405+
rt = kvm_vcpu_sys_get_rt(vcpu);
406+
fn(vcpu, vmcr, rt);
407+
408+
return 1;
409+
}
410+
411+
#endif

‎virt/kvm/arm/vgic/vgic-v3.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ int vgic_v3_map_resources(struct kvm *kvm)
429429
return ret;
430430
}
431431

432+
DEFINE_STATIC_KEY_FALSE(vgic_v3_cpuif_trap);
433+
432434
/**
433435
* vgic_v3_probe - probe for a GICv3 compatible interrupt controller in DT
434436
* @node: pointer to the DT node

0 commit comments

Comments
 (0)