Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp672856pxb; Wed, 3 Feb 2021 15:00:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJx9tf1BGQzx6vXkr4mOzj+KWVGdmpPzEz7GJJwVmwxrU83IBKxQq/ZAFQ3KeVJejzo5OWR8 X-Received: by 2002:a17:907:2088:: with SMTP id pv8mr5146463ejb.131.1612393211915; Wed, 03 Feb 2021 15:00:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612393211; cv=none; d=google.com; s=arc-20160816; b=wrT7TDjvOaIfyv1mnKRNerCtNp+mcVfgzOoxaRv2oOfb23gnJCmk6t60sxn6zZKVeK 1KHnLgk+QXdTXcUGscfmGq6xFu2s594Uw5G3Xd95UTEYIM4PJvUd4Mrr0GKTGtKLSi8r gMH48V4heOxWmxryPt3PQdtpi0EomMvnvUB+bnsVWbT2RGTix0R6Bl7j7+so+79UsqhM wH7LW0+NGx+Y7SJL9uv5/ui1Sg2VwW/Ujxxd0n+9/BKZU5un7KYJYp5X6et9lmqHTAwn PxjVx6KjqmnyHddVHF12GsgZh8Ax3wbytqqF5c6tBU8cA+B81U/bjI5ttX8m6ispfSS2 y5gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=1Ojh+rQsSZl7ei9keTu5/KNyEUD1ZLDsds+FOylfCwY=; b=gvkPKmgK2BaB3mpEDTCK7R7uY7dFgQUVlhsC8U1dyFuVBaXSHpo0d0AVpLvhmyN8Qw qUwtb22yfcNxMLxcREIgg/A+aj5YKgYXxAnDlHiPmeC0SI5GfYM9NueMHTwZZ1kkJ6Qb FNZtRwt2OSk+ylfEAacJlWrWkXZyW0naRs9GGhDS/m665DT5aPOKCB+VOI8KhcCX2wwJ WiYvNK5rrmgb3aw6k4heMGXm5uvZ0/2ern8iZXKxtv40F72WlmMMtm6DDVPCV6ekGbEE hK5Zfh1JekmkH4z+zZNbG5PlPVHgHdVZF4cvUlt7mj4yJsP00Qdf/zOcELO1gp7E22DN 3Gqw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i17si2331288ejf.482.2021.02.03.14.59.47; Wed, 03 Feb 2021 15:00:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233172AbhBCW52 (ORCPT + 99 others); Wed, 3 Feb 2021 17:57:28 -0500 Received: from mga06.intel.com ([134.134.136.31]:30220 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233137AbhBCW5M (ORCPT ); Wed, 3 Feb 2021 17:57:12 -0500 IronPort-SDR: sz4iIB2p6NrFxnCE3KXioZTowxmQiJ9CrapEh1NkPFLh9rhn1JPwchlvK3UsWYVhCg5pyf7S3H hA8ncw4Iq2qQ== X-IronPort-AV: E=McAfee;i="6000,8403,9884"; a="242642348" X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="242642348" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:22 -0800 IronPort-SDR: 2E0bEbI74QgQaRyPnQgL2mFah1Pwwi5Lr4ZtG96FG91FOKTbjE3BkDeaRRWEY5kK4eWcdlUHwu PxGufRz6pEaA== X-IronPort-AV: E=Sophos;i="5.79,399,1602572400"; d="scan'208";a="507921072" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 14:56:21 -0800 From: Yu-cheng Yu To: x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang , Pengfei Xu Cc: Yu-cheng Yu Subject: [PATCH v19 04/25] x86/cpufeatures: Introduce X86_FEATURE_CET and setup functions Date: Wed, 3 Feb 2021 14:55:26 -0800 Message-Id: <20210203225547.32221-5-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20210203225547.32221-1-yu-cheng.yu@intel.com> References: <20210203225547.32221-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Introduce a software-defined X86_FEATURE_CET, which indicates either Shadow Stack or Indirect Branch Tracking (or both) is present. Also introduce related cpu init/setup functions. Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/cpufeatures.h | 2 +- arch/x86/include/asm/disabled-features.h | 5 ++++- arch/x86/include/uapi/asm/processor-flags.h | 2 ++ arch/x86/kernel/cpu/common.c | 16 ++++++++++++++++ arch/x86/kernel/cpu/intel.c | 3 +++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 292fe87b26b3..d1866659edbd 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -108,7 +108,7 @@ #define X86_FEATURE_EXTD_APICID ( 3*32+26) /* Extended APICID (8 bits) */ #define X86_FEATURE_AMD_DCM ( 3*32+27) /* AMD multi-node processor */ #define X86_FEATURE_APERFMPERF ( 3*32+28) /* P-State hardware coordination feedback capability (APERF/MPERF MSRs) */ -/* free ( 3*32+29) */ +#define X86_FEATURE_CET ( 3*32+29) /* Control-flow enforcement */ #define X86_FEATURE_NONSTOP_TSC_S3 ( 3*32+30) /* TSC doesn't stop in S3 state */ #define X86_FEATURE_TSC_KNOWN_FREQ ( 3*32+31) /* TSC has known frequency */ diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index a66e7f34b788..ab28dc7b30d0 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -71,9 +71,11 @@ #ifdef CONFIG_X86_CET #define DISABLE_SHSTK 0 #define DISABLE_IBT 0 +#define DISABLE_CET 0 #else #define DISABLE_SHSTK (1 << (X86_FEATURE_SHSTK & 31)) #define DISABLE_IBT (1 << (X86_FEATURE_IBT & 31)) +#define DISABLE_CET (1 << (X86_FEATURE_CET & 31)) #endif /* @@ -82,7 +84,8 @@ #define DISABLED_MASK0 (DISABLE_VME) #define DISABLED_MASK1 0 #define DISABLED_MASK2 0 -#define DISABLED_MASK3 (DISABLE_CYRIX_ARR|DISABLE_CENTAUR_MCR|DISABLE_K6_MTRR) +#define DISABLED_MASK3 (DISABLE_CYRIX_ARR|DISABLE_CENTAUR_MCR|DISABLE_K6_MTRR| \ + DISABLE_CET) #define DISABLED_MASK4 (DISABLE_PCID) #define DISABLED_MASK5 0 #define DISABLED_MASK6 0 diff --git a/arch/x86/include/uapi/asm/processor-flags.h b/arch/x86/include/uapi/asm/processor-flags.h index bcba3c643e63..a8df907e8017 100644 --- a/arch/x86/include/uapi/asm/processor-flags.h +++ b/arch/x86/include/uapi/asm/processor-flags.h @@ -130,6 +130,8 @@ #define X86_CR4_SMAP _BITUL(X86_CR4_SMAP_BIT) #define X86_CR4_PKE_BIT 22 /* enable Protection Keys support */ #define X86_CR4_PKE _BITUL(X86_CR4_PKE_BIT) +#define X86_CR4_CET_BIT 23 /* enable Control-flow Enforcement */ +#define X86_CR4_CET _BITUL(X86_CR4_CET_BIT) /* * x86-64 Task Priority Register, CR8 diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index 35ad8480c464..803d0749b302 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -510,6 +510,14 @@ static __init int setup_disable_pku(char *arg) __setup("nopku", setup_disable_pku); #endif /* CONFIG_X86_64 */ +static __always_inline void setup_cet(struct cpuinfo_x86 *c) +{ + if (!cpu_feature_enabled(X86_FEATURE_CET)) + return; + + cr4_set_bits(X86_CR4_CET); +} + /* * Some CPU features depend on higher CPUID levels, which may not always * be available due to CPUID level capping or broken virtualization @@ -1252,6 +1260,13 @@ static void __init cpu_parse_early_param(void) if (cmdline_find_option_bool(boot_command_line, "noxsaves")) setup_clear_cpu_cap(X86_FEATURE_XSAVES); +#ifdef CONFIG_X86_CET + if (cmdline_find_option_bool(boot_command_line, "no_user_shstk")) + setup_clear_cpu_cap(X86_FEATURE_SHSTK); + if (cmdline_find_option_bool(boot_command_line, "no_user_ibt")) + setup_clear_cpu_cap(X86_FEATURE_IBT); +#endif + arglen = cmdline_find_option(boot_command_line, "clearcpuid", arg, sizeof(arg)); if (arglen <= 0) return; @@ -1591,6 +1606,7 @@ static void identify_cpu(struct cpuinfo_x86 *c) x86_init_rdrand(c); setup_pku(c); + setup_cet(c); /* * Clear/Set all flags overridden by options, need do it diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index 59a1e3ce3f14..1784b9f95239 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -333,6 +333,9 @@ static void early_init_intel(struct cpuinfo_x86 *c) static void bsp_init_intel(struct cpuinfo_x86 *c) { + if (cpu_has(c, X86_FEATURE_SHSTK) || cpu_has(c, X86_FEATURE_IBT)) + setup_force_cpu_cap(X86_FEATURE_CET); + resctrl_cpu_detect(c); } -- 2.21.0