From patchwork Tue Dec 5 10:50:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Xin3" X-Patchwork-Id: 173988 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3359239vqy; Tue, 5 Dec 2023 03:22:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IHP/URxZ/IRdmacHFbI1hyL0tAAIxrMPMmKf2d57ddwLwlhw8/uBI1hlWFbcc7J73mBvKfy X-Received: by 2002:a05:6a20:1610:b0:18b:9053:d865 with SMTP id l16-20020a056a20161000b0018b9053d865mr6123797pzj.42.1701775370044; Tue, 05 Dec 2023 03:22:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701775370; cv=none; d=google.com; s=arc-20160816; b=1EqlIZRBNw+suCwVTH45GjWIlUqzBGGj4GCScEQsYmb0Mv87zpyrs2QZ9VGFp2ZQsH 6BCYQ8wHsRcoA/KygznUJeqFx7RO5VKuG2f4HCSrTQ6xbRHRNxkt2PwYWqRoduman4Qq bsyb3CVRjG51xHTDwRNz7Mgcc1+ywC1rucPMX6+1JQ7qaILEiQPz0/I9cpgTj0dwbpbH yQ0Og/3enG7ema/di5XjiU0Gv9Q+TBxnfkUYEHlUTWNuEBgDLROA35oNVG2T6X6jirG+ JTPToKojGKW6tJK2H0RSFaxbBzYXWCf4VjK4mqKWAPXQFuv7nAaKvvyfyUTZhRMRSMW4 JcoA== 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=6HH2mDkuMBEjf5Od79G87c1nkRT3q94619zejo/f/RM=; fh=uCCWEUVpLc/xDP/D3WvGY/BxHU9d0NQYu/T71GEsNmA=; b=yjNrCN+g0YHZrDxN5aw5TrPtFtOC3vxJfaACqghNY3a8fn8WyoeVQCeusg91kVG2lC FGgNg2w7YEniMxHIRQj+26ASsCi+wfvPRR80ksF6086Br3lz1oJ2Piw0jK5obJVECia2 ENEaRD5m8Z2l2+jUV4vRN8tve9qTrlvefVV2bl4EeMrWCS3FiUUJKwtheidxBXFE72Yn I8QfrMA7lfEN6D/ucIe4Ti6W7AGRQbwAgT2Pc7yu4dGH746QwIomrDsSsD2fA0Z9t5Ku Cc5uAoN1XSfiwkpQ+SKFM9HTgayQIjZSSk8UY7m/fqKgnBvmTO+1qP6g2AJZvbSiCIsV ORRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bSXS6L4r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id 142-20020a630194000000b005bd2b19dfc4si9860566pgb.493.2023.12.05.03.22.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 03:22:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bSXS6L4r; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0125380C0A55; Tue, 5 Dec 2023 03:22:49 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442204AbjLELW1 (ORCPT + 99 others); Tue, 5 Dec 2023 06:22:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35080 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442136AbjLELVa (ORCPT ); Tue, 5 Dec 2023 06:21:30 -0500 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2ED5191; Tue, 5 Dec 2023 03:21:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701775291; x=1733311291; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=g+1BPOHGWOCmAN2ihj8vafw+Z+qq/YZJt19PCQv0I50=; b=bSXS6L4rRWoTVSOpcVap6XvNVoD8BW+aww5jAQrZ4tiqKRGLeeGftAzo I/j9hA2wBEdmgwWCpO/T3esiw3qHr+mywJlnULjh9WMlNmwwvlwfL16sS 5xPrZB4UM7dOgCUGFkmLx7xhBBS2Cy/PsDdEYTeAcU4yppRzql9vqDaJz JOhyvs1tZeiUJi4d/AqTGsJbrCezCOXayyop6c3yycL6YYTRKopdBmX1J lsYZjXMpFGLjZytjJuKHF7mk5WJbJ0averYf6g71Xef2/+w5sVMYUbbOj Tuo+gwW/oBX8VsNfgMPweYAIDv9X2JLnoePmWRibL6ASeQPuZfPEEYXnI Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="942543" X-IronPort-AV: E=Sophos;i="6.04,252,1695711600"; d="scan'208";a="942543" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2023 03:21:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10914"; a="1018192968" X-IronPort-AV: E=Sophos;i="6.04,252,1695711600"; d="scan'208";a="1018192968" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmsmga006.fm.intel.com with ESMTP; 05 Dec 2023 03:21:22 -0800 From: Xin Li To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-edac@vger.kernel.org, linux-hyperv@vger.kernel.org, kvm@vger.kernel.org, xen-devel@lists.xenproject.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, luto@kernel.org, pbonzini@redhat.com, seanjc@google.com, peterz@infradead.org, jgross@suse.com, ravi.v.shankar@intel.com, mhiramat@kernel.org, andrew.cooper3@citrix.com, jiangshanlai@gmail.com, nik.borisov@suse.com, shan.kang@intel.com Subject: [PATCH v13 20/35] x86/fred: Allow single-step trap and NMI when starting a new task Date: Tue, 5 Dec 2023 02:50:09 -0800 Message-ID: <20231205105030.8698-21-xin3.li@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231205105030.8698-1-xin3.li@intel.com> References: <20231205105030.8698-1-xin3.li@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 05 Dec 2023 03:22:49 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784440810197963415 X-GMAIL-MSGID: 1784440810197963415 From: "H. Peter Anvin (Intel)" Entering a new task is logically speaking a return from a system call (exec, fork, clone, etc.). As such, if ptrace enables single stepping a single step exception should be allowed to trigger immediately upon entering user space. This is not optional. NMI should *never* be disabled in user space. As such, this is an optional, opportunistic way to catch errors. Allow single-step trap and NMI when starting a new task, thus once the new task enters user space, single-step trap and NMI are both enabled immediately. Signed-off-by: H. Peter Anvin (Intel) Tested-by: Shan Kang Signed-off-by: Xin Li --- Changes since v8: * Use high-order 48 bits above the lowest 16 bit SS only when FRED is enabled (Thomas Gleixner). --- arch/x86/kernel/process_64.c | 38 ++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 7f66c0b14de6..7062b84dd467 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -56,6 +56,7 @@ #include #include #include +#include #ifdef CONFIG_IA32_EMULATION /* Not included via unistd.h */ #include @@ -528,7 +529,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()); @@ -545,11 +546,36 @@ 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; + regs->ssx = _ss; + /* + * Allow single-step trap and NMI when starting a new task, thus + * once the new task enters user space, single-step trap and NMI + * are both enabled immediately. + * + * Entering a new task is logically speaking a return from a + * system call (exec, fork, clone, etc.). As such, if ptrace + * enables single stepping a single step exception should be + * allowed to trigger immediately upon entering user space. + * This is not optional. + * + * NMI should *never* be disabled in user space. As such, this + * is an optional, opportunistic way to catch errors. + * + * Paranoia: High-order 48 bits above the lowest 16 bit SS are + * discarded by the legacy IRET instruction on all Intel, AMD, + * and Cyrix/Centaur/VIA CPUs, thus can be set unconditionally, + * even when FRED is not enabled. But we choose the safer side + * to use these bits only when FRED is enabled. + */ + if (cpu_feature_enabled(X86_FEATURE_FRED)) { + regs->fred_ss.swevent = true; + regs->fred_ss.nmi = true; + } + + regs->flags = X86_EFLAGS_IF | X86_EFLAGS_FIXED; } void