From: Dan Carpenter <dan@pereq.eu>
To: Marc Zyngier <maz@kernel.org>
Cc: "Oliver Upton" <oupton@kernel.org>,
"Fuad Tabba" <fuad.tabba@linux.dev>,
"Joey Gouly" <joey.gouly@arm.com>,
"Steffen Eiden" <seiden@linux.ibm.com>,
"Suzuki K Poulose" <suzuki.poulose@arm.com>,
"Zenghui Yu" <yuzenghui@huawei.com>,
"Catalin Marinas" <catalin.marinas@arm.com>,
"Will Deacon" <will@kernel.org>,
"Mark Rutland" <mark.rutland@arm.com>,
"Jintack Lim" <jintack.lim@linaro.org>,
linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
linux-kernel@vger.kernel.org,
"Karthik Bhargavan" <karthik@pereq.eu>,
"Clément Blaudeau" <clement@cryspen.com>
Subject: [PATCH 1/2] KVM: arm64: nv: Fix AT emulation for TCR2_EL1
Date: Mon, 16 Nov 2026 09:00:52 +0300 [thread overview]
Message-ID: <eec50693a12e1ba34d60458ae8fe605ac198a79c.1794808373.git.dan@pereq.eu> (raw)
In-Reply-To: <cover.1794808373.git.dan@pereq.eu>
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. 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 writing zero to TCR2_EL1 when the VM doesn't have TCR2, and
by clearing the PIE and POE bits when the VM doesn't have those
features. Also it should be effective_tcr2(vcpu, TR_EL10) to read the
TCR2 for the L1 instead of vcpu_read_sys_reg(vcpu, TCR2_EL1).
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>
---
arch/arm64/kvm/at.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c
index 30d8f062b2c2..06765881a4c8 100644
--- a/arch/arm64/kvm/at.c
+++ b/arch/arm64/kvm/at.c
@@ -1438,7 +1438,13 @@ 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);
+ u64 tcr2 = effective_tcr2(vcpu, TR_EL10);
+
+ if (!kvm_has_s1pie(vcpu->kvm))
+ tcr2 &= ~TCR2_EL1_PIE;
+ if (!kvm_has_s1poe(vcpu->kvm))
+ tcr2 &= ~(TCR2_EL1_POE | TCR2_EL1_E0POE);
+ write_sysreg_el1(tcr2, 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 +1458,9 @@ static u64 __kvm_at_s1e01_fast(struct kvm_vcpu *vcpu, u32 op, u64 vaddr)
__load_stage2(mmu);
skip_mmu_switch:
+ if (cpus_have_final_cap(ARM64_HAS_TCR2) && !kvm_has_tcr2(vcpu->kvm))
+ write_sysreg_el1(0, SYS_TCR2);
+
/* Temporarily switch back to guest context */
write_sysreg_hcr(vcpu->arch.hcr_el2);
isb();
@@ -1554,6 +1563,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) && !kvm_has_tcr2(vcpu->kvm))
+ write_sysreg_el1(0, SYS_TCR2);
+
write_sysreg_hcr(val);
isb();
--
2.53.0
prev parent reply other threads:[~2026-11-16 6:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 6:00 [PATCH 0/2] KVM: arm64: nv: fix a couple bugs in AT emulation Dan Carpenter
2026-11-16 6:00 ` [PATCH 2/2] KVM: arm64: nv: Update the shadow MMU's VMID for " Dan Carpenter
2026-11-16 6:00 ` Dan Carpenter [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=eec50693a12e1ba34d60458ae8fe605ac198a79c.1794808373.git.dan@pereq.eu \
--to=dan@pereq.eu \
--cc=catalin.marinas@arm.com \
--cc=clement@cryspen.com \
--cc=fuad.tabba@linux.dev \
--cc=jintack.lim@linaro.org \
--cc=joey.gouly@arm.com \
--cc=karthik@pereq.eu \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=maz@kernel.org \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®