From patchwork Wed Nov 17 15:38:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Elisei X-Patchwork-Id: 12692949 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49DE0C433EF for ; Wed, 17 Nov 2021 15:48:14 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 146AD613A9 for ; Wed, 17 Nov 2021 15:48:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 146AD613A9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=bTI0ZdUxX7+maM2b+goTO3QYq+zXC9u6EfVTpzzdCU0=; b=F0KW8BE/NO8CFS RNoIKuMFz9+DQtW128d9Q01dIlQgxwWVJ/EQ+Yw4hQ+pBizooZ7F2BrzT8MQdxQKB7gHElqrq/gJt PFaYYHe+DPefBYsM3AkHkyCo/intGn2upEjydyQe2YqdiyHGB+z6Pj9YC8Y/0pjGax0Qjv5vI75du 0qUyn1B6bSzIFWHLJ6C/TqYJem4a1dtVJpemFRtOOlB3wbFzCC8UaG97IIzs4K5aNl4or1gTnNCJn DvWW4nYzlcs1C62oxj1AkWcsNwElkDSQEVqKRmH9owtKRBOS42/PR12wnF8oMRtoc6weMHHZkpHLo j/l8n4wP0DGw2hRfdQFg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mnN8e-005UOE-Nv; Wed, 17 Nov 2021 15:46:21 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mnN07-005Q5e-QM for linux-arm-kernel@lists.infradead.org; Wed, 17 Nov 2021 15:37:33 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 43BC4106F; Wed, 17 Nov 2021 07:37:31 -0800 (PST) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id EAB603F5A1; Wed, 17 Nov 2021 07:37:29 -0800 (PST) From: Alexandru Elisei To: maz@kernel.org, james.morse@arm.com, suzuki.poulose@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, will@kernel.org, mark.rutland@arm.com Subject: [RFC PATCH v5 17/38] KVM: arm64: Allow userspace to set the SPE feature only if SPE is present Date: Wed, 17 Nov 2021 15:38:21 +0000 Message-Id: <20211117153842.302159-18-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211117153842.302159-1-alexandru.elisei@arm.com> References: <20211117153842.302159-1-alexandru.elisei@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211117_073732_005381_7002BA8E X-CRM114-Status: GOOD ( 13.88 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Check that KVM SPE emulation is supported before allowing userspace to set the KVM_ARM_VCPU_SPE feature. According to ARM DDI 0487G.a, page D9-2946 the Profiling Buffer is disabled if the owning Exception level is 32 bit, reject the SPE feature if the VCPU's execution state at EL1 is aarch32. Signed-off-by: Alexandru Elisei --- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/include/asm/kvm_spe.h | 7 +++++++ arch/arm64/kvm/reset.c | 8 ++++++++ arch/arm64/kvm/spe.c | 16 ++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 0ebdef158020..8b3faed48914 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -815,6 +815,9 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu); #define kvm_vcpu_has_pmu(vcpu) \ (test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features)) +#define kvm_vcpu_has_spe(vcpu) \ + (test_bit(KVM_ARM_VCPU_SPE, (vcpu)->arch.features)) + int kvm_trng_call(struct kvm_vcpu *vcpu); #ifdef CONFIG_KVM extern phys_addr_t hyp_mem_base; diff --git a/arch/arm64/include/asm/kvm_spe.h b/arch/arm64/include/asm/kvm_spe.h index 8f8b7dd7fd90..d33a46a74f78 100644 --- a/arch/arm64/include/asm/kvm_spe.h +++ b/arch/arm64/include/asm/kvm_spe.h @@ -13,8 +13,15 @@ static __always_inline bool kvm_supports_spe(void) { return static_branch_likely(&kvm_spe_available); } + +int kvm_spe_vcpu_enable_spe(struct kvm_vcpu *vcpu); #else #define kvm_supports_spe() (false) + +static inline int kvm_spe_vcpu_enable_spe(struct kvm_vcpu *vcpu) +{ + return 0; +} #endif /* CONFIG_KVM_ARM_SPE */ #endif /* __ARM64_KVM_SPE_H__ */ diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index 426bd7fbc3fd..8198156978bc 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -27,6 +27,7 @@ #include #include #include +#include #include /* Maximum phys_shift supported for any VM on this host */ @@ -251,6 +252,13 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu) goto out; } + if (kvm_vcpu_has_spe(vcpu)) { + if (kvm_spe_vcpu_enable_spe(vcpu)) { + ret = -EINVAL; + goto out; + } + } + switch (vcpu->arch.target) { default: if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) { diff --git a/arch/arm64/kvm/spe.c b/arch/arm64/kvm/spe.c index 6cd0e24ddeec..7c6f94358cc1 100644 --- a/arch/arm64/kvm/spe.c +++ b/arch/arm64/kvm/spe.c @@ -7,6 +7,7 @@ #include #include +#include #include DEFINE_STATIC_KEY_FALSE(kvm_spe_available); @@ -26,3 +27,18 @@ void kvm_host_spe_init(struct arm_spe_pmu *spe_pmu) mutex_unlock(&supported_cpus_lock); } + +int kvm_spe_vcpu_enable_spe(struct kvm_vcpu *vcpu) +{ + if (!kvm_supports_spe()) + return -EINVAL; + + /* + * The Profiling Buffer is disabled if the owning Exception level is + * aarch32. + */ + if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_EL1_32BIT)) + return -EINVAL; + + return 0; +}