KVM: arm64: Confine protected VM vCPU state to EL2
Subsystems (4)
Fuad Tabba
reviewercover KVM: arm64: Confine protected VM vCPU state to EL2
Hi folks,
Following the vCPU state-sync series [1], this series completes the job for protected VMs: a protected guest's register state stays at EL2, and the host sees only what handling each exit needs.
EL2 marshals a protected vCPU's state per exception class instead of copying the whole context both ways. It owns the vCPU's trap configuration, system register reset and HVC handling, and implements PSCI itself: AFFINITY_INFO never reaches the host, and CPU_ON and CPU_OFF are decided at EL2 with the host only scheduling or parking the target. Host ioctls that would reach the state EL2 owns fail with a clean errno, so a protected VM's state is not save/restorable. All of this is scoped to KVM_VM_TYPE_ARM_PROTECTED, and pkvm.rst describes the resulting API.
The kvmtool changes that go with this will be posted separately, and I will reply here with a link.
Patch 1 is the HCR_EL2.VSE fix posted separately [2]. It is not part of this series; it is carried so the series applies as is and Sashiko can run on it.
The KVM_ARM_PREFERRED_TARGET documentation fix [3] went out just ahead of this series. Nothing here needs it to apply, but patch 17 documents vCPU feature availability as something the capabilities report, while api.rst 4.83 still points userspace at a bitmap that has always been empty.
The series is structured as follows:
01: The HCR_EL2.VSE fix, posted separately. 02-03: Capability allowlist and the PVTIME rejection. 04-05: Per-exception-class entry handlers; EL2 owns a protected
vCPU's trap configuration.
06-08: Timer state, system register reset and HVC handling at EL2. 09-10: PSCI at EL2, and the KVM_ARM_VCPU_INIT and PSCI version
restrictions.
11-14: Host PC adjustments blocked; an UNDEF at EL2 for exit
classes the host does not emulate; per-class state marshalling; a protected guest's SError pended with HCR_EL2.VSE.
15-16: Host access to private state, and host power-on of a vCPU
EL2 holds powered off, rejected.
17: Documentation.
Still to come: selftests, self-hosted debug, SVE for protected guests, and much more, as separate series.
Based on v7.3-rc1 (cee9395acd804).
Cheers,
/fuad
P.S. Sashiko, bring it on!
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
Fuad Tabba (15):
KVM: arm64: Sync HCR_EL2.VSE back to the host vCPU under pKVM
KVM: arm64: Advertise the capabilities that protected VMs support
KVM: arm64: Reject the PVTIME vCPU attribute for protected VMs
KVM: arm64: Skip fixed-feature state flush for protected vCPUs
KVM: arm64: Add system register reset framework for protected VMs
KVM: arm64: Implement HVC handling for protected guests at EL2
KVM: arm64: Handle PSCI calls for protected VMs at EL2
KVM: arm64: Restrict KVM_ARM_VCPU_INIT and PSCI version for protected
VMs
KVM: arm64: Prevent host PC adjustments for protected vCPUs
KVM: arm64: Inject an UNDEF at EL2 for unhandled protected guest exits
KVM: arm64: Add per-EC entry/exit state marshalling for protected
guests
KVM: arm64: Pend a protected guest's SError with HCR_EL2.VSE only
KVM: arm64: Reject host access to protected VM private state
KVM: arm64: Reject host power-on of a vCPU that EL2 holds powered off
KVM: arm64: Document the protected VM userspace API
Marc Zyngier (2):
KVM: arm64: Introduce per-EC entry handlers for pKVM
KVM: arm64: Add {flush,sync}_hyp_timer_state() primitives
Documentation/virt/kvm/api.rst | 22 +- .../virt/kvm/arm/fw-pseudo-registers.rst | 2 + Documentation/virt/kvm/arm/pkvm.rst | 141 ++++- Documentation/virt/kvm/devices/vcpu.rst | 4 +- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_host.h | 21 + arch/arm64/include/asm/kvm_pkvm.h | 34 +- arch/arm64/kvm/arm.c | 40 ++ arch/arm64/kvm/guest.c | 29 + arch/arm64/kvm/hyp/exception.c | 27 +- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 18 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 564 +++++++++++++++++- arch/arm64/kvm/hyp/nvhe/pkvm.c | 401 ++++++++++++- arch/arm64/kvm/hyp/nvhe/switch.c | 29 +- arch/arm64/kvm/hyp/nvhe/sys_regs.c | 91 ++- arch/arm64/kvm/hypercalls.c | 7 + arch/arm64/kvm/inject_fault.c | 5 +- arch/arm64/kvm/pkvm.c | 21 +- arch/arm64/kvm/psci.c | 3 + 19 files changed, 1378 insertions(+), 82 deletions(-)
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 1/17 KVM: arm64: Sync HCR_EL2.VSE back to the host vCPU under pKVM
flush_hyp_vcpu() copies HCR_EL2.VSE from the host vCPU into the hyp vCPU on every entry, and nothing copies it back. Once the guest takes the vSError the hyp vCPU's copy clears with the hardware bit while the host's stays set, so the next entry pends the same SError again, KVM_GET_VCPU_EVENTS keeps reporting it, and kvm_arch_vcpu_runnable() never lets the vCPU block in WFI.
Reflect VSE back on every exit, which the flush-side comment already states.
--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4 -0
@@ -276,6 +276,10 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) host_vcpu->arch.iflags = hyp_vcpu->vcpu.arch.iflags; +/* Cleared by hardware once the guest takes the vSError. */ + host_vcpu->arch.hcr_el2 &= ~HCR_VSE ; + host_vcpu->arch.hcr_el2 |= hyp_vcpu->vcpu.arch.hcr_el2 &HCR_VSE ; +sync_hyp_vgic_state (hyp_vcpu); }
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 2/17 KVM: arm64: Advertise the capabilities that protected VMs support
In reply to Fuad Tabbakvm_pkvm_ext_allowed() denies every capability it does not name, so a protected VM reports 0 for interfaces it implements: KVM_CHECK_EXTENSION denies KVM_CAP_ONE_REG while KVM_{GET,SET}_ONE_REG stage the guest's boot state.
Allow the capabilities that work for a protected guest, the vGIC and the I/O bus being host-managed, subject to the restrictions the rest of this series adds.
--- arch/arm64/include/asm/kvm_pkvm.h | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/arm64/include/asm/kvm_pkvm.h +9 -0
@@ -42,6 +42,15 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)case KVM_CAP_ARM_VM_IPA_SIZE :case KVM_CAP_ARM_PTRAUTH_ADDRESS :case KVM_CAP_ARM_PTRAUTH_GENERIC : +case KVM_CAP_ONE_REG : +case KVM_CAP_MP_STATE : +case KVM_CAP_VCPU_EVENTS : +case KVM_CAP_VCPU_ATTRIBUTES : +case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_IOEVENTFD : +case KVM_CAP_IRQFD_RESAMPLE : +case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 : +case KVM_CAP_ARM_INJECT_SERROR_ESR :return true;case KVM_CAP_ARM_MTE :return false;
Signature · 1 line
2.39.5
Re: [PATCH 02/17] KVM: arm64: Advertise the capabilities that protected VMs support
Quoted 19 lines · Fuad Tabba
kvm_pkvm_ext_allowed()denies every capability it does not name, so a protected VM reports 0 for interfaces it implements:KVM_CHECK_EXTENSIONdeniesKVM_CAP_ONE_REGwhile KVM_{GET,SET}_ONE_REG stage the guest's boot state.Allow the capabilities that work for a protected guest, the vGIC and the I/O bus being host-managed, subject to the restrictions the rest of this series adds.
Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/include/asm/kvm_pkvm.h | 9 +++++++++ 1 file changed, 9 insertions(+)arch/arm64/include/asm/kvm_pkvm.h +0
-0
arch/arm64/include/asm/kvm_pkvm.h static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) · line 45
Quoted 4 lines
arch/arm64/include/asm/kvm_pkvm.h +1
-0@@ -42,6 +42,15 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)case KVM_CAP_ARM_VM_IPA_SIZE :case KVM_CAP_ARM_PTRAUTH_ADDRESS :case KVM_CAP_ARM_PTRAUTH_GENERIC : +case KVM_CAP_ONE_REG :
Shouldn't we bring the VCPU_PKVM_FINALIZED restriction in kvm_arch_vcpu_ioctl() first before we allow SET_ONE_REG/GET_ONE_REG?
Quoted 14 lines
arch/arm64/include/asm/kvm_pkvm.h +8
-0+case KVM_CAP_MP_STATE : +case KVM_CAP_VCPU_EVENTS : +case KVM_CAP_VCPU_ATTRIBUTES : +case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_IOEVENTFD : +case KVM_CAP_IRQFD_RESAMPLE : +case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 : +case KVM_CAP_ARM_INJECT_SERROR_ESR :return true;case KVM_CAP_ARM_MTE :return false;Signature · 1 line
2.39.5
Fuad Tabba
reviewerRe: [PATCH 02/17] KVM: arm64: Advertise the capabilities that protected VMs support
Quoted 16 lines · Vincent Donnefort
Quoted 14 lines · Fuad Tabba
kvm_pkvm_ext_allowed()denies every capability it does not name, so a protected VM reports 0 for interfaces it implements:KVM_CHECK_EXTENSIONdeniesKVM_CAP_ONE_REGwhile KVM_{GET,SET}_ONE_REG stage the guest's boot state.Allow the capabilities that work for a protected guest, the vGIC and the I/O bus being host-managed, subject to the restrictions the rest of this series adds.
Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/include/asm/kvm_pkvm.h | 9 +++++++++ 1 file changed, 9 insertions(+)
Quoted 12 lines
arch/arm64/include/asm/kvm_pkvm.h static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) · line 45
Quoted 9 lines
arch/arm64/include/asm/kvm_pkvm.h +1
-0@@ -42,6 +42,15 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext)case KVM_CAP_ARM_VM_IPA_SIZE :case KVM_CAP_ARM_PTRAUTH_ADDRESS :case KVM_CAP_ARM_PTRAUTH_GENERIC : +case KVM_CAP_ONE_REG :Shouldn't we bring the
VCPU_PKVM_FINALIZEDrestriction inkvm_arch_vcpu_ioctl()first before we allow SET_ONE_REG/GET_ONE_REG?
KVM_{GET,SET}_ONE_REG don't go through kvm_pkvm_ext_allowed(), so they're as reachable before this patch as after it.
The ordering is wrong though, and there are a few more capabilities advertised here and restricted later. I'll fix it for all of them in v2.
Thanks,
/fuad
Quoted 15 lines
Quoted 15 lines
arch/arm64/include/asm/kvm_pkvm.h +8
-1+case KVM_CAP_MP_STATE : +case KVM_CAP_VCPU_EVENTS : +case KVM_CAP_VCPU_ATTRIBUTES : +case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_IOEVENTFD : +case KVM_CAP_IRQFD_RESAMPLE : +case KVM_CAP_ARM_IRQ_LINE_LAYOUT_2 : +case KVM_CAP_ARM_INJECT_SERROR_ESR :return true;case KVM_CAP_ARM_MTE :return false; --2.39.5
Fuad Tabba
reviewerPATCH 3/17 KVM: arm64: Reject the PVTIME vCPU attribute for protected VMs
In reply to Fuad TabbapKVM currently does not support steal time for protected guests:KVM_CAP_STEAL_TIME reports 0 for them. The host still accepts the KVM_ARM_VCPU_PVTIME_CTRL attribute, whose IPA would point kvm_update_stolen_time() at the guest's private memory on every vCPU load. Return -EPERM for the attribute group instead.
--- arch/arm64/kvm/guest.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/arm64/kvm/guest.c +10 -0
@@ -916,6 +916,10 @@ int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, ret =kvm_arm_timer_set_attr (vcpu, attr);break ;case KVM_ARM_VCPU_PVTIME_CTRL : +/* Steal time is not offered to protected guests. */ +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; + ret =kvm_arm_pvtime_set_attr (vcpu, attr);break ;default :
@@ -939,6 +943,9 @@ int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu, ret =kvm_arm_timer_get_attr (vcpu, attr);break ;case KVM_ARM_VCPU_PVTIME_CTRL : +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; + ret =kvm_arm_pvtime_get_attr (vcpu, attr);break ;default :
@@ -962,6 +969,9 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu, ret =kvm_arm_timer_has_attr (vcpu, attr);break ;case KVM_ARM_VCPU_PVTIME_CTRL : +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; + ret =kvm_arm_pvtime_has_attr (vcpu, attr);break ;default :
Signature · 1 line
2.39.5
Re: [PATCH 03/17] KVM: arm64: Reject the PVTIME vCPU attribute for protected VMs
Quoted 16 lines · Fuad Tabba
pKVM currently does not support steal time for protected guests:
KVM_CAP_STEAL_TIMEreports 0 for them. The host still accepts theKVM_ARM_VCPU_PVTIME_CTRLattribute, whose IPA would pointkvm_update_stolen_time()at the guest's private memory on every vCPU load. Return-EPERMfor the attribute group instead.Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/guest.c | 10 ++++++++++ 1 file changed, 10 insertions(+)arch/arm64/kvm/guest.c +0
-0
arch/arm64/kvm/guest.c int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, · line 922
Quoted 7 lines
arch/arm64/kvm/guest.c +4
-0@@ -916,6 +916,10 @@ int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, ret =kvm_arm_timer_set_attr (vcpu, attr);break ;case KVM_ARM_VCPU_PVTIME_CTRL : +/* Steal time is not offered to protected guests. */ +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; +
nit: We have vcpu_is_protected()
Also, would that make more sense to put that check in kvm_arm_pvtime_supported()?
Quoted 26 lines
arch/arm64/kvm/guest.c +6
-0ret =kvm_arm_pvtime_set_attr (vcpu, attr);break ;default :@@ -939,6 +943,9 @@ int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu, ret =kvm_arm_timer_get_attr (vcpu, attr);break ;case KVM_ARM_VCPU_PVTIME_CTRL : +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; + ret =kvm_arm_pvtime_get_attr (vcpu, attr);break ;default :@@ -962,6 +969,9 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu, ret =kvm_arm_timer_has_attr (vcpu, attr);break ;case KVM_ARM_VCPU_PVTIME_CTRL : +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; + ret =kvm_arm_pvtime_has_attr (vcpu, attr);break ;default :Signature · 1 line
2.39.5
Signature · 1 line
Vincent
Fuad Tabba
reviewerRe: [PATCH 03/17] KVM: arm64: Reject the PVTIME vCPU attribute for protected VMs
Quoted 13 lines · Vincent Donnefort
Quoted 11 lines · Fuad Tabba
pKVM currently does not support steal time for protected guests:
KVM_CAP_STEAL_TIMEreports 0 for them. The host still accepts theKVM_ARM_VCPU_PVTIME_CTRLattribute, whose IPA would pointkvm_update_stolen_time()at the guest's private memory on every vCPU load. Return-EPERMfor the attribute group instead.Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/guest.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
Quoted 14 lines
arch/arm64/kvm/guest.c int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, · line 922
Quoted 12 lines
arch/arm64/kvm/guest.c +4
-0@@ -916,6 +916,10 @@ int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu, ret =kvm_arm_timer_set_attr (vcpu, attr);break ;case KVM_ARM_VCPU_PVTIME_CTRL : +/* Steal time is not offered to protected guests. */ +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; +nit: We have
vcpu_is_protected()
Will do, and I found others in this series to fix with it.
Quoted 1 line
Also, would that make more sense to put that check in
kvm_arm_pvtime_supported()?
kvm_arm_pvtime_supported() takes no vcpu, and its other caller reports KVM_CAP_STEAL_TIME, which is already 0 for a protected VM. More important, moving them would turn these three into -ENXIO, where -EPERM is the right errno: steal time is implemented, it's just not permitted for a protected VM.
Cheers,
/fuad
Quoted 30 lines
Quoted 25 lines
arch/arm64/kvm/guest.c +6
-1ret =kvm_arm_pvtime_set_attr (vcpu, attr);break ;default :@@ -939,6 +943,9 @@ int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu, ret =kvm_arm_timer_get_attr (vcpu, attr);break ;case KVM_ARM_VCPU_PVTIME_CTRL : +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; + ret =kvm_arm_pvtime_get_attr (vcpu, attr);break ;default :@@ -962,6 +969,9 @@ int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu, ret =kvm_arm_timer_has_attr (vcpu, attr);break ;case KVM_ARM_VCPU_PVTIME_CTRL : +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; + ret =kvm_arm_pvtime_has_attr (vcpu, attr);break ;default : --Quoted 2 lines
2.39.5
--
Vincent
Fuad Tabba
reviewerPATCH 4/17 KVM: arm64: Introduce per-EC entry handlers for pKVM
In reply to Fuad TabbaFrom: Marc Zyngier <[email protected]>
Add an ESR_EL2.EC-indexed handler table, entry_hyp_vm_handlers[], consulted from flush_hyp_vcpu() on re-entry when the previous exit was a trap: sync_hyp_vcpu() records the exit reason in the hyp vCPU as exit_code, and the trap's exception class comes from its ESR_EL2.
Entry carries the host's requested PC updates to the hyp vCPU: add vcpu_copy_flag() to copy a masked set of flags between the two vCPU structures, with PC_UPDATE_REQ covering INCREMENT_PC and the pending-exception flags. The wholesale iflags copy moves into the non-protected branch; a protected vCPU takes only PC_UPDATE_REQ.
--- arch/arm64/include/asm/kvm_host.h | 19 +++++++++++++ arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 3 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 39 +++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +19 -0
@@ -1033,11 +1033,28 @@ struct kvm_vcpu_arch { set; \ }) +#define __vcpu_copy_flag (vt, vs, flagset, f, m) \ + do { \ + typeof(vs->arch.flagset) tmp, val; \ + \ + __build_check_flag(vs, flagset, f, m); \ + \ + val = READ_ONCE(vs->arch.flagset); \ + val &= (m); \ + __vcpu_flags_preempt_disable(); \ + tmp = READ_ONCE(vt->arch.flagset); \ + tmp &= ~(m); \ + tmp |= val; \ + WRITE_ONCE(vt->arch.flagset, tmp); \ + __vcpu_flags_preempt_enable(); \ + } while (0) +#define vcpu_get_flag (v, ...) __vcpu_get_flag((v), __VA_ARGS__)#define vcpu_set_flag (v, ...) __vcpu_set_flag((v), __VA_ARGS__)#define vcpu_clear_flag (v, ...) __vcpu_clear_flag((v), __VA_ARGS__)#define vcpu_test_and_clear_flag (v, ...) \ __vcpu_test_and_clear_flag((v), __VA_ARGS__) +#define vcpu_copy_flag (vt, vs, ...) __vcpu_copy_flag((vt), (vs), __VA_ARGS__)/* KVM_ARM_VCPU_INIT completed */ #define VCPU_INITIALIZED __vcpu_single_flag(cflags, BIT(0))
@@ -1055,6 +1072,8 @@ struct kvm_vcpu_arch {#define INCREMENT_PC __vcpu_single_flag(iflags, BIT(1))/* Target EL/MODE (not a single flag, but let's abuse the macro) */ #define EXCEPT_MASK __vcpu_single_flag(iflags, GENMASK(3, 1)) +/* Cover both PENDING_EXCEPTION and EXCEPT_MASK for global operations */ +#define PC_UPDATE_REQ __vcpu_single_flag(iflags, GENMASK(3, 0))/* Host-set: the hyp flushes the non-protected vCPU state in on entry */ #define PKVM_HOST_STATE_DIRTY __vcpu_single_flag(iflags, BIT(4))
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +3 -0
@@ -26,6 +26,9 @@ struct pkvm_hyp_vcpu {* per-cpu pointer tracking us. Otherwise, NULL if not loaded. */ struct pkvm_hyp_vcpu **loaded_hyp_vcpu; + +/* The previous exit's ARM_EXCEPTION_* code. */ +u32 exit_code; };/*
arch/arm64/kvm/hyp/nvhe/hyp-main.c +35 -4
@@ -31,6 +31,17 @@ unsigned int hyp_gicv3_nr_lr;void __kvm_hyp_host_forward_smc (struct kvm_cpu_context *host_ctxt); +typedef void (*hyp_entry_exit_handler_fn )(struct pkvm_hyp_vcpu *); + +static void handle_vm_entry_generic (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +vcpu_copy_flag (&hyp_vcpu->vcpu, hyp_vcpu->host_vcpu,PC_UPDATE_REQ ); +} + +static const hyp_entry_exit_handler_fn entry_hyp_vm_handlers[] = { + [0 ... ESR_ELx_EC_MAX] = handle_vm_entry_generic, +}; +static void __hyp_sve_save_guest (struct kvm_vcpu *vcpu) {__vcpu_assign_sys_reg (vcpu,ZCR_EL1 ,read_sysreg_el1 (SYS_ZCR ));
@@ -216,6 +227,8 @@ static void sync_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu)static void flush_hyp_vcpu (struct pkvm_hyp_vcpu *hyp_vcpu) {struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; +hyp_entry_exit_handler_fn ec_handler; +u8 esr_ec;fpsimd_sve_flush ();flush_debug_state (hyp_vcpu);
@@ -228,6 +241,7 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) {if (vcpu_get_flag (host_vcpu,PKVM_HOST_STATE_DIRTY ))flush_hyp_vcpu_state (hyp_vcpu); + hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags; }else { hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; }
@@ -245,16 +259,31 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & (HCR_TWI |HCR_TWE |HCR_VSE ); - hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags; - hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2;flush_hyp_vgic_state (hyp_vcpu); hyp_vcpu->vcpu.arch.pid = host_vcpu->arch.pid; + +switch (ARM_EXCEPTION_CODE (hyp_vcpu->exit_code)) { +case ARM_EXCEPTION_IRQ : +case ARM_EXCEPTION_EL1_SERROR : +case ARM_EXCEPTION_IL : +break ; +case ARM_EXCEPTION_TRAP : + esr_ec =ESR_ELx_EC (kvm_vcpu_get_esr (&hyp_vcpu->vcpu)); + ec_handler = entry_hyp_vm_handlers[esr_ec]; +if (ec_handler) +ec_handler (hyp_vcpu); +break ; +default : +BUG (); + } + + hyp_vcpu->exit_code =0 ; } -static void sync_hyp_vcpu (struct pkvm_hyp_vcpu *hyp_vcpu) +static void sync_hyp_vcpu (struct pkvm_hyp_vcpu *hyp_vcpu,u32 exit_reason) {struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
@@ -281,6 +310,8 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) host_vcpu->arch.hcr_el2 |= hyp_vcpu->vcpu.arch.hcr_el2 &HCR_VSE ;sync_hyp_vgic_state (hyp_vcpu); + + hyp_vcpu->exit_code = exit_reason; }static void handle___pkvm_vcpu_load (struct kvm_cpu_context *host_ctxt)
@@ -395,7 +426,7 @@ static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt) ret =__kvm_vcpu_run (&hyp_vcpu->vcpu); -sync_hyp_vcpu (hyp_vcpu); +sync_hyp_vcpu (hyp_vcpu, ret); }else {/* The host is fully trusted, run its vCPU directly. */ fpsimd_lazy_switch_to_guest (host_vcpu);
Signature · 1 line
2.39.5
Re: [PATCH 04/17] KVM: arm64: Introduce per-EC entry handlers for pKVM
Quoted 13 lines · Fuad Tabba
From: Marc Zyngier <[email protected]>
Add an ESR_EL2.EC-indexed handler table, entry_hyp_vm_handlers[], consulted from
flush_hyp_vcpu()on re-entry when the previous exit was a trap:sync_hyp_vcpu()records the exit reason in the hyp vCPU asexit_code, and the trap's exception class comes from itsESR_EL2.Entry carries the host's requested PC updates to the hyp vCPU: add
vcpu_copy_flag()to copy a masked set of flags between the two vCPU structures, withPC_UPDATE_REQcoveringINCREMENT_PCand the pending-exception flags. The wholesale iflags copy moves into the non-protected branch; a protected vCPU takes onlyPC_UPDATE_REQ.
Quoted 3 lines
Signed-off-by Marc Zyngier <[email protected]>Co-developed-by Fuad Tabba <[email protected]>Signed-off-by Fuad Tabba <[email protected]>
Not really looked at nVHE much (or pkvm ever), but:
Quoted 67 lines
--- arch/arm64/include/asm/kvm_host.h | 19 +++++++++++++ arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 3 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 39 +++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 4 deletions(-)arch/arm64/include/asm/kvm_host.h +19
-0@@ -1033,11 +1033,28 @@ struct kvm_vcpu_arch { set; \ }) +#define __vcpu_copy_flag (vt, vs, flagset, f, m) \ + do { \ + typeof(vs->arch.flagset) tmp, val; \ + \ + __build_check_flag(vs, flagset, f, m); \ + \ + val = READ_ONCE(vs->arch.flagset); \ + val &= (m); \ + __vcpu_flags_preempt_disable(); \ + tmp = READ_ONCE(vt->arch.flagset); \ + tmp &= ~(m); \ + tmp |= val; \ + WRITE_ONCE(vt->arch.flagset, tmp); \ + __vcpu_flags_preempt_enable(); \ + } while (0) +#define vcpu_get_flag (v, ...) __vcpu_get_flag((v), __VA_ARGS__)#define vcpu_set_flag (v, ...) __vcpu_set_flag((v), __VA_ARGS__)#define vcpu_clear_flag (v, ...) __vcpu_clear_flag((v), __VA_ARGS__)#define vcpu_test_and_clear_flag (v, ...) \ __vcpu_test_and_clear_flag((v), __VA_ARGS__) +#define vcpu_copy_flag (vt, vs, ...) __vcpu_copy_flag((vt), (vs), __VA_ARGS__)/* KVM_ARM_VCPU_INIT completed */ #define VCPU_INITIALIZED __vcpu_single_flag(cflags, BIT(0))@@ -1055,6 +1072,8 @@ struct kvm_vcpu_arch {#define INCREMENT_PC __vcpu_single_flag(iflags, BIT(1))/* Target EL/MODE (not a single flag, but let's abuse the macro) */ #define EXCEPT_MASK __vcpu_single_flag(iflags, GENMASK(3, 1)) +/* Cover both PENDING_EXCEPTION and EXCEPT_MASK for global operations */ +#define PC_UPDATE_REQ __vcpu_single_flag(iflags, GENMASK(3, 0))/* Host-set: the hyp flushes the non-protected vCPU state in on entry */ #define PKVM_HOST_STATE_DIRTY __vcpu_single_flag(iflags, BIT(4))arch/arm64/kvm/hyp/include/nvhe/pkvm.h +3
-0@@ -26,6 +26,9 @@ struct pkvm_hyp_vcpu {* per-cpu pointer tracking us. Otherwise, NULL if not loaded. */ struct pkvm_hyp_vcpu **loaded_hyp_vcpu; + +/* The previous exit's ARM_EXCEPTION_* code. */ +u32 exit_code; };/* arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0
arch/arm64/kvm/hyp/nvhe/hyp-main.c unsigned int hyp_gicv3_nr_lr; · line 39
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6
-0@@ -31,6 +31,17 @@ unsigned int hyp_gicv3_nr_lr;void __kvm_hyp_host_forward_smc (struct kvm_cpu_context *host_ctxt); +typedef void (*hyp_entry_exit_handler_fn )(struct pkvm_hyp_vcpu *); + +static void handle_vm_entry_generic (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +vcpu_copy_flag (&hyp_vcpu->vcpu, hyp_vcpu->host_vcpu,PC_UPDATE_REQ ); +}
This is added here because the full copy of iflags was moved into the !pkvm_hyp_vcpu_is_protected() section, so is needed temporarily until each EC has its own handler.
Thanks,
Joey
Fuad Tabba
reviewerRe: [PATCH 04/17] KVM: arm64: Introduce per-EC entry handlers for pKVM
Quoted 15 lines · Joey Gouly
Quoted 13 lines · Fuad Tabba
From: Marc Zyngier <[email protected]>
Add an ESR_EL2.EC-indexed handler table, entry_hyp_vm_handlers[], consulted from
flush_hyp_vcpu()on re-entry when the previous exit was a trap:sync_hyp_vcpu()records the exit reason in the hyp vCPU asexit_code, and the trap's exception class comes from itsESR_EL2.Entry carries the host's requested PC updates to the hyp vCPU: add
vcpu_copy_flag()to copy a masked set of flags between the two vCPU structures, withPC_UPDATE_REQcoveringINCREMENT_PCand the pending-exception flags. The wholesale iflags copy moves into the non-protected branch; a protected vCPU takes onlyPC_UPDATE_REQ.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Marc Zyngier <[email protected]>Co-developed-by Fuad Tabba <[email protected]>Signed-off-by Fuad Tabba <[email protected]>Not really looked at nVHE much (or pkvm ever), but:
That's a plus. Good to get a fresh perspective on the code.
Quoted 1 line
Reviewed-by Joey Gouly <[email protected]>
Thanks for the review!
/fuad
Quoted 84 lines
Quoted 68 lines
--- arch/arm64/include/asm/kvm_host.h | 19 +++++++++++++ arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 3 ++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 39 +++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 4 deletions(-)arch/arm64/include/asm/kvm_host.h +19
-0@@ -1033,11 +1033,28 @@ struct kvm_vcpu_arch { set; \ }) +#define __vcpu_copy_flag (vt, vs, flagset, f, m) \ + do { \ + typeof(vs->arch.flagset) tmp, val; \ + \ + __build_check_flag(vs, flagset, f, m); \ + \ + val = READ_ONCE(vs->arch.flagset); \ + val &= (m); \ + __vcpu_flags_preempt_disable(); \ + tmp = READ_ONCE(vt->arch.flagset); \ + tmp &= ~(m); \ + tmp |= val; \ + WRITE_ONCE(vt->arch.flagset, tmp); \ + __vcpu_flags_preempt_enable(); \ + } while (0) +#define vcpu_get_flag (v, ...) __vcpu_get_flag((v), __VA_ARGS__)#define vcpu_set_flag (v, ...) __vcpu_set_flag((v), __VA_ARGS__)#define vcpu_clear_flag (v, ...) __vcpu_clear_flag((v), __VA_ARGS__)#define vcpu_test_and_clear_flag (v, ...) \ __vcpu_test_and_clear_flag((v), __VA_ARGS__) +#define vcpu_copy_flag (vt, vs, ...) __vcpu_copy_flag((vt), (vs), __VA_ARGS__)/* KVM_ARM_VCPU_INIT completed */ #define VCPU_INITIALIZED __vcpu_single_flag(cflags, BIT(0))@@ -1055,6 +1072,8 @@ struct kvm_vcpu_arch {#define INCREMENT_PC __vcpu_single_flag(iflags, BIT(1))/* Target EL/MODE (not a single flag, but let's abuse the macro) */ #define EXCEPT_MASK __vcpu_single_flag(iflags, GENMASK(3, 1)) +/* Cover both PENDING_EXCEPTION and EXCEPT_MASK for global operations */ +#define PC_UPDATE_REQ __vcpu_single_flag(iflags, GENMASK(3, 0))/* Host-set: the hyp flushes the non-protected vCPU state in on entry */ #define PKVM_HOST_STATE_DIRTY __vcpu_single_flag(iflags, BIT(4))arch/arm64/kvm/hyp/include/nvhe/pkvm.h +3
-0@@ -26,6 +26,9 @@ struct pkvm_hyp_vcpu {* per-cpu pointer tracking us. Otherwise, NULL if not loaded. */ struct pkvm_hyp_vcpu **loaded_hyp_vcpu; + +/* The previous exit's ARM_EXCEPTION_* code. */ +u32 exit_code; };/* arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0arch/arm64/kvm/hyp/nvhe/hyp-main.c unsigned int hyp_gicv3_nr_lr; · line 39
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6
-0@@ -31,6 +31,17 @@ unsigned int hyp_gicv3_nr_lr;void __kvm_hyp_host_forward_smc (struct kvm_cpu_context *host_ctxt); +typedef void (*hyp_entry_exit_handler_fn )(struct pkvm_hyp_vcpu *); + +static void handle_vm_entry_generic (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +vcpu_copy_flag (&hyp_vcpu->vcpu, hyp_vcpu->host_vcpu,PC_UPDATE_REQ ); +}This is added here because the full copy of iflags was moved into the !pkvm_hyp_vcpu_is_protected() section, so is needed temporarily until each EC has its own handler.
Thanks,
Joey
Fuad Tabba
reviewerPATCH 5/17 KVM: arm64: Skip fixed-feature state flush for protected vCPUs
In reply to Fuad Tabbaflush_hyp_vcpu() copies the host's mdcr_el2, TWI/TWE and debug state into the hyp vCPU on every entry, so for a protected vCPU the host's trap configuration overrides the one pkvm_vcpu_init_traps() computed at EL2. Move those copies into the non-protected branch, and sync the debug state back for non-protected vCPUs only: a protected guest's debug registers are hypervisor-owned, and it takes the host's TWI/TWE at vCPU load. MDCR_EL2.TDA traps only the guest's own accesses; it does not stop the world switch from loading host-supplied breakpoints and MDSCR_EL1.
EL2's mdcr_el2 starts from 0, and HPMN == 0 is reserved without FEAT_HPMN0. Set HPMN to the CPU's event counter count at vCPU load, the value the host's copy carried; a protected guest's PMU accesses trap regardless.
--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 31 +++++++++++++++++++----------- arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +20 -11
@@ -231,7 +231,6 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)u8 esr_ec;fpsimd_sve_flush (); -flush_debug_state (hyp_vcpu);/* * If we deal with a non-protected guest and the state is potentially
@@ -241,7 +240,14 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) {if (vcpu_get_flag (host_vcpu,PKVM_HOST_STATE_DIRTY ))flush_hyp_vcpu_state (hyp_vcpu); + + hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE ); + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & + (HCR_TWI |HCR_TWE ); + + hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2; hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags; +flush_debug_state (hyp_vcpu); }else { hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; }
@@ -249,17 +255,13 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)/* __hyp_running_vcpu must be NULL in a guest context. */ hyp_vcpu->vcpu.arch.ctxt.__hyp_running_vcpu =NULL ; - hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;/* -* HCR_EL2.VSE is host-owned (a pending virtual SError to inject), not a -* trap-control bit, so it must flow to the hyp vCPU alongside TWI/TWE -* for the vSError to be delivered. sync_hyp_vcpu() reflects it back. +* A host-injected vSError is masked by the guest's own PSTATE.A, so it +* applies to protected guests too. */ - hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE |HCR_VSE ); - hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & - (HCR_TWI |HCR_TWE |HCR_VSE ); - - hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2; + hyp_vcpu->vcpu.arch.hcr_el2 &= ~HCR_VSE ; + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) &HCR_VSE ; + hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2;flush_hyp_vgic_state (hyp_vcpu);
@@ -288,7 +290,8 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason)struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;fpsimd_sve_sync (&hyp_vcpu->vcpu); -sync_debug_state (hyp_vcpu); +if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +sync_debug_state (hyp_vcpu);if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) { host_vcpu->arch.ctxt = hyp_vcpu->vcpu.arch.ctxt;
@@ -329,6 +332,12 @@ static void handle___pkvm_vcpu_load(struct kvm_cpu_context *host_ctxt)/* Propagate WFx trapping flags */ hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWE |HCR_TWI ); hyp_vcpu->vcpu.arch.hcr_el2 |= hcr_el2 & (HCR_TWE |HCR_TWI ); + +/* HPMN == 0 is reserved without FEAT_HPMN0. */ +if (system_supports_pmuv3 ()) +u64p_replace_bits (&hyp_vcpu->vcpu.arch.mdcr_el2, +FIELD_GET (ARMV8_PMU_PMCR_N ,read_sysreg (pmcr_el0)), +MDCR_EL2_HPMN ); }else {memcpy (&hyp_vcpu->vcpu.arch.fgt, hyp_vcpu->host_vcpu->arch.fgt,sizeof (hyp_vcpu->vcpu.arch.fgt));
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
@@ -102,7 +102,7 @@ static void pvm_init_traps_mdcr(struct kvm_vcpu *vcpu)if (!kvm_has_feat (kvm,ID_AA64DFR0_EL1 , PMUVer,IMP )) { val |=MDCR_EL2_TPM |MDCR_EL2_TPMCR ; - val &= ~(MDCR_EL2_HPME |MDCR_EL2_MTPME |MDCR_EL2_HPMN_MASK ); + val &= ~(MDCR_EL2_HPME |MDCR_EL2_MTPME ); }if (!kvm_has_feat (kvm,ID_AA64DFR0_EL1 , DebugVer,IMP ))
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH 05/17] KVM: arm64: Skip fixed-feature state flush for protected vCPUs
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Under-inclusion in the per-entry `HCR_EL2` allowlist for protected guests, dropping dynamic WFx…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Under-inclusion in the per-entry HCR_EL2 allowlist for protected guests, dropping dynamic WFx trapping policy updates. --
commit 2c210642954dbb2df4858e3ad74ba54ba96b05d7
Author: Fuad Tabba <[email protected]>
KVM: arm64: Skip fixed-feature state flush for protected vCPUs
This commit optimizes the per-entry synchronization for protected vCPUs by moving the copy of fixed-feature state (like mdcr_el2, TWI/TWE, and debug state) to the non-protected branch and the per-vCPU load path.
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0
[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) · line 246
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4
-0@@ -241,7 +240,14 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) {if (vcpu_get_flag (host_vcpu,PKVM_HOST_STATE_DIRTY ))flush_hyp_vcpu_state (hyp_vcpu); + + hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE ); + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & + (HCR_TWI |HCR_TWE );
[Severity: Medium]
Does restricting the per-entry synchronization of HCR_TWI and HCR_TWE to only non-protected guests break dynamic scheduling behavior?
These bits act as host-owned runtime signals. If the host dynamically updates vcpu->arch.hcr_el2 (for example, via kvm_vcpu_on_spin() to yield on spinlocks) and re-enters the guest, the updated WFI/WFE trapping policy won't be synced on entry for protected guests because it is now skipped here.
[ ... ]
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-4@@ -249,17 +255,13 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)/* __hyp_running_vcpu must be NULL in a guest context. */ hyp_vcpu->vcpu.arch.ctxt.__hyp_running_vcpu =NULL ; - hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;/* -* HCR_EL2.VSE is host-owned (a pending virtual SError to inject), not a -* trap-control bit, so it must flow to the hyp vCPU alongside TWI/TWE -* for the vSError to be delivered. sync_hyp_vcpu() reflects it back.
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) · line 263
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4
-5+* A host-injected vSError is masked by the guest's own PSTATE.A, so it +* applies to protected guests too. */ - hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE |HCR_VSE ); - hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & - (HCR_TWI |HCR_TWE |HCR_VSE ); - - hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2; + hyp_vcpu->vcpu.arch.hcr_el2 &= ~HCR_VSE ; + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) &HCR_VSE ;
[Severity: Medium]
Because HCR_TWI and HCR_TWE were removed from this unconditional allowlist in flush_hyp_vcpu(), are we under-including these required bits for protected guests on re-entry?
[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___pkvm_vcpu_load(struct kvm_cpu_context *host_ctxt) · line 334
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0@@ -329,6 +332,12 @@ static void handle___pkvm_vcpu_load(struct kvm_cpu_context *host_ctxt)/* Propagate WFx trapping flags */ hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWE |HCR_TWI ); hyp_vcpu->vcpu.arch.hcr_el2 |= hcr_el2 & (HCR_TWE |HCR_TWI );
[Severity: Medium]
While this code satisfies the WFx trapping configuration in handle___pkvm_vcpu_load(), doesn't moving it exclusively to the per-load path mean that any subsequent per-entry policy updates made by the host will be silently dropped for protected VMs?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Fuad Tabba
reviewerRe: [PATCH 05/17] KVM: arm64: Skip fixed-feature state flush for protected vCPUs
Not this one Sashiko.,..
Quoted 30 lines · On Mon, 31 Aug 2026 at 20:59, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Under-inclusion in the per-entryHCR_EL2allowlist for protected guests, dropping dynamic WFx trapping policy updates. --commit
2c210642954dbb2df4858e3ad74ba54ba96b05d7
Author: Fuad Tabba <[email protected]>KVM: arm64: Skip fixed-feature state flush for protected vCPUs
This commit optimizes the per-entry synchronization for protected vCPUs by moving the copy of fixed-feature state (like
mdcr_el2, TWI/TWE, and debug state) to the non-protected branch and the per-vCPU load path.Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) · line 246
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4
-0@@ -241,7 +240,14 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) {if (vcpu_get_flag (host_vcpu,PKVM_HOST_STATE_DIRTY ))flush_hyp_vcpu_state (hyp_vcpu); + + hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE ); + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & + (HCR_TWI |HCR_TWE );[Severity: Medium]
Does restricting the per-entry synchronization ofHCR_TWIandHCR_TWEto only non-protected guests break dynamic scheduling behavior?
Quoted 4 lines
These bits act as host-owned runtime signals. If the host dynamically updates
vcpu->arch.hcr_el2(for example, viakvm_vcpu_on_spin()to yield on spinlocks) and re-enters the guest, the updated WFI/WFE trapping policy won't be synced on entry for protected guests because it is now skipped here.
kvm_arch_vcpu_load() is the only writer of those two bits, and it calls __pkvm_vcpu_load() with hcr_el2 a few lines later. There is no update in between to drop.
/fuad
/fuad
Quoted 41 lines
[ ... ]
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-4@@ -249,17 +255,13 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)/* __hyp_running_vcpu must be NULL in a guest context. */ hyp_vcpu->vcpu.arch.ctxt.__hyp_running_vcpu =NULL ; - hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;/* -* HCR_EL2.VSE is host-owned (a pending virtual SError to inject), not a -* trap-control bit, so it must flow to the hyp vCPU alongside TWI/TWE -* for the vSError to be delivered. sync_hyp_vcpu() reflects it back. arch/arm64/kvm/hyp/nvhe/hyp-main.c static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) · line 263
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4
-5+* A host-injected vSError is masked by the guest's own PSTATE.A, so it +* applies to protected guests too. */ - hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE |HCR_VSE ); - hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & - (HCR_TWI |HCR_TWE |HCR_VSE ); - - hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2; + hyp_vcpu->vcpu.arch.hcr_el2 &= ~HCR_VSE ; + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) &HCR_VSE ;[Severity: Medium]
BecauseHCR_TWIandHCR_TWEwere removed from this unconditional allowlist inflush_hyp_vcpu(), are we under-including these required bits for protected guests on re-entry?[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle___pkvm_vcpu_load(struct kvm_cpu_context *host_ctxt) · line 334
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0@@ -329,6 +332,12 @@ static void handle___pkvm_vcpu_load(struct kvm_cpu_context *host_ctxt)/* Propagate WFx trapping flags */ hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWE |HCR_TWI ); hyp_vcpu->vcpu.arch.hcr_el2 |= hcr_el2 & (HCR_TWE |HCR_TWI );[Severity: Medium]
While this code satisfies the WFx trapping configuration inhandle___pkvm_vcpu_load(), doesn't moving it exclusively to the per-load path mean that any subsequent per-entry policy updates made by the host will be silently dropped for protected VMs?--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH 05/17] KVM: arm64: Skip fixed-feature state flush for protected vCPUs
In reply to Fuad TabbaQuoted 72 lines · Fuad Tabba
flush_hyp_vcpu()copies the host'smdcr_el2, TWI/TWE and debug state into the hyp vCPU on every entry, so for a protected vCPU the host's trap configuration overrides the onepkvm_vcpu_init_traps()computed at EL2. Move those copies into the non-protected branch, and sync the debug state back for non-protected vCPUs only: a protected guest's debug registers are hypervisor-owned, and it takes the host's TWI/TWE at vCPU load. MDCR_EL2.TDA traps only the guest's own accesses; it does not stop the world switch from loading host-supplied breakpoints andMDSCR_EL1.EL2's
mdcr_el2starts from 0, and HPMN == 0 is reserved withoutFEAT_HPMN0. Set HPMN to the CPU's event counter count at vCPU load, the value the host's copy carried; a protected guest's PMU accesses trap regardless.Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 31 +++++++++++++++++++----------- arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-)arch/arm64/kvm/hyp/nvhe/hyp-main.c +12
-10@@ -231,7 +231,6 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)u8 esr_ec;fpsimd_sve_flush (); -flush_debug_state (hyp_vcpu);/* * If we deal with a non-protected guest and the state is potentially @@ -241,7 +240,14 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) {if (vcpu_get_flag (host_vcpu,PKVM_HOST_STATE_DIRTY ))flush_hyp_vcpu_state (hyp_vcpu); + + hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE ); + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & + (HCR_TWI |HCR_TWE ); + + hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2; hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags; +flush_debug_state (hyp_vcpu); }else { hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; }@@ -249,17 +255,13 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)/* __hyp_running_vcpu must be NULL in a guest context. */ hyp_vcpu->vcpu.arch.ctxt.__hyp_running_vcpu =NULL ; - hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;/* -* HCR_EL2.VSE is host-owned (a pending virtual SError to inject), not a -* trap-control bit, so it must flow to the hyp vCPU alongside TWI/TWE -* for the vSError to be delivered. sync_hyp_vcpu() reflects it back. +* A host-injected vSError is masked by the guest's own PSTATE.A, so it +* applies to protected guests too. */ - hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE |HCR_VSE ); - hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & - (HCR_TWI |HCR_TWE |HCR_VSE ); - - hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2; + hyp_vcpu->vcpu.arch.hcr_el2 &= ~HCR_VSE ; + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) &HCR_VSE ; + hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2;flush_hyp_vgic_state (hyp_vcpu);
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason) · line 294
Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2
-1@@ -288,7 +290,8 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason)struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;fpsimd_sve_sync (&hyp_vcpu->vcpu); -sync_debug_state (hyp_vcpu); +if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +sync_debug_state (hyp_vcpu);
nit: We can't use the else arm below?
Quoted 32 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6
-0if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) { host_vcpu->arch.ctxt = hyp_vcpu->vcpu.arch.ctxt;@@ -329,6 +332,12 @@ static void handle___pkvm_vcpu_load(struct kvm_cpu_context *host_ctxt)/* Propagate WFx trapping flags */ hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWE |HCR_TWI ); hyp_vcpu->vcpu.arch.hcr_el2 |= hcr_el2 & (HCR_TWE |HCR_TWI ); + +/* HPMN == 0 is reserved without FEAT_HPMN0. */ +if (system_supports_pmuv3 ()) +u64p_replace_bits (&hyp_vcpu->vcpu.arch.mdcr_el2, +FIELD_GET (ARMV8_PMU_PMCR_N ,read_sysreg (pmcr_el0)), +MDCR_EL2_HPMN ); }else {memcpy (&hyp_vcpu->vcpu.arch.fgt, hyp_vcpu->host_vcpu->arch.fgt,sizeof (hyp_vcpu->vcpu.arch.fgt));arch/arm64/kvm/hyp/nvhe/pkvm.c +1
-1@@ -102,7 +102,7 @@ static void pvm_init_traps_mdcr(struct kvm_vcpu *vcpu)if (!kvm_has_feat (kvm,ID_AA64DFR0_EL1 , PMUVer,IMP )) { val |=MDCR_EL2_TPM |MDCR_EL2_TPMCR ; - val &= ~(MDCR_EL2_HPME |MDCR_EL2_MTPME |MDCR_EL2_HPMN_MASK ); + val &= ~(MDCR_EL2_HPME |MDCR_EL2_MTPME ); }if (!kvm_has_feat (kvm,ID_AA64DFR0_EL1 , DebugVer,IMP ))Signature · 1 line
2.39.5
Re: [PATCH 05/17] KVM: arm64: Skip fixed-feature state flush for protected vCPUs
Quoted 75 lines · Vincent Donnefort
Quoted 74 lines · Fuad Tabba
flush_hyp_vcpu()copies the host'smdcr_el2, TWI/TWE and debug state into the hyp vCPU on every entry, so for a protected vCPU the host's trap configuration overrides the onepkvm_vcpu_init_traps()computed at EL2. Move those copies into the non-protected branch, and sync the debug state back for non-protected vCPUs only: a protected guest's debug registers are hypervisor-owned, and it takes the host's TWI/TWE at vCPU load. MDCR_EL2.TDA traps only the guest's own accesses; it does not stop the world switch from loading host-supplied breakpoints andMDSCR_EL1.EL2's
mdcr_el2starts from 0, and HPMN == 0 is reserved withoutFEAT_HPMN0. Set HPMN to the CPU's event counter count at vCPU load, the value the host's copy carried; a protected guest's PMU accesses trap regardless.Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 31 +++++++++++++++++++----------- arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-)arch/arm64/kvm/hyp/nvhe/hyp-main.c +12
-10@@ -231,7 +231,6 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)u8 esr_ec;fpsimd_sve_flush (); -flush_debug_state (hyp_vcpu);/* * If we deal with a non-protected guest and the state is potentially @@ -241,7 +240,14 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) {if (vcpu_get_flag (host_vcpu,PKVM_HOST_STATE_DIRTY ))flush_hyp_vcpu_state (hyp_vcpu); + + hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE ); + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & + (HCR_TWI |HCR_TWE ); + + hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2; hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags; +flush_debug_state (hyp_vcpu); }else { hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; }@@ -249,17 +255,13 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)/* __hyp_running_vcpu must be NULL in a guest context. */ hyp_vcpu->vcpu.arch.ctxt.__hyp_running_vcpu =NULL ; - hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;/* -* HCR_EL2.VSE is host-owned (a pending virtual SError to inject), not a -* trap-control bit, so it must flow to the hyp vCPU alongside TWI/TWE -* for the vSError to be delivered. sync_hyp_vcpu() reflects it back. +* A host-injected vSError is masked by the guest's own PSTATE.A, so it +* applies to protected guests too. */ - hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE |HCR_VSE ); - hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & - (HCR_TWI |HCR_TWE |HCR_VSE ); - - hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2; + hyp_vcpu->vcpu.arch.hcr_el2 &= ~HCR_VSE ; + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) &HCR_VSE ; + hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2;flush_hyp_vgic_state (hyp_vcpu);@@ -288,7 +290,8 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason)struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;
Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason) · line 294
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2
-1fpsimd_sve_sync (&hyp_vcpu->vcpu); -sync_debug_state (hyp_vcpu); +if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +sync_debug_state (hyp_vcpu);nit: We can't use the else arm below?
or actually could we move the check into sync_debug_state() just like sync_hyp_timer_state() does?
Quoted 33 lines
Quoted 33 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6
-0if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) { host_vcpu->arch.ctxt = hyp_vcpu->vcpu.arch.ctxt;@@ -329,6 +332,12 @@ static void handle___pkvm_vcpu_load(struct kvm_cpu_context *host_ctxt)/* Propagate WFx trapping flags */ hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWE |HCR_TWI ); hyp_vcpu->vcpu.arch.hcr_el2 |= hcr_el2 & (HCR_TWE |HCR_TWI ); + +/* HPMN == 0 is reserved without FEAT_HPMN0. */ +if (system_supports_pmuv3 ()) +u64p_replace_bits (&hyp_vcpu->vcpu.arch.mdcr_el2, +FIELD_GET (ARMV8_PMU_PMCR_N ,read_sysreg (pmcr_el0)), +MDCR_EL2_HPMN ); }else {memcpy (&hyp_vcpu->vcpu.arch.fgt, hyp_vcpu->host_vcpu->arch.fgt,sizeof (hyp_vcpu->vcpu.arch.fgt));arch/arm64/kvm/hyp/nvhe/pkvm.c +1
-1@@ -102,7 +102,7 @@ static void pvm_init_traps_mdcr(struct kvm_vcpu *vcpu)if (!kvm_has_feat (kvm,ID_AA64DFR0_EL1 , PMUVer,IMP )) { val |=MDCR_EL2_TPM |MDCR_EL2_TPMCR ; - val &= ~(MDCR_EL2_HPME |MDCR_EL2_MTPME |MDCR_EL2_HPMN_MASK ); + val &= ~(MDCR_EL2_HPME |MDCR_EL2_MTPME ); }if (!kvm_has_feat (kvm,ID_AA64DFR0_EL1 , DebugVer,IMP ))Signature · 1 line
2.39.5
Signature · 1 line
Vincent
Fuad Tabba
reviewerRe: [PATCH 05/17] KVM: arm64: Skip fixed-feature state flush for protected vCPUs
Quoted 84 lines · Vincent Donnefort
Quoted 75 lines · Vincent Donnefort
Quoted 74 lines · Fuad Tabba
flush_hyp_vcpu()copies the host'smdcr_el2, TWI/TWE and debug state into the hyp vCPU on every entry, so for a protected vCPU the host's trap configuration overrides the onepkvm_vcpu_init_traps()computed at EL2. Move those copies into the non-protected branch, and sync the debug state back for non-protected vCPUs only: a protected guest's debug registers are hypervisor-owned, and it takes the host's TWI/TWE at vCPU load. MDCR_EL2.TDA traps only the guest's own accesses; it does not stop the world switch from loading host-supplied breakpoints andMDSCR_EL1.EL2's
mdcr_el2starts from 0, and HPMN == 0 is reserved withoutFEAT_HPMN0. Set HPMN to the CPU's event counter count at vCPU load, the value the host's copy carried; a protected guest's PMU accesses trap regardless.Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 31 +++++++++++++++++++----------- arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-)arch/arm64/kvm/hyp/nvhe/hyp-main.c +12
-10@@ -231,7 +231,6 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)u8 esr_ec;fpsimd_sve_flush (); -flush_debug_state (hyp_vcpu);/* * If we deal with a non-protected guest and the state is potentially @@ -241,7 +240,14 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) {if (vcpu_get_flag (host_vcpu,PKVM_HOST_STATE_DIRTY ))flush_hyp_vcpu_state (hyp_vcpu); + + hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE ); + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & + (HCR_TWI |HCR_TWE ); + + hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2; hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags; +flush_debug_state (hyp_vcpu); }else { hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; }@@ -249,17 +255,13 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)/* __hyp_running_vcpu must be NULL in a guest context. */ hyp_vcpu->vcpu.arch.ctxt.__hyp_running_vcpu =NULL ; - hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;/* -* HCR_EL2.VSE is host-owned (a pending virtual SError to inject), not a -* trap-control bit, so it must flow to the hyp vCPU alongside TWI/TWE -* for the vSError to be delivered. sync_hyp_vcpu() reflects it back. +* A host-injected vSError is masked by the guest's own PSTATE.A, so it +* applies to protected guests too. */ - hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI |HCR_TWE |HCR_VSE ); - hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) & - (HCR_TWI |HCR_TWE |HCR_VSE ); - - hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2; + hyp_vcpu->vcpu.arch.hcr_el2 &= ~HCR_VSE ; + hyp_vcpu->vcpu.arch.hcr_el2 |=READ_ONCE (host_vcpu->arch.hcr_el2) &HCR_VSE ; + hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2;flush_hyp_vgic_state (hyp_vcpu);@@ -288,7 +290,8 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason)struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason) · line 294
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2
-1fpsimd_sve_sync (&hyp_vcpu->vcpu); -sync_debug_state (hyp_vcpu); +if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +sync_debug_state (hyp_vcpu);nit: We can't use the else arm below?
Quoted 2 lines
or actually could we move the check into
sync_debug_state()just likesync_hyp_timer_state()does?
Will do, and there's another case of this in the series that gets fixed with it.
/fuad
Quoted 37 lines
Quoted 30 lines
Quoted 30 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6
-0if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) { host_vcpu->arch.ctxt = hyp_vcpu->vcpu.arch.ctxt;@@ -329,6 +332,12 @@ static void handle___pkvm_vcpu_load(struct kvm_cpu_context *host_ctxt)/* Propagate WFx trapping flags */ hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWE |HCR_TWI ); hyp_vcpu->vcpu.arch.hcr_el2 |= hcr_el2 & (HCR_TWE |HCR_TWI ); + +/* HPMN == 0 is reserved without FEAT_HPMN0. */ +if (system_supports_pmuv3 ()) +u64p_replace_bits (&hyp_vcpu->vcpu.arch.mdcr_el2, +FIELD_GET (ARMV8_PMU_PMCR_N ,read_sysreg (pmcr_el0)), +MDCR_EL2_HPMN ); }else {memcpy (&hyp_vcpu->vcpu.arch.fgt, hyp_vcpu->host_vcpu->arch.fgt,sizeof (hyp_vcpu->vcpu.arch.fgt));arch/arm64/kvm/hyp/nvhe/pkvm.c +1
-1@@ -102,7 +102,7 @@ static void pvm_init_traps_mdcr(struct kvm_vcpu *vcpu)if (!kvm_has_feat (kvm,ID_AA64DFR0_EL1 , PMUVer,IMP )) { val |=MDCR_EL2_TPM |MDCR_EL2_TPMCR ; - val &= ~(MDCR_EL2_HPME |MDCR_EL2_MTPME |MDCR_EL2_HPMN_MASK ); + val &= ~(MDCR_EL2_HPME |MDCR_EL2_MTPME ); }Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-1if (!kvm_has_feat (kvm,ID_AA64DFR0_EL1 , DebugVer,IMP )) --2.39.5
--
Vincent
Fuad Tabba
reviewerPATCH 6/17 KVM: arm64: Add {flush,sync}_hyp_timer_state() primitives
In reply to Fuad TabbaFrom: Marc Zyngier <[email protected]>
A protected guest's virtual timer is programmed from EL2's own copy of the state and read back there, so the host cannot change the guest's deadline. cntvoff_el2 is zeroed, so the guest's virtual time is the physical time.
The flush also preserves the guest's CNTV and CNTP CVAL/CTL across the host's full-context copy into the hyp vCPU.
--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +39 -0
@@ -154,6 +154,33 @@ static void sync_hyp_vgic_state(struct pkvm_hyp_vcpu *hyp_vcpu) host_cpu_if->vgic_lr[i] = hyp_cpu_if->vgic_lr[i]; } +static void flush_hyp_timer_state (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +return ; + +/* A hyp vcpu has no offset, and sees vtime == ptime. */ +write_sysreg (0 , cntvoff_el2); +write_sysreg_el0 (__vcpu_sys_reg (&hyp_vcpu->vcpu,CNTV_CVAL_EL0 ), +SYS_CNTV_CVAL ); +isb (); +write_sysreg_el0 (__vcpu_sys_reg (&hyp_vcpu->vcpu,CNTV_CTL_EL0 ), +SYS_CNTV_CTL ); +} + +static void sync_hyp_timer_state (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +return ; + +/* +* Preserve the vtimer state so that it is always correct, +* even if the host tries to make a mess. +*/ +__vcpu_assign_sys_reg (&hyp_vcpu->vcpu,CNTV_CVAL_EL0 ,read_sysreg_el0 (SYS_CNTV_CVAL )); +__vcpu_assign_sys_reg (&hyp_vcpu->vcpu,CNTV_CTL_EL0 ,read_sysreg_el0 (SYS_CNTV_CTL )); +} +static void __copy_vcpu_state (const struct kvm_vcpu *from_vcpu,struct kvm_vcpu *to_vcpu) {
@@ -249,7 +276,17 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags;flush_debug_state (hyp_vcpu); }else { +u64 v_cval = hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTV_CVAL_EL0 ]; +u64 v_ctl = hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTV_CTL_EL0 ]; +u64 p_cval = hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTP_CVAL_EL0 ]; +u64 p_ctl = hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTP_CTL_EL0 ]; + hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; + + hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTV_CVAL_EL0 ] = v_cval; + hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTV_CTL_EL0 ] = v_ctl; + hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTP_CVAL_EL0 ] = p_cval; + hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTP_CTL_EL0 ] = p_ctl; }/* __hyp_running_vcpu must be NULL in a guest context. */
@@ -264,6 +301,7 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) hyp_vcpu->vcpu.arch.vsesr_el2 = host_vcpu->arch.vsesr_el2;flush_hyp_vgic_state (hyp_vcpu); +flush_hyp_timer_state (hyp_vcpu); hyp_vcpu->vcpu.arch.pid = host_vcpu->arch.pid;
@@ -313,6 +351,7 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason) host_vcpu->arch.hcr_el2 |= hyp_vcpu->vcpu.arch.hcr_el2 &HCR_VSE ;sync_hyp_vgic_state (hyp_vcpu); +sync_hyp_timer_state (hyp_vcpu); hyp_vcpu->exit_code = exit_reason; }
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 7/17 KVM: arm64: Add system register reset framework for protected VMs
In reply to Fuad TabbaReset a protected VM's system registers at EL2 rather than taking the host's values: add kvm_reset_pvm_sys_regs() and the pvm_sys_reg_reset_vals[] table that drives it, and call it from init_pkvm_hyp_vcpu() for protected vCPUs. The values follow the host-side reset in sys_regs.c, with a poison value where it resets to UNKNOWN, and for VBAR_EL1 and CONTEXTIDR_EL1 in place of the 0 it resets them to. MPIDR_EL1 is derived from vcpu_id, as for any KVM guest.
--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 1 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 5 ++ arch/arm64/kvm/hyp/nvhe/sys_regs.c | 91 ++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 4 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1 -0
@@ -95,6 +95,7 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code);bool kvm_handle_pvm_sysreg (struct kvm_vcpu *vcpu,u64 *exit_code);bool kvm_handle_pvm_restricted (struct kvm_vcpu *vcpu,u64 *exit_code);void kvm_init_pvm_id_regs (struct kvm_vcpu *vcpu); +void kvm_reset_pvm_sys_regs (struct kvm_vcpu *vcpu);int kvm_check_pvm_sysreg_table (void );#endif /* __ARM64_KVM_NVHE_PKVM_H__ */
arch/arm64/kvm/hyp/nvhe/pkvm.c +5 -0
@@ -551,6 +551,11 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, goto done; ret =pkvm_vcpu_init_sve (hyp_vcpu, host_vcpu); +if (ret) + goto done; + +if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +kvm_reset_pvm_sys_regs (&hyp_vcpu->vcpu); done:if (ret)unpin_host_vcpu (host_vcpu);
arch/arm64/kvm/hyp/nvhe/sys_regs.c +87 -4
109 lines not on this page · raw message
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH 07/17] KVM: arm64: Add system register reset framework for protected VMs
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] EL2 reads host-controlled system registers (ACTLR_EL1 and AMAIR_EL1) to initialize protected VM…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] EL2 reads host-controlled system registers (ACTLR_EL1 and AMAIR_EL1) to initialize protected VM state, violating the pKVM trust boundary.
- [High] Host-provided vcpu_id is used to calculate the protected VM's MPIDR_EL1 without uniqueness validation, violating architectural constraints. --
commit 84ffa62c045287810116009ea32700416618900e
Author: Fuad Tabba <[email protected]>
Subject: KVM: arm64: Add system register reset framework for protected VMs
Reset a protected VM's system registers at EL2 rather than taking the host's values: add kvm_reset_pvm_sys_regs() and the pvm_sys_reg_reset_vals[] table that drives it, and call it from init_pkvm_hyp_vcpu() for protected vCPUs. The values follow the host-side reset in sys_regs.c, with a poison value where it resets to UNKNOWN, and for VBAR_EL1 and CONTEXTIDR_EL1 in place of the 0 it resets them to. MPIDR_EL1 is derived from vcpu_id, as for any KVM guest.
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0
-0
[ ... ]
arch/arm64/kvm/hyp/nvhe/sys_regs.c static const struct sys_reg_desc pvm_sys_reg_descs[] = { · line 543
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +10
-0+/* Hardware value, as sys_regs.c's reset_actlr()/reset_amair_el1(). */ +static void reset_actlr (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,read_sysreg (actlr_el1)); +} + +static void reset_amair_el1 (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,read_sysreg (amair_el1)); +}
[Severity: High]
Does this allow the host to dictate the protected guest's initial register state?
Since EL1 has write access to both the actlr_el1 and amair_el1 hardware registers, the host could set them to arbitrary values before the vCPU creation hypercall is invoked. Seeding EL2-private state for a protected guest by reading host-modifiable hardware registers instead of using hardcoded architectural constants appears to breach the pKVM trust boundary.
arch/arm64/kvm/hyp/nvhe/sys_regs.c static const struct sys_reg_desc pvm_sys_reg_descs[] = { · line 548
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +5
-0+ +static void reset_mpidr (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,kvm_calculate_mpidr (vcpu)); +}
[Severity: High]
Can a malicious host supply duplicate vcpu_id values?
Earlier in init_pkvm_hyp_vcpu(), hyp_vcpu->vcpu.vcpu_id is directly copied from the untrusted host_vcpu. Since kvm_calculate_mpidr() calculates the guest's MPIDR_EL1 value based on this unvalidated vcpu_id, could the host use this to instantiate a protected VM with duplicate MPIDR_EL1 values across its vCPUs?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Fuad Tabba
reviewerRe: [PATCH 07/17] KVM: arm64: Add system register reset framework for protected VMs
Sashiko, I challenged you, and you delievered... I'll give you 1/2 here...
Quoted 26 lines · On Mon, 31 Aug 2026 at 21:22, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] EL2 reads host-controlled system registers (ACTLR_EL1andAMAIR_EL1) to initialize protected VM state, violating the pKVM trust boundary.
- [High] Host-providedvcpu_idis used to calculate the protected VM'sMPIDR_EL1without uniqueness validation, violating architectural constraints. --commit
84ffa62c045287810116009ea32700416618900e
Author: Fuad Tabba <[email protected]>
Subject: KVM: arm64: Add system register reset framework for protected VMsReset a protected VM's system registers at EL2 rather than taking the host's values: add
kvm_reset_pvm_sys_regs()and the pvm_sys_reg_reset_vals[] table that drives it, and call it frominit_pkvm_hyp_vcpu()for protected vCPUs. The values follow the host-side reset insys_regs.c, with a poison value where it resets to UNKNOWN, and forVBAR_EL1andCONTEXTIDR_EL1in place of the 0 it resets them to.MPIDR_EL1is derived fromvcpu_id, as for any KVM guest.Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +0
-0[ ... ]
Quoted 14 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c static const struct sys_reg_desc pvm_sys_reg_descs[] = { · line 543
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +10
-0+/* Hardware value, as sys_regs.c's reset_actlr()/reset_amair_el1(). */ +static void reset_actlr (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,read_sysreg (actlr_el1)); +} + +static void reset_amair_el1 (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,read_sysreg (amair_el1)); +}[Severity: High]
Does this allow the host to dictate the protected guest's initial register state?
I'll give you this one Sashiko... Both will reset to 0 in v2, and these two functions go with them. Mirroring KVM's reset was the wrong call here.
Quoted 16 lines
Since EL1 has write access to both the
actlr_el1andamair_el1hardware registers, the host could set them to arbitrary values before the vCPU creation hypercall is invoked. Seeding EL2-private state for a protected guest by reading host-modifiable hardware registers instead of using hardcoded architectural constants appears to breach the pKVM trust boundary.arch/arm64/kvm/hyp/nvhe/sys_regs.c static const struct sys_reg_desc pvm_sys_reg_descs[] = { · line 548
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/sys_regs.c +5
-0+ +static void reset_mpidr (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,kvm_calculate_mpidr (vcpu)); +}[Severity: High]
Can a malicious host supply duplicatevcpu_idvalues?
Quoted 5 lines
Earlier in
init_pkvm_hyp_vcpu(),hyp_vcpu->vcpu.vcpu_idis directly copied from the untrustedhost_vcpu. Sincekvm_calculate_mpidr()calculates the guest'sMPIDR_EL1value based on this unvalidatedvcpu_id, could the host use this to instantiate a protected VM with duplicateMPIDR_EL1values across its vCPUs?
... but not this one. KVM does not check for uniqueness either:kvm_init_mpidr_data() keeps the last vCPU written into an aliased slot.
Cheers,
/fuad
Quoted 3 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH 07/17] KVM: arm64: Add system register reset framework for protected VMs
In reply to Fuad TabbaQuoted 10 lines · Fuad Tabba
Reset a protected VM's system registers at EL2 rather than taking the host's values: add
kvm_reset_pvm_sys_regs()and the pvm_sys_reg_reset_vals[] table that drives it, and call it frominit_pkvm_hyp_vcpu()for protected vCPUs. The values follow the host-side reset insys_regs.c, with a poison value where it resets to UNKNOWN, and forVBAR_EL1andCONTEXTIDR_EL1in place of the 0 it resets them to.MPIDR_EL1is derived fromvcpu_id, as for any KVM guest.Signed-off-by Fuad Tabba <[email protected]>
The commit message says "rather than taking the host's values", but if I understand correctly, at this point in the series, they still take the hosts values because this is still present:
hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt;
Not necessarily saying anything needs to change, just to write down what I noticed while reviewing / reading this code!
The only other thing I was a bit unsure of was the actual list of registers pvm_sys_reg_reset_vals. It's the EL1 registers, minus unsupported FEATs (MTE, debug(?), GCS, etc)
Otherwise:
Thanks,
Joey
Quoted 152 lines
--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 1 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 5 ++ arch/arm64/kvm/hyp/nvhe/sys_regs.c | 91 ++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 4 deletions(-)arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1
-0@@ -95,6 +95,7 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code);bool kvm_handle_pvm_sysreg (struct kvm_vcpu *vcpu,u64 *exit_code);bool kvm_handle_pvm_restricted (struct kvm_vcpu *vcpu,u64 *exit_code);void kvm_init_pvm_id_regs (struct kvm_vcpu *vcpu); +void kvm_reset_pvm_sys_regs (struct kvm_vcpu *vcpu);int kvm_check_pvm_sysreg_table (void );#endif /* __ARM64_KVM_NVHE_PKVM_H__ */ arch/arm64/kvm/hyp/nvhe/pkvm.c +5
-0@@ -551,6 +551,11 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, goto done; ret =pkvm_vcpu_init_sve (hyp_vcpu, host_vcpu); +if (ret) + goto done; + +if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +kvm_reset_pvm_sys_regs (&hyp_vcpu->vcpu); done:if (ret)unpin_host_vcpu (host_vcpu);arch/arm64/kvm/hyp/nvhe/sys_regs.c +87
-4@@ -525,6 +525,84 @@ static const struct sys_reg_desc pvm_sys_reg_descs[] = {/* Performance Monitoring Registers are restricted. */ }; +struct sys_reg_desc_reset { +int reg; +void (*reset)(struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *rd); +u64 value; +}; + +/* Hardware value, as sys_regs.c's reset_actlr()/reset_amair_el1(). */ +static void reset_actlr (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,read_sysreg (actlr_el1)); +} + +static void reset_amair_el1 (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,read_sysreg (amair_el1)); +} + +static void reset_mpidr (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,kvm_calculate_mpidr (vcpu)); +} + +static void reset_value (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg, r->value); +} + +#define RESET_VAL (REG ,RESET_VAL ) { REG, reset_value, RESET_VAL } + +#define RESET_ZERO (REG ) RESET_VAL(REG, 0) + +#define RESET_UNKNOWN (REG ) RESET_VAL(REG, 0x1de7ec7edbadc0deULL) + +#define RESET_FUNC (REG ,RESET_FUNC ) { REG, RESET_FUNC, 0 } + +/* Sorted ascending by reg; kvm_check_pvm_sysreg_table() enforces it. */ +static const struct sys_reg_desc_reset pvm_sys_reg_reset_vals[] = { +RESET_FUNC (MPIDR_EL1 , reset_mpidr), +RESET_UNKNOWN (TPIDR_EL0 ), +RESET_UNKNOWN (TPIDRRO_EL0 ), +RESET_UNKNOWN (TPIDR_EL1 ), +RESET_ZERO (CNTKCTL_EL1 ), +RESET_UNKNOWN (PAR_EL1 ), +RESET_ZERO (MDCCINT_EL1 ), +RESET_ZERO (DISR_EL1 ), +RESET_ZERO (PMCCFILTR_EL0 ), +RESET_ZERO (PMUSERENR_EL0 ), +RESET_ZERO (CPACR_EL1 ), +RESET_VAL (CONTEXTIDR_EL1 ,0x00000000dbadc0deULL ), +RESET_VAL (SCTLR_EL1 ,0x00C50078ULL ), +RESET_FUNC (ACTLR_EL1 , reset_actlr), +RESET_ZERO (TCR_EL1 ), +RESET_UNKNOWN (AFSR0_EL1 ), +RESET_UNKNOWN (AFSR1_EL1 ), +RESET_UNKNOWN (ESR_EL1 ), +RESET_UNKNOWN (MAIR_EL1 ), +RESET_FUNC (AMAIR_EL1 , reset_amair_el1), +RESET_ZERO (MDSCR_EL1 ), +RESET_ZERO (ZCR_EL1 ), +RESET_UNKNOWN (TTBR0_EL1 ), +RESET_UNKNOWN (TTBR1_EL1 ), +RESET_UNKNOWN (FAR_EL1 ), +RESET_VAL (VBAR_EL1 ,0x1de7ec7edbadc000ULL ), +RESET_UNKNOWN (PIRE0_EL1 ), +RESET_UNKNOWN (PIR_EL1 ), +}; + +void kvm_reset_pvm_sys_regs (struct kvm_vcpu *vcpu) +{ +unsigned long i; + +for (i =0 ; i <ARRAY_SIZE (pvm_sys_reg_reset_vals); i++) { +const struct sys_reg_desc_reset *r = &pvm_sys_reg_reset_vals[i]; + + r->reset (vcpu, r); + } +} +/* * Initializes feature registers for protected vms. */ @@ -550,16 +628,21 @@ void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu) }/* -* Checks that the sysreg table is unique and in-order. -* -* Returns 0 if the table is consistent, or 1 otherwise. +* Both tables must be unique and sorted ascending. pvm_sys_reg_descs.reg is the +* sys_reg() encoding, pvm_sys_reg_reset_vals.reg the vcpu_sysreg index, so they +* compare differently. BUG_ON() at __pkvm_init: fatal at boot. */ int kvm_check_pvm_sysreg_table (void ) {unsigned int i;for (i =1 ; i <ARRAY_SIZE (pvm_sys_reg_descs); i++) { -if (cmp_sys_reg (&pvm_sys_reg_descs[i-1 ], &pvm_sys_reg_descs[i]) >=0 ) +if (cmp_sys_reg (&pvm_sys_reg_descs[i -1 ], &pvm_sys_reg_descs[i]) >=0 ) +return 1 ; + } + +for (i =1 ; i <ARRAY_SIZE (pvm_sys_reg_reset_vals); i++) { +if (pvm_sys_reg_reset_vals[i -1 ].reg >= pvm_sys_reg_reset_vals[i].reg)return 1 ; }Signature · 1 line
2.39.5
Fuad Tabba
reviewerRe: [PATCH 07/17] KVM: arm64: Add system register reset framework for protected VMs
Quoted 13 lines · Joey Gouly
Quoted 10 lines · Fuad Tabba
Reset a protected VM's system registers at EL2 rather than taking the host's values: add
kvm_reset_pvm_sys_regs()and the pvm_sys_reg_reset_vals[] table that drives it, and call it frominit_pkvm_hyp_vcpu()for protected vCPUs. The values follow the host-side reset insys_regs.c, with a poison value where it resets to UNKNOWN, and forVBAR_EL1andCONTEXTIDR_EL1in place of the 0 it resets them to.MPIDR_EL1is derived fromvcpu_id, as for any KVM guest.Signed-off-by Fuad Tabba <[email protected]>
Quoted 8 lines
The commit message says "rather than taking the host's values", but if I understand correctly, at this point in the series, they still take the hosts values because this is still present:
hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt;Not necessarily saying anything needs to change, just to write down what I noticed while reviewing / reading this code!
Actually, you're right. This should change. That copy runs on every entry and is removed only in patch 13, so until then the reset values are overwritten and this patch has no observable effect for a protected VM. The message will say so for v2. Patch 6 hits the same copy and works around it, preserving the timer registers across it.
Quoted 4 lines
The only other thing I was a bit unsure of was the actual list of registers
pvm_sys_reg_reset_vals. It's the EL1 registers, minus unsupported FEATs (MTE, debug(?), GCS, etc)
Not quite: some EL0 registers are in it, and debug isn't excluded. I'll state the rule in the commit message. And there's the Sashiko fix too.
Thanks!
/fuad
Quoted 159 lines
Otherwise:
Acked-by Joey Gouly <[email protected]>Thanks,
JoeyQuoted 152 lines
--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 1 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 5 ++ arch/arm64/kvm/hyp/nvhe/sys_regs.c | 91 ++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 4 deletions(-)arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1
-0@@ -95,6 +95,7 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code);bool kvm_handle_pvm_sysreg (struct kvm_vcpu *vcpu,u64 *exit_code);bool kvm_handle_pvm_restricted (struct kvm_vcpu *vcpu,u64 *exit_code);void kvm_init_pvm_id_regs (struct kvm_vcpu *vcpu); +void kvm_reset_pvm_sys_regs (struct kvm_vcpu *vcpu);int kvm_check_pvm_sysreg_table (void );#endif /* __ARM64_KVM_NVHE_PKVM_H__ */ arch/arm64/kvm/hyp/nvhe/pkvm.c +5
-0@@ -551,6 +551,11 @@ static int init_pkvm_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, goto done; ret =pkvm_vcpu_init_sve (hyp_vcpu, host_vcpu); +if (ret) + goto done; + +if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +kvm_reset_pvm_sys_regs (&hyp_vcpu->vcpu); done:if (ret)unpin_host_vcpu (host_vcpu);arch/arm64/kvm/hyp/nvhe/sys_regs.c +87
-5@@ -525,6 +525,84 @@ static const struct sys_reg_desc pvm_sys_reg_descs[] = {/* Performance Monitoring Registers are restricted. */ }; +struct sys_reg_desc_reset { +int reg; +void (*reset)(struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *rd); +u64 value; +}; + +/* Hardware value, as sys_regs.c's reset_actlr()/reset_amair_el1(). */ +static void reset_actlr (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,read_sysreg (actlr_el1)); +} + +static void reset_amair_el1 (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,read_sysreg (amair_el1)); +} + +static void reset_mpidr (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg,kvm_calculate_mpidr (vcpu)); +} + +static void reset_value (struct kvm_vcpu *vcpu,const struct sys_reg_desc_reset *r) +{ +__vcpu_assign_sys_reg (vcpu, r->reg, r->value); +} + +#define RESET_VAL (REG ,RESET_VAL ) { REG, reset_value, RESET_VAL } + +#define RESET_ZERO (REG ) RESET_VAL(REG, 0) + +#define RESET_UNKNOWN (REG ) RESET_VAL(REG, 0x1de7ec7edbadc0deULL) + +#define RESET_FUNC (REG ,RESET_FUNC ) { REG, RESET_FUNC, 0 } + +/* Sorted ascending by reg; kvm_check_pvm_sysreg_table() enforces it. */ +static const struct sys_reg_desc_reset pvm_sys_reg_reset_vals[] = { +RESET_FUNC (MPIDR_EL1 , reset_mpidr), +RESET_UNKNOWN (TPIDR_EL0 ), +RESET_UNKNOWN (TPIDRRO_EL0 ), +RESET_UNKNOWN (TPIDR_EL1 ), +RESET_ZERO (CNTKCTL_EL1 ), +RESET_UNKNOWN (PAR_EL1 ), +RESET_ZERO (MDCCINT_EL1 ), +RESET_ZERO (DISR_EL1 ), +RESET_ZERO (PMCCFILTR_EL0 ), +RESET_ZERO (PMUSERENR_EL0 ), +RESET_ZERO (CPACR_EL1 ), +RESET_VAL (CONTEXTIDR_EL1 ,0x00000000dbadc0deULL ), +RESET_VAL (SCTLR_EL1 ,0x00C50078ULL ), +RESET_FUNC (ACTLR_EL1 , reset_actlr), +RESET_ZERO (TCR_EL1 ), +RESET_UNKNOWN (AFSR0_EL1 ), +RESET_UNKNOWN (AFSR1_EL1 ), +RESET_UNKNOWN (ESR_EL1 ), +RESET_UNKNOWN (MAIR_EL1 ), +RESET_FUNC (AMAIR_EL1 , reset_amair_el1), +RESET_ZERO (MDSCR_EL1 ), +RESET_ZERO (ZCR_EL1 ), +RESET_UNKNOWN (TTBR0_EL1 ), +RESET_UNKNOWN (TTBR1_EL1 ), +RESET_UNKNOWN (FAR_EL1 ), +RESET_VAL (VBAR_EL1 ,0x1de7ec7edbadc000ULL ), +RESET_UNKNOWN (PIRE0_EL1 ), +RESET_UNKNOWN (PIR_EL1 ), +}; + +void kvm_reset_pvm_sys_regs (struct kvm_vcpu *vcpu) +{ +unsigned long i; + +for (i =0 ; i <ARRAY_SIZE (pvm_sys_reg_reset_vals); i++) { +const struct sys_reg_desc_reset *r = &pvm_sys_reg_reset_vals[i]; + + r->reset (vcpu, r); + } +} +/* * Initializes feature registers for protected vms. */ @@ -550,16 +628,21 @@ void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu) }/* -* Checks that the sysreg table is unique and in-order. -* -* Returns 0 if the table is consistent, or 1 otherwise. +* Both tables must be unique and sorted ascending. pvm_sys_reg_descs.reg is the +* sys_reg() encoding, pvm_sys_reg_reset_vals.reg the vcpu_sysreg index, so they +* compare differently. BUG_ON() at __pkvm_init: fatal at boot. */ int kvm_check_pvm_sysreg_table (void ) {unsigned int i;for (i =1 ; i <ARRAY_SIZE (pvm_sys_reg_descs); i++) { -if (cmp_sys_reg (&pvm_sys_reg_descs[i-1 ], &pvm_sys_reg_descs[i]) >=0 ) +if (cmp_sys_reg (&pvm_sys_reg_descs[i -1 ], &pvm_sys_reg_descs[i]) >=0 ) +return 1 ; + } + +for (i =1 ; i <ARRAY_SIZE (pvm_sys_reg_reset_vals); i++) { +if (pvm_sys_reg_reset_vals[i -1 ].reg >= pvm_sys_reg_reset_vals[i].reg)return 1 ; } --2.39.5
Fuad Tabba
reviewerPATCH 8/17 KVM: arm64: Implement HVC handling for protected guests at EL2
In reply to Fuad TabbaExtend kvm_handle_pvm_hvc64() to handle SMCCC_VERSION, SMCCC_ARCH_FEATURES and the vendor hypervisor call UID at EL2, so these queries do not reach the host. ARCH_FEATURES is mandatory from SMCCC 1.1, the version EL2 reports: it returns SUCCESS for itself and for SMCCC_VERSION, and NOT_SUPPORTED for anything else.
Add handle_pvm_entry_hvc64() and handle_pvm_exit_hvc64(), which forward a protected guest's HVCs to the host and return the reply; a later patch wires them into the per-EC dispatch tables.
--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 28 ++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +28 -0
@@ -4,6 +4,8 @@* Author: Andrew Scull <[email protected]> */ +#include <kvm/arm_hypercalls.h> +#include <hyp/adjust_pc.h> #include <hyp/switch.h>
@@ -33,6 +35,32 @@ void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt);typedef void (*hyp_entry_exit_handler_fn )(struct pkvm_hyp_vcpu *); +static void __maybe_unused handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +int i; + +for (i =0 ; i <4 ; i++) { +u64 ret = +READ_ONCE (hyp_vcpu->host_vcpu->arch.ctxt.regs.regs[i]); +vcpu_set_reg (&hyp_vcpu->vcpu, i, ret); + } +} + +static void __maybe_unused handle_pvm_exit_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; +int i; + +WRITE_ONCE (host_vcpu->arch.fault.esr_el2, + hyp_vcpu->vcpu.arch.fault.esr_el2); + +/* Pass the HVC function id (r0) and its arguments. */ +for (i =0 ; i <8 ; i++) { +WRITE_ONCE (host_vcpu->arch.ctxt.regs.regs[i], +vcpu_get_reg (&hyp_vcpu->vcpu, i)); + } +} +static void handle_vm_entry_generic (struct pkvm_hyp_vcpu *hyp_vcpu) {vcpu_copy_flag (&hyp_vcpu->vcpu, hyp_vcpu->host_vcpu,PC_UPDATE_REQ );
arch/arm64/kvm/hyp/nvhe/pkvm.c +29 -0
@@ -1144,8 +1144,37 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code) {u64 val[4 ] = {SMCCC_RET_INVALID_PARAMETER };bool handled = true; +u32 feature; +uuid_t uuid;switch (smccc_get_function (vcpu)) { +case ARM_SMCCC_VERSION_FUNC_ID : +/* Nothing to be handled by the host. Go back to the guest. */ + val[0 ] =ARM_SMCCC_VERSION_1_1 ; + val[1 ] =0 ; + val[2 ] =0 ; + val[3 ] =0 ; +break ; +case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : +/* SUCCESS only for the architecture calls EL2 implements. */ + feature =smccc_get_arg1 (vcpu); +switch (feature) { +case ARM_SMCCC_VERSION_FUNC_ID : +case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : + val[0 ] =SMCCC_RET_SUCCESS ; +break ; +default : + val[0 ] =SMCCC_RET_NOT_SUPPORTED ; +break ; + } +break ; +case ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID : + uuid =ARM_SMCCC_VENDOR_HYP_UID_KVM ; + val[0 ] =smccc_uuid_to_reg (&uuid,0 ); + val[1 ] =smccc_uuid_to_reg (&uuid,1 ); + val[2 ] =smccc_uuid_to_reg (&uuid,2 ); + val[3 ] =smccc_uuid_to_reg (&uuid,3 ); +break ;case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID : val[0 ] =BIT (ARM_SMCCC_KVM_FUNC_FEATURES ); val[0 ] |=BIT (ARM_SMCCC_KVM_FUNC_HYP_MEMINFO );
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH 08/17] KVM: arm64: Implement HVC handling for protected guests at EL2
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kvm_handle_pvm_hvc64 incorrectly intercepts ARM_SMCCC_ARCH_FEATURES_FUNC_ID and unconditionally…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm_handle_pvm_hvc64 incorrectly intercepts ARM_SMCCC_ARCH_FEATURES_FUNC_ID and unconditionally returns NOT_SUPPORTED for Spectre mitigations (ARCH_WORKAROUND_1, 2, 3), leaving protected guests vulnerable. --
commit 3a993faeabc602db9d44970bfb0c7813227c0324
Author: Fuad Tabba <[email protected]>
KVM: arm64: Implement HVC handling for protected guests at EL2
This commit extends kvm_handle_pvm_hvc64() to handle SMCCC_VERSION, SMCCC_ARCH_FEATURES, and the vendor hypervisor call UID at EL2. It also adds handlers to forward protected guest HVCs to the host.
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0
[ ... ]
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +2
-0@@ -1144,8 +1144,37 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code) {u64 val[4 ] = {SMCCC_RET_INVALID_PARAMETER };bool handled = true; +u32 feature; +uuid_t uuid;switch (smccc_get_function (vcpu)) {
[ ... ]
arch/arm64/kvm/hyp/nvhe/pkvm.c bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code) · line 1168
Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +11
-0+case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : +/* SUCCESS only for the architecture calls EL2 implements. */ + feature =smccc_get_arg1 (vcpu); +switch (feature) { +case ARM_SMCCC_VERSION_FUNC_ID : +case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : + val[0 ] =SMCCC_RET_SUCCESS ; +break ; +default : + val[0 ] =SMCCC_RET_NOT_SUPPORTED ; +break ;
[Severity: High]
Does this catch-all default case inadvertently disable Spectre mitigations in the protected guest kernel?
When the guest OS boots, it queries ARM_SMCCC_ARCH_FEATURES_FUNC_ID for ARM_SMCCC_ARCH_WORKAROUND_1 (and others) to check availability. Because this default case blindly returns SMCCC_RET_NOT_SUPPORTED, the guest will assume the workarounds are unavailable.
Could this allow an untrusted guest userspace process to exploit transient execution attacks (like V2, V4, BHB) and leak guest kernel or other guest userspace memory?
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +2
-0+ } +break ;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Fuad Tabba
reviewerRe: [PATCH 08/17] KVM: arm64: Implement HVC handling for protected guests at EL2
And you got me here too, Sashiko. 1/1
Quoted 44 lines · On Mon, 31 Aug 2026 at 22:00, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High]kvm_handle_pvm_hvc64incorrectly interceptsARM_SMCCC_ARCH_FEATURES_FUNC_IDand unconditionally returnsNOT_SUPPORTEDfor Spectre mitigations (ARCH_WORKAROUND_1, 2, 3), leaving protected guests vulnerable. --commit
3a993faeabc602db9d44970bfb0c7813227c0324
Author: Fuad Tabba <[email protected]>KVM: arm64: Implement HVC handling for protected guests at EL2
This commit extends
kvm_handle_pvm_hvc64()to handleSMCCC_VERSION,SMCCC_ARCH_FEATURES, and the vendor hypervisor call UID at EL2. It also adds handlers to forward protected guest HVCs to the host.Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0[ ... ]
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +2
-0@@ -1144,8 +1144,37 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code) {u64 val[4 ] = {SMCCC_RET_INVALID_PARAMETER };bool handled = true; +u32 feature; +uuid_t uuid;switch (smccc_get_function (vcpu)) {[ ... ]
arch/arm64/kvm/hyp/nvhe/pkvm.c bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code) · line 1168
Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +11
-0+case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : +/* SUCCESS only for the architecture calls EL2 implements. */ + feature =smccc_get_arg1 (vcpu); +switch (feature) { +case ARM_SMCCC_VERSION_FUNC_ID : +case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : + val[0 ] =SMCCC_RET_SUCCESS ; +break ; +default : + val[0 ] =SMCCC_RET_NOT_SUPPORTED ; +break ;
Quoted 3 lines
[Severity: High]
Does this catch-all default case inadvertently disable Spectre mitigations in the protected guest kernel?
It does, and it's a regression: the base punts ARCH_FEATURES to the host, which answers it from the spectre state. v2 will copy that state over and handle it at EL2.
Thank you,
/fuad
/fuad
Quoted 17 lines
When the guest OS boots, it queries
ARM_SMCCC_ARCH_FEATURES_FUNC_IDforARM_SMCCC_ARCH_WORKAROUND_1(and others) to check availability. Because this default case blindly returnsSMCCC_RET_NOT_SUPPORTED, the guest will assume the workarounds are unavailable.Could this allow an untrusted guest userspace process to exploit transient execution attacks (like V2, V4, BHB) and leak guest kernel or other guest userspace memory?
arch/arm64/kvm/hyp/nvhe/pkvm.c
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +2
-0+ } +break ;[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Joey Gouly
reviewerRe: [PATCH 08/17] KVM: arm64: Implement HVC handling for protected guests at EL2
In reply to Fuad TabbaQuoted 9 lines · Fuad Tabba
Extend
kvm_handle_pvm_hvc64()to handleSMCCC_VERSION,SMCCC_ARCH_FEATURESand the vendor hypervisor call UID at EL2, so these queries do not reach the host.ARCH_FEATURESis mandatory from SMCCC 1.1, the version EL2 reports: it returns SUCCESS for itself and forSMCCC_VERSION, andNOT_SUPPORTEDfor anything else.Add
handle_pvm_entry_hvc64()andhandle_pvm_exit_hvc64(), which forward a protected guest's HVCs to the host and return the reply; a later patch wires them into the per-EC dispatch tables.
These two functions are unused in this commit, and are modified in the next commit, while still being unused. Then handle_pvm_entry_hvc64() becomes a wrapper for handle_pvm_entry_psci() (which was the whole body of handle_pvm_entry_hvc64). It's quite hard to follow. If these functions are really unused and then modified, maybe it's just better to remove them from this commit?
Thanks,
Joey
Quoted 98 lines
Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 28 ++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+)arch/arm64/kvm/hyp/nvhe/hyp-main.c +28
-0@@ -4,6 +4,8 @@* Author: Andrew Scull <[email protected]> */ +#include <kvm/arm_hypercalls.h> +#include <hyp/adjust_pc.h> #include <hyp/switch.h> @@ -33,6 +35,32 @@ void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt);typedef void (*hyp_entry_exit_handler_fn )(struct pkvm_hyp_vcpu *); +static void __maybe_unused handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +int i; + +for (i =0 ; i <4 ; i++) { +u64 ret = +READ_ONCE (hyp_vcpu->host_vcpu->arch.ctxt.regs.regs[i]); +vcpu_set_reg (&hyp_vcpu->vcpu, i, ret); + } +} + +static void __maybe_unused handle_pvm_exit_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; +int i; + +WRITE_ONCE (host_vcpu->arch.fault.esr_el2, + hyp_vcpu->vcpu.arch.fault.esr_el2); + +/* Pass the HVC function id (r0) and its arguments. */ +for (i =0 ; i <8 ; i++) { +WRITE_ONCE (host_vcpu->arch.ctxt.regs.regs[i], +vcpu_get_reg (&hyp_vcpu->vcpu, i)); + } +} +static void handle_vm_entry_generic (struct pkvm_hyp_vcpu *hyp_vcpu) {vcpu_copy_flag (&hyp_vcpu->vcpu, hyp_vcpu->host_vcpu,PC_UPDATE_REQ );arch/arm64/kvm/hyp/nvhe/pkvm.c +29
-0@@ -1144,8 +1144,37 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code) {u64 val[4 ] = {SMCCC_RET_INVALID_PARAMETER };bool handled = true; +u32 feature; +uuid_t uuid;switch (smccc_get_function (vcpu)) { +case ARM_SMCCC_VERSION_FUNC_ID : +/* Nothing to be handled by the host. Go back to the guest. */ + val[0 ] =ARM_SMCCC_VERSION_1_1 ; + val[1 ] =0 ; + val[2 ] =0 ; + val[3 ] =0 ; +break ; +case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : +/* SUCCESS only for the architecture calls EL2 implements. */ + feature =smccc_get_arg1 (vcpu); +switch (feature) { +case ARM_SMCCC_VERSION_FUNC_ID : +case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : + val[0 ] =SMCCC_RET_SUCCESS ; +break ; +default : + val[0 ] =SMCCC_RET_NOT_SUPPORTED ; +break ; + } +break ; +case ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID : + uuid =ARM_SMCCC_VENDOR_HYP_UID_KVM ; + val[0 ] =smccc_uuid_to_reg (&uuid,0 ); + val[1 ] =smccc_uuid_to_reg (&uuid,1 ); + val[2 ] =smccc_uuid_to_reg (&uuid,2 ); + val[3 ] =smccc_uuid_to_reg (&uuid,3 ); +break ;case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID : val[0 ] =BIT (ARM_SMCCC_KVM_FUNC_FEATURES ); val[0 ] |=BIT (ARM_SMCCC_KVM_FUNC_HYP_MEMINFO );Signature · 1 line
2.39.5
Fuad Tabba
reviewerRe: [PATCH 08/17] KVM: arm64: Implement HVC handling for protected guests at EL2
Quoted 12 lines · Joey Gouly
Quoted 9 lines · Fuad Tabba
Extend
kvm_handle_pvm_hvc64()to handleSMCCC_VERSION,SMCCC_ARCH_FEATURESand the vendor hypervisor call UID at EL2, so these queries do not reach the host.ARCH_FEATURESis mandatory from SMCCC 1.1, the version EL2 reports: it returns SUCCESS for itself and forSMCCC_VERSION, andNOT_SUPPORTEDfor anything else.Add
handle_pvm_entry_hvc64()andhandle_pvm_exit_hvc64(), which forward a protected guest's HVCs to the host and return the reply; a later patch wires them into the per-EC dispatch tables.
Quoted 6 lines
These two functions are unused in this commit, and are modified in the next commit, while still being unused. Then
handle_pvm_entry_hvc64()becomes a wrapper forhandle_pvm_entry_psci()(which was the whole body ofhandle_pvm_entry_hvc64). It's quite hard to follow. If these functions are really unused and then modified, maybe it's just better to remove them from this commit?
Both move to the patch that first calls them, so each appears once, in its final form, beside its caller.
The wrapper should go with it: handle_pvm_entry_hvc64() only calls handle_pvm_entry_psci(), and PSCI calls are the only HVCs forwarded to the host, so the split has no non-PSCI case to handle.
Thanks for the reviews!
/fuad
Quoted 102 lines
Thanks,
JoeyQuoted 98 lines
Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 28 ++++++++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+)arch/arm64/kvm/hyp/nvhe/hyp-main.c +28
-0@@ -4,6 +4,8 @@* Author: Andrew Scull <[email protected]> */ +#include <kvm/arm_hypercalls.h> +#include <hyp/adjust_pc.h> #include <hyp/switch.h> @@ -33,6 +35,32 @@ void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt);typedef void (*hyp_entry_exit_handler_fn )(struct pkvm_hyp_vcpu *); +static void __maybe_unused handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +int i; + +for (i =0 ; i <4 ; i++) { +u64 ret = +READ_ONCE (hyp_vcpu->host_vcpu->arch.ctxt.regs.regs[i]); +vcpu_set_reg (&hyp_vcpu->vcpu, i, ret); + } +} + +static void __maybe_unused handle_pvm_exit_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; +int i; + +WRITE_ONCE (host_vcpu->arch.fault.esr_el2, + hyp_vcpu->vcpu.arch.fault.esr_el2); + +/* Pass the HVC function id (r0) and its arguments. */ +for (i =0 ; i <8 ; i++) { +WRITE_ONCE (host_vcpu->arch.ctxt.regs.regs[i], +vcpu_get_reg (&hyp_vcpu->vcpu, i)); + } +} +static void handle_vm_entry_generic (struct pkvm_hyp_vcpu *hyp_vcpu) {vcpu_copy_flag (&hyp_vcpu->vcpu, hyp_vcpu->host_vcpu,PC_UPDATE_REQ );arch/arm64/kvm/hyp/nvhe/pkvm.c +29
-1@@ -1144,8 +1144,37 @@ bool kvm_handle_pvm_hvc64(struct kvm_vcpu *vcpu, u64 *exit_code) {u64 val[4 ] = {SMCCC_RET_INVALID_PARAMETER };bool handled = true; +u32 feature; +uuid_t uuid;switch (smccc_get_function (vcpu)) { +case ARM_SMCCC_VERSION_FUNC_ID : +/* Nothing to be handled by the host. Go back to the guest. */ + val[0 ] =ARM_SMCCC_VERSION_1_1 ; + val[1 ] =0 ; + val[2 ] =0 ; + val[3 ] =0 ; +break ; +case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : +/* SUCCESS only for the architecture calls EL2 implements. */ + feature =smccc_get_arg1 (vcpu); +switch (feature) { +case ARM_SMCCC_VERSION_FUNC_ID : +case ARM_SMCCC_ARCH_FEATURES_FUNC_ID : + val[0 ] =SMCCC_RET_SUCCESS ; +break ; +default : + val[0 ] =SMCCC_RET_NOT_SUPPORTED ; +break ; + } +break ; +case ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID : + uuid =ARM_SMCCC_VENDOR_HYP_UID_KVM ; + val[0 ] =smccc_uuid_to_reg (&uuid,0 ); + val[1 ] =smccc_uuid_to_reg (&uuid,1 ); + val[2 ] =smccc_uuid_to_reg (&uuid,2 ); + val[3 ] =smccc_uuid_to_reg (&uuid,3 ); +break ;case ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID : val[0 ] =BIT (ARM_SMCCC_KVM_FUNC_FEATURES ); val[0 ] |=BIT (ARM_SMCCC_KVM_FUNC_HYP_MEMINFO ); --2.39.5
Fuad Tabba
reviewerPATCH 9/17 KVM: arm64: Handle PSCI calls for protected VMs at EL2
In reply to Fuad TabbaEL2 implements PSCI 1.1 for protected VMs: CPU_ON, CPU_OFF, AFFINITY_INFO, PSCI_VERSION and PSCI_FEATURES are decided at EL2 (CPU_ON and CPU_OFF still exit to the host, which only schedules or parks the target), CPU_SUSPEND and the platform power operations are forwarded to the host, and anything else returns NOT_SUPPORTED.
Three consequences follow:
- A protected VM has one primary vCPU, the first whose hyp vCPU is created with mp_state RUNNABLE. A second one, or an mp_state other than RUNNABLE or STOPPED, fails that vCPU's first KVM_RUN with -EINVAL.
- CPU_ON finds its target among the hyp vCPUs, which exist from the target's first KVM_RUN; before that the guest gets INVALID_PARAMETERS.
- A vCPU EL2 holds powered off does not run: handle___kvm_vcpu_run() returns ARM_EXCEPTION_IL, reported as KVM_EXIT_FAIL_ENTRY. Its existing bail-outs return the same code instead of an -EINVAL that handle_exit() did not recognise, for every hyp vCPU.
Non-protected VMs keep power_state ON and accept any mp_state.
Each protected vCPU is OFF, ON_PENDING or ON. CPU_ON moves the target to ON_PENDING, the target's next run resets it and moves it to ON, and a CPU_ON that fails in the host rolls it back to OFF. The racing transitions are cmpxchg, and the reset state is published with a release/acquire pair, documented at each site.
--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 14 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 125 +++++++-- arch/arm64/kvm/hyp/nvhe/pkvm.c | 348 ++++++++++++++++++++++++- 3 files changed, 465 insertions(+), 22 deletions(-)
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +14 -0
@@ -29,6 +29,12 @@ struct pkvm_hyp_vcpu {/* The previous exit's ARM_EXCEPTION_* code. */ u32 exit_code; + +/* +* PSCI_0_2_AFFINITY_LEVEL_{OFF, ON_PENDING, ON}. A non-protected +* vCPU is always ON. +*/ +int power_state; };/*
@@ -46,6 +52,12 @@ struct pkvm_hyp_vm {struct hyp_pool pool;hyp_spinlock_t lock; +/* +* The vCPU initialised RUNNABLE: claimed under vm_table_lock, +* released only if its own init fails. +*/ +struct pkvm_hyp_vcpu *primary_vcpu; +/* Array of the hyp vCPU structures for this VM. */ struct pkvm_hyp_vcpu *vcpus[]; };
@@ -98,4 +110,6 @@ void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu);void kvm_reset_pvm_sys_regs (struct kvm_vcpu *vcpu);int kvm_check_pvm_sysreg_table (void ); +int pkvm_reset_vcpu (struct pkvm_hyp_vcpu *hyp_vcpu); +struct pkvm_hyp_vcpu *pkvm_mpidr_to_hyp_vcpu (struct pkvm_hyp_vm *vm,u64 mpidr);#endif /* __ARM64_KVM_NVHE_PKVM_H__ */
arch/arm64/kvm/hyp/nvhe/hyp-main.c +108 -17
169 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/pkvm.c +343 -5
420 lines not on this page · raw message
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH 09/17] KVM: arm64: Handle PSCI calls for protected VMs at EL2
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Malicious host can hijack guest execution flow via CPU_ON TOCTOU race caused by stale…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Malicious host can hijack guest execution flow via CPU_ON TOCTOU race caused by stale reset_state.reset flag. --
commit f4181bbdd19d9d3775a4f479d2c7c70dc5c753b8
Author: Fuad Tabba <[email protected]>
KVM: arm64: Handle PSCI calls for protected VMs at EL2
This patch implements PSCI 1.1 for protected VMs in EL2, handling power state transitions (CPU_ON, CPU_OFF, AFFINITY_INFO) within the hypervisor, while keeping non-protected VMs unchanged. It defines protected vCPU power states and synchronizes racing transitions via cmpxchg and release/acquire barriers.
Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0
[ ... ]
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0@@ -37,27 +40,103 @@ typedef void (*hyp_entry_exit_handler_fn)(struct pkvm_hyp_vcpu *);static void __maybe_unused handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) {
[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c typedef void (*hyp_entry_exit_handler_fn)(struct pkvm_hyp_vcpu *); · line 84
Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +13
-0+ prev =cmpxchg_relaxed (&target_vcpu->power_state, +PSCI_0_2_AFFINITY_LEVEL_ON_PENDING , +PSCI_0_2_AFFINITY_LEVEL_OFF ); +switch (prev) { +case PSCI_0_2_AFFINITY_LEVEL_ON_PENDING : +/* +* Leave reset_state.reset set: clearing it +* races a concurrent CPU_ON's re-publish and +* wedges the target at ON_PENDING. The stale +* pc/r0/be are the guest's own. +*/ + ret =PSCI_RET_INTERNAL_FAILURE ; +break ;
[Severity: High]
Does this rollback logic allow a malicious host to hijack guest execution flow by exploiting a TOCTOU race?
If guest vCPU A calls CPU_ON for target T with PC1, the hypervisor sets power_state = ON_PENDING. If the host then rejects the call, this rollback logic resets power_state to OFF but intentionally leaves reset_state.reset = true and pc = PC1.
If guest vCPU B then calls CPU_ON for target T with PC2, pvm_psci_vcpu_on() sets power_state = ON_PENDING. Before the hypervisor writes PC2, a malicious host could concurrently issue KVM_RUN for target T on another CPU.
Because the stale reset flag is still true from the previous failure, target T will immediately boot using the stale PC1 instead of PC2, redirecting the guest control flow.
Should this rollback clear the reset state, or is there another way to prevent the host from forcing a boot with a stale PC?
[ ... ]
Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0
[ ... ]
arch/arm64/kvm/hyp/nvhe/pkvm.c static void pkvm_memunshare_call(u64 *ret, struct kvm_vcpu *vcpu) · line 1204
Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +13
-0+int pkvm_reset_vcpu (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct vcpu_reset_state *reset_state = &hyp_vcpu->vcpu.arch.reset_state; +int prev; + +/* +* Pairs with smp_store_release(&reset_state->reset, true) in +* pvm_psci_vcpu_on(). The acquire must precede the cmpxchg: reversed, a +* winning cmpxchg with a false acquire would leave power_state == ON +* with the reset skipped. +*/ +if (!smp_load_acquire (&reset_state->reset)) +return -ECANCELED ;
[Severity: High]
Can the host force pkvm_reset_vcpu() to succeed prematurely here?
Since the rollback in handle_pvm_entry_hvc64() leaves reset_state.reset set to true, if a new CPU_ON sets power_state to ON_PENDING, a host-controlled KVM_RUN can enter this function and pass this smp_load_acquire() check before the new PC is actually written by pvm_psci_vcpu_on().
[ ... ]
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +2
-0+static bool pvm_psci_vcpu_on (struct pkvm_hyp_vcpu *hyp_vcpu) +{
[ ... ]
arch/arm64/kvm/hyp/nvhe/pkvm.c static void pkvm_memunshare_call(u64 *ret, struct kvm_vcpu *vcpu) · line 1282
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +7
-0+/* +* vCPUs race to power on the same target. Relaxed: reset_state +* is published by the release on reset_state.reset below. +*/ + power_state =cmpxchg_relaxed (&target->power_state, +PSCI_0_2_AFFINITY_LEVEL_OFF , +PSCI_0_2_AFFINITY_LEVEL_ON_PENDING );
[Severity: High]
Does setting power_state to ON_PENDING here open a TOCTOU window?
If the target already has a stale reset_state.reset == true from a previously failed CPU_ON attempt, making the target ON_PENDING here allows the host to schedule the target and boot it before the new PC/r0/be states are updated below.
Quoted 1 line
arch/arm64/kvm/hyp/nvhe/pkvm.c +1
-0+switch (power_state) {
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Fuad Tabba
reviewerRe: [PATCH 09/17] KVM: arm64: Handle PSCI calls for protected VMs at EL2
Nor this one...
Quoted 51 lines · On Mon, 31 Aug 2026 at 22:15, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Malicious host can hijack guest execution flow viaCPU_ONTOCTOU race caused by stale reset_state.reset flag. --commit
f4181bbdd19d9d3775a4f479d2c7c70dc5c753b8
Author: Fuad Tabba <[email protected]>KVM: arm64: Handle PSCI calls for protected VMs at EL2
This patch implements PSCI 1.1 for protected VMs in EL2, handling power state transitions (
CPU_ON,CPU_OFF,AFFINITY_INFO) within the hypervisor, while keeping non-protected VMs unchanged. It defines protected vCPU power states and synchronizes racing transitions via cmpxchg and release/acquire barriers.Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0[ ... ]
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0@@ -37,27 +40,103 @@ typedef void (*hyp_entry_exit_handler_fn)(struct pkvm_hyp_vcpu *);static void __maybe_unused handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) {[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c typedef void (*hyp_entry_exit_handler_fn)(struct pkvm_hyp_vcpu *); · line 84
Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +13
-0+ prev =cmpxchg_relaxed (&target_vcpu->power_state, +PSCI_0_2_AFFINITY_LEVEL_ON_PENDING , +PSCI_0_2_AFFINITY_LEVEL_OFF ); +switch (prev) { +case PSCI_0_2_AFFINITY_LEVEL_ON_PENDING : +/* +* Leave reset_state.reset set: clearing it +* races a concurrent CPU_ON's re-publish and +* wedges the target at ON_PENDING. The stale +* pc/r0/be are the guest's own. +*/ + ret =PSCI_RET_INTERNAL_FAILURE ; +break ;[Severity: High]
Does this rollback logic allow a malicious host to hijack guest execution flow by exploiting a TOCTOU race?If guest vCPU A calls
CPU_ONfor target T with PC1, the hypervisor setspower_state=ON_PENDING. If the host then rejects the call, this rollback logic resetspower_stateto OFF but intentionally leaves reset_state.reset = true and pc = PC1.If guest vCPU B then calls
CPU_ONfor target T with PC2,pvm_psci_vcpu_on()setspower_state=ON_PENDING. Before the hypervisor writes PC2, a malicious host could concurrently issueKVM_RUNfor target T on another CPU.
Quoted 6 lines
Because the stale reset flag is still true from the previous failure, target T will immediately boot using the stale PC1 instead of PC2, redirecting the guest control flow.
Should this rollback clear the reset state, or is there another way to prevent the host from forcing a boot with a stale PC?
The stale pc/r0/be are from an earlier CPU_ON the guest itself issued, so the host can only pick between addresses the guest wrote. Clearing the flag races a concurrent CPU_ON's re-publish and wedges the target at ON_PENDING, which is why it stays set.
/fuad
/fuad
Quoted 53 lines
[ ... ]
Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0[ ... ]
arch/arm64/kvm/hyp/nvhe/pkvm.c static void pkvm_memunshare_call(u64 *ret, struct kvm_vcpu *vcpu) · line 1204
Quoted 13 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +13
-0+int pkvm_reset_vcpu (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct vcpu_reset_state *reset_state = &hyp_vcpu->vcpu.arch.reset_state; +int prev; + +/* +* Pairs with smp_store_release(&reset_state->reset, true) in +* pvm_psci_vcpu_on(). The acquire must precede the cmpxchg: reversed, a +* winning cmpxchg with a false acquire would leave power_state == ON +* with the reset skipped. +*/ +if (!smp_load_acquire (&reset_state->reset)) +return -ECANCELED ;[Severity: High]
Can the host forcepkvm_reset_vcpu()to succeed prematurely here?Since the rollback in
handle_pvm_entry_hvc64()leaves reset_state.reset set to true, if a newCPU_ONsetspower_statetoON_PENDING, a host-controlledKVM_RUNcan enter this function and pass thissmp_load_acquire()check before the new PC is actually written bypvm_psci_vcpu_on().[ ... ]
Quoted 2 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +2
-0+static bool pvm_psci_vcpu_on (struct pkvm_hyp_vcpu *hyp_vcpu) +{[ ... ]
arch/arm64/kvm/hyp/nvhe/pkvm.c static void pkvm_memunshare_call(u64 *ret, struct kvm_vcpu *vcpu) · line 1282
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +7
-0+/* +* vCPUs race to power on the same target. Relaxed: reset_state +* is published by the release on reset_state.reset below. +*/ + power_state =cmpxchg_relaxed (&target->power_state, +PSCI_0_2_AFFINITY_LEVEL_OFF , +PSCI_0_2_AFFINITY_LEVEL_ON_PENDING );[Severity: High]
Does settingpower_statetoON_PENDINGhere open a TOCTOU window?If the target already has a stale reset_state.reset == true from a previously failed
CPU_ONattempt, making the targetON_PENDINGhere allows the host to schedule the target and boot it before the new PC/r0/be states are updated below.Quoted 1 line
+ switch (
power_state) {[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Fuad Tabba
reviewerPATCH 10/17 KVM: arm64: Restrict KVM_ARM_VCPU_INIT and PSCI version for protected VMs
In reply to Fuad TabbaEL2 masks a protected VM's vCPU features down to the set pKVM allows, so a KVM_ARM_VCPU_INIT that requests more succeeds and produces a guest without them. Reject such an init with -EINVAL. The host and EL2 both take the allowed set from kvm_pkvm_vcpu_allowed_features().
EL2 returns 1.1 for a protected guest's PSCI_VERSION whatever the host is configured for, and forwards the platform calls to the host. Require KVM_ARM_VCPU_PSCI_0_2: without it the host dispatches the forwarded calls as PSCI 0.1. Reject a KVM_REG_ARM_PSCI_VERSION write below 1.1: below it the host rejects the SYSTEM_RESET2 that EL2 advertises. KVM_CAP_ARM_PSCI is no longer advertised to protected VMs.
--- arch/arm64/include/asm/kvm_pkvm.h | 25 ++++++++++++++++++++++++- arch/arm64/kvm/arm.c | 13 +++++++++++++ arch/arm64/kvm/hyp/nvhe/pkvm.c | 17 ++--------------- arch/arm64/kvm/hypercalls.c | 7 +++++++ 4 files changed, 46 insertions(+), 16 deletions(-)
arch/arm64/include/asm/kvm_pkvm.h +24 -1
@@ -33,7 +33,6 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) {switch (ext) {case KVM_CAP_IRQCHIP : -case KVM_CAP_ARM_PSCI :case KVM_CAP_ARM_PSCI_0_2 :case KVM_CAP_NR_VCPUS :case KVM_CAP_MAX_VCPUS :
@@ -62,6 +61,30 @@ static inline bool kvm_pkvm_ext_allowed(struct kvm *kvm, long ext) } } +/* +* The vCPU features a protected VM may use: checked by the host at +* KVM_ARM_VCPU_INIT, applied by EL2 when the hyp VM is created. +*/ +static inline void kvm_pkvm_vcpu_allowed_features (struct kvm *kvm, +unsigned long *allowed) +{ +bitmap_zero (allowed,KVM_VCPU_MAX_FEATURES ); + +set_bit (KVM_ARM_VCPU_PSCI_0_2 , allowed); + +if (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_PMU_V3 )) +set_bit (KVM_ARM_VCPU_PMU_V3 , allowed); + +if (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_PTRAUTH_ADDRESS )) +set_bit (KVM_ARM_VCPU_PTRAUTH_ADDRESS , allowed); + +if (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_PTRAUTH_GENERIC )) +set_bit (KVM_ARM_VCPU_PTRAUTH_GENERIC , allowed); + +if (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_SVE )) +set_bit (KVM_ARM_VCPU_SVE , allowed); +} +/* * Check whether the KVM VM IOCTL is allowed in pKVM. *
arch/arm64/kvm/arm.c +13 -0
@@ -1620,6 +1620,19 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu,if (features & ~system_supported_vcpu_features ())return -EINVAL ; +/* Reject features EL2 would drop when it creates the hyp VM. */ +if (kvm_vm_is_protected (vcpu->kvm)) { +DECLARE_BITMAP (allowed,KVM_VCPU_MAX_FEATURES ); + +kvm_pkvm_vcpu_allowed_features (vcpu->kvm, allowed); +if (!bitmap_subset (&features, allowed,KVM_VCPU_MAX_FEATURES )) +return -EINVAL ; + +/* EL2 implements PSCI 1.1; the host must not dispatch as 0.1. */ +if (!test_bit (KVM_ARM_VCPU_PSCI_0_2 , &features)) +return -EINVAL ; + } +/* * For now make sure that both address/generic pointer authentication * features are requested by the userspace together.
arch/arm64/kvm/hyp/nvhe/pkvm.c +2 -15
@@ -367,23 +367,10 @@ static void pkvm_init_features_from_host(struct pkvm_hyp_vm *hyp_vm, const strucif (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_MTE )) kvm->arch.flags |= host_arch_flags &BIT (KVM_ARCH_FLAG_MTE_ENABLED ); -bitmap_zero (allowed_features,KVM_VCPU_MAX_FEATURES ); +kvm_pkvm_vcpu_allowed_features (kvm, allowed_features); -set_bit (KVM_ARM_VCPU_PSCI_0_2 , allowed_features); - -if (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_PMU_V3 )) -set_bit (KVM_ARM_VCPU_PMU_V3 , allowed_features); - -if (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_PTRAUTH_ADDRESS )) -set_bit (KVM_ARM_VCPU_PTRAUTH_ADDRESS , allowed_features); - -if (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_PTRAUTH_GENERIC )) -set_bit (KVM_ARM_VCPU_PTRAUTH_GENERIC , allowed_features); - -if (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_SVE )) { -set_bit (KVM_ARM_VCPU_SVE , allowed_features); +if (kvm_pkvm_ext_allowed (kvm,KVM_CAP_ARM_SVE )) kvm->arch.flags |= host_arch_flags &BIT (KVM_ARCH_FLAG_GUEST_HAS_SVE ); - }bitmap_and (kvm->arch.vcpu_features, host_kvm->arch.vcpu_features, allowed_features,KVM_VCPU_MAX_FEATURES );
arch/arm64/kvm/hypercalls.c +7 -0
@@ -580,6 +580,13 @@ int kvm_arm_set_fw_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) wants_02 =vcpu_has_feature (vcpu,KVM_ARM_VCPU_PSCI_0_2 ); +/* +* EL2 advertises PSCI 1.1; the host handles the forwarded calls +* by this version. +*/ +if (kvm_vm_is_protected (vcpu->kvm) && val <KVM_ARM_PSCI_1_1 ) +return -EINVAL ; +switch (val) {case KVM_ARM_PSCI_0_1 :if (wants_02)
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 11/17 KVM: arm64: Prevent host PC adjustments for protected vCPUs
In reply to Fuad Tabba__kvm_adjust_pc() lets the host advance a vCPU's PC or inject an exception, which for a protected vCPU would let the host redirect guest execution. Drop the request there: the entry handlers apply the host's PC_UPDATE_REQ on re-entry, where EL2 allows it.
For a non-protected vCPU, adjusting the hyp vCPU while PKVM_HOST_STATE_DIRTY is set loses the update at the next flush and the guest re-executes an emulated MMIO access, so the flag selects which copy to adjust. Adjusting the hyp vCPU copies PC_UPDATE_REQ in and back out again. Without the copy back, INCREMENT_PC outlives the adjustment and the next KVM_SET_VCPU_EVENTS trips WARN_ON(INCREMENT_PC) in kvm_pend_exception().
Unloaded, the host copy is host-writable, so pin it and its VM before adjusting. Only commit_pending_events(), under KVM_SET_VCPU_EVENTS, reaches EL2 with no hyp vCPU loaded. KVM_RUN's call always has one. enter_exception64() reads the VM's MTE flag and a host copy's vcpu->kvm is host-written, so __kvm_adjust_pc_vm() takes the VM as a
parameter: the pinned host struct kvm when unloaded, the hyp VM otherwise.
--- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/kvm/hyp/exception.c | 27 ++++++++++------- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 47 +++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_asm.h +1 -0
@@ -280,6 +280,7 @@ extern int __kvm_at_s12(struct kvm_vcpu *vcpu, u32 op, u64 vaddr);extern int __kvm_vcpu_run (struct kvm_vcpu *vcpu);extern void __kvm_adjust_pc (struct kvm_vcpu *vcpu); +extern void __kvm_adjust_pc_vm (struct kvm_vcpu *vcpu,struct kvm *kvm);extern bool __vgic_v3_get_gic_config (void );extern void __vgic_v3_init_lrs (void );
arch/arm64/kvm/hyp/exception.c +16 -11
@@ -66,8 +66,8 @@ static void __vcpu_write_spsr_und(struct kvm_vcpu *vcpu, u64 val)* Here we manipulate the fields in order of the AArch64 SPSR_ELx layout, from * MSB to LSB. */ -static void enter_exception64 (struct kvm_vcpu *vcpu,unsigned long target_mode, -enum exception_type type) +static void enter_exception64 (struct kvm_vcpu *vcpu,struct kvm *kvm, +unsigned long target_mode,enum exception_type type) {unsigned long sctlr, vbar, old, new, mode;u64 exc_offset;
@@ -109,7 +109,7 @@ static void enter_exception64(struct kvm_vcpu *vcpu, unsigned long target_mode, new |= (old &PSR_C_BIT ); new |= (old &PSR_V_BIT ); -if (kvm_has_mte (kern_hyp_va (vcpu->kvm))) +if (kvm_has_mte (kvm)) new |=PSR_TCO_BIT ; new |= (old &PSR_DIT_BIT );
@@ -294,7 +294,7 @@ static void enter_exception32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset) *vcpu_pc (vcpu) = vect_offset; } -static void kvm_inject_exception (struct kvm_vcpu *vcpu) +static void kvm_inject_exception (struct kvm_vcpu *vcpu,struct kvm *kvm) {if (vcpu_el1_is_32bit (vcpu)) {switch (vcpu_get_flag (vcpu,EXCEPT_MASK )) {
@@ -314,23 +314,23 @@ static void kvm_inject_exception(struct kvm_vcpu *vcpu) }else {switch (vcpu_get_flag (vcpu,EXCEPT_MASK )) {case unpack_vcpu_flag (EXCEPT_AA64_EL1_SYNC ): -enter_exception64 (vcpu, PSR_MODE_EL1h, except_type_sync); +enter_exception64 (vcpu, kvm, PSR_MODE_EL1h, except_type_sync);break ;case unpack_vcpu_flag (EXCEPT_AA64_EL1_SERR ): -enter_exception64 (vcpu, PSR_MODE_EL1h, except_type_serror); +enter_exception64 (vcpu, kvm, PSR_MODE_EL1h, except_type_serror);break ;case unpack_vcpu_flag (EXCEPT_AA64_EL2_SYNC ): -enter_exception64 (vcpu, PSR_MODE_EL2h, except_type_sync); +enter_exception64 (vcpu, kvm, PSR_MODE_EL2h, except_type_sync);break ;case unpack_vcpu_flag (EXCEPT_AA64_EL2_IRQ ): -enter_exception64 (vcpu, PSR_MODE_EL2h, except_type_irq); +enter_exception64 (vcpu, kvm, PSR_MODE_EL2h, except_type_irq);break ;case unpack_vcpu_flag (EXCEPT_AA64_EL2_SERR ): -enter_exception64 (vcpu, PSR_MODE_EL2h, except_type_serror); +enter_exception64 (vcpu, kvm, PSR_MODE_EL2h, except_type_serror);break ;default :
@@ -348,10 +348,10 @@ static void kvm_inject_exception(struct kvm_vcpu *vcpu)* Adjust the guest PC (and potentially exception state) depending on * flags provided by the emulation code. */ -void __kvm_adjust_pc (struct kvm_vcpu *vcpu) +void __kvm_adjust_pc_vm (struct kvm_vcpu *vcpu,struct kvm *kvm) {if (vcpu_get_flag (vcpu,PENDING_EXCEPTION )) { -kvm_inject_exception (vcpu); +kvm_inject_exception (vcpu, kvm);vcpu_clear_flag (vcpu,PENDING_EXCEPTION );vcpu_clear_flag (vcpu,EXCEPT_MASK ); }else if (vcpu_get_flag (vcpu,INCREMENT_PC )) {
@@ -359,3 +359,8 @@ void __kvm_adjust_pc(struct kvm_vcpu *vcpu)vcpu_clear_flag (vcpu,INCREMENT_PC ); } } + +void __kvm_adjust_pc (struct kvm_vcpu *vcpu) +{ +__kvm_adjust_pc_vm (vcpu,kern_hyp_va (vcpu->kvm)); +}
arch/arm64/kvm/hyp/nvhe/hyp-main.c +46 -1
57 lines not on this page · raw message
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 12/17 KVM: arm64: Inject an UNDEF at EL2 for unhandled protected guest exits
In reply to Fuad TabbaMake pvm_exit_handlers[] an allow-list: a protected guest's exit class is handled at EL2, forwarded where the host emulates it, or results in an UNDEF at EL2. Any other class, once forwarded, would re-execute forever. The host resolves such an exit by injecting an exception or incrementing the PC. The marshalling that follows allows neither: a protected vCPU takes only the UNDEF on a SYS64 trap and the external abort on a forwarded IABT or DABT that EL2 builds for it.
WFxT takes the UNDEF, as it is not offered to protected guests. FP/SIMD keeps the lazy switch, and the watchpoint entry is dropped: a protected vCPU runs with TDE clear.
--- arch/arm64/kvm/hyp/nvhe/switch.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-)
arch/arm64/kvm/hyp/nvhe/switch.c +25 -4
@@ -203,15 +203,36 @@ static const exit_handler_fn hyp_exit_handlers[] = { [ESR_ELx_EC_MOPS] = kvm_hyp_handle_mops, }; +/* WFI/WFE exit to the host, which emulates them. WFxT is not offered. */ +static bool kvm_handle_pvm_wfx (struct kvm_vcpu *vcpu,u64 *exit_code) +{ +if (kvm_vcpu_get_esr (vcpu) & ESR_ELx_WFx_ISS_WFxT) +return kvm_handle_pvm_restricted (vcpu, exit_code); + +return false; +} + +/* Lazy FP/SIMD switch, or the UNDEF the host would otherwise be asked for. */ +static bool kvm_handle_pvm_fpsimd (struct kvm_vcpu *vcpu,u64 *exit_code) +{ +if (kvm_hyp_handle_fpsimd (vcpu, exit_code)) +return true; + +return kvm_handle_pvm_restricted (vcpu, exit_code); +} + +/* +* A class not listed takes an UNDEF at EL2: the host has no way to +* inject one into a protected vCPU. +*/ static const exit_handler_fn pvm_exit_handlers[] = { - [0 ... ESR_ELx_EC_MAX] =NULL , + [0 ... ESR_ELx_EC_MAX] = kvm_handle_pvm_restricted, + [ESR_ELx_EC_WFx] = kvm_handle_pvm_wfx, [ESR_ELx_EC_HVC64] = kvm_handle_pvm_hvc64, [ESR_ELx_EC_SYS64] = kvm_handle_pvm_sys64, - [ESR_ELx_EC_SVE] = kvm_handle_pvm_restricted, - [ESR_ELx_EC_FP_ASIMD] = kvm_hyp_handle_fpsimd, + [ESR_ELx_EC_FP_ASIMD] = kvm_handle_pvm_fpsimd, [ESR_ELx_EC_IABT_LOW] = kvm_hyp_handle_iabt_low, [ESR_ELx_EC_DABT_LOW] = kvm_hyp_handle_dabt_low, - [ESR_ELx_EC_WATCHPT_LOW] = kvm_hyp_handle_watchpt_low, [ESR_ELx_EC_MOPS] = kvm_hyp_handle_mops, };
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 13/17 KVM: arm64: Add per-EC entry/exit state marshalling for protected guests
In reply to Fuad TabbaMove a protected guest's state between the hyp vCPU and the host per exception class instead of copying the whole context. Add entry_hyp_pvm_handlers[] and exit_hyp_pvm_handlers[] for WFx, SYS64, IABT, DABT and HVC64, and route protected guests through them: on exit each handler copies out only what its class needs, and on re-entry only what the host may have changed, once the host has completed the access (INCREMENT_PC). entry_hyp_vm_handlers[] is removed: a non-protected vCPU's iflags are copied wholesale.
The host's fault view is EL2's own syndrome with the guest register index withheld, plus the addresses each class needs. The value of a written register is passed in r0. MMIO data is clamped to the access width and, for a load, sign-extended at EL2 from EL2's syndrome. Endianness stays with the host.
Neither dispatch runs for a trap taken with an SError pending: EL2 does not handle it, and the guest replays it once the host has injected the SError. The exit handlers would otherwise marshal a trap EL2 never handled, and handle_pvm_exit_hvc64() would panic on an unfiltered function id.
Wire in the HVC64 entry and exit handlers added earlier in the series.
--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 319 ++++++++++++++++++++++++++--- 1 file changed, 290 insertions(+), 29 deletions(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +290 -29
@@ -38,7 +38,7 @@ void __kvm_hyp_host_forward_smc(struct kvm_cpu_context *host_ctxt);typedef void (*hyp_entry_exit_handler_fn )(struct pkvm_hyp_vcpu *); -static void __maybe_unused handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +static void handle_pvm_entry_psci (struct pkvm_hyp_vcpu *hyp_vcpu) {u64 ret =READ_ONCE (hyp_vcpu->host_vcpu->arch.ctxt.regs.regs[0 ]);u32 psci_fn =smccc_get_function (&hyp_vcpu->vcpu);
@@ -101,7 +101,12 @@ static void __maybe_unused handle_pvm_entry_hvc64(struct pkvm_hyp_vcpu *hyp_vcpuvcpu_set_reg (&hyp_vcpu->vcpu,0 , ret); } -static void __maybe_unused handle_pvm_exit_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +static void handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +handle_pvm_entry_psci (hyp_vcpu); +} + +static void handle_pvm_exit_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) {struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu;int n, i;
@@ -131,8 +136,6 @@ static void __maybe_unused handle_pvm_exit_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu)hyp_panic (); } - host_vcpu->arch.fault.esr_el2 = hyp_vcpu->vcpu.arch.fault.esr_el2; -/* Pass the HVC function id (r0) and its arguments. */ for (i =0 ; i < n; i++) { host_vcpu->arch.ctxt.regs.regs[i] =
@@ -140,13 +143,242 @@ static void __maybe_unused handle_pvm_exit_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu) } } -static void handle_vm_entry_generic (struct pkvm_hyp_vcpu *hyp_vcpu) +static void handle_pvm_entry_wfx (struct pkvm_hyp_vcpu *hyp_vcpu) { -vcpu_copy_flag (&hyp_vcpu->vcpu, hyp_vcpu->host_vcpu,PC_UPDATE_REQ ); +if (vcpu_get_flag (hyp_vcpu->host_vcpu,INCREMENT_PC )) { +vcpu_clear_flag (&hyp_vcpu->vcpu,PC_UPDATE_REQ ); +kvm_incr_pc (&hyp_vcpu->vcpu); + } } -static const hyp_entry_exit_handler_fn entry_hyp_vm_handlers[] = { - [0 ... ESR_ELx_EC_MAX] = handle_vm_entry_generic, +static void handle_pvm_entry_sys64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; +bool pc_update; + +/* Exceptions have priority over anything else */ +if (vcpu_get_flag (host_vcpu,PENDING_EXCEPTION )) { +/* A host-requested exception on SYS64 is always an UNDEF. */ +u32 esr = (ESR_ELx_EC_UNKNOWN << ESR_ELx_EC_SHIFT) | ESR_ELx_IL; + +__vcpu_assign_sys_reg (&hyp_vcpu->vcpu,ESR_EL1 , esr); +kvm_pend_exception (&hyp_vcpu->vcpu,EXCEPT_AA64_EL1_SYNC ); +return ; + } + +/* Handle PC increment on a host-emulated access */ + pc_update =vcpu_get_flag (host_vcpu,INCREMENT_PC ); +if (pc_update) { +vcpu_clear_flag (&hyp_vcpu->vcpu,PC_UPDATE_REQ ); +kvm_incr_pc (&hyp_vcpu->vcpu); + } + +/* If the host emulated a read access, update the register */ +if (pc_update && + !esr_sys64_to_params (hyp_vcpu->vcpu.arch.fault.esr_el2).is_write) { +/* r0 as transfer register between the guest and the host. */ +u64 rt_val =READ_ONCE (host_vcpu->arch.ctxt.regs.regs[0 ]); +int rt =kvm_vcpu_sys_get_rt (&hyp_vcpu->vcpu); + +vcpu_set_reg (&hyp_vcpu->vcpu, rt, rt_val); + } +} + +static void handle_pvm_entry_iabt (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +unsigned long cpsr = *vcpu_cpsr (&hyp_vcpu->vcpu); +u32 esr = ESR_ELx_IL; + +if (!vcpu_get_flag (hyp_vcpu->host_vcpu,PENDING_EXCEPTION )) +return ; + +/* The host's only IABT injection: an external abort. */ +if ((cpsr &PSR_MODE_MASK ) == PSR_MODE_EL0t) + esr |= (ESR_ELx_EC_IABT_LOW << ESR_ELx_EC_SHIFT); +else + esr |= (ESR_ELx_EC_IABT_CUR << ESR_ELx_EC_SHIFT); + + esr |= ESR_ELx_FSC_EXTABT; + +__vcpu_assign_sys_reg (&hyp_vcpu->vcpu,ESR_EL1 , esr); +__vcpu_assign_sys_reg (&hyp_vcpu->vcpu,FAR_EL1 , +kvm_vcpu_get_hfar (&hyp_vcpu->vcpu)); + +/* Injected by __kvm_adjust_pc() on entry. */ +kvm_pend_exception (&hyp_vcpu->vcpu,EXCEPT_AA64_EL1_SYNC ); +} + +/* +* Clamp MMIO data to the access width, so a write does not leak the +* register's upper bits and a read takes no bits beyond the load. The +* host applies endianness. +*/ +static inline u64 kvm_mmio_clamp_data (struct kvm_vcpu *vcpu,u64 val) +{ +unsigned int len =kvm_vcpu_dabt_get_as (vcpu); + +return val &GENMASK_U64 (len *8 -1 ,0 ); +} + +/* +* Complete an MMIO load: sign-extend from EL2's own syndrome, as the +* architecture does. +*/ +static inline u64 kvm_mmio_read_data (struct kvm_vcpu *vcpu,u64 val) +{ + val =kvm_mmio_clamp_data (vcpu, val); + +if (kvm_vcpu_dabt_issext (vcpu)) + val =sign_extend64 (val,kvm_vcpu_dabt_get_as (vcpu) *8 -1 ); + +if (!kvm_vcpu_dabt_issf (vcpu)) + val &=GENMASK_U64 (31 ,0 ); + +return val; +} + +static void handle_pvm_entry_dabt (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; +bool pc_update; + +/* Exceptions have priority over anything else */ +if (vcpu_get_flag (host_vcpu,PENDING_EXCEPTION )) { +unsigned long cpsr = *vcpu_cpsr (&hyp_vcpu->vcpu); +u32 esr = ESR_ELx_IL; + +if ((cpsr &PSR_MODE_MASK ) == PSR_MODE_EL0t) + esr |= (ESR_ELx_EC_DABT_LOW << ESR_ELx_EC_SHIFT); +else + esr |= (ESR_ELx_EC_DABT_CUR << ESR_ELx_EC_SHIFT); + + esr |= ESR_ELx_FSC_EXTABT; + +__vcpu_assign_sys_reg (&hyp_vcpu->vcpu,ESR_EL1 , esr); +__vcpu_assign_sys_reg (&hyp_vcpu->vcpu,FAR_EL1 , +kvm_vcpu_get_hfar (&hyp_vcpu->vcpu)); + +/* Injected by __kvm_adjust_pc() on entry. */ +kvm_pend_exception (&hyp_vcpu->vcpu,EXCEPT_AA64_EL1_SYNC ); + +/* Cancel any in-flight MMIO */ + hyp_vcpu->vcpu.mmio_needed = false; +return ; + } + +/* Handle PC increment on MMIO */ + pc_update = (hyp_vcpu->vcpu.mmio_needed && +vcpu_get_flag (host_vcpu,INCREMENT_PC )); +if (pc_update) { +vcpu_clear_flag (&hyp_vcpu->vcpu,PC_UPDATE_REQ ); +kvm_incr_pc (&hyp_vcpu->vcpu); + } + +/* If the host emulated an MMIO read, update the register */ +if (pc_update && !kvm_vcpu_dabt_iswrite (&hyp_vcpu->vcpu)) { +/* r0 as transfer register between the guest and the host. */ +u64 rd_val =READ_ONCE (host_vcpu->arch.ctxt.regs.regs[0 ]); +int rd =kvm_vcpu_dabt_get_rd (&hyp_vcpu->vcpu); + + rd_val =kvm_mmio_read_data (&hyp_vcpu->vcpu, rd_val); +vcpu_set_reg (&hyp_vcpu->vcpu, rd, rd_val); + } + + hyp_vcpu->vcpu.mmio_needed = false; +} + +/* The host's view of a syndrome: the guest register index is withheld. */ +static u64 pvm_host_esr (u64 esr) +{ +switch (ESR_ELx_EC (esr)) { +case ESR_ELx_EC_WFx: +return esr & ~ESR_ELx_WFx_ISS_RN; +case ESR_ELx_EC_SYS64: +return esr & ~ESR_ELx_SYS64_ISS_RT_MASK; +case ESR_ELx_EC_DABT_LOW: +return esr & ~ESR_ELx_SRT_MASK; +default : +return esr; + } +} + +static void handle_pvm_exit_wfx (struct pkvm_hyp_vcpu *hyp_vcpu) +{ + hyp_vcpu->host_vcpu->arch.ctxt.regs.pstate = + hyp_vcpu->vcpu.arch.ctxt.regs.pstate &PSR_MODE_MASK ; +} + +static void handle_pvm_exit_sys64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; +u32 esr_el2 = hyp_vcpu->vcpu.arch.fault.esr_el2; + +/* The mode is required for the host to emulate some sysregs */ + host_vcpu->arch.ctxt.regs.pstate = + hyp_vcpu->vcpu.arch.ctxt.regs.pstate &PSR_MODE_MASK ; + +/* r0 as transfer register between the guest and the host. */ +if (esr_sys64_to_params (esr_el2).is_write) { +int rt =kvm_vcpu_sys_get_rt (&hyp_vcpu->vcpu); +u64 rt_val =vcpu_get_reg (&hyp_vcpu->vcpu, rt); + + host_vcpu->arch.ctxt.regs.regs[0 ] = rt_val; + } +} + +static void handle_pvm_exit_iabt (struct pkvm_hyp_vcpu *hyp_vcpu) +{ + hyp_vcpu->host_vcpu->arch.fault.hpfar_el2 = + hyp_vcpu->vcpu.arch.fault.hpfar_el2; +} + +static void handle_pvm_exit_dabt (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; + +/* +* EL2 has no memslot view: a decodable data abort is prepared as MMIO +* for the host to resolve. One with ISV clear (LDP/STP, atomics) on +* unbacked memory gets an SEA from the host; EL2 does not decode it. +*/ + hyp_vcpu->vcpu.mmio_needed =kvm_vcpu_dabt_isvalid (&hyp_vcpu->vcpu); + +/* r0 as transfer register between the guest and the host. */ +if (hyp_vcpu->vcpu.mmio_needed && +kvm_vcpu_dabt_iswrite (&hyp_vcpu->vcpu)) { +int rt =kvm_vcpu_dabt_get_rd (&hyp_vcpu->vcpu); +u64 rt_val =vcpu_get_reg (&hyp_vcpu->vcpu, rt); + + rt_val =kvm_mmio_clamp_data (&hyp_vcpu->vcpu, rt_val); + host_vcpu->arch.ctxt.regs.regs[0 ] = rt_val; + } + + host_vcpu->arch.ctxt.regs.pstate = + hyp_vcpu->vcpu.arch.ctxt.regs.pstate &PSR_MODE_MASK ; + host_vcpu->arch.fault.far_el2 = + hyp_vcpu->vcpu.arch.fault.far_el2 &GENMASK (11 ,0 ); + host_vcpu->arch.fault.hpfar_el2 = hyp_vcpu->vcpu.arch.fault.hpfar_el2; +__vcpu_assign_sys_reg (host_vcpu,SCTLR_EL1 , +__vcpu_sys_reg (&hyp_vcpu->vcpu,SCTLR_EL1 ) & + (SCTLR_ELx_EE |SCTLR_EL1_E0E )); +} + +static const hyp_entry_exit_handler_fn entry_hyp_pvm_handlers[] = { + [0 ... ESR_ELx_EC_MAX] =NULL , + [ESR_ELx_EC_WFx] = handle_pvm_entry_wfx, + [ESR_ELx_EC_SYS64] = handle_pvm_entry_sys64, + [ESR_ELx_EC_IABT_LOW] = handle_pvm_entry_iabt, + [ESR_ELx_EC_DABT_LOW] = handle_pvm_entry_dabt, + [ESR_ELx_EC_HVC64] = handle_pvm_entry_hvc64, +}; + +static const hyp_entry_exit_handler_fn exit_hyp_pvm_handlers[] = { + [0 ... ESR_ELx_EC_MAX] =NULL , + [ESR_ELx_EC_WFx] = handle_pvm_exit_wfx, + [ESR_ELx_EC_SYS64] = handle_pvm_exit_sys64, + [ESR_ELx_EC_IABT_LOW] = handle_pvm_exit_iabt, + [ESR_ELx_EC_DABT_LOW] = handle_pvm_exit_dabt, + [ESR_ELx_EC_HVC64] = handle_pvm_exit_hvc64, };static void __hyp_sve_save_guest (struct kvm_vcpu *vcpu)
@@ -382,18 +614,6 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2; hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags;flush_debug_state (hyp_vcpu); - }else { -u64 v_cval = hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTV_CVAL_EL0 ]; -u64 v_ctl = hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTV_CTL_EL0 ]; -u64 p_cval = hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTP_CVAL_EL0 ]; -u64 p_ctl = hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTP_CTL_EL0 ]; - - hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; - - hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTV_CVAL_EL0 ] = v_cval; - hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTV_CTL_EL0 ] = v_ctl; - hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTP_CVAL_EL0 ] = p_cval; - hyp_vcpu->vcpu.arch.ctxt.sys_regs[CNTP_CTL_EL0 ] = p_ctl; }/* __hyp_running_vcpu must be NULL in a guest context. */
@@ -418,10 +638,16 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)case ARM_EXCEPTION_IL :break ;case ARM_EXCEPTION_TRAP : - esr_ec =ESR_ELx_EC (kvm_vcpu_get_esr (&hyp_vcpu->vcpu)); - ec_handler = entry_hyp_vm_handlers[esr_ec]; -if (ec_handler) -ec_handler (hyp_vcpu); +/* Nothing was marshalled for this trap, see sync_hyp_vcpu(). */ +if (ARM_SERROR_PENDING (hyp_vcpu->exit_code)) +break ; + +if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) { + esr_ec =ESR_ELx_EC (kvm_vcpu_get_esr (&hyp_vcpu->vcpu)); + ec_handler = entry_hyp_pvm_handlers[esr_ec]; +if (ec_handler) +ec_handler (hyp_vcpu); + }break ;default :BUG ();
@@ -433,14 +659,30 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)static void sync_hyp_vcpu (struct pkvm_hyp_vcpu *hyp_vcpu,u32 exit_reason) {struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; +hyp_entry_exit_handler_fn ec_handler; +u8 esr_ec;fpsimd_sve_sync (&hyp_vcpu->vcpu);if (!pkvm_hyp_vcpu_is_protected (hyp_vcpu))sync_debug_state (hyp_vcpu); +sync_hyp_vgic_state (hyp_vcpu); +sync_hyp_timer_state (hyp_vcpu); +if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) { - host_vcpu->arch.ctxt = hyp_vcpu->vcpu.arch.ctxt; +/* +* Protected: the host sees ESR_EL2 as EL2 took it, register +* index withheld; the fault addresses stay withheld unless the +* EC handler below adds them. +*/ + host_vcpu->arch.fault = (struct kvm_vcpu_fault_info ) { + .esr_el2 =pvm_host_esr (hyp_vcpu->vcpu.arch.fault.esr_el2), + .disr_el1 = hyp_vcpu->vcpu.arch.fault.disr_el1, + }; }else { +/* Non-protected: the host gets the full fault. */ + host_vcpu->arch.fault = hyp_vcpu->vcpu.arch.fault; + host_vcpu->arch.iflags = hyp_vcpu->vcpu.arch.iflags;/* * PC feeds trace_kvm_exit(), PSTATE.SS the host software-step * machine, and both run before the next on-demand ctxt sync.
@@ -449,16 +691,35 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason) host_vcpu->arch.ctxt.regs.pstate = hyp_vcpu->vcpu.arch.ctxt.regs.pstate; } - host_vcpu->arch.fault = hyp_vcpu->vcpu.arch.fault; +switch (ARM_EXCEPTION_CODE (exit_reason)) { +case ARM_EXCEPTION_IRQ : +break ; +case ARM_EXCEPTION_TRAP : +/* SError pending: not handled at EL2, the guest replays it. */ +if (ARM_SERROR_PENDING (exit_reason)) +break ; - host_vcpu->arch.iflags = hyp_vcpu->vcpu.arch.iflags; +/* Per-EC marshalling is for protected guests only. */ +if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) { + esr_ec =ESR_ELx_EC (kvm_vcpu_get_esr (&hyp_vcpu->vcpu)); + ec_handler = exit_hyp_pvm_handlers[esr_ec]; +if (ec_handler) +ec_handler (hyp_vcpu); + } +break ; +case ARM_EXCEPTION_EL1_SERROR : +case ARM_EXCEPTION_IL : +break ; +default : +BUG (); + }/* Cleared by hardware once the guest takes the vSError. */ host_vcpu->arch.hcr_el2 &= ~HCR_VSE ; host_vcpu->arch.hcr_el2 |= hyp_vcpu->vcpu.arch.hcr_el2 &HCR_VSE ; -sync_hyp_vgic_state (hyp_vcpu); -sync_hyp_timer_state (hyp_vcpu); +if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) +vcpu_clear_flag (host_vcpu,PC_UPDATE_REQ ); hyp_vcpu->exit_code = exit_reason; }
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH 13/17] KVM: arm64: Add per-EC entry/exit state marshalling for protected guests
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The HVC64 entry handler ignores host-requested PC increments and exception injections, causing an…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The HVC64 entry handler ignores host-requested PC increments and exception injections, causing an infinite trap loop.
- [High] Host-injected exceptions and PC advancements are silently ignored for unhandled Exception Classes, causing infinite trap loops.
- [High] handle_pvm_exit_wfx fails to marshal the WFx timeout register to x0, breaking WFET and WFIT emulation.
--
commit 0b0e245ce37c95597a108d7e1ed3684401ade81b
Author: Fuad Tabba <[email protected]>
Subject: KVM: arm64: Add per-EC entry/exit state marshalling for protected guests
This patch moves a protected guest's state between the hyp vCPU and the host per exception class instead of copying the whole context. It adds per-EC entry and exit handlers for various traps and routes protected guests through them to marshal only the necessary state.
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0
[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void __maybe_unused handle_pvm_entry_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu · line 107
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4
-1-static void __maybe_unused handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +static void handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +handle_pvm_entry_psci (hyp_vcpu); +}
[Severity: High]
Does this code omit handling for host-requested PC increments and exception injections?
If the guest executes an HVC instruction, which traps to EL2, and the host handles the call (e.g., standard PSCI fallback) by setting the INCREMENT_PC or PENDING_EXCEPTION flags, it appears handle_pvm_entry_hvc64() doesn't apply them on re-entry. As a result, the guest's PC wouldn't advance and it could infinitely retry the same HVC.
[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void __maybe_unused handle_pvm_exit_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu) · line 309
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +5
-0+static void handle_pvm_exit_wfx (struct pkvm_hyp_vcpu *hyp_vcpu) +{ + hyp_vcpu->host_vcpu->arch.ctxt.regs.pstate = + hyp_vcpu->vcpu.arch.ctxt.regs.pstate &PSR_MODE_MASK ; +}
[Severity: High]
Can this cause the host to read uninitialized or stale data for WFET and WFIT timeout values?
When a protected guest traps to EL2 on a WFET/WFIT, pvm_host_esr() deliberately masks out the RN field (the register index holding the timeout) from the host's view of the ESR. Because of this, the host's kvm_handle_wfx() expects the timeout register value to be marshalled into regs[0]. It looks like handle_pvm_exit_wfx() doesn't copy the guest's RN register to host_vcpu->arch.ctxt.regs.regs[0] before exiting to the host.
[ ... ]
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +1
-4@@ -418,10 +638,16 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)case ARM_EXCEPTION_IL :break ;case ARM_EXCEPTION_TRAP : - esr_ec =ESR_ELx_EC (kvm_vcpu_get_esr (&hyp_vcpu->vcpu)); - ec_handler = entry_hyp_vm_handlers[esr_ec]; -if (ec_handler) -ec_handler (hyp_vcpu); +/* Nothing was marshalled for this trap, see sync_hyp_vcpu(). */
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) · line 651
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +9
-0+if (ARM_SERROR_PENDING (hyp_vcpu->exit_code)) +break ; + +if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) { + esr_ec =ESR_ELx_EC (kvm_vcpu_get_esr (&hyp_vcpu->vcpu)); + ec_handler = entry_hyp_pvm_handlers[esr_ec]; +if (ec_handler) +ec_handler (hyp_vcpu); + }break ;
[Severity: High]
Does this logic silently ignore host-injected exceptions and PC advancements for unhandled Exception Classes?
If a protected guest executes an unhandled instruction (like SMC64), it traps to EL2. The host cannot emulate the unknown EC and pends an exception via the PENDING_EXCEPTION flag. Because there is no specific entry handler for this EC in entry_hyp_pvm_handlers, ec_handler is NULL and the re-entry skips entry handling.
Since the generic fallback was removed, the PENDING_EXCEPTION flag is ignored, the PC is not advanced, and the guest could hang trying to retry the failing instruction infinitely.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Fuad Tabba
reviewerRe: [PATCH 13/17] KVM: arm64: Add per-EC entry/exit state marshalling for protected guests
And 0/3 here...
Quoted 33 lines · On Mon, 31 Aug 2026 at 23:15, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The HVC64 entry handler ignores host-requested PC increments and exception injections, causing an infinite trap loop.
- [High] Host-injected exceptions and PC advancements are silently ignored for unhandled Exception Classes, causing infinite trap loops.
- [High]handle_pvm_exit_wfxfails to marshal the WFx timeout register to x0, breaking WFET and WFIT emulation.
--commit
0b0e245ce37c95597a108d7e1ed3684401ade81b
Author: Fuad Tabba <[email protected]>
Subject: KVM: arm64: Add per-EC entry/exit state marshalling for protected guestsThis patch moves a protected guest's state between the hyp vCPU and the host per exception class instead of copying the whole context. It adds per-EC entry and exit handlers for various traps and routes protected guests through them to marshal only the necessary state.
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0
-0[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void __maybe_unused handle_pvm_entry_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu · line 107
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4
-1-static void __maybe_unused handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +static void handle_pvm_entry_hvc64 (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +handle_pvm_entry_psci (hyp_vcpu); +}[Severity: High]
Does this code omit handling for host-requested PC increments and exception injections?
Quoted 5 lines
If the guest executes an HVC instruction, which traps to EL2, and the host handles the call (e.g., standard PSCI fallback) by setting the
INCREMENT_PCorPENDING_EXCEPTIONflags, it appearshandle_pvm_entry_hvc64()doesn't apply them on re-entry. As a result, the guest's PC wouldn't advance and it could infinitely retry the same HVC.
An executed HVC has already advanced ELR_EL2, and handle_hvc() does not call kvm_incr_pc(). Nor is an exception pending:kvm_smccc_call_handler() only sets the return registers.
Quoted 13 lines
[ ... ]
arch/arm64/kvm/hyp/nvhe/hyp-main.c static void __maybe_unused handle_pvm_exit_hvc64(struct pkvm_hyp_vcpu *hyp_vcpu) · line 309
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +5
-0+static void handle_pvm_exit_wfx (struct pkvm_hyp_vcpu *hyp_vcpu) +{ + hyp_vcpu->host_vcpu->arch.ctxt.regs.pstate = + hyp_vcpu->vcpu.arch.ctxt.regs.pstate &PSR_MODE_MASK ; +}[Severity: High]
Can this cause the host to read uninitialized or stale data for WFET and WFIT timeout values?
Quoted 6 lines
When a protected guest traps to EL2 on a WFET/WFIT,
pvm_host_esr()deliberately masks out the RN field (the register index holding the timeout) from the host's view of the ESR. Because of this, the host'skvm_handle_wfx()expects the timeout register value to be marshalled into regs[0]. It looks likehandle_pvm_exit_wfx()doesn't copy the guest's RN register to host_vcpu->arch.ctxt.regs.regs[0] before exiting to the host.
A protected guest's WFIT/WFET takes an UNDEF at EL2:kvm_handle_pvm_wfx() routes ESR_ELx_WFx_ISS_WFxT to kvm_handle_pvm_restricted(). It never reaches the host.
Quoted 33 lines
[ ... ]
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +1
-4@@ -418,10 +638,16 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)case ARM_EXCEPTION_IL :break ;case ARM_EXCEPTION_TRAP : - esr_ec =ESR_ELx_EC (kvm_vcpu_get_esr (&hyp_vcpu->vcpu)); - ec_handler = entry_hyp_vm_handlers[esr_ec]; -if (ec_handler) -ec_handler (hyp_vcpu); +/* Nothing was marshalled for this trap, see sync_hyp_vcpu(). */ arch/arm64/kvm/hyp/nvhe/hyp-main.c static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) · line 651
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +9
-0+if (ARM_SERROR_PENDING (hyp_vcpu->exit_code)) +break ; + +if (pkvm_hyp_vcpu_is_protected (hyp_vcpu)) { + esr_ec =ESR_ELx_EC (kvm_vcpu_get_esr (&hyp_vcpu->vcpu)); + ec_handler = entry_hyp_pvm_handlers[esr_ec]; +if (ec_handler) +ec_handler (hyp_vcpu); + }break ;[Severity: High]
Does this logic silently ignore host-injected exceptions and PC advancements for unhandled Exception Classes?If a protected guest executes an unhandled instruction (like SMC64), it traps to EL2. The host cannot emulate the unknown EC and pends an exception via the
PENDING_EXCEPTIONflag. Because there is no specific entry handler for this EC inentry_hyp_pvm_handlers,ec_handleris NULL and the re-entry skips entry handling.
Quoted 3 lines
Since the generic fallback was removed, the
PENDING_EXCEPTIONflag is ignored, the PC is not advanced, and the guest could hang trying to retry the failing instruction infinitely.
Nor does an unhandled EC. pvm_exit_handlers[] defaults to kvm_handle_pvm_restricted() for every class, SMC64 included, so EL2 injects the UNDEF and returns to the guest.
Thanks for the reviews!
/fuad
Quoted 5 lines
[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Fuad Tabba
reviewerPATCH 14/17 KVM: arm64: Pend a protected guest's SError with HCR_EL2.VSE only
In reply to Fuad Tabbakvm_inject_serror_esr() emulates the exception entry when serror_is_masked() reads PSTATE.A clear, but the host has no view of a protected guest's PSTATE.A, and an emulated entry never reaches the
guest: the SError is lost. Set VSE instead and let the guest's own PSTATE.A mask it.
--- arch/arm64/kvm/inject_fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/arm64/kvm/inject_fault.c +4 -1
@@ -378,8 +378,11 @@ int kvm_inject_serror_esr(struct kvm_vcpu *vcpu, u64 esr)* * As we're emulating the SError injection we need to explicitly populate * ESR_ELx.EC because hardware will not do it on our behalf. +* +* The host does not see a protected guest's PSTATE.A: leave the +* vSError to HCR_EL2.VSE below, which the guest masks itself. */ -if (!serror_is_masked (vcpu)) { +if (!vcpu_is_protected (vcpu) && !serror_is_masked (vcpu)) {pend_serror_exception (vcpu); esr |=FIELD_PREP (ESR_ELx_EC_MASK, ESR_ELx_EC_SERROR) | ESR_ELx_IL;vcpu_write_sys_reg (vcpu, esr,exception_esr_elx (vcpu));
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 15/17 KVM: arm64: Reject host access to protected VM private state
In reply to Fuad TabbaA protected vCPU's register and debug state is no longer exposed to the host. Host ioctls that would reach that state now fail rather than operate on a copy that is not the guest's:
- KVM_GET_ONE_REG and KVM_SET_ONE_REG return -EPERM once the vCPU has
run: the copy then holds reset values plus what the exit handlers marshal out. Pre-run access still builds the guest's boot state.
- KVM_ARM_VCPU_INIT returns -EPERM once the vCPU has run: it would reset the host copy alone and rewrite mp_state, which EL2 reads only at hyp vCPU creation, so a vCPU the guest powered off would come back RUNNABLE.
- KVM_SET_VCPU_EVENTS rejects external-abort injection with -EPERM; SError injection is forwarded and stays permitted.
- KVM_SET_GUEST_DEBUG returns -EPERM: a protected guest's debug state is hypervisor-owned.
--- arch/arm64/kvm/arm.c | 7 +++++++ arch/arm64/kvm/guest.c | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+)
arch/arm64/kvm/arm.c +7 -0
@@ -1750,6 +1750,13 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,bool power_off = false;int ret; +/* +* Re-init would reset the host copy alone and rewrite mp_state +* after EL2 has read it. Pre-run init stays allowed. +*/ +if (kvm_vm_is_protected (vcpu->kvm) &&vcpu_has_run_once (vcpu)) +return -EPERM ; +/* * Treat the power-off vCPU feature as ephemeral. Clear the bit to avoid * reflecting it in the finalized feature set, thus limiting its scope
arch/arm64/kvm/guest.c +19 -0
@@ -701,6 +701,10 @@ int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)int kvm_arm_get_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) { +/* Once the vCPU has run, the host copy is not the guest's state. */ +if (kvm_vm_is_protected (vcpu->kvm) &&vcpu_has_run_once (vcpu)) +return -EPERM ; +/* We currently use nothing arch-specific in upper 32 bits */ if ((reg->id & ~KVM_REG_SIZE_MASK ) >>32 !=KVM_REG_ARM64 >>32 )return -EINVAL ;
@@ -718,6 +722,10 @@ int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)int kvm_arm_set_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg) { +/* Writes build the boot state; once run, EL2 owns the registers. */ +if (kvm_vm_is_protected (vcpu->kvm) &&vcpu_has_run_once (vcpu)) +return -EPERM ; +/* We currently use nothing arch-specific in upper 32 bits */ if ((reg->id & ~KVM_REG_SIZE_MASK ) >>32 !=KVM_REG_ARM64 >>32 )return -EINVAL ;
@@ -786,6 +794,13 @@ int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,u64 esr = events->exception.serror_esr;int ret =0 ; +/* +* EL2 injects an external abort only to complete a forwarded abort. +* SError injection is forwarded. +*/ +if (kvm_vm_is_protected (vcpu->kvm) && ext_dabt_pending) +return -EPERM ; +/* * Immediately commit the pending SEA to the vCPU's architectural * state which is necessary since we do not return a pending SEA
@@ -883,6 +898,10 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, {trace_kvm_set_guest_debug (vcpu, dbg->control); +/* A protected guest's debug state is not exposed to the host. */ +if (kvm_vm_is_protected (vcpu->kvm)) +return -EPERM ; +if (dbg->control & ~KVM_GUESTDBG_VALID_MASK )return -EINVAL ;
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 16/17 KVM: arm64: Reject host power-on of a vCPU that EL2 holds powered off
In reply to Fuad TabbaA protected vCPU's power state belongs to EL2, which changes it only on the guest's own PSCI calls. KVM_SET_MP_STATE(RUNNABLE) on a vCPU EL2 holds powered off changes the host's copy alone, and the guest's next CPU_ON of that vCPU then fails: mp_state is no longer STOPPED, so the host returns ALREADY_ON, which EL2 turns into INTERNAL_FAILURE. SUSPENDED has the same effect.
Track at the host whether EL2 holds a protected vCPU powered off and return -EPERM for both writes in that state. STOPPED stays permitted, so a VMM can pause a vCPU, and RUNNABLE on a vCPU EL2 has powered on, so it can resume one.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/kvm/arm.c | 20 ++++++++++++++++++++ arch/arm64/kvm/pkvm.c | 21 +++++++++++++++++---- arch/arm64/kvm/psci.c | 3 +++ 4 files changed, 42 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -922,6 +922,8 @@ struct kvm_vcpu_arch {/* vcpu power state */ struct kvm_mp_state mp_state;spinlock_t mp_state_lock; +/* EL2 holds the protected vCPU powered off. Under mp_state_lock. */ +bool pkvm_powered_off;/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache;
arch/arm64/kvm/arm.c +20 -0
@@ -777,9 +777,12 @@ static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu)kvm_vcpu_kick (vcpu); } +/* The guest's own CPU_OFF: EL2 has already powered the vCPU off. */ void kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) {spin_lock (&vcpu->arch.mp_state_lock); +if (kvm_vm_is_protected (vcpu->kvm)) + vcpu->arch.pkvm_powered_off = true;__kvm_arm_vcpu_power_off (vcpu);spin_unlock (&vcpu->arch.mp_state_lock); }
@@ -801,6 +804,13 @@ static bool kvm_arm_vcpu_suspended(struct kvm_vcpu *vcpu)return READ_ONCE (vcpu->arch.mp_state.mp_state) ==KVM_MP_STATE_SUSPENDED ; } +/* Only the guest's CPU_ON may start a vCPU EL2 holds powered off. */ +static bool kvm_pkvm_vcpu_is_powered_off (struct kvm_vcpu *vcpu) +{ +return kvm_vm_is_protected (vcpu->kvm) && + vcpu->arch.pkvm_powered_off; +} +int kvm_arch_vcpu_ioctl_get_mpstate (struct kvm_vcpu *vcpu,struct kvm_mp_state *mp_state) {
@@ -818,12 +828,22 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,switch (mp_state->mp_state) {case KVM_MP_STATE_RUNNABLE : +if (kvm_pkvm_vcpu_is_powered_off (vcpu)) { + ret = -EPERM ; +break ; + } +WRITE_ONCE (vcpu->arch.mp_state, *mp_state);break ;case KVM_MP_STATE_STOPPED :__kvm_arm_vcpu_power_off (vcpu);break ;case KVM_MP_STATE_SUSPENDED : +if (kvm_pkvm_vcpu_is_powered_off (vcpu)) { + ret = -EPERM ; +break ; + } +kvm_arm_vcpu_suspend (vcpu);break ;default :
arch/arm64/kvm/pkvm.c +17 -4
@@ -118,12 +118,25 @@ static int __pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu)return -ENOMEM ; ret =kvm_call_hyp_nvhe (__pkvm_init_vcpu, handle, vcpu, hyp_vcpu); -if (!ret) -vcpu_set_flag (vcpu,VCPU_PKVM_FINALIZED ); -else +if (ret) {free_pages_exact (hyp_vcpu, hyp_vcpu_sz); +return ret; + } -return ret; +/* +* Mirror EL2's seeding of power_state from mp_state. The hyp vCPU is +* published, so take mp_state_lock against kvm_psci_vcpu_on(). +*/ +if (kvm_vm_is_protected (vcpu->kvm)) { +spin_lock (&vcpu->arch.mp_state_lock); +if (kvm_arm_vcpu_stopped (vcpu)) + vcpu->arch.pkvm_powered_off = true; +spin_unlock (&vcpu->arch.mp_state_lock); + } + +vcpu_set_flag (vcpu,VCPU_PKVM_FINALIZED ); + +return 0 ; }/*
arch/arm64/kvm/psci.c +3 -0
@@ -94,6 +94,9 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)*/ smp_wmb (); +/* EL2 has committed the protected vCPU to ON_PENDING to get here. */ + vcpu->arch.pkvm_powered_off = false; +WRITE_ONCE (vcpu->arch.mp_state.mp_state,KVM_MP_STATE_RUNNABLE );kvm_vcpu_wake_up (vcpu);
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH 17/17 KVM: arm64: Document the protected VM userspace API
In reply to Fuad TabbaEL2 owns a protected vCPU's state once it has run, and the ioctls that would access it fail rather than silently operate on a copy that is not the guest's. Describe the resulting API in pkvm.rst, point api.rst at it from each ioctl that behaves differently, and note the errnos beside the PVTIME attribute in devices/vcpu.rst and the PSCI version register in fw-pseudo-registers.rst.
No functional change intended.
--- Documentation/virt/kvm/api.rst | 22 ++- .../virt/kvm/arm/fw-pseudo-registers.rst | 2 + Documentation/virt/kvm/arm/pkvm.rst | 141 +++++++++++++++++- Documentation/virt/kvm/devices/vcpu.rst | 4 +- 4 files changed, 161 insertions(+), 8 deletions(-)
Documentation/virt/kvm/api.rst +20 -2
@@ -1322,6 +1322,9 @@ guests, across different userspace implementations. Nevertheless, userspace can still emulate all Arm exceptions by manipulating individual registers using the KVM_SET_ONE_REG API. +For a protected VM, setting ext_dabt_pending returns -EPERM; see +Documentation/virt/kvm/arm/pkvm.rst. Injecting an SError is unaffected. + See KVM_GET_VCPU_EVENTS for the data structure. Calling this ioctl on a vCPU that hasn't been initialized will return
@@ -1624,6 +1627,11 @@ For arm64/riscv: The only states that are valid are KVM_MP_STATE_STOPPED and KVM_MP_STATE_RUNNABLE which reflect if the vcpu should be paused or not. +On arm64, once a protected VM's vcpu has run, KVM_MP_STATE_RUNNABLE and +KVM_MP_STATE_SUSPENDED return -EPERM if the guest has powered it off with +CPU_OFF or has not yet brought it online with CPU_ON: only the guest can +power it on. See Documentation/virt/kvm/arm/pkvm.rst. + On LoongArch, only the KVM_MP_STATE_RUNNABLE state is used to reflect whether the vcpu is runnable.
@@ -2324,7 +2332,8 @@ Errors: ENOENT no such register EINVAL invalid register ID, or no such register or used with VMs in protected virtualization mode on s390 - EPERM (arm64) register access not allowed before vcpu finalization + EPERM (arm64) register access not allowed before vcpu + finalization, or after a protected VM's vcpu has run EBUSY (riscv) changing register value not allowed after the vcpu has run at least once ====== ============================================================
@@ -2949,7 +2958,8 @@ Errors include: ENOENT no such register EINVAL invalid register ID, or no such register or used with VMs in protected virtualization mode on s390 - EPERM (arm64) register access not allowed before vcpu finalization + EPERM (arm64) register access not allowed before vcpu + finalization, or after a protected VM's vcpu has run ======== ============================================================ (These error codes are indicative only: do not rely on a specific error
@@ -3484,6 +3494,7 @@ Errors: ====== ================================================================= EINVAL the target is unknown, or the combination of features is invalid. ENOENT a features bit specified is unknown. + EPERM the vcpu belongs to a protected VM and has already run. ====== ================================================================= This tells KVM what type of CPU to present to the guest, and what
@@ -3512,6 +3523,10 @@ after the vcpu has been run. This will reset the vcpu to its initial state. All calls to this function after the initial call must use the same target and same set of feature flags, otherwise EINVAL will be returned. +For a protected VM this ioctl returns EPERM once the vcpu has run, and the +features it accepts are restricted: KVM_ARM_VCPU_PSCI_0_2 is required. See +Documentation/virt/kvm/arm/pkvm.rst. + Possible features: - KVM_ARM_VCPU_POWER_OFF: Starts the CPU in a power-off state.
@@ -3790,6 +3805,9 @@ can be determined by querying the KVM_CAP_GUEST_DEBUG_HW_BPS and KVM_CAP_GUEST_DEBUG_HW_WPS capabilities which return a positive number indicating the number of supported registers. +On arm64, this ioctl returns -EPERM for a protected VM: debugging a +protected guest is not supported. See Documentation/virt/kvm/arm/pkvm.rst. + For ppc, the KVM_CAP_PPC_GUEST_DEBUG_SSTEP capability indicates whether the single-step debug event (KVM_GUESTDBG_SINGLESTEP) is supported.
Documentation/virt/kvm/arm/fw-pseudo-registers.rst +2 -0
@@ -33,6 +33,8 @@ The following registers are defined: highest PSCI version implemented by KVM and compatible with v0.2) - Allows any PSCI version implemented by KVM and compatible with v0.2 to be set with SET_ONE_REG + - Returns -EINVAL for a version below v1.1 on a protected VM (see + Documentation/virt/kvm/arm/pkvm.rst) - Affects the whole VM (even if the register view is per-vcpu) * KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_1:
Documentation/virt/kvm/arm/pkvm.rst +136 -5
175 lines not on this page · raw message
Documentation/virt/kvm/devices/vcpu.rst +3 -1
@@ -226,12 +226,14 @@ Returns: -ENXIO Stolen time not implemented -EEXIST Base address already set for this VCPU -EINVAL Base address not 64 byte aligned + -EPERM The VCPU belongs to a protected VM ======= ====================================== Specifies the base address of the stolen time structure for this VCPU. The base address must be 64 byte aligned and exist within a valid guest memory region. See Documentation/virt/kvm/arm/pvtime.rst for more information -including the layout of the stolen time structure. +including the layout of the stolen time structure. Stolen time is not +supported for protected VMs (see Documentation/virt/kvm/arm/pkvm.rst). 4. GROUP: KVM_VCPU_TSC_CTRL ===========================
Signature · 1 line
2.39.5
Fuad Tabba
reviewerOn Mon, 31 Aug 2026 at 17:34, Fuad Tabba <[email protected]> wrote:
...
Quoted 3 lines
The kvmtool changes that go with this will be posted separately, and I will reply here with a link.
... and here they are:
https://lore.kernel.org/all/[email protected]/
Cheers,
/fuad
Quoted 91 lines
Patch 1 is the HCR_EL2.VSE fix posted separately [2]. It is not part of this series; it is carried so the series applies as is and Sashiko can run on it.
The
KVM_ARM_PREFERRED_TARGETdocumentation fix [3] went out just ahead of this series. Nothing here needs it to apply, but patch 17 documents vCPU feature availability as something the capabilities report, whileapi.rst4.83 still points userspace at a bitmap that has always been empty.The series is structured as follows:
01: The HCR_EL2.VSE fix, posted separately. 02-03: Capability allowlist and the PVTIME rejection. 04-05: Per-exception-class entry handlers; EL2 owns a protectedvCPU's trap configuration.
06-08: Timer state, system register reset and HVC handling at EL2. 09-10: PSCI at EL2, and theKVM_ARM_VCPU_INITand PSCI version
restrictions.
11-14: Host PC adjustments blocked; an UNDEF at EL2 for exit
classes the host does not emulate; per-class state marshalling; a protected guest's SError pended with HCR_EL2.VSE.
15-16: Host access to private state, and host power-on of a vCPU
EL2 holds powered off, rejected.
17: Documentation.Still to come: selftests, self-hosted debug, SVE for protected guests, and much more, as separate series.
Based on v7.3-rc1 (
cee9395acd804).Cheers,
/fuad
P.S. Sashiko, bring it on![1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/Fuad Tabba (15):
KVM: arm64: Sync HCR_EL2.VSE back to the host vCPU under pKVM
KVM: arm64: Advertise the capabilities that protected VMs support
KVM: arm64: Reject the PVTIME vCPU attribute for protected VMs
KVM: arm64: Skip fixed-feature state flush for protected vCPUs
KVM: arm64: Add system register reset framework for protected VMs
KVM: arm64: Implement HVC handling for protected guests at EL2
KVM: arm64: Handle PSCI calls for protected VMs at EL2
KVM: arm64: RestrictKVM_ARM_VCPU_INITand PSCI version for protected
VMs
KVM: arm64: Prevent host PC adjustments for protected vCPUs
KVM: arm64: Inject an UNDEF at EL2 for unhandled protected guest exits
KVM: arm64: Add per-EC entry/exit state marshalling for protected
guests
KVM: arm64: Pend a protected guest's SError with HCR_EL2.VSE only
KVM: arm64: Reject host access to protected VM private state
KVM: arm64: Reject host power-on of a vCPU that EL2 holds powered off
KVM: arm64: Document the protected VM userspace APIMarc Zyngier (2):
KVM: arm64: Introduce per-EC entry handlers for pKVM
KVM: arm64: Add {flush,sync}_hyp_timer_state() primitivesDocumentation/virt/kvm/api.rst | 22 +- .../virt/kvm/arm/fw-pseudo-registers.rst | 2 + Documentation/virt/kvm/arm/pkvm.rst | 141 ++++- Documentation/virt/kvm/devices/vcpu.rst | 4 +- arch/arm64/include/asm/kvm_asm.h | 1 + arch/arm64/include/asm/kvm_host.h | 21 + arch/arm64/include/asm/kvm_pkvm.h | 34 +- arch/arm64/kvm/arm.c | 40 ++ arch/arm64/kvm/guest.c | 29 + arch/arm64/kvm/hyp/exception.c | 27 +- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 18 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 564 +++++++++++++++++- arch/arm64/kvm/hyp/nvhe/pkvm.c | 401 ++++++++++++- arch/arm64/kvm/hyp/nvhe/switch.c | 29 +- arch/arm64/kvm/hyp/nvhe/sys_regs.c | 91 ++- arch/arm64/kvm/hypercalls.c | 7 + arch/arm64/kvm/inject_fault.c | 5 +- arch/arm64/kvm/pkvm.c | 21 +- arch/arm64/kvm/psci.c | 3 + 19 files changed, 1378 insertions(+), 82 deletions(-)base-commit:
cee9395acd8043be0644b25c34bfa86623f2b935--
2.39.5