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 };
31 
32 #define MAX_FEATS_SZ	128
33 static char feats_string[MAX_FEATS_SZ];
34 
35 static inline char *feats_to_string(unsigned long feats)
36 {
37 	size_t flen = MAX_FEATS_SZ - 1;
38 
39 	feats_string[0] = '\0';
40 
41 	for (int i = 0; i < FMAX_END; i++) {
42 		if (feats & (1UL << i)) {
43 			size_t tlen = strlen(feats_names[i]);
44 
45 			assert(flen > tlen);
46 			flen -= tlen;
47 			strncat(feats_string, feats_names[i], flen);
48 		}
49 	}
50 
51 	return feats_string;
52 }
53 
54 static void unblock_signal(int signum)
55 {
56 	sigset_t sset;
57 
58 	sigemptyset(&sset);
59 	sigaddset(&sset, signum);
60 	sigprocmask(SIG_UNBLOCK, &sset, NULL);
61 }
62 
63 static void default_result(struct tdescr *td, bool force_exit)
64 {
65 	if (td->result == KSFT_SKIP) {
66 		fprintf(stderr, "==>> completed. SKIP.\n");
67 	} else if (td->pass) {
68 		fprintf(stderr, "==>> completed. PASS(1)\n");
69 		td->result = KSFT_PASS;
70 	} else {
71 		fprintf(stdout, "==>> completed. FAIL(0)\n");
72 		td->result = KSFT_FAIL;
73 	}
74 
75 	if (force_exit)
76 		exit(td->result);
77 }
78 
79 /*
80  * The following handle_signal_* helpers are used by main default_handler
81  * and are meant to return true when signal is handled successfully:
82  * when false is returned instead, it means that the signal was somehow
83  * unexpected in that context and it was NOT handled; default_handler will
84  * take care of such unexpected situations.
85  */
86 
87 static bool handle_signal_unsupported(struct tdescr *td,
88 				      siginfo_t *si, void *uc)
89 {
90 	if (feats_ok(td))
91 		return false;
92 
93 	/* Mangling PC to avoid loops on original SIGILL */
94 	((ucontext_t *)uc)->uc_mcontext.pc += 4;
95 
96 	if (!td->initialized) {
97 		fprintf(stderr,
98 			"Got SIG_UNSUPP @test_init. Ignore.\n");
99 	} else {
100 		fprintf(stderr,
101 			"-- RX SIG_UNSUPP on unsupported feat...OK\n");
102 		td->pass = 1;
103 		default_result(current, 1);
104 	}
105 
106 	return true;
107 }
108 
109 static bool handle_signal_trigger(struct tdescr *td,
110 				  siginfo_t *si, void *uc)
111 {
112 	td->triggered = 1;
113 	/* ->run was asserted NON-NULL in test_setup() already */
114 	td->run(td, si, uc);
115 
116 	return true;
117 }
118 
119 static bool handle_signal_ok(struct tdescr *td,
120 			     siginfo_t *si, void *uc)
121 {
122 	/*
123 	 * it's a bug in the test code when this assert fail:
124 	 * if sig_trig was defined, it must have been used before getting here.
125 	 */
126 	assert(!td->sig_trig || td->triggered);
127 	fprintf(stderr,
128 		"SIG_OK -- SP:0x%llX  si_addr@:%p  si_code:%d  token@:%p  offset:%ld\n",
129 		((ucontext_t *)uc)->uc_mcontext.sp,
130 		si->si_addr, si->si_code, td->token, td->token - si->si_addr);
131 	/*
132 	 * fake_sigreturn tests, which have sanity_enabled=1, set, at the very
133 	 * last time, the token field to the SP address used to place the fake
134 	 * sigframe: so token==0 means we never made it to the end,
135 	 * segfaulting well-before, and the test is possibly broken.
136 	 */
137 	if (!td->sanity_disabled && !td->token) {
138 		fprintf(stdout,
139 			"current->token ZEROED...test is probably broken!\n");
140 		abort();
141 	}
142 	/*
143 	 * Trying to narrow down the SEGV to the ones generated by Kernel itself
144 	 * via arm64_notify_segfault(). This is a best-effort check anyway, and
145 	 * the si_code check may need to change if this aspect of the kernel
146 	 * ABI changes.
147 	 */
148 	if (td->sig_ok == SIGSEGV && si->si_code != SEGV_ACCERR) {
149 		fprintf(stdout,
150 			"si_code != SEGV_ACCERR...test is probably broken!\n");
151 		abort();
152 	}
153 	td->pass = 1;
154 	/*
155 	 * Some tests can lead to SEGV loops: in such a case we want to
156 	 * terminate immediately exiting straight away; some others are not
157 	 * supposed to outlive the signal handler code, due to the content of
158 	 * the fake sigframe which caused the signal itself.
159 	 */
160 	default_result(current, 1);
161 
162 	return true;
163 }
164 
165 static bool handle_signal_copyctx(struct tdescr *td,
166 				  siginfo_t *si, void *uc)
167 {
168 	/* Mangling PC to avoid loops on original BRK instr */
169 	((ucontext_t *)uc)->uc_mcontext.pc += 4;
170 	memcpy(td->live_uc, uc, td->live_sz);
171 	ASSERT_GOOD_CONTEXT(td->live_uc);
172 	td->live_uc_valid = 1;
173 	fprintf(stderr,
174 		"GOOD CONTEXT grabbed from sig_copyctx handler\n");
175 
176 	return true;
177 }
178 
179 static void default_handler(int signum, siginfo_t *si, void *uc)
180 {
181 	if (current->sig_unsupp && signum == current->sig_unsupp &&
182 	    handle_signal_unsupported(current, si, uc)) {
183 		fprintf(stderr, "Handled SIG_UNSUPP\n");
184 	} else if (current->sig_trig && signum == current->sig_trig &&
185 		   handle_signal_trigger(current, si, uc)) {
186 		fprintf(stderr, "Handled SIG_TRIG\n");
187 	} else if (current->sig_ok && signum == current->sig_ok &&
188 		   handle_signal_ok(current, si, uc)) {
189 		fprintf(stderr, "Handled SIG_OK\n");
190 	} else if (signum == sig_copyctx && current->live_uc &&
191 		   handle_signal_copyctx(current, si, uc)) {
192 		fprintf(stderr, "Handled SIG_COPYCTX\n");
193 	} else {
194 		if (signum == SIGALRM && current->timeout) {
195 			fprintf(stderr, "-- Timeout !\n");
196 		} else {
197 			fprintf(stderr,
198 				"-- RX UNEXPECTED SIGNAL: %d\n", signum);
199 		}
200 		default_result(current, 1);
201 	}
202 }
203 
204 static int default_setup(struct tdescr *td)
205 {
206 	struct sigaction sa;
207 
208 	sa.sa_sigaction = default_handler;
209 	sa.sa_flags = SA_SIGINFO | SA_RESTART;
210 	sa.sa_flags |= td->sa_flags;
211 	sigemptyset(&sa.sa_mask);
212 	/* uncatchable signals naturally skipped ... */
213 	for (int sig = 1; sig < 32; sig++)
214 		sigaction(sig, &sa, NULL);
215 	/*
216 	 * RT Signals default disposition is Term but they cannot be
217 	 * generated by the Kernel in response to our tests; so just catch
218 	 * them all and report them as UNEXPECTED signals.
219 	 */
220 	for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++)
221 		sigaction(sig, &sa, NULL);
222 
223 	/* just in case...unblock explicitly all we need */
224 	if (td->sig_trig)
225 		unblock_signal(td->sig_trig);
226 	if (td->sig_ok)
227 		unblock_signal(td->sig_ok);
228 	if (td->sig_unsupp)
229 		unblock_signal(td->sig_unsupp);
230 
231 	if (td->timeout) {
232 		unblock_signal(SIGALRM);
233 		alarm(td->timeout);
234 	}
235 	fprintf(stderr, "Registered handlers for all signals.\n");
236 
237 	return 1;
238 }
239 
240 static inline int default_trigger(struct tdescr *td)
241 {
242 	return !raise(td->sig_trig);
243 }
244 
245 int test_init(struct tdescr *td)
246 {
247 	if (td->sig_trig == sig_copyctx) {
248 		fprintf(stdout,
249 			"Signal %d is RESERVED, cannot be used as a trigger. Aborting\n",
250 			sig_copyctx);
251 		return 0;
252 	}
253 	/* just in case */
254 	unblock_signal(sig_copyctx);
255 
256 	td->minsigstksz = getauxval(AT_MINSIGSTKSZ);
257 	if (!td->minsigstksz)
258 		td->minsigstksz = MINSIGSTKSZ;
259 	fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz);
260 
261 	if (td->feats_required || td->feats_incompatible) {
262 		td->feats_supported = 0;
263 		/*
264 		 * Checking for CPU required features using both the
265 		 * auxval and the arm64 MRS Emulation to read sysregs.
266 		 */
267 		if (getauxval(AT_HWCAP) & HWCAP_SSBS)
268 			td->feats_supported |= FEAT_SSBS;
269 		if (getauxval(AT_HWCAP) & HWCAP_SVE)
270 			td->feats_supported |= FEAT_SVE;
271 		if (feats_ok(td)) {
272 			if (td->feats_required & td->feats_supported)
273 				fprintf(stderr,
274 					"Required Features: [%s] supported\n",
275 					feats_to_string(td->feats_required &
276 							td->feats_supported));
277 			if (!(td->feats_incompatible & td->feats_supported))
278 				fprintf(stderr,
279 					"Incompatible Features: [%s] absent\n",
280 					feats_to_string(td->feats_incompatible));
281 		} else {
282 			if ((td->feats_required & td->feats_supported) !=
283 			    td->feats_supported)
284 				fprintf(stderr,
285 					"Required Features: [%s] NOT supported\n",
286 					feats_to_string(td->feats_required &
287 							~td->feats_supported));
288 			if (td->feats_incompatible & td->feats_supported)
289 				fprintf(stderr,
290 					"Incompatible Features: [%s] supported\n",
291 					feats_to_string(td->feats_incompatible &
292 							~td->feats_supported));
293 
294 
295 			td->result = KSFT_SKIP;
296 			return 0;
297 		}
298 	}
299 
300 	/* Perform test specific additional initialization */
301 	if (td->init && !td->init(td)) {
302 		fprintf(stderr, "FAILED Testcase initialization.\n");
303 		return 0;
304 	}
305 	td->initialized = 1;
306 	fprintf(stderr, "Testcase initialized.\n");
307 
308 	return 1;
309 }
310 
311 int test_setup(struct tdescr *td)
312 {
313 	/* assert core invariants symptom of a rotten testcase */
314 	assert(current);
315 	assert(td);
316 	assert(td->name);
317 	assert(td->run);
318 
319 	/* Default result is FAIL if test setup fails */
320 	td->result = KSFT_FAIL;
321 	if (td->setup)
322 		return td->setup(td);
323 	else
324 		return default_setup(td);
325 }
326 
327 int test_run(struct tdescr *td)
328 {
329 	if (td->trigger)
330 		return td->trigger(td);
331 	else if (td->sig_trig)
332 		return default_trigger(td);
333 	else
334 		return td->run(td, NULL, NULL);
335 }
336 
337 void test_result(struct tdescr *td)
338 {
339 	if (td->initialized && td->result != KSFT_SKIP && td->check_result)
340 		td->check_result(td);
341 	default_result(td, 0);
342 }
343 
344 void test_cleanup(struct tdescr *td)
345 {
346 	if (td->cleanup)
347 		td->cleanup(td);
348 }
349