Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <broonie@kernel.org>
Link: https://lore.kernel.org/r/20250609-kselftest-arm64-nolibc-header-v1-1-16ee1c6fbfed@kernel.org
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>

authored by

Mark Brown and committed by
Catalin Marinas
6d80cb73 19272b37

+38 -104
+1 -1
tools/testing/selftests/arm64/abi/Makefile
··· 12 12 $(OUTPUT)/tpidr2: tpidr2.c 13 13 $(CC) -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ 14 14 -static -include ../../../../include/nolibc/nolibc.h \ 15 - -ffreestanding -Wall $^ -o $@ -lgcc 15 + -I../.. -ffreestanding -Wall $^ -o $@ -lgcc
+37 -103
tools/testing/selftests/arm64/abi/tpidr2.c
··· 3 3 #include <linux/sched.h> 4 4 #include <linux/wait.h> 5 5 6 + #include "kselftest.h" 7 + 6 8 #define SYS_TPIDR2 "S3_3_C13_C0_5" 7 9 8 10 #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 11 31 12 static void set_tpidr2(uint64_t val) 32 13 { ··· 29 48 : "cc"); 30 49 31 50 return val; 32 - } 33 - 34 - static void print_summary(void) 35 - { 36 - if (tests_passed + tests_failed + tests_skipped != EXPECTED_TESTS) 37 - putstr("# UNEXPECTED TEST COUNT: "); 38 - 39 - putstr("# Totals: pass:"); 40 - putnum(tests_passed); 41 - putstr(" fail:"); 42 - putnum(tests_failed); 43 - putstr(" xfail:0 xpass:0 skip:"); 44 - putnum(tests_skipped); 45 - putstr(" error:0\n"); 46 51 } 47 52 48 53 /* Processes should start with TPIDR2 == 0 */ ··· 72 105 if (newpid == 0) { 73 106 /* In child */ 74 107 if (get_tpidr2() != oldpid) { 75 - putstr("# TPIDR2 changed in child: "); 76 - putnum(get_tpidr2()); 77 - putstr("\n"); 108 + ksft_print_msg("TPIDR2 changed in child: %llx\n", 109 + get_tpidr2()); 78 110 exit(0); 79 111 } 80 112 ··· 81 115 if (get_tpidr2() == getpid()) { 82 116 exit(1); 83 117 } else { 84 - putstr("# Failed to set TPIDR2 in child\n"); 118 + ksft_print_msg("Failed to set TPIDR2 in child\n"); 85 119 exit(0); 86 120 } 87 121 } 88 122 if (newpid < 0) { 89 - putstr("# fork() failed: -"); 90 - putnum(-newpid); 91 - putstr("\n"); 123 + ksft_print_msg("fork() failed: %d\n", newpid); 92 124 return 0; 93 125 } 94 126 ··· 96 132 if (waiting < 0) { 97 133 if (errno == EINTR) 98 134 continue; 99 - putstr("# waitpid() failed: "); 100 - putnum(errno); 101 - putstr("\n"); 135 + ksft_print_msg("waitpid() failed: %d\n", errno); 102 136 return 0; 103 137 } 104 138 if (waiting != newpid) { 105 - putstr("# waitpid() returned wrong PID\n"); 139 + ksft_print_msg("waitpid() returned wrong PID: %d != %d\n", 140 + waiting, newpid); 106 141 return 0; 107 142 } 108 143 109 144 if (!WIFEXITED(status)) { 110 - putstr("# child did not exit\n"); 145 + ksft_print_msg("child did not exit\n"); 111 146 return 0; 112 147 } 113 148 114 149 if (getpid() != get_tpidr2()) { 115 - putstr("# TPIDR2 corrupted in parent\n"); 150 + ksft_print_msg("TPIDR2 corrupted in parent\n"); 116 151 return 0; 117 152 } 118 153 ··· 151 188 152 189 stack = malloc(__STACK_SIZE); 153 190 if (!stack) { 154 - putstr("# malloc() failed\n"); 191 + ksft_print_msg("malloc() failed\n"); 155 192 return 0; 156 193 } 157 194 158 195 ret = sys_clone(CLONE_VM, (unsigned long)stack + __STACK_SIZE, 159 196 &parent_tid, 0, &child_tid); 160 197 if (ret == -1) { 161 - putstr("# clone() failed\n"); 162 - putnum(errno); 163 - putstr("\n"); 198 + ksft_print_msg("clone() failed: %d\n", errno); 164 199 return 0; 165 200 } 166 201 167 202 if (ret == 0) { 168 203 /* In child */ 169 204 if (get_tpidr2() != 0) { 170 - putstr("# TPIDR2 non-zero in child: "); 171 - putnum(get_tpidr2()); 172 - putstr("\n"); 205 + ksft_print_msg("TPIDR2 non-zero in child: %llx\n", 206 + get_tpidr2()); 173 207 exit(0); 174 208 } 175 209 176 210 if (gettid() == 0) 177 - putstr("# Child TID==0\n"); 211 + ksft_print_msg("Child TID==0\n"); 178 212 set_tpidr2(gettid()); 179 213 if (get_tpidr2() == gettid()) { 180 214 exit(1); 181 215 } else { 182 - putstr("# Failed to set TPIDR2 in child\n"); 216 + ksft_print_msg("Failed to set TPIDR2 in child\n"); 183 217 exit(0); 184 218 } 185 219 } ··· 187 227 if (waiting < 0) { 188 228 if (errno == EINTR) 189 229 continue; 190 - putstr("# wait4() failed: "); 191 - putnum(errno); 192 - putstr("\n"); 230 + ksft_print_msg("wait4() failed: %d\n", errno); 193 231 return 0; 194 232 } 195 233 if (waiting != ret) { 196 - putstr("# wait4() returned wrong PID "); 197 - putnum(waiting); 198 - putstr("\n"); 234 + ksft_print_msg("wait4() returned wrong PID %d\n", 235 + waiting); 199 236 return 0; 200 237 } 201 238 202 239 if (!WIFEXITED(status)) { 203 - putstr("# child did not exit\n"); 240 + ksft_print_msg("child did not exit\n"); 204 241 return 0; 205 242 } 206 243 207 244 if (parent != get_tpidr2()) { 208 - putstr("# TPIDR2 corrupted in parent\n"); 245 + ksft_print_msg("TPIDR2 corrupted in parent\n"); 209 246 return 0; 210 247 } 211 248 ··· 210 253 } 211 254 } 212 255 213 - #define run_test(name) \ 214 - if (name()) { \ 215 - tests_passed++; \ 216 - } else { \ 217 - tests_failed++; \ 218 - putstr("not "); \ 219 - } \ 220 - putstr("ok "); \ 221 - putnum(++tests_run); \ 222 - putstr(" " #name "\n"); 223 - 224 - #define skip_test(name) \ 225 - tests_skipped++; \ 226 - putstr("ok "); \ 227 - putnum(++tests_run); \ 228 - putstr(" # SKIP " #name "\n"); 229 - 230 256 int main(int argc, char **argv) 231 257 { 232 258 int ret; 233 259 234 - putstr("TAP version 13\n"); 235 - putstr("1.."); 236 - putnum(EXPECTED_TESTS); 237 - putstr("\n"); 260 + ksft_print_header(); 261 + ksft_set_plan(5); 238 262 239 - putstr("# PID: "); 240 - putnum(getpid()); 241 - putstr("\n"); 263 + ksft_print_msg("PID: %d\n", getpid()); 242 264 243 265 /* 244 266 * This test is run with nolibc which doesn't support hwcap and ··· 226 290 */ 227 291 ret = open("/proc/sys/abi/sme_default_vector_length", O_RDONLY, 0); 228 292 if (ret >= 0) { 229 - run_test(default_value); 230 - run_test(write_read); 231 - run_test(write_sleep_read); 232 - run_test(write_fork_read); 233 - run_test(write_clone_read); 293 + ksft_test_result(default_value(), "default_value\n"); 294 + ksft_test_result(write_read, "write_read\n"); 295 + ksft_test_result(write_sleep_read, "write_sleep_read\n"); 296 + ksft_test_result(write_fork_read, "write_fork_read\n"); 297 + ksft_test_result(write_clone_read, "write_clone_read\n"); 234 298 235 299 } else { 236 - putstr("# SME support not present\n"); 300 + ksft_print_msg("SME support not present\n"); 237 301 238 - skip_test(default_value); 239 - skip_test(write_read); 240 - skip_test(write_sleep_read); 241 - skip_test(write_fork_read); 242 - skip_test(write_clone_read); 302 + ksft_test_result_skip("default_value\n"); 303 + ksft_test_result_skip("write_read\n"); 304 + ksft_test_result_skip("write_sleep_read\n"); 305 + ksft_test_result_skip("write_fork_read\n"); 306 + ksft_test_result_skip("write_clone_read\n"); 243 307 } 244 308 245 - print_summary(); 246 - 247 - return 0; 309 + ksft_finished(); 248 310 }