xref: /linux/tools/testing/selftests/arm64/signal/test_signals_utils.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
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 = __builtin_offsetof(ucontext_t,
197 					     uc_mcontext.__reserved);
198 		to_copy += offset + sizeof(struct extra_context) + 16;
199 		to_copy += extra->size;
200 		copied_extra = (struct extra_context *)&(td->live_uc->uc_mcontext.__reserved[offset]);
201 	} else {
202 		copied_extra = NULL;
203 		to_copy = sizeof(ucontext_t);
204 	}
205 
206 	if (to_copy > td->live_sz) {
207 		fprintf(stderr,
208 			"Not enough space to grab context, %lu/%lu bytes\n",
209 			td->live_sz, to_copy);
210 		return false;
211 	}
212 
213 	memcpy(td->live_uc, uc, to_copy);
214 
215 	/*
216 	 * If there was any EXTRA_CONTEXT fix up the size to be the
217 	 * struct extra_context and the following terminator record,
218 	 * this means that the rest of the code does not need to have
219 	 * special handling for the record and we don't need to fix up
220 	 * datap for the new location.
221 	 */
222 	if (copied_extra)
223 		copied_extra->head.size = sizeof(*copied_extra) + 16;
224 
225 	td->live_uc_valid = 1;
226 	fprintf(stderr,
227 		"%lu byte GOOD CONTEXT grabbed from sig_copyctx handler\n",
228 		to_copy);
229 
230 	return true;
231 }
232 
233 static void default_handler(int signum, siginfo_t *si, void *uc)
234 {
235 	if (current->sig_unsupp && signum == current->sig_unsupp &&
236 	    handle_signal_unsupported(current, si, uc)) {
237 		fprintf(stderr, "Handled SIG_UNSUPP\n");
238 	} else if (current->sig_trig && signum == current->sig_trig &&
239 		   handle_signal_trigger(current, si, uc)) {
240 		fprintf(stderr, "Handled SIG_TRIG\n");
241 	} else if (current->sig_ok && signum == current->sig_ok &&
242 		   handle_signal_ok(current, si, uc)) {
243 		fprintf(stderr, "Handled SIG_OK\n");
244 	} else if (signum == sig_copyctx && current->live_uc &&
245 		   handle_signal_copyctx(current, si, uc)) {
246 		fprintf(stderr, "Handled SIG_COPYCTX\n");
247 	} else {
248 		if (signum == SIGALRM && current->timeout) {
249 			fprintf(stderr, "-- Timeout !\n");
250 		} else {
251 			fprintf(stderr,
252 				"-- RX UNEXPECTED SIGNAL: %d code %d address %p\n",
253 				signum, si->si_code, si->si_addr);
254 		}
255 		default_result(current, 1);
256 	}
257 }
258 
259 static int default_setup(struct tdescr *td)
260 {
261 	struct sigaction sa;
262 
263 	sa.sa_sigaction = default_handler;
264 	sa.sa_flags = SA_SIGINFO | SA_RESTART;
265 	sa.sa_flags |= td->sa_flags;
266 	sigemptyset(&sa.sa_mask);
267 	/* uncatchable signals naturally skipped ... */
268 	for (int sig = 1; sig < 32; sig++)
269 		sigaction(sig, &sa, NULL);
270 	/*
271 	 * RT Signals default disposition is Term but they cannot be
272 	 * generated by the Kernel in response to our tests; so just catch
273 	 * them all and report them as UNEXPECTED signals.
274 	 */
275 	for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++)
276 		sigaction(sig, &sa, NULL);
277 
278 	/* just in case...unblock explicitly all we need */
279 	if (td->sig_trig)
280 		unblock_signal(td->sig_trig);
281 	if (td->sig_ok)
282 		unblock_signal(td->sig_ok);
283 	if (td->sig_unsupp)
284 		unblock_signal(td->sig_unsupp);
285 
286 	if (td->timeout) {
287 		unblock_signal(SIGALRM);
288 		alarm(td->timeout);
289 	}
290 	fprintf(stderr, "Registered handlers for all signals.\n");
291 
292 	return 1;
293 }
294 
295 static inline int default_trigger(struct tdescr *td)
296 {
297 	return !raise(td->sig_trig);
298 }
299 
300 int test_init(struct tdescr *td)
301 {
302 	if (td->sig_trig == sig_copyctx) {
303 		fprintf(stdout,
304 			"Signal %d is RESERVED, cannot be used as a trigger. Aborting\n",
305 			sig_copyctx);
306 		return 0;
307 	}
308 	/* just in case */
309 	unblock_signal(sig_copyctx);
310 
311 	td->minsigstksz = getauxval(AT_MINSIGSTKSZ);
312 	if (!td->minsigstksz)
313 		td->minsigstksz = MINSIGSTKSZ;
314 	fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz);
315 
316 	if (td->feats_required || td->feats_incompatible) {
317 		td->feats_supported = 0;
318 		/*
319 		 * Checking for CPU required features using both the
320 		 * auxval and the arm64 MRS Emulation to read sysregs.
321 		 */
322 		if (getauxval(AT_HWCAP) & HWCAP_SSBS)
323 			td->feats_supported |= FEAT_SSBS;
324 		if (getauxval(AT_HWCAP) & HWCAP_SVE)
325 			td->feats_supported |= FEAT_SVE;
326 		if (getauxval(AT_HWCAP2) & HWCAP2_SME)
327 			td->feats_supported |= FEAT_SME;
328 		if (getauxval(AT_HWCAP2) & HWCAP2_SME_FA64)
329 			td->feats_supported |= FEAT_SME_FA64;
330 		if (getauxval(AT_HWCAP2) & HWCAP2_SME2)
331 			td->feats_supported |= FEAT_SME2;
332 		if (feats_ok(td)) {
333 			if (td->feats_required & td->feats_supported)
334 				fprintf(stderr,
335 					"Required Features: [%s] supported\n",
336 					feats_to_string(td->feats_required &
337 							td->feats_supported));
338 			if (!(td->feats_incompatible & td->feats_supported))
339 				fprintf(stderr,
340 					"Incompatible Features: [%s] absent\n",
341 					feats_to_string(td->feats_incompatible));
342 		} else {
343 			if ((td->feats_required & td->feats_supported) !=
344 			    td->feats_supported)
345 				fprintf(stderr,
346 					"Required Features: [%s] NOT supported\n",
347 					feats_to_string(td->feats_required &
348 							~td->feats_supported));
349 			if (td->feats_incompatible & td->feats_supported)
350 				fprintf(stderr,
351 					"Incompatible Features: [%s] supported\n",
352 					feats_to_string(td->feats_incompatible &
353 							~td->feats_supported));
354 
355 
356 			td->result = KSFT_SKIP;
357 			return 0;
358 		}
359 	}
360 
361 	/* Perform test specific additional initialization */
362 	if (td->init && !td->init(td)) {
363 		fprintf(stderr, "FAILED Testcase initialization.\n");
364 		return 0;
365 	}
366 	td->initialized = 1;
367 	fprintf(stderr, "Testcase initialized.\n");
368 
369 	return 1;
370 }
371 
372 int test_setup(struct tdescr *td)
373 {
374 	/* assert core invariants symptom of a rotten testcase */
375 	assert(current);
376 	assert(td);
377 	assert(td->name);
378 	assert(td->run);
379 
380 	/* Default result is FAIL if test setup fails */
381 	td->result = KSFT_FAIL;
382 	if (td->setup)
383 		return td->setup(td);
384 	else
385 		return default_setup(td);
386 }
387 
388 int test_run(struct tdescr *td)
389 {
390 	if (td->trigger)
391 		return td->trigger(td);
392 	else if (td->sig_trig)
393 		return default_trigger(td);
394 	else
395 		return td->run(td, NULL, NULL);
396 }
397 
398 void test_result(struct tdescr *td)
399 {
400 	if (td->initialized && td->result != KSFT_SKIP && td->check_result)
401 		td->check_result(td);
402 	default_result(td, 0);
403 }
404 
405 void test_cleanup(struct tdescr *td)
406 {
407 	if (td->cleanup)
408 		td->cleanup(td);
409 }
410