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