blob: 0c5944abab917cf9a0f17d3b305ae6f3cb30698a [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard66fb9762003-03-23 01:06:05 +00002 * Emulation of Linux signals
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <stdlib.h>
21#include <stdio.h>
bellard66fb9762003-03-23 01:06:05 +000022#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000023#include <stdarg.h>
bellard2677e102003-04-10 00:03:27 +000024#include <unistd.h>
bellard31e31b82003-02-18 22:55:36 +000025#include <signal.h>
bellard66fb9762003-03-23 01:06:05 +000026#include <errno.h>
bellard31e31b82003-02-18 22:55:36 +000027#include <sys/ucontext.h>
28
bellard3ef693a2003-03-23 20:17:16 +000029#include "qemu.h"
blueswir1992f48a2007-10-14 16:27:31 +000030#include "target_signal.h"
bellard66fb9762003-03-23 01:06:05 +000031
32//#define DEBUG_SIGNAL
33
34#define MAX_SIGQUEUE_SIZE 1024
35
36struct sigqueue {
37 struct sigqueue *next;
bellard9de5e442003-03-23 16:49:39 +000038 target_siginfo_t info;
bellard66fb9762003-03-23 01:06:05 +000039};
bellard31e31b82003-02-18 22:55:36 +000040
41struct emulated_sigaction {
42 struct target_sigaction sa;
bellard66fb9762003-03-23 01:06:05 +000043 int pending; /* true if signal is pending */
44 struct sigqueue *first;
45 struct sigqueue info; /* in order to always have memory for the
46 first signal, we put it here */
bellard31e31b82003-02-18 22:55:36 +000047};
48
thsa04e1342007-09-27 13:57:58 +000049struct target_sigaltstack target_sigaltstack_used = {
50 .ss_sp = 0,
51 .ss_size = 0,
52 .ss_flags = TARGET_SS_DISABLE,
53};
54
bellard66fb9762003-03-23 01:06:05 +000055static struct emulated_sigaction sigact_table[TARGET_NSIG];
56static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57static struct sigqueue *first_free; /* first free siginfo queue entry */
58static int signal_pending; /* non zero if a signal may be pending */
bellard31e31b82003-02-18 22:55:36 +000059
ths5fafdf22007-09-16 21:08:06 +000060static void host_signal_handler(int host_signum, siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +000061 void *puc);
62
bellard9e5f5282003-07-13 17:33:54 +000063static uint8_t host_to_target_signal_table[65] = {
64 [SIGHUP] = TARGET_SIGHUP,
65 [SIGINT] = TARGET_SIGINT,
66 [SIGQUIT] = TARGET_SIGQUIT,
67 [SIGILL] = TARGET_SIGILL,
68 [SIGTRAP] = TARGET_SIGTRAP,
69 [SIGABRT] = TARGET_SIGABRT,
bellard01e3b762003-09-30 21:10:14 +000070/* [SIGIOT] = TARGET_SIGIOT,*/
bellard9e5f5282003-07-13 17:33:54 +000071 [SIGBUS] = TARGET_SIGBUS,
72 [SIGFPE] = TARGET_SIGFPE,
73 [SIGKILL] = TARGET_SIGKILL,
74 [SIGUSR1] = TARGET_SIGUSR1,
75 [SIGSEGV] = TARGET_SIGSEGV,
76 [SIGUSR2] = TARGET_SIGUSR2,
77 [SIGPIPE] = TARGET_SIGPIPE,
78 [SIGALRM] = TARGET_SIGALRM,
79 [SIGTERM] = TARGET_SIGTERM,
80#ifdef SIGSTKFLT
81 [SIGSTKFLT] = TARGET_SIGSTKFLT,
82#endif
83 [SIGCHLD] = TARGET_SIGCHLD,
84 [SIGCONT] = TARGET_SIGCONT,
85 [SIGSTOP] = TARGET_SIGSTOP,
86 [SIGTSTP] = TARGET_SIGTSTP,
87 [SIGTTIN] = TARGET_SIGTTIN,
88 [SIGTTOU] = TARGET_SIGTTOU,
89 [SIGURG] = TARGET_SIGURG,
90 [SIGXCPU] = TARGET_SIGXCPU,
91 [SIGXFSZ] = TARGET_SIGXFSZ,
92 [SIGVTALRM] = TARGET_SIGVTALRM,
93 [SIGPROF] = TARGET_SIGPROF,
94 [SIGWINCH] = TARGET_SIGWINCH,
95 [SIGIO] = TARGET_SIGIO,
96 [SIGPWR] = TARGET_SIGPWR,
97 [SIGSYS] = TARGET_SIGSYS,
98 /* next signals stay the same */
99};
100static uint8_t target_to_host_signal_table[65];
101
thsa04e1342007-09-27 13:57:58 +0000102static inline int on_sig_stack(unsigned long sp)
103{
104 return (sp - target_sigaltstack_used.ss_sp
105 < target_sigaltstack_used.ss_size);
106}
107
108static inline int sas_ss_flags(unsigned long sp)
109{
110 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
111 : on_sig_stack(sp) ? SS_ONSTACK : 0);
112}
113
bellard31e31b82003-02-18 22:55:36 +0000114static inline int host_to_target_signal(int sig)
115{
bellard9e5f5282003-07-13 17:33:54 +0000116 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000117}
118
119static inline int target_to_host_signal(int sig)
120{
bellard9e5f5282003-07-13 17:33:54 +0000121 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000122}
123
ths5fafdf22007-09-16 21:08:06 +0000124static void host_to_target_sigset_internal(target_sigset_t *d,
bellard92319442004-06-19 16:58:13 +0000125 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000126{
127 int i;
bellard9e5f5282003-07-13 17:33:54 +0000128 unsigned long sigmask;
129 uint32_t target_sigmask;
ths3b46e622007-09-17 08:09:54 +0000130
bellard9e5f5282003-07-13 17:33:54 +0000131 sigmask = ((unsigned long *)s)[0];
132 target_sigmask = 0;
133 for(i = 0; i < 32; i++) {
ths5fafdf22007-09-16 21:08:06 +0000134 if (sigmask & (1 << i))
bellard9e5f5282003-07-13 17:33:54 +0000135 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
136 }
blueswir1992f48a2007-10-14 16:27:31 +0000137#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
bellard92319442004-06-19 16:58:13 +0000138 d->sig[0] = target_sigmask;
bellard9e5f5282003-07-13 17:33:54 +0000139 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard92319442004-06-19 16:58:13 +0000140 d->sig[i] = ((unsigned long *)s)[i];
bellard66fb9762003-03-23 01:06:05 +0000141 }
blueswir1992f48a2007-10-14 16:27:31 +0000142#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
bellard92319442004-06-19 16:58:13 +0000143 d->sig[0] = target_sigmask;
144 d->sig[1] = sigmask >> 32;
bellard9e5f5282003-07-13 17:33:54 +0000145#else
bellarda315a142005-01-30 22:59:18 +0000146#warning host_to_target_sigset
bellard9e5f5282003-07-13 17:33:54 +0000147#endif
bellard66fb9762003-03-23 01:06:05 +0000148}
149
bellard92319442004-06-19 16:58:13 +0000150void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
151{
152 target_sigset_t d1;
153 int i;
154
155 host_to_target_sigset_internal(&d1, s);
156 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000157 d->sig[i] = tswapl(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000158}
159
160void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000161{
162 int i;
bellard9e5f5282003-07-13 17:33:54 +0000163 unsigned long sigmask;
blueswir1992f48a2007-10-14 16:27:31 +0000164 abi_ulong target_sigmask;
bellard9e5f5282003-07-13 17:33:54 +0000165
bellard92319442004-06-19 16:58:13 +0000166 target_sigmask = s->sig[0];
bellard9e5f5282003-07-13 17:33:54 +0000167 sigmask = 0;
168 for(i = 0; i < 32; i++) {
ths5fafdf22007-09-16 21:08:06 +0000169 if (target_sigmask & (1 << i))
bellard9e5f5282003-07-13 17:33:54 +0000170 sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
171 }
blueswir1992f48a2007-10-14 16:27:31 +0000172#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
bellard9e5f5282003-07-13 17:33:54 +0000173 ((unsigned long *)d)[0] = sigmask;
174 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard92319442004-06-19 16:58:13 +0000175 ((unsigned long *)d)[i] = s->sig[i];
bellard66fb9762003-03-23 01:06:05 +0000176 }
blueswir1992f48a2007-10-14 16:27:31 +0000177#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
bellard92319442004-06-19 16:58:13 +0000178 ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
bellard9e5f5282003-07-13 17:33:54 +0000179#else
bellarda315a142005-01-30 22:59:18 +0000180#warning target_to_host_sigset
blueswir1992f48a2007-10-14 16:27:31 +0000181#endif /* TARGET_ABI_BITS */
bellard66fb9762003-03-23 01:06:05 +0000182}
183
bellard92319442004-06-19 16:58:13 +0000184void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
185{
186 target_sigset_t s1;
187 int i;
188
189 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000190 s1.sig[i] = tswapl(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000191 target_to_host_sigset_internal(d, &s1);
192}
ths3b46e622007-09-17 08:09:54 +0000193
blueswir1992f48a2007-10-14 16:27:31 +0000194void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000195 const sigset_t *sigset)
196{
bellard9e5f5282003-07-13 17:33:54 +0000197 target_sigset_t d;
198 host_to_target_sigset(&d, sigset);
199 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000200}
201
ths5fafdf22007-09-16 21:08:06 +0000202void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000203 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000204{
bellard9e5f5282003-07-13 17:33:54 +0000205 target_sigset_t d;
206 int i;
207
208 d.sig[0] = *old_sigset;
209 for(i = 1;i < TARGET_NSIG_WORDS; i++)
210 d.sig[i] = 0;
211 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000212}
213
bellard9de5e442003-03-23 16:49:39 +0000214/* siginfo conversion */
215
ths5fafdf22007-09-16 21:08:06 +0000216static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000217 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000218{
bellard9de5e442003-03-23 16:49:39 +0000219 int sig;
220 sig = host_to_target_signal(info->si_signo);
221 tinfo->si_signo = sig;
222 tinfo->si_errno = 0;
223 tinfo->si_code = 0;
ths5fafdf22007-09-16 21:08:06 +0000224 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000225 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000226 /* should never come here, but who knows. The information for
227 the target is irrelevant */
228 tinfo->_sifields._sigfault._addr = 0;
ths7f7f7c82007-07-12 11:02:46 +0000229 } else if (sig == SIGIO) {
230 tinfo->_sifields._sigpoll._fd = info->si_fd;
bellard9de5e442003-03-23 16:49:39 +0000231 } else if (sig >= TARGET_SIGRTMIN) {
232 tinfo->_sifields._rt._pid = info->si_pid;
233 tinfo->_sifields._rt._uid = info->si_uid;
234 /* XXX: potential problem if 64 bit */
ths5fafdf22007-09-16 21:08:06 +0000235 tinfo->_sifields._rt._sigval.sival_ptr =
blueswir1992f48a2007-10-14 16:27:31 +0000236 (abi_ulong)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000237 }
bellard66fb9762003-03-23 01:06:05 +0000238}
239
ths5fafdf22007-09-16 21:08:06 +0000240static void tswap_siginfo(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000241 const target_siginfo_t *info)
242{
243 int sig;
244 sig = info->si_signo;
245 tinfo->si_signo = tswap32(sig);
246 tinfo->si_errno = tswap32(info->si_errno);
247 tinfo->si_code = tswap32(info->si_code);
ths5fafdf22007-09-16 21:08:06 +0000248 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000249 sig == SIGBUS || sig == SIGTRAP) {
ths5fafdf22007-09-16 21:08:06 +0000250 tinfo->_sifields._sigfault._addr =
bellard9de5e442003-03-23 16:49:39 +0000251 tswapl(info->_sifields._sigfault._addr);
ths7f7f7c82007-07-12 11:02:46 +0000252 } else if (sig == SIGIO) {
253 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
bellard9de5e442003-03-23 16:49:39 +0000254 } else if (sig >= TARGET_SIGRTMIN) {
255 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
256 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000257 tinfo->_sifields._rt._sigval.sival_ptr =
bellard9de5e442003-03-23 16:49:39 +0000258 tswapl(info->_sifields._rt._sigval.sival_ptr);
259 }
260}
261
262
263void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
264{
265 host_to_target_siginfo_noswap(tinfo, info);
266 tswap_siginfo(tinfo, tinfo);
267}
268
269/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000270/* XXX: find a solution for 64 bit (additional malloced data is needed) */
bellard9de5e442003-03-23 16:49:39 +0000271void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000272{
273 info->si_signo = tswap32(tinfo->si_signo);
274 info->si_errno = tswap32(tinfo->si_errno);
275 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000276 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
277 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000278 info->si_value.sival_ptr =
bellard9de5e442003-03-23 16:49:39 +0000279 (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000280}
281
bellard31e31b82003-02-18 22:55:36 +0000282void signal_init(void)
283{
284 struct sigaction act;
bellard9e5f5282003-07-13 17:33:54 +0000285 int i, j;
bellard31e31b82003-02-18 22:55:36 +0000286
bellard9e5f5282003-07-13 17:33:54 +0000287 /* generate signal conversion tables */
288 for(i = 1; i <= 64; i++) {
289 if (host_to_target_signal_table[i] == 0)
290 host_to_target_signal_table[i] = i;
291 }
292 for(i = 1; i <= 64; i++) {
293 j = host_to_target_signal_table[i];
294 target_to_host_signal_table[j] = i;
295 }
ths3b46e622007-09-17 08:09:54 +0000296
bellard9de5e442003-03-23 16:49:39 +0000297 /* set all host signal handlers. ALL signals are blocked during
298 the handlers to serialize them. */
299 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000300 act.sa_flags = SA_SIGINFO;
301 act.sa_sigaction = host_signal_handler;
302 for(i = 1; i < NSIG; i++) {
bellardc9087c22003-05-27 23:25:41 +0000303 sigaction(i, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000304 }
ths3b46e622007-09-17 08:09:54 +0000305
bellard31e31b82003-02-18 22:55:36 +0000306 memset(sigact_table, 0, sizeof(sigact_table));
bellard66fb9762003-03-23 01:06:05 +0000307
308 first_free = &sigqueue_table[0];
ths5fafdf22007-09-16 21:08:06 +0000309 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
bellard66fb9762003-03-23 01:06:05 +0000310 sigqueue_table[i].next = &sigqueue_table[i + 1];
311 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
bellard31e31b82003-02-18 22:55:36 +0000312}
313
bellard66fb9762003-03-23 01:06:05 +0000314/* signal queue handling */
315
316static inline struct sigqueue *alloc_sigqueue(void)
317{
318 struct sigqueue *q = first_free;
319 if (!q)
320 return NULL;
321 first_free = q->next;
322 return q;
323}
324
325static inline void free_sigqueue(struct sigqueue *q)
326{
327 q->next = first_free;
328 first_free = q;
329}
330
bellard9de5e442003-03-23 16:49:39 +0000331/* abort execution with signal */
332void __attribute((noreturn)) force_sig(int sig)
bellard66fb9762003-03-23 01:06:05 +0000333{
334 int host_sig;
bellard66fb9762003-03-23 01:06:05 +0000335 host_sig = target_to_host_signal(sig);
ths5fafdf22007-09-16 21:08:06 +0000336 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
bellard66fb9762003-03-23 01:06:05 +0000337 sig, strsignal(host_sig));
bellard9de5e442003-03-23 16:49:39 +0000338#if 1
bellard66fb9762003-03-23 01:06:05 +0000339 _exit(-host_sig);
bellard9de5e442003-03-23 16:49:39 +0000340#else
341 {
342 struct sigaction act;
343 sigemptyset(&act.sa_mask);
344 act.sa_flags = SA_SIGINFO;
345 act.sa_sigaction = SIG_DFL;
346 sigaction(SIGABRT, &act, NULL);
347 abort();
348 }
349#endif
bellard66fb9762003-03-23 01:06:05 +0000350}
351
bellard9de5e442003-03-23 16:49:39 +0000352/* queue a signal so that it will be send to the virtual CPU as soon
353 as possible */
354int queue_signal(int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000355{
bellard66fb9762003-03-23 01:06:05 +0000356 struct emulated_sigaction *k;
bellard9de5e442003-03-23 16:49:39 +0000357 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000358 abi_ulong handler;
bellard66fb9762003-03-23 01:06:05 +0000359
bellard9de5e442003-03-23 16:49:39 +0000360#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000361 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000362 sig);
bellard66fb9762003-03-23 01:06:05 +0000363#endif
bellard9de5e442003-03-23 16:49:39 +0000364 k = &sigact_table[sig - 1];
bellard66fb9762003-03-23 01:06:05 +0000365 handler = k->sa._sa_handler;
366 if (handler == TARGET_SIG_DFL) {
367 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000368 if (sig != TARGET_SIGCHLD &&
369 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +0000370 sig != TARGET_SIGWINCH) {
371 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000372 } else {
373 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000374 }
375 } else if (handler == TARGET_SIG_IGN) {
376 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000377 return 0;
bellard66fb9762003-03-23 01:06:05 +0000378 } else if (handler == TARGET_SIG_ERR) {
379 force_sig(sig);
380 } else {
bellard9de5e442003-03-23 16:49:39 +0000381 pq = &k->first;
382 if (sig < TARGET_SIGRTMIN) {
383 /* if non real time signal, we queue exactly one signal */
384 if (!k->pending)
385 q = &k->info;
386 else
387 return 0;
388 } else {
389 if (!k->pending) {
390 /* first signal */
391 q = &k->info;
392 } else {
393 q = alloc_sigqueue();
394 if (!q)
395 return -EAGAIN;
396 while (*pq != NULL)
397 pq = &(*pq)->next;
398 }
399 }
400 *pq = q;
401 q->info = *info;
402 q->next = NULL;
403 k->pending = 1;
404 /* signal that a new signal is pending */
405 signal_pending = 1;
406 return 1; /* indicates that the signal was queued */
407 }
408}
409
ths5fafdf22007-09-16 21:08:06 +0000410static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000411 void *puc)
412{
413 int sig;
414 target_siginfo_t tinfo;
415
416 /* the CPU emulator uses some host signals to detect exceptions,
417 we we forward to it some signals */
ths5fafdf22007-09-16 21:08:06 +0000418 if (host_signum == SIGSEGV || host_signum == SIGBUS
bellard58fe2f12004-02-16 22:11:32 +0000419#if defined(TARGET_I386) && defined(USE_CODE_COPY)
420 || host_signum == SIGFPE
421#endif
422 ) {
bellardb346ff42003-06-15 20:05:50 +0000423 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000424 return;
425 }
426
427 /* get target signal number */
428 sig = host_to_target_signal(host_signum);
429 if (sig < 1 || sig > TARGET_NSIG)
430 return;
431#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000432 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000433#endif
434 host_to_target_siginfo_noswap(&tinfo, info);
435 if (queue_signal(sig, &tinfo) == 1) {
436 /* interrupt the virtual CPU as soon as possible */
bellard68a79312003-06-30 13:12:32 +0000437 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
bellard66fb9762003-03-23 01:06:05 +0000438 }
bellard31e31b82003-02-18 22:55:36 +0000439}
440
ths0da46a62007-10-20 20:23:07 +0000441/* do_sigaltstack() returns target values and errnos. */
thsa04e1342007-09-27 13:57:58 +0000442int do_sigaltstack(const struct target_sigaltstack *uss,
443 struct target_sigaltstack *uoss,
blueswir1992f48a2007-10-14 16:27:31 +0000444 abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000445{
446 int ret;
447 struct target_sigaltstack oss;
448
449 /* XXX: test errors */
450 if(uoss)
451 {
452 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
453 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
454 __put_user(sas_ss_flags(sp), &oss.ss_flags);
455 }
456
457 if(uss)
458 {
459 struct target_sigaltstack ss;
460
ths0da46a62007-10-20 20:23:07 +0000461 ret = -TARGET_EFAULT;
thsa04e1342007-09-27 13:57:58 +0000462 if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
463 || __get_user(ss.ss_sp, &uss->ss_sp)
464 || __get_user(ss.ss_size, &uss->ss_size)
465 || __get_user(ss.ss_flags, &uss->ss_flags))
466 goto out;
467
ths0da46a62007-10-20 20:23:07 +0000468 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000469 if (on_sig_stack(sp))
470 goto out;
471
ths0da46a62007-10-20 20:23:07 +0000472 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000473 if (ss.ss_flags != TARGET_SS_DISABLE
474 && ss.ss_flags != TARGET_SS_ONSTACK
475 && ss.ss_flags != 0)
476 goto out;
477
478 if (ss.ss_flags == TARGET_SS_DISABLE) {
479 ss.ss_size = 0;
480 ss.ss_sp = 0;
481 } else {
ths0da46a62007-10-20 20:23:07 +0000482 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000483 if (ss.ss_size < MINSIGSTKSZ)
484 goto out;
485 }
486
487 target_sigaltstack_used.ss_sp = ss.ss_sp;
488 target_sigaltstack_used.ss_size = ss.ss_size;
489 }
490
491 if (uoss) {
ths0da46a62007-10-20 20:23:07 +0000492 ret = -TARGET_EFAULT;
thsa04e1342007-09-27 13:57:58 +0000493 if (!access_ok(VERIFY_WRITE, uoss, sizeof(oss)))
494 goto out;
495 memcpy(uoss, &oss, sizeof(oss));
496 }
497
498 ret = 0;
499out:
500 return ret;
501}
502
ths0da46a62007-10-20 20:23:07 +0000503/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000504int do_sigaction(int sig, const struct target_sigaction *act,
505 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000506{
bellard66fb9762003-03-23 01:06:05 +0000507 struct emulated_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000508 struct sigaction act1;
509 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000510 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000511
ths0aeaa8c2007-03-31 19:29:06 +0000512 if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000513 return -EINVAL;
514 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000515#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000516 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
bellard66fb9762003-03-23 01:06:05 +0000517 sig, (int)act, (int)oact);
518#endif
519 if (oact) {
520 oact->_sa_handler = tswapl(k->sa._sa_handler);
521 oact->sa_flags = tswapl(k->sa.sa_flags);
ths388bb212007-05-13 13:58:00 +0000522#if !defined(TARGET_MIPS)
523 oact->sa_restorer = tswapl(k->sa.sa_restorer);
524#endif
bellard66fb9762003-03-23 01:06:05 +0000525 oact->sa_mask = k->sa.sa_mask;
526 }
527 if (act) {
528 k->sa._sa_handler = tswapl(act->_sa_handler);
529 k->sa.sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000530#if !defined(TARGET_MIPS)
531 k->sa.sa_restorer = tswapl(act->sa_restorer);
532#endif
bellard66fb9762003-03-23 01:06:05 +0000533 k->sa.sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000534
535 /* we update the host linux signal state */
536 host_sig = target_to_host_signal(sig);
537 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
538 sigfillset(&act1.sa_mask);
539 act1.sa_flags = SA_SIGINFO;
540 if (k->sa.sa_flags & TARGET_SA_RESTART)
541 act1.sa_flags |= SA_RESTART;
542 /* NOTE: it is important to update the host kernel signal
543 ignore state to avoid getting unexpected interrupted
544 syscalls */
545 if (k->sa._sa_handler == TARGET_SIG_IGN) {
546 act1.sa_sigaction = (void *)SIG_IGN;
547 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
548 act1.sa_sigaction = (void *)SIG_DFL;
549 } else {
550 act1.sa_sigaction = host_signal_handler;
551 }
ths0da46a62007-10-20 20:23:07 +0000552 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000553 }
bellard66fb9762003-03-23 01:06:05 +0000554 }
ths0da46a62007-10-20 20:23:07 +0000555 return ret;
bellard66fb9762003-03-23 01:06:05 +0000556}
bellard31e31b82003-02-18 22:55:36 +0000557
bellard43fff232003-07-09 19:31:39 +0000558#ifndef offsetof
559#define offsetof(type, field) ((size_t) &((type *)0)->field)
560#endif
561
ths5fafdf22007-09-16 21:08:06 +0000562static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
bellard43fff232003-07-09 19:31:39 +0000563 const target_siginfo_t *info)
564{
565 tswap_siginfo(tinfo, info);
566 return 0;
567}
568
bellard66fb9762003-03-23 01:06:05 +0000569#ifdef TARGET_I386
570
571/* from the Linux kernel */
572
573struct target_fpreg {
574 uint16_t significand[4];
575 uint16_t exponent;
576};
577
578struct target_fpxreg {
579 uint16_t significand[4];
580 uint16_t exponent;
581 uint16_t padding[3];
582};
583
584struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000585 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000586};
587
588struct target_fpstate {
589 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000590 abi_ulong cw;
591 abi_ulong sw;
592 abi_ulong tag;
593 abi_ulong ipoff;
594 abi_ulong cssel;
595 abi_ulong dataoff;
596 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000597 struct target_fpreg _st[8];
598 uint16_t status;
599 uint16_t magic; /* 0xffff = regular FPU data only */
600
601 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000602 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
603 abi_ulong mxcsr;
604 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000605 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
606 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000607 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000608};
609
610#define X86_FXSR_MAGIC 0x0000
611
612struct target_sigcontext {
613 uint16_t gs, __gsh;
614 uint16_t fs, __fsh;
615 uint16_t es, __esh;
616 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000617 abi_ulong edi;
618 abi_ulong esi;
619 abi_ulong ebp;
620 abi_ulong esp;
621 abi_ulong ebx;
622 abi_ulong edx;
623 abi_ulong ecx;
624 abi_ulong eax;
625 abi_ulong trapno;
626 abi_ulong err;
627 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000628 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000629 abi_ulong eflags;
630 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000631 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000632 abi_ulong fpstate; /* pointer */
633 abi_ulong oldmask;
634 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000635};
636
bellard66fb9762003-03-23 01:06:05 +0000637struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000638 abi_ulong tuc_flags;
639 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +0000640 target_stack_t tuc_stack;
641 struct target_sigcontext tuc_mcontext;
642 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000643};
644
645struct sigframe
646{
blueswir1992f48a2007-10-14 16:27:31 +0000647 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000648 int sig;
649 struct target_sigcontext sc;
650 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000651 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000652 char retcode[8];
653};
654
655struct rt_sigframe
656{
blueswir1992f48a2007-10-14 16:27:31 +0000657 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000658 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000659 abi_ulong pinfo;
660 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000661 struct target_siginfo info;
662 struct target_ucontext uc;
663 struct target_fpstate fpstate;
664 char retcode[8];
665};
666
667/*
668 * Set up a signal frame.
669 */
670
bellard66fb9762003-03-23 01:06:05 +0000671/* XXX: save x87 state */
672static int
673setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
674 CPUX86State *env, unsigned long mask)
675{
676 int err = 0;
677
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
698 cpu_x86_fsave(env, (void *)fpstate, 1);
699 fpstate->status = fpstate->sw;
700 err |= __put_user(0xffff, &fpstate->magic);
701 err |= __put_user(fpstate, &sc->fpstate);
702
bellard66fb9762003-03-23 01:06:05 +0000703 /* non-iBCS2 extensions.. */
704 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000705 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000706 return err;
707}
708
709/*
710 * Determine which stack to use..
711 */
712
713static inline void *
714get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
715{
716 unsigned long esp;
717
718 /* Default to using normal stack */
719 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000720 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +0000721 if (ka->sa.sa_flags & TARGET_SA_ONSTACK) {
722 if (sas_ss_flags(esp) == 0)
723 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
724 }
bellard66fb9762003-03-23 01:06:05 +0000725
726 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000727 else
bellarda52c7572003-06-21 13:14:12 +0000728 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
729 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
730 ka->sa.sa_restorer) {
731 esp = (unsigned long) ka->sa.sa_restorer;
732 }
pbrook53a59602006-03-25 19:31:22 +0000733 return g2h((esp - frame_size) & -8ul);
bellard66fb9762003-03-23 01:06:05 +0000734}
735
bellard66fb9762003-03-23 01:06:05 +0000736static void setup_frame(int sig, struct emulated_sigaction *ka,
737 target_sigset_t *set, CPUX86State *env)
738{
739 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000740 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000741
742 frame = get_sigframe(ka, env, sizeof(*frame));
743
bellard66fb9762003-03-23 01:06:05 +0000744 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
745 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000746 err |= __put_user((/*current->exec_domain
747 && current->exec_domain->signal_invmap
748 && sig < 32
749 ? current->exec_domain->signal_invmap[sig]
750 : */ sig),
751 &frame->sig);
752 if (err)
753 goto give_sigsegv;
754
755 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
756 if (err)
757 goto give_sigsegv;
758
bellard92319442004-06-19 16:58:13 +0000759 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
760 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
761 goto give_sigsegv;
762 }
bellard66fb9762003-03-23 01:06:05 +0000763
764 /* Set up to return from userspace. If provided, use a stub
765 already in userspace. */
766 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
767 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
768 } else {
769 err |= __put_user(frame->retcode, &frame->pretcode);
770 /* This is popl %eax ; movl $,%eax ; int $0x80 */
771 err |= __put_user(0xb858, (short *)(frame->retcode+0));
j_mayer84409dd2007-04-06 08:56:50 +0000772#if defined(TARGET_X86_64)
773#warning "Fix this !"
774#else
bellard66fb9762003-03-23 01:06:05 +0000775 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
j_mayer84409dd2007-04-06 08:56:50 +0000776#endif
bellard66fb9762003-03-23 01:06:05 +0000777 err |= __put_user(0x80cd, (short *)(frame->retcode+6));
778 }
779
780 if (err)
781 goto give_sigsegv;
782
783 /* Set up registers for signal handler */
pbrook53a59602006-03-25 19:31:22 +0000784 env->regs[R_ESP] = h2g(frame);
bellard66fb9762003-03-23 01:06:05 +0000785 env->eip = (unsigned long) ka->sa._sa_handler;
786
787 cpu_x86_load_seg(env, R_DS, __USER_DS);
788 cpu_x86_load_seg(env, R_ES, __USER_DS);
789 cpu_x86_load_seg(env, R_SS, __USER_DS);
790 cpu_x86_load_seg(env, R_CS, __USER_CS);
791 env->eflags &= ~TF_MASK;
792
793 return;
794
795give_sigsegv:
796 if (sig == TARGET_SIGSEGV)
797 ka->sa._sa_handler = TARGET_SIG_DFL;
798 force_sig(TARGET_SIGSEGV /* , current */);
799}
800
ths5fafdf22007-09-16 21:08:06 +0000801static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard9de5e442003-03-23 16:49:39 +0000802 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000803 target_sigset_t *set, CPUX86State *env)
804{
805 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000806 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000807
808 frame = get_sigframe(ka, env, sizeof(*frame));
809
bellard66fb9762003-03-23 01:06:05 +0000810 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
811 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000812
813 err |= __put_user((/*current->exec_domain
814 && current->exec_domain->signal_invmap
815 && sig < 32
816 ? current->exec_domain->signal_invmap[sig]
817 : */sig),
818 &frame->sig);
blueswir1992f48a2007-10-14 16:27:31 +0000819 err |= __put_user((abi_ulong)&frame->info, &frame->pinfo);
820 err |= __put_user((abi_ulong)&frame->uc, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000821 err |= copy_siginfo_to_user(&frame->info, info);
822 if (err)
823 goto give_sigsegv;
824
825 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000826 err |= __put_user(0, &frame->uc.tuc_flags);
827 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000828 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000829 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000830 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000831 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000832 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000833 &frame->uc.tuc_stack.ss_size);
834 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard66fb9762003-03-23 01:06:05 +0000835 env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +0000836 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000837 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000838 goto give_sigsegv;
839 }
bellard66fb9762003-03-23 01:06:05 +0000840
841 /* Set up to return from userspace. If provided, use a stub
842 already in userspace. */
843 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
844 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
845 } else {
846 err |= __put_user(frame->retcode, &frame->pretcode);
847 /* This is movl $,%eax ; int $0x80 */
848 err |= __put_user(0xb8, (char *)(frame->retcode+0));
849 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
850 err |= __put_user(0x80cd, (short *)(frame->retcode+5));
851 }
852
853 if (err)
854 goto give_sigsegv;
855
856 /* Set up registers for signal handler */
857 env->regs[R_ESP] = (unsigned long) frame;
858 env->eip = (unsigned long) ka->sa._sa_handler;
859
860 cpu_x86_load_seg(env, R_DS, __USER_DS);
861 cpu_x86_load_seg(env, R_ES, __USER_DS);
862 cpu_x86_load_seg(env, R_SS, __USER_DS);
863 cpu_x86_load_seg(env, R_CS, __USER_CS);
864 env->eflags &= ~TF_MASK;
865
866 return;
867
868give_sigsegv:
869 if (sig == TARGET_SIGSEGV)
870 ka->sa._sa_handler = TARGET_SIG_DFL;
871 force_sig(TARGET_SIGSEGV /* , current */);
872}
873
874static int
875restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
876{
877 unsigned int err = 0;
878
bellard66fb9762003-03-23 01:06:05 +0000879 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
880 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
881 cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
882 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
883
884 env->regs[R_EDI] = ldl(&sc->edi);
885 env->regs[R_ESI] = ldl(&sc->esi);
886 env->regs[R_EBP] = ldl(&sc->ebp);
887 env->regs[R_ESP] = ldl(&sc->esp);
888 env->regs[R_EBX] = ldl(&sc->ebx);
889 env->regs[R_EDX] = ldl(&sc->edx);
890 env->regs[R_ECX] = ldl(&sc->ecx);
891 env->eip = ldl(&sc->eip);
892
893 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
894 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000895
bellard66fb9762003-03-23 01:06:05 +0000896 {
897 unsigned int tmpflags;
898 tmpflags = ldl(&sc->eflags);
899 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
900 // regs->orig_eax = -1; /* disable syscall checks */
901 }
902
bellard66fb9762003-03-23 01:06:05 +0000903 {
904 struct _fpstate * buf;
bellarded2dcdf2003-05-29 20:06:27 +0000905 buf = (void *)ldl(&sc->fpstate);
bellard66fb9762003-03-23 01:06:05 +0000906 if (buf) {
bellarded2dcdf2003-05-29 20:06:27 +0000907#if 0
bellard66fb9762003-03-23 01:06:05 +0000908 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
909 goto badframe;
bellarded2dcdf2003-05-29 20:06:27 +0000910#endif
911 cpu_x86_frstor(env, (void *)buf, 1);
bellard66fb9762003-03-23 01:06:05 +0000912 }
913 }
bellarded2dcdf2003-05-29 20:06:27 +0000914
bellard66fb9762003-03-23 01:06:05 +0000915 *peax = ldl(&sc->eax);
916 return err;
917#if 0
918badframe:
919 return 1;
920#endif
921}
922
923long do_sigreturn(CPUX86State *env)
924{
pbrook53a59602006-03-25 19:31:22 +0000925 struct sigframe *frame = (struct sigframe *)g2h(env->regs[R_ESP] - 8);
bellard66fb9762003-03-23 01:06:05 +0000926 target_sigset_t target_set;
927 sigset_t set;
928 int eax, i;
929
bellard447db212003-05-10 15:10:36 +0000930#if defined(DEBUG_SIGNAL)
931 fprintf(stderr, "do_sigreturn\n");
932#endif
bellard66fb9762003-03-23 01:06:05 +0000933 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +0000934 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
935 goto badframe;
936 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
937 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
938 goto badframe;
939 }
bellard66fb9762003-03-23 01:06:05 +0000940
bellard92319442004-06-19 16:58:13 +0000941 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +0000942 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +0000943
bellard66fb9762003-03-23 01:06:05 +0000944 /* restore registers */
945 if (restore_sigcontext(env, &frame->sc, &eax))
946 goto badframe;
947 return eax;
948
949badframe:
950 force_sig(TARGET_SIGSEGV);
951 return 0;
952}
953
954long do_rt_sigreturn(CPUX86State *env)
955{
pbrook53a59602006-03-25 19:31:22 +0000956 struct rt_sigframe *frame = (struct rt_sigframe *)g2h(env->regs[R_ESP] - 4);
bellard66fb9762003-03-23 01:06:05 +0000957 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +0000958 int eax;
959
960#if 0
961 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
962 goto badframe;
963#endif
bellardb8076a72005-04-07 22:20:31 +0000964 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +0000965 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +0000966
bellardb8076a72005-04-07 22:20:31 +0000967 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +0000968 goto badframe;
969
thsa04e1342007-09-27 13:57:58 +0000970 if (do_sigaltstack(&frame->uc.tuc_stack, NULL, get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +0000971 goto badframe;
thsa04e1342007-09-27 13:57:58 +0000972
bellard66fb9762003-03-23 01:06:05 +0000973 return eax;
974
975badframe:
976 force_sig(TARGET_SIGSEGV);
977 return 0;
978}
979
bellard43fff232003-07-09 19:31:39 +0000980#elif defined(TARGET_ARM)
981
982struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +0000983 abi_ulong trap_no;
984 abi_ulong error_code;
985 abi_ulong oldmask;
986 abi_ulong arm_r0;
987 abi_ulong arm_r1;
988 abi_ulong arm_r2;
989 abi_ulong arm_r3;
990 abi_ulong arm_r4;
991 abi_ulong arm_r5;
992 abi_ulong arm_r6;
993 abi_ulong arm_r7;
994 abi_ulong arm_r8;
995 abi_ulong arm_r9;
996 abi_ulong arm_r10;
997 abi_ulong arm_fp;
998 abi_ulong arm_ip;
999 abi_ulong arm_sp;
1000 abi_ulong arm_lr;
1001 abi_ulong arm_pc;
1002 abi_ulong arm_cpsr;
1003 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001004};
1005
bellard43fff232003-07-09 19:31:39 +00001006struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +00001007 abi_ulong tuc_flags;
1008 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +00001009 target_stack_t tuc_stack;
1010 struct target_sigcontext tuc_mcontext;
1011 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001012};
1013
1014struct sigframe
1015{
1016 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001017 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1018 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001019};
1020
1021struct rt_sigframe
1022{
1023 struct target_siginfo *pinfo;
1024 void *puc;
1025 struct target_siginfo info;
1026 struct target_ucontext uc;
blueswir1992f48a2007-10-14 16:27:31 +00001027 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001028};
1029
1030#define TARGET_CONFIG_CPU_32 1
1031
1032/*
1033 * For ARM syscalls, we encode the syscall number into the instruction.
1034 */
1035#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1036#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1037
1038/*
1039 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1040 * need two 16-bit instructions.
1041 */
1042#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1043#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1044
blueswir1992f48a2007-10-14 16:27:31 +00001045static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001046 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1047 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1048};
1049
1050
1051#define __put_user_error(x,p,e) __put_user(x, p)
1052#define __get_user_error(x,p,e) __get_user(x, p)
1053
1054static inline int valid_user_regs(CPUState *regs)
1055{
1056 return 1;
1057}
1058
1059static int
1060setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1061 CPUState *env, unsigned long mask)
1062{
1063 int err = 0;
1064
1065 __put_user_error(env->regs[0], &sc->arm_r0, err);
1066 __put_user_error(env->regs[1], &sc->arm_r1, err);
1067 __put_user_error(env->regs[2], &sc->arm_r2, err);
1068 __put_user_error(env->regs[3], &sc->arm_r3, err);
1069 __put_user_error(env->regs[4], &sc->arm_r4, err);
1070 __put_user_error(env->regs[5], &sc->arm_r5, err);
1071 __put_user_error(env->regs[6], &sc->arm_r6, err);
1072 __put_user_error(env->regs[7], &sc->arm_r7, err);
1073 __put_user_error(env->regs[8], &sc->arm_r8, err);
1074 __put_user_error(env->regs[9], &sc->arm_r9, err);
1075 __put_user_error(env->regs[10], &sc->arm_r10, err);
1076 __put_user_error(env->regs[11], &sc->arm_fp, err);
1077 __put_user_error(env->regs[12], &sc->arm_ip, err);
1078 __put_user_error(env->regs[13], &sc->arm_sp, err);
1079 __put_user_error(env->regs[14], &sc->arm_lr, err);
1080 __put_user_error(env->regs[15], &sc->arm_pc, err);
1081#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001082 __put_user_error(cpsr_read(env), &sc->arm_cpsr, err);
bellard43fff232003-07-09 19:31:39 +00001083#endif
1084
1085 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1086 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1087 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1088 __put_user_error(mask, &sc->oldmask, err);
1089
1090 return err;
1091}
1092
1093static inline void *
1094get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1095{
1096 unsigned long sp = regs->regs[13];
1097
bellard43fff232003-07-09 19:31:39 +00001098 /*
1099 * This is the X/Open sanctioned signal stack switching.
1100 */
thsa04e1342007-09-27 13:57:58 +00001101 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1102 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001103 /*
1104 * ATPCS B01 mandates 8-byte alignment
1105 */
pbrook53a59602006-03-25 19:31:22 +00001106 return g2h((sp - framesize) & ~7);
bellard43fff232003-07-09 19:31:39 +00001107}
1108
1109static int
1110setup_return(CPUState *env, struct emulated_sigaction *ka,
blueswir1992f48a2007-10-14 16:27:31 +00001111 abi_ulong *rc, void *frame, int usig)
bellard43fff232003-07-09 19:31:39 +00001112{
blueswir1992f48a2007-10-14 16:27:31 +00001113 abi_ulong handler = (abi_ulong)ka->sa._sa_handler;
1114 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001115 int thumb = 0;
1116#if defined(TARGET_CONFIG_CPU_32)
bellardb5ff1b32005-11-26 10:38:39 +00001117#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001118 abi_ulong cpsr = env->cpsr;
bellard43fff232003-07-09 19:31:39 +00001119
bellard43fff232003-07-09 19:31:39 +00001120 /*
1121 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1122 */
1123 if (ka->sa.sa_flags & SA_THIRTYTWO)
1124 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1125
1126#ifdef CONFIG_ARM_THUMB
1127 if (elf_hwcap & HWCAP_THUMB) {
1128 /*
1129 * The LSB of the handler determines if we're going to
1130 * be using THUMB or ARM mode for this signal handler.
1131 */
1132 thumb = handler & 1;
1133
1134 if (thumb)
1135 cpsr |= T_BIT;
1136 else
1137 cpsr &= ~T_BIT;
1138 }
thsa04e1342007-09-27 13:57:58 +00001139#endif /* CONFIG_ARM_THUMB */
1140#endif /* 0 */
bellard43fff232003-07-09 19:31:39 +00001141#endif /* TARGET_CONFIG_CPU_32 */
1142
1143 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
blueswir1992f48a2007-10-14 16:27:31 +00001144 retcode = (abi_ulong)ka->sa.sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001145 } else {
1146 unsigned int idx = thumb;
1147
1148 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1149 idx += 2;
1150
1151 if (__put_user(retcodes[idx], rc))
1152 return 1;
1153#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001154 flush_icache_range((abi_ulong)rc,
1155 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001156#endif
blueswir1992f48a2007-10-14 16:27:31 +00001157 retcode = ((abi_ulong)rc) + thumb;
bellard43fff232003-07-09 19:31:39 +00001158 }
1159
1160 env->regs[0] = usig;
pbrook53a59602006-03-25 19:31:22 +00001161 env->regs[13] = h2g(frame);
bellard43fff232003-07-09 19:31:39 +00001162 env->regs[14] = retcode;
1163 env->regs[15] = handler & (thumb ? ~1 : ~3);
1164
bellardb5ff1b32005-11-26 10:38:39 +00001165#if 0
bellard43fff232003-07-09 19:31:39 +00001166#ifdef TARGET_CONFIG_CPU_32
1167 env->cpsr = cpsr;
1168#endif
bellardb5ff1b32005-11-26 10:38:39 +00001169#endif
bellard43fff232003-07-09 19:31:39 +00001170
1171 return 0;
1172}
1173
1174static void setup_frame(int usig, struct emulated_sigaction *ka,
1175 target_sigset_t *set, CPUState *regs)
1176{
1177 struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
bellard92319442004-06-19 16:58:13 +00001178 int i, err = 0;
bellard43fff232003-07-09 19:31:39 +00001179
1180 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1181
bellard92319442004-06-19 16:58:13 +00001182 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1183 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1184 return;
bellard43fff232003-07-09 19:31:39 +00001185 }
1186
1187 if (err == 0)
1188 err = setup_return(regs, ka, &frame->retcode, frame, usig);
1189 // return err;
1190}
1191
ths5fafdf22007-09-16 21:08:06 +00001192static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
bellard43fff232003-07-09 19:31:39 +00001193 target_siginfo_t *info,
1194 target_sigset_t *set, CPUState *env)
1195{
1196 struct rt_sigframe *frame = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001197 struct target_sigaltstack stack;
bellard92319442004-06-19 16:58:13 +00001198 int i, err = 0;
bellard43fff232003-07-09 19:31:39 +00001199
bellard43fff232003-07-09 19:31:39 +00001200 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
bellardedf779f2004-02-22 13:40:13 +00001201 return /* 1 */;
1202
blueswir1992f48a2007-10-14 16:27:31 +00001203 __put_user_error(&frame->info, (abi_ulong *)&frame->pinfo, err);
1204 __put_user_error(&frame->uc, (abi_ulong *)&frame->puc, err);
bellard43fff232003-07-09 19:31:39 +00001205 err |= copy_siginfo_to_user(&frame->info, info);
1206
1207 /* Clear all the bits of the ucontext we don't use. */
pbrook53a59602006-03-25 19:31:22 +00001208 memset(&frame->uc, 0, offsetof(struct target_ucontext, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001209
thsa04e1342007-09-27 13:57:58 +00001210 memset(&stack, 0, sizeof(stack));
1211 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1212 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1213 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1214 if (!access_ok(VERIFY_WRITE, &frame->uc.tuc_stack, sizeof(stack)))
1215 err = 1;
1216 else
1217 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1218
bellardb8076a72005-04-07 22:20:31 +00001219 err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
bellard43fff232003-07-09 19:31:39 +00001220 env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001221 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001222 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +00001223 return;
1224 }
bellard43fff232003-07-09 19:31:39 +00001225
1226 if (err == 0)
1227 err = setup_return(env, ka, &frame->retcode, frame, usig);
1228
1229 if (err == 0) {
1230 /*
1231 * For realtime signals we must also set the second and third
1232 * arguments for the signal handler.
1233 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1234 */
blueswir1992f48a2007-10-14 16:27:31 +00001235 env->regs[1] = (abi_ulong)frame->pinfo;
1236 env->regs[2] = (abi_ulong)frame->puc;
bellard43fff232003-07-09 19:31:39 +00001237 }
1238
1239 // return err;
1240}
1241
1242static int
1243restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1244{
1245 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001246 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001247
1248 __get_user_error(env->regs[0], &sc->arm_r0, err);
1249 __get_user_error(env->regs[1], &sc->arm_r1, err);
1250 __get_user_error(env->regs[2], &sc->arm_r2, err);
1251 __get_user_error(env->regs[3], &sc->arm_r3, err);
1252 __get_user_error(env->regs[4], &sc->arm_r4, err);
1253 __get_user_error(env->regs[5], &sc->arm_r5, err);
1254 __get_user_error(env->regs[6], &sc->arm_r6, err);
1255 __get_user_error(env->regs[7], &sc->arm_r7, err);
1256 __get_user_error(env->regs[8], &sc->arm_r8, err);
1257 __get_user_error(env->regs[9], &sc->arm_r9, err);
1258 __get_user_error(env->regs[10], &sc->arm_r10, err);
1259 __get_user_error(env->regs[11], &sc->arm_fp, err);
1260 __get_user_error(env->regs[12], &sc->arm_ip, err);
1261 __get_user_error(env->regs[13], &sc->arm_sp, err);
1262 __get_user_error(env->regs[14], &sc->arm_lr, err);
1263 __get_user_error(env->regs[15], &sc->arm_pc, err);
1264#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001265 __get_user_error(cpsr, &sc->arm_cpsr, err);
1266 cpsr_write(env, cpsr, 0xffffffff);
bellard43fff232003-07-09 19:31:39 +00001267#endif
1268
1269 err |= !valid_user_regs(env);
1270
1271 return err;
1272}
1273
1274long do_sigreturn(CPUState *env)
1275{
1276 struct sigframe *frame;
1277 target_sigset_t set;
1278 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001279 int i;
bellard43fff232003-07-09 19:31:39 +00001280
1281 /*
1282 * Since we stacked the signal on a 64-bit boundary,
1283 * then 'sp' should be word aligned here. If it's
1284 * not, then the user is trying to mess with us.
1285 */
1286 if (env->regs[13] & 7)
1287 goto badframe;
1288
pbrook53a59602006-03-25 19:31:22 +00001289 frame = (struct sigframe *)g2h(env->regs[13]);
bellard43fff232003-07-09 19:31:39 +00001290
1291#if 0
1292 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1293 goto badframe;
1294#endif
bellard92319442004-06-19 16:58:13 +00001295 if (__get_user(set.sig[0], &frame->sc.oldmask))
1296 goto badframe;
1297 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1298 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1299 goto badframe;
1300 }
bellard43fff232003-07-09 19:31:39 +00001301
bellard92319442004-06-19 16:58:13 +00001302 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001303 sigprocmask(SIG_SETMASK, &host_set, NULL);
1304
1305 if (restore_sigcontext(env, &frame->sc))
1306 goto badframe;
1307
1308#if 0
1309 /* Send SIGTRAP if we're single-stepping */
1310 if (ptrace_cancel_bpt(current))
1311 send_sig(SIGTRAP, current, 1);
1312#endif
1313 return env->regs[0];
1314
1315badframe:
1316 force_sig(SIGSEGV /* , current */);
1317 return 0;
1318}
1319
1320long do_rt_sigreturn(CPUState *env)
1321{
1322 struct rt_sigframe *frame;
bellard43fff232003-07-09 19:31:39 +00001323 sigset_t host_set;
1324
1325 /*
1326 * Since we stacked the signal on a 64-bit boundary,
1327 * then 'sp' should be word aligned here. If it's
1328 * not, then the user is trying to mess with us.
1329 */
1330 if (env->regs[13] & 7)
1331 goto badframe;
1332
1333 frame = (struct rt_sigframe *)env->regs[13];
1334
1335#if 0
1336 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1337 goto badframe;
1338#endif
bellardb8076a72005-04-07 22:20:31 +00001339 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001340 sigprocmask(SIG_SETMASK, &host_set, NULL);
1341
bellardb8076a72005-04-07 22:20:31 +00001342 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001343 goto badframe;
1344
thsa04e1342007-09-27 13:57:58 +00001345 if (do_sigaltstack(&frame->uc.tuc_stack, NULL, get_sp_from_cpustate(env)) == -EFAULT)
1346 goto badframe;
1347
bellard43fff232003-07-09 19:31:39 +00001348#if 0
1349 /* Send SIGTRAP if we're single-stepping */
1350 if (ptrace_cancel_bpt(current))
1351 send_sig(SIGTRAP, current, 1);
1352#endif
1353 return env->regs[0];
1354
1355badframe:
1356 force_sig(SIGSEGV /* , current */);
1357 return 0;
1358}
1359
bellard6d5e2162004-09-30 22:04:13 +00001360#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001361
bellard6d5e2162004-09-30 22:04:13 +00001362#define __SUNOS_MAXWIN 31
1363
1364/* This is what SunOS does, so shall I. */
1365struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001366 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001367
blueswir1992f48a2007-10-14 16:27:31 +00001368 abi_ulong sigc_mask; /* sigmask to restore */
1369 abi_ulong sigc_sp; /* stack pointer */
1370 abi_ulong sigc_pc; /* program counter */
1371 abi_ulong sigc_npc; /* next program counter */
1372 abi_ulong sigc_psr; /* for condition codes etc */
1373 abi_ulong sigc_g1; /* User uses these two registers */
1374 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001375
1376 /* Now comes information regarding the users window set
1377 * at the time of the signal.
1378 */
blueswir1992f48a2007-10-14 16:27:31 +00001379 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001380
1381 /* stack ptrs for each regwin buf */
1382 char *sigc_spbuf[__SUNOS_MAXWIN];
1383
1384 /* Windows to restore after signal */
1385 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001386 abi_ulong locals[8];
1387 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001388 } sigc_wbuf[__SUNOS_MAXWIN];
1389};
1390/* A Sparc stack frame */
1391struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001392 abi_ulong locals[8];
1393 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001394 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001395 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001396 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001397 abi_ulong xargs[6];
1398 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001399};
1400
1401typedef struct {
1402 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001403 abi_ulong psr;
1404 abi_ulong pc;
1405 abi_ulong npc;
1406 abi_ulong y;
1407 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001408 } si_regs;
1409 int si_mask;
1410} __siginfo_t;
1411
1412typedef struct {
1413 unsigned long si_float_regs [32];
1414 unsigned long si_fsr;
1415 unsigned long si_fpqdepth;
1416 struct {
1417 unsigned long *insn_addr;
1418 unsigned long insn;
1419 } si_fpqueue [16];
bellard74ccb342006-07-18 21:23:34 +00001420} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001421
1422
1423struct target_signal_frame {
1424 struct sparc_stackf ss;
1425 __siginfo_t info;
bellard74ccb342006-07-18 21:23:34 +00001426 qemu_siginfo_fpu_t *fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001427 abi_ulong insns[2] __attribute__ ((aligned (8)));
1428 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1429 abi_ulong extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001430 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001431};
1432struct target_rt_signal_frame {
1433 struct sparc_stackf ss;
1434 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001435 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001436 sigset_t mask;
bellard74ccb342006-07-18 21:23:34 +00001437 qemu_siginfo_fpu_t *fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001438 unsigned int insns[2];
1439 stack_t stack;
1440 unsigned int extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001441 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001442};
1443
bellarde80cfcf2004-12-19 23:18:01 +00001444#define UREG_O0 16
1445#define UREG_O6 22
1446#define UREG_I0 0
1447#define UREG_I1 1
1448#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001449#define UREG_I3 3
1450#define UREG_I4 4
1451#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001452#define UREG_I6 6
1453#define UREG_I7 7
1454#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001455#define UREG_FP UREG_I6
1456#define UREG_SP UREG_O6
1457
1458static inline void *get_sigframe(struct emulated_sigaction *sa, CPUState *env, unsigned long framesize)
1459{
1460 unsigned long sp;
1461
1462 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001463
1464 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00001465 if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1466 if (!on_sig_stack(sp)
1467 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1468 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001469 }
pbrook53a59602006-03-25 19:31:22 +00001470 return g2h(sp - framesize);
bellard6d5e2162004-09-30 22:04:13 +00001471}
1472
1473static int
blueswir1992f48a2007-10-14 16:27:31 +00001474setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001475{
1476 int err = 0, i;
1477
bellard6d5e2162004-09-30 22:04:13 +00001478 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001479 err |= __put_user(env->pc, &si->si_regs.pc);
1480 err |= __put_user(env->npc, &si->si_regs.npc);
1481 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001482 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001483 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1484 }
bellarda315a142005-01-30 22:59:18 +00001485 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001486 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001487 }
bellard6d5e2162004-09-30 22:04:13 +00001488 err |= __put_user(mask, &si->si_mask);
1489 return err;
1490}
bellarde80cfcf2004-12-19 23:18:01 +00001491
bellard80a9d032005-01-03 23:31:27 +00001492#if 0
bellard6d5e2162004-09-30 22:04:13 +00001493static int
1494setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1495 CPUState *env, unsigned long mask)
1496{
1497 int err = 0;
1498
1499 err |= __put_user(mask, &sc->sigc_mask);
1500 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1501 err |= __put_user(env->pc, &sc->sigc_pc);
1502 err |= __put_user(env->npc, &sc->sigc_npc);
1503 err |= __put_user(env->psr, &sc->sigc_psr);
1504 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1505 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1506
1507 return err;
1508}
bellard80a9d032005-01-03 23:31:27 +00001509#endif
bellard6d5e2162004-09-30 22:04:13 +00001510#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1511
1512static void setup_frame(int sig, struct emulated_sigaction *ka,
1513 target_sigset_t *set, CPUState *env)
1514{
1515 struct target_signal_frame *sf;
1516 int sigframe_size, err, i;
1517
1518 /* 1. Make sure everything is clean */
1519 //synchronize_user_stack();
1520
1521 sigframe_size = NF_ALIGNEDSZ;
1522
1523 sf = (struct target_signal_frame *)
1524 get_sigframe(ka, env, sigframe_size);
1525
bellarde80cfcf2004-12-19 23:18:01 +00001526 //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 +00001527#if 0
1528 if (invalid_frame_pointer(sf, sigframe_size))
1529 goto sigill_and_return;
1530#endif
1531 /* 2. Save the current process state */
1532 err = setup___siginfo(&sf->info, env, set->sig[0]);
1533 err |= __put_user(0, &sf->extra_size);
1534
1535 //err |= save_fpu_state(regs, &sf->fpu_state);
1536 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1537
1538 err |= __put_user(set->sig[0], &sf->info.si_mask);
1539 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1540 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1541 }
1542
bellarda315a142005-01-30 22:59:18 +00001543 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001544 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001545 }
bellarda315a142005-01-30 22:59:18 +00001546 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001547 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001548 }
bellard6d5e2162004-09-30 22:04:13 +00001549 if (err)
1550 goto sigsegv;
1551
1552 /* 3. signal handler back-trampoline and parameters */
pbrook53a59602006-03-25 19:31:22 +00001553 env->regwptr[UREG_FP] = h2g(sf);
bellard6d5e2162004-09-30 22:04:13 +00001554 env->regwptr[UREG_I0] = sig;
pbrook53a59602006-03-25 19:31:22 +00001555 env->regwptr[UREG_I1] = h2g(&sf->info);
1556 env->regwptr[UREG_I2] = h2g(&sf->info);
bellard6d5e2162004-09-30 22:04:13 +00001557
1558 /* 4. signal handler */
1559 env->pc = (unsigned long) ka->sa._sa_handler;
1560 env->npc = (env->pc + 4);
1561 /* 5. return to kernel instructions */
1562 if (ka->sa.sa_restorer)
1563 env->regwptr[UREG_I7] = (unsigned long)ka->sa.sa_restorer;
1564 else {
pbrook53a59602006-03-25 19:31:22 +00001565 env->regwptr[UREG_I7] = h2g(&(sf->insns[0]) - 2);
bellard6d5e2162004-09-30 22:04:13 +00001566
1567 /* mov __NR_sigreturn, %g1 */
1568 err |= __put_user(0x821020d8, &sf->insns[0]);
1569
1570 /* t 0x10 */
1571 err |= __put_user(0x91d02010, &sf->insns[1]);
1572 if (err)
1573 goto sigsegv;
1574
1575 /* Flush instruction space. */
1576 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001577 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001578 }
1579 return;
1580
bellard80a9d032005-01-03 23:31:27 +00001581 //sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001582 force_sig(TARGET_SIGILL);
1583sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001584 //fprintf(stderr, "force_sig\n");
bellard6d5e2162004-09-30 22:04:13 +00001585 force_sig(TARGET_SIGSEGV);
1586}
1587static inline int
bellard74ccb342006-07-18 21:23:34 +00001588restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001589{
1590 int err;
1591#if 0
1592#ifdef CONFIG_SMP
1593 if (current->flags & PF_USEDFPU)
1594 regs->psr &= ~PSR_EF;
1595#else
1596 if (current == last_task_used_math) {
1597 last_task_used_math = 0;
1598 regs->psr &= ~PSR_EF;
1599 }
1600#endif
1601 current->used_math = 1;
1602 current->flags &= ~PF_USEDFPU;
1603#endif
1604#if 0
1605 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1606 return -EFAULT;
1607#endif
1608
bellardfafffae2006-10-28 12:09:16 +00001609#if 0
1610 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001611 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1612 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001613#endif
bellard6d5e2162004-09-30 22:04:13 +00001614 err |= __get_user(env->fsr, &fpu->si_fsr);
1615#if 0
1616 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1617 if (current->thread.fpqdepth != 0)
1618 err |= __copy_from_user(&current->thread.fpqueue[0],
1619 &fpu->si_fpqueue[0],
1620 ((sizeof(unsigned long) +
1621 (sizeof(unsigned long *)))*16));
1622#endif
1623 return err;
1624}
1625
1626
ths5fafdf22007-09-16 21:08:06 +00001627static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001628 target_siginfo_t *info,
1629 target_sigset_t *set, CPUState *env)
1630{
1631 fprintf(stderr, "setup_rt_frame: not implemented\n");
1632}
1633
1634long do_sigreturn(CPUState *env)
1635{
1636 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001637 uint32_t up_psr, pc, npc;
bellard6d5e2162004-09-30 22:04:13 +00001638 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001639 sigset_t host_set;
blueswir1992f48a2007-10-14 16:27:31 +00001640 abi_ulong fpu_save;
bellarde80cfcf2004-12-19 23:18:01 +00001641 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001642
pbrook53a59602006-03-25 19:31:22 +00001643 sf = (struct target_signal_frame *)g2h(env->regwptr[UREG_FP]);
bellard80a9d032005-01-03 23:31:27 +00001644#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001645 fprintf(stderr, "sigreturn\n");
1646 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 +00001647#endif
bellarde80cfcf2004-12-19 23:18:01 +00001648 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001649
1650 /* 1. Make sure we are not getting garbage from the user */
1651#if 0
1652 if (verify_area (VERIFY_READ, sf, sizeof (*sf)))
1653 goto segv_and_exit;
1654#endif
1655
1656 if (((uint) sf) & 3)
1657 goto segv_and_exit;
1658
1659 err = __get_user(pc, &sf->info.si_regs.pc);
1660 err |= __get_user(npc, &sf->info.si_regs.npc);
1661
bellard6d5e2162004-09-30 22:04:13 +00001662 if ((pc | npc) & 3)
1663 goto segv_and_exit;
1664
1665 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001666 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1667
bellard6d5e2162004-09-30 22:04:13 +00001668 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001669 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1670 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1671
1672 env->pc = pc;
1673 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001674 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001675 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001676 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1677 }
bellarda315a142005-01-30 22:59:18 +00001678 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001679 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1680 }
bellard6d5e2162004-09-30 22:04:13 +00001681
blueswir1992f48a2007-10-14 16:27:31 +00001682 err |= __get_user(fpu_save, (abi_ulong *)&sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001683
bellarde80cfcf2004-12-19 23:18:01 +00001684 //if (fpu_save)
1685 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001686
1687 /* This is pretty much atomic, no amount locking would prevent
1688 * the races which exist anyways.
1689 */
1690 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001691 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1692 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1693 }
1694
1695 target_to_host_sigset_internal(&host_set, &set);
1696 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001697
1698 if (err)
1699 goto segv_and_exit;
1700
bellard6d5e2162004-09-30 22:04:13 +00001701 return env->regwptr[0];
1702
1703segv_and_exit:
1704 force_sig(TARGET_SIGSEGV);
1705}
1706
1707long do_rt_sigreturn(CPUState *env)
1708{
1709 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1710 return -ENOSYS;
1711}
1712
blueswir15bfb56b2007-10-05 17:01:51 +00001713#ifdef TARGET_SPARC64
1714#define MC_TSTATE 0
1715#define MC_PC 1
1716#define MC_NPC 2
1717#define MC_Y 3
1718#define MC_G1 4
1719#define MC_G2 5
1720#define MC_G3 6
1721#define MC_G4 7
1722#define MC_G5 8
1723#define MC_G6 9
1724#define MC_G7 10
1725#define MC_O0 11
1726#define MC_O1 12
1727#define MC_O2 13
1728#define MC_O3 14
1729#define MC_O4 15
1730#define MC_O5 16
1731#define MC_O6 17
1732#define MC_O7 18
1733#define MC_NGREG 19
1734
blueswir1992f48a2007-10-14 16:27:31 +00001735typedef abi_ulong target_mc_greg_t;
blueswir15bfb56b2007-10-05 17:01:51 +00001736typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1737
1738struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00001739 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001740 uint32_t mcfq_insn;
1741};
1742
1743struct target_mc_fpu {
1744 union {
1745 uint32_t sregs[32];
1746 uint64_t dregs[32];
1747 //uint128_t qregs[16];
1748 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00001749 abi_ulong mcfpu_fsr;
1750 abi_ulong mcfpu_fprs;
1751 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00001752 struct target_mc_fq *mcfpu_fq;
1753 unsigned char mcfpu_qcnt;
1754 unsigned char mcfpu_qentsz;
1755 unsigned char mcfpu_enab;
1756};
1757typedef struct target_mc_fpu target_mc_fpu_t;
1758
1759typedef struct {
1760 target_mc_gregset_t mc_gregs;
1761 target_mc_greg_t mc_fp;
1762 target_mc_greg_t mc_i7;
1763 target_mc_fpu_t mc_fpregs;
1764} target_mcontext_t;
1765
1766struct target_ucontext {
1767 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00001768 abi_ulong uc_flags;
blueswir15bfb56b2007-10-05 17:01:51 +00001769 target_sigset_t uc_sigmask;
1770 target_mcontext_t uc_mcontext;
1771};
1772
1773/* A V9 register window */
1774struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00001775 abi_ulong locals[8];
1776 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00001777};
1778
1779#define TARGET_STACK_BIAS 2047
1780
1781/* {set, get}context() needed for 64-bit SparcLinux userland. */
1782void sparc64_set_context(CPUSPARCState *env)
1783{
1784 struct target_ucontext *ucp = (struct target_ucontext *)
1785 env->regwptr[UREG_I0];
1786 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00001787 abi_ulong pc, npc, tstate;
1788 abi_ulong fp, i7;
blueswir15bfb56b2007-10-05 17:01:51 +00001789 unsigned char fenab;
1790 int err;
1791 unsigned int i;
blueswir1992f48a2007-10-14 16:27:31 +00001792 abi_ulong *src, *dst;
blueswir15bfb56b2007-10-05 17:01:51 +00001793
1794 grp = &ucp->uc_mcontext.mc_gregs;
1795 err = get_user(pc, &((*grp)[MC_PC]));
1796 err |= get_user(npc, &((*grp)[MC_NPC]));
1797 if (err || ((pc | npc) & 3))
1798 goto do_sigsegv;
1799 if (env->regwptr[UREG_I1]) {
1800 target_sigset_t target_set;
1801 sigset_t set;
1802
1803 if (TARGET_NSIG_WORDS == 1) {
1804 if (get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
1805 goto do_sigsegv;
1806 } else {
1807 src = &ucp->uc_sigmask;
1808 dst = &target_set;
blueswir1992f48a2007-10-14 16:27:31 +00001809 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00001810 i++, dst++, src++)
1811 err |= get_user(dst, src);
1812 if (err)
1813 goto do_sigsegv;
1814 }
1815 target_to_host_sigset_internal(&set, &target_set);
1816 sigprocmask(SIG_SETMASK, &set, NULL);
1817 }
1818 env->pc = pc;
1819 env->npc = npc;
1820 err |= get_user(env->y, &((*grp)[MC_Y]));
1821 err |= get_user(tstate, &((*grp)[MC_TSTATE]));
1822 env->asi = (tstate >> 24) & 0xff;
1823 PUT_CCR(env, tstate >> 32);
1824 PUT_CWP64(env, tstate & 0x1f);
1825 err |= get_user(env->gregs[1], (&(*grp)[MC_G1]));
1826 err |= get_user(env->gregs[2], (&(*grp)[MC_G2]));
1827 err |= get_user(env->gregs[3], (&(*grp)[MC_G3]));
1828 err |= get_user(env->gregs[4], (&(*grp)[MC_G4]));
1829 err |= get_user(env->gregs[5], (&(*grp)[MC_G5]));
1830 err |= get_user(env->gregs[6], (&(*grp)[MC_G6]));
1831 err |= get_user(env->gregs[7], (&(*grp)[MC_G7]));
1832 err |= get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
1833 err |= get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
1834 err |= get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
1835 err |= get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
1836 err |= get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
1837 err |= get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
1838 err |= get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
1839 err |= get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
1840
1841 err |= get_user(fp, &(ucp->uc_mcontext.mc_fp));
1842 err |= get_user(i7, &(ucp->uc_mcontext.mc_i7));
1843 err |= put_user(fp,
1844 (&(((struct target_reg_window *)(TARGET_STACK_BIAS+env->regwptr[UREG_I6]))->ins[6])));
1845 err |= put_user(i7,
1846 (&(((struct target_reg_window *)(TARGET_STACK_BIAS+env->regwptr[UREG_I6]))->ins[7])));
1847
1848 err |= get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
1849 err |= get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
1850 src = &(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs);
1851 dst = &env->fpr;
1852 for (i = 0; i < 64; i++, dst++, src++)
1853 err |= get_user(dst, src);
1854 err |= get_user(env->fsr,
1855 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
1856 err |= get_user(env->gsr,
1857 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
1858 if (err)
1859 goto do_sigsegv;
1860
1861 return;
1862 do_sigsegv:
1863 force_sig(SIGSEGV);
1864}
1865
1866void sparc64_get_context(CPUSPARCState *env)
1867{
1868 struct target_ucontext *ucp = (struct target_ucontext *)
1869 env->regwptr[UREG_I0];
1870 target_mc_gregset_t *grp;
1871 target_mcontext_t *mcp;
blueswir1992f48a2007-10-14 16:27:31 +00001872 abi_ulong fp, i7;
blueswir15bfb56b2007-10-05 17:01:51 +00001873 int err;
1874 unsigned int i;
blueswir1992f48a2007-10-14 16:27:31 +00001875 abi_ulong *src, *dst;
blueswir15bfb56b2007-10-05 17:01:51 +00001876 target_sigset_t target_set;
1877 sigset_t set;
1878
1879 mcp = &ucp->uc_mcontext;
1880 grp = &mcp->mc_gregs;
1881
1882 /* Skip over the trap instruction, first. */
1883 env->pc = env->npc;
1884 env->npc += 4;
1885
1886 err = 0;
1887
1888 sigprocmask(0, NULL, &set);
1889 host_to_target_sigset_internal(&target_set, &set);
1890 if (TARGET_NSIG_WORDS == 1)
1891 err |= put_user(target_set.sig[0],
blueswir1992f48a2007-10-14 16:27:31 +00001892 (abi_ulong *)&ucp->uc_sigmask);
blueswir15bfb56b2007-10-05 17:01:51 +00001893 else {
1894 src = &target_set;
1895 dst = &ucp->uc_sigmask;
blueswir1992f48a2007-10-14 16:27:31 +00001896 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00001897 i++, dst++, src++)
1898 err |= put_user(src, dst);
1899 if (err)
1900 goto do_sigsegv;
1901 }
1902
1903 err |= put_user(env->tstate, &((*grp)[MC_TSTATE]));
1904 err |= put_user(env->pc, &((*grp)[MC_PC]));
1905 err |= put_user(env->npc, &((*grp)[MC_NPC]));
1906 err |= put_user(env->y, &((*grp)[MC_Y]));
1907 err |= put_user(env->gregs[1], &((*grp)[MC_G1]));
1908 err |= put_user(env->gregs[2], &((*grp)[MC_G2]));
1909 err |= put_user(env->gregs[3], &((*grp)[MC_G3]));
1910 err |= put_user(env->gregs[4], &((*grp)[MC_G4]));
1911 err |= put_user(env->gregs[5], &((*grp)[MC_G5]));
1912 err |= put_user(env->gregs[6], &((*grp)[MC_G6]));
1913 err |= put_user(env->gregs[7], &((*grp)[MC_G7]));
1914 err |= put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
1915 err |= put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
1916 err |= put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
1917 err |= put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
1918 err |= put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
1919 err |= put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
1920 err |= put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
1921 err |= put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
1922
1923 err |= get_user(fp,
1924 (&(((struct target_reg_window *)(TARGET_STACK_BIAS+env->regwptr[UREG_I6]))->ins[6])));
1925 err |= get_user(i7,
1926 (&(((struct target_reg_window *)(TARGET_STACK_BIAS+env->regwptr[UREG_I6]))->ins[7])));
1927 err |= put_user(fp, &(mcp->mc_fp));
1928 err |= put_user(i7, &(mcp->mc_i7));
1929
1930 src = &env->fpr;
1931 dst = &(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs);
1932 for (i = 0; i < 64; i++, dst++, src++)
1933 err |= put_user(src, dst);
1934 err |= put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
1935 err |= put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
1936 err |= put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
1937
1938 if (err)
1939 goto do_sigsegv;
1940
1941 return;
1942 do_sigsegv:
1943 force_sig(SIGSEGV);
1944}
1945#endif
ths540635b2007-09-30 01:58:33 +00001946#elif defined(TARGET_MIPS64)
1947
1948# warning signal handling not implemented
1949
1950static void setup_frame(int sig, struct emulated_sigaction *ka,
1951 target_sigset_t *set, CPUState *env)
1952{
1953 fprintf(stderr, "setup_frame: not implemented\n");
1954}
1955
1956static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1957 target_siginfo_t *info,
1958 target_sigset_t *set, CPUState *env)
1959{
1960 fprintf(stderr, "setup_rt_frame: not implemented\n");
1961}
1962
1963long do_sigreturn(CPUState *env)
1964{
1965 fprintf(stderr, "do_sigreturn: not implemented\n");
1966 return -ENOSYS;
1967}
1968
1969long do_rt_sigreturn(CPUState *env)
1970{
1971 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1972 return -ENOSYS;
1973}
1974
1975#elif defined(TARGET_MIPSN32)
1976
1977# warning signal handling not implemented
1978
1979static void setup_frame(int sig, struct emulated_sigaction *ka,
1980 target_sigset_t *set, CPUState *env)
1981{
1982 fprintf(stderr, "setup_frame: not implemented\n");
1983}
1984
1985static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1986 target_siginfo_t *info,
1987 target_sigset_t *set, CPUState *env)
1988{
1989 fprintf(stderr, "setup_rt_frame: not implemented\n");
1990}
1991
1992long do_sigreturn(CPUState *env)
1993{
1994 fprintf(stderr, "do_sigreturn: not implemented\n");
1995 return -ENOSYS;
1996}
1997
1998long do_rt_sigreturn(CPUState *env)
1999{
2000 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2001 return -ENOSYS;
2002}
2003
bellard106ec872006-06-27 21:08:10 +00002004#elif defined(TARGET_MIPS)
2005
2006struct target_sigcontext {
2007 uint32_t sc_regmask; /* Unused */
2008 uint32_t sc_status;
2009 uint64_t sc_pc;
2010 uint64_t sc_regs[32];
2011 uint64_t sc_fpregs[32];
2012 uint32_t sc_ownedfp; /* Unused */
2013 uint32_t sc_fpc_csr;
2014 uint32_t sc_fpc_eir; /* Unused */
2015 uint32_t sc_used_math;
2016 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2017 uint64_t sc_mdhi;
2018 uint64_t sc_mdlo;
2019 target_ulong sc_hi1; /* Was sc_cause */
2020 target_ulong sc_lo1; /* Was sc_badvaddr */
2021 target_ulong sc_hi2; /* Was sc_sigset[4] */
2022 target_ulong sc_lo2;
2023 target_ulong sc_hi3;
2024 target_ulong sc_lo3;
2025};
2026
2027struct sigframe {
2028 uint32_t sf_ass[4]; /* argument save space for o32 */
2029 uint32_t sf_code[2]; /* signal trampoline */
2030 struct target_sigcontext sf_sc;
2031 target_sigset_t sf_mask;
2032};
2033
2034/* Install trampoline to jump back from signal handler */
2035static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2036{
2037 int err;
2038
2039 /*
2040 * Set up the return code ...
2041 *
2042 * li v0, __NR__foo_sigreturn
2043 * syscall
2044 */
2045
2046 err = __put_user(0x24020000 + syscall, tramp + 0);
2047 err |= __put_user(0x0000000c , tramp + 1);
2048 /* flush_cache_sigtramp((unsigned long) tramp); */
2049 return err;
2050}
2051
2052static inline int
2053setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2054{
2055 int err = 0;
2056
thsead93602007-09-06 00:18:15 +00002057 err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002058
thsead93602007-09-06 00:18:15 +00002059#define save_gp_reg(i) do { \
2060 err |= __put_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002061 } while(0)
2062 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2063 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2064 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2065 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2066 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2067 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2068 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2069 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2070 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002071#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002072
thsead93602007-09-06 00:18:15 +00002073 err |= __put_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
2074 err |= __put_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002075
2076 /* Not used yet, but might be useful if we ever have DSP suppport */
2077#if 0
2078 if (cpu_has_dsp) {
2079 err |= __put_user(mfhi1(), &sc->sc_hi1);
2080 err |= __put_user(mflo1(), &sc->sc_lo1);
2081 err |= __put_user(mfhi2(), &sc->sc_hi2);
2082 err |= __put_user(mflo2(), &sc->sc_lo2);
2083 err |= __put_user(mfhi3(), &sc->sc_hi3);
2084 err |= __put_user(mflo3(), &sc->sc_lo3);
2085 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2086 }
2087 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002088#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002089 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2090 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2091 if (cpu_has_dsp) {
2092 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2093 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2094 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2095 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2096 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2097 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2098 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2099 }
ths388bb212007-05-13 13:58:00 +00002100#endif
2101#endif
bellard106ec872006-06-27 21:08:10 +00002102
ths388bb212007-05-13 13:58:00 +00002103#if 0
bellard106ec872006-06-27 21:08:10 +00002104 err |= __put_user(!!used_math(), &sc->sc_used_math);
2105
2106 if (!used_math())
2107 goto out;
2108
2109 /*
2110 * Save FPU state to signal context. Signal handler will "inherit"
2111 * current FPU state.
2112 */
2113 preempt_disable();
2114
2115 if (!is_fpu_owner()) {
2116 own_fpu();
2117 restore_fp(current);
2118 }
2119 err |= save_fp_context(sc);
2120
2121 preempt_enable();
2122 out:
2123#endif
2124 return err;
2125}
2126
2127static inline int
2128restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2129{
2130 int err = 0;
2131
2132 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2133
thsead93602007-09-06 00:18:15 +00002134 err |= __get_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
2135 err |= __get_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002136
thsead93602007-09-06 00:18:15 +00002137#define restore_gp_reg(i) do { \
2138 err |= __get_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002139 } while(0)
2140 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2141 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2142 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2143 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2144 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2145 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2146 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2147 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2148 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2149 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2150 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002151#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002152
2153#if 0
2154 if (cpu_has_dsp) {
2155 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2156 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2157 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2158 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2159 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2160 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2161 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2162 }
ths388bb212007-05-13 13:58:00 +00002163#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002164 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2165 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2166 if (cpu_has_dsp) {
2167 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2168 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2169 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2170 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2171 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2172 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2173 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2174 }
ths388bb212007-05-13 13:58:00 +00002175#endif
bellard106ec872006-06-27 21:08:10 +00002176
2177 err |= __get_user(used_math, &sc->sc_used_math);
2178 conditional_used_math(used_math);
2179
2180 preempt_disable();
2181
2182 if (used_math()) {
2183 /* restore fpu context if we have used it before */
2184 own_fpu();
2185 err |= restore_fp_context(sc);
2186 } else {
2187 /* signal handler may have used FPU. Give it up. */
2188 lose_fpu();
2189 }
2190
2191 preempt_enable();
2192#endif
2193 return err;
2194}
2195/*
2196 * Determine which stack to use..
2197 */
2198static inline void *
2199get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
2200{
2201 unsigned long sp;
2202
2203 /* Default to using normal stack */
thsead93602007-09-06 00:18:15 +00002204 sp = regs->gpr[29][regs->current_tc];
bellard106ec872006-06-27 21:08:10 +00002205
2206 /*
2207 * FPU emulator may have it's own trampoline active just
2208 * above the user stack, 16-bytes before the next lowest
2209 * 16 byte boundary. Try to avoid trashing it.
2210 */
2211 sp -= 32;
2212
bellard106ec872006-06-27 21:08:10 +00002213 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00002214 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2215 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2216 }
bellard106ec872006-06-27 21:08:10 +00002217
2218 return g2h((sp - frame_size) & ~7);
2219}
2220
ths5fafdf22007-09-16 21:08:06 +00002221static void setup_frame(int sig, struct emulated_sigaction * ka,
bellard106ec872006-06-27 21:08:10 +00002222 target_sigset_t *set, CPUState *regs)
2223{
2224 struct sigframe *frame;
2225 int i;
2226
2227 frame = get_sigframe(ka, regs, sizeof(*frame));
2228 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
2229 goto give_sigsegv;
2230
2231 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2232
2233 if(setup_sigcontext(regs, &frame->sf_sc))
2234 goto give_sigsegv;
2235
2236 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2237 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2238 goto give_sigsegv;
2239 }
2240
2241 /*
2242 * Arguments to signal handler:
2243 *
2244 * a0 = signal number
2245 * a1 = 0 (should be cause)
2246 * a2 = pointer to struct sigcontext
2247 *
2248 * $25 and PC point to the signal handler, $29 points to the
2249 * struct sigframe.
2250 */
thsead93602007-09-06 00:18:15 +00002251 regs->gpr[ 4][regs->current_tc] = sig;
2252 regs->gpr[ 5][regs->current_tc] = 0;
2253 regs->gpr[ 6][regs->current_tc] = h2g(&frame->sf_sc);
2254 regs->gpr[29][regs->current_tc] = h2g(frame);
2255 regs->gpr[31][regs->current_tc] = h2g(frame->sf_code);
bellard106ec872006-06-27 21:08:10 +00002256 /* The original kernel code sets CP0_EPC to the handler
2257 * since it returns to userland using eret
2258 * we cannot do this here, and we must set PC directly */
thsead93602007-09-06 00:18:15 +00002259 regs->PC[regs->current_tc] = regs->gpr[25][regs->current_tc] = ka->sa._sa_handler;
bellard106ec872006-06-27 21:08:10 +00002260 return;
2261
2262give_sigsegv:
2263 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002264 return;
bellard106ec872006-06-27 21:08:10 +00002265}
2266
2267long do_sigreturn(CPUState *regs)
2268{
ths388bb212007-05-13 13:58:00 +00002269 struct sigframe *frame;
2270 sigset_t blocked;
2271 target_sigset_t target_set;
2272 int i;
bellard106ec872006-06-27 21:08:10 +00002273
2274#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002275 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002276#endif
thsead93602007-09-06 00:18:15 +00002277 frame = (struct sigframe *) regs->gpr[29][regs->current_tc];
ths388bb212007-05-13 13:58:00 +00002278 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
bellard106ec872006-06-27 21:08:10 +00002279 goto badframe;
2280
ths388bb212007-05-13 13:58:00 +00002281 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002282 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2283 goto badframe;
ths388bb212007-05-13 13:58:00 +00002284 }
bellard106ec872006-06-27 21:08:10 +00002285
ths388bb212007-05-13 13:58:00 +00002286 target_to_host_sigset_internal(&blocked, &target_set);
2287 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002288
ths388bb212007-05-13 13:58:00 +00002289 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002290 goto badframe;
2291
2292#if 0
ths388bb212007-05-13 13:58:00 +00002293 /*
2294 * Don't let your children do this ...
2295 */
2296 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002297 "move\t$29, %0\n\t"
2298 "j\tsyscall_exit"
2299 :/* no outputs */
2300 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002301 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002302#endif
ths3b46e622007-09-17 08:09:54 +00002303
thsead93602007-09-06 00:18:15 +00002304 regs->PC[regs->current_tc] = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002305 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002306 * maybe a problem with nested signals ? */
2307 regs->CP0_EPC = 0;
2308 return 0;
2309
2310badframe:
ths388bb212007-05-13 13:58:00 +00002311 force_sig(TARGET_SIGSEGV/*, current*/);
2312 return 0;
bellard106ec872006-06-27 21:08:10 +00002313}
2314
ths5fafdf22007-09-16 21:08:06 +00002315static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard106ec872006-06-27 21:08:10 +00002316 target_siginfo_t *info,
2317 target_sigset_t *set, CPUState *env)
2318{
2319 fprintf(stderr, "setup_rt_frame: not implemented\n");
2320}
2321
2322long do_rt_sigreturn(CPUState *env)
2323{
2324 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2325 return -ENOSYS;
2326}
bellard6d5e2162004-09-30 22:04:13 +00002327
bellardb346ff42003-06-15 20:05:50 +00002328#else
2329
2330static void setup_frame(int sig, struct emulated_sigaction *ka,
2331 target_sigset_t *set, CPUState *env)
2332{
2333 fprintf(stderr, "setup_frame: not implemented\n");
2334}
2335
ths5fafdf22007-09-16 21:08:06 +00002336static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00002337 target_siginfo_t *info,
2338 target_sigset_t *set, CPUState *env)
2339{
2340 fprintf(stderr, "setup_rt_frame: not implemented\n");
2341}
2342
2343long do_sigreturn(CPUState *env)
2344{
2345 fprintf(stderr, "do_sigreturn: not implemented\n");
2346 return -ENOSYS;
2347}
2348
2349long do_rt_sigreturn(CPUState *env)
2350{
2351 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2352 return -ENOSYS;
2353}
2354
bellard66fb9762003-03-23 01:06:05 +00002355#endif
2356
2357void process_pending_signals(void *cpu_env)
2358{
2359 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00002360 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00002361 sigset_t set, old_set;
2362 target_sigset_t target_old_set;
bellard66fb9762003-03-23 01:06:05 +00002363 struct emulated_sigaction *k;
2364 struct sigqueue *q;
ths3b46e622007-09-17 08:09:54 +00002365
bellard31e31b82003-02-18 22:55:36 +00002366 if (!signal_pending)
2367 return;
2368
bellard66fb9762003-03-23 01:06:05 +00002369 k = sigact_table;
2370 for(sig = 1; sig <= TARGET_NSIG; sig++) {
2371 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00002372 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00002373 k++;
bellard31e31b82003-02-18 22:55:36 +00002374 }
2375 /* if no signal is pending, just return */
2376 signal_pending = 0;
2377 return;
bellard66fb9762003-03-23 01:06:05 +00002378
bellard31e31b82003-02-18 22:55:36 +00002379 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00002380#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00002381 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00002382#endif
2383 /* dequeue signal */
2384 q = k->first;
2385 k->first = q->next;
2386 if (!k->first)
2387 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00002388
bellard1fddef42005-04-17 19:16:13 +00002389 sig = gdb_handlesig (cpu_env, sig);
2390 if (!sig) {
2391 fprintf (stderr, "Lost signal\n");
2392 abort();
2393 }
bellard66fb9762003-03-23 01:06:05 +00002394
2395 handler = k->sa._sa_handler;
2396 if (handler == TARGET_SIG_DFL) {
2397 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +00002398 if (sig != TARGET_SIGCHLD &&
2399 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +00002400 sig != TARGET_SIGWINCH) {
2401 force_sig(sig);
2402 }
2403 } else if (handler == TARGET_SIG_IGN) {
2404 /* ignore sig */
2405 } else if (handler == TARGET_SIG_ERR) {
2406 force_sig(sig);
2407 } else {
bellard9de5e442003-03-23 16:49:39 +00002408 /* compute the blocked signals during the handler execution */
2409 target_to_host_sigset(&set, &k->sa.sa_mask);
2410 /* SA_NODEFER indicates that the current signal should not be
2411 blocked during the handler */
2412 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
2413 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00002414
bellard9de5e442003-03-23 16:49:39 +00002415 /* block signals in the handler using Linux */
2416 sigprocmask(SIG_BLOCK, &set, &old_set);
2417 /* save the previous blocked signal state to restore it at the
2418 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00002419 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00002420
bellardbc8a22c2003-03-30 21:02:40 +00002421 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00002422#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00002423 {
2424 CPUX86State *env = cpu_env;
2425 if (env->eflags & VM_MASK)
2426 save_v86_state(env);
2427 }
2428#endif
bellard9de5e442003-03-23 16:49:39 +00002429 /* prepare the stack frame of the virtual CPU */
bellard66fb9762003-03-23 01:06:05 +00002430 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
bellard9de5e442003-03-23 16:49:39 +00002431 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00002432 else
bellard9de5e442003-03-23 16:49:39 +00002433 setup_frame(sig, k, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00002434 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
2435 k->sa._sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00002436 }
bellard66fb9762003-03-23 01:06:05 +00002437 if (q != &k->info)
2438 free_sigqueue(q);
bellard31e31b82003-02-18 22:55:36 +00002439}