xref: /openbmc/linux/tools/testing/selftests/arm64/signal/test_signals_utils.c (revision 619cec00857f21dbc6db5ef9e0b9c613479f3745)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019 ARM Limited */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <signal.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <assert.h>
10 #include <sys/auxv.h>
11 #include <linux/auxvec.h>
12 #include <ucontext.h>
13 
14 #include <asm/unistd.h>
15 
16 #include <kselftest.h>
17 
18 #include "test_signals.h"
19 #include "test_signals_utils.h"
20 #include "testcases/testcases.h"
21 
22 
23 extern struct tdescr *current;
24 
25 static int sig_copyctx = SIGTRAP;
26 
27 static char const *const feats_names[FMAX_END] = {
28 	" SSBS ",
29 	" SVE ",
30 	" SME ",
31 	" FA64 ",
32 	" SME2 ",
33 };
34 
35 #define MAX_FEATS_SZ	128
36 static char feats_string[MAX_FEATS_SZ];
37 
38 static inline char *feats_to_string(unsigned long feats)
39 {
40 	size_t flen = MAX_FEATS_SZ - 1;
41 
42 	feats_string[0] = '\0';
43 
44 	for (int i = 0; i < FMAX_END; i++) {
45 		if (feats & (1UL << i)) {
46 			size_t tlen = strlen(feats_names[i]);
47 
48 			assert(flen > tlen);
49 			flen -= tlen;
50 			strncat(feats_string, feats_names[i], flen);
51 		}
52 	}
53 
54 	return feats_string;
55 }
56 
57 static void unblock_signal(int signum)
58 {
59 	sigset_t sset;
60 
61 	sigemptyset(&sset);
62 	sigaddset(&sset, signum);
63 	sigprocmask(SIG_UNBLOCK, &sset, NULL);
64 }
65 
66 static void default_result(struct tdescr *td, bool force_exit)
67 {
68 	if (td->result == KSFT_SKIP) {
69 		fprintf(stderr, "==>> completed. SKIP.\n");
70 	} else if (td->pass) {
71 		fprintf(stderr, "==>> completed. PASS(1)\n");
72 		td->result = KSFT_PASS;
73 	} else {
74 		fprintf(stdout, "==>> completed. FAIL(0)\n");
75 		td->result = KSFT_FAIL;
76 	}
77 
78 	if (force_exit)
79 		exit(td->result);
80 }
81 
82 /*
83  * The following handle_signal_* helpers are used by main default_handler
84  * and are meant to return true when signal is handled successfully:
85  * when false is returned instead, it means that the signal was somehow
86  * unexpected in that context and it was NOT handled; default_handler will
87  * take care of such unexpected situations.
88  */
89 
90 static bool handle_signal_unsupported(struct tdescr *td,
91 				      siginfo_t *si, void *uc)
92 {
93 	if (feats_ok(td))
94 		return false;
95 
96 	/* Mangling PC to avoid loops on original SIGILL */
97 	((ucontext_t *)uc)->uc_mcontext.pc += 4;
98 
99 	if (!td->initialized) {
100 		fprintf(stderr,
101 			"Got SIG_UNSUPP @test_init. Ignore.\n");
102 	} else {
103 		fprintf(stderr,
104 			"-- RX SIG_UNSUPP on unsupported feat...OK\n");
105 		td->pass = 1;
106 		default_result(current, 1);
107 	}
108 
109 	return true;
110 }
111 
112 static bool handle_signal_trigger(struct tdescr *td,
113 				  siginfo_t *si, void *uc)
114 {
115 	td->triggered = 1;
116 	/* ->run was asserted NON-NULL in test_setup() already */
117 	td->run(td, si, uc);
118 
119 	return true;
120 }
121 
122 static bool handle_signal_ok(struct tdescr *td,
123 			     siginfo_t *si, void *uc)
124 {
125 	/*
126 	 * it's a bug in the test code when this assert fail:
127 	 * if sig_trig was defined, it must have been used before getting here.
128 	 */
129 	assert(!td->sig_trig || td->triggered);
130 	fprintf(stderr,
131 		"SIG_OK -- SP:0x%llX  si_addr@:%p  si_code:%d  token@:%p  offset:%ld\n",
132 		((ucontext_t *)uc)->uc_mcontext.sp,
133 		si->si_addr, si->si_code, td->token, td->token - si->si_addr);
134 	/*
135 	 * fake_sigreturn tests, which have sanity_enabled=1, set, at the very
136 	 * last time, the token field to the SP address used to place the fake
137 	 * sigframe: so token==0 means we never made it to the end,
138 	 * segfaulting well-before, and the test is possibly broken.
139 	 */
140 	if (!td->sanity_disabled && !td->token) {
141 		fprintf(stdout,
142 			"current->token ZEROED...test is probably broken!\n");
143 		abort();
144 	}
145 	/*
146 	 * Trying to narrow down the SEGV to the ones generated by Kernel itself
147 	 * via arm64_notify_segfault(). This is a best-effort check anyway, and
148 	 * the si_code check may need to change if this aspect of the kernel
149 	 * ABI changes.
150 	 */
151 	if (td->sig_ok == SIGSEGV && si->si_code != SEGV_ACCERR) {
152 		fprintf(stdout,
153 			"si_code != SEGV_ACCERR...test is probably broken!\n");
154 		abort();
155 	}
156 	td->pass = 1;
157 	/*
158 	 * Some tests can lead to SEGV loops: in such a case we want to
159 	 * terminate immediately exiting straight away; some others are not
160 	 * supposed to outlive the signal handler code, due to the content of
161 	 * the fake sigframe which caused the signal itself.
162 	 */
163 	default_result(current, 1);
164 
165 	return true;
166 }
167 
168 static bool handle_signal_copyctx(struct tdescr *td,
169 				  siginfo_t *si, void *uc_in)
170 {
171 	ucontext_t *uc = uc_in;
172 	struct _aarch64_ctx *head;
173 	struct extra_context *extra, *copied_extra;
174 	size_t offset = 0;
175 	size_t to_copy;
176 
177 	ASSERT_GOOD_CONTEXT(uc);
178 
179 	/* Mangling PC to avoid loops on original BRK instr */
180 	uc->uc_mcontext.pc += 4;
181 
182 	/*
183 	 * Check for an preserve any extra data too with fixups.
184 	 */
185 	head = (struct _aarch64_ctx *)uc->uc_mcontext.__reserved;
186 	head = get_header(head, EXTRA_MAGIC, td->live_sz, &offset);
187 	if (head) {
188 		extra = (struct extra_context *)head;
189 
190 		/*
191 		 * The extra buffer must be immediately after the
192 		 * extra_context and a 16 byte terminator. Include it
193 		 * in the copy, this was previously validated in
194 		 * ASSERT_GOOD_CONTEXT().
195 		 */
196 		to_copy = offset + sizeof(struct extra_context) + 16 +
197 			extra->size;
198 		copied_extra = (struct extra_context *)&(td->live_uc->uc_mcontext.__reserved[offset]);
199 	} else {
200 		copied_extra = NULL;
201 		to_copy = sizeof(ucontext_t);
202 	}
203 
204 	if (to_copy > td->live_sz) {
205 		fprintf(stderr,
206 			"Not enough space to grab context, %lu/%lu bytes\n",
207 			td->live_sz, to_copy);
208 		return false;
209 	}
210 
211 	memcpy(td->live_uc, uc, to_copy);
212 
213 	/*
214 	 * If there was any EXTRA_CONTEXT fix up the size to be the
215 	 * struct extra_context and the following terminator record,
216 	 * this means that the rest of the code does not need to have
217 	 * special handling for the record and we don't need to fix up
218 	 * datap for the new location.
219 	 */
220 	if (copied_extra)
221 		copied_extra->head.size = sizeof(*copied_extra) + 16;
222 
223 	td->live_uc_valid = 1;
224 	fprintf(stderr,
225 		"%lu byte GOOD CONTEXT grabbed from sig_copyctx handler\n",
226 		to_copy);
227 
228 	return true;
229 }
230 
231 static void default_handler(int signum, siginfo_t *si, void *uc)
232 {
233 	if (current->sig_unsupp && signum == current->sig_unsupp &&
234 	    handle_signal_unsupported(current, si, uc)) {
235 		fprintf(stderr, "Handled SIG_UNSUPP\n");
236 	} else if (current->sig_trig && signum == current->sig_trig &&
237 		   handle_signal_trigger(current, si, uc)) {
238 		fprintf(stderr, "Handled SIG_TRIG\n");
239 	} else if (current->sig_ok && signum == current->sig_ok &&
240 		   handle_signal_ok(current, si, uc)) {
241 		fprintf(stderr, "Handled SIG_OK\n");
242 	} else if (signum == sig_copyctx && current->live_uc &&
243 		   handle_signal_copyctx(current, si, uc)) {
244 		fprintf(stderr, "Handled SIG_COPYCTX\n");
245 	} else {
246 		if (signum == SIGALRM && current->timeout) {
247 			fprintf(stderr, "-- Timeout !\n");
248 		} else {
249 			fprintf(stderr,
250 				"-- RX UNEXPECTED SIGNAL: %d\n", signum);
251 		}
252 		default_result(current, 1);
253 	}
254 }
255 
256 static int default_setup(struct tdescr *td)
257 {
258 	struct sigaction sa;
259 
260 	sa.sa_sigaction = default_handler;
261 	sa.sa_flags = SA_SIGINFO | SA_RESTART;
262 	sa.sa_flags |= td->sa_flags;
263 	sigemptyset(&sa.sa_mask);
264 	/* uncatchable signals naturally skipped ... */
265 	for (int sig = 1; sig < 32; sig++)
266 		sigaction(sig, &sa, NULL);
267 	/*
268 	 * RT Signals default disposition is Term but they cannot be
269 	 * generated by the Kernel in response to our tests; so just catch
270 	 * them all and report them as UNEXPECTED signals.
271 	 */
272 	for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++)
273 		sigaction(sig, &sa, NULL);
274 
275 	/* just in case...unblock explicitly all we need */
276 	if (td->sig_trig)
277 		unblock_signal(td->sig_trig);
278 	if (td->sig_ok)
279 		unblock_signal(td->sig_ok);
280 	if (td->sig_unsupp)
281 		unblock_signal(td->sig_unsupp);
282 
283 	if (td->timeout) {
284 		unblock_signal(SIGALRM);
285 		alarm(td->timeout);
286 	}
287 	fprintf(stderr, "Registered handlers for all signals.\n");
288 
289 	return 1;
290 }
291 
292 static inline int default_trigger(struct tdescr *td)
293 {
294 	return !raise(td->sig_trig);
295 }
296 
297 int test_init(struct tdescr *td)
298 {
299 	if (td->sig_trig == sig_copyctx) {
300 		fprintf(stdout,
301 			"Signal %d is RESERVED, cannot be used as a trigger. Aborting\n",
302 			sig_copyctx);
303 		return 0;
304 	}
305 	/* just in case */
306 	unblock_signal(sig_copyctx);
307 
308 	td->minsigstksz = getauxval(AT_MINSIGSTKSZ);
309 	if (!td->minsigstksz)
310 		td->minsigstksz = MINSIGSTKSZ;
311 	fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz);
312 
313 	if (td->feats_required || td->feats_incompatible) {
314 		td->feats_supported = 0;
315 		/*
316 		 * Checking for CPU required features using both the
317 		 * auxval and the arm64 MRS Emulation to read sysregs.
318 		 */
319 		if (getauxval(AT_HWCAP) & HWCAP_SSBS)
320 			td->feats_supported |= FEAT_SSBS;
321 		if (getauxval(AT_HWCAP) & HWCAP_SVE)
322 			td->feats_supported |= FEAT_SVE;
323 		if (getauxval(AT_HWCAP2) & HWCAP2_SME)
324 			td->feats_supported |= FEAT_SME;
325 		if (getauxval(AT_HWCAP2) & HWCAP2_SME_FA64)
326 			td->feats_supported |= FEAT_SME_FA64;
327 		if (getauxval(AT_HWCAP2) & HWCAP2_SME2)
328 			td->feats_supported |= FEAT_SME2;
329 		if (feats_ok(td)) {
330 			if (td->feats_required & td->feats_supported)
331 				fprintf(stderr,
332 					"Required Features: [%s] supported\n",
333 					feats_to_string(td->feats_required &
334 							td->feats_supported));
335 			if (!(td->feats_incompatible & td->feats_supported))
336 				fprintf(stderr,
337 					"Incompatible Features: [%s] absent\n",
338 					feats_to_string(td->feats_incompatible));
339 		} else {
340 			if ((td->feats_required & td->feats_supported) !=
341 			    td->feats_supported)
342 				fprintf(stderr,
343 					"Required Features: [%s] NOT supported\n",
344 					feats_to_string(td->feats_required &
345 							~td->feats_supported));
346 			if (td->feats_incompatible & td->feats_supported)
347 				fprintf(stderr,
348 					"Incompatible Features: [%s] supported\n",
349 					feats_to_string(td->feats_incompatible &
350 							~td->feats_supported));
351 
352 
353 			td->result = KSFT_SKIP;
354 			return 0;
355 		}
356 	}
357 
358 	/* Perform test specific additional initialization */
359 	if (td->init && !td->init(td)) {
360 		fprintf(stderr, "FAILED Testcase initialization.\n");
361 		return 0;
362 	}
363 	td->initialized = 1;
364 	fprintf(stderr, "Testcase initialized.\n");
365 
366 	return 1;
367 }
368 
369 int test_setup(struct tdescr *td)
370 {
371 	/* assert core invariants symptom of a rotten testcase */
372 	assert(current);
373 	assert(td);
374 	assert(td->name);
375 	assert(td->run);
376 
377 	/* Default result is FAIL if test setup fails */
378 	td->result = KSFT_FAIL;
379 	if (td->setup)
380 		return td->setup(td);
381 	else
382 		return default_setup(td);
383 }
384 
385 int test_run(struct tdescr *td)
386 {
387 	if (td->trigger)
388 		return td->trigger(td);
389 	else if (td->sig_trig)
390 		return default_trigger(td);
391 	else
392 		return td->run(td, NULL, NULL);
393 }
394 
395 void test_result(struct tdescr *td)
396 {
397 	if (td->initialized && td->result != KSFT_SKIP && td->check_result)
398 		td->check_result(td);
399 	default_result(td, 0);
400 }
401 
402 void test_cleanup(struct tdescr *td)
403 {
404 	if (td->cleanup)
405 		td->cleanup(td);
406 }
407