Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4090433pxb; Wed, 13 Oct 2021 20:45:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwgAWkJy4pG3FOBrEOQbwAshAUK72AIztdgMctesbVX2LFkYxFW4ZBQYC+yaEpcQ8GadxzL X-Received: by 2002:a17:90a:191a:: with SMTP id 26mr3751384pjg.79.1634183120625; Wed, 13 Oct 2021 20:45:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634183120; cv=none; d=google.com; s=arc-20160816; b=F8gUOsDmzqQW+LttEq8HGvMq4PwaL5tMA/u61VlkRT7IrPpBymLuux1mHo/k1fu7pF LjNxzYFo1NeqwMhuoO4J4kyFr6/eOllYa8y+G8r4bcQUNymiZROtzanxXvYU7dR4URKL xiGWrTkMHqtxUbOEPhBfyOR47uLIURPF8D7n8J55YRacAmozWNX+j8QBnOgLJEU568j4 qhjBnFTec+wMz8uKHng4lu3gtiHDJb9g2XWQU2OqmUHctO6iPxI8hN2lM2a4oXYr2/WJ Sb84V+W0RjzmJxUZ2W/9avCsunBfgJuE7C1j3fg4KS+Hcl8efK0NwWrMiCkmqPghzifu ggFw== 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=uKNvVQ6CjLjkusQqsZ94xWd5Ds6mJwvJCMcrOnvil24=; b=UMbup1n1KodxHMAajcaIx9zL2zV4IZYxNmaSYCtflfD+Ijo1ixpor46NkZP2iJo6G0 H41IuCHXRiW/BPzl5lWaqbPf1bWOAMdsStQ2ZdYY00E1nK1Z4ILgJGqnHjbY4pJ3VsHN kZSiGunMG7oit1U4c+V1K3Hry31L7TbsXJ6OZo1eaihPTJBtc7kzbiyzOQYR9S5izgon BRz2+9pI79MY64whHDwAgDtdAbWHRDhuiJmMR54LloBfxwm4HZdPrc/qEoHw5BJ88iZC NBUb+jWg2lZLozwfeYN+J3Hr64l8IG4d2hW2dN2Yur5DWaXRC0LLrfj3NYzH94FyebXF fsiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=J4zPbRGC; 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 c10si2322793pgn.220.2021.10.13.20.45.08; Wed, 13 Oct 2021 20:45:20 -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=J4zPbRGC; 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 S229902AbhJNDom (ORCPT + 99 others); Wed, 13 Oct 2021 23:44:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230032AbhJNDnz (ORCPT ); Wed, 13 Oct 2021 23:43:55 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CBA5C06174E for ; Wed, 13 Oct 2021 20:41:41 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id i76so2139175pfe.13 for ; Wed, 13 Oct 2021 20:41:41 -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=uKNvVQ6CjLjkusQqsZ94xWd5Ds6mJwvJCMcrOnvil24=; b=J4zPbRGCtQ1rSy1irQqwZ2TMpvhFvCAvhV3hlfE7k5zUVQOScoyyFsgft7sljwpqFx J971B7tHjycDDmfNMqATnl7WzSXAVv7DPVNKVAfTcWRiSJyzjCiq0Jhac2IsvWKRvAc9 lS4NHqI7ncUZPtUTDdTekYdCKhcnKvzZAT2fNEpMOdZWobUP7vsU8R9Ra+jvErP1E1qk YigtrvuKwLCNetL+hM22C7pA72K0W/dGwJcQROIx3jKp4oFUcwqG6d9TtT8m7Xj+/QGs EwzuUSIfxCQsONa+B4+4d537dY8jLCaxo80TobLNIB5Djh9tL3E5V32rHEurfrM/aBf/ zHkw== 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=uKNvVQ6CjLjkusQqsZ94xWd5Ds6mJwvJCMcrOnvil24=; b=25TjpaEyXnwpEvk3u0H/BSwxOyXUn34FxB6xmWL4hRFv/GXpyJx8Td3WYYmetnOLst M826lZ6rMIx8KwXXWJt4x+v8WKHLpOMX6sjFDu4F4OSRlOIk51Tx3NowgI4KVf5/A7GX j14/eyu+Kt1xmH1psA4+fclegNBmzNewCjLBmiEk7AY6xW8DqOtRNOQyf2JF9wrJ0aHg yaTqb8EMHJwTmdhpMff8apbuZq4+JGM/EWF4avZfVVbWwxnDbTZubG/kkGgcY1sZsgK0 Aa/qoQQwrnDo/1MEwvfaF+P9E9tmK3JNP6GMcXMTF9pvEL7y536wnTOARCN5NgUCWgqK I9OQ== X-Gm-Message-State: AOAM531Z4FVSREKlMYi4+QMWjJPvCxzHd6Uuo2aptpAPjmeas1FrQOfJ UKoRJldG+StCIo4jHf+tAPhDSSkZdbM= X-Received: by 2002:a62:2c82:0:b0:44d:71c3:8a3 with SMTP id s124-20020a622c82000000b0044d71c308a3mr3245710pfs.84.1634182900537; Wed, 13 Oct 2021 20:41:40 -0700 (PDT) Received: from localhost ([198.11.178.15]) by smtp.gmail.com with ESMTPSA id 63sm833631pfv.192.2021.10.13.20.41.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Oct 2021 20:41:40 -0700 (PDT) From: Lai Jiangshan To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, Lai Jiangshan , Andy Lutomirski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Juergen Gross , "Peter Zijlstra (Intel)" , Joerg Roedel Subject: [PATCH V3 35/49] x86/entry: Implement the C version ist_paranoid_entry() Date: Thu, 14 Oct 2021 11:41:07 +0800 Message-Id: <20211014034121.17025-4-jiangshanlai@gmail.com> X-Mailer: git-send-email 2.19.1.6.gb485710b In-Reply-To: <20211014031413.14471-1-jiangshanlai@gmail.com> References: <20211014031413.14471-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 It implements the whole ASM version paranoid_entry(). No functional difference intended. Signed-off-by: Lai Jiangshan --- arch/x86/entry/entry64.c | 37 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/idtentry.h | 3 +++ 2 files changed, 40 insertions(+) diff --git a/arch/x86/entry/entry64.c b/arch/x86/entry/entry64.c index 06f7476d78b4..8e1474ece7f9 100644 --- a/arch/x86/entry/entry64.c +++ b/arch/x86/entry/entry64.c @@ -288,3 +288,40 @@ static __always_inline unsigned long ist_switch_to_kernel_gsbase(void) /* SWAPGS required on exit */ return 0; } + +/* + * Switch and save CR3 in *@cr3 if PTI enabled. Return GSBASE related + * information in *@gsbase depending on the availability of the FSGSBASE + * instructions: + * + * FSGSBASE *@gsbase + * N 0 -> SWAPGS on exit + * 1 -> no SWAPGS on exit + * + * Y GSBASE value at entry, must be restored in ist_paranoid_exit + */ +__visible __entry_text +void ist_paranoid_entry(unsigned long *cr3, unsigned long *gsbase) +{ + /* + * Always stash CR3 in *@cr3. This value will be restored, + * verbatim, at exit. Needed if ist_paranoid_entry interrupted + * another entry that already switched to the user CR3 value + * but has not yet returned to userspace. + * + * This is also why CS (stashed in the "iret frame" by the + * hardware at entry) can not be used: this may be a return + * to kernel code, but with a user CR3 value. + * + * Switching CR3 does not depend on kernel GSBASE so it can + * be done before switching to the kernel GSBASE. This is + * required for FSGSBASE because the kernel GSBASE has to + * be retrieved from a kernel internal table. + */ + *cr3 = ist_switch_to_kernel_cr3(); + + barrier(); + + /* Handle GSBASE, store the return value in *@gsbase for exit. */ + *gsbase = ist_switch_to_kernel_gsbase(); +} diff --git a/arch/x86/include/asm/idtentry.h b/arch/x86/include/asm/idtentry.h index 49fabc3e3f0d..f6efa21ec242 100644 --- a/arch/x86/include/asm/idtentry.h +++ b/arch/x86/include/asm/idtentry.h @@ -307,6 +307,9 @@ static __always_inline void __##func(struct pt_regs *regs) DECLARE_IDTENTRY(vector, func) #ifdef CONFIG_X86_64 +__visible __entry_text +void ist_paranoid_entry(unsigned long *cr3, unsigned long *gsbase); + /** * DECLARE_IDTENTRY_IST - Declare functions for IST handling IDT entry points * @vector: Vector number (ignored for C) -- 2.19.1.6.gb485710b