blob: 6ee61a918424231699834173995fddfb4d191576 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard66fb9762003-03-23 01:06:05 +00002 * Emulation of Linux signals
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
blueswir1530e7612009-01-05 18:11:53 +000018 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 * MA 02110-1301, USA.
bellard31e31b82003-02-18 22:55:36 +000020 */
21#include <stdlib.h>
22#include <stdio.h>
bellard66fb9762003-03-23 01:06:05 +000023#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000024#include <stdarg.h>
bellard2677e102003-04-10 00:03:27 +000025#include <unistd.h>
bellard31e31b82003-02-18 22:55:36 +000026#include <signal.h>
bellard66fb9762003-03-23 01:06:05 +000027#include <errno.h>
aurel32603e4fd2009-04-15 16:18:38 +000028#include <assert.h>
bellard31e31b82003-02-18 22:55:36 +000029#include <sys/ucontext.h>
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030030#include <sys/resource.h>
bellard31e31b82003-02-18 22:55:36 +000031
bellard3ef693a2003-03-23 20:17:16 +000032#include "qemu.h"
blueswir17d99a002009-01-14 19:00:36 +000033#include "qemu-common.h"
blueswir1992f48a2007-10-14 16:27:31 +000034#include "target_signal.h"
bellard66fb9762003-03-23 01:06:05 +000035
36//#define DEBUG_SIGNAL
37
blueswir1249c4c32008-10-05 11:09:37 +000038static struct target_sigaltstack target_sigaltstack_used = {
thsa04e1342007-09-27 13:57:58 +000039 .ss_sp = 0,
40 .ss_size = 0,
41 .ss_flags = TARGET_SS_DISABLE,
42};
43
pbrook624f7972008-05-31 16:11:38 +000044static struct target_sigaction sigact_table[TARGET_NSIG];
bellard31e31b82003-02-18 22:55:36 +000045
ths5fafdf22007-09-16 21:08:06 +000046static void host_signal_handler(int host_signum, siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +000047 void *puc);
48
bellard9e5f5282003-07-13 17:33:54 +000049static uint8_t host_to_target_signal_table[65] = {
50 [SIGHUP] = TARGET_SIGHUP,
51 [SIGINT] = TARGET_SIGINT,
52 [SIGQUIT] = TARGET_SIGQUIT,
53 [SIGILL] = TARGET_SIGILL,
54 [SIGTRAP] = TARGET_SIGTRAP,
55 [SIGABRT] = TARGET_SIGABRT,
bellard01e3b762003-09-30 21:10:14 +000056/* [SIGIOT] = TARGET_SIGIOT,*/
bellard9e5f5282003-07-13 17:33:54 +000057 [SIGBUS] = TARGET_SIGBUS,
58 [SIGFPE] = TARGET_SIGFPE,
59 [SIGKILL] = TARGET_SIGKILL,
60 [SIGUSR1] = TARGET_SIGUSR1,
61 [SIGSEGV] = TARGET_SIGSEGV,
62 [SIGUSR2] = TARGET_SIGUSR2,
63 [SIGPIPE] = TARGET_SIGPIPE,
64 [SIGALRM] = TARGET_SIGALRM,
65 [SIGTERM] = TARGET_SIGTERM,
66#ifdef SIGSTKFLT
67 [SIGSTKFLT] = TARGET_SIGSTKFLT,
68#endif
69 [SIGCHLD] = TARGET_SIGCHLD,
70 [SIGCONT] = TARGET_SIGCONT,
71 [SIGSTOP] = TARGET_SIGSTOP,
72 [SIGTSTP] = TARGET_SIGTSTP,
73 [SIGTTIN] = TARGET_SIGTTIN,
74 [SIGTTOU] = TARGET_SIGTTOU,
75 [SIGURG] = TARGET_SIGURG,
76 [SIGXCPU] = TARGET_SIGXCPU,
77 [SIGXFSZ] = TARGET_SIGXFSZ,
78 [SIGVTALRM] = TARGET_SIGVTALRM,
79 [SIGPROF] = TARGET_SIGPROF,
80 [SIGWINCH] = TARGET_SIGWINCH,
81 [SIGIO] = TARGET_SIGIO,
82 [SIGPWR] = TARGET_SIGPWR,
83 [SIGSYS] = TARGET_SIGSYS,
84 /* next signals stay the same */
pbrook624f7972008-05-31 16:11:38 +000085 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
86 host libpthread signals. This assumes noone actually uses SIGRTMAX :-/
87 To fix this properly we need to do manual signal delivery multiplexed
88 over a single host signal. */
89 [__SIGRTMIN] = __SIGRTMAX,
90 [__SIGRTMAX] = __SIGRTMIN,
bellard9e5f5282003-07-13 17:33:54 +000091};
92static uint8_t target_to_host_signal_table[65];
93
thsa04e1342007-09-27 13:57:58 +000094static inline int on_sig_stack(unsigned long sp)
95{
96 return (sp - target_sigaltstack_used.ss_sp
97 < target_sigaltstack_used.ss_size);
98}
99
100static inline int sas_ss_flags(unsigned long sp)
101{
102 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
103 : on_sig_stack(sp) ? SS_ONSTACK : 0);
104}
105
pbrook1d9d8b52009-04-16 15:17:02 +0000106int host_to_target_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +0000107{
pbrook4cb05962008-05-30 18:05:19 +0000108 if (sig > 64)
109 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000110 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000111}
112
pbrook4cb05962008-05-30 18:05:19 +0000113int target_to_host_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +0000114{
pbrook4cb05962008-05-30 18:05:19 +0000115 if (sig > 64)
116 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000117 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000118}
119
pbrookf5545b52008-05-30 22:37:07 +0000120static inline void target_sigemptyset(target_sigset_t *set)
121{
122 memset(set, 0, sizeof(*set));
123}
124
125static inline void target_sigaddset(target_sigset_t *set, int signum)
126{
127 signum--;
128 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
129 set->sig[signum / TARGET_NSIG_BPW] |= mask;
130}
131
132static inline int target_sigismember(const target_sigset_t *set, int signum)
133{
134 signum--;
135 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
136 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
137}
138
ths5fafdf22007-09-16 21:08:06 +0000139static void host_to_target_sigset_internal(target_sigset_t *d,
bellard92319442004-06-19 16:58:13 +0000140 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000141{
142 int i;
pbrookf5545b52008-05-30 22:37:07 +0000143 target_sigemptyset(d);
144 for (i = 1; i <= TARGET_NSIG; i++) {
145 if (sigismember(s, i)) {
146 target_sigaddset(d, host_to_target_signal(i));
147 }
bellard9e5f5282003-07-13 17:33:54 +0000148 }
bellard66fb9762003-03-23 01:06:05 +0000149}
150
bellard92319442004-06-19 16:58:13 +0000151void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
152{
153 target_sigset_t d1;
154 int i;
155
156 host_to_target_sigset_internal(&d1, s);
157 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000158 d->sig[i] = tswapl(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000159}
160
blueswir18fcd3692008-08-17 20:26:25 +0000161static void target_to_host_sigset_internal(sigset_t *d,
162 const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000163{
164 int i;
pbrookf5545b52008-05-30 22:37:07 +0000165 sigemptyset(d);
166 for (i = 1; i <= TARGET_NSIG; i++) {
167 if (target_sigismember(s, i)) {
168 sigaddset(d, target_to_host_signal(i));
169 }
170 }
bellard66fb9762003-03-23 01:06:05 +0000171}
172
bellard92319442004-06-19 16:58:13 +0000173void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
174{
175 target_sigset_t s1;
176 int i;
177
178 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000179 s1.sig[i] = tswapl(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000180 target_to_host_sigset_internal(d, &s1);
181}
ths3b46e622007-09-17 08:09:54 +0000182
blueswir1992f48a2007-10-14 16:27:31 +0000183void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000184 const sigset_t *sigset)
185{
bellard9e5f5282003-07-13 17:33:54 +0000186 target_sigset_t d;
187 host_to_target_sigset(&d, sigset);
188 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000189}
190
ths5fafdf22007-09-16 21:08:06 +0000191void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000192 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000193{
bellard9e5f5282003-07-13 17:33:54 +0000194 target_sigset_t d;
195 int i;
196
197 d.sig[0] = *old_sigset;
198 for(i = 1;i < TARGET_NSIG_WORDS; i++)
199 d.sig[i] = 0;
200 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000201}
202
bellard9de5e442003-03-23 16:49:39 +0000203/* siginfo conversion */
204
ths5fafdf22007-09-16 21:08:06 +0000205static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000206 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000207{
bellard9de5e442003-03-23 16:49:39 +0000208 int sig;
209 sig = host_to_target_signal(info->si_signo);
210 tinfo->si_signo = sig;
211 tinfo->si_errno = 0;
pbrookafd7cd92008-05-31 12:14:21 +0000212 tinfo->si_code = info->si_code;
ths5fafdf22007-09-16 21:08:06 +0000213 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000214 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000215 /* should never come here, but who knows. The information for
216 the target is irrelevant */
217 tinfo->_sifields._sigfault._addr = 0;
ths7f7f7c82007-07-12 11:02:46 +0000218 } else if (sig == SIGIO) {
219 tinfo->_sifields._sigpoll._fd = info->si_fd;
bellard9de5e442003-03-23 16:49:39 +0000220 } else if (sig >= TARGET_SIGRTMIN) {
221 tinfo->_sifields._rt._pid = info->si_pid;
222 tinfo->_sifields._rt._uid = info->si_uid;
223 /* XXX: potential problem if 64 bit */
ths5fafdf22007-09-16 21:08:06 +0000224 tinfo->_sifields._rt._sigval.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000225 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000226 }
bellard66fb9762003-03-23 01:06:05 +0000227}
228
ths5fafdf22007-09-16 21:08:06 +0000229static void tswap_siginfo(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000230 const target_siginfo_t *info)
231{
232 int sig;
233 sig = info->si_signo;
234 tinfo->si_signo = tswap32(sig);
235 tinfo->si_errno = tswap32(info->si_errno);
236 tinfo->si_code = tswap32(info->si_code);
ths5fafdf22007-09-16 21:08:06 +0000237 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000238 sig == SIGBUS || sig == SIGTRAP) {
ths5fafdf22007-09-16 21:08:06 +0000239 tinfo->_sifields._sigfault._addr =
bellard9de5e442003-03-23 16:49:39 +0000240 tswapl(info->_sifields._sigfault._addr);
ths7f7f7c82007-07-12 11:02:46 +0000241 } else if (sig == SIGIO) {
242 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
bellard9de5e442003-03-23 16:49:39 +0000243 } else if (sig >= TARGET_SIGRTMIN) {
244 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
245 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000246 tinfo->_sifields._rt._sigval.sival_ptr =
bellard9de5e442003-03-23 16:49:39 +0000247 tswapl(info->_sifields._rt._sigval.sival_ptr);
248 }
249}
250
251
252void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
253{
254 host_to_target_siginfo_noswap(tinfo, info);
255 tswap_siginfo(tinfo, tinfo);
256}
257
258/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000259/* XXX: find a solution for 64 bit (additional malloced data is needed) */
bellard9de5e442003-03-23 16:49:39 +0000260void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000261{
262 info->si_signo = tswap32(tinfo->si_signo);
263 info->si_errno = tswap32(tinfo->si_errno);
264 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000265 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
266 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000267 info->si_value.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000268 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000269}
270
aurel32ca587a82008-12-18 22:44:13 +0000271static int fatal_signal (int sig)
272{
273 switch (sig) {
274 case TARGET_SIGCHLD:
275 case TARGET_SIGURG:
276 case TARGET_SIGWINCH:
277 /* Ignored by default. */
278 return 0;
279 case TARGET_SIGCONT:
280 case TARGET_SIGSTOP:
281 case TARGET_SIGTSTP:
282 case TARGET_SIGTTIN:
283 case TARGET_SIGTTOU:
284 /* Job control signals. */
285 return 0;
286 default:
287 return 1;
288 }
289}
290
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300291/* returns 1 if given signal should dump core if not handled */
292static int core_dump_signal(int sig)
293{
294 switch (sig) {
295 case TARGET_SIGABRT:
296 case TARGET_SIGFPE:
297 case TARGET_SIGILL:
298 case TARGET_SIGQUIT:
299 case TARGET_SIGSEGV:
300 case TARGET_SIGTRAP:
301 case TARGET_SIGBUS:
302 return (1);
303 default:
304 return (0);
305 }
306}
307
bellard31e31b82003-02-18 22:55:36 +0000308void signal_init(void)
309{
310 struct sigaction act;
pbrook624f7972008-05-31 16:11:38 +0000311 struct sigaction oact;
bellard9e5f5282003-07-13 17:33:54 +0000312 int i, j;
pbrook624f7972008-05-31 16:11:38 +0000313 int host_sig;
bellard31e31b82003-02-18 22:55:36 +0000314
bellard9e5f5282003-07-13 17:33:54 +0000315 /* generate signal conversion tables */
316 for(i = 1; i <= 64; i++) {
317 if (host_to_target_signal_table[i] == 0)
318 host_to_target_signal_table[i] = i;
319 }
320 for(i = 1; i <= 64; i++) {
321 j = host_to_target_signal_table[i];
322 target_to_host_signal_table[j] = i;
323 }
ths3b46e622007-09-17 08:09:54 +0000324
bellard9de5e442003-03-23 16:49:39 +0000325 /* set all host signal handlers. ALL signals are blocked during
326 the handlers to serialize them. */
pbrook624f7972008-05-31 16:11:38 +0000327 memset(sigact_table, 0, sizeof(sigact_table));
328
bellard9de5e442003-03-23 16:49:39 +0000329 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000330 act.sa_flags = SA_SIGINFO;
331 act.sa_sigaction = host_signal_handler;
pbrook624f7972008-05-31 16:11:38 +0000332 for(i = 1; i <= TARGET_NSIG; i++) {
333 host_sig = target_to_host_signal(i);
334 sigaction(host_sig, NULL, &oact);
335 if (oact.sa_sigaction == (void *)SIG_IGN) {
336 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
337 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
338 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
339 }
340 /* If there's already a handler installed then something has
341 gone horribly wrong, so don't even try to handle that case. */
aurel32ca587a82008-12-18 22:44:13 +0000342 /* Install some handlers for our own use. We need at least
343 SIGSEGV and SIGBUS, to detect exceptions. We can not just
344 trap all signals because it affects syscall interrupt
345 behavior. But do trap all default-fatal signals. */
346 if (fatal_signal (i))
pbrook624f7972008-05-31 16:11:38 +0000347 sigaction(host_sig, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000348 }
bellard31e31b82003-02-18 22:55:36 +0000349}
350
bellard66fb9762003-03-23 01:06:05 +0000351/* signal queue handling */
352
pbrook624f7972008-05-31 16:11:38 +0000353static inline struct sigqueue *alloc_sigqueue(CPUState *env)
bellard66fb9762003-03-23 01:06:05 +0000354{
pbrook624f7972008-05-31 16:11:38 +0000355 TaskState *ts = env->opaque;
356 struct sigqueue *q = ts->first_free;
bellard66fb9762003-03-23 01:06:05 +0000357 if (!q)
358 return NULL;
pbrook624f7972008-05-31 16:11:38 +0000359 ts->first_free = q->next;
bellard66fb9762003-03-23 01:06:05 +0000360 return q;
361}
362
pbrook624f7972008-05-31 16:11:38 +0000363static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
bellard66fb9762003-03-23 01:06:05 +0000364{
pbrook624f7972008-05-31 16:11:38 +0000365 TaskState *ts = env->opaque;
366 q->next = ts->first_free;
367 ts->first_free = q;
bellard66fb9762003-03-23 01:06:05 +0000368}
369
bellard9de5e442003-03-23 16:49:39 +0000370/* abort execution with signal */
malca5e50b22009-02-01 22:19:27 +0000371static void QEMU_NORETURN force_sig(int sig)
bellard66fb9762003-03-23 01:06:05 +0000372{
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300373 TaskState *ts = (TaskState *)thread_env->opaque;
374 int host_sig, core_dumped = 0;
aurel32603e4fd2009-04-15 16:18:38 +0000375 struct sigaction act;
bellard66fb9762003-03-23 01:06:05 +0000376 host_sig = target_to_host_signal(sig);
aurel32ca587a82008-12-18 22:44:13 +0000377 gdb_signalled(thread_env, sig);
aurel32603e4fd2009-04-15 16:18:38 +0000378
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300379 /* dump core if supported by target binary format */
380 if (core_dump_signal(sig) && (ts->bprm->core_dump != NULL)) {
381 stop_all_tasks();
382 core_dumped =
383 ((*ts->bprm->core_dump)(sig, thread_env) == 0);
384 }
385 if (core_dumped) {
386 /* we already dumped the core of target process, we don't want
387 * a coredump of qemu itself */
388 struct rlimit nodump;
389 getrlimit(RLIMIT_CORE, &nodump);
390 nodump.rlim_cur=0;
391 setrlimit(RLIMIT_CORE, &nodump);
392 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
393 sig, strsignal(host_sig), "core dumped" );
394 }
395
aurel32603e4fd2009-04-15 16:18:38 +0000396 /* The proper exit code for dieing from an uncaught signal is
397 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
398 * a negative value. To get the proper exit code we need to
399 * actually die from an uncaught signal. Here the default signal
400 * handler is installed, we send ourself a signal and we wait for
401 * it to arrive. */
402 sigfillset(&act.sa_mask);
403 act.sa_handler = SIG_DFL;
404 sigaction(host_sig, &act, NULL);
405
406 /* For some reason raise(host_sig) doesn't send the signal when
407 * statically linked on x86-64. */
408 kill(getpid(), host_sig);
409
410 /* Make sure the signal isn't masked (just reuse the mask inside
411 of act) */
412 sigdelset(&act.sa_mask, host_sig);
413 sigsuspend(&act.sa_mask);
414
415 /* unreachable */
416 assert(0);
417
bellard66fb9762003-03-23 01:06:05 +0000418}
419
bellard9de5e442003-03-23 16:49:39 +0000420/* queue a signal so that it will be send to the virtual CPU as soon
421 as possible */
pbrook624f7972008-05-31 16:11:38 +0000422int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000423{
pbrook624f7972008-05-31 16:11:38 +0000424 TaskState *ts = env->opaque;
425 struct emulated_sigtable *k;
bellard9de5e442003-03-23 16:49:39 +0000426 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000427 abi_ulong handler;
aurel32ca587a82008-12-18 22:44:13 +0000428 int queue;
bellard66fb9762003-03-23 01:06:05 +0000429
bellard9de5e442003-03-23 16:49:39 +0000430#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000431 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000432 sig);
bellard66fb9762003-03-23 01:06:05 +0000433#endif
pbrook624f7972008-05-31 16:11:38 +0000434 k = &ts->sigtab[sig - 1];
aurel32ca587a82008-12-18 22:44:13 +0000435 queue = gdb_queuesig ();
pbrook624f7972008-05-31 16:11:38 +0000436 handler = sigact_table[sig - 1]._sa_handler;
aurel32ca587a82008-12-18 22:44:13 +0000437 if (!queue && handler == TARGET_SIG_DFL) {
ths60b19692008-11-27 15:47:15 +0000438 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
439 kill(getpid(),SIGSTOP);
440 return 0;
441 } else
bellard66fb9762003-03-23 01:06:05 +0000442 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000443 if (sig != TARGET_SIGCHLD &&
444 sig != TARGET_SIGURG &&
ths60b19692008-11-27 15:47:15 +0000445 sig != TARGET_SIGWINCH &&
446 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +0000447 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000448 } else {
449 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000450 }
aurel32ca587a82008-12-18 22:44:13 +0000451 } else if (!queue && handler == TARGET_SIG_IGN) {
bellard66fb9762003-03-23 01:06:05 +0000452 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000453 return 0;
aurel32ca587a82008-12-18 22:44:13 +0000454 } else if (!queue && handler == TARGET_SIG_ERR) {
bellard66fb9762003-03-23 01:06:05 +0000455 force_sig(sig);
456 } else {
bellard9de5e442003-03-23 16:49:39 +0000457 pq = &k->first;
458 if (sig < TARGET_SIGRTMIN) {
459 /* if non real time signal, we queue exactly one signal */
460 if (!k->pending)
461 q = &k->info;
462 else
463 return 0;
464 } else {
465 if (!k->pending) {
466 /* first signal */
467 q = &k->info;
468 } else {
pbrook624f7972008-05-31 16:11:38 +0000469 q = alloc_sigqueue(env);
bellard9de5e442003-03-23 16:49:39 +0000470 if (!q)
471 return -EAGAIN;
472 while (*pq != NULL)
473 pq = &(*pq)->next;
474 }
475 }
476 *pq = q;
477 q->info = *info;
478 q->next = NULL;
479 k->pending = 1;
480 /* signal that a new signal is pending */
pbrook624f7972008-05-31 16:11:38 +0000481 ts->signal_pending = 1;
bellard9de5e442003-03-23 16:49:39 +0000482 return 1; /* indicates that the signal was queued */
483 }
484}
485
ths5fafdf22007-09-16 21:08:06 +0000486static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000487 void *puc)
488{
489 int sig;
490 target_siginfo_t tinfo;
491
492 /* the CPU emulator uses some host signals to detect exceptions,
aurel32eaa449b2009-01-03 13:14:52 +0000493 we forward to it some signals */
aurel32ca587a82008-12-18 22:44:13 +0000494 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
aurel32eaa449b2009-01-03 13:14:52 +0000495 && info->si_code > 0) {
bellardb346ff42003-06-15 20:05:50 +0000496 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000497 return;
498 }
499
500 /* get target signal number */
501 sig = host_to_target_signal(host_signum);
502 if (sig < 1 || sig > TARGET_NSIG)
503 return;
504#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000505 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000506#endif
507 host_to_target_siginfo_noswap(&tinfo, info);
pbrookd5975362008-06-07 20:50:51 +0000508 if (queue_signal(thread_env, sig, &tinfo) == 1) {
bellard9de5e442003-03-23 16:49:39 +0000509 /* interrupt the virtual CPU as soon as possible */
aurel323098dba2009-03-07 21:28:24 +0000510 cpu_exit(thread_env);
bellard66fb9762003-03-23 01:06:05 +0000511 }
bellard31e31b82003-02-18 22:55:36 +0000512}
513
ths0da46a62007-10-20 20:23:07 +0000514/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000515/* compare linux/kernel/signal.c:do_sigaltstack() */
516abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000517{
518 int ret;
519 struct target_sigaltstack oss;
520
521 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000522 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000523 {
524 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
525 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
526 __put_user(sas_ss_flags(sp), &oss.ss_flags);
527 }
528
bellard579a97f2007-11-11 14:26:47 +0000529 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000530 {
bellard579a97f2007-11-11 14:26:47 +0000531 struct target_sigaltstack *uss;
532 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000533
ths0da46a62007-10-20 20:23:07 +0000534 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000535 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000536 || __get_user(ss.ss_sp, &uss->ss_sp)
537 || __get_user(ss.ss_size, &uss->ss_size)
538 || __get_user(ss.ss_flags, &uss->ss_flags))
539 goto out;
bellard579a97f2007-11-11 14:26:47 +0000540 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000541
ths0da46a62007-10-20 20:23:07 +0000542 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000543 if (on_sig_stack(sp))
544 goto out;
545
ths0da46a62007-10-20 20:23:07 +0000546 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000547 if (ss.ss_flags != TARGET_SS_DISABLE
548 && ss.ss_flags != TARGET_SS_ONSTACK
549 && ss.ss_flags != 0)
550 goto out;
551
552 if (ss.ss_flags == TARGET_SS_DISABLE) {
553 ss.ss_size = 0;
554 ss.ss_sp = 0;
555 } else {
ths0da46a62007-10-20 20:23:07 +0000556 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000557 if (ss.ss_size < MINSIGSTKSZ)
558 goto out;
559 }
560
561 target_sigaltstack_used.ss_sp = ss.ss_sp;
562 target_sigaltstack_used.ss_size = ss.ss_size;
563 }
564
bellard579a97f2007-11-11 14:26:47 +0000565 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000566 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000567 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000568 goto out;
thsa04e1342007-09-27 13:57:58 +0000569 }
570
571 ret = 0;
572out:
573 return ret;
574}
575
ths0da46a62007-10-20 20:23:07 +0000576/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000577int do_sigaction(int sig, const struct target_sigaction *act,
578 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000579{
pbrook624f7972008-05-31 16:11:38 +0000580 struct target_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000581 struct sigaction act1;
582 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000583 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000584
ths2a913eb2008-11-27 15:46:25 +0000585 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000586 return -EINVAL;
587 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000588#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000589 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
bellard66fb9762003-03-23 01:06:05 +0000590 sig, (int)act, (int)oact);
591#endif
592 if (oact) {
pbrook624f7972008-05-31 16:11:38 +0000593 oact->_sa_handler = tswapl(k->_sa_handler);
594 oact->sa_flags = tswapl(k->sa_flags);
ths388bb212007-05-13 13:58:00 +0000595#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000596 oact->sa_restorer = tswapl(k->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000597#endif
pbrook624f7972008-05-31 16:11:38 +0000598 oact->sa_mask = k->sa_mask;
bellard66fb9762003-03-23 01:06:05 +0000599 }
600 if (act) {
pbrook624f7972008-05-31 16:11:38 +0000601 /* FIXME: This is not threadsafe. */
602 k->_sa_handler = tswapl(act->_sa_handler);
603 k->sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000604#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000605 k->sa_restorer = tswapl(act->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000606#endif
pbrook624f7972008-05-31 16:11:38 +0000607 k->sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000608
609 /* we update the host linux signal state */
610 host_sig = target_to_host_signal(sig);
611 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
612 sigfillset(&act1.sa_mask);
613 act1.sa_flags = SA_SIGINFO;
pbrook624f7972008-05-31 16:11:38 +0000614 if (k->sa_flags & TARGET_SA_RESTART)
bellard773b93e2004-01-04 17:15:59 +0000615 act1.sa_flags |= SA_RESTART;
616 /* NOTE: it is important to update the host kernel signal
617 ignore state to avoid getting unexpected interrupted
618 syscalls */
pbrook624f7972008-05-31 16:11:38 +0000619 if (k->_sa_handler == TARGET_SIG_IGN) {
bellard773b93e2004-01-04 17:15:59 +0000620 act1.sa_sigaction = (void *)SIG_IGN;
pbrook624f7972008-05-31 16:11:38 +0000621 } else if (k->_sa_handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +0000622 if (fatal_signal (sig))
623 act1.sa_sigaction = host_signal_handler;
624 else
625 act1.sa_sigaction = (void *)SIG_DFL;
bellard773b93e2004-01-04 17:15:59 +0000626 } else {
627 act1.sa_sigaction = host_signal_handler;
628 }
ths0da46a62007-10-20 20:23:07 +0000629 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000630 }
bellard66fb9762003-03-23 01:06:05 +0000631 }
ths0da46a62007-10-20 20:23:07 +0000632 return ret;
bellard66fb9762003-03-23 01:06:05 +0000633}
bellard31e31b82003-02-18 22:55:36 +0000634
ths5fafdf22007-09-16 21:08:06 +0000635static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
bellard43fff232003-07-09 19:31:39 +0000636 const target_siginfo_t *info)
637{
638 tswap_siginfo(tinfo, info);
639 return 0;
640}
641
thsc3b5bc82007-12-02 06:31:25 +0000642static inline int current_exec_domain_sig(int sig)
643{
644 return /* current->exec_domain && current->exec_domain->signal_invmap
645 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
646}
647
bellard459a4012007-11-11 19:45:10 +0000648#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000649
650/* from the Linux kernel */
651
652struct target_fpreg {
653 uint16_t significand[4];
654 uint16_t exponent;
655};
656
657struct target_fpxreg {
658 uint16_t significand[4];
659 uint16_t exponent;
660 uint16_t padding[3];
661};
662
663struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000664 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000665};
666
667struct target_fpstate {
668 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000669 abi_ulong cw;
670 abi_ulong sw;
671 abi_ulong tag;
672 abi_ulong ipoff;
673 abi_ulong cssel;
674 abi_ulong dataoff;
675 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000676 struct target_fpreg _st[8];
677 uint16_t status;
678 uint16_t magic; /* 0xffff = regular FPU data only */
679
680 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000681 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
682 abi_ulong mxcsr;
683 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000684 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
685 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000686 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000687};
688
689#define X86_FXSR_MAGIC 0x0000
690
691struct target_sigcontext {
692 uint16_t gs, __gsh;
693 uint16_t fs, __fsh;
694 uint16_t es, __esh;
695 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000696 abi_ulong edi;
697 abi_ulong esi;
698 abi_ulong ebp;
699 abi_ulong esp;
700 abi_ulong ebx;
701 abi_ulong edx;
702 abi_ulong ecx;
703 abi_ulong eax;
704 abi_ulong trapno;
705 abi_ulong err;
706 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000707 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000708 abi_ulong eflags;
709 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000710 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000711 abi_ulong fpstate; /* pointer */
712 abi_ulong oldmask;
713 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000714};
715
bellard66fb9762003-03-23 01:06:05 +0000716struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000717 abi_ulong tuc_flags;
718 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +0000719 target_stack_t tuc_stack;
720 struct target_sigcontext tuc_mcontext;
721 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000722};
723
724struct sigframe
725{
blueswir1992f48a2007-10-14 16:27:31 +0000726 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000727 int sig;
728 struct target_sigcontext sc;
729 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000730 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000731 char retcode[8];
732};
733
734struct rt_sigframe
735{
blueswir1992f48a2007-10-14 16:27:31 +0000736 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000737 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000738 abi_ulong pinfo;
739 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000740 struct target_siginfo info;
741 struct target_ucontext uc;
742 struct target_fpstate fpstate;
743 char retcode[8];
744};
745
746/*
747 * Set up a signal frame.
748 */
749
bellard66fb9762003-03-23 01:06:05 +0000750/* XXX: save x87 state */
751static int
752setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
bellard28be6232007-11-11 22:23:38 +0000753 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000754{
755 int err = 0;
bellard775b58d2007-11-11 16:22:17 +0000756 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000757
bellard579a97f2007-11-11 14:26:47 +0000758 /* already locked in setup_frame() */
bellarda52c7572003-06-21 13:14:12 +0000759 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
760 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
761 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
762 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000763 err |= __put_user(env->regs[R_EDI], &sc->edi);
764 err |= __put_user(env->regs[R_ESI], &sc->esi);
765 err |= __put_user(env->regs[R_EBP], &sc->ebp);
766 err |= __put_user(env->regs[R_ESP], &sc->esp);
767 err |= __put_user(env->regs[R_EBX], &sc->ebx);
768 err |= __put_user(env->regs[R_EDX], &sc->edx);
769 err |= __put_user(env->regs[R_ECX], &sc->ecx);
770 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000771 err |= __put_user(env->exception_index, &sc->trapno);
772 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000773 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000774 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000775 err |= __put_user(env->eflags, &sc->eflags);
776 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000777 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000778
bellard28be6232007-11-11 22:23:38 +0000779 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000780 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000781 magic = 0xffff;
782 err |= __put_user(magic, &fpstate->magic);
bellard28be6232007-11-11 22:23:38 +0000783 err |= __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000784
bellard66fb9762003-03-23 01:06:05 +0000785 /* non-iBCS2 extensions.. */
786 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000787 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000788 return err;
789}
790
791/*
792 * Determine which stack to use..
793 */
794
bellard579a97f2007-11-11 14:26:47 +0000795static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +0000796get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
bellard66fb9762003-03-23 01:06:05 +0000797{
798 unsigned long esp;
799
800 /* Default to using normal stack */
801 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000802 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +0000803 if (ka->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +0000804 if (sas_ss_flags(esp) == 0)
805 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
806 }
bellard66fb9762003-03-23 01:06:05 +0000807
808 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000809 else
bellarda52c7572003-06-21 13:14:12 +0000810 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
pbrook624f7972008-05-31 16:11:38 +0000811 !(ka->sa_flags & TARGET_SA_RESTORER) &&
812 ka->sa_restorer) {
813 esp = (unsigned long) ka->sa_restorer;
bellarda52c7572003-06-21 13:14:12 +0000814 }
bellard579a97f2007-11-11 14:26:47 +0000815 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000816}
817
bellard579a97f2007-11-11 14:26:47 +0000818/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +0000819static void setup_frame(int sig, struct target_sigaction *ka,
bellard66fb9762003-03-23 01:06:05 +0000820 target_sigset_t *set, CPUX86State *env)
821{
bellard579a97f2007-11-11 14:26:47 +0000822 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000823 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000824 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000825
bellard579a97f2007-11-11 14:26:47 +0000826 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000827
bellard579a97f2007-11-11 14:26:47 +0000828 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000829 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000830
thsc3b5bc82007-12-02 06:31:25 +0000831 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000832 &frame->sig);
833 if (err)
834 goto give_sigsegv;
835
bellard28be6232007-11-11 22:23:38 +0000836 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
837 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000838 if (err)
839 goto give_sigsegv;
840
bellard92319442004-06-19 16:58:13 +0000841 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
842 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
843 goto give_sigsegv;
844 }
bellard66fb9762003-03-23 01:06:05 +0000845
846 /* Set up to return from userspace. If provided, use a stub
847 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000848 if (ka->sa_flags & TARGET_SA_RESTORER) {
849 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000850 } else {
bellard775b58d2007-11-11 16:22:17 +0000851 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000852 abi_ulong retcode_addr;
853 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
854 err |= __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000855 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000856 val16 = 0xb858;
857 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000858 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000859 val16 = 0x80cd;
860 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000861 }
862
863 if (err)
864 goto give_sigsegv;
865
866 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000867 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000868 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000869
870 cpu_x86_load_seg(env, R_DS, __USER_DS);
871 cpu_x86_load_seg(env, R_ES, __USER_DS);
872 cpu_x86_load_seg(env, R_SS, __USER_DS);
873 cpu_x86_load_seg(env, R_CS, __USER_CS);
874 env->eflags &= ~TF_MASK;
875
bellard579a97f2007-11-11 14:26:47 +0000876 unlock_user_struct(frame, frame_addr, 1);
877
bellard66fb9762003-03-23 01:06:05 +0000878 return;
879
880give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000881 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000882 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000883 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000884 force_sig(TARGET_SIGSEGV /* , current */);
885}
886
bellard579a97f2007-11-11 14:26:47 +0000887/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +0000888static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellard9de5e442003-03-23 16:49:39 +0000889 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000890 target_sigset_t *set, CPUX86State *env)
891{
bellard28be6232007-11-11 22:23:38 +0000892 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000893 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000894 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000895
bellard579a97f2007-11-11 14:26:47 +0000896 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000897
bellard579a97f2007-11-11 14:26:47 +0000898 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000899 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000900
thsc3b5bc82007-12-02 06:31:25 +0000901 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000902 &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000903 addr = frame_addr + offsetof(struct rt_sigframe, info);
904 err |= __put_user(addr, &frame->pinfo);
905 addr = frame_addr + offsetof(struct rt_sigframe, uc);
906 err |= __put_user(addr, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000907 err |= copy_siginfo_to_user(&frame->info, info);
908 if (err)
909 goto give_sigsegv;
910
911 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000912 err |= __put_user(0, &frame->uc.tuc_flags);
913 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000914 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000915 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000916 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000917 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000918 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000919 &frame->uc.tuc_stack.ss_size);
920 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000921 env, set->sig[0],
922 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +0000923 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000924 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000925 goto give_sigsegv;
926 }
bellard66fb9762003-03-23 01:06:05 +0000927
928 /* Set up to return from userspace. If provided, use a stub
929 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000930 if (ka->sa_flags & TARGET_SA_RESTORER) {
931 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000932 } else {
bellard775b58d2007-11-11 16:22:17 +0000933 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000934 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
935 err |= __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000936 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000937 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000938 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000939 val16 = 0x80cd;
940 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000941 }
942
943 if (err)
944 goto give_sigsegv;
945
946 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000947 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000948 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000949
950 cpu_x86_load_seg(env, R_DS, __USER_DS);
951 cpu_x86_load_seg(env, R_ES, __USER_DS);
952 cpu_x86_load_seg(env, R_SS, __USER_DS);
953 cpu_x86_load_seg(env, R_CS, __USER_CS);
954 env->eflags &= ~TF_MASK;
955
bellard579a97f2007-11-11 14:26:47 +0000956 unlock_user_struct(frame, frame_addr, 1);
957
bellard66fb9762003-03-23 01:06:05 +0000958 return;
959
960give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000961 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000962 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000963 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000964 force_sig(TARGET_SIGSEGV /* , current */);
965}
966
967static int
968restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
969{
970 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +0000971 abi_ulong fpstate_addr;
972 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +0000973
bellard28be6232007-11-11 22:23:38 +0000974 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
975 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
976 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
977 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +0000978
bellard28be6232007-11-11 22:23:38 +0000979 env->regs[R_EDI] = tswapl(sc->edi);
980 env->regs[R_ESI] = tswapl(sc->esi);
981 env->regs[R_EBP] = tswapl(sc->ebp);
982 env->regs[R_ESP] = tswapl(sc->esp);
983 env->regs[R_EBX] = tswapl(sc->ebx);
984 env->regs[R_EDX] = tswapl(sc->edx);
985 env->regs[R_ECX] = tswapl(sc->ecx);
986 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +0000987
988 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
989 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000990
bellard28be6232007-11-11 22:23:38 +0000991 tmpflags = tswapl(sc->eflags);
992 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
993 // regs->orig_eax = -1; /* disable syscall checks */
994
995 fpstate_addr = tswapl(sc->fpstate);
996 if (fpstate_addr != 0) {
997 if (!access_ok(VERIFY_READ, fpstate_addr,
998 sizeof(struct target_fpstate)))
999 goto badframe;
1000 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +00001001 }
1002
bellard28be6232007-11-11 22:23:38 +00001003 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +00001004 return err;
bellard66fb9762003-03-23 01:06:05 +00001005badframe:
1006 return 1;
bellard66fb9762003-03-23 01:06:05 +00001007}
1008
1009long do_sigreturn(CPUX86State *env)
1010{
bellard579a97f2007-11-11 14:26:47 +00001011 struct sigframe *frame;
1012 abi_ulong frame_addr = env->regs[R_ESP] - 8;
bellard66fb9762003-03-23 01:06:05 +00001013 target_sigset_t target_set;
1014 sigset_t set;
1015 int eax, i;
1016
bellard447db212003-05-10 15:10:36 +00001017#if defined(DEBUG_SIGNAL)
1018 fprintf(stderr, "do_sigreturn\n");
1019#endif
bellard579a97f2007-11-11 14:26:47 +00001020 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1021 goto badframe;
bellard66fb9762003-03-23 01:06:05 +00001022 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +00001023 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1024 goto badframe;
1025 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1026 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1027 goto badframe;
1028 }
bellard66fb9762003-03-23 01:06:05 +00001029
bellard92319442004-06-19 16:58:13 +00001030 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +00001031 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +00001032
bellard66fb9762003-03-23 01:06:05 +00001033 /* restore registers */
1034 if (restore_sigcontext(env, &frame->sc, &eax))
1035 goto badframe;
bellard579a97f2007-11-11 14:26:47 +00001036 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001037 return eax;
1038
1039badframe:
bellard579a97f2007-11-11 14:26:47 +00001040 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001041 force_sig(TARGET_SIGSEGV);
1042 return 0;
1043}
1044
1045long do_rt_sigreturn(CPUX86State *env)
1046{
bellard28be6232007-11-11 22:23:38 +00001047 abi_ulong frame_addr;
1048 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +00001049 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +00001050 int eax;
1051
bellard28be6232007-11-11 22:23:38 +00001052 frame_addr = env->regs[R_ESP] - 4;
1053 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1054 goto badframe;
bellardb8076a72005-04-07 22:20:31 +00001055 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +00001056 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +00001057
bellardb8076a72005-04-07 22:20:31 +00001058 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +00001059 goto badframe;
1060
bellard28be6232007-11-11 22:23:38 +00001061 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1062 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +00001063 goto badframe;
thsa04e1342007-09-27 13:57:58 +00001064
bellard28be6232007-11-11 22:23:38 +00001065 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001066 return eax;
1067
1068badframe:
bellard28be6232007-11-11 22:23:38 +00001069 unlock_user_struct(frame, frame_addr, 0);
1070 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +00001071 return 0;
1072}
1073
bellard43fff232003-07-09 19:31:39 +00001074#elif defined(TARGET_ARM)
1075
1076struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001077 abi_ulong trap_no;
1078 abi_ulong error_code;
1079 abi_ulong oldmask;
1080 abi_ulong arm_r0;
1081 abi_ulong arm_r1;
1082 abi_ulong arm_r2;
1083 abi_ulong arm_r3;
1084 abi_ulong arm_r4;
1085 abi_ulong arm_r5;
1086 abi_ulong arm_r6;
1087 abi_ulong arm_r7;
1088 abi_ulong arm_r8;
1089 abi_ulong arm_r9;
1090 abi_ulong arm_r10;
1091 abi_ulong arm_fp;
1092 abi_ulong arm_ip;
1093 abi_ulong arm_sp;
1094 abi_ulong arm_lr;
1095 abi_ulong arm_pc;
1096 abi_ulong arm_cpsr;
1097 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001098};
1099
pbrooka745ec62008-05-06 15:36:17 +00001100struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001101 abi_ulong tuc_flags;
1102 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +00001103 target_stack_t tuc_stack;
1104 struct target_sigcontext tuc_mcontext;
1105 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001106};
1107
pbrooka745ec62008-05-06 15:36:17 +00001108struct target_ucontext_v2 {
1109 abi_ulong tuc_flags;
1110 abi_ulong tuc_link;
1111 target_stack_t tuc_stack;
1112 struct target_sigcontext tuc_mcontext;
1113 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1114 char __unused[128 - sizeof(sigset_t)];
1115 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1116};
1117
pbrooka8c33202008-05-07 23:22:46 +00001118struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001119{
1120 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001121 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1122 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001123};
1124
pbrooka8c33202008-05-07 23:22:46 +00001125struct sigframe_v2
1126{
1127 struct target_ucontext_v2 uc;
1128 abi_ulong retcode;
1129};
1130
pbrooka745ec62008-05-06 15:36:17 +00001131struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001132{
bellardf8b0aa22007-11-11 23:03:42 +00001133 abi_ulong pinfo;
1134 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001135 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001136 struct target_ucontext_v1 uc;
1137 abi_ulong retcode;
1138};
1139
1140struct rt_sigframe_v2
1141{
1142 struct target_siginfo info;
1143 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001144 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001145};
1146
1147#define TARGET_CONFIG_CPU_32 1
1148
1149/*
1150 * For ARM syscalls, we encode the syscall number into the instruction.
1151 */
1152#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1153#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1154
1155/*
1156 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1157 * need two 16-bit instructions.
1158 */
1159#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1160#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1161
blueswir1992f48a2007-10-14 16:27:31 +00001162static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001163 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1164 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1165};
1166
1167
bellard43fff232003-07-09 19:31:39 +00001168#define __get_user_error(x,p,e) __get_user(x, p)
1169
1170static inline int valid_user_regs(CPUState *regs)
1171{
1172 return 1;
1173}
1174
pbrooka8c33202008-05-07 23:22:46 +00001175static void
bellard43fff232003-07-09 19:31:39 +00001176setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
bellardf8b0aa22007-11-11 23:03:42 +00001177 CPUState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001178{
pbrooka8c33202008-05-07 23:22:46 +00001179 __put_user(env->regs[0], &sc->arm_r0);
1180 __put_user(env->regs[1], &sc->arm_r1);
1181 __put_user(env->regs[2], &sc->arm_r2);
1182 __put_user(env->regs[3], &sc->arm_r3);
1183 __put_user(env->regs[4], &sc->arm_r4);
1184 __put_user(env->regs[5], &sc->arm_r5);
1185 __put_user(env->regs[6], &sc->arm_r6);
1186 __put_user(env->regs[7], &sc->arm_r7);
1187 __put_user(env->regs[8], &sc->arm_r8);
1188 __put_user(env->regs[9], &sc->arm_r9);
1189 __put_user(env->regs[10], &sc->arm_r10);
1190 __put_user(env->regs[11], &sc->arm_fp);
1191 __put_user(env->regs[12], &sc->arm_ip);
1192 __put_user(env->regs[13], &sc->arm_sp);
1193 __put_user(env->regs[14], &sc->arm_lr);
1194 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001195#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001196 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001197#endif
1198
pbrooka8c33202008-05-07 23:22:46 +00001199 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1200 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1201 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1202 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001203}
1204
bellard579a97f2007-11-11 14:26:47 +00001205static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00001206get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
bellard43fff232003-07-09 19:31:39 +00001207{
1208 unsigned long sp = regs->regs[13];
1209
bellard43fff232003-07-09 19:31:39 +00001210 /*
1211 * This is the X/Open sanctioned signal stack switching.
1212 */
pbrook624f7972008-05-31 16:11:38 +00001213 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
thsa04e1342007-09-27 13:57:58 +00001214 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001215 /*
1216 * ATPCS B01 mandates 8-byte alignment
1217 */
bellard579a97f2007-11-11 14:26:47 +00001218 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001219}
1220
1221static int
pbrook624f7972008-05-31 16:11:38 +00001222setup_return(CPUState *env, struct target_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001223 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001224{
pbrook624f7972008-05-31 16:11:38 +00001225 abi_ulong handler = ka->_sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001226 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001227 int thumb = handler & 1;
bellard43fff232003-07-09 19:31:39 +00001228
pbrook624f7972008-05-31 16:11:38 +00001229 if (ka->sa_flags & TARGET_SA_RESTORER) {
1230 retcode = ka->sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001231 } else {
1232 unsigned int idx = thumb;
1233
pbrook624f7972008-05-31 16:11:38 +00001234 if (ka->sa_flags & TARGET_SA_SIGINFO)
bellard43fff232003-07-09 19:31:39 +00001235 idx += 2;
1236
1237 if (__put_user(retcodes[idx], rc))
1238 return 1;
1239#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001240 flush_icache_range((abi_ulong)rc,
1241 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001242#endif
bellardf8b0aa22007-11-11 23:03:42 +00001243 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001244 }
1245
1246 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001247 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001248 env->regs[14] = retcode;
1249 env->regs[15] = handler & (thumb ? ~1 : ~3);
pbrook75b680e2008-03-21 16:07:30 +00001250 env->thumb = thumb;
bellard43fff232003-07-09 19:31:39 +00001251
bellardb5ff1b32005-11-26 10:38:39 +00001252#if 0
bellard43fff232003-07-09 19:31:39 +00001253#ifdef TARGET_CONFIG_CPU_32
1254 env->cpsr = cpsr;
1255#endif
bellardb5ff1b32005-11-26 10:38:39 +00001256#endif
bellard43fff232003-07-09 19:31:39 +00001257
1258 return 0;
1259}
1260
pbrooka8c33202008-05-07 23:22:46 +00001261static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1262 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001263{
pbrooka8c33202008-05-07 23:22:46 +00001264 struct target_sigaltstack stack;
1265 int i;
1266
1267 /* Clear all the bits of the ucontext we don't use. */
1268 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1269
1270 memset(&stack, 0, sizeof(stack));
1271 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1272 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1273 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1274 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1275
1276 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1277 /* FIXME: Save coprocessor signal frame. */
1278 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1279 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1280 }
1281}
1282
1283/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00001284static void setup_frame_v1(int usig, struct target_sigaction *ka,
pbrooka8c33202008-05-07 23:22:46 +00001285 target_sigset_t *set, CPUState *regs)
1286{
1287 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001288 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001289 int i;
bellard43fff232003-07-09 19:31:39 +00001290
bellard579a97f2007-11-11 14:26:47 +00001291 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1292 return;
1293
pbrooka8c33202008-05-07 23:22:46 +00001294 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001295
bellard92319442004-06-19 16:58:13 +00001296 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1297 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001298 goto end;
bellard43fff232003-07-09 19:31:39 +00001299 }
1300
pbrooka8c33202008-05-07 23:22:46 +00001301 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1302 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001303
1304end:
1305 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001306}
1307
pbrook624f7972008-05-31 16:11:38 +00001308static void setup_frame_v2(int usig, struct target_sigaction *ka,
pbrooka8c33202008-05-07 23:22:46 +00001309 target_sigset_t *set, CPUState *regs)
1310{
1311 struct sigframe_v2 *frame;
1312 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1313
1314 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1315 return;
1316
1317 setup_sigframe_v2(&frame->uc, set, regs);
1318
1319 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1320 frame_addr + offsetof(struct sigframe_v2, retcode));
1321
1322 unlock_user_struct(frame, frame_addr, 1);
1323}
1324
pbrook624f7972008-05-31 16:11:38 +00001325static void setup_frame(int usig, struct target_sigaction *ka,
pbrooka8c33202008-05-07 23:22:46 +00001326 target_sigset_t *set, CPUState *regs)
1327{
1328 if (get_osversion() >= 0x020612) {
1329 setup_frame_v2(usig, ka, set, regs);
1330 } else {
1331 setup_frame_v1(usig, ka, set, regs);
1332 }
bellard43fff232003-07-09 19:31:39 +00001333}
1334
bellard579a97f2007-11-11 14:26:47 +00001335/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +00001336static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
pbrooka745ec62008-05-06 15:36:17 +00001337 target_siginfo_t *info,
1338 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001339{
pbrooka745ec62008-05-06 15:36:17 +00001340 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001341 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001342 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001343 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001344 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001345
bellard579a97f2007-11-11 14:26:47 +00001346 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001347 return /* 1 */;
1348
pbrooka745ec62008-05-06 15:36:17 +00001349 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001350 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001351 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001352 __put_user(uc_addr, &frame->puc);
1353 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001354
1355 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001356 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001357
thsa04e1342007-09-27 13:57:58 +00001358 memset(&stack, 0, sizeof(stack));
1359 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1360 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1361 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001362 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001363
pbrooka8c33202008-05-07 23:22:46 +00001364 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001365 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001366 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001367 goto end;
bellard92319442004-06-19 16:58:13 +00001368 }
bellard43fff232003-07-09 19:31:39 +00001369
pbrooka8c33202008-05-07 23:22:46 +00001370 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1371 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001372
pbrooka8c33202008-05-07 23:22:46 +00001373 env->regs[1] = info_addr;
1374 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001375
1376end:
1377 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001378}
1379
pbrook624f7972008-05-31 16:11:38 +00001380static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
pbrooka745ec62008-05-06 15:36:17 +00001381 target_siginfo_t *info,
1382 target_sigset_t *set, CPUState *env)
1383{
1384 struct rt_sigframe_v2 *frame;
1385 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001386 abi_ulong info_addr, uc_addr;
1387
1388 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1389 return /* 1 */;
1390
1391 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1392 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001393 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001394
pbrooka8c33202008-05-07 23:22:46 +00001395 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001396
pbrooka8c33202008-05-07 23:22:46 +00001397 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1398 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001399
pbrooka8c33202008-05-07 23:22:46 +00001400 env->regs[1] = info_addr;
1401 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001402
bellard579a97f2007-11-11 14:26:47 +00001403 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001404}
1405
pbrook624f7972008-05-31 16:11:38 +00001406static void setup_rt_frame(int usig, struct target_sigaction *ka,
pbrooka745ec62008-05-06 15:36:17 +00001407 target_siginfo_t *info,
1408 target_sigset_t *set, CPUState *env)
1409{
1410 if (get_osversion() >= 0x020612) {
1411 setup_rt_frame_v2(usig, ka, info, set, env);
1412 } else {
1413 setup_rt_frame_v1(usig, ka, info, set, env);
1414 }
1415}
1416
bellard43fff232003-07-09 19:31:39 +00001417static int
1418restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1419{
1420 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001421 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001422
1423 __get_user_error(env->regs[0], &sc->arm_r0, err);
1424 __get_user_error(env->regs[1], &sc->arm_r1, err);
1425 __get_user_error(env->regs[2], &sc->arm_r2, err);
1426 __get_user_error(env->regs[3], &sc->arm_r3, err);
1427 __get_user_error(env->regs[4], &sc->arm_r4, err);
1428 __get_user_error(env->regs[5], &sc->arm_r5, err);
1429 __get_user_error(env->regs[6], &sc->arm_r6, err);
1430 __get_user_error(env->regs[7], &sc->arm_r7, err);
1431 __get_user_error(env->regs[8], &sc->arm_r8, err);
1432 __get_user_error(env->regs[9], &sc->arm_r9, err);
1433 __get_user_error(env->regs[10], &sc->arm_r10, err);
1434 __get_user_error(env->regs[11], &sc->arm_fp, err);
1435 __get_user_error(env->regs[12], &sc->arm_ip, err);
1436 __get_user_error(env->regs[13], &sc->arm_sp, err);
1437 __get_user_error(env->regs[14], &sc->arm_lr, err);
1438 __get_user_error(env->regs[15], &sc->arm_pc, err);
1439#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001440 __get_user_error(cpsr, &sc->arm_cpsr, err);
pbrook75b680e2008-03-21 16:07:30 +00001441 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001442#endif
1443
1444 err |= !valid_user_regs(env);
1445
1446 return err;
1447}
1448
aurel32dc7eea62009-01-30 20:15:32 +00001449static long do_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001450{
bellardf8b0aa22007-11-11 23:03:42 +00001451 abi_ulong frame_addr;
pbrooka8c33202008-05-07 23:22:46 +00001452 struct sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001453 target_sigset_t set;
1454 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001455 int i;
bellard43fff232003-07-09 19:31:39 +00001456
1457 /*
1458 * Since we stacked the signal on a 64-bit boundary,
1459 * then 'sp' should be word aligned here. If it's
1460 * not, then the user is trying to mess with us.
1461 */
1462 if (env->regs[13] & 7)
1463 goto badframe;
1464
bellardf8b0aa22007-11-11 23:03:42 +00001465 frame_addr = env->regs[13];
1466 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1467 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001468
bellard92319442004-06-19 16:58:13 +00001469 if (__get_user(set.sig[0], &frame->sc.oldmask))
1470 goto badframe;
1471 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1472 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1473 goto badframe;
1474 }
bellard43fff232003-07-09 19:31:39 +00001475
bellard92319442004-06-19 16:58:13 +00001476 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001477 sigprocmask(SIG_SETMASK, &host_set, NULL);
1478
1479 if (restore_sigcontext(env, &frame->sc))
1480 goto badframe;
1481
1482#if 0
1483 /* Send SIGTRAP if we're single-stepping */
1484 if (ptrace_cancel_bpt(current))
1485 send_sig(SIGTRAP, current, 1);
1486#endif
bellardf8b0aa22007-11-11 23:03:42 +00001487 unlock_user_struct(frame, frame_addr, 0);
1488 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001489
1490badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001491 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001492 force_sig(SIGSEGV /* , current */);
1493 return 0;
1494}
1495
pbrooka8c33202008-05-07 23:22:46 +00001496static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1497 struct target_ucontext_v2 *uc)
1498{
1499 sigset_t host_set;
1500
1501 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1502 sigprocmask(SIG_SETMASK, &host_set, NULL);
1503
1504 if (restore_sigcontext(env, &uc->tuc_mcontext))
1505 return 1;
1506
1507 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1508 return 1;
1509
1510#if 0
1511 /* Send SIGTRAP if we're single-stepping */
1512 if (ptrace_cancel_bpt(current))
1513 send_sig(SIGTRAP, current, 1);
1514#endif
1515
1516 return 0;
1517}
1518
aurel32dc7eea62009-01-30 20:15:32 +00001519static long do_sigreturn_v2(CPUState *env)
pbrooka8c33202008-05-07 23:22:46 +00001520{
1521 abi_ulong frame_addr;
1522 struct sigframe_v2 *frame;
1523
1524 /*
1525 * Since we stacked the signal on a 64-bit boundary,
1526 * then 'sp' should be word aligned here. If it's
1527 * not, then the user is trying to mess with us.
1528 */
1529 if (env->regs[13] & 7)
1530 goto badframe;
1531
1532 frame_addr = env->regs[13];
1533 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1534 goto badframe;
1535
1536 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1537 goto badframe;
1538
1539 unlock_user_struct(frame, frame_addr, 0);
1540 return env->regs[0];
1541
1542badframe:
1543 unlock_user_struct(frame, frame_addr, 0);
1544 force_sig(SIGSEGV /* , current */);
1545 return 0;
1546}
1547
1548long do_sigreturn(CPUState *env)
1549{
1550 if (get_osversion() >= 0x020612) {
1551 return do_sigreturn_v2(env);
1552 } else {
1553 return do_sigreturn_v1(env);
1554 }
1555}
1556
aurel32dc7eea62009-01-30 20:15:32 +00001557static long do_rt_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001558{
bellardf8b0aa22007-11-11 23:03:42 +00001559 abi_ulong frame_addr;
pbrooka745ec62008-05-06 15:36:17 +00001560 struct rt_sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001561 sigset_t host_set;
1562
1563 /*
1564 * Since we stacked the signal on a 64-bit boundary,
1565 * then 'sp' should be word aligned here. If it's
1566 * not, then the user is trying to mess with us.
1567 */
1568 if (env->regs[13] & 7)
1569 goto badframe;
1570
bellardf8b0aa22007-11-11 23:03:42 +00001571 frame_addr = env->regs[13];
1572 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1573 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001574
bellardb8076a72005-04-07 22:20:31 +00001575 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001576 sigprocmask(SIG_SETMASK, &host_set, NULL);
1577
bellardb8076a72005-04-07 22:20:31 +00001578 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001579 goto badframe;
1580
pbrooka745ec62008-05-06 15:36:17 +00001581 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
thsa04e1342007-09-27 13:57:58 +00001582 goto badframe;
1583
bellard43fff232003-07-09 19:31:39 +00001584#if 0
1585 /* Send SIGTRAP if we're single-stepping */
1586 if (ptrace_cancel_bpt(current))
1587 send_sig(SIGTRAP, current, 1);
1588#endif
bellardf8b0aa22007-11-11 23:03:42 +00001589 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001590 return env->regs[0];
1591
1592badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001593 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001594 force_sig(SIGSEGV /* , current */);
1595 return 0;
1596}
1597
aurel32dc7eea62009-01-30 20:15:32 +00001598static long do_rt_sigreturn_v2(CPUState *env)
pbrooka745ec62008-05-06 15:36:17 +00001599{
1600 abi_ulong frame_addr;
1601 struct rt_sigframe_v2 *frame;
pbrooka745ec62008-05-06 15:36:17 +00001602
1603 /*
1604 * Since we stacked the signal on a 64-bit boundary,
1605 * then 'sp' should be word aligned here. If it's
1606 * not, then the user is trying to mess with us.
1607 */
1608 if (env->regs[13] & 7)
1609 goto badframe;
1610
1611 frame_addr = env->regs[13];
1612 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1613 goto badframe;
1614
pbrooka8c33202008-05-07 23:22:46 +00001615 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1616 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00001617
pbrooka745ec62008-05-06 15:36:17 +00001618 unlock_user_struct(frame, frame_addr, 0);
1619 return env->regs[0];
1620
1621badframe:
1622 unlock_user_struct(frame, frame_addr, 0);
1623 force_sig(SIGSEGV /* , current */);
1624 return 0;
1625}
1626
1627long do_rt_sigreturn(CPUState *env)
1628{
1629 if (get_osversion() >= 0x020612) {
1630 return do_rt_sigreturn_v2(env);
1631 } else {
1632 return do_rt_sigreturn_v1(env);
1633 }
1634}
1635
bellard6d5e2162004-09-30 22:04:13 +00001636#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001637
bellard6d5e2162004-09-30 22:04:13 +00001638#define __SUNOS_MAXWIN 31
1639
1640/* This is what SunOS does, so shall I. */
1641struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001642 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001643
blueswir1992f48a2007-10-14 16:27:31 +00001644 abi_ulong sigc_mask; /* sigmask to restore */
1645 abi_ulong sigc_sp; /* stack pointer */
1646 abi_ulong sigc_pc; /* program counter */
1647 abi_ulong sigc_npc; /* next program counter */
1648 abi_ulong sigc_psr; /* for condition codes etc */
1649 abi_ulong sigc_g1; /* User uses these two registers */
1650 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001651
1652 /* Now comes information regarding the users window set
1653 * at the time of the signal.
1654 */
blueswir1992f48a2007-10-14 16:27:31 +00001655 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001656
1657 /* stack ptrs for each regwin buf */
1658 char *sigc_spbuf[__SUNOS_MAXWIN];
1659
1660 /* Windows to restore after signal */
1661 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001662 abi_ulong locals[8];
1663 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001664 } sigc_wbuf[__SUNOS_MAXWIN];
1665};
1666/* A Sparc stack frame */
1667struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001668 abi_ulong locals[8];
1669 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001670 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001671 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001672 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001673 abi_ulong xargs[6];
1674 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001675};
1676
1677typedef struct {
1678 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001679 abi_ulong psr;
1680 abi_ulong pc;
1681 abi_ulong npc;
1682 abi_ulong y;
1683 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001684 } si_regs;
1685 int si_mask;
1686} __siginfo_t;
1687
1688typedef struct {
1689 unsigned long si_float_regs [32];
1690 unsigned long si_fsr;
1691 unsigned long si_fpqdepth;
1692 struct {
1693 unsigned long *insn_addr;
1694 unsigned long insn;
1695 } si_fpqueue [16];
bellard74ccb342006-07-18 21:23:34 +00001696} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001697
1698
1699struct target_signal_frame {
1700 struct sparc_stackf ss;
1701 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00001702 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001703 abi_ulong insns[2] __attribute__ ((aligned (8)));
1704 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1705 abi_ulong extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001706 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001707};
1708struct target_rt_signal_frame {
1709 struct sparc_stackf ss;
1710 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001711 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001712 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00001713 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001714 unsigned int insns[2];
1715 stack_t stack;
1716 unsigned int extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001717 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001718};
1719
bellarde80cfcf2004-12-19 23:18:01 +00001720#define UREG_O0 16
1721#define UREG_O6 22
1722#define UREG_I0 0
1723#define UREG_I1 1
1724#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001725#define UREG_I3 3
1726#define UREG_I4 4
1727#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001728#define UREG_I6 6
1729#define UREG_I7 7
1730#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001731#define UREG_FP UREG_I6
1732#define UREG_SP UREG_O6
1733
pbrook624f7972008-05-31 16:11:38 +00001734static inline abi_ulong get_sigframe(struct target_sigaction *sa,
bellard459a4012007-11-11 19:45:10 +00001735 CPUState *env, unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001736{
bellard459a4012007-11-11 19:45:10 +00001737 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001738
1739 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001740
1741 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00001742 if (sa->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +00001743 if (!on_sig_stack(sp)
1744 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1745 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001746 }
bellard459a4012007-11-11 19:45:10 +00001747 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001748}
1749
1750static int
blueswir1992f48a2007-10-14 16:27:31 +00001751setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001752{
1753 int err = 0, i;
1754
bellard6d5e2162004-09-30 22:04:13 +00001755 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001756 err |= __put_user(env->pc, &si->si_regs.pc);
1757 err |= __put_user(env->npc, &si->si_regs.npc);
1758 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001759 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001760 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1761 }
bellarda315a142005-01-30 22:59:18 +00001762 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001763 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001764 }
bellard6d5e2162004-09-30 22:04:13 +00001765 err |= __put_user(mask, &si->si_mask);
1766 return err;
1767}
bellarde80cfcf2004-12-19 23:18:01 +00001768
bellard80a9d032005-01-03 23:31:27 +00001769#if 0
bellard6d5e2162004-09-30 22:04:13 +00001770static int
1771setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1772 CPUState *env, unsigned long mask)
1773{
1774 int err = 0;
1775
1776 err |= __put_user(mask, &sc->sigc_mask);
1777 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1778 err |= __put_user(env->pc, &sc->sigc_pc);
1779 err |= __put_user(env->npc, &sc->sigc_npc);
1780 err |= __put_user(env->psr, &sc->sigc_psr);
1781 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1782 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1783
1784 return err;
1785}
bellard80a9d032005-01-03 23:31:27 +00001786#endif
bellard6d5e2162004-09-30 22:04:13 +00001787#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1788
pbrook624f7972008-05-31 16:11:38 +00001789static void setup_frame(int sig, struct target_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001790 target_sigset_t *set, CPUState *env)
1791{
bellard459a4012007-11-11 19:45:10 +00001792 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001793 struct target_signal_frame *sf;
1794 int sigframe_size, err, i;
1795
1796 /* 1. Make sure everything is clean */
1797 //synchronize_user_stack();
1798
1799 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001800 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001801
bellard459a4012007-11-11 19:45:10 +00001802 sf = lock_user(VERIFY_WRITE, sf_addr,
1803 sizeof(struct target_signal_frame), 0);
1804 if (!sf)
1805 goto sigsegv;
1806
bellarde80cfcf2004-12-19 23:18:01 +00001807 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
bellard6d5e2162004-09-30 22:04:13 +00001808#if 0
1809 if (invalid_frame_pointer(sf, sigframe_size))
1810 goto sigill_and_return;
1811#endif
1812 /* 2. Save the current process state */
1813 err = setup___siginfo(&sf->info, env, set->sig[0]);
1814 err |= __put_user(0, &sf->extra_size);
1815
1816 //err |= save_fpu_state(regs, &sf->fpu_state);
1817 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1818
1819 err |= __put_user(set->sig[0], &sf->info.si_mask);
1820 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1821 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1822 }
1823
bellarda315a142005-01-30 22:59:18 +00001824 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001825 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001826 }
bellarda315a142005-01-30 22:59:18 +00001827 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001828 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001829 }
bellard6d5e2162004-09-30 22:04:13 +00001830 if (err)
1831 goto sigsegv;
1832
1833 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001834 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001835 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001836 env->regwptr[UREG_I1] = sf_addr +
1837 offsetof(struct target_signal_frame, info);
1838 env->regwptr[UREG_I2] = sf_addr +
1839 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001840
1841 /* 4. signal handler */
pbrook624f7972008-05-31 16:11:38 +00001842 env->pc = ka->_sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00001843 env->npc = (env->pc + 4);
1844 /* 5. return to kernel instructions */
pbrook624f7972008-05-31 16:11:38 +00001845 if (ka->sa_restorer)
1846 env->regwptr[UREG_I7] = ka->sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00001847 else {
bellard775b58d2007-11-11 16:22:17 +00001848 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00001849
1850 env->regwptr[UREG_I7] = sf_addr +
1851 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00001852
1853 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001854 val32 = 0x821020d8;
1855 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001856
1857 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001858 val32 = 0x91d02010;
1859 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001860 if (err)
1861 goto sigsegv;
1862
1863 /* Flush instruction space. */
1864 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001865 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001866 }
bellard459a4012007-11-11 19:45:10 +00001867 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001868 return;
bellard459a4012007-11-11 19:45:10 +00001869#if 0
1870sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001871 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00001872#endif
bellard6d5e2162004-09-30 22:04:13 +00001873sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001874 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00001875 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001876 force_sig(TARGET_SIGSEGV);
1877}
1878static inline int
bellard74ccb342006-07-18 21:23:34 +00001879restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001880{
1881 int err;
1882#if 0
1883#ifdef CONFIG_SMP
1884 if (current->flags & PF_USEDFPU)
1885 regs->psr &= ~PSR_EF;
1886#else
1887 if (current == last_task_used_math) {
1888 last_task_used_math = 0;
1889 regs->psr &= ~PSR_EF;
1890 }
1891#endif
1892 current->used_math = 1;
1893 current->flags &= ~PF_USEDFPU;
1894#endif
1895#if 0
1896 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1897 return -EFAULT;
1898#endif
1899
bellardfafffae2006-10-28 12:09:16 +00001900#if 0
1901 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001902 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1903 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001904#endif
bellard6d5e2162004-09-30 22:04:13 +00001905 err |= __get_user(env->fsr, &fpu->si_fsr);
1906#if 0
1907 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1908 if (current->thread.fpqdepth != 0)
1909 err |= __copy_from_user(&current->thread.fpqueue[0],
1910 &fpu->si_fpqueue[0],
1911 ((sizeof(unsigned long) +
1912 (sizeof(unsigned long *)))*16));
1913#endif
1914 return err;
1915}
1916
1917
pbrook624f7972008-05-31 16:11:38 +00001918static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001919 target_siginfo_t *info,
1920 target_sigset_t *set, CPUState *env)
1921{
1922 fprintf(stderr, "setup_rt_frame: not implemented\n");
1923}
1924
1925long do_sigreturn(CPUState *env)
1926{
bellardf8b0aa22007-11-11 23:03:42 +00001927 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001928 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001929 uint32_t up_psr, pc, npc;
bellard6d5e2162004-09-30 22:04:13 +00001930 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001931 sigset_t host_set;
bellardf8b0aa22007-11-11 23:03:42 +00001932 abi_ulong fpu_save_addr;
bellarde80cfcf2004-12-19 23:18:01 +00001933 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001934
bellardf8b0aa22007-11-11 23:03:42 +00001935 sf_addr = env->regwptr[UREG_FP];
1936 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1937 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00001938#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001939 fprintf(stderr, "sigreturn\n");
1940 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
bellard80a9d032005-01-03 23:31:27 +00001941#endif
bellarde80cfcf2004-12-19 23:18:01 +00001942 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001943
1944 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00001945
bellardf8b0aa22007-11-11 23:03:42 +00001946 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00001947 goto segv_and_exit;
1948
1949 err = __get_user(pc, &sf->info.si_regs.pc);
1950 err |= __get_user(npc, &sf->info.si_regs.npc);
1951
bellard6d5e2162004-09-30 22:04:13 +00001952 if ((pc | npc) & 3)
1953 goto segv_and_exit;
1954
1955 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001956 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1957
bellard6d5e2162004-09-30 22:04:13 +00001958 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001959 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1960 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1961
1962 env->pc = pc;
1963 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001964 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001965 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001966 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1967 }
bellarda315a142005-01-30 22:59:18 +00001968 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001969 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1970 }
bellard6d5e2162004-09-30 22:04:13 +00001971
bellardf8b0aa22007-11-11 23:03:42 +00001972 err |= __get_user(fpu_save_addr, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001973
bellarde80cfcf2004-12-19 23:18:01 +00001974 //if (fpu_save)
1975 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001976
1977 /* This is pretty much atomic, no amount locking would prevent
1978 * the races which exist anyways.
1979 */
1980 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001981 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1982 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1983 }
1984
1985 target_to_host_sigset_internal(&host_set, &set);
1986 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001987
1988 if (err)
1989 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00001990 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001991 return env->regwptr[0];
1992
1993segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00001994 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001995 force_sig(TARGET_SIGSEGV);
1996}
1997
1998long do_rt_sigreturn(CPUState *env)
1999{
2000 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002001 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00002002}
2003
bellard459a4012007-11-11 19:45:10 +00002004#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00002005#define MC_TSTATE 0
2006#define MC_PC 1
2007#define MC_NPC 2
2008#define MC_Y 3
2009#define MC_G1 4
2010#define MC_G2 5
2011#define MC_G3 6
2012#define MC_G4 7
2013#define MC_G5 8
2014#define MC_G6 9
2015#define MC_G7 10
2016#define MC_O0 11
2017#define MC_O1 12
2018#define MC_O2 13
2019#define MC_O3 14
2020#define MC_O4 15
2021#define MC_O5 16
2022#define MC_O6 17
2023#define MC_O7 18
2024#define MC_NGREG 19
2025
blueswir1992f48a2007-10-14 16:27:31 +00002026typedef abi_ulong target_mc_greg_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002027typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2028
2029struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00002030 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002031 uint32_t mcfq_insn;
2032};
2033
2034struct target_mc_fpu {
2035 union {
2036 uint32_t sregs[32];
2037 uint64_t dregs[32];
2038 //uint128_t qregs[16];
2039 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00002040 abi_ulong mcfpu_fsr;
2041 abi_ulong mcfpu_fprs;
2042 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00002043 struct target_mc_fq *mcfpu_fq;
2044 unsigned char mcfpu_qcnt;
2045 unsigned char mcfpu_qentsz;
2046 unsigned char mcfpu_enab;
2047};
2048typedef struct target_mc_fpu target_mc_fpu_t;
2049
2050typedef struct {
2051 target_mc_gregset_t mc_gregs;
2052 target_mc_greg_t mc_fp;
2053 target_mc_greg_t mc_i7;
2054 target_mc_fpu_t mc_fpregs;
2055} target_mcontext_t;
2056
2057struct target_ucontext {
2058 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00002059 abi_ulong uc_flags;
blueswir15bfb56b2007-10-05 17:01:51 +00002060 target_sigset_t uc_sigmask;
2061 target_mcontext_t uc_mcontext;
2062};
2063
2064/* A V9 register window */
2065struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00002066 abi_ulong locals[8];
2067 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00002068};
2069
2070#define TARGET_STACK_BIAS 2047
2071
2072/* {set, get}context() needed for 64-bit SparcLinux userland. */
2073void sparc64_set_context(CPUSPARCState *env)
2074{
bellard459a4012007-11-11 19:45:10 +00002075 abi_ulong ucp_addr;
2076 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00002077 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00002078 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00002079 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002080 unsigned char fenab;
2081 int err;
2082 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002083
bellard459a4012007-11-11 19:45:10 +00002084 ucp_addr = env->regwptr[UREG_I0];
2085 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2086 goto do_sigsegv;
blueswir15bfb56b2007-10-05 17:01:51 +00002087 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00002088 err = __get_user(pc, &((*grp)[MC_PC]));
2089 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002090 if (err || ((pc | npc) & 3))
2091 goto do_sigsegv;
2092 if (env->regwptr[UREG_I1]) {
2093 target_sigset_t target_set;
2094 sigset_t set;
2095
2096 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002097 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002098 goto do_sigsegv;
2099 } else {
bellard459a4012007-11-11 19:45:10 +00002100 abi_ulong *src, *dst;
2101 src = ucp->uc_sigmask.sig;
2102 dst = target_set.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002103 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002104 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002105 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00002106 if (err)
2107 goto do_sigsegv;
2108 }
2109 target_to_host_sigset_internal(&set, &target_set);
2110 sigprocmask(SIG_SETMASK, &set, NULL);
2111 }
2112 env->pc = pc;
2113 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00002114 err |= __get_user(env->y, &((*grp)[MC_Y]));
2115 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002116 env->asi = (tstate >> 24) & 0xff;
2117 PUT_CCR(env, tstate >> 32);
2118 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00002119 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2120 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2121 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2122 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2123 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2124 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2125 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2126 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2127 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2128 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2129 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2130 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2131 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2132 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2133 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002134
bellard579a97f2007-11-11 14:26:47 +00002135 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2136 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002137
bellard459a4012007-11-11 19:45:10 +00002138 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2139 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2140 abi_ulong) != 0)
2141 goto do_sigsegv;
2142 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2143 abi_ulong) != 0)
2144 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002145 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2146 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002147 {
2148 uint32_t *src, *dst;
2149 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2150 dst = env->fpr;
2151 /* XXX: check that the CPU storage is the same as user context */
2152 for (i = 0; i < 64; i++, dst++, src++)
2153 err |= __get_user(*dst, src);
2154 }
bellard579a97f2007-11-11 14:26:47 +00002155 err |= __get_user(env->fsr,
2156 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2157 err |= __get_user(env->gsr,
2158 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002159 if (err)
2160 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002161 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002162 return;
2163 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002164 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002165 force_sig(SIGSEGV);
2166}
2167
2168void sparc64_get_context(CPUSPARCState *env)
2169{
bellard459a4012007-11-11 19:45:10 +00002170 abi_ulong ucp_addr;
2171 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00002172 target_mc_gregset_t *grp;
2173 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002174 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002175 int err;
2176 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002177 target_sigset_t target_set;
2178 sigset_t set;
2179
bellard459a4012007-11-11 19:45:10 +00002180 ucp_addr = env->regwptr[UREG_I0];
2181 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2182 goto do_sigsegv;
2183
blueswir15bfb56b2007-10-05 17:01:51 +00002184 mcp = &ucp->uc_mcontext;
2185 grp = &mcp->mc_gregs;
2186
2187 /* Skip over the trap instruction, first. */
2188 env->pc = env->npc;
2189 env->npc += 4;
2190
2191 err = 0;
2192
2193 sigprocmask(0, NULL, &set);
2194 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002195 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002196 err |= __put_user(target_set.sig[0],
2197 (abi_ulong *)&ucp->uc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002198 } else {
2199 abi_ulong *src, *dst;
2200 src = target_set.sig;
2201 dst = ucp->uc_sigmask.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002202 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002203 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002204 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00002205 if (err)
2206 goto do_sigsegv;
2207 }
2208
bellard459a4012007-11-11 19:45:10 +00002209 /* XXX: tstate must be saved properly */
2210 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00002211 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2212 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2213 err |= __put_user(env->y, &((*grp)[MC_Y]));
2214 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2215 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2216 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2217 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2218 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2219 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2220 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2221 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2222 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2223 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2224 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2225 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2226 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2227 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2228 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002229
bellard459a4012007-11-11 19:45:10 +00002230 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2231 fp = i7 = 0;
2232 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2233 abi_ulong) != 0)
2234 goto do_sigsegv;
2235 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2236 abi_ulong) != 0)
2237 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002238 err |= __put_user(fp, &(mcp->mc_fp));
2239 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002240
bellard459a4012007-11-11 19:45:10 +00002241 {
2242 uint32_t *src, *dst;
2243 src = env->fpr;
2244 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2245 /* XXX: check that the CPU storage is the same as user context */
2246 for (i = 0; i < 64; i++, dst++, src++)
2247 err |= __put_user(*src, dst);
2248 }
bellard579a97f2007-11-11 14:26:47 +00002249 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2250 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2251 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002252
2253 if (err)
2254 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002255 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002256 return;
2257 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002258 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002259 force_sig(SIGSEGV);
2260}
2261#endif
thsd26bc212007-11-08 18:05:37 +00002262#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002263
2264# warning signal handling not implemented
2265
pbrook624f7972008-05-31 16:11:38 +00002266static void setup_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002267 target_sigset_t *set, CPUState *env)
2268{
2269 fprintf(stderr, "setup_frame: not implemented\n");
2270}
2271
pbrook624f7972008-05-31 16:11:38 +00002272static void setup_rt_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002273 target_siginfo_t *info,
2274 target_sigset_t *set, CPUState *env)
2275{
2276 fprintf(stderr, "setup_rt_frame: not implemented\n");
2277}
2278
2279long do_sigreturn(CPUState *env)
2280{
2281 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002282 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002283}
2284
2285long do_rt_sigreturn(CPUState *env)
2286{
2287 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002288 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002289}
2290
thsd26bc212007-11-08 18:05:37 +00002291#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002292
2293# warning signal handling not implemented
2294
pbrook624f7972008-05-31 16:11:38 +00002295static void setup_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002296 target_sigset_t *set, CPUState *env)
2297{
2298 fprintf(stderr, "setup_frame: not implemented\n");
2299}
2300
pbrook624f7972008-05-31 16:11:38 +00002301static void setup_rt_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002302 target_siginfo_t *info,
2303 target_sigset_t *set, CPUState *env)
2304{
2305 fprintf(stderr, "setup_rt_frame: not implemented\n");
2306}
2307
2308long do_sigreturn(CPUState *env)
2309{
2310 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002311 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002312}
2313
2314long do_rt_sigreturn(CPUState *env)
2315{
2316 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002317 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002318}
2319
thsd26bc212007-11-08 18:05:37 +00002320#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002321
2322struct target_sigcontext {
2323 uint32_t sc_regmask; /* Unused */
2324 uint32_t sc_status;
2325 uint64_t sc_pc;
2326 uint64_t sc_regs[32];
2327 uint64_t sc_fpregs[32];
2328 uint32_t sc_ownedfp; /* Unused */
2329 uint32_t sc_fpc_csr;
2330 uint32_t sc_fpc_eir; /* Unused */
2331 uint32_t sc_used_math;
2332 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
Paul Brook94c54952009-07-09 18:40:15 +01002333 uint32_t pad0;
bellard106ec872006-06-27 21:08:10 +00002334 uint64_t sc_mdhi;
2335 uint64_t sc_mdlo;
2336 target_ulong sc_hi1; /* Was sc_cause */
2337 target_ulong sc_lo1; /* Was sc_badvaddr */
2338 target_ulong sc_hi2; /* Was sc_sigset[4] */
2339 target_ulong sc_lo2;
2340 target_ulong sc_hi3;
2341 target_ulong sc_lo3;
2342};
2343
2344struct sigframe {
2345 uint32_t sf_ass[4]; /* argument save space for o32 */
2346 uint32_t sf_code[2]; /* signal trampoline */
2347 struct target_sigcontext sf_sc;
2348 target_sigset_t sf_mask;
2349};
2350
pbrook0b1bcb02009-04-21 01:41:10 +00002351struct target_ucontext {
2352 target_ulong uc_flags;
2353 target_ulong uc_link;
2354 target_stack_t uc_stack;
Paul Brook94c54952009-07-09 18:40:15 +01002355 target_ulong pad0;
pbrook0b1bcb02009-04-21 01:41:10 +00002356 struct target_sigcontext uc_mcontext;
2357 target_sigset_t uc_sigmask;
2358};
2359
2360struct target_rt_sigframe {
2361 uint32_t rs_ass[4]; /* argument save space for o32 */
2362 uint32_t rs_code[2]; /* signal trampoline */
2363 struct target_siginfo rs_info;
2364 struct target_ucontext rs_uc;
2365};
2366
bellard106ec872006-06-27 21:08:10 +00002367/* Install trampoline to jump back from signal handler */
2368static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2369{
2370 int err;
2371
2372 /*
2373 * Set up the return code ...
2374 *
2375 * li v0, __NR__foo_sigreturn
2376 * syscall
2377 */
2378
2379 err = __put_user(0x24020000 + syscall, tramp + 0);
2380 err |= __put_user(0x0000000c , tramp + 1);
2381 /* flush_cache_sigtramp((unsigned long) tramp); */
2382 return err;
2383}
2384
2385static inline int
2386setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2387{
2388 int err = 0;
2389
thsb5dc7732008-06-27 10:02:35 +00002390 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002391
thsb5dc7732008-06-27 10:02:35 +00002392#define save_gp_reg(i) do { \
2393 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002394 } while(0)
2395 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2396 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2397 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2398 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2399 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2400 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2401 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2402 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2403 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002404#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002405
thsb5dc7732008-06-27 10:02:35 +00002406 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2407 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002408
2409 /* Not used yet, but might be useful if we ever have DSP suppport */
2410#if 0
2411 if (cpu_has_dsp) {
2412 err |= __put_user(mfhi1(), &sc->sc_hi1);
2413 err |= __put_user(mflo1(), &sc->sc_lo1);
2414 err |= __put_user(mfhi2(), &sc->sc_hi2);
2415 err |= __put_user(mflo2(), &sc->sc_lo2);
2416 err |= __put_user(mfhi3(), &sc->sc_hi3);
2417 err |= __put_user(mflo3(), &sc->sc_lo3);
2418 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2419 }
2420 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002421#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002422 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2423 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2424 if (cpu_has_dsp) {
2425 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2426 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2427 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2428 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2429 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2430 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2431 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2432 }
ths388bb212007-05-13 13:58:00 +00002433#endif
2434#endif
bellard106ec872006-06-27 21:08:10 +00002435
ths388bb212007-05-13 13:58:00 +00002436#if 0
bellard106ec872006-06-27 21:08:10 +00002437 err |= __put_user(!!used_math(), &sc->sc_used_math);
2438
2439 if (!used_math())
2440 goto out;
2441
2442 /*
2443 * Save FPU state to signal context. Signal handler will "inherit"
2444 * current FPU state.
2445 */
2446 preempt_disable();
2447
2448 if (!is_fpu_owner()) {
2449 own_fpu();
2450 restore_fp(current);
2451 }
2452 err |= save_fp_context(sc);
2453
2454 preempt_enable();
2455 out:
2456#endif
2457 return err;
2458}
2459
2460static inline int
2461restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2462{
2463 int err = 0;
2464
2465 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2466
thsb5dc7732008-06-27 10:02:35 +00002467 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2468 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002469
thsead93602007-09-06 00:18:15 +00002470#define restore_gp_reg(i) do { \
thsb5dc7732008-06-27 10:02:35 +00002471 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002472 } while(0)
2473 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2474 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2475 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2476 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2477 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2478 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2479 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2480 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2481 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2482 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2483 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002484#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002485
2486#if 0
2487 if (cpu_has_dsp) {
2488 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2489 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2490 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2491 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2492 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2493 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2494 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2495 }
ths388bb212007-05-13 13:58:00 +00002496#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002497 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2498 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2499 if (cpu_has_dsp) {
2500 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2501 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2502 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2503 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2504 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2505 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2506 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2507 }
ths388bb212007-05-13 13:58:00 +00002508#endif
bellard106ec872006-06-27 21:08:10 +00002509
2510 err |= __get_user(used_math, &sc->sc_used_math);
2511 conditional_used_math(used_math);
2512
2513 preempt_disable();
2514
2515 if (used_math()) {
2516 /* restore fpu context if we have used it before */
2517 own_fpu();
2518 err |= restore_fp_context(sc);
2519 } else {
2520 /* signal handler may have used FPU. Give it up. */
2521 lose_fpu();
2522 }
2523
2524 preempt_enable();
2525#endif
2526 return err;
2527}
2528/*
2529 * Determine which stack to use..
2530 */
bellard579a97f2007-11-11 14:26:47 +00002531static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00002532get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
bellard106ec872006-06-27 21:08:10 +00002533{
2534 unsigned long sp;
2535
2536 /* Default to using normal stack */
thsb5dc7732008-06-27 10:02:35 +00002537 sp = regs->active_tc.gpr[29];
bellard106ec872006-06-27 21:08:10 +00002538
2539 /*
2540 * FPU emulator may have it's own trampoline active just
2541 * above the user stack, 16-bytes before the next lowest
2542 * 16 byte boundary. Try to avoid trashing it.
2543 */
2544 sp -= 32;
2545
bellard106ec872006-06-27 21:08:10 +00002546 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002547 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
thsa04e1342007-09-27 13:57:58 +00002548 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2549 }
bellard106ec872006-06-27 21:08:10 +00002550
bellard579a97f2007-11-11 14:26:47 +00002551 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002552}
2553
bellard579a97f2007-11-11 14:26:47 +00002554/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00002555static void setup_frame(int sig, struct target_sigaction * ka,
bellard579a97f2007-11-11 14:26:47 +00002556 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002557{
2558 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002559 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002560 int i;
2561
bellard579a97f2007-11-11 14:26:47 +00002562 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2563 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002564 goto give_sigsegv;
2565
2566 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2567
2568 if(setup_sigcontext(regs, &frame->sf_sc))
2569 goto give_sigsegv;
2570
2571 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2572 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2573 goto give_sigsegv;
2574 }
2575
2576 /*
2577 * Arguments to signal handler:
2578 *
2579 * a0 = signal number
2580 * a1 = 0 (should be cause)
2581 * a2 = pointer to struct sigcontext
2582 *
2583 * $25 and PC point to the signal handler, $29 points to the
2584 * struct sigframe.
2585 */
thsb5dc7732008-06-27 10:02:35 +00002586 regs->active_tc.gpr[ 4] = sig;
2587 regs->active_tc.gpr[ 5] = 0;
2588 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2589 regs->active_tc.gpr[29] = frame_addr;
2590 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002591 /* The original kernel code sets CP0_EPC to the handler
2592 * since it returns to userland using eret
2593 * we cannot do this here, and we must set PC directly */
thsb5dc7732008-06-27 10:02:35 +00002594 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002595 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002596 return;
2597
2598give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002599 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002600 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002601 return;
bellard106ec872006-06-27 21:08:10 +00002602}
2603
2604long do_sigreturn(CPUState *regs)
2605{
ths388bb212007-05-13 13:58:00 +00002606 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002607 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002608 sigset_t blocked;
2609 target_sigset_t target_set;
2610 int i;
bellard106ec872006-06-27 21:08:10 +00002611
2612#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002613 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002614#endif
thsb5dc7732008-06-27 10:02:35 +00002615 frame_addr = regs->active_tc.gpr[29];
bellard579a97f2007-11-11 14:26:47 +00002616 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002617 goto badframe;
2618
ths388bb212007-05-13 13:58:00 +00002619 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002620 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2621 goto badframe;
ths388bb212007-05-13 13:58:00 +00002622 }
bellard106ec872006-06-27 21:08:10 +00002623
ths388bb212007-05-13 13:58:00 +00002624 target_to_host_sigset_internal(&blocked, &target_set);
2625 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002626
ths388bb212007-05-13 13:58:00 +00002627 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002628 goto badframe;
2629
2630#if 0
ths388bb212007-05-13 13:58:00 +00002631 /*
2632 * Don't let your children do this ...
2633 */
2634 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002635 "move\t$29, %0\n\t"
2636 "j\tsyscall_exit"
2637 :/* no outputs */
2638 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002639 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002640#endif
ths3b46e622007-09-17 08:09:54 +00002641
thsb5dc7732008-06-27 10:02:35 +00002642 regs->active_tc.PC = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002643 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002644 * maybe a problem with nested signals ? */
2645 regs->CP0_EPC = 0;
pbrook0b1bcb02009-04-21 01:41:10 +00002646 return -TARGET_QEMU_ESIGRETURN;
bellard106ec872006-06-27 21:08:10 +00002647
2648badframe:
ths388bb212007-05-13 13:58:00 +00002649 force_sig(TARGET_SIGSEGV/*, current*/);
2650 return 0;
bellard106ec872006-06-27 21:08:10 +00002651}
2652
pbrook624f7972008-05-31 16:11:38 +00002653static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellard106ec872006-06-27 21:08:10 +00002654 target_siginfo_t *info,
2655 target_sigset_t *set, CPUState *env)
2656{
pbrook0b1bcb02009-04-21 01:41:10 +00002657 struct target_rt_sigframe *frame;
2658 abi_ulong frame_addr;
2659 int i;
2660
2661 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2662 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2663 goto give_sigsegv;
2664
2665 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2666
2667 copy_siginfo_to_user(&frame->rs_info, info);
2668
2669 __put_user(0, &frame->rs_uc.uc_flags);
2670 __put_user(0, &frame->rs_uc.uc_link);
2671 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.uc_stack.ss_sp);
2672 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.uc_stack.ss_size);
2673 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2674 &frame->rs_uc.uc_stack.ss_flags);
2675
2676 setup_sigcontext(env, &frame->rs_uc.uc_mcontext);
2677
2678 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2679 __put_user(set->sig[i], &frame->rs_uc.uc_sigmask.sig[i]);
2680 }
2681
2682 /*
2683 * Arguments to signal handler:
2684 *
2685 * a0 = signal number
2686 * a1 = pointer to struct siginfo
2687 * a2 = pointer to struct ucontext
2688 *
2689 * $25 and PC point to the signal handler, $29 points to the
2690 * struct sigframe.
2691 */
2692 env->active_tc.gpr[ 4] = sig;
2693 env->active_tc.gpr[ 5] = frame_addr
2694 + offsetof(struct target_rt_sigframe, rs_info);
2695 env->active_tc.gpr[ 6] = frame_addr
2696 + offsetof(struct target_rt_sigframe, rs_uc);
2697 env->active_tc.gpr[29] = frame_addr;
2698 env->active_tc.gpr[31] = frame_addr
2699 + offsetof(struct target_rt_sigframe, rs_code);
2700 /* The original kernel code sets CP0_EPC to the handler
2701 * since it returns to userland using eret
2702 * we cannot do this here, and we must set PC directly */
2703 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2704 unlock_user_struct(frame, frame_addr, 1);
2705 return;
2706
2707give_sigsegv:
2708 unlock_user_struct(frame, frame_addr, 1);
2709 force_sig(TARGET_SIGSEGV/*, current*/);
2710 return;
bellard106ec872006-06-27 21:08:10 +00002711}
2712
2713long do_rt_sigreturn(CPUState *env)
2714{
pbrook0b1bcb02009-04-21 01:41:10 +00002715 struct target_rt_sigframe *frame;
2716 abi_ulong frame_addr;
2717 sigset_t blocked;
2718
2719#if defined(DEBUG_SIGNAL)
2720 fprintf(stderr, "do_rt_sigreturn\n");
2721#endif
2722 frame_addr = env->active_tc.gpr[29];
2723 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2724 goto badframe;
2725
2726 target_to_host_sigset(&blocked, &frame->rs_uc.uc_sigmask);
2727 sigprocmask(SIG_SETMASK, &blocked, NULL);
2728
2729 if (restore_sigcontext(env, &frame->rs_uc.uc_mcontext))
2730 goto badframe;
2731
2732 if (do_sigaltstack(frame_addr +
2733 offsetof(struct target_rt_sigframe, rs_uc.uc_stack),
2734 0, get_sp_from_cpustate(env)) == -EFAULT)
2735 goto badframe;
2736
2737 env->active_tc.PC = env->CP0_EPC;
2738 /* I am not sure this is right, but it seems to work
2739 * maybe a problem with nested signals ? */
2740 env->CP0_EPC = 0;
2741 return -TARGET_QEMU_ESIGRETURN;
2742
2743badframe:
2744 force_sig(TARGET_SIGSEGV/*, current*/);
2745 return 0;
bellard106ec872006-06-27 21:08:10 +00002746}
bellard6d5e2162004-09-30 22:04:13 +00002747
thsc3b5bc82007-12-02 06:31:25 +00002748#elif defined(TARGET_SH4)
2749
2750/*
2751 * code and data structures from linux kernel:
2752 * include/asm-sh/sigcontext.h
2753 * arch/sh/kernel/signal.c
2754 */
2755
2756struct target_sigcontext {
2757 target_ulong oldmask;
2758
2759 /* CPU registers */
2760 target_ulong sc_gregs[16];
2761 target_ulong sc_pc;
2762 target_ulong sc_pr;
2763 target_ulong sc_sr;
2764 target_ulong sc_gbr;
2765 target_ulong sc_mach;
2766 target_ulong sc_macl;
2767
2768 /* FPU registers */
2769 target_ulong sc_fpregs[16];
2770 target_ulong sc_xfpregs[16];
2771 unsigned int sc_fpscr;
2772 unsigned int sc_fpul;
2773 unsigned int sc_ownedfp;
2774};
2775
2776struct target_sigframe
2777{
2778 struct target_sigcontext sc;
2779 target_ulong extramask[TARGET_NSIG_WORDS-1];
2780 uint16_t retcode[3];
2781};
2782
2783
2784struct target_ucontext {
2785 target_ulong uc_flags;
2786 struct target_ucontext *uc_link;
2787 target_stack_t uc_stack;
2788 struct target_sigcontext uc_mcontext;
2789 target_sigset_t uc_sigmask; /* mask last for extensibility */
2790};
2791
2792struct target_rt_sigframe
2793{
2794 struct target_siginfo info;
2795 struct target_ucontext uc;
2796 uint16_t retcode[3];
2797};
2798
2799
2800#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2801#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2802
pbrook624f7972008-05-31 16:11:38 +00002803static abi_ulong get_sigframe(struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002804 unsigned long sp, size_t frame_size)
2805{
pbrook624f7972008-05-31 16:11:38 +00002806 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
thsc3b5bc82007-12-02 06:31:25 +00002807 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2808 }
2809
2810 return (sp - frame_size) & -8ul;
2811}
2812
2813static int setup_sigcontext(struct target_sigcontext *sc,
2814 CPUState *regs, unsigned long mask)
2815{
2816 int err = 0;
2817
2818#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2819 COPY(gregs[0]); COPY(gregs[1]);
2820 COPY(gregs[2]); COPY(gregs[3]);
2821 COPY(gregs[4]); COPY(gregs[5]);
2822 COPY(gregs[6]); COPY(gregs[7]);
2823 COPY(gregs[8]); COPY(gregs[9]);
2824 COPY(gregs[10]); COPY(gregs[11]);
2825 COPY(gregs[12]); COPY(gregs[13]);
2826 COPY(gregs[14]); COPY(gregs[15]);
2827 COPY(gbr); COPY(mach);
2828 COPY(macl); COPY(pr);
2829 COPY(sr); COPY(pc);
2830#undef COPY
2831
2832 /* todo: save FPU registers here */
2833
2834 /* non-iBCS2 extensions.. */
2835 err |= __put_user(mask, &sc->oldmask);
2836
2837 return err;
2838}
2839
pbrookc2764712009-03-07 15:24:59 +00002840static int restore_sigcontext(CPUState *regs,
thsc3b5bc82007-12-02 06:31:25 +00002841 struct target_sigcontext *sc)
2842{
2843 unsigned int err = 0;
2844
2845#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2846 COPY(gregs[1]);
2847 COPY(gregs[2]); COPY(gregs[3]);
2848 COPY(gregs[4]); COPY(gregs[5]);
2849 COPY(gregs[6]); COPY(gregs[7]);
2850 COPY(gregs[8]); COPY(gregs[9]);
2851 COPY(gregs[10]); COPY(gregs[11]);
2852 COPY(gregs[12]); COPY(gregs[13]);
2853 COPY(gregs[14]); COPY(gregs[15]);
2854 COPY(gbr); COPY(mach);
2855 COPY(macl); COPY(pr);
2856 COPY(sr); COPY(pc);
2857#undef COPY
2858
2859 /* todo: restore FPU registers here */
2860
2861 regs->tra = -1; /* disable syscall checks */
2862 return err;
2863}
2864
pbrook624f7972008-05-31 16:11:38 +00002865static void setup_frame(int sig, struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002866 target_sigset_t *set, CPUState *regs)
2867{
2868 struct target_sigframe *frame;
2869 abi_ulong frame_addr;
2870 int i;
2871 int err = 0;
2872 int signal;
2873
2874 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2875 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2876 goto give_sigsegv;
2877
2878 signal = current_exec_domain_sig(sig);
2879
2880 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2881
2882 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2883 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2884 }
2885
2886 /* Set up to return from userspace. If provided, use a stub
2887 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002888 if (ka->sa_flags & TARGET_SA_RESTORER) {
2889 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002890 } else {
2891 /* Generate return code (system call to sigreturn) */
2892 err |= __put_user(MOVW(2), &frame->retcode[0]);
2893 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2894 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2895 regs->pr = (unsigned long) frame->retcode;
2896 }
2897
2898 if (err)
2899 goto give_sigsegv;
2900
2901 /* Set up registers for signal handler */
2902 regs->gregs[15] = (unsigned long) frame;
2903 regs->gregs[4] = signal; /* Arg for signal handler */
2904 regs->gregs[5] = 0;
2905 regs->gregs[6] = (unsigned long) &frame->sc;
pbrook624f7972008-05-31 16:11:38 +00002906 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002907
2908 unlock_user_struct(frame, frame_addr, 1);
2909 return;
2910
2911give_sigsegv:
2912 unlock_user_struct(frame, frame_addr, 1);
2913 force_sig(SIGSEGV);
2914}
2915
pbrook624f7972008-05-31 16:11:38 +00002916static void setup_rt_frame(int sig, struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002917 target_siginfo_t *info,
2918 target_sigset_t *set, CPUState *regs)
2919{
2920 struct target_rt_sigframe *frame;
2921 abi_ulong frame_addr;
2922 int i;
2923 int err = 0;
2924 int signal;
2925
2926 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2927 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2928 goto give_sigsegv;
2929
2930 signal = current_exec_domain_sig(sig);
2931
2932 err |= copy_siginfo_to_user(&frame->info, info);
2933
2934 /* Create the ucontext. */
2935 err |= __put_user(0, &frame->uc.uc_flags);
2936 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
balrog526ccb72008-07-16 12:13:52 +00002937 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
thsc3b5bc82007-12-02 06:31:25 +00002938 &frame->uc.uc_stack.ss_sp);
2939 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2940 &frame->uc.uc_stack.ss_flags);
2941 err |= __put_user(target_sigaltstack_used.ss_size,
2942 &frame->uc.uc_stack.ss_size);
2943 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2944 regs, set->sig[0]);
2945 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2946 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2947 }
2948
2949 /* Set up to return from userspace. If provided, use a stub
2950 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002951 if (ka->sa_flags & TARGET_SA_RESTORER) {
2952 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002953 } else {
2954 /* Generate return code (system call to sigreturn) */
2955 err |= __put_user(MOVW(2), &frame->retcode[0]);
2956 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2957 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2958 regs->pr = (unsigned long) frame->retcode;
2959 }
2960
2961 if (err)
2962 goto give_sigsegv;
2963
2964 /* Set up registers for signal handler */
2965 regs->gregs[15] = (unsigned long) frame;
2966 regs->gregs[4] = signal; /* Arg for signal handler */
2967 regs->gregs[5] = (unsigned long) &frame->info;
2968 regs->gregs[6] = (unsigned long) &frame->uc;
pbrook624f7972008-05-31 16:11:38 +00002969 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002970
2971 unlock_user_struct(frame, frame_addr, 1);
2972 return;
2973
2974give_sigsegv:
2975 unlock_user_struct(frame, frame_addr, 1);
2976 force_sig(SIGSEGV);
2977}
2978
2979long do_sigreturn(CPUState *regs)
2980{
2981 struct target_sigframe *frame;
2982 abi_ulong frame_addr;
2983 sigset_t blocked;
2984 target_sigset_t target_set;
2985 int i;
2986 int err = 0;
2987
2988#if defined(DEBUG_SIGNAL)
2989 fprintf(stderr, "do_sigreturn\n");
2990#endif
2991 frame_addr = regs->gregs[15];
2992 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2993 goto badframe;
2994
2995 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2996 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2997 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2998 }
2999
3000 if (err)
3001 goto badframe;
3002
3003 target_to_host_sigset_internal(&blocked, &target_set);
3004 sigprocmask(SIG_SETMASK, &blocked, NULL);
3005
3006 if (restore_sigcontext(regs, &frame->sc))
3007 goto badframe;
3008
3009 unlock_user_struct(frame, frame_addr, 0);
3010 return regs->gregs[0];
3011
3012badframe:
3013 unlock_user_struct(frame, frame_addr, 0);
3014 force_sig(TARGET_SIGSEGV);
3015 return 0;
3016}
3017
3018long do_rt_sigreturn(CPUState *regs)
3019{
3020 struct target_rt_sigframe *frame;
3021 abi_ulong frame_addr;
3022 sigset_t blocked;
3023
3024#if defined(DEBUG_SIGNAL)
3025 fprintf(stderr, "do_rt_sigreturn\n");
3026#endif
3027 frame_addr = regs->gregs[15];
3028 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3029 goto badframe;
3030
3031 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
3032 sigprocmask(SIG_SETMASK, &blocked, NULL);
3033
3034 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
3035 goto badframe;
3036
3037 if (do_sigaltstack(frame_addr +
3038 offsetof(struct target_rt_sigframe, uc.uc_stack),
3039 0, get_sp_from_cpustate(regs)) == -EFAULT)
3040 goto badframe;
3041
3042 unlock_user_struct(frame, frame_addr, 0);
3043 return regs->gregs[0];
3044
3045badframe:
3046 unlock_user_struct(frame, frame_addr, 0);
3047 force_sig(TARGET_SIGSEGV);
3048 return 0;
3049}
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003050#elif defined(TARGET_MICROBLAZE)
3051
3052struct target_sigcontext {
3053 struct target_pt_regs regs; /* needs to be first */
3054 uint32_t oldmask;
3055};
3056
3057/* Signal frames. */
3058struct target_signal_frame {
3059 struct target_sigcontext sc;
3060 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3061 uint32_t tramp[2];
3062};
3063
3064struct rt_signal_frame {
3065 struct siginfo info;
3066 struct ucontext uc;
3067 uint32_t tramp[2];
3068};
3069
3070static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3071{
3072 __put_user(env->regs[0], &sc->regs.r0);
3073 __put_user(env->regs[1], &sc->regs.r1);
3074 __put_user(env->regs[2], &sc->regs.r2);
3075 __put_user(env->regs[3], &sc->regs.r3);
3076 __put_user(env->regs[4], &sc->regs.r4);
3077 __put_user(env->regs[5], &sc->regs.r5);
3078 __put_user(env->regs[6], &sc->regs.r6);
3079 __put_user(env->regs[7], &sc->regs.r7);
3080 __put_user(env->regs[8], &sc->regs.r8);
3081 __put_user(env->regs[9], &sc->regs.r9);
3082 __put_user(env->regs[10], &sc->regs.r10);
3083 __put_user(env->regs[11], &sc->regs.r11);
3084 __put_user(env->regs[12], &sc->regs.r12);
3085 __put_user(env->regs[13], &sc->regs.r13);
3086 __put_user(env->regs[14], &sc->regs.r14);
3087 __put_user(env->regs[15], &sc->regs.r15);
3088 __put_user(env->regs[16], &sc->regs.r16);
3089 __put_user(env->regs[17], &sc->regs.r17);
3090 __put_user(env->regs[18], &sc->regs.r18);
3091 __put_user(env->regs[19], &sc->regs.r19);
3092 __put_user(env->regs[20], &sc->regs.r20);
3093 __put_user(env->regs[21], &sc->regs.r21);
3094 __put_user(env->regs[22], &sc->regs.r22);
3095 __put_user(env->regs[23], &sc->regs.r23);
3096 __put_user(env->regs[24], &sc->regs.r24);
3097 __put_user(env->regs[25], &sc->regs.r25);
3098 __put_user(env->regs[26], &sc->regs.r26);
3099 __put_user(env->regs[27], &sc->regs.r27);
3100 __put_user(env->regs[28], &sc->regs.r28);
3101 __put_user(env->regs[29], &sc->regs.r29);
3102 __put_user(env->regs[30], &sc->regs.r30);
3103 __put_user(env->regs[31], &sc->regs.r31);
3104 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3105}
3106
3107static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3108{
3109 __get_user(env->regs[0], &sc->regs.r0);
3110 __get_user(env->regs[1], &sc->regs.r1);
3111 __get_user(env->regs[2], &sc->regs.r2);
3112 __get_user(env->regs[3], &sc->regs.r3);
3113 __get_user(env->regs[4], &sc->regs.r4);
3114 __get_user(env->regs[5], &sc->regs.r5);
3115 __get_user(env->regs[6], &sc->regs.r6);
3116 __get_user(env->regs[7], &sc->regs.r7);
3117 __get_user(env->regs[8], &sc->regs.r8);
3118 __get_user(env->regs[9], &sc->regs.r9);
3119 __get_user(env->regs[10], &sc->regs.r10);
3120 __get_user(env->regs[11], &sc->regs.r11);
3121 __get_user(env->regs[12], &sc->regs.r12);
3122 __get_user(env->regs[13], &sc->regs.r13);
3123 __get_user(env->regs[14], &sc->regs.r14);
3124 __get_user(env->regs[15], &sc->regs.r15);
3125 __get_user(env->regs[16], &sc->regs.r16);
3126 __get_user(env->regs[17], &sc->regs.r17);
3127 __get_user(env->regs[18], &sc->regs.r18);
3128 __get_user(env->regs[19], &sc->regs.r19);
3129 __get_user(env->regs[20], &sc->regs.r20);
3130 __get_user(env->regs[21], &sc->regs.r21);
3131 __get_user(env->regs[22], &sc->regs.r22);
3132 __get_user(env->regs[23], &sc->regs.r23);
3133 __get_user(env->regs[24], &sc->regs.r24);
3134 __get_user(env->regs[25], &sc->regs.r25);
3135 __get_user(env->regs[26], &sc->regs.r26);
3136 __get_user(env->regs[27], &sc->regs.r27);
3137 __get_user(env->regs[28], &sc->regs.r28);
3138 __get_user(env->regs[29], &sc->regs.r29);
3139 __get_user(env->regs[30], &sc->regs.r30);
3140 __get_user(env->regs[31], &sc->regs.r31);
3141 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3142}
3143
3144static abi_ulong get_sigframe(struct target_sigaction *ka,
3145 CPUState *env, int frame_size)
3146{
3147 abi_ulong sp = env->regs[1];
3148
3149 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3150 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3151
3152 return ((sp - frame_size) & -8UL);
3153}
3154
3155static void setup_frame(int sig, struct target_sigaction *ka,
3156 target_sigset_t *set, CPUState *env)
3157{
3158 struct target_signal_frame *frame;
3159 abi_ulong frame_addr;
3160 int err = 0;
3161 int i;
3162
3163 frame_addr = get_sigframe(ka, env, sizeof *frame);
3164 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3165 goto badframe;
3166
3167 /* Save the mask. */
3168 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3169 if (err)
3170 goto badframe;
3171
3172 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3173 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3174 goto badframe;
3175 }
3176
3177 setup_sigcontext(&frame->sc, env);
3178
3179 /* Set up to return from userspace. If provided, use a stub
3180 already in userspace. */
3181 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3182 if (ka->sa_flags & TARGET_SA_RESTORER) {
3183 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3184 } else {
3185 uint32_t t;
3186 /* Note, these encodings are _big endian_! */
3187 /* addi r12, r0, __NR_sigreturn */
3188 t = 0x31800000UL | TARGET_NR_sigreturn;
3189 err |= __put_user(t, frame->tramp + 0);
3190 /* brki r14, 0x8 */
3191 t = 0xb9cc0008UL;
3192 err |= __put_user(t, frame->tramp + 1);
3193
3194 /* Return from sighandler will jump to the tramp.
3195 Negative 8 offset because return is rtsd r15, 8 */
3196 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3197 }
3198
3199 if (err)
3200 goto badframe;
3201
3202 /* Set up registers for signal handler */
3203 env->regs[1] = (unsigned long) frame;
3204 /* Signal handler args: */
3205 env->regs[5] = sig; /* Arg 0: signum */
3206 env->regs[6] = (unsigned long) &frame->sc; /* arg 1: sigcontext */
3207
3208 /* Offset of 4 to handle microblaze rtid r14, 0 */
3209 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3210
3211 unlock_user_struct(frame, frame_addr, 1);
3212 return;
3213 badframe:
3214 unlock_user_struct(frame, frame_addr, 1);
3215 force_sig(TARGET_SIGSEGV);
3216}
3217
3218static void setup_rt_frame(int sig, struct target_sigaction *ka,
3219 target_siginfo_t *info,
3220 target_sigset_t *set, CPUState *env)
3221{
3222 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3223}
3224
3225long do_sigreturn(CPUState *env)
3226{
3227 struct target_signal_frame *frame;
3228 abi_ulong frame_addr;
3229 target_sigset_t target_set;
3230 sigset_t set;
3231 int i;
3232
3233 frame_addr = env->regs[R_SP];
3234 /* Make sure the guest isn't playing games. */
3235 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3236 goto badframe;
3237
3238 /* Restore blocked signals */
3239 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3240 goto badframe;
3241 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3242 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3243 goto badframe;
3244 }
3245 target_to_host_sigset_internal(&set, &target_set);
3246 sigprocmask(SIG_SETMASK, &set, NULL);
3247
3248 restore_sigcontext(&frame->sc, env);
3249 /* We got here through a sigreturn syscall, our path back is via an
3250 rtb insn so setup r14 for that. */
3251 env->regs[14] = env->sregs[SR_PC];
3252
3253 unlock_user_struct(frame, frame_addr, 0);
3254 return env->regs[10];
3255 badframe:
3256 unlock_user_struct(frame, frame_addr, 0);
3257 force_sig(TARGET_SIGSEGV);
3258}
3259
3260long do_rt_sigreturn(CPUState *env)
3261{
3262 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3263 return -TARGET_ENOSYS;
3264}
3265
edgar_iglb6d3abd2008-02-28 11:29:27 +00003266#elif defined(TARGET_CRIS)
3267
3268struct target_sigcontext {
3269 struct target_pt_regs regs; /* needs to be first */
3270 uint32_t oldmask;
3271 uint32_t usp; /* usp before stacking this gunk on it */
3272};
3273
3274/* Signal frames. */
3275struct target_signal_frame {
3276 struct target_sigcontext sc;
3277 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3278 uint8_t retcode[8]; /* Trampoline code. */
3279};
3280
3281struct rt_signal_frame {
3282 struct siginfo *pinfo;
3283 void *puc;
3284 struct siginfo info;
3285 struct ucontext uc;
3286 uint8_t retcode[8]; /* Trampoline code. */
3287};
3288
3289static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3290{
edgar_igl9664d922008-03-03 22:23:53 +00003291 __put_user(env->regs[0], &sc->regs.r0);
3292 __put_user(env->regs[1], &sc->regs.r1);
3293 __put_user(env->regs[2], &sc->regs.r2);
3294 __put_user(env->regs[3], &sc->regs.r3);
3295 __put_user(env->regs[4], &sc->regs.r4);
3296 __put_user(env->regs[5], &sc->regs.r5);
3297 __put_user(env->regs[6], &sc->regs.r6);
3298 __put_user(env->regs[7], &sc->regs.r7);
3299 __put_user(env->regs[8], &sc->regs.r8);
3300 __put_user(env->regs[9], &sc->regs.r9);
3301 __put_user(env->regs[10], &sc->regs.r10);
3302 __put_user(env->regs[11], &sc->regs.r11);
3303 __put_user(env->regs[12], &sc->regs.r12);
3304 __put_user(env->regs[13], &sc->regs.r13);
3305 __put_user(env->regs[14], &sc->usp);
3306 __put_user(env->regs[15], &sc->regs.acr);
3307 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3308 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3309 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003310}
edgar_igl9664d922008-03-03 22:23:53 +00003311
edgar_iglb6d3abd2008-02-28 11:29:27 +00003312static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3313{
edgar_igl9664d922008-03-03 22:23:53 +00003314 __get_user(env->regs[0], &sc->regs.r0);
3315 __get_user(env->regs[1], &sc->regs.r1);
3316 __get_user(env->regs[2], &sc->regs.r2);
3317 __get_user(env->regs[3], &sc->regs.r3);
3318 __get_user(env->regs[4], &sc->regs.r4);
3319 __get_user(env->regs[5], &sc->regs.r5);
3320 __get_user(env->regs[6], &sc->regs.r6);
3321 __get_user(env->regs[7], &sc->regs.r7);
3322 __get_user(env->regs[8], &sc->regs.r8);
3323 __get_user(env->regs[9], &sc->regs.r9);
3324 __get_user(env->regs[10], &sc->regs.r10);
3325 __get_user(env->regs[11], &sc->regs.r11);
3326 __get_user(env->regs[12], &sc->regs.r12);
3327 __get_user(env->regs[13], &sc->regs.r13);
3328 __get_user(env->regs[14], &sc->usp);
3329 __get_user(env->regs[15], &sc->regs.acr);
3330 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3331 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3332 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003333}
3334
edgar_igl9664d922008-03-03 22:23:53 +00003335static abi_ulong get_sigframe(CPUState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003336{
edgar_igl9664d922008-03-03 22:23:53 +00003337 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003338 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00003339 sp = (env->regs[R_SP] & ~3);
3340 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003341}
3342
pbrook624f7972008-05-31 16:11:38 +00003343static void setup_frame(int sig, struct target_sigaction *ka,
edgar_iglb6d3abd2008-02-28 11:29:27 +00003344 target_sigset_t *set, CPUState *env)
3345{
3346 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003347 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003348 int err = 0;
3349 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003350
edgar_igl9664d922008-03-03 22:23:53 +00003351 frame_addr = get_sigframe(env, sizeof *frame);
3352 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003353 goto badframe;
3354
3355 /*
3356 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3357 * use this trampoline anymore but it sets it up for GDB.
3358 * In QEMU, using the trampoline simplifies things a bit so we use it.
3359 *
3360 * This is movu.w __NR_sigreturn, r9; break 13;
3361 */
3362 err |= __put_user(0x9c5f, frame->retcode+0);
3363 err |= __put_user(TARGET_NR_sigreturn,
3364 frame->retcode+2);
3365 err |= __put_user(0xe93d, frame->retcode+4);
3366
3367 /* Save the mask. */
3368 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3369 if (err)
3370 goto badframe;
3371
3372 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3373 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3374 goto badframe;
3375 }
3376
3377 setup_sigcontext(&frame->sc, env);
3378
3379 /* Move the stack and setup the arguments for the handler. */
balrog526ccb72008-07-16 12:13:52 +00003380 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003381 env->regs[10] = sig;
pbrook624f7972008-05-31 16:11:38 +00003382 env->pc = (unsigned long) ka->_sa_handler;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003383 /* Link SRP so the guest returns through the trampoline. */
balrog526ccb72008-07-16 12:13:52 +00003384 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003385
edgar_igl9664d922008-03-03 22:23:53 +00003386 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003387 return;
3388 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003389 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003390 force_sig(TARGET_SIGSEGV);
3391}
3392
pbrook624f7972008-05-31 16:11:38 +00003393static void setup_rt_frame(int sig, struct target_sigaction *ka,
edgar_iglb6d3abd2008-02-28 11:29:27 +00003394 target_siginfo_t *info,
3395 target_sigset_t *set, CPUState *env)
3396{
3397 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3398}
3399
3400long do_sigreturn(CPUState *env)
3401{
3402 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003403 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003404 target_sigset_t target_set;
3405 sigset_t set;
3406 int i;
3407
edgar_igl9664d922008-03-03 22:23:53 +00003408 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003409 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003410 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003411 goto badframe;
3412
3413 /* Restore blocked signals */
3414 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3415 goto badframe;
3416 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3417 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3418 goto badframe;
3419 }
3420 target_to_host_sigset_internal(&set, &target_set);
3421 sigprocmask(SIG_SETMASK, &set, NULL);
3422
3423 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003424 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003425 return env->regs[10];
3426 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003427 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003428 force_sig(TARGET_SIGSEGV);
3429}
3430
3431long do_rt_sigreturn(CPUState *env)
3432{
3433 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3434 return -TARGET_ENOSYS;
3435}
thsc3b5bc82007-12-02 06:31:25 +00003436
Nathan Froydbcd49332009-05-12 19:13:18 -07003437#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
3438
3439/* FIXME: Many of the structures are defined for both PPC and PPC64, but
3440 the signal handling is different enough that we haven't implemented
3441 support for PPC64 yet. Hence the restriction above.
3442
3443 There are various #if'd blocks for code for TARGET_PPC64. These
3444 blocks should go away so that we can successfully run 32-bit and
3445 64-bit binaries on a QEMU configured for PPC64. */
3446
3447/* Size of dummy stack frame allocated when calling signal handler.
3448 See arch/powerpc/include/asm/ptrace.h. */
3449#if defined(TARGET_PPC64)
3450#define SIGNAL_FRAMESIZE 128
3451#else
3452#define SIGNAL_FRAMESIZE 64
3453#endif
3454
3455/* See arch/powerpc/include/asm/sigcontext.h. */
3456struct target_sigcontext {
3457 target_ulong _unused[4];
3458 int32_t signal;
3459#if defined(TARGET_PPC64)
3460 int32_t pad0;
3461#endif
3462 target_ulong handler;
3463 target_ulong oldmask;
3464 target_ulong regs; /* struct pt_regs __user * */
3465 /* TODO: PPC64 includes extra bits here. */
3466};
3467
3468/* Indices for target_mcontext.mc_gregs, below.
3469 See arch/powerpc/include/asm/ptrace.h for details. */
3470enum {
3471 TARGET_PT_R0 = 0,
3472 TARGET_PT_R1 = 1,
3473 TARGET_PT_R2 = 2,
3474 TARGET_PT_R3 = 3,
3475 TARGET_PT_R4 = 4,
3476 TARGET_PT_R5 = 5,
3477 TARGET_PT_R6 = 6,
3478 TARGET_PT_R7 = 7,
3479 TARGET_PT_R8 = 8,
3480 TARGET_PT_R9 = 9,
3481 TARGET_PT_R10 = 10,
3482 TARGET_PT_R11 = 11,
3483 TARGET_PT_R12 = 12,
3484 TARGET_PT_R13 = 13,
3485 TARGET_PT_R14 = 14,
3486 TARGET_PT_R15 = 15,
3487 TARGET_PT_R16 = 16,
3488 TARGET_PT_R17 = 17,
3489 TARGET_PT_R18 = 18,
3490 TARGET_PT_R19 = 19,
3491 TARGET_PT_R20 = 20,
3492 TARGET_PT_R21 = 21,
3493 TARGET_PT_R22 = 22,
3494 TARGET_PT_R23 = 23,
3495 TARGET_PT_R24 = 24,
3496 TARGET_PT_R25 = 25,
3497 TARGET_PT_R26 = 26,
3498 TARGET_PT_R27 = 27,
3499 TARGET_PT_R28 = 28,
3500 TARGET_PT_R29 = 29,
3501 TARGET_PT_R30 = 30,
3502 TARGET_PT_R31 = 31,
3503 TARGET_PT_NIP = 32,
3504 TARGET_PT_MSR = 33,
3505 TARGET_PT_ORIG_R3 = 34,
3506 TARGET_PT_CTR = 35,
3507 TARGET_PT_LNK = 36,
3508 TARGET_PT_XER = 37,
3509 TARGET_PT_CCR = 38,
3510 /* Yes, there are two registers with #39. One is 64-bit only. */
3511 TARGET_PT_MQ = 39,
3512 TARGET_PT_SOFTE = 39,
3513 TARGET_PT_TRAP = 40,
3514 TARGET_PT_DAR = 41,
3515 TARGET_PT_DSISR = 42,
3516 TARGET_PT_RESULT = 43,
3517 TARGET_PT_REGS_COUNT = 44
3518};
3519
3520/* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
3521 on 64-bit PPC, sigcontext and mcontext are one and the same. */
3522struct target_mcontext {
3523 target_ulong mc_gregs[48];
3524 /* Includes fpscr. */
3525 uint64_t mc_fregs[33];
3526 target_ulong mc_pad[2];
3527 /* We need to handle Altivec and SPE at the same time, which no
3528 kernel needs to do. Fortunately, the kernel defines this bit to
3529 be Altivec-register-large all the time, rather than trying to
3530 twiddle it based on the specific platform. */
3531 union {
3532 /* SPE vector registers. One extra for SPEFSCR. */
3533 uint32_t spe[33];
3534 /* Altivec vector registers. The packing of VSCR and VRSAVE
3535 varies depending on whether we're PPC64 or not: PPC64 splits
3536 them apart; PPC32 stuffs them together. */
3537#if defined(TARGET_PPC64)
3538#define NVRREG 34
3539#else
3540#define NVRREG 33
3541#endif
3542 ppc_avr_t altivec[NVRREG];
3543#undef NVRREG
3544 } mc_vregs __attribute__((__aligned__(16)));
3545};
3546
3547struct target_ucontext {
3548 target_ulong uc_flags;
3549 target_ulong uc_link; /* struct ucontext __user * */
3550 struct target_sigaltstack uc_stack;
3551#if !defined(TARGET_PPC64)
3552 int32_t uc_pad[7];
3553 target_ulong uc_regs; /* struct mcontext __user *
3554 points to uc_mcontext field */
3555#endif
3556 target_sigset_t uc_sigmask;
3557#if defined(TARGET_PPC64)
3558 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
3559 struct target_sigcontext uc_mcontext;
3560#else
3561 int32_t uc_maskext[30];
3562 int32_t uc_pad2[3];
3563 struct target_mcontext uc_mcontext;
3564#endif
3565};
3566
3567/* See arch/powerpc/kernel/signal_32.c. */
3568struct target_sigframe {
3569 struct target_sigcontext sctx;
3570 struct target_mcontext mctx;
3571 int32_t abigap[56];
3572};
3573
3574struct target_rt_sigframe {
3575 struct target_siginfo info;
3576 struct target_ucontext uc;
3577 int32_t abigap[56];
3578};
3579
3580/* We use the mc_pad field for the signal return trampoline. */
3581#define tramp mc_pad
3582
3583/* See arch/powerpc/kernel/signal.c. */
3584static target_ulong get_sigframe(struct target_sigaction *ka,
3585 CPUState *env,
3586 int frame_size)
3587{
3588 target_ulong oldsp, newsp;
3589
3590 oldsp = env->gpr[1];
3591
3592 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
3593 (sas_ss_flags(oldsp))) {
3594 oldsp = (target_sigaltstack_used.ss_sp
3595 + target_sigaltstack_used.ss_size);
3596 }
3597
3598 newsp = (oldsp - frame_size) & ~0xFUL;
3599
3600 return newsp;
3601}
3602
3603static int save_user_regs(CPUState *env, struct target_mcontext *frame,
3604 int sigret)
3605{
3606 target_ulong msr = env->msr;
3607 int i;
3608 target_ulong ccr = 0;
3609
3610 /* In general, the kernel attempts to be intelligent about what it
3611 needs to save for Altivec/FP/SPE registers. We don't care that
3612 much, so we just go ahead and save everything. */
3613
3614 /* Save general registers. */
3615 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3616 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
3617 return 1;
3618 }
3619 }
3620 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3621 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3622 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3623 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3624 return 1;
3625
3626 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3627 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
3628 }
3629 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3630 return 1;
3631
3632 /* Save Altivec registers if necessary. */
3633 if (env->insns_flags & PPC_ALTIVEC) {
3634 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
3635 ppc_avr_t *avr = &env->avr[i];
3636 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
3637
3638 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
3639 __put_user(avr->u64[1], &vreg->u64[1])) {
3640 return 1;
3641 }
3642 }
3643 /* Set MSR_VR in the saved MSR value to indicate that
3644 frame->mc_vregs contains valid data. */
3645 msr |= MSR_VR;
3646 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
3647 &frame->mc_vregs.altivec[32].u32[3]))
3648 return 1;
3649 }
3650
3651 /* Save floating point registers. */
3652 if (env->insns_flags & PPC_FLOAT) {
3653 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3654 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
3655 return 1;
3656 }
3657 }
3658 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
3659 return 1;
3660 }
3661
3662 /* Save SPE registers. The kernel only saves the high half. */
3663 if (env->insns_flags & PPC_SPE) {
3664#if defined(TARGET_PPC64)
3665 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3666 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
3667 return 1;
3668 }
3669 }
3670#else
3671 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3672 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3673 return 1;
3674 }
3675 }
3676#endif
3677 /* Set MSR_SPE in the saved MSR value to indicate that
3678 frame->mc_vregs contains valid data. */
3679 msr |= MSR_SPE;
3680 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3681 return 1;
3682 }
3683
3684 /* Store MSR. */
3685 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3686 return 1;
3687
3688 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
3689 if (sigret) {
3690 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
3691 __put_user(0x44000002UL, &frame->tramp[1])) {
3692 return 1;
3693 }
3694 }
3695
3696 return 0;
3697}
3698
3699static int restore_user_regs(CPUState *env,
3700 struct target_mcontext *frame, int sig)
3701{
3702 target_ulong save_r2 = 0;
3703 target_ulong msr;
3704 target_ulong ccr;
3705
3706 int i;
3707
3708 if (!sig) {
3709 save_r2 = env->gpr[2];
3710 }
3711
3712 /* Restore general registers. */
3713 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3714 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
3715 return 1;
3716 }
3717 }
3718 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3719 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3720 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3721 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3722 return 1;
3723 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3724 return 1;
3725
3726 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3727 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
3728 }
3729
3730 if (!sig) {
3731 env->gpr[2] = save_r2;
3732 }
3733 /* Restore MSR. */
3734 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3735 return 1;
3736
3737 /* If doing signal return, restore the previous little-endian mode. */
3738 if (sig)
3739 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
3740
3741 /* Restore Altivec registers if necessary. */
3742 if (env->insns_flags & PPC_ALTIVEC) {
3743 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
3744 ppc_avr_t *avr = &env->avr[i];
3745 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
3746
3747 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
3748 __get_user(avr->u64[1], &vreg->u64[1])) {
3749 return 1;
3750 }
3751 }
3752 /* Set MSR_VEC in the saved MSR value to indicate that
3753 frame->mc_vregs contains valid data. */
3754 if (__get_user(env->spr[SPR_VRSAVE],
3755 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
3756 return 1;
3757 }
3758
3759 /* Restore floating point registers. */
3760 if (env->insns_flags & PPC_FLOAT) {
3761 uint64_t fpscr;
3762 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3763 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
3764 return 1;
3765 }
3766 }
3767 if (__get_user(fpscr, &frame->mc_fregs[32]))
3768 return 1;
3769 env->fpscr = (uint32_t) fpscr;
3770 }
3771
3772 /* Save SPE registers. The kernel only saves the high half. */
3773 if (env->insns_flags & PPC_SPE) {
3774#if defined(TARGET_PPC64)
3775 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3776 uint32_t hi;
3777
3778 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
3779 return 1;
3780 }
3781 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
3782 }
3783#else
3784 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3785 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3786 return 1;
3787 }
3788 }
3789#endif
3790 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3791 return 1;
3792 }
3793
3794 return 0;
3795}
3796
3797static void setup_frame(int sig, struct target_sigaction *ka,
3798 target_sigset_t *set, CPUState *env)
3799{
3800 struct target_sigframe *frame;
3801 struct target_sigcontext *sc;
3802 target_ulong frame_addr, newsp;
3803 int err = 0;
3804 int signal;
3805
3806 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3807 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3808 goto sigsegv;
3809 sc = &frame->sctx;
3810
3811 signal = current_exec_domain_sig(sig);
3812
3813 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
3814 err |= __put_user(set->sig[0], &sc->oldmask);
3815#if defined(TARGET_PPC64)
3816 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
3817#else
3818 err |= __put_user(set->sig[1], &sc->_unused[3]);
3819#endif
3820 err |= __put_user(h2g(&frame->mctx), &sc->regs);
3821 err |= __put_user(sig, &sc->signal);
3822
3823 /* Save user regs. */
3824 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
3825
3826 /* The kernel checks for the presence of a VDSO here. We don't
3827 emulate a vdso, so use a sigreturn system call. */
3828 env->lr = (target_ulong) h2g(frame->mctx.tramp);
3829
3830 /* Turn off all fp exceptions. */
3831 env->fpscr = 0;
3832
3833 /* Create a stack frame for the caller of the handler. */
3834 newsp = frame_addr - SIGNAL_FRAMESIZE;
3835 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3836
3837 if (err)
3838 goto sigsegv;
3839
3840 /* Set up registers for signal handler. */
3841 env->gpr[1] = newsp;
3842 env->gpr[3] = signal;
3843 env->gpr[4] = (target_ulong) h2g(sc);
3844 env->nip = (target_ulong) ka->_sa_handler;
3845 /* Signal handlers are entered in big-endian mode. */
3846 env->msr &= ~MSR_LE;
3847
3848 unlock_user_struct(frame, frame_addr, 1);
3849 return;
3850
3851sigsegv:
3852 unlock_user_struct(frame, frame_addr, 1);
3853 if (logfile)
3854 fprintf (logfile, "segfaulting from setup_frame\n");
3855 force_sig(SIGSEGV);
3856}
3857
3858static void setup_rt_frame(int sig, struct target_sigaction *ka,
3859 target_siginfo_t *info,
3860 target_sigset_t *set, CPUState *env)
3861{
3862 struct target_rt_sigframe *rt_sf;
3863 struct target_mcontext *frame;
3864 target_ulong rt_sf_addr, newsp = 0;
3865 int i, err = 0;
3866 int signal;
3867
3868 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
3869 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
3870 goto sigsegv;
3871
3872 signal = current_exec_domain_sig(sig);
3873
3874 err |= copy_siginfo_to_user(&rt_sf->info, info);
3875
3876 err |= __put_user(0, &rt_sf->uc.uc_flags);
3877 err |= __put_user(0, &rt_sf->uc.uc_link);
3878 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
3879 &rt_sf->uc.uc_stack.ss_sp);
3880 err |= __put_user(sas_ss_flags(env->gpr[1]),
3881 &rt_sf->uc.uc_stack.ss_flags);
3882 err |= __put_user(target_sigaltstack_used.ss_size,
3883 &rt_sf->uc.uc_stack.ss_size);
3884 err |= __put_user(h2g (&rt_sf->uc.uc_mcontext),
3885 &rt_sf->uc.uc_regs);
3886 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3887 err |= __put_user(set->sig[i], &rt_sf->uc.uc_sigmask.sig[i]);
3888 }
3889
3890 frame = &rt_sf->uc.uc_mcontext;
3891 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
3892
3893 /* The kernel checks for the presence of a VDSO here. We don't
3894 emulate a vdso, so use a sigreturn system call. */
3895 env->lr = (target_ulong) h2g(frame->tramp);
3896
3897 /* Turn off all fp exceptions. */
3898 env->fpscr = 0;
3899
3900 /* Create a stack frame for the caller of the handler. */
3901 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
3902 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3903
3904 if (err)
3905 goto sigsegv;
3906
3907 /* Set up registers for signal handler. */
3908 env->gpr[1] = newsp;
3909 env->gpr[3] = (target_ulong) signal;
3910 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
3911 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
3912 env->gpr[6] = (target_ulong) h2g(rt_sf);
3913 env->nip = (target_ulong) ka->_sa_handler;
3914 /* Signal handlers are entered in big-endian mode. */
3915 env->msr &= ~MSR_LE;
3916
3917 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3918 return;
3919
3920sigsegv:
3921 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3922 if (logfile)
3923 fprintf (logfile, "segfaulting from setup_rt_frame\n");
3924 force_sig(SIGSEGV);
3925
3926}
3927
3928long do_sigreturn(CPUState *env)
3929{
3930 struct target_sigcontext *sc = NULL;
3931 struct target_mcontext *sr = NULL;
3932 target_ulong sr_addr, sc_addr;
3933 sigset_t blocked;
3934 target_sigset_t set;
3935
3936 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
3937 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
3938 goto sigsegv;
3939
3940#if defined(TARGET_PPC64)
3941 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
3942#else
3943 if(__get_user(set.sig[0], &sc->oldmask) ||
3944 __get_user(set.sig[1], &sc->_unused[3]))
3945 goto sigsegv;
3946#endif
3947 target_to_host_sigset_internal(&blocked, &set);
3948 sigprocmask(SIG_SETMASK, &blocked, NULL);
3949
3950 if (__get_user(sr_addr, &sc->regs))
3951 goto sigsegv;
3952 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
3953 goto sigsegv;
3954 if (restore_user_regs(env, sr, 1))
3955 goto sigsegv;
3956
3957 unlock_user_struct(sr, sr_addr, 1);
3958 unlock_user_struct(sc, sc_addr, 1);
3959 return -TARGET_QEMU_ESIGRETURN;
3960
3961sigsegv:
3962 unlock_user_struct(sr, sr_addr, 1);
3963 unlock_user_struct(sc, sc_addr, 1);
3964 if (logfile)
3965 fprintf (logfile, "segfaulting from do_sigreturn\n");
3966 force_sig(SIGSEGV);
3967 return 0;
3968}
3969
3970/* See arch/powerpc/kernel/signal_32.c. */
3971static int do_setcontext(struct target_ucontext *ucp, CPUState *env, int sig)
3972{
3973 struct target_mcontext *mcp;
3974 target_ulong mcp_addr;
3975 sigset_t blocked;
3976 target_sigset_t set;
3977
3978 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, uc_sigmask),
3979 sizeof (set)))
3980 return 1;
3981
3982#if defined(TARGET_PPC64)
3983 fprintf (stderr, "do_setcontext: not implemented\n");
3984 return 0;
3985#else
3986 if (__get_user(mcp_addr, &ucp->uc_regs))
3987 return 1;
3988
3989 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
3990 return 1;
3991
3992 target_to_host_sigset_internal(&blocked, &set);
3993 sigprocmask(SIG_SETMASK, &blocked, NULL);
3994 if (restore_user_regs(env, mcp, sig))
3995 goto sigsegv;
3996
3997 unlock_user_struct(mcp, mcp_addr, 1);
3998 return 0;
3999
4000sigsegv:
4001 unlock_user_struct(mcp, mcp_addr, 1);
4002 return 1;
4003#endif
4004}
4005
4006long do_rt_sigreturn(CPUState *env)
4007{
4008 struct target_rt_sigframe *rt_sf = NULL;
4009 target_ulong rt_sf_addr;
4010
4011 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4012 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4013 goto sigsegv;
4014
4015 if (do_setcontext(&rt_sf->uc, env, 1))
4016 goto sigsegv;
4017
4018 do_sigaltstack(rt_sf_addr
4019 + offsetof(struct target_rt_sigframe, uc.uc_stack),
4020 0, env->gpr[1]);
4021
4022 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4023 return -TARGET_QEMU_ESIGRETURN;
4024
4025sigsegv:
4026 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4027 if (logfile)
4028 fprintf (logfile, "segfaulting from do_rt_sigreturn\n");
4029 force_sig(SIGSEGV);
4030 return 0;
4031}
4032
bellardb346ff42003-06-15 20:05:50 +00004033#else
4034
pbrook624f7972008-05-31 16:11:38 +00004035static void setup_frame(int sig, struct target_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00004036 target_sigset_t *set, CPUState *env)
4037{
4038 fprintf(stderr, "setup_frame: not implemented\n");
4039}
4040
pbrook624f7972008-05-31 16:11:38 +00004041static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00004042 target_siginfo_t *info,
4043 target_sigset_t *set, CPUState *env)
4044{
4045 fprintf(stderr, "setup_rt_frame: not implemented\n");
4046}
4047
4048long do_sigreturn(CPUState *env)
4049{
4050 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00004051 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00004052}
4053
4054long do_rt_sigreturn(CPUState *env)
4055{
4056 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00004057 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00004058}
4059
bellard66fb9762003-03-23 01:06:05 +00004060#endif
4061
pbrook624f7972008-05-31 16:11:38 +00004062void process_pending_signals(CPUState *cpu_env)
bellard66fb9762003-03-23 01:06:05 +00004063{
4064 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00004065 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00004066 sigset_t set, old_set;
4067 target_sigset_t target_old_set;
pbrook624f7972008-05-31 16:11:38 +00004068 struct emulated_sigtable *k;
4069 struct target_sigaction *sa;
bellard66fb9762003-03-23 01:06:05 +00004070 struct sigqueue *q;
pbrook624f7972008-05-31 16:11:38 +00004071 TaskState *ts = cpu_env->opaque;
ths3b46e622007-09-17 08:09:54 +00004072
pbrook624f7972008-05-31 16:11:38 +00004073 if (!ts->signal_pending)
bellard31e31b82003-02-18 22:55:36 +00004074 return;
4075
pbrook624f7972008-05-31 16:11:38 +00004076 /* FIXME: This is not threadsafe. */
4077 k = ts->sigtab;
bellard66fb9762003-03-23 01:06:05 +00004078 for(sig = 1; sig <= TARGET_NSIG; sig++) {
4079 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00004080 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00004081 k++;
bellard31e31b82003-02-18 22:55:36 +00004082 }
4083 /* if no signal is pending, just return */
pbrook624f7972008-05-31 16:11:38 +00004084 ts->signal_pending = 0;
bellard31e31b82003-02-18 22:55:36 +00004085 return;
bellard66fb9762003-03-23 01:06:05 +00004086
bellard31e31b82003-02-18 22:55:36 +00004087 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00004088#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00004089 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00004090#endif
4091 /* dequeue signal */
4092 q = k->first;
4093 k->first = q->next;
4094 if (!k->first)
4095 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00004096
bellard1fddef42005-04-17 19:16:13 +00004097 sig = gdb_handlesig (cpu_env, sig);
4098 if (!sig) {
aurel32ca587a82008-12-18 22:44:13 +00004099 sa = NULL;
4100 handler = TARGET_SIG_IGN;
4101 } else {
4102 sa = &sigact_table[sig - 1];
4103 handler = sa->_sa_handler;
bellard1fddef42005-04-17 19:16:13 +00004104 }
bellard66fb9762003-03-23 01:06:05 +00004105
bellard66fb9762003-03-23 01:06:05 +00004106 if (handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +00004107 /* default handler : ignore some signal. The other are job control or fatal */
4108 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
4109 kill(getpid(),SIGSTOP);
4110 } else if (sig != TARGET_SIGCHLD &&
4111 sig != TARGET_SIGURG &&
4112 sig != TARGET_SIGWINCH &&
4113 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +00004114 force_sig(sig);
4115 }
4116 } else if (handler == TARGET_SIG_IGN) {
4117 /* ignore sig */
4118 } else if (handler == TARGET_SIG_ERR) {
4119 force_sig(sig);
4120 } else {
bellard9de5e442003-03-23 16:49:39 +00004121 /* compute the blocked signals during the handler execution */
pbrook624f7972008-05-31 16:11:38 +00004122 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +00004123 /* SA_NODEFER indicates that the current signal should not be
4124 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +00004125 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +00004126 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00004127
bellard9de5e442003-03-23 16:49:39 +00004128 /* block signals in the handler using Linux */
4129 sigprocmask(SIG_BLOCK, &set, &old_set);
4130 /* save the previous blocked signal state to restore it at the
4131 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00004132 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00004133
bellardbc8a22c2003-03-30 21:02:40 +00004134 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00004135#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00004136 {
4137 CPUX86State *env = cpu_env;
4138 if (env->eflags & VM_MASK)
4139 save_v86_state(env);
4140 }
4141#endif
bellard9de5e442003-03-23 16:49:39 +00004142 /* prepare the stack frame of the virtual CPU */
pbrook624f7972008-05-31 16:11:38 +00004143 if (sa->sa_flags & TARGET_SA_SIGINFO)
4144 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00004145 else
pbrook624f7972008-05-31 16:11:38 +00004146 setup_frame(sig, sa, &target_old_set, cpu_env);
4147 if (sa->sa_flags & TARGET_SA_RESETHAND)
4148 sa->_sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00004149 }
bellard66fb9762003-03-23 01:06:05 +00004150 if (q != &k->info)
pbrook624f7972008-05-31 16:11:38 +00004151 free_sigqueue(cpu_env, q);
bellard31e31b82003-02-18 22:55:36 +00004152}