From nobody Fri Dec 19 12:36:21 2025 Received: from mail.zytor.com (terminus.zytor.com [198.137.202.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C898C13B584 for ; Wed, 3 Jul 2024 08:55:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.136 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719996911; cv=none; b=E6V8Lf4YT1+iURH6vSK8nRWDa8E37KpRYK4Hl7Pq7pRcfH8K+6a7RY7rzG7yD8QLs8R4n3dTrvHO4Qzw25kJSHumezDX69qAjpnFBksKOQGTVN/MqH8VQwo5ygleyQhbHmVY9rpVaJYLDM5URZ4F+Hi9njMMKQEIY7ntKP84FiI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719996911; c=relaxed/simple; bh=xpI+15o/ggAcbKaDEyciMD3Vb9gLEsyJeQ86OGReybc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XAMM+gH8I8Aujjdc0wIGh7JIfMZQeMzw3Sn48ET6+38kqhL74CZCDfH5HgTN4pWgJgdT7zgxa43CnWuKX39PR+DvmxswYRrysSLSe7lX2JzaVTsE7sgXPVG9F0Kdii/aqye1SK8r557p102Lwl65AC6Y7P9tSOr1dUUXh6A+r5E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com; spf=pass smtp.mailfrom=zytor.com; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b=YT7KmYad; arc=none smtp.client-ip=198.137.202.136 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zytor.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b="YT7KmYad" Received: from terminus.zytor.com (terminus.zytor.com [IPv6:2607:7c80:54:3:0:0:0:136]) (authenticated bits=0) by mail.zytor.com (8.17.2/8.17.1) with ESMTPSA id 4638sQ17274819 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 3 Jul 2024 01:54:32 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 4638sQ17274819 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2024061501; t=1719996872; bh=kyoFq7oawAKp06S/tI3mHScc7IohshnSow91+CyJz78=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YT7KmYadpSiqzMhoAHY5uxMYobVALTVwQDI3guXxVBO5u2Xnq362DPTD/K+g0fgMd s2NKRcdM6xauVQH6NW9SAFU/fq7ydQTfkchVqHoD2k5qRGKjkgRga2/+J89EuiB5mC sf8NcGuNvUSi9UuOnATruREHvcGBTfn60u3QXF+f3UynyvFT5VxenAEvvIuCcFR3uX HrVn8ZNf23gaqHuO3nkPHNxjVruUymFvq+rRMqaNmvcKbhaxHAlr37pkueM8VF4Rrs hvDBHzIkmAj/FSpHM1hnMe5VRzsLWSfHHVnaYV678fkVbt5SGxdcLZqBeB+ZQPVX6E Hx+qttqT23Mfg== From: "Xin Li (Intel)" To: linux-kernel@vger.kernel.org Cc: hpa@zytor.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, peterz@infradead.org, andrew.cooper3@citrix.com, nik.borisov@suse.com, houwenlong.hwl@antgroup.com Subject: [PATCH v1 1/4] x86/fred: Parse cmdline param "fred=" in cpu_parse_early_param() Date: Wed, 3 Jul 2024 01:54:23 -0700 Message-ID: <20240703085426.274801-2-xin@zytor.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240703085426.274801-1-xin@zytor.com> References: <20240703085426.274801-1-xin@zytor.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Depending on whether FRED will be used, sysvec_install() installs a system interrupt handler into FRED system vector dispatch table or IDT. However FRED can be disabled later in trap_init(), after sysvec_install() is called. E.g., the HYPERVISOR_CALLBACK_VECTOR handler is registered with sysvec_install() in kvm_guest_init(), which is called in setup_arch() but way before trap_init(). IOW, there is a gap between FRED is available and available but disabled. As a result, when FRED is available but disabled, its IDT handler is not installed thus spurious_interrupt() will be invoked. Fix it by parsing cmdline param "fred=3D" in cpu_parse_early_param() to minimize the gap between FRED is available and available but disabled. Fixes: 3810da12710a ("x86/fred: Add a fred=3D cmdline param") Reported-by: Hou Wenlong Suggested-by: Thomas Gleixner Signed-off-by: Xin Li (Intel) --- arch/x86/kernel/cpu/common.c | 5 +++++ arch/x86/kernel/traps.c | 26 -------------------------- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index d4e539d4e158..9a904fe7c829 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -1510,6 +1510,11 @@ static void __init cpu_parse_early_param(void) if (cmdline_find_option_bool(boot_command_line, "nousershstk")) setup_clear_cpu_cap(X86_FEATURE_USER_SHSTK); =20 + /* Minimize the gap between FRED is available and available but disabled.= */ + arglen =3D cmdline_find_option(boot_command_line, "fred", arg, sizeof(arg= )); + if (arglen !=3D strlen("on") || strcmp(arg, "on")) + setup_clear_cpu_cap(X86_FEATURE_FRED); + arglen =3D cmdline_find_option(boot_command_line, "clearcpuid", arg, size= of(arg)); if (arglen <=3D 0) return; diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index 4fa0b17e5043..6afb41e6cbbb 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -1402,34 +1402,8 @@ DEFINE_IDTENTRY_SW(iret_error) } #endif =20 -/* Do not enable FRED by default yet. */ -static bool enable_fred __ro_after_init =3D false; - -#ifdef CONFIG_X86_FRED -static int __init fred_setup(char *str) -{ - if (!str) - return -EINVAL; - - if (!cpu_feature_enabled(X86_FEATURE_FRED)) - return 0; - - if (!strcmp(str, "on")) - enable_fred =3D true; - else if (!strcmp(str, "off")) - enable_fred =3D false; - else - pr_warn("invalid FRED option: 'fred=3D%s'\n", str); - return 0; -} -early_param("fred", fred_setup); -#endif - void __init trap_init(void) { - if (cpu_feature_enabled(X86_FEATURE_FRED) && !enable_fred) - setup_clear_cpu_cap(X86_FEATURE_FRED); - /* Init cpu_entry_area before IST entries are set up */ setup_cpu_entry_areas(); =20 --=20 2.45.2 From nobody Fri Dec 19 12:36:21 2025 Received: from mail.zytor.com (terminus.zytor.com [198.137.202.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E6F9133291 for ; Wed, 3 Jul 2024 08:55:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.136 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719996911; cv=none; b=RoDrAAuIclZ0md+4gTDTyHaSbl+a67vF/c3b0tWJ1+mapbwthl74c+2zZycawXdxc74AHn7tmizD6ZX8Q0buWi9n8WrATLvTXcYJNnPeDO+XtPTiABn45JdjCv1yH3+Abh5TWpfljv3RNswI7eFPJ+7NXaV3CKpCq5Urbpp+0NI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719996911; c=relaxed/simple; bh=TWoIecHkxnmQ5CI1vLC3ZAecLTeVYy7pmZuisYvLqOU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G/t5BMny82dokOocmYP3PDhUespRtCPwCCtee8Q41iw/ex+I5Vrfu5ODm886qkHcy6IqjnK3PaAotd8i6FG4pqNIZoO68f2t3SXYhsZFJEachpBSRM52VE2bCJmaut9LX7/rRKb6uN7dLm3hvjL1wW71uK9FlsM7M+8AXFK4X/w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com; spf=pass smtp.mailfrom=zytor.com; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b=UYlhhlp5; arc=none smtp.client-ip=198.137.202.136 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zytor.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b="UYlhhlp5" Received: from terminus.zytor.com (terminus.zytor.com [IPv6:2607:7c80:54:3:0:0:0:136]) (authenticated bits=0) by mail.zytor.com (8.17.2/8.17.1) with ESMTPSA id 4638sQ18274819 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 3 Jul 2024 01:54:32 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 4638sQ18274819 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2024061501; t=1719996873; bh=+f2c92dRlNI6Bo2LbRHWJ0Bigiez0ZfjI89E6Xah1/g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UYlhhlp528Y1DPjzmqIvsckdKWNWFYRMv0Ylhlm4Z8z7IwfBy+akwwLypwpKtwOmt 4zw4Mo9D5Pi3IYAmRQJT+MVJd5YgXRcqVPpC+XzxNP6YnseUlvW7x79NJkMhLaiU3g PTBDTTW+HRYUzh55jD9x/Zk8yTkziLk2IRFWJMBy+3sT8RAze5074FQ7S4l/UOeT/k VMaLB8GvFd+K6tuwkX/nYAbmQZyCbBm8j389AzSiAB0f7FoqcuQWrWleC6U0LHuV2C Czl+JjobdxIHqB3b2evXrDr1xpGV77adGV42KmLefoa9kJVsibesIVSmA/pgMsg4p2 bD/LlVdBR5lIQ== From: "Xin Li (Intel)" To: linux-kernel@vger.kernel.org Cc: hpa@zytor.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, peterz@infradead.org, andrew.cooper3@citrix.com, nik.borisov@suse.com, houwenlong.hwl@antgroup.com Subject: [PATCH v1 2/4] x86/fred: Write to FRED MSRs with wrmsrns() Date: Wed, 3 Jul 2024 01:54:24 -0700 Message-ID: <20240703085426.274801-3-xin@zytor.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240703085426.274801-1-xin@zytor.com> References: <20240703085426.274801-1-xin@zytor.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Do FRED MSR writes with wrmsrns() rather than wrmsrl(). No functional change intended. Signed-off-by: Xin Li (Intel) --- arch/x86/kernel/fred.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/arch/x86/kernel/fred.c b/arch/x86/kernel/fred.c index 4bcd8791ad96..b202685b8e77 100644 --- a/arch/x86/kernel/fred.c +++ b/arch/x86/kernel/fred.c @@ -26,27 +26,27 @@ void cpu_init_fred_exceptions(void) /* When FRED is enabled by default, remove this log message */ pr_info("Initialize FRED on CPU%d\n", smp_processor_id()); =20 - wrmsrl(MSR_IA32_FRED_CONFIG, - /* Reserve for CALL emulation */ - FRED_CONFIG_REDZONE | - FRED_CONFIG_INT_STKLVL(0) | - FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user)); + wrmsrns(MSR_IA32_FRED_CONFIG, + /* Reserve for CALL emulation */ + FRED_CONFIG_REDZONE | + FRED_CONFIG_INT_STKLVL(0) | + FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user)); =20 /* * The purpose of separate stacks for NMI, #DB and #MC *in the kernel* * (remember that user space faults are always taken on stack level 0) * is to avoid overflowing the kernel stack. */ - wrmsrl(MSR_IA32_FRED_STKLVLS, - FRED_STKLVL(X86_TRAP_DB, FRED_DB_STACK_LEVEL) | - FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) | - FRED_STKLVL(X86_TRAP_MC, FRED_MC_STACK_LEVEL) | - FRED_STKLVL(X86_TRAP_DF, FRED_DF_STACK_LEVEL)); + wrmsrns(MSR_IA32_FRED_STKLVLS, + FRED_STKLVL(X86_TRAP_DB, FRED_DB_STACK_LEVEL) | + FRED_STKLVL(X86_TRAP_NMI, FRED_NMI_STACK_LEVEL) | + FRED_STKLVL(X86_TRAP_MC, FRED_MC_STACK_LEVEL) | + FRED_STKLVL(X86_TRAP_DF, FRED_DF_STACK_LEVEL)); =20 /* The FRED equivalents to IST stacks... */ - wrmsrl(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB)); - wrmsrl(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI)); - wrmsrl(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF)); + wrmsrns(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB)); + wrmsrns(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI)); + wrmsrns(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF)); =20 /* Enable FRED */ cr4_set_bits(X86_CR4_FRED); --=20 2.45.2 From nobody Fri Dec 19 12:36:21 2025 Received: from mail.zytor.com (terminus.zytor.com [198.137.202.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B42B313BAE5 for ; Wed, 3 Jul 2024 08:55:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.136 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719996913; cv=none; b=oloiH601OMeHhKo+udV2fq2oWWe3PEdUiPxC0XJinXg/E878aLxh7plWU8sNkTD89LaHPRs5iShxsKxh4xY+m8LoCHLuVXhxsc0DmvKbs7coll8YZaseJBCtjwhLheeJ1V6kQLMHDdTIh0NgJXKYC4rlQdDWN2iG6THmlhbImps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719996913; c=relaxed/simple; bh=qWdd/U5P843zZgQZi8pLesLG9Ojih5CBGvIIxi8Vhjs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=da5m4VVhS5xMVw2gdwZOQKlG3NNRakknEh81C5xhVcVKWvnBqQZtTVXl6clm14shXKbo5jH5CuhmhxnobMktv2tETOZx3YGN0mQxqC2Dk521NbjZrvsDT0dM+a0jqqhfvT8nLE1UJ4lC7LgQuoOqVVXFLlMg/5eGBj/C7kJX+bg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com; spf=pass smtp.mailfrom=zytor.com; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b=fjru07oX; arc=none smtp.client-ip=198.137.202.136 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zytor.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b="fjru07oX" Received: from terminus.zytor.com (terminus.zytor.com [IPv6:2607:7c80:54:3:0:0:0:136]) (authenticated bits=0) by mail.zytor.com (8.17.2/8.17.1) with ESMTPSA id 4638sQ19274819 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 3 Jul 2024 01:54:33 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 4638sQ19274819 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2024061501; t=1719996874; bh=ioOo8doTD6S3vVDLWeOjIsJkTYwDmB9mJWFS1Qkccb8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fjru07oXkjTbBgMZ/4zzzBEtGbyCK7dRi9pKBpayaskwjRWeSJK6ujK9hPPq414E5 UzeUmE3DT2YRX1fUDHpSAfqOY7cfRKFlM8k4+iLvF2Vdta627cg9n8Ikz2ZNvG6SpV V3PLlya2xZknZ0W2wkxwiosQ38XiakKvv/Sroct48UvkaEb5nsEEpP4cCgJXmDxJuF FxMTXf1WXPUnKlS+uSvdaa2ZYV2Xn8HYHm1IwYQudMVTsFGcV520eAHv5bypcD0et2 E/pw2UrCx6wGS5LDf87KAXrhv0p89lrffJBadZG9AyO/j7nBWvxM82ebS5FirAhNQ/ Ap+KPueGqXL+A== From: "Xin Li (Intel)" To: linux-kernel@vger.kernel.org Cc: hpa@zytor.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, peterz@infradead.org, andrew.cooper3@citrix.com, nik.borisov@suse.com, houwenlong.hwl@antgroup.com Subject: [PATCH v1 3/4] x86/fred: Split FRED RSP initialization into a separate function Date: Wed, 3 Jul 2024 01:54:25 -0700 Message-ID: <20240703085426.274801-4-xin@zytor.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240703085426.274801-1-xin@zytor.com> References: <20240703085426.274801-1-xin@zytor.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To enable FRED earlier, split FRED RSP initialization into a separate function, as they are initialized with memory from CPU entry areas, thus their initialization has to be kept after setup_cpu_entry_areas(). No functional change intended. Signed-off-by: Xin Li (Intel) --- arch/x86/include/asm/fred.h | 2 ++ arch/x86/kernel/cpu/common.c | 6 ++++-- arch/x86/kernel/fred.c | 28 +++++++++++++++++++--------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/fred.h b/arch/x86/include/asm/fred.h index e86c7ba32435..66d7dbe2d314 100644 --- a/arch/x86/include/asm/fred.h +++ b/arch/x86/include/asm/fred.h @@ -84,11 +84,13 @@ static __always_inline void fred_entry_from_kvm(unsigne= d int type, unsigned int } =20 void cpu_init_fred_exceptions(void); +void cpu_init_fred_rsps(void); void fred_complete_exception_setup(void); =20 #else /* CONFIG_X86_FRED */ static __always_inline unsigned long fred_event_data(struct pt_regs *regs)= { return 0; } static inline void cpu_init_fred_exceptions(void) { } +static inline void cpu_init_fred_rsps(void) { } static inline void fred_complete_exception_setup(void) { } static __always_inline void fred_entry_from_kvm(unsigned int type, unsigne= d int vector) { } #endif /* CONFIG_X86_FRED */ diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index 9a904fe7c829..022ae4ba7997 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -2195,10 +2195,12 @@ void cpu_init_exception_handling(void) /* GHCB needs to be setup to handle #VC. */ setup_ghcb(); =20 - if (cpu_feature_enabled(X86_FEATURE_FRED)) + if (cpu_feature_enabled(X86_FEATURE_FRED)) { cpu_init_fred_exceptions(); - else + cpu_init_fred_rsps(); + } else { load_current_idt(); + } } =20 /* diff --git a/arch/x86/kernel/fred.c b/arch/x86/kernel/fred.c index b202685b8e77..1788f28754f3 100644 --- a/arch/x86/kernel/fred.c +++ b/arch/x86/kernel/fred.c @@ -32,6 +32,25 @@ void cpu_init_fred_exceptions(void) FRED_CONFIG_INT_STKLVL(0) | FRED_CONFIG_ENTRYPOINT(asm_fred_entrypoint_user)); =20 + wrmsrns(MSR_IA32_FRED_STKLVLS, 0); + wrmsrns(MSR_IA32_FRED_RSP0, 0); + wrmsrns(MSR_IA32_FRED_RSP1, 0); + wrmsrns(MSR_IA32_FRED_RSP2, 0); + wrmsrns(MSR_IA32_FRED_RSP3, 0); + + /* Enable FRED */ + cr4_set_bits(X86_CR4_FRED); + /* Any further IDT use is a bug */ + idt_invalidate(); + + /* Use int $0x80 for 32-bit system calls in FRED mode */ + setup_clear_cpu_cap(X86_FEATURE_SYSENTER32); + setup_clear_cpu_cap(X86_FEATURE_SYSCALL32); +} + +/* Must be called after setup_cpu_entry_areas() */ +void cpu_init_fred_rsps(void) +{ /* * The purpose of separate stacks for NMI, #DB and #MC *in the kernel* * (remember that user space faults are always taken on stack level 0) @@ -47,13 +66,4 @@ void cpu_init_fred_exceptions(void) wrmsrns(MSR_IA32_FRED_RSP1, __this_cpu_ist_top_va(DB)); wrmsrns(MSR_IA32_FRED_RSP2, __this_cpu_ist_top_va(NMI)); wrmsrns(MSR_IA32_FRED_RSP3, __this_cpu_ist_top_va(DF)); - - /* Enable FRED */ - cr4_set_bits(X86_CR4_FRED); - /* Any further IDT use is a bug */ - idt_invalidate(); - - /* Use int $0x80 for 32-bit system calls in FRED mode */ - setup_clear_cpu_cap(X86_FEATURE_SYSENTER32); - setup_clear_cpu_cap(X86_FEATURE_SYSCALL32); } --=20 2.45.2 From nobody Fri Dec 19 12:36:21 2025 Received: from mail.zytor.com (terminus.zytor.com [198.137.202.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9FD213B5A1 for ; Wed, 3 Jul 2024 08:55:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.136 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719996913; cv=none; b=uhR7a6HrfpQdajI+Fn2C7ZZvLToJNMiSYx9wwWiJsUudfgBTEAgL1HVVAYzYEEKFKO4cK1OmDuAwtsSwY7HdvJ+LugMJ7fOIUiD6818Fnd17pfTL98XSkDpBe5DxW0D2aNqIkyhpguggzwe4tKHMm9w+kn2+oMQ9bgA+vJyFzjQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719996913; c=relaxed/simple; bh=kdikNib9x+eTgS5GaTuz0ap4G90ilWCwk8wef1VJOOQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I/fjJ0QgKexqk1p1HqiYALIHMMcaH/crzC9wD6wugOY3a++Wlzg7ynAS7q8Hn+QVPHLY2bGrEigPl9nTP8M500gM71nAprFN/T7k6is22gtqGfE1140yMBlYb13KznlOU7pjIwkmfq23FUHOfSHep0guD3VlRZ07si/jqykjlnk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com; spf=pass smtp.mailfrom=zytor.com; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b=RtTNXEpB; arc=none smtp.client-ip=198.137.202.136 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zytor.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b="RtTNXEpB" Received: from terminus.zytor.com (terminus.zytor.com [IPv6:2607:7c80:54:3:0:0:0:136]) (authenticated bits=0) by mail.zytor.com (8.17.2/8.17.1) with ESMTPSA id 4638sQ1A274819 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Wed, 3 Jul 2024 01:54:34 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 4638sQ1A274819 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2024061501; t=1719996874; bh=Ko+FE7nG9WBnR7O9m6WOO/3Vh122gx7NsoMXFF+ciuI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RtTNXEpBkHFfKjaLaqu7V8zjxdM8U+AwvSJ12KTjjGK2DSCiYsKg/IiiGGfz2/hNf b4IUHgoRAMu65YQBkKdRjn7oOtgeO35pb9NuZ/XQCVbzJ+61YNi9rRl4G4Vi8b2mcc f3e6x2k0b/paIvlnlqbYbqhNVwdvWYnHXF2EGMq3yIxSk5/Deb/bKi1n3UCjjLNzQe Nnv/rzo9vGe8tVIArLjN+ns5B03Y/O00vMYzGHuaWrX7rEVyd9cQR7ljcnk0u6kGJO +NgTpmQuGW1sr9fyhrcN2ME8Q88HRs2nZroddv9GN6nsDifKnRDsXFYKVZ/n/eed3J gaOGn0svYavYA== From: "Xin Li (Intel)" To: linux-kernel@vger.kernel.org Cc: hpa@zytor.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, peterz@infradead.org, andrew.cooper3@citrix.com, nik.borisov@suse.com, houwenlong.hwl@antgroup.com Subject: [PATCH v1 4/4] x86/fred: Enable FRED right after init_mem_mapping() Date: Wed, 3 Jul 2024 01:54:26 -0700 Message-ID: <20240703085426.274801-5-xin@zytor.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240703085426.274801-1-xin@zytor.com> References: <20240703085426.274801-1-xin@zytor.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Enable FRED right after init_mem_mapping() to avoid #PF handler, exc_page_fault(), fetching its faulting address from the stack before FRED is enabled. Fixes: 14619d912b65 ("x86/fred: FRED entry/exit and dispatch code") Reported-by: Hou Wenlong Suggested-by: Thomas Gleixner Signed-off-by: Xin Li (Intel) --- arch/x86/kernel/cpu/common.c | 6 +----- arch/x86/kernel/setup.c | 7 ++++++- arch/x86/kernel/smpboot.c | 6 ++++++ arch/x86/kernel/traps.c | 4 ++++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c index 022ae4ba7997..8d454b6f7def 100644 --- a/arch/x86/kernel/cpu/common.c +++ b/arch/x86/kernel/cpu/common.c @@ -2195,12 +2195,8 @@ void cpu_init_exception_handling(void) /* GHCB needs to be setup to handle #VC. */ setup_ghcb(); =20 - if (cpu_feature_enabled(X86_FEATURE_FRED)) { - cpu_init_fred_exceptions(); - cpu_init_fred_rsps(); - } else { + if (!cpu_feature_enabled(X86_FEATURE_FRED)) load_current_idt(); - } } =20 /* diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 728927e4ba51..d866136a89ff 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -1040,7 +1041,11 @@ void __init setup_arch(char **cmdline_p) =20 init_mem_mapping(); =20 - idt_setup_early_pf(); + /* Switch to FRED from early IDT ASAP */ + if (cpu_feature_enabled(X86_FEATURE_FRED)) + cpu_init_fred_exceptions(); + else + idt_setup_early_pf(); =20 /* * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features) diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c index 0c35207320cb..0d83377f9dcd 100644 --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -64,6 +64,7 @@ #include #include #include +#include #include #include #include @@ -248,6 +249,11 @@ static void notrace start_secondary(void *unused) =20 cpu_init_exception_handling(); =20 + if (cpu_feature_enabled(X86_FEATURE_FRED)) { + cpu_init_fred_exceptions(); + cpu_init_fred_rsps(); + } + /* * Load the microcode before reaching the AP alive synchronization * point below so it is not part of the full per CPU serialized diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c index 6afb41e6cbbb..81648bd07576 100644 --- a/arch/x86/kernel/traps.c +++ b/arch/x86/kernel/traps.c @@ -1407,6 +1407,10 @@ void __init trap_init(void) /* Init cpu_entry_area before IST entries are set up */ setup_cpu_entry_areas(); =20 + /* FRED RSPs pointing to memory from CPU entry areas */ + if (cpu_feature_enabled(X86_FEATURE_FRED)) + cpu_init_fred_rsps(); + /* Init GHCB memory pages when running as an SEV-ES guest */ sev_es_init_vc_handling(); =20 --=20 2.45.2