From: "tip-bot2 for Chang S. Bae" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: "Chang S. Bae" <chang.seok.bae@intel.com>,
Ingo Molnar <mingo@kernel.org>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: x86/fpu] selftests/x86/xstate: Introduce signal ABI test
Date: Wed, 26 Feb 2025 12:15:05 -0000 [thread overview]
Message-ID: <174057210535.10177.10652890663366192077.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20250226010731.2456-7-chang.seok.bae@intel.com>
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: e075d9fa16b3681b8a375cb47f4b5a1901e93530
Gitweb: https://git.kernel.org/tip/e075d9fa16b3681b8a375cb47f4b5a1901e93530
Author: Chang S. Bae <chang.seok.bae@intel.com>
AuthorDate: Tue, 25 Feb 2025 17:07:26 -08:00
Committer: Ingo Molnar <mingo@kernel.org>
CommitterDate: Wed, 26 Feb 2025 13:05:29 +01:00
selftests/x86/xstate: Introduce signal ABI test
With the refactored test cases, another xstate exposure to userspace is
through signal delivery. While amx.c includes signal-related scenarios,
its primary focus is on xstate permission management, which is largely
specific to dynamic states.
The remaining gap is testing xstate preservation and restoration across
signal delivery. The kernel defines an ABI for presenting xstate in the
signal frame, closely resembling the hardware XSAVE format, where xstate
modification is also possible.
Introduce a new test case to verify xstate preservation across signal
delivery and return, that is ensuring ABI compatibility by:
- Loading xstate before raising a signal.
- Verifying correct exposure in the signal frame
- Modifying xstate in the signal frame before returning.
- Checking the state restoration upon signal return.
Integrate this test into the AMX test suite as an initial usage site.
Expected output:
$ amx_64
...
[RUN] AMX Tile data: load xstate and raise SIGUSR1
[OK] 'magic1' is valid
[OK] 'xfeatures' in SW reserved area is valid
[OK] 'xfeatures' in XSAVE header is valid
[OK] xstate delivery was successful
[OK] 'magic2' is valid
[RUN] AMX Tile data: load new xstate from sighandler and check it after sigreturn
[OK] xstate was restored correctly
Signed-off-by: Chang S. Bae <chang.seok.bae@intel.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20250226010731.2456-7-chang.seok.bae@intel.com
---
tools/testing/selftests/x86/amx.c | 2 +-
tools/testing/selftests/x86/xstate.c | 108 ++++++++++++++++++++++++++-
tools/testing/selftests/x86/xstate.h | 1 +-
3 files changed, 111 insertions(+)
diff --git a/tools/testing/selftests/x86/amx.c b/tools/testing/selftests/x86/amx.c
index 4bafbb7..9cb691d 100644
--- a/tools/testing/selftests/x86/amx.c
+++ b/tools/testing/selftests/x86/amx.c
@@ -510,6 +510,8 @@ int main(void)
test_ptrace(XFEATURE_XTILEDATA);
+ test_signal(XFEATURE_XTILEDATA);
+
clearhandler(SIGILL);
free_stashed_xsave();
diff --git a/tools/testing/selftests/x86/xstate.c b/tools/testing/selftests/x86/xstate.c
index d318b35..b5600f4 100644
--- a/tools/testing/selftests/x86/xstate.c
+++ b/tools/testing/selftests/x86/xstate.c
@@ -21,6 +21,11 @@ static inline uint64_t xgetbv(uint32_t index)
return eax + ((uint64_t)edx << 32);
}
+static inline uint64_t get_xstatebv(struct xsave_buffer *xbuf)
+{
+ return *(uint64_t *)(&xbuf->header);
+}
+
static struct xstate_info xstate;
struct futex_info {
@@ -325,3 +330,106 @@ void test_ptrace(uint32_t feature_num)
if (!WIFEXITED(status) || WEXITSTATUS(status))
ksft_exit_fail_msg("ptracee exit error\n");
}
+
+/*
+ * Test signal delivery for the ABI compatibility.
+ * See the ABI format: arch/x86/include/uapi/asm/sigcontext.h
+ */
+
+/*
+ * Avoid using printf() in signal handlers as it is not
+ * async-signal-safe.
+ */
+#define SIGNAL_BUF_LEN 1000
+static char signal_message_buffer[SIGNAL_BUF_LEN];
+static void sig_print(char *msg)
+{
+ int left = SIGNAL_BUF_LEN - strlen(signal_message_buffer) - 1;
+
+ strncat(signal_message_buffer, msg, left);
+}
+
+static struct xsave_buffer *stashed_xbuf;
+
+static void validate_sigfpstate(int sig, siginfo_t *si, void *ctx_void)
+{
+ ucontext_t *ctx = (ucontext_t *)ctx_void;
+ void *xbuf = ctx->uc_mcontext.fpregs;
+ struct _fpx_sw_bytes *sw_bytes;
+ uint32_t magic2;
+
+ /* Reset the signal message buffer: */
+ signal_message_buffer[0] = '\0';
+
+ sw_bytes = get_fpx_sw_bytes(xbuf);
+ if (sw_bytes->magic1 == FP_XSTATE_MAGIC1)
+ sig_print("[OK]\t'magic1' is valid\n");
+ else
+ sig_print("[FAIL]\t'magic1' is not valid\n");
+
+ if (get_fpx_sw_bytes_features(xbuf) & xstate.mask)
+ sig_print("[OK]\t'xfeatures' in SW reserved area is valid\n");
+ else
+ sig_print("[FAIL]\t'xfeatures' in SW reserved area is not valid\n");
+
+ if (get_xstatebv(xbuf) & xstate.mask)
+ sig_print("[OK]\t'xfeatures' in XSAVE header is valid\n");
+ else
+ sig_print("[FAIL]\t'xfeatures' in XSAVE hader is not valid\n");
+
+ if (validate_xstate_same(stashed_xbuf, xbuf))
+ sig_print("[OK]\txstate delivery was successful\n");
+ else
+ sig_print("[FAIL]\txstate delivery was not successful\n");
+
+ magic2 = *(uint32_t *)(xbuf + sw_bytes->xstate_size);
+ if (magic2 == FP_XSTATE_MAGIC2)
+ sig_print("[OK]\t'magic2' is valid\n");
+ else
+ sig_print("[FAIL]\t'magic2' is not valid\n");
+
+ set_rand_data(&xstate, xbuf);
+ copy_xstate(stashed_xbuf, xbuf);
+}
+
+void test_signal(uint32_t feature_num)
+{
+ bool valid_xstate;
+
+ xstate = get_xstate_info(feature_num);
+
+ /*
+ * The signal handler will access this to verify xstate context
+ * preservation.
+ */
+
+ stashed_xbuf = alloc_xbuf();
+ if (!stashed_xbuf)
+ ksft_exit_fail_msg("unable to allocate XSAVE buffer\n");
+
+ printf("[RUN]\t%s: load xstate and raise SIGUSR1\n", xstate.name);
+
+ sethandler(SIGUSR1, validate_sigfpstate, 0);
+
+ load_rand_xstate(&xstate, stashed_xbuf);
+
+ raise(SIGUSR1);
+
+ /*
+ * Immediately record the test result, deferring printf() to
+ * prevent unintended state contamination by that.
+ */
+ valid_xstate = validate_xregs_same(stashed_xbuf);
+ printf("%s", signal_message_buffer);
+
+ printf("[RUN]\t%s: load new xstate from sighandler and check it after sigreturn\n",
+ xstate.name);
+
+ if (valid_xstate)
+ printf("[OK]\txstate was restored correctly\n");
+ else
+ printf("[FAIL]\txstate restoration failed\n");
+
+ clearhandler(SIGUSR1);
+ free(stashed_xbuf);
+}
diff --git a/tools/testing/selftests/x86/xstate.h b/tools/testing/selftests/x86/xstate.h
index 2bf11d3..4d0ffe9 100644
--- a/tools/testing/selftests/x86/xstate.h
+++ b/tools/testing/selftests/x86/xstate.h
@@ -191,5 +191,6 @@ static inline void set_rand_data(struct xstate_info *xstate, struct xsave_buffer
void test_context_switch(uint32_t feature_num, uint32_t num_threads, uint32_t iterations);
void test_ptrace(uint32_t feature_num);
+void test_signal(uint32_t feature_num);
#endif /* __SELFTESTS_X86_XSTATE_H */
next prev parent reply other threads:[~2025-02-26 12:15 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-26 1:07 [PATCH 0/9] selftests/x86/xstate: Introduce common code for testing extended states Chang S. Bae
2025-02-26 1:07 ` [PATCH 1/9] selftests/x86: Consolidate redundant signal helper functions Chang S. Bae
2025-02-26 12:15 ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26 1:07 ` [PATCH 2/9] selftests/x86/xstate: Refactor XSAVE helpers for general use Chang S. Bae
2025-02-26 12:15 ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26 1:07 ` [PATCH 3/9] selftests/x86/xstate: Enumerate and name xstate components Chang S. Bae
2025-02-26 12:15 ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26 1:07 ` [PATCH 4/9] selftests/x86/xstate: Refactor context switching test Chang S. Bae
2025-02-26 12:15 ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26 1:07 ` [PATCH 5/9] selftests/x86/xstate: Refactor ptrace ABI test Chang S. Bae
2025-02-26 12:15 ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26 1:07 ` [PATCH 6/9] selftests/x86/xstate: Introduce signal " Chang S. Bae
2025-02-26 12:15 ` tip-bot2 for Chang S. Bae [this message]
2025-02-26 1:07 ` [PATCH 7/9] selftests/x86/xstate: Consolidate test invocations into a single entry Chang S. Bae
2025-02-26 12:15 ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26 1:07 ` [PATCH 8/9] selftests/x86/xstate: Clarify supported xstates Chang S. Bae
2025-02-26 12:15 ` [tip: x86/fpu] " tip-bot2 for Chang S. Bae
2025-02-26 1:07 ` [PATCH 9/9] selftests/x86/avx: Add AVX test Chang S. Bae
2025-02-26 12:15 ` [tip: x86/fpu] selftests/x86/avx: Add AVX tests tip-bot2 for Chang S. Bae
2025-02-26 12:07 ` [PATCH 0/9] selftests/x86/xstate: Introduce common code for testing extended states Ingo Molnar
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=174057210535.10177.10652890663366192077.tip-bot2@tip-bot2 \
--to=tip-bot2@linutronix.de \
--cc=chang.seok.bae@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=x86@kernel.org \
/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
all inboxes | Powered by JetHome®