Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1564281pxb; Tue, 26 Oct 2021 11:19:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3Rz/JMapqx6DKleR7VRIpIvH5J/Jm5dl57x50RajfoZpCAqB7TSm1VhLgJnmLiygUfRIH X-Received: by 2002:a65:530d:: with SMTP id m13mr20154302pgq.128.1635272396412; Tue, 26 Oct 2021 11:19:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635272396; cv=none; d=google.com; s=arc-20160816; b=LI8O9Cj9+6igZJsWCjkvy35QL8RdyR/DvFODQdVP5Lpx+PnfunQ6bLbdw8yaj4FRDb iCDSdAuwSzGxnMxZykJqNPNLZH5LS36dbIGZhTiEOFsCHZdicCx8QE9htXoDN5U3WD9D QFELWg/37sTVsDXYKPykk1Jrt1dnzA3KQruGyzNqq1CqM+EF4GJR5HlfBjOFc2MtGjxC 63/kXPTNkeKumfTF2MAEfZR0Y7rzKxHtp9To3a5hSa2/hl3X5MY+96P2qSHk0Jq+KuMI LL5X7iCPdisf46VMTJ714z1fh+taKcJ+a2ZU001kjhmv/FRPTMuvt4xMyW+EQm+TfhM5 8DHA== 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 :dkim-signature; bh=2yczlImmJZUDVVto43TWdibC8uGbZcKJHr/ucX+Q9lY=; b=mUIBhnuP5yaR+pVkHg8UoXxTpwGfLK04JMpiCTljpcd5o9fGIxFUyyZ8ofHiFiBACC Nct0XVy9qUmm3jJ0H16V9eSh/1Bcpxn2QiAqEzziS3jftI1gMOtqNzYnHcq//LwW20Fm 6I7j2e2rWEXnfZAs9twet4vkvBzC6dJ4+ysUlnUgvp08tg0I1Q9/ek5+MGKbM0zR8ke6 PWenBDtZPehbGiWjcc9j8CfSYDv39fp9Fnde4iKXM8Kx89GdQ71R5eZ7lXWnrUp0WWM9 zkqD+LkJ1aZJyUKs7923qu09CwuxpXZhMtBQqbmG8auyuIcyO5Oymw6x7Q8NfDtfmUmg YJ4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SYV4FUVc; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h9si31538029pgs.569.2021.10.26.11.19.41; Tue, 26 Oct 2021 11:19:56 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SYV4FUVc; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236621AbhJZOba (ORCPT + 99 others); Tue, 26 Oct 2021 10:31:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236600AbhJZObW (ORCPT ); Tue, 26 Oct 2021 10:31:22 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE55CC06122F for ; Tue, 26 Oct 2021 07:28:55 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id ls14-20020a17090b350e00b001a00e2251c8so1835807pjb.4 for ; Tue, 26 Oct 2021 07:28:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2yczlImmJZUDVVto43TWdibC8uGbZcKJHr/ucX+Q9lY=; b=SYV4FUVc8NrScaxthjnP7GD18ZtIcy7rnsdDoYhPp/eqBnpmQSORXVHCRbYKZtdT+l h6g4D3g845jMPtkwMBI9j61AIzRYVudS9e3cWyneu9piLcJeKYGoecedkOJe1d54U9Nq vcbeRtkx2/pKESw5DJKy+X6c7ml2rUKfuHxqeL4EqKtMBP5lbCcTaPHt4Fi9voeeWQC6 TzDgdX17QHmwzQI9x8btVE2gsrlMYIN+9ZT6u2YLrwRZ2fy1XkBqAPAD8QN24HN6Hmzd HZvXTBkfjYIuPp+A1EDmYqSNI5AoFQY74siDc5uO2EkftMHHlnB6ycAmMhHkLuHSafER U41g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2yczlImmJZUDVVto43TWdibC8uGbZcKJHr/ucX+Q9lY=; b=kvQJ6wjTzCLBWiQS7Vmlzor6PmbxrlmoSg6w9M/LS4K3w9iOUZSrFxLNK7BhKuPEDD vkumxATOAnG8lqS5gfwVTSkCMmYCdV7m4850a+80fsL3VwbWbU8Cts8VQ2Twlv8iG6Kn NE+GBH39cbdM0n/kd4efvh/qSYNQlusEfLynr5czDal0qYAUONCmC5wsskr5BoIEco2o jolIyItbfrZMkXKa4/nN/K/0Dy+18bLnUJHBTcKsysGMZayndi/nezbaP2A6wR4crzJA DMLcIPGTS280ftJTyGXZiZA+U5J+KmpHSoTxKAT/MzkekYwhYLLOjSAN6plbYDkzUkzc 5Zdw== X-Gm-Message-State: AOAM530nF3kWH0T4R04E1gDo6o75TT+mUJ+Xaxxc3anpQ06jJfIScn4D wx+yif0kwbh8Ik0gJ5JOAgdKmrZRu14= X-Received: by 2002:a17:903:182:b0:140:47c1:1f13 with SMTP id z2-20020a170903018200b0014047c11f13mr14651407plg.63.1635258535420; Tue, 26 Oct 2021 07:28:55 -0700 (PDT) Received: from localhost ([47.88.5.130]) by smtp.gmail.com with ESMTPSA id e1sm20497103pgi.43.2021.10.26.07.28.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 26 Oct 2021 07:28:55 -0700 (PDT) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, Lai Jiangshan , Josh Poimboeuf , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH V4 17/50] x86/entry: Add C {user,kernel}_entry_swapgs_and_fence() Date: Tue, 26 Oct 2021 22:27:49 +0800 Message-Id: <20211026142824.18362-7-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211026141420.17138-1-jiangshanlai@gmail.com> References: <20211026141420.17138-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lai Jiangshan The C user_entry_swapgs_and_fence() implements the ASM code: swapgs FENCE_SWAPGS_USER_ENTRY It will be used in the user entry swapgs code path, doing the swapgs and lfence to prevent a speculative swapgs when coming from kernel space. The C kernel_entry_swapgs_and_fence() implements the ASM code: swapgs FENCE_SWAPGS_KERNEL_ENTRY It will be used in the kernel entry swapgs code path to prevent the swapgs from getting speculatively skipped when coming from user space. Cc: Josh Poimboeuf Suggested-by: Peter Zijlstra Signed-off-by: Lai Jiangshan --- arch/x86/entry/entry64.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/x86/entry/entry64.c b/arch/x86/entry/entry64.c index bdc9540f25d3..173cf621317c 100644 --- a/arch/x86/entry/entry64.c +++ b/arch/x86/entry/entry64.c @@ -49,6 +49,12 @@ static __always_inline void switch_to_kernel_cr3(void) {} * fence_swapgs_kernel_entry is used in the kernel entry code path without * CR3 write or with conditinal CR3 write only, to prevent the swapgs from * getting speculatively skipped when coming from user space. + * + * user_entry_swapgs_and_fence is a wrapper of swapgs and fence for user entry + * code path. + * + * kernel_entry_swapgs_and_fence is a wrapper of swapgs and fence for kernel + * entry with user gsbase code path. */ static __always_inline void fence_swapgs_user_entry(void) { @@ -59,3 +65,15 @@ static __always_inline void fence_swapgs_kernel_entry(void) { alternative("", "lfence", X86_FEATURE_FENCE_SWAPGS_KERNEL); } + +static __always_inline void user_entry_swapgs_and_fence(void) +{ + native_swapgs(); + fence_swapgs_user_entry(); +} + +static __always_inline void kernel_entry_swapgs_and_fence(void) +{ + native_swapgs(); + fence_swapgs_kernel_entry(); +} -- 2.19.1.6.gb485710b