blob: f054907acc1c0421afbeb30146c5271e717ccf0c [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
pbrookf5545b52008-05-30 22:37:07 +0000128static inline void target_sigemptyset(target_sigset_t *set)
129{
130 memset(set, 0, sizeof(*set));
131}
132
133static inline void target_sigaddset(target_sigset_t *set, int signum)
134{
135 signum--;
136 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
137 set->sig[signum / TARGET_NSIG_BPW] |= mask;
138}
139
140static inline int target_sigismember(const target_sigset_t *set, int signum)
141{
142 signum--;
143 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
144 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
145}
146
ths5fafdf22007-09-16 21:08:06 +0000147static void host_to_target_sigset_internal(target_sigset_t *d,
bellard92319442004-06-19 16:58:13 +0000148 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000149{
150 int i;
pbrookf5545b52008-05-30 22:37:07 +0000151 target_sigemptyset(d);
152 for (i = 1; i <= TARGET_NSIG; i++) {
153 if (sigismember(s, i)) {
154 target_sigaddset(d, host_to_target_signal(i));
155 }
bellard9e5f5282003-07-13 17:33:54 +0000156 }
bellard66fb9762003-03-23 01:06:05 +0000157}
158
bellard92319442004-06-19 16:58:13 +0000159void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
160{
161 target_sigset_t d1;
162 int i;
163
164 host_to_target_sigset_internal(&d1, s);
165 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000166 d->sig[i] = tswapl(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000167}
168
169void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000170{
171 int i;
pbrookf5545b52008-05-30 22:37:07 +0000172 sigemptyset(d);
173 for (i = 1; i <= TARGET_NSIG; i++) {
174 if (target_sigismember(s, i)) {
175 sigaddset(d, target_to_host_signal(i));
176 }
177 }
bellard66fb9762003-03-23 01:06:05 +0000178}
179
bellard92319442004-06-19 16:58:13 +0000180void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
181{
182 target_sigset_t s1;
183 int i;
184
185 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000186 s1.sig[i] = tswapl(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000187 target_to_host_sigset_internal(d, &s1);
188}
ths3b46e622007-09-17 08:09:54 +0000189
blueswir1992f48a2007-10-14 16:27:31 +0000190void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000191 const sigset_t *sigset)
192{
bellard9e5f5282003-07-13 17:33:54 +0000193 target_sigset_t d;
194 host_to_target_sigset(&d, sigset);
195 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000196}
197
ths5fafdf22007-09-16 21:08:06 +0000198void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000199 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000200{
bellard9e5f5282003-07-13 17:33:54 +0000201 target_sigset_t d;
202 int i;
203
204 d.sig[0] = *old_sigset;
205 for(i = 1;i < TARGET_NSIG_WORDS; i++)
206 d.sig[i] = 0;
207 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000208}
209
bellard9de5e442003-03-23 16:49:39 +0000210/* siginfo conversion */
211
ths5fafdf22007-09-16 21:08:06 +0000212static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000213 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000214{
bellard9de5e442003-03-23 16:49:39 +0000215 int sig;
216 sig = host_to_target_signal(info->si_signo);
217 tinfo->si_signo = sig;
218 tinfo->si_errno = 0;
219 tinfo->si_code = 0;
ths5fafdf22007-09-16 21:08:06 +0000220 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000221 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000222 /* should never come here, but who knows. The information for
223 the target is irrelevant */
224 tinfo->_sifields._sigfault._addr = 0;
ths7f7f7c82007-07-12 11:02:46 +0000225 } else if (sig == SIGIO) {
226 tinfo->_sifields._sigpoll._fd = info->si_fd;
bellard9de5e442003-03-23 16:49:39 +0000227 } else if (sig >= TARGET_SIGRTMIN) {
228 tinfo->_sifields._rt._pid = info->si_pid;
229 tinfo->_sifields._rt._uid = info->si_uid;
230 /* XXX: potential problem if 64 bit */
ths5fafdf22007-09-16 21:08:06 +0000231 tinfo->_sifields._rt._sigval.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000232 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000233 }
bellard66fb9762003-03-23 01:06:05 +0000234}
235
ths5fafdf22007-09-16 21:08:06 +0000236static void tswap_siginfo(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000237 const target_siginfo_t *info)
238{
239 int sig;
240 sig = info->si_signo;
241 tinfo->si_signo = tswap32(sig);
242 tinfo->si_errno = tswap32(info->si_errno);
243 tinfo->si_code = tswap32(info->si_code);
ths5fafdf22007-09-16 21:08:06 +0000244 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000245 sig == SIGBUS || sig == SIGTRAP) {
ths5fafdf22007-09-16 21:08:06 +0000246 tinfo->_sifields._sigfault._addr =
bellard9de5e442003-03-23 16:49:39 +0000247 tswapl(info->_sifields._sigfault._addr);
ths7f7f7c82007-07-12 11:02:46 +0000248 } else if (sig == SIGIO) {
249 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
bellard9de5e442003-03-23 16:49:39 +0000250 } else if (sig >= TARGET_SIGRTMIN) {
251 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
252 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000253 tinfo->_sifields._rt._sigval.sival_ptr =
bellard9de5e442003-03-23 16:49:39 +0000254 tswapl(info->_sifields._rt._sigval.sival_ptr);
255 }
256}
257
258
259void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
260{
261 host_to_target_siginfo_noswap(tinfo, info);
262 tswap_siginfo(tinfo, tinfo);
263}
264
265/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000266/* XXX: find a solution for 64 bit (additional malloced data is needed) */
bellard9de5e442003-03-23 16:49:39 +0000267void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000268{
269 info->si_signo = tswap32(tinfo->si_signo);
270 info->si_errno = tswap32(tinfo->si_errno);
271 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000272 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
273 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000274 info->si_value.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000275 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000276}
277
bellard31e31b82003-02-18 22:55:36 +0000278void signal_init(void)
279{
280 struct sigaction act;
bellard9e5f5282003-07-13 17:33:54 +0000281 int i, j;
bellard31e31b82003-02-18 22:55:36 +0000282
bellard9e5f5282003-07-13 17:33:54 +0000283 /* generate signal conversion tables */
284 for(i = 1; i <= 64; i++) {
285 if (host_to_target_signal_table[i] == 0)
286 host_to_target_signal_table[i] = i;
287 }
288 for(i = 1; i <= 64; i++) {
289 j = host_to_target_signal_table[i];
290 target_to_host_signal_table[j] = i;
291 }
ths3b46e622007-09-17 08:09:54 +0000292
bellard9de5e442003-03-23 16:49:39 +0000293 /* set all host signal handlers. ALL signals are blocked during
294 the handlers to serialize them. */
295 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000296 act.sa_flags = SA_SIGINFO;
297 act.sa_sigaction = host_signal_handler;
298 for(i = 1; i < NSIG; i++) {
bellardc9087c22003-05-27 23:25:41 +0000299 sigaction(i, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000300 }
ths3b46e622007-09-17 08:09:54 +0000301
bellard31e31b82003-02-18 22:55:36 +0000302 memset(sigact_table, 0, sizeof(sigact_table));
bellard66fb9762003-03-23 01:06:05 +0000303
304 first_free = &sigqueue_table[0];
ths5fafdf22007-09-16 21:08:06 +0000305 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
bellard66fb9762003-03-23 01:06:05 +0000306 sigqueue_table[i].next = &sigqueue_table[i + 1];
307 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
bellard31e31b82003-02-18 22:55:36 +0000308}
309
bellard66fb9762003-03-23 01:06:05 +0000310/* signal queue handling */
311
312static inline struct sigqueue *alloc_sigqueue(void)
313{
314 struct sigqueue *q = first_free;
315 if (!q)
316 return NULL;
317 first_free = q->next;
318 return q;
319}
320
321static inline void free_sigqueue(struct sigqueue *q)
322{
323 q->next = first_free;
324 first_free = q;
325}
326
bellard9de5e442003-03-23 16:49:39 +0000327/* abort execution with signal */
328void __attribute((noreturn)) force_sig(int sig)
bellard66fb9762003-03-23 01:06:05 +0000329{
330 int host_sig;
bellard66fb9762003-03-23 01:06:05 +0000331 host_sig = target_to_host_signal(sig);
ths5fafdf22007-09-16 21:08:06 +0000332 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
bellard66fb9762003-03-23 01:06:05 +0000333 sig, strsignal(host_sig));
bellard9de5e442003-03-23 16:49:39 +0000334#if 1
bellard66fb9762003-03-23 01:06:05 +0000335 _exit(-host_sig);
bellard9de5e442003-03-23 16:49:39 +0000336#else
337 {
338 struct sigaction act;
339 sigemptyset(&act.sa_mask);
340 act.sa_flags = SA_SIGINFO;
341 act.sa_sigaction = SIG_DFL;
342 sigaction(SIGABRT, &act, NULL);
343 abort();
344 }
345#endif
bellard66fb9762003-03-23 01:06:05 +0000346}
347
bellard9de5e442003-03-23 16:49:39 +0000348/* queue a signal so that it will be send to the virtual CPU as soon
349 as possible */
350int queue_signal(int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000351{
bellard66fb9762003-03-23 01:06:05 +0000352 struct emulated_sigaction *k;
bellard9de5e442003-03-23 16:49:39 +0000353 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000354 abi_ulong handler;
bellard66fb9762003-03-23 01:06:05 +0000355
bellard9de5e442003-03-23 16:49:39 +0000356#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000357 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000358 sig);
bellard66fb9762003-03-23 01:06:05 +0000359#endif
bellard9de5e442003-03-23 16:49:39 +0000360 k = &sigact_table[sig - 1];
bellard66fb9762003-03-23 01:06:05 +0000361 handler = k->sa._sa_handler;
362 if (handler == TARGET_SIG_DFL) {
363 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000364 if (sig != TARGET_SIGCHLD &&
365 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +0000366 sig != TARGET_SIGWINCH) {
367 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000368 } else {
369 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000370 }
371 } else if (handler == TARGET_SIG_IGN) {
372 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000373 return 0;
bellard66fb9762003-03-23 01:06:05 +0000374 } else if (handler == TARGET_SIG_ERR) {
375 force_sig(sig);
376 } else {
bellard9de5e442003-03-23 16:49:39 +0000377 pq = &k->first;
378 if (sig < TARGET_SIGRTMIN) {
379 /* if non real time signal, we queue exactly one signal */
380 if (!k->pending)
381 q = &k->info;
382 else
383 return 0;
384 } else {
385 if (!k->pending) {
386 /* first signal */
387 q = &k->info;
388 } else {
389 q = alloc_sigqueue();
390 if (!q)
391 return -EAGAIN;
392 while (*pq != NULL)
393 pq = &(*pq)->next;
394 }
395 }
396 *pq = q;
397 q->info = *info;
398 q->next = NULL;
399 k->pending = 1;
400 /* signal that a new signal is pending */
401 signal_pending = 1;
402 return 1; /* indicates that the signal was queued */
403 }
404}
405
ths5fafdf22007-09-16 21:08:06 +0000406static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000407 void *puc)
408{
409 int sig;
410 target_siginfo_t tinfo;
411
412 /* the CPU emulator uses some host signals to detect exceptions,
413 we we forward to it some signals */
bellardec6338b2007-11-08 14:25:03 +0000414 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
bellardb346ff42003-06-15 20:05:50 +0000415 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000416 return;
417 }
418
419 /* get target signal number */
420 sig = host_to_target_signal(host_signum);
421 if (sig < 1 || sig > TARGET_NSIG)
422 return;
423#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000424 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000425#endif
426 host_to_target_siginfo_noswap(&tinfo, info);
427 if (queue_signal(sig, &tinfo) == 1) {
428 /* interrupt the virtual CPU as soon as possible */
bellard68a79312003-06-30 13:12:32 +0000429 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
bellard66fb9762003-03-23 01:06:05 +0000430 }
bellard31e31b82003-02-18 22:55:36 +0000431}
432
ths0da46a62007-10-20 20:23:07 +0000433/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000434/* compare linux/kernel/signal.c:do_sigaltstack() */
435abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000436{
437 int ret;
438 struct target_sigaltstack oss;
439
440 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000441 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000442 {
443 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
444 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
445 __put_user(sas_ss_flags(sp), &oss.ss_flags);
446 }
447
bellard579a97f2007-11-11 14:26:47 +0000448 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000449 {
bellard579a97f2007-11-11 14:26:47 +0000450 struct target_sigaltstack *uss;
451 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000452
ths0da46a62007-10-20 20:23:07 +0000453 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000454 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000455 || __get_user(ss.ss_sp, &uss->ss_sp)
456 || __get_user(ss.ss_size, &uss->ss_size)
457 || __get_user(ss.ss_flags, &uss->ss_flags))
458 goto out;
bellard579a97f2007-11-11 14:26:47 +0000459 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000460
ths0da46a62007-10-20 20:23:07 +0000461 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000462 if (on_sig_stack(sp))
463 goto out;
464
ths0da46a62007-10-20 20:23:07 +0000465 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000466 if (ss.ss_flags != TARGET_SS_DISABLE
467 && ss.ss_flags != TARGET_SS_ONSTACK
468 && ss.ss_flags != 0)
469 goto out;
470
471 if (ss.ss_flags == TARGET_SS_DISABLE) {
472 ss.ss_size = 0;
473 ss.ss_sp = 0;
474 } else {
ths0da46a62007-10-20 20:23:07 +0000475 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000476 if (ss.ss_size < MINSIGSTKSZ)
477 goto out;
478 }
479
480 target_sigaltstack_used.ss_sp = ss.ss_sp;
481 target_sigaltstack_used.ss_size = ss.ss_size;
482 }
483
bellard579a97f2007-11-11 14:26:47 +0000484 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000485 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000486 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000487 goto out;
thsa04e1342007-09-27 13:57:58 +0000488 }
489
490 ret = 0;
491out:
492 return ret;
493}
494
ths0da46a62007-10-20 20:23:07 +0000495/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000496int do_sigaction(int sig, const struct target_sigaction *act,
497 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000498{
bellard66fb9762003-03-23 01:06:05 +0000499 struct emulated_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000500 struct sigaction act1;
501 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000502 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000503
ths0aeaa8c2007-03-31 19:29:06 +0000504 if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000505 return -EINVAL;
506 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000507#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000508 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
bellard66fb9762003-03-23 01:06:05 +0000509 sig, (int)act, (int)oact);
510#endif
511 if (oact) {
512 oact->_sa_handler = tswapl(k->sa._sa_handler);
513 oact->sa_flags = tswapl(k->sa.sa_flags);
ths388bb212007-05-13 13:58:00 +0000514#if !defined(TARGET_MIPS)
515 oact->sa_restorer = tswapl(k->sa.sa_restorer);
516#endif
bellard66fb9762003-03-23 01:06:05 +0000517 oact->sa_mask = k->sa.sa_mask;
518 }
519 if (act) {
520 k->sa._sa_handler = tswapl(act->_sa_handler);
521 k->sa.sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000522#if !defined(TARGET_MIPS)
523 k->sa.sa_restorer = tswapl(act->sa_restorer);
524#endif
bellard66fb9762003-03-23 01:06:05 +0000525 k->sa.sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000526
527 /* we update the host linux signal state */
528 host_sig = target_to_host_signal(sig);
529 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
530 sigfillset(&act1.sa_mask);
531 act1.sa_flags = SA_SIGINFO;
532 if (k->sa.sa_flags & TARGET_SA_RESTART)
533 act1.sa_flags |= SA_RESTART;
534 /* NOTE: it is important to update the host kernel signal
535 ignore state to avoid getting unexpected interrupted
536 syscalls */
537 if (k->sa._sa_handler == TARGET_SIG_IGN) {
538 act1.sa_sigaction = (void *)SIG_IGN;
539 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
540 act1.sa_sigaction = (void *)SIG_DFL;
541 } else {
542 act1.sa_sigaction = host_signal_handler;
543 }
ths0da46a62007-10-20 20:23:07 +0000544 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000545 }
bellard66fb9762003-03-23 01:06:05 +0000546 }
ths0da46a62007-10-20 20:23:07 +0000547 return ret;
bellard66fb9762003-03-23 01:06:05 +0000548}
bellard31e31b82003-02-18 22:55:36 +0000549
bellard43fff232003-07-09 19:31:39 +0000550#ifndef offsetof
551#define offsetof(type, field) ((size_t) &((type *)0)->field)
552#endif
553
ths5fafdf22007-09-16 21:08:06 +0000554static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
bellard43fff232003-07-09 19:31:39 +0000555 const target_siginfo_t *info)
556{
557 tswap_siginfo(tinfo, info);
558 return 0;
559}
560
thsc3b5bc82007-12-02 06:31:25 +0000561static inline int current_exec_domain_sig(int sig)
562{
563 return /* current->exec_domain && current->exec_domain->signal_invmap
564 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
565}
566
bellard459a4012007-11-11 19:45:10 +0000567#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000568
569/* from the Linux kernel */
570
571struct target_fpreg {
572 uint16_t significand[4];
573 uint16_t exponent;
574};
575
576struct target_fpxreg {
577 uint16_t significand[4];
578 uint16_t exponent;
579 uint16_t padding[3];
580};
581
582struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000583 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000584};
585
586struct target_fpstate {
587 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000588 abi_ulong cw;
589 abi_ulong sw;
590 abi_ulong tag;
591 abi_ulong ipoff;
592 abi_ulong cssel;
593 abi_ulong dataoff;
594 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000595 struct target_fpreg _st[8];
596 uint16_t status;
597 uint16_t magic; /* 0xffff = regular FPU data only */
598
599 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000600 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
601 abi_ulong mxcsr;
602 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000603 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
604 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000605 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000606};
607
608#define X86_FXSR_MAGIC 0x0000
609
610struct target_sigcontext {
611 uint16_t gs, __gsh;
612 uint16_t fs, __fsh;
613 uint16_t es, __esh;
614 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000615 abi_ulong edi;
616 abi_ulong esi;
617 abi_ulong ebp;
618 abi_ulong esp;
619 abi_ulong ebx;
620 abi_ulong edx;
621 abi_ulong ecx;
622 abi_ulong eax;
623 abi_ulong trapno;
624 abi_ulong err;
625 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000626 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000627 abi_ulong eflags;
628 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000629 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000630 abi_ulong fpstate; /* pointer */
631 abi_ulong oldmask;
632 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000633};
634
bellard66fb9762003-03-23 01:06:05 +0000635struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000636 abi_ulong tuc_flags;
637 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +0000638 target_stack_t tuc_stack;
639 struct target_sigcontext tuc_mcontext;
640 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000641};
642
643struct sigframe
644{
blueswir1992f48a2007-10-14 16:27:31 +0000645 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000646 int sig;
647 struct target_sigcontext sc;
648 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000649 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000650 char retcode[8];
651};
652
653struct rt_sigframe
654{
blueswir1992f48a2007-10-14 16:27:31 +0000655 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000656 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000657 abi_ulong pinfo;
658 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000659 struct target_siginfo info;
660 struct target_ucontext uc;
661 struct target_fpstate fpstate;
662 char retcode[8];
663};
664
665/*
666 * Set up a signal frame.
667 */
668
bellard66fb9762003-03-23 01:06:05 +0000669/* XXX: save x87 state */
670static int
671setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
bellard28be6232007-11-11 22:23:38 +0000672 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000673{
674 int err = 0;
bellard775b58d2007-11-11 16:22:17 +0000675 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000676
bellard579a97f2007-11-11 14:26:47 +0000677 /* already locked in setup_frame() */
bellarda52c7572003-06-21 13:14:12 +0000678 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
679 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
680 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
681 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000682 err |= __put_user(env->regs[R_EDI], &sc->edi);
683 err |= __put_user(env->regs[R_ESI], &sc->esi);
684 err |= __put_user(env->regs[R_EBP], &sc->ebp);
685 err |= __put_user(env->regs[R_ESP], &sc->esp);
686 err |= __put_user(env->regs[R_EBX], &sc->ebx);
687 err |= __put_user(env->regs[R_EDX], &sc->edx);
688 err |= __put_user(env->regs[R_ECX], &sc->ecx);
689 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000690 err |= __put_user(env->exception_index, &sc->trapno);
691 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000692 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000693 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000694 err |= __put_user(env->eflags, &sc->eflags);
695 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000696 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000697
bellard28be6232007-11-11 22:23:38 +0000698 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000699 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000700 magic = 0xffff;
701 err |= __put_user(magic, &fpstate->magic);
bellard28be6232007-11-11 22:23:38 +0000702 err |= __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000703
bellard66fb9762003-03-23 01:06:05 +0000704 /* non-iBCS2 extensions.. */
705 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000706 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000707 return err;
708}
709
710/*
711 * Determine which stack to use..
712 */
713
bellard579a97f2007-11-11 14:26:47 +0000714static inline abi_ulong
bellard66fb9762003-03-23 01:06:05 +0000715get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
716{
717 unsigned long esp;
718
719 /* Default to using normal stack */
720 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000721 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +0000722 if (ka->sa.sa_flags & TARGET_SA_ONSTACK) {
723 if (sas_ss_flags(esp) == 0)
724 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
725 }
bellard66fb9762003-03-23 01:06:05 +0000726
727 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000728 else
bellarda52c7572003-06-21 13:14:12 +0000729 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
730 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
731 ka->sa.sa_restorer) {
732 esp = (unsigned long) ka->sa.sa_restorer;
733 }
bellard579a97f2007-11-11 14:26:47 +0000734 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000735}
736
bellard579a97f2007-11-11 14:26:47 +0000737/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
bellard66fb9762003-03-23 01:06:05 +0000738static void setup_frame(int sig, struct emulated_sigaction *ka,
739 target_sigset_t *set, CPUX86State *env)
740{
bellard579a97f2007-11-11 14:26:47 +0000741 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000742 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000743 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000744
bellard579a97f2007-11-11 14:26:47 +0000745 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000746
bellard579a97f2007-11-11 14:26:47 +0000747 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000748 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000749
thsc3b5bc82007-12-02 06:31:25 +0000750 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000751 &frame->sig);
752 if (err)
753 goto give_sigsegv;
754
bellard28be6232007-11-11 22:23:38 +0000755 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
756 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000757 if (err)
758 goto give_sigsegv;
759
bellard92319442004-06-19 16:58:13 +0000760 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
761 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
762 goto give_sigsegv;
763 }
bellard66fb9762003-03-23 01:06:05 +0000764
765 /* Set up to return from userspace. If provided, use a stub
766 already in userspace. */
767 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
768 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
769 } else {
bellard775b58d2007-11-11 16:22:17 +0000770 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000771 abi_ulong retcode_addr;
772 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
773 err |= __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000774 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000775 val16 = 0xb858;
776 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000777 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000778 val16 = 0x80cd;
779 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000780 }
781
782 if (err)
783 goto give_sigsegv;
784
785 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000786 env->regs[R_ESP] = frame_addr;
787 env->eip = ka->sa._sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000788
789 cpu_x86_load_seg(env, R_DS, __USER_DS);
790 cpu_x86_load_seg(env, R_ES, __USER_DS);
791 cpu_x86_load_seg(env, R_SS, __USER_DS);
792 cpu_x86_load_seg(env, R_CS, __USER_CS);
793 env->eflags &= ~TF_MASK;
794
bellard579a97f2007-11-11 14:26:47 +0000795 unlock_user_struct(frame, frame_addr, 1);
796
bellard66fb9762003-03-23 01:06:05 +0000797 return;
798
799give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000800 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000801 if (sig == TARGET_SIGSEGV)
802 ka->sa._sa_handler = TARGET_SIG_DFL;
803 force_sig(TARGET_SIGSEGV /* , current */);
804}
805
bellard579a97f2007-11-11 14:26:47 +0000806/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
ths5fafdf22007-09-16 21:08:06 +0000807static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard9de5e442003-03-23 16:49:39 +0000808 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000809 target_sigset_t *set, CPUX86State *env)
810{
bellard28be6232007-11-11 22:23:38 +0000811 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000812 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000813 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000814
bellard579a97f2007-11-11 14:26:47 +0000815 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000816
bellard579a97f2007-11-11 14:26:47 +0000817 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000818 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000819
thsc3b5bc82007-12-02 06:31:25 +0000820 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000821 &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000822 addr = frame_addr + offsetof(struct rt_sigframe, info);
823 err |= __put_user(addr, &frame->pinfo);
824 addr = frame_addr + offsetof(struct rt_sigframe, uc);
825 err |= __put_user(addr, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000826 err |= copy_siginfo_to_user(&frame->info, info);
827 if (err)
828 goto give_sigsegv;
829
830 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000831 err |= __put_user(0, &frame->uc.tuc_flags);
832 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000833 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000834 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000835 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000836 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000837 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000838 &frame->uc.tuc_stack.ss_size);
839 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000840 env, set->sig[0],
841 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +0000842 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000843 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000844 goto give_sigsegv;
845 }
bellard66fb9762003-03-23 01:06:05 +0000846
847 /* Set up to return from userspace. If provided, use a stub
848 already in userspace. */
849 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
850 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
851 } else {
bellard775b58d2007-11-11 16:22:17 +0000852 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000853 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
854 err |= __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000855 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000856 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000857 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000858 val16 = 0x80cd;
859 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000860 }
861
862 if (err)
863 goto give_sigsegv;
864
865 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000866 env->regs[R_ESP] = frame_addr;
867 env->eip = ka->sa._sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000868
869 cpu_x86_load_seg(env, R_DS, __USER_DS);
870 cpu_x86_load_seg(env, R_ES, __USER_DS);
871 cpu_x86_load_seg(env, R_SS, __USER_DS);
872 cpu_x86_load_seg(env, R_CS, __USER_CS);
873 env->eflags &= ~TF_MASK;
874
bellard579a97f2007-11-11 14:26:47 +0000875 unlock_user_struct(frame, frame_addr, 1);
876
bellard66fb9762003-03-23 01:06:05 +0000877 return;
878
879give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000880 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000881 if (sig == TARGET_SIGSEGV)
882 ka->sa._sa_handler = TARGET_SIG_DFL;
883 force_sig(TARGET_SIGSEGV /* , current */);
884}
885
886static int
887restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
888{
889 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +0000890 abi_ulong fpstate_addr;
891 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +0000892
bellard28be6232007-11-11 22:23:38 +0000893 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
894 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
895 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
896 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +0000897
bellard28be6232007-11-11 22:23:38 +0000898 env->regs[R_EDI] = tswapl(sc->edi);
899 env->regs[R_ESI] = tswapl(sc->esi);
900 env->regs[R_EBP] = tswapl(sc->ebp);
901 env->regs[R_ESP] = tswapl(sc->esp);
902 env->regs[R_EBX] = tswapl(sc->ebx);
903 env->regs[R_EDX] = tswapl(sc->edx);
904 env->regs[R_ECX] = tswapl(sc->ecx);
905 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +0000906
907 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
908 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000909
bellard28be6232007-11-11 22:23:38 +0000910 tmpflags = tswapl(sc->eflags);
911 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
912 // regs->orig_eax = -1; /* disable syscall checks */
913
914 fpstate_addr = tswapl(sc->fpstate);
915 if (fpstate_addr != 0) {
916 if (!access_ok(VERIFY_READ, fpstate_addr,
917 sizeof(struct target_fpstate)))
918 goto badframe;
919 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000920 }
921
bellard28be6232007-11-11 22:23:38 +0000922 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +0000923 return err;
bellard66fb9762003-03-23 01:06:05 +0000924badframe:
925 return 1;
bellard66fb9762003-03-23 01:06:05 +0000926}
927
928long do_sigreturn(CPUX86State *env)
929{
bellard579a97f2007-11-11 14:26:47 +0000930 struct sigframe *frame;
931 abi_ulong frame_addr = env->regs[R_ESP] - 8;
bellard66fb9762003-03-23 01:06:05 +0000932 target_sigset_t target_set;
933 sigset_t set;
934 int eax, i;
935
bellard447db212003-05-10 15:10:36 +0000936#if defined(DEBUG_SIGNAL)
937 fprintf(stderr, "do_sigreturn\n");
938#endif
bellard579a97f2007-11-11 14:26:47 +0000939 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
940 goto badframe;
bellard66fb9762003-03-23 01:06:05 +0000941 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +0000942 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
943 goto badframe;
944 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
945 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
946 goto badframe;
947 }
bellard66fb9762003-03-23 01:06:05 +0000948
bellard92319442004-06-19 16:58:13 +0000949 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +0000950 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +0000951
bellard66fb9762003-03-23 01:06:05 +0000952 /* restore registers */
953 if (restore_sigcontext(env, &frame->sc, &eax))
954 goto badframe;
bellard579a97f2007-11-11 14:26:47 +0000955 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000956 return eax;
957
958badframe:
bellard579a97f2007-11-11 14:26:47 +0000959 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000960 force_sig(TARGET_SIGSEGV);
961 return 0;
962}
963
964long do_rt_sigreturn(CPUX86State *env)
965{
bellard28be6232007-11-11 22:23:38 +0000966 abi_ulong frame_addr;
967 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +0000968 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +0000969 int eax;
970
bellard28be6232007-11-11 22:23:38 +0000971 frame_addr = env->regs[R_ESP] - 4;
972 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
973 goto badframe;
bellardb8076a72005-04-07 22:20:31 +0000974 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +0000975 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +0000976
bellardb8076a72005-04-07 22:20:31 +0000977 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +0000978 goto badframe;
979
bellard28be6232007-11-11 22:23:38 +0000980 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
981 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +0000982 goto badframe;
thsa04e1342007-09-27 13:57:58 +0000983
bellard28be6232007-11-11 22:23:38 +0000984 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000985 return eax;
986
987badframe:
bellard28be6232007-11-11 22:23:38 +0000988 unlock_user_struct(frame, frame_addr, 0);
989 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +0000990 return 0;
991}
992
bellard43fff232003-07-09 19:31:39 +0000993#elif defined(TARGET_ARM)
994
995struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +0000996 abi_ulong trap_no;
997 abi_ulong error_code;
998 abi_ulong oldmask;
999 abi_ulong arm_r0;
1000 abi_ulong arm_r1;
1001 abi_ulong arm_r2;
1002 abi_ulong arm_r3;
1003 abi_ulong arm_r4;
1004 abi_ulong arm_r5;
1005 abi_ulong arm_r6;
1006 abi_ulong arm_r7;
1007 abi_ulong arm_r8;
1008 abi_ulong arm_r9;
1009 abi_ulong arm_r10;
1010 abi_ulong arm_fp;
1011 abi_ulong arm_ip;
1012 abi_ulong arm_sp;
1013 abi_ulong arm_lr;
1014 abi_ulong arm_pc;
1015 abi_ulong arm_cpsr;
1016 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001017};
1018
pbrooka745ec62008-05-06 15:36:17 +00001019struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001020 abi_ulong tuc_flags;
1021 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +00001022 target_stack_t tuc_stack;
1023 struct target_sigcontext tuc_mcontext;
1024 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001025};
1026
pbrooka745ec62008-05-06 15:36:17 +00001027struct target_ucontext_v2 {
1028 abi_ulong tuc_flags;
1029 abi_ulong tuc_link;
1030 target_stack_t tuc_stack;
1031 struct target_sigcontext tuc_mcontext;
1032 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1033 char __unused[128 - sizeof(sigset_t)];
1034 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1035};
1036
pbrooka8c33202008-05-07 23:22:46 +00001037struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001038{
1039 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001040 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1041 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001042};
1043
pbrooka8c33202008-05-07 23:22:46 +00001044struct sigframe_v2
1045{
1046 struct target_ucontext_v2 uc;
1047 abi_ulong retcode;
1048};
1049
pbrooka745ec62008-05-06 15:36:17 +00001050struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001051{
bellardf8b0aa22007-11-11 23:03:42 +00001052 abi_ulong pinfo;
1053 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001054 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001055 struct target_ucontext_v1 uc;
1056 abi_ulong retcode;
1057};
1058
1059struct rt_sigframe_v2
1060{
1061 struct target_siginfo info;
1062 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001063 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001064};
1065
1066#define TARGET_CONFIG_CPU_32 1
1067
1068/*
1069 * For ARM syscalls, we encode the syscall number into the instruction.
1070 */
1071#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1072#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1073
1074/*
1075 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1076 * need two 16-bit instructions.
1077 */
1078#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1079#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1080
blueswir1992f48a2007-10-14 16:27:31 +00001081static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001082 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1083 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1084};
1085
1086
bellard43fff232003-07-09 19:31:39 +00001087#define __get_user_error(x,p,e) __get_user(x, p)
1088
1089static inline int valid_user_regs(CPUState *regs)
1090{
1091 return 1;
1092}
1093
pbrooka8c33202008-05-07 23:22:46 +00001094static void
bellard43fff232003-07-09 19:31:39 +00001095setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
bellardf8b0aa22007-11-11 23:03:42 +00001096 CPUState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001097{
pbrooka8c33202008-05-07 23:22:46 +00001098 __put_user(env->regs[0], &sc->arm_r0);
1099 __put_user(env->regs[1], &sc->arm_r1);
1100 __put_user(env->regs[2], &sc->arm_r2);
1101 __put_user(env->regs[3], &sc->arm_r3);
1102 __put_user(env->regs[4], &sc->arm_r4);
1103 __put_user(env->regs[5], &sc->arm_r5);
1104 __put_user(env->regs[6], &sc->arm_r6);
1105 __put_user(env->regs[7], &sc->arm_r7);
1106 __put_user(env->regs[8], &sc->arm_r8);
1107 __put_user(env->regs[9], &sc->arm_r9);
1108 __put_user(env->regs[10], &sc->arm_r10);
1109 __put_user(env->regs[11], &sc->arm_fp);
1110 __put_user(env->regs[12], &sc->arm_ip);
1111 __put_user(env->regs[13], &sc->arm_sp);
1112 __put_user(env->regs[14], &sc->arm_lr);
1113 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001114#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001115 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001116#endif
1117
pbrooka8c33202008-05-07 23:22:46 +00001118 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1119 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1120 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1121 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001122}
1123
bellard579a97f2007-11-11 14:26:47 +00001124static inline abi_ulong
bellard43fff232003-07-09 19:31:39 +00001125get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1126{
1127 unsigned long sp = regs->regs[13];
1128
bellard43fff232003-07-09 19:31:39 +00001129 /*
1130 * This is the X/Open sanctioned signal stack switching.
1131 */
thsa04e1342007-09-27 13:57:58 +00001132 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1133 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001134 /*
1135 * ATPCS B01 mandates 8-byte alignment
1136 */
bellard579a97f2007-11-11 14:26:47 +00001137 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001138}
1139
1140static int
1141setup_return(CPUState *env, struct emulated_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001142 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001143{
bellardf8b0aa22007-11-11 23:03:42 +00001144 abi_ulong handler = ka->sa._sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001145 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001146 int thumb = handler & 1;
bellard43fff232003-07-09 19:31:39 +00001147
1148 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
bellardf8b0aa22007-11-11 23:03:42 +00001149 retcode = ka->sa.sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001150 } else {
1151 unsigned int idx = thumb;
1152
1153 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1154 idx += 2;
1155
1156 if (__put_user(retcodes[idx], rc))
1157 return 1;
1158#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001159 flush_icache_range((abi_ulong)rc,
1160 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001161#endif
bellardf8b0aa22007-11-11 23:03:42 +00001162 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001163 }
1164
1165 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001166 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001167 env->regs[14] = retcode;
1168 env->regs[15] = handler & (thumb ? ~1 : ~3);
pbrook75b680e2008-03-21 16:07:30 +00001169 env->thumb = thumb;
bellard43fff232003-07-09 19:31:39 +00001170
bellardb5ff1b32005-11-26 10:38:39 +00001171#if 0
bellard43fff232003-07-09 19:31:39 +00001172#ifdef TARGET_CONFIG_CPU_32
1173 env->cpsr = cpsr;
1174#endif
bellardb5ff1b32005-11-26 10:38:39 +00001175#endif
bellard43fff232003-07-09 19:31:39 +00001176
1177 return 0;
1178}
1179
pbrooka8c33202008-05-07 23:22:46 +00001180static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1181 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001182{
pbrooka8c33202008-05-07 23:22:46 +00001183 struct target_sigaltstack stack;
1184 int i;
1185
1186 /* Clear all the bits of the ucontext we don't use. */
1187 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1188
1189 memset(&stack, 0, sizeof(stack));
1190 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1191 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1192 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1193 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1194
1195 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1196 /* FIXME: Save coprocessor signal frame. */
1197 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1198 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1199 }
1200}
1201
1202/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1203static void setup_frame_v1(int usig, struct emulated_sigaction *ka,
1204 target_sigset_t *set, CPUState *regs)
1205{
1206 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001207 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001208 int i;
bellard43fff232003-07-09 19:31:39 +00001209
bellard579a97f2007-11-11 14:26:47 +00001210 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1211 return;
1212
pbrooka8c33202008-05-07 23:22:46 +00001213 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001214
bellard92319442004-06-19 16:58:13 +00001215 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1216 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001217 goto end;
bellard43fff232003-07-09 19:31:39 +00001218 }
1219
pbrooka8c33202008-05-07 23:22:46 +00001220 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1221 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001222
1223end:
1224 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001225}
1226
1227static void setup_frame_v2(int usig, struct emulated_sigaction *ka,
1228 target_sigset_t *set, CPUState *regs)
1229{
1230 struct sigframe_v2 *frame;
1231 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1232
1233 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1234 return;
1235
1236 setup_sigframe_v2(&frame->uc, set, regs);
1237
1238 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1239 frame_addr + offsetof(struct sigframe_v2, retcode));
1240
1241 unlock_user_struct(frame, frame_addr, 1);
1242}
1243
1244static void setup_frame(int usig, struct emulated_sigaction *ka,
1245 target_sigset_t *set, CPUState *regs)
1246{
1247 if (get_osversion() >= 0x020612) {
1248 setup_frame_v2(usig, ka, set, regs);
1249 } else {
1250 setup_frame_v1(usig, ka, set, regs);
1251 }
bellard43fff232003-07-09 19:31:39 +00001252}
1253
bellard579a97f2007-11-11 14:26:47 +00001254/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrooka745ec62008-05-06 15:36:17 +00001255static void setup_rt_frame_v1(int usig, struct emulated_sigaction *ka,
1256 target_siginfo_t *info,
1257 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001258{
pbrooka745ec62008-05-06 15:36:17 +00001259 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001260 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001261 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001262 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001263 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001264
bellard579a97f2007-11-11 14:26:47 +00001265 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001266 return /* 1 */;
1267
pbrooka745ec62008-05-06 15:36:17 +00001268 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001269 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001270 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001271 __put_user(uc_addr, &frame->puc);
1272 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001273
1274 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001275 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001276
thsa04e1342007-09-27 13:57:58 +00001277 memset(&stack, 0, sizeof(stack));
1278 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1279 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1280 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001281 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001282
pbrooka8c33202008-05-07 23:22:46 +00001283 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001284 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001285 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001286 goto end;
bellard92319442004-06-19 16:58:13 +00001287 }
bellard43fff232003-07-09 19:31:39 +00001288
pbrooka8c33202008-05-07 23:22:46 +00001289 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1290 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001291
pbrooka8c33202008-05-07 23:22:46 +00001292 env->regs[1] = info_addr;
1293 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001294
1295end:
1296 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001297}
1298
1299static void setup_rt_frame_v2(int usig, struct emulated_sigaction *ka,
1300 target_siginfo_t *info,
1301 target_sigset_t *set, CPUState *env)
1302{
1303 struct rt_sigframe_v2 *frame;
1304 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001305 abi_ulong info_addr, uc_addr;
1306
1307 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1308 return /* 1 */;
1309
1310 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1311 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001312 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001313
pbrooka8c33202008-05-07 23:22:46 +00001314 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001315
pbrooka8c33202008-05-07 23:22:46 +00001316 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1317 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001318
pbrooka8c33202008-05-07 23:22:46 +00001319 env->regs[1] = info_addr;
1320 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001321
bellard579a97f2007-11-11 14:26:47 +00001322 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001323}
1324
pbrooka745ec62008-05-06 15:36:17 +00001325static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1326 target_siginfo_t *info,
1327 target_sigset_t *set, CPUState *env)
1328{
1329 if (get_osversion() >= 0x020612) {
1330 setup_rt_frame_v2(usig, ka, info, set, env);
1331 } else {
1332 setup_rt_frame_v1(usig, ka, info, set, env);
1333 }
1334}
1335
bellard43fff232003-07-09 19:31:39 +00001336static int
1337restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1338{
1339 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001340 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001341
1342 __get_user_error(env->regs[0], &sc->arm_r0, err);
1343 __get_user_error(env->regs[1], &sc->arm_r1, err);
1344 __get_user_error(env->regs[2], &sc->arm_r2, err);
1345 __get_user_error(env->regs[3], &sc->arm_r3, err);
1346 __get_user_error(env->regs[4], &sc->arm_r4, err);
1347 __get_user_error(env->regs[5], &sc->arm_r5, err);
1348 __get_user_error(env->regs[6], &sc->arm_r6, err);
1349 __get_user_error(env->regs[7], &sc->arm_r7, err);
1350 __get_user_error(env->regs[8], &sc->arm_r8, err);
1351 __get_user_error(env->regs[9], &sc->arm_r9, err);
1352 __get_user_error(env->regs[10], &sc->arm_r10, err);
1353 __get_user_error(env->regs[11], &sc->arm_fp, err);
1354 __get_user_error(env->regs[12], &sc->arm_ip, err);
1355 __get_user_error(env->regs[13], &sc->arm_sp, err);
1356 __get_user_error(env->regs[14], &sc->arm_lr, err);
1357 __get_user_error(env->regs[15], &sc->arm_pc, err);
1358#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001359 __get_user_error(cpsr, &sc->arm_cpsr, err);
pbrook75b680e2008-03-21 16:07:30 +00001360 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001361#endif
1362
1363 err |= !valid_user_regs(env);
1364
1365 return err;
1366}
1367
pbrooka8c33202008-05-07 23:22:46 +00001368long do_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001369{
bellardf8b0aa22007-11-11 23:03:42 +00001370 abi_ulong frame_addr;
pbrooka8c33202008-05-07 23:22:46 +00001371 struct sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001372 target_sigset_t set;
1373 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001374 int i;
bellard43fff232003-07-09 19:31:39 +00001375
1376 /*
1377 * Since we stacked the signal on a 64-bit boundary,
1378 * then 'sp' should be word aligned here. If it's
1379 * not, then the user is trying to mess with us.
1380 */
1381 if (env->regs[13] & 7)
1382 goto badframe;
1383
bellardf8b0aa22007-11-11 23:03:42 +00001384 frame_addr = env->regs[13];
1385 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1386 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001387
bellard92319442004-06-19 16:58:13 +00001388 if (__get_user(set.sig[0], &frame->sc.oldmask))
1389 goto badframe;
1390 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1391 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1392 goto badframe;
1393 }
bellard43fff232003-07-09 19:31:39 +00001394
bellard92319442004-06-19 16:58:13 +00001395 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001396 sigprocmask(SIG_SETMASK, &host_set, NULL);
1397
1398 if (restore_sigcontext(env, &frame->sc))
1399 goto badframe;
1400
1401#if 0
1402 /* Send SIGTRAP if we're single-stepping */
1403 if (ptrace_cancel_bpt(current))
1404 send_sig(SIGTRAP, current, 1);
1405#endif
bellardf8b0aa22007-11-11 23:03:42 +00001406 unlock_user_struct(frame, frame_addr, 0);
1407 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001408
1409badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001410 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001411 force_sig(SIGSEGV /* , current */);
1412 return 0;
1413}
1414
pbrooka8c33202008-05-07 23:22:46 +00001415static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1416 struct target_ucontext_v2 *uc)
1417{
1418 sigset_t host_set;
1419
1420 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1421 sigprocmask(SIG_SETMASK, &host_set, NULL);
1422
1423 if (restore_sigcontext(env, &uc->tuc_mcontext))
1424 return 1;
1425
1426 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1427 return 1;
1428
1429#if 0
1430 /* Send SIGTRAP if we're single-stepping */
1431 if (ptrace_cancel_bpt(current))
1432 send_sig(SIGTRAP, current, 1);
1433#endif
1434
1435 return 0;
1436}
1437
1438long do_sigreturn_v2(CPUState *env)
1439{
1440 abi_ulong frame_addr;
1441 struct sigframe_v2 *frame;
1442
1443 /*
1444 * Since we stacked the signal on a 64-bit boundary,
1445 * then 'sp' should be word aligned here. If it's
1446 * not, then the user is trying to mess with us.
1447 */
1448 if (env->regs[13] & 7)
1449 goto badframe;
1450
1451 frame_addr = env->regs[13];
1452 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1453 goto badframe;
1454
1455 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1456 goto badframe;
1457
1458 unlock_user_struct(frame, frame_addr, 0);
1459 return env->regs[0];
1460
1461badframe:
1462 unlock_user_struct(frame, frame_addr, 0);
1463 force_sig(SIGSEGV /* , current */);
1464 return 0;
1465}
1466
1467long do_sigreturn(CPUState *env)
1468{
1469 if (get_osversion() >= 0x020612) {
1470 return do_sigreturn_v2(env);
1471 } else {
1472 return do_sigreturn_v1(env);
1473 }
1474}
1475
pbrooka745ec62008-05-06 15:36:17 +00001476long do_rt_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001477{
bellardf8b0aa22007-11-11 23:03:42 +00001478 abi_ulong frame_addr;
pbrooka745ec62008-05-06 15:36:17 +00001479 struct rt_sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001480 sigset_t host_set;
1481
1482 /*
1483 * Since we stacked the signal on a 64-bit boundary,
1484 * then 'sp' should be word aligned here. If it's
1485 * not, then the user is trying to mess with us.
1486 */
1487 if (env->regs[13] & 7)
1488 goto badframe;
1489
bellardf8b0aa22007-11-11 23:03:42 +00001490 frame_addr = env->regs[13];
1491 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1492 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001493
bellardb8076a72005-04-07 22:20:31 +00001494 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001495 sigprocmask(SIG_SETMASK, &host_set, NULL);
1496
bellardb8076a72005-04-07 22:20:31 +00001497 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001498 goto badframe;
1499
pbrooka745ec62008-05-06 15:36:17 +00001500 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 +00001501 goto badframe;
1502
bellard43fff232003-07-09 19:31:39 +00001503#if 0
1504 /* Send SIGTRAP if we're single-stepping */
1505 if (ptrace_cancel_bpt(current))
1506 send_sig(SIGTRAP, current, 1);
1507#endif
bellardf8b0aa22007-11-11 23:03:42 +00001508 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001509 return env->regs[0];
1510
1511badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001512 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001513 force_sig(SIGSEGV /* , current */);
1514 return 0;
1515}
1516
pbrooka745ec62008-05-06 15:36:17 +00001517long do_rt_sigreturn_v2(CPUState *env)
1518{
1519 abi_ulong frame_addr;
1520 struct rt_sigframe_v2 *frame;
pbrooka745ec62008-05-06 15:36:17 +00001521
1522 /*
1523 * Since we stacked the signal on a 64-bit boundary,
1524 * then 'sp' should be word aligned here. If it's
1525 * not, then the user is trying to mess with us.
1526 */
1527 if (env->regs[13] & 7)
1528 goto badframe;
1529
1530 frame_addr = env->regs[13];
1531 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1532 goto badframe;
1533
pbrooka8c33202008-05-07 23:22:46 +00001534 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1535 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00001536
pbrooka745ec62008-05-06 15:36:17 +00001537 unlock_user_struct(frame, frame_addr, 0);
1538 return env->regs[0];
1539
1540badframe:
1541 unlock_user_struct(frame, frame_addr, 0);
1542 force_sig(SIGSEGV /* , current */);
1543 return 0;
1544}
1545
1546long do_rt_sigreturn(CPUState *env)
1547{
1548 if (get_osversion() >= 0x020612) {
1549 return do_rt_sigreturn_v2(env);
1550 } else {
1551 return do_rt_sigreturn_v1(env);
1552 }
1553}
1554
bellard6d5e2162004-09-30 22:04:13 +00001555#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001556
bellard6d5e2162004-09-30 22:04:13 +00001557#define __SUNOS_MAXWIN 31
1558
1559/* This is what SunOS does, so shall I. */
1560struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001561 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001562
blueswir1992f48a2007-10-14 16:27:31 +00001563 abi_ulong sigc_mask; /* sigmask to restore */
1564 abi_ulong sigc_sp; /* stack pointer */
1565 abi_ulong sigc_pc; /* program counter */
1566 abi_ulong sigc_npc; /* next program counter */
1567 abi_ulong sigc_psr; /* for condition codes etc */
1568 abi_ulong sigc_g1; /* User uses these two registers */
1569 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001570
1571 /* Now comes information regarding the users window set
1572 * at the time of the signal.
1573 */
blueswir1992f48a2007-10-14 16:27:31 +00001574 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001575
1576 /* stack ptrs for each regwin buf */
1577 char *sigc_spbuf[__SUNOS_MAXWIN];
1578
1579 /* Windows to restore after signal */
1580 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001581 abi_ulong locals[8];
1582 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001583 } sigc_wbuf[__SUNOS_MAXWIN];
1584};
1585/* A Sparc stack frame */
1586struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001587 abi_ulong locals[8];
1588 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001589 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001590 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001591 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001592 abi_ulong xargs[6];
1593 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001594};
1595
1596typedef struct {
1597 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001598 abi_ulong psr;
1599 abi_ulong pc;
1600 abi_ulong npc;
1601 abi_ulong y;
1602 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001603 } si_regs;
1604 int si_mask;
1605} __siginfo_t;
1606
1607typedef struct {
1608 unsigned long si_float_regs [32];
1609 unsigned long si_fsr;
1610 unsigned long si_fpqdepth;
1611 struct {
1612 unsigned long *insn_addr;
1613 unsigned long insn;
1614 } si_fpqueue [16];
bellard74ccb342006-07-18 21:23:34 +00001615} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001616
1617
1618struct target_signal_frame {
1619 struct sparc_stackf ss;
1620 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00001621 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001622 abi_ulong insns[2] __attribute__ ((aligned (8)));
1623 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1624 abi_ulong extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001625 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001626};
1627struct target_rt_signal_frame {
1628 struct sparc_stackf ss;
1629 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001630 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001631 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00001632 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001633 unsigned int insns[2];
1634 stack_t stack;
1635 unsigned int extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001636 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001637};
1638
bellarde80cfcf2004-12-19 23:18:01 +00001639#define UREG_O0 16
1640#define UREG_O6 22
1641#define UREG_I0 0
1642#define UREG_I1 1
1643#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001644#define UREG_I3 3
1645#define UREG_I4 4
1646#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001647#define UREG_I6 6
1648#define UREG_I7 7
1649#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001650#define UREG_FP UREG_I6
1651#define UREG_SP UREG_O6
1652
bellard459a4012007-11-11 19:45:10 +00001653static inline abi_ulong get_sigframe(struct emulated_sigaction *sa,
1654 CPUState *env, unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001655{
bellard459a4012007-11-11 19:45:10 +00001656 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001657
1658 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001659
1660 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00001661 if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1662 if (!on_sig_stack(sp)
1663 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1664 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001665 }
bellard459a4012007-11-11 19:45:10 +00001666 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001667}
1668
1669static int
blueswir1992f48a2007-10-14 16:27:31 +00001670setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001671{
1672 int err = 0, i;
1673
bellard6d5e2162004-09-30 22:04:13 +00001674 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001675 err |= __put_user(env->pc, &si->si_regs.pc);
1676 err |= __put_user(env->npc, &si->si_regs.npc);
1677 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001678 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001679 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1680 }
bellarda315a142005-01-30 22:59:18 +00001681 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001682 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001683 }
bellard6d5e2162004-09-30 22:04:13 +00001684 err |= __put_user(mask, &si->si_mask);
1685 return err;
1686}
bellarde80cfcf2004-12-19 23:18:01 +00001687
bellard80a9d032005-01-03 23:31:27 +00001688#if 0
bellard6d5e2162004-09-30 22:04:13 +00001689static int
1690setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1691 CPUState *env, unsigned long mask)
1692{
1693 int err = 0;
1694
1695 err |= __put_user(mask, &sc->sigc_mask);
1696 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1697 err |= __put_user(env->pc, &sc->sigc_pc);
1698 err |= __put_user(env->npc, &sc->sigc_npc);
1699 err |= __put_user(env->psr, &sc->sigc_psr);
1700 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1701 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1702
1703 return err;
1704}
bellard80a9d032005-01-03 23:31:27 +00001705#endif
bellard6d5e2162004-09-30 22:04:13 +00001706#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1707
1708static void setup_frame(int sig, struct emulated_sigaction *ka,
1709 target_sigset_t *set, CPUState *env)
1710{
bellard459a4012007-11-11 19:45:10 +00001711 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001712 struct target_signal_frame *sf;
1713 int sigframe_size, err, i;
1714
1715 /* 1. Make sure everything is clean */
1716 //synchronize_user_stack();
1717
1718 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001719 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001720
bellard459a4012007-11-11 19:45:10 +00001721 sf = lock_user(VERIFY_WRITE, sf_addr,
1722 sizeof(struct target_signal_frame), 0);
1723 if (!sf)
1724 goto sigsegv;
1725
bellarde80cfcf2004-12-19 23:18:01 +00001726 //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 +00001727#if 0
1728 if (invalid_frame_pointer(sf, sigframe_size))
1729 goto sigill_and_return;
1730#endif
1731 /* 2. Save the current process state */
1732 err = setup___siginfo(&sf->info, env, set->sig[0]);
1733 err |= __put_user(0, &sf->extra_size);
1734
1735 //err |= save_fpu_state(regs, &sf->fpu_state);
1736 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1737
1738 err |= __put_user(set->sig[0], &sf->info.si_mask);
1739 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1740 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1741 }
1742
bellarda315a142005-01-30 22:59:18 +00001743 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001744 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001745 }
bellarda315a142005-01-30 22:59:18 +00001746 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001747 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001748 }
bellard6d5e2162004-09-30 22:04:13 +00001749 if (err)
1750 goto sigsegv;
1751
1752 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001753 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001754 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001755 env->regwptr[UREG_I1] = sf_addr +
1756 offsetof(struct target_signal_frame, info);
1757 env->regwptr[UREG_I2] = sf_addr +
1758 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001759
1760 /* 4. signal handler */
bellard459a4012007-11-11 19:45:10 +00001761 env->pc = ka->sa._sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00001762 env->npc = (env->pc + 4);
1763 /* 5. return to kernel instructions */
1764 if (ka->sa.sa_restorer)
bellard459a4012007-11-11 19:45:10 +00001765 env->regwptr[UREG_I7] = ka->sa.sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00001766 else {
bellard775b58d2007-11-11 16:22:17 +00001767 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00001768
1769 env->regwptr[UREG_I7] = sf_addr +
1770 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00001771
1772 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001773 val32 = 0x821020d8;
1774 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001775
1776 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001777 val32 = 0x91d02010;
1778 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001779 if (err)
1780 goto sigsegv;
1781
1782 /* Flush instruction space. */
1783 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001784 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001785 }
bellard459a4012007-11-11 19:45:10 +00001786 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001787 return;
bellard459a4012007-11-11 19:45:10 +00001788#if 0
1789sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001790 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00001791#endif
bellard6d5e2162004-09-30 22:04:13 +00001792sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001793 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00001794 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001795 force_sig(TARGET_SIGSEGV);
1796}
1797static inline int
bellard74ccb342006-07-18 21:23:34 +00001798restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001799{
1800 int err;
1801#if 0
1802#ifdef CONFIG_SMP
1803 if (current->flags & PF_USEDFPU)
1804 regs->psr &= ~PSR_EF;
1805#else
1806 if (current == last_task_used_math) {
1807 last_task_used_math = 0;
1808 regs->psr &= ~PSR_EF;
1809 }
1810#endif
1811 current->used_math = 1;
1812 current->flags &= ~PF_USEDFPU;
1813#endif
1814#if 0
1815 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1816 return -EFAULT;
1817#endif
1818
bellardfafffae2006-10-28 12:09:16 +00001819#if 0
1820 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001821 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1822 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001823#endif
bellard6d5e2162004-09-30 22:04:13 +00001824 err |= __get_user(env->fsr, &fpu->si_fsr);
1825#if 0
1826 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1827 if (current->thread.fpqdepth != 0)
1828 err |= __copy_from_user(&current->thread.fpqueue[0],
1829 &fpu->si_fpqueue[0],
1830 ((sizeof(unsigned long) +
1831 (sizeof(unsigned long *)))*16));
1832#endif
1833 return err;
1834}
1835
1836
ths5fafdf22007-09-16 21:08:06 +00001837static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001838 target_siginfo_t *info,
1839 target_sigset_t *set, CPUState *env)
1840{
1841 fprintf(stderr, "setup_rt_frame: not implemented\n");
1842}
1843
1844long do_sigreturn(CPUState *env)
1845{
bellardf8b0aa22007-11-11 23:03:42 +00001846 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001847 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001848 uint32_t up_psr, pc, npc;
bellard6d5e2162004-09-30 22:04:13 +00001849 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001850 sigset_t host_set;
bellardf8b0aa22007-11-11 23:03:42 +00001851 abi_ulong fpu_save_addr;
bellarde80cfcf2004-12-19 23:18:01 +00001852 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001853
bellardf8b0aa22007-11-11 23:03:42 +00001854 sf_addr = env->regwptr[UREG_FP];
1855 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1856 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00001857#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001858 fprintf(stderr, "sigreturn\n");
1859 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 +00001860#endif
bellarde80cfcf2004-12-19 23:18:01 +00001861 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001862
1863 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00001864
bellardf8b0aa22007-11-11 23:03:42 +00001865 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00001866 goto segv_and_exit;
1867
1868 err = __get_user(pc, &sf->info.si_regs.pc);
1869 err |= __get_user(npc, &sf->info.si_regs.npc);
1870
bellard6d5e2162004-09-30 22:04:13 +00001871 if ((pc | npc) & 3)
1872 goto segv_and_exit;
1873
1874 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001875 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1876
bellard6d5e2162004-09-30 22:04:13 +00001877 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001878 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1879 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1880
1881 env->pc = pc;
1882 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001883 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001884 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001885 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1886 }
bellarda315a142005-01-30 22:59:18 +00001887 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001888 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1889 }
bellard6d5e2162004-09-30 22:04:13 +00001890
bellardf8b0aa22007-11-11 23:03:42 +00001891 err |= __get_user(fpu_save_addr, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001892
bellarde80cfcf2004-12-19 23:18:01 +00001893 //if (fpu_save)
1894 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001895
1896 /* This is pretty much atomic, no amount locking would prevent
1897 * the races which exist anyways.
1898 */
1899 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001900 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1901 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1902 }
1903
1904 target_to_host_sigset_internal(&host_set, &set);
1905 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001906
1907 if (err)
1908 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00001909 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001910 return env->regwptr[0];
1911
1912segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00001913 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001914 force_sig(TARGET_SIGSEGV);
1915}
1916
1917long do_rt_sigreturn(CPUState *env)
1918{
1919 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00001920 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00001921}
1922
bellard459a4012007-11-11 19:45:10 +00001923#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00001924#define MC_TSTATE 0
1925#define MC_PC 1
1926#define MC_NPC 2
1927#define MC_Y 3
1928#define MC_G1 4
1929#define MC_G2 5
1930#define MC_G3 6
1931#define MC_G4 7
1932#define MC_G5 8
1933#define MC_G6 9
1934#define MC_G7 10
1935#define MC_O0 11
1936#define MC_O1 12
1937#define MC_O2 13
1938#define MC_O3 14
1939#define MC_O4 15
1940#define MC_O5 16
1941#define MC_O6 17
1942#define MC_O7 18
1943#define MC_NGREG 19
1944
blueswir1992f48a2007-10-14 16:27:31 +00001945typedef abi_ulong target_mc_greg_t;
blueswir15bfb56b2007-10-05 17:01:51 +00001946typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1947
1948struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00001949 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001950 uint32_t mcfq_insn;
1951};
1952
1953struct target_mc_fpu {
1954 union {
1955 uint32_t sregs[32];
1956 uint64_t dregs[32];
1957 //uint128_t qregs[16];
1958 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00001959 abi_ulong mcfpu_fsr;
1960 abi_ulong mcfpu_fprs;
1961 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00001962 struct target_mc_fq *mcfpu_fq;
1963 unsigned char mcfpu_qcnt;
1964 unsigned char mcfpu_qentsz;
1965 unsigned char mcfpu_enab;
1966};
1967typedef struct target_mc_fpu target_mc_fpu_t;
1968
1969typedef struct {
1970 target_mc_gregset_t mc_gregs;
1971 target_mc_greg_t mc_fp;
1972 target_mc_greg_t mc_i7;
1973 target_mc_fpu_t mc_fpregs;
1974} target_mcontext_t;
1975
1976struct target_ucontext {
1977 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00001978 abi_ulong uc_flags;
blueswir15bfb56b2007-10-05 17:01:51 +00001979 target_sigset_t uc_sigmask;
1980 target_mcontext_t uc_mcontext;
1981};
1982
1983/* A V9 register window */
1984struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00001985 abi_ulong locals[8];
1986 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00001987};
1988
1989#define TARGET_STACK_BIAS 2047
1990
1991/* {set, get}context() needed for 64-bit SparcLinux userland. */
1992void sparc64_set_context(CPUSPARCState *env)
1993{
bellard459a4012007-11-11 19:45:10 +00001994 abi_ulong ucp_addr;
1995 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00001996 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00001997 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00001998 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001999 unsigned char fenab;
2000 int err;
2001 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002002
bellard459a4012007-11-11 19:45:10 +00002003 ucp_addr = env->regwptr[UREG_I0];
2004 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2005 goto do_sigsegv;
blueswir15bfb56b2007-10-05 17:01:51 +00002006 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00002007 err = __get_user(pc, &((*grp)[MC_PC]));
2008 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002009 if (err || ((pc | npc) & 3))
2010 goto do_sigsegv;
2011 if (env->regwptr[UREG_I1]) {
2012 target_sigset_t target_set;
2013 sigset_t set;
2014
2015 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002016 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002017 goto do_sigsegv;
2018 } else {
bellard459a4012007-11-11 19:45:10 +00002019 abi_ulong *src, *dst;
2020 src = ucp->uc_sigmask.sig;
2021 dst = target_set.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002022 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002023 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002024 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00002025 if (err)
2026 goto do_sigsegv;
2027 }
2028 target_to_host_sigset_internal(&set, &target_set);
2029 sigprocmask(SIG_SETMASK, &set, NULL);
2030 }
2031 env->pc = pc;
2032 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00002033 err |= __get_user(env->y, &((*grp)[MC_Y]));
2034 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002035 env->asi = (tstate >> 24) & 0xff;
2036 PUT_CCR(env, tstate >> 32);
2037 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00002038 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2039 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2040 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2041 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2042 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2043 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2044 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2045 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2046 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2047 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2048 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2049 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2050 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2051 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2052 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002053
bellard579a97f2007-11-11 14:26:47 +00002054 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2055 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002056
bellard459a4012007-11-11 19:45:10 +00002057 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2058 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2059 abi_ulong) != 0)
2060 goto do_sigsegv;
2061 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2062 abi_ulong) != 0)
2063 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002064 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2065 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002066 {
2067 uint32_t *src, *dst;
2068 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2069 dst = env->fpr;
2070 /* XXX: check that the CPU storage is the same as user context */
2071 for (i = 0; i < 64; i++, dst++, src++)
2072 err |= __get_user(*dst, src);
2073 }
bellard579a97f2007-11-11 14:26:47 +00002074 err |= __get_user(env->fsr,
2075 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2076 err |= __get_user(env->gsr,
2077 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002078 if (err)
2079 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002080 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002081 return;
2082 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002083 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002084 force_sig(SIGSEGV);
2085}
2086
2087void sparc64_get_context(CPUSPARCState *env)
2088{
bellard459a4012007-11-11 19:45:10 +00002089 abi_ulong ucp_addr;
2090 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00002091 target_mc_gregset_t *grp;
2092 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002093 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002094 int err;
2095 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002096 target_sigset_t target_set;
2097 sigset_t set;
2098
bellard459a4012007-11-11 19:45:10 +00002099 ucp_addr = env->regwptr[UREG_I0];
2100 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2101 goto do_sigsegv;
2102
blueswir15bfb56b2007-10-05 17:01:51 +00002103 mcp = &ucp->uc_mcontext;
2104 grp = &mcp->mc_gregs;
2105
2106 /* Skip over the trap instruction, first. */
2107 env->pc = env->npc;
2108 env->npc += 4;
2109
2110 err = 0;
2111
2112 sigprocmask(0, NULL, &set);
2113 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002114 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002115 err |= __put_user(target_set.sig[0],
2116 (abi_ulong *)&ucp->uc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002117 } else {
2118 abi_ulong *src, *dst;
2119 src = target_set.sig;
2120 dst = ucp->uc_sigmask.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002121 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002122 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002123 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00002124 if (err)
2125 goto do_sigsegv;
2126 }
2127
bellard459a4012007-11-11 19:45:10 +00002128 /* XXX: tstate must be saved properly */
2129 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00002130 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2131 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2132 err |= __put_user(env->y, &((*grp)[MC_Y]));
2133 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2134 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2135 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2136 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2137 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2138 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2139 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2140 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2141 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2142 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2143 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2144 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2145 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2146 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2147 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002148
bellard459a4012007-11-11 19:45:10 +00002149 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2150 fp = i7 = 0;
2151 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2152 abi_ulong) != 0)
2153 goto do_sigsegv;
2154 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2155 abi_ulong) != 0)
2156 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002157 err |= __put_user(fp, &(mcp->mc_fp));
2158 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002159
bellard459a4012007-11-11 19:45:10 +00002160 {
2161 uint32_t *src, *dst;
2162 src = env->fpr;
2163 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2164 /* XXX: check that the CPU storage is the same as user context */
2165 for (i = 0; i < 64; i++, dst++, src++)
2166 err |= __put_user(*src, dst);
2167 }
bellard579a97f2007-11-11 14:26:47 +00002168 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2169 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2170 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002171
2172 if (err)
2173 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002174 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002175 return;
2176 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002177 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002178 force_sig(SIGSEGV);
2179}
2180#endif
thsd26bc212007-11-08 18:05:37 +00002181#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002182
2183# warning signal handling not implemented
2184
2185static void setup_frame(int sig, struct emulated_sigaction *ka,
2186 target_sigset_t *set, CPUState *env)
2187{
2188 fprintf(stderr, "setup_frame: not implemented\n");
2189}
2190
2191static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2192 target_siginfo_t *info,
2193 target_sigset_t *set, CPUState *env)
2194{
2195 fprintf(stderr, "setup_rt_frame: not implemented\n");
2196}
2197
2198long do_sigreturn(CPUState *env)
2199{
2200 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002201 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002202}
2203
2204long do_rt_sigreturn(CPUState *env)
2205{
2206 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002207 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002208}
2209
thsd26bc212007-11-08 18:05:37 +00002210#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002211
2212# warning signal handling not implemented
2213
2214static void setup_frame(int sig, struct emulated_sigaction *ka,
2215 target_sigset_t *set, CPUState *env)
2216{
2217 fprintf(stderr, "setup_frame: not implemented\n");
2218}
2219
2220static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2221 target_siginfo_t *info,
2222 target_sigset_t *set, CPUState *env)
2223{
2224 fprintf(stderr, "setup_rt_frame: not implemented\n");
2225}
2226
2227long do_sigreturn(CPUState *env)
2228{
2229 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002230 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002231}
2232
2233long do_rt_sigreturn(CPUState *env)
2234{
2235 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002236 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002237}
2238
thsd26bc212007-11-08 18:05:37 +00002239#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002240
2241struct target_sigcontext {
2242 uint32_t sc_regmask; /* Unused */
2243 uint32_t sc_status;
2244 uint64_t sc_pc;
2245 uint64_t sc_regs[32];
2246 uint64_t sc_fpregs[32];
2247 uint32_t sc_ownedfp; /* Unused */
2248 uint32_t sc_fpc_csr;
2249 uint32_t sc_fpc_eir; /* Unused */
2250 uint32_t sc_used_math;
2251 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2252 uint64_t sc_mdhi;
2253 uint64_t sc_mdlo;
2254 target_ulong sc_hi1; /* Was sc_cause */
2255 target_ulong sc_lo1; /* Was sc_badvaddr */
2256 target_ulong sc_hi2; /* Was sc_sigset[4] */
2257 target_ulong sc_lo2;
2258 target_ulong sc_hi3;
2259 target_ulong sc_lo3;
2260};
2261
2262struct sigframe {
2263 uint32_t sf_ass[4]; /* argument save space for o32 */
2264 uint32_t sf_code[2]; /* signal trampoline */
2265 struct target_sigcontext sf_sc;
2266 target_sigset_t sf_mask;
2267};
2268
2269/* Install trampoline to jump back from signal handler */
2270static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2271{
2272 int err;
2273
2274 /*
2275 * Set up the return code ...
2276 *
2277 * li v0, __NR__foo_sigreturn
2278 * syscall
2279 */
2280
2281 err = __put_user(0x24020000 + syscall, tramp + 0);
2282 err |= __put_user(0x0000000c , tramp + 1);
2283 /* flush_cache_sigtramp((unsigned long) tramp); */
2284 return err;
2285}
2286
2287static inline int
2288setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2289{
2290 int err = 0;
2291
thsead93602007-09-06 00:18:15 +00002292 err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002293
thsead93602007-09-06 00:18:15 +00002294#define save_gp_reg(i) do { \
thsd0dc7dc2008-02-12 21:01:26 +00002295 err |= __put_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002296 } while(0)
2297 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2298 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2299 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2300 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2301 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2302 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2303 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2304 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2305 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002306#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002307
thsd0dc7dc2008-02-12 21:01:26 +00002308 err |= __put_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2309 err |= __put_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002310
2311 /* Not used yet, but might be useful if we ever have DSP suppport */
2312#if 0
2313 if (cpu_has_dsp) {
2314 err |= __put_user(mfhi1(), &sc->sc_hi1);
2315 err |= __put_user(mflo1(), &sc->sc_lo1);
2316 err |= __put_user(mfhi2(), &sc->sc_hi2);
2317 err |= __put_user(mflo2(), &sc->sc_lo2);
2318 err |= __put_user(mfhi3(), &sc->sc_hi3);
2319 err |= __put_user(mflo3(), &sc->sc_lo3);
2320 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2321 }
2322 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002323#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002324 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2325 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2326 if (cpu_has_dsp) {
2327 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2328 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2329 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2330 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2331 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2332 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2333 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2334 }
ths388bb212007-05-13 13:58:00 +00002335#endif
2336#endif
bellard106ec872006-06-27 21:08:10 +00002337
ths388bb212007-05-13 13:58:00 +00002338#if 0
bellard106ec872006-06-27 21:08:10 +00002339 err |= __put_user(!!used_math(), &sc->sc_used_math);
2340
2341 if (!used_math())
2342 goto out;
2343
2344 /*
2345 * Save FPU state to signal context. Signal handler will "inherit"
2346 * current FPU state.
2347 */
2348 preempt_disable();
2349
2350 if (!is_fpu_owner()) {
2351 own_fpu();
2352 restore_fp(current);
2353 }
2354 err |= save_fp_context(sc);
2355
2356 preempt_enable();
2357 out:
2358#endif
2359 return err;
2360}
2361
2362static inline int
2363restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2364{
2365 int err = 0;
2366
2367 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2368
thsd0dc7dc2008-02-12 21:01:26 +00002369 err |= __get_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2370 err |= __get_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002371
thsead93602007-09-06 00:18:15 +00002372#define restore_gp_reg(i) do { \
thsd0dc7dc2008-02-12 21:01:26 +00002373 err |= __get_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002374 } while(0)
2375 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2376 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2377 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2378 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2379 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2380 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2381 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2382 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2383 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2384 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2385 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002386#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002387
2388#if 0
2389 if (cpu_has_dsp) {
2390 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2391 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2392 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2393 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2394 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2395 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2396 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2397 }
ths388bb212007-05-13 13:58:00 +00002398#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002399 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2400 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2401 if (cpu_has_dsp) {
2402 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2403 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2404 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2405 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2406 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2407 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2408 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2409 }
ths388bb212007-05-13 13:58:00 +00002410#endif
bellard106ec872006-06-27 21:08:10 +00002411
2412 err |= __get_user(used_math, &sc->sc_used_math);
2413 conditional_used_math(used_math);
2414
2415 preempt_disable();
2416
2417 if (used_math()) {
2418 /* restore fpu context if we have used it before */
2419 own_fpu();
2420 err |= restore_fp_context(sc);
2421 } else {
2422 /* signal handler may have used FPU. Give it up. */
2423 lose_fpu();
2424 }
2425
2426 preempt_enable();
2427#endif
2428 return err;
2429}
2430/*
2431 * Determine which stack to use..
2432 */
bellard579a97f2007-11-11 14:26:47 +00002433static inline abi_ulong
bellard106ec872006-06-27 21:08:10 +00002434get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
2435{
2436 unsigned long sp;
2437
2438 /* Default to using normal stack */
thsd0dc7dc2008-02-12 21:01:26 +00002439 sp = regs->gpr[regs->current_tc][29];
bellard106ec872006-06-27 21:08:10 +00002440
2441 /*
2442 * FPU emulator may have it's own trampoline active just
2443 * above the user stack, 16-bytes before the next lowest
2444 * 16 byte boundary. Try to avoid trashing it.
2445 */
2446 sp -= 32;
2447
bellard106ec872006-06-27 21:08:10 +00002448 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00002449 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2450 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2451 }
bellard106ec872006-06-27 21:08:10 +00002452
bellard579a97f2007-11-11 14:26:47 +00002453 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002454}
2455
bellard579a97f2007-11-11 14:26:47 +00002456/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
ths5fafdf22007-09-16 21:08:06 +00002457static void setup_frame(int sig, struct emulated_sigaction * ka,
bellard579a97f2007-11-11 14:26:47 +00002458 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002459{
2460 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002461 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002462 int i;
2463
bellard579a97f2007-11-11 14:26:47 +00002464 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2465 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002466 goto give_sigsegv;
2467
2468 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2469
2470 if(setup_sigcontext(regs, &frame->sf_sc))
2471 goto give_sigsegv;
2472
2473 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2474 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2475 goto give_sigsegv;
2476 }
2477
2478 /*
2479 * Arguments to signal handler:
2480 *
2481 * a0 = signal number
2482 * a1 = 0 (should be cause)
2483 * a2 = pointer to struct sigcontext
2484 *
2485 * $25 and PC point to the signal handler, $29 points to the
2486 * struct sigframe.
2487 */
thsd0dc7dc2008-02-12 21:01:26 +00002488 regs->gpr[regs->current_tc][ 4] = sig;
2489 regs->gpr[regs->current_tc][ 5] = 0;
2490 regs->gpr[regs->current_tc][ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2491 regs->gpr[regs->current_tc][29] = frame_addr;
2492 regs->gpr[regs->current_tc][31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002493 /* The original kernel code sets CP0_EPC to the handler
2494 * since it returns to userland using eret
2495 * we cannot do this here, and we must set PC directly */
thsd0dc7dc2008-02-12 21:01:26 +00002496 regs->PC[regs->current_tc] = regs->gpr[regs->current_tc][25] = ka->sa._sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002497 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002498 return;
2499
2500give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002501 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002502 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002503 return;
bellard106ec872006-06-27 21:08:10 +00002504}
2505
2506long do_sigreturn(CPUState *regs)
2507{
ths388bb212007-05-13 13:58:00 +00002508 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002509 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002510 sigset_t blocked;
2511 target_sigset_t target_set;
2512 int i;
bellard106ec872006-06-27 21:08:10 +00002513
2514#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002515 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002516#endif
thsd0dc7dc2008-02-12 21:01:26 +00002517 frame_addr = regs->gpr[regs->current_tc][29];
bellard579a97f2007-11-11 14:26:47 +00002518 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002519 goto badframe;
2520
ths388bb212007-05-13 13:58:00 +00002521 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002522 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2523 goto badframe;
ths388bb212007-05-13 13:58:00 +00002524 }
bellard106ec872006-06-27 21:08:10 +00002525
ths388bb212007-05-13 13:58:00 +00002526 target_to_host_sigset_internal(&blocked, &target_set);
2527 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002528
ths388bb212007-05-13 13:58:00 +00002529 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002530 goto badframe;
2531
2532#if 0
ths388bb212007-05-13 13:58:00 +00002533 /*
2534 * Don't let your children do this ...
2535 */
2536 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002537 "move\t$29, %0\n\t"
2538 "j\tsyscall_exit"
2539 :/* no outputs */
2540 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002541 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002542#endif
ths3b46e622007-09-17 08:09:54 +00002543
thsead93602007-09-06 00:18:15 +00002544 regs->PC[regs->current_tc] = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002545 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002546 * maybe a problem with nested signals ? */
2547 regs->CP0_EPC = 0;
2548 return 0;
2549
2550badframe:
ths388bb212007-05-13 13:58:00 +00002551 force_sig(TARGET_SIGSEGV/*, current*/);
2552 return 0;
bellard106ec872006-06-27 21:08:10 +00002553}
2554
ths5fafdf22007-09-16 21:08:06 +00002555static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard106ec872006-06-27 21:08:10 +00002556 target_siginfo_t *info,
2557 target_sigset_t *set, CPUState *env)
2558{
2559 fprintf(stderr, "setup_rt_frame: not implemented\n");
2560}
2561
2562long do_rt_sigreturn(CPUState *env)
2563{
2564 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002565 return -TARGET_ENOSYS;
bellard106ec872006-06-27 21:08:10 +00002566}
bellard6d5e2162004-09-30 22:04:13 +00002567
thsc3b5bc82007-12-02 06:31:25 +00002568#elif defined(TARGET_SH4)
2569
2570/*
2571 * code and data structures from linux kernel:
2572 * include/asm-sh/sigcontext.h
2573 * arch/sh/kernel/signal.c
2574 */
2575
2576struct target_sigcontext {
2577 target_ulong oldmask;
2578
2579 /* CPU registers */
2580 target_ulong sc_gregs[16];
2581 target_ulong sc_pc;
2582 target_ulong sc_pr;
2583 target_ulong sc_sr;
2584 target_ulong sc_gbr;
2585 target_ulong sc_mach;
2586 target_ulong sc_macl;
2587
2588 /* FPU registers */
2589 target_ulong sc_fpregs[16];
2590 target_ulong sc_xfpregs[16];
2591 unsigned int sc_fpscr;
2592 unsigned int sc_fpul;
2593 unsigned int sc_ownedfp;
2594};
2595
2596struct target_sigframe
2597{
2598 struct target_sigcontext sc;
2599 target_ulong extramask[TARGET_NSIG_WORDS-1];
2600 uint16_t retcode[3];
2601};
2602
2603
2604struct target_ucontext {
2605 target_ulong uc_flags;
2606 struct target_ucontext *uc_link;
2607 target_stack_t uc_stack;
2608 struct target_sigcontext uc_mcontext;
2609 target_sigset_t uc_sigmask; /* mask last for extensibility */
2610};
2611
2612struct target_rt_sigframe
2613{
2614 struct target_siginfo info;
2615 struct target_ucontext uc;
2616 uint16_t retcode[3];
2617};
2618
2619
2620#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2621#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2622
2623static abi_ulong get_sigframe(struct emulated_sigaction *ka,
2624 unsigned long sp, size_t frame_size)
2625{
2626 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2627 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2628 }
2629
2630 return (sp - frame_size) & -8ul;
2631}
2632
2633static int setup_sigcontext(struct target_sigcontext *sc,
2634 CPUState *regs, unsigned long mask)
2635{
2636 int err = 0;
2637
2638#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2639 COPY(gregs[0]); COPY(gregs[1]);
2640 COPY(gregs[2]); COPY(gregs[3]);
2641 COPY(gregs[4]); COPY(gregs[5]);
2642 COPY(gregs[6]); COPY(gregs[7]);
2643 COPY(gregs[8]); COPY(gregs[9]);
2644 COPY(gregs[10]); COPY(gregs[11]);
2645 COPY(gregs[12]); COPY(gregs[13]);
2646 COPY(gregs[14]); COPY(gregs[15]);
2647 COPY(gbr); COPY(mach);
2648 COPY(macl); COPY(pr);
2649 COPY(sr); COPY(pc);
2650#undef COPY
2651
2652 /* todo: save FPU registers here */
2653
2654 /* non-iBCS2 extensions.. */
2655 err |= __put_user(mask, &sc->oldmask);
2656
2657 return err;
2658}
2659
2660static int restore_sigcontext(struct CPUState *regs,
2661 struct target_sigcontext *sc)
2662{
2663 unsigned int err = 0;
2664
2665#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2666 COPY(gregs[1]);
2667 COPY(gregs[2]); COPY(gregs[3]);
2668 COPY(gregs[4]); COPY(gregs[5]);
2669 COPY(gregs[6]); COPY(gregs[7]);
2670 COPY(gregs[8]); COPY(gregs[9]);
2671 COPY(gregs[10]); COPY(gregs[11]);
2672 COPY(gregs[12]); COPY(gregs[13]);
2673 COPY(gregs[14]); COPY(gregs[15]);
2674 COPY(gbr); COPY(mach);
2675 COPY(macl); COPY(pr);
2676 COPY(sr); COPY(pc);
2677#undef COPY
2678
2679 /* todo: restore FPU registers here */
2680
2681 regs->tra = -1; /* disable syscall checks */
2682 return err;
2683}
2684
2685static void setup_frame(int sig, struct emulated_sigaction *ka,
2686 target_sigset_t *set, CPUState *regs)
2687{
2688 struct target_sigframe *frame;
2689 abi_ulong frame_addr;
2690 int i;
2691 int err = 0;
2692 int signal;
2693
2694 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2695 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2696 goto give_sigsegv;
2697
2698 signal = current_exec_domain_sig(sig);
2699
2700 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2701
2702 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2703 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2704 }
2705
2706 /* Set up to return from userspace. If provided, use a stub
2707 already in userspace. */
2708 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2709 regs->pr = (unsigned long) ka->sa.sa_restorer;
2710 } else {
2711 /* Generate return code (system call to sigreturn) */
2712 err |= __put_user(MOVW(2), &frame->retcode[0]);
2713 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2714 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2715 regs->pr = (unsigned long) frame->retcode;
2716 }
2717
2718 if (err)
2719 goto give_sigsegv;
2720
2721 /* Set up registers for signal handler */
2722 regs->gregs[15] = (unsigned long) frame;
2723 regs->gregs[4] = signal; /* Arg for signal handler */
2724 regs->gregs[5] = 0;
2725 regs->gregs[6] = (unsigned long) &frame->sc;
2726 regs->pc = (unsigned long) ka->sa._sa_handler;
2727
2728 unlock_user_struct(frame, frame_addr, 1);
2729 return;
2730
2731give_sigsegv:
2732 unlock_user_struct(frame, frame_addr, 1);
2733 force_sig(SIGSEGV);
2734}
2735
2736static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2737 target_siginfo_t *info,
2738 target_sigset_t *set, CPUState *regs)
2739{
2740 struct target_rt_sigframe *frame;
2741 abi_ulong frame_addr;
2742 int i;
2743 int err = 0;
2744 int signal;
2745
2746 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2747 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2748 goto give_sigsegv;
2749
2750 signal = current_exec_domain_sig(sig);
2751
2752 err |= copy_siginfo_to_user(&frame->info, info);
2753
2754 /* Create the ucontext. */
2755 err |= __put_user(0, &frame->uc.uc_flags);
2756 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
2757 err |= __put_user((void *)target_sigaltstack_used.ss_sp,
2758 &frame->uc.uc_stack.ss_sp);
2759 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2760 &frame->uc.uc_stack.ss_flags);
2761 err |= __put_user(target_sigaltstack_used.ss_size,
2762 &frame->uc.uc_stack.ss_size);
2763 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2764 regs, set->sig[0]);
2765 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2766 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2767 }
2768
2769 /* Set up to return from userspace. If provided, use a stub
2770 already in userspace. */
2771 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2772 regs->pr = (unsigned long) ka->sa.sa_restorer;
2773 } else {
2774 /* Generate return code (system call to sigreturn) */
2775 err |= __put_user(MOVW(2), &frame->retcode[0]);
2776 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2777 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2778 regs->pr = (unsigned long) frame->retcode;
2779 }
2780
2781 if (err)
2782 goto give_sigsegv;
2783
2784 /* Set up registers for signal handler */
2785 regs->gregs[15] = (unsigned long) frame;
2786 regs->gregs[4] = signal; /* Arg for signal handler */
2787 regs->gregs[5] = (unsigned long) &frame->info;
2788 regs->gregs[6] = (unsigned long) &frame->uc;
2789 regs->pc = (unsigned long) ka->sa._sa_handler;
2790
2791 unlock_user_struct(frame, frame_addr, 1);
2792 return;
2793
2794give_sigsegv:
2795 unlock_user_struct(frame, frame_addr, 1);
2796 force_sig(SIGSEGV);
2797}
2798
2799long do_sigreturn(CPUState *regs)
2800{
2801 struct target_sigframe *frame;
2802 abi_ulong frame_addr;
2803 sigset_t blocked;
2804 target_sigset_t target_set;
2805 int i;
2806 int err = 0;
2807
2808#if defined(DEBUG_SIGNAL)
2809 fprintf(stderr, "do_sigreturn\n");
2810#endif
2811 frame_addr = regs->gregs[15];
2812 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2813 goto badframe;
2814
2815 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2816 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2817 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2818 }
2819
2820 if (err)
2821 goto badframe;
2822
2823 target_to_host_sigset_internal(&blocked, &target_set);
2824 sigprocmask(SIG_SETMASK, &blocked, NULL);
2825
2826 if (restore_sigcontext(regs, &frame->sc))
2827 goto badframe;
2828
2829 unlock_user_struct(frame, frame_addr, 0);
2830 return regs->gregs[0];
2831
2832badframe:
2833 unlock_user_struct(frame, frame_addr, 0);
2834 force_sig(TARGET_SIGSEGV);
2835 return 0;
2836}
2837
2838long do_rt_sigreturn(CPUState *regs)
2839{
2840 struct target_rt_sigframe *frame;
2841 abi_ulong frame_addr;
2842 sigset_t blocked;
2843
2844#if defined(DEBUG_SIGNAL)
2845 fprintf(stderr, "do_rt_sigreturn\n");
2846#endif
2847 frame_addr = regs->gregs[15];
2848 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2849 goto badframe;
2850
2851 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
2852 sigprocmask(SIG_SETMASK, &blocked, NULL);
2853
2854 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
2855 goto badframe;
2856
2857 if (do_sigaltstack(frame_addr +
2858 offsetof(struct target_rt_sigframe, uc.uc_stack),
2859 0, get_sp_from_cpustate(regs)) == -EFAULT)
2860 goto badframe;
2861
2862 unlock_user_struct(frame, frame_addr, 0);
2863 return regs->gregs[0];
2864
2865badframe:
2866 unlock_user_struct(frame, frame_addr, 0);
2867 force_sig(TARGET_SIGSEGV);
2868 return 0;
2869}
edgar_iglb6d3abd2008-02-28 11:29:27 +00002870#elif defined(TARGET_CRIS)
2871
2872struct target_sigcontext {
2873 struct target_pt_regs regs; /* needs to be first */
2874 uint32_t oldmask;
2875 uint32_t usp; /* usp before stacking this gunk on it */
2876};
2877
2878/* Signal frames. */
2879struct target_signal_frame {
2880 struct target_sigcontext sc;
2881 uint32_t extramask[TARGET_NSIG_WORDS - 1];
2882 uint8_t retcode[8]; /* Trampoline code. */
2883};
2884
2885struct rt_signal_frame {
2886 struct siginfo *pinfo;
2887 void *puc;
2888 struct siginfo info;
2889 struct ucontext uc;
2890 uint8_t retcode[8]; /* Trampoline code. */
2891};
2892
2893static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
2894{
edgar_igl9664d922008-03-03 22:23:53 +00002895 __put_user(env->regs[0], &sc->regs.r0);
2896 __put_user(env->regs[1], &sc->regs.r1);
2897 __put_user(env->regs[2], &sc->regs.r2);
2898 __put_user(env->regs[3], &sc->regs.r3);
2899 __put_user(env->regs[4], &sc->regs.r4);
2900 __put_user(env->regs[5], &sc->regs.r5);
2901 __put_user(env->regs[6], &sc->regs.r6);
2902 __put_user(env->regs[7], &sc->regs.r7);
2903 __put_user(env->regs[8], &sc->regs.r8);
2904 __put_user(env->regs[9], &sc->regs.r9);
2905 __put_user(env->regs[10], &sc->regs.r10);
2906 __put_user(env->regs[11], &sc->regs.r11);
2907 __put_user(env->regs[12], &sc->regs.r12);
2908 __put_user(env->regs[13], &sc->regs.r13);
2909 __put_user(env->regs[14], &sc->usp);
2910 __put_user(env->regs[15], &sc->regs.acr);
2911 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
2912 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
2913 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002914}
edgar_igl9664d922008-03-03 22:23:53 +00002915
edgar_iglb6d3abd2008-02-28 11:29:27 +00002916static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
2917{
edgar_igl9664d922008-03-03 22:23:53 +00002918 __get_user(env->regs[0], &sc->regs.r0);
2919 __get_user(env->regs[1], &sc->regs.r1);
2920 __get_user(env->regs[2], &sc->regs.r2);
2921 __get_user(env->regs[3], &sc->regs.r3);
2922 __get_user(env->regs[4], &sc->regs.r4);
2923 __get_user(env->regs[5], &sc->regs.r5);
2924 __get_user(env->regs[6], &sc->regs.r6);
2925 __get_user(env->regs[7], &sc->regs.r7);
2926 __get_user(env->regs[8], &sc->regs.r8);
2927 __get_user(env->regs[9], &sc->regs.r9);
2928 __get_user(env->regs[10], &sc->regs.r10);
2929 __get_user(env->regs[11], &sc->regs.r11);
2930 __get_user(env->regs[12], &sc->regs.r12);
2931 __get_user(env->regs[13], &sc->regs.r13);
2932 __get_user(env->regs[14], &sc->usp);
2933 __get_user(env->regs[15], &sc->regs.acr);
2934 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
2935 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
2936 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002937}
2938
edgar_igl9664d922008-03-03 22:23:53 +00002939static abi_ulong get_sigframe(CPUState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00002940{
edgar_igl9664d922008-03-03 22:23:53 +00002941 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002942 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00002943 sp = (env->regs[R_SP] & ~3);
2944 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002945}
2946
2947static void setup_frame(int sig, struct emulated_sigaction *ka,
2948 target_sigset_t *set, CPUState *env)
2949{
2950 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00002951 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002952 int err = 0;
2953 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002954
edgar_igl9664d922008-03-03 22:23:53 +00002955 frame_addr = get_sigframe(env, sizeof *frame);
2956 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00002957 goto badframe;
2958
2959 /*
2960 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
2961 * use this trampoline anymore but it sets it up for GDB.
2962 * In QEMU, using the trampoline simplifies things a bit so we use it.
2963 *
2964 * This is movu.w __NR_sigreturn, r9; break 13;
2965 */
2966 err |= __put_user(0x9c5f, frame->retcode+0);
2967 err |= __put_user(TARGET_NR_sigreturn,
2968 frame->retcode+2);
2969 err |= __put_user(0xe93d, frame->retcode+4);
2970
2971 /* Save the mask. */
2972 err |= __put_user(set->sig[0], &frame->sc.oldmask);
2973 if (err)
2974 goto badframe;
2975
2976 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2977 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
2978 goto badframe;
2979 }
2980
2981 setup_sigcontext(&frame->sc, env);
2982
2983 /* Move the stack and setup the arguments for the handler. */
2984 env->regs[R_SP] = (uint32_t) frame;
2985 env->regs[10] = sig;
2986 env->pc = (unsigned long) ka->sa._sa_handler;
2987 /* Link SRP so the guest returns through the trampoline. */
2988 env->pregs[PR_SRP] = (uint32_t) &frame->retcode[0];
2989
edgar_igl9664d922008-03-03 22:23:53 +00002990 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002991 return;
2992 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00002993 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002994 force_sig(TARGET_SIGSEGV);
2995}
2996
2997static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2998 target_siginfo_t *info,
2999 target_sigset_t *set, CPUState *env)
3000{
3001 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3002}
3003
3004long do_sigreturn(CPUState *env)
3005{
3006 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003007 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003008 target_sigset_t target_set;
3009 sigset_t set;
3010 int i;
3011
edgar_igl9664d922008-03-03 22:23:53 +00003012 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003013 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003014 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003015 goto badframe;
3016
3017 /* Restore blocked signals */
3018 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3019 goto badframe;
3020 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3021 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3022 goto badframe;
3023 }
3024 target_to_host_sigset_internal(&set, &target_set);
3025 sigprocmask(SIG_SETMASK, &set, NULL);
3026
3027 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003028 /* Compensate for the syscall return path advancing brk. */
3029 env->pc -= 2;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003030
edgar_igl9664d922008-03-03 22:23:53 +00003031 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003032 return env->regs[10];
3033 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003034 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003035 force_sig(TARGET_SIGSEGV);
3036}
3037
3038long do_rt_sigreturn(CPUState *env)
3039{
3040 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3041 return -TARGET_ENOSYS;
3042}
thsc3b5bc82007-12-02 06:31:25 +00003043
bellardb346ff42003-06-15 20:05:50 +00003044#else
3045
3046static void setup_frame(int sig, struct emulated_sigaction *ka,
3047 target_sigset_t *set, CPUState *env)
3048{
3049 fprintf(stderr, "setup_frame: not implemented\n");
3050}
3051
ths5fafdf22007-09-16 21:08:06 +00003052static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00003053 target_siginfo_t *info,
3054 target_sigset_t *set, CPUState *env)
3055{
3056 fprintf(stderr, "setup_rt_frame: not implemented\n");
3057}
3058
3059long do_sigreturn(CPUState *env)
3060{
3061 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003062 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003063}
3064
3065long do_rt_sigreturn(CPUState *env)
3066{
3067 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003068 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003069}
3070
bellard66fb9762003-03-23 01:06:05 +00003071#endif
3072
3073void process_pending_signals(void *cpu_env)
3074{
3075 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00003076 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00003077 sigset_t set, old_set;
3078 target_sigset_t target_old_set;
bellard66fb9762003-03-23 01:06:05 +00003079 struct emulated_sigaction *k;
3080 struct sigqueue *q;
ths3b46e622007-09-17 08:09:54 +00003081
bellard31e31b82003-02-18 22:55:36 +00003082 if (!signal_pending)
3083 return;
3084
bellard66fb9762003-03-23 01:06:05 +00003085 k = sigact_table;
3086 for(sig = 1; sig <= TARGET_NSIG; sig++) {
3087 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00003088 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00003089 k++;
bellard31e31b82003-02-18 22:55:36 +00003090 }
3091 /* if no signal is pending, just return */
3092 signal_pending = 0;
3093 return;
bellard66fb9762003-03-23 01:06:05 +00003094
bellard31e31b82003-02-18 22:55:36 +00003095 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00003096#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00003097 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00003098#endif
3099 /* dequeue signal */
3100 q = k->first;
3101 k->first = q->next;
3102 if (!k->first)
3103 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00003104
bellard1fddef42005-04-17 19:16:13 +00003105 sig = gdb_handlesig (cpu_env, sig);
3106 if (!sig) {
3107 fprintf (stderr, "Lost signal\n");
3108 abort();
3109 }
bellard66fb9762003-03-23 01:06:05 +00003110
3111 handler = k->sa._sa_handler;
3112 if (handler == TARGET_SIG_DFL) {
3113 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +00003114 if (sig != TARGET_SIGCHLD &&
3115 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +00003116 sig != TARGET_SIGWINCH) {
3117 force_sig(sig);
3118 }
3119 } else if (handler == TARGET_SIG_IGN) {
3120 /* ignore sig */
3121 } else if (handler == TARGET_SIG_ERR) {
3122 force_sig(sig);
3123 } else {
bellard9de5e442003-03-23 16:49:39 +00003124 /* compute the blocked signals during the handler execution */
3125 target_to_host_sigset(&set, &k->sa.sa_mask);
3126 /* SA_NODEFER indicates that the current signal should not be
3127 blocked during the handler */
3128 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
3129 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00003130
bellard9de5e442003-03-23 16:49:39 +00003131 /* block signals in the handler using Linux */
3132 sigprocmask(SIG_BLOCK, &set, &old_set);
3133 /* save the previous blocked signal state to restore it at the
3134 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00003135 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00003136
bellardbc8a22c2003-03-30 21:02:40 +00003137 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00003138#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00003139 {
3140 CPUX86State *env = cpu_env;
3141 if (env->eflags & VM_MASK)
3142 save_v86_state(env);
3143 }
3144#endif
bellard9de5e442003-03-23 16:49:39 +00003145 /* prepare the stack frame of the virtual CPU */
bellard66fb9762003-03-23 01:06:05 +00003146 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
bellard9de5e442003-03-23 16:49:39 +00003147 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00003148 else
bellard9de5e442003-03-23 16:49:39 +00003149 setup_frame(sig, k, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00003150 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
3151 k->sa._sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00003152 }
bellard66fb9762003-03-23 01:06:05 +00003153 if (q != &k->info)
3154 free_sigqueue(q);
bellard31e31b82003-02-18 22:55:36 +00003155}