blob: c3b0cde3b96b90e6ec3ac6471a7632a5ecadb17a [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{
pbrook4cb05962008-05-30 18:05:19 +0000116 if (sig > 64)
117 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000118 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000119}
120
pbrook4cb05962008-05-30 18:05:19 +0000121int target_to_host_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +0000122{
pbrook4cb05962008-05-30 18:05:19 +0000123 if (sig > 64)
124 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000125 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000126}
127
ths5fafdf22007-09-16 21:08:06 +0000128static void host_to_target_sigset_internal(target_sigset_t *d,
bellard92319442004-06-19 16:58:13 +0000129 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000130{
131 int i;
bellard9e5f5282003-07-13 17:33:54 +0000132 unsigned long sigmask;
133 uint32_t target_sigmask;
ths3b46e622007-09-17 08:09:54 +0000134
bellard9e5f5282003-07-13 17:33:54 +0000135 sigmask = ((unsigned long *)s)[0];
136 target_sigmask = 0;
137 for(i = 0; i < 32; i++) {
ths5fafdf22007-09-16 21:08:06 +0000138 if (sigmask & (1 << i))
bellard9e5f5282003-07-13 17:33:54 +0000139 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
140 }
blueswir1992f48a2007-10-14 16:27:31 +0000141#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
bellard92319442004-06-19 16:58:13 +0000142 d->sig[0] = target_sigmask;
bellard9e5f5282003-07-13 17:33:54 +0000143 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard92319442004-06-19 16:58:13 +0000144 d->sig[i] = ((unsigned long *)s)[i];
bellard66fb9762003-03-23 01:06:05 +0000145 }
blueswir1992f48a2007-10-14 16:27:31 +0000146#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
bellard92319442004-06-19 16:58:13 +0000147 d->sig[0] = target_sigmask;
148 d->sig[1] = sigmask >> 32;
bellard9e5f5282003-07-13 17:33:54 +0000149#else
bellard459a4012007-11-11 19:45:10 +0000150 /* XXX: do it */
bellard9e5f5282003-07-13 17:33:54 +0000151#endif
bellard66fb9762003-03-23 01:06:05 +0000152}
153
bellard92319442004-06-19 16:58:13 +0000154void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
155{
156 target_sigset_t d1;
157 int i;
158
159 host_to_target_sigset_internal(&d1, s);
160 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000161 d->sig[i] = tswapl(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000162}
163
164void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000165{
166 int i;
bellard9e5f5282003-07-13 17:33:54 +0000167 unsigned long sigmask;
blueswir1992f48a2007-10-14 16:27:31 +0000168 abi_ulong target_sigmask;
bellard9e5f5282003-07-13 17:33:54 +0000169
bellard92319442004-06-19 16:58:13 +0000170 target_sigmask = s->sig[0];
bellard9e5f5282003-07-13 17:33:54 +0000171 sigmask = 0;
172 for(i = 0; i < 32; i++) {
ths5fafdf22007-09-16 21:08:06 +0000173 if (target_sigmask & (1 << i))
bellard9e5f5282003-07-13 17:33:54 +0000174 sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
175 }
blueswir1992f48a2007-10-14 16:27:31 +0000176#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
bellard9e5f5282003-07-13 17:33:54 +0000177 ((unsigned long *)d)[0] = sigmask;
178 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard92319442004-06-19 16:58:13 +0000179 ((unsigned long *)d)[i] = s->sig[i];
bellard66fb9762003-03-23 01:06:05 +0000180 }
blueswir1992f48a2007-10-14 16:27:31 +0000181#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
bellard92319442004-06-19 16:58:13 +0000182 ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
bellard9e5f5282003-07-13 17:33:54 +0000183#else
bellard459a4012007-11-11 19:45:10 +0000184 /* XXX: do it */
blueswir1992f48a2007-10-14 16:27:31 +0000185#endif /* TARGET_ABI_BITS */
bellard66fb9762003-03-23 01:06:05 +0000186}
187
bellard92319442004-06-19 16:58:13 +0000188void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
189{
190 target_sigset_t s1;
191 int i;
192
193 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000194 s1.sig[i] = tswapl(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000195 target_to_host_sigset_internal(d, &s1);
196}
ths3b46e622007-09-17 08:09:54 +0000197
blueswir1992f48a2007-10-14 16:27:31 +0000198void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000199 const sigset_t *sigset)
200{
bellard9e5f5282003-07-13 17:33:54 +0000201 target_sigset_t d;
202 host_to_target_sigset(&d, sigset);
203 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000204}
205
ths5fafdf22007-09-16 21:08:06 +0000206void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000207 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000208{
bellard9e5f5282003-07-13 17:33:54 +0000209 target_sigset_t d;
210 int i;
211
212 d.sig[0] = *old_sigset;
213 for(i = 1;i < TARGET_NSIG_WORDS; i++)
214 d.sig[i] = 0;
215 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000216}
217
bellard9de5e442003-03-23 16:49:39 +0000218/* siginfo conversion */
219
ths5fafdf22007-09-16 21:08:06 +0000220static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000221 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000222{
bellard9de5e442003-03-23 16:49:39 +0000223 int sig;
224 sig = host_to_target_signal(info->si_signo);
225 tinfo->si_signo = sig;
226 tinfo->si_errno = 0;
227 tinfo->si_code = 0;
ths5fafdf22007-09-16 21:08:06 +0000228 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000229 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000230 /* should never come here, but who knows. The information for
231 the target is irrelevant */
232 tinfo->_sifields._sigfault._addr = 0;
ths7f7f7c82007-07-12 11:02:46 +0000233 } else if (sig == SIGIO) {
234 tinfo->_sifields._sigpoll._fd = info->si_fd;
bellard9de5e442003-03-23 16:49:39 +0000235 } else if (sig >= TARGET_SIGRTMIN) {
236 tinfo->_sifields._rt._pid = info->si_pid;
237 tinfo->_sifields._rt._uid = info->si_uid;
238 /* XXX: potential problem if 64 bit */
ths5fafdf22007-09-16 21:08:06 +0000239 tinfo->_sifields._rt._sigval.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000240 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000241 }
bellard66fb9762003-03-23 01:06:05 +0000242}
243
ths5fafdf22007-09-16 21:08:06 +0000244static void tswap_siginfo(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000245 const target_siginfo_t *info)
246{
247 int sig;
248 sig = info->si_signo;
249 tinfo->si_signo = tswap32(sig);
250 tinfo->si_errno = tswap32(info->si_errno);
251 tinfo->si_code = tswap32(info->si_code);
ths5fafdf22007-09-16 21:08:06 +0000252 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000253 sig == SIGBUS || sig == SIGTRAP) {
ths5fafdf22007-09-16 21:08:06 +0000254 tinfo->_sifields._sigfault._addr =
bellard9de5e442003-03-23 16:49:39 +0000255 tswapl(info->_sifields._sigfault._addr);
ths7f7f7c82007-07-12 11:02:46 +0000256 } else if (sig == SIGIO) {
257 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
bellard9de5e442003-03-23 16:49:39 +0000258 } else if (sig >= TARGET_SIGRTMIN) {
259 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
260 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000261 tinfo->_sifields._rt._sigval.sival_ptr =
bellard9de5e442003-03-23 16:49:39 +0000262 tswapl(info->_sifields._rt._sigval.sival_ptr);
263 }
264}
265
266
267void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
268{
269 host_to_target_siginfo_noswap(tinfo, info);
270 tswap_siginfo(tinfo, tinfo);
271}
272
273/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000274/* XXX: find a solution for 64 bit (additional malloced data is needed) */
bellard9de5e442003-03-23 16:49:39 +0000275void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000276{
277 info->si_signo = tswap32(tinfo->si_signo);
278 info->si_errno = tswap32(tinfo->si_errno);
279 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000280 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
281 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000282 info->si_value.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000283 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000284}
285
bellard31e31b82003-02-18 22:55:36 +0000286void signal_init(void)
287{
288 struct sigaction act;
bellard9e5f5282003-07-13 17:33:54 +0000289 int i, j;
bellard31e31b82003-02-18 22:55:36 +0000290
bellard9e5f5282003-07-13 17:33:54 +0000291 /* generate signal conversion tables */
292 for(i = 1; i <= 64; i++) {
293 if (host_to_target_signal_table[i] == 0)
294 host_to_target_signal_table[i] = i;
295 }
296 for(i = 1; i <= 64; i++) {
297 j = host_to_target_signal_table[i];
298 target_to_host_signal_table[j] = i;
299 }
ths3b46e622007-09-17 08:09:54 +0000300
bellard9de5e442003-03-23 16:49:39 +0000301 /* set all host signal handlers. ALL signals are blocked during
302 the handlers to serialize them. */
303 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000304 act.sa_flags = SA_SIGINFO;
305 act.sa_sigaction = host_signal_handler;
306 for(i = 1; i < NSIG; i++) {
bellardc9087c22003-05-27 23:25:41 +0000307 sigaction(i, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000308 }
ths3b46e622007-09-17 08:09:54 +0000309
bellard31e31b82003-02-18 22:55:36 +0000310 memset(sigact_table, 0, sizeof(sigact_table));
bellard66fb9762003-03-23 01:06:05 +0000311
312 first_free = &sigqueue_table[0];
ths5fafdf22007-09-16 21:08:06 +0000313 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
bellard66fb9762003-03-23 01:06:05 +0000314 sigqueue_table[i].next = &sigqueue_table[i + 1];
315 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
bellard31e31b82003-02-18 22:55:36 +0000316}
317
bellard66fb9762003-03-23 01:06:05 +0000318/* signal queue handling */
319
320static inline struct sigqueue *alloc_sigqueue(void)
321{
322 struct sigqueue *q = first_free;
323 if (!q)
324 return NULL;
325 first_free = q->next;
326 return q;
327}
328
329static inline void free_sigqueue(struct sigqueue *q)
330{
331 q->next = first_free;
332 first_free = q;
333}
334
bellard9de5e442003-03-23 16:49:39 +0000335/* abort execution with signal */
336void __attribute((noreturn)) force_sig(int sig)
bellard66fb9762003-03-23 01:06:05 +0000337{
338 int host_sig;
bellard66fb9762003-03-23 01:06:05 +0000339 host_sig = target_to_host_signal(sig);
ths5fafdf22007-09-16 21:08:06 +0000340 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
bellard66fb9762003-03-23 01:06:05 +0000341 sig, strsignal(host_sig));
bellard9de5e442003-03-23 16:49:39 +0000342#if 1
bellard66fb9762003-03-23 01:06:05 +0000343 _exit(-host_sig);
bellard9de5e442003-03-23 16:49:39 +0000344#else
345 {
346 struct sigaction act;
347 sigemptyset(&act.sa_mask);
348 act.sa_flags = SA_SIGINFO;
349 act.sa_sigaction = SIG_DFL;
350 sigaction(SIGABRT, &act, NULL);
351 abort();
352 }
353#endif
bellard66fb9762003-03-23 01:06:05 +0000354}
355
bellard9de5e442003-03-23 16:49:39 +0000356/* queue a signal so that it will be send to the virtual CPU as soon
357 as possible */
358int queue_signal(int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000359{
bellard66fb9762003-03-23 01:06:05 +0000360 struct emulated_sigaction *k;
bellard9de5e442003-03-23 16:49:39 +0000361 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000362 abi_ulong handler;
bellard66fb9762003-03-23 01:06:05 +0000363
bellard9de5e442003-03-23 16:49:39 +0000364#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000365 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000366 sig);
bellard66fb9762003-03-23 01:06:05 +0000367#endif
bellard9de5e442003-03-23 16:49:39 +0000368 k = &sigact_table[sig - 1];
bellard66fb9762003-03-23 01:06:05 +0000369 handler = k->sa._sa_handler;
370 if (handler == TARGET_SIG_DFL) {
371 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000372 if (sig != TARGET_SIGCHLD &&
373 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +0000374 sig != TARGET_SIGWINCH) {
375 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000376 } else {
377 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000378 }
379 } else if (handler == TARGET_SIG_IGN) {
380 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000381 return 0;
bellard66fb9762003-03-23 01:06:05 +0000382 } else if (handler == TARGET_SIG_ERR) {
383 force_sig(sig);
384 } else {
bellard9de5e442003-03-23 16:49:39 +0000385 pq = &k->first;
386 if (sig < TARGET_SIGRTMIN) {
387 /* if non real time signal, we queue exactly one signal */
388 if (!k->pending)
389 q = &k->info;
390 else
391 return 0;
392 } else {
393 if (!k->pending) {
394 /* first signal */
395 q = &k->info;
396 } else {
397 q = alloc_sigqueue();
398 if (!q)
399 return -EAGAIN;
400 while (*pq != NULL)
401 pq = &(*pq)->next;
402 }
403 }
404 *pq = q;
405 q->info = *info;
406 q->next = NULL;
407 k->pending = 1;
408 /* signal that a new signal is pending */
409 signal_pending = 1;
410 return 1; /* indicates that the signal was queued */
411 }
412}
413
ths5fafdf22007-09-16 21:08:06 +0000414static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000415 void *puc)
416{
417 int sig;
418 target_siginfo_t tinfo;
419
420 /* the CPU emulator uses some host signals to detect exceptions,
421 we we forward to it some signals */
bellardec6338b2007-11-08 14:25:03 +0000422 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
bellardb346ff42003-06-15 20:05:50 +0000423 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000424 return;
425 }
426
427 /* get target signal number */
428 sig = host_to_target_signal(host_signum);
429 if (sig < 1 || sig > TARGET_NSIG)
430 return;
431#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000432 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000433#endif
434 host_to_target_siginfo_noswap(&tinfo, info);
435 if (queue_signal(sig, &tinfo) == 1) {
436 /* interrupt the virtual CPU as soon as possible */
bellard68a79312003-06-30 13:12:32 +0000437 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
bellard66fb9762003-03-23 01:06:05 +0000438 }
bellard31e31b82003-02-18 22:55:36 +0000439}
440
ths0da46a62007-10-20 20:23:07 +0000441/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000442/* compare linux/kernel/signal.c:do_sigaltstack() */
443abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000444{
445 int ret;
446 struct target_sigaltstack oss;
447
448 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000449 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000450 {
451 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
452 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
453 __put_user(sas_ss_flags(sp), &oss.ss_flags);
454 }
455
bellard579a97f2007-11-11 14:26:47 +0000456 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000457 {
bellard579a97f2007-11-11 14:26:47 +0000458 struct target_sigaltstack *uss;
459 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000460
ths0da46a62007-10-20 20:23:07 +0000461 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000462 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000463 || __get_user(ss.ss_sp, &uss->ss_sp)
464 || __get_user(ss.ss_size, &uss->ss_size)
465 || __get_user(ss.ss_flags, &uss->ss_flags))
466 goto out;
bellard579a97f2007-11-11 14:26:47 +0000467 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000468
ths0da46a62007-10-20 20:23:07 +0000469 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000470 if (on_sig_stack(sp))
471 goto out;
472
ths0da46a62007-10-20 20:23:07 +0000473 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000474 if (ss.ss_flags != TARGET_SS_DISABLE
475 && ss.ss_flags != TARGET_SS_ONSTACK
476 && ss.ss_flags != 0)
477 goto out;
478
479 if (ss.ss_flags == TARGET_SS_DISABLE) {
480 ss.ss_size = 0;
481 ss.ss_sp = 0;
482 } else {
ths0da46a62007-10-20 20:23:07 +0000483 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000484 if (ss.ss_size < MINSIGSTKSZ)
485 goto out;
486 }
487
488 target_sigaltstack_used.ss_sp = ss.ss_sp;
489 target_sigaltstack_used.ss_size = ss.ss_size;
490 }
491
bellard579a97f2007-11-11 14:26:47 +0000492 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000493 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000494 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000495 goto out;
thsa04e1342007-09-27 13:57:58 +0000496 }
497
498 ret = 0;
499out:
500 return ret;
501}
502
ths0da46a62007-10-20 20:23:07 +0000503/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000504int do_sigaction(int sig, const struct target_sigaction *act,
505 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000506{
bellard66fb9762003-03-23 01:06:05 +0000507 struct emulated_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000508 struct sigaction act1;
509 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000510 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000511
ths0aeaa8c2007-03-31 19:29:06 +0000512 if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000513 return -EINVAL;
514 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000515#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000516 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
bellard66fb9762003-03-23 01:06:05 +0000517 sig, (int)act, (int)oact);
518#endif
519 if (oact) {
520 oact->_sa_handler = tswapl(k->sa._sa_handler);
521 oact->sa_flags = tswapl(k->sa.sa_flags);
ths388bb212007-05-13 13:58:00 +0000522#if !defined(TARGET_MIPS)
523 oact->sa_restorer = tswapl(k->sa.sa_restorer);
524#endif
bellard66fb9762003-03-23 01:06:05 +0000525 oact->sa_mask = k->sa.sa_mask;
526 }
527 if (act) {
528 k->sa._sa_handler = tswapl(act->_sa_handler);
529 k->sa.sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000530#if !defined(TARGET_MIPS)
531 k->sa.sa_restorer = tswapl(act->sa_restorer);
532#endif
bellard66fb9762003-03-23 01:06:05 +0000533 k->sa.sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000534
535 /* we update the host linux signal state */
536 host_sig = target_to_host_signal(sig);
537 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
538 sigfillset(&act1.sa_mask);
539 act1.sa_flags = SA_SIGINFO;
540 if (k->sa.sa_flags & TARGET_SA_RESTART)
541 act1.sa_flags |= SA_RESTART;
542 /* NOTE: it is important to update the host kernel signal
543 ignore state to avoid getting unexpected interrupted
544 syscalls */
545 if (k->sa._sa_handler == TARGET_SIG_IGN) {
546 act1.sa_sigaction = (void *)SIG_IGN;
547 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
548 act1.sa_sigaction = (void *)SIG_DFL;
549 } else {
550 act1.sa_sigaction = host_signal_handler;
551 }
ths0da46a62007-10-20 20:23:07 +0000552 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000553 }
bellard66fb9762003-03-23 01:06:05 +0000554 }
ths0da46a62007-10-20 20:23:07 +0000555 return ret;
bellard66fb9762003-03-23 01:06:05 +0000556}
bellard31e31b82003-02-18 22:55:36 +0000557
bellard43fff232003-07-09 19:31:39 +0000558#ifndef offsetof
559#define offsetof(type, field) ((size_t) &((type *)0)->field)
560#endif
561
ths5fafdf22007-09-16 21:08:06 +0000562static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
bellard43fff232003-07-09 19:31:39 +0000563 const target_siginfo_t *info)
564{
565 tswap_siginfo(tinfo, info);
566 return 0;
567}
568
thsc3b5bc82007-12-02 06:31:25 +0000569static inline int current_exec_domain_sig(int sig)
570{
571 return /* current->exec_domain && current->exec_domain->signal_invmap
572 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
573}
574
bellard459a4012007-11-11 19:45:10 +0000575#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000576
577/* from the Linux kernel */
578
579struct target_fpreg {
580 uint16_t significand[4];
581 uint16_t exponent;
582};
583
584struct target_fpxreg {
585 uint16_t significand[4];
586 uint16_t exponent;
587 uint16_t padding[3];
588};
589
590struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000591 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000592};
593
594struct target_fpstate {
595 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000596 abi_ulong cw;
597 abi_ulong sw;
598 abi_ulong tag;
599 abi_ulong ipoff;
600 abi_ulong cssel;
601 abi_ulong dataoff;
602 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000603 struct target_fpreg _st[8];
604 uint16_t status;
605 uint16_t magic; /* 0xffff = regular FPU data only */
606
607 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000608 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
609 abi_ulong mxcsr;
610 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000611 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
612 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000613 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000614};
615
616#define X86_FXSR_MAGIC 0x0000
617
618struct target_sigcontext {
619 uint16_t gs, __gsh;
620 uint16_t fs, __fsh;
621 uint16_t es, __esh;
622 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000623 abi_ulong edi;
624 abi_ulong esi;
625 abi_ulong ebp;
626 abi_ulong esp;
627 abi_ulong ebx;
628 abi_ulong edx;
629 abi_ulong ecx;
630 abi_ulong eax;
631 abi_ulong trapno;
632 abi_ulong err;
633 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000634 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000635 abi_ulong eflags;
636 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000637 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000638 abi_ulong fpstate; /* pointer */
639 abi_ulong oldmask;
640 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000641};
642
bellard66fb9762003-03-23 01:06:05 +0000643struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000644 abi_ulong tuc_flags;
645 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +0000646 target_stack_t tuc_stack;
647 struct target_sigcontext tuc_mcontext;
648 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000649};
650
651struct sigframe
652{
blueswir1992f48a2007-10-14 16:27:31 +0000653 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000654 int sig;
655 struct target_sigcontext sc;
656 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000657 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000658 char retcode[8];
659};
660
661struct rt_sigframe
662{
blueswir1992f48a2007-10-14 16:27:31 +0000663 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000664 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000665 abi_ulong pinfo;
666 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000667 struct target_siginfo info;
668 struct target_ucontext uc;
669 struct target_fpstate fpstate;
670 char retcode[8];
671};
672
673/*
674 * Set up a signal frame.
675 */
676
bellard66fb9762003-03-23 01:06:05 +0000677/* XXX: save x87 state */
678static int
679setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
bellard28be6232007-11-11 22:23:38 +0000680 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000681{
682 int err = 0;
bellard775b58d2007-11-11 16:22:17 +0000683 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000684
bellard579a97f2007-11-11 14:26:47 +0000685 /* already locked in setup_frame() */
bellarda52c7572003-06-21 13:14:12 +0000686 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
687 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
688 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
689 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000690 err |= __put_user(env->regs[R_EDI], &sc->edi);
691 err |= __put_user(env->regs[R_ESI], &sc->esi);
692 err |= __put_user(env->regs[R_EBP], &sc->ebp);
693 err |= __put_user(env->regs[R_ESP], &sc->esp);
694 err |= __put_user(env->regs[R_EBX], &sc->ebx);
695 err |= __put_user(env->regs[R_EDX], &sc->edx);
696 err |= __put_user(env->regs[R_ECX], &sc->ecx);
697 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000698 err |= __put_user(env->exception_index, &sc->trapno);
699 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000700 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000701 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000702 err |= __put_user(env->eflags, &sc->eflags);
703 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000704 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000705
bellard28be6232007-11-11 22:23:38 +0000706 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000707 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000708 magic = 0xffff;
709 err |= __put_user(magic, &fpstate->magic);
bellard28be6232007-11-11 22:23:38 +0000710 err |= __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000711
bellard66fb9762003-03-23 01:06:05 +0000712 /* non-iBCS2 extensions.. */
713 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000714 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000715 return err;
716}
717
718/*
719 * Determine which stack to use..
720 */
721
bellard579a97f2007-11-11 14:26:47 +0000722static inline abi_ulong
bellard66fb9762003-03-23 01:06:05 +0000723get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
724{
725 unsigned long esp;
726
727 /* Default to using normal stack */
728 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000729 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +0000730 if (ka->sa.sa_flags & TARGET_SA_ONSTACK) {
731 if (sas_ss_flags(esp) == 0)
732 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
733 }
bellard66fb9762003-03-23 01:06:05 +0000734
735 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000736 else
bellarda52c7572003-06-21 13:14:12 +0000737 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
738 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
739 ka->sa.sa_restorer) {
740 esp = (unsigned long) ka->sa.sa_restorer;
741 }
bellard579a97f2007-11-11 14:26:47 +0000742 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000743}
744
bellard579a97f2007-11-11 14:26:47 +0000745/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
bellard66fb9762003-03-23 01:06:05 +0000746static void setup_frame(int sig, struct emulated_sigaction *ka,
747 target_sigset_t *set, CPUX86State *env)
748{
bellard579a97f2007-11-11 14:26:47 +0000749 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000750 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000751 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000752
bellard579a97f2007-11-11 14:26:47 +0000753 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000754
bellard579a97f2007-11-11 14:26:47 +0000755 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000756 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000757
thsc3b5bc82007-12-02 06:31:25 +0000758 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000759 &frame->sig);
760 if (err)
761 goto give_sigsegv;
762
bellard28be6232007-11-11 22:23:38 +0000763 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
764 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000765 if (err)
766 goto give_sigsegv;
767
bellard92319442004-06-19 16:58:13 +0000768 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
769 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
770 goto give_sigsegv;
771 }
bellard66fb9762003-03-23 01:06:05 +0000772
773 /* Set up to return from userspace. If provided, use a stub
774 already in userspace. */
775 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
776 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
777 } else {
bellard775b58d2007-11-11 16:22:17 +0000778 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000779 abi_ulong retcode_addr;
780 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
781 err |= __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000782 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000783 val16 = 0xb858;
784 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000785 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000786 val16 = 0x80cd;
787 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000788 }
789
790 if (err)
791 goto give_sigsegv;
792
793 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000794 env->regs[R_ESP] = frame_addr;
795 env->eip = ka->sa._sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000796
797 cpu_x86_load_seg(env, R_DS, __USER_DS);
798 cpu_x86_load_seg(env, R_ES, __USER_DS);
799 cpu_x86_load_seg(env, R_SS, __USER_DS);
800 cpu_x86_load_seg(env, R_CS, __USER_CS);
801 env->eflags &= ~TF_MASK;
802
bellard579a97f2007-11-11 14:26:47 +0000803 unlock_user_struct(frame, frame_addr, 1);
804
bellard66fb9762003-03-23 01:06:05 +0000805 return;
806
807give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000808 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000809 if (sig == TARGET_SIGSEGV)
810 ka->sa._sa_handler = TARGET_SIG_DFL;
811 force_sig(TARGET_SIGSEGV /* , current */);
812}
813
bellard579a97f2007-11-11 14:26:47 +0000814/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
ths5fafdf22007-09-16 21:08:06 +0000815static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard9de5e442003-03-23 16:49:39 +0000816 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000817 target_sigset_t *set, CPUX86State *env)
818{
bellard28be6232007-11-11 22:23:38 +0000819 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000820 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000821 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000822
bellard579a97f2007-11-11 14:26:47 +0000823 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000824
bellard579a97f2007-11-11 14:26:47 +0000825 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000826 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000827
thsc3b5bc82007-12-02 06:31:25 +0000828 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000829 &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000830 addr = frame_addr + offsetof(struct rt_sigframe, info);
831 err |= __put_user(addr, &frame->pinfo);
832 addr = frame_addr + offsetof(struct rt_sigframe, uc);
833 err |= __put_user(addr, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000834 err |= copy_siginfo_to_user(&frame->info, info);
835 if (err)
836 goto give_sigsegv;
837
838 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000839 err |= __put_user(0, &frame->uc.tuc_flags);
840 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000841 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000842 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000843 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000844 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000845 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000846 &frame->uc.tuc_stack.ss_size);
847 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000848 env, set->sig[0],
849 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +0000850 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000851 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000852 goto give_sigsegv;
853 }
bellard66fb9762003-03-23 01:06:05 +0000854
855 /* Set up to return from userspace. If provided, use a stub
856 already in userspace. */
857 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
858 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
859 } else {
bellard775b58d2007-11-11 16:22:17 +0000860 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000861 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
862 err |= __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000863 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000864 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000865 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000866 val16 = 0x80cd;
867 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000868 }
869
870 if (err)
871 goto give_sigsegv;
872
873 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000874 env->regs[R_ESP] = frame_addr;
875 env->eip = ka->sa._sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000876
877 cpu_x86_load_seg(env, R_DS, __USER_DS);
878 cpu_x86_load_seg(env, R_ES, __USER_DS);
879 cpu_x86_load_seg(env, R_SS, __USER_DS);
880 cpu_x86_load_seg(env, R_CS, __USER_CS);
881 env->eflags &= ~TF_MASK;
882
bellard579a97f2007-11-11 14:26:47 +0000883 unlock_user_struct(frame, frame_addr, 1);
884
bellard66fb9762003-03-23 01:06:05 +0000885 return;
886
887give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000888 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000889 if (sig == TARGET_SIGSEGV)
890 ka->sa._sa_handler = TARGET_SIG_DFL;
891 force_sig(TARGET_SIGSEGV /* , current */);
892}
893
894static int
895restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
896{
897 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +0000898 abi_ulong fpstate_addr;
899 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +0000900
bellard28be6232007-11-11 22:23:38 +0000901 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
902 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
903 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
904 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +0000905
bellard28be6232007-11-11 22:23:38 +0000906 env->regs[R_EDI] = tswapl(sc->edi);
907 env->regs[R_ESI] = tswapl(sc->esi);
908 env->regs[R_EBP] = tswapl(sc->ebp);
909 env->regs[R_ESP] = tswapl(sc->esp);
910 env->regs[R_EBX] = tswapl(sc->ebx);
911 env->regs[R_EDX] = tswapl(sc->edx);
912 env->regs[R_ECX] = tswapl(sc->ecx);
913 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +0000914
915 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
916 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000917
bellard28be6232007-11-11 22:23:38 +0000918 tmpflags = tswapl(sc->eflags);
919 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
920 // regs->orig_eax = -1; /* disable syscall checks */
921
922 fpstate_addr = tswapl(sc->fpstate);
923 if (fpstate_addr != 0) {
924 if (!access_ok(VERIFY_READ, fpstate_addr,
925 sizeof(struct target_fpstate)))
926 goto badframe;
927 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000928 }
929
bellard28be6232007-11-11 22:23:38 +0000930 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +0000931 return err;
bellard66fb9762003-03-23 01:06:05 +0000932badframe:
933 return 1;
bellard66fb9762003-03-23 01:06:05 +0000934}
935
936long do_sigreturn(CPUX86State *env)
937{
bellard579a97f2007-11-11 14:26:47 +0000938 struct sigframe *frame;
939 abi_ulong frame_addr = env->regs[R_ESP] - 8;
bellard66fb9762003-03-23 01:06:05 +0000940 target_sigset_t target_set;
941 sigset_t set;
942 int eax, i;
943
bellard447db212003-05-10 15:10:36 +0000944#if defined(DEBUG_SIGNAL)
945 fprintf(stderr, "do_sigreturn\n");
946#endif
bellard579a97f2007-11-11 14:26:47 +0000947 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
948 goto badframe;
bellard66fb9762003-03-23 01:06:05 +0000949 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +0000950 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
951 goto badframe;
952 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
953 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
954 goto badframe;
955 }
bellard66fb9762003-03-23 01:06:05 +0000956
bellard92319442004-06-19 16:58:13 +0000957 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +0000958 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +0000959
bellard66fb9762003-03-23 01:06:05 +0000960 /* restore registers */
961 if (restore_sigcontext(env, &frame->sc, &eax))
962 goto badframe;
bellard579a97f2007-11-11 14:26:47 +0000963 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000964 return eax;
965
966badframe:
bellard579a97f2007-11-11 14:26:47 +0000967 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000968 force_sig(TARGET_SIGSEGV);
969 return 0;
970}
971
972long do_rt_sigreturn(CPUX86State *env)
973{
bellard28be6232007-11-11 22:23:38 +0000974 abi_ulong frame_addr;
975 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +0000976 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +0000977 int eax;
978
bellard28be6232007-11-11 22:23:38 +0000979 frame_addr = env->regs[R_ESP] - 4;
980 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
981 goto badframe;
bellardb8076a72005-04-07 22:20:31 +0000982 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +0000983 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +0000984
bellardb8076a72005-04-07 22:20:31 +0000985 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +0000986 goto badframe;
987
bellard28be6232007-11-11 22:23:38 +0000988 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
989 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +0000990 goto badframe;
thsa04e1342007-09-27 13:57:58 +0000991
bellard28be6232007-11-11 22:23:38 +0000992 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000993 return eax;
994
995badframe:
bellard28be6232007-11-11 22:23:38 +0000996 unlock_user_struct(frame, frame_addr, 0);
997 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +0000998 return 0;
999}
1000
bellard43fff232003-07-09 19:31:39 +00001001#elif defined(TARGET_ARM)
1002
1003struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001004 abi_ulong trap_no;
1005 abi_ulong error_code;
1006 abi_ulong oldmask;
1007 abi_ulong arm_r0;
1008 abi_ulong arm_r1;
1009 abi_ulong arm_r2;
1010 abi_ulong arm_r3;
1011 abi_ulong arm_r4;
1012 abi_ulong arm_r5;
1013 abi_ulong arm_r6;
1014 abi_ulong arm_r7;
1015 abi_ulong arm_r8;
1016 abi_ulong arm_r9;
1017 abi_ulong arm_r10;
1018 abi_ulong arm_fp;
1019 abi_ulong arm_ip;
1020 abi_ulong arm_sp;
1021 abi_ulong arm_lr;
1022 abi_ulong arm_pc;
1023 abi_ulong arm_cpsr;
1024 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001025};
1026
pbrooka745ec62008-05-06 15:36:17 +00001027struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001028 abi_ulong tuc_flags;
1029 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +00001030 target_stack_t tuc_stack;
1031 struct target_sigcontext tuc_mcontext;
1032 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001033};
1034
pbrooka745ec62008-05-06 15:36:17 +00001035struct target_ucontext_v2 {
1036 abi_ulong tuc_flags;
1037 abi_ulong tuc_link;
1038 target_stack_t tuc_stack;
1039 struct target_sigcontext tuc_mcontext;
1040 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1041 char __unused[128 - sizeof(sigset_t)];
1042 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1043};
1044
pbrooka8c33202008-05-07 23:22:46 +00001045struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001046{
1047 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001048 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1049 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001050};
1051
pbrooka8c33202008-05-07 23:22:46 +00001052struct sigframe_v2
1053{
1054 struct target_ucontext_v2 uc;
1055 abi_ulong retcode;
1056};
1057
pbrooka745ec62008-05-06 15:36:17 +00001058struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001059{
bellardf8b0aa22007-11-11 23:03:42 +00001060 abi_ulong pinfo;
1061 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001062 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001063 struct target_ucontext_v1 uc;
1064 abi_ulong retcode;
1065};
1066
1067struct rt_sigframe_v2
1068{
1069 struct target_siginfo info;
1070 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001071 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001072};
1073
1074#define TARGET_CONFIG_CPU_32 1
1075
1076/*
1077 * For ARM syscalls, we encode the syscall number into the instruction.
1078 */
1079#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1080#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1081
1082/*
1083 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1084 * need two 16-bit instructions.
1085 */
1086#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1087#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1088
blueswir1992f48a2007-10-14 16:27:31 +00001089static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001090 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1091 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1092};
1093
1094
bellard43fff232003-07-09 19:31:39 +00001095#define __get_user_error(x,p,e) __get_user(x, p)
1096
1097static inline int valid_user_regs(CPUState *regs)
1098{
1099 return 1;
1100}
1101
pbrooka8c33202008-05-07 23:22:46 +00001102static void
bellard43fff232003-07-09 19:31:39 +00001103setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
bellardf8b0aa22007-11-11 23:03:42 +00001104 CPUState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001105{
pbrooka8c33202008-05-07 23:22:46 +00001106 __put_user(env->regs[0], &sc->arm_r0);
1107 __put_user(env->regs[1], &sc->arm_r1);
1108 __put_user(env->regs[2], &sc->arm_r2);
1109 __put_user(env->regs[3], &sc->arm_r3);
1110 __put_user(env->regs[4], &sc->arm_r4);
1111 __put_user(env->regs[5], &sc->arm_r5);
1112 __put_user(env->regs[6], &sc->arm_r6);
1113 __put_user(env->regs[7], &sc->arm_r7);
1114 __put_user(env->regs[8], &sc->arm_r8);
1115 __put_user(env->regs[9], &sc->arm_r9);
1116 __put_user(env->regs[10], &sc->arm_r10);
1117 __put_user(env->regs[11], &sc->arm_fp);
1118 __put_user(env->regs[12], &sc->arm_ip);
1119 __put_user(env->regs[13], &sc->arm_sp);
1120 __put_user(env->regs[14], &sc->arm_lr);
1121 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001122#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001123 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001124#endif
1125
pbrooka8c33202008-05-07 23:22:46 +00001126 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1127 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1128 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1129 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001130}
1131
bellard579a97f2007-11-11 14:26:47 +00001132static inline abi_ulong
bellard43fff232003-07-09 19:31:39 +00001133get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1134{
1135 unsigned long sp = regs->regs[13];
1136
bellard43fff232003-07-09 19:31:39 +00001137 /*
1138 * This is the X/Open sanctioned signal stack switching.
1139 */
thsa04e1342007-09-27 13:57:58 +00001140 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1141 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001142 /*
1143 * ATPCS B01 mandates 8-byte alignment
1144 */
bellard579a97f2007-11-11 14:26:47 +00001145 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001146}
1147
1148static int
1149setup_return(CPUState *env, struct emulated_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001150 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001151{
bellardf8b0aa22007-11-11 23:03:42 +00001152 abi_ulong handler = ka->sa._sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001153 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001154 int thumb = handler & 1;
bellard43fff232003-07-09 19:31:39 +00001155
1156 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
bellardf8b0aa22007-11-11 23:03:42 +00001157 retcode = ka->sa.sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001158 } else {
1159 unsigned int idx = thumb;
1160
1161 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1162 idx += 2;
1163
1164 if (__put_user(retcodes[idx], rc))
1165 return 1;
1166#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001167 flush_icache_range((abi_ulong)rc,
1168 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001169#endif
bellardf8b0aa22007-11-11 23:03:42 +00001170 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001171 }
1172
1173 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001174 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001175 env->regs[14] = retcode;
1176 env->regs[15] = handler & (thumb ? ~1 : ~3);
pbrook75b680e2008-03-21 16:07:30 +00001177 env->thumb = thumb;
bellard43fff232003-07-09 19:31:39 +00001178
bellardb5ff1b32005-11-26 10:38:39 +00001179#if 0
bellard43fff232003-07-09 19:31:39 +00001180#ifdef TARGET_CONFIG_CPU_32
1181 env->cpsr = cpsr;
1182#endif
bellardb5ff1b32005-11-26 10:38:39 +00001183#endif
bellard43fff232003-07-09 19:31:39 +00001184
1185 return 0;
1186}
1187
pbrooka8c33202008-05-07 23:22:46 +00001188static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1189 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001190{
pbrooka8c33202008-05-07 23:22:46 +00001191 struct target_sigaltstack stack;
1192 int i;
1193
1194 /* Clear all the bits of the ucontext we don't use. */
1195 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1196
1197 memset(&stack, 0, sizeof(stack));
1198 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1199 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1200 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1201 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1202
1203 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1204 /* FIXME: Save coprocessor signal frame. */
1205 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1206 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1207 }
1208}
1209
1210/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1211static void setup_frame_v1(int usig, struct emulated_sigaction *ka,
1212 target_sigset_t *set, CPUState *regs)
1213{
1214 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001215 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001216 int i;
bellard43fff232003-07-09 19:31:39 +00001217
bellard579a97f2007-11-11 14:26:47 +00001218 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1219 return;
1220
pbrooka8c33202008-05-07 23:22:46 +00001221 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001222
bellard92319442004-06-19 16:58:13 +00001223 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1224 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001225 goto end;
bellard43fff232003-07-09 19:31:39 +00001226 }
1227
pbrooka8c33202008-05-07 23:22:46 +00001228 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1229 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001230
1231end:
1232 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001233}
1234
1235static void setup_frame_v2(int usig, struct emulated_sigaction *ka,
1236 target_sigset_t *set, CPUState *regs)
1237{
1238 struct sigframe_v2 *frame;
1239 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1240
1241 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1242 return;
1243
1244 setup_sigframe_v2(&frame->uc, set, regs);
1245
1246 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1247 frame_addr + offsetof(struct sigframe_v2, retcode));
1248
1249 unlock_user_struct(frame, frame_addr, 1);
1250}
1251
1252static void setup_frame(int usig, struct emulated_sigaction *ka,
1253 target_sigset_t *set, CPUState *regs)
1254{
1255 if (get_osversion() >= 0x020612) {
1256 setup_frame_v2(usig, ka, set, regs);
1257 } else {
1258 setup_frame_v1(usig, ka, set, regs);
1259 }
bellard43fff232003-07-09 19:31:39 +00001260}
1261
bellard579a97f2007-11-11 14:26:47 +00001262/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrooka745ec62008-05-06 15:36:17 +00001263static void setup_rt_frame_v1(int usig, struct emulated_sigaction *ka,
1264 target_siginfo_t *info,
1265 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001266{
pbrooka745ec62008-05-06 15:36:17 +00001267 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001268 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001269 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001270 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001271 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001272
bellard579a97f2007-11-11 14:26:47 +00001273 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001274 return /* 1 */;
1275
pbrooka745ec62008-05-06 15:36:17 +00001276 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001277 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001278 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001279 __put_user(uc_addr, &frame->puc);
1280 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001281
1282 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001283 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001284
thsa04e1342007-09-27 13:57:58 +00001285 memset(&stack, 0, sizeof(stack));
1286 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1287 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1288 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001289 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001290
pbrooka8c33202008-05-07 23:22:46 +00001291 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001292 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001293 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001294 goto end;
bellard92319442004-06-19 16:58:13 +00001295 }
bellard43fff232003-07-09 19:31:39 +00001296
pbrooka8c33202008-05-07 23:22:46 +00001297 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1298 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001299
pbrooka8c33202008-05-07 23:22:46 +00001300 env->regs[1] = info_addr;
1301 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001302
1303end:
1304 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001305}
1306
1307static void setup_rt_frame_v2(int usig, struct emulated_sigaction *ka,
1308 target_siginfo_t *info,
1309 target_sigset_t *set, CPUState *env)
1310{
1311 struct rt_sigframe_v2 *frame;
1312 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001313 abi_ulong info_addr, uc_addr;
1314
1315 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1316 return /* 1 */;
1317
1318 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1319 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001320 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001321
pbrooka8c33202008-05-07 23:22:46 +00001322 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001323
pbrooka8c33202008-05-07 23:22:46 +00001324 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1325 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001326
pbrooka8c33202008-05-07 23:22:46 +00001327 env->regs[1] = info_addr;
1328 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001329
bellard579a97f2007-11-11 14:26:47 +00001330 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001331}
1332
pbrooka745ec62008-05-06 15:36:17 +00001333static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1334 target_siginfo_t *info,
1335 target_sigset_t *set, CPUState *env)
1336{
1337 if (get_osversion() >= 0x020612) {
1338 setup_rt_frame_v2(usig, ka, info, set, env);
1339 } else {
1340 setup_rt_frame_v1(usig, ka, info, set, env);
1341 }
1342}
1343
bellard43fff232003-07-09 19:31:39 +00001344static int
1345restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1346{
1347 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001348 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001349
1350 __get_user_error(env->regs[0], &sc->arm_r0, err);
1351 __get_user_error(env->regs[1], &sc->arm_r1, err);
1352 __get_user_error(env->regs[2], &sc->arm_r2, err);
1353 __get_user_error(env->regs[3], &sc->arm_r3, err);
1354 __get_user_error(env->regs[4], &sc->arm_r4, err);
1355 __get_user_error(env->regs[5], &sc->arm_r5, err);
1356 __get_user_error(env->regs[6], &sc->arm_r6, err);
1357 __get_user_error(env->regs[7], &sc->arm_r7, err);
1358 __get_user_error(env->regs[8], &sc->arm_r8, err);
1359 __get_user_error(env->regs[9], &sc->arm_r9, err);
1360 __get_user_error(env->regs[10], &sc->arm_r10, err);
1361 __get_user_error(env->regs[11], &sc->arm_fp, err);
1362 __get_user_error(env->regs[12], &sc->arm_ip, err);
1363 __get_user_error(env->regs[13], &sc->arm_sp, err);
1364 __get_user_error(env->regs[14], &sc->arm_lr, err);
1365 __get_user_error(env->regs[15], &sc->arm_pc, err);
1366#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001367 __get_user_error(cpsr, &sc->arm_cpsr, err);
pbrook75b680e2008-03-21 16:07:30 +00001368 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001369#endif
1370
1371 err |= !valid_user_regs(env);
1372
1373 return err;
1374}
1375
pbrooka8c33202008-05-07 23:22:46 +00001376long do_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001377{
bellardf8b0aa22007-11-11 23:03:42 +00001378 abi_ulong frame_addr;
pbrooka8c33202008-05-07 23:22:46 +00001379 struct sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001380 target_sigset_t set;
1381 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001382 int i;
bellard43fff232003-07-09 19:31:39 +00001383
1384 /*
1385 * Since we stacked the signal on a 64-bit boundary,
1386 * then 'sp' should be word aligned here. If it's
1387 * not, then the user is trying to mess with us.
1388 */
1389 if (env->regs[13] & 7)
1390 goto badframe;
1391
bellardf8b0aa22007-11-11 23:03:42 +00001392 frame_addr = env->regs[13];
1393 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1394 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001395
bellard92319442004-06-19 16:58:13 +00001396 if (__get_user(set.sig[0], &frame->sc.oldmask))
1397 goto badframe;
1398 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1399 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1400 goto badframe;
1401 }
bellard43fff232003-07-09 19:31:39 +00001402
bellard92319442004-06-19 16:58:13 +00001403 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001404 sigprocmask(SIG_SETMASK, &host_set, NULL);
1405
1406 if (restore_sigcontext(env, &frame->sc))
1407 goto badframe;
1408
1409#if 0
1410 /* Send SIGTRAP if we're single-stepping */
1411 if (ptrace_cancel_bpt(current))
1412 send_sig(SIGTRAP, current, 1);
1413#endif
bellardf8b0aa22007-11-11 23:03:42 +00001414 unlock_user_struct(frame, frame_addr, 0);
1415 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001416
1417badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001418 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001419 force_sig(SIGSEGV /* , current */);
1420 return 0;
1421}
1422
pbrooka8c33202008-05-07 23:22:46 +00001423static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1424 struct target_ucontext_v2 *uc)
1425{
1426 sigset_t host_set;
1427
1428 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1429 sigprocmask(SIG_SETMASK, &host_set, NULL);
1430
1431 if (restore_sigcontext(env, &uc->tuc_mcontext))
1432 return 1;
1433
1434 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1435 return 1;
1436
1437#if 0
1438 /* Send SIGTRAP if we're single-stepping */
1439 if (ptrace_cancel_bpt(current))
1440 send_sig(SIGTRAP, current, 1);
1441#endif
1442
1443 return 0;
1444}
1445
1446long do_sigreturn_v2(CPUState *env)
1447{
1448 abi_ulong frame_addr;
1449 struct sigframe_v2 *frame;
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 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1464 goto badframe;
1465
1466 unlock_user_struct(frame, frame_addr, 0);
1467 return env->regs[0];
1468
1469badframe:
1470 unlock_user_struct(frame, frame_addr, 0);
1471 force_sig(SIGSEGV /* , current */);
1472 return 0;
1473}
1474
1475long do_sigreturn(CPUState *env)
1476{
1477 if (get_osversion() >= 0x020612) {
1478 return do_sigreturn_v2(env);
1479 } else {
1480 return do_sigreturn_v1(env);
1481 }
1482}
1483
pbrooka745ec62008-05-06 15:36:17 +00001484long do_rt_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001485{
bellardf8b0aa22007-11-11 23:03:42 +00001486 abi_ulong frame_addr;
pbrooka745ec62008-05-06 15:36:17 +00001487 struct rt_sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001488 sigset_t host_set;
1489
1490 /*
1491 * Since we stacked the signal on a 64-bit boundary,
1492 * then 'sp' should be word aligned here. If it's
1493 * not, then the user is trying to mess with us.
1494 */
1495 if (env->regs[13] & 7)
1496 goto badframe;
1497
bellardf8b0aa22007-11-11 23:03:42 +00001498 frame_addr = env->regs[13];
1499 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1500 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001501
bellardb8076a72005-04-07 22:20:31 +00001502 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001503 sigprocmask(SIG_SETMASK, &host_set, NULL);
1504
bellardb8076a72005-04-07 22:20:31 +00001505 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001506 goto badframe;
1507
pbrooka745ec62008-05-06 15:36:17 +00001508 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 +00001509 goto badframe;
1510
bellard43fff232003-07-09 19:31:39 +00001511#if 0
1512 /* Send SIGTRAP if we're single-stepping */
1513 if (ptrace_cancel_bpt(current))
1514 send_sig(SIGTRAP, current, 1);
1515#endif
bellardf8b0aa22007-11-11 23:03:42 +00001516 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001517 return env->regs[0];
1518
1519badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001520 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001521 force_sig(SIGSEGV /* , current */);
1522 return 0;
1523}
1524
pbrooka745ec62008-05-06 15:36:17 +00001525long do_rt_sigreturn_v2(CPUState *env)
1526{
1527 abi_ulong frame_addr;
1528 struct rt_sigframe_v2 *frame;
pbrooka745ec62008-05-06 15:36:17 +00001529
1530 /*
1531 * Since we stacked the signal on a 64-bit boundary,
1532 * then 'sp' should be word aligned here. If it's
1533 * not, then the user is trying to mess with us.
1534 */
1535 if (env->regs[13] & 7)
1536 goto badframe;
1537
1538 frame_addr = env->regs[13];
1539 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1540 goto badframe;
1541
pbrooka8c33202008-05-07 23:22:46 +00001542 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1543 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00001544
pbrooka745ec62008-05-06 15:36:17 +00001545 unlock_user_struct(frame, frame_addr, 0);
1546 return env->regs[0];
1547
1548badframe:
1549 unlock_user_struct(frame, frame_addr, 0);
1550 force_sig(SIGSEGV /* , current */);
1551 return 0;
1552}
1553
1554long do_rt_sigreturn(CPUState *env)
1555{
1556 if (get_osversion() >= 0x020612) {
1557 return do_rt_sigreturn_v2(env);
1558 } else {
1559 return do_rt_sigreturn_v1(env);
1560 }
1561}
1562
bellard6d5e2162004-09-30 22:04:13 +00001563#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001564
bellard6d5e2162004-09-30 22:04:13 +00001565#define __SUNOS_MAXWIN 31
1566
1567/* This is what SunOS does, so shall I. */
1568struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001569 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001570
blueswir1992f48a2007-10-14 16:27:31 +00001571 abi_ulong sigc_mask; /* sigmask to restore */
1572 abi_ulong sigc_sp; /* stack pointer */
1573 abi_ulong sigc_pc; /* program counter */
1574 abi_ulong sigc_npc; /* next program counter */
1575 abi_ulong sigc_psr; /* for condition codes etc */
1576 abi_ulong sigc_g1; /* User uses these two registers */
1577 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001578
1579 /* Now comes information regarding the users window set
1580 * at the time of the signal.
1581 */
blueswir1992f48a2007-10-14 16:27:31 +00001582 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001583
1584 /* stack ptrs for each regwin buf */
1585 char *sigc_spbuf[__SUNOS_MAXWIN];
1586
1587 /* Windows to restore after signal */
1588 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001589 abi_ulong locals[8];
1590 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001591 } sigc_wbuf[__SUNOS_MAXWIN];
1592};
1593/* A Sparc stack frame */
1594struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001595 abi_ulong locals[8];
1596 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001597 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001598 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001599 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001600 abi_ulong xargs[6];
1601 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001602};
1603
1604typedef struct {
1605 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001606 abi_ulong psr;
1607 abi_ulong pc;
1608 abi_ulong npc;
1609 abi_ulong y;
1610 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001611 } si_regs;
1612 int si_mask;
1613} __siginfo_t;
1614
1615typedef struct {
1616 unsigned long si_float_regs [32];
1617 unsigned long si_fsr;
1618 unsigned long si_fpqdepth;
1619 struct {
1620 unsigned long *insn_addr;
1621 unsigned long insn;
1622 } si_fpqueue [16];
bellard74ccb342006-07-18 21:23:34 +00001623} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001624
1625
1626struct target_signal_frame {
1627 struct sparc_stackf ss;
1628 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00001629 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001630 abi_ulong insns[2] __attribute__ ((aligned (8)));
1631 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1632 abi_ulong extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001633 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001634};
1635struct target_rt_signal_frame {
1636 struct sparc_stackf ss;
1637 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001638 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001639 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00001640 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001641 unsigned int insns[2];
1642 stack_t stack;
1643 unsigned int extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001644 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001645};
1646
bellarde80cfcf2004-12-19 23:18:01 +00001647#define UREG_O0 16
1648#define UREG_O6 22
1649#define UREG_I0 0
1650#define UREG_I1 1
1651#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001652#define UREG_I3 3
1653#define UREG_I4 4
1654#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001655#define UREG_I6 6
1656#define UREG_I7 7
1657#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001658#define UREG_FP UREG_I6
1659#define UREG_SP UREG_O6
1660
bellard459a4012007-11-11 19:45:10 +00001661static inline abi_ulong get_sigframe(struct emulated_sigaction *sa,
1662 CPUState *env, unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001663{
bellard459a4012007-11-11 19:45:10 +00001664 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001665
1666 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001667
1668 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00001669 if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1670 if (!on_sig_stack(sp)
1671 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1672 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001673 }
bellard459a4012007-11-11 19:45:10 +00001674 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001675}
1676
1677static int
blueswir1992f48a2007-10-14 16:27:31 +00001678setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001679{
1680 int err = 0, i;
1681
bellard6d5e2162004-09-30 22:04:13 +00001682 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001683 err |= __put_user(env->pc, &si->si_regs.pc);
1684 err |= __put_user(env->npc, &si->si_regs.npc);
1685 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001686 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001687 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1688 }
bellarda315a142005-01-30 22:59:18 +00001689 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001690 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001691 }
bellard6d5e2162004-09-30 22:04:13 +00001692 err |= __put_user(mask, &si->si_mask);
1693 return err;
1694}
bellarde80cfcf2004-12-19 23:18:01 +00001695
bellard80a9d032005-01-03 23:31:27 +00001696#if 0
bellard6d5e2162004-09-30 22:04:13 +00001697static int
1698setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1699 CPUState *env, unsigned long mask)
1700{
1701 int err = 0;
1702
1703 err |= __put_user(mask, &sc->sigc_mask);
1704 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1705 err |= __put_user(env->pc, &sc->sigc_pc);
1706 err |= __put_user(env->npc, &sc->sigc_npc);
1707 err |= __put_user(env->psr, &sc->sigc_psr);
1708 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1709 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1710
1711 return err;
1712}
bellard80a9d032005-01-03 23:31:27 +00001713#endif
bellard6d5e2162004-09-30 22:04:13 +00001714#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1715
1716static void setup_frame(int sig, struct emulated_sigaction *ka,
1717 target_sigset_t *set, CPUState *env)
1718{
bellard459a4012007-11-11 19:45:10 +00001719 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001720 struct target_signal_frame *sf;
1721 int sigframe_size, err, i;
1722
1723 /* 1. Make sure everything is clean */
1724 //synchronize_user_stack();
1725
1726 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001727 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001728
bellard459a4012007-11-11 19:45:10 +00001729 sf = lock_user(VERIFY_WRITE, sf_addr,
1730 sizeof(struct target_signal_frame), 0);
1731 if (!sf)
1732 goto sigsegv;
1733
bellarde80cfcf2004-12-19 23:18:01 +00001734 //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 +00001735#if 0
1736 if (invalid_frame_pointer(sf, sigframe_size))
1737 goto sigill_and_return;
1738#endif
1739 /* 2. Save the current process state */
1740 err = setup___siginfo(&sf->info, env, set->sig[0]);
1741 err |= __put_user(0, &sf->extra_size);
1742
1743 //err |= save_fpu_state(regs, &sf->fpu_state);
1744 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1745
1746 err |= __put_user(set->sig[0], &sf->info.si_mask);
1747 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1748 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1749 }
1750
bellarda315a142005-01-30 22:59:18 +00001751 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001752 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001753 }
bellarda315a142005-01-30 22:59:18 +00001754 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001755 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001756 }
bellard6d5e2162004-09-30 22:04:13 +00001757 if (err)
1758 goto sigsegv;
1759
1760 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001761 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001762 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001763 env->regwptr[UREG_I1] = sf_addr +
1764 offsetof(struct target_signal_frame, info);
1765 env->regwptr[UREG_I2] = sf_addr +
1766 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001767
1768 /* 4. signal handler */
bellard459a4012007-11-11 19:45:10 +00001769 env->pc = ka->sa._sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00001770 env->npc = (env->pc + 4);
1771 /* 5. return to kernel instructions */
1772 if (ka->sa.sa_restorer)
bellard459a4012007-11-11 19:45:10 +00001773 env->regwptr[UREG_I7] = ka->sa.sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00001774 else {
bellard775b58d2007-11-11 16:22:17 +00001775 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00001776
1777 env->regwptr[UREG_I7] = sf_addr +
1778 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00001779
1780 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001781 val32 = 0x821020d8;
1782 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001783
1784 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001785 val32 = 0x91d02010;
1786 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001787 if (err)
1788 goto sigsegv;
1789
1790 /* Flush instruction space. */
1791 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001792 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001793 }
bellard459a4012007-11-11 19:45:10 +00001794 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001795 return;
bellard459a4012007-11-11 19:45:10 +00001796#if 0
1797sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001798 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00001799#endif
bellard6d5e2162004-09-30 22:04:13 +00001800sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001801 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00001802 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001803 force_sig(TARGET_SIGSEGV);
1804}
1805static inline int
bellard74ccb342006-07-18 21:23:34 +00001806restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001807{
1808 int err;
1809#if 0
1810#ifdef CONFIG_SMP
1811 if (current->flags & PF_USEDFPU)
1812 regs->psr &= ~PSR_EF;
1813#else
1814 if (current == last_task_used_math) {
1815 last_task_used_math = 0;
1816 regs->psr &= ~PSR_EF;
1817 }
1818#endif
1819 current->used_math = 1;
1820 current->flags &= ~PF_USEDFPU;
1821#endif
1822#if 0
1823 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1824 return -EFAULT;
1825#endif
1826
bellardfafffae2006-10-28 12:09:16 +00001827#if 0
1828 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001829 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1830 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001831#endif
bellard6d5e2162004-09-30 22:04:13 +00001832 err |= __get_user(env->fsr, &fpu->si_fsr);
1833#if 0
1834 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1835 if (current->thread.fpqdepth != 0)
1836 err |= __copy_from_user(&current->thread.fpqueue[0],
1837 &fpu->si_fpqueue[0],
1838 ((sizeof(unsigned long) +
1839 (sizeof(unsigned long *)))*16));
1840#endif
1841 return err;
1842}
1843
1844
ths5fafdf22007-09-16 21:08:06 +00001845static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001846 target_siginfo_t *info,
1847 target_sigset_t *set, CPUState *env)
1848{
1849 fprintf(stderr, "setup_rt_frame: not implemented\n");
1850}
1851
1852long do_sigreturn(CPUState *env)
1853{
bellardf8b0aa22007-11-11 23:03:42 +00001854 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001855 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001856 uint32_t up_psr, pc, npc;
bellard6d5e2162004-09-30 22:04:13 +00001857 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001858 sigset_t host_set;
bellardf8b0aa22007-11-11 23:03:42 +00001859 abi_ulong fpu_save_addr;
bellarde80cfcf2004-12-19 23:18:01 +00001860 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001861
bellardf8b0aa22007-11-11 23:03:42 +00001862 sf_addr = env->regwptr[UREG_FP];
1863 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1864 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00001865#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001866 fprintf(stderr, "sigreturn\n");
1867 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 +00001868#endif
bellarde80cfcf2004-12-19 23:18:01 +00001869 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001870
1871 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00001872
bellardf8b0aa22007-11-11 23:03:42 +00001873 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00001874 goto segv_and_exit;
1875
1876 err = __get_user(pc, &sf->info.si_regs.pc);
1877 err |= __get_user(npc, &sf->info.si_regs.npc);
1878
bellard6d5e2162004-09-30 22:04:13 +00001879 if ((pc | npc) & 3)
1880 goto segv_and_exit;
1881
1882 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001883 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1884
bellard6d5e2162004-09-30 22:04:13 +00001885 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001886 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1887 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1888
1889 env->pc = pc;
1890 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001891 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001892 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001893 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1894 }
bellarda315a142005-01-30 22:59:18 +00001895 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001896 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1897 }
bellard6d5e2162004-09-30 22:04:13 +00001898
bellardf8b0aa22007-11-11 23:03:42 +00001899 err |= __get_user(fpu_save_addr, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001900
bellarde80cfcf2004-12-19 23:18:01 +00001901 //if (fpu_save)
1902 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001903
1904 /* This is pretty much atomic, no amount locking would prevent
1905 * the races which exist anyways.
1906 */
1907 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001908 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1909 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1910 }
1911
1912 target_to_host_sigset_internal(&host_set, &set);
1913 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001914
1915 if (err)
1916 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00001917 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001918 return env->regwptr[0];
1919
1920segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00001921 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001922 force_sig(TARGET_SIGSEGV);
1923}
1924
1925long do_rt_sigreturn(CPUState *env)
1926{
1927 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00001928 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00001929}
1930
bellard459a4012007-11-11 19:45:10 +00001931#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00001932#define MC_TSTATE 0
1933#define MC_PC 1
1934#define MC_NPC 2
1935#define MC_Y 3
1936#define MC_G1 4
1937#define MC_G2 5
1938#define MC_G3 6
1939#define MC_G4 7
1940#define MC_G5 8
1941#define MC_G6 9
1942#define MC_G7 10
1943#define MC_O0 11
1944#define MC_O1 12
1945#define MC_O2 13
1946#define MC_O3 14
1947#define MC_O4 15
1948#define MC_O5 16
1949#define MC_O6 17
1950#define MC_O7 18
1951#define MC_NGREG 19
1952
blueswir1992f48a2007-10-14 16:27:31 +00001953typedef abi_ulong target_mc_greg_t;
blueswir15bfb56b2007-10-05 17:01:51 +00001954typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1955
1956struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00001957 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001958 uint32_t mcfq_insn;
1959};
1960
1961struct target_mc_fpu {
1962 union {
1963 uint32_t sregs[32];
1964 uint64_t dregs[32];
1965 //uint128_t qregs[16];
1966 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00001967 abi_ulong mcfpu_fsr;
1968 abi_ulong mcfpu_fprs;
1969 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00001970 struct target_mc_fq *mcfpu_fq;
1971 unsigned char mcfpu_qcnt;
1972 unsigned char mcfpu_qentsz;
1973 unsigned char mcfpu_enab;
1974};
1975typedef struct target_mc_fpu target_mc_fpu_t;
1976
1977typedef struct {
1978 target_mc_gregset_t mc_gregs;
1979 target_mc_greg_t mc_fp;
1980 target_mc_greg_t mc_i7;
1981 target_mc_fpu_t mc_fpregs;
1982} target_mcontext_t;
1983
1984struct target_ucontext {
1985 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00001986 abi_ulong uc_flags;
blueswir15bfb56b2007-10-05 17:01:51 +00001987 target_sigset_t uc_sigmask;
1988 target_mcontext_t uc_mcontext;
1989};
1990
1991/* A V9 register window */
1992struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00001993 abi_ulong locals[8];
1994 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00001995};
1996
1997#define TARGET_STACK_BIAS 2047
1998
1999/* {set, get}context() needed for 64-bit SparcLinux userland. */
2000void sparc64_set_context(CPUSPARCState *env)
2001{
bellard459a4012007-11-11 19:45:10 +00002002 abi_ulong ucp_addr;
2003 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00002004 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00002005 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00002006 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002007 unsigned char fenab;
2008 int err;
2009 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002010
bellard459a4012007-11-11 19:45:10 +00002011 ucp_addr = env->regwptr[UREG_I0];
2012 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2013 goto do_sigsegv;
blueswir15bfb56b2007-10-05 17:01:51 +00002014 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00002015 err = __get_user(pc, &((*grp)[MC_PC]));
2016 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002017 if (err || ((pc | npc) & 3))
2018 goto do_sigsegv;
2019 if (env->regwptr[UREG_I1]) {
2020 target_sigset_t target_set;
2021 sigset_t set;
2022
2023 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002024 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002025 goto do_sigsegv;
2026 } else {
bellard459a4012007-11-11 19:45:10 +00002027 abi_ulong *src, *dst;
2028 src = ucp->uc_sigmask.sig;
2029 dst = target_set.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002030 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002031 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002032 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00002033 if (err)
2034 goto do_sigsegv;
2035 }
2036 target_to_host_sigset_internal(&set, &target_set);
2037 sigprocmask(SIG_SETMASK, &set, NULL);
2038 }
2039 env->pc = pc;
2040 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00002041 err |= __get_user(env->y, &((*grp)[MC_Y]));
2042 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002043 env->asi = (tstate >> 24) & 0xff;
2044 PUT_CCR(env, tstate >> 32);
2045 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00002046 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2047 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2048 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2049 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2050 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2051 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2052 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2053 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2054 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2055 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2056 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2057 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2058 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2059 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2060 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002061
bellard579a97f2007-11-11 14:26:47 +00002062 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2063 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002064
bellard459a4012007-11-11 19:45:10 +00002065 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2066 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2067 abi_ulong) != 0)
2068 goto do_sigsegv;
2069 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2070 abi_ulong) != 0)
2071 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002072 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2073 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002074 {
2075 uint32_t *src, *dst;
2076 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2077 dst = env->fpr;
2078 /* XXX: check that the CPU storage is the same as user context */
2079 for (i = 0; i < 64; i++, dst++, src++)
2080 err |= __get_user(*dst, src);
2081 }
bellard579a97f2007-11-11 14:26:47 +00002082 err |= __get_user(env->fsr,
2083 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2084 err |= __get_user(env->gsr,
2085 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002086 if (err)
2087 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002088 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002089 return;
2090 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002091 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002092 force_sig(SIGSEGV);
2093}
2094
2095void sparc64_get_context(CPUSPARCState *env)
2096{
bellard459a4012007-11-11 19:45:10 +00002097 abi_ulong ucp_addr;
2098 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00002099 target_mc_gregset_t *grp;
2100 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002101 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002102 int err;
2103 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002104 target_sigset_t target_set;
2105 sigset_t set;
2106
bellard459a4012007-11-11 19:45:10 +00002107 ucp_addr = env->regwptr[UREG_I0];
2108 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2109 goto do_sigsegv;
2110
blueswir15bfb56b2007-10-05 17:01:51 +00002111 mcp = &ucp->uc_mcontext;
2112 grp = &mcp->mc_gregs;
2113
2114 /* Skip over the trap instruction, first. */
2115 env->pc = env->npc;
2116 env->npc += 4;
2117
2118 err = 0;
2119
2120 sigprocmask(0, NULL, &set);
2121 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002122 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002123 err |= __put_user(target_set.sig[0],
2124 (abi_ulong *)&ucp->uc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002125 } else {
2126 abi_ulong *src, *dst;
2127 src = target_set.sig;
2128 dst = ucp->uc_sigmask.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002129 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002130 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002131 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00002132 if (err)
2133 goto do_sigsegv;
2134 }
2135
bellard459a4012007-11-11 19:45:10 +00002136 /* XXX: tstate must be saved properly */
2137 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00002138 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2139 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2140 err |= __put_user(env->y, &((*grp)[MC_Y]));
2141 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2142 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2143 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2144 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2145 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2146 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2147 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2148 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2149 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2150 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2151 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2152 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2153 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2154 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2155 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002156
bellard459a4012007-11-11 19:45:10 +00002157 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2158 fp = i7 = 0;
2159 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2160 abi_ulong) != 0)
2161 goto do_sigsegv;
2162 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2163 abi_ulong) != 0)
2164 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002165 err |= __put_user(fp, &(mcp->mc_fp));
2166 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002167
bellard459a4012007-11-11 19:45:10 +00002168 {
2169 uint32_t *src, *dst;
2170 src = env->fpr;
2171 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2172 /* XXX: check that the CPU storage is the same as user context */
2173 for (i = 0; i < 64; i++, dst++, src++)
2174 err |= __put_user(*src, dst);
2175 }
bellard579a97f2007-11-11 14:26:47 +00002176 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2177 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2178 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002179
2180 if (err)
2181 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002182 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002183 return;
2184 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002185 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002186 force_sig(SIGSEGV);
2187}
2188#endif
thsd26bc212007-11-08 18:05:37 +00002189#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002190
2191# warning signal handling not implemented
2192
2193static void setup_frame(int sig, struct emulated_sigaction *ka,
2194 target_sigset_t *set, CPUState *env)
2195{
2196 fprintf(stderr, "setup_frame: not implemented\n");
2197}
2198
2199static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2200 target_siginfo_t *info,
2201 target_sigset_t *set, CPUState *env)
2202{
2203 fprintf(stderr, "setup_rt_frame: not implemented\n");
2204}
2205
2206long do_sigreturn(CPUState *env)
2207{
2208 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002209 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002210}
2211
2212long do_rt_sigreturn(CPUState *env)
2213{
2214 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002215 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002216}
2217
thsd26bc212007-11-08 18:05:37 +00002218#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002219
2220# warning signal handling not implemented
2221
2222static void setup_frame(int sig, struct emulated_sigaction *ka,
2223 target_sigset_t *set, CPUState *env)
2224{
2225 fprintf(stderr, "setup_frame: not implemented\n");
2226}
2227
2228static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2229 target_siginfo_t *info,
2230 target_sigset_t *set, CPUState *env)
2231{
2232 fprintf(stderr, "setup_rt_frame: not implemented\n");
2233}
2234
2235long do_sigreturn(CPUState *env)
2236{
2237 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002238 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002239}
2240
2241long do_rt_sigreturn(CPUState *env)
2242{
2243 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002244 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002245}
2246
thsd26bc212007-11-08 18:05:37 +00002247#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002248
2249struct target_sigcontext {
2250 uint32_t sc_regmask; /* Unused */
2251 uint32_t sc_status;
2252 uint64_t sc_pc;
2253 uint64_t sc_regs[32];
2254 uint64_t sc_fpregs[32];
2255 uint32_t sc_ownedfp; /* Unused */
2256 uint32_t sc_fpc_csr;
2257 uint32_t sc_fpc_eir; /* Unused */
2258 uint32_t sc_used_math;
2259 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2260 uint64_t sc_mdhi;
2261 uint64_t sc_mdlo;
2262 target_ulong sc_hi1; /* Was sc_cause */
2263 target_ulong sc_lo1; /* Was sc_badvaddr */
2264 target_ulong sc_hi2; /* Was sc_sigset[4] */
2265 target_ulong sc_lo2;
2266 target_ulong sc_hi3;
2267 target_ulong sc_lo3;
2268};
2269
2270struct sigframe {
2271 uint32_t sf_ass[4]; /* argument save space for o32 */
2272 uint32_t sf_code[2]; /* signal trampoline */
2273 struct target_sigcontext sf_sc;
2274 target_sigset_t sf_mask;
2275};
2276
2277/* Install trampoline to jump back from signal handler */
2278static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2279{
2280 int err;
2281
2282 /*
2283 * Set up the return code ...
2284 *
2285 * li v0, __NR__foo_sigreturn
2286 * syscall
2287 */
2288
2289 err = __put_user(0x24020000 + syscall, tramp + 0);
2290 err |= __put_user(0x0000000c , tramp + 1);
2291 /* flush_cache_sigtramp((unsigned long) tramp); */
2292 return err;
2293}
2294
2295static inline int
2296setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2297{
2298 int err = 0;
2299
thsead93602007-09-06 00:18:15 +00002300 err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002301
thsead93602007-09-06 00:18:15 +00002302#define save_gp_reg(i) do { \
thsd0dc7dc2008-02-12 21:01:26 +00002303 err |= __put_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002304 } while(0)
2305 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2306 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2307 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2308 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2309 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2310 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2311 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2312 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2313 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002314#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002315
thsd0dc7dc2008-02-12 21:01:26 +00002316 err |= __put_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2317 err |= __put_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002318
2319 /* Not used yet, but might be useful if we ever have DSP suppport */
2320#if 0
2321 if (cpu_has_dsp) {
2322 err |= __put_user(mfhi1(), &sc->sc_hi1);
2323 err |= __put_user(mflo1(), &sc->sc_lo1);
2324 err |= __put_user(mfhi2(), &sc->sc_hi2);
2325 err |= __put_user(mflo2(), &sc->sc_lo2);
2326 err |= __put_user(mfhi3(), &sc->sc_hi3);
2327 err |= __put_user(mflo3(), &sc->sc_lo3);
2328 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2329 }
2330 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002331#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002332 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2333 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2334 if (cpu_has_dsp) {
2335 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2336 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2337 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2338 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2339 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2340 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2341 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2342 }
ths388bb212007-05-13 13:58:00 +00002343#endif
2344#endif
bellard106ec872006-06-27 21:08:10 +00002345
ths388bb212007-05-13 13:58:00 +00002346#if 0
bellard106ec872006-06-27 21:08:10 +00002347 err |= __put_user(!!used_math(), &sc->sc_used_math);
2348
2349 if (!used_math())
2350 goto out;
2351
2352 /*
2353 * Save FPU state to signal context. Signal handler will "inherit"
2354 * current FPU state.
2355 */
2356 preempt_disable();
2357
2358 if (!is_fpu_owner()) {
2359 own_fpu();
2360 restore_fp(current);
2361 }
2362 err |= save_fp_context(sc);
2363
2364 preempt_enable();
2365 out:
2366#endif
2367 return err;
2368}
2369
2370static inline int
2371restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2372{
2373 int err = 0;
2374
2375 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2376
thsd0dc7dc2008-02-12 21:01:26 +00002377 err |= __get_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2378 err |= __get_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002379
thsead93602007-09-06 00:18:15 +00002380#define restore_gp_reg(i) do { \
thsd0dc7dc2008-02-12 21:01:26 +00002381 err |= __get_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002382 } while(0)
2383 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2384 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2385 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2386 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2387 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2388 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2389 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2390 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2391 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2392 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2393 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002394#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002395
2396#if 0
2397 if (cpu_has_dsp) {
2398 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2399 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2400 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2401 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2402 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2403 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2404 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2405 }
ths388bb212007-05-13 13:58:00 +00002406#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002407 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2408 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2409 if (cpu_has_dsp) {
2410 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2411 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2412 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2413 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2414 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2415 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2416 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2417 }
ths388bb212007-05-13 13:58:00 +00002418#endif
bellard106ec872006-06-27 21:08:10 +00002419
2420 err |= __get_user(used_math, &sc->sc_used_math);
2421 conditional_used_math(used_math);
2422
2423 preempt_disable();
2424
2425 if (used_math()) {
2426 /* restore fpu context if we have used it before */
2427 own_fpu();
2428 err |= restore_fp_context(sc);
2429 } else {
2430 /* signal handler may have used FPU. Give it up. */
2431 lose_fpu();
2432 }
2433
2434 preempt_enable();
2435#endif
2436 return err;
2437}
2438/*
2439 * Determine which stack to use..
2440 */
bellard579a97f2007-11-11 14:26:47 +00002441static inline abi_ulong
bellard106ec872006-06-27 21:08:10 +00002442get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
2443{
2444 unsigned long sp;
2445
2446 /* Default to using normal stack */
thsd0dc7dc2008-02-12 21:01:26 +00002447 sp = regs->gpr[regs->current_tc][29];
bellard106ec872006-06-27 21:08:10 +00002448
2449 /*
2450 * FPU emulator may have it's own trampoline active just
2451 * above the user stack, 16-bytes before the next lowest
2452 * 16 byte boundary. Try to avoid trashing it.
2453 */
2454 sp -= 32;
2455
bellard106ec872006-06-27 21:08:10 +00002456 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00002457 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2458 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2459 }
bellard106ec872006-06-27 21:08:10 +00002460
bellard579a97f2007-11-11 14:26:47 +00002461 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002462}
2463
bellard579a97f2007-11-11 14:26:47 +00002464/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
ths5fafdf22007-09-16 21:08:06 +00002465static void setup_frame(int sig, struct emulated_sigaction * ka,
bellard579a97f2007-11-11 14:26:47 +00002466 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002467{
2468 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002469 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002470 int i;
2471
bellard579a97f2007-11-11 14:26:47 +00002472 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2473 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002474 goto give_sigsegv;
2475
2476 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2477
2478 if(setup_sigcontext(regs, &frame->sf_sc))
2479 goto give_sigsegv;
2480
2481 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2482 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2483 goto give_sigsegv;
2484 }
2485
2486 /*
2487 * Arguments to signal handler:
2488 *
2489 * a0 = signal number
2490 * a1 = 0 (should be cause)
2491 * a2 = pointer to struct sigcontext
2492 *
2493 * $25 and PC point to the signal handler, $29 points to the
2494 * struct sigframe.
2495 */
thsd0dc7dc2008-02-12 21:01:26 +00002496 regs->gpr[regs->current_tc][ 4] = sig;
2497 regs->gpr[regs->current_tc][ 5] = 0;
2498 regs->gpr[regs->current_tc][ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2499 regs->gpr[regs->current_tc][29] = frame_addr;
2500 regs->gpr[regs->current_tc][31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002501 /* The original kernel code sets CP0_EPC to the handler
2502 * since it returns to userland using eret
2503 * we cannot do this here, and we must set PC directly */
thsd0dc7dc2008-02-12 21:01:26 +00002504 regs->PC[regs->current_tc] = regs->gpr[regs->current_tc][25] = ka->sa._sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002505 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002506 return;
2507
2508give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002509 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002510 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002511 return;
bellard106ec872006-06-27 21:08:10 +00002512}
2513
2514long do_sigreturn(CPUState *regs)
2515{
ths388bb212007-05-13 13:58:00 +00002516 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002517 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002518 sigset_t blocked;
2519 target_sigset_t target_set;
2520 int i;
bellard106ec872006-06-27 21:08:10 +00002521
2522#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002523 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002524#endif
thsd0dc7dc2008-02-12 21:01:26 +00002525 frame_addr = regs->gpr[regs->current_tc][29];
bellard579a97f2007-11-11 14:26:47 +00002526 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002527 goto badframe;
2528
ths388bb212007-05-13 13:58:00 +00002529 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002530 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2531 goto badframe;
ths388bb212007-05-13 13:58:00 +00002532 }
bellard106ec872006-06-27 21:08:10 +00002533
ths388bb212007-05-13 13:58:00 +00002534 target_to_host_sigset_internal(&blocked, &target_set);
2535 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002536
ths388bb212007-05-13 13:58:00 +00002537 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002538 goto badframe;
2539
2540#if 0
ths388bb212007-05-13 13:58:00 +00002541 /*
2542 * Don't let your children do this ...
2543 */
2544 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002545 "move\t$29, %0\n\t"
2546 "j\tsyscall_exit"
2547 :/* no outputs */
2548 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002549 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002550#endif
ths3b46e622007-09-17 08:09:54 +00002551
thsead93602007-09-06 00:18:15 +00002552 regs->PC[regs->current_tc] = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002553 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002554 * maybe a problem with nested signals ? */
2555 regs->CP0_EPC = 0;
2556 return 0;
2557
2558badframe:
ths388bb212007-05-13 13:58:00 +00002559 force_sig(TARGET_SIGSEGV/*, current*/);
2560 return 0;
bellard106ec872006-06-27 21:08:10 +00002561}
2562
ths5fafdf22007-09-16 21:08:06 +00002563static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard106ec872006-06-27 21:08:10 +00002564 target_siginfo_t *info,
2565 target_sigset_t *set, CPUState *env)
2566{
2567 fprintf(stderr, "setup_rt_frame: not implemented\n");
2568}
2569
2570long do_rt_sigreturn(CPUState *env)
2571{
2572 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002573 return -TARGET_ENOSYS;
bellard106ec872006-06-27 21:08:10 +00002574}
bellard6d5e2162004-09-30 22:04:13 +00002575
thsc3b5bc82007-12-02 06:31:25 +00002576#elif defined(TARGET_SH4)
2577
2578/*
2579 * code and data structures from linux kernel:
2580 * include/asm-sh/sigcontext.h
2581 * arch/sh/kernel/signal.c
2582 */
2583
2584struct target_sigcontext {
2585 target_ulong oldmask;
2586
2587 /* CPU registers */
2588 target_ulong sc_gregs[16];
2589 target_ulong sc_pc;
2590 target_ulong sc_pr;
2591 target_ulong sc_sr;
2592 target_ulong sc_gbr;
2593 target_ulong sc_mach;
2594 target_ulong sc_macl;
2595
2596 /* FPU registers */
2597 target_ulong sc_fpregs[16];
2598 target_ulong sc_xfpregs[16];
2599 unsigned int sc_fpscr;
2600 unsigned int sc_fpul;
2601 unsigned int sc_ownedfp;
2602};
2603
2604struct target_sigframe
2605{
2606 struct target_sigcontext sc;
2607 target_ulong extramask[TARGET_NSIG_WORDS-1];
2608 uint16_t retcode[3];
2609};
2610
2611
2612struct target_ucontext {
2613 target_ulong uc_flags;
2614 struct target_ucontext *uc_link;
2615 target_stack_t uc_stack;
2616 struct target_sigcontext uc_mcontext;
2617 target_sigset_t uc_sigmask; /* mask last for extensibility */
2618};
2619
2620struct target_rt_sigframe
2621{
2622 struct target_siginfo info;
2623 struct target_ucontext uc;
2624 uint16_t retcode[3];
2625};
2626
2627
2628#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2629#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2630
2631static abi_ulong get_sigframe(struct emulated_sigaction *ka,
2632 unsigned long sp, size_t frame_size)
2633{
2634 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2635 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2636 }
2637
2638 return (sp - frame_size) & -8ul;
2639}
2640
2641static int setup_sigcontext(struct target_sigcontext *sc,
2642 CPUState *regs, unsigned long mask)
2643{
2644 int err = 0;
2645
2646#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2647 COPY(gregs[0]); COPY(gregs[1]);
2648 COPY(gregs[2]); COPY(gregs[3]);
2649 COPY(gregs[4]); COPY(gregs[5]);
2650 COPY(gregs[6]); COPY(gregs[7]);
2651 COPY(gregs[8]); COPY(gregs[9]);
2652 COPY(gregs[10]); COPY(gregs[11]);
2653 COPY(gregs[12]); COPY(gregs[13]);
2654 COPY(gregs[14]); COPY(gregs[15]);
2655 COPY(gbr); COPY(mach);
2656 COPY(macl); COPY(pr);
2657 COPY(sr); COPY(pc);
2658#undef COPY
2659
2660 /* todo: save FPU registers here */
2661
2662 /* non-iBCS2 extensions.. */
2663 err |= __put_user(mask, &sc->oldmask);
2664
2665 return err;
2666}
2667
2668static int restore_sigcontext(struct CPUState *regs,
2669 struct target_sigcontext *sc)
2670{
2671 unsigned int err = 0;
2672
2673#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2674 COPY(gregs[1]);
2675 COPY(gregs[2]); COPY(gregs[3]);
2676 COPY(gregs[4]); COPY(gregs[5]);
2677 COPY(gregs[6]); COPY(gregs[7]);
2678 COPY(gregs[8]); COPY(gregs[9]);
2679 COPY(gregs[10]); COPY(gregs[11]);
2680 COPY(gregs[12]); COPY(gregs[13]);
2681 COPY(gregs[14]); COPY(gregs[15]);
2682 COPY(gbr); COPY(mach);
2683 COPY(macl); COPY(pr);
2684 COPY(sr); COPY(pc);
2685#undef COPY
2686
2687 /* todo: restore FPU registers here */
2688
2689 regs->tra = -1; /* disable syscall checks */
2690 return err;
2691}
2692
2693static void setup_frame(int sig, struct emulated_sigaction *ka,
2694 target_sigset_t *set, CPUState *regs)
2695{
2696 struct target_sigframe *frame;
2697 abi_ulong frame_addr;
2698 int i;
2699 int err = 0;
2700 int signal;
2701
2702 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2703 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2704 goto give_sigsegv;
2705
2706 signal = current_exec_domain_sig(sig);
2707
2708 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2709
2710 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2711 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2712 }
2713
2714 /* Set up to return from userspace. If provided, use a stub
2715 already in userspace. */
2716 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2717 regs->pr = (unsigned long) ka->sa.sa_restorer;
2718 } else {
2719 /* Generate return code (system call to sigreturn) */
2720 err |= __put_user(MOVW(2), &frame->retcode[0]);
2721 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2722 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2723 regs->pr = (unsigned long) frame->retcode;
2724 }
2725
2726 if (err)
2727 goto give_sigsegv;
2728
2729 /* Set up registers for signal handler */
2730 regs->gregs[15] = (unsigned long) frame;
2731 regs->gregs[4] = signal; /* Arg for signal handler */
2732 regs->gregs[5] = 0;
2733 regs->gregs[6] = (unsigned long) &frame->sc;
2734 regs->pc = (unsigned long) ka->sa._sa_handler;
2735
2736 unlock_user_struct(frame, frame_addr, 1);
2737 return;
2738
2739give_sigsegv:
2740 unlock_user_struct(frame, frame_addr, 1);
2741 force_sig(SIGSEGV);
2742}
2743
2744static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2745 target_siginfo_t *info,
2746 target_sigset_t *set, CPUState *regs)
2747{
2748 struct target_rt_sigframe *frame;
2749 abi_ulong frame_addr;
2750 int i;
2751 int err = 0;
2752 int signal;
2753
2754 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2755 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2756 goto give_sigsegv;
2757
2758 signal = current_exec_domain_sig(sig);
2759
2760 err |= copy_siginfo_to_user(&frame->info, info);
2761
2762 /* Create the ucontext. */
2763 err |= __put_user(0, &frame->uc.uc_flags);
2764 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
2765 err |= __put_user((void *)target_sigaltstack_used.ss_sp,
2766 &frame->uc.uc_stack.ss_sp);
2767 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2768 &frame->uc.uc_stack.ss_flags);
2769 err |= __put_user(target_sigaltstack_used.ss_size,
2770 &frame->uc.uc_stack.ss_size);
2771 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2772 regs, set->sig[0]);
2773 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2774 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2775 }
2776
2777 /* Set up to return from userspace. If provided, use a stub
2778 already in userspace. */
2779 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2780 regs->pr = (unsigned long) ka->sa.sa_restorer;
2781 } else {
2782 /* Generate return code (system call to sigreturn) */
2783 err |= __put_user(MOVW(2), &frame->retcode[0]);
2784 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2785 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2786 regs->pr = (unsigned long) frame->retcode;
2787 }
2788
2789 if (err)
2790 goto give_sigsegv;
2791
2792 /* Set up registers for signal handler */
2793 regs->gregs[15] = (unsigned long) frame;
2794 regs->gregs[4] = signal; /* Arg for signal handler */
2795 regs->gregs[5] = (unsigned long) &frame->info;
2796 regs->gregs[6] = (unsigned long) &frame->uc;
2797 regs->pc = (unsigned long) ka->sa._sa_handler;
2798
2799 unlock_user_struct(frame, frame_addr, 1);
2800 return;
2801
2802give_sigsegv:
2803 unlock_user_struct(frame, frame_addr, 1);
2804 force_sig(SIGSEGV);
2805}
2806
2807long do_sigreturn(CPUState *regs)
2808{
2809 struct target_sigframe *frame;
2810 abi_ulong frame_addr;
2811 sigset_t blocked;
2812 target_sigset_t target_set;
2813 int i;
2814 int err = 0;
2815
2816#if defined(DEBUG_SIGNAL)
2817 fprintf(stderr, "do_sigreturn\n");
2818#endif
2819 frame_addr = regs->gregs[15];
2820 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2821 goto badframe;
2822
2823 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2824 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2825 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2826 }
2827
2828 if (err)
2829 goto badframe;
2830
2831 target_to_host_sigset_internal(&blocked, &target_set);
2832 sigprocmask(SIG_SETMASK, &blocked, NULL);
2833
2834 if (restore_sigcontext(regs, &frame->sc))
2835 goto badframe;
2836
2837 unlock_user_struct(frame, frame_addr, 0);
2838 return regs->gregs[0];
2839
2840badframe:
2841 unlock_user_struct(frame, frame_addr, 0);
2842 force_sig(TARGET_SIGSEGV);
2843 return 0;
2844}
2845
2846long do_rt_sigreturn(CPUState *regs)
2847{
2848 struct target_rt_sigframe *frame;
2849 abi_ulong frame_addr;
2850 sigset_t blocked;
2851
2852#if defined(DEBUG_SIGNAL)
2853 fprintf(stderr, "do_rt_sigreturn\n");
2854#endif
2855 frame_addr = regs->gregs[15];
2856 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2857 goto badframe;
2858
2859 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
2860 sigprocmask(SIG_SETMASK, &blocked, NULL);
2861
2862 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
2863 goto badframe;
2864
2865 if (do_sigaltstack(frame_addr +
2866 offsetof(struct target_rt_sigframe, uc.uc_stack),
2867 0, get_sp_from_cpustate(regs)) == -EFAULT)
2868 goto badframe;
2869
2870 unlock_user_struct(frame, frame_addr, 0);
2871 return regs->gregs[0];
2872
2873badframe:
2874 unlock_user_struct(frame, frame_addr, 0);
2875 force_sig(TARGET_SIGSEGV);
2876 return 0;
2877}
edgar_iglb6d3abd2008-02-28 11:29:27 +00002878#elif defined(TARGET_CRIS)
2879
2880struct target_sigcontext {
2881 struct target_pt_regs regs; /* needs to be first */
2882 uint32_t oldmask;
2883 uint32_t usp; /* usp before stacking this gunk on it */
2884};
2885
2886/* Signal frames. */
2887struct target_signal_frame {
2888 struct target_sigcontext sc;
2889 uint32_t extramask[TARGET_NSIG_WORDS - 1];
2890 uint8_t retcode[8]; /* Trampoline code. */
2891};
2892
2893struct rt_signal_frame {
2894 struct siginfo *pinfo;
2895 void *puc;
2896 struct siginfo info;
2897 struct ucontext uc;
2898 uint8_t retcode[8]; /* Trampoline code. */
2899};
2900
2901static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
2902{
edgar_igl9664d922008-03-03 22:23:53 +00002903 __put_user(env->regs[0], &sc->regs.r0);
2904 __put_user(env->regs[1], &sc->regs.r1);
2905 __put_user(env->regs[2], &sc->regs.r2);
2906 __put_user(env->regs[3], &sc->regs.r3);
2907 __put_user(env->regs[4], &sc->regs.r4);
2908 __put_user(env->regs[5], &sc->regs.r5);
2909 __put_user(env->regs[6], &sc->regs.r6);
2910 __put_user(env->regs[7], &sc->regs.r7);
2911 __put_user(env->regs[8], &sc->regs.r8);
2912 __put_user(env->regs[9], &sc->regs.r9);
2913 __put_user(env->regs[10], &sc->regs.r10);
2914 __put_user(env->regs[11], &sc->regs.r11);
2915 __put_user(env->regs[12], &sc->regs.r12);
2916 __put_user(env->regs[13], &sc->regs.r13);
2917 __put_user(env->regs[14], &sc->usp);
2918 __put_user(env->regs[15], &sc->regs.acr);
2919 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
2920 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
2921 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002922}
edgar_igl9664d922008-03-03 22:23:53 +00002923
edgar_iglb6d3abd2008-02-28 11:29:27 +00002924static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
2925{
edgar_igl9664d922008-03-03 22:23:53 +00002926 __get_user(env->regs[0], &sc->regs.r0);
2927 __get_user(env->regs[1], &sc->regs.r1);
2928 __get_user(env->regs[2], &sc->regs.r2);
2929 __get_user(env->regs[3], &sc->regs.r3);
2930 __get_user(env->regs[4], &sc->regs.r4);
2931 __get_user(env->regs[5], &sc->regs.r5);
2932 __get_user(env->regs[6], &sc->regs.r6);
2933 __get_user(env->regs[7], &sc->regs.r7);
2934 __get_user(env->regs[8], &sc->regs.r8);
2935 __get_user(env->regs[9], &sc->regs.r9);
2936 __get_user(env->regs[10], &sc->regs.r10);
2937 __get_user(env->regs[11], &sc->regs.r11);
2938 __get_user(env->regs[12], &sc->regs.r12);
2939 __get_user(env->regs[13], &sc->regs.r13);
2940 __get_user(env->regs[14], &sc->usp);
2941 __get_user(env->regs[15], &sc->regs.acr);
2942 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
2943 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
2944 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002945}
2946
edgar_igl9664d922008-03-03 22:23:53 +00002947static abi_ulong get_sigframe(CPUState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00002948{
edgar_igl9664d922008-03-03 22:23:53 +00002949 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002950 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00002951 sp = (env->regs[R_SP] & ~3);
2952 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002953}
2954
2955static void setup_frame(int sig, struct emulated_sigaction *ka,
2956 target_sigset_t *set, CPUState *env)
2957{
2958 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00002959 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002960 int err = 0;
2961 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002962
edgar_igl9664d922008-03-03 22:23:53 +00002963 frame_addr = get_sigframe(env, sizeof *frame);
2964 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00002965 goto badframe;
2966
2967 /*
2968 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
2969 * use this trampoline anymore but it sets it up for GDB.
2970 * In QEMU, using the trampoline simplifies things a bit so we use it.
2971 *
2972 * This is movu.w __NR_sigreturn, r9; break 13;
2973 */
2974 err |= __put_user(0x9c5f, frame->retcode+0);
2975 err |= __put_user(TARGET_NR_sigreturn,
2976 frame->retcode+2);
2977 err |= __put_user(0xe93d, frame->retcode+4);
2978
2979 /* Save the mask. */
2980 err |= __put_user(set->sig[0], &frame->sc.oldmask);
2981 if (err)
2982 goto badframe;
2983
2984 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2985 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
2986 goto badframe;
2987 }
2988
2989 setup_sigcontext(&frame->sc, env);
2990
2991 /* Move the stack and setup the arguments for the handler. */
2992 env->regs[R_SP] = (uint32_t) frame;
2993 env->regs[10] = sig;
2994 env->pc = (unsigned long) ka->sa._sa_handler;
2995 /* Link SRP so the guest returns through the trampoline. */
2996 env->pregs[PR_SRP] = (uint32_t) &frame->retcode[0];
2997
edgar_igl9664d922008-03-03 22:23:53 +00002998 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002999 return;
3000 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003001 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003002 force_sig(TARGET_SIGSEGV);
3003}
3004
3005static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
3006 target_siginfo_t *info,
3007 target_sigset_t *set, CPUState *env)
3008{
3009 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3010}
3011
3012long do_sigreturn(CPUState *env)
3013{
3014 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003015 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003016 target_sigset_t target_set;
3017 sigset_t set;
3018 int i;
3019
edgar_igl9664d922008-03-03 22:23:53 +00003020 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003021 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003022 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003023 goto badframe;
3024
3025 /* Restore blocked signals */
3026 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3027 goto badframe;
3028 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3029 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3030 goto badframe;
3031 }
3032 target_to_host_sigset_internal(&set, &target_set);
3033 sigprocmask(SIG_SETMASK, &set, NULL);
3034
3035 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003036 /* Compensate for the syscall return path advancing brk. */
3037 env->pc -= 2;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003038
edgar_igl9664d922008-03-03 22:23:53 +00003039 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003040 return env->regs[10];
3041 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003042 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003043 force_sig(TARGET_SIGSEGV);
3044}
3045
3046long do_rt_sigreturn(CPUState *env)
3047{
3048 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3049 return -TARGET_ENOSYS;
3050}
thsc3b5bc82007-12-02 06:31:25 +00003051
bellardb346ff42003-06-15 20:05:50 +00003052#else
3053
3054static void setup_frame(int sig, struct emulated_sigaction *ka,
3055 target_sigset_t *set, CPUState *env)
3056{
3057 fprintf(stderr, "setup_frame: not implemented\n");
3058}
3059
ths5fafdf22007-09-16 21:08:06 +00003060static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00003061 target_siginfo_t *info,
3062 target_sigset_t *set, CPUState *env)
3063{
3064 fprintf(stderr, "setup_rt_frame: not implemented\n");
3065}
3066
3067long do_sigreturn(CPUState *env)
3068{
3069 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003070 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003071}
3072
3073long do_rt_sigreturn(CPUState *env)
3074{
3075 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003076 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003077}
3078
bellard66fb9762003-03-23 01:06:05 +00003079#endif
3080
3081void process_pending_signals(void *cpu_env)
3082{
3083 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00003084 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00003085 sigset_t set, old_set;
3086 target_sigset_t target_old_set;
bellard66fb9762003-03-23 01:06:05 +00003087 struct emulated_sigaction *k;
3088 struct sigqueue *q;
ths3b46e622007-09-17 08:09:54 +00003089
bellard31e31b82003-02-18 22:55:36 +00003090 if (!signal_pending)
3091 return;
3092
bellard66fb9762003-03-23 01:06:05 +00003093 k = sigact_table;
3094 for(sig = 1; sig <= TARGET_NSIG; sig++) {
3095 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00003096 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00003097 k++;
bellard31e31b82003-02-18 22:55:36 +00003098 }
3099 /* if no signal is pending, just return */
3100 signal_pending = 0;
3101 return;
bellard66fb9762003-03-23 01:06:05 +00003102
bellard31e31b82003-02-18 22:55:36 +00003103 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00003104#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00003105 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00003106#endif
3107 /* dequeue signal */
3108 q = k->first;
3109 k->first = q->next;
3110 if (!k->first)
3111 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00003112
bellard1fddef42005-04-17 19:16:13 +00003113 sig = gdb_handlesig (cpu_env, sig);
3114 if (!sig) {
3115 fprintf (stderr, "Lost signal\n");
3116 abort();
3117 }
bellard66fb9762003-03-23 01:06:05 +00003118
3119 handler = k->sa._sa_handler;
3120 if (handler == TARGET_SIG_DFL) {
3121 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +00003122 if (sig != TARGET_SIGCHLD &&
3123 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +00003124 sig != TARGET_SIGWINCH) {
3125 force_sig(sig);
3126 }
3127 } else if (handler == TARGET_SIG_IGN) {
3128 /* ignore sig */
3129 } else if (handler == TARGET_SIG_ERR) {
3130 force_sig(sig);
3131 } else {
bellard9de5e442003-03-23 16:49:39 +00003132 /* compute the blocked signals during the handler execution */
3133 target_to_host_sigset(&set, &k->sa.sa_mask);
3134 /* SA_NODEFER indicates that the current signal should not be
3135 blocked during the handler */
3136 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
3137 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00003138
bellard9de5e442003-03-23 16:49:39 +00003139 /* block signals in the handler using Linux */
3140 sigprocmask(SIG_BLOCK, &set, &old_set);
3141 /* save the previous blocked signal state to restore it at the
3142 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00003143 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00003144
bellardbc8a22c2003-03-30 21:02:40 +00003145 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00003146#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00003147 {
3148 CPUX86State *env = cpu_env;
3149 if (env->eflags & VM_MASK)
3150 save_v86_state(env);
3151 }
3152#endif
bellard9de5e442003-03-23 16:49:39 +00003153 /* prepare the stack frame of the virtual CPU */
bellard66fb9762003-03-23 01:06:05 +00003154 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
bellard9de5e442003-03-23 16:49:39 +00003155 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00003156 else
bellard9de5e442003-03-23 16:49:39 +00003157 setup_frame(sig, k, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00003158 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
3159 k->sa._sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00003160 }
bellard66fb9762003-03-23 01:06:05 +00003161 if (q != &k->info)
3162 free_sigqueue(q);
bellard31e31b82003-02-18 22:55:36 +00003163}