aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/arm64/fp/za-fork.c
blob: ff475c649e96698eab7c0613c131d4a2025d0b6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2022 ARM Limited.
 * Original author: Mark Brown <broonie@kernel.org>
 */

// SPDX-License-Identifier: GPL-2.0-only

#include <linux/sched.h>
#include <linux/wait.h>

#define EXPECTED_TESTS 1

static void putstr(const char *str)
{
	write(1, str, strlen(str));
}

static void putnum(unsigned int num)
{
	char c;

	if (num / 10)
		putnum(num / 10);

	c = '0' + (num % 10);
	write(1, &c, 1);
}

static int tests_run;
static int tests_passed;
static int tests_failed;
static int tests_skipped;

static void print_summary(void)
{
	if (tests_passed + tests_failed + tests_skipped != EXPECTED_TESTS)
		putstr("# UNEXPECTED TEST COUNT: ");

	putstr("# Totals: pass:");
	putnum(tests_passed);
	putstr(" fail:");
	putnum(tests_failed);
	putstr(" xfail:0 xpass:0 skip:");
	putnum(tests_skipped);
	putstr(" error:0\n");
}

int fork_test(void);
int verify_fork(void);

/*
 * If we fork the value in the parent should be unchanged and the
 * child should start with the same value.  This is called from the
 * fork_test() asm function.
 */
int fork_test_c(void)
{
	pid_t newpid, waiting;
	int child_status, parent_result;

	newpid = fork();
	if (newpid == 0) {
		/* In child */
		if (!verify_fork()) {
			putstr("# ZA state invalid in child\n");
			exit(0);
		} else {
			exit(1);
		}
	}
	if (newpid < 0) {
		putstr("# fork() failed: -");
		putnum(-newpid);
		putstr("\n");
		return 0;
	}

	parent_result = verify_fork();
	if (!parent_result)
		putstr("# ZA state invalid in parent\n");

	for (;;) {
		waiting = waitpid(newpid, &child_status, 0);

		if (waiting < 0) {
			if (errno == EINTR)
				continue;
			putstr("# waitpid() failed: ");
			putnum(errno);
			putstr("\n");
			return 0;
		}
		if (waiting != newpid) {
			putstr("# waitpid() returned wrong PID\n");
			return 0;
		}

		if (!WIFEXITED(child_status)) {
			putstr("# child did not exit\n");
			return 0;
		}

		return WEXITSTATUS(child_status) && parent_result;
	}
}

#define run_test(name)			     \
	if (name()) {			     \
		tests_passed++;		     \
	} else {			     \
		tests_failed++;		     \
		putstr("not ");		     \
	}				     \
	putstr("ok ");			     \
	putnum(++tests_run);		     \
	putstr(" " #name "\n");

int main(int argc, char **argv)
{
	int ret, i;

	putstr("TAP version 13\n");
	putstr("1..");
	putnum(EXPECTED_TESTS);
	putstr("\n");

	putstr("# PID: ");
	putnum(getpid());
	putstr("\n");

	/*
	 * This test is run with nolibc which doesn't support hwcap and
	 * it's probably disproportionate to implement so instead check
	 * for the default vector length configuration in /proc.
	 */
	ret = open("/proc/sys/abi/sme_default_vector_length", O_RDONLY, 0);
	if (ret >= 0) {
		run_test(fork_test);

	} else {
		putstr("# SME support not present\n");

		for (i = 0; i < EXPECTED_TESTS; i++) {
			putstr("ok ");
			putnum(i);
			putstr(" skipped\n");
		}

		tests_skipped += EXPECTED_TESTS;
	}

	print_summary();

	return 0;
}