blob: 1dd634f796050f63698a67f155f94a52ede447eb [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
blueswir1249c4c32008-10-05 11:09:37 +000034static struct target_sigaltstack target_sigaltstack_used = {
thsa04e1342007-09-27 13:57:58 +000035 .ss_sp = 0,
36 .ss_size = 0,
37 .ss_flags = TARGET_SS_DISABLE,
38};
39
pbrook624f7972008-05-31 16:11:38 +000040static struct target_sigaction sigact_table[TARGET_NSIG];
bellard31e31b82003-02-18 22:55:36 +000041
ths5fafdf22007-09-16 21:08:06 +000042static void host_signal_handler(int host_signum, siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +000043 void *puc);
44
bellard9e5f5282003-07-13 17:33:54 +000045static uint8_t host_to_target_signal_table[65] = {
46 [SIGHUP] = TARGET_SIGHUP,
47 [SIGINT] = TARGET_SIGINT,
48 [SIGQUIT] = TARGET_SIGQUIT,
49 [SIGILL] = TARGET_SIGILL,
50 [SIGTRAP] = TARGET_SIGTRAP,
51 [SIGABRT] = TARGET_SIGABRT,
bellard01e3b762003-09-30 21:10:14 +000052/* [SIGIOT] = TARGET_SIGIOT,*/
bellard9e5f5282003-07-13 17:33:54 +000053 [SIGBUS] = TARGET_SIGBUS,
54 [SIGFPE] = TARGET_SIGFPE,
55 [SIGKILL] = TARGET_SIGKILL,
56 [SIGUSR1] = TARGET_SIGUSR1,
57 [SIGSEGV] = TARGET_SIGSEGV,
58 [SIGUSR2] = TARGET_SIGUSR2,
59 [SIGPIPE] = TARGET_SIGPIPE,
60 [SIGALRM] = TARGET_SIGALRM,
61 [SIGTERM] = TARGET_SIGTERM,
62#ifdef SIGSTKFLT
63 [SIGSTKFLT] = TARGET_SIGSTKFLT,
64#endif
65 [SIGCHLD] = TARGET_SIGCHLD,
66 [SIGCONT] = TARGET_SIGCONT,
67 [SIGSTOP] = TARGET_SIGSTOP,
68 [SIGTSTP] = TARGET_SIGTSTP,
69 [SIGTTIN] = TARGET_SIGTTIN,
70 [SIGTTOU] = TARGET_SIGTTOU,
71 [SIGURG] = TARGET_SIGURG,
72 [SIGXCPU] = TARGET_SIGXCPU,
73 [SIGXFSZ] = TARGET_SIGXFSZ,
74 [SIGVTALRM] = TARGET_SIGVTALRM,
75 [SIGPROF] = TARGET_SIGPROF,
76 [SIGWINCH] = TARGET_SIGWINCH,
77 [SIGIO] = TARGET_SIGIO,
78 [SIGPWR] = TARGET_SIGPWR,
79 [SIGSYS] = TARGET_SIGSYS,
80 /* next signals stay the same */
pbrook624f7972008-05-31 16:11:38 +000081 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
82 host libpthread signals. This assumes noone actually uses SIGRTMAX :-/
83 To fix this properly we need to do manual signal delivery multiplexed
84 over a single host signal. */
85 [__SIGRTMIN] = __SIGRTMAX,
86 [__SIGRTMAX] = __SIGRTMIN,
bellard9e5f5282003-07-13 17:33:54 +000087};
88static uint8_t target_to_host_signal_table[65];
89
thsa04e1342007-09-27 13:57:58 +000090static inline int on_sig_stack(unsigned long sp)
91{
92 return (sp - target_sigaltstack_used.ss_sp
93 < target_sigaltstack_used.ss_size);
94}
95
96static inline int sas_ss_flags(unsigned long sp)
97{
98 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
99 : on_sig_stack(sp) ? SS_ONSTACK : 0);
100}
101
bellard31e31b82003-02-18 22:55:36 +0000102static inline int host_to_target_signal(int sig)
103{
pbrook4cb05962008-05-30 18:05:19 +0000104 if (sig > 64)
105 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000106 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000107}
108
pbrook4cb05962008-05-30 18:05:19 +0000109int target_to_host_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +0000110{
pbrook4cb05962008-05-30 18:05:19 +0000111 if (sig > 64)
112 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000113 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000114}
115
pbrookf5545b52008-05-30 22:37:07 +0000116static inline void target_sigemptyset(target_sigset_t *set)
117{
118 memset(set, 0, sizeof(*set));
119}
120
121static inline void target_sigaddset(target_sigset_t *set, int signum)
122{
123 signum--;
124 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
125 set->sig[signum / TARGET_NSIG_BPW] |= mask;
126}
127
128static inline int target_sigismember(const target_sigset_t *set, int signum)
129{
130 signum--;
131 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
132 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
133}
134
ths5fafdf22007-09-16 21:08:06 +0000135static void host_to_target_sigset_internal(target_sigset_t *d,
bellard92319442004-06-19 16:58:13 +0000136 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000137{
138 int i;
pbrookf5545b52008-05-30 22:37:07 +0000139 target_sigemptyset(d);
140 for (i = 1; i <= TARGET_NSIG; i++) {
141 if (sigismember(s, i)) {
142 target_sigaddset(d, host_to_target_signal(i));
143 }
bellard9e5f5282003-07-13 17:33:54 +0000144 }
bellard66fb9762003-03-23 01:06:05 +0000145}
146
bellard92319442004-06-19 16:58:13 +0000147void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
148{
149 target_sigset_t d1;
150 int i;
151
152 host_to_target_sigset_internal(&d1, s);
153 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000154 d->sig[i] = tswapl(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000155}
156
blueswir18fcd3692008-08-17 20:26:25 +0000157static void target_to_host_sigset_internal(sigset_t *d,
158 const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000159{
160 int i;
pbrookf5545b52008-05-30 22:37:07 +0000161 sigemptyset(d);
162 for (i = 1; i <= TARGET_NSIG; i++) {
163 if (target_sigismember(s, i)) {
164 sigaddset(d, target_to_host_signal(i));
165 }
166 }
bellard66fb9762003-03-23 01:06:05 +0000167}
168
bellard92319442004-06-19 16:58:13 +0000169void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
170{
171 target_sigset_t s1;
172 int i;
173
174 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000175 s1.sig[i] = tswapl(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000176 target_to_host_sigset_internal(d, &s1);
177}
ths3b46e622007-09-17 08:09:54 +0000178
blueswir1992f48a2007-10-14 16:27:31 +0000179void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000180 const sigset_t *sigset)
181{
bellard9e5f5282003-07-13 17:33:54 +0000182 target_sigset_t d;
183 host_to_target_sigset(&d, sigset);
184 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000185}
186
ths5fafdf22007-09-16 21:08:06 +0000187void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000188 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000189{
bellard9e5f5282003-07-13 17:33:54 +0000190 target_sigset_t d;
191 int i;
192
193 d.sig[0] = *old_sigset;
194 for(i = 1;i < TARGET_NSIG_WORDS; i++)
195 d.sig[i] = 0;
196 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000197}
198
bellard9de5e442003-03-23 16:49:39 +0000199/* siginfo conversion */
200
ths5fafdf22007-09-16 21:08:06 +0000201static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000202 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000203{
bellard9de5e442003-03-23 16:49:39 +0000204 int sig;
205 sig = host_to_target_signal(info->si_signo);
206 tinfo->si_signo = sig;
207 tinfo->si_errno = 0;
pbrookafd7cd92008-05-31 12:14:21 +0000208 tinfo->si_code = info->si_code;
ths5fafdf22007-09-16 21:08:06 +0000209 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000210 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000211 /* should never come here, but who knows. The information for
212 the target is irrelevant */
213 tinfo->_sifields._sigfault._addr = 0;
ths7f7f7c82007-07-12 11:02:46 +0000214 } else if (sig == SIGIO) {
215 tinfo->_sifields._sigpoll._fd = info->si_fd;
bellard9de5e442003-03-23 16:49:39 +0000216 } else if (sig >= TARGET_SIGRTMIN) {
217 tinfo->_sifields._rt._pid = info->si_pid;
218 tinfo->_sifields._rt._uid = info->si_uid;
219 /* XXX: potential problem if 64 bit */
ths5fafdf22007-09-16 21:08:06 +0000220 tinfo->_sifields._rt._sigval.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000221 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000222 }
bellard66fb9762003-03-23 01:06:05 +0000223}
224
ths5fafdf22007-09-16 21:08:06 +0000225static void tswap_siginfo(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000226 const target_siginfo_t *info)
227{
228 int sig;
229 sig = info->si_signo;
230 tinfo->si_signo = tswap32(sig);
231 tinfo->si_errno = tswap32(info->si_errno);
232 tinfo->si_code = tswap32(info->si_code);
ths5fafdf22007-09-16 21:08:06 +0000233 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000234 sig == SIGBUS || sig == SIGTRAP) {
ths5fafdf22007-09-16 21:08:06 +0000235 tinfo->_sifields._sigfault._addr =
bellard9de5e442003-03-23 16:49:39 +0000236 tswapl(info->_sifields._sigfault._addr);
ths7f7f7c82007-07-12 11:02:46 +0000237 } else if (sig == SIGIO) {
238 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
bellard9de5e442003-03-23 16:49:39 +0000239 } else if (sig >= TARGET_SIGRTMIN) {
240 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
241 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000242 tinfo->_sifields._rt._sigval.sival_ptr =
bellard9de5e442003-03-23 16:49:39 +0000243 tswapl(info->_sifields._rt._sigval.sival_ptr);
244 }
245}
246
247
248void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
249{
250 host_to_target_siginfo_noswap(tinfo, info);
251 tswap_siginfo(tinfo, tinfo);
252}
253
254/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000255/* XXX: find a solution for 64 bit (additional malloced data is needed) */
bellard9de5e442003-03-23 16:49:39 +0000256void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000257{
258 info->si_signo = tswap32(tinfo->si_signo);
259 info->si_errno = tswap32(tinfo->si_errno);
260 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000261 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
262 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000263 info->si_value.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000264 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000265}
266
bellard31e31b82003-02-18 22:55:36 +0000267void signal_init(void)
268{
269 struct sigaction act;
pbrook624f7972008-05-31 16:11:38 +0000270 struct sigaction oact;
bellard9e5f5282003-07-13 17:33:54 +0000271 int i, j;
pbrook624f7972008-05-31 16:11:38 +0000272 int host_sig;
bellard31e31b82003-02-18 22:55:36 +0000273
bellard9e5f5282003-07-13 17:33:54 +0000274 /* generate signal conversion tables */
275 for(i = 1; i <= 64; i++) {
276 if (host_to_target_signal_table[i] == 0)
277 host_to_target_signal_table[i] = i;
278 }
279 for(i = 1; i <= 64; i++) {
280 j = host_to_target_signal_table[i];
281 target_to_host_signal_table[j] = i;
282 }
ths3b46e622007-09-17 08:09:54 +0000283
bellard9de5e442003-03-23 16:49:39 +0000284 /* set all host signal handlers. ALL signals are blocked during
285 the handlers to serialize them. */
pbrook624f7972008-05-31 16:11:38 +0000286 memset(sigact_table, 0, sizeof(sigact_table));
287
bellard9de5e442003-03-23 16:49:39 +0000288 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000289 act.sa_flags = SA_SIGINFO;
290 act.sa_sigaction = host_signal_handler;
pbrook624f7972008-05-31 16:11:38 +0000291 for(i = 1; i <= TARGET_NSIG; i++) {
292 host_sig = target_to_host_signal(i);
293 sigaction(host_sig, NULL, &oact);
294 if (oact.sa_sigaction == (void *)SIG_IGN) {
295 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
296 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
297 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
298 }
299 /* If there's already a handler installed then something has
300 gone horribly wrong, so don't even try to handle that case. */
301 /* Install some handlers for our own use. */
302 if (host_sig == SIGSEGV || host_sig == SIGBUS) {
303 sigaction(host_sig, &act, NULL);
304 }
bellard31e31b82003-02-18 22:55:36 +0000305 }
bellard31e31b82003-02-18 22:55:36 +0000306}
307
bellard66fb9762003-03-23 01:06:05 +0000308/* signal queue handling */
309
pbrook624f7972008-05-31 16:11:38 +0000310static inline struct sigqueue *alloc_sigqueue(CPUState *env)
bellard66fb9762003-03-23 01:06:05 +0000311{
pbrook624f7972008-05-31 16:11:38 +0000312 TaskState *ts = env->opaque;
313 struct sigqueue *q = ts->first_free;
bellard66fb9762003-03-23 01:06:05 +0000314 if (!q)
315 return NULL;
pbrook624f7972008-05-31 16:11:38 +0000316 ts->first_free = q->next;
bellard66fb9762003-03-23 01:06:05 +0000317 return q;
318}
319
pbrook624f7972008-05-31 16:11:38 +0000320static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
bellard66fb9762003-03-23 01:06:05 +0000321{
pbrook624f7972008-05-31 16:11:38 +0000322 TaskState *ts = env->opaque;
323 q->next = ts->first_free;
324 ts->first_free = q;
bellard66fb9762003-03-23 01:06:05 +0000325}
326
bellard9de5e442003-03-23 16:49:39 +0000327/* abort execution with signal */
blueswir18fcd3692008-08-17 20:26:25 +0000328static void __attribute((noreturn)) force_sig(int sig)
bellard66fb9762003-03-23 01:06:05 +0000329{
330 int host_sig;
bellard66fb9762003-03-23 01:06:05 +0000331 host_sig = target_to_host_signal(sig);
ths5fafdf22007-09-16 21:08:06 +0000332 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
bellard66fb9762003-03-23 01:06:05 +0000333 sig, strsignal(host_sig));
bellard9de5e442003-03-23 16:49:39 +0000334#if 1
bellard66fb9762003-03-23 01:06:05 +0000335 _exit(-host_sig);
bellard9de5e442003-03-23 16:49:39 +0000336#else
337 {
338 struct sigaction act;
339 sigemptyset(&act.sa_mask);
340 act.sa_flags = SA_SIGINFO;
341 act.sa_sigaction = SIG_DFL;
342 sigaction(SIGABRT, &act, NULL);
343 abort();
344 }
345#endif
bellard66fb9762003-03-23 01:06:05 +0000346}
347
bellard9de5e442003-03-23 16:49:39 +0000348/* queue a signal so that it will be send to the virtual CPU as soon
349 as possible */
pbrook624f7972008-05-31 16:11:38 +0000350int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000351{
pbrook624f7972008-05-31 16:11:38 +0000352 TaskState *ts = env->opaque;
353 struct emulated_sigtable *k;
bellard9de5e442003-03-23 16:49:39 +0000354 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000355 abi_ulong handler;
bellard66fb9762003-03-23 01:06:05 +0000356
bellard9de5e442003-03-23 16:49:39 +0000357#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000358 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000359 sig);
bellard66fb9762003-03-23 01:06:05 +0000360#endif
pbrook624f7972008-05-31 16:11:38 +0000361 k = &ts->sigtab[sig - 1];
362 handler = sigact_table[sig - 1]._sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000363 if (handler == TARGET_SIG_DFL) {
364 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000365 if (sig != TARGET_SIGCHLD &&
366 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +0000367 sig != TARGET_SIGWINCH) {
368 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000369 } else {
370 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000371 }
372 } else if (handler == TARGET_SIG_IGN) {
373 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000374 return 0;
bellard66fb9762003-03-23 01:06:05 +0000375 } else if (handler == TARGET_SIG_ERR) {
376 force_sig(sig);
377 } else {
bellard9de5e442003-03-23 16:49:39 +0000378 pq = &k->first;
379 if (sig < TARGET_SIGRTMIN) {
380 /* if non real time signal, we queue exactly one signal */
381 if (!k->pending)
382 q = &k->info;
383 else
384 return 0;
385 } else {
386 if (!k->pending) {
387 /* first signal */
388 q = &k->info;
389 } else {
pbrook624f7972008-05-31 16:11:38 +0000390 q = alloc_sigqueue(env);
bellard9de5e442003-03-23 16:49:39 +0000391 if (!q)
392 return -EAGAIN;
393 while (*pq != NULL)
394 pq = &(*pq)->next;
395 }
396 }
397 *pq = q;
398 q->info = *info;
399 q->next = NULL;
400 k->pending = 1;
401 /* signal that a new signal is pending */
pbrook624f7972008-05-31 16:11:38 +0000402 ts->signal_pending = 1;
bellard9de5e442003-03-23 16:49:39 +0000403 return 1; /* indicates that the signal was queued */
404 }
405}
406
ths5fafdf22007-09-16 21:08:06 +0000407static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000408 void *puc)
409{
410 int sig;
411 target_siginfo_t tinfo;
412
413 /* the CPU emulator uses some host signals to detect exceptions,
414 we we forward to it some signals */
bellardec6338b2007-11-08 14:25:03 +0000415 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
bellardb346ff42003-06-15 20:05:50 +0000416 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000417 return;
418 }
419
420 /* get target signal number */
421 sig = host_to_target_signal(host_signum);
422 if (sig < 1 || sig > TARGET_NSIG)
423 return;
424#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000425 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000426#endif
427 host_to_target_siginfo_noswap(&tinfo, info);
pbrookd5975362008-06-07 20:50:51 +0000428 if (queue_signal(thread_env, sig, &tinfo) == 1) {
bellard9de5e442003-03-23 16:49:39 +0000429 /* interrupt the virtual CPU as soon as possible */
pbrookd5975362008-06-07 20:50:51 +0000430 cpu_interrupt(thread_env, CPU_INTERRUPT_EXIT);
bellard66fb9762003-03-23 01:06:05 +0000431 }
bellard31e31b82003-02-18 22:55:36 +0000432}
433
ths0da46a62007-10-20 20:23:07 +0000434/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000435/* compare linux/kernel/signal.c:do_sigaltstack() */
436abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000437{
438 int ret;
439 struct target_sigaltstack oss;
440
441 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000442 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000443 {
444 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
445 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
446 __put_user(sas_ss_flags(sp), &oss.ss_flags);
447 }
448
bellard579a97f2007-11-11 14:26:47 +0000449 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000450 {
bellard579a97f2007-11-11 14:26:47 +0000451 struct target_sigaltstack *uss;
452 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000453
ths0da46a62007-10-20 20:23:07 +0000454 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000455 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000456 || __get_user(ss.ss_sp, &uss->ss_sp)
457 || __get_user(ss.ss_size, &uss->ss_size)
458 || __get_user(ss.ss_flags, &uss->ss_flags))
459 goto out;
bellard579a97f2007-11-11 14:26:47 +0000460 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000461
ths0da46a62007-10-20 20:23:07 +0000462 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000463 if (on_sig_stack(sp))
464 goto out;
465
ths0da46a62007-10-20 20:23:07 +0000466 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000467 if (ss.ss_flags != TARGET_SS_DISABLE
468 && ss.ss_flags != TARGET_SS_ONSTACK
469 && ss.ss_flags != 0)
470 goto out;
471
472 if (ss.ss_flags == TARGET_SS_DISABLE) {
473 ss.ss_size = 0;
474 ss.ss_sp = 0;
475 } else {
ths0da46a62007-10-20 20:23:07 +0000476 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000477 if (ss.ss_size < MINSIGSTKSZ)
478 goto out;
479 }
480
481 target_sigaltstack_used.ss_sp = ss.ss_sp;
482 target_sigaltstack_used.ss_size = ss.ss_size;
483 }
484
bellard579a97f2007-11-11 14:26:47 +0000485 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000486 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000487 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000488 goto out;
thsa04e1342007-09-27 13:57:58 +0000489 }
490
491 ret = 0;
492out:
493 return ret;
494}
495
ths0da46a62007-10-20 20:23:07 +0000496/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000497int do_sigaction(int sig, const struct target_sigaction *act,
498 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000499{
pbrook624f7972008-05-31 16:11:38 +0000500 struct target_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000501 struct sigaction act1;
502 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000503 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000504
ths0aeaa8c2007-03-31 19:29:06 +0000505 if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000506 return -EINVAL;
507 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000508#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000509 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
bellard66fb9762003-03-23 01:06:05 +0000510 sig, (int)act, (int)oact);
511#endif
512 if (oact) {
pbrook624f7972008-05-31 16:11:38 +0000513 oact->_sa_handler = tswapl(k->_sa_handler);
514 oact->sa_flags = tswapl(k->sa_flags);
ths388bb212007-05-13 13:58:00 +0000515#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000516 oact->sa_restorer = tswapl(k->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000517#endif
pbrook624f7972008-05-31 16:11:38 +0000518 oact->sa_mask = k->sa_mask;
bellard66fb9762003-03-23 01:06:05 +0000519 }
520 if (act) {
pbrook624f7972008-05-31 16:11:38 +0000521 /* FIXME: This is not threadsafe. */
522 k->_sa_handler = tswapl(act->_sa_handler);
523 k->sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000524#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000525 k->sa_restorer = tswapl(act->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000526#endif
pbrook624f7972008-05-31 16:11:38 +0000527 k->sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000528
529 /* we update the host linux signal state */
530 host_sig = target_to_host_signal(sig);
531 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
532 sigfillset(&act1.sa_mask);
533 act1.sa_flags = SA_SIGINFO;
pbrook624f7972008-05-31 16:11:38 +0000534 if (k->sa_flags & TARGET_SA_RESTART)
bellard773b93e2004-01-04 17:15:59 +0000535 act1.sa_flags |= SA_RESTART;
536 /* NOTE: it is important to update the host kernel signal
537 ignore state to avoid getting unexpected interrupted
538 syscalls */
pbrook624f7972008-05-31 16:11:38 +0000539 if (k->_sa_handler == TARGET_SIG_IGN) {
bellard773b93e2004-01-04 17:15:59 +0000540 act1.sa_sigaction = (void *)SIG_IGN;
pbrook624f7972008-05-31 16:11:38 +0000541 } else if (k->_sa_handler == TARGET_SIG_DFL) {
bellard773b93e2004-01-04 17:15:59 +0000542 act1.sa_sigaction = (void *)SIG_DFL;
543 } else {
544 act1.sa_sigaction = host_signal_handler;
545 }
ths0da46a62007-10-20 20:23:07 +0000546 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000547 }
bellard66fb9762003-03-23 01:06:05 +0000548 }
ths0da46a62007-10-20 20:23:07 +0000549 return ret;
bellard66fb9762003-03-23 01:06:05 +0000550}
bellard31e31b82003-02-18 22:55:36 +0000551
ths5fafdf22007-09-16 21:08:06 +0000552static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
bellard43fff232003-07-09 19:31:39 +0000553 const target_siginfo_t *info)
554{
555 tswap_siginfo(tinfo, info);
556 return 0;
557}
558
thsc3b5bc82007-12-02 06:31:25 +0000559static inline int current_exec_domain_sig(int sig)
560{
561 return /* current->exec_domain && current->exec_domain->signal_invmap
562 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
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,
bellard28be6232007-11-11 22:23:38 +0000670 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000671{
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
bellard28be6232007-11-11 22:23:38 +0000696 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000697 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000698 magic = 0xffff;
699 err |= __put_user(magic, &fpstate->magic);
bellard28be6232007-11-11 22:23:38 +0000700 err |= __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000701
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
pbrook624f7972008-05-31 16:11:38 +0000713get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
bellard66fb9762003-03-23 01:06:05 +0000714{
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. */
pbrook624f7972008-05-31 16:11:38 +0000720 if (ka->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +0000721 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 &&
pbrook624f7972008-05-31 16:11:38 +0000728 !(ka->sa_flags & TARGET_SA_RESTORER) &&
729 ka->sa_restorer) {
730 esp = (unsigned long) ka->sa_restorer;
bellarda52c7572003-06-21 13:14:12 +0000731 }
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() */
pbrook624f7972008-05-31 16:11:38 +0000736static void setup_frame(int sig, struct target_sigaction *ka,
bellard66fb9762003-03-23 01:06:05 +0000737 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
thsc3b5bc82007-12-02 06:31:25 +0000748 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000749 &frame->sig);
750 if (err)
751 goto give_sigsegv;
752
bellard28be6232007-11-11 22:23:38 +0000753 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
754 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000755 if (err)
756 goto give_sigsegv;
757
bellard92319442004-06-19 16:58:13 +0000758 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
759 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
760 goto give_sigsegv;
761 }
bellard66fb9762003-03-23 01:06:05 +0000762
763 /* Set up to return from userspace. If provided, use a stub
764 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000765 if (ka->sa_flags & TARGET_SA_RESTORER) {
766 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000767 } else {
bellard775b58d2007-11-11 16:22:17 +0000768 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000769 abi_ulong retcode_addr;
770 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
771 err |= __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000772 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000773 val16 = 0xb858;
774 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000775 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000776 val16 = 0x80cd;
777 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000778 }
779
780 if (err)
781 goto give_sigsegv;
782
783 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000784 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000785 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000786
787 cpu_x86_load_seg(env, R_DS, __USER_DS);
788 cpu_x86_load_seg(env, R_ES, __USER_DS);
789 cpu_x86_load_seg(env, R_SS, __USER_DS);
790 cpu_x86_load_seg(env, R_CS, __USER_CS);
791 env->eflags &= ~TF_MASK;
792
bellard579a97f2007-11-11 14:26:47 +0000793 unlock_user_struct(frame, frame_addr, 1);
794
bellard66fb9762003-03-23 01:06:05 +0000795 return;
796
797give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000798 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000799 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000800 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000801 force_sig(TARGET_SIGSEGV /* , current */);
802}
803
bellard579a97f2007-11-11 14:26:47 +0000804/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +0000805static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellard9de5e442003-03-23 16:49:39 +0000806 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000807 target_sigset_t *set, CPUX86State *env)
808{
bellard28be6232007-11-11 22:23:38 +0000809 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000810 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000811 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000812
bellard579a97f2007-11-11 14:26:47 +0000813 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000814
bellard579a97f2007-11-11 14:26:47 +0000815 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000816 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000817
thsc3b5bc82007-12-02 06:31:25 +0000818 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000819 &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000820 addr = frame_addr + offsetof(struct rt_sigframe, info);
821 err |= __put_user(addr, &frame->pinfo);
822 addr = frame_addr + offsetof(struct rt_sigframe, uc);
823 err |= __put_user(addr, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000824 err |= copy_siginfo_to_user(&frame->info, info);
825 if (err)
826 goto give_sigsegv;
827
828 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000829 err |= __put_user(0, &frame->uc.tuc_flags);
830 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000831 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000832 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000833 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000834 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000835 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000836 &frame->uc.tuc_stack.ss_size);
837 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000838 env, set->sig[0],
839 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +0000840 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000841 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000842 goto give_sigsegv;
843 }
bellard66fb9762003-03-23 01:06:05 +0000844
845 /* Set up to return from userspace. If provided, use a stub
846 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000847 if (ka->sa_flags & TARGET_SA_RESTORER) {
848 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000849 } else {
bellard775b58d2007-11-11 16:22:17 +0000850 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000851 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
852 err |= __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000853 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000854 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000855 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000856 val16 = 0x80cd;
857 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000858 }
859
860 if (err)
861 goto give_sigsegv;
862
863 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000864 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000865 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000866
867 cpu_x86_load_seg(env, R_DS, __USER_DS);
868 cpu_x86_load_seg(env, R_ES, __USER_DS);
869 cpu_x86_load_seg(env, R_SS, __USER_DS);
870 cpu_x86_load_seg(env, R_CS, __USER_CS);
871 env->eflags &= ~TF_MASK;
872
bellard579a97f2007-11-11 14:26:47 +0000873 unlock_user_struct(frame, frame_addr, 1);
874
bellard66fb9762003-03-23 01:06:05 +0000875 return;
876
877give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000878 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000879 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000880 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000881 force_sig(TARGET_SIGSEGV /* , current */);
882}
883
884static int
885restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
886{
887 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +0000888 abi_ulong fpstate_addr;
889 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +0000890
bellard28be6232007-11-11 22:23:38 +0000891 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
892 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
893 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
894 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +0000895
bellard28be6232007-11-11 22:23:38 +0000896 env->regs[R_EDI] = tswapl(sc->edi);
897 env->regs[R_ESI] = tswapl(sc->esi);
898 env->regs[R_EBP] = tswapl(sc->ebp);
899 env->regs[R_ESP] = tswapl(sc->esp);
900 env->regs[R_EBX] = tswapl(sc->ebx);
901 env->regs[R_EDX] = tswapl(sc->edx);
902 env->regs[R_ECX] = tswapl(sc->ecx);
903 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +0000904
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
bellard28be6232007-11-11 22:23:38 +0000908 tmpflags = tswapl(sc->eflags);
909 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
910 // regs->orig_eax = -1; /* disable syscall checks */
911
912 fpstate_addr = tswapl(sc->fpstate);
913 if (fpstate_addr != 0) {
914 if (!access_ok(VERIFY_READ, fpstate_addr,
915 sizeof(struct target_fpstate)))
916 goto badframe;
917 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000918 }
919
bellard28be6232007-11-11 22:23:38 +0000920 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +0000921 return err;
bellard66fb9762003-03-23 01:06:05 +0000922badframe:
923 return 1;
bellard66fb9762003-03-23 01:06:05 +0000924}
925
926long do_sigreturn(CPUX86State *env)
927{
bellard579a97f2007-11-11 14:26:47 +0000928 struct sigframe *frame;
929 abi_ulong frame_addr = env->regs[R_ESP] - 8;
bellard66fb9762003-03-23 01:06:05 +0000930 target_sigset_t target_set;
931 sigset_t set;
932 int eax, i;
933
bellard447db212003-05-10 15:10:36 +0000934#if defined(DEBUG_SIGNAL)
935 fprintf(stderr, "do_sigreturn\n");
936#endif
bellard579a97f2007-11-11 14:26:47 +0000937 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
938 goto badframe;
bellard66fb9762003-03-23 01:06:05 +0000939 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +0000940 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
941 goto badframe;
942 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
943 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
944 goto badframe;
945 }
bellard66fb9762003-03-23 01:06:05 +0000946
bellard92319442004-06-19 16:58:13 +0000947 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +0000948 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +0000949
bellard66fb9762003-03-23 01:06:05 +0000950 /* restore registers */
951 if (restore_sigcontext(env, &frame->sc, &eax))
952 goto badframe;
bellard579a97f2007-11-11 14:26:47 +0000953 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000954 return eax;
955
956badframe:
bellard579a97f2007-11-11 14:26:47 +0000957 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000958 force_sig(TARGET_SIGSEGV);
959 return 0;
960}
961
962long do_rt_sigreturn(CPUX86State *env)
963{
bellard28be6232007-11-11 22:23:38 +0000964 abi_ulong frame_addr;
965 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +0000966 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +0000967 int eax;
968
bellard28be6232007-11-11 22:23:38 +0000969 frame_addr = env->regs[R_ESP] - 4;
970 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
971 goto badframe;
bellardb8076a72005-04-07 22:20:31 +0000972 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +0000973 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +0000974
bellardb8076a72005-04-07 22:20:31 +0000975 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +0000976 goto badframe;
977
bellard28be6232007-11-11 22:23:38 +0000978 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
979 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +0000980 goto badframe;
thsa04e1342007-09-27 13:57:58 +0000981
bellard28be6232007-11-11 22:23:38 +0000982 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000983 return eax;
984
985badframe:
bellard28be6232007-11-11 22:23:38 +0000986 unlock_user_struct(frame, frame_addr, 0);
987 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +0000988 return 0;
989}
990
bellard43fff232003-07-09 19:31:39 +0000991#elif defined(TARGET_ARM)
992
993struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +0000994 abi_ulong trap_no;
995 abi_ulong error_code;
996 abi_ulong oldmask;
997 abi_ulong arm_r0;
998 abi_ulong arm_r1;
999 abi_ulong arm_r2;
1000 abi_ulong arm_r3;
1001 abi_ulong arm_r4;
1002 abi_ulong arm_r5;
1003 abi_ulong arm_r6;
1004 abi_ulong arm_r7;
1005 abi_ulong arm_r8;
1006 abi_ulong arm_r9;
1007 abi_ulong arm_r10;
1008 abi_ulong arm_fp;
1009 abi_ulong arm_ip;
1010 abi_ulong arm_sp;
1011 abi_ulong arm_lr;
1012 abi_ulong arm_pc;
1013 abi_ulong arm_cpsr;
1014 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001015};
1016
pbrooka745ec62008-05-06 15:36:17 +00001017struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001018 abi_ulong tuc_flags;
1019 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +00001020 target_stack_t tuc_stack;
1021 struct target_sigcontext tuc_mcontext;
1022 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001023};
1024
pbrooka745ec62008-05-06 15:36:17 +00001025struct target_ucontext_v2 {
1026 abi_ulong tuc_flags;
1027 abi_ulong tuc_link;
1028 target_stack_t tuc_stack;
1029 struct target_sigcontext tuc_mcontext;
1030 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1031 char __unused[128 - sizeof(sigset_t)];
1032 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1033};
1034
pbrooka8c33202008-05-07 23:22:46 +00001035struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001036{
1037 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001038 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1039 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001040};
1041
pbrooka8c33202008-05-07 23:22:46 +00001042struct sigframe_v2
1043{
1044 struct target_ucontext_v2 uc;
1045 abi_ulong retcode;
1046};
1047
pbrooka745ec62008-05-06 15:36:17 +00001048struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001049{
bellardf8b0aa22007-11-11 23:03:42 +00001050 abi_ulong pinfo;
1051 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001052 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001053 struct target_ucontext_v1 uc;
1054 abi_ulong retcode;
1055};
1056
1057struct rt_sigframe_v2
1058{
1059 struct target_siginfo info;
1060 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001061 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001062};
1063
1064#define TARGET_CONFIG_CPU_32 1
1065
1066/*
1067 * For ARM syscalls, we encode the syscall number into the instruction.
1068 */
1069#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1070#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1071
1072/*
1073 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1074 * need two 16-bit instructions.
1075 */
1076#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1077#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1078
blueswir1992f48a2007-10-14 16:27:31 +00001079static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001080 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1081 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1082};
1083
1084
bellard43fff232003-07-09 19:31:39 +00001085#define __get_user_error(x,p,e) __get_user(x, p)
1086
1087static inline int valid_user_regs(CPUState *regs)
1088{
1089 return 1;
1090}
1091
pbrooka8c33202008-05-07 23:22:46 +00001092static void
bellard43fff232003-07-09 19:31:39 +00001093setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
bellardf8b0aa22007-11-11 23:03:42 +00001094 CPUState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001095{
pbrooka8c33202008-05-07 23:22:46 +00001096 __put_user(env->regs[0], &sc->arm_r0);
1097 __put_user(env->regs[1], &sc->arm_r1);
1098 __put_user(env->regs[2], &sc->arm_r2);
1099 __put_user(env->regs[3], &sc->arm_r3);
1100 __put_user(env->regs[4], &sc->arm_r4);
1101 __put_user(env->regs[5], &sc->arm_r5);
1102 __put_user(env->regs[6], &sc->arm_r6);
1103 __put_user(env->regs[7], &sc->arm_r7);
1104 __put_user(env->regs[8], &sc->arm_r8);
1105 __put_user(env->regs[9], &sc->arm_r9);
1106 __put_user(env->regs[10], &sc->arm_r10);
1107 __put_user(env->regs[11], &sc->arm_fp);
1108 __put_user(env->regs[12], &sc->arm_ip);
1109 __put_user(env->regs[13], &sc->arm_sp);
1110 __put_user(env->regs[14], &sc->arm_lr);
1111 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001112#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001113 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001114#endif
1115
pbrooka8c33202008-05-07 23:22:46 +00001116 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1117 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1118 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1119 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001120}
1121
bellard579a97f2007-11-11 14:26:47 +00001122static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00001123get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
bellard43fff232003-07-09 19:31:39 +00001124{
1125 unsigned long sp = regs->regs[13];
1126
bellard43fff232003-07-09 19:31:39 +00001127 /*
1128 * This is the X/Open sanctioned signal stack switching.
1129 */
pbrook624f7972008-05-31 16:11:38 +00001130 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
thsa04e1342007-09-27 13:57:58 +00001131 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001132 /*
1133 * ATPCS B01 mandates 8-byte alignment
1134 */
bellard579a97f2007-11-11 14:26:47 +00001135 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001136}
1137
1138static int
pbrook624f7972008-05-31 16:11:38 +00001139setup_return(CPUState *env, struct target_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001140 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001141{
pbrook624f7972008-05-31 16:11:38 +00001142 abi_ulong handler = ka->_sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001143 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001144 int thumb = handler & 1;
bellard43fff232003-07-09 19:31:39 +00001145
pbrook624f7972008-05-31 16:11:38 +00001146 if (ka->sa_flags & TARGET_SA_RESTORER) {
1147 retcode = ka->sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001148 } else {
1149 unsigned int idx = thumb;
1150
pbrook624f7972008-05-31 16:11:38 +00001151 if (ka->sa_flags & TARGET_SA_SIGINFO)
bellard43fff232003-07-09 19:31:39 +00001152 idx += 2;
1153
1154 if (__put_user(retcodes[idx], rc))
1155 return 1;
1156#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001157 flush_icache_range((abi_ulong)rc,
1158 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001159#endif
bellardf8b0aa22007-11-11 23:03:42 +00001160 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001161 }
1162
1163 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001164 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001165 env->regs[14] = retcode;
1166 env->regs[15] = handler & (thumb ? ~1 : ~3);
pbrook75b680e2008-03-21 16:07:30 +00001167 env->thumb = thumb;
bellard43fff232003-07-09 19:31:39 +00001168
bellardb5ff1b32005-11-26 10:38:39 +00001169#if 0
bellard43fff232003-07-09 19:31:39 +00001170#ifdef TARGET_CONFIG_CPU_32
1171 env->cpsr = cpsr;
1172#endif
bellardb5ff1b32005-11-26 10:38:39 +00001173#endif
bellard43fff232003-07-09 19:31:39 +00001174
1175 return 0;
1176}
1177
pbrooka8c33202008-05-07 23:22:46 +00001178static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1179 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001180{
pbrooka8c33202008-05-07 23:22:46 +00001181 struct target_sigaltstack stack;
1182 int i;
1183
1184 /* Clear all the bits of the ucontext we don't use. */
1185 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1186
1187 memset(&stack, 0, sizeof(stack));
1188 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1189 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1190 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1191 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1192
1193 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1194 /* FIXME: Save coprocessor signal frame. */
1195 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1196 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1197 }
1198}
1199
1200/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00001201static void setup_frame_v1(int usig, struct target_sigaction *ka,
pbrooka8c33202008-05-07 23:22:46 +00001202 target_sigset_t *set, CPUState *regs)
1203{
1204 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001205 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001206 int i;
bellard43fff232003-07-09 19:31:39 +00001207
bellard579a97f2007-11-11 14:26:47 +00001208 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1209 return;
1210
pbrooka8c33202008-05-07 23:22:46 +00001211 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001212
bellard92319442004-06-19 16:58:13 +00001213 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1214 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001215 goto end;
bellard43fff232003-07-09 19:31:39 +00001216 }
1217
pbrooka8c33202008-05-07 23:22:46 +00001218 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1219 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001220
1221end:
1222 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001223}
1224
pbrook624f7972008-05-31 16:11:38 +00001225static void setup_frame_v2(int usig, struct target_sigaction *ka,
pbrooka8c33202008-05-07 23:22:46 +00001226 target_sigset_t *set, CPUState *regs)
1227{
1228 struct sigframe_v2 *frame;
1229 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1230
1231 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1232 return;
1233
1234 setup_sigframe_v2(&frame->uc, set, regs);
1235
1236 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1237 frame_addr + offsetof(struct sigframe_v2, retcode));
1238
1239 unlock_user_struct(frame, frame_addr, 1);
1240}
1241
pbrook624f7972008-05-31 16:11:38 +00001242static void setup_frame(int usig, struct target_sigaction *ka,
pbrooka8c33202008-05-07 23:22:46 +00001243 target_sigset_t *set, CPUState *regs)
1244{
1245 if (get_osversion() >= 0x020612) {
1246 setup_frame_v2(usig, ka, set, regs);
1247 } else {
1248 setup_frame_v1(usig, ka, set, regs);
1249 }
bellard43fff232003-07-09 19:31:39 +00001250}
1251
bellard579a97f2007-11-11 14:26:47 +00001252/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +00001253static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
pbrooka745ec62008-05-06 15:36:17 +00001254 target_siginfo_t *info,
1255 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001256{
pbrooka745ec62008-05-06 15:36:17 +00001257 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001258 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001259 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001260 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001261 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001262
bellard579a97f2007-11-11 14:26:47 +00001263 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001264 return /* 1 */;
1265
pbrooka745ec62008-05-06 15:36:17 +00001266 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001267 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001268 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001269 __put_user(uc_addr, &frame->puc);
1270 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001271
1272 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001273 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001274
thsa04e1342007-09-27 13:57:58 +00001275 memset(&stack, 0, sizeof(stack));
1276 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1277 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1278 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001279 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001280
pbrooka8c33202008-05-07 23:22:46 +00001281 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001282 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001283 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001284 goto end;
bellard92319442004-06-19 16:58:13 +00001285 }
bellard43fff232003-07-09 19:31:39 +00001286
pbrooka8c33202008-05-07 23:22:46 +00001287 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1288 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001289
pbrooka8c33202008-05-07 23:22:46 +00001290 env->regs[1] = info_addr;
1291 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001292
1293end:
1294 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001295}
1296
pbrook624f7972008-05-31 16:11:38 +00001297static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
pbrooka745ec62008-05-06 15:36:17 +00001298 target_siginfo_t *info,
1299 target_sigset_t *set, CPUState *env)
1300{
1301 struct rt_sigframe_v2 *frame;
1302 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001303 abi_ulong info_addr, uc_addr;
1304
1305 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1306 return /* 1 */;
1307
1308 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1309 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001310 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001311
pbrooka8c33202008-05-07 23:22:46 +00001312 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001313
pbrooka8c33202008-05-07 23:22:46 +00001314 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1315 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001316
pbrooka8c33202008-05-07 23:22:46 +00001317 env->regs[1] = info_addr;
1318 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001319
bellard579a97f2007-11-11 14:26:47 +00001320 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001321}
1322
pbrook624f7972008-05-31 16:11:38 +00001323static void setup_rt_frame(int usig, struct target_sigaction *ka,
pbrooka745ec62008-05-06 15:36:17 +00001324 target_siginfo_t *info,
1325 target_sigset_t *set, CPUState *env)
1326{
1327 if (get_osversion() >= 0x020612) {
1328 setup_rt_frame_v2(usig, ka, info, set, env);
1329 } else {
1330 setup_rt_frame_v1(usig, ka, info, set, env);
1331 }
1332}
1333
bellard43fff232003-07-09 19:31:39 +00001334static int
1335restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1336{
1337 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001338 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001339
1340 __get_user_error(env->regs[0], &sc->arm_r0, err);
1341 __get_user_error(env->regs[1], &sc->arm_r1, err);
1342 __get_user_error(env->regs[2], &sc->arm_r2, err);
1343 __get_user_error(env->regs[3], &sc->arm_r3, err);
1344 __get_user_error(env->regs[4], &sc->arm_r4, err);
1345 __get_user_error(env->regs[5], &sc->arm_r5, err);
1346 __get_user_error(env->regs[6], &sc->arm_r6, err);
1347 __get_user_error(env->regs[7], &sc->arm_r7, err);
1348 __get_user_error(env->regs[8], &sc->arm_r8, err);
1349 __get_user_error(env->regs[9], &sc->arm_r9, err);
1350 __get_user_error(env->regs[10], &sc->arm_r10, err);
1351 __get_user_error(env->regs[11], &sc->arm_fp, err);
1352 __get_user_error(env->regs[12], &sc->arm_ip, err);
1353 __get_user_error(env->regs[13], &sc->arm_sp, err);
1354 __get_user_error(env->regs[14], &sc->arm_lr, err);
1355 __get_user_error(env->regs[15], &sc->arm_pc, err);
1356#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001357 __get_user_error(cpsr, &sc->arm_cpsr, err);
pbrook75b680e2008-03-21 16:07:30 +00001358 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001359#endif
1360
1361 err |= !valid_user_regs(env);
1362
1363 return err;
1364}
1365
pbrooka8c33202008-05-07 23:22:46 +00001366long do_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001367{
bellardf8b0aa22007-11-11 23:03:42 +00001368 abi_ulong frame_addr;
pbrooka8c33202008-05-07 23:22:46 +00001369 struct sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001370 target_sigset_t set;
1371 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001372 int i;
bellard43fff232003-07-09 19:31:39 +00001373
1374 /*
1375 * Since we stacked the signal on a 64-bit boundary,
1376 * then 'sp' should be word aligned here. If it's
1377 * not, then the user is trying to mess with us.
1378 */
1379 if (env->regs[13] & 7)
1380 goto badframe;
1381
bellardf8b0aa22007-11-11 23:03:42 +00001382 frame_addr = env->regs[13];
1383 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1384 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001385
bellard92319442004-06-19 16:58:13 +00001386 if (__get_user(set.sig[0], &frame->sc.oldmask))
1387 goto badframe;
1388 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1389 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1390 goto badframe;
1391 }
bellard43fff232003-07-09 19:31:39 +00001392
bellard92319442004-06-19 16:58:13 +00001393 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001394 sigprocmask(SIG_SETMASK, &host_set, NULL);
1395
1396 if (restore_sigcontext(env, &frame->sc))
1397 goto badframe;
1398
1399#if 0
1400 /* Send SIGTRAP if we're single-stepping */
1401 if (ptrace_cancel_bpt(current))
1402 send_sig(SIGTRAP, current, 1);
1403#endif
bellardf8b0aa22007-11-11 23:03:42 +00001404 unlock_user_struct(frame, frame_addr, 0);
1405 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001406
1407badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001408 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001409 force_sig(SIGSEGV /* , current */);
1410 return 0;
1411}
1412
pbrooka8c33202008-05-07 23:22:46 +00001413static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1414 struct target_ucontext_v2 *uc)
1415{
1416 sigset_t host_set;
1417
1418 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1419 sigprocmask(SIG_SETMASK, &host_set, NULL);
1420
1421 if (restore_sigcontext(env, &uc->tuc_mcontext))
1422 return 1;
1423
1424 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1425 return 1;
1426
1427#if 0
1428 /* Send SIGTRAP if we're single-stepping */
1429 if (ptrace_cancel_bpt(current))
1430 send_sig(SIGTRAP, current, 1);
1431#endif
1432
1433 return 0;
1434}
1435
1436long do_sigreturn_v2(CPUState *env)
1437{
1438 abi_ulong frame_addr;
1439 struct sigframe_v2 *frame;
1440
1441 /*
1442 * Since we stacked the signal on a 64-bit boundary,
1443 * then 'sp' should be word aligned here. If it's
1444 * not, then the user is trying to mess with us.
1445 */
1446 if (env->regs[13] & 7)
1447 goto badframe;
1448
1449 frame_addr = env->regs[13];
1450 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1451 goto badframe;
1452
1453 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1454 goto badframe;
1455
1456 unlock_user_struct(frame, frame_addr, 0);
1457 return env->regs[0];
1458
1459badframe:
1460 unlock_user_struct(frame, frame_addr, 0);
1461 force_sig(SIGSEGV /* , current */);
1462 return 0;
1463}
1464
1465long do_sigreturn(CPUState *env)
1466{
1467 if (get_osversion() >= 0x020612) {
1468 return do_sigreturn_v2(env);
1469 } else {
1470 return do_sigreturn_v1(env);
1471 }
1472}
1473
pbrooka745ec62008-05-06 15:36:17 +00001474long do_rt_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001475{
bellardf8b0aa22007-11-11 23:03:42 +00001476 abi_ulong frame_addr;
pbrooka745ec62008-05-06 15:36:17 +00001477 struct rt_sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001478 sigset_t host_set;
1479
1480 /*
1481 * Since we stacked the signal on a 64-bit boundary,
1482 * then 'sp' should be word aligned here. If it's
1483 * not, then the user is trying to mess with us.
1484 */
1485 if (env->regs[13] & 7)
1486 goto badframe;
1487
bellardf8b0aa22007-11-11 23:03:42 +00001488 frame_addr = env->regs[13];
1489 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1490 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001491
bellardb8076a72005-04-07 22:20:31 +00001492 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001493 sigprocmask(SIG_SETMASK, &host_set, NULL);
1494
bellardb8076a72005-04-07 22:20:31 +00001495 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001496 goto badframe;
1497
pbrooka745ec62008-05-06 15:36:17 +00001498 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
thsa04e1342007-09-27 13:57:58 +00001499 goto badframe;
1500
bellard43fff232003-07-09 19:31:39 +00001501#if 0
1502 /* Send SIGTRAP if we're single-stepping */
1503 if (ptrace_cancel_bpt(current))
1504 send_sig(SIGTRAP, current, 1);
1505#endif
bellardf8b0aa22007-11-11 23:03:42 +00001506 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001507 return env->regs[0];
1508
1509badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001510 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001511 force_sig(SIGSEGV /* , current */);
1512 return 0;
1513}
1514
pbrooka745ec62008-05-06 15:36:17 +00001515long do_rt_sigreturn_v2(CPUState *env)
1516{
1517 abi_ulong frame_addr;
1518 struct rt_sigframe_v2 *frame;
pbrooka745ec62008-05-06 15:36:17 +00001519
1520 /*
1521 * Since we stacked the signal on a 64-bit boundary,
1522 * then 'sp' should be word aligned here. If it's
1523 * not, then the user is trying to mess with us.
1524 */
1525 if (env->regs[13] & 7)
1526 goto badframe;
1527
1528 frame_addr = env->regs[13];
1529 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1530 goto badframe;
1531
pbrooka8c33202008-05-07 23:22:46 +00001532 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1533 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00001534
pbrooka745ec62008-05-06 15:36:17 +00001535 unlock_user_struct(frame, frame_addr, 0);
1536 return env->regs[0];
1537
1538badframe:
1539 unlock_user_struct(frame, frame_addr, 0);
1540 force_sig(SIGSEGV /* , current */);
1541 return 0;
1542}
1543
1544long do_rt_sigreturn(CPUState *env)
1545{
1546 if (get_osversion() >= 0x020612) {
1547 return do_rt_sigreturn_v2(env);
1548 } else {
1549 return do_rt_sigreturn_v1(env);
1550 }
1551}
1552
bellard6d5e2162004-09-30 22:04:13 +00001553#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001554
bellard6d5e2162004-09-30 22:04:13 +00001555#define __SUNOS_MAXWIN 31
1556
1557/* This is what SunOS does, so shall I. */
1558struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001559 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001560
blueswir1992f48a2007-10-14 16:27:31 +00001561 abi_ulong sigc_mask; /* sigmask to restore */
1562 abi_ulong sigc_sp; /* stack pointer */
1563 abi_ulong sigc_pc; /* program counter */
1564 abi_ulong sigc_npc; /* next program counter */
1565 abi_ulong sigc_psr; /* for condition codes etc */
1566 abi_ulong sigc_g1; /* User uses these two registers */
1567 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001568
1569 /* Now comes information regarding the users window set
1570 * at the time of the signal.
1571 */
blueswir1992f48a2007-10-14 16:27:31 +00001572 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001573
1574 /* stack ptrs for each regwin buf */
1575 char *sigc_spbuf[__SUNOS_MAXWIN];
1576
1577 /* Windows to restore after signal */
1578 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001579 abi_ulong locals[8];
1580 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001581 } sigc_wbuf[__SUNOS_MAXWIN];
1582};
1583/* A Sparc stack frame */
1584struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001585 abi_ulong locals[8];
1586 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001587 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001588 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001589 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001590 abi_ulong xargs[6];
1591 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001592};
1593
1594typedef struct {
1595 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001596 abi_ulong psr;
1597 abi_ulong pc;
1598 abi_ulong npc;
1599 abi_ulong y;
1600 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001601 } si_regs;
1602 int si_mask;
1603} __siginfo_t;
1604
1605typedef struct {
1606 unsigned long si_float_regs [32];
1607 unsigned long si_fsr;
1608 unsigned long si_fpqdepth;
1609 struct {
1610 unsigned long *insn_addr;
1611 unsigned long insn;
1612 } si_fpqueue [16];
bellard74ccb342006-07-18 21:23:34 +00001613} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001614
1615
1616struct target_signal_frame {
1617 struct sparc_stackf ss;
1618 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00001619 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001620 abi_ulong insns[2] __attribute__ ((aligned (8)));
1621 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1622 abi_ulong extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001623 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001624};
1625struct target_rt_signal_frame {
1626 struct sparc_stackf ss;
1627 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001628 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001629 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00001630 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001631 unsigned int insns[2];
1632 stack_t stack;
1633 unsigned int extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001634 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001635};
1636
bellarde80cfcf2004-12-19 23:18:01 +00001637#define UREG_O0 16
1638#define UREG_O6 22
1639#define UREG_I0 0
1640#define UREG_I1 1
1641#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001642#define UREG_I3 3
1643#define UREG_I4 4
1644#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001645#define UREG_I6 6
1646#define UREG_I7 7
1647#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001648#define UREG_FP UREG_I6
1649#define UREG_SP UREG_O6
1650
pbrook624f7972008-05-31 16:11:38 +00001651static inline abi_ulong get_sigframe(struct target_sigaction *sa,
bellard459a4012007-11-11 19:45:10 +00001652 CPUState *env, unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001653{
bellard459a4012007-11-11 19:45:10 +00001654 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001655
1656 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001657
1658 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00001659 if (sa->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +00001660 if (!on_sig_stack(sp)
1661 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1662 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001663 }
bellard459a4012007-11-11 19:45:10 +00001664 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001665}
1666
1667static int
blueswir1992f48a2007-10-14 16:27:31 +00001668setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001669{
1670 int err = 0, i;
1671
bellard6d5e2162004-09-30 22:04:13 +00001672 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001673 err |= __put_user(env->pc, &si->si_regs.pc);
1674 err |= __put_user(env->npc, &si->si_regs.npc);
1675 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001676 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001677 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1678 }
bellarda315a142005-01-30 22:59:18 +00001679 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001680 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001681 }
bellard6d5e2162004-09-30 22:04:13 +00001682 err |= __put_user(mask, &si->si_mask);
1683 return err;
1684}
bellarde80cfcf2004-12-19 23:18:01 +00001685
bellard80a9d032005-01-03 23:31:27 +00001686#if 0
bellard6d5e2162004-09-30 22:04:13 +00001687static int
1688setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1689 CPUState *env, unsigned long mask)
1690{
1691 int err = 0;
1692
1693 err |= __put_user(mask, &sc->sigc_mask);
1694 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1695 err |= __put_user(env->pc, &sc->sigc_pc);
1696 err |= __put_user(env->npc, &sc->sigc_npc);
1697 err |= __put_user(env->psr, &sc->sigc_psr);
1698 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1699 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1700
1701 return err;
1702}
bellard80a9d032005-01-03 23:31:27 +00001703#endif
bellard6d5e2162004-09-30 22:04:13 +00001704#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1705
pbrook624f7972008-05-31 16:11:38 +00001706static void setup_frame(int sig, struct target_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001707 target_sigset_t *set, CPUState *env)
1708{
bellard459a4012007-11-11 19:45:10 +00001709 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001710 struct target_signal_frame *sf;
1711 int sigframe_size, err, i;
1712
1713 /* 1. Make sure everything is clean */
1714 //synchronize_user_stack();
1715
1716 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001717 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001718
bellard459a4012007-11-11 19:45:10 +00001719 sf = lock_user(VERIFY_WRITE, sf_addr,
1720 sizeof(struct target_signal_frame), 0);
1721 if (!sf)
1722 goto sigsegv;
1723
bellarde80cfcf2004-12-19 23:18:01 +00001724 //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 +00001725#if 0
1726 if (invalid_frame_pointer(sf, sigframe_size))
1727 goto sigill_and_return;
1728#endif
1729 /* 2. Save the current process state */
1730 err = setup___siginfo(&sf->info, env, set->sig[0]);
1731 err |= __put_user(0, &sf->extra_size);
1732
1733 //err |= save_fpu_state(regs, &sf->fpu_state);
1734 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1735
1736 err |= __put_user(set->sig[0], &sf->info.si_mask);
1737 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1738 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1739 }
1740
bellarda315a142005-01-30 22:59:18 +00001741 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001742 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001743 }
bellarda315a142005-01-30 22:59:18 +00001744 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001745 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001746 }
bellard6d5e2162004-09-30 22:04:13 +00001747 if (err)
1748 goto sigsegv;
1749
1750 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001751 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001752 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001753 env->regwptr[UREG_I1] = sf_addr +
1754 offsetof(struct target_signal_frame, info);
1755 env->regwptr[UREG_I2] = sf_addr +
1756 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001757
1758 /* 4. signal handler */
pbrook624f7972008-05-31 16:11:38 +00001759 env->pc = ka->_sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00001760 env->npc = (env->pc + 4);
1761 /* 5. return to kernel instructions */
pbrook624f7972008-05-31 16:11:38 +00001762 if (ka->sa_restorer)
1763 env->regwptr[UREG_I7] = ka->sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00001764 else {
bellard775b58d2007-11-11 16:22:17 +00001765 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00001766
1767 env->regwptr[UREG_I7] = sf_addr +
1768 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00001769
1770 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001771 val32 = 0x821020d8;
1772 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001773
1774 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001775 val32 = 0x91d02010;
1776 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001777 if (err)
1778 goto sigsegv;
1779
1780 /* Flush instruction space. */
1781 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001782 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001783 }
bellard459a4012007-11-11 19:45:10 +00001784 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001785 return;
bellard459a4012007-11-11 19:45:10 +00001786#if 0
1787sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001788 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00001789#endif
bellard6d5e2162004-09-30 22:04:13 +00001790sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001791 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00001792 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001793 force_sig(TARGET_SIGSEGV);
1794}
1795static inline int
bellard74ccb342006-07-18 21:23:34 +00001796restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001797{
1798 int err;
1799#if 0
1800#ifdef CONFIG_SMP
1801 if (current->flags & PF_USEDFPU)
1802 regs->psr &= ~PSR_EF;
1803#else
1804 if (current == last_task_used_math) {
1805 last_task_used_math = 0;
1806 regs->psr &= ~PSR_EF;
1807 }
1808#endif
1809 current->used_math = 1;
1810 current->flags &= ~PF_USEDFPU;
1811#endif
1812#if 0
1813 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1814 return -EFAULT;
1815#endif
1816
bellardfafffae2006-10-28 12:09:16 +00001817#if 0
1818 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001819 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1820 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001821#endif
bellard6d5e2162004-09-30 22:04:13 +00001822 err |= __get_user(env->fsr, &fpu->si_fsr);
1823#if 0
1824 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1825 if (current->thread.fpqdepth != 0)
1826 err |= __copy_from_user(&current->thread.fpqueue[0],
1827 &fpu->si_fpqueue[0],
1828 ((sizeof(unsigned long) +
1829 (sizeof(unsigned long *)))*16));
1830#endif
1831 return err;
1832}
1833
1834
pbrook624f7972008-05-31 16:11:38 +00001835static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001836 target_siginfo_t *info,
1837 target_sigset_t *set, CPUState *env)
1838{
1839 fprintf(stderr, "setup_rt_frame: not implemented\n");
1840}
1841
1842long do_sigreturn(CPUState *env)
1843{
bellardf8b0aa22007-11-11 23:03:42 +00001844 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001845 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001846 uint32_t up_psr, pc, npc;
bellard6d5e2162004-09-30 22:04:13 +00001847 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001848 sigset_t host_set;
bellardf8b0aa22007-11-11 23:03:42 +00001849 abi_ulong fpu_save_addr;
bellarde80cfcf2004-12-19 23:18:01 +00001850 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001851
bellardf8b0aa22007-11-11 23:03:42 +00001852 sf_addr = env->regwptr[UREG_FP];
1853 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1854 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00001855#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001856 fprintf(stderr, "sigreturn\n");
1857 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 +00001858#endif
bellarde80cfcf2004-12-19 23:18:01 +00001859 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001860
1861 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00001862
bellardf8b0aa22007-11-11 23:03:42 +00001863 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00001864 goto segv_and_exit;
1865
1866 err = __get_user(pc, &sf->info.si_regs.pc);
1867 err |= __get_user(npc, &sf->info.si_regs.npc);
1868
bellard6d5e2162004-09-30 22:04:13 +00001869 if ((pc | npc) & 3)
1870 goto segv_and_exit;
1871
1872 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001873 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1874
bellard6d5e2162004-09-30 22:04:13 +00001875 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001876 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1877 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1878
1879 env->pc = pc;
1880 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001881 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001882 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001883 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1884 }
bellarda315a142005-01-30 22:59:18 +00001885 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001886 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1887 }
bellard6d5e2162004-09-30 22:04:13 +00001888
bellardf8b0aa22007-11-11 23:03:42 +00001889 err |= __get_user(fpu_save_addr, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001890
bellarde80cfcf2004-12-19 23:18:01 +00001891 //if (fpu_save)
1892 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001893
1894 /* This is pretty much atomic, no amount locking would prevent
1895 * the races which exist anyways.
1896 */
1897 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001898 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1899 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1900 }
1901
1902 target_to_host_sigset_internal(&host_set, &set);
1903 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001904
1905 if (err)
1906 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00001907 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001908 return env->regwptr[0];
1909
1910segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00001911 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001912 force_sig(TARGET_SIGSEGV);
1913}
1914
1915long do_rt_sigreturn(CPUState *env)
1916{
1917 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00001918 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00001919}
1920
bellard459a4012007-11-11 19:45:10 +00001921#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00001922#define MC_TSTATE 0
1923#define MC_PC 1
1924#define MC_NPC 2
1925#define MC_Y 3
1926#define MC_G1 4
1927#define MC_G2 5
1928#define MC_G3 6
1929#define MC_G4 7
1930#define MC_G5 8
1931#define MC_G6 9
1932#define MC_G7 10
1933#define MC_O0 11
1934#define MC_O1 12
1935#define MC_O2 13
1936#define MC_O3 14
1937#define MC_O4 15
1938#define MC_O5 16
1939#define MC_O6 17
1940#define MC_O7 18
1941#define MC_NGREG 19
1942
blueswir1992f48a2007-10-14 16:27:31 +00001943typedef abi_ulong target_mc_greg_t;
blueswir15bfb56b2007-10-05 17:01:51 +00001944typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1945
1946struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00001947 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001948 uint32_t mcfq_insn;
1949};
1950
1951struct target_mc_fpu {
1952 union {
1953 uint32_t sregs[32];
1954 uint64_t dregs[32];
1955 //uint128_t qregs[16];
1956 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00001957 abi_ulong mcfpu_fsr;
1958 abi_ulong mcfpu_fprs;
1959 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00001960 struct target_mc_fq *mcfpu_fq;
1961 unsigned char mcfpu_qcnt;
1962 unsigned char mcfpu_qentsz;
1963 unsigned char mcfpu_enab;
1964};
1965typedef struct target_mc_fpu target_mc_fpu_t;
1966
1967typedef struct {
1968 target_mc_gregset_t mc_gregs;
1969 target_mc_greg_t mc_fp;
1970 target_mc_greg_t mc_i7;
1971 target_mc_fpu_t mc_fpregs;
1972} target_mcontext_t;
1973
1974struct target_ucontext {
1975 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00001976 abi_ulong uc_flags;
blueswir15bfb56b2007-10-05 17:01:51 +00001977 target_sigset_t uc_sigmask;
1978 target_mcontext_t uc_mcontext;
1979};
1980
1981/* A V9 register window */
1982struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00001983 abi_ulong locals[8];
1984 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00001985};
1986
1987#define TARGET_STACK_BIAS 2047
1988
1989/* {set, get}context() needed for 64-bit SparcLinux userland. */
1990void sparc64_set_context(CPUSPARCState *env)
1991{
bellard459a4012007-11-11 19:45:10 +00001992 abi_ulong ucp_addr;
1993 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00001994 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00001995 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00001996 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001997 unsigned char fenab;
1998 int err;
1999 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002000
bellard459a4012007-11-11 19:45:10 +00002001 ucp_addr = env->regwptr[UREG_I0];
2002 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2003 goto do_sigsegv;
blueswir15bfb56b2007-10-05 17:01:51 +00002004 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00002005 err = __get_user(pc, &((*grp)[MC_PC]));
2006 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002007 if (err || ((pc | npc) & 3))
2008 goto do_sigsegv;
2009 if (env->regwptr[UREG_I1]) {
2010 target_sigset_t target_set;
2011 sigset_t set;
2012
2013 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002014 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002015 goto do_sigsegv;
2016 } else {
bellard459a4012007-11-11 19:45:10 +00002017 abi_ulong *src, *dst;
2018 src = ucp->uc_sigmask.sig;
2019 dst = target_set.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002020 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002021 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002022 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00002023 if (err)
2024 goto do_sigsegv;
2025 }
2026 target_to_host_sigset_internal(&set, &target_set);
2027 sigprocmask(SIG_SETMASK, &set, NULL);
2028 }
2029 env->pc = pc;
2030 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00002031 err |= __get_user(env->y, &((*grp)[MC_Y]));
2032 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002033 env->asi = (tstate >> 24) & 0xff;
2034 PUT_CCR(env, tstate >> 32);
2035 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00002036 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2037 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2038 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2039 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2040 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2041 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2042 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2043 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2044 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2045 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2046 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2047 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2048 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2049 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2050 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002051
bellard579a97f2007-11-11 14:26:47 +00002052 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2053 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002054
bellard459a4012007-11-11 19:45:10 +00002055 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2056 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2057 abi_ulong) != 0)
2058 goto do_sigsegv;
2059 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2060 abi_ulong) != 0)
2061 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002062 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2063 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002064 {
2065 uint32_t *src, *dst;
2066 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2067 dst = env->fpr;
2068 /* XXX: check that the CPU storage is the same as user context */
2069 for (i = 0; i < 64; i++, dst++, src++)
2070 err |= __get_user(*dst, src);
2071 }
bellard579a97f2007-11-11 14:26:47 +00002072 err |= __get_user(env->fsr,
2073 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2074 err |= __get_user(env->gsr,
2075 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002076 if (err)
2077 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002078 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002079 return;
2080 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002081 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002082 force_sig(SIGSEGV);
2083}
2084
2085void sparc64_get_context(CPUSPARCState *env)
2086{
bellard459a4012007-11-11 19:45:10 +00002087 abi_ulong ucp_addr;
2088 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00002089 target_mc_gregset_t *grp;
2090 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002091 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002092 int err;
2093 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002094 target_sigset_t target_set;
2095 sigset_t set;
2096
bellard459a4012007-11-11 19:45:10 +00002097 ucp_addr = env->regwptr[UREG_I0];
2098 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2099 goto do_sigsegv;
2100
blueswir15bfb56b2007-10-05 17:01:51 +00002101 mcp = &ucp->uc_mcontext;
2102 grp = &mcp->mc_gregs;
2103
2104 /* Skip over the trap instruction, first. */
2105 env->pc = env->npc;
2106 env->npc += 4;
2107
2108 err = 0;
2109
2110 sigprocmask(0, NULL, &set);
2111 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002112 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002113 err |= __put_user(target_set.sig[0],
2114 (abi_ulong *)&ucp->uc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002115 } else {
2116 abi_ulong *src, *dst;
2117 src = target_set.sig;
2118 dst = ucp->uc_sigmask.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002119 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002120 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002121 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00002122 if (err)
2123 goto do_sigsegv;
2124 }
2125
bellard459a4012007-11-11 19:45:10 +00002126 /* XXX: tstate must be saved properly */
2127 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00002128 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2129 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2130 err |= __put_user(env->y, &((*grp)[MC_Y]));
2131 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2132 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2133 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2134 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2135 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2136 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2137 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2138 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2139 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2140 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2141 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2142 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2143 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2144 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2145 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002146
bellard459a4012007-11-11 19:45:10 +00002147 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2148 fp = i7 = 0;
2149 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2150 abi_ulong) != 0)
2151 goto do_sigsegv;
2152 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2153 abi_ulong) != 0)
2154 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002155 err |= __put_user(fp, &(mcp->mc_fp));
2156 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002157
bellard459a4012007-11-11 19:45:10 +00002158 {
2159 uint32_t *src, *dst;
2160 src = env->fpr;
2161 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2162 /* XXX: check that the CPU storage is the same as user context */
2163 for (i = 0; i < 64; i++, dst++, src++)
2164 err |= __put_user(*src, dst);
2165 }
bellard579a97f2007-11-11 14:26:47 +00002166 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2167 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2168 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002169
2170 if (err)
2171 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002172 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002173 return;
2174 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002175 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002176 force_sig(SIGSEGV);
2177}
2178#endif
thsd26bc212007-11-08 18:05:37 +00002179#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002180
2181# warning signal handling not implemented
2182
pbrook624f7972008-05-31 16:11:38 +00002183static void setup_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002184 target_sigset_t *set, CPUState *env)
2185{
2186 fprintf(stderr, "setup_frame: not implemented\n");
2187}
2188
pbrook624f7972008-05-31 16:11:38 +00002189static void setup_rt_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002190 target_siginfo_t *info,
2191 target_sigset_t *set, CPUState *env)
2192{
2193 fprintf(stderr, "setup_rt_frame: not implemented\n");
2194}
2195
2196long do_sigreturn(CPUState *env)
2197{
2198 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002199 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002200}
2201
2202long do_rt_sigreturn(CPUState *env)
2203{
2204 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002205 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002206}
2207
thsd26bc212007-11-08 18:05:37 +00002208#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002209
2210# warning signal handling not implemented
2211
pbrook624f7972008-05-31 16:11:38 +00002212static void setup_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002213 target_sigset_t *set, CPUState *env)
2214{
2215 fprintf(stderr, "setup_frame: not implemented\n");
2216}
2217
pbrook624f7972008-05-31 16:11:38 +00002218static void setup_rt_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002219 target_siginfo_t *info,
2220 target_sigset_t *set, CPUState *env)
2221{
2222 fprintf(stderr, "setup_rt_frame: not implemented\n");
2223}
2224
2225long do_sigreturn(CPUState *env)
2226{
2227 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002228 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002229}
2230
2231long do_rt_sigreturn(CPUState *env)
2232{
2233 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002234 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002235}
2236
thsd26bc212007-11-08 18:05:37 +00002237#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002238
2239struct target_sigcontext {
2240 uint32_t sc_regmask; /* Unused */
2241 uint32_t sc_status;
2242 uint64_t sc_pc;
2243 uint64_t sc_regs[32];
2244 uint64_t sc_fpregs[32];
2245 uint32_t sc_ownedfp; /* Unused */
2246 uint32_t sc_fpc_csr;
2247 uint32_t sc_fpc_eir; /* Unused */
2248 uint32_t sc_used_math;
2249 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2250 uint64_t sc_mdhi;
2251 uint64_t sc_mdlo;
2252 target_ulong sc_hi1; /* Was sc_cause */
2253 target_ulong sc_lo1; /* Was sc_badvaddr */
2254 target_ulong sc_hi2; /* Was sc_sigset[4] */
2255 target_ulong sc_lo2;
2256 target_ulong sc_hi3;
2257 target_ulong sc_lo3;
2258};
2259
2260struct sigframe {
2261 uint32_t sf_ass[4]; /* argument save space for o32 */
2262 uint32_t sf_code[2]; /* signal trampoline */
2263 struct target_sigcontext sf_sc;
2264 target_sigset_t sf_mask;
2265};
2266
2267/* Install trampoline to jump back from signal handler */
2268static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2269{
2270 int err;
2271
2272 /*
2273 * Set up the return code ...
2274 *
2275 * li v0, __NR__foo_sigreturn
2276 * syscall
2277 */
2278
2279 err = __put_user(0x24020000 + syscall, tramp + 0);
2280 err |= __put_user(0x0000000c , tramp + 1);
2281 /* flush_cache_sigtramp((unsigned long) tramp); */
2282 return err;
2283}
2284
2285static inline int
2286setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2287{
2288 int err = 0;
2289
thsb5dc7732008-06-27 10:02:35 +00002290 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002291
thsb5dc7732008-06-27 10:02:35 +00002292#define save_gp_reg(i) do { \
2293 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002294 } while(0)
2295 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2296 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2297 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2298 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2299 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2300 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2301 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2302 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2303 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002304#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002305
thsb5dc7732008-06-27 10:02:35 +00002306 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2307 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002308
2309 /* Not used yet, but might be useful if we ever have DSP suppport */
2310#if 0
2311 if (cpu_has_dsp) {
2312 err |= __put_user(mfhi1(), &sc->sc_hi1);
2313 err |= __put_user(mflo1(), &sc->sc_lo1);
2314 err |= __put_user(mfhi2(), &sc->sc_hi2);
2315 err |= __put_user(mflo2(), &sc->sc_lo2);
2316 err |= __put_user(mfhi3(), &sc->sc_hi3);
2317 err |= __put_user(mflo3(), &sc->sc_lo3);
2318 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2319 }
2320 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002321#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002322 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2323 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2324 if (cpu_has_dsp) {
2325 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2326 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2327 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2328 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2329 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2330 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2331 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2332 }
ths388bb212007-05-13 13:58:00 +00002333#endif
2334#endif
bellard106ec872006-06-27 21:08:10 +00002335
ths388bb212007-05-13 13:58:00 +00002336#if 0
bellard106ec872006-06-27 21:08:10 +00002337 err |= __put_user(!!used_math(), &sc->sc_used_math);
2338
2339 if (!used_math())
2340 goto out;
2341
2342 /*
2343 * Save FPU state to signal context. Signal handler will "inherit"
2344 * current FPU state.
2345 */
2346 preempt_disable();
2347
2348 if (!is_fpu_owner()) {
2349 own_fpu();
2350 restore_fp(current);
2351 }
2352 err |= save_fp_context(sc);
2353
2354 preempt_enable();
2355 out:
2356#endif
2357 return err;
2358}
2359
2360static inline int
2361restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2362{
2363 int err = 0;
2364
2365 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2366
thsb5dc7732008-06-27 10:02:35 +00002367 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2368 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002369
thsead93602007-09-06 00:18:15 +00002370#define restore_gp_reg(i) do { \
thsb5dc7732008-06-27 10:02:35 +00002371 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002372 } while(0)
2373 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2374 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2375 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2376 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2377 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2378 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2379 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2380 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2381 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2382 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2383 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002384#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002385
2386#if 0
2387 if (cpu_has_dsp) {
2388 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2389 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2390 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2391 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2392 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2393 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2394 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2395 }
ths388bb212007-05-13 13:58:00 +00002396#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002397 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2398 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2399 if (cpu_has_dsp) {
2400 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2401 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2402 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2403 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2404 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2405 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2406 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2407 }
ths388bb212007-05-13 13:58:00 +00002408#endif
bellard106ec872006-06-27 21:08:10 +00002409
2410 err |= __get_user(used_math, &sc->sc_used_math);
2411 conditional_used_math(used_math);
2412
2413 preempt_disable();
2414
2415 if (used_math()) {
2416 /* restore fpu context if we have used it before */
2417 own_fpu();
2418 err |= restore_fp_context(sc);
2419 } else {
2420 /* signal handler may have used FPU. Give it up. */
2421 lose_fpu();
2422 }
2423
2424 preempt_enable();
2425#endif
2426 return err;
2427}
2428/*
2429 * Determine which stack to use..
2430 */
bellard579a97f2007-11-11 14:26:47 +00002431static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00002432get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
bellard106ec872006-06-27 21:08:10 +00002433{
2434 unsigned long sp;
2435
2436 /* Default to using normal stack */
thsb5dc7732008-06-27 10:02:35 +00002437 sp = regs->active_tc.gpr[29];
bellard106ec872006-06-27 21:08:10 +00002438
2439 /*
2440 * FPU emulator may have it's own trampoline active just
2441 * above the user stack, 16-bytes before the next lowest
2442 * 16 byte boundary. Try to avoid trashing it.
2443 */
2444 sp -= 32;
2445
bellard106ec872006-06-27 21:08:10 +00002446 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002447 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
thsa04e1342007-09-27 13:57:58 +00002448 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2449 }
bellard106ec872006-06-27 21:08:10 +00002450
bellard579a97f2007-11-11 14:26:47 +00002451 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002452}
2453
bellard579a97f2007-11-11 14:26:47 +00002454/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00002455static void setup_frame(int sig, struct target_sigaction * ka,
bellard579a97f2007-11-11 14:26:47 +00002456 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002457{
2458 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002459 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002460 int i;
2461
bellard579a97f2007-11-11 14:26:47 +00002462 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2463 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002464 goto give_sigsegv;
2465
2466 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2467
2468 if(setup_sigcontext(regs, &frame->sf_sc))
2469 goto give_sigsegv;
2470
2471 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2472 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2473 goto give_sigsegv;
2474 }
2475
2476 /*
2477 * Arguments to signal handler:
2478 *
2479 * a0 = signal number
2480 * a1 = 0 (should be cause)
2481 * a2 = pointer to struct sigcontext
2482 *
2483 * $25 and PC point to the signal handler, $29 points to the
2484 * struct sigframe.
2485 */
thsb5dc7732008-06-27 10:02:35 +00002486 regs->active_tc.gpr[ 4] = sig;
2487 regs->active_tc.gpr[ 5] = 0;
2488 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2489 regs->active_tc.gpr[29] = frame_addr;
2490 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002491 /* The original kernel code sets CP0_EPC to the handler
2492 * since it returns to userland using eret
2493 * we cannot do this here, and we must set PC directly */
thsb5dc7732008-06-27 10:02:35 +00002494 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002495 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002496 return;
2497
2498give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002499 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002500 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002501 return;
bellard106ec872006-06-27 21:08:10 +00002502}
2503
2504long do_sigreturn(CPUState *regs)
2505{
ths388bb212007-05-13 13:58:00 +00002506 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002507 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002508 sigset_t blocked;
2509 target_sigset_t target_set;
2510 int i;
bellard106ec872006-06-27 21:08:10 +00002511
2512#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002513 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002514#endif
thsb5dc7732008-06-27 10:02:35 +00002515 frame_addr = regs->active_tc.gpr[29];
bellard579a97f2007-11-11 14:26:47 +00002516 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002517 goto badframe;
2518
ths388bb212007-05-13 13:58:00 +00002519 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002520 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2521 goto badframe;
ths388bb212007-05-13 13:58:00 +00002522 }
bellard106ec872006-06-27 21:08:10 +00002523
ths388bb212007-05-13 13:58:00 +00002524 target_to_host_sigset_internal(&blocked, &target_set);
2525 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002526
ths388bb212007-05-13 13:58:00 +00002527 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002528 goto badframe;
2529
2530#if 0
ths388bb212007-05-13 13:58:00 +00002531 /*
2532 * Don't let your children do this ...
2533 */
2534 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002535 "move\t$29, %0\n\t"
2536 "j\tsyscall_exit"
2537 :/* no outputs */
2538 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002539 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002540#endif
ths3b46e622007-09-17 08:09:54 +00002541
thsb5dc7732008-06-27 10:02:35 +00002542 regs->active_tc.PC = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002543 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002544 * maybe a problem with nested signals ? */
2545 regs->CP0_EPC = 0;
2546 return 0;
2547
2548badframe:
ths388bb212007-05-13 13:58:00 +00002549 force_sig(TARGET_SIGSEGV/*, current*/);
2550 return 0;
bellard106ec872006-06-27 21:08:10 +00002551}
2552
pbrook624f7972008-05-31 16:11:38 +00002553static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellard106ec872006-06-27 21:08:10 +00002554 target_siginfo_t *info,
2555 target_sigset_t *set, CPUState *env)
2556{
2557 fprintf(stderr, "setup_rt_frame: not implemented\n");
2558}
2559
2560long do_rt_sigreturn(CPUState *env)
2561{
2562 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002563 return -TARGET_ENOSYS;
bellard106ec872006-06-27 21:08:10 +00002564}
bellard6d5e2162004-09-30 22:04:13 +00002565
thsc3b5bc82007-12-02 06:31:25 +00002566#elif defined(TARGET_SH4)
2567
2568/*
2569 * code and data structures from linux kernel:
2570 * include/asm-sh/sigcontext.h
2571 * arch/sh/kernel/signal.c
2572 */
2573
2574struct target_sigcontext {
2575 target_ulong oldmask;
2576
2577 /* CPU registers */
2578 target_ulong sc_gregs[16];
2579 target_ulong sc_pc;
2580 target_ulong sc_pr;
2581 target_ulong sc_sr;
2582 target_ulong sc_gbr;
2583 target_ulong sc_mach;
2584 target_ulong sc_macl;
2585
2586 /* FPU registers */
2587 target_ulong sc_fpregs[16];
2588 target_ulong sc_xfpregs[16];
2589 unsigned int sc_fpscr;
2590 unsigned int sc_fpul;
2591 unsigned int sc_ownedfp;
2592};
2593
2594struct target_sigframe
2595{
2596 struct target_sigcontext sc;
2597 target_ulong extramask[TARGET_NSIG_WORDS-1];
2598 uint16_t retcode[3];
2599};
2600
2601
2602struct target_ucontext {
2603 target_ulong uc_flags;
2604 struct target_ucontext *uc_link;
2605 target_stack_t uc_stack;
2606 struct target_sigcontext uc_mcontext;
2607 target_sigset_t uc_sigmask; /* mask last for extensibility */
2608};
2609
2610struct target_rt_sigframe
2611{
2612 struct target_siginfo info;
2613 struct target_ucontext uc;
2614 uint16_t retcode[3];
2615};
2616
2617
2618#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2619#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2620
pbrook624f7972008-05-31 16:11:38 +00002621static abi_ulong get_sigframe(struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002622 unsigned long sp, size_t frame_size)
2623{
pbrook624f7972008-05-31 16:11:38 +00002624 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
thsc3b5bc82007-12-02 06:31:25 +00002625 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2626 }
2627
2628 return (sp - frame_size) & -8ul;
2629}
2630
2631static int setup_sigcontext(struct target_sigcontext *sc,
2632 CPUState *regs, unsigned long mask)
2633{
2634 int err = 0;
2635
2636#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2637 COPY(gregs[0]); COPY(gregs[1]);
2638 COPY(gregs[2]); COPY(gregs[3]);
2639 COPY(gregs[4]); COPY(gregs[5]);
2640 COPY(gregs[6]); COPY(gregs[7]);
2641 COPY(gregs[8]); COPY(gregs[9]);
2642 COPY(gregs[10]); COPY(gregs[11]);
2643 COPY(gregs[12]); COPY(gregs[13]);
2644 COPY(gregs[14]); COPY(gregs[15]);
2645 COPY(gbr); COPY(mach);
2646 COPY(macl); COPY(pr);
2647 COPY(sr); COPY(pc);
2648#undef COPY
2649
2650 /* todo: save FPU registers here */
2651
2652 /* non-iBCS2 extensions.. */
2653 err |= __put_user(mask, &sc->oldmask);
2654
2655 return err;
2656}
2657
2658static int restore_sigcontext(struct CPUState *regs,
2659 struct target_sigcontext *sc)
2660{
2661 unsigned int err = 0;
2662
2663#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2664 COPY(gregs[1]);
2665 COPY(gregs[2]); COPY(gregs[3]);
2666 COPY(gregs[4]); COPY(gregs[5]);
2667 COPY(gregs[6]); COPY(gregs[7]);
2668 COPY(gregs[8]); COPY(gregs[9]);
2669 COPY(gregs[10]); COPY(gregs[11]);
2670 COPY(gregs[12]); COPY(gregs[13]);
2671 COPY(gregs[14]); COPY(gregs[15]);
2672 COPY(gbr); COPY(mach);
2673 COPY(macl); COPY(pr);
2674 COPY(sr); COPY(pc);
2675#undef COPY
2676
2677 /* todo: restore FPU registers here */
2678
2679 regs->tra = -1; /* disable syscall checks */
2680 return err;
2681}
2682
pbrook624f7972008-05-31 16:11:38 +00002683static void setup_frame(int sig, struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002684 target_sigset_t *set, CPUState *regs)
2685{
2686 struct target_sigframe *frame;
2687 abi_ulong frame_addr;
2688 int i;
2689 int err = 0;
2690 int signal;
2691
2692 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2693 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2694 goto give_sigsegv;
2695
2696 signal = current_exec_domain_sig(sig);
2697
2698 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2699
2700 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2701 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2702 }
2703
2704 /* Set up to return from userspace. If provided, use a stub
2705 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002706 if (ka->sa_flags & TARGET_SA_RESTORER) {
2707 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002708 } else {
2709 /* Generate return code (system call to sigreturn) */
2710 err |= __put_user(MOVW(2), &frame->retcode[0]);
2711 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2712 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2713 regs->pr = (unsigned long) frame->retcode;
2714 }
2715
2716 if (err)
2717 goto give_sigsegv;
2718
2719 /* Set up registers for signal handler */
2720 regs->gregs[15] = (unsigned long) frame;
2721 regs->gregs[4] = signal; /* Arg for signal handler */
2722 regs->gregs[5] = 0;
2723 regs->gregs[6] = (unsigned long) &frame->sc;
pbrook624f7972008-05-31 16:11:38 +00002724 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002725
2726 unlock_user_struct(frame, frame_addr, 1);
2727 return;
2728
2729give_sigsegv:
2730 unlock_user_struct(frame, frame_addr, 1);
2731 force_sig(SIGSEGV);
2732}
2733
pbrook624f7972008-05-31 16:11:38 +00002734static void setup_rt_frame(int sig, struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002735 target_siginfo_t *info,
2736 target_sigset_t *set, CPUState *regs)
2737{
2738 struct target_rt_sigframe *frame;
2739 abi_ulong frame_addr;
2740 int i;
2741 int err = 0;
2742 int signal;
2743
2744 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2745 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2746 goto give_sigsegv;
2747
2748 signal = current_exec_domain_sig(sig);
2749
2750 err |= copy_siginfo_to_user(&frame->info, info);
2751
2752 /* Create the ucontext. */
2753 err |= __put_user(0, &frame->uc.uc_flags);
2754 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
balrog526ccb72008-07-16 12:13:52 +00002755 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
thsc3b5bc82007-12-02 06:31:25 +00002756 &frame->uc.uc_stack.ss_sp);
2757 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2758 &frame->uc.uc_stack.ss_flags);
2759 err |= __put_user(target_sigaltstack_used.ss_size,
2760 &frame->uc.uc_stack.ss_size);
2761 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2762 regs, set->sig[0]);
2763 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2764 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2765 }
2766
2767 /* Set up to return from userspace. If provided, use a stub
2768 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002769 if (ka->sa_flags & TARGET_SA_RESTORER) {
2770 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002771 } else {
2772 /* Generate return code (system call to sigreturn) */
2773 err |= __put_user(MOVW(2), &frame->retcode[0]);
2774 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2775 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2776 regs->pr = (unsigned long) frame->retcode;
2777 }
2778
2779 if (err)
2780 goto give_sigsegv;
2781
2782 /* Set up registers for signal handler */
2783 regs->gregs[15] = (unsigned long) frame;
2784 regs->gregs[4] = signal; /* Arg for signal handler */
2785 regs->gregs[5] = (unsigned long) &frame->info;
2786 regs->gregs[6] = (unsigned long) &frame->uc;
pbrook624f7972008-05-31 16:11:38 +00002787 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002788
2789 unlock_user_struct(frame, frame_addr, 1);
2790 return;
2791
2792give_sigsegv:
2793 unlock_user_struct(frame, frame_addr, 1);
2794 force_sig(SIGSEGV);
2795}
2796
2797long do_sigreturn(CPUState *regs)
2798{
2799 struct target_sigframe *frame;
2800 abi_ulong frame_addr;
2801 sigset_t blocked;
2802 target_sigset_t target_set;
2803 int i;
2804 int err = 0;
2805
2806#if defined(DEBUG_SIGNAL)
2807 fprintf(stderr, "do_sigreturn\n");
2808#endif
2809 frame_addr = regs->gregs[15];
2810 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2811 goto badframe;
2812
2813 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2814 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2815 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2816 }
2817
2818 if (err)
2819 goto badframe;
2820
2821 target_to_host_sigset_internal(&blocked, &target_set);
2822 sigprocmask(SIG_SETMASK, &blocked, NULL);
2823
2824 if (restore_sigcontext(regs, &frame->sc))
2825 goto badframe;
2826
2827 unlock_user_struct(frame, frame_addr, 0);
2828 return regs->gregs[0];
2829
2830badframe:
2831 unlock_user_struct(frame, frame_addr, 0);
2832 force_sig(TARGET_SIGSEGV);
2833 return 0;
2834}
2835
2836long do_rt_sigreturn(CPUState *regs)
2837{
2838 struct target_rt_sigframe *frame;
2839 abi_ulong frame_addr;
2840 sigset_t blocked;
2841
2842#if defined(DEBUG_SIGNAL)
2843 fprintf(stderr, "do_rt_sigreturn\n");
2844#endif
2845 frame_addr = regs->gregs[15];
2846 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2847 goto badframe;
2848
2849 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
2850 sigprocmask(SIG_SETMASK, &blocked, NULL);
2851
2852 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
2853 goto badframe;
2854
2855 if (do_sigaltstack(frame_addr +
2856 offsetof(struct target_rt_sigframe, uc.uc_stack),
2857 0, get_sp_from_cpustate(regs)) == -EFAULT)
2858 goto badframe;
2859
2860 unlock_user_struct(frame, frame_addr, 0);
2861 return regs->gregs[0];
2862
2863badframe:
2864 unlock_user_struct(frame, frame_addr, 0);
2865 force_sig(TARGET_SIGSEGV);
2866 return 0;
2867}
edgar_iglb6d3abd2008-02-28 11:29:27 +00002868#elif defined(TARGET_CRIS)
2869
2870struct target_sigcontext {
2871 struct target_pt_regs regs; /* needs to be first */
2872 uint32_t oldmask;
2873 uint32_t usp; /* usp before stacking this gunk on it */
2874};
2875
2876/* Signal frames. */
2877struct target_signal_frame {
2878 struct target_sigcontext sc;
2879 uint32_t extramask[TARGET_NSIG_WORDS - 1];
2880 uint8_t retcode[8]; /* Trampoline code. */
2881};
2882
2883struct rt_signal_frame {
2884 struct siginfo *pinfo;
2885 void *puc;
2886 struct siginfo info;
2887 struct ucontext uc;
2888 uint8_t retcode[8]; /* Trampoline code. */
2889};
2890
2891static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
2892{
edgar_igl9664d922008-03-03 22:23:53 +00002893 __put_user(env->regs[0], &sc->regs.r0);
2894 __put_user(env->regs[1], &sc->regs.r1);
2895 __put_user(env->regs[2], &sc->regs.r2);
2896 __put_user(env->regs[3], &sc->regs.r3);
2897 __put_user(env->regs[4], &sc->regs.r4);
2898 __put_user(env->regs[5], &sc->regs.r5);
2899 __put_user(env->regs[6], &sc->regs.r6);
2900 __put_user(env->regs[7], &sc->regs.r7);
2901 __put_user(env->regs[8], &sc->regs.r8);
2902 __put_user(env->regs[9], &sc->regs.r9);
2903 __put_user(env->regs[10], &sc->regs.r10);
2904 __put_user(env->regs[11], &sc->regs.r11);
2905 __put_user(env->regs[12], &sc->regs.r12);
2906 __put_user(env->regs[13], &sc->regs.r13);
2907 __put_user(env->regs[14], &sc->usp);
2908 __put_user(env->regs[15], &sc->regs.acr);
2909 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
2910 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
2911 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002912}
edgar_igl9664d922008-03-03 22:23:53 +00002913
edgar_iglb6d3abd2008-02-28 11:29:27 +00002914static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
2915{
edgar_igl9664d922008-03-03 22:23:53 +00002916 __get_user(env->regs[0], &sc->regs.r0);
2917 __get_user(env->regs[1], &sc->regs.r1);
2918 __get_user(env->regs[2], &sc->regs.r2);
2919 __get_user(env->regs[3], &sc->regs.r3);
2920 __get_user(env->regs[4], &sc->regs.r4);
2921 __get_user(env->regs[5], &sc->regs.r5);
2922 __get_user(env->regs[6], &sc->regs.r6);
2923 __get_user(env->regs[7], &sc->regs.r7);
2924 __get_user(env->regs[8], &sc->regs.r8);
2925 __get_user(env->regs[9], &sc->regs.r9);
2926 __get_user(env->regs[10], &sc->regs.r10);
2927 __get_user(env->regs[11], &sc->regs.r11);
2928 __get_user(env->regs[12], &sc->regs.r12);
2929 __get_user(env->regs[13], &sc->regs.r13);
2930 __get_user(env->regs[14], &sc->usp);
2931 __get_user(env->regs[15], &sc->regs.acr);
2932 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
2933 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
2934 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002935}
2936
edgar_igl9664d922008-03-03 22:23:53 +00002937static abi_ulong get_sigframe(CPUState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00002938{
edgar_igl9664d922008-03-03 22:23:53 +00002939 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002940 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00002941 sp = (env->regs[R_SP] & ~3);
2942 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002943}
2944
pbrook624f7972008-05-31 16:11:38 +00002945static void setup_frame(int sig, struct target_sigaction *ka,
edgar_iglb6d3abd2008-02-28 11:29:27 +00002946 target_sigset_t *set, CPUState *env)
2947{
2948 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00002949 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002950 int err = 0;
2951 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002952
edgar_igl9664d922008-03-03 22:23:53 +00002953 frame_addr = get_sigframe(env, sizeof *frame);
2954 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00002955 goto badframe;
2956
2957 /*
2958 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
2959 * use this trampoline anymore but it sets it up for GDB.
2960 * In QEMU, using the trampoline simplifies things a bit so we use it.
2961 *
2962 * This is movu.w __NR_sigreturn, r9; break 13;
2963 */
2964 err |= __put_user(0x9c5f, frame->retcode+0);
2965 err |= __put_user(TARGET_NR_sigreturn,
2966 frame->retcode+2);
2967 err |= __put_user(0xe93d, frame->retcode+4);
2968
2969 /* Save the mask. */
2970 err |= __put_user(set->sig[0], &frame->sc.oldmask);
2971 if (err)
2972 goto badframe;
2973
2974 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2975 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
2976 goto badframe;
2977 }
2978
2979 setup_sigcontext(&frame->sc, env);
2980
2981 /* Move the stack and setup the arguments for the handler. */
balrog526ccb72008-07-16 12:13:52 +00002982 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002983 env->regs[10] = sig;
pbrook624f7972008-05-31 16:11:38 +00002984 env->pc = (unsigned long) ka->_sa_handler;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002985 /* Link SRP so the guest returns through the trampoline. */
balrog526ccb72008-07-16 12:13:52 +00002986 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
edgar_iglb6d3abd2008-02-28 11:29:27 +00002987
edgar_igl9664d922008-03-03 22:23:53 +00002988 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002989 return;
2990 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00002991 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002992 force_sig(TARGET_SIGSEGV);
2993}
2994
pbrook624f7972008-05-31 16:11:38 +00002995static void setup_rt_frame(int sig, struct target_sigaction *ka,
edgar_iglb6d3abd2008-02-28 11:29:27 +00002996 target_siginfo_t *info,
2997 target_sigset_t *set, CPUState *env)
2998{
2999 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3000}
3001
3002long do_sigreturn(CPUState *env)
3003{
3004 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003005 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003006 target_sigset_t target_set;
3007 sigset_t set;
3008 int i;
3009
edgar_igl9664d922008-03-03 22:23:53 +00003010 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003011 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003012 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003013 goto badframe;
3014
3015 /* Restore blocked signals */
3016 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3017 goto badframe;
3018 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3019 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3020 goto badframe;
3021 }
3022 target_to_host_sigset_internal(&set, &target_set);
3023 sigprocmask(SIG_SETMASK, &set, NULL);
3024
3025 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003026 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003027 return env->regs[10];
3028 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003029 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003030 force_sig(TARGET_SIGSEGV);
3031}
3032
3033long do_rt_sigreturn(CPUState *env)
3034{
3035 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3036 return -TARGET_ENOSYS;
3037}
thsc3b5bc82007-12-02 06:31:25 +00003038
bellardb346ff42003-06-15 20:05:50 +00003039#else
3040
pbrook624f7972008-05-31 16:11:38 +00003041static void setup_frame(int sig, struct target_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00003042 target_sigset_t *set, CPUState *env)
3043{
3044 fprintf(stderr, "setup_frame: not implemented\n");
3045}
3046
pbrook624f7972008-05-31 16:11:38 +00003047static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00003048 target_siginfo_t *info,
3049 target_sigset_t *set, CPUState *env)
3050{
3051 fprintf(stderr, "setup_rt_frame: not implemented\n");
3052}
3053
3054long do_sigreturn(CPUState *env)
3055{
3056 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003057 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003058}
3059
3060long do_rt_sigreturn(CPUState *env)
3061{
3062 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003063 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003064}
3065
bellard66fb9762003-03-23 01:06:05 +00003066#endif
3067
pbrook624f7972008-05-31 16:11:38 +00003068void process_pending_signals(CPUState *cpu_env)
bellard66fb9762003-03-23 01:06:05 +00003069{
3070 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00003071 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00003072 sigset_t set, old_set;
3073 target_sigset_t target_old_set;
pbrook624f7972008-05-31 16:11:38 +00003074 struct emulated_sigtable *k;
3075 struct target_sigaction *sa;
bellard66fb9762003-03-23 01:06:05 +00003076 struct sigqueue *q;
pbrook624f7972008-05-31 16:11:38 +00003077 TaskState *ts = cpu_env->opaque;
ths3b46e622007-09-17 08:09:54 +00003078
pbrook624f7972008-05-31 16:11:38 +00003079 if (!ts->signal_pending)
bellard31e31b82003-02-18 22:55:36 +00003080 return;
3081
pbrook624f7972008-05-31 16:11:38 +00003082 /* FIXME: This is not threadsafe. */
3083 k = ts->sigtab;
bellard66fb9762003-03-23 01:06:05 +00003084 for(sig = 1; sig <= TARGET_NSIG; sig++) {
3085 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00003086 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00003087 k++;
bellard31e31b82003-02-18 22:55:36 +00003088 }
3089 /* if no signal is pending, just return */
pbrook624f7972008-05-31 16:11:38 +00003090 ts->signal_pending = 0;
bellard31e31b82003-02-18 22:55:36 +00003091 return;
bellard66fb9762003-03-23 01:06:05 +00003092
bellard31e31b82003-02-18 22:55:36 +00003093 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00003094#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00003095 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00003096#endif
3097 /* dequeue signal */
3098 q = k->first;
3099 k->first = q->next;
3100 if (!k->first)
3101 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00003102
bellard1fddef42005-04-17 19:16:13 +00003103 sig = gdb_handlesig (cpu_env, sig);
3104 if (!sig) {
3105 fprintf (stderr, "Lost signal\n");
3106 abort();
3107 }
bellard66fb9762003-03-23 01:06:05 +00003108
pbrook624f7972008-05-31 16:11:38 +00003109 sa = &sigact_table[sig - 1];
3110 handler = sa->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +00003111 if (handler == TARGET_SIG_DFL) {
3112 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +00003113 if (sig != TARGET_SIGCHLD &&
3114 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +00003115 sig != TARGET_SIGWINCH) {
3116 force_sig(sig);
3117 }
3118 } else if (handler == TARGET_SIG_IGN) {
3119 /* ignore sig */
3120 } else if (handler == TARGET_SIG_ERR) {
3121 force_sig(sig);
3122 } else {
bellard9de5e442003-03-23 16:49:39 +00003123 /* compute the blocked signals during the handler execution */
pbrook624f7972008-05-31 16:11:38 +00003124 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +00003125 /* SA_NODEFER indicates that the current signal should not be
3126 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +00003127 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +00003128 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00003129
bellard9de5e442003-03-23 16:49:39 +00003130 /* block signals in the handler using Linux */
3131 sigprocmask(SIG_BLOCK, &set, &old_set);
3132 /* save the previous blocked signal state to restore it at the
3133 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00003134 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00003135
bellardbc8a22c2003-03-30 21:02:40 +00003136 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00003137#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00003138 {
3139 CPUX86State *env = cpu_env;
3140 if (env->eflags & VM_MASK)
3141 save_v86_state(env);
3142 }
3143#endif
bellard9de5e442003-03-23 16:49:39 +00003144 /* prepare the stack frame of the virtual CPU */
pbrook624f7972008-05-31 16:11:38 +00003145 if (sa->sa_flags & TARGET_SA_SIGINFO)
3146 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00003147 else
pbrook624f7972008-05-31 16:11:38 +00003148 setup_frame(sig, sa, &target_old_set, cpu_env);
3149 if (sa->sa_flags & TARGET_SA_RESETHAND)
3150 sa->_sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00003151 }
bellard66fb9762003-03-23 01:06:05 +00003152 if (q != &k->info)
pbrook624f7972008-05-31 16:11:38 +00003153 free_sigqueue(cpu_env, q);
bellard31e31b82003-02-18 22:55:36 +00003154}