summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/arm64/abi/tpidr2.c
blob: 351a098b503ad2bacdb56bb6489c08223a39576f (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// SPDX-License-Identifier: GPL-2.0-only

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

#define SYS_TPIDR2 "S3_3_C13_C0_5"

#define EXPECTED_TESTS 5

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 set_tpidr2(uint64_t val)
{
	asm volatile (
		"msr	" SYS_TPIDR2 ", %0\n"
		:
		: "r"(val)
		: "cc");
}

static uint64_t get_tpidr2(void)
{
	uint64_t val;

	asm volatile (
		"mrs	%0, " SYS_TPIDR2 "\n"
		: "=r"(val)
		:
		: "cc");

	return val;
}

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");
}

/* Processes should start with TPIDR2 == 0 */
static int default_value(void)
{
	return get_tpidr2() == 0;
}

/* If we set TPIDR2 we should read that value */
static int write_read(void)
{
	set_tpidr2(getpid());

	return getpid() == get_tpidr2();
}

/* If we set a value we should read the same value after scheduling out */
static int write_sleep_read(void)
{
	set_tpidr2(getpid());

	msleep(100);

	return getpid() == get_tpidr2();
}

/*
 * If we fork the value in the parent should be unchanged and the
 * child should start with the same value and be able to set its own
 * value.
 */
static int write_fork_read(void)
{
	pid_t newpid, waiting, oldpid;
	int status;

	set_tpidr2(getpid());

	oldpid = getpid();
	newpid = fork();
	if (newpid == 0) {
		/* In child */
		if (get_tpidr2() != oldpid) {
			putstr("# TPIDR2 changed in child: ");
			putnum(get_tpidr2());
			putstr("\n");
			exit(0);
		}

		set_tpidr2(getpid());
		if (get_tpidr2() == getpid()) {
			exit(1);
		} else {
			putstr("# Failed to set TPIDR2 in child\n");
			exit(0);
		}
	}
	if (newpid < 0) {
		putstr("# fork() failed: -");
		putnum(-newpid);
		putstr("\n");
		return 0;
	}

	for (;;) {
		waiting = waitpid(newpid, &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(status)) {
			putstr("# child did not exit\n");
			return 0;
		}

		if (getpid() != get_tpidr2()) {
			putstr("# TPIDR2 corrupted in parent\n");
			return 0;
		}

		return WEXITSTATUS(status);
	}
}

/*
 * sys_clone() has a lot of per architecture variation so just define
 * it here rather than adding it to nolibc, plus the raw API is a
 * little more convenient for this test.
 */
static int sys_clone(unsigned long clone_flags, unsigned long newsp,
		     int *parent_tidptr, unsigned long tls,
		     int *child_tidptr)
{
	return my_syscall5(__NR_clone, clone_flags, newsp, parent_tidptr, tls,
			   child_tidptr);
}

/*
 * If we clone with CLONE_SETTLS then the value in the parent should
 * be unchanged and the child should start with zero and be able to
 * set its own value.
 */
static int write_clone_read(void)
{
	int parent_tid, child_tid;
	pid_t parent, waiting;
	int ret, status;

	parent = getpid();
	set_tpidr2(parent);

	ret = sys_clone(CLONE_SETTLS, 0, &parent_tid, 0, &child_tid);
	if (ret == -1) {
		putstr("# clone() failed\n");
		putnum(errno);
		putstr("\n");
		return 0;
	}

	if (ret == 0) {
		/* In child */
		if (get_tpidr2() != 0) {
			putstr("# TPIDR2 non-zero in child: ");
			putnum(get_tpidr2());
			putstr("\n");
			exit(0);
		}

		if (gettid() == 0)
			putstr("# Child TID==0\n");
		set_tpidr2(gettid());
		if (get_tpidr2() == gettid()) {
			exit(1);
		} else {
			putstr("# Failed to set TPIDR2 in child\n");
			exit(0);
		}
	}

	for (;;) {
		waiting = wait4(ret, &status, __WCLONE, NULL);

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

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

		if (parent != get_tpidr2()) {
			putstr("# TPIDR2 corrupted in parent\n");
			return 0;
		}

		return WEXITSTATUS(status);
	}
}

#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(default_value);
		run_test(write_read);
		run_test(write_sleep_read);
		run_test(write_fork_read);
		run_test(write_clone_read);

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

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

		tests_skipped += EXPECTED_TESTS;
	}

	print_summary();

	return 0;
}