@@ -45,7 +45,7 @@
#include "xstate_helpers.h"
#include "../kselftest.h"
-#define NUM_TESTS 1
+#define NUM_TESTS 3
#define xstate_test_array_init(idx, init_opt, fill_opt) \
do { \
xstate_tests[idx].init = init_opt; \
@@ -135,6 +135,25 @@ static void test_xstate_sig_handle(void)
compare_buf_result(valid_xbuf, compared_xbuf, case_name1);
}
+static void test_xstate_fork(void)
+{
+ const char *case_name2 = "xstate of child process should be same as xstate of parent";
+ const char *case_name3 = "parent xstate should be same after context switch";
+
+ ksft_print_msg("[RUN]\tParent pid:%d check xstate around fork test.\n",
+ getpid());
+ /* Child process xstate should be same as the parent process xstate. */
+ if (xstate_fork(valid_xbuf, compared_xbuf, xstate_info.mask,
+ xstate_size)) {
+ ksft_test_result_pass("The case: %s.\n", case_name2);
+ } else {
+ ksft_test_result_fail("The case: %s.\n", case_name2);
+ }
+
+ /* The parent process xstate should not change after context switch. */
+ compare_buf_result(valid_xbuf, compared_xbuf, case_name3);
+}
+
static void prepare_xstate_test(void)
{
xstate_test_array_init(XFEATURE_FP, init_legacy_info,
@@ -153,6 +172,7 @@ static void prepare_xstate_test(void)
fill_pkru_xstate_buf);
xstate_tests[XSTATE_CASE_SIG].xstate_case = test_xstate_sig_handle;
+ xstate_tests[XSTATE_CASE_FORK].xstate_case = test_xstate_fork;
}
static void test_xstate(void)
@@ -87,6 +87,7 @@ enum supportability {
enum xstate_case {
XSTATE_CASE_SIG,
+ XSTATE_CASE_FORK,
XSTATE_CASE_MAX,
};
@@ -158,3 +158,54 @@ bool xstate_sig_handle(void *valid_xbuf, void *compared_xbuf, uint64_t mask,
return sigusr1_done;
}
+
+bool xstate_fork(void *valid_xbuf, void *compared_xbuf, uint64_t mask,
+ uint32_t xstate_size)
+{
+ pid_t child;
+ int status, fd[2];
+ bool child_result;
+
+ memset(compared_xbuf, 0, xstate_size);
+ /* Use pipe to transfer test result to parent process. */
+ if (pipe(fd) < 0)
+ fatal_error("create pipe failed");
+ /*
+ * Xrstor the valid_xbuf and call syscall assembly instruction, then
+ * save the xstate to compared_xbuf in child process for comparison.
+ */
+ __xrstor(valid_xbuf, mask);
+ child = __fork();
+ if (child < 0) {
+ /* Fork syscall failed */
+ fatal_error("fork failed");
+ } else if (child == 0) {
+ /* Fork syscall succeeded, now in the child. */
+ __xsave(compared_xbuf, mask);
+
+ if (memcmp(valid_xbuf, compared_xbuf, xstate_size))
+ child_result = false;
+ else
+ child_result = true;
+
+ /*
+ * Transfer the child process test result to
+ * the parent process for aggregation.
+ */
+ close(fd[0]);
+ if (!write(fd[1], &child_result, sizeof(child_result)))
+ fatal_error("write fd failed");
+ _exit(0);
+ } else {
+ /* Fork syscall succeeded, now in the parent. */
+ __xsave(compared_xbuf, mask);
+ if (waitpid(child, &status, 0) != child || !WIFEXITED(status)) {
+ fatal_error("Child exit with error status");
+ } else {
+ close(fd[1]);
+ if (!read(fd[0], &child_result, sizeof(child_result)))
+ fatal_error("read fd failed");
+ return child_result;
+ }
+ }
+}
@@ -6,3 +6,5 @@ extern void fill_fp_mxcsr_xstate_buf(void *buf, int xfeature_num,
uint8_t ui8_fp);
extern bool xstate_sig_handle(void *valid_xbuf, void *compared_xbuf,
uint64_t mask, uint32_t xstate_size);
+extern bool xstate_fork(void *valid_xbuf, void *compared_xbuf,
+ uint64_t mask, uint32_t xstate_size);