Debian Codegraph @

psx/psx_calls.c

raw
1 /*
2 * Copyright (c) 2025 Andrew G. Morgan <[email protected]>
3 *
4 * This file contains the low level magic to ensure that the PSX
5 * mechanism is the first to intercept the specified psx_sig signal.
6 * This is a hidden function of the psx library in its own file
7 * because its use of kernel headers conflicts badly with the more
8 * traditional *libc provided headers.
9 */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE
13 #endif
14
15 #include <signal.h>
16 #include <string.h>
17 #include <sys/syscall.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20
21 #include "psx_syscall.h"
22 #include "libpsx.h"
23
24 /*
25 * This is terrible, but some of the OS installed <asm/signal.h>
26 * headers only seem to know about the legacy 32-bit signal masks. So,
27 * we need to #define our way around that...
28 *
29 * Also, the rt_sig*() system calls use a different sigaction
30 * definition in these cases, so #define around that too.
31 *
32 * Coupled with the glibc suppression of access to the exact signal we
33 * want to use, we have ended up just inlining all architecture
34 * support here.
35 *
36 * To figure out all this stuff, I started with the following in the
37 * linux source tree:
38 *
39 * mkdir -p junk/uapi
40 * ln -s ../include/asm-generic ./junk/asm
41 * ln -s ../../include/uapi/asm-generic ./junk/uapi/asm
42 * a=arm ; gcc -I ./arch/$a/include -I ./arch/$a/include/generated \
43 * -I ./include -I ./tools/include/generated/ -I ./junk \
44 * -E ./include/uapi/linux/signal.h | less -psigaction
45 */
46 #if defined(__x86_64__) || defined(__i386__) \
47 || defined(__arm__) || defined(__aarch64__) \
48 || defined(__mips__) || defined(__loongarch__) \
49 || defined(__powerpc__) || defined(__s390__) || defined(__riscv) \
50 || defined(__alpha__) || defined(__hppa__) || defined(__sh__) \
51 || defined(__m68k__) || defined(__sparc__) || defined(__arc__) \
52 || defined(__microblaze__) || defined(__or1k__) \
53 || defined(__xtensa__)
54
55 #undef _NSIG
56 #undef _NSIG_BPW
57 #undef _NSIG_WORDS
58 #undef SA_RESTORER
59 #undef _HAS_SA_RESTORER
60 #undef sa_handler
61 #undef sa_sigaction
62
63 #if defined(__mips__)
64 #define _NSIG 128
65 #else
66 #define _NSIG 64
67 #endif
68
69 #define _NSIG_BPW (8*sizeof(unsigned long))
70 #define _NSIG_WORDS (_NSIG / _NSIG_BPW)
71
72 #if defined(__x86_64__) || defined(__i386__) \
73 || defined(__arm__) || defined(__powerpc__) \
74 || defined(__arc__)
75 /* field used */
76 #define SA_RESTORER 0x04000000
77 #endif /* architectures that use SA_RESTORER */
78
79 #if defined(SA_RESTORER) \
80 || defined(__aarch64__) \
81 || defined(__m68k__) || defined(__sh__) \
82 || defined(__s390__) || defined(__sparc__) \
83 || defined(__xtensa__)
84 /* field defined */
85 #define _HAS_SA_RESTORER void *sa_restorer;
86 #else
87 #define _HAS_SA_RESTORER
88 #endif /* architectures that include sa_restorer field */
89
90 typedef struct {
91 unsigned long sig[_NSIG_WORDS];
92 } psx_sigset_t;
93
94 #define sigset_t psx_sigset_t
95
96 struct psx_sigaction {
97 #if defined(__m68k__) || defined(__alpha__) || defined(__xtensa__)
98 void *sa_handler;
99 sigset_t sa_mask;
100 unsigned long sa_flags;
101 _HAS_SA_RESTORER
102 #else /* ndef (__m68k__ or __alpha__) */
103 #if defined(__mips__)
104 unsigned long sa_flags;
105 void *sa_handler;
106 #else
107 void *sa_handler;
108 unsigned long sa_flags;
109 #endif
110 _HAS_SA_RESTORER
111 sigset_t sa_mask;
112 #endif /* def (__m68k__ or __alpha__) */
113 };
114
115 #define sigaction psx_sigaction
116
117 #endif /* various architecture defines */
118
119 /*
120 * Local definition of the "sigaction"s for signal handling including
121 * chaining of signal handlers. The content of this type is not known
122 * outside of this present source file. The reason for this
123 * obfuscation is the fact that this present file uses kernel headers
124 * which may differ in structure definitions from the regular *libc
125 * implementations with the same name.
126 */
127 typedef struct {
128 struct sigaction sig_action;
129 struct sigaction chained_action;
130 } psx_actions_t;
131
132 /*
133 * Used to allocate space for the actions in the tracker, without
134 * revealing the content outside this current file.
135 */
136 __attribute__((visibility ("hidden"))) int psx_actions_size(void) {
137 return sizeof(psx_actions_t);
138 }
139
140 /* int how, sigset_t *nset, sigset_t *oset */
141 #define _psx_rt_sigprocmask(how, nset, oset) \
142 syscall(SYS_rt_sigprocmask, how, nset, oset, sizeof(sigset_t))
143
144 /* int sig, struct sigaction *act, struct sigaction *oact */
145 #define _psx_rt_sigaction(sig, act, oact) \
146 syscall(SYS_rt_sigaction, sig, act, oact, sizeof(sigset_t))
147
148 #define _psx_sigemptyset(pmask) \
149 memset(pmask, 0, sizeof(*(pmask)))
150
151 #ifdef _NSIG_WORDS
152
153 #define _psx_sigaddset(pmask, signo) \
154 (pmask)->sig[(signo-1)/_NSIG_BPW] |= 1UL << (((signo-1)%_NSIG_BPW))
155
156 #else /* ndef _NSIG_WORDS */
157
158 #define _psx_sigaddset(pmask, signo) \
159 *(pmask) |= 1UL << (signo-1)
160
161 #endif /* def _NSIG_WORDS */
162
163 #ifdef SA_RESTORER
164 /*
165 * Actual assembly code for this "function" is embedded in
166 * hidden form in psx_posix_syscall_actor() below.
167 */
168 __attribute__((visibility ("hidden"))) void psx_restorer(void);
169 #endif /* def SA_RESTORER */
170
171 /*
172 * psx_posix_syscall_actor performs the system call on the targeted
173 * thread and signals it is no longer pending.
174 */
175 static void psx_posix_syscall_actor(int signum, siginfo_t *info, void *ignore) {
176 /* bail early to the next in the chain if not something we recognize */
177 psx_lock();
178 if (signum != psx_tracker.psx_sig || !psx_tracker.cmd.active ||
179 info == NULL || info->si_code != SI_TKILL ||
180 info->si_pid != psx_tracker.pid) {
181 psx_actions_t *actions;
182 void (*chained_actor)(int, siginfo_t *,void *);
183 psx_unlock();
184 actions = psx_tracker.actions;
185 chained_actor = (void *) actions->chained_action.sa_handler;
186 if (chained_actor != 0) {
187 chained_actor(signum, info, ignore);
188 }
189 #ifdef SA_RESTORER
190 /*
191 * This architecture requires we provide a signal exit fixup
192 * trampoline. We use some assembly to provide this, aliased
193 * to a function (prototype above). This assembly won't be
194 * executed as part of the preceding code, but has to live
195 * somewhere so we hide it here where it will get compiled,
196 * but not slow down the PSX functionality.
197 */
198 if (psx_tracker.force_failure) {
199 #if defined(__x86_64__)
200 __asm__ __volatile__("\npsx_restorer:\n\tmov $15,%rax\n\tsyscall\n");
201 #elif defined(__i386__)
202 __asm__ __volatile__("\npsx_restorer:\n\tmov $173, %eax\n\tint $0x80\n");
203 #elif defined(__arm__)
204 __asm__ __volatile__("\npsx_restorer:\n\tmov r7,#173\n\tswi 0\n");
205 #elif defined(__powerpc__)
206 __asm__ __volatile__("\npsx_restorer:\n\tli 0, 172\n\tsc\n");
207 #elif defined(__arc__)
208 __asm__ __volatile__("\npsx_restorer:\n\tmov r8, 139\n\ttrap_s 0\n");
209 #else
210 #error "unsupported architecture - https://bugzilla.kernel.org/show_bug.cgi?id=219687"
211 #endif /* supported architectures */
212 }
213 #endif /* def SA_RESTORER */
214 return;
215 }
216 psx_unlock();
217
218 long int retval;
219 if (!psx_tracker.cmd.six) {
220 retval = syscall(psx_tracker.cmd.syscall_nr,
221 psx_tracker.cmd.arg1,
222 psx_tracker.cmd.arg2,
223 psx_tracker.cmd.arg3);
224 } else {
225 retval = syscall(psx_tracker.cmd.syscall_nr,
226 psx_tracker.cmd.arg1,
227 psx_tracker.cmd.arg2,
228 psx_tracker.cmd.arg3,
229 psx_tracker.cmd.arg4,
230 psx_tracker.cmd.arg5,
231 psx_tracker.cmd.arg6);
232 }
233
234 /*
235 * communicate the result of the thread's attempt to perform the
236 * syscall.
237 */
238 long tid = _psx_gettid();
239 psx_lock();
240 psx_thread_ref_t *ref =
241 &psx_tracker.map[psx_mix(tid) & psx_tracker.map_mask];
242 ref->retval = retval;
243 ref->pending = 0;
244 /*
245 * Block this thread until all threads have been interrupted.
246 * This prevents threads clone()ing after running the syscall and
247 * confusing the psx mechanism into thinking those new threads
248 * need to also run the syscall. They wouldn't need to run it,
249 * because they would inherit the thread state of a syscall that
250 * has already happened. However, figuring that out for an
251 * unblocked thread is hard, so we prevent it from happening.
252 */
253 while (psx_tracker.cmd.active) {
254 psx_cond_wait();
255 }
256 psx_tracker.incomplete--;
257 psx_unlock();
258 }
259
260 /*
261 * only used by psx_confirm_sigaction() which is called under lock. We
262 * don't have to rely on the stack for this structure because we're
263 * reserving static space for it.
264 */
265 static struct sigaction existing_sa;
266
267 /*
268 * psx_confirm_sigaction (re)confirms that the psx handler is the
269 * first handler to respond to the psx signal. It assumes that
270 * psx_tracker.psx_sig has been set.
271 */
272 __attribute__((visibility ("hidden"))) void psx_confirm_sigaction(void) {
273 sigset_t mask, orig;
274 psx_actions_t *actions = psx_tracker.actions;
275
276 /*
277 * Block interrupts while potentially rewriting the handler.
278 */
279 _psx_sigemptyset(&mask);
280 _psx_sigaddset(&mask, psx_tracker.psx_sig);
281 _psx_rt_sigprocmask(SIG_BLOCK, &mask, &orig);
282 _psx_rt_sigaction(psx_tracker.psx_sig, NULL, &existing_sa);
283 if (existing_sa.sa_handler != (void *) &psx_posix_syscall_actor) {
284 memcpy(&actions->chained_action, &existing_sa,
285 sizeof(struct sigaction));
286 /*
287 * no less masking etc than original handler (some
288 * architectures hook things on the restorer etc)
289 */
290 memcpy(&actions->sig_action, &existing_sa, sizeof(existing_sa));
291 actions->sig_action.sa_handler = (void *) &psx_posix_syscall_actor;
292 actions->sig_action.sa_flags |= SA_SIGINFO | SA_ONSTACK | SA_RESTART
293 #ifdef SA_RESTORER
294 | SA_RESTORER;
295 /* if we need a restorer, and nothing is set, use our own */
296 if (actions->sig_action.sa_restorer == 0) {
297 actions->sig_action.sa_restorer = (void *) &psx_restorer;
298 }
299 #else
300 ;
301 #endif /* def SA_RESTORER */
302 _psx_rt_sigaction(psx_tracker.psx_sig, &actions->sig_action, NULL);
303 }
304 _psx_rt_sigprocmask(SIG_SETMASK, &orig, NULL);
305 }