blob: 8c0e02abc0e3b1ebbbc933b1e8b51c172c855c4b [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 */
bellardec6338b2007-11-08 14:25:03 +0000418 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
bellardb346ff42003-06-15 20:05:50 +0000419 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000420 return;
421 }
422
423 /* get target signal number */
424 sig = host_to_target_signal(host_signum);
425 if (sig < 1 || sig > TARGET_NSIG)
426 return;
427#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000428 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000429#endif
430 host_to_target_siginfo_noswap(&tinfo, info);
431 if (queue_signal(sig, &tinfo) == 1) {
432 /* interrupt the virtual CPU as soon as possible */
bellard68a79312003-06-30 13:12:32 +0000433 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
bellard66fb9762003-03-23 01:06:05 +0000434 }
bellard31e31b82003-02-18 22:55:36 +0000435}
436
ths0da46a62007-10-20 20:23:07 +0000437/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000438/* compare linux/kernel/signal.c:do_sigaltstack() */
439abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000440{
441 int ret;
442 struct target_sigaltstack oss;
443
444 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000445 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000446 {
447 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
448 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
449 __put_user(sas_ss_flags(sp), &oss.ss_flags);
450 }
451
bellard579a97f2007-11-11 14:26:47 +0000452 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000453 {
bellard579a97f2007-11-11 14:26:47 +0000454 struct target_sigaltstack *uss;
455 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000456
ths0da46a62007-10-20 20:23:07 +0000457 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000458 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000459 || __get_user(ss.ss_sp, &uss->ss_sp)
460 || __get_user(ss.ss_size, &uss->ss_size)
461 || __get_user(ss.ss_flags, &uss->ss_flags))
462 goto out;
bellard579a97f2007-11-11 14:26:47 +0000463 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000464
ths0da46a62007-10-20 20:23:07 +0000465 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000466 if (on_sig_stack(sp))
467 goto out;
468
ths0da46a62007-10-20 20:23:07 +0000469 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000470 if (ss.ss_flags != TARGET_SS_DISABLE
471 && ss.ss_flags != TARGET_SS_ONSTACK
472 && ss.ss_flags != 0)
473 goto out;
474
475 if (ss.ss_flags == TARGET_SS_DISABLE) {
476 ss.ss_size = 0;
477 ss.ss_sp = 0;
478 } else {
ths0da46a62007-10-20 20:23:07 +0000479 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000480 if (ss.ss_size < MINSIGSTKSZ)
481 goto out;
482 }
483
484 target_sigaltstack_used.ss_sp = ss.ss_sp;
485 target_sigaltstack_used.ss_size = ss.ss_size;
486 }
487
bellard579a97f2007-11-11 14:26:47 +0000488 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000489 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000490 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000491 goto out;
thsa04e1342007-09-27 13:57:58 +0000492 }
493
494 ret = 0;
495out:
496 return ret;
497}
498
ths0da46a62007-10-20 20:23:07 +0000499/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000500int do_sigaction(int sig, const struct target_sigaction *act,
501 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000502{
bellard66fb9762003-03-23 01:06:05 +0000503 struct emulated_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000504 struct sigaction act1;
505 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000506 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000507
ths0aeaa8c2007-03-31 19:29:06 +0000508 if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000509 return -EINVAL;
510 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000511#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000512 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
bellard66fb9762003-03-23 01:06:05 +0000513 sig, (int)act, (int)oact);
514#endif
515 if (oact) {
516 oact->_sa_handler = tswapl(k->sa._sa_handler);
517 oact->sa_flags = tswapl(k->sa.sa_flags);
ths388bb212007-05-13 13:58:00 +0000518#if !defined(TARGET_MIPS)
519 oact->sa_restorer = tswapl(k->sa.sa_restorer);
520#endif
bellard66fb9762003-03-23 01:06:05 +0000521 oact->sa_mask = k->sa.sa_mask;
522 }
523 if (act) {
524 k->sa._sa_handler = tswapl(act->_sa_handler);
525 k->sa.sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000526#if !defined(TARGET_MIPS)
527 k->sa.sa_restorer = tswapl(act->sa_restorer);
528#endif
bellard66fb9762003-03-23 01:06:05 +0000529 k->sa.sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000530
531 /* we update the host linux signal state */
532 host_sig = target_to_host_signal(sig);
533 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
534 sigfillset(&act1.sa_mask);
535 act1.sa_flags = SA_SIGINFO;
536 if (k->sa.sa_flags & TARGET_SA_RESTART)
537 act1.sa_flags |= SA_RESTART;
538 /* NOTE: it is important to update the host kernel signal
539 ignore state to avoid getting unexpected interrupted
540 syscalls */
541 if (k->sa._sa_handler == TARGET_SIG_IGN) {
542 act1.sa_sigaction = (void *)SIG_IGN;
543 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
544 act1.sa_sigaction = (void *)SIG_DFL;
545 } else {
546 act1.sa_sigaction = host_signal_handler;
547 }
ths0da46a62007-10-20 20:23:07 +0000548 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000549 }
bellard66fb9762003-03-23 01:06:05 +0000550 }
ths0da46a62007-10-20 20:23:07 +0000551 return ret;
bellard66fb9762003-03-23 01:06:05 +0000552}
bellard31e31b82003-02-18 22:55:36 +0000553
bellard43fff232003-07-09 19:31:39 +0000554#ifndef offsetof
555#define offsetof(type, field) ((size_t) &((type *)0)->field)
556#endif
557
ths5fafdf22007-09-16 21:08:06 +0000558static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
bellard43fff232003-07-09 19:31:39 +0000559 const target_siginfo_t *info)
560{
561 tswap_siginfo(tinfo, info);
562 return 0;
563}
564
bellard66fb9762003-03-23 01:06:05 +0000565#ifdef TARGET_I386
566
567/* from the Linux kernel */
568
569struct target_fpreg {
570 uint16_t significand[4];
571 uint16_t exponent;
572};
573
574struct target_fpxreg {
575 uint16_t significand[4];
576 uint16_t exponent;
577 uint16_t padding[3];
578};
579
580struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000581 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000582};
583
584struct target_fpstate {
585 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000586 abi_ulong cw;
587 abi_ulong sw;
588 abi_ulong tag;
589 abi_ulong ipoff;
590 abi_ulong cssel;
591 abi_ulong dataoff;
592 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000593 struct target_fpreg _st[8];
594 uint16_t status;
595 uint16_t magic; /* 0xffff = regular FPU data only */
596
597 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000598 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
599 abi_ulong mxcsr;
600 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000601 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
602 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000603 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000604};
605
606#define X86_FXSR_MAGIC 0x0000
607
608struct target_sigcontext {
609 uint16_t gs, __gsh;
610 uint16_t fs, __fsh;
611 uint16_t es, __esh;
612 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000613 abi_ulong edi;
614 abi_ulong esi;
615 abi_ulong ebp;
616 abi_ulong esp;
617 abi_ulong ebx;
618 abi_ulong edx;
619 abi_ulong ecx;
620 abi_ulong eax;
621 abi_ulong trapno;
622 abi_ulong err;
623 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000624 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000625 abi_ulong eflags;
626 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000627 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000628 abi_ulong fpstate; /* pointer */
629 abi_ulong oldmask;
630 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000631};
632
bellard66fb9762003-03-23 01:06:05 +0000633struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000634 abi_ulong tuc_flags;
635 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +0000636 target_stack_t tuc_stack;
637 struct target_sigcontext tuc_mcontext;
638 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000639};
640
641struct sigframe
642{
blueswir1992f48a2007-10-14 16:27:31 +0000643 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000644 int sig;
645 struct target_sigcontext sc;
646 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000647 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000648 char retcode[8];
649};
650
651struct rt_sigframe
652{
blueswir1992f48a2007-10-14 16:27:31 +0000653 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000654 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000655 abi_ulong pinfo;
656 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000657 struct target_siginfo info;
658 struct target_ucontext uc;
659 struct target_fpstate fpstate;
660 char retcode[8];
661};
662
663/*
664 * Set up a signal frame.
665 */
666
bellard66fb9762003-03-23 01:06:05 +0000667/* XXX: save x87 state */
668static int
669setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
670 CPUX86State *env, unsigned long mask)
671{
672 int err = 0;
bellard775b58d2007-11-11 16:22:17 +0000673 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000674
bellard579a97f2007-11-11 14:26:47 +0000675 /* already locked in setup_frame() */
bellarda52c7572003-06-21 13:14:12 +0000676 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
677 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
678 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
679 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000680 err |= __put_user(env->regs[R_EDI], &sc->edi);
681 err |= __put_user(env->regs[R_ESI], &sc->esi);
682 err |= __put_user(env->regs[R_EBP], &sc->ebp);
683 err |= __put_user(env->regs[R_ESP], &sc->esp);
684 err |= __put_user(env->regs[R_EBX], &sc->ebx);
685 err |= __put_user(env->regs[R_EDX], &sc->edx);
686 err |= __put_user(env->regs[R_ECX], &sc->ecx);
687 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000688 err |= __put_user(env->exception_index, &sc->trapno);
689 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000690 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000691 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000692 err |= __put_user(env->eflags, &sc->eflags);
693 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000694 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000695
696 cpu_x86_fsave(env, (void *)fpstate, 1);
697 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000698 magic = 0xffff;
699 err |= __put_user(magic, &fpstate->magic);
bellarded2dcdf2003-05-29 20:06:27 +0000700 err |= __put_user(fpstate, &sc->fpstate);
701
bellard66fb9762003-03-23 01:06:05 +0000702 /* non-iBCS2 extensions.. */
703 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000704 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000705 return err;
706}
707
708/*
709 * Determine which stack to use..
710 */
711
bellard579a97f2007-11-11 14:26:47 +0000712static inline abi_ulong
bellard66fb9762003-03-23 01:06:05 +0000713get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
714{
715 unsigned long esp;
716
717 /* Default to using normal stack */
718 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000719 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +0000720 if (ka->sa.sa_flags & TARGET_SA_ONSTACK) {
721 if (sas_ss_flags(esp) == 0)
722 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
723 }
bellard66fb9762003-03-23 01:06:05 +0000724
725 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000726 else
bellarda52c7572003-06-21 13:14:12 +0000727 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
728 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
729 ka->sa.sa_restorer) {
730 esp = (unsigned long) ka->sa.sa_restorer;
731 }
bellard579a97f2007-11-11 14:26:47 +0000732 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000733}
734
bellard579a97f2007-11-11 14:26:47 +0000735/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
bellard66fb9762003-03-23 01:06:05 +0000736static void setup_frame(int sig, struct emulated_sigaction *ka,
737 target_sigset_t *set, CPUX86State *env)
738{
bellard579a97f2007-11-11 14:26:47 +0000739 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000740 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000741 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000742
bellard579a97f2007-11-11 14:26:47 +0000743 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000744
bellard579a97f2007-11-11 14:26:47 +0000745 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000746 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000747
bellard66fb9762003-03-23 01:06:05 +0000748 err |= __put_user((/*current->exec_domain
749 && current->exec_domain->signal_invmap
750 && sig < 32
751 ? current->exec_domain->signal_invmap[sig]
752 : */ sig),
753 &frame->sig);
754 if (err)
755 goto give_sigsegv;
756
757 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
758 if (err)
759 goto give_sigsegv;
760
bellard92319442004-06-19 16:58:13 +0000761 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
762 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
763 goto give_sigsegv;
764 }
bellard66fb9762003-03-23 01:06:05 +0000765
766 /* Set up to return from userspace. If provided, use a stub
767 already in userspace. */
768 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
769 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
770 } else {
bellard775b58d2007-11-11 16:22:17 +0000771 uint16_t val16;
bellard66fb9762003-03-23 01:06:05 +0000772 err |= __put_user(frame->retcode, &frame->pretcode);
773 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000774 val16 = 0xb858;
775 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
j_mayer84409dd2007-04-06 08:56:50 +0000776#if defined(TARGET_X86_64)
777#warning "Fix this !"
778#else
bellard66fb9762003-03-23 01:06:05 +0000779 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
j_mayer84409dd2007-04-06 08:56:50 +0000780#endif
bellard775b58d2007-11-11 16:22:17 +0000781 val16 = 0x80cd;
782 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000783 }
784
785 if (err)
786 goto give_sigsegv;
787
788 /* Set up registers for signal handler */
pbrook53a59602006-03-25 19:31:22 +0000789 env->regs[R_ESP] = h2g(frame);
bellard66fb9762003-03-23 01:06:05 +0000790 env->eip = (unsigned long) ka->sa._sa_handler;
791
792 cpu_x86_load_seg(env, R_DS, __USER_DS);
793 cpu_x86_load_seg(env, R_ES, __USER_DS);
794 cpu_x86_load_seg(env, R_SS, __USER_DS);
795 cpu_x86_load_seg(env, R_CS, __USER_CS);
796 env->eflags &= ~TF_MASK;
797
bellard579a97f2007-11-11 14:26:47 +0000798 unlock_user_struct(frame, frame_addr, 1);
799
bellard66fb9762003-03-23 01:06:05 +0000800 return;
801
802give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000803 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000804 if (sig == TARGET_SIGSEGV)
805 ka->sa._sa_handler = TARGET_SIG_DFL;
806 force_sig(TARGET_SIGSEGV /* , current */);
807}
808
bellard579a97f2007-11-11 14:26:47 +0000809/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
ths5fafdf22007-09-16 21:08:06 +0000810static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard9de5e442003-03-23 16:49:39 +0000811 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000812 target_sigset_t *set, CPUX86State *env)
813{
bellard579a97f2007-11-11 14:26:47 +0000814 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000815 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000816 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000817
bellard579a97f2007-11-11 14:26:47 +0000818 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000819
bellard579a97f2007-11-11 14:26:47 +0000820 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000821 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000822
823 err |= __put_user((/*current->exec_domain
824 && current->exec_domain->signal_invmap
825 && sig < 32
826 ? current->exec_domain->signal_invmap[sig]
827 : */sig),
828 &frame->sig);
blueswir1992f48a2007-10-14 16:27:31 +0000829 err |= __put_user((abi_ulong)&frame->info, &frame->pinfo);
830 err |= __put_user((abi_ulong)&frame->uc, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000831 err |= copy_siginfo_to_user(&frame->info, info);
832 if (err)
833 goto give_sigsegv;
834
835 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000836 err |= __put_user(0, &frame->uc.tuc_flags);
837 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000838 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000839 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000840 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000841 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000842 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000843 &frame->uc.tuc_stack.ss_size);
844 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard66fb9762003-03-23 01:06:05 +0000845 env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +0000846 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000847 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000848 goto give_sigsegv;
849 }
bellard66fb9762003-03-23 01:06:05 +0000850
851 /* Set up to return from userspace. If provided, use a stub
852 already in userspace. */
853 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
854 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
855 } else {
bellard775b58d2007-11-11 16:22:17 +0000856 uint16_t val16;
857
bellard66fb9762003-03-23 01:06:05 +0000858 err |= __put_user(frame->retcode, &frame->pretcode);
859 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000860 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000861 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000862 val16 = 0x80cd;
863 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000864 }
865
866 if (err)
867 goto give_sigsegv;
868
869 /* Set up registers for signal handler */
870 env->regs[R_ESP] = (unsigned long) frame;
871 env->eip = (unsigned long) ka->sa._sa_handler;
872
873 cpu_x86_load_seg(env, R_DS, __USER_DS);
874 cpu_x86_load_seg(env, R_ES, __USER_DS);
875 cpu_x86_load_seg(env, R_SS, __USER_DS);
876 cpu_x86_load_seg(env, R_CS, __USER_CS);
877 env->eflags &= ~TF_MASK;
878
bellard579a97f2007-11-11 14:26:47 +0000879 unlock_user_struct(frame, frame_addr, 1);
880
bellard66fb9762003-03-23 01:06:05 +0000881 return;
882
883give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000884 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000885 if (sig == TARGET_SIGSEGV)
886 ka->sa._sa_handler = TARGET_SIG_DFL;
887 force_sig(TARGET_SIGSEGV /* , current */);
888}
889
890static int
891restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
892{
893 unsigned int err = 0;
894
bellard66fb9762003-03-23 01:06:05 +0000895 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
896 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
897 cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
898 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
899
900 env->regs[R_EDI] = ldl(&sc->edi);
901 env->regs[R_ESI] = ldl(&sc->esi);
902 env->regs[R_EBP] = ldl(&sc->ebp);
903 env->regs[R_ESP] = ldl(&sc->esp);
904 env->regs[R_EBX] = ldl(&sc->ebx);
905 env->regs[R_EDX] = ldl(&sc->edx);
906 env->regs[R_ECX] = ldl(&sc->ecx);
907 env->eip = ldl(&sc->eip);
908
909 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
910 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000911
bellard66fb9762003-03-23 01:06:05 +0000912 {
913 unsigned int tmpflags;
914 tmpflags = ldl(&sc->eflags);
915 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
916 // regs->orig_eax = -1; /* disable syscall checks */
917 }
918
bellard66fb9762003-03-23 01:06:05 +0000919 {
920 struct _fpstate * buf;
bellarded2dcdf2003-05-29 20:06:27 +0000921 buf = (void *)ldl(&sc->fpstate);
bellard66fb9762003-03-23 01:06:05 +0000922 if (buf) {
bellarded2dcdf2003-05-29 20:06:27 +0000923#if 0
bellard66fb9762003-03-23 01:06:05 +0000924 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
925 goto badframe;
bellarded2dcdf2003-05-29 20:06:27 +0000926#endif
927 cpu_x86_frstor(env, (void *)buf, 1);
bellard66fb9762003-03-23 01:06:05 +0000928 }
929 }
bellarded2dcdf2003-05-29 20:06:27 +0000930
bellard66fb9762003-03-23 01:06:05 +0000931 *peax = ldl(&sc->eax);
932 return err;
933#if 0
934badframe:
935 return 1;
936#endif
937}
938
939long do_sigreturn(CPUX86State *env)
940{
bellard579a97f2007-11-11 14:26:47 +0000941 struct sigframe *frame;
942 abi_ulong frame_addr = env->regs[R_ESP] - 8;
bellard66fb9762003-03-23 01:06:05 +0000943 target_sigset_t target_set;
944 sigset_t set;
945 int eax, i;
946
bellard447db212003-05-10 15:10:36 +0000947#if defined(DEBUG_SIGNAL)
948 fprintf(stderr, "do_sigreturn\n");
949#endif
bellard579a97f2007-11-11 14:26:47 +0000950 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
951 goto badframe;
bellard66fb9762003-03-23 01:06:05 +0000952 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +0000953 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
954 goto badframe;
955 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
956 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
957 goto badframe;
958 }
bellard66fb9762003-03-23 01:06:05 +0000959
bellard92319442004-06-19 16:58:13 +0000960 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +0000961 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +0000962
bellard66fb9762003-03-23 01:06:05 +0000963 /* restore registers */
964 if (restore_sigcontext(env, &frame->sc, &eax))
965 goto badframe;
bellard579a97f2007-11-11 14:26:47 +0000966 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000967 return eax;
968
969badframe:
bellard579a97f2007-11-11 14:26:47 +0000970 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000971 force_sig(TARGET_SIGSEGV);
972 return 0;
973}
974
975long do_rt_sigreturn(CPUX86State *env)
976{
pbrook53a59602006-03-25 19:31:22 +0000977 struct rt_sigframe *frame = (struct rt_sigframe *)g2h(env->regs[R_ESP] - 4);
bellard66fb9762003-03-23 01:06:05 +0000978 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +0000979 int eax;
980
981#if 0
982 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
983 goto badframe;
984#endif
bellardb8076a72005-04-07 22:20:31 +0000985 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +0000986 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +0000987
bellardb8076a72005-04-07 22:20:31 +0000988 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +0000989 goto badframe;
990
bellard579a97f2007-11-11 14:26:47 +0000991 if (do_sigaltstack(h2g(&frame->uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +0000992 goto badframe;
thsa04e1342007-09-27 13:57:58 +0000993
bellard66fb9762003-03-23 01:06:05 +0000994 return eax;
995
996badframe:
997 force_sig(TARGET_SIGSEGV);
998 return 0;
999}
1000
bellard43fff232003-07-09 19:31:39 +00001001#elif defined(TARGET_ARM)
1002
1003struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001004 abi_ulong trap_no;
1005 abi_ulong error_code;
1006 abi_ulong oldmask;
1007 abi_ulong arm_r0;
1008 abi_ulong arm_r1;
1009 abi_ulong arm_r2;
1010 abi_ulong arm_r3;
1011 abi_ulong arm_r4;
1012 abi_ulong arm_r5;
1013 abi_ulong arm_r6;
1014 abi_ulong arm_r7;
1015 abi_ulong arm_r8;
1016 abi_ulong arm_r9;
1017 abi_ulong arm_r10;
1018 abi_ulong arm_fp;
1019 abi_ulong arm_ip;
1020 abi_ulong arm_sp;
1021 abi_ulong arm_lr;
1022 abi_ulong arm_pc;
1023 abi_ulong arm_cpsr;
1024 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001025};
1026
bellard43fff232003-07-09 19:31:39 +00001027struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +00001028 abi_ulong tuc_flags;
1029 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +00001030 target_stack_t tuc_stack;
1031 struct target_sigcontext tuc_mcontext;
1032 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001033};
1034
1035struct sigframe
1036{
1037 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001038 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1039 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001040};
1041
1042struct rt_sigframe
1043{
1044 struct target_siginfo *pinfo;
1045 void *puc;
1046 struct target_siginfo info;
1047 struct target_ucontext uc;
blueswir1992f48a2007-10-14 16:27:31 +00001048 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001049};
1050
1051#define TARGET_CONFIG_CPU_32 1
1052
1053/*
1054 * For ARM syscalls, we encode the syscall number into the instruction.
1055 */
1056#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1057#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1058
1059/*
1060 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1061 * need two 16-bit instructions.
1062 */
1063#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1064#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1065
blueswir1992f48a2007-10-14 16:27:31 +00001066static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001067 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1068 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1069};
1070
1071
1072#define __put_user_error(x,p,e) __put_user(x, p)
1073#define __get_user_error(x,p,e) __get_user(x, p)
1074
1075static inline int valid_user_regs(CPUState *regs)
1076{
1077 return 1;
1078}
1079
1080static int
1081setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1082 CPUState *env, unsigned long mask)
1083{
1084 int err = 0;
1085
1086 __put_user_error(env->regs[0], &sc->arm_r0, err);
1087 __put_user_error(env->regs[1], &sc->arm_r1, err);
1088 __put_user_error(env->regs[2], &sc->arm_r2, err);
1089 __put_user_error(env->regs[3], &sc->arm_r3, err);
1090 __put_user_error(env->regs[4], &sc->arm_r4, err);
1091 __put_user_error(env->regs[5], &sc->arm_r5, err);
1092 __put_user_error(env->regs[6], &sc->arm_r6, err);
1093 __put_user_error(env->regs[7], &sc->arm_r7, err);
1094 __put_user_error(env->regs[8], &sc->arm_r8, err);
1095 __put_user_error(env->regs[9], &sc->arm_r9, err);
1096 __put_user_error(env->regs[10], &sc->arm_r10, err);
1097 __put_user_error(env->regs[11], &sc->arm_fp, err);
1098 __put_user_error(env->regs[12], &sc->arm_ip, err);
1099 __put_user_error(env->regs[13], &sc->arm_sp, err);
1100 __put_user_error(env->regs[14], &sc->arm_lr, err);
1101 __put_user_error(env->regs[15], &sc->arm_pc, err);
1102#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001103 __put_user_error(cpsr_read(env), &sc->arm_cpsr, err);
bellard43fff232003-07-09 19:31:39 +00001104#endif
1105
1106 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1107 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1108 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1109 __put_user_error(mask, &sc->oldmask, err);
1110
1111 return err;
1112}
1113
bellard579a97f2007-11-11 14:26:47 +00001114static inline abi_ulong
bellard43fff232003-07-09 19:31:39 +00001115get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1116{
1117 unsigned long sp = regs->regs[13];
1118
bellard43fff232003-07-09 19:31:39 +00001119 /*
1120 * This is the X/Open sanctioned signal stack switching.
1121 */
thsa04e1342007-09-27 13:57:58 +00001122 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1123 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001124 /*
1125 * ATPCS B01 mandates 8-byte alignment
1126 */
bellard579a97f2007-11-11 14:26:47 +00001127 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001128}
1129
1130static int
1131setup_return(CPUState *env, struct emulated_sigaction *ka,
blueswir1992f48a2007-10-14 16:27:31 +00001132 abi_ulong *rc, void *frame, int usig)
bellard43fff232003-07-09 19:31:39 +00001133{
blueswir1992f48a2007-10-14 16:27:31 +00001134 abi_ulong handler = (abi_ulong)ka->sa._sa_handler;
1135 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001136 int thumb = 0;
1137#if defined(TARGET_CONFIG_CPU_32)
bellardb5ff1b32005-11-26 10:38:39 +00001138#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001139 abi_ulong cpsr = env->cpsr;
bellard43fff232003-07-09 19:31:39 +00001140
bellard43fff232003-07-09 19:31:39 +00001141 /*
1142 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1143 */
1144 if (ka->sa.sa_flags & SA_THIRTYTWO)
1145 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1146
1147#ifdef CONFIG_ARM_THUMB
1148 if (elf_hwcap & HWCAP_THUMB) {
1149 /*
1150 * The LSB of the handler determines if we're going to
1151 * be using THUMB or ARM mode for this signal handler.
1152 */
1153 thumb = handler & 1;
1154
1155 if (thumb)
1156 cpsr |= T_BIT;
1157 else
1158 cpsr &= ~T_BIT;
1159 }
thsa04e1342007-09-27 13:57:58 +00001160#endif /* CONFIG_ARM_THUMB */
1161#endif /* 0 */
bellard43fff232003-07-09 19:31:39 +00001162#endif /* TARGET_CONFIG_CPU_32 */
1163
1164 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
blueswir1992f48a2007-10-14 16:27:31 +00001165 retcode = (abi_ulong)ka->sa.sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001166 } else {
1167 unsigned int idx = thumb;
1168
1169 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1170 idx += 2;
1171
1172 if (__put_user(retcodes[idx], rc))
1173 return 1;
1174#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001175 flush_icache_range((abi_ulong)rc,
1176 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001177#endif
blueswir1992f48a2007-10-14 16:27:31 +00001178 retcode = ((abi_ulong)rc) + thumb;
bellard43fff232003-07-09 19:31:39 +00001179 }
1180
1181 env->regs[0] = usig;
pbrook53a59602006-03-25 19:31:22 +00001182 env->regs[13] = h2g(frame);
bellard43fff232003-07-09 19:31:39 +00001183 env->regs[14] = retcode;
1184 env->regs[15] = handler & (thumb ? ~1 : ~3);
1185
bellardb5ff1b32005-11-26 10:38:39 +00001186#if 0
bellard43fff232003-07-09 19:31:39 +00001187#ifdef TARGET_CONFIG_CPU_32
1188 env->cpsr = cpsr;
1189#endif
bellardb5ff1b32005-11-26 10:38:39 +00001190#endif
bellard43fff232003-07-09 19:31:39 +00001191
1192 return 0;
1193}
1194
bellard579a97f2007-11-11 14:26:47 +00001195/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
bellard43fff232003-07-09 19:31:39 +00001196static void setup_frame(int usig, struct emulated_sigaction *ka,
1197 target_sigset_t *set, CPUState *regs)
1198{
bellard579a97f2007-11-11 14:26:47 +00001199 struct sigframe *frame;
1200 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
bellard92319442004-06-19 16:58:13 +00001201 int i, err = 0;
bellard43fff232003-07-09 19:31:39 +00001202
bellard579a97f2007-11-11 14:26:47 +00001203 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1204 return;
1205
bellard43fff232003-07-09 19:31:39 +00001206 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1207
bellard92319442004-06-19 16:58:13 +00001208 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1209 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001210 goto end;
bellard43fff232003-07-09 19:31:39 +00001211 }
1212
1213 if (err == 0)
1214 err = setup_return(regs, ka, &frame->retcode, frame, usig);
bellard579a97f2007-11-11 14:26:47 +00001215
1216end:
1217 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001218 // return err;
1219}
1220
bellard579a97f2007-11-11 14:26:47 +00001221/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
ths5fafdf22007-09-16 21:08:06 +00001222static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
bellard43fff232003-07-09 19:31:39 +00001223 target_siginfo_t *info,
1224 target_sigset_t *set, CPUState *env)
1225{
bellard579a97f2007-11-11 14:26:47 +00001226 struct rt_sigframe *frame;
1227 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001228 struct target_sigaltstack stack;
bellard92319442004-06-19 16:58:13 +00001229 int i, err = 0;
bellard43fff232003-07-09 19:31:39 +00001230
bellard579a97f2007-11-11 14:26:47 +00001231 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001232 return /* 1 */;
1233
blueswir1992f48a2007-10-14 16:27:31 +00001234 __put_user_error(&frame->info, (abi_ulong *)&frame->pinfo, err);
1235 __put_user_error(&frame->uc, (abi_ulong *)&frame->puc, err);
bellard43fff232003-07-09 19:31:39 +00001236 err |= copy_siginfo_to_user(&frame->info, info);
1237
1238 /* Clear all the bits of the ucontext we don't use. */
pbrook53a59602006-03-25 19:31:22 +00001239 memset(&frame->uc, 0, offsetof(struct target_ucontext, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001240
thsa04e1342007-09-27 13:57:58 +00001241 memset(&stack, 0, sizeof(stack));
1242 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1243 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1244 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001245 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001246
bellardb8076a72005-04-07 22:20:31 +00001247 err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
bellard43fff232003-07-09 19:31:39 +00001248 env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001249 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001250 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001251 goto end;
bellard92319442004-06-19 16:58:13 +00001252 }
bellard43fff232003-07-09 19:31:39 +00001253
1254 if (err == 0)
1255 err = setup_return(env, ka, &frame->retcode, frame, usig);
1256
1257 if (err == 0) {
1258 /*
1259 * For realtime signals we must also set the second and third
1260 * arguments for the signal handler.
1261 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1262 */
blueswir1992f48a2007-10-14 16:27:31 +00001263 env->regs[1] = (abi_ulong)frame->pinfo;
1264 env->regs[2] = (abi_ulong)frame->puc;
bellard43fff232003-07-09 19:31:39 +00001265 }
1266
bellard579a97f2007-11-11 14:26:47 +00001267end:
1268 unlock_user_struct(frame, frame_addr, 1);
1269
bellard43fff232003-07-09 19:31:39 +00001270 // return err;
1271}
1272
1273static int
1274restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1275{
1276 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001277 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001278
1279 __get_user_error(env->regs[0], &sc->arm_r0, err);
1280 __get_user_error(env->regs[1], &sc->arm_r1, err);
1281 __get_user_error(env->regs[2], &sc->arm_r2, err);
1282 __get_user_error(env->regs[3], &sc->arm_r3, err);
1283 __get_user_error(env->regs[4], &sc->arm_r4, err);
1284 __get_user_error(env->regs[5], &sc->arm_r5, err);
1285 __get_user_error(env->regs[6], &sc->arm_r6, err);
1286 __get_user_error(env->regs[7], &sc->arm_r7, err);
1287 __get_user_error(env->regs[8], &sc->arm_r8, err);
1288 __get_user_error(env->regs[9], &sc->arm_r9, err);
1289 __get_user_error(env->regs[10], &sc->arm_r10, err);
1290 __get_user_error(env->regs[11], &sc->arm_fp, err);
1291 __get_user_error(env->regs[12], &sc->arm_ip, err);
1292 __get_user_error(env->regs[13], &sc->arm_sp, err);
1293 __get_user_error(env->regs[14], &sc->arm_lr, err);
1294 __get_user_error(env->regs[15], &sc->arm_pc, err);
1295#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001296 __get_user_error(cpsr, &sc->arm_cpsr, err);
1297 cpsr_write(env, cpsr, 0xffffffff);
bellard43fff232003-07-09 19:31:39 +00001298#endif
1299
1300 err |= !valid_user_regs(env);
1301
1302 return err;
1303}
1304
1305long do_sigreturn(CPUState *env)
1306{
1307 struct sigframe *frame;
1308 target_sigset_t set;
1309 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001310 int i;
bellard43fff232003-07-09 19:31:39 +00001311
1312 /*
1313 * Since we stacked the signal on a 64-bit boundary,
1314 * then 'sp' should be word aligned here. If it's
1315 * not, then the user is trying to mess with us.
1316 */
1317 if (env->regs[13] & 7)
1318 goto badframe;
1319
pbrook53a59602006-03-25 19:31:22 +00001320 frame = (struct sigframe *)g2h(env->regs[13]);
bellard43fff232003-07-09 19:31:39 +00001321
1322#if 0
1323 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1324 goto badframe;
1325#endif
bellard92319442004-06-19 16:58:13 +00001326 if (__get_user(set.sig[0], &frame->sc.oldmask))
1327 goto badframe;
1328 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1329 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1330 goto badframe;
1331 }
bellard43fff232003-07-09 19:31:39 +00001332
bellard92319442004-06-19 16:58:13 +00001333 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001334 sigprocmask(SIG_SETMASK, &host_set, NULL);
1335
1336 if (restore_sigcontext(env, &frame->sc))
1337 goto badframe;
1338
1339#if 0
1340 /* Send SIGTRAP if we're single-stepping */
1341 if (ptrace_cancel_bpt(current))
1342 send_sig(SIGTRAP, current, 1);
1343#endif
1344 return env->regs[0];
1345
1346badframe:
1347 force_sig(SIGSEGV /* , current */);
1348 return 0;
1349}
1350
1351long do_rt_sigreturn(CPUState *env)
1352{
1353 struct rt_sigframe *frame;
bellard43fff232003-07-09 19:31:39 +00001354 sigset_t host_set;
1355
1356 /*
1357 * Since we stacked the signal on a 64-bit boundary,
1358 * then 'sp' should be word aligned here. If it's
1359 * not, then the user is trying to mess with us.
1360 */
1361 if (env->regs[13] & 7)
1362 goto badframe;
1363
1364 frame = (struct rt_sigframe *)env->regs[13];
1365
1366#if 0
1367 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1368 goto badframe;
1369#endif
bellardb8076a72005-04-07 22:20:31 +00001370 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001371 sigprocmask(SIG_SETMASK, &host_set, NULL);
1372
bellardb8076a72005-04-07 22:20:31 +00001373 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001374 goto badframe;
1375
bellard579a97f2007-11-11 14:26:47 +00001376 if (do_sigaltstack(h2g(&frame->uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
thsa04e1342007-09-27 13:57:58 +00001377 goto badframe;
1378
bellard43fff232003-07-09 19:31:39 +00001379#if 0
1380 /* Send SIGTRAP if we're single-stepping */
1381 if (ptrace_cancel_bpt(current))
1382 send_sig(SIGTRAP, current, 1);
1383#endif
1384 return env->regs[0];
1385
1386badframe:
1387 force_sig(SIGSEGV /* , current */);
1388 return 0;
1389}
1390
bellard6d5e2162004-09-30 22:04:13 +00001391#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001392
bellard6d5e2162004-09-30 22:04:13 +00001393#define __SUNOS_MAXWIN 31
1394
1395/* This is what SunOS does, so shall I. */
1396struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001397 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001398
blueswir1992f48a2007-10-14 16:27:31 +00001399 abi_ulong sigc_mask; /* sigmask to restore */
1400 abi_ulong sigc_sp; /* stack pointer */
1401 abi_ulong sigc_pc; /* program counter */
1402 abi_ulong sigc_npc; /* next program counter */
1403 abi_ulong sigc_psr; /* for condition codes etc */
1404 abi_ulong sigc_g1; /* User uses these two registers */
1405 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001406
1407 /* Now comes information regarding the users window set
1408 * at the time of the signal.
1409 */
blueswir1992f48a2007-10-14 16:27:31 +00001410 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001411
1412 /* stack ptrs for each regwin buf */
1413 char *sigc_spbuf[__SUNOS_MAXWIN];
1414
1415 /* Windows to restore after signal */
1416 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001417 abi_ulong locals[8];
1418 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001419 } sigc_wbuf[__SUNOS_MAXWIN];
1420};
1421/* A Sparc stack frame */
1422struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001423 abi_ulong locals[8];
1424 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001425 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001426 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001427 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001428 abi_ulong xargs[6];
1429 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001430};
1431
1432typedef struct {
1433 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001434 abi_ulong psr;
1435 abi_ulong pc;
1436 abi_ulong npc;
1437 abi_ulong y;
1438 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001439 } si_regs;
1440 int si_mask;
1441} __siginfo_t;
1442
1443typedef struct {
1444 unsigned long si_float_regs [32];
1445 unsigned long si_fsr;
1446 unsigned long si_fpqdepth;
1447 struct {
1448 unsigned long *insn_addr;
1449 unsigned long insn;
1450 } si_fpqueue [16];
bellard74ccb342006-07-18 21:23:34 +00001451} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001452
1453
1454struct target_signal_frame {
1455 struct sparc_stackf ss;
1456 __siginfo_t info;
bellard74ccb342006-07-18 21:23:34 +00001457 qemu_siginfo_fpu_t *fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001458 abi_ulong insns[2] __attribute__ ((aligned (8)));
1459 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1460 abi_ulong extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001461 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001462};
1463struct target_rt_signal_frame {
1464 struct sparc_stackf ss;
1465 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001466 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001467 sigset_t mask;
bellard74ccb342006-07-18 21:23:34 +00001468 qemu_siginfo_fpu_t *fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001469 unsigned int insns[2];
1470 stack_t stack;
1471 unsigned int extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001472 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001473};
1474
bellarde80cfcf2004-12-19 23:18:01 +00001475#define UREG_O0 16
1476#define UREG_O6 22
1477#define UREG_I0 0
1478#define UREG_I1 1
1479#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001480#define UREG_I3 3
1481#define UREG_I4 4
1482#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001483#define UREG_I6 6
1484#define UREG_I7 7
1485#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001486#define UREG_FP UREG_I6
1487#define UREG_SP UREG_O6
1488
1489static inline void *get_sigframe(struct emulated_sigaction *sa, CPUState *env, unsigned long framesize)
1490{
1491 unsigned long sp;
1492
1493 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001494
1495 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00001496 if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1497 if (!on_sig_stack(sp)
1498 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1499 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001500 }
pbrook53a59602006-03-25 19:31:22 +00001501 return g2h(sp - framesize);
bellard6d5e2162004-09-30 22:04:13 +00001502}
1503
1504static int
blueswir1992f48a2007-10-14 16:27:31 +00001505setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001506{
1507 int err = 0, i;
1508
bellard6d5e2162004-09-30 22:04:13 +00001509 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001510 err |= __put_user(env->pc, &si->si_regs.pc);
1511 err |= __put_user(env->npc, &si->si_regs.npc);
1512 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001513 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001514 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1515 }
bellarda315a142005-01-30 22:59:18 +00001516 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001517 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001518 }
bellard6d5e2162004-09-30 22:04:13 +00001519 err |= __put_user(mask, &si->si_mask);
1520 return err;
1521}
bellarde80cfcf2004-12-19 23:18:01 +00001522
bellard80a9d032005-01-03 23:31:27 +00001523#if 0
bellard6d5e2162004-09-30 22:04:13 +00001524static int
1525setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1526 CPUState *env, unsigned long mask)
1527{
1528 int err = 0;
1529
1530 err |= __put_user(mask, &sc->sigc_mask);
1531 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1532 err |= __put_user(env->pc, &sc->sigc_pc);
1533 err |= __put_user(env->npc, &sc->sigc_npc);
1534 err |= __put_user(env->psr, &sc->sigc_psr);
1535 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1536 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1537
1538 return err;
1539}
bellard80a9d032005-01-03 23:31:27 +00001540#endif
bellard6d5e2162004-09-30 22:04:13 +00001541#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1542
1543static void setup_frame(int sig, struct emulated_sigaction *ka,
1544 target_sigset_t *set, CPUState *env)
1545{
1546 struct target_signal_frame *sf;
1547 int sigframe_size, err, i;
1548
1549 /* 1. Make sure everything is clean */
1550 //synchronize_user_stack();
1551
1552 sigframe_size = NF_ALIGNEDSZ;
1553
1554 sf = (struct target_signal_frame *)
1555 get_sigframe(ka, env, sigframe_size);
1556
bellarde80cfcf2004-12-19 23:18:01 +00001557 //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 +00001558#if 0
1559 if (invalid_frame_pointer(sf, sigframe_size))
1560 goto sigill_and_return;
1561#endif
1562 /* 2. Save the current process state */
1563 err = setup___siginfo(&sf->info, env, set->sig[0]);
1564 err |= __put_user(0, &sf->extra_size);
1565
1566 //err |= save_fpu_state(regs, &sf->fpu_state);
1567 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1568
1569 err |= __put_user(set->sig[0], &sf->info.si_mask);
1570 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1571 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1572 }
1573
bellarda315a142005-01-30 22:59:18 +00001574 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001575 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001576 }
bellarda315a142005-01-30 22:59:18 +00001577 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001578 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001579 }
bellard6d5e2162004-09-30 22:04:13 +00001580 if (err)
1581 goto sigsegv;
1582
1583 /* 3. signal handler back-trampoline and parameters */
pbrook53a59602006-03-25 19:31:22 +00001584 env->regwptr[UREG_FP] = h2g(sf);
bellard6d5e2162004-09-30 22:04:13 +00001585 env->regwptr[UREG_I0] = sig;
pbrook53a59602006-03-25 19:31:22 +00001586 env->regwptr[UREG_I1] = h2g(&sf->info);
1587 env->regwptr[UREG_I2] = h2g(&sf->info);
bellard6d5e2162004-09-30 22:04:13 +00001588
1589 /* 4. signal handler */
1590 env->pc = (unsigned long) ka->sa._sa_handler;
1591 env->npc = (env->pc + 4);
1592 /* 5. return to kernel instructions */
1593 if (ka->sa.sa_restorer)
1594 env->regwptr[UREG_I7] = (unsigned long)ka->sa.sa_restorer;
1595 else {
bellard775b58d2007-11-11 16:22:17 +00001596 uint32_t val32;
pbrook53a59602006-03-25 19:31:22 +00001597 env->regwptr[UREG_I7] = h2g(&(sf->insns[0]) - 2);
bellard6d5e2162004-09-30 22:04:13 +00001598
1599 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001600 val32 = 0x821020d8;
1601 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001602
1603 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001604 val32 = 0x91d02010;
1605 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001606 if (err)
1607 goto sigsegv;
1608
1609 /* Flush instruction space. */
1610 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001611 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001612 }
1613 return;
1614
bellard80a9d032005-01-03 23:31:27 +00001615 //sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001616 force_sig(TARGET_SIGILL);
1617sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001618 //fprintf(stderr, "force_sig\n");
bellard6d5e2162004-09-30 22:04:13 +00001619 force_sig(TARGET_SIGSEGV);
1620}
1621static inline int
bellard74ccb342006-07-18 21:23:34 +00001622restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001623{
1624 int err;
1625#if 0
1626#ifdef CONFIG_SMP
1627 if (current->flags & PF_USEDFPU)
1628 regs->psr &= ~PSR_EF;
1629#else
1630 if (current == last_task_used_math) {
1631 last_task_used_math = 0;
1632 regs->psr &= ~PSR_EF;
1633 }
1634#endif
1635 current->used_math = 1;
1636 current->flags &= ~PF_USEDFPU;
1637#endif
1638#if 0
1639 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1640 return -EFAULT;
1641#endif
1642
bellardfafffae2006-10-28 12:09:16 +00001643#if 0
1644 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001645 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1646 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001647#endif
bellard6d5e2162004-09-30 22:04:13 +00001648 err |= __get_user(env->fsr, &fpu->si_fsr);
1649#if 0
1650 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1651 if (current->thread.fpqdepth != 0)
1652 err |= __copy_from_user(&current->thread.fpqueue[0],
1653 &fpu->si_fpqueue[0],
1654 ((sizeof(unsigned long) +
1655 (sizeof(unsigned long *)))*16));
1656#endif
1657 return err;
1658}
1659
1660
ths5fafdf22007-09-16 21:08:06 +00001661static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001662 target_siginfo_t *info,
1663 target_sigset_t *set, CPUState *env)
1664{
1665 fprintf(stderr, "setup_rt_frame: not implemented\n");
1666}
1667
1668long do_sigreturn(CPUState *env)
1669{
1670 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001671 uint32_t up_psr, pc, npc;
bellard6d5e2162004-09-30 22:04:13 +00001672 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001673 sigset_t host_set;
blueswir1992f48a2007-10-14 16:27:31 +00001674 abi_ulong fpu_save;
bellarde80cfcf2004-12-19 23:18:01 +00001675 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001676
pbrook53a59602006-03-25 19:31:22 +00001677 sf = (struct target_signal_frame *)g2h(env->regwptr[UREG_FP]);
bellard80a9d032005-01-03 23:31:27 +00001678#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001679 fprintf(stderr, "sigreturn\n");
1680 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 +00001681#endif
bellarde80cfcf2004-12-19 23:18:01 +00001682 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001683
1684 /* 1. Make sure we are not getting garbage from the user */
1685#if 0
1686 if (verify_area (VERIFY_READ, sf, sizeof (*sf)))
1687 goto segv_and_exit;
1688#endif
1689
1690 if (((uint) sf) & 3)
1691 goto segv_and_exit;
1692
1693 err = __get_user(pc, &sf->info.si_regs.pc);
1694 err |= __get_user(npc, &sf->info.si_regs.npc);
1695
bellard6d5e2162004-09-30 22:04:13 +00001696 if ((pc | npc) & 3)
1697 goto segv_and_exit;
1698
1699 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001700 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1701
bellard6d5e2162004-09-30 22:04:13 +00001702 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001703 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1704 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1705
1706 env->pc = pc;
1707 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001708 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001709 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001710 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1711 }
bellarda315a142005-01-30 22:59:18 +00001712 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001713 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1714 }
bellard6d5e2162004-09-30 22:04:13 +00001715
blueswir1992f48a2007-10-14 16:27:31 +00001716 err |= __get_user(fpu_save, (abi_ulong *)&sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001717
bellarde80cfcf2004-12-19 23:18:01 +00001718 //if (fpu_save)
1719 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001720
1721 /* This is pretty much atomic, no amount locking would prevent
1722 * the races which exist anyways.
1723 */
1724 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001725 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1726 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1727 }
1728
1729 target_to_host_sigset_internal(&host_set, &set);
1730 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001731
1732 if (err)
1733 goto segv_and_exit;
1734
bellard6d5e2162004-09-30 22:04:13 +00001735 return env->regwptr[0];
1736
1737segv_and_exit:
1738 force_sig(TARGET_SIGSEGV);
1739}
1740
1741long do_rt_sigreturn(CPUState *env)
1742{
1743 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1744 return -ENOSYS;
1745}
1746
blueswir15bfb56b2007-10-05 17:01:51 +00001747#ifdef TARGET_SPARC64
1748#define MC_TSTATE 0
1749#define MC_PC 1
1750#define MC_NPC 2
1751#define MC_Y 3
1752#define MC_G1 4
1753#define MC_G2 5
1754#define MC_G3 6
1755#define MC_G4 7
1756#define MC_G5 8
1757#define MC_G6 9
1758#define MC_G7 10
1759#define MC_O0 11
1760#define MC_O1 12
1761#define MC_O2 13
1762#define MC_O3 14
1763#define MC_O4 15
1764#define MC_O5 16
1765#define MC_O6 17
1766#define MC_O7 18
1767#define MC_NGREG 19
1768
blueswir1992f48a2007-10-14 16:27:31 +00001769typedef abi_ulong target_mc_greg_t;
blueswir15bfb56b2007-10-05 17:01:51 +00001770typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1771
1772struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00001773 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001774 uint32_t mcfq_insn;
1775};
1776
1777struct target_mc_fpu {
1778 union {
1779 uint32_t sregs[32];
1780 uint64_t dregs[32];
1781 //uint128_t qregs[16];
1782 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00001783 abi_ulong mcfpu_fsr;
1784 abi_ulong mcfpu_fprs;
1785 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00001786 struct target_mc_fq *mcfpu_fq;
1787 unsigned char mcfpu_qcnt;
1788 unsigned char mcfpu_qentsz;
1789 unsigned char mcfpu_enab;
1790};
1791typedef struct target_mc_fpu target_mc_fpu_t;
1792
1793typedef struct {
1794 target_mc_gregset_t mc_gregs;
1795 target_mc_greg_t mc_fp;
1796 target_mc_greg_t mc_i7;
1797 target_mc_fpu_t mc_fpregs;
1798} target_mcontext_t;
1799
1800struct target_ucontext {
1801 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00001802 abi_ulong uc_flags;
blueswir15bfb56b2007-10-05 17:01:51 +00001803 target_sigset_t uc_sigmask;
1804 target_mcontext_t uc_mcontext;
1805};
1806
1807/* A V9 register window */
1808struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00001809 abi_ulong locals[8];
1810 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00001811};
1812
1813#define TARGET_STACK_BIAS 2047
1814
1815/* {set, get}context() needed for 64-bit SparcLinux userland. */
1816void sparc64_set_context(CPUSPARCState *env)
1817{
1818 struct target_ucontext *ucp = (struct target_ucontext *)
1819 env->regwptr[UREG_I0];
1820 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00001821 abi_ulong pc, npc, tstate;
1822 abi_ulong fp, i7;
blueswir15bfb56b2007-10-05 17:01:51 +00001823 unsigned char fenab;
1824 int err;
1825 unsigned int i;
blueswir1992f48a2007-10-14 16:27:31 +00001826 abi_ulong *src, *dst;
blueswir15bfb56b2007-10-05 17:01:51 +00001827
1828 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00001829 err = __get_user(pc, &((*grp)[MC_PC]));
1830 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00001831 if (err || ((pc | npc) & 3))
1832 goto do_sigsegv;
1833 if (env->regwptr[UREG_I1]) {
1834 target_sigset_t target_set;
1835 sigset_t set;
1836
1837 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00001838 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00001839 goto do_sigsegv;
1840 } else {
1841 src = &ucp->uc_sigmask;
1842 dst = &target_set;
blueswir1992f48a2007-10-14 16:27:31 +00001843 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00001844 i++, dst++, src++)
bellard579a97f2007-11-11 14:26:47 +00001845 err |= __get_user(dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00001846 if (err)
1847 goto do_sigsegv;
1848 }
1849 target_to_host_sigset_internal(&set, &target_set);
1850 sigprocmask(SIG_SETMASK, &set, NULL);
1851 }
1852 env->pc = pc;
1853 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00001854 err |= __get_user(env->y, &((*grp)[MC_Y]));
1855 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00001856 env->asi = (tstate >> 24) & 0xff;
1857 PUT_CCR(env, tstate >> 32);
1858 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00001859 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
1860 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
1861 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
1862 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
1863 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
1864 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
1865 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
1866 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
1867 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
1868 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
1869 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
1870 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
1871 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
1872 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
1873 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00001874
bellard579a97f2007-11-11 14:26:47 +00001875 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
1876 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
1877 err |= __put_user(fp,
1878 (&(((struct target_reg_window *)(TARGET_STACK_BIAS+env->regwptr[UREG_I6]))->ins[6])));
1879 err |= __put_user(i7,
1880 (&(((struct target_reg_window *)(TARGET_STACK_BIAS+env->regwptr[UREG_I6]))->ins[7])));
blueswir15bfb56b2007-10-05 17:01:51 +00001881
bellard579a97f2007-11-11 14:26:47 +00001882 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
1883 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00001884 src = &(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs);
1885 dst = &env->fpr;
1886 for (i = 0; i < 64; i++, dst++, src++)
bellard579a97f2007-11-11 14:26:47 +00001887 err |= __get_user(dst, src);
1888 err |= __get_user(env->fsr,
1889 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
1890 err |= __get_user(env->gsr,
1891 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00001892 if (err)
1893 goto do_sigsegv;
1894
1895 return;
1896 do_sigsegv:
1897 force_sig(SIGSEGV);
1898}
1899
1900void sparc64_get_context(CPUSPARCState *env)
1901{
1902 struct target_ucontext *ucp = (struct target_ucontext *)
1903 env->regwptr[UREG_I0];
1904 target_mc_gregset_t *grp;
1905 target_mcontext_t *mcp;
blueswir1992f48a2007-10-14 16:27:31 +00001906 abi_ulong fp, i7;
blueswir15bfb56b2007-10-05 17:01:51 +00001907 int err;
1908 unsigned int i;
blueswir1992f48a2007-10-14 16:27:31 +00001909 abi_ulong *src, *dst;
blueswir15bfb56b2007-10-05 17:01:51 +00001910 target_sigset_t target_set;
1911 sigset_t set;
1912
1913 mcp = &ucp->uc_mcontext;
1914 grp = &mcp->mc_gregs;
1915
1916 /* Skip over the trap instruction, first. */
1917 env->pc = env->npc;
1918 env->npc += 4;
1919
1920 err = 0;
1921
1922 sigprocmask(0, NULL, &set);
1923 host_to_target_sigset_internal(&target_set, &set);
1924 if (TARGET_NSIG_WORDS == 1)
bellard579a97f2007-11-11 14:26:47 +00001925 err |= __put_user(target_set.sig[0],
1926 (abi_ulong *)&ucp->uc_sigmask);
blueswir15bfb56b2007-10-05 17:01:51 +00001927 else {
1928 src = &target_set;
1929 dst = &ucp->uc_sigmask;
blueswir1992f48a2007-10-14 16:27:31 +00001930 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00001931 i++, dst++, src++)
bellard579a97f2007-11-11 14:26:47 +00001932 err |= __put_user(src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00001933 if (err)
1934 goto do_sigsegv;
1935 }
1936
bellard579a97f2007-11-11 14:26:47 +00001937 err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
1938 err |= __put_user(env->pc, &((*grp)[MC_PC]));
1939 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
1940 err |= __put_user(env->y, &((*grp)[MC_Y]));
1941 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
1942 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
1943 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
1944 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
1945 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
1946 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
1947 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
1948 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
1949 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
1950 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
1951 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
1952 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
1953 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
1954 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
1955 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00001956
bellard579a97f2007-11-11 14:26:47 +00001957 err |= __get_user(fp,
1958 (&(((struct target_reg_window *)(TARGET_STACK_BIAS+env->regwptr[UREG_I6]))->ins[6])));
1959 err |= __get_user(i7,
1960 (&(((struct target_reg_window *)(TARGET_STACK_BIAS+env->regwptr[UREG_I6]))->ins[7])));
1961 err |= __put_user(fp, &(mcp->mc_fp));
1962 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00001963
1964 src = &env->fpr;
1965 dst = &(ucp->uc_mcontext.mc_fpregs.mcfpu_fregs);
1966 for (i = 0; i < 64; i++, dst++, src++)
bellard579a97f2007-11-11 14:26:47 +00001967 err |= __put_user(src, dst);
1968 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
1969 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
1970 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00001971
1972 if (err)
1973 goto do_sigsegv;
1974
1975 return;
1976 do_sigsegv:
1977 force_sig(SIGSEGV);
1978}
1979#endif
thsd26bc212007-11-08 18:05:37 +00001980#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00001981
1982# warning signal handling not implemented
1983
1984static void setup_frame(int sig, struct emulated_sigaction *ka,
1985 target_sigset_t *set, CPUState *env)
1986{
1987 fprintf(stderr, "setup_frame: not implemented\n");
1988}
1989
1990static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1991 target_siginfo_t *info,
1992 target_sigset_t *set, CPUState *env)
1993{
1994 fprintf(stderr, "setup_rt_frame: not implemented\n");
1995}
1996
1997long do_sigreturn(CPUState *env)
1998{
1999 fprintf(stderr, "do_sigreturn: not implemented\n");
2000 return -ENOSYS;
2001}
2002
2003long do_rt_sigreturn(CPUState *env)
2004{
2005 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2006 return -ENOSYS;
2007}
2008
thsd26bc212007-11-08 18:05:37 +00002009#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002010
2011# warning signal handling not implemented
2012
2013static void setup_frame(int sig, struct emulated_sigaction *ka,
2014 target_sigset_t *set, CPUState *env)
2015{
2016 fprintf(stderr, "setup_frame: not implemented\n");
2017}
2018
2019static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2020 target_siginfo_t *info,
2021 target_sigset_t *set, CPUState *env)
2022{
2023 fprintf(stderr, "setup_rt_frame: not implemented\n");
2024}
2025
2026long do_sigreturn(CPUState *env)
2027{
2028 fprintf(stderr, "do_sigreturn: not implemented\n");
2029 return -ENOSYS;
2030}
2031
2032long do_rt_sigreturn(CPUState *env)
2033{
2034 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2035 return -ENOSYS;
2036}
2037
thsd26bc212007-11-08 18:05:37 +00002038#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002039
2040struct target_sigcontext {
2041 uint32_t sc_regmask; /* Unused */
2042 uint32_t sc_status;
2043 uint64_t sc_pc;
2044 uint64_t sc_regs[32];
2045 uint64_t sc_fpregs[32];
2046 uint32_t sc_ownedfp; /* Unused */
2047 uint32_t sc_fpc_csr;
2048 uint32_t sc_fpc_eir; /* Unused */
2049 uint32_t sc_used_math;
2050 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2051 uint64_t sc_mdhi;
2052 uint64_t sc_mdlo;
2053 target_ulong sc_hi1; /* Was sc_cause */
2054 target_ulong sc_lo1; /* Was sc_badvaddr */
2055 target_ulong sc_hi2; /* Was sc_sigset[4] */
2056 target_ulong sc_lo2;
2057 target_ulong sc_hi3;
2058 target_ulong sc_lo3;
2059};
2060
2061struct sigframe {
2062 uint32_t sf_ass[4]; /* argument save space for o32 */
2063 uint32_t sf_code[2]; /* signal trampoline */
2064 struct target_sigcontext sf_sc;
2065 target_sigset_t sf_mask;
2066};
2067
2068/* Install trampoline to jump back from signal handler */
2069static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2070{
2071 int err;
2072
2073 /*
2074 * Set up the return code ...
2075 *
2076 * li v0, __NR__foo_sigreturn
2077 * syscall
2078 */
2079
2080 err = __put_user(0x24020000 + syscall, tramp + 0);
2081 err |= __put_user(0x0000000c , tramp + 1);
2082 /* flush_cache_sigtramp((unsigned long) tramp); */
2083 return err;
2084}
2085
2086static inline int
2087setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2088{
2089 int err = 0;
2090
thsead93602007-09-06 00:18:15 +00002091 err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002092
thsead93602007-09-06 00:18:15 +00002093#define save_gp_reg(i) do { \
2094 err |= __put_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002095 } while(0)
2096 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2097 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2098 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2099 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2100 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2101 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2102 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2103 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2104 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002105#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002106
thsead93602007-09-06 00:18:15 +00002107 err |= __put_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
2108 err |= __put_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002109
2110 /* Not used yet, but might be useful if we ever have DSP suppport */
2111#if 0
2112 if (cpu_has_dsp) {
2113 err |= __put_user(mfhi1(), &sc->sc_hi1);
2114 err |= __put_user(mflo1(), &sc->sc_lo1);
2115 err |= __put_user(mfhi2(), &sc->sc_hi2);
2116 err |= __put_user(mflo2(), &sc->sc_lo2);
2117 err |= __put_user(mfhi3(), &sc->sc_hi3);
2118 err |= __put_user(mflo3(), &sc->sc_lo3);
2119 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2120 }
2121 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002122#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002123 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2124 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2125 if (cpu_has_dsp) {
2126 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2127 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2128 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2129 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2130 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2131 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2132 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2133 }
ths388bb212007-05-13 13:58:00 +00002134#endif
2135#endif
bellard106ec872006-06-27 21:08:10 +00002136
ths388bb212007-05-13 13:58:00 +00002137#if 0
bellard106ec872006-06-27 21:08:10 +00002138 err |= __put_user(!!used_math(), &sc->sc_used_math);
2139
2140 if (!used_math())
2141 goto out;
2142
2143 /*
2144 * Save FPU state to signal context. Signal handler will "inherit"
2145 * current FPU state.
2146 */
2147 preempt_disable();
2148
2149 if (!is_fpu_owner()) {
2150 own_fpu();
2151 restore_fp(current);
2152 }
2153 err |= save_fp_context(sc);
2154
2155 preempt_enable();
2156 out:
2157#endif
2158 return err;
2159}
2160
2161static inline int
2162restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2163{
2164 int err = 0;
2165
2166 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2167
thsead93602007-09-06 00:18:15 +00002168 err |= __get_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
2169 err |= __get_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002170
thsead93602007-09-06 00:18:15 +00002171#define restore_gp_reg(i) do { \
2172 err |= __get_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002173 } while(0)
2174 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2175 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2176 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2177 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2178 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2179 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2180 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2181 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2182 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2183 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2184 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002185#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002186
2187#if 0
2188 if (cpu_has_dsp) {
2189 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2190 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2191 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2192 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2193 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2194 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2195 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2196 }
ths388bb212007-05-13 13:58:00 +00002197#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002198 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2199 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2200 if (cpu_has_dsp) {
2201 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2202 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2203 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2204 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2205 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2206 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2207 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2208 }
ths388bb212007-05-13 13:58:00 +00002209#endif
bellard106ec872006-06-27 21:08:10 +00002210
2211 err |= __get_user(used_math, &sc->sc_used_math);
2212 conditional_used_math(used_math);
2213
2214 preempt_disable();
2215
2216 if (used_math()) {
2217 /* restore fpu context if we have used it before */
2218 own_fpu();
2219 err |= restore_fp_context(sc);
2220 } else {
2221 /* signal handler may have used FPU. Give it up. */
2222 lose_fpu();
2223 }
2224
2225 preempt_enable();
2226#endif
2227 return err;
2228}
2229/*
2230 * Determine which stack to use..
2231 */
bellard579a97f2007-11-11 14:26:47 +00002232static inline abi_ulong
bellard106ec872006-06-27 21:08:10 +00002233get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
2234{
2235 unsigned long sp;
2236
2237 /* Default to using normal stack */
thsead93602007-09-06 00:18:15 +00002238 sp = regs->gpr[29][regs->current_tc];
bellard106ec872006-06-27 21:08:10 +00002239
2240 /*
2241 * FPU emulator may have it's own trampoline active just
2242 * above the user stack, 16-bytes before the next lowest
2243 * 16 byte boundary. Try to avoid trashing it.
2244 */
2245 sp -= 32;
2246
bellard106ec872006-06-27 21:08:10 +00002247 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00002248 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2249 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2250 }
bellard106ec872006-06-27 21:08:10 +00002251
bellard579a97f2007-11-11 14:26:47 +00002252 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002253}
2254
bellard579a97f2007-11-11 14:26:47 +00002255/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
ths5fafdf22007-09-16 21:08:06 +00002256static void setup_frame(int sig, struct emulated_sigaction * ka,
bellard579a97f2007-11-11 14:26:47 +00002257 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002258{
2259 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002260 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002261 int i;
2262
bellard579a97f2007-11-11 14:26:47 +00002263 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2264 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002265 goto give_sigsegv;
2266
2267 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2268
2269 if(setup_sigcontext(regs, &frame->sf_sc))
2270 goto give_sigsegv;
2271
2272 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2273 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2274 goto give_sigsegv;
2275 }
2276
2277 /*
2278 * Arguments to signal handler:
2279 *
2280 * a0 = signal number
2281 * a1 = 0 (should be cause)
2282 * a2 = pointer to struct sigcontext
2283 *
2284 * $25 and PC point to the signal handler, $29 points to the
2285 * struct sigframe.
2286 */
thsead93602007-09-06 00:18:15 +00002287 regs->gpr[ 4][regs->current_tc] = sig;
2288 regs->gpr[ 5][regs->current_tc] = 0;
2289 regs->gpr[ 6][regs->current_tc] = h2g(&frame->sf_sc);
2290 regs->gpr[29][regs->current_tc] = h2g(frame);
2291 regs->gpr[31][regs->current_tc] = h2g(frame->sf_code);
bellard106ec872006-06-27 21:08:10 +00002292 /* The original kernel code sets CP0_EPC to the handler
2293 * since it returns to userland using eret
2294 * we cannot do this here, and we must set PC directly */
thsead93602007-09-06 00:18:15 +00002295 regs->PC[regs->current_tc] = regs->gpr[25][regs->current_tc] = ka->sa._sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002296 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002297 return;
2298
2299give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002300 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002301 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002302 return;
bellard106ec872006-06-27 21:08:10 +00002303}
2304
2305long do_sigreturn(CPUState *regs)
2306{
ths388bb212007-05-13 13:58:00 +00002307 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002308 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002309 sigset_t blocked;
2310 target_sigset_t target_set;
2311 int i;
bellard106ec872006-06-27 21:08:10 +00002312
2313#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002314 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002315#endif
bellard579a97f2007-11-11 14:26:47 +00002316 frame_addr = regs->gpr[29][regs->current_tc];
2317 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002318 goto badframe;
2319
ths388bb212007-05-13 13:58:00 +00002320 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002321 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2322 goto badframe;
ths388bb212007-05-13 13:58:00 +00002323 }
bellard106ec872006-06-27 21:08:10 +00002324
ths388bb212007-05-13 13:58:00 +00002325 target_to_host_sigset_internal(&blocked, &target_set);
2326 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002327
ths388bb212007-05-13 13:58:00 +00002328 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002329 goto badframe;
2330
2331#if 0
ths388bb212007-05-13 13:58:00 +00002332 /*
2333 * Don't let your children do this ...
2334 */
2335 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002336 "move\t$29, %0\n\t"
2337 "j\tsyscall_exit"
2338 :/* no outputs */
2339 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002340 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002341#endif
ths3b46e622007-09-17 08:09:54 +00002342
thsead93602007-09-06 00:18:15 +00002343 regs->PC[regs->current_tc] = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002344 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002345 * maybe a problem with nested signals ? */
2346 regs->CP0_EPC = 0;
2347 return 0;
2348
2349badframe:
ths388bb212007-05-13 13:58:00 +00002350 force_sig(TARGET_SIGSEGV/*, current*/);
2351 return 0;
bellard106ec872006-06-27 21:08:10 +00002352}
2353
ths5fafdf22007-09-16 21:08:06 +00002354static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard106ec872006-06-27 21:08:10 +00002355 target_siginfo_t *info,
2356 target_sigset_t *set, CPUState *env)
2357{
2358 fprintf(stderr, "setup_rt_frame: not implemented\n");
2359}
2360
2361long do_rt_sigreturn(CPUState *env)
2362{
2363 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2364 return -ENOSYS;
2365}
bellard6d5e2162004-09-30 22:04:13 +00002366
bellardb346ff42003-06-15 20:05:50 +00002367#else
2368
2369static void setup_frame(int sig, struct emulated_sigaction *ka,
2370 target_sigset_t *set, CPUState *env)
2371{
2372 fprintf(stderr, "setup_frame: not implemented\n");
2373}
2374
ths5fafdf22007-09-16 21:08:06 +00002375static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00002376 target_siginfo_t *info,
2377 target_sigset_t *set, CPUState *env)
2378{
2379 fprintf(stderr, "setup_rt_frame: not implemented\n");
2380}
2381
2382long do_sigreturn(CPUState *env)
2383{
2384 fprintf(stderr, "do_sigreturn: not implemented\n");
2385 return -ENOSYS;
2386}
2387
2388long do_rt_sigreturn(CPUState *env)
2389{
2390 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2391 return -ENOSYS;
2392}
2393
bellard66fb9762003-03-23 01:06:05 +00002394#endif
2395
2396void process_pending_signals(void *cpu_env)
2397{
2398 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00002399 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00002400 sigset_t set, old_set;
2401 target_sigset_t target_old_set;
bellard66fb9762003-03-23 01:06:05 +00002402 struct emulated_sigaction *k;
2403 struct sigqueue *q;
ths3b46e622007-09-17 08:09:54 +00002404
bellard31e31b82003-02-18 22:55:36 +00002405 if (!signal_pending)
2406 return;
2407
bellard66fb9762003-03-23 01:06:05 +00002408 k = sigact_table;
2409 for(sig = 1; sig <= TARGET_NSIG; sig++) {
2410 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00002411 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00002412 k++;
bellard31e31b82003-02-18 22:55:36 +00002413 }
2414 /* if no signal is pending, just return */
2415 signal_pending = 0;
2416 return;
bellard66fb9762003-03-23 01:06:05 +00002417
bellard31e31b82003-02-18 22:55:36 +00002418 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00002419#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00002420 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00002421#endif
2422 /* dequeue signal */
2423 q = k->first;
2424 k->first = q->next;
2425 if (!k->first)
2426 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00002427
bellard1fddef42005-04-17 19:16:13 +00002428 sig = gdb_handlesig (cpu_env, sig);
2429 if (!sig) {
2430 fprintf (stderr, "Lost signal\n");
2431 abort();
2432 }
bellard66fb9762003-03-23 01:06:05 +00002433
2434 handler = k->sa._sa_handler;
2435 if (handler == TARGET_SIG_DFL) {
2436 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +00002437 if (sig != TARGET_SIGCHLD &&
2438 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +00002439 sig != TARGET_SIGWINCH) {
2440 force_sig(sig);
2441 }
2442 } else if (handler == TARGET_SIG_IGN) {
2443 /* ignore sig */
2444 } else if (handler == TARGET_SIG_ERR) {
2445 force_sig(sig);
2446 } else {
bellard9de5e442003-03-23 16:49:39 +00002447 /* compute the blocked signals during the handler execution */
2448 target_to_host_sigset(&set, &k->sa.sa_mask);
2449 /* SA_NODEFER indicates that the current signal should not be
2450 blocked during the handler */
2451 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
2452 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00002453
bellard9de5e442003-03-23 16:49:39 +00002454 /* block signals in the handler using Linux */
2455 sigprocmask(SIG_BLOCK, &set, &old_set);
2456 /* save the previous blocked signal state to restore it at the
2457 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00002458 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00002459
bellardbc8a22c2003-03-30 21:02:40 +00002460 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00002461#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00002462 {
2463 CPUX86State *env = cpu_env;
2464 if (env->eflags & VM_MASK)
2465 save_v86_state(env);
2466 }
2467#endif
bellard9de5e442003-03-23 16:49:39 +00002468 /* prepare the stack frame of the virtual CPU */
bellard66fb9762003-03-23 01:06:05 +00002469 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
bellard9de5e442003-03-23 16:49:39 +00002470 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00002471 else
bellard9de5e442003-03-23 16:49:39 +00002472 setup_frame(sig, k, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00002473 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
2474 k->sa._sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00002475 }
bellard66fb9762003-03-23 01:06:05 +00002476 if (q != &k->info)
2477 free_sigqueue(q);
bellard31e31b82003-02-18 22:55:36 +00002478}