Skip to content

Commit 6d80cb7

Browse files
brooniectmarinas
authored andcommitted
kselftest/arm64: Convert tpidr2 test to use kselftest.h
Recent work by Thomas Weißschuh means that it is now possible to use kselftest.h with nolibc. Convert the tpidr2 test which is nolibc specific to use kselftest.h, making it look more standard and ensuring it gets the benefit of any work done on kselftest.h. Signed-off-by: Mark Brown <[email protected]> Link: https://lore.kernel.org/r/20250609-kselftest-arm64-nolibc-header-v1-1-16ee1c6fbfed@kernel.org Signed-off-by: Catalin Marinas <[email protected]>
1 parent 19272b3 commit 6d80cb7

File tree

2 files changed

+38
-104
lines changed

2 files changed

+38
-104
lines changed

tools/testing/selftests/arm64/abi/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ $(OUTPUT)/syscall-abi: syscall-abi.c syscall-abi-asm.S
1212
$(OUTPUT)/tpidr2: tpidr2.c
1313
$(CC) -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
1414
-static -include ../../../../include/nolibc/nolibc.h \
15-
-ffreestanding -Wall $^ -o $@ -lgcc
15+
-I../.. -ffreestanding -Wall $^ -o $@ -lgcc

tools/testing/selftests/arm64/abi/tpidr2.c

Lines changed: 37 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,12 @@
33
#include <linux/sched.h>
44
#include <linux/wait.h>
55

6+
#include "kselftest.h"
7+
68
#define SYS_TPIDR2 "S3_3_C13_C0_5"
79

810
#define EXPECTED_TESTS 5
911

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-
3112
static void set_tpidr2(uint64_t val)
3213
{
3314
asm volatile (
@@ -50,20 +31,6 @@ static uint64_t get_tpidr2(void)
5031
return val;
5132
}
5233

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-
6734
/* Processes should start with TPIDR2 == 0 */
6835
static int default_value(void)
6936
{
@@ -105,24 +72,21 @@ static int write_fork_read(void)
10572
if (newpid == 0) {
10673
/* In child */
10774
if (get_tpidr2() != oldpid) {
108-
putstr("# TPIDR2 changed in child: ");
109-
putnum(get_tpidr2());
110-
putstr("\n");
75+
ksft_print_msg("TPIDR2 changed in child: %llx\n",
76+
get_tpidr2());
11177
exit(0);
11278
}
11379

11480
set_tpidr2(getpid());
11581
if (get_tpidr2() == getpid()) {
11682
exit(1);
11783
} else {
118-
putstr("# Failed to set TPIDR2 in child\n");
84+
ksft_print_msg("Failed to set TPIDR2 in child\n");
11985
exit(0);
12086
}
12187
}
12288
if (newpid < 0) {
123-
putstr("# fork() failed: -");
124-
putnum(-newpid);
125-
putstr("\n");
89+
ksft_print_msg("fork() failed: %d\n", newpid);
12690
return 0;
12791
}
12892

@@ -132,23 +96,22 @@ static int write_fork_read(void)
13296
if (waiting < 0) {
13397
if (errno == EINTR)
13498
continue;
135-
putstr("# waitpid() failed: ");
136-
putnum(errno);
137-
putstr("\n");
99+
ksft_print_msg("waitpid() failed: %d\n", errno);
138100
return 0;
139101
}
140102
if (waiting != newpid) {
141-
putstr("# waitpid() returned wrong PID\n");
103+
ksft_print_msg("waitpid() returned wrong PID: %d != %d\n",
104+
waiting, newpid);
142105
return 0;
143106
}
144107

145108
if (!WIFEXITED(status)) {
146-
putstr("# child did not exit\n");
109+
ksft_print_msg("child did not exit\n");
147110
return 0;
148111
}
149112

150113
if (getpid() != get_tpidr2()) {
151-
putstr("# TPIDR2 corrupted in parent\n");
114+
ksft_print_msg("TPIDR2 corrupted in parent\n");
152115
return 0;
153116
}
154117

@@ -188,35 +151,32 @@ static int write_clone_read(void)
188151

189152
stack = malloc(__STACK_SIZE);
190153
if (!stack) {
191-
putstr("# malloc() failed\n");
154+
ksft_print_msg("malloc() failed\n");
192155
return 0;
193156
}
194157

195158
ret = sys_clone(CLONE_VM, (unsigned long)stack + __STACK_SIZE,
196159
&parent_tid, 0, &child_tid);
197160
if (ret == -1) {
198-
putstr("# clone() failed\n");
199-
putnum(errno);
200-
putstr("\n");
161+
ksft_print_msg("clone() failed: %d\n", errno);
201162
return 0;
202163
}
203164

204165
if (ret == 0) {
205166
/* In child */
206167
if (get_tpidr2() != 0) {
207-
putstr("# TPIDR2 non-zero in child: ");
208-
putnum(get_tpidr2());
209-
putstr("\n");
168+
ksft_print_msg("TPIDR2 non-zero in child: %llx\n",
169+
get_tpidr2());
210170
exit(0);
211171
}
212172

213173
if (gettid() == 0)
214-
putstr("# Child TID==0\n");
174+
ksft_print_msg("Child TID==0\n");
215175
set_tpidr2(gettid());
216176
if (get_tpidr2() == gettid()) {
217177
exit(1);
218178
} else {
219-
putstr("# Failed to set TPIDR2 in child\n");
179+
ksft_print_msg("Failed to set TPIDR2 in child\n");
220180
exit(0);
221181
}
222182
}
@@ -227,61 +187,37 @@ static int write_clone_read(void)
227187
if (waiting < 0) {
228188
if (errno == EINTR)
229189
continue;
230-
putstr("# wait4() failed: ");
231-
putnum(errno);
232-
putstr("\n");
190+
ksft_print_msg("wait4() failed: %d\n", errno);
233191
return 0;
234192
}
235193
if (waiting != ret) {
236-
putstr("# wait4() returned wrong PID ");
237-
putnum(waiting);
238-
putstr("\n");
194+
ksft_print_msg("wait4() returned wrong PID %d\n",
195+
waiting);
239196
return 0;
240197
}
241198

242199
if (!WIFEXITED(status)) {
243-
putstr("# child did not exit\n");
200+
ksft_print_msg("child did not exit\n");
244201
return 0;
245202
}
246203

247204
if (parent != get_tpidr2()) {
248-
putstr("# TPIDR2 corrupted in parent\n");
205+
ksft_print_msg("TPIDR2 corrupted in parent\n");
249206
return 0;
250207
}
251208

252209
return WEXITSTATUS(status);
253210
}
254211
}
255212

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-
273213
int main(int argc, char **argv)
274214
{
275215
int ret;
276216

277-
putstr("TAP version 13\n");
278-
putstr("1..");
279-
putnum(EXPECTED_TESTS);
280-
putstr("\n");
217+
ksft_print_header();
218+
ksft_set_plan(5);
281219

282-
putstr("# PID: ");
283-
putnum(getpid());
284-
putstr("\n");
220+
ksft_print_msg("PID: %d\n", getpid());
285221

286222
/*
287223
* This test is run with nolibc which doesn't support hwcap and
@@ -290,23 +226,21 @@ int main(int argc, char **argv)
290226
*/
291227
ret = open("/proc/sys/abi/sme_default_vector_length", O_RDONLY, 0);
292228
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);
229+
ksft_test_result(default_value(), "default_value\n");
230+
ksft_test_result(write_read, "write_read\n");
231+
ksft_test_result(write_sleep_read, "write_sleep_read\n");
232+
ksft_test_result(write_fork_read, "write_fork_read\n");
233+
ksft_test_result(write_clone_read, "write_clone_read\n");
298234

299235
} else {
300-
putstr("# SME support not present\n");
236+
ksft_print_msg("SME support not present\n");
301237

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);
238+
ksft_test_result_skip("default_value\n");
239+
ksft_test_result_skip("write_read\n");
240+
ksft_test_result_skip("write_sleep_read\n");
241+
ksft_test_result_skip("write_fork_read\n");
242+
ksft_test_result_skip("write_clone_read\n");
307243
}
308244

309-
print_summary();
310-
311-
return 0;
245+
ksft_finished();
312246
}

0 commit comments

Comments
 (0)