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 	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  */
58 static __always_inline bool get_current_context(struct tdescr *td,
59 						ucontext_t *dest_uc,
60 						size_t dest_sz)
61 {
62 	static volatile bool seen_already;
63 
64 	assert(td && dest_uc);
65 	/* it's a genuine invocation..reinit */
66 	seen_already = 0;
67 	td->live_uc_valid = 0;
68 	td->live_sz = dest_sz;
69 	memset(dest_uc, 0x00, td->live_sz);
70 	td->live_uc = dest_uc;
71 	/*
72 	 * Grab ucontext_t triggering a SIGTRAP.
73 	 *
74 	 * Note that:
75 	 * - live_uc_valid is declared volatile sig_atomic_t in
76 	 *   struct tdescr since it will be changed inside the
77 	 *   sig_copyctx handler
78 	 * - the additional 'memory' clobber is there to avoid possible
79 	 *   compiler's assumption on live_uc_valid and the content
80 	 *   pointed by dest_uc, which are all changed inside the signal
81 	 *   handler
82 	 * - BRK causes a debug exception which is handled by the Kernel
83 	 *   and finally causes the SIGTRAP signal to be delivered to this
84 	 *   test thread. Since such delivery happens on the ret_to_user()
85 	 *   /do_notify_resume() debug exception return-path, we are sure
86 	 *   that the registered SIGTRAP handler has been run to completion
87 	 *   before the execution path is restored here: as a consequence
88 	 *   we can be sure that the volatile sig_atomic_t live_uc_valid
89 	 *   carries a meaningful result. Being in a single thread context
90 	 *   we'll also be sure that any access to memory modified by the
91 	 *   handler (namely ucontext_t) will be visible once returned.
92 	 * - note that since we are using a breakpoint instruction here
93 	 *   to cause a SIGTRAP, the ucontext_t grabbed from the signal
94 	 *   handler would naturally contain a PC pointing exactly to this
95 	 *   BRK line, which means that, on return from the signal handler,
96 	 *   or if we place the ucontext_t on the stack to fake a sigreturn,
97 	 *   we'll end up in an infinite loop of BRK-SIGTRAP-handler.
98 	 *   For this reason we take care to artificially move forward the
99 	 *   PC to the next instruction while inside the signal handler.
100 	 */
101 	asm volatile ("brk #666"
102 		      : "+m" (*dest_uc)
103 		      :
104 		      : "memory");
105 
106 	/*
107 	 * If we get here with seen_already==1 it implies the td->live_uc
108 	 * context has been used to get back here....this probably means
109 	 * a test has failed to cause a SEGV...anyway live_uc does not
110 	 * point to a just acquired copy of ucontext_t...so return 0
111 	 */
112 	if (seen_already) {
113 		fprintf(stdout,
114 			"Unexpected successful sigreturn detected: live_uc is stale !\n");
115 		return 0;
116 	}
117 	seen_already = 1;
118 
119 	return td->live_uc_valid;
120 }
121 
122 int fake_sigreturn(void *sigframe, size_t sz, int misalign_bytes);
123 #endif
124