From patchwork Thu Sep 14 04:47:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Xin3" X-Patchwork-Id: 139488 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp250082vqi; Thu, 14 Sep 2023 03:34:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGsK9z7CwHYUdGUM/hSQZWzuV589iCgPk+GVb6c9cHipdSmAYUfB6s7A9dW+RrR+MKoVScr X-Received: by 2002:a25:a1c2:0:b0:d71:6bc4:ac6d with SMTP id a60-20020a25a1c2000000b00d716bc4ac6dmr4990003ybi.65.1694687644621; Thu, 14 Sep 2023 03:34:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694687644; cv=none; d=google.com; s=arc-20160816; b=ms5dxHH9Maf+Qe4apRe+PJxZQZaUjtgq/aAjFybXvUJwB33doXKZ6/wZNwvWzw+acg B74hAzhXFt92Dro//R3aezMgVe/ujoBP/8tggkCwQnH9wdihSQmvRbYb3t7isHz6DBxx mJ1WIsn36FZj75Kflb8zIPlZhLwju7R2gLM6+OwQZCjVc//AlDwxyyplsXoESmF/AGyy UwOzbH51K1fg5h+5K6KhSw5jKtbx1OXDi2av/4tln6Gkejrl9Lob5Uu9G1xuSRMNfWLh eJrpS11TdQAjLyU5sta86n5NAmpLmI+4FnXOwA/JKUehe43on6/Zg7P2HQYidVyfS7Ga hemw== 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=jqCbnajAXgJ+s7A1aA7DOHJD13/+EpD442pw8d+Ofss=; b=O4iLnetDGpwLDrBub+tPm4Rbl529OyQy1yaNkZN2LkNxzDbfDyWHnV80mebSMZa+A3 2wtbviYTccPOnAyN7mem+jattk3FV7HL2i8oU4SppVNF/43Hg7zpz6K/Fbv+JzC+YGoi cU841x7NcBid6zQyutKYtHz398pMWGwMzEWKE5pxPAWXII8Yuv6dJmvgbjHIPQevaU0i s4VQAQvRfUHdgPhTTohDvxOAsyeadnPybdAqenTdZ/S+PK4t+f+z4LyHgulSIKoLLpMO Pm72egZSVooZNFxzMUMW/GvIf11OtjIUtQ4lhhRGotIU15PdaiTIA33WsMQMmZ97eTJp sO4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dDcQJUjJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id q22-20020a637516000000b00577723d24b0si1226658pgc.46.2023.09.14.03.34.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 03:34:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dDcQJUjJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 7E51081DC7A9; Wed, 13 Sep 2023 22:20:26 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235137AbjINFUO (ORCPT + 35 others); Thu, 14 Sep 2023 01:20:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235144AbjINFT3 (ORCPT ); Thu, 14 Sep 2023 01:19:29 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C28591FDC; Wed, 13 Sep 2023 22:19:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694668762; x=1726204762; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=m7R91mzhAyMwuxf88uvtu3V1UGcA6vznqDxNdL57F/c=; b=dDcQJUjJrPEaEE6iYwnAL4W+3ZiDIqKPvQ80QJvHyunisaaptRhg7MCW XZRBi9uAnGvZEAFpZMRKwcA5hFVqhWOAx5rg3kgtqRhB+QVIo438n7O19 pvieavFEU4bIepgwFDoWANfM9Eor1UeDIjfPBNk9WbEF6KexYkZyB5BRS Mtvvi0O7GCQZVrseJ7KHxo28SKLR1p92rce7YYwGvLzKaR82fTqI8L3/7 fvGxFZPyUZa0+6f5OtOR/8o6zVo/7IMx0Esp3QPO/iZgpUbpMLq8vxanS vHcHVerWIyuQ2Ga8GwfullB3Tnb3DiC9iwjWynyeMt8eVC2yz1Ya2pgKD A==; X-IronPort-AV: E=McAfee;i="6600,9927,10832"; a="382661356" X-IronPort-AV: E=Sophos;i="6.02,145,1688454000"; d="scan'208";a="382661356" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Sep 2023 22:17:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10832"; a="779488805" X-IronPort-AV: E=Sophos;i="6.02,145,1688454000"; d="scan'208";a="779488805" Received: from unknown (HELO fred..) ([172.25.112.68]) by orsmga001.jf.intel.com with ESMTP; 13 Sep 2023 22:17:40 -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 Subject: [PATCH v10 22/38] x86/fred: Allow single-step trap and NMI when starting a new task Date: Wed, 13 Sep 2023 21:47:49 -0700 Message-Id: <20230914044805.301390-23-xin3.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230914044805.301390-1-xin3.li@intel.com> References: <20230914044805.301390-1-xin3.li@intel.com> MIME-Version: 1.0 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]); Wed, 13 Sep 2023 22:20:26 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777008791347513461 X-GMAIL-MSGID: 1777008791347513461 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