Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp3281349iob; Mon, 16 May 2022 18:02:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwiFql/Fp7CiaPas583ryK9MIPfs9b9YP9XnZsZnUcpaqCTelsJrc8Z0q/LWDGm7Ayj2iHy X-Received: by 2002:a17:907:98cf:b0:6f3:9901:bc0c with SMTP id kd15-20020a17090798cf00b006f39901bc0cmr16826535ejc.315.1652749356707; Mon, 16 May 2022 18:02:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652749356; cv=none; d=google.com; s=arc-20160816; b=Q6Z3thU3mNJx7HZX6nJszxupiK6+mD+4gzSFyALmaOm6CBFH17/qQhJspUwpoHDU6W cvlyXZ2wddSZXUsh7ygDeJ4F8p6OE0SA+0PW9Yd5y1xKhUAyMPXxR5OEit1H3Zu4eudi z+zZVBVmyy7Z6bhzhbYald8ZfgM67NygxEZtmWOtWkJkqYTYJYlE7GOCyVRROUefiaY0 DsPYdDcl8oGF102S22iuS+pjB2OHAx8X4kyCMP3r6s2dHFajtNc7Vbq0CQ/GQ2AGMtdB 4q+of6T3K76wgV9/QFu9+q2Wc7dVi4FWAqX1L23SaMLJWs9cETUlLwgo9BkS8jAxEt+d 8Z4w== 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=FSeMlanFh2svXxaNRkwpD5s2Jt4O19S77leAuNHNOlo=; b=MRUgfuMRUh0JQqbMmCgnwxsSUCq3YUBJeGmoU5PW3+QV5ce4ldv4BN2zEtF6qYz0WA GbQCOVNsaC+IArmK0Rd+arVQKzwNasS0KBaglQXaZRRvYP/5MUm2oPqno5rJPcCdKyVP 2wLL4wefcWS4+H1oJ0+2G/InIXsO1lez0dzci98G5WTbT0B47GkOiv7XJrd370BM/eXX rYIYQ9HrXza0mpnQ5CJ3B1qDr0tTe87Sf5ZGs50CLOiY4g8hqbc94KTCoRdaUYhX9WLl YpCgzsTG/T5dDeuwOIbr/f1iQB9N96oDkoBynHyxF5PZLjPk6u3PzFB1fhzZzTfMn3iZ 9U/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=f5bpF3Jg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hq4-20020a1709073f0400b006e8c412ccc4si1273359ejc.296.2022.05.16.18.02.08; Mon, 16 May 2022 18:02:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=f5bpF3Jg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S243841AbiEPNRf (ORCPT + 99 others); Mon, 16 May 2022 09:17:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243796AbiEPNRV (ORCPT ); Mon, 16 May 2022 09:17:21 -0400 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A01A52529B for ; Mon, 16 May 2022 06:17:20 -0700 (PDT) Received: by mail-pj1-x1036.google.com with SMTP id t11-20020a17090a6a0b00b001df6f318a8bso1253845pjj.4 for ; Mon, 16 May 2022 06:17:20 -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=FSeMlanFh2svXxaNRkwpD5s2Jt4O19S77leAuNHNOlo=; b=f5bpF3JgDxt953UNKD7px9h21rVHMzamUxSzdhuN+W5C3vs/DafoFQLV2eCR4gml3C SY0T1kPjdmclW0ZLySYYdws0f4oCIs9wH3olszqyO7jfj5rt/XGVkV4jpg/uQK3Tk8SO Y+quy7MLrfa7JseKtmQ0KArxfT2wgHF+yObcsJ2DetEcGj8nyi7IIXnTr1xm3KZbQrvF TdLQ0FQnnSx/5tHQx7sMGyVJmUZt6wVITKvawFvVjC2iL0dIcQiCzcxewzMMIY2sPjV2 4HX/N8+qoi/Pnx/Z4tthJDMNXQeNNNHyEJfQ3fA5lIJEAcvzsI1bt18UHNDXsYf1uwlt 3B3g== 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=FSeMlanFh2svXxaNRkwpD5s2Jt4O19S77leAuNHNOlo=; b=Wjem9v6jcn/ZjKylkym0mN2SRjQlllM07che1zwLPNCnD0VKKzxn/1z/mne+4SKI3H 78LioS+86RE00SjCQoRq4ZHc93nZHMVI+Ve16+Pqe6GcoLkGRbLl9Dv5GckpsAJONsdS hdIhrL3gLtAYKrX3iN8j4KtRSM90jcjjKNKfafv9FkIdc1y99ietuYq9k1xJ0czMj7A3 31LPc4rmF6BopVebfxGPE1oSBjbTaRY6AUrRzltT9qQhJMspfPTunmtPi58PSMwaK10w 4Y0uMi/dR4u4FPS5TRhogyyoSRB1FTHEXoKYB9fjICDS1vbZ5TMY3spvel2sqlngQGVv IRSQ== X-Gm-Message-State: AOAM531ik0P9s9/Ic/VT+0/0biby1VOh2+8PKv6KMq+1kXbG3Si9DpQM +BPqA5ZlyeDEDM2zgQMMnOPr8KdsBvc= X-Received: by 2002:a17:902:9a4c:b0:156:6735:b438 with SMTP id x12-20020a1709029a4c00b001566735b438mr17862669plv.46.1652707039909; Mon, 16 May 2022 06:17:19 -0700 (PDT) Received: from localhost ([47.251.4.198]) by smtp.gmail.com with ESMTPSA id h17-20020a62b411000000b0050df474e4d2sm6807753pfn.218.2022.05.16.06.17.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 May 2022 06:17:19 -0700 (PDT) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: Borislav Petkov , Peter Zijlstra , Josh Poimboeuf , Andy Lutomirski , Thomas Gleixner , x86@kernel.org, Lai Jiangshan , Ingo Molnar , Dave Hansen , "H. Peter Anvin" Subject: [PATCH V2 5/7] x86/entry: Add the C verion of SWITCH_TO_KERNEL_CR3 as switch_to_kernel_cr3() Date: Mon, 16 May 2022 21:17:36 +0800 Message-Id: <20220516131739.521817-6-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20220516131739.521817-1-jiangshanlai@gmail.com> References: <20220516131739.521817-1-jiangshanlai@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lai Jiangshan Add the C version switch_to_kernel_cr3() which implements the macro SWITCH_TO_KERNEL_CR3() in arch/x86/entry/calling.h. No functional difference intended. Note: The compiler generates "AND $0xe7,%ah" (3 bytes) for the code "cr3 = user_cr3 & ~PTI_USER_PGTABLE_AND_PCID_MASK" while the ASM code in SWITCH_TO_KERNEL_CR3() results "AND $0xffffffffffffe7ff,%rax" (6 bytes). The compiler generates lengthier code for "cr3 |= X86_CR3_PCID_NOFLUSH" because it uses "MOVABS+OR" (13 bytes) rather than a single "BTS" (5 bytes). Signed-off-by: Lai Jiangshan --- arch/x86/entry/entry64.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/arch/x86/entry/entry64.c b/arch/x86/entry/entry64.c index ace73861c2a0..bd77cc8373ce 100644 --- a/arch/x86/entry/entry64.c +++ b/arch/x86/entry/entry64.c @@ -12,3 +12,27 @@ * is the PTI user CR3 or both. */ #include + +#ifdef CONFIG_PAGE_TABLE_ISOLATION +static __always_inline void pti_switch_to_kernel_cr3(unsigned long user_cr3) +{ + /* + * Clear PCID and "PAGE_TABLE_ISOLATION bit", point CR3 + * at kernel pagetables: + */ + unsigned long cr3 = user_cr3 & ~PTI_USER_PGTABLE_AND_PCID_MASK; + + if (static_cpu_has(X86_FEATURE_PCID)) + cr3 |= X86_CR3_PCID_NOFLUSH; + + native_write_cr3(cr3); +} + +static __always_inline void switch_to_kernel_cr3(void) +{ + if (static_cpu_has(X86_FEATURE_PTI)) + pti_switch_to_kernel_cr3(__native_read_cr3()); +} +#else +static __always_inline void switch_to_kernel_cr3(void) {} +#endif -- 2.19.1.6.gb485710b