linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Oliver Upton <oupton@google.com>
To: Reiji Watanabe <reijiw@google.com>, h@google.com
Cc: Marc Zyngier <maz@kernel.org>,
	kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	James Morse <james.morse@arm.com>,
	Alexandru Elisei <alexandru.elisei@arm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Will Deacon <will@kernel.org>, Andrew Jones <drjones@redhat.com>,
	Fuad Tabba <tabba@google.com>,
	Peng Liang <liangpeng10@huawei.com>,
	Peter Shier <pshier@google.com>,
	Ricardo Koller <ricarkol@google.com>,
	Jing Zhang <jingzhangos@google.com>,
	Raghavendra Rao Anata <rananta@google.com>
Subject: Re: [PATCH v7 01/38] KVM: arm64: Introduce a validation function for an ID register
Date: Wed, 4 May 2022 06:35:29 +0000	[thread overview]
Message-ID: <YnIesawWNhBwZydM@google.com> (raw)
In-Reply-To: <20220419065544.3616948-2-reijiw@google.com>

On Mon, Apr 18, 2022 at 11:55:07PM -0700, Reiji Watanabe wrote:
> Introduce arm64_check_features(), which does a basic validity checking
> of an ID register value against the register's limit value, which is
> generally the host's sanitized value.
> 
> This function will be used by the following patches to check if an ID
> register value that userspace tries to set for a guest can be supported
> on the host.
> 
> Signed-off-by: Reiji Watanabe <reijiw@google.com>
> ---
>  arch/arm64/include/asm/cpufeature.h |  1 +
>  arch/arm64/kernel/cpufeature.c      | 52 +++++++++++++++++++++++++++++
>  2 files changed, 53 insertions(+)
> 
> diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h
> index c62e7e5e2f0c..7a009d4e18a6 100644
> --- a/arch/arm64/include/asm/cpufeature.h
> +++ b/arch/arm64/include/asm/cpufeature.h
> @@ -634,6 +634,7 @@ void check_local_cpu_capabilities(void);
>  
>  u64 read_sanitised_ftr_reg(u32 id);
>  u64 __read_sysreg_by_encoding(u32 sys_id);
> +int arm64_check_features(const struct arm64_ftr_bits *ftrp, u64 val, u64 limit);
>  
>  static inline bool cpu_supports_mixed_endian_el0(void)
>  {
> diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c
> index d72c4b4d389c..dbbc69745f22 100644
> --- a/arch/arm64/kernel/cpufeature.c
> +++ b/arch/arm64/kernel/cpufeature.c
> @@ -3239,3 +3239,55 @@ ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr,
>  		return sprintf(buf, "Vulnerable\n");
>  	}
>  }
> +
> +/**
> + * arm64_check_features() - Check if a feature register value constitutes
> + * a subset of features indicated by @limit.
> + *
> + * @ftrp: Pointer to an array of arm64_ftr_bits. It must be terminated by
> + * an item whose width field is zero.
> + * @val: The feature register value to check
> + * @limit: The limit value of the feature register
> + *
> + * This function will check if each feature field of @val is the "safe" value
> + * against @limit based on @ftrp[], each of which specifies the target field
> + * (shift, width), whether or not the field is for a signed value (sign),
> + * how the field is determined to be "safe" (type), and the safe value
> + * (safe_val) when type == FTR_EXACT (safe_val won't be used by this
> + * function when type != FTR_EXACT). Any other fields in arm64_ftr_bits
> + * won't be used by this function. If a field value in @val is the same
> + * as the one in @limit, it is always considered the safe value regardless
> + * of the type. For register fields that are not in @ftrp[], only the value
> + * in @limit is considered the safe value.
> + *
> + * Return: 0 if all the fields are safe. Otherwise, return negative errno.
> + */
> +int arm64_check_features(const struct arm64_ftr_bits *ftrp, u64 val, u64 limit)
> +{
> +	u64 mask = 0;
> +
> +	for (; ftrp->width; ftrp++) {
> +		s64 f_val, f_lim, safe_val;
> +
> +		f_val = arm64_ftr_value(ftrp, val);
> +		f_lim = arm64_ftr_value(ftrp, limit);
> +		mask |= arm64_ftr_mask(ftrp);
> +
> +		if (f_val == f_lim)
> +			safe_val = f_val;
> +		else
> +			safe_val = arm64_ftr_safe_value(ftrp, f_val, f_lim);
> +
> +		if (safe_val != f_val)
> +			return -E2BIG;
> +	}
> +
> +	/*
> +	 * For fields that are not indicated in ftrp, values in limit are the
> +	 * safe values.
> +	 */
> +	if ((val & ~mask) != (limit & ~mask))
> +		return -E2BIG;

This bit is interesting. Apologies if I paged out relevant context. What
features are we trying to limit that exist outside of an arm64_ftr_bits
definition? I'll follow the series and see if I figure out later :-P

Generally speaking, though, it seems to me that we'd prefer to have an
arm64_ftr_bits struct plumbed up for whatever hits this case.

--
Thanks,
Oliver

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  reply	other threads:[~2022-05-04  6:36 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-19  6:55 [PATCH v7 00/38] KVM: arm64: Make CPU ID registers writable by userspace Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 01/38] KVM: arm64: Introduce a validation function for an ID register Reiji Watanabe
2022-05-04  6:35   ` Oliver Upton [this message]
2022-06-01  6:16     ` Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 02/38] KVM: arm64: Save ID registers' sanitized value per guest Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 03/38] KVM: arm64: Introduce struct id_reg_desc Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 04/38] KVM: arm64: Generate id_reg_desc's ftr_bits at KVM init when needed Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 05/38] KVM: arm64: Prohibit modifying values of ID regs for 32bit EL1 guests Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 06/38] KVM: arm64: Make ID_AA64PFR0_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 07/38] KVM: arm64: Make ID_AA64PFR1_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 08/38] KVM: arm64: Make ID_AA64ISAR0_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 09/38] KVM: arm64: Make ID_AA64ISAR1_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 10/38] KVM: arm64: Make ID_AA64ISAR2_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 11/38] KVM: arm64: Make ID_AA64MMFR0_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 12/38] KVM: arm64: Add a KVM flag indicating emulating debug regs access is needed Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 13/38] KVM: arm64: Emulate dbgbcr/dbgbvr accesses Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 14/38] KVM: arm64: Emulate dbgwcr accesses Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 15/38] KVM: arm64: Make ID_AA64DFR0_EL1/ID_DFR0_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 16/38] KVM: arm64: KVM: arm64: Make ID_DFR1_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 17/38] KVM: arm64: KVM: arm64: Make ID_MMFR0_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 18/38] KVM: arm64: Make MVFR1_EL1 writable Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 19/38] KVM: arm64: Add remaining ID registers to id_reg_desc_table Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 20/38] KVM: arm64: Use id_reg_desc_table for ID registers Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 21/38] KVM: arm64: Add consistency checking for frac fields of " Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 22/38] KVM: arm64: Introduce KVM_CAP_ARM_ID_REG_CONFIGURABLE capability Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 23/38] KVM: arm64: Add kunit test for ID register validation Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 24/38] KVM: arm64: Use vcpu->arch cptr_el2 to track value of cptr_el2 for VHE Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 25/38] KVM: arm64: Use vcpu->arch.mdcr_el2 to track value of mdcr_el2 Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 26/38] KVM: arm64: Introduce framework to trap disabled features Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 27/38] KVM: arm64: Trap disabled features of ID_AA64PFR0_EL1 Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 28/38] KVM: arm64: Trap disabled features of ID_AA64PFR1_EL1 Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 29/38] KVM: arm64: Trap disabled features of ID_AA64DFR0_EL1 Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 30/38] KVM: arm64: Trap disabled features of ID_AA64MMFR1_EL1 Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 31/38] KVM: arm64: Trap disabled features of ID_AA64ISAR1_EL1 Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 32/38] KVM: arm64: Add kunit test for trap initialization Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 33/38] KVM: arm64: selftests: Add helpers to extract a field of ID registers Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 34/38] KVM: arm64: selftests: Introduce id_reg_test Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 35/38] KVM: arm64: selftests: Test linked breakpoint and watchpoint Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 36/38] KVM: arm64: selftests: Test breakpoint/watchpoint register access Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 37/38] KVM: arm64: selftests: Test with every breakpoint/watchpoint Reiji Watanabe
2022-04-19  6:55 ` [PATCH v7 38/38] KVM: arm64: selftests: Test breakpoint/watchpoint changing ID_AA64DFR0_EL1 Reiji Watanabe

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=YnIesawWNhBwZydM@google.com \
    --to=oupton@google.com \
    --cc=alexandru.elisei@arm.com \
    --cc=drjones@redhat.com \
    --cc=h@google.com \
    --cc=james.morse@arm.com \
    --cc=jingzhangos@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.cs.columbia.edu \
    --cc=liangpeng10@huawei.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=maz@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=pshier@google.com \
    --cc=rananta@google.com \
    --cc=reijiw@google.com \
    --cc=ricarkol@google.com \
    --cc=suzuki.poulose@arm.com \
    --cc=tabba@google.com \
    --cc=will@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).