All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexandru Elisei <alexandru.elisei@arm.com>
To: James Clark <james.clark@linaro.org>
Cc: mark.rutland@arm.com, james.morse@arm.com, maz@kernel.org,
	oliver.upton@linux.dev, joey.gouly@arm.com,
	suzuki.poulose@arm.com, yuzenghui@huawei.com, will@kernel.org,
	catalin.marinas@arm.com, linux-arm-kernel@lists.infradead.org,
	kvmarm@lists.linux.dev
Subject: Re: [RFC PATCH v6 19/35] KVM: arm64: Trap PMBIDR_EL1 and PMSIDR_EL1
Date: Mon, 12 Jan 2026 11:28:47 +0000	[thread overview]
Message-ID: <aWTa76KXpIwzPh4k@raptor> (raw)
In-Reply-To: <f7d73dc3-aa4a-4c32-aabb-21a61a2f773f@linaro.org>

Hi James,

On Fri, Jan 09, 2026 at 04:29:37PM +0000, James Clark wrote:
> 
> 
> On 14/11/2025 4:07 pm, Alexandru Elisei wrote:
> > PMBIDR_EL1 and PMSIDR_EL1 are read-only registers that describe the SPE
> > implementation. Trap reads to allow KVM to control how SPE is described to
> > a virtual machine. In particular, this is needed to:
> > 
> > - Advertise the maximum buffer size set by userspace in
> >    PMBIDR_EL1.MaxBuffSize.
> > - Hide in PMSIDR_EL1, if necessary, the presence of FEAT_SPE_FDS and
> >    FEAT_SPE_FnE, both of which add new registers which are already
> >    trapped via the FGU mechanism.
> > 
> > Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
> > ---
> >   arch/arm64/include/asm/kvm_spe.h |  4 +-
> >   arch/arm64/kvm/config.c          | 13 ++++-
> >   arch/arm64/kvm/spe.c             | 82 +++++++++++++++++++++++++++++++-
> >   arch/arm64/kvm/sys_regs.c        | 13 +++--
> >   4 files changed, 103 insertions(+), 9 deletions(-)
> > 
> > diff --git a/arch/arm64/include/asm/kvm_spe.h b/arch/arm64/include/asm/kvm_spe.h
> > index 3506d8c4c661..47b94794cc5f 100644
> > --- a/arch/arm64/include/asm/kvm_spe.h
> > +++ b/arch/arm64/include/asm/kvm_spe.h
> > @@ -40,7 +40,7 @@ int kvm_spe_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
> >   int kvm_spe_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr);
> >   bool kvm_spe_write_sysreg(struct kvm_vcpu *vcpu, int reg, u64 val);
> > -u64 kvm_spe_read_sysreg(struct kvm_vcpu *vcpu, int reg);
> > +u64 kvm_spe_read_sysreg(struct kvm_vcpu *vcpu, int reg, u32 encoding);
> >   #else
> >   struct kvm_spe {
> >   };
> > @@ -78,7 +78,7 @@ static inline bool kvm_spe_write_sysreg(struct kvm_vcpu *vcpu, int reg, u64 val)
> >   {
> >   	return true;
> >   }
> > -static inline u64 kvm_spe_read_sysreg(struct kvm_vcpu *vcpu, int reg)
> > +static inline u64 kvm_spe_read_sysreg(struct kvm_vcpu *vcpu, int reg, u32 encoding)
> >   {
> >   	return 0;
> >   }
> > diff --git a/arch/arm64/kvm/config.c b/arch/arm64/kvm/config.c
> > index 24bb3f36e9d5..ed6b167b7aa8 100644
> > --- a/arch/arm64/kvm/config.c
> > +++ b/arch/arm64/kvm/config.c
> > @@ -6,6 +6,7 @@
> >   #include <linux/kvm_host.h>
> >   #include <asm/kvm_emulate.h>
> > +#include <asm/kvm_spe.h>
> >   #include <asm/kvm_nested.h>
> >   #include <asm/sysreg.h>
> > @@ -1489,6 +1490,16 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu)
> >   		*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
> >   }
> > +static void __compute_hdfgrtr(struct kvm_vcpu *vcpu)
> > +{
> > +	__compute_fgt(vcpu, HDFGRTR_EL2);
> > +
> > +	if (vcpu_has_spe(vcpu)) {
> > +		*vcpu_fgt(vcpu, HDFGRTR_EL2) |= HDFGRTR_EL2_PMBIDR_EL1;
> > +		*vcpu_fgt(vcpu, HDFGRTR_EL2) |= HDFGRTR_EL2_PMSIDR_EL1;
> > +	}
> > +}
> > +
> >   static void __compute_hdfgwtr(struct kvm_vcpu *vcpu)
> >   {
> >   	__compute_fgt(vcpu, HDFGWTR_EL2);
> > @@ -1505,7 +1516,7 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)
> >   	__compute_fgt(vcpu, HFGRTR_EL2);
> >   	__compute_hfgwtr(vcpu);
> >   	__compute_fgt(vcpu, HFGITR_EL2);
> > -	__compute_fgt(vcpu, HDFGRTR_EL2);
> > +	__compute_hdfgrtr(vcpu);
> >   	__compute_hdfgwtr(vcpu);
> >   	__compute_fgt(vcpu, HAFGRTR_EL2);
> > diff --git a/arch/arm64/kvm/spe.c b/arch/arm64/kvm/spe.c
> > index 5b3dc622cf82..92eb46276c71 100644
> > --- a/arch/arm64/kvm/spe.c
> > +++ b/arch/arm64/kvm/spe.c
> > @@ -22,10 +22,16 @@ struct arm_spu_entry {
> >   	struct arm_spe_pmu *arm_spu;
> >   };
> > +static u64 max_buffer_size_to_pmbidr_el1(u64 size);
> > +
> >   void kvm_host_spe_init(struct arm_spe_pmu *arm_spu)
> >   {
> >   	struct arm_spu_entry *entry;
> > +	/* PMBIDR_EL1 cannot be trapped without FEAT_FGT. */
> > +	if (!cpus_have_final_cap(ARM64_HAS_FGT))
> > +		return;
> > +
> 
> Isn't this only required if you want to report a different PMBIDR value? If
> the value in hardware is already what you want to give to guests then it's
> not strictly required. Maybe it's something we can do as an addition later
> to make it usable in more places.

The maximum buffer size is advertised in PMBIDR_EL1.

Thanks,
Alex

> 
> >   	/* TODO: pKVM and nVHE support */
> >   	if (is_protected_kvm_enabled() || !has_vhe())
> >   		return;
> > @@ -86,9 +92,81 @@ bool kvm_spe_write_sysreg(struct kvm_vcpu *vcpu, int reg, u64 val)
> >   	return true;
> >   }
> > -u64 kvm_spe_read_sysreg(struct kvm_vcpu *vcpu, int reg)
> > +static bool kvm_spe_has_feat_spe_fds(struct kvm *kvm)
> > +{
> > +	struct arm_spe_pmu *spe_pmu = kvm->arch.kvm_spe.arm_spu;
> > +
> > +	return kvm_has_feat(kvm, ID_AA64DFR0_EL1, PMSVer, V1P4) &&
> > +	       FIELD_GET(PMSIDR_EL1_FDS, spe_pmu->pmsidr_el1);
> > +}
> > +
> > +static bool kvm_spe_has_feat_spe_fne(struct kvm *kvm)
> > +{
> > +	struct arm_spe_pmu *spe_pmu = kvm->arch.kvm_spe.arm_spu;
> > +
> > +	return kvm_has_feat(kvm, ID_AA64DFR0_EL1, PMSVer, V1P2) &&
> > +	       FIELD_GET(PMSIDR_EL1_FnE, spe_pmu->pmsidr_el1);
> > +}
> > +
> > +static u64 kvm_spe_get_pmsidr_el1(struct kvm_vcpu *vcpu)
> >   {
> > -	return __vcpu_sys_reg(vcpu, reg);
> > +	struct kvm *kvm = vcpu->kvm;
> > +	u64 pmsidr = kvm->arch.kvm_spe.arm_spu->pmsidr_el1;
> > +
> > +	/* Filter out known RES0 bits. */
> > +	pmsidr &= ~GENMASK_ULL(63, 33);
> > +
> > +	if (!kvm_spe_has_feat_spe_fne(kvm))
> > +		pmsidr &= ~PMSIDR_EL1_FnE;
> > +
> > +	if (!kvm_spe_has_feat_spe_fds(kvm))
> > +		pmsidr &= ~PMSIDR_EL1_FDS;
> > +
> > +	return pmsidr;
> > +}
> > +
> > +static u64 kvm_spe_get_pmbidr_el1(struct kvm_vcpu *vcpu)
> > +{
> > +	struct kvm *kvm = vcpu->kvm;
> > +	struct kvm_spe *kvm_spe = &kvm->arch.kvm_spe;
> > +	u64 max_buffer_size = kvm_spe->max_buffer_size;
> > +	u64 pmbidr_el1 = kvm_spe->arm_spu->pmbidr_el1;
> > +
> > +	/* Filter out known RES0 bits. */
> > +	pmbidr_el1 &= ~(GENMASK_ULL(63, 48) | GENMASK_ULL(31, 12));
> > +
> > +	if (!kvm_has_feat(kvm, ID_AA64DFR2_EL1, SPE_nVM, IMP)) {
> > +		pmbidr_el1 &= ~PMBIDR_EL1_AddrMode;
> > +		pmbidr_el1 |= PMBIDR_EL1_AddrMode_VM_ONLY;
> > +	}
> > +
> > +	pmbidr_el1 &= ~PMBIDR_EL1_MaxBuffSize;
> > +	pmbidr_el1 |= max_buffer_size_to_pmbidr_el1(max_buffer_size);
> > +
> > +	return pmbidr_el1;
> > +}
> > +
> > +u64 kvm_spe_read_sysreg(struct kvm_vcpu *vcpu, int reg, u32 encoding)
> > +{
> > +	u64 val = 0;
> > +
> > +	switch (encoding) {
> > +	case SYS_PMBIDR_EL1:
> > +		val = kvm_spe_get_pmbidr_el1(vcpu);
> > +		break;
> > +	case SYS_PMSIDR_EL1:
> > +		val = kvm_spe_get_pmsidr_el1(vcpu);
> > +		break;
> > +	case SYS_PMBLIMITR_EL1:
> > +	case SYS_PMBPTR_EL1:
> > +	case SYS_PMBSR_EL1:
> > +		val = __vcpu_sys_reg(vcpu, reg);
> > +		break;
> > +	default:
> > +		WARN_ON_ONCE(1);
> > +	}
> > +
> > +	return val;
> >   }
> >   static u64 max_buffer_size_to_pmbidr_el1(u64 size)
> > diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
> > index 5eeea229b46e..db86d1dcd148 100644
> > --- a/arch/arm64/kvm/sys_regs.c
> > +++ b/arch/arm64/kvm/sys_regs.c
> > @@ -1386,13 +1386,18 @@ static unsigned int spe_visibility(const struct kvm_vcpu *vcpu,
> >   static bool access_spe_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
> >   			   const struct sys_reg_desc *r)
> >   {
> > +	u32 encoding = reg_to_encoding(r);
> >   	u64 val = p->regval;
> >   	int reg = r->reg;
> > -	if (p->is_write)
> > +	if (p->is_write) {
> > +		if (WARN_ON_ONCE(encoding == SYS_PMBIDR_EL1 ||
> > +				 encoding == SYS_PMSIDR_EL1))
> > +			return write_to_read_only(vcpu, p, r);
> >   		return kvm_spe_write_sysreg(vcpu, reg, val);
> > +	}
> > -	p->regval = kvm_spe_read_sysreg(vcpu, reg);
> > +	p->regval = kvm_spe_read_sysreg(vcpu, reg, encoding);
> >   	return true;
> >   }
> > @@ -3360,12 +3365,12 @@ static const struct sys_reg_desc sys_reg_descs[] = {
> >   	{ SPE_UNTRAPPED_REG(PMSFCR_EL1) },
> >   	{ SPE_UNTRAPPED_REG(PMSEVFR_EL1) },
> >   	{ SPE_UNTRAPPED_REG(PMSLATFR_EL1) },
> > -	{ SYS_DESC(SYS_PMSIDR_EL1), .access = undef_access },
> > +	{ SYS_DESC(SYS_PMSIDR_EL1), .access = access_spe_reg, .visibility = spe_visibility },
> >   	{ SPE_TRAPPED_REG(PMBLIMITR_EL1) },
> >   	{ SPE_TRAPPED_REG(PMBPTR_EL1) },
> >   	{ SPE_TRAPPED_REG(PMBSR_EL1) },
> >   	{ SPE_UNTRAPPED_REG(PMSDSFR_EL1) },
> > -	{ SYS_DESC(SYS_PMBIDR_EL1), .access = undef_access },
> > +	{ SYS_DESC(SYS_PMBIDR_EL1), .access = access_spe_reg, .visibility = spe_visibility },
> >   	{ PMU_SYS_REG(PMINTENSET_EL1),
> >   	  .access = access_pminten, .reg = PMINTENSET_EL1,
> 

  reply	other threads:[~2026-01-12 11:28 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-14 16:06 [RFC PATCH v6 00/35] KVM: arm64: Add Statistical Profiling Extension (SPE) support Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 01/35] arm64/sysreg: Add new SPE fields Alexandru Elisei
2025-12-10 18:38   ` Leo Yan
2025-12-12  9:39     ` Alexandru Elisei
2025-12-15 21:42   ` Suzuki K Poulose
2025-11-14 16:06 ` [RFC PATCH v6 02/35] arm64/sysreg: Define MDCR_EL2.E2PB values Alexandru Elisei
2025-12-15 21:33   ` Suzuki K Poulose
2025-11-14 16:06 ` [RFC PATCH v6 03/35] KVM: arm64: Add CONFIG_KVM_ARM_SPE Kconfig option Alexandru Elisei
2026-01-09 16:29   ` James Clark
2026-01-12 11:26     ` Alexandru Elisei
2026-01-12 12:09       ` James Clark
2026-01-12 12:14         ` James Clark
2026-01-12 15:18         ` Alexandru Elisei
2026-01-13 10:25           ` Alexandru Elisei
2026-01-13 15:00             ` James Clark
2026-01-13 17:03               ` Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 04/35] perf: arm_spe_pmu: Move struct arm_spe_pmu to a separate header file Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 05/35] KVM: arm64: Add KVM_CAP_ARM_SPE capability Alexandru Elisei
2025-12-14 12:18   ` Leo Yan
2025-12-15 11:46     ` Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 06/35] KVM: arm64: Add KVM_ARM_VCPU_SPE VCPU feature Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 07/35] HACK! KVM: arm64: Disable SPE virtualization if protected KVM is enabled Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 08/35] HACK! KVM: arm64: Enable SPE virtualization only in VHE mode Alexandru Elisei
2025-12-15 17:49   ` Leo Yan
2025-11-14 16:06 ` [RFC PATCH v6 09/35] HACK! KVM: arm64: Disable SPE virtualization if nested virt is enabled Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 10/35] KVM: arm64: Add a new VCPU device control group for SPE Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 11/35] KVM: arm64: Add SPE VCPU device attribute to set the interrupt number Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 12/35] KVM: arm64: Add SPE VCPU device attribute to set the SPU device Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 13/35] perf: arm_spe_pmu: Add PMBIDR_EL1 to struct arm_spe_pmu Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 14/35] KVM: arm64: Add SPE VCPU device attribute to set the max buffer size Alexandru Elisei
2026-01-09 16:29   ` James Clark
2026-01-12 11:28     ` Alexandru Elisei
2026-01-12 11:50       ` James Clark
2026-01-12 14:03         ` Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 15/35] KVM: arm64: Add SPE VCPU device attribute to initialize SPE Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 16/35] KVM: arm64: Advertise SPE version in ID_AA64DFR0_EL1.PMSver Alexandru Elisei
2025-12-16 11:40   ` Suzuki K Poulose
2026-01-05 16:42     ` Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 17/35] KVM: arm64: Add writable SPE system registers to VCPU context Alexandru Elisei
2025-12-16 11:54   ` Suzuki K Poulose
2026-01-05 16:42     ` Alexandru Elisei
2025-11-14 16:06 ` [RFC PATCH v6 18/35] perf: arm_spe_pmu: Add PMSIDR_EL1 to struct arm_spe_pmu Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 19/35] KVM: arm64: Trap PMBIDR_EL1 and PMSIDR_EL1 Alexandru Elisei
2026-01-09 16:29   ` James Clark
2026-01-12 11:28     ` Alexandru Elisei [this message]
2026-01-12 11:54       ` James Clark
2026-01-13 12:48         ` Alexandru Elisei
2026-01-13 14:22           ` James Clark
2025-11-14 16:07 ` [RFC PATCH v6 20/35] KVM: arm64: config: Use functions from spe.c to test FEAT_SPE_{FnE,FDS} Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 21/35] KVM: arm64: Check for unsupported CPU early in kvm_arch_vcpu_load() Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 22/35] KVM: arm64: VHE: Context switch SPE state Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 23/35] KVM: arm64: Allow guest SPE physical timestamps only if perfmon_capable() Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 24/35] KVM: arm64: Handle SPE hardware maintenance interrupts Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 25/35] KVM: arm64: Add basic handling of SPE buffer control registers writes Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 26/35] KVM: arm64: Add comment to explain how trapped SPE registers are handled Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 27/35] KVM: arm64: Make MTE functions public Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 28/35] KVM: arm64: at: Use callback for reading descriptor Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 29/35] KVM: arm64: Pin the SPE buffer in the host and map it at stage 2 Alexandru Elisei
2026-01-09 16:29   ` James Clark
2026-01-09 16:35     ` James Clark
2026-01-12 12:01     ` Alexandru Elisei
2026-01-13 14:18       ` James Clark
2025-11-14 16:07 ` [RFC PATCH v6 30/35] KVM: Propagate MMU event to the MMU notifier handlers Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 31/35] KVM: arm64: Handle MMU notifiers for the SPE buffer Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 32/35] KVM: Add KVM_EXIT_RLIMIT exit_reason Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 33/35] KVM: arm64: Implement locked memory accounting for the SPE buffer Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 34/35] KVM: arm64: Add hugetlb support for SPE Alexandru Elisei
2025-11-14 16:07 ` [RFC PATCH v6 35/35] KVM: arm64: Allow the creation of a SPE enabled VM Alexandru Elisei
2025-12-11 16:34 ` [RFC PATCH v6 00/35] KVM: arm64: Add Statistical Profiling Extension (SPE) support Leo Yan
2025-12-12 10:18   ` Alexandru Elisei
2025-12-12 11:15     ` Leo Yan
2025-12-12 11:54       ` Alexandru Elisei

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=aWTa76KXpIwzPh4k@raptor \
    --to=alexandru.elisei@arm.com \
    --cc=catalin.marinas@arm.com \
    --cc=james.clark@linaro.org \
    --cc=james.morse@arm.com \
    --cc=joey.gouly@arm.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=oliver.upton@linux.dev \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.