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
feats_to_string(unsigned long feats)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
unblock_signal(int signum)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
default_result(struct tdescr * td,bool force_exit)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
handle_signal_unsupported(struct tdescr * td,siginfo_t * si,void * uc)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
handle_signal_trigger(struct tdescr * td,siginfo_t * si,void * uc)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
handle_signal_ok(struct tdescr * td,siginfo_t * si,void * uc)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
handle_signal_copyctx(struct tdescr * td,siginfo_t * si,void * uc_in)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
default_handler(int signum,siginfo_t * si,void * uc)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\n", signum);
253 }
254 default_result(current, 1);
255 }
256 }
257
default_setup(struct tdescr * td)258 static int default_setup(struct tdescr *td)
259 {
260 struct sigaction sa;
261
262 sa.sa_sigaction = default_handler;
263 sa.sa_flags = SA_SIGINFO | SA_RESTART;
264 sa.sa_flags |= td->sa_flags;
265 sigemptyset(&sa.sa_mask);
266 /* uncatchable signals naturally skipped ... */
267 for (int sig = 1; sig < 32; sig++)
268 sigaction(sig, &sa, NULL);
269 /*
270 * RT Signals default disposition is Term but they cannot be
271 * generated by the Kernel in response to our tests; so just catch
272 * them all and report them as UNEXPECTED signals.
273 */
274 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++)
275 sigaction(sig, &sa, NULL);
276
277 /* just in case...unblock explicitly all we need */
278 if (td->sig_trig)
279 unblock_signal(td->sig_trig);
280 if (td->sig_ok)
281 unblock_signal(td->sig_ok);
282 if (td->sig_unsupp)
283 unblock_signal(td->sig_unsupp);
284
285 if (td->timeout) {
286 unblock_signal(SIGALRM);
287 alarm(td->timeout);
288 }
289 fprintf(stderr, "Registered handlers for all signals.\n");
290
291 return 1;
292 }
293
default_trigger(struct tdescr * td)294 static inline int default_trigger(struct tdescr *td)
295 {
296 return !raise(td->sig_trig);
297 }
298
test_init(struct tdescr * td)299 int test_init(struct tdescr *td)
300 {
301 if (td->sig_trig == sig_copyctx) {
302 fprintf(stdout,
303 "Signal %d is RESERVED, cannot be used as a trigger. Aborting\n",
304 sig_copyctx);
305 return 0;
306 }
307 /* just in case */
308 unblock_signal(sig_copyctx);
309
310 td->minsigstksz = getauxval(AT_MINSIGSTKSZ);
311 if (!td->minsigstksz)
312 td->minsigstksz = MINSIGSTKSZ;
313 fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz);
314
315 if (td->feats_required || td->feats_incompatible) {
316 td->feats_supported = 0;
317 /*
318 * Checking for CPU required features using both the
319 * auxval and the arm64 MRS Emulation to read sysregs.
320 */
321 if (getauxval(AT_HWCAP) & HWCAP_SSBS)
322 td->feats_supported |= FEAT_SSBS;
323 if (getauxval(AT_HWCAP) & HWCAP_SVE)
324 td->feats_supported |= FEAT_SVE;
325 if (getauxval(AT_HWCAP2) & HWCAP2_SME)
326 td->feats_supported |= FEAT_SME;
327 if (getauxval(AT_HWCAP2) & HWCAP2_SME_FA64)
328 td->feats_supported |= FEAT_SME_FA64;
329 if (getauxval(AT_HWCAP2) & HWCAP2_SME2)
330 td->feats_supported |= FEAT_SME2;
331 if (feats_ok(td)) {
332 if (td->feats_required & td->feats_supported)
333 fprintf(stderr,
334 "Required Features: [%s] supported\n",
335 feats_to_string(td->feats_required &
336 td->feats_supported));
337 if (!(td->feats_incompatible & td->feats_supported))
338 fprintf(stderr,
339 "Incompatible Features: [%s] absent\n",
340 feats_to_string(td->feats_incompatible));
341 } else {
342 if ((td->feats_required & td->feats_supported) !=
343 td->feats_supported)
344 fprintf(stderr,
345 "Required Features: [%s] NOT supported\n",
346 feats_to_string(td->feats_required &
347 ~td->feats_supported));
348 if (td->feats_incompatible & td->feats_supported)
349 fprintf(stderr,
350 "Incompatible Features: [%s] supported\n",
351 feats_to_string(td->feats_incompatible &
352 ~td->feats_supported));
353
354
355 td->result = KSFT_SKIP;
356 return 0;
357 }
358 }
359
360 /* Perform test specific additional initialization */
361 if (td->init && !td->init(td)) {
362 fprintf(stderr, "FAILED Testcase initialization.\n");
363 return 0;
364 }
365 td->initialized = 1;
366 fprintf(stderr, "Testcase initialized.\n");
367
368 return 1;
369 }
370
test_setup(struct tdescr * td)371 int test_setup(struct tdescr *td)
372 {
373 /* assert core invariants symptom of a rotten testcase */
374 assert(current);
375 assert(td);
376 assert(td->name);
377 assert(td->run);
378
379 /* Default result is FAIL if test setup fails */
380 td->result = KSFT_FAIL;
381 if (td->setup)
382 return td->setup(td);
383 else
384 return default_setup(td);
385 }
386
test_run(struct tdescr * td)387 int test_run(struct tdescr *td)
388 {
389 if (td->trigger)
390 return td->trigger(td);
391 else if (td->sig_trig)
392 return default_trigger(td);
393 else
394 return td->run(td, NULL, NULL);
395 }
396
test_result(struct tdescr * td)397 void test_result(struct tdescr *td)
398 {
399 if (td->initialized && td->result != KSFT_SKIP && td->check_result)
400 td->check_result(td);
401 default_result(td, 0);
402 }
403
test_cleanup(struct tdescr * td)404 void test_cleanup(struct tdescr *td)
405 {
406 if (td->cleanup)
407 td->cleanup(td);
408 }
409