Linux Perf Users
 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 07/16] KVM: loongarch: Implement kvm_arch_vcpu_get_ip_gpa()
Date: Tue,  1 Dec 2026 15:21:07 -0800	[thread overview]
Message-ID: <20261201232116.1988791-8-irogers@google.com> (raw)
In-Reply-To: <20261201232116.1988791-1-irogers@google.com>

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

Handle direct-addressing mode, DMWIN0/DMWIN1 instruction fetch windows,
and PWCTL0/PWCTL1 multi-level page-table walks (up to 4 levels plus huge
pages) with execute and PLV/RPLV permission checks using
kvm_vcpu_read_guest_nofault().

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

diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
index 76afd386abd2..32985380a636 100644
--- a/arch/loongarch/kvm/vcpu.c
+++ b/arch/loongarch/kvm/vcpu.c
@@ -4,9 +4,12 @@
  */
 
 #include <linux/kvm_host.h>
+#include <asm/addrspace.h>
 #include <asm/fpu.h>
+#include <asm/kvm_csr.h>
 #include <asm/lbt.h>
 #include <asm/loongarch.h>
+#include <asm/pgtable-bits.h>
 #include <asm/setup.h>
 #include <asm/time.h>
 #include <asm/timex.h>
@@ -396,6 +399,107 @@ unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
 	return vcpu->arch.pc;
 }
 
+static bool kvm_loongarch_pte_exec_allowed(u64 entry, unsigned long plv)
+{
+	unsigned long entry_plv = (entry & _PAGE_PLV) >> _PAGE_PLV_SHIFT;
+
+	if (!(entry & _PAGE_VALID) || (entry & _PAGE_NO_EXEC))
+		return false;
+	if (plv > entry_plv)
+		return false;
+	if ((entry & _PAGE_RPLV) && plv != entry_plv)
+		return false;
+	return true;
+}
+
+gpa_t kvm_arch_vcpu_get_ip_gpa(struct kvm_vcpu *vcpu)
+{
+	unsigned long gva = vcpu->arch.pc;
+	unsigned long crmd, plv, dmw0, dmw1, pgd, pwctl0, pwctl1;
+	struct {
+		unsigned int base;
+		unsigned int width;
+	} dirs[4];
+	unsigned int ptbase, ptwidth;
+	u64 entry;
+	gpa_t table_gpa;
+	int i;
+
+	preempt_disable();
+	crmd = read_gcsr_crmd();
+	dmw0 = read_gcsr_directwin0();
+	dmw1 = read_gcsr_directwin1();
+	pgd = (gva & BIT_ULL(63)) ? read_gcsr_pgdh() : read_gcsr_pgdl();
+	pwctl0 = read_gcsr_pwctl0();
+	pwctl1 = read_gcsr_pwctl1();
+	preempt_enable();
+
+	if ((crmd & CSR_CRMD_DA) && !(crmd & CSR_CRMD_PG))
+		return gva & TO_PHYS_MASK;
+
+	if (!(crmd & CSR_CRMD_PG))
+		return INVALID_GPA;
+
+	plv = crmd & CSR_CRMD_PLV;
+	if (((dmw0 & BIT(plv)) && ((dmw0 ^ gva) >> 60) == 0) ||
+	    ((dmw1 & BIT(plv)) && ((dmw1 ^ gva) >> 60) == 0))
+		return gva & TO_PHYS_MASK;
+
+	dirs[0].base = (pwctl1 >> CSR_PWCTL1_DIR3BASE_SHIFT) & 0x3f;
+	dirs[0].width = (pwctl1 >> CSR_PWCTL1_DIR3WIDTH_SHIFT) & 0x1f;
+	dirs[1].base = (pwctl1 >> CSR_PWCTL1_DIR2BASE_SHIFT) & 0x3f;
+	dirs[1].width = (pwctl1 >> CSR_PWCTL1_DIR2WIDTH_SHIFT) & 0x1f;
+	dirs[2].base = (pwctl0 >> CSR_PWCTL0_DIR1BASE_SHIFT) & 0x1f;
+	dirs[2].width = (pwctl0 >> CSR_PWCTL0_DIR1WIDTH_SHIFT) & 0x1f;
+	dirs[3].base = (pwctl0 >> CSR_PWCTL0_DIR0BASE_SHIFT) & 0x1f;
+	dirs[3].width = (pwctl0 >> CSR_PWCTL0_DIR0WIDTH_SHIFT) & 0x1f;
+	ptbase = (pwctl0 >> CSR_PWCTL0_PTBASE_SHIFT) & 0x1f;
+	ptwidth = (pwctl0 >> CSR_PWCTL0_PTWIDTH_SHIFT) & 0x1f;
+
+	if (ptbase < 12 || ptbase > 16 || !ptwidth || ptbase + ptwidth > 48)
+		return INVALID_GPA;
+
+	table_gpa = pgd & GENMASK_ULL(47, 12);
+	for (i = 0; i < ARRAY_SIZE(dirs); i++) {
+		unsigned long idx;
+
+		if (!dirs[i].width)
+			continue;
+		if (dirs[i].base < ptbase + ptwidth ||
+		    dirs[i].base + dirs[i].width > 63)
+			return INVALID_GPA;
+
+		idx = (gva >> dirs[i].base) & ((1UL << dirs[i].width) - 1);
+		if (kvm_vcpu_read_guest_nofault(vcpu,
+						table_gpa + idx * sizeof(u64),
+						&entry, sizeof(entry)))
+			return INVALID_GPA;
+
+		if (entry & _PAGE_HUGE) {
+			if (dirs[i].base >= 48 ||
+			    !kvm_loongarch_pte_exec_allowed(entry, plv))
+				return INVALID_GPA;
+			return (entry & GENMASK_ULL(47, dirs[i].base)) |
+			       (gva & GENMASK_ULL(dirs[i].base - 1, 0));
+		}
+
+		table_gpa = entry & GENMASK_ULL(47, 12);
+	}
+
+	if (kvm_vcpu_read_guest_nofault(vcpu,
+					table_gpa +
+					(((gva >> ptbase) & ((1UL << ptwidth) - 1)) *
+					 sizeof(u64)),
+					&entry, sizeof(entry)))
+		return INVALID_GPA;
+
+	if (!kvm_loongarch_pte_exec_allowed(entry, plv))
+		return INVALID_GPA;
+
+	return (entry & GENMASK_ULL(47, ptbase)) |
+	       (gva & GENMASK_ULL(ptbase - 1, 0));
+}
+
 /*
  * Returns true if a Performance Monitoring Interrupt (PMI), a.k.a. perf event,
  * arrived in guest context.  For LoongArch64, if PMU is not passthrough to VM,
-- 
2.56.1.915.g69d5ec7298-goog


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

Thread overview: 24+ 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:40   ` sashiko-bot
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:41   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 06/16] KVM: riscv: " Ian Rogers
2026-12-01 23:21 ` Ian Rogers [this message]
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:36   ` sashiko-bot
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:41   ` sashiko-bot
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:43   ` sashiko-bot
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
2026-12-01 23:40   ` sashiko-bot
2026-12-01 23:43 ` [RFC PATCH 00/16] perf, kvm: Add PERF_SAMPLE_HOST_IP for host-side guest symbolization sashiko-bot

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-8-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