mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	 Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: Marc Zyngier <maz@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	linux-perf-users@vger.kernel.org,  kvm@vger.kernel.org,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Ian Rogers <irogers@google.com>
Subject: [RFC PATCH 06/16] KVM: riscv: Implement kvm_arch_vcpu_get_ip_gpa()
Date: Tue,  1 Dec 2026 15:21:06 -0800	[thread overview]
Message-ID: <20261201232116.1988791-7-irogers@google.com> (raw)
In-Reply-To: <20261201232116.1988791-1-irogers@google.com>

Implement kvm_arch_vcpu_get_ip_gpa() on RISC-V so PERF_SAMPLE_HOST_IP
translates the guest PC to a guest physical address using non-faulting
guest memory reads.

Read live CSR_VSATP when the vCPU is loaded on the current CPU (via NACL
shmem or hardware CSR, falling back to saved guest_csr.vsatp otherwise)
and walk Sv32, Sv39, Sv48, or Sv57 guest page tables (including Svnapot
64KB pages) with execute and U/S privilege checks using
kvm_vcpu_read_guest_nofault().

Signed-off-by: Ian Rogers <irogers@google.com>
---
 arch/riscv/kvm/vcpu.c | 103 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 103 insertions(+)

diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index 570b6864e94c..fe9ceec6a7c1 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -17,9 +17,11 @@
 #include <linux/fs.h>
 #include <linux/kvm_host.h>
 #include <asm/cacheflush.h>
+#include <asm/csr.h>
 #include <asm/kvm_mmu.h>
 #include <asm/kvm_nacl.h>
 #include <asm/kvm_vcpu_vector.h>
+#include <asm/pgtable.h>
 
 #define CREATE_TRACE_POINTS
 #include "trace.h"
@@ -249,6 +251,107 @@ unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
 {
 	return vcpu->arch.guest_context.sepc;
 }
+
+gpa_t kvm_arch_vcpu_get_ip_gpa(struct kvm_vcpu *vcpu)
+{
+	unsigned long gva = vcpu->arch.guest_context.sepc;
+	unsigned long vsatp, mode, ppn, pte;
+	unsigned int levels, vpn_bits;
+	int level;
+
+	if (vcpu->cpu == raw_smp_processor_id()) {
+		if (kvm_riscv_nacl_available())
+			vsatp = nacl_csr_read(nacl_shmem(), CSR_VSATP);
+		else
+			vsatp = csr_read(CSR_VSATP);
+	} else {
+		vsatp = vcpu->arch.guest_csr.vsatp;
+	}
+
+	mode = vsatp >> SATP_MODE_SHIFT;
+	if (mode == 0)
+		return (gpa_t)gva;
+
+#ifdef CONFIG_64BIT
+	switch (vsatp & (0xfUL << SATP_MODE_SHIFT)) {
+	case SATP_MODE_39:
+		levels = 3;
+		if ((unsigned long)sign_extend64(gva, 38) != gva)
+			return INVALID_GPA;
+		break;
+	case SATP_MODE_48:
+		levels = 4;
+		if ((unsigned long)sign_extend64(gva, 47) != gva)
+			return INVALID_GPA;
+		break;
+	case SATP_MODE_57:
+		levels = 5;
+		if ((unsigned long)sign_extend64(gva, 56) != gva)
+			return INVALID_GPA;
+		break;
+	default:
+		return INVALID_GPA;
+	}
+	vpn_bits = 9;
+#else
+	if ((vsatp & SATP_MODE_32) == 0)
+		return INVALID_GPA;
+	levels = 2;
+	vpn_bits = 10;
+#endif
+
+	ppn = vsatp & SATP_PPN;
+	for (level = levels - 1; level >= 0; level--) {
+		unsigned int shift = PAGE_SHIFT + level * vpn_bits;
+		unsigned long vpn = (gva >> shift) & ((1UL << vpn_bits) - 1);
+		gpa_t pte_gpa = ((gpa_t)ppn << PAGE_SHIFT) +
+				vpn * sizeof(unsigned long);
+
+		if (kvm_vcpu_read_guest_nofault(vcpu, pte_gpa, &pte, sizeof(pte)))
+			return INVALID_GPA;
+
+		if (!(pte & _PAGE_PRESENT))
+			return INVALID_GPA;
+		if ((pte & (_PAGE_READ | _PAGE_WRITE)) == _PAGE_WRITE)
+			return INVALID_GPA;
+
+#ifdef CONFIG_64BIT
+		ppn = (pte & GENMASK_ULL(53, 10)) >> _PAGE_PFN_SHIFT;
+#else
+		ppn = pte >> _PAGE_PFN_SHIFT;
+#endif
+
+		if (pte & (_PAGE_READ | _PAGE_WRITE | _PAGE_EXEC)) {
+			if (!(pte & _PAGE_EXEC))
+				return INVALID_GPA;
+			if (kvm_arch_vcpu_in_kernel(vcpu) ?
+			    (pte & _PAGE_USER) : !(pte & _PAGE_USER))
+				return INVALID_GPA;
+			if (level > 0 && (ppn & ((1UL << (level * vpn_bits)) - 1)))
+				return INVALID_GPA;
+#ifdef CONFIG_64BIT
+			if (pte & _PAGE_NAPOT) {
+				if (!riscv_isa_extension_available(vcpu->arch.isa, SVNAPOT) ||
+				    level != 0 ||
+				    (ppn & ((1UL << NAPOT_CONT64KB_ORDER) - 1)) !=
+				    BIT(NAPOT_CONT64KB_ORDER - 1))
+					return INVALID_GPA;
+				ppn &= ~((1UL << NAPOT_CONT64KB_ORDER) - 1);
+				return ((gpa_t)ppn << PAGE_SHIFT) |
+				       (gva & (napot_cont_size(NAPOT_CONT64KB_ORDER) - 1));
+			}
+#endif
+			return ((gpa_t)ppn << PAGE_SHIFT) |
+			       (gva & ((1ULL << shift) - 1));
+		}
+#ifdef CONFIG_64BIT
+		if (pte & _PAGE_NAPOT)
+			return INVALID_GPA;
+#endif
+	}
+
+	return INVALID_GPA;
+}
 #endif
 
 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
-- 
2.56.1.915.g69d5ec7298-goog


  parent reply	other threads:[~2026-12-01 23:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 23:21 [RFC PATCH 00/16] perf, kvm: Add PERF_SAMPLE_HOST_IP for host-side guest symbolization Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 01/16] perf/core: Add PERF_SAMPLE_HOST_IP sample format Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 02/16] perf/core, bpf: Support BPF filtering of mmap_data events Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 03/16] KVM: Add NMI-safe guest-to-host IP translation infrastructure Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 04/16] KVM: x86: Implement kvm_arch_vcpu_get_ip_gpa() Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 05/16] KVM: arm64: " Ian Rogers
2026-12-01 23:21 ` Ian Rogers [this message]
2026-12-01 23:21 ` [RFC PATCH 07/16] KVM: loongarch: " Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 08/16] tools headers UAPI: Sync linux/perf_event.h with the kernel sources Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 09/16] perf tools: Add PERF_SAMPLE_HOST_IP parsing, synthesis, and record --host-ip Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 10/16] perf symbol: Support host IP resolution and guest fallback symbolization Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 11/16] perf report, script: Add host_ip, host_dso, and host_sym fields Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 12/16] perf record: Synthesize hypervisor file-backed data mappings for --host-ip Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 13/16] perf record: Filter runtime hypervisor data mmaps with BPF " Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 14/16] perf bpf-filter: Support host_ip in BPF sample filters Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 15/16] perf aslr: Anonymize sampled host_ip values Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 16/16] perf test: Add PERF_SAMPLE_HOST_IP symbolization, sorting, and ASLR tests Ian Rogers

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=20261201232116.1988791-7-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.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®