KVM: arm64: Decouple computation of the GICv5 trap registers from FEAT_FGT
PATCH 1/1 KVM: arm64: Decouple computation of the GICv5 trap registers from FEAT_FGT
Currently, kvm_vcpu_load_fgt() returns early when the CPU lacks FEAT_FGT, so the per-vCPU ICH_HFGRTR_EL2, ICH_HFGWTR_EL2 and ICH_HFGITR_EL2 values are never computed and stay 0, meaning any access to several GICv5 registers will trap, like an access to ICC_PPI_PRIORITYR0_EL1.
On the other hand, __activate_traps_ich_hfgxtr() is gated only on the GICv5 CPU interface and writes them on every guest entry, which is correct.
Returning early when the CPU lacks FEAT_FGT and leaving the GICv5 traps enabled can lead to a KVM "Unsupported guest access at" error when a GICv5 register access by the guest traps to KVM, because the register has no emulation handler.
A real machine with no FEAT_FGT and with GICv5 support enabled is unlikely to exist, but such a setup can happen on some simulators, like FVP with the following flags:
-C cluster0.has_arm_v8-5=1
-C bp.is_gicv5_used=1
Hence, because the ICH trap registers are part of FEAT_GCIE, not part of FEAT_FGT, compute them before the FGT check, making this computation independent of FEAT_FGT.
--- arch/arm64/kvm/config.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/arm64/kvm/config.c +6 -6
@@ -1744,6 +1744,12 @@ static void __compute_ich_hfgwtr(struct kvm_vcpu *vcpu)void kvm_vcpu_load_fgt (struct kvm_vcpu *vcpu) { +if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { +__compute_ich_hfgrtr (vcpu); +__compute_ich_hfgwtr (vcpu); +__compute_fgt (vcpu,ICH_HFGITR_EL2 ); + } +if (!cpus_have_final_cap (ARM64_HAS_FGT ))return ;
@@ -1761,10 +1767,4 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)__compute_fgt (vcpu,HDFGRTR2_EL2 );__compute_fgt (vcpu,HDFGWTR2_EL2 ); } - -if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { -__compute_ich_hfgrtr (vcpu); -__compute_ich_hfgwtr (vcpu); -__compute_fgt (vcpu,ICH_HFGITR_EL2 ); - } }
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] KVM: arm64: populate_nv_trap_config() skips GICv5 FGTs when FEAT_FGT is absent --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] KVM: arm64: populate_nv_trap_config() skips GICv5 FGTs when FEAT_FGT is absent --
commit 78d2daa299df3d77e7dfa794f19cd0a1eee7919c
Author: Gustavo Romero <[email protected]>
KVM: arm64: Decouple computation of the GICv5 trap registers from FEAT_FGT
This commit moves the computation of ICH_HFGRTR_EL2, ICH_HFGWTR_EL2, and ICH_HFGITR_EL2 in kvm_vcpu_load_fgt() ahead of the ARM64_HAS_FGT check so that GICv5 trap register values are computed whenever ARM64_HAS_GICV5_CPUIF is present, even if the CPU lacks FEAT_FGT.
Quoted 6 lines
arch/arm64/kvm/config.c +0
-0@@ -1744,6 +1744,12 @@ static void __compute_ich_hfgwtr(struct kvm_vcpu *vcpu)
arch/arm64/kvm/config.c static void __compute_ich_hfgwtr(struct kvm_vcpu *vcpu) · line 1754
Quoted 10 lines
arch/arm64/kvm/config.c +6
-0void kvm_vcpu_load_fgt (struct kvm_vcpu *vcpu) { +if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { +__compute_ich_hfgrtr (vcpu); +__compute_ich_hfgwtr (vcpu); +__compute_fgt (vcpu,ICH_HFGITR_EL2 ); + } +if (!cpus_have_final_cap (ARM64_HAS_FGT ))return ;
[Severity: High]
Does populate_nv_trap_config() in arch/arm64/kvm/emulate-nested.c also need to be updated to store the ICH_HFGRTR_GROUP and ICH_HFGITR_GROUP entries into sr_forward_xa when ARM64_HAS_GICV5_CPUIF is present but ARM64_HAS_FGT is absent?
Currently, populate_nv_trap_config() skips storing any entry into sr_forward_xa when ARM64_HAS_FGT is not supported:
arch/arm64/kvm/emulate-nested.c:populate_nv_trap_config() {
...
for_each_encoding_in(enc, fgt->encoding, fgt->end) {
...
if (!cpus_have_final_cap(ARM64_HAS_FGT))
continue;
prev = xa_store(&sr_forward_xa, enc,
xa_mk_value(tc.val), GFP_KERNEL);
...
}
}Because of that, on a system with ARM64_HAS_GICV5_CPUIF and !ARM64_HAS_FGT, the GICv5 entries in sr_forward_xa are left with tc.fgt == __NO_FGT_GROUP__ (populated only with tc.sri by populate_sysreg_config() for registers in sys_reg_descs[], or left at 0 for all other GICv5 registers and instructions).
When a guest running without FEAT_GCIE (for example, a GICv3 guest in compatibility mode, or a VM where ID_AA64PFR2_EL1.GCIE was cleared) executes a GICv5 system register or instruction access, kvm_vcpu_load_fgt() has cleared the trap bits in ICH_HFGRTR_EL2, ICH_HFGWTR_EL2, and ICH_HFGITR_EL2 from kvm->arch.fgu[], so the access traps to kvm_handle_sys_reg() -> triage_sysreg_trap():
arch/arm64/kvm/emulate-nested.c:triage_sysreg_trap() {
...
if (tc.fgt != __NO_FGT_GROUP__ &&
(vcpu->kvm->arch.fgu[tc.fgt] & BIT(tc.bit))) {
kvm_inject_undefined(vcpu);
return true;
}
...
}With tc.fgt == __NO_FGT_GROUP__, this FGU check is bypassed instead of injecting an UNDEF:
- A guest read of SYS_ICC_PPI_ENABLER0_EL1 or SYS_ICC_PPI_ENABLER1_EL1 dispatches to access_gicv5_ppi_enabler() in arch/arm64/kvm/sys_regs.c with !p->is_write, triggering WARN_ON_ONCE(!p->is_write).
- A guest write to SYS_ICC_PPI_ENABLER0_EL1 dispatches to access_gicv5_ppi_enabler() and writes to vcpu->arch.vgic_cpu.vgic_v5.vgic_ppi_enabler, which shares a union with vgic_v3 in struct vgic_cpu.
- Guest reads of SYS_ICC_IDR0_EL1 or SYS_ICC_IAFFIDR_EL1 execute access_gicv5_idr0() or access_gicv5_iaffid() instead of injecting UNDEF.
- Guest accesses to other GICv5 registers or instructions fall through to the local label with !tc.sri and log "Unsupported guest access at:" on the host before injecting UNDEF.
Quoted 12 lines
arch/arm64/kvm/config.c +0
-6@@ -1761,10 +1767,4 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)__compute_fgt (vcpu,HDFGRTR2_EL2 );__compute_fgt (vcpu,HDFGWTR2_EL2 ); } - -if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { -__compute_ich_hfgrtr (vcpu); -__compute_ich_hfgwtr (vcpu); -__compute_fgt (vcpu,ICH_HFGITR_EL2 ); - } }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi robot,
Quoted 20 lines · On Tue, Oct 20, 2026 at 10:53:22AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] KVM: arm64:populate_nv_trap_config()skips GICv5 FGTs whenFEAT_FGTis absent --commit
78d2daa299df3d77e7dfa794f19cd0a1eee7919c
Author: Gustavo Romero <[email protected]>KVM: arm64: Decouple computation of the GICv5 trap registers from
FEAT_FGTThis commit moves the computation of
ICH_HFGRTR_EL2,ICH_HFGWTR_EL2, andICH_HFGITR_EL2inkvm_vcpu_load_fgt()ahead of theARM64_HAS_FGTcheck so that GICv5 trap register values are computed wheneverARM64_HAS_GICV5_CPUIFis present, even if the CPU lacksFEAT_FGT.Quoted 6 lines
arch/arm64/kvm/config.c +0
-0@@ -1744,6 +1744,12 @@ static void __compute_ich_hfgwtr(struct kvm_vcpu *vcpu)
Quoted 16 lines
arch/arm64/kvm/config.c static void __compute_ich_hfgwtr(struct kvm_vcpu *vcpu) · line 1754
Quoted 10 lines
arch/arm64/kvm/config.c +6
-0void kvm_vcpu_load_fgt (struct kvm_vcpu *vcpu) { +if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { +__compute_ich_hfgrtr (vcpu); +__compute_ich_hfgwtr (vcpu); +__compute_fgt (vcpu,ICH_HFGITR_EL2 ); + } +if (!cpus_have_final_cap (ARM64_HAS_FGT ))return ;[Severity: High]
Doespopulate_nv_trap_config()inarch/arm64/kvm/emulate-nested.calso need to be updated to store theICH_HFGRTR_GROUPandICH_HFGITR_GROUPentries intosr_forward_xawhenARM64_HAS_GICV5_CPUIFis present butARM64_HAS_FGTis absent?
I guess you're right but it was already addressed by Fuad's patch:
https://lore.kernel.org/all/[email protected]/
that is already in the mainline:
Maybe you're looking at some branch that doesn't have this fix?
Cheers,
Gustavo
Quoted 73 lines
Currently,
populate_nv_trap_config()skips storing any entry intosr_forward_xawhenARM64_HAS_FGTis not supported:arch/arm64/kvm/emulate-nested.c:populate_nv_trap_config() { ... for_each_encoding_in(enc, fgt->encoding, fgt->end) { ... if (!cpus_have_final_cap(ARM64_HAS_FGT)) continue; prev = xa_store(&sr_forward_xa, enc, xa_mk_value(tc.val), GFP_KERNEL); ... } }Because of that, on a system with
ARM64_HAS_GICV5_CPUIFand !ARM64_HAS_FGT, the GICv5 entries insr_forward_xaare left with tc.fgt ==__NO_FGT_GROUP__(populated only with tc.sri bypopulate_sysreg_config()for registers in sys_reg_descs[], or left at 0 for all other GICv5 registers and instructions).When a guest running without
FEAT_GCIE(for example, a GICv3 guest in compatibility mode, or a VM where ID_AA64PFR2_EL1.GCIE was cleared) executes a GICv5 system register or instruction access,kvm_vcpu_load_fgt()has cleared the trap bits inICH_HFGRTR_EL2,ICH_HFGWTR_EL2, andICH_HFGITR_EL2from kvm->arch.fgu[], so the access traps tokvm_handle_sys_reg()->triage_sysreg_trap():arch/arm64/kvm/emulate-nested.c:triage_sysreg_trap() { ... if (tc.fgt != __NO_FGT_GROUP__ && (vcpu->kvm->arch.fgu[tc.fgt] & BIT(tc.bit))) { kvm_inject_undefined(vcpu); return true; } ... }With tc.fgt ==
__NO_FGT_GROUP__, this FGU check is bypassed instead of injecting an UNDEF:- A guest read of
SYS_ICC_PPI_ENABLER0_EL1orSYS_ICC_PPI_ENABLER1_EL1dispatches toaccess_gicv5_ppi_enabler()inarch/arm64/kvm/sys_regs.cwith !p->is_write, triggeringWARN_ON_ONCE(!p->is_write).- A guest write to
SYS_ICC_PPI_ENABLER0_EL1dispatches toaccess_gicv5_ppi_enabler()and writes tovcpu->arch.vgic_cpu.vgic_v5.vgic_ppi_enabler, which shares a union withvgic_v3in structvgic_cpu.- Guest reads of
SYS_ICC_IDR0_EL1orSYS_ICC_IAFFIDR_EL1executeaccess_gicv5_idr0()oraccess_gicv5_iaffid()instead of injecting UNDEF.- Guest accesses to other GICv5 registers or instructions fall through to the local label with !tc.sri and log "Unsupported guest access at:" on the host before injecting UNDEF.
Quoted 12 lines
arch/arm64/kvm/config.c +0
-6@@ -1761,10 +1767,4 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)__compute_fgt (vcpu,HDFGRTR2_EL2 );__compute_fgt (vcpu,HDFGWTR2_EL2 ); } - -if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { -__compute_ich_hfgrtr (vcpu); -__compute_ich_hfgwtr (vcpu); -__compute_fgt (vcpu,ICH_HFGITR_EL2 ); - } }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi Gustavo,
Quoted 4 lines · Gustavo Romero
Currently,
kvm_vcpu_load_fgt()returns early when the CPU lacksFEAT_FGT, so the per-vCPUICH_HFGRTR_EL2,ICH_HFGWTR_EL2andICH_HFGITR_EL2values are never computed and stay 0, meaning any access to several GICv5 registers will trap, like an access toICC_PPI_PRIORITYR0_EL1.
This is never a valid configuration for GICv5 on a system that has EL2, i.e., one that supports KVM.
* FEAT_GCIE (GICv5) is optional from v9.3.
* FEAT_FGT is mandatory from v8.6, if an implementation has EL2 or EL3.
* v9.1 must be v8.6 compliant.
* If we are running KVM, we must have EL2.
There's no way to legally have GICv5 without also having support for FEAT_FGT.
FWIW, Marc caught me making the same mistake:
https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
On the other hand,
__activate_traps_ich_hfgxtr()is gated only on the GICv5 CPU interface and writes them on every guest entry, which is correct.
Quoted 7 lines
Returning early when the CPU lacks
FEAT_FGTand leaving the GICv5 traps enabled can lead to a KVM "Unsupported guest access at" error when a GICv5 register access by the guest traps to KVM, because the register has no emulation handler.A real machine with no
FEAT_FGTand with GICv5 support enabled is unlikely to exist
Given the above, very unlikely. I hope... :)
Review
Quoted 5 lines
, but such a setup can happen on some simulators, like FVP with the following flags:
(quoted diff) +0
-2-C cluster0.has_arm_v8-5=1 -C bp.is_gicv5_used=1
Even if the simulator allows it, it isn't a valid configuration. You want v9.3 or greater here. Moreover, we shouldn't be writing code to support architecturally invalid configurations.
Thanks,
Sascha
Quoted 39 lines
Hence, because the ICH trap registers are part of
FEAT_GCIE, not part ofFEAT_FGT, compute them before the FGT check, making this computation independent ofFEAT_FGT.Fixes 9d6d9514c08f ("KVM: arm64: gic-v5: Support GICv5 FGTs & FGUs")Signed-off-by Gustavo Romero <[email protected]>--- arch/arm64/kvm/config.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)arch/arm64/kvm/config.c +6
-6@@ -1744,6 +1744,12 @@ static void __compute_ich_hfgwtr(struct kvm_vcpu *vcpu)void kvm_vcpu_load_fgt (struct kvm_vcpu *vcpu) { +if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { +__compute_ich_hfgrtr (vcpu); +__compute_ich_hfgwtr (vcpu); +__compute_fgt (vcpu,ICH_HFGITR_EL2 ); + } +if (!cpus_have_final_cap (ARM64_HAS_FGT ))return ;@@ -1761,10 +1767,4 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)__compute_fgt (vcpu,HDFGRTR2_EL2 );__compute_fgt (vcpu,HDFGWTR2_EL2 ); } - -if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { -__compute_ich_hfgrtr (vcpu); -__compute_ich_hfgwtr (vcpu); -__compute_fgt (vcpu,ICH_HFGITR_EL2 ); - } }
Hi Sascha,
Quoted 16 lines · Sascha Bischoff
Hi Gustavo,
Quoted 4 lines · Gustavo Romero
Currently,
kvm_vcpu_load_fgt()returns early when the CPU lacksFEAT_FGT, so the per-vCPUICH_HFGRTR_EL2,ICH_HFGWTR_EL2andICH_HFGITR_EL2values are never computed and stay 0, meaning any access to several GICv5 registers will trap, like an access toICC_PPI_PRIORITYR0_EL1.This is never a valid configuration for GICv5 on a system that has EL2, i.e., one that supports KVM.
*
FEAT_GCIE(GICv5) is optional from v9.3.
*FEAT_FGTis mandatory from v8.6, if an implementation has EL2 or EL3.
* v9.1 must be v8.6 compliant.
* If we are running KVM, we must have EL2.
Quoted 6 lines
There's no way to legally have GICv5 without also having support for
FEAT_FGT.FWIW, Marc caught me making the same mistake:
oh, so you've been there already :)
Thanks a lot for pointing me to that discussion.
Quoted 18 lines
Quoted 5 lines
On the other hand,
__activate_traps_ich_hfgxtr()is gated only on the GICv5 CPU interface and writes them on every guest entry, which is correct.Quoted 7 lines
Returning early when the CPU lacks
FEAT_FGTand leaving the GICv5 traps enabled can lead to a KVM "Unsupported guest access at" error when a GICv5 register access by the guest traps to KVM, because the register has no emulation handler.A real machine with no
FEAT_FGTand with GICv5 support enabled is unlikely to existGiven the above, very unlikely. I hope... :)
Quoted 3 lines
, but such a setup can happen on some simulators, like FVP with the following flags:
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +0
-2-C cluster0.has_arm_v8-5=1 -C bp.is_gicv5_used=1Even if the simulator allows it, it isn't a valid configuration. You want v9.3 or greater here. Moreover, we shouldn't be writing code to support architecturally invalid configurations.
Yeah, I agree.
Now I also see the code has the assumption that FEAT_GCIE always imply FEAT_FGT, but FEAT_FGT does not imply FEAT_GCIE, so indeed these two features are, in that sense, not totally independent.
BTW, the output in the thread you pointed out:
(FEAT_GCIE ==> v9Ap3) (FEAT_FGT ==> v8Ap5) (v9Ap3 ==> (v9Ap2 && v8Ap8))
seems some form of processed AARCHMRS. Are you folks using any script to digest it?
Cheers,
Gustavo
Quoted 41 lines
Thanks,
SaschaQuoted 37 lines
Hence, because the ICH trap registers are part of
FEAT_GCIE, not part ofFEAT_FGT, compute them before the FGT check, making this computation independent ofFEAT_FGT.Fixes 9d6d9514c08f ("KVM: arm64: gic-v5: Support GICv5 FGTs & FGUs")Signed-off-by Gustavo Romero <[email protected]>--- arch/arm64/kvm/config.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)arch/arm64/kvm/config.c +6
-6@@ -1744,6 +1744,12 @@ static void __compute_ich_hfgwtr(struct kvm_vcpu *vcpu)void kvm_vcpu_load_fgt (struct kvm_vcpu *vcpu) { +if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { +__compute_ich_hfgrtr (vcpu); +__compute_ich_hfgwtr (vcpu); +__compute_fgt (vcpu,ICH_HFGITR_EL2 ); + } +if (!cpus_have_final_cap (ARM64_HAS_FGT ))return ;@@ -1761,10 +1767,4 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)__compute_fgt (vcpu,HDFGRTR2_EL2 );__compute_fgt (vcpu,HDFGWTR2_EL2 ); } - -if (cpus_have_final_cap (ARM64_HAS_GICV5_CPUIF )) { -__compute_ich_hfgrtr (vcpu); -__compute_ich_hfgwtr (vcpu); -__compute_fgt (vcpu,ICH_HFGITR_EL2 ); - } }
Marc Zyngier
maintainerQuoted 9 lines · Gustavo Romero
BTW, the output in the thread you pointed out:
(FEAT_GCIE ==> v9Ap3) (FEAT_FGT ==> v8Ap5) (v9Ap3 ==> (v9Ap2 && v8Ap8))seems some form of processed AARCHMRS. Are you folks using any script to digest it?
See the tooling branch in [1]. You can point the scripts at any of the commits to pick the version of the JSON you want.
M.
[1] git://git.kernel.org/pub/scm/linux/kernel/git/maz/AARCHMRS
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 9 lines · Marc Zyngier
Quoted 7 lines · Gustavo Romero
BTW, the output in the thread you pointed out:
(FEAT_GCIE ==> v9Ap3) (FEAT_FGT ==> v8Ap5) (v9Ap3 ==> (v9Ap2 && v8Ap8))
Quoted 5 lines
Quoted 2 lines
seems some form of processed AARCHMRS. Are you folks using any script to digest it?
See the tooling branch in [1]. You can point the scripts at any of the commits to pick the version of the JSON you want.
Thanks, Marc!
Cheers,
Gustavo
Quoted 7 lines
M.[1]
git://git.kernel.org/pub/scm/linux/kernel/git/maz/AARCHMRSSignature · 1 line
Without deviation from the norm, progress is not possible.