From patchwork Fri Sep 21 15:05:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu-cheng Yu X-Patchwork-Id: 10610311 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0A98D14BD for ; Fri, 21 Sep 2018 15:11:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E8D312E4E5 for ; Fri, 21 Sep 2018 15:11:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E65582E550; Fri, 21 Sep 2018 15:11:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 53F042E4E5 for ; Fri, 21 Sep 2018 15:11:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AFC98E002B; Fri, 21 Sep 2018 11:10:38 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 762E38E002A; Fri, 21 Sep 2018 11:10:38 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6053A8E002B; Fri, 21 Sep 2018 11:10:38 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by kanga.kvack.org (Postfix) with ESMTP id 1EFF28E0028 for ; Fri, 21 Sep 2018 11:10:38 -0400 (EDT) Received: by mail-pf1-f198.google.com with SMTP id d1-v6so6706815pfo.16 for ; Fri, 21 Sep 2018 08:10:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=19kvAQGrLEU6DwDdanz0hEuJCCTFazxdJr+BJdT9MqE=; b=gg762d/M4FBiXpEAW9ZP/RN/uh2xBRP7zyz4Mhl2QvxV84DIqm1PjmYscpbPEn5oiC 4vyHe4+9i5Rqtg2YtJlkc4Y17kmlDGolniZU74ZIOuGvlrCeGmgCvW5lzX35pJNMWRtF DmapjaAUMS8ldAb3dYYuDnvTAGbfXTAlShRuZRvceO6opqtz/nhp9f9LgPco8vYiItUO zIp7agAs75jFuhmsJLiGgu0HGLoVzgZR7iQA1rgZc3a3QGTOK5xvDoOFLHS4pP2XY2J2 byE1/HMfzcc7z/b6lSqYYXiZmJK4AVWTGglathesnkCi4NaH/W/S0rFFQ4nz0zULGLNu LmMA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: ABuFfojegpOcqLSOyq5sapysjVvUYNpEaFghXT9OGGCqDwBAzJmeb7lP ymHgg7JCRLI/xPT/05m4ok9EX76TD2Ihjg3EWYjKfzAuhzi2XNYCDaNn3Lk2BFQBeIICRO8FHOX aEJlayuNhDJZ5JU5vdoFstYzyd4S62l7fOB3ZRxKWrz1vockOIzacHkfG6GsuXUgFOg== X-Received: by 2002:a17:902:24e7:: with SMTP id l36-v6mr2211288plg.234.1537542637793; Fri, 21 Sep 2018 08:10:37 -0700 (PDT) X-Google-Smtp-Source: ACcGV63T//fmeKkol/Ms0gJeKQWQJx3mnDyXflfrfcOlSEmu6Xtc6U6hHC6osix+JszoHu85RG19 X-Received: by 2002:a17:902:24e7:: with SMTP id l36-v6mr2210659plg.234.1537542628747; Fri, 21 Sep 2018 08:10:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537542628; cv=none; d=google.com; s=arc-20160816; b=EZAw6en9mAi0hQK1lsvfeeyCV55ZX9RBhQcXrLrGyhRYXWZ38frp6/hYjh04mLypeY 5c5iCf6woJ/XFcyJa+4RkDDSibf6K5XcX6sjdATT74RqwMI5quvxZTHjgfK2nztVfxNj e9v6BHUQnyTSeuvJvHiqzEJmmnwZSY6eUKio/jbrHzs1Mkm7roEJ1+nTIQ8M8K+qluYR meoyXMGJX6vWe0v3sEo0s/5h58eKgWzhonvZurt5ow9RAwuqgup7juB0CwvKrs1KpHrH //ROS4PEqQrXkJUAPAbhjLpOv/R4RgF7Zczukz4l5NUX4Yoa8w46CSTCwwjZHYiIYlOp BIyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=19kvAQGrLEU6DwDdanz0hEuJCCTFazxdJr+BJdT9MqE=; b=eBrIH8VBWE4dffpyRNujLARDMWEBTLDh3A0vS2kT0mChVwsGe4MS+2AfiA7S8q9pDH t43/cwofGHixF88vth8MKXE7ObpsqCz+6Dana4zxN7XnnYYZTVsfo5hMdFvNULJB6P5k goImOsswSN8fDeCd1CTaCE+IhNC2fpwg9lFNSsezmiK4e7fLcrTejrp0BBJOBfunHW4X E2Nzt13ptVeqzLWSEtVYRM3vmjhOU+X3YmdRFK/kMI+BPgdykD4y/fxlPRnSuBoLaMzz IA4CtHqQUcpN7bhntTNZk+KqPZ56/8V/sdJIrpAb/s1m1WymxEzGg0aML6+MczsvIyqH H1Ag== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga14.intel.com (mga14.intel.com. [192.55.52.115]) by mx.google.com with ESMTPS id g16-v6si2805450pgd.354.2018.09.21.08.10.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 21 Sep 2018 08:10:28 -0700 (PDT) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.115 as permitted sender) client-ip=192.55.52.115; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 192.55.52.115 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Sep 2018 08:10:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,285,1534834800"; d="scan'208";a="88187965" Received: from 2b52.sc.intel.com ([143.183.136.51]) by fmsmga002.fm.intel.com with ESMTP; 21 Sep 2018 08:10:27 -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 , Cyrill Gorcunov , Dave Hansen , 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 Cc: Yu-cheng Yu Subject: [RFC PATCH v4 2/9] x86/cet/ibt: User-mode indirect branch tracking support Date: Fri, 21 Sep 2018 08:05:46 -0700 Message-Id: <20180921150553.21016-3-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180921150553.21016-1-yu-cheng.yu@intel.com> References: <20180921150553.21016-1-yu-cheng.yu@intel.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Add user-mode indirect branch tracking enabling/disabling and supporting routines. Signed-off-by: H.J. Lu Signed-off-by: Yu-cheng Yu --- arch/x86/include/asm/cet.h | 8 ++++++ arch/x86/include/asm/disabled-features.h | 8 +++++- arch/x86/kernel/cet.c | 31 ++++++++++++++++++++++++ arch/x86/kernel/cpu/common.c | 20 ++++++++++++++- arch/x86/kernel/process.c | 1 + 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/cet.h b/arch/x86/include/asm/cet.h index 212bd68e31d3..1fea93fd436a 100644 --- a/arch/x86/include/asm/cet.h +++ b/arch/x86/include/asm/cet.h @@ -12,8 +12,11 @@ struct task_struct; struct cet_status { unsigned long shstk_base; unsigned long shstk_size; + unsigned long ibt_bitmap_addr; + unsigned long ibt_bitmap_size; unsigned int locked:1; unsigned int shstk_enabled:1; + unsigned int ibt_enabled:1; }; #ifdef CONFIG_X86_INTEL_CET @@ -25,6 +28,9 @@ void cet_disable_shstk(void); void cet_disable_free_shstk(struct task_struct *p); int cet_restore_signal(unsigned long ssp); int cet_setup_signal(bool ia32, unsigned long rstor, unsigned long *new_ssp); +int cet_setup_ibt(void); +int cet_setup_ibt_bitmap(void); +void cet_disable_ibt(void); #else static inline int prctl_cet(int option, unsigned long arg2) { return 0; } static inline int cet_setup_shstk(void) { return 0; } @@ -35,6 +41,8 @@ static inline void cet_disable_free_shstk(struct task_struct *p) {} static inline int cet_restore_signal(unsigned long ssp) { return 0; } static inline int cet_setup_signal(bool ia32, unsigned long rstor, unsigned long *new_ssp) { return 0; } +static inline int cet_setup_ibt(void) { return 0; } +static inline void cet_disable_ibt(void) {} #endif #endif /* __ASSEMBLY__ */ diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index 3624a11e5ba6..ce5bdaf0f1ff 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 */ @@ -72,7 +78,7 @@ #define DISABLED_MASK4 (DISABLE_PCID) #define DISABLED_MASK5 0 #define DISABLED_MASK6 0 -#define DISABLED_MASK7 (DISABLE_PTI) +#define DISABLED_MASK7 (DISABLE_PTI|DISABLE_IBT) #define DISABLED_MASK8 0 #define DISABLED_MASK9 (DISABLE_MPX) #define DISABLED_MASK10 0 diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index 1c2689738604..6adfe795d692 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include #include @@ -283,3 +285,32 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, set_shstk_ptr(ssp); return 0; } + +int cet_setup_ibt(void) +{ + u64 r; + + if (!cpu_feature_enabled(X86_FEATURE_IBT)) + return -EOPNOTSUPP; + + rdmsrl(MSR_IA32_U_CET, r); + r |= (MSR_IA32_CET_ENDBR_EN | MSR_IA32_CET_NO_TRACK_EN); + wrmsrl(MSR_IA32_U_CET, r); + + current->thread.cet.ibt_enabled = 1; + return 0; +} + +void cet_disable_ibt(void) +{ + u64 r; + + if (!cpu_feature_enabled(X86_FEATURE_IBT)) + return; + + rdmsrl(MSR_IA32_U_CET, r); + r &= ~(MSR_IA32_CET_ENDBR_EN | MSR_IA32_CET_LEG_IW_EN | + MSR_IA32_CET_NO_TRACK_EN); + wrmsrl(MSR_IA32_U_CET, r); + current->thread.cet.ibt_enabled = 0; +} diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index bffa9ef47832..230f65ee881e 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -413,7 +413,8 @@ __setup("nopku", setup_disable_pku); static __always_inline void setup_cet(struct cpuinfo_x86 *c) { - if (cpu_feature_enabled(X86_FEATURE_SHSTK)) + if (cpu_feature_enabled(X86_FEATURE_SHSTK) || + cpu_feature_enabled(X86_FEATURE_IBT)) cr4_set_bits(X86_CR4_CET); } @@ -434,6 +435,23 @@ static __init int setup_disable_shstk(char *s) __setup("no_cet_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 (strlen(s)) + return 0; + + if (!boot_cpu_has(X86_FEATURE_IBT)) + return 1; + + setup_clear_cpu_cap(X86_FEATURE_IBT); + pr_info("x86: 'no_cet_ibt' specified, disabling Branch Tracking\n"); + return 1; +} +__setup("no_cet_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/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 251b8714f9a3..ac0ea9c7e89f 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -137,6 +137,7 @@ void flush_thread(void) memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array)); cet_disable_shstk(); + cet_disable_ibt(); fpu__clear(&tsk->thread.fpu); }