From patchwork Tue Apr 4 10:27: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: 79007 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2942027vqo; Tue, 4 Apr 2023 04:15:50 -0700 (PDT) X-Google-Smtp-Source: AKy350adWwUAZbPuqAKsIlrNjnpLYT5FSBlqqvuRx9JAEWLyrF0DB1EbELYokKmoBnmCsZWzFgxt X-Received: by 2002:a17:902:cf45:b0:1a1:b7fc:eeba with SMTP id e5-20020a170902cf4500b001a1b7fceebamr2003633plg.19.1680606950086; Tue, 04 Apr 2023 04:15:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680606950; cv=none; d=google.com; s=arc-20160816; b=CPd3gSg3k9YHUCK3bv2/YVrzkdBbh1T/MmmL+pmvsTrVFCeLM4Y99qujxr/PlcX8Kk fmMxFImxebmaUZaWmKfxJkmlNcGLTr6d3qam43IUIDjJ6ShMBWB0kTZ9IjdVLkpZYRga 2uRDYLx9JcJ8GiLyUP5I1S3wm3uEJgIgIocXA3F2E5nIHnVqQwXOdpDgJO6Jx8vPijka iNSsmCJfRkdJkz8Mee3ZiJ82qumy8JjvzBtvlKO1kVFbgfbt7fFMPEWtifuTy03oOcYr 9uy5d/8+bfLZkSgt9g0g5Nw7cDhmuPa+KcovS1/XVQSPKtGKmR1a8eaGPzRH4lnbhTbM XuPA== 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=dXk7rGWXn1+raSuko+HE7ZydNglgwfgo+bHOiS4p1Pg=; b=cT4LyYAZmWzIXNyTWpmn1Uw2XBxVrnYuE5Uydg/eHViY+wXSFascP6yvHEX5x+ti35 xdn/XajGii8C7AON4NEIFjUqnBeI1kJxNYyP64YrYP9IWzXPp7LN5xQTVoWiqCLHI8sp QWyTVBoOIgX9id32iHNFMqK1SrcZLwayy9ewTOPPPIWQYsjuPIrML/ivuJfj70lkcr1W 9b+f1tZMVl4nPspI0qxH4SM4HRzrcZJfnsPwoTfwWZKL73nRcVEnbv0SvOkEi7RODUSo tJeVsQ7L3VRvQQT90/X6/NWpBgIZqf4KZ+ZYjC1Ma7E55Duc0/UKEM1emA8cEFSNAnq+ 7ivA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=jabhYiQg; 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 h13-20020a170902f54d00b001a199eaf57csi3976984plf.404.2023.04.04.04.15.37; Tue, 04 Apr 2023 04:15:50 -0700 (PDT) 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=jabhYiQg; 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 S234678AbjDDK4M (ORCPT + 99 others); Tue, 4 Apr 2023 06:56:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234846AbjDDKzV (ORCPT ); Tue, 4 Apr 2023 06:55:21 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36E293A9C; Tue, 4 Apr 2023 03:54:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680605643; x=1712141643; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kdsDSL0FM4cOW58nPFQAA0OCrgImaKiH8KbKpW/8YIw=; b=jabhYiQgBmG028+fXjKXvV2YbG3CIj7jEC1Swq9UB2MGmQgBzJVsITbZ WheoPAsx+Rxz60jwTPmpFRdV9sWwNf6m5VWGxMhTZS+TRrMbIjmAjAIyc P+q9Eh9HlkAuFdFWqjjEd9Z18TbY+AxV/sMJG279CgYbt5C1sn5e3Btuo L9NqkunQZU+3QHqPl/S2rOWkNw7as/Z0puedUmZTUhmA+z44N3soxbLmG KzJRNm911arwRMZQ729vGZ/JCHLdqssLuU/dCmsPl4V2WCl66KJ4O6jGg v3ovUT/EOS1RzzH5u/IctXDLr9vMLk5z8h8Ppx2m2YiLEMyJ9aNKLKVbc g==; X-IronPort-AV: E=McAfee;i="6600,9927,10669"; a="330734187" X-IronPort-AV: E=Sophos;i="5.98,317,1673942400"; d="scan'208";a="330734187" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2023 03:53:06 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10669"; a="775597856" X-IronPort-AV: E=Sophos;i="5.98,317,1673942400"; d="scan'208";a="775597856" Received: from unknown (HELO fred..) ([172.25.112.68]) by FMSMGA003.fm.intel.com with ESMTP; 04 Apr 2023 03:53:05 -0700 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, jiangshanlai@gmail.com, shan.kang@intel.com Subject: [PATCH v7 26/33] x86/fred: allow single-step trap and NMI when starting a new thread Date: Tue, 4 Apr 2023 03:27:09 -0700 Message-Id: <20230404102716.1795-27-xin3.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230404102716.1795-1-xin3.li@intel.com> References: <20230404102716.1795-1-xin3.li@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.5 required=5.0 tests=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=unavailable 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?1762244113345323558?= X-GMAIL-MSGID: =?utf-8?q?1762244113345323558?= 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 c5fbc4f18059..f7caf3b2f3f7 100644 --- a/arch/x86/include/asm/fred.h +++ b/arch/x86/include/asm/fred.h @@ -70,6 +70,14 @@ */ #define FRED_DF_STACK_LEVEL 3 +/* + * 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 CSX_PROCESS_START \ + (FRED_CSX_ENABLE_NMI | FRED_CSX_ALLOW_SINGLE_STEP) + #ifndef __ASSEMBLY__ #include @@ -133,6 +141,9 @@ void fred_setup_apic(void); #else #define cpu_init_fred_exceptions() BUG() #define fred_setup_apic() BUG() + +#define CSX_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 2bea86073646..c732d9dbff3a 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 | CSX_PROCESS_START; + regs->ssx = _ss; + regs->flags = X86_EFLAGS_IF | X86_EFLAGS_FIXED; } void