1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2019 ARM Limited */
3
4 #ifndef __TEST_SIGNALS_UTILS_H__
5 #define __TEST_SIGNALS_UTILS_H__
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "test_signals.h"
12
13 int test_init(struct tdescr *td);
14 int test_setup(struct tdescr *td);
15 void test_cleanup(struct tdescr *td);
16 int test_run(struct tdescr *td);
17 void test_result(struct tdescr *td);
18
feats_ok(struct tdescr * td)19 static inline bool feats_ok(struct tdescr *td)
20 {
21 if (td->feats_incompatible & td->feats_supported)
22 return false;
23 return (td->feats_required & td->feats_supported) == td->feats_required;
24 }
25
26 /*
27 * Obtaining a valid and full-blown ucontext_t from userspace is tricky:
28 * libc getcontext does() not save all the regs and messes with some of
29 * them (pstate value in particular is not reliable).
30 *
31 * Here we use a service signal to grab the ucontext_t from inside a
32 * dedicated signal handler, since there, it is populated by Kernel
33 * itself in setup_sigframe(). The grabbed context is then stored and
34 * made available in td->live_uc.
35 *
36 * As service-signal is used a SIGTRAP induced by a 'brk' instruction,
37 * because here we have to avoid syscalls to trigger the signal since
38 * they would cause any SVE sigframe content (if any) to be removed.
39 *
40 * Anyway this function really serves a dual purpose:
41 *
42 * 1. grab a valid sigcontext into td->live_uc for result analysis: in
43 * such case it returns 1.
44 *
45 * 2. detect if, somehow, a previously grabbed live_uc context has been
46 * used actively with a sigreturn: in such a case the execution would have
47 * magically resumed in the middle of this function itself (seen_already==1):
48 * in such a case return 0, since in fact we have not just simply grabbed
49 * the context.
50 *
51 * This latter case is useful to detect when a fake_sigreturn test-case has
52 * unexpectedly survived without hitting a SEGV.
53 *
54 * Note that the case of runtime dynamically sized sigframes (like in SVE
55 * context) is still NOT addressed: sigframe size is supposed to be fixed
56 * at sizeof(ucontext_t).
57 */
get_current_context(struct tdescr * td,ucontext_t * dest_uc)58 static __always_inline bool get_current_context(struct tdescr *td,
59 ucontext_t *dest_uc)
60 {
61 static volatile bool seen_already;
62
63 assert(td && dest_uc);
64 /* it's a genuine invocation..reinit */
65 seen_already = 0;
66 td->live_uc_valid = 0;
67 td->live_sz = sizeof(*dest_uc);
68 memset(dest_uc, 0x00, td->live_sz);
69 td->live_uc = dest_uc;
70 /*
71 * Grab ucontext_t triggering a SIGTRAP.
72 *
73 * Note that:
74 * - live_uc_valid is declared volatile sig_atomic_t in
75 * struct tdescr since it will be changed inside the
76 * sig_copyctx handler
77 * - the additional 'memory' clobber is there to avoid possible
78 * compiler's assumption on live_uc_valid and the content
79 * pointed by dest_uc, which are all changed inside the signal
80 * handler
81 * - BRK causes a debug exception which is handled by the Kernel
82 * and finally causes the SIGTRAP signal to be delivered to this
83 * test thread. Since such delivery happens on the ret_to_user()
84 * /do_notify_resume() debug exception return-path, we are sure
85 * that the registered SIGTRAP handler has been run to completion
86 * before the execution path is restored here: as a consequence
87 * we can be sure that the volatile sig_atomic_t live_uc_valid
88 * carries a meaningful result. Being in a single thread context
89 * we'll also be sure that any access to memory modified by the
90 * handler (namely ucontext_t) will be visible once returned.
91 * - note that since we are using a breakpoint instruction here
92 * to cause a SIGTRAP, the ucontext_t grabbed from the signal
93 * handler would naturally contain a PC pointing exactly to this
94 * BRK line, which means that, on return from the signal handler,
95 * or if we place the ucontext_t on the stack to fake a sigreturn,
96 * we'll end up in an infinite loop of BRK-SIGTRAP-handler.
97 * For this reason we take care to artificially move forward the
98 * PC to the next instruction while inside the signal handler.
99 */
100 asm volatile ("brk #666"
101 : "+m" (*dest_uc)
102 :
103 : "memory");
104
105 /*
106 * If we get here with seen_already==1 it implies the td->live_uc
107 * context has been used to get back here....this probably means
108 * a test has failed to cause a SEGV...anyway live_uc does not
109 * point to a just acquired copy of ucontext_t...so return 0
110 */
111 if (seen_already) {
112 fprintf(stdout,
113 "Unexpected successful sigreturn detected: live_uc is stale !\n");
114 return 0;
115 }
116 seen_already = 1;
117
118 return td->live_uc_valid;
119 }
120
121 int fake_sigreturn(void *sigframe, size_t sz, int misalign_bytes);
122 #endif
123