1212b0426SMark Brown // SPDX-License-Identifier: GPL-2.0-only
2212b0426SMark Brown /*
3212b0426SMark Brown * Copyright (C) 2022 ARM Limited.
4212b0426SMark Brown * Original author: Mark Brown <broonie@kernel.org>
5212b0426SMark Brown */
6212b0426SMark Brown
7212b0426SMark Brown // SPDX-License-Identifier: GPL-2.0-only
8212b0426SMark Brown
9212b0426SMark Brown #include <linux/sched.h>
10212b0426SMark Brown #include <linux/wait.h>
11212b0426SMark Brown
12266679ffSMark Brown #include "kselftest.h"
13266679ffSMark Brown
14212b0426SMark Brown #define EXPECTED_TESTS 1
15212b0426SMark Brown
16212b0426SMark Brown int fork_test(void);
17212b0426SMark Brown int verify_fork(void);
18212b0426SMark Brown
19212b0426SMark Brown /*
20212b0426SMark Brown * If we fork the value in the parent should be unchanged and the
21212b0426SMark Brown * child should start with the same value. This is called from the
22212b0426SMark Brown * fork_test() asm function.
23212b0426SMark Brown */
fork_test_c(void)24212b0426SMark Brown int fork_test_c(void)
25212b0426SMark Brown {
26212b0426SMark Brown pid_t newpid, waiting;
27212b0426SMark Brown int child_status, parent_result;
28212b0426SMark Brown
29212b0426SMark Brown newpid = fork();
30212b0426SMark Brown if (newpid == 0) {
31212b0426SMark Brown /* In child */
32212b0426SMark Brown if (!verify_fork()) {
33266679ffSMark Brown ksft_print_msg("ZA state invalid in child\n");
34212b0426SMark Brown exit(0);
35212b0426SMark Brown } else {
36212b0426SMark Brown exit(1);
37212b0426SMark Brown }
38212b0426SMark Brown }
39212b0426SMark Brown if (newpid < 0) {
40266679ffSMark Brown ksft_print_msg("fork() failed: %d\n", newpid);
41266679ffSMark Brown
42212b0426SMark Brown return 0;
43212b0426SMark Brown }
44212b0426SMark Brown
45212b0426SMark Brown parent_result = verify_fork();
46212b0426SMark Brown if (!parent_result)
47266679ffSMark Brown ksft_print_msg("ZA state invalid in parent\n");
48212b0426SMark Brown
49212b0426SMark Brown for (;;) {
50212b0426SMark Brown waiting = waitpid(newpid, &child_status, 0);
51212b0426SMark Brown
52212b0426SMark Brown if (waiting < 0) {
53212b0426SMark Brown if (errno == EINTR)
54212b0426SMark Brown continue;
55266679ffSMark Brown ksft_print_msg("waitpid() failed: %d\n", errno);
56212b0426SMark Brown return 0;
57212b0426SMark Brown }
58212b0426SMark Brown if (waiting != newpid) {
59266679ffSMark Brown ksft_print_msg("waitpid() returned wrong PID\n");
60212b0426SMark Brown return 0;
61212b0426SMark Brown }
62212b0426SMark Brown
63212b0426SMark Brown if (!WIFEXITED(child_status)) {
64266679ffSMark Brown ksft_print_msg("child did not exit\n");
65212b0426SMark Brown return 0;
66212b0426SMark Brown }
67212b0426SMark Brown
68212b0426SMark Brown return WEXITSTATUS(child_status) && parent_result;
69212b0426SMark Brown }
70212b0426SMark Brown }
71212b0426SMark Brown
main(int argc,char ** argv)72212b0426SMark Brown int main(int argc, char **argv)
73212b0426SMark Brown {
74212b0426SMark Brown int ret, i;
75212b0426SMark Brown
76266679ffSMark Brown ksft_print_header();
77266679ffSMark Brown ksft_set_plan(EXPECTED_TESTS);
78212b0426SMark Brown
79266679ffSMark Brown ksft_print_msg("PID: %d\n", getpid());
80212b0426SMark Brown
81212b0426SMark Brown /*
82212b0426SMark Brown * This test is run with nolibc which doesn't support hwcap and
83212b0426SMark Brown * it's probably disproportionate to implement so instead check
84212b0426SMark Brown * for the default vector length configuration in /proc.
85212b0426SMark Brown */
86212b0426SMark Brown ret = open("/proc/sys/abi/sme_default_vector_length", O_RDONLY, 0);
87212b0426SMark Brown if (ret >= 0) {
88*fbb41cf2SMark Brown ksft_test_result(fork_test(), "fork_test\n");
89212b0426SMark Brown
90212b0426SMark Brown } else {
91266679ffSMark Brown ksft_print_msg("SME not supported\n");
92212b0426SMark Brown for (i = 0; i < EXPECTED_TESTS; i++) {
93266679ffSMark Brown ksft_test_result_skip("fork_test\n");
94266679ffSMark Brown }
95212b0426SMark Brown }
96212b0426SMark Brown
97266679ffSMark Brown ksft_finished();
98212b0426SMark Brown
99212b0426SMark Brown return 0;
100212b0426SMark Brown }
101