* [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization
@ 2026-11-19 13:25 Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1 Dan Carpenter
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Jintack Lim
Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
Clément Blaudeau, stable
This fixes two issues in handling AT emulation for nested virt.
* VMs were potentially using the TCR2 bits which weren't intended for
them.
* It turned out that guests could write to the TCR2 bits even when
those were supposed to be hidden. That is patches 2 and 3.
* The VMID wasn't handled correctly. Roll over could happen before
or during the AT execution leading to invalid results.
Changes in v2:
1) Sashiko pointed out that we need to restore the original TCR2
bits at the end of the AT command.
2) Clean up based on Marc's feed back.
3) Trap the writes to the TCR2 registers (normal and nested)
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
Dan Carpenter (4):
KVM: arm64: nv: Fix AT emulation for TCR2_EL1
KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden
KVM: arm64: nv: Clear hidden TCR2 bits for nested guests
KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation
arch/arm64/kvm/at.c | 54 +++++++++++++++++++++++++++---
arch/arm64/kvm/config.c | 5 +++
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 10 +++++-
arch/arm64/kvm/sys_regs.c | 33 ++++++++++++++++--
4 files changed, 95 insertions(+), 7 deletions(-)
---
base-commit: 5a7235e9adff42aa043ac9473dbeb0354dca0967
change-id: 20261119-nv-tcr2-59b13df5ecf6
Best regards,
--
Dan Carpenter <dan@pereq.eu>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1
2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
@ 2026-11-19 13:25 ` Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden Dan Carpenter
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Jintack Lim
Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
Clément Blaudeau, stable
Arm provides an AT instruction that translates a virtual address the way
the MMU would and returns the result. There is a bug in the way this
instruction is emulated for nested virtualization because of how TCR2_EL1
is handled. The bug is fairly minor.
The TCR2_EL1 register has bits which turn on permission indirection
(PIE) and permission overlays (POE). When these bits are set, the AT
instruction reads the permissions stored in PIR_EL1 and POR_EL1. If
you have the right features and permissions then in the return value from
the AT instruction we set one bit to either 1 or 0 depending on whether
the access would be allowed or whether it would fault.
However, the bug is that just because the host has TCR2 enabled, it
doesn't mean the VM has it. In the current code KVM doesn't load these
registers for a VM which doesn't have the features, instead the AT runs
with the host's HCRX_EL2, which has TCR2En set. So the AT uses the
TCR2_EL1, PIR_EL1 and POR_EL1 values another VM left on the CPU, or
possibly the VMM's POR_EL0. Likewise, for a VM which has TCR2 but not
PIE or POE, the L2's TCR2_EL1 is loaded unmasked.
The impact of this bug is that we leak a single bit of data to the L1,
but in real life it's hard to imagine how it is useful to an attacker.
Fix this by clearing the PIE and POE bits in effective_tcr2() when the
VM doesn't have those features, and use it for the L2's TCR2_EL1. When
the AT runs on the guest hypervisor's own registers, in __kvm_at_s1e2()
and in __kvm_at_s1e01_fast(), load the guest's TCR2_EL2 for the AT and
restore the live value afterwards.
Fixes: 477e89cabb14 ("KVM: arm64: nv: Add basic emulation of AT S1E{0,1}{R,W}")
Fixes: 23e7a34c8397 ("KVM: arm64: Add AT fast-path support for S1PIE")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: Restore original TCR2 values at the end of the AT command
Cleanup the code based on Marc's review.
Add some comments
---
arch/arm64/kvm/at.c | 48 +++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 45 insertions(+), 3 deletions(-)
diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c
index 30d8f062b2c2..9dfabd0586e2 100644
--- a/arch/arm64/kvm/at.c
+++ b/arch/arm64/kvm/at.c
@@ -91,11 +91,18 @@ static enum trans_regime compute_translation_regime(struct kvm_vcpu *vcpu, u32 o
static u64 effective_tcr2(struct kvm_vcpu *vcpu, enum trans_regime regime)
{
if (regime == TR_EL10) {
+ u64 val;
+
if (vcpu_has_nv(vcpu) &&
!(__vcpu_sys_reg(vcpu, HCRX_EL2) & HCRX_EL2_TCR2En))
return 0;
- return vcpu_read_sys_reg(vcpu, TCR2_EL1);
+ val = vcpu_read_sys_reg(vcpu, TCR2_EL1);
+ if (!kvm_has_s1pie(vcpu->kvm))
+ val &= ~TCR2_EL1_PIE;
+ if (!kvm_has_s1poe(vcpu->kvm))
+ val &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+ return val;
}
return vcpu_read_sys_reg(vcpu, TCR2_EL2);
@@ -1384,6 +1391,24 @@ static int handle_at_slow(struct kvm_vcpu *vcpu, u32 op, u64 vaddr, u64 *par)
return 0;
}
+/*
+ * TCR2 features are always exposed to the guests. Clear the ones
+ * which are not exposed on this VM. Return the original TCR2 so we
+ * can restore it after running the AT command.
+ */
+static u64 load_guest_tcr2(struct kvm_vcpu *vcpu)
+{
+ u64 orig = read_sysreg_el1(SYS_TCR2);
+
+ /*
+ * This relies on the reset value of TCR2_EL2 being 0, and not
+ * being modified when FEAT_TCR2 is not exposed to the guest.
+ */
+ write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR2_EL2), SYS_TCR2);
+
+ return orig;
+}
+
/*
* Return the PAR_EL1 value as the result of a valid translation.
*
@@ -1397,7 +1422,9 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
{
struct mmu_config config;
struct kvm_s2_mmu *mmu;
+ bool swap_tcr2 = false;
bool fail, mmu_cs;
+ u64 orig_tcr2;
u64 par;
par = SYS_PAR_EL1_F;
@@ -1438,7 +1465,7 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR_EL1), SYS_TCR);
write_sysreg_el1(vcpu_read_sys_reg(vcpu, MAIR_EL1), SYS_MAIR);
if (kvm_has_tcr2(vcpu->kvm)) {
- write_sysreg_el1(vcpu_read_sys_reg(vcpu, TCR2_EL1), SYS_TCR2);
+ write_sysreg_el1(effective_tcr2(vcpu, TR_EL10), SYS_TCR2);
if (kvm_has_s1pie(vcpu->kvm)) {
write_sysreg_el1(vcpu_read_sys_reg(vcpu, PIR_EL1), SYS_PIR);
write_sysreg_el1(vcpu_read_sys_reg(vcpu, PIRE0_EL1), SYS_PIRE0);
@@ -1452,6 +1479,12 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
__load_stage2(mmu);
skip_mmu_switch:
+ if (!mmu_cs && vcpu_has_nv(vcpu) &&
+ cpus_have_final_cap(ARM64_HAS_TCR2)) {
+ swap_tcr2 = true;
+ orig_tcr2 = load_guest_tcr2(vcpu);
+ }
+
/* Temporarily switch back to guest context */
write_sysreg_hcr(vcpu->arch.hcr_el2);
isb();
@@ -1485,6 +1518,9 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
if (!fail)
par = read_sysreg_par();
+ if (swap_tcr2)
+ write_sysreg_el1(orig_tcr2, SYS_TCR2);
+
write_sysreg_hcr(HCR_HOST_VHE_FLAGS);
if (mmu_cs)
@@ -1544,7 +1580,7 @@ int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
* switching context behind everybody's back, disable interrupts...
*/
scoped_guard(write_lock_irqsave, &vcpu->kvm->mmu_lock) {
- u64 val, hcr;
+ u64 val, hcr, orig_tcr2;
bool fail;
val = hcr = read_sysreg(hcr_el2);
@@ -1554,6 +1590,9 @@ int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
if (!vcpu_el2_e2h_is_set(vcpu))
val |= HCR_EL2_NV | HCR_EL2_NV1;
+ if (cpus_have_final_cap(ARM64_HAS_TCR2))
+ orig_tcr2 = load_guest_tcr2(vcpu);
+
write_sysreg_hcr(val);
isb();
@@ -1577,6 +1616,9 @@ int __kvm_at_s1e2(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
if (!fail)
par = read_sysreg_par();
+ if (cpus_have_final_cap(ARM64_HAS_TCR2))
+ write_sysreg_el1(orig_tcr2, SYS_TCR2);
+
write_sysreg_hcr(hcr);
isb();
}
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden
2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1 Dan Carpenter
@ 2026-11-19 13:25 ` Dan Carpenter
2026-11-19 16:32 ` Marc Zyngier
2026-11-19 13:25 ` [PATCH v2 3/4] KVM: arm64: nv: Clear hidden TCR2 bits for nested guests Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 4/4] KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation Dan Carpenter
3 siblings, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Jintack Lim
Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
Clément Blaudeau, stable
A VMM can offer FEAT_TCR2 to a guest but hide FEAT_S1PIE or FEAT_S1POE,
for example this is done to keep the VM migratable. KVM then stops
loading the PIR_EL1, PIRE0_EL1 and POR_EL1 registers for that guest. But
the guest's writes to the TCR2_EL1 register don't trap, and the CPU still
acts on its PIE, POE and E0POE bits.
If a guest turned these features on, it would run with the permission
settings another VM left on the CPU. Technically this is a kind of
information leak and a guest could figure out another guests permission
setting based on which accesses fault.
For these VMs, set the TCR_EL1 bit in HFGWTR_EL2 so that writes to
TCR_EL1 and TCR2_EL1 trap, and clear the hidden bits in a new
access_tcr2_el1() handler.
Fixes: 58627b722ee2 ("KVM: arm64: Make PIR{,E0}_EL1 UNDEF if S1PIE is not advertised to the guest")
Fixes: b86c9bea6349 ("KVM: arm64: Save/restore POE registers")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: This is a new patch
This is only a partial fix, I will update nested virtualization in the
next patch and the !vcpu_has_nv(vcpu) condition in __compute_hfgwtr() will
go away.
---
arch/arm64/kvm/config.c | 5 +++++
arch/arm64/kvm/sys_regs.c | 14 +++++++++++++-
2 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index e295435a30b8..f756f7c4fdf8 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -1896,6 +1896,11 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu)
if (cpus_have_final_cap(ARM64_WORKAROUND_AMPERE_AC03_CPU_38))
*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
+
+ /* The CPU acts on hidden TCR2_EL1 bits, so trap writes to clear them */
+ if (!vcpu_has_nv(vcpu) && kvm_has_tcr2(vcpu->kvm) &&
+ (!kvm_has_s1pie(vcpu->kvm) || !kvm_has_s1poe(vcpu->kvm)))
+ *vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
}
static void __compute_hdfgwtr(struct kvm_vcpu *vcpu)
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index afd7e76bedff..3899702d438b 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -615,6 +615,18 @@ static bool access_vm_reg(struct kvm_vcpu *vcpu,
return true;
}
+static bool access_tcr2_el1(struct kvm_vcpu *vcpu,
+ struct sys_reg_params *p,
+ const struct sys_reg_desc *r)
+{
+ if (!kvm_has_s1pie(vcpu->kvm))
+ p->regval &= ~TCR2_EL1_PIE;
+ if (!kvm_has_s1poe(vcpu->kvm))
+ p->regval &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+
+ return access_vm_reg(vcpu, p, r);
+}
+
static bool access_actlr(struct kvm_vcpu *vcpu,
struct sys_reg_params *p,
const struct sys_reg_desc *r)
@@ -3501,7 +3513,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
{ SYS_DESC(SYS_TTBR0_EL1), access_vm_reg, reset_unknown, TTBR0_EL1 },
{ SYS_DESC(SYS_TTBR1_EL1), access_vm_reg, reset_unknown, TTBR1_EL1 },
{ SYS_DESC(SYS_TCR_EL1), access_vm_reg, reset_val, TCR_EL1, 0 },
- { SYS_DESC(SYS_TCR2_EL1), access_vm_reg, reset_val, TCR2_EL1, 0,
+ { SYS_DESC(SYS_TCR2_EL1), access_tcr2_el1, reset_val, TCR2_EL1, 0,
.visibility = tcr2_visibility },
PTRAUTH_KEY(APIA),
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 3/4] KVM: arm64: nv: Clear hidden TCR2 bits for nested guests
2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1 Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden Dan Carpenter
@ 2026-11-19 13:25 ` Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 4/4] KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation Dan Carpenter
3 siblings, 0 replies; 6+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Jintack Lim
Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
Clément Blaudeau, stable
The previous patch added traps for TCR2_EL1 writes on VMs that have
FEAT_TCR2 but not FEAT_S1PIE or FEAT_S1POE. This patch extends that
fix to cover nested virtualization.
The bug is that a KVM can hide the PIE and POE bits in TCR2_EL1 but a VM
can still write to them. It obviously makes no sense to hide a register
from the VM but still allow writes. The implication of this bug is a
small information leak because a VM can determine the permission bits of
another VM's page tables.
The previous patch trapped writes to TCR2_EL1 but nested virtualization
is more complicated because we have to deal with TCR2_EL2 as well. A
trapped TCR2_EL1 write has to go to either TCR2_EL2 or TCR2_EL1 depending
on who made it. With nested virtualization the hidden bits can reach the
hardware in three ways:
- A guest hypervisor at vEL2 writes its TCR2_EL2.
- An L2 writes its own TCR2_EL1.
- An L1 writes its L2's TCR2_EL1 in the VNCR page, which KVM loads as is.
To fix this, remove the vcpu_has_nv() check in __compute_hfgwtr() so that
these writes are trapped too. When the guest hypervisor is at vEL2,
update TCR2_EL2 and TCR_EL2 instead of L2's registers. Also clear the
hidden bits in __sysreg_restore_el1_state() when it loads a guest's
TCR2_EL1.
Fixes: 58627b722ee2 ("KVM: arm64: Make PIR{,E0}_EL1 UNDEF if S1PIE is not advertised to the guest")
Fixes: b86c9bea6349 ("KVM: arm64: Save/restore POE registers")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: This is a new patch
---
arch/arm64/kvm/config.c | 2 +-
arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h | 10 +++++++++-
arch/arm64/kvm/sys_regs.c | 19 ++++++++++++++++++-
3 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
index f756f7c4fdf8..8bde0a5eafb6 100644
--- a/arch/arm64/kvm/config.c
+++ b/arch/arm64/kvm/config.c
@@ -1898,7 +1898,7 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu)
*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
/* The CPU acts on hidden TCR2_EL1 bits, so trap writes to clear them */
- if (!vcpu_has_nv(vcpu) && kvm_has_tcr2(vcpu->kvm) &&
+ if (kvm_has_tcr2(vcpu->kvm) &&
(!kvm_has_s1pie(vcpu->kvm) || !kvm_has_s1poe(vcpu->kvm)))
*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
}
diff --git a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
index 4a008c8a696b..d6e8bab14ed3 100644
--- a/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
+++ b/arch/arm64/kvm/hyp/include/hyp/sysreg-sr.h
@@ -261,7 +261,15 @@ static inline void __sysreg_restore_el1_state(struct kvm_cpu_context *ctxt,
write_sysreg_el1(ctxt_sys_reg(ctxt, TTBR0_EL1), SYS_TTBR0);
write_sysreg_el1(ctxt_sys_reg(ctxt, TTBR1_EL1), SYS_TTBR1);
if (ctxt_has_tcrx(ctxt)) {
- write_sysreg_el1(ctxt_sys_reg(ctxt, TCR2_EL1), SYS_TCR2);
+ u64 tcr2 = ctxt_sys_reg(ctxt, TCR2_EL1);
+
+ if (ctxt_is_guest(ctxt)) {
+ if (!ctxt_has_s1pie(ctxt))
+ tcr2 &= ~TCR2_EL1_PIE;
+ if (!ctxt_has_s1poe(ctxt))
+ tcr2 &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+ }
+ write_sysreg_el1(tcr2, SYS_TCR2);
if (ctxt_has_s1pie(ctxt)) {
write_sysreg_el1(ctxt_sys_reg(ctxt, PIR_EL1), SYS_PIR);
diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
index 3899702d438b..e4a3beac6ec9 100644
--- a/arch/arm64/kvm/sys_regs.c
+++ b/arch/arm64/kvm/sys_regs.c
@@ -624,6 +624,23 @@ static bool access_tcr2_el1(struct kvm_vcpu *vcpu,
if (!kvm_has_s1poe(vcpu->kvm))
p->regval &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+ if (is_hyp_ctxt(vcpu) && vcpu_el2_e2h_is_set(vcpu)) {
+ vcpu_write_sys_reg(vcpu, p->regval, TCR2_EL2);
+ return true;
+ }
+
+ return access_vm_reg(vcpu, p, r);
+}
+
+static bool access_tcr_el1(struct kvm_vcpu *vcpu,
+ struct sys_reg_params *p,
+ const struct sys_reg_desc *r)
+{
+ if (is_hyp_ctxt(vcpu) && vcpu_el2_e2h_is_set(vcpu)) {
+ vcpu_write_sys_reg(vcpu, p->regval, TCR_EL2);
+ return true;
+ }
+
return access_vm_reg(vcpu, p, r);
}
@@ -3512,7 +3529,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
{ SYS_DESC(SYS_SMCR_EL1), undef_access },
{ SYS_DESC(SYS_TTBR0_EL1), access_vm_reg, reset_unknown, TTBR0_EL1 },
{ SYS_DESC(SYS_TTBR1_EL1), access_vm_reg, reset_unknown, TTBR1_EL1 },
- { SYS_DESC(SYS_TCR_EL1), access_vm_reg, reset_val, TCR_EL1, 0 },
+ { SYS_DESC(SYS_TCR_EL1), access_tcr_el1, reset_val, TCR_EL1, 0 },
{ SYS_DESC(SYS_TCR2_EL1), access_tcr2_el1, reset_val, TCR2_EL1, 0,
.visibility = tcr2_visibility },
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 4/4] KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation
2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
` (2 preceding siblings ...)
2026-11-19 13:25 ` [PATCH v2 3/4] KVM: arm64: nv: Clear hidden TCR2 bits for nested guests Dan Carpenter
@ 2026-11-19 13:25 ` Dan Carpenter
3 siblings, 0 replies; 6+ messages in thread
From: Dan Carpenter @ 2026-11-19 13:25 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Jintack Lim
Cc: linux-arm-kernel, kvmarm, Dan Carpenter, Karthik Bhargavan,
Clément Blaudeau, stable
There is a missing kvm_arm_vmid_update() before the __load_stage2()
in __kvm_at_s1e01_fast(). The bug affects nested virtualization on
arm64.
In KVM a "stage 2" refers to the VM's page table mappings. The
__kvm_at_s1e01_fast() function emulates the AT instruction, which
translates a virtual address the way the MMU would and returns the
result.
Each stage 2 has a VMID, which the hardware uses to tag TLB and
walk-cache entries, so that one VM never uses another's entries. There
are only 2^8 or 2^16 VMIDs, which is quite limited, so vmid.c hands
them out by generation. When they run out, a rollover starts a new
generation and flushes the TLBs: the VMIDs active on a CPU at that
moment are kept, and every other VMID can be given to another VM.
The kvm_arm_vmid_update() function checks the VMID's generation, takes
a new VMID if it is from an old generation, and marks it as active on
this CPU. Without this, the shadow MMU's VMID could be an old one that
now belongs to another VM, or a rollover on another CPU could hand it
to another VM while we are using it. Either way the AT fills TLB entries
tagged with another VM's VMID.
To fix this, call kvm_arm_vmid_update() to update the VMID before loading
the shadow stage 2. That marks it as active on the CPU. Then at the end
of the function, undo that. Restore the original VMID as active while
checking for and handling rollover.
Fixes: 477e89cabb14 ("KVM: arm64: nv: Add basic emulation of AT S1E{0,1}{R,W}")
Cc: stable@vger.kernel.org
Signed-off-by: Dan Carpenter <dan@pereq.eu>
---
v2: Unchanged
---
arch/arm64/kvm/at.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c
index 9dfabd0586e2..5924cfa01bd4 100644
--- a/arch/arm64/kvm/at.c
+++ b/arch/arm64/kvm/at.c
@@ -1476,6 +1476,7 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
}
}
write_sysreg_el1(vcpu_read_sys_reg(vcpu, SCTLR_EL1), SYS_SCTLR);
+ kvm_arm_vmid_update(&mmu->vmid);
__load_stage2(mmu);
skip_mmu_switch:
@@ -1523,8 +1524,11 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
write_sysreg_hcr(HCR_HOST_VHE_FLAGS);
- if (mmu_cs)
+ if (mmu_cs) {
+ kvm_arm_vmid_update(&vcpu->arch.hw_mmu->vmid);
+ config.vttbr = kvm_get_vttbr(vcpu->arch.hw_mmu);
__mmu_config_restore(&config);
+ }
return par;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden
2026-11-19 13:25 ` [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden Dan Carpenter
@ 2026-11-19 16:32 ` Marc Zyngier
0 siblings, 0 replies; 6+ messages in thread
From: Marc Zyngier @ 2026-11-19 16:32 UTC (permalink / raw)
To: Dan Carpenter
Cc: Oliver Upton, Fuad Tabba, Joey Gouly, Steffen Eiden,
Suzuki K Poulose, Zenghui Yu, Catalin Marinas, Will Deacon,
Mark Rutland, Jintack Lim, linux-arm-kernel, kvmarm,
Karthik Bhargavan, Clément Blaudeau, stable
On Thu, 19 Nov 2026 13:25:35 +0000,
Dan Carpenter <dan@pereq.eu> wrote:
>
> A VMM can offer FEAT_TCR2 to a guest but hide FEAT_S1PIE or FEAT_S1POE,
> for example this is done to keep the VM migratable. KVM then stops
> loading the PIR_EL1, PIRE0_EL1 and POR_EL1 registers for that guest. But
> the guest's writes to the TCR2_EL1 register don't trap, and the CPU still
> acts on its PIE, POE and E0POE bits.
>
> If a guest turned these features on, it would run with the permission
> settings another VM left on the CPU. Technically this is a kind of
> information leak and a guest could figure out another guests permission
> setting based on which accesses fault.
>
> For these VMs, set the TCR_EL1 bit in HFGWTR_EL2 so that writes to
> TCR_EL1 and TCR2_EL1 trap, and clear the hidden bits in a new
> access_tcr2_el1() handler.
>
> Fixes: 58627b722ee2 ("KVM: arm64: Make PIR{,E0}_EL1 UNDEF if S1PIE is not advertised to the guest")
> Fixes: b86c9bea6349 ("KVM: arm64: Save/restore POE registers")
> Cc: stable@vger.kernel.org
> Signed-off-by: Dan Carpenter <dan@pereq.eu>
> ---
> v2: This is a new patch
>
> This is only a partial fix, I will update nested virtualization in the
> next patch and the !vcpu_has_nv(vcpu) condition in __compute_hfgwtr() will
> go away.
No, I really don't want to go down that road.
Trapping TCR_EL1 is an absolute performance sink, and at this point it
is not worth running VMs (it puts everyone at the level of AmpereOne).
It also puts the penalty on well behaved guests which should be able
to access TCR_EL1 as much as they want without penalties.
Yes, the architecture is terrible, and we have to work around it. I
can see two ways to address this:
- load PIR/PIRE0/POR unconditionally so that they do not contain any
state that logically belong to another guest, preferably with values
that prevent any access (0 is a good candidate for the permissions).
It means that if the guest plays with us, it will lose permission to
its own memory accesses.
- Move TCR2_EL1 to the sanitised set, and make sure that these bits are
never evaluated by KVM, and will be cleared on each entry (we
already do this for TCR2_EL2).
Thanks,
M.
--
Without deviation from the norm, progress is not possible.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-19 16:32 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 13:25 [PATCH v2 0/4] KVM: arm64: nv: Fix AT emulation for nested virtualization Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 1/4] KVM: arm64: nv: Fix AT emulation for TCR2_EL1 Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 2/4] KVM: arm64: Trap TCR2_EL1 writes when S1PIE or S1POE is hidden Dan Carpenter
2026-11-19 16:32 ` Marc Zyngier
2026-11-19 13:25 ` [PATCH v2 3/4] KVM: arm64: nv: Clear hidden TCR2 bits for nested guests Dan Carpenter
2026-11-19 13:25 ` [PATCH v2 4/4] KVM: arm64: nv: Update the shadow MMU's VMID for AT emulation Dan Carpenter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox