xref: /linux/tools/testing/selftests/arm64/abi/tpidr2.c (revision 47cf96fbe393839b125a9b694a8cfdd3f4216baa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/sched.h>
4 #include <linux/wait.h>
5 
6 #define SYS_TPIDR2 "S3_3_C13_C0_5"
7 
8 #define EXPECTED_TESTS 5
9 
10 static void putstr(const char *str)
11 {
12 	write(1, str, strlen(str));
13 }
14 
15 static void putnum(unsigned int num)
16 {
17 	char c;
18 
19 	if (num / 10)
20 		putnum(num / 10);
21 
22 	c = '0' + (num % 10);
23 	write(1, &c, 1);
24 }
25 
26 static int tests_run;
27 static int tests_passed;
28 static int tests_failed;
29 static int tests_skipped;
30 
31 static void set_tpidr2(uint64_t val)
32 {
33 	asm volatile (
34 		"msr	" SYS_TPIDR2 ", %0\n"
35 		:
36 		: "r"(val)
37 		: "cc");
38 }
39 
40 static uint64_t get_tpidr2(void)
41 {
42 	uint64_t val;
43 
44 	asm volatile (
45 		"mrs	%0, " SYS_TPIDR2 "\n"
46 		: "=r"(val)
47 		:
48 		: "cc");
49 
50 	return val;
51 }
52 
53 static void print_summary(void)
54 {
55 	if (tests_passed + tests_failed + tests_skipped != EXPECTED_TESTS)
56 		putstr("# UNEXPECTED TEST COUNT: ");
57 
58 	putstr("# Totals: pass:");
59 	putnum(tests_passed);
60 	putstr(" fail:");
61 	putnum(tests_failed);
62 	putstr(" xfail:0 xpass:0 skip:");
63 	putnum(tests_skipped);
64 	putstr(" error:0\n");
65 }
66 
67 /* Processes should start with TPIDR2 == 0 */
68 static int default_value(void)
69 {
70 	return get_tpidr2() == 0;
71 }
72 
73 /* If we set TPIDR2 we should read that value */
74 static int write_read(void)
75 {
76 	set_tpidr2(getpid());
77 
78 	return getpid() == get_tpidr2();
79 }
80 
81 /* If we set a value we should read the same value after scheduling out */
82 static int write_sleep_read(void)
83 {
84 	set_tpidr2(getpid());
85 
86 	msleep(100);
87 
88 	return getpid() == get_tpidr2();
89 }
90 
91 /*
92  * If we fork the value in the parent should be unchanged and the
93  * child should start with the same value and be able to set its own
94  * value.
95  */
96 static int write_fork_read(void)
97 {
98 	pid_t newpid, waiting, oldpid;
99 	int status;
100 
101 	set_tpidr2(getpid());
102 
103 	oldpid = getpid();
104 	newpid = fork();
105 	if (newpid == 0) {
106 		/* In child */
107 		if (get_tpidr2() != oldpid) {
108 			putstr("# TPIDR2 changed in child: ");
109 			putnum(get_tpidr2());
110 			putstr("\n");
111 			exit(0);
112 		}
113 
114 		set_tpidr2(getpid());
115 		if (get_tpidr2() == getpid()) {
116 			exit(1);
117 		} else {
118 			putstr("# Failed to set TPIDR2 in child\n");
119 			exit(0);
120 		}
121 	}
122 	if (newpid < 0) {
123 		putstr("# fork() failed: -");
124 		putnum(-newpid);
125 		putstr("\n");
126 		return 0;
127 	}
128 
129 	for (;;) {
130 		waiting = waitpid(newpid, &status, 0);
131 
132 		if (waiting < 0) {
133 			if (errno == EINTR)
134 				continue;
135 			putstr("# waitpid() failed: ");
136 			putnum(errno);
137 			putstr("\n");
138 			return 0;
139 		}
140 		if (waiting != newpid) {
141 			putstr("# waitpid() returned wrong PID\n");
142 			return 0;
143 		}
144 
145 		if (!WIFEXITED(status)) {
146 			putstr("# child did not exit\n");
147 			return 0;
148 		}
149 
150 		if (getpid() != get_tpidr2()) {
151 			putstr("# TPIDR2 corrupted in parent\n");
152 			return 0;
153 		}
154 
155 		return WEXITSTATUS(status);
156 	}
157 }
158 
159 /*
160  * sys_clone() has a lot of per architecture variation so just define
161  * it here rather than adding it to nolibc, plus the raw API is a
162  * little more convenient for this test.
163  */
164 static int sys_clone(unsigned long clone_flags, unsigned long newsp,
165 		     int *parent_tidptr, unsigned long tls,
166 		     int *child_tidptr)
167 {
168 	return my_syscall5(__NR_clone, clone_flags, newsp, parent_tidptr, tls,
169 			   child_tidptr);
170 }
171 
172 #define __STACK_SIZE (8 * 1024 * 1024)
173 
174 /*
175  * If we clone with CLONE_VM then the value in the parent should
176  * be unchanged and the child should start with zero and be able to
177  * set its own value.
178  */
179 static int write_clone_read(void)
180 {
181 	int parent_tid, child_tid;
182 	pid_t parent, waiting;
183 	int ret, status;
184 	void *stack;
185 
186 	parent = getpid();
187 	set_tpidr2(parent);
188 
189 	stack = malloc(__STACK_SIZE);
190 	if (!stack) {
191 		putstr("# malloc() failed\n");
192 		return 0;
193 	}
194 
195 	ret = sys_clone(CLONE_VM, (unsigned long)stack + __STACK_SIZE,
196 			&parent_tid, 0, &child_tid);
197 	if (ret == -1) {
198 		putstr("# clone() failed\n");
199 		putnum(errno);
200 		putstr("\n");
201 		return 0;
202 	}
203 
204 	if (ret == 0) {
205 		/* In child */
206 		if (get_tpidr2() != 0) {
207 			putstr("# TPIDR2 non-zero in child: ");
208 			putnum(get_tpidr2());
209 			putstr("\n");
210 			exit(0);
211 		}
212 
213 		if (gettid() == 0)
214 			putstr("# Child TID==0\n");
215 		set_tpidr2(gettid());
216 		if (get_tpidr2() == gettid()) {
217 			exit(1);
218 		} else {
219 			putstr("# Failed to set TPIDR2 in child\n");
220 			exit(0);
221 		}
222 	}
223 
224 	for (;;) {
225 		waiting = wait4(ret, &status, __WCLONE, NULL);
226 
227 		if (waiting < 0) {
228 			if (errno == EINTR)
229 				continue;
230 			putstr("# wait4() failed: ");
231 			putnum(errno);
232 			putstr("\n");
233 			return 0;
234 		}
235 		if (waiting != ret) {
236 			putstr("# wait4() returned wrong PID ");
237 			putnum(waiting);
238 			putstr("\n");
239 			return 0;
240 		}
241 
242 		if (!WIFEXITED(status)) {
243 			putstr("# child did not exit\n");
244 			return 0;
245 		}
246 
247 		if (parent != get_tpidr2()) {
248 			putstr("# TPIDR2 corrupted in parent\n");
249 			return 0;
250 		}
251 
252 		return WEXITSTATUS(status);
253 	}
254 }
255 
256 #define run_test(name)			     \
257 	if (name()) {			     \
258 		tests_passed++;		     \
259 	} else {			     \
260 		tests_failed++;		     \
261 		putstr("not ");		     \
262 	}				     \
263 	putstr("ok ");			     \
264 	putnum(++tests_run);		     \
265 	putstr(" " #name "\n");
266 
267 #define skip_test(name)			     \
268 	tests_skipped++;		     \
269 	putstr("ok ");			     \
270 	putnum(++tests_run);		     \
271 	putstr(" # SKIP " #name "\n");
272 
273 int main(int argc, char **argv)
274 {
275 	int ret;
276 
277 	putstr("TAP version 13\n");
278 	putstr("1..");
279 	putnum(EXPECTED_TESTS);
280 	putstr("\n");
281 
282 	putstr("# PID: ");
283 	putnum(getpid());
284 	putstr("\n");
285 
286 	/*
287 	 * This test is run with nolibc which doesn't support hwcap and
288 	 * it's probably disproportionate to implement so instead check
289 	 * for the default vector length configuration in /proc.
290 	 */
291 	ret = open("/proc/sys/abi/sme_default_vector_length", O_RDONLY, 0);
292 	if (ret >= 0) {
293 		run_test(default_value);
294 		run_test(write_read);
295 		run_test(write_sleep_read);
296 		run_test(write_fork_read);
297 		run_test(write_clone_read);
298 
299 	} else {
300 		putstr("# SME support not present\n");
301 
302 		skip_test(default_value);
303 		skip_test(write_read);
304 		skip_test(write_sleep_read);
305 		skip_test(write_fork_read);
306 		skip_test(write_clone_read);
307 	}
308 
309 	print_summary();
310 
311 	return 0;
312 }
313