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 for (int i = 0; i < FMAX_END; i++) {
40 if (feats & (1UL << i)) {
41 size_t tlen = strlen(feats_names[i]);
45 strncat(feats_string, feats_names[i], flen);
52 static void unblock_signal(int signum)
57 sigaddset(&sset, signum);
58 sigprocmask(SIG_UNBLOCK, &sset, NULL);
61 static void default_result(struct tdescr *td, bool force_exit)
63 if (td->result == KSFT_SKIP) {
64 fprintf(stderr, "==>> completed. SKIP.\n");
65 } else if (td->pass) {
66 fprintf(stderr, "==>> completed. PASS(1)\n");
67 td->result = KSFT_PASS;
69 fprintf(stdout, "==>> completed. FAIL(0)\n");
70 td->result = KSFT_FAIL;
78 * The following handle_signal_* helpers are used by main default_handler
79 * and are meant to return true when signal is handled successfully:
80 * when false is returned instead, it means that the signal was somehow
81 * unexpected in that context and it was NOT handled; default_handler will
82 * take care of such unexpected situations.
85 static bool handle_signal_unsupported(struct tdescr *td,
86 siginfo_t *si, void *uc)
91 /* Mangling PC to avoid loops on original SIGILL */
92 ((ucontext_t *)uc)->uc_mcontext.pc += 4;
94 if (!td->initialized) {
96 "Got SIG_UNSUPP @test_init. Ignore.\n");
99 "-- RX SIG_UNSUPP on unsupported feat...OK\n");
101 default_result(current, 1);
107 static bool handle_signal_trigger(struct tdescr *td,
108 siginfo_t *si, void *uc)
111 /* ->run was asserted NON-NULL in test_setup() already */
117 static bool handle_signal_ok(struct tdescr *td,
118 siginfo_t *si, void *uc)
121 * it's a bug in the test code when this assert fail:
122 * if sig_trig was defined, it must have been used before getting here.
124 assert(!td->sig_trig || td->triggered);
126 "SIG_OK -- SP:0x%llX si_addr@:%p si_code:%d token@:%p offset:%ld\n",
127 ((ucontext_t *)uc)->uc_mcontext.sp,
128 si->si_addr, si->si_code, td->token, td->token - si->si_addr);
130 * fake_sigreturn tests, which have sanity_enabled=1, set, at the very
131 * last time, the token field to the SP address used to place the fake
132 * sigframe: so token==0 means we never made it to the end,
133 * segfaulting well-before, and the test is possibly broken.
135 if (!td->sanity_disabled && !td->token) {
137 "current->token ZEROED...test is probably broken!\n");
141 * Trying to narrow down the SEGV to the ones generated by Kernel itself
142 * via arm64_notify_segfault(). This is a best-effort check anyway, and
143 * the si_code check may need to change if this aspect of the kernel
146 if (td->sig_ok == SIGSEGV && si->si_code != SEGV_ACCERR) {
148 "si_code != SEGV_ACCERR...test is probably broken!\n");
153 * Some tests can lead to SEGV loops: in such a case we want to
154 * terminate immediately exiting straight away; some others are not
155 * supposed to outlive the signal handler code, due to the content of
156 * the fake sigframe which caused the signal itself.
158 default_result(current, 1);
163 static bool handle_signal_copyctx(struct tdescr *td,
164 siginfo_t *si, void *uc)
166 /* Mangling PC to avoid loops on original BRK instr */
167 ((ucontext_t *)uc)->uc_mcontext.pc += 4;
168 memcpy(td->live_uc, uc, td->live_sz);
169 ASSERT_GOOD_CONTEXT(td->live_uc);
170 td->live_uc_valid = 1;
172 "GOOD CONTEXT grabbed from sig_copyctx handler\n");
177 static void default_handler(int signum, siginfo_t *si, void *uc)
179 if (current->sig_unsupp && signum == current->sig_unsupp &&
180 handle_signal_unsupported(current, si, uc)) {
181 fprintf(stderr, "Handled SIG_UNSUPP\n");
182 } else if (current->sig_trig && signum == current->sig_trig &&
183 handle_signal_trigger(current, si, uc)) {
184 fprintf(stderr, "Handled SIG_TRIG\n");
185 } else if (current->sig_ok && signum == current->sig_ok &&
186 handle_signal_ok(current, si, uc)) {
187 fprintf(stderr, "Handled SIG_OK\n");
188 } else if (signum == sig_copyctx && current->live_uc &&
189 handle_signal_copyctx(current, si, uc)) {
190 fprintf(stderr, "Handled SIG_COPYCTX\n");
192 if (signum == SIGALRM && current->timeout) {
193 fprintf(stderr, "-- Timeout !\n");
196 "-- RX UNEXPECTED SIGNAL: %d\n", signum);
198 default_result(current, 1);
202 static int default_setup(struct tdescr *td)
206 sa.sa_sigaction = default_handler;
207 sa.sa_flags = SA_SIGINFO | SA_RESTART;
208 sa.sa_flags |= td->sa_flags;
209 sigemptyset(&sa.sa_mask);
210 /* uncatchable signals naturally skipped ... */
211 for (int sig = 1; sig < 32; sig++)
212 sigaction(sig, &sa, NULL);
214 * RT Signals default disposition is Term but they cannot be
215 * generated by the Kernel in response to our tests; so just catch
216 * them all and report them as UNEXPECTED signals.
218 for (int sig = SIGRTMIN; sig <= SIGRTMAX; sig++)
219 sigaction(sig, &sa, NULL);
221 /* just in case...unblock explicitly all we need */
223 unblock_signal(td->sig_trig);
225 unblock_signal(td->sig_ok);
227 unblock_signal(td->sig_unsupp);
230 unblock_signal(SIGALRM);
233 fprintf(stderr, "Registered handlers for all signals.\n");
238 static inline int default_trigger(struct tdescr *td)
240 return !raise(td->sig_trig);
243 int test_init(struct tdescr *td)
245 if (td->sig_trig == sig_copyctx) {
247 "Signal %d is RESERVED, cannot be used as a trigger. Aborting\n",
252 unblock_signal(sig_copyctx);
254 td->minsigstksz = getauxval(AT_MINSIGSTKSZ);
255 if (!td->minsigstksz)
256 td->minsigstksz = MINSIGSTKSZ;
257 fprintf(stderr, "Detected MINSTKSIGSZ:%d\n", td->minsigstksz);
259 if (td->feats_required) {
260 td->feats_supported = 0;
262 * Checking for CPU required features using both the
263 * auxval and the arm64 MRS Emulation to read sysregs.
265 if (getauxval(AT_HWCAP) & HWCAP_SSBS)
266 td->feats_supported |= FEAT_SSBS;
267 if (getauxval(AT_HWCAP) & HWCAP_SVE)
268 td->feats_supported |= FEAT_SVE;
271 "Required Features: [%s] supported\n",
272 feats_to_string(td->feats_required &
273 td->feats_supported));
276 "Required Features: [%s] NOT supported\n",
277 feats_to_string(td->feats_required &
278 ~td->feats_supported));
281 /* Perform test specific additional initialization */
282 if (td->init && !td->init(td)) {
283 fprintf(stderr, "FAILED Testcase initialization.\n");
287 fprintf(stderr, "Testcase initialized.\n");
292 int test_setup(struct tdescr *td)
294 /* assert core invariants symptom of a rotten testcase */
300 /* Default result is FAIL if test setup fails */
301 td->result = KSFT_FAIL;
303 return td->setup(td);
305 return default_setup(td);
308 int test_run(struct tdescr *td)
312 return td->trigger(td);
314 return default_trigger(td);
316 return td->run(td, NULL, NULL);
320 void test_result(struct tdescr *td)
322 if (td->initialized && td->result != KSFT_SKIP && td->check_result)
323 td->check_result(td);
324 default_result(td, 0);
327 void test_cleanup(struct tdescr *td)