]> Git Repo - qemu.git/blame - linux-user/signal.c
Disable the temporary timer slowdown for etrax.
[qemu.git] / linux-user / signal.c
CommitLineData
31e31b8a 1/*
66fb9763 2 * Emulation of Linux signals
5fafdf24 3 *
31e31b8a
FB
4 * 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
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <stdlib.h>
21#include <stdio.h>
66fb9763 22#include <string.h>
31e31b8a 23#include <stdarg.h>
2677e107 24#include <unistd.h>
31e31b8a 25#include <signal.h>
66fb9763 26#include <errno.h>
31e31b8a
FB
27#include <sys/ucontext.h>
28
3ef693a0 29#include "qemu.h"
992f48a0 30#include "target_signal.h"
66fb9763
FB
31
32//#define DEBUG_SIGNAL
33
34#define MAX_SIGQUEUE_SIZE 1024
35
36struct sigqueue {
37 struct sigqueue *next;
9de5e440 38 target_siginfo_t info;
66fb9763 39};
31e31b8a
FB
40
41struct emulated_sigaction {
42 struct target_sigaction sa;
66fb9763
FB
43 int pending; /* true if signal is pending */
44 struct sigqueue *first;
45 struct sigqueue info; /* in order to always have memory for the
46 first signal, we put it here */
31e31b8a
FB
47};
48
a04e134a
TS
49struct target_sigaltstack target_sigaltstack_used = {
50 .ss_sp = 0,
51 .ss_size = 0,
52 .ss_flags = TARGET_SS_DISABLE,
53};
54
66fb9763
FB
55static struct emulated_sigaction sigact_table[TARGET_NSIG];
56static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57static struct sigqueue *first_free; /* first free siginfo queue entry */
58static int signal_pending; /* non zero if a signal may be pending */
31e31b8a 59
5fafdf24 60static void host_signal_handler(int host_signum, siginfo_t *info,
66fb9763
FB
61 void *puc);
62
9e5f5284
FB
63static uint8_t host_to_target_signal_table[65] = {
64 [SIGHUP] = TARGET_SIGHUP,
65 [SIGINT] = TARGET_SIGINT,
66 [SIGQUIT] = TARGET_SIGQUIT,
67 [SIGILL] = TARGET_SIGILL,
68 [SIGTRAP] = TARGET_SIGTRAP,
69 [SIGABRT] = TARGET_SIGABRT,
01e3b763 70/* [SIGIOT] = TARGET_SIGIOT,*/
9e5f5284
FB
71 [SIGBUS] = TARGET_SIGBUS,
72 [SIGFPE] = TARGET_SIGFPE,
73 [SIGKILL] = TARGET_SIGKILL,
74 [SIGUSR1] = TARGET_SIGUSR1,
75 [SIGSEGV] = TARGET_SIGSEGV,
76 [SIGUSR2] = TARGET_SIGUSR2,
77 [SIGPIPE] = TARGET_SIGPIPE,
78 [SIGALRM] = TARGET_SIGALRM,
79 [SIGTERM] = TARGET_SIGTERM,
80#ifdef SIGSTKFLT
81 [SIGSTKFLT] = TARGET_SIGSTKFLT,
82#endif
83 [SIGCHLD] = TARGET_SIGCHLD,
84 [SIGCONT] = TARGET_SIGCONT,
85 [SIGSTOP] = TARGET_SIGSTOP,
86 [SIGTSTP] = TARGET_SIGTSTP,
87 [SIGTTIN] = TARGET_SIGTTIN,
88 [SIGTTOU] = TARGET_SIGTTOU,
89 [SIGURG] = TARGET_SIGURG,
90 [SIGXCPU] = TARGET_SIGXCPU,
91 [SIGXFSZ] = TARGET_SIGXFSZ,
92 [SIGVTALRM] = TARGET_SIGVTALRM,
93 [SIGPROF] = TARGET_SIGPROF,
94 [SIGWINCH] = TARGET_SIGWINCH,
95 [SIGIO] = TARGET_SIGIO,
96 [SIGPWR] = TARGET_SIGPWR,
97 [SIGSYS] = TARGET_SIGSYS,
98 /* next signals stay the same */
99};
100static uint8_t target_to_host_signal_table[65];
101
a04e134a
TS
102static inline int on_sig_stack(unsigned long sp)
103{
104 return (sp - target_sigaltstack_used.ss_sp
105 < target_sigaltstack_used.ss_size);
106}
107
108static inline int sas_ss_flags(unsigned long sp)
109{
110 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
111 : on_sig_stack(sp) ? SS_ONSTACK : 0);
112}
113
31e31b8a
FB
114static inline int host_to_target_signal(int sig)
115{
9e5f5284 116 return host_to_target_signal_table[sig];
31e31b8a
FB
117}
118
119static inline int target_to_host_signal(int sig)
120{
9e5f5284 121 return target_to_host_signal_table[sig];
31e31b8a
FB
122}
123
5fafdf24 124static void host_to_target_sigset_internal(target_sigset_t *d,
9231944d 125 const sigset_t *s)
66fb9763
FB
126{
127 int i;
9e5f5284
FB
128 unsigned long sigmask;
129 uint32_t target_sigmask;
3b46e624 130
9e5f5284
FB
131 sigmask = ((unsigned long *)s)[0];
132 target_sigmask = 0;
133 for(i = 0; i < 32; i++) {
5fafdf24 134 if (sigmask & (1 << i))
9e5f5284
FB
135 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
136 }
992f48a0 137#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
9231944d 138 d->sig[0] = target_sigmask;
9e5f5284 139 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
9231944d 140 d->sig[i] = ((unsigned long *)s)[i];
66fb9763 141 }
992f48a0 142#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
9231944d
FB
143 d->sig[0] = target_sigmask;
144 d->sig[1] = sigmask >> 32;
9e5f5284 145#else
459a4017 146 /* XXX: do it */
9e5f5284 147#endif
66fb9763
FB
148}
149
9231944d
FB
150void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
151{
152 target_sigset_t d1;
153 int i;
154
155 host_to_target_sigset_internal(&d1, s);
156 for(i = 0;i < TARGET_NSIG_WORDS; i++)
53a5960a 157 d->sig[i] = tswapl(d1.sig[i]);
9231944d
FB
158}
159
160void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
66fb9763
FB
161{
162 int i;
9e5f5284 163 unsigned long sigmask;
992f48a0 164 abi_ulong target_sigmask;
9e5f5284 165
9231944d 166 target_sigmask = s->sig[0];
9e5f5284
FB
167 sigmask = 0;
168 for(i = 0; i < 32; i++) {
5fafdf24 169 if (target_sigmask & (1 << i))
9e5f5284
FB
170 sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
171 }
992f48a0 172#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
9e5f5284
FB
173 ((unsigned long *)d)[0] = sigmask;
174 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
9231944d 175 ((unsigned long *)d)[i] = s->sig[i];
66fb9763 176 }
992f48a0 177#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
9231944d 178 ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
9e5f5284 179#else
459a4017 180 /* XXX: do it */
992f48a0 181#endif /* TARGET_ABI_BITS */
66fb9763
FB
182}
183
9231944d
FB
184void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
185{
186 target_sigset_t s1;
187 int i;
188
189 for(i = 0;i < TARGET_NSIG_WORDS; i++)
53a5960a 190 s1.sig[i] = tswapl(s->sig[i]);
9231944d
FB
191 target_to_host_sigset_internal(d, &s1);
192}
3b46e624 193
992f48a0 194void host_to_target_old_sigset(abi_ulong *old_sigset,
66fb9763
FB
195 const sigset_t *sigset)
196{
9e5f5284
FB
197 target_sigset_t d;
198 host_to_target_sigset(&d, sigset);
199 *old_sigset = d.sig[0];
66fb9763
FB
200}
201
5fafdf24 202void target_to_host_old_sigset(sigset_t *sigset,
992f48a0 203 const abi_ulong *old_sigset)
66fb9763 204{
9e5f5284
FB
205 target_sigset_t d;
206 int i;
207
208 d.sig[0] = *old_sigset;
209 for(i = 1;i < TARGET_NSIG_WORDS; i++)
210 d.sig[i] = 0;
211 target_to_host_sigset(sigset, &d);
66fb9763
FB
212}
213
9de5e440
FB
214/* siginfo conversion */
215
5fafdf24 216static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
9de5e440 217 const siginfo_t *info)
66fb9763 218{
9de5e440
FB
219 int sig;
220 sig = host_to_target_signal(info->si_signo);
221 tinfo->si_signo = sig;
222 tinfo->si_errno = 0;
223 tinfo->si_code = 0;
5fafdf24 224 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
447db213 225 sig == SIGBUS || sig == SIGTRAP) {
9de5e440
FB
226 /* should never come here, but who knows. The information for
227 the target is irrelevant */
228 tinfo->_sifields._sigfault._addr = 0;
7f7f7c84
TS
229 } else if (sig == SIGIO) {
230 tinfo->_sifields._sigpoll._fd = info->si_fd;
9de5e440
FB
231 } else if (sig >= TARGET_SIGRTMIN) {
232 tinfo->_sifields._rt._pid = info->si_pid;
233 tinfo->_sifields._rt._uid = info->si_uid;
234 /* XXX: potential problem if 64 bit */
5fafdf24 235 tinfo->_sifields._rt._sigval.sival_ptr =
459a4017 236 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
9de5e440
FB
237 }
238}
239
5fafdf24 240static void tswap_siginfo(target_siginfo_t *tinfo,
9de5e440
FB
241 const target_siginfo_t *info)
242{
243 int sig;
244 sig = info->si_signo;
245 tinfo->si_signo = tswap32(sig);
66fb9763
FB
246 tinfo->si_errno = tswap32(info->si_errno);
247 tinfo->si_code = tswap32(info->si_code);
5fafdf24 248 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
447db213 249 sig == SIGBUS || sig == SIGTRAP) {
5fafdf24 250 tinfo->_sifields._sigfault._addr =
9de5e440 251 tswapl(info->_sifields._sigfault._addr);
7f7f7c84
TS
252 } else if (sig == SIGIO) {
253 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
9de5e440
FB
254 } else if (sig >= TARGET_SIGRTMIN) {
255 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
256 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
5fafdf24 257 tinfo->_sifields._rt._sigval.sival_ptr =
9de5e440
FB
258 tswapl(info->_sifields._rt._sigval.sival_ptr);
259 }
260}
261
262
263void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
264{
265 host_to_target_siginfo_noswap(tinfo, info);
266 tswap_siginfo(tinfo, tinfo);
66fb9763
FB
267}
268
9de5e440 269/* XXX: we support only POSIX RT signals are used. */
aa1f17c1 270/* XXX: find a solution for 64 bit (additional malloced data is needed) */
9de5e440 271void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
66fb9763
FB
272{
273 info->si_signo = tswap32(tinfo->si_signo);
274 info->si_errno = tswap32(tinfo->si_errno);
275 info->si_code = tswap32(tinfo->si_code);
9de5e440
FB
276 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
277 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
5fafdf24 278 info->si_value.sival_ptr =
459a4017 279 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
66fb9763
FB
280}
281
31e31b8a
FB
282void signal_init(void)
283{
284 struct sigaction act;
9e5f5284 285 int i, j;
31e31b8a 286
9e5f5284
FB
287 /* generate signal conversion tables */
288 for(i = 1; i <= 64; i++) {
289 if (host_to_target_signal_table[i] == 0)
290 host_to_target_signal_table[i] = i;
291 }
292 for(i = 1; i <= 64; i++) {
293 j = host_to_target_signal_table[i];
294 target_to_host_signal_table[j] = i;
295 }
3b46e624 296
9de5e440
FB
297 /* set all host signal handlers. ALL signals are blocked during
298 the handlers to serialize them. */
299 sigfillset(&act.sa_mask);
31e31b8a
FB
300 act.sa_flags = SA_SIGINFO;
301 act.sa_sigaction = host_signal_handler;
302 for(i = 1; i < NSIG; i++) {
c9087c2a 303 sigaction(i, &act, NULL);
31e31b8a 304 }
3b46e624 305
31e31b8a 306 memset(sigact_table, 0, sizeof(sigact_table));
66fb9763
FB
307
308 first_free = &sigqueue_table[0];
5fafdf24 309 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
66fb9763
FB
310 sigqueue_table[i].next = &sigqueue_table[i + 1];
311 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
312}
313
314/* signal queue handling */
315
316static inline struct sigqueue *alloc_sigqueue(void)
317{
318 struct sigqueue *q = first_free;
319 if (!q)
320 return NULL;
321 first_free = q->next;
322 return q;
31e31b8a
FB
323}
324
66fb9763
FB
325static inline void free_sigqueue(struct sigqueue *q)
326{
327 q->next = first_free;
328 first_free = q;
329}
330
9de5e440
FB
331/* abort execution with signal */
332void __attribute((noreturn)) force_sig(int sig)
66fb9763
FB
333{
334 int host_sig;
66fb9763 335 host_sig = target_to_host_signal(sig);
5fafdf24 336 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
66fb9763 337 sig, strsignal(host_sig));
9de5e440 338#if 1
66fb9763 339 _exit(-host_sig);
9de5e440
FB
340#else
341 {
342 struct sigaction act;
343 sigemptyset(&act.sa_mask);
344 act.sa_flags = SA_SIGINFO;
345 act.sa_sigaction = SIG_DFL;
346 sigaction(SIGABRT, &act, NULL);
347 abort();
348 }
349#endif
66fb9763
FB
350}
351
9de5e440
FB
352/* queue a signal so that it will be send to the virtual CPU as soon
353 as possible */
354int queue_signal(int sig, target_siginfo_t *info)
31e31b8a 355{
66fb9763 356 struct emulated_sigaction *k;
9de5e440 357 struct sigqueue *q, **pq;
992f48a0 358 abi_ulong handler;
66fb9763 359
9de5e440 360#if defined(DEBUG_SIGNAL)
5fafdf24 361 fprintf(stderr, "queue_signal: sig=%d\n",
9de5e440 362 sig);
66fb9763 363#endif
9de5e440 364 k = &sigact_table[sig - 1];
66fb9763
FB
365 handler = k->sa._sa_handler;
366 if (handler == TARGET_SIG_DFL) {
367 /* default handler : ignore some signal. The other are fatal */
5fafdf24
TS
368 if (sig != TARGET_SIGCHLD &&
369 sig != TARGET_SIGURG &&
66fb9763
FB
370 sig != TARGET_SIGWINCH) {
371 force_sig(sig);
9de5e440
FB
372 } else {
373 return 0; /* indicate ignored */
66fb9763
FB
374 }
375 } else if (handler == TARGET_SIG_IGN) {
376 /* ignore signal */
9de5e440 377 return 0;
66fb9763
FB
378 } else if (handler == TARGET_SIG_ERR) {
379 force_sig(sig);
380 } else {
9de5e440
FB
381 pq = &k->first;
382 if (sig < TARGET_SIGRTMIN) {
383 /* if non real time signal, we queue exactly one signal */
384 if (!k->pending)
385 q = &k->info;
386 else
387 return 0;
388 } else {
389 if (!k->pending) {
390 /* first signal */
391 q = &k->info;
392 } else {
393 q = alloc_sigqueue();
394 if (!q)
395 return -EAGAIN;
396 while (*pq != NULL)
397 pq = &(*pq)->next;
398 }
399 }
400 *pq = q;
401 q->info = *info;
402 q->next = NULL;
403 k->pending = 1;
404 /* signal that a new signal is pending */
405 signal_pending = 1;
406 return 1; /* indicates that the signal was queued */
407 }
408}
409
5fafdf24 410static void host_signal_handler(int host_signum, siginfo_t *info,
9de5e440
FB
411 void *puc)
412{
413 int sig;
414 target_siginfo_t tinfo;
415
416 /* the CPU emulator uses some host signals to detect exceptions,
417 we we forward to it some signals */
ec6338ba 418 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
b346ff46 419 if (cpu_signal_handler(host_signum, info, puc))
9de5e440
FB
420 return;
421 }
422
423 /* get target signal number */
424 sig = host_to_target_signal(host_signum);
425 if (sig < 1 || sig > TARGET_NSIG)
426 return;
427#if defined(DEBUG_SIGNAL)
bc8a22cc 428 fprintf(stderr, "qemu: got signal %d\n", sig);
9de5e440
FB
429#endif
430 host_to_target_siginfo_noswap(&tinfo, info);
431 if (queue_signal(sig, &tinfo) == 1) {
432 /* interrupt the virtual CPU as soon as possible */
68a79315 433 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
66fb9763
FB
434 }
435}
436
0da46a6e 437/* do_sigaltstack() returns target values and errnos. */
579a97f7
FB
438/* compare linux/kernel/signal.c:do_sigaltstack() */
439abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
a04e134a
TS
440{
441 int ret;
442 struct target_sigaltstack oss;
443
444 /* XXX: test errors */
579a97f7 445 if(uoss_addr)
a04e134a
TS
446 {
447 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
448 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
449 __put_user(sas_ss_flags(sp), &oss.ss_flags);
450 }
451
579a97f7 452 if(uss_addr)
a04e134a 453 {
579a97f7
FB
454 struct target_sigaltstack *uss;
455 struct target_sigaltstack ss;
a04e134a 456
0da46a6e 457 ret = -TARGET_EFAULT;
579a97f7 458 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
a04e134a
TS
459 || __get_user(ss.ss_sp, &uss->ss_sp)
460 || __get_user(ss.ss_size, &uss->ss_size)
461 || __get_user(ss.ss_flags, &uss->ss_flags))
462 goto out;
579a97f7 463 unlock_user_struct(uss, uss_addr, 0);
a04e134a 464
0da46a6e 465 ret = -TARGET_EPERM;
a04e134a
TS
466 if (on_sig_stack(sp))
467 goto out;
468
0da46a6e 469 ret = -TARGET_EINVAL;
a04e134a
TS
470 if (ss.ss_flags != TARGET_SS_DISABLE
471 && ss.ss_flags != TARGET_SS_ONSTACK
472 && ss.ss_flags != 0)
473 goto out;
474
475 if (ss.ss_flags == TARGET_SS_DISABLE) {
476 ss.ss_size = 0;
477 ss.ss_sp = 0;
478 } else {
0da46a6e 479 ret = -TARGET_ENOMEM;
a04e134a
TS
480 if (ss.ss_size < MINSIGSTKSZ)
481 goto out;
482 }
483
484 target_sigaltstack_used.ss_sp = ss.ss_sp;
485 target_sigaltstack_used.ss_size = ss.ss_size;
486 }
487
579a97f7 488 if (uoss_addr) {
0da46a6e 489 ret = -TARGET_EFAULT;
579a97f7 490 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
a04e134a 491 goto out;
a04e134a
TS
492 }
493
494 ret = 0;
495out:
496 return ret;
497}
498
0da46a6e 499/* do_sigaction() return host values and errnos */
66fb9763
FB
500int do_sigaction(int sig, const struct target_sigaction *act,
501 struct target_sigaction *oact)
502{
503 struct emulated_sigaction *k;
773b93ee
FB
504 struct sigaction act1;
505 int host_sig;
0da46a6e 506 int ret = 0;
66fb9763 507
0aeaa8ce 508 if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
66fb9763
FB
509 return -EINVAL;
510 k = &sigact_table[sig - 1];
773b93ee 511#if defined(DEBUG_SIGNAL)
5fafdf24 512 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
66fb9763
FB
513 sig, (int)act, (int)oact);
514#endif
515 if (oact) {
516 oact->_sa_handler = tswapl(k->sa._sa_handler);
517 oact->sa_flags = tswapl(k->sa.sa_flags);
388bb21a
TS
518#if !defined(TARGET_MIPS)
519 oact->sa_restorer = tswapl(k->sa.sa_restorer);
520#endif
66fb9763
FB
521 oact->sa_mask = k->sa.sa_mask;
522 }
523 if (act) {
524 k->sa._sa_handler = tswapl(act->_sa_handler);
525 k->sa.sa_flags = tswapl(act->sa_flags);
388bb21a
TS
526#if !defined(TARGET_MIPS)
527 k->sa.sa_restorer = tswapl(act->sa_restorer);
528#endif
66fb9763 529 k->sa.sa_mask = act->sa_mask;
773b93ee
FB
530
531 /* we update the host linux signal state */
532 host_sig = target_to_host_signal(sig);
533 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
534 sigfillset(&act1.sa_mask);
535 act1.sa_flags = SA_SIGINFO;
536 if (k->sa.sa_flags & TARGET_SA_RESTART)
537 act1.sa_flags |= SA_RESTART;
538 /* NOTE: it is important to update the host kernel signal
539 ignore state to avoid getting unexpected interrupted
540 syscalls */
541 if (k->sa._sa_handler == TARGET_SIG_IGN) {
542 act1.sa_sigaction = (void *)SIG_IGN;
543 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
544 act1.sa_sigaction = (void *)SIG_DFL;
545 } else {
546 act1.sa_sigaction = host_signal_handler;
547 }
0da46a6e 548 ret = sigaction(host_sig, &act1, NULL);
773b93ee 549 }
66fb9763 550 }
0da46a6e 551 return ret;
66fb9763
FB
552}
553
43fff238
FB
554#ifndef offsetof
555#define offsetof(type, field) ((size_t) &((type *)0)->field)
556#endif
557
5fafdf24 558static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
43fff238
FB
559 const target_siginfo_t *info)
560{
561 tswap_siginfo(tinfo, info);
562 return 0;
563}
564
c3b5bc8a
TS
565static inline int current_exec_domain_sig(int sig)
566{
567 return /* current->exec_domain && current->exec_domain->signal_invmap
568 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
569}
570
459a4017 571#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
66fb9763
FB
572
573/* from the Linux kernel */
574
575struct target_fpreg {
576 uint16_t significand[4];
577 uint16_t exponent;
578};
579
580struct target_fpxreg {
581 uint16_t significand[4];
582 uint16_t exponent;
583 uint16_t padding[3];
584};
585
586struct target_xmmreg {
992f48a0 587 abi_ulong element[4];
66fb9763
FB
588};
589
590struct target_fpstate {
591 /* Regular FPU environment */
992f48a0
BS
592 abi_ulong cw;
593 abi_ulong sw;
594 abi_ulong tag;
595 abi_ulong ipoff;
596 abi_ulong cssel;
597 abi_ulong dataoff;
598 abi_ulong datasel;
66fb9763
FB
599 struct target_fpreg _st[8];
600 uint16_t status;
601 uint16_t magic; /* 0xffff = regular FPU data only */
602
603 /* FXSR FPU environment */
992f48a0
BS
604 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
605 abi_ulong mxcsr;
606 abi_ulong reserved;
66fb9763
FB
607 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
608 struct target_xmmreg _xmm[8];
992f48a0 609 abi_ulong padding[56];
66fb9763
FB
610};
611
612#define X86_FXSR_MAGIC 0x0000
613
614struct target_sigcontext {
615 uint16_t gs, __gsh;
616 uint16_t fs, __fsh;
617 uint16_t es, __esh;
618 uint16_t ds, __dsh;
992f48a0
BS
619 abi_ulong edi;
620 abi_ulong esi;
621 abi_ulong ebp;
622 abi_ulong esp;
623 abi_ulong ebx;
624 abi_ulong edx;
625 abi_ulong ecx;
626 abi_ulong eax;
627 abi_ulong trapno;
628 abi_ulong err;
629 abi_ulong eip;
66fb9763 630 uint16_t cs, __csh;
992f48a0
BS
631 abi_ulong eflags;
632 abi_ulong esp_at_signal;
66fb9763 633 uint16_t ss, __ssh;
992f48a0
BS
634 abi_ulong fpstate; /* pointer */
635 abi_ulong oldmask;
636 abi_ulong cr2;
66fb9763
FB
637};
638
66fb9763 639struct target_ucontext {
992f48a0
BS
640 abi_ulong tuc_flags;
641 abi_ulong tuc_link;
b8076a74
FB
642 target_stack_t tuc_stack;
643 struct target_sigcontext tuc_mcontext;
644 target_sigset_t tuc_sigmask; /* mask last for extensibility */
66fb9763
FB
645};
646
647struct sigframe
648{
992f48a0 649 abi_ulong pretcode;
66fb9763
FB
650 int sig;
651 struct target_sigcontext sc;
652 struct target_fpstate fpstate;
992f48a0 653 abi_ulong extramask[TARGET_NSIG_WORDS-1];
66fb9763
FB
654 char retcode[8];
655};
656
657struct rt_sigframe
658{
992f48a0 659 abi_ulong pretcode;
66fb9763 660 int sig;
992f48a0
BS
661 abi_ulong pinfo;
662 abi_ulong puc;
66fb9763
FB
663 struct target_siginfo info;
664 struct target_ucontext uc;
665 struct target_fpstate fpstate;
666 char retcode[8];
667};
668
669/*
670 * Set up a signal frame.
671 */
672
66fb9763
FB
673/* XXX: save x87 state */
674static int
675setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
28be6234 676 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
66fb9763
FB
677{
678 int err = 0;
775b58d8 679 uint16_t magic;
66fb9763 680
579a97f7 681 /* already locked in setup_frame() */
a52c757c
FB
682 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
683 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
684 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
685 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
66fb9763
FB
686 err |= __put_user(env->regs[R_EDI], &sc->edi);
687 err |= __put_user(env->regs[R_ESI], &sc->esi);
688 err |= __put_user(env->regs[R_EBP], &sc->ebp);
689 err |= __put_user(env->regs[R_ESP], &sc->esp);
690 err |= __put_user(env->regs[R_EBX], &sc->ebx);
691 err |= __put_user(env->regs[R_EDX], &sc->edx);
692 err |= __put_user(env->regs[R_ECX], &sc->ecx);
693 err |= __put_user(env->regs[R_EAX], &sc->eax);
66099dd9
FB
694 err |= __put_user(env->exception_index, &sc->trapno);
695 err |= __put_user(env->error_code, &sc->err);
66fb9763 696 err |= __put_user(env->eip, &sc->eip);
a52c757c 697 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
66fb9763
FB
698 err |= __put_user(env->eflags, &sc->eflags);
699 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
a52c757c 700 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
ed2dcdf6 701
28be6234 702 cpu_x86_fsave(env, fpstate_addr, 1);
ed2dcdf6 703 fpstate->status = fpstate->sw;
775b58d8
FB
704 magic = 0xffff;
705 err |= __put_user(magic, &fpstate->magic);
28be6234 706 err |= __put_user(fpstate_addr, &sc->fpstate);
ed2dcdf6 707
66fb9763
FB
708 /* non-iBCS2 extensions.. */
709 err |= __put_user(mask, &sc->oldmask);
a52c757c 710 err |= __put_user(env->cr[2], &sc->cr2);
66fb9763 711 return err;
31e31b8a
FB
712}
713
66fb9763
FB
714/*
715 * Determine which stack to use..
716 */
31e31b8a 717
579a97f7 718static inline abi_ulong
66fb9763 719get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
31e31b8a 720{
66fb9763
FB
721 unsigned long esp;
722
723 /* Default to using normal stack */
724 esp = env->regs[R_ESP];
66fb9763 725 /* This is the X/Open sanctioned signal stack switching. */
a04e134a
TS
726 if (ka->sa.sa_flags & TARGET_SA_ONSTACK) {
727 if (sas_ss_flags(esp) == 0)
728 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
729 }
66fb9763
FB
730
731 /* This is the legacy signal stack switching. */
5fafdf24 732 else
a52c757c
FB
733 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
734 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
735 ka->sa.sa_restorer) {
736 esp = (unsigned long) ka->sa.sa_restorer;
737 }
579a97f7 738 return (esp - frame_size) & -8ul;
66fb9763
FB
739}
740
579a97f7 741/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
66fb9763
FB
742static void setup_frame(int sig, struct emulated_sigaction *ka,
743 target_sigset_t *set, CPUX86State *env)
744{
579a97f7 745 abi_ulong frame_addr;
66fb9763 746 struct sigframe *frame;
9231944d 747 int i, err = 0;
66fb9763 748
579a97f7 749 frame_addr = get_sigframe(ka, env, sizeof(*frame));
66fb9763 750
579a97f7 751 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
66fb9763 752 goto give_sigsegv;
579a97f7 753
c3b5bc8a 754 err |= __put_user(current_exec_domain_sig(sig),
66fb9763
FB
755 &frame->sig);
756 if (err)
757 goto give_sigsegv;
758
28be6234
FB
759 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
760 frame_addr + offsetof(struct sigframe, fpstate));
66fb9763
FB
761 if (err)
762 goto give_sigsegv;
763
9231944d
FB
764 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
765 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
766 goto give_sigsegv;
767 }
66fb9763
FB
768
769 /* Set up to return from userspace. If provided, use a stub
770 already in userspace. */
771 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
772 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
773 } else {
775b58d8 774 uint16_t val16;
28be6234
FB
775 abi_ulong retcode_addr;
776 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
777 err |= __put_user(retcode_addr, &frame->pretcode);
66fb9763 778 /* This is popl %eax ; movl $,%eax ; int $0x80 */
775b58d8
FB
779 val16 = 0xb858;
780 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
66fb9763 781 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
775b58d8
FB
782 val16 = 0x80cd;
783 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
66fb9763
FB
784 }
785
786 if (err)
787 goto give_sigsegv;
788
789 /* Set up registers for signal handler */
28be6234
FB
790 env->regs[R_ESP] = frame_addr;
791 env->eip = ka->sa._sa_handler;
66fb9763
FB
792
793 cpu_x86_load_seg(env, R_DS, __USER_DS);
794 cpu_x86_load_seg(env, R_ES, __USER_DS);
795 cpu_x86_load_seg(env, R_SS, __USER_DS);
796 cpu_x86_load_seg(env, R_CS, __USER_CS);
797 env->eflags &= ~TF_MASK;
798
579a97f7
FB
799 unlock_user_struct(frame, frame_addr, 1);
800
66fb9763
FB
801 return;
802
803give_sigsegv:
579a97f7 804 unlock_user_struct(frame, frame_addr, 1);
66fb9763
FB
805 if (sig == TARGET_SIGSEGV)
806 ka->sa._sa_handler = TARGET_SIG_DFL;
807 force_sig(TARGET_SIGSEGV /* , current */);
808}
809
579a97f7 810/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
5fafdf24 811static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
9de5e440 812 target_siginfo_t *info,
66fb9763
FB
813 target_sigset_t *set, CPUX86State *env)
814{
28be6234 815 abi_ulong frame_addr, addr;
66fb9763 816 struct rt_sigframe *frame;
9231944d 817 int i, err = 0;
66fb9763 818
579a97f7 819 frame_addr = get_sigframe(ka, env, sizeof(*frame));
66fb9763 820
579a97f7 821 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
66fb9763 822 goto give_sigsegv;
66fb9763 823
c3b5bc8a 824 err |= __put_user(current_exec_domain_sig(sig),
66fb9763 825 &frame->sig);
28be6234
FB
826 addr = frame_addr + offsetof(struct rt_sigframe, info);
827 err |= __put_user(addr, &frame->pinfo);
828 addr = frame_addr + offsetof(struct rt_sigframe, uc);
829 err |= __put_user(addr, &frame->puc);
66fb9763
FB
830 err |= copy_siginfo_to_user(&frame->info, info);
831 if (err)
832 goto give_sigsegv;
31e31b8a 833
66fb9763 834 /* Create the ucontext. */
b8076a74
FB
835 err |= __put_user(0, &frame->uc.tuc_flags);
836 err |= __put_user(0, &frame->uc.tuc_link);
a04e134a 837 err |= __put_user(target_sigaltstack_used.ss_sp,
b8076a74 838 &frame->uc.tuc_stack.ss_sp);
a04e134a 839 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
b8076a74 840 &frame->uc.tuc_stack.ss_flags);
a04e134a 841 err |= __put_user(target_sigaltstack_used.ss_size,
b8076a74
FB
842 &frame->uc.tuc_stack.ss_size);
843 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
28be6234
FB
844 env, set->sig[0],
845 frame_addr + offsetof(struct rt_sigframe, fpstate));
9231944d 846 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
b8076a74 847 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
9231944d
FB
848 goto give_sigsegv;
849 }
31e31b8a 850
66fb9763
FB
851 /* Set up to return from userspace. If provided, use a stub
852 already in userspace. */
853 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
854 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
855 } else {
775b58d8 856 uint16_t val16;
28be6234
FB
857 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
858 err |= __put_user(addr, &frame->pretcode);
66fb9763 859 /* This is movl $,%eax ; int $0x80 */
775b58d8 860 err |= __put_user(0xb8, (char *)(frame->retcode+0));
66fb9763 861 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
775b58d8
FB
862 val16 = 0x80cd;
863 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
66fb9763
FB
864 }
865
866 if (err)
867 goto give_sigsegv;
868
869 /* Set up registers for signal handler */
28be6234
FB
870 env->regs[R_ESP] = frame_addr;
871 env->eip = ka->sa._sa_handler;
66fb9763
FB
872
873 cpu_x86_load_seg(env, R_DS, __USER_DS);
874 cpu_x86_load_seg(env, R_ES, __USER_DS);
875 cpu_x86_load_seg(env, R_SS, __USER_DS);
876 cpu_x86_load_seg(env, R_CS, __USER_CS);
877 env->eflags &= ~TF_MASK;
878
579a97f7
FB
879 unlock_user_struct(frame, frame_addr, 1);
880
66fb9763
FB
881 return;
882
883give_sigsegv:
579a97f7 884 unlock_user_struct(frame, frame_addr, 1);
66fb9763
FB
885 if (sig == TARGET_SIGSEGV)
886 ka->sa._sa_handler = TARGET_SIG_DFL;
887 force_sig(TARGET_SIGSEGV /* , current */);
888}
889
890static int
891restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
892{
893 unsigned int err = 0;
28be6234
FB
894 abi_ulong fpstate_addr;
895 unsigned int tmpflags;
896
897 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
898 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
899 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
900 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
901
902 env->regs[R_EDI] = tswapl(sc->edi);
903 env->regs[R_ESI] = tswapl(sc->esi);
904 env->regs[R_EBP] = tswapl(sc->ebp);
905 env->regs[R_ESP] = tswapl(sc->esp);
906 env->regs[R_EBX] = tswapl(sc->ebx);
907 env->regs[R_EDX] = tswapl(sc->edx);
908 env->regs[R_ECX] = tswapl(sc->ecx);
909 env->eip = tswapl(sc->eip);
66fb9763
FB
910
911 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
912 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
5fafdf24 913
28be6234
FB
914 tmpflags = tswapl(sc->eflags);
915 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
916 // regs->orig_eax = -1; /* disable syscall checks */
66fb9763 917
28be6234
FB
918 fpstate_addr = tswapl(sc->fpstate);
919 if (fpstate_addr != 0) {
920 if (!access_ok(VERIFY_READ, fpstate_addr,
921 sizeof(struct target_fpstate)))
922 goto badframe;
923 cpu_x86_frstor(env, fpstate_addr, 1);
66fb9763 924 }
ed2dcdf6 925
28be6234 926 *peax = tswapl(sc->eax);
66fb9763 927 return err;
66fb9763
FB
928badframe:
929 return 1;
66fb9763
FB
930}
931
932long do_sigreturn(CPUX86State *env)
933{
579a97f7
FB
934 struct sigframe *frame;
935 abi_ulong frame_addr = env->regs[R_ESP] - 8;
66fb9763
FB
936 target_sigset_t target_set;
937 sigset_t set;
938 int eax, i;
939
447db213
FB
940#if defined(DEBUG_SIGNAL)
941 fprintf(stderr, "do_sigreturn\n");
942#endif
579a97f7
FB
943 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
944 goto badframe;
66fb9763 945 /* set blocked signals */
9231944d
FB
946 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
947 goto badframe;
948 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
949 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
950 goto badframe;
951 }
66fb9763 952
9231944d 953 target_to_host_sigset_internal(&set, &target_set);
66fb9763 954 sigprocmask(SIG_SETMASK, &set, NULL);
3b46e624 955
66fb9763
FB
956 /* restore registers */
957 if (restore_sigcontext(env, &frame->sc, &eax))
958 goto badframe;
579a97f7 959 unlock_user_struct(frame, frame_addr, 0);
66fb9763
FB
960 return eax;
961
962badframe:
579a97f7 963 unlock_user_struct(frame, frame_addr, 0);
66fb9763
FB
964 force_sig(TARGET_SIGSEGV);
965 return 0;
966}
967
968long do_rt_sigreturn(CPUX86State *env)
969{
28be6234
FB
970 abi_ulong frame_addr;
971 struct rt_sigframe *frame;
66fb9763 972 sigset_t set;
66fb9763
FB
973 int eax;
974
28be6234
FB
975 frame_addr = env->regs[R_ESP] - 4;
976 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
977 goto badframe;
b8076a74 978 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
66fb9763 979 sigprocmask(SIG_SETMASK, &set, NULL);
5fafdf24 980
b8076a74 981 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
66fb9763
FB
982 goto badframe;
983
28be6234
FB
984 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
985 get_sp_from_cpustate(env)) == -EFAULT)
66fb9763 986 goto badframe;
a04e134a 987
28be6234 988 unlock_user_struct(frame, frame_addr, 0);
66fb9763
FB
989 return eax;
990
991badframe:
28be6234
FB
992 unlock_user_struct(frame, frame_addr, 0);
993 force_sig(TARGET_SIGSEGV);
66fb9763
FB
994 return 0;
995}
996
43fff238
FB
997#elif defined(TARGET_ARM)
998
999struct target_sigcontext {
992f48a0
BS
1000 abi_ulong trap_no;
1001 abi_ulong error_code;
1002 abi_ulong oldmask;
1003 abi_ulong arm_r0;
1004 abi_ulong arm_r1;
1005 abi_ulong arm_r2;
1006 abi_ulong arm_r3;
1007 abi_ulong arm_r4;
1008 abi_ulong arm_r5;
1009 abi_ulong arm_r6;
1010 abi_ulong arm_r7;
1011 abi_ulong arm_r8;
1012 abi_ulong arm_r9;
1013 abi_ulong arm_r10;
1014 abi_ulong arm_fp;
1015 abi_ulong arm_ip;
1016 abi_ulong arm_sp;
1017 abi_ulong arm_lr;
1018 abi_ulong arm_pc;
1019 abi_ulong arm_cpsr;
1020 abi_ulong fault_address;
43fff238
FB
1021};
1022
43fff238 1023struct target_ucontext {
992f48a0
BS
1024 abi_ulong tuc_flags;
1025 abi_ulong tuc_link;
b8076a74
FB
1026 target_stack_t tuc_stack;
1027 struct target_sigcontext tuc_mcontext;
1028 target_sigset_t tuc_sigmask; /* mask last for extensibility */
43fff238
FB
1029};
1030
1031struct sigframe
1032{
1033 struct target_sigcontext sc;
992f48a0
BS
1034 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1035 abi_ulong retcode;
43fff238
FB
1036};
1037
1038struct rt_sigframe
1039{
f8b0aa25
FB
1040 abi_ulong pinfo;
1041 abi_ulong puc;
43fff238
FB
1042 struct target_siginfo info;
1043 struct target_ucontext uc;
992f48a0 1044 abi_ulong retcode;
43fff238
FB
1045};
1046
1047#define TARGET_CONFIG_CPU_32 1
1048
1049/*
1050 * For ARM syscalls, we encode the syscall number into the instruction.
1051 */
1052#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1053#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1054
1055/*
1056 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1057 * need two 16-bit instructions.
1058 */
1059#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1060#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1061
992f48a0 1062static const abi_ulong retcodes[4] = {
43fff238
FB
1063 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1064 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1065};
1066
1067
1068#define __put_user_error(x,p,e) __put_user(x, p)
1069#define __get_user_error(x,p,e) __get_user(x, p)
1070
1071static inline int valid_user_regs(CPUState *regs)
1072{
1073 return 1;
1074}
1075
1076static int
1077setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
f8b0aa25 1078 CPUState *env, abi_ulong mask)
43fff238
FB
1079{
1080 int err = 0;
1081
1082 __put_user_error(env->regs[0], &sc->arm_r0, err);
1083 __put_user_error(env->regs[1], &sc->arm_r1, err);
1084 __put_user_error(env->regs[2], &sc->arm_r2, err);
1085 __put_user_error(env->regs[3], &sc->arm_r3, err);
1086 __put_user_error(env->regs[4], &sc->arm_r4, err);
1087 __put_user_error(env->regs[5], &sc->arm_r5, err);
1088 __put_user_error(env->regs[6], &sc->arm_r6, err);
1089 __put_user_error(env->regs[7], &sc->arm_r7, err);
1090 __put_user_error(env->regs[8], &sc->arm_r8, err);
1091 __put_user_error(env->regs[9], &sc->arm_r9, err);
1092 __put_user_error(env->regs[10], &sc->arm_r10, err);
1093 __put_user_error(env->regs[11], &sc->arm_fp, err);
1094 __put_user_error(env->regs[12], &sc->arm_ip, err);
1095 __put_user_error(env->regs[13], &sc->arm_sp, err);
1096 __put_user_error(env->regs[14], &sc->arm_lr, err);
1097 __put_user_error(env->regs[15], &sc->arm_pc, err);
1098#ifdef TARGET_CONFIG_CPU_32
b5ff1b31 1099 __put_user_error(cpsr_read(env), &sc->arm_cpsr, err);
43fff238
FB
1100#endif
1101
1102 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1103 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1104 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1105 __put_user_error(mask, &sc->oldmask, err);
1106
1107 return err;
1108}
1109
579a97f7 1110static inline abi_ulong
43fff238
FB
1111get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1112{
1113 unsigned long sp = regs->regs[13];
1114
43fff238
FB
1115 /*
1116 * This is the X/Open sanctioned signal stack switching.
1117 */
a04e134a
TS
1118 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1119 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
43fff238
FB
1120 /*
1121 * ATPCS B01 mandates 8-byte alignment
1122 */
579a97f7 1123 return (sp - framesize) & ~7;
43fff238
FB
1124}
1125
1126static int
1127setup_return(CPUState *env, struct emulated_sigaction *ka,
f8b0aa25 1128 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
43fff238 1129{
f8b0aa25 1130 abi_ulong handler = ka->sa._sa_handler;
992f48a0 1131 abi_ulong retcode;
75b680e5 1132 int thumb = handler & 1;
43fff238
FB
1133
1134 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
f8b0aa25 1135 retcode = ka->sa.sa_restorer;
43fff238
FB
1136 } else {
1137 unsigned int idx = thumb;
1138
1139 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1140 idx += 2;
1141
1142 if (__put_user(retcodes[idx], rc))
1143 return 1;
1144#if 0
992f48a0
BS
1145 flush_icache_range((abi_ulong)rc,
1146 (abi_ulong)(rc + 1));
43fff238 1147#endif
f8b0aa25 1148 retcode = rc_addr + thumb;
43fff238
FB
1149 }
1150
1151 env->regs[0] = usig;
f8b0aa25 1152 env->regs[13] = frame_addr;
43fff238
FB
1153 env->regs[14] = retcode;
1154 env->regs[15] = handler & (thumb ? ~1 : ~3);
75b680e5 1155 env->thumb = thumb;
43fff238 1156
b5ff1b31 1157#if 0
43fff238
FB
1158#ifdef TARGET_CONFIG_CPU_32
1159 env->cpsr = cpsr;
b5ff1b31 1160#endif
43fff238
FB
1161#endif
1162
1163 return 0;
1164}
1165
579a97f7 1166/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
43fff238
FB
1167static void setup_frame(int usig, struct emulated_sigaction *ka,
1168 target_sigset_t *set, CPUState *regs)
1169{
579a97f7
FB
1170 struct sigframe *frame;
1171 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
9231944d 1172 int i, err = 0;
43fff238 1173
579a97f7
FB
1174 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1175 return;
1176
43fff238
FB
1177 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1178
9231944d
FB
1179 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1180 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
579a97f7 1181 goto end;
43fff238
FB
1182 }
1183
1184 if (err == 0)
f8b0aa25
FB
1185 err = setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1186 frame_addr + offsetof(struct sigframe, retcode));
579a97f7
FB
1187
1188end:
1189 unlock_user_struct(frame, frame_addr, 1);
43fff238
FB
1190 // return err;
1191}
1192
579a97f7 1193/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
5fafdf24 1194static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
43fff238
FB
1195 target_siginfo_t *info,
1196 target_sigset_t *set, CPUState *env)
1197{
579a97f7
FB
1198 struct rt_sigframe *frame;
1199 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
a04e134a 1200 struct target_sigaltstack stack;
9231944d 1201 int i, err = 0;
f8b0aa25 1202 abi_ulong info_addr, uc_addr;
43fff238 1203
579a97f7 1204 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edf779ff
FB
1205 return /* 1 */;
1206
f8b0aa25
FB
1207 info_addr = frame_addr + offsetof(struct rt_sigframe, info);
1208 __put_user_error(info_addr, &frame->pinfo, err);
1209 uc_addr = frame_addr + offsetof(struct rt_sigframe, uc);
1210 __put_user_error(uc_addr, &frame->puc, err);
43fff238
FB
1211 err |= copy_siginfo_to_user(&frame->info, info);
1212
1213 /* Clear all the bits of the ucontext we don't use. */
53a5960a 1214 memset(&frame->uc, 0, offsetof(struct target_ucontext, tuc_mcontext));
43fff238 1215
a04e134a
TS
1216 memset(&stack, 0, sizeof(stack));
1217 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1218 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1219 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
775b58d8 1220 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
a04e134a 1221
b8076a74 1222 err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
43fff238 1223 env, set->sig[0]);
9231944d 1224 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
b8076a74 1225 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
579a97f7 1226 goto end;
9231944d 1227 }
43fff238
FB
1228
1229 if (err == 0)
f8b0aa25
FB
1230 err = setup_return(env, ka, &frame->retcode, frame_addr, usig,
1231 frame_addr + offsetof(struct rt_sigframe, retcode));
43fff238
FB
1232
1233 if (err == 0) {
1234 /*
1235 * For realtime signals we must also set the second and third
1236 * arguments for the signal handler.
1237 * -- Peter Maydell <[email protected]> 2000-12-06
1238 */
f8b0aa25
FB
1239 env->regs[1] = info_addr;
1240 env->regs[2] = uc_addr;
43fff238
FB
1241 }
1242
579a97f7
FB
1243end:
1244 unlock_user_struct(frame, frame_addr, 1);
1245
43fff238
FB
1246 // return err;
1247}
1248
1249static int
1250restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1251{
1252 int err = 0;
b5ff1b31 1253 uint32_t cpsr;
43fff238
FB
1254
1255 __get_user_error(env->regs[0], &sc->arm_r0, err);
1256 __get_user_error(env->regs[1], &sc->arm_r1, err);
1257 __get_user_error(env->regs[2], &sc->arm_r2, err);
1258 __get_user_error(env->regs[3], &sc->arm_r3, err);
1259 __get_user_error(env->regs[4], &sc->arm_r4, err);
1260 __get_user_error(env->regs[5], &sc->arm_r5, err);
1261 __get_user_error(env->regs[6], &sc->arm_r6, err);
1262 __get_user_error(env->regs[7], &sc->arm_r7, err);
1263 __get_user_error(env->regs[8], &sc->arm_r8, err);
1264 __get_user_error(env->regs[9], &sc->arm_r9, err);
1265 __get_user_error(env->regs[10], &sc->arm_r10, err);
1266 __get_user_error(env->regs[11], &sc->arm_fp, err);
1267 __get_user_error(env->regs[12], &sc->arm_ip, err);
1268 __get_user_error(env->regs[13], &sc->arm_sp, err);
1269 __get_user_error(env->regs[14], &sc->arm_lr, err);
1270 __get_user_error(env->regs[15], &sc->arm_pc, err);
1271#ifdef TARGET_CONFIG_CPU_32
b5ff1b31 1272 __get_user_error(cpsr, &sc->arm_cpsr, err);
75b680e5 1273 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
43fff238
FB
1274#endif
1275
1276 err |= !valid_user_regs(env);
1277
1278 return err;
1279}
1280
1281long do_sigreturn(CPUState *env)
1282{
f8b0aa25 1283 abi_ulong frame_addr;
43fff238
FB
1284 struct sigframe *frame;
1285 target_sigset_t set;
1286 sigset_t host_set;
9231944d 1287 int i;
43fff238
FB
1288
1289 /*
1290 * Since we stacked the signal on a 64-bit boundary,
1291 * then 'sp' should be word aligned here. If it's
1292 * not, then the user is trying to mess with us.
1293 */
1294 if (env->regs[13] & 7)
1295 goto badframe;
1296
f8b0aa25
FB
1297 frame_addr = env->regs[13];
1298 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1299 goto badframe;
43fff238 1300
9231944d
FB
1301 if (__get_user(set.sig[0], &frame->sc.oldmask))
1302 goto badframe;
1303 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1304 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1305 goto badframe;
1306 }
43fff238 1307
9231944d 1308 target_to_host_sigset_internal(&host_set, &set);
43fff238
FB
1309 sigprocmask(SIG_SETMASK, &host_set, NULL);
1310
1311 if (restore_sigcontext(env, &frame->sc))
1312 goto badframe;
1313
1314#if 0
1315 /* Send SIGTRAP if we're single-stepping */
1316 if (ptrace_cancel_bpt(current))
1317 send_sig(SIGTRAP, current, 1);
1318#endif
f8b0aa25
FB
1319 unlock_user_struct(frame, frame_addr, 0);
1320 return env->regs[0];
43fff238
FB
1321
1322badframe:
f8b0aa25 1323 unlock_user_struct(frame, frame_addr, 0);
43fff238
FB
1324 force_sig(SIGSEGV /* , current */);
1325 return 0;
1326}
1327
1328long do_rt_sigreturn(CPUState *env)
1329{
f8b0aa25 1330 abi_ulong frame_addr;
43fff238 1331 struct rt_sigframe *frame;
43fff238
FB
1332 sigset_t host_set;
1333
1334 /*
1335 * Since we stacked the signal on a 64-bit boundary,
1336 * then 'sp' should be word aligned here. If it's
1337 * not, then the user is trying to mess with us.
1338 */
1339 if (env->regs[13] & 7)
1340 goto badframe;
1341
f8b0aa25
FB
1342 frame_addr = env->regs[13];
1343 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1344 goto badframe;
43fff238 1345
b8076a74 1346 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
43fff238
FB
1347 sigprocmask(SIG_SETMASK, &host_set, NULL);
1348
b8076a74 1349 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
43fff238
FB
1350 goto badframe;
1351
f8b0aa25 1352 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
a04e134a
TS
1353 goto badframe;
1354
43fff238
FB
1355#if 0
1356 /* Send SIGTRAP if we're single-stepping */
1357 if (ptrace_cancel_bpt(current))
1358 send_sig(SIGTRAP, current, 1);
1359#endif
f8b0aa25 1360 unlock_user_struct(frame, frame_addr, 0);
43fff238
FB
1361 return env->regs[0];
1362
1363badframe:
f8b0aa25 1364 unlock_user_struct(frame, frame_addr, 0);
43fff238
FB
1365 force_sig(SIGSEGV /* , current */);
1366 return 0;
1367}
1368
6d5e216d 1369#elif defined(TARGET_SPARC)
80a9d035 1370
6d5e216d
FB
1371#define __SUNOS_MAXWIN 31
1372
1373/* This is what SunOS does, so shall I. */
1374struct target_sigcontext {
992f48a0 1375 abi_ulong sigc_onstack; /* state to restore */
6d5e216d 1376
992f48a0
BS
1377 abi_ulong sigc_mask; /* sigmask to restore */
1378 abi_ulong sigc_sp; /* stack pointer */
1379 abi_ulong sigc_pc; /* program counter */
1380 abi_ulong sigc_npc; /* next program counter */
1381 abi_ulong sigc_psr; /* for condition codes etc */
1382 abi_ulong sigc_g1; /* User uses these two registers */
1383 abi_ulong sigc_o0; /* within the trampoline code. */
6d5e216d
FB
1384
1385 /* Now comes information regarding the users window set
1386 * at the time of the signal.
1387 */
992f48a0 1388 abi_ulong sigc_oswins; /* outstanding windows */
6d5e216d
FB
1389
1390 /* stack ptrs for each regwin buf */
1391 char *sigc_spbuf[__SUNOS_MAXWIN];
1392
1393 /* Windows to restore after signal */
1394 struct {
992f48a0
BS
1395 abi_ulong locals[8];
1396 abi_ulong ins[8];
6d5e216d
FB
1397 } sigc_wbuf[__SUNOS_MAXWIN];
1398};
1399/* A Sparc stack frame */
1400struct sparc_stackf {
992f48a0
BS
1401 abi_ulong locals[8];
1402 abi_ulong ins[6];
6d5e216d 1403 struct sparc_stackf *fp;
992f48a0 1404 abi_ulong callers_pc;
6d5e216d 1405 char *structptr;
992f48a0
BS
1406 abi_ulong xargs[6];
1407 abi_ulong xxargs[1];
6d5e216d
FB
1408};
1409
1410typedef struct {
1411 struct {
992f48a0
BS
1412 abi_ulong psr;
1413 abi_ulong pc;
1414 abi_ulong npc;
1415 abi_ulong y;
1416 abi_ulong u_regs[16]; /* globals and ins */
6d5e216d
FB
1417 } si_regs;
1418 int si_mask;
1419} __siginfo_t;
1420
1421typedef struct {
1422 unsigned long si_float_regs [32];
1423 unsigned long si_fsr;
1424 unsigned long si_fpqdepth;
1425 struct {
1426 unsigned long *insn_addr;
1427 unsigned long insn;
1428 } si_fpqueue [16];
74ccb34e 1429} qemu_siginfo_fpu_t;
6d5e216d
FB
1430
1431
1432struct target_signal_frame {
1433 struct sparc_stackf ss;
1434 __siginfo_t info;
f8b0aa25 1435 abi_ulong fpu_save;
992f48a0
BS
1436 abi_ulong insns[2] __attribute__ ((aligned (8)));
1437 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1438 abi_ulong extra_size; /* Should be 0 */
74ccb34e 1439 qemu_siginfo_fpu_t fpu_state;
6d5e216d
FB
1440};
1441struct target_rt_signal_frame {
1442 struct sparc_stackf ss;
1443 siginfo_t info;
992f48a0 1444 abi_ulong regs[20];
6d5e216d 1445 sigset_t mask;
f8b0aa25 1446 abi_ulong fpu_save;
6d5e216d
FB
1447 unsigned int insns[2];
1448 stack_t stack;
1449 unsigned int extra_size; /* Should be 0 */
74ccb34e 1450 qemu_siginfo_fpu_t fpu_state;
6d5e216d
FB
1451};
1452
e80cfcfc
FB
1453#define UREG_O0 16
1454#define UREG_O6 22
1455#define UREG_I0 0
1456#define UREG_I1 1
1457#define UREG_I2 2
5bfb56b2
BS
1458#define UREG_I3 3
1459#define UREG_I4 4
1460#define UREG_I5 5
e80cfcfc
FB
1461#define UREG_I6 6
1462#define UREG_I7 7
1463#define UREG_L0 8
6d5e216d
FB
1464#define UREG_FP UREG_I6
1465#define UREG_SP UREG_O6
1466
459a4017
FB
1467static inline abi_ulong get_sigframe(struct emulated_sigaction *sa,
1468 CPUState *env, unsigned long framesize)
6d5e216d 1469{
459a4017 1470 abi_ulong sp;
6d5e216d
FB
1471
1472 sp = env->regwptr[UREG_FP];
6d5e216d
FB
1473
1474 /* This is the X/Open sanctioned signal stack switching. */
a04e134a
TS
1475 if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1476 if (!on_sig_stack(sp)
1477 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1478 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
6d5e216d 1479 }
459a4017 1480 return sp - framesize;
6d5e216d
FB
1481}
1482
1483static int
992f48a0 1484setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
6d5e216d
FB
1485{
1486 int err = 0, i;
1487
6d5e216d 1488 err |= __put_user(env->psr, &si->si_regs.psr);
6d5e216d
FB
1489 err |= __put_user(env->pc, &si->si_regs.pc);
1490 err |= __put_user(env->npc, &si->si_regs.npc);
1491 err |= __put_user(env->y, &si->si_regs.y);
a315a145 1492 for (i=0; i < 8; i++) {
6d5e216d
FB
1493 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1494 }
a315a145 1495 for (i=0; i < 8; i++) {
e80cfcfc 1496 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
6d5e216d 1497 }
6d5e216d
FB
1498 err |= __put_user(mask, &si->si_mask);
1499 return err;
1500}
e80cfcfc 1501
80a9d035 1502#if 0
6d5e216d
FB
1503static int
1504setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1505 CPUState *env, unsigned long mask)
1506{
1507 int err = 0;
1508
1509 err |= __put_user(mask, &sc->sigc_mask);
1510 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1511 err |= __put_user(env->pc, &sc->sigc_pc);
1512 err |= __put_user(env->npc, &sc->sigc_npc);
1513 err |= __put_user(env->psr, &sc->sigc_psr);
1514 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1515 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1516
1517 return err;
1518}
80a9d035 1519#endif
6d5e216d
FB
1520#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1521
1522static void setup_frame(int sig, struct emulated_sigaction *ka,
1523 target_sigset_t *set, CPUState *env)
1524{
459a4017 1525 abi_ulong sf_addr;
6d5e216d
FB
1526 struct target_signal_frame *sf;
1527 int sigframe_size, err, i;
1528
1529 /* 1. Make sure everything is clean */
1530 //synchronize_user_stack();
1531
1532 sigframe_size = NF_ALIGNEDSZ;
459a4017 1533 sf_addr = get_sigframe(ka, env, sigframe_size);
6d5e216d 1534
459a4017
FB
1535 sf = lock_user(VERIFY_WRITE, sf_addr,
1536 sizeof(struct target_signal_frame), 0);
1537 if (!sf)
1538 goto sigsegv;
1539
e80cfcfc 1540 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
6d5e216d
FB
1541#if 0
1542 if (invalid_frame_pointer(sf, sigframe_size))
1543 goto sigill_and_return;
1544#endif
1545 /* 2. Save the current process state */
1546 err = setup___siginfo(&sf->info, env, set->sig[0]);
1547 err |= __put_user(0, &sf->extra_size);
1548
1549 //err |= save_fpu_state(regs, &sf->fpu_state);
1550 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1551
1552 err |= __put_user(set->sig[0], &sf->info.si_mask);
1553 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1554 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1555 }
1556
a315a145 1557 for (i = 0; i < 8; i++) {
e80cfcfc 1558 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
6d5e216d 1559 }
a315a145 1560 for (i = 0; i < 8; i++) {
e80cfcfc 1561 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
6d5e216d 1562 }
6d5e216d
FB
1563 if (err)
1564 goto sigsegv;
1565
1566 /* 3. signal handler back-trampoline and parameters */
459a4017 1567 env->regwptr[UREG_FP] = sf_addr;
6d5e216d 1568 env->regwptr[UREG_I0] = sig;
459a4017
FB
1569 env->regwptr[UREG_I1] = sf_addr +
1570 offsetof(struct target_signal_frame, info);
1571 env->regwptr[UREG_I2] = sf_addr +
1572 offsetof(struct target_signal_frame, info);
6d5e216d
FB
1573
1574 /* 4. signal handler */
459a4017 1575 env->pc = ka->sa._sa_handler;
6d5e216d
FB
1576 env->npc = (env->pc + 4);
1577 /* 5. return to kernel instructions */
1578 if (ka->sa.sa_restorer)
459a4017 1579 env->regwptr[UREG_I7] = ka->sa.sa_restorer;
6d5e216d 1580 else {
775b58d8 1581 uint32_t val32;
459a4017
FB
1582
1583 env->regwptr[UREG_I7] = sf_addr +
1584 offsetof(struct target_signal_frame, insns) - 2 * 4;
6d5e216d
FB
1585
1586 /* mov __NR_sigreturn, %g1 */
775b58d8
FB
1587 val32 = 0x821020d8;
1588 err |= __put_user(val32, &sf->insns[0]);
6d5e216d
FB
1589
1590 /* t 0x10 */
775b58d8
FB
1591 val32 = 0x91d02010;
1592 err |= __put_user(val32, &sf->insns[1]);
6d5e216d
FB
1593 if (err)
1594 goto sigsegv;
1595
1596 /* Flush instruction space. */
1597 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
80a9d035 1598 // tb_flush(env);
6d5e216d 1599 }
459a4017 1600 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
6d5e216d 1601 return;
459a4017
FB
1602#if 0
1603sigill_and_return:
6d5e216d 1604 force_sig(TARGET_SIGILL);
459a4017 1605#endif
6d5e216d 1606sigsegv:
e80cfcfc 1607 //fprintf(stderr, "force_sig\n");
459a4017 1608 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
6d5e216d
FB
1609 force_sig(TARGET_SIGSEGV);
1610}
1611static inline int
74ccb34e 1612restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
6d5e216d
FB
1613{
1614 int err;
1615#if 0
1616#ifdef CONFIG_SMP
1617 if (current->flags & PF_USEDFPU)
1618 regs->psr &= ~PSR_EF;
1619#else
1620 if (current == last_task_used_math) {
1621 last_task_used_math = 0;
1622 regs->psr &= ~PSR_EF;
1623 }
1624#endif
1625 current->used_math = 1;
1626 current->flags &= ~PF_USEDFPU;
1627#endif
1628#if 0
1629 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1630 return -EFAULT;
1631#endif
1632
fafffaef
FB
1633#if 0
1634 /* XXX: incorrect */
6d5e216d
FB
1635 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1636 (sizeof(unsigned long) * 32));
fafffaef 1637#endif
6d5e216d
FB
1638 err |= __get_user(env->fsr, &fpu->si_fsr);
1639#if 0
1640 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1641 if (current->thread.fpqdepth != 0)
1642 err |= __copy_from_user(&current->thread.fpqueue[0],
1643 &fpu->si_fpqueue[0],
1644 ((sizeof(unsigned long) +
1645 (sizeof(unsigned long *)))*16));
1646#endif
1647 return err;
1648}
1649
1650
5fafdf24 1651static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
6d5e216d
FB
1652 target_siginfo_t *info,
1653 target_sigset_t *set, CPUState *env)
1654{
1655 fprintf(stderr, "setup_rt_frame: not implemented\n");
1656}
1657
1658long do_sigreturn(CPUState *env)
1659{
f8b0aa25 1660 abi_ulong sf_addr;
6d5e216d 1661 struct target_signal_frame *sf;
e80cfcfc 1662 uint32_t up_psr, pc, npc;
6d5e216d 1663 target_sigset_t set;
e80cfcfc 1664 sigset_t host_set;
f8b0aa25 1665 abi_ulong fpu_save_addr;
e80cfcfc 1666 int err, i;
6d5e216d 1667
f8b0aa25
FB
1668 sf_addr = env->regwptr[UREG_FP];
1669 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1670 goto segv_and_exit;
80a9d035 1671#if 0
e80cfcfc
FB
1672 fprintf(stderr, "sigreturn\n");
1673 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
80a9d035 1674#endif
e80cfcfc 1675 //cpu_dump_state(env, stderr, fprintf, 0);
6d5e216d
FB
1676
1677 /* 1. Make sure we are not getting garbage from the user */
6d5e216d 1678
f8b0aa25 1679 if (sf_addr & 3)
6d5e216d
FB
1680 goto segv_and_exit;
1681
1682 err = __get_user(pc, &sf->info.si_regs.pc);
1683 err |= __get_user(npc, &sf->info.si_regs.npc);
1684
6d5e216d
FB
1685 if ((pc | npc) & 3)
1686 goto segv_and_exit;
1687
1688 /* 2. Restore the state */
e80cfcfc
FB
1689 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1690
6d5e216d 1691 /* User can only change condition codes and FPU enabling in %psr. */
a315a145
FB
1692 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1693 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1694
1695 env->pc = pc;
1696 env->npc = npc;
e80cfcfc 1697 err |= __get_user(env->y, &sf->info.si_regs.y);
a315a145 1698 for (i=0; i < 8; i++) {
e80cfcfc
FB
1699 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1700 }
a315a145 1701 for (i=0; i < 8; i++) {
e80cfcfc
FB
1702 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1703 }
6d5e216d 1704
f8b0aa25 1705 err |= __get_user(fpu_save_addr, &sf->fpu_save);
6d5e216d 1706
e80cfcfc
FB
1707 //if (fpu_save)
1708 // err |= restore_fpu_state(env, fpu_save);
6d5e216d
FB
1709
1710 /* This is pretty much atomic, no amount locking would prevent
1711 * the races which exist anyways.
1712 */
1713 err |= __get_user(set.sig[0], &sf->info.si_mask);
e80cfcfc
FB
1714 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1715 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1716 }
1717
1718 target_to_host_sigset_internal(&host_set, &set);
1719 sigprocmask(SIG_SETMASK, &host_set, NULL);
6d5e216d
FB
1720
1721 if (err)
1722 goto segv_and_exit;
f8b0aa25 1723 unlock_user_struct(sf, sf_addr, 0);
6d5e216d
FB
1724 return env->regwptr[0];
1725
1726segv_and_exit:
f8b0aa25 1727 unlock_user_struct(sf, sf_addr, 0);
6d5e216d
FB
1728 force_sig(TARGET_SIGSEGV);
1729}
1730
1731long do_rt_sigreturn(CPUState *env)
1732{
1733 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
f8b0aa25 1734 return -TARGET_ENOSYS;
6d5e216d
FB
1735}
1736
459a4017 1737#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
5bfb56b2
BS
1738#define MC_TSTATE 0
1739#define MC_PC 1
1740#define MC_NPC 2
1741#define MC_Y 3
1742#define MC_G1 4
1743#define MC_G2 5
1744#define MC_G3 6
1745#define MC_G4 7
1746#define MC_G5 8
1747#define MC_G6 9
1748#define MC_G7 10
1749#define MC_O0 11
1750#define MC_O1 12
1751#define MC_O2 13
1752#define MC_O3 14
1753#define MC_O4 15
1754#define MC_O5 16
1755#define MC_O6 17
1756#define MC_O7 18
1757#define MC_NGREG 19
1758
992f48a0 1759typedef abi_ulong target_mc_greg_t;
5bfb56b2
BS
1760typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1761
1762struct target_mc_fq {
992f48a0 1763 abi_ulong *mcfq_addr;
5bfb56b2
BS
1764 uint32_t mcfq_insn;
1765};
1766
1767struct target_mc_fpu {
1768 union {
1769 uint32_t sregs[32];
1770 uint64_t dregs[32];
1771 //uint128_t qregs[16];
1772 } mcfpu_fregs;
992f48a0
BS
1773 abi_ulong mcfpu_fsr;
1774 abi_ulong mcfpu_fprs;
1775 abi_ulong mcfpu_gsr;
5bfb56b2
BS
1776 struct target_mc_fq *mcfpu_fq;
1777 unsigned char mcfpu_qcnt;
1778 unsigned char mcfpu_qentsz;
1779 unsigned char mcfpu_enab;
1780};
1781typedef struct target_mc_fpu target_mc_fpu_t;
1782
1783typedef struct {
1784 target_mc_gregset_t mc_gregs;
1785 target_mc_greg_t mc_fp;
1786 target_mc_greg_t mc_i7;
1787 target_mc_fpu_t mc_fpregs;
1788} target_mcontext_t;
1789
1790struct target_ucontext {
1791 struct target_ucontext *uc_link;
992f48a0 1792 abi_ulong uc_flags;
5bfb56b2
BS
1793 target_sigset_t uc_sigmask;
1794 target_mcontext_t uc_mcontext;
1795};
1796
1797/* A V9 register window */
1798struct target_reg_window {
992f48a0
BS
1799 abi_ulong locals[8];
1800 abi_ulong ins[8];
5bfb56b2
BS
1801};
1802
1803#define TARGET_STACK_BIAS 2047
1804
1805/* {set, get}context() needed for 64-bit SparcLinux userland. */
1806void sparc64_set_context(CPUSPARCState *env)
1807{
459a4017
FB
1808 abi_ulong ucp_addr;
1809 struct target_ucontext *ucp;
5bfb56b2 1810 target_mc_gregset_t *grp;
992f48a0 1811 abi_ulong pc, npc, tstate;
459a4017 1812 abi_ulong fp, i7, w_addr;
5bfb56b2
BS
1813 unsigned char fenab;
1814 int err;
1815 unsigned int i;
5bfb56b2 1816
459a4017
FB
1817 ucp_addr = env->regwptr[UREG_I0];
1818 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
1819 goto do_sigsegv;
5bfb56b2 1820 grp = &ucp->uc_mcontext.mc_gregs;
579a97f7
FB
1821 err = __get_user(pc, &((*grp)[MC_PC]));
1822 err |= __get_user(npc, &((*grp)[MC_NPC]));
5bfb56b2
BS
1823 if (err || ((pc | npc) & 3))
1824 goto do_sigsegv;
1825 if (env->regwptr[UREG_I1]) {
1826 target_sigset_t target_set;
1827 sigset_t set;
1828
1829 if (TARGET_NSIG_WORDS == 1) {
579a97f7 1830 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
5bfb56b2
BS
1831 goto do_sigsegv;
1832 } else {
459a4017
FB
1833 abi_ulong *src, *dst;
1834 src = ucp->uc_sigmask.sig;
1835 dst = target_set.sig;
992f48a0 1836 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
5bfb56b2 1837 i++, dst++, src++)
459a4017 1838 err |= __get_user(*dst, src);
5bfb56b2
BS
1839 if (err)
1840 goto do_sigsegv;
1841 }
1842 target_to_host_sigset_internal(&set, &target_set);
1843 sigprocmask(SIG_SETMASK, &set, NULL);
1844 }
1845 env->pc = pc;
1846 env->npc = npc;
579a97f7
FB
1847 err |= __get_user(env->y, &((*grp)[MC_Y]));
1848 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
5bfb56b2
BS
1849 env->asi = (tstate >> 24) & 0xff;
1850 PUT_CCR(env, tstate >> 32);
1851 PUT_CWP64(env, tstate & 0x1f);
579a97f7
FB
1852 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
1853 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
1854 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
1855 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
1856 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
1857 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
1858 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
1859 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
1860 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
1861 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
1862 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
1863 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
1864 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
1865 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
1866 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
1867
1868 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
1869 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
579a97f7 1870
459a4017
FB
1871 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
1872 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
1873 abi_ulong) != 0)
1874 goto do_sigsegv;
1875 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
1876 abi_ulong) != 0)
1877 goto do_sigsegv;
579a97f7
FB
1878 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
1879 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
459a4017
FB
1880 {
1881 uint32_t *src, *dst;
1882 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
1883 dst = env->fpr;
1884 /* XXX: check that the CPU storage is the same as user context */
1885 for (i = 0; i < 64; i++, dst++, src++)
1886 err |= __get_user(*dst, src);
1887 }
579a97f7
FB
1888 err |= __get_user(env->fsr,
1889 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
1890 err |= __get_user(env->gsr,
1891 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
5bfb56b2
BS
1892 if (err)
1893 goto do_sigsegv;
459a4017 1894 unlock_user_struct(ucp, ucp_addr, 0);
5bfb56b2
BS
1895 return;
1896 do_sigsegv:
459a4017 1897 unlock_user_struct(ucp, ucp_addr, 0);
5bfb56b2
BS
1898 force_sig(SIGSEGV);
1899}
1900
1901void sparc64_get_context(CPUSPARCState *env)
1902{
459a4017
FB
1903 abi_ulong ucp_addr;
1904 struct target_ucontext *ucp;
5bfb56b2
BS
1905 target_mc_gregset_t *grp;
1906 target_mcontext_t *mcp;
459a4017 1907 abi_ulong fp, i7, w_addr;
5bfb56b2
BS
1908 int err;
1909 unsigned int i;
5bfb56b2
BS
1910 target_sigset_t target_set;
1911 sigset_t set;
1912
459a4017
FB
1913 ucp_addr = env->regwptr[UREG_I0];
1914 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
1915 goto do_sigsegv;
1916
5bfb56b2
BS
1917 mcp = &ucp->uc_mcontext;
1918 grp = &mcp->mc_gregs;
1919
1920 /* Skip over the trap instruction, first. */
1921 env->pc = env->npc;
1922 env->npc += 4;
1923
1924 err = 0;
1925
1926 sigprocmask(0, NULL, &set);
1927 host_to_target_sigset_internal(&target_set, &set);
459a4017 1928 if (TARGET_NSIG_WORDS == 1) {
579a97f7
FB
1929 err |= __put_user(target_set.sig[0],
1930 (abi_ulong *)&ucp->uc_sigmask);
459a4017
FB
1931 } else {
1932 abi_ulong *src, *dst;
1933 src = target_set.sig;
1934 dst = ucp->uc_sigmask.sig;
992f48a0 1935 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
5bfb56b2 1936 i++, dst++, src++)
459a4017 1937 err |= __put_user(*src, dst);
5bfb56b2
BS
1938 if (err)
1939 goto do_sigsegv;
1940 }
1941
459a4017
FB
1942 /* XXX: tstate must be saved properly */
1943 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
579a97f7
FB
1944 err |= __put_user(env->pc, &((*grp)[MC_PC]));
1945 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
1946 err |= __put_user(env->y, &((*grp)[MC_Y]));
1947 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
1948 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
1949 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
1950 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
1951 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
1952 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
1953 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
1954 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
1955 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
1956 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
1957 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
1958 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
1959 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
1960 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
1961 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
1962
459a4017
FB
1963 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
1964 fp = i7 = 0;
1965 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
1966 abi_ulong) != 0)
1967 goto do_sigsegv;
1968 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
1969 abi_ulong) != 0)
1970 goto do_sigsegv;
579a97f7
FB
1971 err |= __put_user(fp, &(mcp->mc_fp));
1972 err |= __put_user(i7, &(mcp->mc_i7));
5bfb56b2 1973
459a4017
FB
1974 {
1975 uint32_t *src, *dst;
1976 src = env->fpr;
1977 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
1978 /* XXX: check that the CPU storage is the same as user context */
1979 for (i = 0; i < 64; i++, dst++, src++)
1980 err |= __put_user(*src, dst);
1981 }
579a97f7
FB
1982 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
1983 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
1984 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
5bfb56b2
BS
1985
1986 if (err)
1987 goto do_sigsegv;
459a4017 1988 unlock_user_struct(ucp, ucp_addr, 1);
5bfb56b2
BS
1989 return;
1990 do_sigsegv:
459a4017 1991 unlock_user_struct(ucp, ucp_addr, 1);
5bfb56b2
BS
1992 force_sig(SIGSEGV);
1993}
1994#endif
d26bc211 1995#elif defined(TARGET_ABI_MIPSN64)
540635ba
TS
1996
1997# warning signal handling not implemented
1998
1999static void setup_frame(int sig, struct emulated_sigaction *ka,
2000 target_sigset_t *set, CPUState *env)
2001{
2002 fprintf(stderr, "setup_frame: not implemented\n");
2003}
2004
2005static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2006 target_siginfo_t *info,
2007 target_sigset_t *set, CPUState *env)
2008{
2009 fprintf(stderr, "setup_rt_frame: not implemented\n");
2010}
2011
2012long do_sigreturn(CPUState *env)
2013{
2014 fprintf(stderr, "do_sigreturn: not implemented\n");
f8b0aa25 2015 return -TARGET_ENOSYS;
540635ba
TS
2016}
2017
2018long do_rt_sigreturn(CPUState *env)
2019{
2020 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
f8b0aa25 2021 return -TARGET_ENOSYS;
540635ba
TS
2022}
2023
d26bc211 2024#elif defined(TARGET_ABI_MIPSN32)
540635ba
TS
2025
2026# warning signal handling not implemented
2027
2028static void setup_frame(int sig, struct emulated_sigaction *ka,
2029 target_sigset_t *set, CPUState *env)
2030{
2031 fprintf(stderr, "setup_frame: not implemented\n");
2032}
2033
2034static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2035 target_siginfo_t *info,
2036 target_sigset_t *set, CPUState *env)
2037{
2038 fprintf(stderr, "setup_rt_frame: not implemented\n");
2039}
2040
2041long do_sigreturn(CPUState *env)
2042{
2043 fprintf(stderr, "do_sigreturn: not implemented\n");
f8b0aa25 2044 return -TARGET_ENOSYS;
540635ba
TS
2045}
2046
2047long do_rt_sigreturn(CPUState *env)
2048{
2049 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
f8b0aa25 2050 return -TARGET_ENOSYS;
540635ba
TS
2051}
2052
d26bc211 2053#elif defined(TARGET_ABI_MIPSO32)
106ec879
FB
2054
2055struct target_sigcontext {
2056 uint32_t sc_regmask; /* Unused */
2057 uint32_t sc_status;
2058 uint64_t sc_pc;
2059 uint64_t sc_regs[32];
2060 uint64_t sc_fpregs[32];
2061 uint32_t sc_ownedfp; /* Unused */
2062 uint32_t sc_fpc_csr;
2063 uint32_t sc_fpc_eir; /* Unused */
2064 uint32_t sc_used_math;
2065 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2066 uint64_t sc_mdhi;
2067 uint64_t sc_mdlo;
2068 target_ulong sc_hi1; /* Was sc_cause */
2069 target_ulong sc_lo1; /* Was sc_badvaddr */
2070 target_ulong sc_hi2; /* Was sc_sigset[4] */
2071 target_ulong sc_lo2;
2072 target_ulong sc_hi3;
2073 target_ulong sc_lo3;
2074};
2075
2076struct sigframe {
2077 uint32_t sf_ass[4]; /* argument save space for o32 */
2078 uint32_t sf_code[2]; /* signal trampoline */
2079 struct target_sigcontext sf_sc;
2080 target_sigset_t sf_mask;
2081};
2082
2083/* Install trampoline to jump back from signal handler */
2084static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2085{
2086 int err;
2087
2088 /*
2089 * Set up the return code ...
2090 *
2091 * li v0, __NR__foo_sigreturn
2092 * syscall
2093 */
2094
2095 err = __put_user(0x24020000 + syscall, tramp + 0);
2096 err |= __put_user(0x0000000c , tramp + 1);
2097 /* flush_cache_sigtramp((unsigned long) tramp); */
2098 return err;
2099}
2100
2101static inline int
2102setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2103{
2104 int err = 0;
2105
ead9360e 2106 err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
106ec879 2107
ead9360e 2108#define save_gp_reg(i) do { \
d0dc7dc3 2109 err |= __put_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
106ec879
FB
2110 } while(0)
2111 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2112 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2113 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2114 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2115 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2116 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2117 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2118 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2119 save_gp_reg(31);
388bb21a 2120#undef save_gp_reg
106ec879 2121
d0dc7dc3
TS
2122 err |= __put_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2123 err |= __put_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
106ec879
FB
2124
2125 /* Not used yet, but might be useful if we ever have DSP suppport */
2126#if 0
2127 if (cpu_has_dsp) {
2128 err |= __put_user(mfhi1(), &sc->sc_hi1);
2129 err |= __put_user(mflo1(), &sc->sc_lo1);
2130 err |= __put_user(mfhi2(), &sc->sc_hi2);
2131 err |= __put_user(mflo2(), &sc->sc_lo2);
2132 err |= __put_user(mfhi3(), &sc->sc_hi3);
2133 err |= __put_user(mflo3(), &sc->sc_lo3);
2134 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2135 }
2136 /* same with 64 bit */
388bb21a 2137#ifdef CONFIG_64BIT
106ec879
FB
2138 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2139 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2140 if (cpu_has_dsp) {
2141 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2142 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2143 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2144 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2145 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2146 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2147 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2148 }
388bb21a
TS
2149#endif
2150#endif
106ec879 2151
388bb21a 2152#if 0
106ec879
FB
2153 err |= __put_user(!!used_math(), &sc->sc_used_math);
2154
2155 if (!used_math())
2156 goto out;
2157
2158 /*
2159 * Save FPU state to signal context. Signal handler will "inherit"
2160 * current FPU state.
2161 */
2162 preempt_disable();
2163
2164 if (!is_fpu_owner()) {
2165 own_fpu();
2166 restore_fp(current);
2167 }
2168 err |= save_fp_context(sc);
2169
2170 preempt_enable();
2171 out:
2172#endif
2173 return err;
2174}
2175
2176static inline int
2177restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2178{
2179 int err = 0;
2180
2181 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2182
d0dc7dc3
TS
2183 err |= __get_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2184 err |= __get_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
106ec879 2185
ead9360e 2186#define restore_gp_reg(i) do { \
d0dc7dc3 2187 err |= __get_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
106ec879
FB
2188 } while(0)
2189 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2190 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2191 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2192 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2193 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2194 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2195 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2196 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2197 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2198 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2199 restore_gp_reg(31);
388bb21a 2200#undef restore_gp_reg
106ec879
FB
2201
2202#if 0
2203 if (cpu_has_dsp) {
2204 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2205 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2206 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2207 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2208 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2209 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2210 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2211 }
388bb21a 2212#ifdef CONFIG_64BIT
106ec879
FB
2213 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2214 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2215 if (cpu_has_dsp) {
2216 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2217 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2218 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2219 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2220 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2221 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2222 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2223 }
388bb21a 2224#endif
106ec879
FB
2225
2226 err |= __get_user(used_math, &sc->sc_used_math);
2227 conditional_used_math(used_math);
2228
2229 preempt_disable();
2230
2231 if (used_math()) {
2232 /* restore fpu context if we have used it before */
2233 own_fpu();
2234 err |= restore_fp_context(sc);
2235 } else {
2236 /* signal handler may have used FPU. Give it up. */
2237 lose_fpu();
2238 }
2239
2240 preempt_enable();
2241#endif
2242 return err;
2243}
2244/*
2245 * Determine which stack to use..
2246 */
579a97f7 2247static inline abi_ulong
106ec879
FB
2248get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
2249{
2250 unsigned long sp;
2251
2252 /* Default to using normal stack */
d0dc7dc3 2253 sp = regs->gpr[regs->current_tc][29];
106ec879
FB
2254
2255 /*
2256 * FPU emulator may have it's own trampoline active just
2257 * above the user stack, 16-bytes before the next lowest
2258 * 16 byte boundary. Try to avoid trashing it.
2259 */
2260 sp -= 32;
2261
106ec879 2262 /* This is the X/Open sanctioned signal stack switching. */
a04e134a
TS
2263 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2264 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2265 }
106ec879 2266
579a97f7 2267 return (sp - frame_size) & ~7;
106ec879
FB
2268}
2269
579a97f7 2270/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
5fafdf24 2271static void setup_frame(int sig, struct emulated_sigaction * ka,
579a97f7 2272 target_sigset_t *set, CPUState *regs)
106ec879
FB
2273{
2274 struct sigframe *frame;
579a97f7 2275 abi_ulong frame_addr;
106ec879
FB
2276 int i;
2277
579a97f7
FB
2278 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2279 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
106ec879
FB
2280 goto give_sigsegv;
2281
2282 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2283
2284 if(setup_sigcontext(regs, &frame->sf_sc))
2285 goto give_sigsegv;
2286
2287 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2288 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2289 goto give_sigsegv;
2290 }
2291
2292 /*
2293 * Arguments to signal handler:
2294 *
2295 * a0 = signal number
2296 * a1 = 0 (should be cause)
2297 * a2 = pointer to struct sigcontext
2298 *
2299 * $25 and PC point to the signal handler, $29 points to the
2300 * struct sigframe.
2301 */
d0dc7dc3
TS
2302 regs->gpr[regs->current_tc][ 4] = sig;
2303 regs->gpr[regs->current_tc][ 5] = 0;
2304 regs->gpr[regs->current_tc][ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2305 regs->gpr[regs->current_tc][29] = frame_addr;
2306 regs->gpr[regs->current_tc][31] = frame_addr + offsetof(struct sigframe, sf_code);
106ec879
FB
2307 /* The original kernel code sets CP0_EPC to the handler
2308 * since it returns to userland using eret
2309 * we cannot do this here, and we must set PC directly */
d0dc7dc3 2310 regs->PC[regs->current_tc] = regs->gpr[regs->current_tc][25] = ka->sa._sa_handler;
579a97f7 2311 unlock_user_struct(frame, frame_addr, 1);
106ec879
FB
2312 return;
2313
2314give_sigsegv:
579a97f7 2315 unlock_user_struct(frame, frame_addr, 1);
106ec879 2316 force_sig(TARGET_SIGSEGV/*, current*/);
5fafdf24 2317 return;
106ec879
FB
2318}
2319
2320long do_sigreturn(CPUState *regs)
2321{
388bb21a 2322 struct sigframe *frame;
579a97f7 2323 abi_ulong frame_addr;
388bb21a
TS
2324 sigset_t blocked;
2325 target_sigset_t target_set;
2326 int i;
106ec879
FB
2327
2328#if defined(DEBUG_SIGNAL)
388bb21a 2329 fprintf(stderr, "do_sigreturn\n");
106ec879 2330#endif
d0dc7dc3 2331 frame_addr = regs->gpr[regs->current_tc][29];
579a97f7 2332 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
106ec879
FB
2333 goto badframe;
2334
388bb21a 2335 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
106ec879
FB
2336 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2337 goto badframe;
388bb21a 2338 }
106ec879 2339
388bb21a
TS
2340 target_to_host_sigset_internal(&blocked, &target_set);
2341 sigprocmask(SIG_SETMASK, &blocked, NULL);
106ec879 2342
388bb21a 2343 if (restore_sigcontext(regs, &frame->sf_sc))
106ec879
FB
2344 goto badframe;
2345
2346#if 0
388bb21a
TS
2347 /*
2348 * Don't let your children do this ...
2349 */
2350 __asm__ __volatile__(
106ec879
FB
2351 "move\t$29, %0\n\t"
2352 "j\tsyscall_exit"
2353 :/* no outputs */
2354 :"r" (&regs));
388bb21a 2355 /* Unreached */
106ec879 2356#endif
3b46e624 2357
ead9360e 2358 regs->PC[regs->current_tc] = regs->CP0_EPC;
388bb21a 2359 /* I am not sure this is right, but it seems to work
106ec879
FB
2360 * maybe a problem with nested signals ? */
2361 regs->CP0_EPC = 0;
2362 return 0;
2363
2364badframe:
388bb21a
TS
2365 force_sig(TARGET_SIGSEGV/*, current*/);
2366 return 0;
106ec879
FB
2367}
2368
5fafdf24 2369static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
106ec879
FB
2370 target_siginfo_t *info,
2371 target_sigset_t *set, CPUState *env)
2372{
2373 fprintf(stderr, "setup_rt_frame: not implemented\n");
2374}
2375
2376long do_rt_sigreturn(CPUState *env)
2377{
2378 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
f8b0aa25 2379 return -TARGET_ENOSYS;
106ec879 2380}
6d5e216d 2381
c3b5bc8a
TS
2382#elif defined(TARGET_SH4)
2383
2384/*
2385 * code and data structures from linux kernel:
2386 * include/asm-sh/sigcontext.h
2387 * arch/sh/kernel/signal.c
2388 */
2389
2390struct target_sigcontext {
2391 target_ulong oldmask;
2392
2393 /* CPU registers */
2394 target_ulong sc_gregs[16];
2395 target_ulong sc_pc;
2396 target_ulong sc_pr;
2397 target_ulong sc_sr;
2398 target_ulong sc_gbr;
2399 target_ulong sc_mach;
2400 target_ulong sc_macl;
2401
2402 /* FPU registers */
2403 target_ulong sc_fpregs[16];
2404 target_ulong sc_xfpregs[16];
2405 unsigned int sc_fpscr;
2406 unsigned int sc_fpul;
2407 unsigned int sc_ownedfp;
2408};
2409
2410struct target_sigframe
2411{
2412 struct target_sigcontext sc;
2413 target_ulong extramask[TARGET_NSIG_WORDS-1];
2414 uint16_t retcode[3];
2415};
2416
2417
2418struct target_ucontext {
2419 target_ulong uc_flags;
2420 struct target_ucontext *uc_link;
2421 target_stack_t uc_stack;
2422 struct target_sigcontext uc_mcontext;
2423 target_sigset_t uc_sigmask; /* mask last for extensibility */
2424};
2425
2426struct target_rt_sigframe
2427{
2428 struct target_siginfo info;
2429 struct target_ucontext uc;
2430 uint16_t retcode[3];
2431};
2432
2433
2434#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2435#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2436
2437static abi_ulong get_sigframe(struct emulated_sigaction *ka,
2438 unsigned long sp, size_t frame_size)
2439{
2440 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2441 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2442 }
2443
2444 return (sp - frame_size) & -8ul;
2445}
2446
2447static int setup_sigcontext(struct target_sigcontext *sc,
2448 CPUState *regs, unsigned long mask)
2449{
2450 int err = 0;
2451
2452#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2453 COPY(gregs[0]); COPY(gregs[1]);
2454 COPY(gregs[2]); COPY(gregs[3]);
2455 COPY(gregs[4]); COPY(gregs[5]);
2456 COPY(gregs[6]); COPY(gregs[7]);
2457 COPY(gregs[8]); COPY(gregs[9]);
2458 COPY(gregs[10]); COPY(gregs[11]);
2459 COPY(gregs[12]); COPY(gregs[13]);
2460 COPY(gregs[14]); COPY(gregs[15]);
2461 COPY(gbr); COPY(mach);
2462 COPY(macl); COPY(pr);
2463 COPY(sr); COPY(pc);
2464#undef COPY
2465
2466 /* todo: save FPU registers here */
2467
2468 /* non-iBCS2 extensions.. */
2469 err |= __put_user(mask, &sc->oldmask);
2470
2471 return err;
2472}
2473
2474static int restore_sigcontext(struct CPUState *regs,
2475 struct target_sigcontext *sc)
2476{
2477 unsigned int err = 0;
2478
2479#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2480 COPY(gregs[1]);
2481 COPY(gregs[2]); COPY(gregs[3]);
2482 COPY(gregs[4]); COPY(gregs[5]);
2483 COPY(gregs[6]); COPY(gregs[7]);
2484 COPY(gregs[8]); COPY(gregs[9]);
2485 COPY(gregs[10]); COPY(gregs[11]);
2486 COPY(gregs[12]); COPY(gregs[13]);
2487 COPY(gregs[14]); COPY(gregs[15]);
2488 COPY(gbr); COPY(mach);
2489 COPY(macl); COPY(pr);
2490 COPY(sr); COPY(pc);
2491#undef COPY
2492
2493 /* todo: restore FPU registers here */
2494
2495 regs->tra = -1; /* disable syscall checks */
2496 return err;
2497}
2498
2499static void setup_frame(int sig, struct emulated_sigaction *ka,
2500 target_sigset_t *set, CPUState *regs)
2501{
2502 struct target_sigframe *frame;
2503 abi_ulong frame_addr;
2504 int i;
2505 int err = 0;
2506 int signal;
2507
2508 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2509 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2510 goto give_sigsegv;
2511
2512 signal = current_exec_domain_sig(sig);
2513
2514 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2515
2516 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2517 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2518 }
2519
2520 /* Set up to return from userspace. If provided, use a stub
2521 already in userspace. */
2522 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2523 regs->pr = (unsigned long) ka->sa.sa_restorer;
2524 } else {
2525 /* Generate return code (system call to sigreturn) */
2526 err |= __put_user(MOVW(2), &frame->retcode[0]);
2527 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2528 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2529 regs->pr = (unsigned long) frame->retcode;
2530 }
2531
2532 if (err)
2533 goto give_sigsegv;
2534
2535 /* Set up registers for signal handler */
2536 regs->gregs[15] = (unsigned long) frame;
2537 regs->gregs[4] = signal; /* Arg for signal handler */
2538 regs->gregs[5] = 0;
2539 regs->gregs[6] = (unsigned long) &frame->sc;
2540 regs->pc = (unsigned long) ka->sa._sa_handler;
2541
2542 unlock_user_struct(frame, frame_addr, 1);
2543 return;
2544
2545give_sigsegv:
2546 unlock_user_struct(frame, frame_addr, 1);
2547 force_sig(SIGSEGV);
2548}
2549
2550static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2551 target_siginfo_t *info,
2552 target_sigset_t *set, CPUState *regs)
2553{
2554 struct target_rt_sigframe *frame;
2555 abi_ulong frame_addr;
2556 int i;
2557 int err = 0;
2558 int signal;
2559
2560 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2561 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2562 goto give_sigsegv;
2563
2564 signal = current_exec_domain_sig(sig);
2565
2566 err |= copy_siginfo_to_user(&frame->info, info);
2567
2568 /* Create the ucontext. */
2569 err |= __put_user(0, &frame->uc.uc_flags);
2570 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
2571 err |= __put_user((void *)target_sigaltstack_used.ss_sp,
2572 &frame->uc.uc_stack.ss_sp);
2573 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2574 &frame->uc.uc_stack.ss_flags);
2575 err |= __put_user(target_sigaltstack_used.ss_size,
2576 &frame->uc.uc_stack.ss_size);
2577 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2578 regs, set->sig[0]);
2579 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2580 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2581 }
2582
2583 /* Set up to return from userspace. If provided, use a stub
2584 already in userspace. */
2585 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2586 regs->pr = (unsigned long) ka->sa.sa_restorer;
2587 } else {
2588 /* Generate return code (system call to sigreturn) */
2589 err |= __put_user(MOVW(2), &frame->retcode[0]);
2590 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2591 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2592 regs->pr = (unsigned long) frame->retcode;
2593 }
2594
2595 if (err)
2596 goto give_sigsegv;
2597
2598 /* Set up registers for signal handler */
2599 regs->gregs[15] = (unsigned long) frame;
2600 regs->gregs[4] = signal; /* Arg for signal handler */
2601 regs->gregs[5] = (unsigned long) &frame->info;
2602 regs->gregs[6] = (unsigned long) &frame->uc;
2603 regs->pc = (unsigned long) ka->sa._sa_handler;
2604
2605 unlock_user_struct(frame, frame_addr, 1);
2606 return;
2607
2608give_sigsegv:
2609 unlock_user_struct(frame, frame_addr, 1);
2610 force_sig(SIGSEGV);
2611}
2612
2613long do_sigreturn(CPUState *regs)
2614{
2615 struct target_sigframe *frame;
2616 abi_ulong frame_addr;
2617 sigset_t blocked;
2618 target_sigset_t target_set;
2619 int i;
2620 int err = 0;
2621
2622#if defined(DEBUG_SIGNAL)
2623 fprintf(stderr, "do_sigreturn\n");
2624#endif
2625 frame_addr = regs->gregs[15];
2626 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2627 goto badframe;
2628
2629 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2630 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2631 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2632 }
2633
2634 if (err)
2635 goto badframe;
2636
2637 target_to_host_sigset_internal(&blocked, &target_set);
2638 sigprocmask(SIG_SETMASK, &blocked, NULL);
2639
2640 if (restore_sigcontext(regs, &frame->sc))
2641 goto badframe;
2642
2643 unlock_user_struct(frame, frame_addr, 0);
2644 return regs->gregs[0];
2645
2646badframe:
2647 unlock_user_struct(frame, frame_addr, 0);
2648 force_sig(TARGET_SIGSEGV);
2649 return 0;
2650}
2651
2652long do_rt_sigreturn(CPUState *regs)
2653{
2654 struct target_rt_sigframe *frame;
2655 abi_ulong frame_addr;
2656 sigset_t blocked;
2657
2658#if defined(DEBUG_SIGNAL)
2659 fprintf(stderr, "do_rt_sigreturn\n");
2660#endif
2661 frame_addr = regs->gregs[15];
2662 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2663 goto badframe;
2664
2665 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
2666 sigprocmask(SIG_SETMASK, &blocked, NULL);
2667
2668 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
2669 goto badframe;
2670
2671 if (do_sigaltstack(frame_addr +
2672 offsetof(struct target_rt_sigframe, uc.uc_stack),
2673 0, get_sp_from_cpustate(regs)) == -EFAULT)
2674 goto badframe;
2675
2676 unlock_user_struct(frame, frame_addr, 0);
2677 return regs->gregs[0];
2678
2679badframe:
2680 unlock_user_struct(frame, frame_addr, 0);
2681 force_sig(TARGET_SIGSEGV);
2682 return 0;
2683}
b6d3abda
EI
2684#elif defined(TARGET_CRIS)
2685
2686struct target_sigcontext {
2687 struct target_pt_regs regs; /* needs to be first */
2688 uint32_t oldmask;
2689 uint32_t usp; /* usp before stacking this gunk on it */
2690};
2691
2692/* Signal frames. */
2693struct target_signal_frame {
2694 struct target_sigcontext sc;
2695 uint32_t extramask[TARGET_NSIG_WORDS - 1];
2696 uint8_t retcode[8]; /* Trampoline code. */
2697};
2698
2699struct rt_signal_frame {
2700 struct siginfo *pinfo;
2701 void *puc;
2702 struct siginfo info;
2703 struct ucontext uc;
2704 uint8_t retcode[8]; /* Trampoline code. */
2705};
2706
2707static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
2708{
9664d928
EI
2709 __put_user(env->regs[0], &sc->regs.r0);
2710 __put_user(env->regs[1], &sc->regs.r1);
2711 __put_user(env->regs[2], &sc->regs.r2);
2712 __put_user(env->regs[3], &sc->regs.r3);
2713 __put_user(env->regs[4], &sc->regs.r4);
2714 __put_user(env->regs[5], &sc->regs.r5);
2715 __put_user(env->regs[6], &sc->regs.r6);
2716 __put_user(env->regs[7], &sc->regs.r7);
2717 __put_user(env->regs[8], &sc->regs.r8);
2718 __put_user(env->regs[9], &sc->regs.r9);
2719 __put_user(env->regs[10], &sc->regs.r10);
2720 __put_user(env->regs[11], &sc->regs.r11);
2721 __put_user(env->regs[12], &sc->regs.r12);
2722 __put_user(env->regs[13], &sc->regs.r13);
2723 __put_user(env->regs[14], &sc->usp);
2724 __put_user(env->regs[15], &sc->regs.acr);
2725 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
2726 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
2727 __put_user(env->pc, &sc->regs.erp);
b6d3abda 2728}
9664d928 2729
b6d3abda
EI
2730static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
2731{
9664d928
EI
2732 __get_user(env->regs[0], &sc->regs.r0);
2733 __get_user(env->regs[1], &sc->regs.r1);
2734 __get_user(env->regs[2], &sc->regs.r2);
2735 __get_user(env->regs[3], &sc->regs.r3);
2736 __get_user(env->regs[4], &sc->regs.r4);
2737 __get_user(env->regs[5], &sc->regs.r5);
2738 __get_user(env->regs[6], &sc->regs.r6);
2739 __get_user(env->regs[7], &sc->regs.r7);
2740 __get_user(env->regs[8], &sc->regs.r8);
2741 __get_user(env->regs[9], &sc->regs.r9);
2742 __get_user(env->regs[10], &sc->regs.r10);
2743 __get_user(env->regs[11], &sc->regs.r11);
2744 __get_user(env->regs[12], &sc->regs.r12);
2745 __get_user(env->regs[13], &sc->regs.r13);
2746 __get_user(env->regs[14], &sc->usp);
2747 __get_user(env->regs[15], &sc->regs.acr);
2748 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
2749 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
2750 __get_user(env->pc, &sc->regs.erp);
2751}
2752
2753static abi_ulong get_sigframe(CPUState *env, int framesize)
2754{
2755 abi_ulong sp;
b6d3abda 2756 /* Align the stack downwards to 4. */
9664d928
EI
2757 sp = (env->regs[R_SP] & ~3);
2758 return sp - framesize;
b6d3abda
EI
2759}
2760
2761static void setup_frame(int sig, struct emulated_sigaction *ka,
2762 target_sigset_t *set, CPUState *env)
2763{
2764 struct target_signal_frame *frame;
9664d928 2765 abi_ulong frame_addr;
b6d3abda
EI
2766 int err = 0;
2767 int i;
b6d3abda 2768
9664d928
EI
2769 frame_addr = get_sigframe(env, sizeof *frame);
2770 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
b6d3abda
EI
2771 goto badframe;
2772
2773 /*
2774 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
2775 * use this trampoline anymore but it sets it up for GDB.
2776 * In QEMU, using the trampoline simplifies things a bit so we use it.
2777 *
2778 * This is movu.w __NR_sigreturn, r9; break 13;
2779 */
2780 err |= __put_user(0x9c5f, frame->retcode+0);
2781 err |= __put_user(TARGET_NR_sigreturn,
2782 frame->retcode+2);
2783 err |= __put_user(0xe93d, frame->retcode+4);
2784
2785 /* Save the mask. */
2786 err |= __put_user(set->sig[0], &frame->sc.oldmask);
2787 if (err)
2788 goto badframe;
2789
2790 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2791 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
2792 goto badframe;
2793 }
2794
2795 setup_sigcontext(&frame->sc, env);
2796
2797 /* Move the stack and setup the arguments for the handler. */
2798 env->regs[R_SP] = (uint32_t) frame;
2799 env->regs[10] = sig;
2800 env->pc = (unsigned long) ka->sa._sa_handler;
2801 /* Link SRP so the guest returns through the trampoline. */
2802 env->pregs[PR_SRP] = (uint32_t) &frame->retcode[0];
2803
9664d928 2804 unlock_user_struct(frame, frame_addr, 1);
b6d3abda
EI
2805 return;
2806 badframe:
9664d928 2807 unlock_user_struct(frame, frame_addr, 1);
b6d3abda
EI
2808 force_sig(TARGET_SIGSEGV);
2809}
2810
2811static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2812 target_siginfo_t *info,
2813 target_sigset_t *set, CPUState *env)
2814{
2815 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
2816}
2817
2818long do_sigreturn(CPUState *env)
2819{
2820 struct target_signal_frame *frame;
9664d928 2821 abi_ulong frame_addr;
b6d3abda
EI
2822 target_sigset_t target_set;
2823 sigset_t set;
2824 int i;
2825
9664d928 2826 frame_addr = env->regs[R_SP];
b6d3abda 2827 /* Make sure the guest isn't playing games. */
9664d928 2828 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
b6d3abda
EI
2829 goto badframe;
2830
2831 /* Restore blocked signals */
2832 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
2833 goto badframe;
2834 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2835 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
2836 goto badframe;
2837 }
2838 target_to_host_sigset_internal(&set, &target_set);
2839 sigprocmask(SIG_SETMASK, &set, NULL);
2840
2841 restore_sigcontext(&frame->sc, env);
9664d928
EI
2842 /* Compensate for the syscall return path advancing brk. */
2843 env->pc -= 2;
b6d3abda 2844
9664d928 2845 unlock_user_struct(frame, frame_addr, 0);
b6d3abda
EI
2846 return env->regs[10];
2847 badframe:
9664d928 2848 unlock_user_struct(frame, frame_addr, 0);
b6d3abda
EI
2849 force_sig(TARGET_SIGSEGV);
2850}
2851
2852long do_rt_sigreturn(CPUState *env)
2853{
2854 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
2855 return -TARGET_ENOSYS;
2856}
c3b5bc8a 2857
b346ff46
FB
2858#else
2859
2860static void setup_frame(int sig, struct emulated_sigaction *ka,
2861 target_sigset_t *set, CPUState *env)
2862{
2863 fprintf(stderr, "setup_frame: not implemented\n");
2864}
2865
5fafdf24 2866static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
b346ff46
FB
2867 target_siginfo_t *info,
2868 target_sigset_t *set, CPUState *env)
2869{
2870 fprintf(stderr, "setup_rt_frame: not implemented\n");
2871}
2872
2873long do_sigreturn(CPUState *env)
2874{
2875 fprintf(stderr, "do_sigreturn: not implemented\n");
f8b0aa25 2876 return -TARGET_ENOSYS;
b346ff46
FB
2877}
2878
2879long do_rt_sigreturn(CPUState *env)
2880{
2881 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
f8b0aa25 2882 return -TARGET_ENOSYS;
b346ff46
FB
2883}
2884
66fb9763
FB
2885#endif
2886
2887void process_pending_signals(void *cpu_env)
2888{
2889 int sig;
992f48a0 2890 abi_ulong handler;
9de5e440
FB
2891 sigset_t set, old_set;
2892 target_sigset_t target_old_set;
66fb9763
FB
2893 struct emulated_sigaction *k;
2894 struct sigqueue *q;
3b46e624 2895
31e31b8a
FB
2896 if (!signal_pending)
2897 return;
2898
66fb9763
FB
2899 k = sigact_table;
2900 for(sig = 1; sig <= TARGET_NSIG; sig++) {
2901 if (k->pending)
31e31b8a 2902 goto handle_signal;
66fb9763 2903 k++;
31e31b8a
FB
2904 }
2905 /* if no signal is pending, just return */
2906 signal_pending = 0;
2907 return;
66fb9763 2908
31e31b8a 2909 handle_signal:
66fb9763 2910#ifdef DEBUG_SIGNAL
bc8a22cc 2911 fprintf(stderr, "qemu: process signal %d\n", sig);
66fb9763
FB
2912#endif
2913 /* dequeue signal */
2914 q = k->first;
2915 k->first = q->next;
2916 if (!k->first)
2917 k->pending = 0;
3b46e624 2918
1fddef4b
FB
2919 sig = gdb_handlesig (cpu_env, sig);
2920 if (!sig) {
2921 fprintf (stderr, "Lost signal\n");
2922 abort();
2923 }
66fb9763
FB
2924
2925 handler = k->sa._sa_handler;
2926 if (handler == TARGET_SIG_DFL) {
2927 /* default handler : ignore some signal. The other are fatal */
5fafdf24
TS
2928 if (sig != TARGET_SIGCHLD &&
2929 sig != TARGET_SIGURG &&
66fb9763
FB
2930 sig != TARGET_SIGWINCH) {
2931 force_sig(sig);
2932 }
2933 } else if (handler == TARGET_SIG_IGN) {
2934 /* ignore sig */
2935 } else if (handler == TARGET_SIG_ERR) {
2936 force_sig(sig);
2937 } else {
9de5e440
FB
2938 /* compute the blocked signals during the handler execution */
2939 target_to_host_sigset(&set, &k->sa.sa_mask);
2940 /* SA_NODEFER indicates that the current signal should not be
2941 blocked during the handler */
2942 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
2943 sigaddset(&set, target_to_host_signal(sig));
3b46e624 2944
9de5e440
FB
2945 /* block signals in the handler using Linux */
2946 sigprocmask(SIG_BLOCK, &set, &old_set);
2947 /* save the previous blocked signal state to restore it at the
2948 end of the signal execution (see do_sigreturn) */
9231944d 2949 host_to_target_sigset_internal(&target_old_set, &old_set);
9de5e440 2950
bc8a22cc 2951 /* if the CPU is in VM86 mode, we restore the 32 bit values */
84409ddb 2952#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bc8a22cc
FB
2953 {
2954 CPUX86State *env = cpu_env;
2955 if (env->eflags & VM_MASK)
2956 save_v86_state(env);
2957 }
2958#endif
9de5e440 2959 /* prepare the stack frame of the virtual CPU */
66fb9763 2960 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
9de5e440 2961 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
66fb9763 2962 else
9de5e440 2963 setup_frame(sig, k, &target_old_set, cpu_env);
66fb9763
FB
2964 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
2965 k->sa._sa_handler = TARGET_SIG_DFL;
31e31b8a 2966 }
66fb9763
FB
2967 if (q != &k->info)
2968 free_sigqueue(q);
2969}
This page took 0.544319 seconds and 4 git commands to generate.