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