From patchwork Thu Mar 2 05:25:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Xin3" X-Patchwork-Id: 63236 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp4064402wrd; Wed, 1 Mar 2023 21:56:41 -0800 (PST) X-Google-Smtp-Source: AK7set+YIEzlWUeYOz7CRx07Yr4S0ohBMFlR3elMCIOTwtIslICz26KV0GVyQPOVfo31OIEpJ7Q5 X-Received: by 2002:a17:90b:1d0e:b0:237:9858:ebbf with SMTP id on14-20020a17090b1d0e00b002379858ebbfmr10431061pjb.30.1677736601367; Wed, 01 Mar 2023 21:56:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677736601; cv=none; d=google.com; s=arc-20160816; b=Yc4oHDcLftWr1egXkCcLsb5e5QzPrA6aGzqVfWNif94lNNaqf5X1yad6ZRStNX5kTE lP6/xTM0vaEA+TmdpTSHU5HDE9sCA7HSDle6UZHqNJIYSRNkMS1SRVZdhLBNhMjoR1nY 7DHBrCjayxwvLFEi5IFJJcSZL5tiJtpq5dcnAg2GSjv61kaDq8S0iFyrbOsZ4sORqO3A /OqCsiPYPIgMXDQHZlNihdHBwe11OKKpO+82A2K5QAfZl2nSwY9rCgG6own9juHq/eHy B9AU/IZJphS6EoodHqL8ajeOe4ur5vLpzAInCHIUviXlzYXV+lmjZ8ukkiFGCRj+zliH gXkA== 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=evUt1h51br55zOpbl7ekH6vYbwwJKnK8ENE5d3fdDfI=; b=bn4FX8wfEkWBxQSwskTiFkhe4UbaPudurNsPjbXER5zCAq3g/BViTV3kmVJGXm6vvf jf15+aPrX6RiMBRCRN7VISzEsG3t9cYs7ga8E0pgv+3r75XaactokyvCk+OVEEZOMuHL 6BM3dKYP6Z49pQilw38/2NXv2fgaNTYCuLc73ienE36cuxwYvDTdj7whzH9Xl+CsPOzN BlNk8NHKDDjV6O87tgZnPzixsD4cvLCELnZiUBxbV6qBElDP43WWv+troutSDc1NCiz0 RFpHS0mS1Fm+OZUaDsjSC/ZuoZUmdp7kCG+bephfAx4TmLjMAzmyCWPsjUSCl9UH6shc 7y+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="Pd/0ZDSL"; 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=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x3-20020a17090a9dc300b00233f147ef63si1522150pjv.40.2023.03.01.21.56.27; Wed, 01 Mar 2023 21:56:41 -0800 (PST) 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=@intel.com header.s=Intel header.b="Pd/0ZDSL"; 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=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230114AbjCBFw4 (ORCPT + 99 others); Thu, 2 Mar 2023 00:52:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230104AbjCBFwX (ORCPT ); Thu, 2 Mar 2023 00:52:23 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 497CD55047; Wed, 1 Mar 2023 21:51:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677736287; x=1709272287; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pLas5p/5GAmOvBK0uD9R5N8t9jJL8ZoKvTscvrwkS8w=; b=Pd/0ZDSL1fPqBYZRIPo1FATEKZYxgGtgIK/oipGpz/CSMDqufEqh580J VP3XMqHjC8/9nZodWifOuRgoBd+v8wExgOQ3m45MgCM4+c26ueHTQEgjw KIb4BBx/hxmsI2RA5Mjj2Ru4Z2iq26v9ysr73QC4XOyBYlZwwoEwnNZg+ Ajd//Ooug4UxKQSrGyj96Nu2SdBc/r5+8vjMBVaD+RXSgyhUZKH8eQEBy 51Kpf9fusviOOdUdXkb8IySK2ePyoe/J/+OoQi5CaPEE1+BtbWfjXH+jb yt3JZgo0Dle+3UY2kBS7kQAYibxxjTNhf/orKFRAuqrJFUTGb/pzaL+2M A==; X-IronPort-AV: E=McAfee;i="6500,9779,10636"; a="420887292" X-IronPort-AV: E=Sophos;i="5.98,226,1673942400"; d="scan'208";a="420887292" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Mar 2023 21:50:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10636"; a="920530960" X-IronPort-AV: E=Sophos;i="5.98,226,1673942400"; d="scan'208";a="920530960" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmsmga006.fm.intel.com with ESMTP; 01 Mar 2023 21:50:50 -0800 From: Xin Li To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, peterz@infradead.org, andrew.cooper3@citrix.com, seanjc@google.com, pbonzini@redhat.com, ravi.v.shankar@intel.com Subject: [PATCH v4 27/34] x86/fred: allow single-step trap and NMI when starting a new thread Date: Wed, 1 Mar 2023 21:25:04 -0800 Message-Id: <20230302052511.1918-28-xin3.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230302052511.1918-1-xin3.li@intel.com> References: <20230302052511.1918-1-xin3.li@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759234334578984868?= X-GMAIL-MSGID: =?utf-8?q?1759234334578984868?= From: "H. Peter Anvin (Intel)" Allow single-step trap and NMI when starting a new thread, thus once the new thread returns to ring3, single-step trap and NMI are both enabled immediately. High-order 48 bits above the lowest 16 bit CS are discarded by the legacy IRET instruction, thus can be set unconditionally, even when FRED is not enabled. Signed-off-by: H. Peter Anvin (Intel) Tested-by: Shan Kang Signed-off-by: Xin Li --- arch/x86/include/asm/fred.h | 11 +++++++++++ arch/x86/kernel/process_64.c | 13 +++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/fred.h b/arch/x86/include/asm/fred.h index cd974edc8e8a..12449448e9bf 100644 --- a/arch/x86/include/asm/fred.h +++ b/arch/x86/include/asm/fred.h @@ -52,6 +52,14 @@ #define FRED_CSL_ALLOW_SINGLE_STEP _BITUL(25) #define FRED_CSL_INTERRUPT_SHADOW _BITUL(24) +/* + * High-order 48 bits above the lowest 16 bit CS are discarded by the + * legacy IRET instruction, thus can be set unconditionally, even when + * FRED is not enabled. + */ +#define CSL_PROCESS_START \ + (FRED_CSL_ENABLE_NMI | FRED_CSL_ALLOW_SINGLE_STEP) + #ifndef __ASSEMBLY__ #include @@ -115,6 +123,9 @@ void fred_setup_apic(void); #else #define cpu_init_fred_exceptions() BUG() #define fred_setup_apic() BUG() + +#define CSL_PROCESS_START 0 + #endif /* CONFIG_X86_FRED */ #endif /* ASM_X86_FRED_H */ diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index ff6594dbea4a..b23850352e7d 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -55,6 +55,7 @@ #include #include #include +#include #ifdef CONFIG_IA32_EMULATION /* Not included via unistd.h */ #include @@ -506,7 +507,7 @@ void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase) static void start_thread_common(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp, - unsigned int _cs, unsigned int _ss, unsigned int _ds) + u16 _cs, u16 _ss, u16 _ds) { WARN_ON_ONCE(regs != current_pt_regs()); @@ -521,11 +522,11 @@ start_thread_common(struct pt_regs *regs, unsigned long new_ip, loadsegment(ds, _ds); load_gs_index(0); - regs->ip = new_ip; - regs->sp = new_sp; - regs->cs = _cs; - regs->ss = _ss; - regs->flags = X86_EFLAGS_IF; + regs->ip = new_ip; + regs->sp = new_sp; + regs->csx = _cs | CSL_PROCESS_START; + regs->ssx = _ss; + regs->flags = X86_EFLAGS_IF | X86_EFLAGS_FIXED; } void