blob: c7b1ea89fffed5da9e1b2ed31dae49bb220102db [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard66fb9762003-03-23 01:06:05 +00002 * Emulation of Linux signals
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard31e31b82003-02-18 22:55:36 +00004 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <stdlib.h>
21#include <stdio.h>
bellard66fb9762003-03-23 01:06:05 +000022#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000023#include <stdarg.h>
bellard2677e102003-04-10 00:03:27 +000024#include <unistd.h>
bellard31e31b82003-02-18 22:55:36 +000025#include <signal.h>
bellard66fb9762003-03-23 01:06:05 +000026#include <errno.h>
bellard31e31b82003-02-18 22:55:36 +000027#include <sys/ucontext.h>
28
bellard3ef693a2003-03-23 20:17:16 +000029#include "qemu.h"
blueswir1992f48a2007-10-14 16:27:31 +000030#include "target_signal.h"
bellard66fb9762003-03-23 01:06:05 +000031
32//#define DEBUG_SIGNAL
33
34#define MAX_SIGQUEUE_SIZE 1024
35
36struct sigqueue {
37 struct sigqueue *next;
bellard9de5e442003-03-23 16:49:39 +000038 target_siginfo_t info;
bellard66fb9762003-03-23 01:06:05 +000039};
bellard31e31b82003-02-18 22:55:36 +000040
41struct emulated_sigaction {
42 struct target_sigaction sa;
bellard66fb9762003-03-23 01:06:05 +000043 int pending; /* true if signal is pending */
44 struct sigqueue *first;
45 struct sigqueue info; /* in order to always have memory for the
46 first signal, we put it here */
bellard31e31b82003-02-18 22:55:36 +000047};
48
thsa04e1342007-09-27 13:57:58 +000049struct target_sigaltstack target_sigaltstack_used = {
50 .ss_sp = 0,
51 .ss_size = 0,
52 .ss_flags = TARGET_SS_DISABLE,
53};
54
bellard66fb9762003-03-23 01:06:05 +000055static struct emulated_sigaction sigact_table[TARGET_NSIG];
56static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57static struct sigqueue *first_free; /* first free siginfo queue entry */
58static int signal_pending; /* non zero if a signal may be pending */
bellard31e31b82003-02-18 22:55:36 +000059
ths5fafdf22007-09-16 21:08:06 +000060static void host_signal_handler(int host_signum, siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +000061 void *puc);
62
bellard9e5f5282003-07-13 17:33:54 +000063static uint8_t host_to_target_signal_table[65] = {
64 [SIGHUP] = TARGET_SIGHUP,
65 [SIGINT] = TARGET_SIGINT,
66 [SIGQUIT] = TARGET_SIGQUIT,
67 [SIGILL] = TARGET_SIGILL,
68 [SIGTRAP] = TARGET_SIGTRAP,
69 [SIGABRT] = TARGET_SIGABRT,
bellard01e3b762003-09-30 21:10:14 +000070/* [SIGIOT] = TARGET_SIGIOT,*/
bellard9e5f5282003-07-13 17:33:54 +000071 [SIGBUS] = TARGET_SIGBUS,
72 [SIGFPE] = TARGET_SIGFPE,
73 [SIGKILL] = TARGET_SIGKILL,
74 [SIGUSR1] = TARGET_SIGUSR1,
75 [SIGSEGV] = TARGET_SIGSEGV,
76 [SIGUSR2] = TARGET_SIGUSR2,
77 [SIGPIPE] = TARGET_SIGPIPE,
78 [SIGALRM] = TARGET_SIGALRM,
79 [SIGTERM] = TARGET_SIGTERM,
80#ifdef SIGSTKFLT
81 [SIGSTKFLT] = TARGET_SIGSTKFLT,
82#endif
83 [SIGCHLD] = TARGET_SIGCHLD,
84 [SIGCONT] = TARGET_SIGCONT,
85 [SIGSTOP] = TARGET_SIGSTOP,
86 [SIGTSTP] = TARGET_SIGTSTP,
87 [SIGTTIN] = TARGET_SIGTTIN,
88 [SIGTTOU] = TARGET_SIGTTOU,
89 [SIGURG] = TARGET_SIGURG,
90 [SIGXCPU] = TARGET_SIGXCPU,
91 [SIGXFSZ] = TARGET_SIGXFSZ,
92 [SIGVTALRM] = TARGET_SIGVTALRM,
93 [SIGPROF] = TARGET_SIGPROF,
94 [SIGWINCH] = TARGET_SIGWINCH,
95 [SIGIO] = TARGET_SIGIO,
96 [SIGPWR] = TARGET_SIGPWR,
97 [SIGSYS] = TARGET_SIGSYS,
98 /* next signals stay the same */
99};
100static uint8_t target_to_host_signal_table[65];
101
thsa04e1342007-09-27 13:57:58 +0000102static inline int on_sig_stack(unsigned long sp)
103{
104 return (sp - target_sigaltstack_used.ss_sp
105 < target_sigaltstack_used.ss_size);
106}
107
108static inline int sas_ss_flags(unsigned long sp)
109{
110 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
111 : on_sig_stack(sp) ? SS_ONSTACK : 0);
112}
113
bellard31e31b82003-02-18 22:55:36 +0000114static inline int host_to_target_signal(int sig)
115{
bellard9e5f5282003-07-13 17:33:54 +0000116 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000117}
118
119static inline int target_to_host_signal(int sig)
120{
bellard9e5f5282003-07-13 17:33:54 +0000121 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000122}
123
ths5fafdf22007-09-16 21:08:06 +0000124static void host_to_target_sigset_internal(target_sigset_t *d,
bellard92319442004-06-19 16:58:13 +0000125 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000126{
127 int i;
bellard9e5f5282003-07-13 17:33:54 +0000128 unsigned long sigmask;
129 uint32_t target_sigmask;
ths3b46e622007-09-17 08:09:54 +0000130
bellard9e5f5282003-07-13 17:33:54 +0000131 sigmask = ((unsigned long *)s)[0];
132 target_sigmask = 0;
133 for(i = 0; i < 32; i++) {
ths5fafdf22007-09-16 21:08:06 +0000134 if (sigmask & (1 << i))
bellard9e5f5282003-07-13 17:33:54 +0000135 target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
136 }
blueswir1992f48a2007-10-14 16:27:31 +0000137#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
bellard92319442004-06-19 16:58:13 +0000138 d->sig[0] = target_sigmask;
bellard9e5f5282003-07-13 17:33:54 +0000139 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard92319442004-06-19 16:58:13 +0000140 d->sig[i] = ((unsigned long *)s)[i];
bellard66fb9762003-03-23 01:06:05 +0000141 }
blueswir1992f48a2007-10-14 16:27:31 +0000142#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
bellard92319442004-06-19 16:58:13 +0000143 d->sig[0] = target_sigmask;
144 d->sig[1] = sigmask >> 32;
bellard9e5f5282003-07-13 17:33:54 +0000145#else
bellard459a4012007-11-11 19:45:10 +0000146 /* XXX: do it */
bellard9e5f5282003-07-13 17:33:54 +0000147#endif
bellard66fb9762003-03-23 01:06:05 +0000148}
149
bellard92319442004-06-19 16:58:13 +0000150void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
151{
152 target_sigset_t d1;
153 int i;
154
155 host_to_target_sigset_internal(&d1, s);
156 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000157 d->sig[i] = tswapl(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000158}
159
160void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000161{
162 int i;
bellard9e5f5282003-07-13 17:33:54 +0000163 unsigned long sigmask;
blueswir1992f48a2007-10-14 16:27:31 +0000164 abi_ulong target_sigmask;
bellard9e5f5282003-07-13 17:33:54 +0000165
bellard92319442004-06-19 16:58:13 +0000166 target_sigmask = s->sig[0];
bellard9e5f5282003-07-13 17:33:54 +0000167 sigmask = 0;
168 for(i = 0; i < 32; i++) {
ths5fafdf22007-09-16 21:08:06 +0000169 if (target_sigmask & (1 << i))
bellard9e5f5282003-07-13 17:33:54 +0000170 sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
171 }
blueswir1992f48a2007-10-14 16:27:31 +0000172#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
bellard9e5f5282003-07-13 17:33:54 +0000173 ((unsigned long *)d)[0] = sigmask;
174 for(i = 1;i < TARGET_NSIG_WORDS; i++) {
bellard92319442004-06-19 16:58:13 +0000175 ((unsigned long *)d)[i] = s->sig[i];
bellard66fb9762003-03-23 01:06:05 +0000176 }
blueswir1992f48a2007-10-14 16:27:31 +0000177#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
bellard92319442004-06-19 16:58:13 +0000178 ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
bellard9e5f5282003-07-13 17:33:54 +0000179#else
bellard459a4012007-11-11 19:45:10 +0000180 /* XXX: do it */
blueswir1992f48a2007-10-14 16:27:31 +0000181#endif /* TARGET_ABI_BITS */
bellard66fb9762003-03-23 01:06:05 +0000182}
183
bellard92319442004-06-19 16:58:13 +0000184void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
185{
186 target_sigset_t s1;
187 int i;
188
189 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000190 s1.sig[i] = tswapl(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000191 target_to_host_sigset_internal(d, &s1);
192}
ths3b46e622007-09-17 08:09:54 +0000193
blueswir1992f48a2007-10-14 16:27:31 +0000194void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000195 const sigset_t *sigset)
196{
bellard9e5f5282003-07-13 17:33:54 +0000197 target_sigset_t d;
198 host_to_target_sigset(&d, sigset);
199 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000200}
201
ths5fafdf22007-09-16 21:08:06 +0000202void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000203 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000204{
bellard9e5f5282003-07-13 17:33:54 +0000205 target_sigset_t d;
206 int i;
207
208 d.sig[0] = *old_sigset;
209 for(i = 1;i < TARGET_NSIG_WORDS; i++)
210 d.sig[i] = 0;
211 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000212}
213
bellard9de5e442003-03-23 16:49:39 +0000214/* siginfo conversion */
215
ths5fafdf22007-09-16 21:08:06 +0000216static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000217 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000218{
bellard9de5e442003-03-23 16:49:39 +0000219 int sig;
220 sig = host_to_target_signal(info->si_signo);
221 tinfo->si_signo = sig;
222 tinfo->si_errno = 0;
223 tinfo->si_code = 0;
ths5fafdf22007-09-16 21:08:06 +0000224 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000225 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000226 /* should never come here, but who knows. The information for
227 the target is irrelevant */
228 tinfo->_sifields._sigfault._addr = 0;
ths7f7f7c82007-07-12 11:02:46 +0000229 } else if (sig == SIGIO) {
230 tinfo->_sifields._sigpoll._fd = info->si_fd;
bellard9de5e442003-03-23 16:49:39 +0000231 } else if (sig >= TARGET_SIGRTMIN) {
232 tinfo->_sifields._rt._pid = info->si_pid;
233 tinfo->_sifields._rt._uid = info->si_uid;
234 /* XXX: potential problem if 64 bit */
ths5fafdf22007-09-16 21:08:06 +0000235 tinfo->_sifields._rt._sigval.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000236 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000237 }
bellard66fb9762003-03-23 01:06:05 +0000238}
239
ths5fafdf22007-09-16 21:08:06 +0000240static void tswap_siginfo(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000241 const target_siginfo_t *info)
242{
243 int sig;
244 sig = info->si_signo;
245 tinfo->si_signo = tswap32(sig);
246 tinfo->si_errno = tswap32(info->si_errno);
247 tinfo->si_code = tswap32(info->si_code);
ths5fafdf22007-09-16 21:08:06 +0000248 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000249 sig == SIGBUS || sig == SIGTRAP) {
ths5fafdf22007-09-16 21:08:06 +0000250 tinfo->_sifields._sigfault._addr =
bellard9de5e442003-03-23 16:49:39 +0000251 tswapl(info->_sifields._sigfault._addr);
ths7f7f7c82007-07-12 11:02:46 +0000252 } else if (sig == SIGIO) {
253 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
bellard9de5e442003-03-23 16:49:39 +0000254 } else if (sig >= TARGET_SIGRTMIN) {
255 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
256 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000257 tinfo->_sifields._rt._sigval.sival_ptr =
bellard9de5e442003-03-23 16:49:39 +0000258 tswapl(info->_sifields._rt._sigval.sival_ptr);
259 }
260}
261
262
263void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
264{
265 host_to_target_siginfo_noswap(tinfo, info);
266 tswap_siginfo(tinfo, tinfo);
267}
268
269/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000270/* XXX: find a solution for 64 bit (additional malloced data is needed) */
bellard9de5e442003-03-23 16:49:39 +0000271void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000272{
273 info->si_signo = tswap32(tinfo->si_signo);
274 info->si_errno = tswap32(tinfo->si_errno);
275 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000276 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
277 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000278 info->si_value.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000279 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000280}
281
bellard31e31b82003-02-18 22:55:36 +0000282void signal_init(void)
283{
284 struct sigaction act;
bellard9e5f5282003-07-13 17:33:54 +0000285 int i, j;
bellard31e31b82003-02-18 22:55:36 +0000286
bellard9e5f5282003-07-13 17:33:54 +0000287 /* generate signal conversion tables */
288 for(i = 1; i <= 64; i++) {
289 if (host_to_target_signal_table[i] == 0)
290 host_to_target_signal_table[i] = i;
291 }
292 for(i = 1; i <= 64; i++) {
293 j = host_to_target_signal_table[i];
294 target_to_host_signal_table[j] = i;
295 }
ths3b46e622007-09-17 08:09:54 +0000296
bellard9de5e442003-03-23 16:49:39 +0000297 /* set all host signal handlers. ALL signals are blocked during
298 the handlers to serialize them. */
299 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000300 act.sa_flags = SA_SIGINFO;
301 act.sa_sigaction = host_signal_handler;
302 for(i = 1; i < NSIG; i++) {
bellardc9087c22003-05-27 23:25:41 +0000303 sigaction(i, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000304 }
ths3b46e622007-09-17 08:09:54 +0000305
bellard31e31b82003-02-18 22:55:36 +0000306 memset(sigact_table, 0, sizeof(sigact_table));
bellard66fb9762003-03-23 01:06:05 +0000307
308 first_free = &sigqueue_table[0];
ths5fafdf22007-09-16 21:08:06 +0000309 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
bellard66fb9762003-03-23 01:06:05 +0000310 sigqueue_table[i].next = &sigqueue_table[i + 1];
311 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
bellard31e31b82003-02-18 22:55:36 +0000312}
313
bellard66fb9762003-03-23 01:06:05 +0000314/* signal queue handling */
315
316static inline struct sigqueue *alloc_sigqueue(void)
317{
318 struct sigqueue *q = first_free;
319 if (!q)
320 return NULL;
321 first_free = q->next;
322 return q;
323}
324
325static inline void free_sigqueue(struct sigqueue *q)
326{
327 q->next = first_free;
328 first_free = q;
329}
330
bellard9de5e442003-03-23 16:49:39 +0000331/* abort execution with signal */
332void __attribute((noreturn)) force_sig(int sig)
bellard66fb9762003-03-23 01:06:05 +0000333{
334 int host_sig;
bellard66fb9762003-03-23 01:06:05 +0000335 host_sig = target_to_host_signal(sig);
ths5fafdf22007-09-16 21:08:06 +0000336 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
bellard66fb9762003-03-23 01:06:05 +0000337 sig, strsignal(host_sig));
bellard9de5e442003-03-23 16:49:39 +0000338#if 1
bellard66fb9762003-03-23 01:06:05 +0000339 _exit(-host_sig);
bellard9de5e442003-03-23 16:49:39 +0000340#else
341 {
342 struct sigaction act;
343 sigemptyset(&act.sa_mask);
344 act.sa_flags = SA_SIGINFO;
345 act.sa_sigaction = SIG_DFL;
346 sigaction(SIGABRT, &act, NULL);
347 abort();
348 }
349#endif
bellard66fb9762003-03-23 01:06:05 +0000350}
351
bellard9de5e442003-03-23 16:49:39 +0000352/* queue a signal so that it will be send to the virtual CPU as soon
353 as possible */
354int queue_signal(int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000355{
bellard66fb9762003-03-23 01:06:05 +0000356 struct emulated_sigaction *k;
bellard9de5e442003-03-23 16:49:39 +0000357 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000358 abi_ulong handler;
bellard66fb9762003-03-23 01:06:05 +0000359
bellard9de5e442003-03-23 16:49:39 +0000360#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000361 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000362 sig);
bellard66fb9762003-03-23 01:06:05 +0000363#endif
bellard9de5e442003-03-23 16:49:39 +0000364 k = &sigact_table[sig - 1];
bellard66fb9762003-03-23 01:06:05 +0000365 handler = k->sa._sa_handler;
366 if (handler == TARGET_SIG_DFL) {
367 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000368 if (sig != TARGET_SIGCHLD &&
369 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +0000370 sig != TARGET_SIGWINCH) {
371 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000372 } else {
373 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000374 }
375 } else if (handler == TARGET_SIG_IGN) {
376 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000377 return 0;
bellard66fb9762003-03-23 01:06:05 +0000378 } else if (handler == TARGET_SIG_ERR) {
379 force_sig(sig);
380 } else {
bellard9de5e442003-03-23 16:49:39 +0000381 pq = &k->first;
382 if (sig < TARGET_SIGRTMIN) {
383 /* if non real time signal, we queue exactly one signal */
384 if (!k->pending)
385 q = &k->info;
386 else
387 return 0;
388 } else {
389 if (!k->pending) {
390 /* first signal */
391 q = &k->info;
392 } else {
393 q = alloc_sigqueue();
394 if (!q)
395 return -EAGAIN;
396 while (*pq != NULL)
397 pq = &(*pq)->next;
398 }
399 }
400 *pq = q;
401 q->info = *info;
402 q->next = NULL;
403 k->pending = 1;
404 /* signal that a new signal is pending */
405 signal_pending = 1;
406 return 1; /* indicates that the signal was queued */
407 }
408}
409
ths5fafdf22007-09-16 21:08:06 +0000410static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000411 void *puc)
412{
413 int sig;
414 target_siginfo_t tinfo;
415
416 /* the CPU emulator uses some host signals to detect exceptions,
417 we we forward to it some signals */
bellardec6338b2007-11-08 14:25:03 +0000418 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
bellardb346ff42003-06-15 20:05:50 +0000419 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000420 return;
421 }
422
423 /* get target signal number */
424 sig = host_to_target_signal(host_signum);
425 if (sig < 1 || sig > TARGET_NSIG)
426 return;
427#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000428 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000429#endif
430 host_to_target_siginfo_noswap(&tinfo, info);
431 if (queue_signal(sig, &tinfo) == 1) {
432 /* interrupt the virtual CPU as soon as possible */
bellard68a79312003-06-30 13:12:32 +0000433 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
bellard66fb9762003-03-23 01:06:05 +0000434 }
bellard31e31b82003-02-18 22:55:36 +0000435}
436
ths0da46a62007-10-20 20:23:07 +0000437/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000438/* compare linux/kernel/signal.c:do_sigaltstack() */
439abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000440{
441 int ret;
442 struct target_sigaltstack oss;
443
444 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000445 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000446 {
447 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
448 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
449 __put_user(sas_ss_flags(sp), &oss.ss_flags);
450 }
451
bellard579a97f2007-11-11 14:26:47 +0000452 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000453 {
bellard579a97f2007-11-11 14:26:47 +0000454 struct target_sigaltstack *uss;
455 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000456
ths0da46a62007-10-20 20:23:07 +0000457 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000458 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000459 || __get_user(ss.ss_sp, &uss->ss_sp)
460 || __get_user(ss.ss_size, &uss->ss_size)
461 || __get_user(ss.ss_flags, &uss->ss_flags))
462 goto out;
bellard579a97f2007-11-11 14:26:47 +0000463 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000464
ths0da46a62007-10-20 20:23:07 +0000465 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000466 if (on_sig_stack(sp))
467 goto out;
468
ths0da46a62007-10-20 20:23:07 +0000469 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000470 if (ss.ss_flags != TARGET_SS_DISABLE
471 && ss.ss_flags != TARGET_SS_ONSTACK
472 && ss.ss_flags != 0)
473 goto out;
474
475 if (ss.ss_flags == TARGET_SS_DISABLE) {
476 ss.ss_size = 0;
477 ss.ss_sp = 0;
478 } else {
ths0da46a62007-10-20 20:23:07 +0000479 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000480 if (ss.ss_size < MINSIGSTKSZ)
481 goto out;
482 }
483
484 target_sigaltstack_used.ss_sp = ss.ss_sp;
485 target_sigaltstack_used.ss_size = ss.ss_size;
486 }
487
bellard579a97f2007-11-11 14:26:47 +0000488 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000489 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000490 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000491 goto out;
thsa04e1342007-09-27 13:57:58 +0000492 }
493
494 ret = 0;
495out:
496 return ret;
497}
498
ths0da46a62007-10-20 20:23:07 +0000499/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000500int do_sigaction(int sig, const struct target_sigaction *act,
501 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000502{
bellard66fb9762003-03-23 01:06:05 +0000503 struct emulated_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000504 struct sigaction act1;
505 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000506 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000507
ths0aeaa8c2007-03-31 19:29:06 +0000508 if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000509 return -EINVAL;
510 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000511#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000512 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
bellard66fb9762003-03-23 01:06:05 +0000513 sig, (int)act, (int)oact);
514#endif
515 if (oact) {
516 oact->_sa_handler = tswapl(k->sa._sa_handler);
517 oact->sa_flags = tswapl(k->sa.sa_flags);
ths388bb212007-05-13 13:58:00 +0000518#if !defined(TARGET_MIPS)
519 oact->sa_restorer = tswapl(k->sa.sa_restorer);
520#endif
bellard66fb9762003-03-23 01:06:05 +0000521 oact->sa_mask = k->sa.sa_mask;
522 }
523 if (act) {
524 k->sa._sa_handler = tswapl(act->_sa_handler);
525 k->sa.sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000526#if !defined(TARGET_MIPS)
527 k->sa.sa_restorer = tswapl(act->sa_restorer);
528#endif
bellard66fb9762003-03-23 01:06:05 +0000529 k->sa.sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000530
531 /* we update the host linux signal state */
532 host_sig = target_to_host_signal(sig);
533 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
534 sigfillset(&act1.sa_mask);
535 act1.sa_flags = SA_SIGINFO;
536 if (k->sa.sa_flags & TARGET_SA_RESTART)
537 act1.sa_flags |= SA_RESTART;
538 /* NOTE: it is important to update the host kernel signal
539 ignore state to avoid getting unexpected interrupted
540 syscalls */
541 if (k->sa._sa_handler == TARGET_SIG_IGN) {
542 act1.sa_sigaction = (void *)SIG_IGN;
543 } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
544 act1.sa_sigaction = (void *)SIG_DFL;
545 } else {
546 act1.sa_sigaction = host_signal_handler;
547 }
ths0da46a62007-10-20 20:23:07 +0000548 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000549 }
bellard66fb9762003-03-23 01:06:05 +0000550 }
ths0da46a62007-10-20 20:23:07 +0000551 return ret;
bellard66fb9762003-03-23 01:06:05 +0000552}
bellard31e31b82003-02-18 22:55:36 +0000553
bellard43fff232003-07-09 19:31:39 +0000554#ifndef offsetof
555#define offsetof(type, field) ((size_t) &((type *)0)->field)
556#endif
557
ths5fafdf22007-09-16 21:08:06 +0000558static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
bellard43fff232003-07-09 19:31:39 +0000559 const target_siginfo_t *info)
560{
561 tswap_siginfo(tinfo, info);
562 return 0;
563}
564
bellard459a4012007-11-11 19:45:10 +0000565#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000566
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));
bellard66fb9762003-03-23 01:06:05 +0000776 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000777 val16 = 0x80cd;
778 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000779 }
780
781 if (err)
782 goto give_sigsegv;
783
784 /* Set up registers for signal handler */
pbrook53a59602006-03-25 19:31:22 +0000785 env->regs[R_ESP] = h2g(frame);
bellard66fb9762003-03-23 01:06:05 +0000786 env->eip = (unsigned long) ka->sa._sa_handler;
787
788 cpu_x86_load_seg(env, R_DS, __USER_DS);
789 cpu_x86_load_seg(env, R_ES, __USER_DS);
790 cpu_x86_load_seg(env, R_SS, __USER_DS);
791 cpu_x86_load_seg(env, R_CS, __USER_CS);
792 env->eflags &= ~TF_MASK;
793
bellard579a97f2007-11-11 14:26:47 +0000794 unlock_user_struct(frame, frame_addr, 1);
795
bellard66fb9762003-03-23 01:06:05 +0000796 return;
797
798give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000799 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000800 if (sig == TARGET_SIGSEGV)
801 ka->sa._sa_handler = TARGET_SIG_DFL;
802 force_sig(TARGET_SIGSEGV /* , current */);
803}
804
bellard579a97f2007-11-11 14:26:47 +0000805/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
ths5fafdf22007-09-16 21:08:06 +0000806static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard9de5e442003-03-23 16:49:39 +0000807 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000808 target_sigset_t *set, CPUX86State *env)
809{
bellard579a97f2007-11-11 14:26:47 +0000810 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000811 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000812 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000813
bellard579a97f2007-11-11 14:26:47 +0000814 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000815
bellard579a97f2007-11-11 14:26:47 +0000816 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000817 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000818
819 err |= __put_user((/*current->exec_domain
820 && current->exec_domain->signal_invmap
821 && sig < 32
822 ? current->exec_domain->signal_invmap[sig]
823 : */sig),
824 &frame->sig);
blueswir1992f48a2007-10-14 16:27:31 +0000825 err |= __put_user((abi_ulong)&frame->info, &frame->pinfo);
826 err |= __put_user((abi_ulong)&frame->uc, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000827 err |= copy_siginfo_to_user(&frame->info, info);
828 if (err)
829 goto give_sigsegv;
830
831 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000832 err |= __put_user(0, &frame->uc.tuc_flags);
833 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000834 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000835 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000836 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000837 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000838 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000839 &frame->uc.tuc_stack.ss_size);
840 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard66fb9762003-03-23 01:06:05 +0000841 env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +0000842 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000843 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000844 goto give_sigsegv;
845 }
bellard66fb9762003-03-23 01:06:05 +0000846
847 /* Set up to return from userspace. If provided, use a stub
848 already in userspace. */
849 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
850 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
851 } else {
bellard775b58d2007-11-11 16:22:17 +0000852 uint16_t val16;
853
bellard66fb9762003-03-23 01:06:05 +0000854 err |= __put_user(frame->retcode, &frame->pretcode);
855 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000856 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000857 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000858 val16 = 0x80cd;
859 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000860 }
861
862 if (err)
863 goto give_sigsegv;
864
865 /* Set up registers for signal handler */
866 env->regs[R_ESP] = (unsigned long) frame;
867 env->eip = (unsigned long) ka->sa._sa_handler;
868
869 cpu_x86_load_seg(env, R_DS, __USER_DS);
870 cpu_x86_load_seg(env, R_ES, __USER_DS);
871 cpu_x86_load_seg(env, R_SS, __USER_DS);
872 cpu_x86_load_seg(env, R_CS, __USER_CS);
873 env->eflags &= ~TF_MASK;
874
bellard579a97f2007-11-11 14:26:47 +0000875 unlock_user_struct(frame, frame_addr, 1);
876
bellard66fb9762003-03-23 01:06:05 +0000877 return;
878
879give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000880 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000881 if (sig == TARGET_SIGSEGV)
882 ka->sa._sa_handler = TARGET_SIG_DFL;
883 force_sig(TARGET_SIGSEGV /* , current */);
884}
885
886static int
887restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
888{
889 unsigned int err = 0;
890
bellard66fb9762003-03-23 01:06:05 +0000891 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
892 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
893 cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
894 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
895
896 env->regs[R_EDI] = ldl(&sc->edi);
897 env->regs[R_ESI] = ldl(&sc->esi);
898 env->regs[R_EBP] = ldl(&sc->ebp);
899 env->regs[R_ESP] = ldl(&sc->esp);
900 env->regs[R_EBX] = ldl(&sc->ebx);
901 env->regs[R_EDX] = ldl(&sc->edx);
902 env->regs[R_ECX] = ldl(&sc->ecx);
903 env->eip = ldl(&sc->eip);
904
905 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
906 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000907
bellard66fb9762003-03-23 01:06:05 +0000908 {
909 unsigned int tmpflags;
910 tmpflags = ldl(&sc->eflags);
911 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
912 // regs->orig_eax = -1; /* disable syscall checks */
913 }
914
bellard66fb9762003-03-23 01:06:05 +0000915 {
916 struct _fpstate * buf;
bellarded2dcdf2003-05-29 20:06:27 +0000917 buf = (void *)ldl(&sc->fpstate);
bellard66fb9762003-03-23 01:06:05 +0000918 if (buf) {
bellarded2dcdf2003-05-29 20:06:27 +0000919#if 0
bellard66fb9762003-03-23 01:06:05 +0000920 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
921 goto badframe;
bellarded2dcdf2003-05-29 20:06:27 +0000922#endif
923 cpu_x86_frstor(env, (void *)buf, 1);
bellard66fb9762003-03-23 01:06:05 +0000924 }
925 }
bellarded2dcdf2003-05-29 20:06:27 +0000926
bellard66fb9762003-03-23 01:06:05 +0000927 *peax = ldl(&sc->eax);
928 return err;
929#if 0
930badframe:
931 return 1;
932#endif
933}
934
935long do_sigreturn(CPUX86State *env)
936{
bellard579a97f2007-11-11 14:26:47 +0000937 struct sigframe *frame;
938 abi_ulong frame_addr = env->regs[R_ESP] - 8;
bellard66fb9762003-03-23 01:06:05 +0000939 target_sigset_t target_set;
940 sigset_t set;
941 int eax, i;
942
bellard447db212003-05-10 15:10:36 +0000943#if defined(DEBUG_SIGNAL)
944 fprintf(stderr, "do_sigreturn\n");
945#endif
bellard579a97f2007-11-11 14:26:47 +0000946 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
947 goto badframe;
bellard66fb9762003-03-23 01:06:05 +0000948 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +0000949 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
950 goto badframe;
951 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
952 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
953 goto badframe;
954 }
bellard66fb9762003-03-23 01:06:05 +0000955
bellard92319442004-06-19 16:58:13 +0000956 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +0000957 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +0000958
bellard66fb9762003-03-23 01:06:05 +0000959 /* restore registers */
960 if (restore_sigcontext(env, &frame->sc, &eax))
961 goto badframe;
bellard579a97f2007-11-11 14:26:47 +0000962 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000963 return eax;
964
965badframe:
bellard579a97f2007-11-11 14:26:47 +0000966 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000967 force_sig(TARGET_SIGSEGV);
968 return 0;
969}
970
971long do_rt_sigreturn(CPUX86State *env)
972{
pbrook53a59602006-03-25 19:31:22 +0000973 struct rt_sigframe *frame = (struct rt_sigframe *)g2h(env->regs[R_ESP] - 4);
bellard66fb9762003-03-23 01:06:05 +0000974 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +0000975 int eax;
976
977#if 0
978 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
979 goto badframe;
980#endif
bellardb8076a72005-04-07 22:20:31 +0000981 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +0000982 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +0000983
bellardb8076a72005-04-07 22:20:31 +0000984 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +0000985 goto badframe;
986
bellard579a97f2007-11-11 14:26:47 +0000987 if (do_sigaltstack(h2g(&frame->uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +0000988 goto badframe;
thsa04e1342007-09-27 13:57:58 +0000989
bellard66fb9762003-03-23 01:06:05 +0000990 return eax;
991
992badframe:
993 force_sig(TARGET_SIGSEGV);
994 return 0;
995}
996
bellard43fff232003-07-09 19:31:39 +0000997#elif defined(TARGET_ARM)
998
999struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001000 abi_ulong trap_no;
1001 abi_ulong error_code;
1002 abi_ulong oldmask;
1003 abi_ulong arm_r0;
1004 abi_ulong arm_r1;
1005 abi_ulong arm_r2;
1006 abi_ulong arm_r3;
1007 abi_ulong arm_r4;
1008 abi_ulong arm_r5;
1009 abi_ulong arm_r6;
1010 abi_ulong arm_r7;
1011 abi_ulong arm_r8;
1012 abi_ulong arm_r9;
1013 abi_ulong arm_r10;
1014 abi_ulong arm_fp;
1015 abi_ulong arm_ip;
1016 abi_ulong arm_sp;
1017 abi_ulong arm_lr;
1018 abi_ulong arm_pc;
1019 abi_ulong arm_cpsr;
1020 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001021};
1022
bellard43fff232003-07-09 19:31:39 +00001023struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +00001024 abi_ulong tuc_flags;
1025 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +00001026 target_stack_t tuc_stack;
1027 struct target_sigcontext tuc_mcontext;
1028 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001029};
1030
1031struct sigframe
1032{
1033 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001034 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1035 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001036};
1037
1038struct rt_sigframe
1039{
1040 struct target_siginfo *pinfo;
1041 void *puc;
1042 struct target_siginfo info;
1043 struct target_ucontext uc;
blueswir1992f48a2007-10-14 16:27:31 +00001044 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001045};
1046
1047#define TARGET_CONFIG_CPU_32 1
1048
1049/*
1050 * For ARM syscalls, we encode the syscall number into the instruction.
1051 */
1052#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1053#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1054
1055/*
1056 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1057 * need two 16-bit instructions.
1058 */
1059#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1060#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1061
blueswir1992f48a2007-10-14 16:27:31 +00001062static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001063 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1064 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1065};
1066
1067
1068#define __put_user_error(x,p,e) __put_user(x, p)
1069#define __get_user_error(x,p,e) __get_user(x, p)
1070
1071static inline int valid_user_regs(CPUState *regs)
1072{
1073 return 1;
1074}
1075
1076static int
1077setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1078 CPUState *env, unsigned long mask)
1079{
1080 int err = 0;
1081
1082 __put_user_error(env->regs[0], &sc->arm_r0, err);
1083 __put_user_error(env->regs[1], &sc->arm_r1, err);
1084 __put_user_error(env->regs[2], &sc->arm_r2, err);
1085 __put_user_error(env->regs[3], &sc->arm_r3, err);
1086 __put_user_error(env->regs[4], &sc->arm_r4, err);
1087 __put_user_error(env->regs[5], &sc->arm_r5, err);
1088 __put_user_error(env->regs[6], &sc->arm_r6, err);
1089 __put_user_error(env->regs[7], &sc->arm_r7, err);
1090 __put_user_error(env->regs[8], &sc->arm_r8, err);
1091 __put_user_error(env->regs[9], &sc->arm_r9, err);
1092 __put_user_error(env->regs[10], &sc->arm_r10, err);
1093 __put_user_error(env->regs[11], &sc->arm_fp, err);
1094 __put_user_error(env->regs[12], &sc->arm_ip, err);
1095 __put_user_error(env->regs[13], &sc->arm_sp, err);
1096 __put_user_error(env->regs[14], &sc->arm_lr, err);
1097 __put_user_error(env->regs[15], &sc->arm_pc, err);
1098#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001099 __put_user_error(cpsr_read(env), &sc->arm_cpsr, err);
bellard43fff232003-07-09 19:31:39 +00001100#endif
1101
1102 __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1103 __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1104 __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1105 __put_user_error(mask, &sc->oldmask, err);
1106
1107 return err;
1108}
1109
bellard579a97f2007-11-11 14:26:47 +00001110static inline abi_ulong
bellard43fff232003-07-09 19:31:39 +00001111get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1112{
1113 unsigned long sp = regs->regs[13];
1114
bellard43fff232003-07-09 19:31:39 +00001115 /*
1116 * This is the X/Open sanctioned signal stack switching.
1117 */
thsa04e1342007-09-27 13:57:58 +00001118 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1119 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001120 /*
1121 * ATPCS B01 mandates 8-byte alignment
1122 */
bellard579a97f2007-11-11 14:26:47 +00001123 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001124}
1125
1126static int
1127setup_return(CPUState *env, struct emulated_sigaction *ka,
blueswir1992f48a2007-10-14 16:27:31 +00001128 abi_ulong *rc, void *frame, int usig)
bellard43fff232003-07-09 19:31:39 +00001129{
blueswir1992f48a2007-10-14 16:27:31 +00001130 abi_ulong handler = (abi_ulong)ka->sa._sa_handler;
1131 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001132 int thumb = 0;
1133#if defined(TARGET_CONFIG_CPU_32)
bellardb5ff1b32005-11-26 10:38:39 +00001134#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001135 abi_ulong cpsr = env->cpsr;
bellard43fff232003-07-09 19:31:39 +00001136
bellard43fff232003-07-09 19:31:39 +00001137 /*
1138 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1139 */
1140 if (ka->sa.sa_flags & SA_THIRTYTWO)
1141 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1142
1143#ifdef CONFIG_ARM_THUMB
1144 if (elf_hwcap & HWCAP_THUMB) {
1145 /*
1146 * The LSB of the handler determines if we're going to
1147 * be using THUMB or ARM mode for this signal handler.
1148 */
1149 thumb = handler & 1;
1150
1151 if (thumb)
1152 cpsr |= T_BIT;
1153 else
1154 cpsr &= ~T_BIT;
1155 }
thsa04e1342007-09-27 13:57:58 +00001156#endif /* CONFIG_ARM_THUMB */
1157#endif /* 0 */
bellard43fff232003-07-09 19:31:39 +00001158#endif /* TARGET_CONFIG_CPU_32 */
1159
1160 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
blueswir1992f48a2007-10-14 16:27:31 +00001161 retcode = (abi_ulong)ka->sa.sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001162 } else {
1163 unsigned int idx = thumb;
1164
1165 if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1166 idx += 2;
1167
1168 if (__put_user(retcodes[idx], rc))
1169 return 1;
1170#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001171 flush_icache_range((abi_ulong)rc,
1172 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001173#endif
blueswir1992f48a2007-10-14 16:27:31 +00001174 retcode = ((abi_ulong)rc) + thumb;
bellard43fff232003-07-09 19:31:39 +00001175 }
1176
1177 env->regs[0] = usig;
pbrook53a59602006-03-25 19:31:22 +00001178 env->regs[13] = h2g(frame);
bellard43fff232003-07-09 19:31:39 +00001179 env->regs[14] = retcode;
1180 env->regs[15] = handler & (thumb ? ~1 : ~3);
1181
bellardb5ff1b32005-11-26 10:38:39 +00001182#if 0
bellard43fff232003-07-09 19:31:39 +00001183#ifdef TARGET_CONFIG_CPU_32
1184 env->cpsr = cpsr;
1185#endif
bellardb5ff1b32005-11-26 10:38:39 +00001186#endif
bellard43fff232003-07-09 19:31:39 +00001187
1188 return 0;
1189}
1190
bellard579a97f2007-11-11 14:26:47 +00001191/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
bellard43fff232003-07-09 19:31:39 +00001192static void setup_frame(int usig, struct emulated_sigaction *ka,
1193 target_sigset_t *set, CPUState *regs)
1194{
bellard579a97f2007-11-11 14:26:47 +00001195 struct sigframe *frame;
1196 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
bellard92319442004-06-19 16:58:13 +00001197 int i, err = 0;
bellard43fff232003-07-09 19:31:39 +00001198
bellard579a97f2007-11-11 14:26:47 +00001199 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1200 return;
1201
bellard43fff232003-07-09 19:31:39 +00001202 err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1203
bellard92319442004-06-19 16:58:13 +00001204 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1205 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001206 goto end;
bellard43fff232003-07-09 19:31:39 +00001207 }
1208
1209 if (err == 0)
1210 err = setup_return(regs, ka, &frame->retcode, frame, usig);
bellard579a97f2007-11-11 14:26:47 +00001211
1212end:
1213 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001214 // return err;
1215}
1216
bellard579a97f2007-11-11 14:26:47 +00001217/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
ths5fafdf22007-09-16 21:08:06 +00001218static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
bellard43fff232003-07-09 19:31:39 +00001219 target_siginfo_t *info,
1220 target_sigset_t *set, CPUState *env)
1221{
bellard579a97f2007-11-11 14:26:47 +00001222 struct rt_sigframe *frame;
1223 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001224 struct target_sigaltstack stack;
bellard92319442004-06-19 16:58:13 +00001225 int i, err = 0;
bellard43fff232003-07-09 19:31:39 +00001226
bellard579a97f2007-11-11 14:26:47 +00001227 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001228 return /* 1 */;
1229
blueswir1992f48a2007-10-14 16:27:31 +00001230 __put_user_error(&frame->info, (abi_ulong *)&frame->pinfo, err);
1231 __put_user_error(&frame->uc, (abi_ulong *)&frame->puc, err);
bellard43fff232003-07-09 19:31:39 +00001232 err |= copy_siginfo_to_user(&frame->info, info);
1233
1234 /* Clear all the bits of the ucontext we don't use. */
pbrook53a59602006-03-25 19:31:22 +00001235 memset(&frame->uc, 0, offsetof(struct target_ucontext, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001236
thsa04e1342007-09-27 13:57:58 +00001237 memset(&stack, 0, sizeof(stack));
1238 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1239 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1240 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001241 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001242
bellardb8076a72005-04-07 22:20:31 +00001243 err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
bellard43fff232003-07-09 19:31:39 +00001244 env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001245 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001246 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001247 goto end;
bellard92319442004-06-19 16:58:13 +00001248 }
bellard43fff232003-07-09 19:31:39 +00001249
1250 if (err == 0)
1251 err = setup_return(env, ka, &frame->retcode, frame, usig);
1252
1253 if (err == 0) {
1254 /*
1255 * For realtime signals we must also set the second and third
1256 * arguments for the signal handler.
1257 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1258 */
blueswir1992f48a2007-10-14 16:27:31 +00001259 env->regs[1] = (abi_ulong)frame->pinfo;
1260 env->regs[2] = (abi_ulong)frame->puc;
bellard43fff232003-07-09 19:31:39 +00001261 }
1262
bellard579a97f2007-11-11 14:26:47 +00001263end:
1264 unlock_user_struct(frame, frame_addr, 1);
1265
bellard43fff232003-07-09 19:31:39 +00001266 // return err;
1267}
1268
1269static int
1270restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1271{
1272 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001273 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001274
1275 __get_user_error(env->regs[0], &sc->arm_r0, err);
1276 __get_user_error(env->regs[1], &sc->arm_r1, err);
1277 __get_user_error(env->regs[2], &sc->arm_r2, err);
1278 __get_user_error(env->regs[3], &sc->arm_r3, err);
1279 __get_user_error(env->regs[4], &sc->arm_r4, err);
1280 __get_user_error(env->regs[5], &sc->arm_r5, err);
1281 __get_user_error(env->regs[6], &sc->arm_r6, err);
1282 __get_user_error(env->regs[7], &sc->arm_r7, err);
1283 __get_user_error(env->regs[8], &sc->arm_r8, err);
1284 __get_user_error(env->regs[9], &sc->arm_r9, err);
1285 __get_user_error(env->regs[10], &sc->arm_r10, err);
1286 __get_user_error(env->regs[11], &sc->arm_fp, err);
1287 __get_user_error(env->regs[12], &sc->arm_ip, err);
1288 __get_user_error(env->regs[13], &sc->arm_sp, err);
1289 __get_user_error(env->regs[14], &sc->arm_lr, err);
1290 __get_user_error(env->regs[15], &sc->arm_pc, err);
1291#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001292 __get_user_error(cpsr, &sc->arm_cpsr, err);
1293 cpsr_write(env, cpsr, 0xffffffff);
bellard43fff232003-07-09 19:31:39 +00001294#endif
1295
1296 err |= !valid_user_regs(env);
1297
1298 return err;
1299}
1300
1301long do_sigreturn(CPUState *env)
1302{
1303 struct sigframe *frame;
1304 target_sigset_t set;
1305 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001306 int i;
bellard43fff232003-07-09 19:31:39 +00001307
1308 /*
1309 * Since we stacked the signal on a 64-bit boundary,
1310 * then 'sp' should be word aligned here. If it's
1311 * not, then the user is trying to mess with us.
1312 */
1313 if (env->regs[13] & 7)
1314 goto badframe;
1315
pbrook53a59602006-03-25 19:31:22 +00001316 frame = (struct sigframe *)g2h(env->regs[13]);
bellard43fff232003-07-09 19:31:39 +00001317
1318#if 0
1319 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1320 goto badframe;
1321#endif
bellard92319442004-06-19 16:58:13 +00001322 if (__get_user(set.sig[0], &frame->sc.oldmask))
1323 goto badframe;
1324 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1325 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1326 goto badframe;
1327 }
bellard43fff232003-07-09 19:31:39 +00001328
bellard92319442004-06-19 16:58:13 +00001329 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001330 sigprocmask(SIG_SETMASK, &host_set, NULL);
1331
1332 if (restore_sigcontext(env, &frame->sc))
1333 goto badframe;
1334
1335#if 0
1336 /* Send SIGTRAP if we're single-stepping */
1337 if (ptrace_cancel_bpt(current))
1338 send_sig(SIGTRAP, current, 1);
1339#endif
1340 return env->regs[0];
1341
1342badframe:
1343 force_sig(SIGSEGV /* , current */);
1344 return 0;
1345}
1346
1347long do_rt_sigreturn(CPUState *env)
1348{
1349 struct rt_sigframe *frame;
bellard43fff232003-07-09 19:31:39 +00001350 sigset_t host_set;
1351
1352 /*
1353 * Since we stacked the signal on a 64-bit boundary,
1354 * then 'sp' should be word aligned here. If it's
1355 * not, then the user is trying to mess with us.
1356 */
1357 if (env->regs[13] & 7)
1358 goto badframe;
1359
1360 frame = (struct rt_sigframe *)env->regs[13];
1361
1362#if 0
1363 if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1364 goto badframe;
1365#endif
bellardb8076a72005-04-07 22:20:31 +00001366 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001367 sigprocmask(SIG_SETMASK, &host_set, NULL);
1368
bellardb8076a72005-04-07 22:20:31 +00001369 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001370 goto badframe;
1371
bellard579a97f2007-11-11 14:26:47 +00001372 if (do_sigaltstack(h2g(&frame->uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
thsa04e1342007-09-27 13:57:58 +00001373 goto badframe;
1374
bellard43fff232003-07-09 19:31:39 +00001375#if 0
1376 /* Send SIGTRAP if we're single-stepping */
1377 if (ptrace_cancel_bpt(current))
1378 send_sig(SIGTRAP, current, 1);
1379#endif
1380 return env->regs[0];
1381
1382badframe:
1383 force_sig(SIGSEGV /* , current */);
1384 return 0;
1385}
1386
bellard6d5e2162004-09-30 22:04:13 +00001387#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001388
bellard6d5e2162004-09-30 22:04:13 +00001389#define __SUNOS_MAXWIN 31
1390
1391/* This is what SunOS does, so shall I. */
1392struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001393 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001394
blueswir1992f48a2007-10-14 16:27:31 +00001395 abi_ulong sigc_mask; /* sigmask to restore */
1396 abi_ulong sigc_sp; /* stack pointer */
1397 abi_ulong sigc_pc; /* program counter */
1398 abi_ulong sigc_npc; /* next program counter */
1399 abi_ulong sigc_psr; /* for condition codes etc */
1400 abi_ulong sigc_g1; /* User uses these two registers */
1401 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001402
1403 /* Now comes information regarding the users window set
1404 * at the time of the signal.
1405 */
blueswir1992f48a2007-10-14 16:27:31 +00001406 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001407
1408 /* stack ptrs for each regwin buf */
1409 char *sigc_spbuf[__SUNOS_MAXWIN];
1410
1411 /* Windows to restore after signal */
1412 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001413 abi_ulong locals[8];
1414 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001415 } sigc_wbuf[__SUNOS_MAXWIN];
1416};
1417/* A Sparc stack frame */
1418struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001419 abi_ulong locals[8];
1420 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001421 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001422 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001423 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001424 abi_ulong xargs[6];
1425 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001426};
1427
1428typedef struct {
1429 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001430 abi_ulong psr;
1431 abi_ulong pc;
1432 abi_ulong npc;
1433 abi_ulong y;
1434 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001435 } si_regs;
1436 int si_mask;
1437} __siginfo_t;
1438
1439typedef struct {
1440 unsigned long si_float_regs [32];
1441 unsigned long si_fsr;
1442 unsigned long si_fpqdepth;
1443 struct {
1444 unsigned long *insn_addr;
1445 unsigned long insn;
1446 } si_fpqueue [16];
bellard74ccb342006-07-18 21:23:34 +00001447} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001448
1449
1450struct target_signal_frame {
1451 struct sparc_stackf ss;
1452 __siginfo_t info;
bellard74ccb342006-07-18 21:23:34 +00001453 qemu_siginfo_fpu_t *fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001454 abi_ulong insns[2] __attribute__ ((aligned (8)));
1455 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1456 abi_ulong extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001457 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001458};
1459struct target_rt_signal_frame {
1460 struct sparc_stackf ss;
1461 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001462 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001463 sigset_t mask;
bellard74ccb342006-07-18 21:23:34 +00001464 qemu_siginfo_fpu_t *fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001465 unsigned int insns[2];
1466 stack_t stack;
1467 unsigned int extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001468 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001469};
1470
bellarde80cfcf2004-12-19 23:18:01 +00001471#define UREG_O0 16
1472#define UREG_O6 22
1473#define UREG_I0 0
1474#define UREG_I1 1
1475#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001476#define UREG_I3 3
1477#define UREG_I4 4
1478#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001479#define UREG_I6 6
1480#define UREG_I7 7
1481#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001482#define UREG_FP UREG_I6
1483#define UREG_SP UREG_O6
1484
bellard459a4012007-11-11 19:45:10 +00001485static inline abi_ulong get_sigframe(struct emulated_sigaction *sa,
1486 CPUState *env, unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001487{
bellard459a4012007-11-11 19:45:10 +00001488 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001489
1490 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001491
1492 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00001493 if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1494 if (!on_sig_stack(sp)
1495 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1496 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001497 }
bellard459a4012007-11-11 19:45:10 +00001498 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001499}
1500
1501static int
blueswir1992f48a2007-10-14 16:27:31 +00001502setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001503{
1504 int err = 0, i;
1505
bellard6d5e2162004-09-30 22:04:13 +00001506 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001507 err |= __put_user(env->pc, &si->si_regs.pc);
1508 err |= __put_user(env->npc, &si->si_regs.npc);
1509 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001510 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001511 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1512 }
bellarda315a142005-01-30 22:59:18 +00001513 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001514 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001515 }
bellard6d5e2162004-09-30 22:04:13 +00001516 err |= __put_user(mask, &si->si_mask);
1517 return err;
1518}
bellarde80cfcf2004-12-19 23:18:01 +00001519
bellard80a9d032005-01-03 23:31:27 +00001520#if 0
bellard6d5e2162004-09-30 22:04:13 +00001521static int
1522setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1523 CPUState *env, unsigned long mask)
1524{
1525 int err = 0;
1526
1527 err |= __put_user(mask, &sc->sigc_mask);
1528 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1529 err |= __put_user(env->pc, &sc->sigc_pc);
1530 err |= __put_user(env->npc, &sc->sigc_npc);
1531 err |= __put_user(env->psr, &sc->sigc_psr);
1532 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1533 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1534
1535 return err;
1536}
bellard80a9d032005-01-03 23:31:27 +00001537#endif
bellard6d5e2162004-09-30 22:04:13 +00001538#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1539
1540static void setup_frame(int sig, struct emulated_sigaction *ka,
1541 target_sigset_t *set, CPUState *env)
1542{
bellard459a4012007-11-11 19:45:10 +00001543 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001544 struct target_signal_frame *sf;
1545 int sigframe_size, err, i;
1546
1547 /* 1. Make sure everything is clean */
1548 //synchronize_user_stack();
1549
1550 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001551 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001552
bellard459a4012007-11-11 19:45:10 +00001553 sf = lock_user(VERIFY_WRITE, sf_addr,
1554 sizeof(struct target_signal_frame), 0);
1555 if (!sf)
1556 goto sigsegv;
1557
bellarde80cfcf2004-12-19 23:18:01 +00001558 //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 +00001559#if 0
1560 if (invalid_frame_pointer(sf, sigframe_size))
1561 goto sigill_and_return;
1562#endif
1563 /* 2. Save the current process state */
1564 err = setup___siginfo(&sf->info, env, set->sig[0]);
1565 err |= __put_user(0, &sf->extra_size);
1566
1567 //err |= save_fpu_state(regs, &sf->fpu_state);
1568 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1569
1570 err |= __put_user(set->sig[0], &sf->info.si_mask);
1571 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1572 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1573 }
1574
bellarda315a142005-01-30 22:59:18 +00001575 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001576 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001577 }
bellarda315a142005-01-30 22:59:18 +00001578 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001579 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001580 }
bellard6d5e2162004-09-30 22:04:13 +00001581 if (err)
1582 goto sigsegv;
1583
1584 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001585 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001586 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001587 env->regwptr[UREG_I1] = sf_addr +
1588 offsetof(struct target_signal_frame, info);
1589 env->regwptr[UREG_I2] = sf_addr +
1590 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001591
1592 /* 4. signal handler */
bellard459a4012007-11-11 19:45:10 +00001593 env->pc = ka->sa._sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00001594 env->npc = (env->pc + 4);
1595 /* 5. return to kernel instructions */
1596 if (ka->sa.sa_restorer)
bellard459a4012007-11-11 19:45:10 +00001597 env->regwptr[UREG_I7] = ka->sa.sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00001598 else {
bellard775b58d2007-11-11 16:22:17 +00001599 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00001600
1601 env->regwptr[UREG_I7] = sf_addr +
1602 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00001603
1604 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001605 val32 = 0x821020d8;
1606 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001607
1608 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001609 val32 = 0x91d02010;
1610 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001611 if (err)
1612 goto sigsegv;
1613
1614 /* Flush instruction space. */
1615 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001616 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001617 }
bellard459a4012007-11-11 19:45:10 +00001618 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001619 return;
bellard459a4012007-11-11 19:45:10 +00001620#if 0
1621sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001622 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00001623#endif
bellard6d5e2162004-09-30 22:04:13 +00001624sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001625 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00001626 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001627 force_sig(TARGET_SIGSEGV);
1628}
1629static inline int
bellard74ccb342006-07-18 21:23:34 +00001630restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001631{
1632 int err;
1633#if 0
1634#ifdef CONFIG_SMP
1635 if (current->flags & PF_USEDFPU)
1636 regs->psr &= ~PSR_EF;
1637#else
1638 if (current == last_task_used_math) {
1639 last_task_used_math = 0;
1640 regs->psr &= ~PSR_EF;
1641 }
1642#endif
1643 current->used_math = 1;
1644 current->flags &= ~PF_USEDFPU;
1645#endif
1646#if 0
1647 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1648 return -EFAULT;
1649#endif
1650
bellardfafffae2006-10-28 12:09:16 +00001651#if 0
1652 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001653 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1654 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001655#endif
bellard6d5e2162004-09-30 22:04:13 +00001656 err |= __get_user(env->fsr, &fpu->si_fsr);
1657#if 0
1658 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1659 if (current->thread.fpqdepth != 0)
1660 err |= __copy_from_user(&current->thread.fpqueue[0],
1661 &fpu->si_fpqueue[0],
1662 ((sizeof(unsigned long) +
1663 (sizeof(unsigned long *)))*16));
1664#endif
1665 return err;
1666}
1667
1668
ths5fafdf22007-09-16 21:08:06 +00001669static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001670 target_siginfo_t *info,
1671 target_sigset_t *set, CPUState *env)
1672{
1673 fprintf(stderr, "setup_rt_frame: not implemented\n");
1674}
1675
1676long do_sigreturn(CPUState *env)
1677{
1678 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001679 uint32_t up_psr, pc, npc;
bellard6d5e2162004-09-30 22:04:13 +00001680 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001681 sigset_t host_set;
blueswir1992f48a2007-10-14 16:27:31 +00001682 abi_ulong fpu_save;
bellarde80cfcf2004-12-19 23:18:01 +00001683 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001684
pbrook53a59602006-03-25 19:31:22 +00001685 sf = (struct target_signal_frame *)g2h(env->regwptr[UREG_FP]);
bellard80a9d032005-01-03 23:31:27 +00001686#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001687 fprintf(stderr, "sigreturn\n");
1688 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 +00001689#endif
bellarde80cfcf2004-12-19 23:18:01 +00001690 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001691
1692 /* 1. Make sure we are not getting garbage from the user */
1693#if 0
1694 if (verify_area (VERIFY_READ, sf, sizeof (*sf)))
1695 goto segv_and_exit;
1696#endif
1697
1698 if (((uint) sf) & 3)
1699 goto segv_and_exit;
1700
1701 err = __get_user(pc, &sf->info.si_regs.pc);
1702 err |= __get_user(npc, &sf->info.si_regs.npc);
1703
bellard6d5e2162004-09-30 22:04:13 +00001704 if ((pc | npc) & 3)
1705 goto segv_and_exit;
1706
1707 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001708 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1709
bellard6d5e2162004-09-30 22:04:13 +00001710 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001711 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1712 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1713
1714 env->pc = pc;
1715 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001716 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001717 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001718 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1719 }
bellarda315a142005-01-30 22:59:18 +00001720 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001721 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1722 }
bellard6d5e2162004-09-30 22:04:13 +00001723
blueswir1992f48a2007-10-14 16:27:31 +00001724 err |= __get_user(fpu_save, (abi_ulong *)&sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001725
bellarde80cfcf2004-12-19 23:18:01 +00001726 //if (fpu_save)
1727 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001728
1729 /* This is pretty much atomic, no amount locking would prevent
1730 * the races which exist anyways.
1731 */
1732 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001733 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1734 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1735 }
1736
1737 target_to_host_sigset_internal(&host_set, &set);
1738 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001739
1740 if (err)
1741 goto segv_and_exit;
1742
bellard6d5e2162004-09-30 22:04:13 +00001743 return env->regwptr[0];
1744
1745segv_and_exit:
1746 force_sig(TARGET_SIGSEGV);
1747}
1748
1749long do_rt_sigreturn(CPUState *env)
1750{
1751 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1752 return -ENOSYS;
1753}
1754
bellard459a4012007-11-11 19:45:10 +00001755#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00001756#define MC_TSTATE 0
1757#define MC_PC 1
1758#define MC_NPC 2
1759#define MC_Y 3
1760#define MC_G1 4
1761#define MC_G2 5
1762#define MC_G3 6
1763#define MC_G4 7
1764#define MC_G5 8
1765#define MC_G6 9
1766#define MC_G7 10
1767#define MC_O0 11
1768#define MC_O1 12
1769#define MC_O2 13
1770#define MC_O3 14
1771#define MC_O4 15
1772#define MC_O5 16
1773#define MC_O6 17
1774#define MC_O7 18
1775#define MC_NGREG 19
1776
blueswir1992f48a2007-10-14 16:27:31 +00001777typedef abi_ulong target_mc_greg_t;
blueswir15bfb56b2007-10-05 17:01:51 +00001778typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1779
1780struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00001781 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001782 uint32_t mcfq_insn;
1783};
1784
1785struct target_mc_fpu {
1786 union {
1787 uint32_t sregs[32];
1788 uint64_t dregs[32];
1789 //uint128_t qregs[16];
1790 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00001791 abi_ulong mcfpu_fsr;
1792 abi_ulong mcfpu_fprs;
1793 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00001794 struct target_mc_fq *mcfpu_fq;
1795 unsigned char mcfpu_qcnt;
1796 unsigned char mcfpu_qentsz;
1797 unsigned char mcfpu_enab;
1798};
1799typedef struct target_mc_fpu target_mc_fpu_t;
1800
1801typedef struct {
1802 target_mc_gregset_t mc_gregs;
1803 target_mc_greg_t mc_fp;
1804 target_mc_greg_t mc_i7;
1805 target_mc_fpu_t mc_fpregs;
1806} target_mcontext_t;
1807
1808struct target_ucontext {
1809 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00001810 abi_ulong uc_flags;
blueswir15bfb56b2007-10-05 17:01:51 +00001811 target_sigset_t uc_sigmask;
1812 target_mcontext_t uc_mcontext;
1813};
1814
1815/* A V9 register window */
1816struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00001817 abi_ulong locals[8];
1818 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00001819};
1820
1821#define TARGET_STACK_BIAS 2047
1822
1823/* {set, get}context() needed for 64-bit SparcLinux userland. */
1824void sparc64_set_context(CPUSPARCState *env)
1825{
bellard459a4012007-11-11 19:45:10 +00001826 abi_ulong ucp_addr;
1827 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00001828 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00001829 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00001830 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001831 unsigned char fenab;
1832 int err;
1833 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00001834
bellard459a4012007-11-11 19:45:10 +00001835 ucp_addr = env->regwptr[UREG_I0];
1836 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
1837 goto do_sigsegv;
blueswir15bfb56b2007-10-05 17:01:51 +00001838 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00001839 err = __get_user(pc, &((*grp)[MC_PC]));
1840 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00001841 if (err || ((pc | npc) & 3))
1842 goto do_sigsegv;
1843 if (env->regwptr[UREG_I1]) {
1844 target_sigset_t target_set;
1845 sigset_t set;
1846
1847 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00001848 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00001849 goto do_sigsegv;
1850 } else {
bellard459a4012007-11-11 19:45:10 +00001851 abi_ulong *src, *dst;
1852 src = ucp->uc_sigmask.sig;
1853 dst = target_set.sig;
blueswir1992f48a2007-10-14 16:27:31 +00001854 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00001855 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00001856 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00001857 if (err)
1858 goto do_sigsegv;
1859 }
1860 target_to_host_sigset_internal(&set, &target_set);
1861 sigprocmask(SIG_SETMASK, &set, NULL);
1862 }
1863 env->pc = pc;
1864 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00001865 err |= __get_user(env->y, &((*grp)[MC_Y]));
1866 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00001867 env->asi = (tstate >> 24) & 0xff;
1868 PUT_CCR(env, tstate >> 32);
1869 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00001870 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
1871 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
1872 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
1873 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
1874 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
1875 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
1876 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
1877 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
1878 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
1879 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
1880 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
1881 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
1882 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
1883 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
1884 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00001885
bellard579a97f2007-11-11 14:26:47 +00001886 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
1887 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00001888
bellard459a4012007-11-11 19:45:10 +00001889 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
1890 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
1891 abi_ulong) != 0)
1892 goto do_sigsegv;
1893 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
1894 abi_ulong) != 0)
1895 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00001896 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
1897 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00001898 {
1899 uint32_t *src, *dst;
1900 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
1901 dst = env->fpr;
1902 /* XXX: check that the CPU storage is the same as user context */
1903 for (i = 0; i < 64; i++, dst++, src++)
1904 err |= __get_user(*dst, src);
1905 }
bellard579a97f2007-11-11 14:26:47 +00001906 err |= __get_user(env->fsr,
1907 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
1908 err |= __get_user(env->gsr,
1909 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00001910 if (err)
1911 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00001912 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00001913 return;
1914 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00001915 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00001916 force_sig(SIGSEGV);
1917}
1918
1919void sparc64_get_context(CPUSPARCState *env)
1920{
bellard459a4012007-11-11 19:45:10 +00001921 abi_ulong ucp_addr;
1922 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00001923 target_mc_gregset_t *grp;
1924 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00001925 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001926 int err;
1927 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00001928 target_sigset_t target_set;
1929 sigset_t set;
1930
bellard459a4012007-11-11 19:45:10 +00001931 ucp_addr = env->regwptr[UREG_I0];
1932 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
1933 goto do_sigsegv;
1934
blueswir15bfb56b2007-10-05 17:01:51 +00001935 mcp = &ucp->uc_mcontext;
1936 grp = &mcp->mc_gregs;
1937
1938 /* Skip over the trap instruction, first. */
1939 env->pc = env->npc;
1940 env->npc += 4;
1941
1942 err = 0;
1943
1944 sigprocmask(0, NULL, &set);
1945 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00001946 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00001947 err |= __put_user(target_set.sig[0],
1948 (abi_ulong *)&ucp->uc_sigmask);
bellard459a4012007-11-11 19:45:10 +00001949 } else {
1950 abi_ulong *src, *dst;
1951 src = target_set.sig;
1952 dst = ucp->uc_sigmask.sig;
blueswir1992f48a2007-10-14 16:27:31 +00001953 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00001954 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00001955 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00001956 if (err)
1957 goto do_sigsegv;
1958 }
1959
bellard459a4012007-11-11 19:45:10 +00001960 /* XXX: tstate must be saved properly */
1961 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00001962 err |= __put_user(env->pc, &((*grp)[MC_PC]));
1963 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
1964 err |= __put_user(env->y, &((*grp)[MC_Y]));
1965 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
1966 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
1967 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
1968 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
1969 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
1970 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
1971 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
1972 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
1973 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
1974 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
1975 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
1976 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
1977 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
1978 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
1979 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00001980
bellard459a4012007-11-11 19:45:10 +00001981 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
1982 fp = i7 = 0;
1983 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
1984 abi_ulong) != 0)
1985 goto do_sigsegv;
1986 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
1987 abi_ulong) != 0)
1988 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00001989 err |= __put_user(fp, &(mcp->mc_fp));
1990 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00001991
bellard459a4012007-11-11 19:45:10 +00001992 {
1993 uint32_t *src, *dst;
1994 src = env->fpr;
1995 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
1996 /* XXX: check that the CPU storage is the same as user context */
1997 for (i = 0; i < 64; i++, dst++, src++)
1998 err |= __put_user(*src, dst);
1999 }
bellard579a97f2007-11-11 14:26:47 +00002000 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2001 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2002 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002003
2004 if (err)
2005 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002006 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002007 return;
2008 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002009 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002010 force_sig(SIGSEGV);
2011}
2012#endif
thsd26bc212007-11-08 18:05:37 +00002013#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002014
2015# warning signal handling not implemented
2016
2017static void setup_frame(int sig, struct emulated_sigaction *ka,
2018 target_sigset_t *set, CPUState *env)
2019{
2020 fprintf(stderr, "setup_frame: not implemented\n");
2021}
2022
2023static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2024 target_siginfo_t *info,
2025 target_sigset_t *set, CPUState *env)
2026{
2027 fprintf(stderr, "setup_rt_frame: not implemented\n");
2028}
2029
2030long do_sigreturn(CPUState *env)
2031{
2032 fprintf(stderr, "do_sigreturn: not implemented\n");
2033 return -ENOSYS;
2034}
2035
2036long do_rt_sigreturn(CPUState *env)
2037{
2038 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2039 return -ENOSYS;
2040}
2041
thsd26bc212007-11-08 18:05:37 +00002042#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002043
2044# warning signal handling not implemented
2045
2046static void setup_frame(int sig, struct emulated_sigaction *ka,
2047 target_sigset_t *set, CPUState *env)
2048{
2049 fprintf(stderr, "setup_frame: not implemented\n");
2050}
2051
2052static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2053 target_siginfo_t *info,
2054 target_sigset_t *set, CPUState *env)
2055{
2056 fprintf(stderr, "setup_rt_frame: not implemented\n");
2057}
2058
2059long do_sigreturn(CPUState *env)
2060{
2061 fprintf(stderr, "do_sigreturn: not implemented\n");
2062 return -ENOSYS;
2063}
2064
2065long do_rt_sigreturn(CPUState *env)
2066{
2067 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2068 return -ENOSYS;
2069}
2070
thsd26bc212007-11-08 18:05:37 +00002071#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002072
2073struct target_sigcontext {
2074 uint32_t sc_regmask; /* Unused */
2075 uint32_t sc_status;
2076 uint64_t sc_pc;
2077 uint64_t sc_regs[32];
2078 uint64_t sc_fpregs[32];
2079 uint32_t sc_ownedfp; /* Unused */
2080 uint32_t sc_fpc_csr;
2081 uint32_t sc_fpc_eir; /* Unused */
2082 uint32_t sc_used_math;
2083 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2084 uint64_t sc_mdhi;
2085 uint64_t sc_mdlo;
2086 target_ulong sc_hi1; /* Was sc_cause */
2087 target_ulong sc_lo1; /* Was sc_badvaddr */
2088 target_ulong sc_hi2; /* Was sc_sigset[4] */
2089 target_ulong sc_lo2;
2090 target_ulong sc_hi3;
2091 target_ulong sc_lo3;
2092};
2093
2094struct sigframe {
2095 uint32_t sf_ass[4]; /* argument save space for o32 */
2096 uint32_t sf_code[2]; /* signal trampoline */
2097 struct target_sigcontext sf_sc;
2098 target_sigset_t sf_mask;
2099};
2100
2101/* Install trampoline to jump back from signal handler */
2102static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2103{
2104 int err;
2105
2106 /*
2107 * Set up the return code ...
2108 *
2109 * li v0, __NR__foo_sigreturn
2110 * syscall
2111 */
2112
2113 err = __put_user(0x24020000 + syscall, tramp + 0);
2114 err |= __put_user(0x0000000c , tramp + 1);
2115 /* flush_cache_sigtramp((unsigned long) tramp); */
2116 return err;
2117}
2118
2119static inline int
2120setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2121{
2122 int err = 0;
2123
thsead93602007-09-06 00:18:15 +00002124 err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002125
thsead93602007-09-06 00:18:15 +00002126#define save_gp_reg(i) do { \
2127 err |= __put_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002128 } while(0)
2129 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2130 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2131 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2132 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2133 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2134 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2135 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2136 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2137 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002138#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002139
thsead93602007-09-06 00:18:15 +00002140 err |= __put_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
2141 err |= __put_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002142
2143 /* Not used yet, but might be useful if we ever have DSP suppport */
2144#if 0
2145 if (cpu_has_dsp) {
2146 err |= __put_user(mfhi1(), &sc->sc_hi1);
2147 err |= __put_user(mflo1(), &sc->sc_lo1);
2148 err |= __put_user(mfhi2(), &sc->sc_hi2);
2149 err |= __put_user(mflo2(), &sc->sc_lo2);
2150 err |= __put_user(mfhi3(), &sc->sc_hi3);
2151 err |= __put_user(mflo3(), &sc->sc_lo3);
2152 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2153 }
2154 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002155#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002156 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2157 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2158 if (cpu_has_dsp) {
2159 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2160 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2161 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2162 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2163 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2164 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2165 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2166 }
ths388bb212007-05-13 13:58:00 +00002167#endif
2168#endif
bellard106ec872006-06-27 21:08:10 +00002169
ths388bb212007-05-13 13:58:00 +00002170#if 0
bellard106ec872006-06-27 21:08:10 +00002171 err |= __put_user(!!used_math(), &sc->sc_used_math);
2172
2173 if (!used_math())
2174 goto out;
2175
2176 /*
2177 * Save FPU state to signal context. Signal handler will "inherit"
2178 * current FPU state.
2179 */
2180 preempt_disable();
2181
2182 if (!is_fpu_owner()) {
2183 own_fpu();
2184 restore_fp(current);
2185 }
2186 err |= save_fp_context(sc);
2187
2188 preempt_enable();
2189 out:
2190#endif
2191 return err;
2192}
2193
2194static inline int
2195restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2196{
2197 int err = 0;
2198
2199 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2200
thsead93602007-09-06 00:18:15 +00002201 err |= __get_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
2202 err |= __get_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002203
thsead93602007-09-06 00:18:15 +00002204#define restore_gp_reg(i) do { \
2205 err |= __get_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002206 } while(0)
2207 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2208 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2209 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2210 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2211 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2212 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2213 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2214 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2215 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2216 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2217 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002218#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002219
2220#if 0
2221 if (cpu_has_dsp) {
2222 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2223 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2224 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2225 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2226 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2227 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2228 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2229 }
ths388bb212007-05-13 13:58:00 +00002230#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002231 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2232 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2233 if (cpu_has_dsp) {
2234 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2235 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2236 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2237 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2238 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2239 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2240 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2241 }
ths388bb212007-05-13 13:58:00 +00002242#endif
bellard106ec872006-06-27 21:08:10 +00002243
2244 err |= __get_user(used_math, &sc->sc_used_math);
2245 conditional_used_math(used_math);
2246
2247 preempt_disable();
2248
2249 if (used_math()) {
2250 /* restore fpu context if we have used it before */
2251 own_fpu();
2252 err |= restore_fp_context(sc);
2253 } else {
2254 /* signal handler may have used FPU. Give it up. */
2255 lose_fpu();
2256 }
2257
2258 preempt_enable();
2259#endif
2260 return err;
2261}
2262/*
2263 * Determine which stack to use..
2264 */
bellard579a97f2007-11-11 14:26:47 +00002265static inline abi_ulong
bellard106ec872006-06-27 21:08:10 +00002266get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
2267{
2268 unsigned long sp;
2269
2270 /* Default to using normal stack */
thsead93602007-09-06 00:18:15 +00002271 sp = regs->gpr[29][regs->current_tc];
bellard106ec872006-06-27 21:08:10 +00002272
2273 /*
2274 * FPU emulator may have it's own trampoline active just
2275 * above the user stack, 16-bytes before the next lowest
2276 * 16 byte boundary. Try to avoid trashing it.
2277 */
2278 sp -= 32;
2279
bellard106ec872006-06-27 21:08:10 +00002280 /* This is the X/Open sanctioned signal stack switching. */
thsa04e1342007-09-27 13:57:58 +00002281 if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2282 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2283 }
bellard106ec872006-06-27 21:08:10 +00002284
bellard579a97f2007-11-11 14:26:47 +00002285 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002286}
2287
bellard579a97f2007-11-11 14:26:47 +00002288/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
ths5fafdf22007-09-16 21:08:06 +00002289static void setup_frame(int sig, struct emulated_sigaction * ka,
bellard579a97f2007-11-11 14:26:47 +00002290 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002291{
2292 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002293 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002294 int i;
2295
bellard579a97f2007-11-11 14:26:47 +00002296 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2297 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002298 goto give_sigsegv;
2299
2300 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2301
2302 if(setup_sigcontext(regs, &frame->sf_sc))
2303 goto give_sigsegv;
2304
2305 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2306 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2307 goto give_sigsegv;
2308 }
2309
2310 /*
2311 * Arguments to signal handler:
2312 *
2313 * a0 = signal number
2314 * a1 = 0 (should be cause)
2315 * a2 = pointer to struct sigcontext
2316 *
2317 * $25 and PC point to the signal handler, $29 points to the
2318 * struct sigframe.
2319 */
thsead93602007-09-06 00:18:15 +00002320 regs->gpr[ 4][regs->current_tc] = sig;
2321 regs->gpr[ 5][regs->current_tc] = 0;
2322 regs->gpr[ 6][regs->current_tc] = h2g(&frame->sf_sc);
2323 regs->gpr[29][regs->current_tc] = h2g(frame);
2324 regs->gpr[31][regs->current_tc] = h2g(frame->sf_code);
bellard106ec872006-06-27 21:08:10 +00002325 /* The original kernel code sets CP0_EPC to the handler
2326 * since it returns to userland using eret
2327 * we cannot do this here, and we must set PC directly */
thsead93602007-09-06 00:18:15 +00002328 regs->PC[regs->current_tc] = regs->gpr[25][regs->current_tc] = ka->sa._sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002329 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002330 return;
2331
2332give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002333 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002334 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002335 return;
bellard106ec872006-06-27 21:08:10 +00002336}
2337
2338long do_sigreturn(CPUState *regs)
2339{
ths388bb212007-05-13 13:58:00 +00002340 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002341 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002342 sigset_t blocked;
2343 target_sigset_t target_set;
2344 int i;
bellard106ec872006-06-27 21:08:10 +00002345
2346#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002347 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002348#endif
bellard579a97f2007-11-11 14:26:47 +00002349 frame_addr = regs->gpr[29][regs->current_tc];
2350 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002351 goto badframe;
2352
ths388bb212007-05-13 13:58:00 +00002353 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002354 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2355 goto badframe;
ths388bb212007-05-13 13:58:00 +00002356 }
bellard106ec872006-06-27 21:08:10 +00002357
ths388bb212007-05-13 13:58:00 +00002358 target_to_host_sigset_internal(&blocked, &target_set);
2359 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002360
ths388bb212007-05-13 13:58:00 +00002361 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002362 goto badframe;
2363
2364#if 0
ths388bb212007-05-13 13:58:00 +00002365 /*
2366 * Don't let your children do this ...
2367 */
2368 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002369 "move\t$29, %0\n\t"
2370 "j\tsyscall_exit"
2371 :/* no outputs */
2372 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002373 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002374#endif
ths3b46e622007-09-17 08:09:54 +00002375
thsead93602007-09-06 00:18:15 +00002376 regs->PC[regs->current_tc] = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002377 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002378 * maybe a problem with nested signals ? */
2379 regs->CP0_EPC = 0;
2380 return 0;
2381
2382badframe:
ths388bb212007-05-13 13:58:00 +00002383 force_sig(TARGET_SIGSEGV/*, current*/);
2384 return 0;
bellard106ec872006-06-27 21:08:10 +00002385}
2386
ths5fafdf22007-09-16 21:08:06 +00002387static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellard106ec872006-06-27 21:08:10 +00002388 target_siginfo_t *info,
2389 target_sigset_t *set, CPUState *env)
2390{
2391 fprintf(stderr, "setup_rt_frame: not implemented\n");
2392}
2393
2394long do_rt_sigreturn(CPUState *env)
2395{
2396 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2397 return -ENOSYS;
2398}
bellard6d5e2162004-09-30 22:04:13 +00002399
bellardb346ff42003-06-15 20:05:50 +00002400#else
2401
2402static void setup_frame(int sig, struct emulated_sigaction *ka,
2403 target_sigset_t *set, CPUState *env)
2404{
2405 fprintf(stderr, "setup_frame: not implemented\n");
2406}
2407
ths5fafdf22007-09-16 21:08:06 +00002408static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00002409 target_siginfo_t *info,
2410 target_sigset_t *set, CPUState *env)
2411{
2412 fprintf(stderr, "setup_rt_frame: not implemented\n");
2413}
2414
2415long do_sigreturn(CPUState *env)
2416{
2417 fprintf(stderr, "do_sigreturn: not implemented\n");
2418 return -ENOSYS;
2419}
2420
2421long do_rt_sigreturn(CPUState *env)
2422{
2423 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2424 return -ENOSYS;
2425}
2426
bellard66fb9762003-03-23 01:06:05 +00002427#endif
2428
2429void process_pending_signals(void *cpu_env)
2430{
2431 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00002432 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00002433 sigset_t set, old_set;
2434 target_sigset_t target_old_set;
bellard66fb9762003-03-23 01:06:05 +00002435 struct emulated_sigaction *k;
2436 struct sigqueue *q;
ths3b46e622007-09-17 08:09:54 +00002437
bellard31e31b82003-02-18 22:55:36 +00002438 if (!signal_pending)
2439 return;
2440
bellard66fb9762003-03-23 01:06:05 +00002441 k = sigact_table;
2442 for(sig = 1; sig <= TARGET_NSIG; sig++) {
2443 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00002444 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00002445 k++;
bellard31e31b82003-02-18 22:55:36 +00002446 }
2447 /* if no signal is pending, just return */
2448 signal_pending = 0;
2449 return;
bellard66fb9762003-03-23 01:06:05 +00002450
bellard31e31b82003-02-18 22:55:36 +00002451 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00002452#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00002453 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00002454#endif
2455 /* dequeue signal */
2456 q = k->first;
2457 k->first = q->next;
2458 if (!k->first)
2459 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00002460
bellard1fddef42005-04-17 19:16:13 +00002461 sig = gdb_handlesig (cpu_env, sig);
2462 if (!sig) {
2463 fprintf (stderr, "Lost signal\n");
2464 abort();
2465 }
bellard66fb9762003-03-23 01:06:05 +00002466
2467 handler = k->sa._sa_handler;
2468 if (handler == TARGET_SIG_DFL) {
2469 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +00002470 if (sig != TARGET_SIGCHLD &&
2471 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +00002472 sig != TARGET_SIGWINCH) {
2473 force_sig(sig);
2474 }
2475 } else if (handler == TARGET_SIG_IGN) {
2476 /* ignore sig */
2477 } else if (handler == TARGET_SIG_ERR) {
2478 force_sig(sig);
2479 } else {
bellard9de5e442003-03-23 16:49:39 +00002480 /* compute the blocked signals during the handler execution */
2481 target_to_host_sigset(&set, &k->sa.sa_mask);
2482 /* SA_NODEFER indicates that the current signal should not be
2483 blocked during the handler */
2484 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
2485 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00002486
bellard9de5e442003-03-23 16:49:39 +00002487 /* block signals in the handler using Linux */
2488 sigprocmask(SIG_BLOCK, &set, &old_set);
2489 /* save the previous blocked signal state to restore it at the
2490 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00002491 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00002492
bellardbc8a22c2003-03-30 21:02:40 +00002493 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00002494#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00002495 {
2496 CPUX86State *env = cpu_env;
2497 if (env->eflags & VM_MASK)
2498 save_v86_state(env);
2499 }
2500#endif
bellard9de5e442003-03-23 16:49:39 +00002501 /* prepare the stack frame of the virtual CPU */
bellard66fb9762003-03-23 01:06:05 +00002502 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
bellard9de5e442003-03-23 16:49:39 +00002503 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00002504 else
bellard9de5e442003-03-23 16:49:39 +00002505 setup_frame(sig, k, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00002506 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
2507 k->sa._sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00002508 }
bellard66fb9762003-03-23 01:06:05 +00002509 if (q != &k->info)
2510 free_sigqueue(q);
bellard31e31b82003-02-18 22:55:36 +00002511}