Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2531508pxa; Mon, 24 Aug 2020 17:34:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzii1m8gaUzFRpkO8+Fuu4Q5MZh7H2b4CMXTa7IwgmIWugAeJtrhfXSHEc7sOXnTS95mqwa X-Received: by 2002:a17:906:38c7:: with SMTP id r7mr8265591ejd.118.1598315663465; Mon, 24 Aug 2020 17:34:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598315663; cv=none; d=google.com; s=arc-20160816; b=oYvALJIoG5ySSIap+aLPN9/r4Tr6jHlT8a5DDGsWTMtfTFXtGo6nEi9BYLAjqdzA7t cMse7C4wTi3CiATZr4TBk0ZpqKR4V0kgjCQWf51Uj2jXcU4rTJjhnTkGGj5BzK2L/nxG TuYck/5A89oGXl5Trpb/2SIukD4S9lSA1NGgX1ZleVOZHgKGNJYFj470xisOkYDISjhm PAuBtzOcNBGBT9K/UXumgY1ZsS8/Mk0vLIlMROTVxn+cI4FuQoPz8iPoFpVknLVn4cXZ NhfL3IRzggFWYXGJbUlv8YGpOaZxQawLWnj1C8zhrE8Nu/szj7d98BHQ16wRsJGoS9Iy ZAOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=PAFetEpEE0sWgHhJIcJ8fHBdOmcmN6myy9F9MkNC0kc=; b=h90l9oftFzo45EVqzRbLRukpNfaKFMgiSpvIOskNwG/mVZPYj3t516JXMyjH+LoSz9 P3/Xm5vMgW1s10v0EGt0KJj/hgVycAS8baeUREVIxqiBdU0KcsTSNkg0+s3AjGsbXsBv +q9PO9bMakH+wVAjewgUP8Bid3NsMRTXiufo7KR5sdAVBRGVcN9vfwT0XY07+G/WSDmY bIcvIAq6qZfbPy7x3mgSU0M+4EuvI9lx9IkpEJ1aQxbBVi6RjXD/GQkybQJCFOyDCBMo 5wTUlkvSGPFbvzA74T3z4a5YUXkNliVWf3+w6iZEWlHXCphgWpKCvnJCscKrxUyL8yuI RkWQ== 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 h89si9293881edd.480.2020.08.24.17.34.00; Mon, 24 Aug 2020 17:34:23 -0700 (PDT) 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 S1728512AbgHYAao (ORCPT + 99 others); Mon, 24 Aug 2020 20:30:44 -0400 Received: from mga01.intel.com ([192.55.52.88]:28757 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728219AbgHYAag (ORCPT ); Mon, 24 Aug 2020 20:30:36 -0400 IronPort-SDR: e4R0YNN6Cs4yhVyUFB46M2dHfVhLYqgXJBS9xqY0JPvxEdTnTuZR5f0LxhHWDQD3A7TNT1YC1G Qj2lEnec9ctA== X-IronPort-AV: E=McAfee;i="6000,8403,9723"; a="174053262" X-IronPort-AV: E=Sophos;i="5.76,350,1592895600"; d="scan'208";a="174053262" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2020 17:30:34 -0700 IronPort-SDR: TFQsm/tu5aV7nP34bj6j/mtL2KBSX8IjvNHKQzQlR+2sGvwEBfTGcI7PRmB5ov5JqKt89kMuCX 8EZxamN/snUg== X-IronPort-AV: E=Sophos;i="5.76,350,1592895600"; d="scan'208";a="443429301" 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; 24 Aug 2020 17:30:33 -0700 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 Cc: Yu-cheng Yu Subject: [PATCH v11 2/9] x86/cet/ibt: User-mode Indirect Branch Tracking support Date: Mon, 24 Aug 2020 17:26:37 -0700 Message-Id: <20200825002645.3658-3-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200825002645.3658-1-yu-cheng.yu@intel.com> References: <20200825002645.3658-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Introduce user-mode Indirect Branch Tracking (IBT) support. Update setup routines to include IBT. Signed-off-by: Yu-cheng Yu --- v10: - Change no_cet_ibt to no_user_ibt. v9: - Change cpu_feature_enabled() to static_cpu_has(). v2: - Change noibt to no_cet_ibt. arch/x86/include/asm/cet.h | 3 ++ arch/x86/include/asm/disabled-features.h | 8 ++++- arch/x86/kernel/cet.c | 33 +++++++++++++++++++ arch/x86/kernel/cpu/common.c | 17 ++++++++++ .../arch/x86/include/asm/disabled-features.h | 8 ++++- 5 files changed, 67 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h index f7eb197998ad..916ac2a0404c 100644 --- a/arch/x86/include/asm/cet.h +++ b/arch/x86/include/asm/cet.h @@ -15,6 +15,7 @@ struct cet_status { unsigned long shstk_base; unsigned long shstk_size; unsigned int locked:1; + unsigned int ibt_enabled:1; }; #ifdef CONFIG_X86_INTEL_CET @@ -26,6 +27,8 @@ void cet_disable_free_shstk(struct task_struct *p); int cet_verify_rstor_token(bool ia32, unsigned long ssp, unsigned long *new_ssp); void cet_restore_signal(struct sc_ext *sc); int cet_setup_signal(bool ia32, unsigned long rstor, struct sc_ext *sc); +int cet_setup_ibt(void); +void cet_disable_ibt(void); #else static inline int prctl_cet(int option, u64 arg2) { return -EINVAL; } static inline int cet_setup_thread_shstk(struct task_struct *p) { return 0; } diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index a0e1b24cfa02..52c9c07cfacc 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -62,6 +62,12 @@ #define DISABLE_SHSTK (1<<(X86_FEATURE_SHSTK & 31)) #endif +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +#define DISABLE_IBT 0 +#else +#define DISABLE_IBT (1<<(X86_FEATURE_IBT & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -83,7 +89,7 @@ #define DISABLED_MASK15 0 #define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP|DISABLE_SHSTK) #define DISABLED_MASK17 0 -#define DISABLED_MASK18 0 +#define DISABLED_MASK18 (DISABLE_IBT) #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19) #endif /* _ASM_X86_DISABLED_FEATURES_H */ diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index 2bf1a6b6abb6..b1c122a5aef4 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include #include #include @@ -355,3 +357,34 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, struct sc_ext *sc_ext) return 0; } + +int cet_setup_ibt(void) +{ + u64 msr_val; + + if (!static_cpu_has(X86_FEATURE_IBT)) + return -EOPNOTSUPP; + + start_update_msrs(); + rdmsrl(MSR_IA32_U_CET, msr_val); + msr_val |= (CET_ENDBR_EN | CET_NO_TRACK_EN); + wrmsrl(MSR_IA32_U_CET, msr_val); + end_update_msrs(); + current->thread.cet.ibt_enabled = 1; + return 0; +} + +void cet_disable_ibt(void) +{ + u64 msr_val; + + if (!static_cpu_has(X86_FEATURE_IBT)) + return; + + start_update_msrs(); + rdmsrl(MSR_IA32_U_CET, msr_val); + msr_val &= CET_SHSTK_EN; + wrmsrl(MSR_IA32_U_CET, msr_val); + end_update_msrs(); + current->thread.cet.ibt_enabled = 0; +} diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index 5f60ddaabc46..43666b1f50a2 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -536,6 +536,23 @@ static __init int setup_disable_shstk(char *s) __setup("no_user_shstk", setup_disable_shstk); #endif +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +static __init int setup_disable_ibt(char *s) +{ + /* require an exact match without trailing characters */ + if (s[0] != '\0') + return 0; + + if (!boot_cpu_has(X86_FEATURE_IBT)) + return 1; + + setup_clear_cpu_cap(X86_FEATURE_IBT); + pr_info("x86: 'no_user_ibt' specified, disabling user Branch Tracking\n"); + return 1; +} +__setup("no_user_ibt", setup_disable_ibt); +#endif + /* * Some CPU features depend on higher CPUID levels, which may not always * be available due to CPUID level capping or broken virtualization diff --git a/tools/arch/x86/include/asm/disabled-features.h b/tools/arch/x86/include/asm/disabled-features.h index a0e1b24cfa02..52c9c07cfacc 100644 --- a/tools/arch/x86/include/asm/disabled-features.h +++ b/tools/arch/x86/include/asm/disabled-features.h @@ -62,6 +62,12 @@ #define DISABLE_SHSTK (1<<(X86_FEATURE_SHSTK & 31)) #endif +#ifdef CONFIG_X86_INTEL_BRANCH_TRACKING_USER +#define DISABLE_IBT 0 +#else +#define DISABLE_IBT (1<<(X86_FEATURE_IBT & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -83,7 +89,7 @@ #define DISABLED_MASK15 0 #define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP|DISABLE_SHSTK) #define DISABLED_MASK17 0 -#define DISABLED_MASK18 0 +#define DISABLED_MASK18 (DISABLE_IBT) #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 19) #endif /* _ASM_X86_DISABLED_FEATURES_H */ -- 2.21.0