From patchwork Tue Jan 23 14:53:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13527724 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 09CA0C48277 for ; Tue, 23 Jan 2024 16:01:11 +0000 (UTC) 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:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=DSQevxT8m3YrmIsF83xN9jTER14uckwI2tVZ3ifl4uk=; b=p11iXTmy77RkMsQJbestrMrpjY lu2Q+RpMTOVgWAqq0RxH49hy7Q9ZwARitUtDo3N2NpWgTL85/AACaezD/ht9y8FohGApCu3trkqs+ w8sXivKYBuVhR/x3WFmGRtlDEoEn2df2k3plc3N+pCPMuiBNGzTN5D9MON3RySlBohVXkSloWu15l L46DAaTwF8AuRIF/fSP4Xu67Dp2oal6ALNHt+6etdSPYQMpSrHTfoGkJ+WAnXXsBn27h9DY5EkOJ6 smBs3lshnGNp/5JQCgVpbvhMUOL8kBjC0GpbI+ljtJGjMXZS005V42+iwIoLv8OkfbR+A6pxA8KJS p94wQI9g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rSJCT-00HAsc-1R; Tue, 23 Jan 2024 16:00:33 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rSIB5-00Gtt5-2J for linux-arm-kernel@lists.infradead.org; Tue, 23 Jan 2024 14:55:11 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-33941f7e299so461266f8f.0 for ; Tue, 23 Jan 2024 06:55:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1706021702; x=1706626502; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=oIPUbU2klustjscI8Xq9j+nMdsdXjLu5K5MyPJ3m0ro=; b=aEVOTkZs1cJWp+lvvls8wmzif5cuj2UhwPTJrwepgFpPZQt0KqvQpat5dW7pMr3IUH O1IolkfWFMCKGsI7IiYe7m8z0IW0eL+ltRP42pqWOOSWmAG0FZ06i5XkuIMlVYlppduz QV2nxdEl/Xwp1te4tCbIFpeUJ26trY0QJHq+5EH8hMvyNgEHrwAQo0Ppq5QlKo+g/Tq1 GzJfYAJa3UNO+igPD1EILFF7kshNBNtemXH5cJ4Cz0wgQdPntMQfhRSJ3r58Nh/g803v pWftPhgjUkZHv0GIUEU96/zYoVdIt5K9kNBtB407Jg+gsU3eYAlFvyw7AA1DwSMsk1Ft YAmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706021702; x=1706626502; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=oIPUbU2klustjscI8Xq9j+nMdsdXjLu5K5MyPJ3m0ro=; b=BLhJbGrVf45xIyF5zp73qPvPvUU7pP12iKn1FwpY/989q+4oYt11e+d+lynjfoBIsf IYG1dV4s2KAMJJCGhmk1nhqbgAdpQvir9sqipt796/vXZ5S+fLUN1HN7kGDO2WOMXNcP O7zFhsn3Vrvb5GTAzcEcDr6Xum/vAXeBm7mr01pkY4XcJJ7Q2NJQOOXsFbxYIcfZ2oKU iz6WedKkV79q7YgYriC+lyujrTHaUhjFAnmKtUkSetuWffLp5ZBqRWn+zxL3sQsJUVxv zf32BZUybLc6dGEoSFV/48BZAIzI8dBotbTlgG8HFrYa+M7QQ1m1cv5+D6v2B3Y86yfu QIMA== X-Gm-Message-State: AOJu0YwZa7a8GynlpNbPVdti7TF5u1yyHvVV+hJQH1Ud9yt1UN7yxONI om25oWOyPNVEHJabV3toZbPQceP7UyjD7YPHyCCmjb/JJMHT0x15WmUNn0Du/RQKy1TtW6cTa2H QwoPucI2+lIGelCzGoj3JhLSeKvnsxRLij6nBjKW6XVgYcnsYZZ6tY2EphA7Ez/HsZv7S5OKiAT YDriqyt2rAI98L9TW2/8cqBBq6GE9jr0Yc+addlKWi X-Google-Smtp-Source: AGHT+IE7+K1q3vhrlJEF4nBo7svGNHkHfViDbX5tuKaxhQAoHtyuJy8zRPF9kT5QKcCi8VmFtdN1zk7W X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a5d:6f0a:0:b0:339:3d4f:33d0 with SMTP id ay10-20020a5d6f0a000000b003393d4f33d0mr7246wrb.10.1706021701653; Tue, 23 Jan 2024 06:55:01 -0800 (PST) Date: Tue, 23 Jan 2024 15:53:16 +0100 In-Reply-To: <20240123145258.1462979-52-ardb+git@google.com> Mime-Version: 1.0 References: <20240123145258.1462979-52-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=3269; i=ardb@kernel.org; h=from:subject; bh=jjJW/+rY1cUPTnYpF7LVY6kB/ZlMNlalGuOVDcgSLzY=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIXX9pbsaFauXZSUdfTu3YbZ4UJz39D2JEvYbl1YKGopLb Qn6pXKqo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAExkjiojw0Sb6ILVzVXzd2ZL vLu2mzPK9JiRmN+pDyKzHER5mUU+vGH4Kymb9/D9hEkn3Jjt+FYs/bxR70HK5mzGvu8R/zunT1f RZAYA X-Mailer: git-send-email 2.43.0.429.g432eaa2c6b-goog Message-ID: <20240123145258.1462979-69-ardb+git@google.com> Subject: [PATCH v7 17/50] arm64: cpufeature: Add helper to test for CPU feature overrides From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Ryan Roberts , Anshuman Khandual , Kees Cook X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240123_065503_804768_6250149C X-CRM114-Status: GOOD ( 18.85 ) 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 From: Ard Biesheuvel Add some helpers to extract and apply feature overrides to the bare idreg values. This involves inspecting the value and mask of the specific field that we are interested in, given that an override value/mask pair might be invalid for one field but valid for another. Then, wire up the new helper for the hVHE test - note that we can drop the sysreg test here, as the override will be invalid when trying to enable hVHE on non-VHE capable hardware. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/cpufeature.h | 39 ++++++++++++++++++++ arch/arm64/kernel/cpufeature.c | 9 +---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/arch/arm64/include/asm/cpufeature.h b/arch/arm64/include/asm/cpufeature.h index 21c824edf8ce..acd8f4949583 100644 --- a/arch/arm64/include/asm/cpufeature.h +++ b/arch/arm64/include/asm/cpufeature.h @@ -915,6 +915,45 @@ extern struct arm64_ftr_override id_aa64isar2_override; extern struct arm64_ftr_override arm64_sw_feature_override; +static inline +u64 arm64_apply_feature_override(u64 val, int feat, int width, + const struct arm64_ftr_override *override) +{ + u64 oval = override->val; + + /* + * When it encounters an invalid override (e.g., an override that + * cannot be honoured due to a missing CPU feature), the early idreg + * override code will set the mask to 0x0 and the value to non-zero for + * the field in question. In order to determine whether the override is + * valid or not for the field we are interested in, we first need to + * disregard bits belonging to other fields. + */ + oval &= GENMASK_ULL(feat + width - 1, feat); + + /* + * The override is valid if all value bits are accounted for in the + * mask. If so, replace the masked bits with the override value. + */ + if (oval == (oval & override->mask)) { + val &= ~override->mask; + val |= oval; + } + + /* Extract the field from the updated value */ + return cpuid_feature_extract_unsigned_field(val, feat); +} + +static inline bool arm64_test_sw_feature_override(int feat) +{ + /* + * Software features are pseudo CPU features that have no underlying + * CPUID system register value to apply the override to. + */ + return arm64_apply_feature_override(0, feat, 4, + &arm64_sw_feature_override); +} + u32 get_kvm_ipa_limit(void); void dump_cpu_features(void); diff --git a/arch/arm64/kernel/cpufeature.c b/arch/arm64/kernel/cpufeature.c index a99ad79adee2..d0ffb872a31a 100644 --- a/arch/arm64/kernel/cpufeature.c +++ b/arch/arm64/kernel/cpufeature.c @@ -2042,14 +2042,7 @@ static bool has_nested_virt_support(const struct arm64_cpu_capabilities *cap, static bool hvhe_possible(const struct arm64_cpu_capabilities *entry, int __unused) { - u64 val; - - val = read_sysreg(id_aa64mmfr1_el1); - if (!cpuid_feature_extract_unsigned_field(val, ID_AA64MMFR1_EL1_VH_SHIFT)) - return false; - - val = arm64_sw_feature_override.val & arm64_sw_feature_override.mask; - return cpuid_feature_extract_unsigned_field(val, ARM64_SW_FEATURE_OVERRIDE_HVHE); + return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE); } #ifdef CONFIG_ARM64_PAN