[PATCH v3 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA

From: Jia He

Date: Fri Nov 27 2026 - 20:15:40 EST


VNCR_EL2 only needs to be 4K aligned. On a host with 16K or 64K pages,
kvm_translate_vncr() records the host page that contains the guest
hypervisor's VNCR page, and that host page is mapped at the VNCR fixmap.
__compute_hcr() then takes the offset of the VNCR page in the host page
from bits [PAGE_SHIFT-1:12] of the guest's VNCR_EL2, which is a VA.

If the guest hypervisor uses a stage-1 granule smaller than the host
page, these bits of the VA and of the PA can differ. The nested context
then accesses another 4K page of the guest instead of the VNCR page.

Take the offset from the PA that the walk returns, and keep it in
vt->hpa. Use it only while the VNCR page is mapped. Otherwise the
fixmap is clear, the access faults and KVM translates VNCR_EL2 again,
so the offset does not matter.

Tested on a Neoverse-N2 host running 16K and 64K page kernels, with the
selftest added later in this series. The test maps the VNCR page at a VA
whose bits [15:12] differ from those of its PA. With the offset taken
from the VA, the nested context reads 0 instead of the value in the VNCR
page. With this patch, it reads the expected value.

Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Closes: https://lore.kernel.org/all/sashiko-outbox-232440@xxxxxxxxxx/
Fixes: 73e1b621b25d ("KVM: arm64: nv: Program host's VNCR_EL2 to the fixmap address")
Signed-off-by: Jia He <justin.he@xxxxxxx>
---
arch/arm64/include/asm/kvm_nested.h | 1 +
arch/arm64/kvm/hyp/vhe/switch.c | 9 +++++++--
arch/arm64/kvm/nested.c | 8 +++++++-
3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h
index 0f899addafa8..bc54a4b2e863 100644
--- a/arch/arm64/include/asm/kvm_nested.h
+++ b/arch/arm64/include/asm/kvm_nested.h
@@ -432,6 +432,7 @@ int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa,
/* VNCR management */
int kvm_vcpu_allocate_vncr_tlb(struct kvm_vcpu *vcpu);
int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu);
+unsigned long kvm_vncr_page_offset(struct kvm_vcpu *vcpu);
void kvm_handle_s1e2_tlbi(struct kvm_vcpu *vcpu, u32 inst, u64 val);

u16 get_asid_by_regime(struct kvm_vcpu *vcpu, enum trans_regime regime);
diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c
index 3f2390a13173..0d15beea418c 100644
--- a/arch/arm64/kvm/hyp/vhe/switch.c
+++ b/arch/arm64/kvm/hyp/vhe/switch.c
@@ -94,8 +94,13 @@ static u64 __compute_hcr(struct kvm_vcpu *vcpu)
if (guest_hcr & HCR_EL2_NV) {
u64 va = __fix_to_virt(vncr_fixmap(smp_processor_id()));

- /* Inherit the low bits from the actual register */
- va |= __vcpu_sys_reg(vcpu, VNCR_EL2) & GENMASK(PAGE_SHIFT - 1, 0);
+ /*
+ * The fixmap maps the host page that contains the VNCR
+ * page. Locate the VNCR page in it from its PA: with a
+ * smaller guest granule, the VA may have another offset.
+ */
+ if (host_data_test_flag(L1_VNCR_MAPPED))
+ va |= kvm_vncr_page_offset(vcpu);
write_sysreg_s(va, SYS_VNCR_EL2);

/* Force NV2 in case the guest is forgetful... */
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 4d83281a7a28..08582fbc09d5 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -1643,7 +1643,7 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
}

vt->gva = va;
- vt->hpa = pfn << PAGE_SHIFT;
+ vt->hpa = (pfn << PAGE_SHIFT) | offset_in_page(vt->wr.pa);
vt->hpa_writable = writable;
vt->valid = true;
atomic_set(&vt->cpu, -1);
@@ -1683,6 +1683,12 @@ static void handle_vncr_perm(struct kvm_vcpu *vcpu)
kvm_inject_nested_sync(vcpu, esr);
}

+/* Offset of the VNCR page in the host page mapped at the VNCR fixmap */
+unsigned long kvm_vncr_page_offset(struct kvm_vcpu *vcpu)
+{
+ return offset_in_page(vcpu->arch.vncr_tlb->hpa);
+}
+
int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu)
{
struct vncr_tlb *vt = vcpu->arch.vncr_tlb;
--
2.34.1