[RFC PATCH 05/16] KVM: arm64: Implement kvm_arch_vcpu_get_ip_gpa()
From: Ian Rogers
Date: Tue Dec 01 2026 - 18:22:07 EST
Implement kvm_arch_vcpu_get_ip_gpa() on arm64 so PERF_SAMPLE_HOST_IP
translates the guest PC to a guest physical address using non-faulting
guest memory reads.
Parameterize the Stage-1 translation walk in at.c with a non-faulting
guest read mode backed by kvm_vcpu_read_guest_nofault() and use
__kvm_translate_va() to translate the guest PC with execute permission
checks (including LPA2, contiguous mappings, S1PIE, and S1POE),
returning INVALID_GPA under pKVM or nested Stage-2.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
arch/arm64/include/asm/kvm_nested.h | 2 ++
arch/arm64/kvm/arm.c | 38 ++++++++++++++++++++++++++++
arch/arm64/kvm/at.c | 39 ++++++++++++++++++-----------
3 files changed, 65 insertions(+), 14 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h
index 27ec73da02c6..4e94b57d21e3 100644
--- a/arch/arm64/include/asm/kvm_nested.h
+++ b/arch/arm64/include/asm/kvm_nested.h
@@ -396,6 +396,8 @@ struct s1_walk_info {
bool s2;
bool pa52bit;
bool ha;
+ bool nofault;
+ bool insn_fetch;
};
struct s1_walk_result {
diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index a148e80d0aef..98563c3bbec3 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -983,6 +983,44 @@ unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
{
return *vcpu_pc(vcpu);
}
+
+gpa_t kvm_arch_vcpu_get_ip_gpa(struct kvm_vcpu *vcpu)
+{
+ struct s1_walk_result wr = {};
+ struct s1_walk_info wi = {
+ .nofault = true,
+ .insn_fetch = true,
+ };
+ u64 va = *vcpu_pc(vcpu);
+
+ if (kvm_vm_is_protected(vcpu->kvm))
+ return INVALID_GPA;
+
+ if (vcpu_mode_is_32bit(vcpu)) {
+ if (vcpu_has_nv(vcpu) &&
+ (__vcpu_sys_reg(vcpu, HCR_EL2) &
+ (HCR_EL2_VM | HCR_EL2_DC | HCR_EL2_TGE)))
+ return INVALID_GPA;
+ if (vcpu_read_sys_reg(vcpu, SCTLR_EL1) & SCTLR_ELx_M)
+ return INVALID_GPA;
+ return va & GENMASK_ULL(31, 0);
+ }
+
+ if (is_hyp_ctxt(vcpu))
+ wi.regime = vcpu_el2_e2h_is_set(vcpu) ? TR_EL20 : TR_EL2;
+ else
+ wi.regime = TR_EL10;
+ wi.as_el0 = !vcpu_mode_priv(vcpu);
+
+ if (__kvm_translate_va(vcpu, &wi, &wr, va))
+ return INVALID_GPA;
+ if (wi.s2)
+ return INVALID_GPA;
+ if (wi.as_el0 ? !wr.ux : !wr.px)
+ return INVALID_GPA;
+
+ return wr.pa;
+}
#endif
static void kvm_init_mpidr_data(struct kvm *kvm)
diff --git a/arch/arm64/kvm/at.c b/arch/arm64/kvm/at.c
index 30d8f062b2c2..f7cfa1793b16 100644
--- a/arch/arm64/kvm/at.c
+++ b/arch/arm64/kvm/at.c
@@ -274,10 +274,13 @@ static int setup_s1_walk(struct kvm_vcpu *vcpu, struct s1_walk_info *wi,
goto addrsz;
tbi = (wi->regime == TR_EL2 ?
- FIELD_GET(TCR_EL2_TBI, tcr) :
+ (FIELD_GET(TCR_EL2_TBI, tcr) &&
+ (!wi->insn_fetch || !FIELD_GET(TCR_EL2_TBID, tcr))) :
(va55 ?
- FIELD_GET(TCR_TBI1, tcr) :
- FIELD_GET(TCR_TBI0, tcr)));
+ (FIELD_GET(TCR_TBI1, tcr) &&
+ (!wi->insn_fetch || !(tcr & TCR_TBID1))) :
+ (FIELD_GET(TCR_TBI0, tcr) &&
+ (!wi->insn_fetch || !(tcr & TCR_TBID0)))));
if (!tbi && (u64)sign_extend64(va, 55) != va)
goto addrsz;
@@ -433,7 +436,10 @@ static int kvm_read_s1_desc(struct kvm_vcpu *vcpu, u64 pa, u64 *desc,
u64 val;
int r;
- r = kvm_read_guest(vcpu->kvm, pa, &val, sizeof(val));
+ if (wi->nofault)
+ r = kvm_vcpu_read_guest_nofault(vcpu, pa, &val, sizeof(val));
+ else
+ r = kvm_read_guest(vcpu->kvm, pa, &val, sizeof(val));
if (r)
return r;
@@ -481,6 +487,9 @@ static int walk_s1(struct kvm_vcpu *vcpu, struct s1_walk_info *wi,
ipa = baddr | index;
if (wi->s2) {
+ if (wi->nofault)
+ return -EOPNOTSUPP;
+
ret = kvm_walk_nested_s2(vcpu, ipa, &s2_trans);
if (ret == -EAGAIN)
return ret;
@@ -580,17 +589,19 @@ static int walk_s1(struct kvm_vcpu *vcpu, struct s1_walk_info *wi,
new_desc |= PTE_AF;
if (new_desc != desc) {
- if (wi->s2 && !kvm_s2_trans_writable(&s2_trans)) {
- fail_s1_walk(wr, ESR_ELx_FSC_PERM_L(level), true);
- return -EPERM;
- }
+ if (!wi->nofault) {
+ if (wi->s2 && !kvm_s2_trans_writable(&s2_trans)) {
+ fail_s1_walk(wr, ESR_ELx_FSC_PERM_L(level), true);
+ return -EPERM;
+ }
- ret = kvm_swap_s1_desc(vcpu, ipa, desc, new_desc, wi);
- if (ret == -EAGAIN)
- return ret;
- if (ret) {
- fail_s1_walk(wr, ESR_ELx_FSC_SEA_TTW(level), false);
- return ret;
+ ret = kvm_swap_s1_desc(vcpu, ipa, desc, new_desc, wi);
+ if (ret == -EAGAIN)
+ return ret;
+ if (ret) {
+ fail_s1_walk(wr, ESR_ELx_FSC_SEA_TTW(level), false);
+ return ret;
+ }
}
desc = new_desc;
--
2.56.1.915.g69d5ec7298-goog