Received: by 2002:a05:6a10:8395:0:0:0:0 with SMTP id n21csp425859pxh; Wed, 10 Nov 2021 04:04:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJxn/ktQO2NgAEGyuG+fgU92NCb46x7Efq8G2TQSs1wxs83DoCjz5AW8/gIZfF2hWehq+sKl X-Received: by 2002:a17:906:2cd5:: with SMTP id r21mr20577549ejr.435.1636545878653; Wed, 10 Nov 2021 04:04:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1636545878; cv=none; d=google.com; s=arc-20160816; b=UIlw8GsDGEyQNf60+t6Z59Jd/LjzCQDZzsiBWYsbGkAA6HRom/4CP14B75D1BPTDve HbDNPqUJnWR2mo/tpOKeWvRzZlViBdoMT3m9jFTCzA8eiUPSr0rrG0dPYWO24K5cZZjT 7joAI6q6aa9IVnQB0USjFEUlw6IQtA3GrnSv7i1gTw7CzqlKpTOMu1QVobXwD/CauYUV ELr0X+zotCYyDK2bnw+2neKO0Buk6kE8ojyhjBGLVG0FfzzR35R61lekgncp92L1Dd5y YpdB1bV0T/rJtb2tvFeIMH3cnIKlVz5rtmRTPTK4AByJFNeC7dHGg8tKdFi8HZoDhtIU HAAQ== 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=3kMfdZR7u3SGVAlrTiFoKn2/xZAEuhY7vC7t+axQgCY=; b=DRCnkKL77VAzYa0olrP54VxHHZxNgMRUP4ar6IrwZomYx7QgLpc0NxWfG9EvkHhxX+ 2wWx1XIhLvUM3mJt1ELOMu6+Cpacu7wCqbPoNwnqOH/RPVXp/CfZ3RpjsuuQz3H0zPz7 Y8n6uehp1RDK4ZSnWG58yON8nu5CizcyW+Hl2mppyKWwsJHGm469XFR6wkEsdQw4NSCV sONzWYhoe2N2uW5/sfQH9xUNlWVU0hO9wCjGKv8QnZJu/vJiBkknx2kGj+fGGdIhsOxm vhVa+WYV+YZLSDUWfuTEOJN5SIpTE2Qp5zoRP7B2JxpOcxRFMh2OzxhBTzwf5f8DE21D loeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jehYUvu9; 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 t16si36672953eds.558.2021.11.10.04.04.13; Wed, 10 Nov 2021 04:04:38 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=jehYUvu9; 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 S231668AbhKJMBz (ORCPT + 99 others); Wed, 10 Nov 2021 07:01:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231488AbhKJMBw (ORCPT ); Wed, 10 Nov 2021 07:01:52 -0500 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E896C061767 for ; Wed, 10 Nov 2021 03:59:05 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id x7so1342856pjn.0 for ; Wed, 10 Nov 2021 03:59:05 -0800 (PST) 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=3kMfdZR7u3SGVAlrTiFoKn2/xZAEuhY7vC7t+axQgCY=; b=jehYUvu9M6N7NpDrUTNNnGX0W3hevA4GxuCLXRywLPzc8Wl3hUsO8MPLt2FjPrjfUc AlMi4v+QbrXmKB8BaqnFjwblM0uohpa1OWc1BflYhmCbEInQ0Lt270O3BKfdjgb6ClPr z4t/nv6RyXMmE4f8kHBiR2ptWuwLwo+8c0TS6vnvMLek95mnfIKAa0J5i860HVWwbSaH Ql+2ZK6JZPbffcb4HjFZ2dDnoFoRFx6B75yAKRgHduoZ9zT5qdICcPsa3J4mwc99gG7B T4t9jKsz3mS4I8mfqT2s+V8SlVo0HgXoDKvFUp4adBw4JPmfyD7Y2Iuss+jW49/jEzmL eoYQ== 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=3kMfdZR7u3SGVAlrTiFoKn2/xZAEuhY7vC7t+axQgCY=; b=zS+0sclGoppZ7HcuJtQ6f287bPfCIEh8egiOJO4yNZNQ9/Ir/9jI47ImOsJCR7ihqX vwoZonPbW/qRek9EpYNeb3v5T2xPxsLIdmMqEJDtR+B41mbEbw68dv2WvOO+RESyG5F9 SOm9V/51na7x4HNVBZ/svBFOOwYeauF1VhvCAhFsu9Kbd7NkMJZvOD4B8mGuNkCkTnnG liUrHmaSSrtRqf/GnAN+w88irwZ58uYp3PeAfKoGA9g7BDcRvZ7orP/m5uz3oUeFxZkC 07854TkGTr1Y2Skce6dRzhwGK4tFpcQ2FIvPB5ed3r1iU9gA67xbcPsNiqdn4TjXhtZB swwA== X-Gm-Message-State: AOAM5321kRfseFnfivE1G29rSvnd/81zre5ZtelxPU6mUKHm+MnqhcJm phdMiC+lamJkBcJ7PqI3IffkN/o0I/A= X-Received: by 2002:a17:90b:1bd1:: with SMTP id oa17mr16456308pjb.246.1636545544457; Wed, 10 Nov 2021 03:59:04 -0800 (PST) Received: from localhost ([198.11.178.15]) by smtp.gmail.com with ESMTPSA id bf19sm5581227pjb.6.2021.11.10.03.59.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Nov 2021 03:59:03 -0800 (PST) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, Lai Jiangshan , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: [PATCH V5 15/50] x86/entry: Add C verion of SWITCH_TO_KERNEL_CR3 as switch_to_kernel_cr3() Date: Wed, 10 Nov 2021 19:57:01 +0800 Message-Id: <20211110115736.3776-16-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211110115736.3776-1-jiangshanlai@gmail.com> References: <20211110115736.3776-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 version switch_to_kernel_cr3() implements SWITCH_TO_KERNEL_CR3(). No functional difference intended. 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 9813a30dbadb..9a5c535b1ddf 100644 --- a/arch/x86/entry/entry64.c +++ b/arch/x86/entry/entry64.c @@ -14,3 +14,27 @@ #include extern unsigned char asm_load_gs_index_gs_change[]; + +#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