1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019 ARM Limited */
11 #include <linux/auxvec.h>
14 #include <asm/unistd.h>
16 #include <kselftest.h>
18 #include "test_signals.h"
19 #include "test_signals_utils.h"
20 #include "testcases/testcases.h"
23 extern struct tdescr *current;
25 static int sig_copyctx = SIGTRAP;
27 static char const *const feats_names[FMAX_END] = {
32 #define MAX_FEATS_SZ 128
33 static char feats_string[MAX_FEATS_SZ];
35 static inline char *feats_to_string(unsigned long feats)
37 size_t flen = MAX_FEATS_SZ - 1;
39 feats_string[0] = '\0';
41 for (int i = 0; i < FMAX_END; i++) {
42 if (feats & (1UL << i)) {
43 size_t tlen = strlen(feats_names[i]);
47 strncat(feats_string, feats_names[i], flen);
54 static void unblock_signal(int signum)
59 sigaddset(&sset, signum);
60 sigprocmask(SIG_UNBLOCK, &sset, NULL);
63 static void default_result(struct tdescr *td, bool force_exit)
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;
71 fprintf(stdout, "==>> completed. FAIL(0)\n");
72 td->result = KSFT_FAIL;
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.
87 static bool handle_signal_unsupported(struct tdescr *td,
88 siginfo_t *si, void *uc)
93 /* Mangling PC to avoid loops on original SIGILL */
94 ((ucontext_t *)uc)->uc_mcontext.pc += 4;
96 if (!td->initialized) {
98 "Got SIG_UNSUPP @test_init. Ignore.\n");
101 "-- RX SIG_UNSUPP on unsupported feat...OK\n");
103 default_result(current, 1);
109 static bool handle_signal_trigger(struct tdescr *td,
110 siginfo_t *si, void *uc)
113 /* ->run was asserted NON-NULL in test_setup() already */
119 static bool handle_signal_ok(struct tdescr *td,
120 siginfo_t *si, void *uc)
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.
126 assert(!td->sig_trig || td->triggered);
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);
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.
137 if (!td->sanity_disabled && !td->token) {
139 "current->token ZEROED...test is probably broken!\n");
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
148 if (td->sig_ok == SIGSEGV && si->si_code != SEGV_ACCERR) {
150 "si_code != SEGV_ACCERR...test is probably broken!\n");
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.
160 default_result(current, 1);
165 static bool handle_signal_copyctx(struct tdescr *td,
166 siginfo_t *si, void *uc)
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;
174 "GOOD CONTEXT grabbed from sig_copyctx handler\n");
179 static void default_handler(int signum, siginfo_t *si, void *uc)
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");
194 if (signum == SIGALRM && current->timeout) {
195 fprintf(stderr, "-- Timeout !\n");
198 "-- RX UNEXPECTED SIGNAL: %d\n", signum);
200 default_result(current, 1);
204 static int default_setup(struct tdescr *td)
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);
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.
220 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++)
221 sigaction(sig, &sa, NULL);
223 /* just in case...unblock explicitly all we need */
225 unblock_signal(td->sig_trig);
227 unblock_signal(td->sig_ok);
229 unblock_signal(td->sig_unsupp);
232 unblock_signal(SIGALRM);
235 fprintf(stderr, "Registered handlers for all signals.\n");
240 static inline int default_trigger(struct tdescr *td)
242 return !raise(td->sig_trig);
245 int test_init(struct tdescr *td)
247 if (td->sig_trig == sig_copyctx) {
249 "Signal %d is RESERVED, cannot be used as a trigger. Aborting\n",
254 unblock_signal(sig_copyctx);
256 td->minsigstksz = getauxval(AT_MINSIGSTKSZ);
257 if (!td->minsigstksz)
258 td->minsigstksz = MINSIGSTKSZ;
259 fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz);
261 if (td->feats_required || td->feats_incompatible) {
262 td->feats_supported = 0;
264 * Checking for CPU required features using both the
265 * auxval and the arm64 MRS Emulation to read sysregs.
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;
272 if (td->feats_required & td->feats_supported)
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))
279 "Incompatible Features: [%s] absent\n",
280 feats_to_string(td->feats_incompatible));
282 if ((td->feats_required & td->feats_supported) !=
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)
290 "Incompatible Features: [%s] supported\n",
291 feats_to_string(td->feats_incompatible &
292 ~td->feats_supported));
295 td->result = KSFT_SKIP;
300 /* Perform test specific additional initialization */
301 if (td->init && !td->init(td)) {
302 fprintf(stderr, "FAILED Testcase initialization.\n");
306 fprintf(stderr, "Testcase initialized.\n");
311 int test_setup(struct tdescr *td)
313 /* assert core invariants symptom of a rotten testcase */
319 /* Default result is FAIL if test setup fails */
320 td->result = KSFT_FAIL;
322 return td->setup(td);
324 return default_setup(td);
327 int test_run(struct tdescr *td)
330 return td->trigger(td);
331 else if (td->sig_trig)
332 return default_trigger(td);
334 return td->run(td, NULL, NULL);
337 void test_result(struct tdescr *td)
339 if (td->initialized && td->result != KSFT_SKIP && td->check_result)
340 td->check_result(td);
341 default_result(td, 0);
344 void test_cleanup(struct tdescr *td)