From: Yi Lai <yi1.lai@intel.com>
To: Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
Andrew Cooper <andrew.cooper3@citrix.com>, Xin Li <xin@zytor.com>,
x86@kernel.org, hpa@zytor.com, Shuah Khan <shuah@kernel.org>,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
yi1.lai@linux.intel.com, yi1.lai@intel.com
Subject: [PATCH v3] selftests/x86: Fix sysret_rip assertion failure on FRED systems
Date: Thu, 26 Mar 2026 17:44:23 +0800 [thread overview]
Message-ID: <20260326094423.711724-1-yi1.lai@intel.com> (raw)
The existing 'sysret_rip' selftest asserts that 'regs->r11 ==
regs->flags'. This check relies on the behavior of the SYSCALL
instruction on legacy x86_64, which saves 'RFLAGS' into 'R11'.
However, on systems with FRED (Flexible Return and Event Delivery)
enabled, instead of using registers, all state is saved onto the stack.
Consequently, 'R11' retains its userspace value, causing the assertion
to fail.
Fix this by detecting if FRED is enabled and skipping the register
assertion in that case. The detection is done by checking if the RPL
bits of the GS selector are preserved after a hardware exception.
IDT (via IRET) clears the RPL bits of NULL selectors, while FRED (via
ERETU) preserves them.
Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
Signed-off-by: Yi Lai <yi1.lai@intel.com>
---
v3:
- Move is_fred_enabled() to helpers.h for other x86 selftests to use.
Rename empty_handler to fred_handler to avoid symbol conflicts.
v2:
- Replaced CPUID check with a runtime probe using INT3 and GS RPL
preservation to robustly detect active FRED usage (Suggested by
Andrew Cooper).
tools/testing/selftests/x86/helpers.h | 34 ++++++++++++++++++++++++
tools/testing/selftests/x86/sysret_rip.c | 12 ++++++---
2 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/x86/helpers.h b/tools/testing/selftests/x86/helpers.h
index 4c747a1278d9..4d09ed97aaac 100644
--- a/tools/testing/selftests/x86/helpers.h
+++ b/tools/testing/selftests/x86/helpers.h
@@ -4,6 +4,7 @@
#include <signal.h>
#include <string.h>
+#include <stdbool.h>
#include <asm/processor-flags.h>
@@ -50,4 +51,37 @@ static inline void clearhandler(int sig)
ksft_exit_fail_msg("sigaction failed");
}
+static inline void fred_handler(int sig, siginfo_t *info, void *ctx_void)
+{
+}
+
+static inline bool is_fred_enabled(void)
+{
+ unsigned short gs_val;
+
+ sethandler(SIGTRAP, fred_handler, 0);
+
+ /*
+ * Distinguish IDT and FRED mode by loading GS with a non-zero RPL and
+ * triggering an exception:
+ * IDT (IRET) clears RPL bits of NULL selectors.
+ * FRED (ERETU) preserves them.
+ *
+ * If GS is loaded with 3 (Index=0, RPL=3), trigger an exception:
+ * IDT should restore GS as 0.
+ * FRED should preserve GS as 3.
+ */
+ asm volatile (
+ "mov %[rpl3], %%gs\n\t"
+ "int3\n\t"
+ "mov %%gs, %[res]"
+ : [res] "=r" (gs_val)
+ : [rpl3] "r" (3)
+ );
+
+ clearhandler(SIGTRAP);
+
+ return gs_val == 3;
+}
+
#endif /* __SELFTESTS_X86_HELPERS_H */
diff --git a/tools/testing/selftests/x86/sysret_rip.c b/tools/testing/selftests/x86/sysret_rip.c
index 2e423a335e1c..30b195266779 100644
--- a/tools/testing/selftests/x86/sysret_rip.c
+++ b/tools/testing/selftests/x86/sysret_rip.c
@@ -64,9 +64,15 @@ static void sigusr1(int sig, siginfo_t *info, void *ctx_void)
ctx->uc_mcontext.gregs[REG_RIP] = rip;
ctx->uc_mcontext.gregs[REG_RCX] = rip;
- /* R11 and EFLAGS should already match. */
- assert(ctx->uc_mcontext.gregs[REG_EFL] ==
- ctx->uc_mcontext.gregs[REG_R11]);
+ /*
+ * SYSCALL works differently on FRED, it does not save RIP and RFLAGS
+ * to RCX and R11.
+ */
+ if (!is_fred_enabled()) {
+ /* R11 and EFLAGS should already match. */
+ assert(ctx->uc_mcontext.gregs[REG_EFL] ==
+ ctx->uc_mcontext.gregs[REG_R11]);
+ }
sethandler(SIGSEGV, sigsegv_for_sigreturn_test, SA_RESETHAND);
}
--
2.43.0
next reply other threads:[~2026-03-26 9:44 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 9:44 Yi Lai [this message]
2026-03-26 22:06 ` [PATCH v3] selftests/x86: Fix sysret_rip assertion failure on FRED systems Andy Lutomirski
2026-03-27 12:33 ` Peter Zijlstra
2026-03-31 2:21 ` Lai, Yi
2026-03-31 6:03 ` Xin Li
2026-04-01 1:59 ` Xin Li
2026-04-01 2:48 ` H. Peter Anvin
2026-04-01 14:36 ` Xin Li
2026-04-01 17:54 ` H. Peter Anvin
2026-04-02 13:21 ` Andy Lutomirski
2026-04-03 17:32 ` H. Peter Anvin
-- strict thread matches above, loose matches on Subject: below --
2026-04-01 14:59 Xin Li
2026-04-01 15:18 ` H. Peter Anvin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260326094423.711724-1-yi1.lai@intel.com \
--to=yi1.lai@intel.com \
--cc=andrew.cooper3@citrix.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=shuah@kernel.org \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
--cc=xin@zytor.com \
--cc=yi1.lai@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox