From patchwork Fri Feb 24 07:01:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Xin3" X-Patchwork-Id: 61173 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp761779wrd; Thu, 23 Feb 2023 23:45:49 -0800 (PST) X-Google-Smtp-Source: AK7set+/Rnq64CCPdE8wyVC0KF7aD74UkZH/X1rXlxWN25vlwABJZVBh+NVDoHfJ2P2j9dqCmmUN X-Received: by 2002:a05:6a20:1601:b0:bb:9ad9:4967 with SMTP id l1-20020a056a20160100b000bb9ad94967mr8654808pzj.44.1677224749659; Thu, 23 Feb 2023 23:45:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677224749; cv=none; d=google.com; s=arc-20160816; b=q6rs/TzcymdvCvFQANjkb6lWeNbdoujX9RpDFxCDQTCSqpxfKIZSVRIqY+PzeC3hg1 6wjOEm9E3tXHCiCWkjcXP/HWaAahZu6nMeibphCt1+dK9jZZlmGTly7vSfZlgPrd0sCv GgcaIKYnHHMgvUnoJULzwwte+XBTr15B19XnKFsAN9+xLTaJE/s+LmgEyEx1yDB2lfrM o8JedU2QDC6Jn/EGHijfKlYT+u9eJwcLeJSvE2AtZIWA97pKdPT+bLwxKo/kpaKnujh4 nHOZEfGsujpY2vftfmRjh7ZXlGM7WApD4mEGfr6BtAvQwyonBJt/bNSHzMUYW+RN9FH6 4Ggw== 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=VpqqLq+HID8y7/JJBNZ6gXdLmBlckDyd7LeeaWr/b9k=; b=rxTIXmmW61tYuvx+LJGt9W0b3ldss09tQ5ZoQrdeiTrv1VwfIvFcSSrW3U4P9Ho+ZI wq7rVxU6TlEAHC1p1DKHxknQQC5WpC7oniSJ5WH24lcSa6ecMZpngC5L/J3nuoSpj1et k4XmvODuF9WU5KHXv4MG1lYifwhVtyj4S57UoUp1chlUtpZdkZJwQsPtNUZyPsVnbDgu 8OcLmNKO+fb7QTSSCXfKmr+ncld3Eyf32fFf5Dhu7SA6wBUI//QgUr13hyTSZI5LVCyo xbex6k3lXBFD86PTIrdNhxWaSRIS9XD97NlnGye/L/JCzOij50sJC2L6677KQqN08j0R A1BA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZPMA+RB5; 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 u24-20020a631418000000b004fd1ec2bf42si4470583pgl.567.2023.02.23.23.45.37; Thu, 23 Feb 2023 23:45:49 -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=ZPMA+RB5; 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 S230086AbjBXH3q (ORCPT + 99 others); Fri, 24 Feb 2023 02:29:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229801AbjBXH3U (ORCPT ); Fri, 24 Feb 2023 02:29:20 -0500 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D62BC63DD2; Thu, 23 Feb 2023 23:28: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=1677223706; x=1708759706; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eNc5nbGpboFMb1VqnWz9V+WMQa1u5sx4ZurIW4MEGgg=; b=ZPMA+RB5PiTkAWZEEM5lkeRixH5/ZcPNuQGSG12iOe9+ICygbA/0kj6v 72fKW9fCkuw4dDDr7raM0heW3Ytr/moY9PbKzGU2qs34bB13XI0+tr68+ Qxk8H5zAvgiCWg16+n8movixqvLEP2a+a1R6Aj/YMRU1/15OvOSVi6yE7 2z/XY65XB8ZZhqTxF9LWy/RWZca1T8SSYjRsk4mQJCjA6kxKK/0uSaLFF b3FLIgc/2v0FOLlBmpDnbTBqrFooH/pccc1dhX/b3jcxP2msaP9XQTjUf NcYyDOokrRF5hYlxw4xCMLzQbK25kGsA2DYPEjNQOAiJJzwdEbQaTGA7M w==; X-IronPort-AV: E=McAfee;i="6500,9779,10630"; a="334836150" X-IronPort-AV: E=Sophos;i="5.97,324,1669104000"; d="scan'208";a="334836150" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Feb 2023 23:27:26 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10630"; a="815639274" X-IronPort-AV: E=Sophos;i="5.97,324,1669104000"; d="scan'208";a="815639274" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmsmga001.fm.intel.com with ESMTP; 23 Feb 2023 23:27:25 -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: [RFC PATCH v3 27/32] x86/fred: allow single-step trap and NMI when starting a new thread Date: Thu, 23 Feb 2023 23:01:40 -0800 Message-Id: <20230224070145.3572-28-xin3.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230224070145.3572-1-xin3.li@intel.com> References: <20230224070145.3572-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, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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?1758697619362689966?= X-GMAIL-MSGID: =?utf-8?q?1758697619362689966?= 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) 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 6a417fc1e7ee..958751f16c94 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..ac3895f4a360 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->csl = _cs | CSL_PROCESS_START; + regs->ssl = _ss; + regs->flags = X86_EFLAGS_IF | X86_EFLAGS_FIXED; } void