blob: 1e5fb20bc9c8cb67ad0ea2fc619775d9772c29fc [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard66fb9762003-03-23 01:06:05 +00002 * Emulation of Linux signals
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <stdlib.h>
21#include <stdio.h>
bellard66fb9762003-03-23 01:06:05 +000022#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000023#include <stdarg.h>
bellard2677e102003-04-10 00:03:27 +000024#include <unistd.h>
bellard31e31b82003-02-18 22:55:36 +000025#include <signal.h>
bellard66fb9762003-03-23 01:06:05 +000026#include <errno.h>
bellard31e31b82003-02-18 22:55:36 +000027#include <sys/ucontext.h>
28
bellard3ef693a2003-03-23 20:17:16 +000029#include "qemu.h"
blueswir1992f48a2007-10-14 16:27:31 +000030#include "target_signal.h"
bellard66fb9762003-03-23 01:06:05 +000031
32//#define DEBUG_SIGNAL
33
34#define MAX_SIGQUEUE_SIZE 1024
35
36struct sigqueue {
37 struct sigqueue *next;
bellard9de5e442003-03-23 16:49:39 +000038 target_siginfo_t info;
bellard66fb9762003-03-23 01:06:05 +000039};
bellard31e31b82003-02-18 22:55:36 +000040
41struct emulated_sigaction {
42 struct target_sigaction sa;
bellard66fb9762003-03-23 01:06:05 +000043 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 */
bellard31e31b82003-02-18 22:55:36 +000047};
48
thsa04e1342007-09-27 13:57:58 +000049struct target_sigaltstack target_sigaltstack_used = {
50 .ss_sp = 0,
51 .ss_size = 0,
52 .ss_flags = TARGET_SS_DISABLE,
53};
54
bellard66fb9762003-03-23 01:06:05 +000055static 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 */
bellard31e31b82003-02-18 22:55:36 +000059
ths5fafdf22007-09-16 21:08:06 +000060static void host_signal_handler(int host_signum, siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +000061 void *puc);
62
bellard9e5f5282003-07-13 17:33:54 +000063static 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,
bellard01e3b762003-09-30 21:10:14 +000070/* [SIGIOT] = TARGET_SIGIOT,*/
bellard9e5f5282003-07-13 17:33:54 +000071 [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
thsa04e1342007-09-27 13:57:58 +0000102static 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
bellard31e31b82003-02-18 22:55:36 +0000114static inline int host_to_target_signal(int sig)
115{
bellard9e5f5282003-07-13 17:33:54 +0000116 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000117}
118
119static inline int target_to_host_signal(int sig)
120{
bellard9e5f5282003-07-13 17:33:54 +0000121 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000122}
123
ths5fafdf22007-09-16 21:08:06 +0000124static void host_to_target_sigset_internal(target_sigset_t *d,
bellard92319442004-06-19 16:58:13 +0000125 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000126{
127 int i;
bellard9e5f5282003-07-13 17:33:54 +0000128 unsigned long sigmask;
129 uint32_t target_sigmask;
ths3b46e622007-09-17 08:09:54 +0000130
bellard9e5f5282003-07-13 17:33:54 +0000131 sigmask = ((unsigned long *)s)[0];
132 target_sigmask = 0;
133 for(i = 0; i < 32; i++) {
ths5fafdf22007-09-16 21:08:06 +0000134 if (sigmask & (1 << i))
bellard9e5f5282003-07-13 17:33:54 +0000135 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
136 }
blueswir1992f48a2007-10-14 16:27:31 +0000137#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
bellard92319442004-06-19 16:58:13 +0000138 d->sig[0] = target_sigmask;
bellard9e5f5282003-07-13 17:33:54 +0000139 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard92319442004-06-19 16:58:13 +0000140 d->sig[i] = ((unsigned long *)s)[i];
bellard66fb9762003-03-23 01:06:05 +0000141 }
blueswir1992f48a2007-10-14 16:27:31 +0000142#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
bellard92319442004-06-19 16:58:13 +0000143 d->sig[0] = target_sigmask;
144 d->sig[1] = sigmask >> 32;
bellard9e5f5282003-07-13 17:33:54 +0000145#else
bellard459a4012007-11-11 19:45:10 +0000146 /* XXX: do it */
bellard9e5f5282003-07-13 17:33:54 +0000147#endif
bellard66fb9762003-03-23 01:06:05 +0000148}
149
bellard92319442004-06-19 16:58:13 +0000150void 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++)
pbrook53a59602006-03-25 19:31:22 +0000157 d->sig[i] = tswapl(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000158}
159
160void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000161{
162 int i;
bellard9e5f5282003-07-13 17:33:54 +0000163 unsigned long sigmask;
blueswir1992f48a2007-10-14 16:27:31 +0000164 abi_ulong target_sigmask;
bellard9e5f5282003-07-13 17:33:54 +0000165
bellard92319442004-06-19 16:58:13 +0000166 target_sigmask = s->sig[0];
bellard9e5f5282003-07-13 17:33:54 +0000167 sigmask = 0;
168 for(i = 0; i < 32; i++) {
ths5fafdf22007-09-16 21:08:06 +0000169 if (target_sigmask & (1 << i))
bellard9e5f5282003-07-13 17:33:54 +0000170 sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
171 }
blueswir1992f48a2007-10-14 16:27:31 +0000172#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
bellard9e5f5282003-07-13 17:33:54 +0000173 ((unsigned long *)d)[0] = sigmask;
174 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard92319442004-06-19 16:58:13 +0000175 ((unsigned long *)d)[i] = s->sig[i];
bellard66fb9762003-03-23 01:06:05 +0000176 }
blueswir1992f48a2007-10-14 16:27:31 +0000177#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
bellard92319442004-06-19 16:58:13 +0000178 ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
bellard9e5f5282003-07-13 17:33:54 +0000179#else
bellard459a4012007-11-11 19:45:10 +0000180 /* XXX: do it */
blueswir1992f48a2007-10-14 16:27:31 +0000181#endif /* TARGET_ABI_BITS */
bellard66fb9762003-03-23 01:06:05 +0000182}
183
bellard92319442004-06-19 16:58:13 +0000184void 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++)
pbrook53a59602006-03-25 19:31:22 +0000190 s1.sig[i] = tswapl(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000191 target_to_host_sigset_internal(d, &s1);
192}
ths3b46e622007-09-17 08:09:54 +0000193
blueswir1992f48a2007-10-14 16:27:31 +0000194void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000195 const sigset_t *sigset)
196{
bellard9e5f5282003-07-13 17:33:54 +0000197 target_sigset_t d;
198 host_to_target_sigset(&d, sigset);
199 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000200}
201
ths5fafdf22007-09-16 21:08:06 +0000202void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000203 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000204{
bellard9e5f5282003-07-13 17:33:54 +0000205 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);
bellard66fb9762003-03-23 01:06:05 +0000212}
213
bellard9de5e442003-03-23 16:49:39 +0000214/* siginfo conversion */
215
ths5fafdf22007-09-16 21:08:06 +0000216static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000217 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000218{
bellard9de5e442003-03-23 16:49:39 +0000219 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;
ths5fafdf22007-09-16 21:08:06 +0000224 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000225 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000226 /* should never come here, but who knows. The information for
227 the target is irrelevant */
228 tinfo->_sifields._sigfault._addr = 0;
ths7f7f7c82007-07-12 11:02:46 +0000229 } else if (sig == SIGIO) {
230 tinfo->_sifields._sigpoll._fd = info->si_fd;
bellard9de5e442003-03-23 16:49:39 +0000231 } 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 */
ths5fafdf22007-09-16 21:08:06 +0000235 tinfo->_sifields._rt._sigval.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000236 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000237 }
bellard66fb9762003-03-23 01:06:05 +0000238}
239
ths5fafdf22007-09-16 21:08:06 +0000240static void tswap_siginfo(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000241 const target_siginfo_t *info)
242{
243 int sig;
244 sig = info->si_signo;
245 tinfo->si_signo = tswap32(sig);
246 tinfo->si_errno = tswap32(info->si_errno);
247 tinfo->si_code = tswap32(info->si_code);
ths5fafdf22007-09-16 21:08:06 +0000248 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000249 sig == SIGBUS || sig == SIGTRAP) {
ths5fafdf22007-09-16 21:08:06 +0000250 tinfo->_sifields._sigfault._addr =
bellard9de5e442003-03-23 16:49:39 +0000251 tswapl(info->_sifields._sigfault._addr);
ths7f7f7c82007-07-12 11:02:46 +0000252 } else if (sig == SIGIO) {
253 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
bellard9de5e442003-03-23 16:49:39 +0000254 } 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);
ths5fafdf22007-09-16 21:08:06 +0000257 tinfo->_sifields._rt._sigval.sival_ptr =
bellard9de5e442003-03-23 16:49:39 +0000258 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);
267}
268
269/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000270/* XXX: find a solution for 64 bit (additional malloced data is needed) */
bellard9de5e442003-03-23 16:49:39 +0000271void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000272{
273 info->si_signo = tswap32(tinfo->si_signo);
274 info->si_errno = tswap32(tinfo->si_errno);
275 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000276 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
277 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000278 info->si_value.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000279 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000280}
281
bellard31e31b82003-02-18 22:55:36 +0000282void signal_init(void)
283{
284 struct sigaction act;
bellard9e5f5282003-07-13 17:33:54 +0000285 int i, j;
bellard31e31b82003-02-18 22:55:36 +0000286
bellard9e5f5282003-07-13 17:33:54 +0000287 /* 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 }
ths3b46e622007-09-17 08:09:54 +0000296
bellard9de5e442003-03-23 16:49:39 +0000297 /* set all host signal handlers. ALL signals are blocked during
298 the handlers to serialize them. */
299 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000300 act.sa_flags = SA_SIGINFO;
301 act.sa_sigaction = host_signal_handler;
302 for(i = 1; i < NSIG; i++) {
bellardc9087c22003-05-27 23:25:41 +0000303 sigaction(i, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000304 }
ths3b46e622007-09-17 08:09:54 +0000305
bellard31e31b82003-02-18 22:55:36 +0000306 memset(sigact_table, 0, sizeof(sigact_table));
bellard66fb9762003-03-23 01:06:05 +0000307
308 first_free = &sigqueue_table[0];
ths5fafdf22007-09-16 21:08:06 +0000309 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
bellard66fb9762003-03-23 01:06:05 +0000310 sigqueue_table[i].next = &sigqueue_table[i + 1];
311 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
bellard31e31b82003-02-18 22:55:36 +0000312}
313
bellard66fb9762003-03-23 01:06:05 +0000314/* 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;
323}
324
325static inline void free_sigqueue(struct sigqueue *q)
326{
327 q->next = first_free;
328 first_free = q;
329}
330
bellard9de5e442003-03-23 16:49:39 +0000331/* abort execution with signal */
332void __attribute((noreturn)) force_sig(int sig)
bellard66fb9762003-03-23 01:06:05 +0000333{
334 int host_sig;
bellard66fb9762003-03-23 01:06:05 +0000335 host_sig = target_to_host_signal(sig);
ths5fafdf22007-09-16 21:08:06 +0000336 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
bellard66fb9762003-03-23 01:06:05 +0000337 sig, strsignal(host_sig));
bellard9de5e442003-03-23 16:49:39 +0000338#if 1
bellard66fb9762003-03-23 01:06:05 +0000339 _exit(-host_sig);
bellard9de5e442003-03-23 16:49:39 +0000340#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
bellard66fb9762003-03-23 01:06:05 +0000350}
351
bellard9de5e442003-03-23 16:49:39 +0000352/* 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)
bellard31e31b82003-02-18 22:55:36 +0000355{
bellard66fb9762003-03-23 01:06:05 +0000356 struct emulated_sigaction *k;
bellard9de5e442003-03-23 16:49:39 +0000357 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000358 abi_ulong handler;
bellard66fb9762003-03-23 01:06:05 +0000359
bellard9de5e442003-03-23 16:49:39 +0000360#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000361 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000362 sig);
bellard66fb9762003-03-23 01:06:05 +0000363#endif
bellard9de5e442003-03-23 16:49:39 +0000364 k = &sigact_table[sig - 1];
bellard66fb9762003-03-23 01:06:05 +0000365 handler = k->sa._sa_handler;
366 if (handler == TARGET_SIG_DFL) {
367 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000368 if (sig != TARGET_SIGCHLD &&
369 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +0000370 sig != TARGET_SIGWINCH) {
371 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000372 } else {
373 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000374 }
375 } else if (handler == TARGET_SIG_IGN) {
376 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000377 return 0;
bellard66fb9762003-03-23 01:06:05 +0000378 } else if (handler == TARGET_SIG_ERR) {
379 force_sig(sig);
380 } else {
bellard9de5e442003-03-23 16:49:39 +0000381 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
ths5fafdf22007-09-16 21:08:06 +0000410static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000411 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 */
bellardec6338b2007-11-08 14:25:03 +0000418 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
bellardb346ff42003-06-15 20:05:50 +0000419 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000420 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)
bellardbc8a22c2003-03-30 21:02:40 +0000428 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000429#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 */
bellard68a79312003-06-30 13:12:32 +0000433 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
bellard66fb9762003-03-23 01:06:05 +0000434 }
bellard31e31b82003-02-18 22:55:36 +0000435}
436
ths0da46a62007-10-20 20:23:07 +0000437/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000438/* compare linux/kernel/signal.c:do_sigaltstack() */
439abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000440{
441 int ret;
442 struct target_sigaltstack oss;
443
444 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000445 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000446 {
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
bellard579a97f2007-11-11 14:26:47 +0000452 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000453 {
bellard579a97f2007-11-11 14:26:47 +0000454 struct target_sigaltstack *uss;
455 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000456
ths0da46a62007-10-20 20:23:07 +0000457 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000458 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000459 || __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;
bellard579a97f2007-11-11 14:26:47 +0000463 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000464
ths0da46a62007-10-20 20:23:07 +0000465 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000466 if (on_sig_stack(sp))
467 goto out;
468
ths0da46a62007-10-20 20:23:07 +0000469 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000470 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 {
ths0da46a62007-10-20 20:23:07 +0000479 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000480 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
bellard579a97f2007-11-11 14:26:47 +0000488 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000489 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000490 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000491 goto out;
thsa04e1342007-09-27 13:57:58 +0000492 }
493
494 ret = 0;
495out:
496 return ret;
497}
498
ths0da46a62007-10-20 20:23:07 +0000499/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000500int do_sigaction(int sig, const struct target_sigaction *act,
501 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000502{
bellard66fb9762003-03-23 01:06:05 +0000503 struct emulated_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000504 struct sigaction act1;
505 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000506 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000507
ths0aeaa8c2007-03-31 19:29:06 +0000508 if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000509 return -EINVAL;
510 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000511#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000512 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
bellard66fb9762003-03-23 01:06:05 +0000513 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);
ths388bb212007-05-13 13:58:00 +0000518#if !defined(TARGET_MIPS)
519 oact->sa_restorer = tswapl(k->sa.sa_restorer);
520#endif
bellard66fb9762003-03-23 01:06:05 +0000521 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);
ths388bb212007-05-13 13:58:00 +0000526#if !defined(TARGET_MIPS)
527 k->sa.sa_restorer = tswapl(act->sa_restorer);
528#endif
bellard66fb9762003-03-23 01:06:05 +0000529 k->sa.sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000530
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 }
ths0da46a62007-10-20 20:23:07 +0000548 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000549 }
bellard66fb9762003-03-23 01:06:05 +0000550 }
ths0da46a62007-10-20 20:23:07 +0000551 return ret;
bellard66fb9762003-03-23 01:06:05 +0000552}
bellard31e31b82003-02-18 22:55:36 +0000553
bellard43fff232003-07-09 19:31:39 +0000554#ifndef offsetof
555#define offsetof(type, field) ((size_t) &((type *)0)->field)
556#endif
557
ths5fafdf22007-09-16 21:08:06 +0000558static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
bellard43fff232003-07-09 19:31:39 +0000559 const target_siginfo_t *info)
560{
561 tswap_siginfo(tinfo, info);
562 return 0;
563}
564
thsc3b5bc82007-12-02 06:31:25 +0000565static 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
bellard459a4012007-11-11 19:45:10 +0000571#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000572
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 {
blueswir1992f48a2007-10-14 16:27:31 +0000587 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000588};
589
590struct target_fpstate {
591 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000592 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;
bellard66fb9762003-03-23 01:06:05 +0000599 struct target_fpreg _st[8];
600 uint16_t status;
601 uint16_t magic; /* 0xffff = regular FPU data only */
602
603 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000604 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
605 abi_ulong mxcsr;
606 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000607 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
608 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000609 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000610};
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;
blueswir1992f48a2007-10-14 16:27:31 +0000619 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;
bellard66fb9762003-03-23 01:06:05 +0000630 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000631 abi_ulong eflags;
632 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000633 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000634 abi_ulong fpstate; /* pointer */
635 abi_ulong oldmask;
636 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000637};
638
bellard66fb9762003-03-23 01:06:05 +0000639struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000640 abi_ulong tuc_flags;
641 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +0000642 target_stack_t tuc_stack;
643 struct target_sigcontext tuc_mcontext;
644 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000645};
646
647struct sigframe
648{
blueswir1992f48a2007-10-14 16:27:31 +0000649 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000650 int sig;
651 struct target_sigcontext sc;
652 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000653 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000654 char retcode[8];
655};
656
657struct rt_sigframe
658{
blueswir1992f48a2007-10-14 16:27:31 +0000659 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000660 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000661 abi_ulong pinfo;
662 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000663 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
bellard66fb9762003-03-23 01:06:05 +0000673/* XXX: save x87 state */
674static int
675setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
bellard28be6232007-11-11 22:23:38 +0000676 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000677{
678 int err = 0;
bellard775b58d2007-11-11 16:22:17 +0000679 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000680
bellard579a97f2007-11-11 14:26:47 +0000681 /* already locked in setup_frame() */
bellarda52c7572003-06-21 13:14:12 +0000682 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);
bellard66fb9762003-03-23 01:06:05 +0000686 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);
bellard66099dd2003-05-08 15:34:02 +0000694 err |= __put_user(env->exception_index, &sc->trapno);
695 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000696 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000697 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000698 err |= __put_user(env->eflags, &sc->eflags);
699 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000700 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000701
bellard28be6232007-11-11 22:23:38 +0000702 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000703 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000704 magic = 0xffff;
705 err |= __put_user(magic, &fpstate->magic);
bellard28be6232007-11-11 22:23:38 +0000706 err |= __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000707
bellard66fb9762003-03-23 01:06:05 +0000708 /* non-iBCS2 extensions.. */
709 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000710 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000711 return err;
712}
713
714/*
715 * Determine which stack to use..
716 */
717
bellard579a97f2007-11-11 14:26:47 +0000718static inline abi_ulong
bellard66fb9762003-03-23 01:06:05 +0000719get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
720{
721 unsigned long esp;
722
723 /* Default to using normal stack */
724 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000725 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +0000726 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 }
bellard66fb9762003-03-23 01:06:05 +0000730
731 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000732 else
bellarda52c7572003-06-21 13:14:12 +0000733 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 }
bellard579a97f2007-11-11 14:26:47 +0000738 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000739}
740
bellard579a97f2007-11-11 14:26:47 +0000741/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
bellard66fb9762003-03-23 01:06:05 +0000742static void setup_frame(int sig, struct emulated_sigaction *ka,
743 target_sigset_t *set, CPUX86State *env)
744{
bellard579a97f2007-11-11 14:26:47 +0000745 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000746 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000747 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000748
bellard579a97f2007-11-11 14:26:47 +0000749 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000750
bellard579a97f2007-11-11 14:26:47 +0000751 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000752 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000753
thsc3b5bc82007-12-02 06:31:25 +0000754 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000755 &frame->sig);
756 if (err)
757 goto give_sigsegv;
758
bellard28be6232007-11-11 22:23:38 +0000759 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
760 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000761 if (err)
762 goto give_sigsegv;
763
bellard92319442004-06-19 16:58:13 +0000764 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
765 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
766 goto give_sigsegv;
767 }
bellard66fb9762003-03-23 01:06:05 +0000768
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 {
bellard775b58d2007-11-11 16:22:17 +0000774 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000775 abi_ulong retcode_addr;
776 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
777 err |= __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000778 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000779 val16 = 0xb858;
780 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000781 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000782 val16 = 0x80cd;
783 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000784 }
785
786 if (err)
787 goto give_sigsegv;
788
789 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000790 env->regs[R_ESP] = frame_addr;
791 env->eip = ka->sa._sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000792
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
bellard579a97f2007-11-11 14:26:47 +0000799 unlock_user_struct(frame, frame_addr, 1);
800
bellard66fb9762003-03-23 01:06:05 +0000801 return;
802
803give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000804 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000805 if (sig == TARGET_SIGSEGV)
806 ka->sa._sa_handler = TARGET_SIG_DFL;
807 force_sig(TARGET_SIGSEGV /* , current */);
808}
809
bellard579a97f2007-11-11 14:26:47 +0000810/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
ths5fafdf22007-09-16 21:08:06 +0000811static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard9de5e442003-03-23 16:49:39 +0000812 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000813 target_sigset_t *set, CPUX86State *env)
814{
bellard28be6232007-11-11 22:23:38 +0000815 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000816 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000817 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000818
bellard579a97f2007-11-11 14:26:47 +0000819 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000820
bellard579a97f2007-11-11 14:26:47 +0000821 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000822 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000823
thsc3b5bc82007-12-02 06:31:25 +0000824 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000825 &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000826 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);
bellard66fb9762003-03-23 01:06:05 +0000830 err |= copy_siginfo_to_user(&frame->info, info);
831 if (err)
832 goto give_sigsegv;
833
834 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000835 err |= __put_user(0, &frame->uc.tuc_flags);
836 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000837 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000838 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000839 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000840 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000841 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000842 &frame->uc.tuc_stack.ss_size);
843 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000844 env, set->sig[0],
845 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +0000846 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000847 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000848 goto give_sigsegv;
849 }
bellard66fb9762003-03-23 01:06:05 +0000850
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 {
bellard775b58d2007-11-11 16:22:17 +0000856 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000857 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
858 err |= __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000859 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000860 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000861 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000862 val16 = 0x80cd;
863 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000864 }
865
866 if (err)
867 goto give_sigsegv;
868
869 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000870 env->regs[R_ESP] = frame_addr;
871 env->eip = ka->sa._sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000872
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
bellard579a97f2007-11-11 14:26:47 +0000879 unlock_user_struct(frame, frame_addr, 1);
880
bellard66fb9762003-03-23 01:06:05 +0000881 return;
882
883give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000884 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000885 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;
bellard28be6232007-11-11 22:23:38 +0000894 abi_ulong fpstate_addr;
895 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +0000896
bellard28be6232007-11-11 22:23:38 +0000897 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));
bellard66fb9762003-03-23 01:06:05 +0000901
bellard28be6232007-11-11 22:23:38 +0000902 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);
bellard66fb9762003-03-23 01:06:05 +0000910
911 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
912 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000913
bellard28be6232007-11-11 22:23:38 +0000914 tmpflags = tswapl(sc->eflags);
915 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
916 // regs->orig_eax = -1; /* disable syscall checks */
917
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);
bellard66fb9762003-03-23 01:06:05 +0000924 }
925
bellard28be6232007-11-11 22:23:38 +0000926 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +0000927 return err;
bellard66fb9762003-03-23 01:06:05 +0000928badframe:
929 return 1;
bellard66fb9762003-03-23 01:06:05 +0000930}
931
932long do_sigreturn(CPUX86State *env)
933{
bellard579a97f2007-11-11 14:26:47 +0000934 struct sigframe *frame;
935 abi_ulong frame_addr = env->regs[R_ESP] - 8;
bellard66fb9762003-03-23 01:06:05 +0000936 target_sigset_t target_set;
937 sigset_t set;
938 int eax, i;
939
bellard447db212003-05-10 15:10:36 +0000940#if defined(DEBUG_SIGNAL)
941 fprintf(stderr, "do_sigreturn\n");
942#endif
bellard579a97f2007-11-11 14:26:47 +0000943 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
944 goto badframe;
bellard66fb9762003-03-23 01:06:05 +0000945 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +0000946 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 }
bellard66fb9762003-03-23 01:06:05 +0000952
bellard92319442004-06-19 16:58:13 +0000953 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +0000954 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +0000955
bellard66fb9762003-03-23 01:06:05 +0000956 /* restore registers */
957 if (restore_sigcontext(env, &frame->sc, &eax))
958 goto badframe;
bellard579a97f2007-11-11 14:26:47 +0000959 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000960 return eax;
961
962badframe:
bellard579a97f2007-11-11 14:26:47 +0000963 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000964 force_sig(TARGET_SIGSEGV);
965 return 0;
966}
967
968long do_rt_sigreturn(CPUX86State *env)
969{
bellard28be6232007-11-11 22:23:38 +0000970 abi_ulong frame_addr;
971 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +0000972 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +0000973 int eax;
974
bellard28be6232007-11-11 22:23:38 +0000975 frame_addr = env->regs[R_ESP] - 4;
976 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
977 goto badframe;
bellardb8076a72005-04-07 22:20:31 +0000978 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +0000979 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +0000980
bellardb8076a72005-04-07 22:20:31 +0000981 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +0000982 goto badframe;
983
bellard28be6232007-11-11 22:23:38 +0000984 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
985 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +0000986 goto badframe;
thsa04e1342007-09-27 13:57:58 +0000987
bellard28be6232007-11-11 22:23:38 +0000988 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000989 return eax;
990
991badframe:
bellard28be6232007-11-11 22:23:38 +0000992 unlock_user_struct(frame, frame_addr, 0);
993 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +0000994 return 0;
995}
996
bellard43fff232003-07-09 19:31:39 +0000997#elif defined(TARGET_ARM)
998
999struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001000 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;
bellard43fff232003-07-09 19:31:39 +00001021};
1022
pbrooka745ec62008-05-06 15:36:17 +00001023struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001024 abi_ulong tuc_flags;
1025 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +00001026 target_stack_t tuc_stack;
1027 struct target_sigcontext tuc_mcontext;
1028 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001029};
1030
pbrooka745ec62008-05-06 15:36:17 +00001031struct target_ucontext_v2 {
1032 abi_ulong tuc_flags;
1033 abi_ulong tuc_link;
1034 target_stack_t tuc_stack;
1035 struct target_sigcontext tuc_mcontext;
1036 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1037 char __unused[128 - sizeof(sigset_t)];
1038 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1039};
1040
bellard43fff232003-07-09 19:31:39 +00001041struct sigframe
1042{
1043 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001044 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1045 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001046};
1047
pbrooka745ec62008-05-06 15:36:17 +00001048struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001049{
bellardf8b0aa22007-11-11 23:03:42 +00001050 abi_ulong pinfo;
1051 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001052 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001053 struct target_ucontext_v1 uc;
1054 abi_ulong retcode;
1055};
1056
1057struct rt_sigframe_v2
1058{
1059 struct target_siginfo info;
1060 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001061 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001062};
1063
1064#define TARGET_CONFIG_CPU_32 1
1065
1066/*
1067 * For ARM syscalls, we encode the syscall number into the instruction.
1068 */
1069#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1070#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1071
1072/*
1073 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1074 * need two 16-bit instructions.
1075 */
1076#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1077#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1078
blueswir1992f48a2007-10-14 16:27:31 +00001079static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001080 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1081 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1082};
1083
1084
1085#define __put_user_error(x,p,e) __put_user(x, p)
1086#define __get_user_error(x,p,e) __get_user(x, p)
1087
1088static inline int valid_user_regs(CPUState *regs)
1089{
1090 return 1;
1091}
1092
1093static int
1094setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
bellardf8b0aa22007-11-11 23:03:42 +00001095 CPUState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001096{
1097 int err = 0;
1098
1099 __put_user_error(env->regs[0], &sc->arm_r0, err);
1100 __put_user_error(env->regs[1], &sc->arm_r1, err);
1101 __put_user_error(env->regs[2], &sc->arm_r2, err);
1102 __put_user_error(env->regs[3], &sc->arm_r3, err);
1103 __put_user_error(env->regs[4], &sc->arm_r4, err);
1104 __put_user_error(env->regs[5], &sc->arm_r5, err);
1105 __put_user_error(env->regs[6], &sc->arm_r6, err);
1106 __put_user_error(env->regs[7], &sc->arm_r7, err);
1107 __put_user_error(env->regs[8], &sc->arm_r8, err);
1108 __put_user_error(env->regs[9], &sc->arm_r9, err);
1109 __put_user_error(env->regs[10], &sc->arm_r10, err);
1110 __put_user_error(env->regs[11], &sc->arm_fp, err);
1111 __put_user_error(env->regs[12], &sc->arm_ip, err);
1112 __put_user_error(env->regs[13], &sc->arm_sp, err);
1113 __put_user_error(env->regs[14], &sc->arm_lr, err);
1114 __put_user_error(env->regs[15], &sc->arm_pc, err);
1115#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001116 __put_user_error(cpsr_read(env), &sc->arm_cpsr, err);
bellard43fff232003-07-09 19:31:39 +00001117#endif
1118
1119 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1120 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1121 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1122 __put_user_error(mask, &sc->oldmask, err);
1123
1124 return err;
1125}
1126
bellard579a97f2007-11-11 14:26:47 +00001127static inline abi_ulong
bellard43fff232003-07-09 19:31:39 +00001128get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1129{
1130 unsigned long sp = regs->regs[13];
1131
bellard43fff232003-07-09 19:31:39 +00001132 /*
1133 * This is the X/Open sanctioned signal stack switching.
1134 */
thsa04e1342007-09-27 13:57:58 +00001135 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1136 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001137 /*
1138 * ATPCS B01 mandates 8-byte alignment
1139 */
bellard579a97f2007-11-11 14:26:47 +00001140 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001141}
1142
1143static int
1144setup_return(CPUState *env, struct emulated_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001145 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001146{
bellardf8b0aa22007-11-11 23:03:42 +00001147 abi_ulong handler = ka->sa._sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001148 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001149 int thumb = handler & 1;
bellard43fff232003-07-09 19:31:39 +00001150
1151 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
bellardf8b0aa22007-11-11 23:03:42 +00001152 retcode = ka->sa.sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001153 } else {
1154 unsigned int idx = thumb;
1155
1156 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1157 idx += 2;
1158
1159 if (__put_user(retcodes[idx], rc))
1160 return 1;
1161#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001162 flush_icache_range((abi_ulong)rc,
1163 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001164#endif
bellardf8b0aa22007-11-11 23:03:42 +00001165 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001166 }
1167
1168 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001169 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001170 env->regs[14] = retcode;
1171 env->regs[15] = handler & (thumb ? ~1 : ~3);
pbrook75b680e2008-03-21 16:07:30 +00001172 env->thumb = thumb;
bellard43fff232003-07-09 19:31:39 +00001173
bellardb5ff1b32005-11-26 10:38:39 +00001174#if 0
bellard43fff232003-07-09 19:31:39 +00001175#ifdef TARGET_CONFIG_CPU_32
1176 env->cpsr = cpsr;
1177#endif
bellardb5ff1b32005-11-26 10:38:39 +00001178#endif
bellard43fff232003-07-09 19:31:39 +00001179
1180 return 0;
1181}
1182
bellard579a97f2007-11-11 14:26:47 +00001183/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
bellard43fff232003-07-09 19:31:39 +00001184static void setup_frame(int usig, struct emulated_sigaction *ka,
1185 target_sigset_t *set, CPUState *regs)
1186{
bellard579a97f2007-11-11 14:26:47 +00001187 struct sigframe *frame;
1188 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
bellard92319442004-06-19 16:58:13 +00001189 int i, err = 0;
bellard43fff232003-07-09 19:31:39 +00001190
bellard579a97f2007-11-11 14:26:47 +00001191 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1192 return;
1193
bellard43fff232003-07-09 19:31:39 +00001194 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1195
bellard92319442004-06-19 16:58:13 +00001196 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1197 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001198 goto end;
bellard43fff232003-07-09 19:31:39 +00001199 }
1200
1201 if (err == 0)
bellardf8b0aa22007-11-11 23:03:42 +00001202 err = setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1203 frame_addr + offsetof(struct sigframe, retcode));
bellard579a97f2007-11-11 14:26:47 +00001204
1205end:
1206 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001207 // return err;
1208}
1209
bellard579a97f2007-11-11 14:26:47 +00001210/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrooka745ec62008-05-06 15:36:17 +00001211static void setup_rt_frame_v1(int usig, struct emulated_sigaction *ka,
1212 target_siginfo_t *info,
1213 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001214{
pbrooka745ec62008-05-06 15:36:17 +00001215 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001216 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001217 struct target_sigaltstack stack;
bellard92319442004-06-19 16:58:13 +00001218 int i, err = 0;
bellardf8b0aa22007-11-11 23:03:42 +00001219 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001220
bellard579a97f2007-11-11 14:26:47 +00001221 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001222 return /* 1 */;
1223
pbrooka745ec62008-05-06 15:36:17 +00001224 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
bellardf8b0aa22007-11-11 23:03:42 +00001225 __put_user_error(info_addr, &frame->pinfo, err);
pbrooka745ec62008-05-06 15:36:17 +00001226 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
bellardf8b0aa22007-11-11 23:03:42 +00001227 __put_user_error(uc_addr, &frame->puc, err);
bellard43fff232003-07-09 19:31:39 +00001228 err |= copy_siginfo_to_user(&frame->info, info);
1229
1230 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001231 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001232
thsa04e1342007-09-27 13:57:58 +00001233 memset(&stack, 0, sizeof(stack));
1234 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1235 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1236 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001237 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001238
bellardb8076a72005-04-07 22:20:31 +00001239 err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
bellard43fff232003-07-09 19:31:39 +00001240 env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001241 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001242 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001243 goto end;
bellard92319442004-06-19 16:58:13 +00001244 }
bellard43fff232003-07-09 19:31:39 +00001245
1246 if (err == 0)
bellardf8b0aa22007-11-11 23:03:42 +00001247 err = setup_return(env, ka, &frame->retcode, frame_addr, usig,
pbrooka745ec62008-05-06 15:36:17 +00001248 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1249
1250 if (err == 0) {
1251 env->regs[1] = info_addr;
1252 env->regs[2] = uc_addr;
1253 }
1254
1255end:
1256 unlock_user_struct(frame, frame_addr, 1);
1257
1258 // return err;
1259}
1260
1261static void setup_rt_frame_v2(int usig, struct emulated_sigaction *ka,
1262 target_siginfo_t *info,
1263 target_sigset_t *set, CPUState *env)
1264{
1265 struct rt_sigframe_v2 *frame;
1266 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1267 struct target_sigaltstack stack;
1268 int i, err = 0;
1269 abi_ulong info_addr, uc_addr;
1270
1271 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1272 return /* 1 */;
1273
1274 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1275 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1276 err |= copy_siginfo_to_user(&frame->info, info);
1277
1278 /* Clear all the bits of the ucontext we don't use. */
1279 memset(&frame->uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1280
1281 memset(&stack, 0, sizeof(stack));
1282 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1283 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1284 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1285 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1286
1287 err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
1288 env, set->sig[0]);
1289 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1290 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1291 goto end;
1292 }
1293
1294 if (err == 0)
1295 err = setup_return(env, ka, &frame->retcode, frame_addr, usig,
1296 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
bellard43fff232003-07-09 19:31:39 +00001297
1298 if (err == 0) {
1299 /*
1300 * For realtime signals we must also set the second and third
1301 * arguments for the signal handler.
1302 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1303 */
bellardf8b0aa22007-11-11 23:03:42 +00001304 env->regs[1] = info_addr;
1305 env->regs[2] = uc_addr;
bellard43fff232003-07-09 19:31:39 +00001306 }
1307
bellard579a97f2007-11-11 14:26:47 +00001308end:
1309 unlock_user_struct(frame, frame_addr, 1);
1310
bellard43fff232003-07-09 19:31:39 +00001311 // return err;
1312}
1313
pbrooka745ec62008-05-06 15:36:17 +00001314static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1315 target_siginfo_t *info,
1316 target_sigset_t *set, CPUState *env)
1317{
1318 if (get_osversion() >= 0x020612) {
1319 setup_rt_frame_v2(usig, ka, info, set, env);
1320 } else {
1321 setup_rt_frame_v1(usig, ka, info, set, env);
1322 }
1323}
1324
bellard43fff232003-07-09 19:31:39 +00001325static int
1326restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1327{
1328 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001329 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001330
1331 __get_user_error(env->regs[0], &sc->arm_r0, err);
1332 __get_user_error(env->regs[1], &sc->arm_r1, err);
1333 __get_user_error(env->regs[2], &sc->arm_r2, err);
1334 __get_user_error(env->regs[3], &sc->arm_r3, err);
1335 __get_user_error(env->regs[4], &sc->arm_r4, err);
1336 __get_user_error(env->regs[5], &sc->arm_r5, err);
1337 __get_user_error(env->regs[6], &sc->arm_r6, err);
1338 __get_user_error(env->regs[7], &sc->arm_r7, err);
1339 __get_user_error(env->regs[8], &sc->arm_r8, err);
1340 __get_user_error(env->regs[9], &sc->arm_r9, err);
1341 __get_user_error(env->regs[10], &sc->arm_r10, err);
1342 __get_user_error(env->regs[11], &sc->arm_fp, err);
1343 __get_user_error(env->regs[12], &sc->arm_ip, err);
1344 __get_user_error(env->regs[13], &sc->arm_sp, err);
1345 __get_user_error(env->regs[14], &sc->arm_lr, err);
1346 __get_user_error(env->regs[15], &sc->arm_pc, err);
1347#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001348 __get_user_error(cpsr, &sc->arm_cpsr, err);
pbrook75b680e2008-03-21 16:07:30 +00001349 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001350#endif
1351
1352 err |= !valid_user_regs(env);
1353
1354 return err;
1355}
1356
1357long do_sigreturn(CPUState *env)
1358{
bellardf8b0aa22007-11-11 23:03:42 +00001359 abi_ulong frame_addr;
bellard43fff232003-07-09 19:31:39 +00001360 struct sigframe *frame;
1361 target_sigset_t set;
1362 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001363 int i;
bellard43fff232003-07-09 19:31:39 +00001364
1365 /*
1366 * Since we stacked the signal on a 64-bit boundary,
1367 * then 'sp' should be word aligned here. If it's
1368 * not, then the user is trying to mess with us.
1369 */
1370 if (env->regs[13] & 7)
1371 goto badframe;
1372
bellardf8b0aa22007-11-11 23:03:42 +00001373 frame_addr = env->regs[13];
1374 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1375 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001376
bellard92319442004-06-19 16:58:13 +00001377 if (__get_user(set.sig[0], &frame->sc.oldmask))
1378 goto badframe;
1379 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1380 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1381 goto badframe;
1382 }
bellard43fff232003-07-09 19:31:39 +00001383
bellard92319442004-06-19 16:58:13 +00001384 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001385 sigprocmask(SIG_SETMASK, &host_set, NULL);
1386
1387 if (restore_sigcontext(env, &frame->sc))
1388 goto badframe;
1389
1390#if 0
1391 /* Send SIGTRAP if we're single-stepping */
1392 if (ptrace_cancel_bpt(current))
1393 send_sig(SIGTRAP, current, 1);
1394#endif
bellardf8b0aa22007-11-11 23:03:42 +00001395 unlock_user_struct(frame, frame_addr, 0);
1396 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001397
1398badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001399 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001400 force_sig(SIGSEGV /* , current */);
1401 return 0;
1402}
1403
pbrooka745ec62008-05-06 15:36:17 +00001404long do_rt_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001405{
bellardf8b0aa22007-11-11 23:03:42 +00001406 abi_ulong frame_addr;
pbrooka745ec62008-05-06 15:36:17 +00001407 struct rt_sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001408 sigset_t host_set;
1409
1410 /*
1411 * Since we stacked the signal on a 64-bit boundary,
1412 * then 'sp' should be word aligned here. If it's
1413 * not, then the user is trying to mess with us.
1414 */
1415 if (env->regs[13] & 7)
1416 goto badframe;
1417
bellardf8b0aa22007-11-11 23:03:42 +00001418 frame_addr = env->regs[13];
1419 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1420 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001421
bellardb8076a72005-04-07 22:20:31 +00001422 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001423 sigprocmask(SIG_SETMASK, &host_set, NULL);
1424
bellardb8076a72005-04-07 22:20:31 +00001425 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001426 goto badframe;
1427
pbrooka745ec62008-05-06 15:36:17 +00001428 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
thsa04e1342007-09-27 13:57:58 +00001429 goto badframe;
1430
bellard43fff232003-07-09 19:31:39 +00001431#if 0
1432 /* Send SIGTRAP if we're single-stepping */
1433 if (ptrace_cancel_bpt(current))
1434 send_sig(SIGTRAP, current, 1);
1435#endif
bellardf8b0aa22007-11-11 23:03:42 +00001436 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001437 return env->regs[0];
1438
1439badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001440 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001441 force_sig(SIGSEGV /* , current */);
1442 return 0;
1443}
1444
pbrooka745ec62008-05-06 15:36:17 +00001445long do_rt_sigreturn_v2(CPUState *env)
1446{
1447 abi_ulong frame_addr;
1448 struct rt_sigframe_v2 *frame;
1449 sigset_t host_set;
1450
1451 /*
1452 * Since we stacked the signal on a 64-bit boundary,
1453 * then 'sp' should be word aligned here. If it's
1454 * not, then the user is trying to mess with us.
1455 */
1456 if (env->regs[13] & 7)
1457 goto badframe;
1458
1459 frame_addr = env->regs[13];
1460 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1461 goto badframe;
1462
1463 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1464 sigprocmask(SIG_SETMASK, &host_set, NULL);
1465
1466 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1467 goto badframe;
1468
1469 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v2, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1470 goto badframe;
1471
1472#if 0
1473 /* Send SIGTRAP if we're single-stepping */
1474 if (ptrace_cancel_bpt(current))
1475 send_sig(SIGTRAP, current, 1);
1476#endif
1477 unlock_user_struct(frame, frame_addr, 0);
1478 return env->regs[0];
1479
1480badframe:
1481 unlock_user_struct(frame, frame_addr, 0);
1482 force_sig(SIGSEGV /* , current */);
1483 return 0;
1484}
1485
1486long do_rt_sigreturn(CPUState *env)
1487{
1488 if (get_osversion() >= 0x020612) {
1489 return do_rt_sigreturn_v2(env);
1490 } else {
1491 return do_rt_sigreturn_v1(env);
1492 }
1493}
1494
bellard6d5e2162004-09-30 22:04:13 +00001495#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001496
bellard6d5e2162004-09-30 22:04:13 +00001497#define __SUNOS_MAXWIN 31
1498
1499/* This is what SunOS does, so shall I. */
1500struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001501 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001502
blueswir1992f48a2007-10-14 16:27:31 +00001503 abi_ulong sigc_mask; /* sigmask to restore */
1504 abi_ulong sigc_sp; /* stack pointer */
1505 abi_ulong sigc_pc; /* program counter */
1506 abi_ulong sigc_npc; /* next program counter */
1507 abi_ulong sigc_psr; /* for condition codes etc */
1508 abi_ulong sigc_g1; /* User uses these two registers */
1509 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001510
1511 /* Now comes information regarding the users window set
1512 * at the time of the signal.
1513 */
blueswir1992f48a2007-10-14 16:27:31 +00001514 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001515
1516 /* stack ptrs for each regwin buf */
1517 char *sigc_spbuf[__SUNOS_MAXWIN];
1518
1519 /* Windows to restore after signal */
1520 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001521 abi_ulong locals[8];
1522 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001523 } sigc_wbuf[__SUNOS_MAXWIN];
1524};
1525/* A Sparc stack frame */
1526struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001527 abi_ulong locals[8];
1528 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001529 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001530 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001531 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001532 abi_ulong xargs[6];
1533 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001534};
1535
1536typedef struct {
1537 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001538 abi_ulong psr;
1539 abi_ulong pc;
1540 abi_ulong npc;
1541 abi_ulong y;
1542 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001543 } si_regs;
1544 int si_mask;
1545} __siginfo_t;
1546
1547typedef struct {
1548 unsigned long si_float_regs [32];
1549 unsigned long si_fsr;
1550 unsigned long si_fpqdepth;
1551 struct {
1552 unsigned long *insn_addr;
1553 unsigned long insn;
1554 } si_fpqueue [16];
bellard74ccb342006-07-18 21:23:34 +00001555} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001556
1557
1558struct target_signal_frame {
1559 struct sparc_stackf ss;
1560 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00001561 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001562 abi_ulong insns[2] __attribute__ ((aligned (8)));
1563 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1564 abi_ulong extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001565 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001566};
1567struct target_rt_signal_frame {
1568 struct sparc_stackf ss;
1569 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001570 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001571 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00001572 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001573 unsigned int insns[2];
1574 stack_t stack;
1575 unsigned int extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001576 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001577};
1578
bellarde80cfcf2004-12-19 23:18:01 +00001579#define UREG_O0 16
1580#define UREG_O6 22
1581#define UREG_I0 0
1582#define UREG_I1 1
1583#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001584#define UREG_I3 3
1585#define UREG_I4 4
1586#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001587#define UREG_I6 6
1588#define UREG_I7 7
1589#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001590#define UREG_FP UREG_I6
1591#define UREG_SP UREG_O6
1592
bellard459a4012007-11-11 19:45:10 +00001593static inline abi_ulong get_sigframe(struct emulated_sigaction *sa,
1594 CPUState *env, unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001595{
bellard459a4012007-11-11 19:45:10 +00001596 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001597
1598 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001599
1600 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00001601 if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1602 if (!on_sig_stack(sp)
1603 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1604 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001605 }
bellard459a4012007-11-11 19:45:10 +00001606 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001607}
1608
1609static int
blueswir1992f48a2007-10-14 16:27:31 +00001610setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001611{
1612 int err = 0, i;
1613
bellard6d5e2162004-09-30 22:04:13 +00001614 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001615 err |= __put_user(env->pc, &si->si_regs.pc);
1616 err |= __put_user(env->npc, &si->si_regs.npc);
1617 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001618 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001619 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1620 }
bellarda315a142005-01-30 22:59:18 +00001621 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001622 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001623 }
bellard6d5e2162004-09-30 22:04:13 +00001624 err |= __put_user(mask, &si->si_mask);
1625 return err;
1626}
bellarde80cfcf2004-12-19 23:18:01 +00001627
bellard80a9d032005-01-03 23:31:27 +00001628#if 0
bellard6d5e2162004-09-30 22:04:13 +00001629static int
1630setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1631 CPUState *env, unsigned long mask)
1632{
1633 int err = 0;
1634
1635 err |= __put_user(mask, &sc->sigc_mask);
1636 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1637 err |= __put_user(env->pc, &sc->sigc_pc);
1638 err |= __put_user(env->npc, &sc->sigc_npc);
1639 err |= __put_user(env->psr, &sc->sigc_psr);
1640 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1641 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1642
1643 return err;
1644}
bellard80a9d032005-01-03 23:31:27 +00001645#endif
bellard6d5e2162004-09-30 22:04:13 +00001646#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1647
1648static void setup_frame(int sig, struct emulated_sigaction *ka,
1649 target_sigset_t *set, CPUState *env)
1650{
bellard459a4012007-11-11 19:45:10 +00001651 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001652 struct target_signal_frame *sf;
1653 int sigframe_size, err, i;
1654
1655 /* 1. Make sure everything is clean */
1656 //synchronize_user_stack();
1657
1658 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001659 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001660
bellard459a4012007-11-11 19:45:10 +00001661 sf = lock_user(VERIFY_WRITE, sf_addr,
1662 sizeof(struct target_signal_frame), 0);
1663 if (!sf)
1664 goto sigsegv;
1665
bellarde80cfcf2004-12-19 23:18:01 +00001666 //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
bellard6d5e2162004-09-30 22:04:13 +00001667#if 0
1668 if (invalid_frame_pointer(sf, sigframe_size))
1669 goto sigill_and_return;
1670#endif
1671 /* 2. Save the current process state */
1672 err = setup___siginfo(&sf->info, env, set->sig[0]);
1673 err |= __put_user(0, &sf->extra_size);
1674
1675 //err |= save_fpu_state(regs, &sf->fpu_state);
1676 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1677
1678 err |= __put_user(set->sig[0], &sf->info.si_mask);
1679 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1680 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1681 }
1682
bellarda315a142005-01-30 22:59:18 +00001683 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001684 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001685 }
bellarda315a142005-01-30 22:59:18 +00001686 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001687 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001688 }
bellard6d5e2162004-09-30 22:04:13 +00001689 if (err)
1690 goto sigsegv;
1691
1692 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001693 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001694 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001695 env->regwptr[UREG_I1] = sf_addr +
1696 offsetof(struct target_signal_frame, info);
1697 env->regwptr[UREG_I2] = sf_addr +
1698 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001699
1700 /* 4. signal handler */
bellard459a4012007-11-11 19:45:10 +00001701 env->pc = ka->sa._sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00001702 env->npc = (env->pc + 4);
1703 /* 5. return to kernel instructions */
1704 if (ka->sa.sa_restorer)
bellard459a4012007-11-11 19:45:10 +00001705 env->regwptr[UREG_I7] = ka->sa.sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00001706 else {
bellard775b58d2007-11-11 16:22:17 +00001707 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00001708
1709 env->regwptr[UREG_I7] = sf_addr +
1710 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00001711
1712 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001713 val32 = 0x821020d8;
1714 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001715
1716 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001717 val32 = 0x91d02010;
1718 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001719 if (err)
1720 goto sigsegv;
1721
1722 /* Flush instruction space. */
1723 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001724 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001725 }
bellard459a4012007-11-11 19:45:10 +00001726 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001727 return;
bellard459a4012007-11-11 19:45:10 +00001728#if 0
1729sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001730 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00001731#endif
bellard6d5e2162004-09-30 22:04:13 +00001732sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001733 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00001734 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001735 force_sig(TARGET_SIGSEGV);
1736}
1737static inline int
bellard74ccb342006-07-18 21:23:34 +00001738restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001739{
1740 int err;
1741#if 0
1742#ifdef CONFIG_SMP
1743 if (current->flags & PF_USEDFPU)
1744 regs->psr &= ~PSR_EF;
1745#else
1746 if (current == last_task_used_math) {
1747 last_task_used_math = 0;
1748 regs->psr &= ~PSR_EF;
1749 }
1750#endif
1751 current->used_math = 1;
1752 current->flags &= ~PF_USEDFPU;
1753#endif
1754#if 0
1755 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1756 return -EFAULT;
1757#endif
1758
bellardfafffae2006-10-28 12:09:16 +00001759#if 0
1760 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001761 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1762 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001763#endif
bellard6d5e2162004-09-30 22:04:13 +00001764 err |= __get_user(env->fsr, &fpu->si_fsr);
1765#if 0
1766 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1767 if (current->thread.fpqdepth != 0)
1768 err |= __copy_from_user(&current->thread.fpqueue[0],
1769 &fpu->si_fpqueue[0],
1770 ((sizeof(unsigned long) +
1771 (sizeof(unsigned long *)))*16));
1772#endif
1773 return err;
1774}
1775
1776
ths5fafdf22007-09-16 21:08:06 +00001777static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001778 target_siginfo_t *info,
1779 target_sigset_t *set, CPUState *env)
1780{
1781 fprintf(stderr, "setup_rt_frame: not implemented\n");
1782}
1783
1784long do_sigreturn(CPUState *env)
1785{
bellardf8b0aa22007-11-11 23:03:42 +00001786 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001787 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001788 uint32_t up_psr, pc, npc;
bellard6d5e2162004-09-30 22:04:13 +00001789 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001790 sigset_t host_set;
bellardf8b0aa22007-11-11 23:03:42 +00001791 abi_ulong fpu_save_addr;
bellarde80cfcf2004-12-19 23:18:01 +00001792 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001793
bellardf8b0aa22007-11-11 23:03:42 +00001794 sf_addr = env->regwptr[UREG_FP];
1795 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1796 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00001797#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001798 fprintf(stderr, "sigreturn\n");
1799 fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
bellard80a9d032005-01-03 23:31:27 +00001800#endif
bellarde80cfcf2004-12-19 23:18:01 +00001801 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001802
1803 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00001804
bellardf8b0aa22007-11-11 23:03:42 +00001805 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00001806 goto segv_and_exit;
1807
1808 err = __get_user(pc, &sf->info.si_regs.pc);
1809 err |= __get_user(npc, &sf->info.si_regs.npc);
1810
bellard6d5e2162004-09-30 22:04:13 +00001811 if ((pc | npc) & 3)
1812 goto segv_and_exit;
1813
1814 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001815 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1816
bellard6d5e2162004-09-30 22:04:13 +00001817 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001818 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1819 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1820
1821 env->pc = pc;
1822 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001823 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001824 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001825 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1826 }
bellarda315a142005-01-30 22:59:18 +00001827 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001828 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1829 }
bellard6d5e2162004-09-30 22:04:13 +00001830
bellardf8b0aa22007-11-11 23:03:42 +00001831 err |= __get_user(fpu_save_addr, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001832
bellarde80cfcf2004-12-19 23:18:01 +00001833 //if (fpu_save)
1834 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001835
1836 /* This is pretty much atomic, no amount locking would prevent
1837 * the races which exist anyways.
1838 */
1839 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001840 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1841 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1842 }
1843
1844 target_to_host_sigset_internal(&host_set, &set);
1845 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001846
1847 if (err)
1848 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00001849 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001850 return env->regwptr[0];
1851
1852segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00001853 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001854 force_sig(TARGET_SIGSEGV);
1855}
1856
1857long do_rt_sigreturn(CPUState *env)
1858{
1859 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00001860 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00001861}
1862
bellard459a4012007-11-11 19:45:10 +00001863#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00001864#define MC_TSTATE 0
1865#define MC_PC 1
1866#define MC_NPC 2
1867#define MC_Y 3
1868#define MC_G1 4
1869#define MC_G2 5
1870#define MC_G3 6
1871#define MC_G4 7
1872#define MC_G5 8
1873#define MC_G6 9
1874#define MC_G7 10
1875#define MC_O0 11
1876#define MC_O1 12
1877#define MC_O2 13
1878#define MC_O3 14
1879#define MC_O4 15
1880#define MC_O5 16
1881#define MC_O6 17
1882#define MC_O7 18
1883#define MC_NGREG 19
1884
blueswir1992f48a2007-10-14 16:27:31 +00001885typedef abi_ulong target_mc_greg_t;
blueswir15bfb56b2007-10-05 17:01:51 +00001886typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1887
1888struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00001889 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001890 uint32_t mcfq_insn;
1891};
1892
1893struct target_mc_fpu {
1894 union {
1895 uint32_t sregs[32];
1896 uint64_t dregs[32];
1897 //uint128_t qregs[16];
1898 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00001899 abi_ulong mcfpu_fsr;
1900 abi_ulong mcfpu_fprs;
1901 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00001902 struct target_mc_fq *mcfpu_fq;
1903 unsigned char mcfpu_qcnt;
1904 unsigned char mcfpu_qentsz;
1905 unsigned char mcfpu_enab;
1906};
1907typedef struct target_mc_fpu target_mc_fpu_t;
1908
1909typedef struct {
1910 target_mc_gregset_t mc_gregs;
1911 target_mc_greg_t mc_fp;
1912 target_mc_greg_t mc_i7;
1913 target_mc_fpu_t mc_fpregs;
1914} target_mcontext_t;
1915
1916struct target_ucontext {
1917 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00001918 abi_ulong uc_flags;
blueswir15bfb56b2007-10-05 17:01:51 +00001919 target_sigset_t uc_sigmask;
1920 target_mcontext_t uc_mcontext;
1921};
1922
1923/* A V9 register window */
1924struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00001925 abi_ulong locals[8];
1926 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00001927};
1928
1929#define TARGET_STACK_BIAS 2047
1930
1931/* {set, get}context() needed for 64-bit SparcLinux userland. */
1932void sparc64_set_context(CPUSPARCState *env)
1933{
bellard459a4012007-11-11 19:45:10 +00001934 abi_ulong ucp_addr;
1935 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00001936 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00001937 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00001938 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001939 unsigned char fenab;
1940 int err;
1941 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00001942
bellard459a4012007-11-11 19:45:10 +00001943 ucp_addr = env->regwptr[UREG_I0];
1944 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
1945 goto do_sigsegv;
blueswir15bfb56b2007-10-05 17:01:51 +00001946 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00001947 err = __get_user(pc, &((*grp)[MC_PC]));
1948 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00001949 if (err || ((pc | npc) & 3))
1950 goto do_sigsegv;
1951 if (env->regwptr[UREG_I1]) {
1952 target_sigset_t target_set;
1953 sigset_t set;
1954
1955 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00001956 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00001957 goto do_sigsegv;
1958 } else {
bellard459a4012007-11-11 19:45:10 +00001959 abi_ulong *src, *dst;
1960 src = ucp->uc_sigmask.sig;
1961 dst = target_set.sig;
blueswir1992f48a2007-10-14 16:27:31 +00001962 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00001963 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00001964 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00001965 if (err)
1966 goto do_sigsegv;
1967 }
1968 target_to_host_sigset_internal(&set, &target_set);
1969 sigprocmask(SIG_SETMASK, &set, NULL);
1970 }
1971 env->pc = pc;
1972 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00001973 err |= __get_user(env->y, &((*grp)[MC_Y]));
1974 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00001975 env->asi = (tstate >> 24) & 0xff;
1976 PUT_CCR(env, tstate >> 32);
1977 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00001978 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
1979 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
1980 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
1981 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
1982 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
1983 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
1984 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
1985 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
1986 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
1987 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
1988 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
1989 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
1990 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
1991 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
1992 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00001993
bellard579a97f2007-11-11 14:26:47 +00001994 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
1995 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00001996
bellard459a4012007-11-11 19:45:10 +00001997 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
1998 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
1999 abi_ulong) != 0)
2000 goto do_sigsegv;
2001 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2002 abi_ulong) != 0)
2003 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002004 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2005 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002006 {
2007 uint32_t *src, *dst;
2008 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2009 dst = env->fpr;
2010 /* XXX: check that the CPU storage is the same as user context */
2011 for (i = 0; i < 64; i++, dst++, src++)
2012 err |= __get_user(*dst, src);
2013 }
bellard579a97f2007-11-11 14:26:47 +00002014 err |= __get_user(env->fsr,
2015 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2016 err |= __get_user(env->gsr,
2017 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002018 if (err)
2019 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002020 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002021 return;
2022 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002023 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002024 force_sig(SIGSEGV);
2025}
2026
2027void sparc64_get_context(CPUSPARCState *env)
2028{
bellard459a4012007-11-11 19:45:10 +00002029 abi_ulong ucp_addr;
2030 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00002031 target_mc_gregset_t *grp;
2032 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002033 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002034 int err;
2035 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002036 target_sigset_t target_set;
2037 sigset_t set;
2038
bellard459a4012007-11-11 19:45:10 +00002039 ucp_addr = env->regwptr[UREG_I0];
2040 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2041 goto do_sigsegv;
2042
blueswir15bfb56b2007-10-05 17:01:51 +00002043 mcp = &ucp->uc_mcontext;
2044 grp = &mcp->mc_gregs;
2045
2046 /* Skip over the trap instruction, first. */
2047 env->pc = env->npc;
2048 env->npc += 4;
2049
2050 err = 0;
2051
2052 sigprocmask(0, NULL, &set);
2053 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002054 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002055 err |= __put_user(target_set.sig[0],
2056 (abi_ulong *)&ucp->uc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002057 } else {
2058 abi_ulong *src, *dst;
2059 src = target_set.sig;
2060 dst = ucp->uc_sigmask.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002061 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002062 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002063 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00002064 if (err)
2065 goto do_sigsegv;
2066 }
2067
bellard459a4012007-11-11 19:45:10 +00002068 /* XXX: tstate must be saved properly */
2069 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00002070 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2071 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2072 err |= __put_user(env->y, &((*grp)[MC_Y]));
2073 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2074 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2075 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2076 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2077 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2078 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2079 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2080 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2081 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2082 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2083 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2084 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2085 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2086 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2087 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002088
bellard459a4012007-11-11 19:45:10 +00002089 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2090 fp = i7 = 0;
2091 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2092 abi_ulong) != 0)
2093 goto do_sigsegv;
2094 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2095 abi_ulong) != 0)
2096 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002097 err |= __put_user(fp, &(mcp->mc_fp));
2098 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002099
bellard459a4012007-11-11 19:45:10 +00002100 {
2101 uint32_t *src, *dst;
2102 src = env->fpr;
2103 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2104 /* XXX: check that the CPU storage is the same as user context */
2105 for (i = 0; i < 64; i++, dst++, src++)
2106 err |= __put_user(*src, dst);
2107 }
bellard579a97f2007-11-11 14:26:47 +00002108 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2109 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2110 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002111
2112 if (err)
2113 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002114 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002115 return;
2116 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002117 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002118 force_sig(SIGSEGV);
2119}
2120#endif
thsd26bc212007-11-08 18:05:37 +00002121#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002122
2123# warning signal handling not implemented
2124
2125static void setup_frame(int sig, struct emulated_sigaction *ka,
2126 target_sigset_t *set, CPUState *env)
2127{
2128 fprintf(stderr, "setup_frame: not implemented\n");
2129}
2130
2131static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2132 target_siginfo_t *info,
2133 target_sigset_t *set, CPUState *env)
2134{
2135 fprintf(stderr, "setup_rt_frame: not implemented\n");
2136}
2137
2138long do_sigreturn(CPUState *env)
2139{
2140 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002141 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002142}
2143
2144long do_rt_sigreturn(CPUState *env)
2145{
2146 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002147 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002148}
2149
thsd26bc212007-11-08 18:05:37 +00002150#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002151
2152# warning signal handling not implemented
2153
2154static void setup_frame(int sig, struct emulated_sigaction *ka,
2155 target_sigset_t *set, CPUState *env)
2156{
2157 fprintf(stderr, "setup_frame: not implemented\n");
2158}
2159
2160static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2161 target_siginfo_t *info,
2162 target_sigset_t *set, CPUState *env)
2163{
2164 fprintf(stderr, "setup_rt_frame: not implemented\n");
2165}
2166
2167long do_sigreturn(CPUState *env)
2168{
2169 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002170 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002171}
2172
2173long do_rt_sigreturn(CPUState *env)
2174{
2175 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002176 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002177}
2178
thsd26bc212007-11-08 18:05:37 +00002179#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002180
2181struct target_sigcontext {
2182 uint32_t sc_regmask; /* Unused */
2183 uint32_t sc_status;
2184 uint64_t sc_pc;
2185 uint64_t sc_regs[32];
2186 uint64_t sc_fpregs[32];
2187 uint32_t sc_ownedfp; /* Unused */
2188 uint32_t sc_fpc_csr;
2189 uint32_t sc_fpc_eir; /* Unused */
2190 uint32_t sc_used_math;
2191 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2192 uint64_t sc_mdhi;
2193 uint64_t sc_mdlo;
2194 target_ulong sc_hi1; /* Was sc_cause */
2195 target_ulong sc_lo1; /* Was sc_badvaddr */
2196 target_ulong sc_hi2; /* Was sc_sigset[4] */
2197 target_ulong sc_lo2;
2198 target_ulong sc_hi3;
2199 target_ulong sc_lo3;
2200};
2201
2202struct sigframe {
2203 uint32_t sf_ass[4]; /* argument save space for o32 */
2204 uint32_t sf_code[2]; /* signal trampoline */
2205 struct target_sigcontext sf_sc;
2206 target_sigset_t sf_mask;
2207};
2208
2209/* Install trampoline to jump back from signal handler */
2210static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2211{
2212 int err;
2213
2214 /*
2215 * Set up the return code ...
2216 *
2217 * li v0, __NR__foo_sigreturn
2218 * syscall
2219 */
2220
2221 err = __put_user(0x24020000 + syscall, tramp + 0);
2222 err |= __put_user(0x0000000c , tramp + 1);
2223 /* flush_cache_sigtramp((unsigned long) tramp); */
2224 return err;
2225}
2226
2227static inline int
2228setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2229{
2230 int err = 0;
2231
thsead93602007-09-06 00:18:15 +00002232 err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002233
thsead93602007-09-06 00:18:15 +00002234#define save_gp_reg(i) do { \
thsd0dc7dc2008-02-12 21:01:26 +00002235 err |= __put_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002236 } while(0)
2237 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2238 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2239 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2240 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2241 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2242 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2243 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2244 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2245 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002246#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002247
thsd0dc7dc2008-02-12 21:01:26 +00002248 err |= __put_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2249 err |= __put_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002250
2251 /* Not used yet, but might be useful if we ever have DSP suppport */
2252#if 0
2253 if (cpu_has_dsp) {
2254 err |= __put_user(mfhi1(), &sc->sc_hi1);
2255 err |= __put_user(mflo1(), &sc->sc_lo1);
2256 err |= __put_user(mfhi2(), &sc->sc_hi2);
2257 err |= __put_user(mflo2(), &sc->sc_lo2);
2258 err |= __put_user(mfhi3(), &sc->sc_hi3);
2259 err |= __put_user(mflo3(), &sc->sc_lo3);
2260 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2261 }
2262 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002263#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002264 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2265 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2266 if (cpu_has_dsp) {
2267 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2268 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2269 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2270 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2271 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2272 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2273 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2274 }
ths388bb212007-05-13 13:58:00 +00002275#endif
2276#endif
bellard106ec872006-06-27 21:08:10 +00002277
ths388bb212007-05-13 13:58:00 +00002278#if 0
bellard106ec872006-06-27 21:08:10 +00002279 err |= __put_user(!!used_math(), &sc->sc_used_math);
2280
2281 if (!used_math())
2282 goto out;
2283
2284 /*
2285 * Save FPU state to signal context. Signal handler will "inherit"
2286 * current FPU state.
2287 */
2288 preempt_disable();
2289
2290 if (!is_fpu_owner()) {
2291 own_fpu();
2292 restore_fp(current);
2293 }
2294 err |= save_fp_context(sc);
2295
2296 preempt_enable();
2297 out:
2298#endif
2299 return err;
2300}
2301
2302static inline int
2303restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2304{
2305 int err = 0;
2306
2307 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2308
thsd0dc7dc2008-02-12 21:01:26 +00002309 err |= __get_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2310 err |= __get_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002311
thsead93602007-09-06 00:18:15 +00002312#define restore_gp_reg(i) do { \
thsd0dc7dc2008-02-12 21:01:26 +00002313 err |= __get_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002314 } while(0)
2315 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2316 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2317 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2318 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2319 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2320 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2321 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2322 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2323 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2324 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2325 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002326#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002327
2328#if 0
2329 if (cpu_has_dsp) {
2330 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2331 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2332 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2333 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2334 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2335 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2336 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2337 }
ths388bb212007-05-13 13:58:00 +00002338#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002339 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2340 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2341 if (cpu_has_dsp) {
2342 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2343 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2344 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2345 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2346 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2347 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2348 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2349 }
ths388bb212007-05-13 13:58:00 +00002350#endif
bellard106ec872006-06-27 21:08:10 +00002351
2352 err |= __get_user(used_math, &sc->sc_used_math);
2353 conditional_used_math(used_math);
2354
2355 preempt_disable();
2356
2357 if (used_math()) {
2358 /* restore fpu context if we have used it before */
2359 own_fpu();
2360 err |= restore_fp_context(sc);
2361 } else {
2362 /* signal handler may have used FPU. Give it up. */
2363 lose_fpu();
2364 }
2365
2366 preempt_enable();
2367#endif
2368 return err;
2369}
2370/*
2371 * Determine which stack to use..
2372 */
bellard579a97f2007-11-11 14:26:47 +00002373static inline abi_ulong
bellard106ec872006-06-27 21:08:10 +00002374get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
2375{
2376 unsigned long sp;
2377
2378 /* Default to using normal stack */
thsd0dc7dc2008-02-12 21:01:26 +00002379 sp = regs->gpr[regs->current_tc][29];
bellard106ec872006-06-27 21:08:10 +00002380
2381 /*
2382 * FPU emulator may have it's own trampoline active just
2383 * above the user stack, 16-bytes before the next lowest
2384 * 16 byte boundary. Try to avoid trashing it.
2385 */
2386 sp -= 32;
2387
bellard106ec872006-06-27 21:08:10 +00002388 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00002389 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2390 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2391 }
bellard106ec872006-06-27 21:08:10 +00002392
bellard579a97f2007-11-11 14:26:47 +00002393 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002394}
2395
bellard579a97f2007-11-11 14:26:47 +00002396/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
ths5fafdf22007-09-16 21:08:06 +00002397static void setup_frame(int sig, struct emulated_sigaction * ka,
bellard579a97f2007-11-11 14:26:47 +00002398 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002399{
2400 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002401 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002402 int i;
2403
bellard579a97f2007-11-11 14:26:47 +00002404 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2405 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002406 goto give_sigsegv;
2407
2408 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2409
2410 if(setup_sigcontext(regs, &frame->sf_sc))
2411 goto give_sigsegv;
2412
2413 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2414 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2415 goto give_sigsegv;
2416 }
2417
2418 /*
2419 * Arguments to signal handler:
2420 *
2421 * a0 = signal number
2422 * a1 = 0 (should be cause)
2423 * a2 = pointer to struct sigcontext
2424 *
2425 * $25 and PC point to the signal handler, $29 points to the
2426 * struct sigframe.
2427 */
thsd0dc7dc2008-02-12 21:01:26 +00002428 regs->gpr[regs->current_tc][ 4] = sig;
2429 regs->gpr[regs->current_tc][ 5] = 0;
2430 regs->gpr[regs->current_tc][ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2431 regs->gpr[regs->current_tc][29] = frame_addr;
2432 regs->gpr[regs->current_tc][31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002433 /* The original kernel code sets CP0_EPC to the handler
2434 * since it returns to userland using eret
2435 * we cannot do this here, and we must set PC directly */
thsd0dc7dc2008-02-12 21:01:26 +00002436 regs->PC[regs->current_tc] = regs->gpr[regs->current_tc][25] = ka->sa._sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002437 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002438 return;
2439
2440give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002441 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002442 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002443 return;
bellard106ec872006-06-27 21:08:10 +00002444}
2445
2446long do_sigreturn(CPUState *regs)
2447{
ths388bb212007-05-13 13:58:00 +00002448 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002449 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002450 sigset_t blocked;
2451 target_sigset_t target_set;
2452 int i;
bellard106ec872006-06-27 21:08:10 +00002453
2454#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002455 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002456#endif
thsd0dc7dc2008-02-12 21:01:26 +00002457 frame_addr = regs->gpr[regs->current_tc][29];
bellard579a97f2007-11-11 14:26:47 +00002458 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002459 goto badframe;
2460
ths388bb212007-05-13 13:58:00 +00002461 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002462 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2463 goto badframe;
ths388bb212007-05-13 13:58:00 +00002464 }
bellard106ec872006-06-27 21:08:10 +00002465
ths388bb212007-05-13 13:58:00 +00002466 target_to_host_sigset_internal(&blocked, &target_set);
2467 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002468
ths388bb212007-05-13 13:58:00 +00002469 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002470 goto badframe;
2471
2472#if 0
ths388bb212007-05-13 13:58:00 +00002473 /*
2474 * Don't let your children do this ...
2475 */
2476 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002477 "move\t$29, %0\n\t"
2478 "j\tsyscall_exit"
2479 :/* no outputs */
2480 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002481 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002482#endif
ths3b46e622007-09-17 08:09:54 +00002483
thsead93602007-09-06 00:18:15 +00002484 regs->PC[regs->current_tc] = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002485 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002486 * maybe a problem with nested signals ? */
2487 regs->CP0_EPC = 0;
2488 return 0;
2489
2490badframe:
ths388bb212007-05-13 13:58:00 +00002491 force_sig(TARGET_SIGSEGV/*, current*/);
2492 return 0;
bellard106ec872006-06-27 21:08:10 +00002493}
2494
ths5fafdf22007-09-16 21:08:06 +00002495static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard106ec872006-06-27 21:08:10 +00002496 target_siginfo_t *info,
2497 target_sigset_t *set, CPUState *env)
2498{
2499 fprintf(stderr, "setup_rt_frame: not implemented\n");
2500}
2501
2502long do_rt_sigreturn(CPUState *env)
2503{
2504 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002505 return -TARGET_ENOSYS;
bellard106ec872006-06-27 21:08:10 +00002506}
bellard6d5e2162004-09-30 22:04:13 +00002507
thsc3b5bc82007-12-02 06:31:25 +00002508#elif defined(TARGET_SH4)
2509
2510/*
2511 * code and data structures from linux kernel:
2512 * include/asm-sh/sigcontext.h
2513 * arch/sh/kernel/signal.c
2514 */
2515
2516struct target_sigcontext {
2517 target_ulong oldmask;
2518
2519 /* CPU registers */
2520 target_ulong sc_gregs[16];
2521 target_ulong sc_pc;
2522 target_ulong sc_pr;
2523 target_ulong sc_sr;
2524 target_ulong sc_gbr;
2525 target_ulong sc_mach;
2526 target_ulong sc_macl;
2527
2528 /* FPU registers */
2529 target_ulong sc_fpregs[16];
2530 target_ulong sc_xfpregs[16];
2531 unsigned int sc_fpscr;
2532 unsigned int sc_fpul;
2533 unsigned int sc_ownedfp;
2534};
2535
2536struct target_sigframe
2537{
2538 struct target_sigcontext sc;
2539 target_ulong extramask[TARGET_NSIG_WORDS-1];
2540 uint16_t retcode[3];
2541};
2542
2543
2544struct target_ucontext {
2545 target_ulong uc_flags;
2546 struct target_ucontext *uc_link;
2547 target_stack_t uc_stack;
2548 struct target_sigcontext uc_mcontext;
2549 target_sigset_t uc_sigmask; /* mask last for extensibility */
2550};
2551
2552struct target_rt_sigframe
2553{
2554 struct target_siginfo info;
2555 struct target_ucontext uc;
2556 uint16_t retcode[3];
2557};
2558
2559
2560#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2561#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2562
2563static abi_ulong get_sigframe(struct emulated_sigaction *ka,
2564 unsigned long sp, size_t frame_size)
2565{
2566 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2567 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2568 }
2569
2570 return (sp - frame_size) & -8ul;
2571}
2572
2573static int setup_sigcontext(struct target_sigcontext *sc,
2574 CPUState *regs, unsigned long mask)
2575{
2576 int err = 0;
2577
2578#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2579 COPY(gregs[0]); COPY(gregs[1]);
2580 COPY(gregs[2]); COPY(gregs[3]);
2581 COPY(gregs[4]); COPY(gregs[5]);
2582 COPY(gregs[6]); COPY(gregs[7]);
2583 COPY(gregs[8]); COPY(gregs[9]);
2584 COPY(gregs[10]); COPY(gregs[11]);
2585 COPY(gregs[12]); COPY(gregs[13]);
2586 COPY(gregs[14]); COPY(gregs[15]);
2587 COPY(gbr); COPY(mach);
2588 COPY(macl); COPY(pr);
2589 COPY(sr); COPY(pc);
2590#undef COPY
2591
2592 /* todo: save FPU registers here */
2593
2594 /* non-iBCS2 extensions.. */
2595 err |= __put_user(mask, &sc->oldmask);
2596
2597 return err;
2598}
2599
2600static int restore_sigcontext(struct CPUState *regs,
2601 struct target_sigcontext *sc)
2602{
2603 unsigned int err = 0;
2604
2605#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2606 COPY(gregs[1]);
2607 COPY(gregs[2]); COPY(gregs[3]);
2608 COPY(gregs[4]); COPY(gregs[5]);
2609 COPY(gregs[6]); COPY(gregs[7]);
2610 COPY(gregs[8]); COPY(gregs[9]);
2611 COPY(gregs[10]); COPY(gregs[11]);
2612 COPY(gregs[12]); COPY(gregs[13]);
2613 COPY(gregs[14]); COPY(gregs[15]);
2614 COPY(gbr); COPY(mach);
2615 COPY(macl); COPY(pr);
2616 COPY(sr); COPY(pc);
2617#undef COPY
2618
2619 /* todo: restore FPU registers here */
2620
2621 regs->tra = -1; /* disable syscall checks */
2622 return err;
2623}
2624
2625static void setup_frame(int sig, struct emulated_sigaction *ka,
2626 target_sigset_t *set, CPUState *regs)
2627{
2628 struct target_sigframe *frame;
2629 abi_ulong frame_addr;
2630 int i;
2631 int err = 0;
2632 int signal;
2633
2634 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2635 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2636 goto give_sigsegv;
2637
2638 signal = current_exec_domain_sig(sig);
2639
2640 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2641
2642 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2643 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2644 }
2645
2646 /* Set up to return from userspace. If provided, use a stub
2647 already in userspace. */
2648 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2649 regs->pr = (unsigned long) ka->sa.sa_restorer;
2650 } else {
2651 /* Generate return code (system call to sigreturn) */
2652 err |= __put_user(MOVW(2), &frame->retcode[0]);
2653 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2654 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2655 regs->pr = (unsigned long) frame->retcode;
2656 }
2657
2658 if (err)
2659 goto give_sigsegv;
2660
2661 /* Set up registers for signal handler */
2662 regs->gregs[15] = (unsigned long) frame;
2663 regs->gregs[4] = signal; /* Arg for signal handler */
2664 regs->gregs[5] = 0;
2665 regs->gregs[6] = (unsigned long) &frame->sc;
2666 regs->pc = (unsigned long) ka->sa._sa_handler;
2667
2668 unlock_user_struct(frame, frame_addr, 1);
2669 return;
2670
2671give_sigsegv:
2672 unlock_user_struct(frame, frame_addr, 1);
2673 force_sig(SIGSEGV);
2674}
2675
2676static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2677 target_siginfo_t *info,
2678 target_sigset_t *set, CPUState *regs)
2679{
2680 struct target_rt_sigframe *frame;
2681 abi_ulong frame_addr;
2682 int i;
2683 int err = 0;
2684 int signal;
2685
2686 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2687 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2688 goto give_sigsegv;
2689
2690 signal = current_exec_domain_sig(sig);
2691
2692 err |= copy_siginfo_to_user(&frame->info, info);
2693
2694 /* Create the ucontext. */
2695 err |= __put_user(0, &frame->uc.uc_flags);
2696 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
2697 err |= __put_user((void *)target_sigaltstack_used.ss_sp,
2698 &frame->uc.uc_stack.ss_sp);
2699 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2700 &frame->uc.uc_stack.ss_flags);
2701 err |= __put_user(target_sigaltstack_used.ss_size,
2702 &frame->uc.uc_stack.ss_size);
2703 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2704 regs, set->sig[0]);
2705 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2706 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2707 }
2708
2709 /* Set up to return from userspace. If provided, use a stub
2710 already in userspace. */
2711 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2712 regs->pr = (unsigned long) ka->sa.sa_restorer;
2713 } else {
2714 /* Generate return code (system call to sigreturn) */
2715 err |= __put_user(MOVW(2), &frame->retcode[0]);
2716 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2717 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2718 regs->pr = (unsigned long) frame->retcode;
2719 }
2720
2721 if (err)
2722 goto give_sigsegv;
2723
2724 /* Set up registers for signal handler */
2725 regs->gregs[15] = (unsigned long) frame;
2726 regs->gregs[4] = signal; /* Arg for signal handler */
2727 regs->gregs[5] = (unsigned long) &frame->info;
2728 regs->gregs[6] = (unsigned long) &frame->uc;
2729 regs->pc = (unsigned long) ka->sa._sa_handler;
2730
2731 unlock_user_struct(frame, frame_addr, 1);
2732 return;
2733
2734give_sigsegv:
2735 unlock_user_struct(frame, frame_addr, 1);
2736 force_sig(SIGSEGV);
2737}
2738
2739long do_sigreturn(CPUState *regs)
2740{
2741 struct target_sigframe *frame;
2742 abi_ulong frame_addr;
2743 sigset_t blocked;
2744 target_sigset_t target_set;
2745 int i;
2746 int err = 0;
2747
2748#if defined(DEBUG_SIGNAL)
2749 fprintf(stderr, "do_sigreturn\n");
2750#endif
2751 frame_addr = regs->gregs[15];
2752 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2753 goto badframe;
2754
2755 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2756 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2757 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2758 }
2759
2760 if (err)
2761 goto badframe;
2762
2763 target_to_host_sigset_internal(&blocked, &target_set);
2764 sigprocmask(SIG_SETMASK, &blocked, NULL);
2765
2766 if (restore_sigcontext(regs, &frame->sc))
2767 goto badframe;
2768
2769 unlock_user_struct(frame, frame_addr, 0);
2770 return regs->gregs[0];
2771
2772badframe:
2773 unlock_user_struct(frame, frame_addr, 0);
2774 force_sig(TARGET_SIGSEGV);
2775 return 0;
2776}
2777
2778long do_rt_sigreturn(CPUState *regs)
2779{
2780 struct target_rt_sigframe *frame;
2781 abi_ulong frame_addr;
2782 sigset_t blocked;
2783
2784#if defined(DEBUG_SIGNAL)
2785 fprintf(stderr, "do_rt_sigreturn\n");
2786#endif
2787 frame_addr = regs->gregs[15];
2788 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2789 goto badframe;
2790
2791 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
2792 sigprocmask(SIG_SETMASK, &blocked, NULL);
2793
2794 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
2795 goto badframe;
2796
2797 if (do_sigaltstack(frame_addr +
2798 offsetof(struct target_rt_sigframe, uc.uc_stack),
2799 0, get_sp_from_cpustate(regs)) == -EFAULT)
2800 goto badframe;
2801
2802 unlock_user_struct(frame, frame_addr, 0);
2803 return regs->gregs[0];
2804
2805badframe:
2806 unlock_user_struct(frame, frame_addr, 0);
2807 force_sig(TARGET_SIGSEGV);
2808 return 0;
2809}
edgar_iglb6d3abd2008-02-28 11:29:27 +00002810#elif defined(TARGET_CRIS)
2811
2812struct target_sigcontext {
2813 struct target_pt_regs regs; /* needs to be first */
2814 uint32_t oldmask;
2815 uint32_t usp; /* usp before stacking this gunk on it */
2816};
2817
2818/* Signal frames. */
2819struct target_signal_frame {
2820 struct target_sigcontext sc;
2821 uint32_t extramask[TARGET_NSIG_WORDS - 1];
2822 uint8_t retcode[8]; /* Trampoline code. */
2823};
2824
2825struct rt_signal_frame {
2826 struct siginfo *pinfo;
2827 void *puc;
2828 struct siginfo info;
2829 struct ucontext uc;
2830 uint8_t retcode[8]; /* Trampoline code. */
2831};
2832
2833static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
2834{
edgar_igl9664d922008-03-03 22:23:53 +00002835 __put_user(env->regs[0], &sc->regs.r0);
2836 __put_user(env->regs[1], &sc->regs.r1);
2837 __put_user(env->regs[2], &sc->regs.r2);
2838 __put_user(env->regs[3], &sc->regs.r3);
2839 __put_user(env->regs[4], &sc->regs.r4);
2840 __put_user(env->regs[5], &sc->regs.r5);
2841 __put_user(env->regs[6], &sc->regs.r6);
2842 __put_user(env->regs[7], &sc->regs.r7);
2843 __put_user(env->regs[8], &sc->regs.r8);
2844 __put_user(env->regs[9], &sc->regs.r9);
2845 __put_user(env->regs[10], &sc->regs.r10);
2846 __put_user(env->regs[11], &sc->regs.r11);
2847 __put_user(env->regs[12], &sc->regs.r12);
2848 __put_user(env->regs[13], &sc->regs.r13);
2849 __put_user(env->regs[14], &sc->usp);
2850 __put_user(env->regs[15], &sc->regs.acr);
2851 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
2852 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
2853 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002854}
edgar_igl9664d922008-03-03 22:23:53 +00002855
edgar_iglb6d3abd2008-02-28 11:29:27 +00002856static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
2857{
edgar_igl9664d922008-03-03 22:23:53 +00002858 __get_user(env->regs[0], &sc->regs.r0);
2859 __get_user(env->regs[1], &sc->regs.r1);
2860 __get_user(env->regs[2], &sc->regs.r2);
2861 __get_user(env->regs[3], &sc->regs.r3);
2862 __get_user(env->regs[4], &sc->regs.r4);
2863 __get_user(env->regs[5], &sc->regs.r5);
2864 __get_user(env->regs[6], &sc->regs.r6);
2865 __get_user(env->regs[7], &sc->regs.r7);
2866 __get_user(env->regs[8], &sc->regs.r8);
2867 __get_user(env->regs[9], &sc->regs.r9);
2868 __get_user(env->regs[10], &sc->regs.r10);
2869 __get_user(env->regs[11], &sc->regs.r11);
2870 __get_user(env->regs[12], &sc->regs.r12);
2871 __get_user(env->regs[13], &sc->regs.r13);
2872 __get_user(env->regs[14], &sc->usp);
2873 __get_user(env->regs[15], &sc->regs.acr);
2874 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
2875 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
2876 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002877}
2878
edgar_igl9664d922008-03-03 22:23:53 +00002879static abi_ulong get_sigframe(CPUState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00002880{
edgar_igl9664d922008-03-03 22:23:53 +00002881 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002882 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00002883 sp = (env->regs[R_SP] & ~3);
2884 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002885}
2886
2887static void setup_frame(int sig, struct emulated_sigaction *ka,
2888 target_sigset_t *set, CPUState *env)
2889{
2890 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00002891 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002892 int err = 0;
2893 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002894
edgar_igl9664d922008-03-03 22:23:53 +00002895 frame_addr = get_sigframe(env, sizeof *frame);
2896 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00002897 goto badframe;
2898
2899 /*
2900 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
2901 * use this trampoline anymore but it sets it up for GDB.
2902 * In QEMU, using the trampoline simplifies things a bit so we use it.
2903 *
2904 * This is movu.w __NR_sigreturn, r9; break 13;
2905 */
2906 err |= __put_user(0x9c5f, frame->retcode+0);
2907 err |= __put_user(TARGET_NR_sigreturn,
2908 frame->retcode+2);
2909 err |= __put_user(0xe93d, frame->retcode+4);
2910
2911 /* Save the mask. */
2912 err |= __put_user(set->sig[0], &frame->sc.oldmask);
2913 if (err)
2914 goto badframe;
2915
2916 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2917 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
2918 goto badframe;
2919 }
2920
2921 setup_sigcontext(&frame->sc, env);
2922
2923 /* Move the stack and setup the arguments for the handler. */
2924 env->regs[R_SP] = (uint32_t) frame;
2925 env->regs[10] = sig;
2926 env->pc = (unsigned long) ka->sa._sa_handler;
2927 /* Link SRP so the guest returns through the trampoline. */
2928 env->pregs[PR_SRP] = (uint32_t) &frame->retcode[0];
2929
edgar_igl9664d922008-03-03 22:23:53 +00002930 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002931 return;
2932 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00002933 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002934 force_sig(TARGET_SIGSEGV);
2935}
2936
2937static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2938 target_siginfo_t *info,
2939 target_sigset_t *set, CPUState *env)
2940{
2941 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
2942}
2943
2944long do_sigreturn(CPUState *env)
2945{
2946 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00002947 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002948 target_sigset_t target_set;
2949 sigset_t set;
2950 int i;
2951
edgar_igl9664d922008-03-03 22:23:53 +00002952 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00002953 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00002954 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00002955 goto badframe;
2956
2957 /* Restore blocked signals */
2958 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
2959 goto badframe;
2960 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2961 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
2962 goto badframe;
2963 }
2964 target_to_host_sigset_internal(&set, &target_set);
2965 sigprocmask(SIG_SETMASK, &set, NULL);
2966
2967 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00002968 /* Compensate for the syscall return path advancing brk. */
2969 env->pc -= 2;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002970
edgar_igl9664d922008-03-03 22:23:53 +00002971 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002972 return env->regs[10];
2973 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00002974 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002975 force_sig(TARGET_SIGSEGV);
2976}
2977
2978long do_rt_sigreturn(CPUState *env)
2979{
2980 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
2981 return -TARGET_ENOSYS;
2982}
thsc3b5bc82007-12-02 06:31:25 +00002983
bellardb346ff42003-06-15 20:05:50 +00002984#else
2985
2986static void setup_frame(int sig, struct emulated_sigaction *ka,
2987 target_sigset_t *set, CPUState *env)
2988{
2989 fprintf(stderr, "setup_frame: not implemented\n");
2990}
2991
ths5fafdf22007-09-16 21:08:06 +00002992static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00002993 target_siginfo_t *info,
2994 target_sigset_t *set, CPUState *env)
2995{
2996 fprintf(stderr, "setup_rt_frame: not implemented\n");
2997}
2998
2999long do_sigreturn(CPUState *env)
3000{
3001 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003002 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003003}
3004
3005long do_rt_sigreturn(CPUState *env)
3006{
3007 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003008 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003009}
3010
bellard66fb9762003-03-23 01:06:05 +00003011#endif
3012
3013void process_pending_signals(void *cpu_env)
3014{
3015 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00003016 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00003017 sigset_t set, old_set;
3018 target_sigset_t target_old_set;
bellard66fb9762003-03-23 01:06:05 +00003019 struct emulated_sigaction *k;
3020 struct sigqueue *q;
ths3b46e622007-09-17 08:09:54 +00003021
bellard31e31b82003-02-18 22:55:36 +00003022 if (!signal_pending)
3023 return;
3024
bellard66fb9762003-03-23 01:06:05 +00003025 k = sigact_table;
3026 for(sig = 1; sig <= TARGET_NSIG; sig++) {
3027 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00003028 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00003029 k++;
bellard31e31b82003-02-18 22:55:36 +00003030 }
3031 /* if no signal is pending, just return */
3032 signal_pending = 0;
3033 return;
bellard66fb9762003-03-23 01:06:05 +00003034
bellard31e31b82003-02-18 22:55:36 +00003035 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00003036#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00003037 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00003038#endif
3039 /* dequeue signal */
3040 q = k->first;
3041 k->first = q->next;
3042 if (!k->first)
3043 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00003044
bellard1fddef42005-04-17 19:16:13 +00003045 sig = gdb_handlesig (cpu_env, sig);
3046 if (!sig) {
3047 fprintf (stderr, "Lost signal\n");
3048 abort();
3049 }
bellard66fb9762003-03-23 01:06:05 +00003050
3051 handler = k->sa._sa_handler;
3052 if (handler == TARGET_SIG_DFL) {
3053 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +00003054 if (sig != TARGET_SIGCHLD &&
3055 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +00003056 sig != TARGET_SIGWINCH) {
3057 force_sig(sig);
3058 }
3059 } else if (handler == TARGET_SIG_IGN) {
3060 /* ignore sig */
3061 } else if (handler == TARGET_SIG_ERR) {
3062 force_sig(sig);
3063 } else {
bellard9de5e442003-03-23 16:49:39 +00003064 /* compute the blocked signals during the handler execution */
3065 target_to_host_sigset(&set, &k->sa.sa_mask);
3066 /* SA_NODEFER indicates that the current signal should not be
3067 blocked during the handler */
3068 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
3069 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00003070
bellard9de5e442003-03-23 16:49:39 +00003071 /* block signals in the handler using Linux */
3072 sigprocmask(SIG_BLOCK, &set, &old_set);
3073 /* save the previous blocked signal state to restore it at the
3074 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00003075 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00003076
bellardbc8a22c2003-03-30 21:02:40 +00003077 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00003078#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00003079 {
3080 CPUX86State *env = cpu_env;
3081 if (env->eflags & VM_MASK)
3082 save_v86_state(env);
3083 }
3084#endif
bellard9de5e442003-03-23 16:49:39 +00003085 /* prepare the stack frame of the virtual CPU */
bellard66fb9762003-03-23 01:06:05 +00003086 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
bellard9de5e442003-03-23 16:49:39 +00003087 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00003088 else
bellard9de5e442003-03-23 16:49:39 +00003089 setup_frame(sig, k, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00003090 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
3091 k->sa._sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00003092 }
bellard66fb9762003-03-23 01:06:05 +00003093 if (q != &k->info)
3094 free_sigqueue(q);
bellard31e31b82003-02-18 22:55:36 +00003095}