Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3551427rdh; Thu, 28 Sep 2023 15:32:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF8R+q/Bo0g8NO3dLY6GgiIG7If5lfdDb0lMI7C+NHxjUODlWm098wL75p9vmZSBuYrtckb X-Received: by 2002:a05:6a20:1607:b0:15d:b407:b0a0 with SMTP id l7-20020a056a20160700b0015db407b0a0mr4020887pzj.26.1695940376967; Thu, 28 Sep 2023 15:32:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695940376; cv=none; d=google.com; s=arc-20160816; b=POZk9X/VyJGwvMPn7igwBEKakHn0+0PiOCfpkkafMvFT72eNv6qelWWx9FZSImj5r0 zZLqL85YN/ZZq21PXkKU/zql98PrI6eUA7EMBU6BBQNSR11YxfuaratZOeumimCdf1f7 xfLN9gfYoYl5bOiRsXFR7gVDR0Qr3Ks+uiAxLScdigBRXFkeCnnxZeEhH1cX2OH9YgOZ YG6/zrEWRkIJJySlgsfA/pXPRWBzADPm14oE2/Fntx2i3i9RtOTUHmsT7wX5CFoHSJJO pCvajEu1ZLDhE3nWesb074xIbeDU2cKAAGqSOODcE3x6+EZcDqK+pC8Ghvu9XD8azA6Z cVFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:organization:user-agent:message-id :date:subject:to:from:dkim-signature; bh=ZKJ9wpeNS4LUEjb1vN4gTa40QcmjmSyZUaX4h/O2gr8=; fh=sQNEfJTpcac57qKeIh+T9YhcvAwyOBlL1FDja0bb/zk=; b=Kr+Y5nL1hsIzYNHuS5HOAtedlLNdGtvBMje/z38vTqaLgFxlg0/O18uH1L67OR11TG kjpK/JZUe++5+JZRV23VzAwNRRZFea3Bs/QP4qWuUEljQ+logADAk0WGatBjWgoVLA1G cWFdba8ZBIAcusF4Rx7e16C2Ai2cQRqYjKeltJMbUuLGho4RIssDNHKJnJ1yG80cs0kR JNQs7q6Fo0q29eWcDCEWMxdx+NRIE04fZZTgcTrWZwv9ubuQDDYgAJPm8JTv+z9XA6C2 RhXn7EcjeGEqbnc2CZt2Rgtn4eMvV1tZSMQ6szGZRZILL5pW0HM5BZ/KCrXw3LHA09cS 9ukw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZXxjqo9o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id y35-20020a056a00182300b00690f191430csi20965275pfa.56.2023.09.28.15.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 15:32:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZXxjqo9o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 6EAFA8071202; Thu, 28 Sep 2023 15:31:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232545AbjI1WbC (ORCPT + 99 others); Thu, 28 Sep 2023 18:31:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231864AbjI1WbB (ORCPT ); Thu, 28 Sep 2023 18:31:01 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F35E619F for ; Thu, 28 Sep 2023 15:30:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695940256; x=1727476256; h=from:to:subject:date:message-id:mime-version; bh=BqWyNM4eUzSAPvjjWKK374LUZn6TqqQ2pHJEfIzXm7I=; b=ZXxjqo9oqcP0G+gX5vtreSMkQ4LjQrr48nQUZQuCC64B2wfn10EOoiYq kzo32tBOabnrwYsfsCMLspobZ+o2Us1iejRX2UJVOG7K7JBBQfQAENVHM Aw3eD3SPR5qunv5Rv8cIRQ/dIc72LyM+iIQtM3yAfWGPmurJmH2XMN2JW 0DPsc157igxV6AF8m2lty2d2iJ8+U1V5gVUI5bUkCORscL4eaddquIsI3 6dgq69+iVJ8taBB7GMeh/ihQE3LS1NjJokZcRJDL9KGIriJvEpKIDUIO2 ysd8a1Xk06IqvylyhiBMP6HKtzuZIKUIYs2ESGuiGdlD5qb04NByGW3Mg g==; X-IronPort-AV: E=McAfee;i="6600,9927,10847"; a="381077813" X-IronPort-AV: E=Sophos;i="6.03,185,1694761200"; d="scan'208";a="381077813" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2023 15:30:56 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10847"; a="840077017" X-IronPort-AV: E=Sophos;i="6.03,185,1694761200"; d="scan'208";a="840077017" Received: from htschmid-mobl2.amr.corp.intel.com (HELO jcompost-mobl.amr.corp.intel.com) ([10.213.188.139]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Sep 2023 15:30:55 -0700 From: "Compostella, Jeremy" To: , , Ingo Molnar , Borislav Petkov , "Li, Xin3" , "Huang, Kai" Subject: [PATCH v3 1/2] x86/cpu/intel: Fix MTRR verification for TME enabled platforms Date: Thu, 28 Sep 2023 15:30:55 -0700 Message-ID: <87a5t6ylpc.fsf@jcompost-mobl.amr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) Organization: Intel Corporation - 2200 Mission College Blvd. Santa Clara, CA 95052. USA MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Thu, 28 Sep 2023 15:31:17 -0700 (PDT) On TME enabled platform, BIOS publishes MTRR taking into account Total Memory Encryption (TME) reserved bits. generic_get_mtrr() performs a sanity check of the MTRRs relying on the `phys_hi_rsvd' variable which is set using the cpuinfo_x86 structure `x86_phys_bits' field. But at the time the generic_get_mtrr() function is ran the `x86_phys_bits' has not been updated by detect_tme() when TME is enabled. Since the x86_phys_bits does not reflect yet the real maximal physical address size yet generic_get_mtrr() complains by logging the following messages. mtrr: your BIOS has configured an incorrect mask, fixing it. mtrr: your BIOS has configured an incorrect mask, fixing it. [...] In such a situation, generic_get_mtrr() returns an incorrect size but no side effect were observed during our testing. For `x86_phys_bits' to be updated before generic_get_mtrr() runs, move the detect_tme() call from init_intel() to early_init_intel(). Change for v3: Take review commit message comments into account. Change for v2: Add a comment in the code explaining why detect_tme() needs to called in early_init_intel(). Signed-off-by: Jeremy Compostella --- arch/x86/kernel/cpu/intel.c | 178 ++++++++++++++++++------------------ 1 file changed, 91 insertions(+), 87 deletions(-) diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index be4045628fd3..17240b96ffda 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -184,6 +184,90 @@ static bool bad_spectre_microcode(struct cpuinfo_x86 *c) return false; } +#define MSR_IA32_TME_ACTIVATE 0x982 + +/* Helpers to access TME_ACTIVATE MSR */ +#define TME_ACTIVATE_LOCKED(x) (x & 0x1) +#define TME_ACTIVATE_ENABLED(x) (x & 0x2) + +#define TME_ACTIVATE_POLICY(x) ((x >> 4) & 0xf) /* Bits 7:4 */ +#define TME_ACTIVATE_POLICY_AES_XTS_128 0 + +#define TME_ACTIVATE_KEYID_BITS(x) ((x >> 32) & 0xf) /* Bits 35:32 */ + +#define TME_ACTIVATE_CRYPTO_ALGS(x) ((x >> 48) & 0xffff) /* Bits 63:48 */ +#define TME_ACTIVATE_CRYPTO_AES_XTS_128 1 + +/* Values for mktme_status (SW only construct) */ +#define MKTME_ENABLED 0 +#define MKTME_DISABLED 1 +#define MKTME_UNINITIALIZED 2 +static int mktme_status = MKTME_UNINITIALIZED; + +static void detect_tme(struct cpuinfo_x86 *c) +{ + u64 tme_activate, tme_policy, tme_crypto_algs; + int keyid_bits = 0, nr_keyids = 0; + static u64 tme_activate_cpu0 = 0; + + rdmsrl(MSR_IA32_TME_ACTIVATE, tme_activate); + + if (mktme_status != MKTME_UNINITIALIZED) { + if (tme_activate != tme_activate_cpu0) { + /* Broken BIOS? */ + pr_err_once("x86/tme: configuration is inconsistent between CPUs\n"); + pr_err_once("x86/tme: MKTME is not usable\n"); + mktme_status = MKTME_DISABLED; + + /* Proceed. We may need to exclude bits from x86_phys_bits. */ + } + } else { + tme_activate_cpu0 = tme_activate; + } + + if (!TME_ACTIVATE_LOCKED(tme_activate) || !TME_ACTIVATE_ENABLED(tme_activate)) { + pr_info_once("x86/tme: not enabled by BIOS\n"); + mktme_status = MKTME_DISABLED; + return; + } + + if (mktme_status != MKTME_UNINITIALIZED) + goto detect_keyid_bits; + + pr_info("x86/tme: enabled by BIOS\n"); + + tme_policy = TME_ACTIVATE_POLICY(tme_activate); + if (tme_policy != TME_ACTIVATE_POLICY_AES_XTS_128) + pr_warn("x86/tme: Unknown policy is active: %#llx\n", tme_policy); + + tme_crypto_algs = TME_ACTIVATE_CRYPTO_ALGS(tme_activate); + if (!(tme_crypto_algs & TME_ACTIVATE_CRYPTO_AES_XTS_128)) { + pr_err("x86/mktme: No known encryption algorithm is supported: %#llx\n", + tme_crypto_algs); + mktme_status = MKTME_DISABLED; + } +detect_keyid_bits: + keyid_bits = TME_ACTIVATE_KEYID_BITS(tme_activate); + nr_keyids = (1UL << keyid_bits) - 1; + if (nr_keyids) { + pr_info_once("x86/mktme: enabled by BIOS\n"); + pr_info_once("x86/mktme: %d KeyIDs available\n", nr_keyids); + } else { + pr_info_once("x86/mktme: disabled by BIOS\n"); + } + + if (mktme_status == MKTME_UNINITIALIZED) { + /* MKTME is usable */ + mktme_status = MKTME_ENABLED; + } + + /* + * KeyID bits effectively lower the number of physical address + * bits. Update cpuinfo_x86::x86_phys_bits accordingly. + */ + c->x86_phys_bits -= keyid_bits; +} + static void early_init_intel(struct cpuinfo_x86 *c) { u64 misc_enable; @@ -335,6 +419,13 @@ static void early_init_intel(struct cpuinfo_x86 *c) */ if (detect_extended_topology_early(c) < 0) detect_ht_early(c); + + /* + * detect_tme() must be called early as it updates the number of + * physical address bits which is used during the MTRR sanity check. + */ + if (cpu_has(c, X86_FEATURE_TME)) + detect_tme(c); } static void bsp_init_intel(struct cpuinfo_x86 *c) @@ -495,90 +586,6 @@ static void srat_detect_node(struct cpuinfo_x86 *c) #endif } -#define MSR_IA32_TME_ACTIVATE 0x982 - -/* Helpers to access TME_ACTIVATE MSR */ -#define TME_ACTIVATE_LOCKED(x) (x & 0x1) -#define TME_ACTIVATE_ENABLED(x) (x & 0x2) - -#define TME_ACTIVATE_POLICY(x) ((x >> 4) & 0xf) /* Bits 7:4 */ -#define TME_ACTIVATE_POLICY_AES_XTS_128 0 - -#define TME_ACTIVATE_KEYID_BITS(x) ((x >> 32) & 0xf) /* Bits 35:32 */ - -#define TME_ACTIVATE_CRYPTO_ALGS(x) ((x >> 48) & 0xffff) /* Bits 63:48 */ -#define TME_ACTIVATE_CRYPTO_AES_XTS_128 1 - -/* Values for mktme_status (SW only construct) */ -#define MKTME_ENABLED 0 -#define MKTME_DISABLED 1 -#define MKTME_UNINITIALIZED 2 -static int mktme_status = MKTME_UNINITIALIZED; - -static void detect_tme(struct cpuinfo_x86 *c) -{ - u64 tme_activate, tme_policy, tme_crypto_algs; - int keyid_bits = 0, nr_keyids = 0; - static u64 tme_activate_cpu0 = 0; - - rdmsrl(MSR_IA32_TME_ACTIVATE, tme_activate); - - if (mktme_status != MKTME_UNINITIALIZED) { - if (tme_activate != tme_activate_cpu0) { - /* Broken BIOS? */ - pr_err_once("x86/tme: configuration is inconsistent between CPUs\n"); - pr_err_once("x86/tme: MKTME is not usable\n"); - mktme_status = MKTME_DISABLED; - - /* Proceed. We may need to exclude bits from x86_phys_bits. */ - } - } else { - tme_activate_cpu0 = tme_activate; - } - - if (!TME_ACTIVATE_LOCKED(tme_activate) || !TME_ACTIVATE_ENABLED(tme_activate)) { - pr_info_once("x86/tme: not enabled by BIOS\n"); - mktme_status = MKTME_DISABLED; - return; - } - - if (mktme_status != MKTME_UNINITIALIZED) - goto detect_keyid_bits; - - pr_info("x86/tme: enabled by BIOS\n"); - - tme_policy = TME_ACTIVATE_POLICY(tme_activate); - if (tme_policy != TME_ACTIVATE_POLICY_AES_XTS_128) - pr_warn("x86/tme: Unknown policy is active: %#llx\n", tme_policy); - - tme_crypto_algs = TME_ACTIVATE_CRYPTO_ALGS(tme_activate); - if (!(tme_crypto_algs & TME_ACTIVATE_CRYPTO_AES_XTS_128)) { - pr_err("x86/mktme: No known encryption algorithm is supported: %#llx\n", - tme_crypto_algs); - mktme_status = MKTME_DISABLED; - } -detect_keyid_bits: - keyid_bits = TME_ACTIVATE_KEYID_BITS(tme_activate); - nr_keyids = (1UL << keyid_bits) - 1; - if (nr_keyids) { - pr_info_once("x86/mktme: enabled by BIOS\n"); - pr_info_once("x86/mktme: %d KeyIDs available\n", nr_keyids); - } else { - pr_info_once("x86/mktme: disabled by BIOS\n"); - } - - if (mktme_status == MKTME_UNINITIALIZED) { - /* MKTME is usable */ - mktme_status = MKTME_ENABLED; - } - - /* - * KeyID bits effectively lower the number of physical address - * bits. Update cpuinfo_x86::x86_phys_bits accordingly. - */ - c->x86_phys_bits -= keyid_bits; -} - static void init_cpuid_fault(struct cpuinfo_x86 *c) { u64 msr; @@ -715,9 +722,6 @@ static void init_intel(struct cpuinfo_x86 *c) init_ia32_feat_ctl(c); - if (cpu_has(c, X86_FEATURE_TME)) - detect_tme(c); - init_intel_misc_features(c); split_lock_init(); -- 2.40.1