From patchwork Sat Sep 23 09:41:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Xin3" X-Patchwork-Id: 143866 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp91702vqu; Sat, 23 Sep 2023 03:18:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGVzCac7OSmLZ9vkBraPWHXIHQB+c/9FEoEZmJDNJYPfhJleEfNgySCwvESmAq+THTG7ruq X-Received: by 2002:a05:6a00:194a:b0:68a:4d66:caf with SMTP id s10-20020a056a00194a00b0068a4d660cafmr1440901pfk.34.1695464297728; Sat, 23 Sep 2023 03:18:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695464297; cv=none; d=google.com; s=arc-20160816; b=UezRUDg3gkGb5ZZVOcLAUsAHh94wH6V4NPtbmW9AhaqbNzOQzNtQvzQ+uFhGzuf1Km W0lKCAjrm5iRhkF5MYfeXVUniJSgo1JpqCatECxQtK6LbB2Le2BOSjTeSt+Sxj5pytTQ P9dFzybpyt4Fu1uMOPIUfmhWSQYFdWDLlXXAxq6f+vX52/pXZ45P6jclaezAE5oF6zsE SwhjFneoLkjjnPz86ZiHHuipFJfnw1NPQvkJfSKe+no+96yigkFNfzYqsJepqalMDE2p Ep3Dy8a9tJ8NAa1JX7dTSQCP5JDRnY8v3Kn1lvEw6zEiHYWeZeOtsZ3N06Cowf9RoruZ ehnw== 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=+jA6WRXXMwDbVu7f/kcJZRwlkFAWTE5T1s7r75eMQrE=; fh=Tb1p8S3iOxe/kX/WSNC3xEJzSVgGJ5DaxSrKb+pzH+4=; b=PCDTb/Wt51qfFUpJaEkIZ2/WxrZPCKwaSEeJSVdaH5CNDcxSlJ4AiTLz9JhULL/YR5 vNt99AY0GXckUHszfTF1lBL9wq3/evXLVYaqUG4oyj05Q8yrOqGfFD+UJizsAgRhLNmw TjkwMSJ/6bxNjFzsVOUr7rMOfAqZnI4O0gMxuF0m2233LavytwruKs0sGMyK4cFVnkSW YlFFUV/nJk+gxKGB9gkkxuKK2Xt1VUSvaxWCUK+e9qHnwKMMxnsRQ7/3XrLcX3YVnd3w 2IbglF3lkbHittiK6AvVtEyiBfda3M0BuE2blmwyYk5YBYVPMIqrehWnOWzUWVM+MVQm WQRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lI3qFdBJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id l3-20020a056a00140300b0068e4037c5f7si5811371pfu.388.2023.09.23.03.18.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 03:18:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lI3qFdBJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 5C338836428D; Sat, 23 Sep 2023 03:13:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231767AbjIWKMm (ORCPT + 29 others); Sat, 23 Sep 2023 06:12:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231476AbjIWKL6 (ORCPT ); Sat, 23 Sep 2023 06:11:58 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 462471BE; Sat, 23 Sep 2023 03:11:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695463908; x=1726999908; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=m7R91mzhAyMwuxf88uvtu3V1UGcA6vznqDxNdL57F/c=; b=lI3qFdBJ0gCc7+51MCb+eAv5IiowMkXv3og/CBaw/6tkoV8wy2mGUviT PYOP9T7oueM4cPUQLlt/QUHvEBwadIDOxJ8sKmjcY0HnkU2EU3vyuA6rl kKkMWLw5k3mMXqyYJq9GT52TEj3IymjS543L+ircqKG/qQapW9JzjdA8Z Yj9Jq1JQqMCxLgw03R4vRj3l8vRqg0PFJ4rRIXFhEmqTYaW55wJIdTrWb 8SrLaU3WgyCuGNha1CNMUezExH/liIDAF8w5MYAwa2JRW+WE/OEyWblPu raCnzzZ4u2tTJ63/SbUrVNsluNIlYy/evAHRKm6mzCpHbZQs+H5teYeRs Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="447492338" X-IronPort-AV: E=Sophos;i="6.03,171,1694761200"; d="scan'208";a="447492338" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Sep 2023 03:11:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="813388180" X-IronPort-AV: E=Sophos;i="6.03,171,1694761200"; d="scan'208";a="813388180" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmsmga008.fm.intel.com with ESMTP; 23 Sep 2023 03:11:46 -0700 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 Subject: [PATCH v11 22/37] x86/fred: Allow single-step trap and NMI when starting a new task Date: Sat, 23 Sep 2023 02:41:57 -0700 Message-Id: <20230923094212.26520-23-xin3.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230923094212.26520-1-xin3.li@intel.com> References: <20230923094212.26520-1-xin3.li@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=2.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email 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 (groat.vger.email [0.0.0.0]); Sat, 23 Sep 2023 03:13:19 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777823171443142232 X-GMAIL-MSGID: 1777823171443142232 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 4f87f5987ae8..c075591b7b46 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