blob: e0f6aaf7332018c183726e745af4ad0553f51462 [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) {
ths60b19692008-11-27 15:47:15 +0000364 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
365 kill(getpid(),SIGSTOP);
366 return 0;
367 } else
bellard66fb9762003-03-23 01:06:05 +0000368 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000369 if (sig != TARGET_SIGCHLD &&
370 sig != TARGET_SIGURG &&
ths60b19692008-11-27 15:47:15 +0000371 sig != TARGET_SIGWINCH &&
372 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +0000373 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000374 } else {
375 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000376 }
377 } else if (handler == TARGET_SIG_IGN) {
378 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000379 return 0;
bellard66fb9762003-03-23 01:06:05 +0000380 } else if (handler == TARGET_SIG_ERR) {
381 force_sig(sig);
382 } else {
bellard9de5e442003-03-23 16:49:39 +0000383 pq = &k->first;
384 if (sig < TARGET_SIGRTMIN) {
385 /* if non real time signal, we queue exactly one signal */
386 if (!k->pending)
387 q = &k->info;
388 else
389 return 0;
390 } else {
391 if (!k->pending) {
392 /* first signal */
393 q = &k->info;
394 } else {
pbrook624f7972008-05-31 16:11:38 +0000395 q = alloc_sigqueue(env);
bellard9de5e442003-03-23 16:49:39 +0000396 if (!q)
397 return -EAGAIN;
398 while (*pq != NULL)
399 pq = &(*pq)->next;
400 }
401 }
402 *pq = q;
403 q->info = *info;
404 q->next = NULL;
405 k->pending = 1;
406 /* signal that a new signal is pending */
pbrook624f7972008-05-31 16:11:38 +0000407 ts->signal_pending = 1;
bellard9de5e442003-03-23 16:49:39 +0000408 return 1; /* indicates that the signal was queued */
409 }
410}
411
ths5fafdf22007-09-16 21:08:06 +0000412static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000413 void *puc)
414{
415 int sig;
416 target_siginfo_t tinfo;
417
418 /* the CPU emulator uses some host signals to detect exceptions,
419 we we forward to it some signals */
bellardec6338b2007-11-08 14:25:03 +0000420 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
bellardb346ff42003-06-15 20:05:50 +0000421 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000422 return;
423 }
424
425 /* get target signal number */
426 sig = host_to_target_signal(host_signum);
427 if (sig < 1 || sig > TARGET_NSIG)
428 return;
429#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000430 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000431#endif
432 host_to_target_siginfo_noswap(&tinfo, info);
pbrookd5975362008-06-07 20:50:51 +0000433 if (queue_signal(thread_env, sig, &tinfo) == 1) {
bellard9de5e442003-03-23 16:49:39 +0000434 /* interrupt the virtual CPU as soon as possible */
pbrookd5975362008-06-07 20:50:51 +0000435 cpu_interrupt(thread_env, CPU_INTERRUPT_EXIT);
bellard66fb9762003-03-23 01:06:05 +0000436 }
bellard31e31b82003-02-18 22:55:36 +0000437}
438
ths0da46a62007-10-20 20:23:07 +0000439/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000440/* compare linux/kernel/signal.c:do_sigaltstack() */
441abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000442{
443 int ret;
444 struct target_sigaltstack oss;
445
446 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000447 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000448 {
449 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
450 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
451 __put_user(sas_ss_flags(sp), &oss.ss_flags);
452 }
453
bellard579a97f2007-11-11 14:26:47 +0000454 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000455 {
bellard579a97f2007-11-11 14:26:47 +0000456 struct target_sigaltstack *uss;
457 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000458
ths0da46a62007-10-20 20:23:07 +0000459 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000460 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000461 || __get_user(ss.ss_sp, &uss->ss_sp)
462 || __get_user(ss.ss_size, &uss->ss_size)
463 || __get_user(ss.ss_flags, &uss->ss_flags))
464 goto out;
bellard579a97f2007-11-11 14:26:47 +0000465 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000466
ths0da46a62007-10-20 20:23:07 +0000467 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000468 if (on_sig_stack(sp))
469 goto out;
470
ths0da46a62007-10-20 20:23:07 +0000471 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000472 if (ss.ss_flags != TARGET_SS_DISABLE
473 && ss.ss_flags != TARGET_SS_ONSTACK
474 && ss.ss_flags != 0)
475 goto out;
476
477 if (ss.ss_flags == TARGET_SS_DISABLE) {
478 ss.ss_size = 0;
479 ss.ss_sp = 0;
480 } else {
ths0da46a62007-10-20 20:23:07 +0000481 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000482 if (ss.ss_size < MINSIGSTKSZ)
483 goto out;
484 }
485
486 target_sigaltstack_used.ss_sp = ss.ss_sp;
487 target_sigaltstack_used.ss_size = ss.ss_size;
488 }
489
bellard579a97f2007-11-11 14:26:47 +0000490 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000491 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000492 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000493 goto out;
thsa04e1342007-09-27 13:57:58 +0000494 }
495
496 ret = 0;
497out:
498 return ret;
499}
500
ths0da46a62007-10-20 20:23:07 +0000501/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000502int do_sigaction(int sig, const struct target_sigaction *act,
503 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000504{
pbrook624f7972008-05-31 16:11:38 +0000505 struct target_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000506 struct sigaction act1;
507 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000508 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000509
ths2a913eb2008-11-27 15:46:25 +0000510 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000511 return -EINVAL;
512 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000513#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000514 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
bellard66fb9762003-03-23 01:06:05 +0000515 sig, (int)act, (int)oact);
516#endif
517 if (oact) {
pbrook624f7972008-05-31 16:11:38 +0000518 oact->_sa_handler = tswapl(k->_sa_handler);
519 oact->sa_flags = tswapl(k->sa_flags);
ths388bb212007-05-13 13:58:00 +0000520#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000521 oact->sa_restorer = tswapl(k->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000522#endif
pbrook624f7972008-05-31 16:11:38 +0000523 oact->sa_mask = k->sa_mask;
bellard66fb9762003-03-23 01:06:05 +0000524 }
525 if (act) {
pbrook624f7972008-05-31 16:11:38 +0000526 /* FIXME: This is not threadsafe. */
527 k->_sa_handler = tswapl(act->_sa_handler);
528 k->sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000529#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000530 k->sa_restorer = tswapl(act->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000531#endif
pbrook624f7972008-05-31 16:11:38 +0000532 k->sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000533
534 /* we update the host linux signal state */
535 host_sig = target_to_host_signal(sig);
536 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
537 sigfillset(&act1.sa_mask);
538 act1.sa_flags = SA_SIGINFO;
pbrook624f7972008-05-31 16:11:38 +0000539 if (k->sa_flags & TARGET_SA_RESTART)
bellard773b93e2004-01-04 17:15:59 +0000540 act1.sa_flags |= SA_RESTART;
541 /* NOTE: it is important to update the host kernel signal
542 ignore state to avoid getting unexpected interrupted
543 syscalls */
pbrook624f7972008-05-31 16:11:38 +0000544 if (k->_sa_handler == TARGET_SIG_IGN) {
bellard773b93e2004-01-04 17:15:59 +0000545 act1.sa_sigaction = (void *)SIG_IGN;
pbrook624f7972008-05-31 16:11:38 +0000546 } else if (k->_sa_handler == TARGET_SIG_DFL) {
bellard773b93e2004-01-04 17:15:59 +0000547 act1.sa_sigaction = (void *)SIG_DFL;
548 } else {
549 act1.sa_sigaction = host_signal_handler;
550 }
ths0da46a62007-10-20 20:23:07 +0000551 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000552 }
bellard66fb9762003-03-23 01:06:05 +0000553 }
ths0da46a62007-10-20 20:23:07 +0000554 return ret;
bellard66fb9762003-03-23 01:06:05 +0000555}
bellard31e31b82003-02-18 22:55:36 +0000556
ths5fafdf22007-09-16 21:08:06 +0000557static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
bellard43fff232003-07-09 19:31:39 +0000558 const target_siginfo_t *info)
559{
560 tswap_siginfo(tinfo, info);
561 return 0;
562}
563
thsc3b5bc82007-12-02 06:31:25 +0000564static inline int current_exec_domain_sig(int sig)
565{
566 return /* current->exec_domain && current->exec_domain->signal_invmap
567 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
568}
569
bellard459a4012007-11-11 19:45:10 +0000570#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000571
572/* from the Linux kernel */
573
574struct target_fpreg {
575 uint16_t significand[4];
576 uint16_t exponent;
577};
578
579struct target_fpxreg {
580 uint16_t significand[4];
581 uint16_t exponent;
582 uint16_t padding[3];
583};
584
585struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000586 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000587};
588
589struct target_fpstate {
590 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000591 abi_ulong cw;
592 abi_ulong sw;
593 abi_ulong tag;
594 abi_ulong ipoff;
595 abi_ulong cssel;
596 abi_ulong dataoff;
597 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000598 struct target_fpreg _st[8];
599 uint16_t status;
600 uint16_t magic; /* 0xffff = regular FPU data only */
601
602 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000603 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
604 abi_ulong mxcsr;
605 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000606 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
607 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000608 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000609};
610
611#define X86_FXSR_MAGIC 0x0000
612
613struct target_sigcontext {
614 uint16_t gs, __gsh;
615 uint16_t fs, __fsh;
616 uint16_t es, __esh;
617 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000618 abi_ulong edi;
619 abi_ulong esi;
620 abi_ulong ebp;
621 abi_ulong esp;
622 abi_ulong ebx;
623 abi_ulong edx;
624 abi_ulong ecx;
625 abi_ulong eax;
626 abi_ulong trapno;
627 abi_ulong err;
628 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000629 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000630 abi_ulong eflags;
631 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000632 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000633 abi_ulong fpstate; /* pointer */
634 abi_ulong oldmask;
635 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000636};
637
bellard66fb9762003-03-23 01:06:05 +0000638struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000639 abi_ulong tuc_flags;
640 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +0000641 target_stack_t tuc_stack;
642 struct target_sigcontext tuc_mcontext;
643 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000644};
645
646struct sigframe
647{
blueswir1992f48a2007-10-14 16:27:31 +0000648 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000649 int sig;
650 struct target_sigcontext sc;
651 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000652 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000653 char retcode[8];
654};
655
656struct rt_sigframe
657{
blueswir1992f48a2007-10-14 16:27:31 +0000658 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000659 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000660 abi_ulong pinfo;
661 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000662 struct target_siginfo info;
663 struct target_ucontext uc;
664 struct target_fpstate fpstate;
665 char retcode[8];
666};
667
668/*
669 * Set up a signal frame.
670 */
671
bellard66fb9762003-03-23 01:06:05 +0000672/* XXX: save x87 state */
673static int
674setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
bellard28be6232007-11-11 22:23:38 +0000675 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000676{
677 int err = 0;
bellard775b58d2007-11-11 16:22:17 +0000678 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000679
bellard579a97f2007-11-11 14:26:47 +0000680 /* already locked in setup_frame() */
bellarda52c7572003-06-21 13:14:12 +0000681 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
682 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
683 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
684 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000685 err |= __put_user(env->regs[R_EDI], &sc->edi);
686 err |= __put_user(env->regs[R_ESI], &sc->esi);
687 err |= __put_user(env->regs[R_EBP], &sc->ebp);
688 err |= __put_user(env->regs[R_ESP], &sc->esp);
689 err |= __put_user(env->regs[R_EBX], &sc->ebx);
690 err |= __put_user(env->regs[R_EDX], &sc->edx);
691 err |= __put_user(env->regs[R_ECX], &sc->ecx);
692 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000693 err |= __put_user(env->exception_index, &sc->trapno);
694 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000695 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000696 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000697 err |= __put_user(env->eflags, &sc->eflags);
698 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000699 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000700
bellard28be6232007-11-11 22:23:38 +0000701 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000702 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000703 magic = 0xffff;
704 err |= __put_user(magic, &fpstate->magic);
bellard28be6232007-11-11 22:23:38 +0000705 err |= __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000706
bellard66fb9762003-03-23 01:06:05 +0000707 /* non-iBCS2 extensions.. */
708 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000709 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000710 return err;
711}
712
713/*
714 * Determine which stack to use..
715 */
716
bellard579a97f2007-11-11 14:26:47 +0000717static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +0000718get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
bellard66fb9762003-03-23 01:06:05 +0000719{
720 unsigned long esp;
721
722 /* Default to using normal stack */
723 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000724 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +0000725 if (ka->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +0000726 if (sas_ss_flags(esp) == 0)
727 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
728 }
bellard66fb9762003-03-23 01:06:05 +0000729
730 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000731 else
bellarda52c7572003-06-21 13:14:12 +0000732 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
pbrook624f7972008-05-31 16:11:38 +0000733 !(ka->sa_flags & TARGET_SA_RESTORER) &&
734 ka->sa_restorer) {
735 esp = (unsigned long) ka->sa_restorer;
bellarda52c7572003-06-21 13:14:12 +0000736 }
bellard579a97f2007-11-11 14:26:47 +0000737 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000738}
739
bellard579a97f2007-11-11 14:26:47 +0000740/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +0000741static void setup_frame(int sig, struct target_sigaction *ka,
bellard66fb9762003-03-23 01:06:05 +0000742 target_sigset_t *set, CPUX86State *env)
743{
bellard579a97f2007-11-11 14:26:47 +0000744 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000745 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000746 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000747
bellard579a97f2007-11-11 14:26:47 +0000748 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000749
bellard579a97f2007-11-11 14:26:47 +0000750 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000751 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000752
thsc3b5bc82007-12-02 06:31:25 +0000753 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000754 &frame->sig);
755 if (err)
756 goto give_sigsegv;
757
bellard28be6232007-11-11 22:23:38 +0000758 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
759 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000760 if (err)
761 goto give_sigsegv;
762
bellard92319442004-06-19 16:58:13 +0000763 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
764 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
765 goto give_sigsegv;
766 }
bellard66fb9762003-03-23 01:06:05 +0000767
768 /* Set up to return from userspace. If provided, use a stub
769 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000770 if (ka->sa_flags & TARGET_SA_RESTORER) {
771 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000772 } else {
bellard775b58d2007-11-11 16:22:17 +0000773 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000774 abi_ulong retcode_addr;
775 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
776 err |= __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000777 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000778 val16 = 0xb858;
779 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000780 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000781 val16 = 0x80cd;
782 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000783 }
784
785 if (err)
786 goto give_sigsegv;
787
788 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000789 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000790 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000791
792 cpu_x86_load_seg(env, R_DS, __USER_DS);
793 cpu_x86_load_seg(env, R_ES, __USER_DS);
794 cpu_x86_load_seg(env, R_SS, __USER_DS);
795 cpu_x86_load_seg(env, R_CS, __USER_CS);
796 env->eflags &= ~TF_MASK;
797
bellard579a97f2007-11-11 14:26:47 +0000798 unlock_user_struct(frame, frame_addr, 1);
799
bellard66fb9762003-03-23 01:06:05 +0000800 return;
801
802give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000803 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000804 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000805 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000806 force_sig(TARGET_SIGSEGV /* , current */);
807}
808
bellard579a97f2007-11-11 14:26:47 +0000809/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +0000810static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellard9de5e442003-03-23 16:49:39 +0000811 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000812 target_sigset_t *set, CPUX86State *env)
813{
bellard28be6232007-11-11 22:23:38 +0000814 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000815 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000816 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000817
bellard579a97f2007-11-11 14:26:47 +0000818 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000819
bellard579a97f2007-11-11 14:26:47 +0000820 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000821 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000822
thsc3b5bc82007-12-02 06:31:25 +0000823 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000824 &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000825 addr = frame_addr + offsetof(struct rt_sigframe, info);
826 err |= __put_user(addr, &frame->pinfo);
827 addr = frame_addr + offsetof(struct rt_sigframe, uc);
828 err |= __put_user(addr, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000829 err |= copy_siginfo_to_user(&frame->info, info);
830 if (err)
831 goto give_sigsegv;
832
833 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000834 err |= __put_user(0, &frame->uc.tuc_flags);
835 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000836 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000837 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000838 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000839 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000840 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000841 &frame->uc.tuc_stack.ss_size);
842 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000843 env, set->sig[0],
844 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +0000845 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000846 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000847 goto give_sigsegv;
848 }
bellard66fb9762003-03-23 01:06:05 +0000849
850 /* Set up to return from userspace. If provided, use a stub
851 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000852 if (ka->sa_flags & TARGET_SA_RESTORER) {
853 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000854 } else {
bellard775b58d2007-11-11 16:22:17 +0000855 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000856 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
857 err |= __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000858 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000859 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000860 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000861 val16 = 0x80cd;
862 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000863 }
864
865 if (err)
866 goto give_sigsegv;
867
868 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000869 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000870 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000871
872 cpu_x86_load_seg(env, R_DS, __USER_DS);
873 cpu_x86_load_seg(env, R_ES, __USER_DS);
874 cpu_x86_load_seg(env, R_SS, __USER_DS);
875 cpu_x86_load_seg(env, R_CS, __USER_CS);
876 env->eflags &= ~TF_MASK;
877
bellard579a97f2007-11-11 14:26:47 +0000878 unlock_user_struct(frame, frame_addr, 1);
879
bellard66fb9762003-03-23 01:06:05 +0000880 return;
881
882give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000883 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000884 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000885 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000886 force_sig(TARGET_SIGSEGV /* , current */);
887}
888
889static int
890restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
891{
892 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +0000893 abi_ulong fpstate_addr;
894 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +0000895
bellard28be6232007-11-11 22:23:38 +0000896 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
897 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
898 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
899 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +0000900
bellard28be6232007-11-11 22:23:38 +0000901 env->regs[R_EDI] = tswapl(sc->edi);
902 env->regs[R_ESI] = tswapl(sc->esi);
903 env->regs[R_EBP] = tswapl(sc->ebp);
904 env->regs[R_ESP] = tswapl(sc->esp);
905 env->regs[R_EBX] = tswapl(sc->ebx);
906 env->regs[R_EDX] = tswapl(sc->edx);
907 env->regs[R_ECX] = tswapl(sc->ecx);
908 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +0000909
910 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
911 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000912
bellard28be6232007-11-11 22:23:38 +0000913 tmpflags = tswapl(sc->eflags);
914 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
915 // regs->orig_eax = -1; /* disable syscall checks */
916
917 fpstate_addr = tswapl(sc->fpstate);
918 if (fpstate_addr != 0) {
919 if (!access_ok(VERIFY_READ, fpstate_addr,
920 sizeof(struct target_fpstate)))
921 goto badframe;
922 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000923 }
924
bellard28be6232007-11-11 22:23:38 +0000925 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +0000926 return err;
bellard66fb9762003-03-23 01:06:05 +0000927badframe:
928 return 1;
bellard66fb9762003-03-23 01:06:05 +0000929}
930
931long do_sigreturn(CPUX86State *env)
932{
bellard579a97f2007-11-11 14:26:47 +0000933 struct sigframe *frame;
934 abi_ulong frame_addr = env->regs[R_ESP] - 8;
bellard66fb9762003-03-23 01:06:05 +0000935 target_sigset_t target_set;
936 sigset_t set;
937 int eax, i;
938
bellard447db212003-05-10 15:10:36 +0000939#if defined(DEBUG_SIGNAL)
940 fprintf(stderr, "do_sigreturn\n");
941#endif
bellard579a97f2007-11-11 14:26:47 +0000942 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
943 goto badframe;
bellard66fb9762003-03-23 01:06:05 +0000944 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +0000945 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
946 goto badframe;
947 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
948 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
949 goto badframe;
950 }
bellard66fb9762003-03-23 01:06:05 +0000951
bellard92319442004-06-19 16:58:13 +0000952 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +0000953 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +0000954
bellard66fb9762003-03-23 01:06:05 +0000955 /* restore registers */
956 if (restore_sigcontext(env, &frame->sc, &eax))
957 goto badframe;
bellard579a97f2007-11-11 14:26:47 +0000958 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000959 return eax;
960
961badframe:
bellard579a97f2007-11-11 14:26:47 +0000962 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000963 force_sig(TARGET_SIGSEGV);
964 return 0;
965}
966
967long do_rt_sigreturn(CPUX86State *env)
968{
bellard28be6232007-11-11 22:23:38 +0000969 abi_ulong frame_addr;
970 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +0000971 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +0000972 int eax;
973
bellard28be6232007-11-11 22:23:38 +0000974 frame_addr = env->regs[R_ESP] - 4;
975 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
976 goto badframe;
bellardb8076a72005-04-07 22:20:31 +0000977 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +0000978 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +0000979
bellardb8076a72005-04-07 22:20:31 +0000980 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +0000981 goto badframe;
982
bellard28be6232007-11-11 22:23:38 +0000983 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
984 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +0000985 goto badframe;
thsa04e1342007-09-27 13:57:58 +0000986
bellard28be6232007-11-11 22:23:38 +0000987 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +0000988 return eax;
989
990badframe:
bellard28be6232007-11-11 22:23:38 +0000991 unlock_user_struct(frame, frame_addr, 0);
992 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +0000993 return 0;
994}
995
bellard43fff232003-07-09 19:31:39 +0000996#elif defined(TARGET_ARM)
997
998struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +0000999 abi_ulong trap_no;
1000 abi_ulong error_code;
1001 abi_ulong oldmask;
1002 abi_ulong arm_r0;
1003 abi_ulong arm_r1;
1004 abi_ulong arm_r2;
1005 abi_ulong arm_r3;
1006 abi_ulong arm_r4;
1007 abi_ulong arm_r5;
1008 abi_ulong arm_r6;
1009 abi_ulong arm_r7;
1010 abi_ulong arm_r8;
1011 abi_ulong arm_r9;
1012 abi_ulong arm_r10;
1013 abi_ulong arm_fp;
1014 abi_ulong arm_ip;
1015 abi_ulong arm_sp;
1016 abi_ulong arm_lr;
1017 abi_ulong arm_pc;
1018 abi_ulong arm_cpsr;
1019 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001020};
1021
pbrooka745ec62008-05-06 15:36:17 +00001022struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001023 abi_ulong tuc_flags;
1024 abi_ulong tuc_link;
bellardb8076a72005-04-07 22:20:31 +00001025 target_stack_t tuc_stack;
1026 struct target_sigcontext tuc_mcontext;
1027 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001028};
1029
pbrooka745ec62008-05-06 15:36:17 +00001030struct target_ucontext_v2 {
1031 abi_ulong tuc_flags;
1032 abi_ulong tuc_link;
1033 target_stack_t tuc_stack;
1034 struct target_sigcontext tuc_mcontext;
1035 target_sigset_t tuc_sigmask; /* mask last for extensibility */
1036 char __unused[128 - sizeof(sigset_t)];
1037 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1038};
1039
pbrooka8c33202008-05-07 23:22:46 +00001040struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001041{
1042 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001043 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1044 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001045};
1046
pbrooka8c33202008-05-07 23:22:46 +00001047struct sigframe_v2
1048{
1049 struct target_ucontext_v2 uc;
1050 abi_ulong retcode;
1051};
1052
pbrooka745ec62008-05-06 15:36:17 +00001053struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001054{
bellardf8b0aa22007-11-11 23:03:42 +00001055 abi_ulong pinfo;
1056 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001057 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001058 struct target_ucontext_v1 uc;
1059 abi_ulong retcode;
1060};
1061
1062struct rt_sigframe_v2
1063{
1064 struct target_siginfo info;
1065 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001066 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001067};
1068
1069#define TARGET_CONFIG_CPU_32 1
1070
1071/*
1072 * For ARM syscalls, we encode the syscall number into the instruction.
1073 */
1074#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1075#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1076
1077/*
1078 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1079 * need two 16-bit instructions.
1080 */
1081#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1082#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1083
blueswir1992f48a2007-10-14 16:27:31 +00001084static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001085 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1086 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1087};
1088
1089
bellard43fff232003-07-09 19:31:39 +00001090#define __get_user_error(x,p,e) __get_user(x, p)
1091
1092static inline int valid_user_regs(CPUState *regs)
1093{
1094 return 1;
1095}
1096
pbrooka8c33202008-05-07 23:22:46 +00001097static void
bellard43fff232003-07-09 19:31:39 +00001098setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
bellardf8b0aa22007-11-11 23:03:42 +00001099 CPUState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001100{
pbrooka8c33202008-05-07 23:22:46 +00001101 __put_user(env->regs[0], &sc->arm_r0);
1102 __put_user(env->regs[1], &sc->arm_r1);
1103 __put_user(env->regs[2], &sc->arm_r2);
1104 __put_user(env->regs[3], &sc->arm_r3);
1105 __put_user(env->regs[4], &sc->arm_r4);
1106 __put_user(env->regs[5], &sc->arm_r5);
1107 __put_user(env->regs[6], &sc->arm_r6);
1108 __put_user(env->regs[7], &sc->arm_r7);
1109 __put_user(env->regs[8], &sc->arm_r8);
1110 __put_user(env->regs[9], &sc->arm_r9);
1111 __put_user(env->regs[10], &sc->arm_r10);
1112 __put_user(env->regs[11], &sc->arm_fp);
1113 __put_user(env->regs[12], &sc->arm_ip);
1114 __put_user(env->regs[13], &sc->arm_sp);
1115 __put_user(env->regs[14], &sc->arm_lr);
1116 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001117#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001118 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001119#endif
1120
pbrooka8c33202008-05-07 23:22:46 +00001121 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1122 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1123 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1124 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001125}
1126
bellard579a97f2007-11-11 14:26:47 +00001127static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00001128get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
bellard43fff232003-07-09 19:31:39 +00001129{
1130 unsigned long sp = regs->regs[13];
1131
bellard43fff232003-07-09 19:31:39 +00001132 /*
1133 * This is the X/Open sanctioned signal stack switching.
1134 */
pbrook624f7972008-05-31 16:11:38 +00001135 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
thsa04e1342007-09-27 13:57:58 +00001136 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001137 /*
1138 * ATPCS B01 mandates 8-byte alignment
1139 */
bellard579a97f2007-11-11 14:26:47 +00001140 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001141}
1142
1143static int
pbrook624f7972008-05-31 16:11:38 +00001144setup_return(CPUState *env, struct target_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001145 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001146{
pbrook624f7972008-05-31 16:11:38 +00001147 abi_ulong handler = ka->_sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001148 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001149 int thumb = handler & 1;
bellard43fff232003-07-09 19:31:39 +00001150
pbrook624f7972008-05-31 16:11:38 +00001151 if (ka->sa_flags & TARGET_SA_RESTORER) {
1152 retcode = ka->sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001153 } else {
1154 unsigned int idx = thumb;
1155
pbrook624f7972008-05-31 16:11:38 +00001156 if (ka->sa_flags & TARGET_SA_SIGINFO)
bellard43fff232003-07-09 19:31:39 +00001157 idx += 2;
1158
1159 if (__put_user(retcodes[idx], rc))
1160 return 1;
1161#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001162 flush_icache_range((abi_ulong)rc,
1163 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001164#endif
bellardf8b0aa22007-11-11 23:03:42 +00001165 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001166 }
1167
1168 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001169 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001170 env->regs[14] = retcode;
1171 env->regs[15] = handler & (thumb ? ~1 : ~3);
pbrook75b680e2008-03-21 16:07:30 +00001172 env->thumb = thumb;
bellard43fff232003-07-09 19:31:39 +00001173
bellardb5ff1b32005-11-26 10:38:39 +00001174#if 0
bellard43fff232003-07-09 19:31:39 +00001175#ifdef TARGET_CONFIG_CPU_32
1176 env->cpsr = cpsr;
1177#endif
bellardb5ff1b32005-11-26 10:38:39 +00001178#endif
bellard43fff232003-07-09 19:31:39 +00001179
1180 return 0;
1181}
1182
pbrooka8c33202008-05-07 23:22:46 +00001183static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1184 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001185{
pbrooka8c33202008-05-07 23:22:46 +00001186 struct target_sigaltstack stack;
1187 int i;
1188
1189 /* Clear all the bits of the ucontext we don't use. */
1190 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1191
1192 memset(&stack, 0, sizeof(stack));
1193 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1194 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1195 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1196 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1197
1198 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1199 /* FIXME: Save coprocessor signal frame. */
1200 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1201 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1202 }
1203}
1204
1205/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00001206static void setup_frame_v1(int usig, struct target_sigaction *ka,
pbrooka8c33202008-05-07 23:22:46 +00001207 target_sigset_t *set, CPUState *regs)
1208{
1209 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001210 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001211 int i;
bellard43fff232003-07-09 19:31:39 +00001212
bellard579a97f2007-11-11 14:26:47 +00001213 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1214 return;
1215
pbrooka8c33202008-05-07 23:22:46 +00001216 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001217
bellard92319442004-06-19 16:58:13 +00001218 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1219 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001220 goto end;
bellard43fff232003-07-09 19:31:39 +00001221 }
1222
pbrooka8c33202008-05-07 23:22:46 +00001223 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1224 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001225
1226end:
1227 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001228}
1229
pbrook624f7972008-05-31 16:11:38 +00001230static void setup_frame_v2(int usig, struct target_sigaction *ka,
pbrooka8c33202008-05-07 23:22:46 +00001231 target_sigset_t *set, CPUState *regs)
1232{
1233 struct sigframe_v2 *frame;
1234 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1235
1236 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1237 return;
1238
1239 setup_sigframe_v2(&frame->uc, set, regs);
1240
1241 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1242 frame_addr + offsetof(struct sigframe_v2, retcode));
1243
1244 unlock_user_struct(frame, frame_addr, 1);
1245}
1246
pbrook624f7972008-05-31 16:11:38 +00001247static void setup_frame(int usig, struct target_sigaction *ka,
pbrooka8c33202008-05-07 23:22:46 +00001248 target_sigset_t *set, CPUState *regs)
1249{
1250 if (get_osversion() >= 0x020612) {
1251 setup_frame_v2(usig, ka, set, regs);
1252 } else {
1253 setup_frame_v1(usig, ka, set, regs);
1254 }
bellard43fff232003-07-09 19:31:39 +00001255}
1256
bellard579a97f2007-11-11 14:26:47 +00001257/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +00001258static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
pbrooka745ec62008-05-06 15:36:17 +00001259 target_siginfo_t *info,
1260 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001261{
pbrooka745ec62008-05-06 15:36:17 +00001262 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001263 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001264 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001265 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001266 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001267
bellard579a97f2007-11-11 14:26:47 +00001268 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001269 return /* 1 */;
1270
pbrooka745ec62008-05-06 15:36:17 +00001271 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001272 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001273 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001274 __put_user(uc_addr, &frame->puc);
1275 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001276
1277 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001278 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001279
thsa04e1342007-09-27 13:57:58 +00001280 memset(&stack, 0, sizeof(stack));
1281 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1282 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1283 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001284 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001285
pbrooka8c33202008-05-07 23:22:46 +00001286 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001287 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001288 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001289 goto end;
bellard92319442004-06-19 16:58:13 +00001290 }
bellard43fff232003-07-09 19:31:39 +00001291
pbrooka8c33202008-05-07 23:22:46 +00001292 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1293 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001294
pbrooka8c33202008-05-07 23:22:46 +00001295 env->regs[1] = info_addr;
1296 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001297
1298end:
1299 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001300}
1301
pbrook624f7972008-05-31 16:11:38 +00001302static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
pbrooka745ec62008-05-06 15:36:17 +00001303 target_siginfo_t *info,
1304 target_sigset_t *set, CPUState *env)
1305{
1306 struct rt_sigframe_v2 *frame;
1307 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001308 abi_ulong info_addr, uc_addr;
1309
1310 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1311 return /* 1 */;
1312
1313 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1314 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001315 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001316
pbrooka8c33202008-05-07 23:22:46 +00001317 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001318
pbrooka8c33202008-05-07 23:22:46 +00001319 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1320 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001321
pbrooka8c33202008-05-07 23:22:46 +00001322 env->regs[1] = info_addr;
1323 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001324
bellard579a97f2007-11-11 14:26:47 +00001325 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001326}
1327
pbrook624f7972008-05-31 16:11:38 +00001328static void setup_rt_frame(int usig, struct target_sigaction *ka,
pbrooka745ec62008-05-06 15:36:17 +00001329 target_siginfo_t *info,
1330 target_sigset_t *set, CPUState *env)
1331{
1332 if (get_osversion() >= 0x020612) {
1333 setup_rt_frame_v2(usig, ka, info, set, env);
1334 } else {
1335 setup_rt_frame_v1(usig, ka, info, set, env);
1336 }
1337}
1338
bellard43fff232003-07-09 19:31:39 +00001339static int
1340restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1341{
1342 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001343 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001344
1345 __get_user_error(env->regs[0], &sc->arm_r0, err);
1346 __get_user_error(env->regs[1], &sc->arm_r1, err);
1347 __get_user_error(env->regs[2], &sc->arm_r2, err);
1348 __get_user_error(env->regs[3], &sc->arm_r3, err);
1349 __get_user_error(env->regs[4], &sc->arm_r4, err);
1350 __get_user_error(env->regs[5], &sc->arm_r5, err);
1351 __get_user_error(env->regs[6], &sc->arm_r6, err);
1352 __get_user_error(env->regs[7], &sc->arm_r7, err);
1353 __get_user_error(env->regs[8], &sc->arm_r8, err);
1354 __get_user_error(env->regs[9], &sc->arm_r9, err);
1355 __get_user_error(env->regs[10], &sc->arm_r10, err);
1356 __get_user_error(env->regs[11], &sc->arm_fp, err);
1357 __get_user_error(env->regs[12], &sc->arm_ip, err);
1358 __get_user_error(env->regs[13], &sc->arm_sp, err);
1359 __get_user_error(env->regs[14], &sc->arm_lr, err);
1360 __get_user_error(env->regs[15], &sc->arm_pc, err);
1361#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001362 __get_user_error(cpsr, &sc->arm_cpsr, err);
pbrook75b680e2008-03-21 16:07:30 +00001363 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001364#endif
1365
1366 err |= !valid_user_regs(env);
1367
1368 return err;
1369}
1370
pbrooka8c33202008-05-07 23:22:46 +00001371long do_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001372{
bellardf8b0aa22007-11-11 23:03:42 +00001373 abi_ulong frame_addr;
pbrooka8c33202008-05-07 23:22:46 +00001374 struct sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001375 target_sigset_t set;
1376 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001377 int i;
bellard43fff232003-07-09 19:31:39 +00001378
1379 /*
1380 * Since we stacked the signal on a 64-bit boundary,
1381 * then 'sp' should be word aligned here. If it's
1382 * not, then the user is trying to mess with us.
1383 */
1384 if (env->regs[13] & 7)
1385 goto badframe;
1386
bellardf8b0aa22007-11-11 23:03:42 +00001387 frame_addr = env->regs[13];
1388 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1389 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001390
bellard92319442004-06-19 16:58:13 +00001391 if (__get_user(set.sig[0], &frame->sc.oldmask))
1392 goto badframe;
1393 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1394 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1395 goto badframe;
1396 }
bellard43fff232003-07-09 19:31:39 +00001397
bellard92319442004-06-19 16:58:13 +00001398 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001399 sigprocmask(SIG_SETMASK, &host_set, NULL);
1400
1401 if (restore_sigcontext(env, &frame->sc))
1402 goto badframe;
1403
1404#if 0
1405 /* Send SIGTRAP if we're single-stepping */
1406 if (ptrace_cancel_bpt(current))
1407 send_sig(SIGTRAP, current, 1);
1408#endif
bellardf8b0aa22007-11-11 23:03:42 +00001409 unlock_user_struct(frame, frame_addr, 0);
1410 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001411
1412badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001413 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001414 force_sig(SIGSEGV /* , current */);
1415 return 0;
1416}
1417
pbrooka8c33202008-05-07 23:22:46 +00001418static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1419 struct target_ucontext_v2 *uc)
1420{
1421 sigset_t host_set;
1422
1423 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1424 sigprocmask(SIG_SETMASK, &host_set, NULL);
1425
1426 if (restore_sigcontext(env, &uc->tuc_mcontext))
1427 return 1;
1428
1429 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1430 return 1;
1431
1432#if 0
1433 /* Send SIGTRAP if we're single-stepping */
1434 if (ptrace_cancel_bpt(current))
1435 send_sig(SIGTRAP, current, 1);
1436#endif
1437
1438 return 0;
1439}
1440
1441long do_sigreturn_v2(CPUState *env)
1442{
1443 abi_ulong frame_addr;
1444 struct sigframe_v2 *frame;
1445
1446 /*
1447 * Since we stacked the signal on a 64-bit boundary,
1448 * then 'sp' should be word aligned here. If it's
1449 * not, then the user is trying to mess with us.
1450 */
1451 if (env->regs[13] & 7)
1452 goto badframe;
1453
1454 frame_addr = env->regs[13];
1455 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1456 goto badframe;
1457
1458 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1459 goto badframe;
1460
1461 unlock_user_struct(frame, frame_addr, 0);
1462 return env->regs[0];
1463
1464badframe:
1465 unlock_user_struct(frame, frame_addr, 0);
1466 force_sig(SIGSEGV /* , current */);
1467 return 0;
1468}
1469
1470long do_sigreturn(CPUState *env)
1471{
1472 if (get_osversion() >= 0x020612) {
1473 return do_sigreturn_v2(env);
1474 } else {
1475 return do_sigreturn_v1(env);
1476 }
1477}
1478
pbrooka745ec62008-05-06 15:36:17 +00001479long do_rt_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001480{
bellardf8b0aa22007-11-11 23:03:42 +00001481 abi_ulong frame_addr;
pbrooka745ec62008-05-06 15:36:17 +00001482 struct rt_sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001483 sigset_t host_set;
1484
1485 /*
1486 * Since we stacked the signal on a 64-bit boundary,
1487 * then 'sp' should be word aligned here. If it's
1488 * not, then the user is trying to mess with us.
1489 */
1490 if (env->regs[13] & 7)
1491 goto badframe;
1492
bellardf8b0aa22007-11-11 23:03:42 +00001493 frame_addr = env->regs[13];
1494 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1495 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001496
bellardb8076a72005-04-07 22:20:31 +00001497 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001498 sigprocmask(SIG_SETMASK, &host_set, NULL);
1499
bellardb8076a72005-04-07 22:20:31 +00001500 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001501 goto badframe;
1502
pbrooka745ec62008-05-06 15:36:17 +00001503 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 +00001504 goto badframe;
1505
bellard43fff232003-07-09 19:31:39 +00001506#if 0
1507 /* Send SIGTRAP if we're single-stepping */
1508 if (ptrace_cancel_bpt(current))
1509 send_sig(SIGTRAP, current, 1);
1510#endif
bellardf8b0aa22007-11-11 23:03:42 +00001511 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001512 return env->regs[0];
1513
1514badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001515 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001516 force_sig(SIGSEGV /* , current */);
1517 return 0;
1518}
1519
pbrooka745ec62008-05-06 15:36:17 +00001520long do_rt_sigreturn_v2(CPUState *env)
1521{
1522 abi_ulong frame_addr;
1523 struct rt_sigframe_v2 *frame;
pbrooka745ec62008-05-06 15:36:17 +00001524
1525 /*
1526 * Since we stacked the signal on a 64-bit boundary,
1527 * then 'sp' should be word aligned here. If it's
1528 * not, then the user is trying to mess with us.
1529 */
1530 if (env->regs[13] & 7)
1531 goto badframe;
1532
1533 frame_addr = env->regs[13];
1534 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1535 goto badframe;
1536
pbrooka8c33202008-05-07 23:22:46 +00001537 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1538 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00001539
pbrooka745ec62008-05-06 15:36:17 +00001540 unlock_user_struct(frame, frame_addr, 0);
1541 return env->regs[0];
1542
1543badframe:
1544 unlock_user_struct(frame, frame_addr, 0);
1545 force_sig(SIGSEGV /* , current */);
1546 return 0;
1547}
1548
1549long do_rt_sigreturn(CPUState *env)
1550{
1551 if (get_osversion() >= 0x020612) {
1552 return do_rt_sigreturn_v2(env);
1553 } else {
1554 return do_rt_sigreturn_v1(env);
1555 }
1556}
1557
bellard6d5e2162004-09-30 22:04:13 +00001558#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001559
bellard6d5e2162004-09-30 22:04:13 +00001560#define __SUNOS_MAXWIN 31
1561
1562/* This is what SunOS does, so shall I. */
1563struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001564 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001565
blueswir1992f48a2007-10-14 16:27:31 +00001566 abi_ulong sigc_mask; /* sigmask to restore */
1567 abi_ulong sigc_sp; /* stack pointer */
1568 abi_ulong sigc_pc; /* program counter */
1569 abi_ulong sigc_npc; /* next program counter */
1570 abi_ulong sigc_psr; /* for condition codes etc */
1571 abi_ulong sigc_g1; /* User uses these two registers */
1572 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001573
1574 /* Now comes information regarding the users window set
1575 * at the time of the signal.
1576 */
blueswir1992f48a2007-10-14 16:27:31 +00001577 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001578
1579 /* stack ptrs for each regwin buf */
1580 char *sigc_spbuf[__SUNOS_MAXWIN];
1581
1582 /* Windows to restore after signal */
1583 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001584 abi_ulong locals[8];
1585 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001586 } sigc_wbuf[__SUNOS_MAXWIN];
1587};
1588/* A Sparc stack frame */
1589struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001590 abi_ulong locals[8];
1591 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001592 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001593 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001594 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001595 abi_ulong xargs[6];
1596 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001597};
1598
1599typedef struct {
1600 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001601 abi_ulong psr;
1602 abi_ulong pc;
1603 abi_ulong npc;
1604 abi_ulong y;
1605 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001606 } si_regs;
1607 int si_mask;
1608} __siginfo_t;
1609
1610typedef struct {
1611 unsigned long si_float_regs [32];
1612 unsigned long si_fsr;
1613 unsigned long si_fpqdepth;
1614 struct {
1615 unsigned long *insn_addr;
1616 unsigned long insn;
1617 } si_fpqueue [16];
bellard74ccb342006-07-18 21:23:34 +00001618} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001619
1620
1621struct target_signal_frame {
1622 struct sparc_stackf ss;
1623 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00001624 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001625 abi_ulong insns[2] __attribute__ ((aligned (8)));
1626 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1627 abi_ulong extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001628 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001629};
1630struct target_rt_signal_frame {
1631 struct sparc_stackf ss;
1632 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001633 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001634 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00001635 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001636 unsigned int insns[2];
1637 stack_t stack;
1638 unsigned int extra_size; /* Should be 0 */
bellard74ccb342006-07-18 21:23:34 +00001639 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001640};
1641
bellarde80cfcf2004-12-19 23:18:01 +00001642#define UREG_O0 16
1643#define UREG_O6 22
1644#define UREG_I0 0
1645#define UREG_I1 1
1646#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001647#define UREG_I3 3
1648#define UREG_I4 4
1649#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001650#define UREG_I6 6
1651#define UREG_I7 7
1652#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001653#define UREG_FP UREG_I6
1654#define UREG_SP UREG_O6
1655
pbrook624f7972008-05-31 16:11:38 +00001656static inline abi_ulong get_sigframe(struct target_sigaction *sa,
bellard459a4012007-11-11 19:45:10 +00001657 CPUState *env, unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001658{
bellard459a4012007-11-11 19:45:10 +00001659 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001660
1661 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001662
1663 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00001664 if (sa->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +00001665 if (!on_sig_stack(sp)
1666 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1667 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001668 }
bellard459a4012007-11-11 19:45:10 +00001669 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001670}
1671
1672static int
blueswir1992f48a2007-10-14 16:27:31 +00001673setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001674{
1675 int err = 0, i;
1676
bellard6d5e2162004-09-30 22:04:13 +00001677 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001678 err |= __put_user(env->pc, &si->si_regs.pc);
1679 err |= __put_user(env->npc, &si->si_regs.npc);
1680 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001681 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001682 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1683 }
bellarda315a142005-01-30 22:59:18 +00001684 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001685 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001686 }
bellard6d5e2162004-09-30 22:04:13 +00001687 err |= __put_user(mask, &si->si_mask);
1688 return err;
1689}
bellarde80cfcf2004-12-19 23:18:01 +00001690
bellard80a9d032005-01-03 23:31:27 +00001691#if 0
bellard6d5e2162004-09-30 22:04:13 +00001692static int
1693setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1694 CPUState *env, unsigned long mask)
1695{
1696 int err = 0;
1697
1698 err |= __put_user(mask, &sc->sigc_mask);
1699 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1700 err |= __put_user(env->pc, &sc->sigc_pc);
1701 err |= __put_user(env->npc, &sc->sigc_npc);
1702 err |= __put_user(env->psr, &sc->sigc_psr);
1703 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1704 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1705
1706 return err;
1707}
bellard80a9d032005-01-03 23:31:27 +00001708#endif
bellard6d5e2162004-09-30 22:04:13 +00001709#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1710
pbrook624f7972008-05-31 16:11:38 +00001711static void setup_frame(int sig, struct target_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001712 target_sigset_t *set, CPUState *env)
1713{
bellard459a4012007-11-11 19:45:10 +00001714 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001715 struct target_signal_frame *sf;
1716 int sigframe_size, err, i;
1717
1718 /* 1. Make sure everything is clean */
1719 //synchronize_user_stack();
1720
1721 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001722 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001723
bellard459a4012007-11-11 19:45:10 +00001724 sf = lock_user(VERIFY_WRITE, sf_addr,
1725 sizeof(struct target_signal_frame), 0);
1726 if (!sf)
1727 goto sigsegv;
1728
bellarde80cfcf2004-12-19 23:18:01 +00001729 //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 +00001730#if 0
1731 if (invalid_frame_pointer(sf, sigframe_size))
1732 goto sigill_and_return;
1733#endif
1734 /* 2. Save the current process state */
1735 err = setup___siginfo(&sf->info, env, set->sig[0]);
1736 err |= __put_user(0, &sf->extra_size);
1737
1738 //err |= save_fpu_state(regs, &sf->fpu_state);
1739 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1740
1741 err |= __put_user(set->sig[0], &sf->info.si_mask);
1742 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1743 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1744 }
1745
bellarda315a142005-01-30 22:59:18 +00001746 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001747 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001748 }
bellarda315a142005-01-30 22:59:18 +00001749 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001750 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001751 }
bellard6d5e2162004-09-30 22:04:13 +00001752 if (err)
1753 goto sigsegv;
1754
1755 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001756 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001757 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001758 env->regwptr[UREG_I1] = sf_addr +
1759 offsetof(struct target_signal_frame, info);
1760 env->regwptr[UREG_I2] = sf_addr +
1761 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001762
1763 /* 4. signal handler */
pbrook624f7972008-05-31 16:11:38 +00001764 env->pc = ka->_sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00001765 env->npc = (env->pc + 4);
1766 /* 5. return to kernel instructions */
pbrook624f7972008-05-31 16:11:38 +00001767 if (ka->sa_restorer)
1768 env->regwptr[UREG_I7] = ka->sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00001769 else {
bellard775b58d2007-11-11 16:22:17 +00001770 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00001771
1772 env->regwptr[UREG_I7] = sf_addr +
1773 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00001774
1775 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001776 val32 = 0x821020d8;
1777 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001778
1779 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001780 val32 = 0x91d02010;
1781 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001782 if (err)
1783 goto sigsegv;
1784
1785 /* Flush instruction space. */
1786 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001787 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001788 }
bellard459a4012007-11-11 19:45:10 +00001789 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001790 return;
bellard459a4012007-11-11 19:45:10 +00001791#if 0
1792sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001793 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00001794#endif
bellard6d5e2162004-09-30 22:04:13 +00001795sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001796 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00001797 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001798 force_sig(TARGET_SIGSEGV);
1799}
1800static inline int
bellard74ccb342006-07-18 21:23:34 +00001801restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001802{
1803 int err;
1804#if 0
1805#ifdef CONFIG_SMP
1806 if (current->flags & PF_USEDFPU)
1807 regs->psr &= ~PSR_EF;
1808#else
1809 if (current == last_task_used_math) {
1810 last_task_used_math = 0;
1811 regs->psr &= ~PSR_EF;
1812 }
1813#endif
1814 current->used_math = 1;
1815 current->flags &= ~PF_USEDFPU;
1816#endif
1817#if 0
1818 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1819 return -EFAULT;
1820#endif
1821
bellardfafffae2006-10-28 12:09:16 +00001822#if 0
1823 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001824 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1825 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001826#endif
bellard6d5e2162004-09-30 22:04:13 +00001827 err |= __get_user(env->fsr, &fpu->si_fsr);
1828#if 0
1829 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1830 if (current->thread.fpqdepth != 0)
1831 err |= __copy_from_user(&current->thread.fpqueue[0],
1832 &fpu->si_fpqueue[0],
1833 ((sizeof(unsigned long) +
1834 (sizeof(unsigned long *)))*16));
1835#endif
1836 return err;
1837}
1838
1839
pbrook624f7972008-05-31 16:11:38 +00001840static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellard6d5e2162004-09-30 22:04:13 +00001841 target_siginfo_t *info,
1842 target_sigset_t *set, CPUState *env)
1843{
1844 fprintf(stderr, "setup_rt_frame: not implemented\n");
1845}
1846
1847long do_sigreturn(CPUState *env)
1848{
bellardf8b0aa22007-11-11 23:03:42 +00001849 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001850 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001851 uint32_t up_psr, pc, npc;
bellard6d5e2162004-09-30 22:04:13 +00001852 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001853 sigset_t host_set;
bellardf8b0aa22007-11-11 23:03:42 +00001854 abi_ulong fpu_save_addr;
bellarde80cfcf2004-12-19 23:18:01 +00001855 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001856
bellardf8b0aa22007-11-11 23:03:42 +00001857 sf_addr = env->regwptr[UREG_FP];
1858 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1859 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00001860#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001861 fprintf(stderr, "sigreturn\n");
1862 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 +00001863#endif
bellarde80cfcf2004-12-19 23:18:01 +00001864 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001865
1866 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00001867
bellardf8b0aa22007-11-11 23:03:42 +00001868 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00001869 goto segv_and_exit;
1870
1871 err = __get_user(pc, &sf->info.si_regs.pc);
1872 err |= __get_user(npc, &sf->info.si_regs.npc);
1873
bellard6d5e2162004-09-30 22:04:13 +00001874 if ((pc | npc) & 3)
1875 goto segv_and_exit;
1876
1877 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001878 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1879
bellard6d5e2162004-09-30 22:04:13 +00001880 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001881 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1882 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1883
1884 env->pc = pc;
1885 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001886 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001887 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001888 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1889 }
bellarda315a142005-01-30 22:59:18 +00001890 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001891 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1892 }
bellard6d5e2162004-09-30 22:04:13 +00001893
bellardf8b0aa22007-11-11 23:03:42 +00001894 err |= __get_user(fpu_save_addr, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001895
bellarde80cfcf2004-12-19 23:18:01 +00001896 //if (fpu_save)
1897 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001898
1899 /* This is pretty much atomic, no amount locking would prevent
1900 * the races which exist anyways.
1901 */
1902 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001903 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1904 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1905 }
1906
1907 target_to_host_sigset_internal(&host_set, &set);
1908 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001909
1910 if (err)
1911 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00001912 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001913 return env->regwptr[0];
1914
1915segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00001916 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001917 force_sig(TARGET_SIGSEGV);
1918}
1919
1920long do_rt_sigreturn(CPUState *env)
1921{
1922 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00001923 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00001924}
1925
bellard459a4012007-11-11 19:45:10 +00001926#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00001927#define MC_TSTATE 0
1928#define MC_PC 1
1929#define MC_NPC 2
1930#define MC_Y 3
1931#define MC_G1 4
1932#define MC_G2 5
1933#define MC_G3 6
1934#define MC_G4 7
1935#define MC_G5 8
1936#define MC_G6 9
1937#define MC_G7 10
1938#define MC_O0 11
1939#define MC_O1 12
1940#define MC_O2 13
1941#define MC_O3 14
1942#define MC_O4 15
1943#define MC_O5 16
1944#define MC_O6 17
1945#define MC_O7 18
1946#define MC_NGREG 19
1947
blueswir1992f48a2007-10-14 16:27:31 +00001948typedef abi_ulong target_mc_greg_t;
blueswir15bfb56b2007-10-05 17:01:51 +00001949typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1950
1951struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00001952 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00001953 uint32_t mcfq_insn;
1954};
1955
1956struct target_mc_fpu {
1957 union {
1958 uint32_t sregs[32];
1959 uint64_t dregs[32];
1960 //uint128_t qregs[16];
1961 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00001962 abi_ulong mcfpu_fsr;
1963 abi_ulong mcfpu_fprs;
1964 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00001965 struct target_mc_fq *mcfpu_fq;
1966 unsigned char mcfpu_qcnt;
1967 unsigned char mcfpu_qentsz;
1968 unsigned char mcfpu_enab;
1969};
1970typedef struct target_mc_fpu target_mc_fpu_t;
1971
1972typedef struct {
1973 target_mc_gregset_t mc_gregs;
1974 target_mc_greg_t mc_fp;
1975 target_mc_greg_t mc_i7;
1976 target_mc_fpu_t mc_fpregs;
1977} target_mcontext_t;
1978
1979struct target_ucontext {
1980 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00001981 abi_ulong uc_flags;
blueswir15bfb56b2007-10-05 17:01:51 +00001982 target_sigset_t uc_sigmask;
1983 target_mcontext_t uc_mcontext;
1984};
1985
1986/* A V9 register window */
1987struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00001988 abi_ulong locals[8];
1989 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00001990};
1991
1992#define TARGET_STACK_BIAS 2047
1993
1994/* {set, get}context() needed for 64-bit SparcLinux userland. */
1995void sparc64_set_context(CPUSPARCState *env)
1996{
bellard459a4012007-11-11 19:45:10 +00001997 abi_ulong ucp_addr;
1998 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00001999 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00002000 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00002001 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002002 unsigned char fenab;
2003 int err;
2004 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002005
bellard459a4012007-11-11 19:45:10 +00002006 ucp_addr = env->regwptr[UREG_I0];
2007 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2008 goto do_sigsegv;
blueswir15bfb56b2007-10-05 17:01:51 +00002009 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00002010 err = __get_user(pc, &((*grp)[MC_PC]));
2011 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002012 if (err || ((pc | npc) & 3))
2013 goto do_sigsegv;
2014 if (env->regwptr[UREG_I1]) {
2015 target_sigset_t target_set;
2016 sigset_t set;
2017
2018 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002019 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002020 goto do_sigsegv;
2021 } else {
bellard459a4012007-11-11 19:45:10 +00002022 abi_ulong *src, *dst;
2023 src = ucp->uc_sigmask.sig;
2024 dst = target_set.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002025 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002026 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002027 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00002028 if (err)
2029 goto do_sigsegv;
2030 }
2031 target_to_host_sigset_internal(&set, &target_set);
2032 sigprocmask(SIG_SETMASK, &set, NULL);
2033 }
2034 env->pc = pc;
2035 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00002036 err |= __get_user(env->y, &((*grp)[MC_Y]));
2037 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002038 env->asi = (tstate >> 24) & 0xff;
2039 PUT_CCR(env, tstate >> 32);
2040 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00002041 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2042 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2043 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2044 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2045 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2046 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2047 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2048 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2049 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2050 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2051 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2052 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2053 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2054 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2055 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002056
bellard579a97f2007-11-11 14:26:47 +00002057 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2058 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002059
bellard459a4012007-11-11 19:45:10 +00002060 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2061 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2062 abi_ulong) != 0)
2063 goto do_sigsegv;
2064 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2065 abi_ulong) != 0)
2066 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002067 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2068 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002069 {
2070 uint32_t *src, *dst;
2071 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2072 dst = env->fpr;
2073 /* XXX: check that the CPU storage is the same as user context */
2074 for (i = 0; i < 64; i++, dst++, src++)
2075 err |= __get_user(*dst, src);
2076 }
bellard579a97f2007-11-11 14:26:47 +00002077 err |= __get_user(env->fsr,
2078 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2079 err |= __get_user(env->gsr,
2080 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002081 if (err)
2082 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002083 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002084 return;
2085 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002086 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002087 force_sig(SIGSEGV);
2088}
2089
2090void sparc64_get_context(CPUSPARCState *env)
2091{
bellard459a4012007-11-11 19:45:10 +00002092 abi_ulong ucp_addr;
2093 struct target_ucontext *ucp;
blueswir15bfb56b2007-10-05 17:01:51 +00002094 target_mc_gregset_t *grp;
2095 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002096 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002097 int err;
2098 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002099 target_sigset_t target_set;
2100 sigset_t set;
2101
bellard459a4012007-11-11 19:45:10 +00002102 ucp_addr = env->regwptr[UREG_I0];
2103 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2104 goto do_sigsegv;
2105
blueswir15bfb56b2007-10-05 17:01:51 +00002106 mcp = &ucp->uc_mcontext;
2107 grp = &mcp->mc_gregs;
2108
2109 /* Skip over the trap instruction, first. */
2110 env->pc = env->npc;
2111 env->npc += 4;
2112
2113 err = 0;
2114
2115 sigprocmask(0, NULL, &set);
2116 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002117 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002118 err |= __put_user(target_set.sig[0],
2119 (abi_ulong *)&ucp->uc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002120 } else {
2121 abi_ulong *src, *dst;
2122 src = target_set.sig;
2123 dst = ucp->uc_sigmask.sig;
blueswir1992f48a2007-10-14 16:27:31 +00002124 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002125 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002126 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00002127 if (err)
2128 goto do_sigsegv;
2129 }
2130
bellard459a4012007-11-11 19:45:10 +00002131 /* XXX: tstate must be saved properly */
2132 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00002133 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2134 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2135 err |= __put_user(env->y, &((*grp)[MC_Y]));
2136 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2137 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2138 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2139 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2140 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2141 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2142 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2143 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2144 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2145 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2146 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2147 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2148 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2149 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2150 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002151
bellard459a4012007-11-11 19:45:10 +00002152 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2153 fp = i7 = 0;
2154 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2155 abi_ulong) != 0)
2156 goto do_sigsegv;
2157 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2158 abi_ulong) != 0)
2159 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002160 err |= __put_user(fp, &(mcp->mc_fp));
2161 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002162
bellard459a4012007-11-11 19:45:10 +00002163 {
2164 uint32_t *src, *dst;
2165 src = env->fpr;
2166 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2167 /* XXX: check that the CPU storage is the same as user context */
2168 for (i = 0; i < 64; i++, dst++, src++)
2169 err |= __put_user(*src, dst);
2170 }
bellard579a97f2007-11-11 14:26:47 +00002171 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2172 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2173 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002174
2175 if (err)
2176 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002177 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002178 return;
2179 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002180 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002181 force_sig(SIGSEGV);
2182}
2183#endif
thsd26bc212007-11-08 18:05:37 +00002184#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002185
2186# warning signal handling not implemented
2187
pbrook624f7972008-05-31 16:11:38 +00002188static void setup_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002189 target_sigset_t *set, CPUState *env)
2190{
2191 fprintf(stderr, "setup_frame: not implemented\n");
2192}
2193
pbrook624f7972008-05-31 16:11:38 +00002194static void setup_rt_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002195 target_siginfo_t *info,
2196 target_sigset_t *set, CPUState *env)
2197{
2198 fprintf(stderr, "setup_rt_frame: not implemented\n");
2199}
2200
2201long do_sigreturn(CPUState *env)
2202{
2203 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002204 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002205}
2206
2207long do_rt_sigreturn(CPUState *env)
2208{
2209 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002210 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002211}
2212
thsd26bc212007-11-08 18:05:37 +00002213#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002214
2215# warning signal handling not implemented
2216
pbrook624f7972008-05-31 16:11:38 +00002217static void setup_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002218 target_sigset_t *set, CPUState *env)
2219{
2220 fprintf(stderr, "setup_frame: not implemented\n");
2221}
2222
pbrook624f7972008-05-31 16:11:38 +00002223static void setup_rt_frame(int sig, struct target_sigaction *ka,
ths540635b2007-09-30 01:58:33 +00002224 target_siginfo_t *info,
2225 target_sigset_t *set, CPUState *env)
2226{
2227 fprintf(stderr, "setup_rt_frame: not implemented\n");
2228}
2229
2230long do_sigreturn(CPUState *env)
2231{
2232 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002233 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002234}
2235
2236long do_rt_sigreturn(CPUState *env)
2237{
2238 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002239 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002240}
2241
thsd26bc212007-11-08 18:05:37 +00002242#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002243
2244struct target_sigcontext {
2245 uint32_t sc_regmask; /* Unused */
2246 uint32_t sc_status;
2247 uint64_t sc_pc;
2248 uint64_t sc_regs[32];
2249 uint64_t sc_fpregs[32];
2250 uint32_t sc_ownedfp; /* Unused */
2251 uint32_t sc_fpc_csr;
2252 uint32_t sc_fpc_eir; /* Unused */
2253 uint32_t sc_used_math;
2254 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
2255 uint64_t sc_mdhi;
2256 uint64_t sc_mdlo;
2257 target_ulong sc_hi1; /* Was sc_cause */
2258 target_ulong sc_lo1; /* Was sc_badvaddr */
2259 target_ulong sc_hi2; /* Was sc_sigset[4] */
2260 target_ulong sc_lo2;
2261 target_ulong sc_hi3;
2262 target_ulong sc_lo3;
2263};
2264
2265struct sigframe {
2266 uint32_t sf_ass[4]; /* argument save space for o32 */
2267 uint32_t sf_code[2]; /* signal trampoline */
2268 struct target_sigcontext sf_sc;
2269 target_sigset_t sf_mask;
2270};
2271
2272/* Install trampoline to jump back from signal handler */
2273static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2274{
2275 int err;
2276
2277 /*
2278 * Set up the return code ...
2279 *
2280 * li v0, __NR__foo_sigreturn
2281 * syscall
2282 */
2283
2284 err = __put_user(0x24020000 + syscall, tramp + 0);
2285 err |= __put_user(0x0000000c , tramp + 1);
2286 /* flush_cache_sigtramp((unsigned long) tramp); */
2287 return err;
2288}
2289
2290static inline int
2291setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2292{
2293 int err = 0;
2294
thsb5dc7732008-06-27 10:02:35 +00002295 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002296
thsb5dc7732008-06-27 10:02:35 +00002297#define save_gp_reg(i) do { \
2298 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002299 } while(0)
2300 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2301 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2302 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2303 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2304 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2305 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2306 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2307 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2308 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002309#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002310
thsb5dc7732008-06-27 10:02:35 +00002311 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2312 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002313
2314 /* Not used yet, but might be useful if we ever have DSP suppport */
2315#if 0
2316 if (cpu_has_dsp) {
2317 err |= __put_user(mfhi1(), &sc->sc_hi1);
2318 err |= __put_user(mflo1(), &sc->sc_lo1);
2319 err |= __put_user(mfhi2(), &sc->sc_hi2);
2320 err |= __put_user(mflo2(), &sc->sc_lo2);
2321 err |= __put_user(mfhi3(), &sc->sc_hi3);
2322 err |= __put_user(mflo3(), &sc->sc_lo3);
2323 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2324 }
2325 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002326#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002327 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2328 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2329 if (cpu_has_dsp) {
2330 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2331 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2332 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2333 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2334 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2335 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2336 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2337 }
ths388bb212007-05-13 13:58:00 +00002338#endif
2339#endif
bellard106ec872006-06-27 21:08:10 +00002340
ths388bb212007-05-13 13:58:00 +00002341#if 0
bellard106ec872006-06-27 21:08:10 +00002342 err |= __put_user(!!used_math(), &sc->sc_used_math);
2343
2344 if (!used_math())
2345 goto out;
2346
2347 /*
2348 * Save FPU state to signal context. Signal handler will "inherit"
2349 * current FPU state.
2350 */
2351 preempt_disable();
2352
2353 if (!is_fpu_owner()) {
2354 own_fpu();
2355 restore_fp(current);
2356 }
2357 err |= save_fp_context(sc);
2358
2359 preempt_enable();
2360 out:
2361#endif
2362 return err;
2363}
2364
2365static inline int
2366restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2367{
2368 int err = 0;
2369
2370 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2371
thsb5dc7732008-06-27 10:02:35 +00002372 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2373 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002374
thsead93602007-09-06 00:18:15 +00002375#define restore_gp_reg(i) do { \
thsb5dc7732008-06-27 10:02:35 +00002376 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002377 } while(0)
2378 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2379 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2380 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2381 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2382 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2383 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2384 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2385 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2386 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2387 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2388 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002389#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002390
2391#if 0
2392 if (cpu_has_dsp) {
2393 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2394 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2395 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2396 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2397 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2398 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2399 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2400 }
ths388bb212007-05-13 13:58:00 +00002401#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002402 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2403 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2404 if (cpu_has_dsp) {
2405 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2406 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2407 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2408 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2409 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2410 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2411 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2412 }
ths388bb212007-05-13 13:58:00 +00002413#endif
bellard106ec872006-06-27 21:08:10 +00002414
2415 err |= __get_user(used_math, &sc->sc_used_math);
2416 conditional_used_math(used_math);
2417
2418 preempt_disable();
2419
2420 if (used_math()) {
2421 /* restore fpu context if we have used it before */
2422 own_fpu();
2423 err |= restore_fp_context(sc);
2424 } else {
2425 /* signal handler may have used FPU. Give it up. */
2426 lose_fpu();
2427 }
2428
2429 preempt_enable();
2430#endif
2431 return err;
2432}
2433/*
2434 * Determine which stack to use..
2435 */
bellard579a97f2007-11-11 14:26:47 +00002436static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00002437get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
bellard106ec872006-06-27 21:08:10 +00002438{
2439 unsigned long sp;
2440
2441 /* Default to using normal stack */
thsb5dc7732008-06-27 10:02:35 +00002442 sp = regs->active_tc.gpr[29];
bellard106ec872006-06-27 21:08:10 +00002443
2444 /*
2445 * FPU emulator may have it's own trampoline active just
2446 * above the user stack, 16-bytes before the next lowest
2447 * 16 byte boundary. Try to avoid trashing it.
2448 */
2449 sp -= 32;
2450
bellard106ec872006-06-27 21:08:10 +00002451 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002452 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
thsa04e1342007-09-27 13:57:58 +00002453 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2454 }
bellard106ec872006-06-27 21:08:10 +00002455
bellard579a97f2007-11-11 14:26:47 +00002456 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002457}
2458
bellard579a97f2007-11-11 14:26:47 +00002459/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00002460static void setup_frame(int sig, struct target_sigaction * ka,
bellard579a97f2007-11-11 14:26:47 +00002461 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002462{
2463 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002464 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002465 int i;
2466
bellard579a97f2007-11-11 14:26:47 +00002467 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2468 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002469 goto give_sigsegv;
2470
2471 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2472
2473 if(setup_sigcontext(regs, &frame->sf_sc))
2474 goto give_sigsegv;
2475
2476 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2477 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2478 goto give_sigsegv;
2479 }
2480
2481 /*
2482 * Arguments to signal handler:
2483 *
2484 * a0 = signal number
2485 * a1 = 0 (should be cause)
2486 * a2 = pointer to struct sigcontext
2487 *
2488 * $25 and PC point to the signal handler, $29 points to the
2489 * struct sigframe.
2490 */
thsb5dc7732008-06-27 10:02:35 +00002491 regs->active_tc.gpr[ 4] = sig;
2492 regs->active_tc.gpr[ 5] = 0;
2493 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2494 regs->active_tc.gpr[29] = frame_addr;
2495 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002496 /* The original kernel code sets CP0_EPC to the handler
2497 * since it returns to userland using eret
2498 * we cannot do this here, and we must set PC directly */
thsb5dc7732008-06-27 10:02:35 +00002499 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002500 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002501 return;
2502
2503give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002504 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002505 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002506 return;
bellard106ec872006-06-27 21:08:10 +00002507}
2508
2509long do_sigreturn(CPUState *regs)
2510{
ths388bb212007-05-13 13:58:00 +00002511 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002512 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002513 sigset_t blocked;
2514 target_sigset_t target_set;
2515 int i;
bellard106ec872006-06-27 21:08:10 +00002516
2517#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002518 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002519#endif
thsb5dc7732008-06-27 10:02:35 +00002520 frame_addr = regs->active_tc.gpr[29];
bellard579a97f2007-11-11 14:26:47 +00002521 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002522 goto badframe;
2523
ths388bb212007-05-13 13:58:00 +00002524 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002525 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2526 goto badframe;
ths388bb212007-05-13 13:58:00 +00002527 }
bellard106ec872006-06-27 21:08:10 +00002528
ths388bb212007-05-13 13:58:00 +00002529 target_to_host_sigset_internal(&blocked, &target_set);
2530 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002531
ths388bb212007-05-13 13:58:00 +00002532 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002533 goto badframe;
2534
2535#if 0
ths388bb212007-05-13 13:58:00 +00002536 /*
2537 * Don't let your children do this ...
2538 */
2539 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002540 "move\t$29, %0\n\t"
2541 "j\tsyscall_exit"
2542 :/* no outputs */
2543 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002544 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002545#endif
ths3b46e622007-09-17 08:09:54 +00002546
thsb5dc7732008-06-27 10:02:35 +00002547 regs->active_tc.PC = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002548 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002549 * maybe a problem with nested signals ? */
2550 regs->CP0_EPC = 0;
2551 return 0;
2552
2553badframe:
ths388bb212007-05-13 13:58:00 +00002554 force_sig(TARGET_SIGSEGV/*, current*/);
2555 return 0;
bellard106ec872006-06-27 21:08:10 +00002556}
2557
pbrook624f7972008-05-31 16:11:38 +00002558static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellard106ec872006-06-27 21:08:10 +00002559 target_siginfo_t *info,
2560 target_sigset_t *set, CPUState *env)
2561{
2562 fprintf(stderr, "setup_rt_frame: not implemented\n");
2563}
2564
2565long do_rt_sigreturn(CPUState *env)
2566{
2567 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002568 return -TARGET_ENOSYS;
bellard106ec872006-06-27 21:08:10 +00002569}
bellard6d5e2162004-09-30 22:04:13 +00002570
thsc3b5bc82007-12-02 06:31:25 +00002571#elif defined(TARGET_SH4)
2572
2573/*
2574 * code and data structures from linux kernel:
2575 * include/asm-sh/sigcontext.h
2576 * arch/sh/kernel/signal.c
2577 */
2578
2579struct target_sigcontext {
2580 target_ulong oldmask;
2581
2582 /* CPU registers */
2583 target_ulong sc_gregs[16];
2584 target_ulong sc_pc;
2585 target_ulong sc_pr;
2586 target_ulong sc_sr;
2587 target_ulong sc_gbr;
2588 target_ulong sc_mach;
2589 target_ulong sc_macl;
2590
2591 /* FPU registers */
2592 target_ulong sc_fpregs[16];
2593 target_ulong sc_xfpregs[16];
2594 unsigned int sc_fpscr;
2595 unsigned int sc_fpul;
2596 unsigned int sc_ownedfp;
2597};
2598
2599struct target_sigframe
2600{
2601 struct target_sigcontext sc;
2602 target_ulong extramask[TARGET_NSIG_WORDS-1];
2603 uint16_t retcode[3];
2604};
2605
2606
2607struct target_ucontext {
2608 target_ulong uc_flags;
2609 struct target_ucontext *uc_link;
2610 target_stack_t uc_stack;
2611 struct target_sigcontext uc_mcontext;
2612 target_sigset_t uc_sigmask; /* mask last for extensibility */
2613};
2614
2615struct target_rt_sigframe
2616{
2617 struct target_siginfo info;
2618 struct target_ucontext uc;
2619 uint16_t retcode[3];
2620};
2621
2622
2623#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2624#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2625
pbrook624f7972008-05-31 16:11:38 +00002626static abi_ulong get_sigframe(struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002627 unsigned long sp, size_t frame_size)
2628{
pbrook624f7972008-05-31 16:11:38 +00002629 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
thsc3b5bc82007-12-02 06:31:25 +00002630 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2631 }
2632
2633 return (sp - frame_size) & -8ul;
2634}
2635
2636static int setup_sigcontext(struct target_sigcontext *sc,
2637 CPUState *regs, unsigned long mask)
2638{
2639 int err = 0;
2640
2641#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2642 COPY(gregs[0]); COPY(gregs[1]);
2643 COPY(gregs[2]); COPY(gregs[3]);
2644 COPY(gregs[4]); COPY(gregs[5]);
2645 COPY(gregs[6]); COPY(gregs[7]);
2646 COPY(gregs[8]); COPY(gregs[9]);
2647 COPY(gregs[10]); COPY(gregs[11]);
2648 COPY(gregs[12]); COPY(gregs[13]);
2649 COPY(gregs[14]); COPY(gregs[15]);
2650 COPY(gbr); COPY(mach);
2651 COPY(macl); COPY(pr);
2652 COPY(sr); COPY(pc);
2653#undef COPY
2654
2655 /* todo: save FPU registers here */
2656
2657 /* non-iBCS2 extensions.. */
2658 err |= __put_user(mask, &sc->oldmask);
2659
2660 return err;
2661}
2662
2663static int restore_sigcontext(struct CPUState *regs,
2664 struct target_sigcontext *sc)
2665{
2666 unsigned int err = 0;
2667
2668#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2669 COPY(gregs[1]);
2670 COPY(gregs[2]); COPY(gregs[3]);
2671 COPY(gregs[4]); COPY(gregs[5]);
2672 COPY(gregs[6]); COPY(gregs[7]);
2673 COPY(gregs[8]); COPY(gregs[9]);
2674 COPY(gregs[10]); COPY(gregs[11]);
2675 COPY(gregs[12]); COPY(gregs[13]);
2676 COPY(gregs[14]); COPY(gregs[15]);
2677 COPY(gbr); COPY(mach);
2678 COPY(macl); COPY(pr);
2679 COPY(sr); COPY(pc);
2680#undef COPY
2681
2682 /* todo: restore FPU registers here */
2683
2684 regs->tra = -1; /* disable syscall checks */
2685 return err;
2686}
2687
pbrook624f7972008-05-31 16:11:38 +00002688static void setup_frame(int sig, struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002689 target_sigset_t *set, CPUState *regs)
2690{
2691 struct target_sigframe *frame;
2692 abi_ulong frame_addr;
2693 int i;
2694 int err = 0;
2695 int signal;
2696
2697 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2698 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2699 goto give_sigsegv;
2700
2701 signal = current_exec_domain_sig(sig);
2702
2703 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2704
2705 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2706 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2707 }
2708
2709 /* Set up to return from userspace. If provided, use a stub
2710 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002711 if (ka->sa_flags & TARGET_SA_RESTORER) {
2712 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002713 } else {
2714 /* Generate return code (system call to sigreturn) */
2715 err |= __put_user(MOVW(2), &frame->retcode[0]);
2716 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2717 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2718 regs->pr = (unsigned long) frame->retcode;
2719 }
2720
2721 if (err)
2722 goto give_sigsegv;
2723
2724 /* Set up registers for signal handler */
2725 regs->gregs[15] = (unsigned long) frame;
2726 regs->gregs[4] = signal; /* Arg for signal handler */
2727 regs->gregs[5] = 0;
2728 regs->gregs[6] = (unsigned long) &frame->sc;
pbrook624f7972008-05-31 16:11:38 +00002729 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002730
2731 unlock_user_struct(frame, frame_addr, 1);
2732 return;
2733
2734give_sigsegv:
2735 unlock_user_struct(frame, frame_addr, 1);
2736 force_sig(SIGSEGV);
2737}
2738
pbrook624f7972008-05-31 16:11:38 +00002739static void setup_rt_frame(int sig, struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002740 target_siginfo_t *info,
2741 target_sigset_t *set, CPUState *regs)
2742{
2743 struct target_rt_sigframe *frame;
2744 abi_ulong frame_addr;
2745 int i;
2746 int err = 0;
2747 int signal;
2748
2749 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2750 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2751 goto give_sigsegv;
2752
2753 signal = current_exec_domain_sig(sig);
2754
2755 err |= copy_siginfo_to_user(&frame->info, info);
2756
2757 /* Create the ucontext. */
2758 err |= __put_user(0, &frame->uc.uc_flags);
2759 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
balrog526ccb72008-07-16 12:13:52 +00002760 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
thsc3b5bc82007-12-02 06:31:25 +00002761 &frame->uc.uc_stack.ss_sp);
2762 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2763 &frame->uc.uc_stack.ss_flags);
2764 err |= __put_user(target_sigaltstack_used.ss_size,
2765 &frame->uc.uc_stack.ss_size);
2766 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2767 regs, set->sig[0]);
2768 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2769 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2770 }
2771
2772 /* Set up to return from userspace. If provided, use a stub
2773 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002774 if (ka->sa_flags & TARGET_SA_RESTORER) {
2775 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002776 } else {
2777 /* Generate return code (system call to sigreturn) */
2778 err |= __put_user(MOVW(2), &frame->retcode[0]);
2779 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2780 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2781 regs->pr = (unsigned long) frame->retcode;
2782 }
2783
2784 if (err)
2785 goto give_sigsegv;
2786
2787 /* Set up registers for signal handler */
2788 regs->gregs[15] = (unsigned long) frame;
2789 regs->gregs[4] = signal; /* Arg for signal handler */
2790 regs->gregs[5] = (unsigned long) &frame->info;
2791 regs->gregs[6] = (unsigned long) &frame->uc;
pbrook624f7972008-05-31 16:11:38 +00002792 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002793
2794 unlock_user_struct(frame, frame_addr, 1);
2795 return;
2796
2797give_sigsegv:
2798 unlock_user_struct(frame, frame_addr, 1);
2799 force_sig(SIGSEGV);
2800}
2801
2802long do_sigreturn(CPUState *regs)
2803{
2804 struct target_sigframe *frame;
2805 abi_ulong frame_addr;
2806 sigset_t blocked;
2807 target_sigset_t target_set;
2808 int i;
2809 int err = 0;
2810
2811#if defined(DEBUG_SIGNAL)
2812 fprintf(stderr, "do_sigreturn\n");
2813#endif
2814 frame_addr = regs->gregs[15];
2815 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2816 goto badframe;
2817
2818 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2819 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2820 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2821 }
2822
2823 if (err)
2824 goto badframe;
2825
2826 target_to_host_sigset_internal(&blocked, &target_set);
2827 sigprocmask(SIG_SETMASK, &blocked, NULL);
2828
2829 if (restore_sigcontext(regs, &frame->sc))
2830 goto badframe;
2831
2832 unlock_user_struct(frame, frame_addr, 0);
2833 return regs->gregs[0];
2834
2835badframe:
2836 unlock_user_struct(frame, frame_addr, 0);
2837 force_sig(TARGET_SIGSEGV);
2838 return 0;
2839}
2840
2841long do_rt_sigreturn(CPUState *regs)
2842{
2843 struct target_rt_sigframe *frame;
2844 abi_ulong frame_addr;
2845 sigset_t blocked;
2846
2847#if defined(DEBUG_SIGNAL)
2848 fprintf(stderr, "do_rt_sigreturn\n");
2849#endif
2850 frame_addr = regs->gregs[15];
2851 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2852 goto badframe;
2853
2854 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
2855 sigprocmask(SIG_SETMASK, &blocked, NULL);
2856
2857 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
2858 goto badframe;
2859
2860 if (do_sigaltstack(frame_addr +
2861 offsetof(struct target_rt_sigframe, uc.uc_stack),
2862 0, get_sp_from_cpustate(regs)) == -EFAULT)
2863 goto badframe;
2864
2865 unlock_user_struct(frame, frame_addr, 0);
2866 return regs->gregs[0];
2867
2868badframe:
2869 unlock_user_struct(frame, frame_addr, 0);
2870 force_sig(TARGET_SIGSEGV);
2871 return 0;
2872}
edgar_iglb6d3abd2008-02-28 11:29:27 +00002873#elif defined(TARGET_CRIS)
2874
2875struct target_sigcontext {
2876 struct target_pt_regs regs; /* needs to be first */
2877 uint32_t oldmask;
2878 uint32_t usp; /* usp before stacking this gunk on it */
2879};
2880
2881/* Signal frames. */
2882struct target_signal_frame {
2883 struct target_sigcontext sc;
2884 uint32_t extramask[TARGET_NSIG_WORDS - 1];
2885 uint8_t retcode[8]; /* Trampoline code. */
2886};
2887
2888struct rt_signal_frame {
2889 struct siginfo *pinfo;
2890 void *puc;
2891 struct siginfo info;
2892 struct ucontext uc;
2893 uint8_t retcode[8]; /* Trampoline code. */
2894};
2895
2896static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
2897{
edgar_igl9664d922008-03-03 22:23:53 +00002898 __put_user(env->regs[0], &sc->regs.r0);
2899 __put_user(env->regs[1], &sc->regs.r1);
2900 __put_user(env->regs[2], &sc->regs.r2);
2901 __put_user(env->regs[3], &sc->regs.r3);
2902 __put_user(env->regs[4], &sc->regs.r4);
2903 __put_user(env->regs[5], &sc->regs.r5);
2904 __put_user(env->regs[6], &sc->regs.r6);
2905 __put_user(env->regs[7], &sc->regs.r7);
2906 __put_user(env->regs[8], &sc->regs.r8);
2907 __put_user(env->regs[9], &sc->regs.r9);
2908 __put_user(env->regs[10], &sc->regs.r10);
2909 __put_user(env->regs[11], &sc->regs.r11);
2910 __put_user(env->regs[12], &sc->regs.r12);
2911 __put_user(env->regs[13], &sc->regs.r13);
2912 __put_user(env->regs[14], &sc->usp);
2913 __put_user(env->regs[15], &sc->regs.acr);
2914 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
2915 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
2916 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002917}
edgar_igl9664d922008-03-03 22:23:53 +00002918
edgar_iglb6d3abd2008-02-28 11:29:27 +00002919static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
2920{
edgar_igl9664d922008-03-03 22:23:53 +00002921 __get_user(env->regs[0], &sc->regs.r0);
2922 __get_user(env->regs[1], &sc->regs.r1);
2923 __get_user(env->regs[2], &sc->regs.r2);
2924 __get_user(env->regs[3], &sc->regs.r3);
2925 __get_user(env->regs[4], &sc->regs.r4);
2926 __get_user(env->regs[5], &sc->regs.r5);
2927 __get_user(env->regs[6], &sc->regs.r6);
2928 __get_user(env->regs[7], &sc->regs.r7);
2929 __get_user(env->regs[8], &sc->regs.r8);
2930 __get_user(env->regs[9], &sc->regs.r9);
2931 __get_user(env->regs[10], &sc->regs.r10);
2932 __get_user(env->regs[11], &sc->regs.r11);
2933 __get_user(env->regs[12], &sc->regs.r12);
2934 __get_user(env->regs[13], &sc->regs.r13);
2935 __get_user(env->regs[14], &sc->usp);
2936 __get_user(env->regs[15], &sc->regs.acr);
2937 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
2938 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
2939 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002940}
2941
edgar_igl9664d922008-03-03 22:23:53 +00002942static abi_ulong get_sigframe(CPUState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00002943{
edgar_igl9664d922008-03-03 22:23:53 +00002944 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002945 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00002946 sp = (env->regs[R_SP] & ~3);
2947 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002948}
2949
pbrook624f7972008-05-31 16:11:38 +00002950static void setup_frame(int sig, struct target_sigaction *ka,
edgar_iglb6d3abd2008-02-28 11:29:27 +00002951 target_sigset_t *set, CPUState *env)
2952{
2953 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00002954 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002955 int err = 0;
2956 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002957
edgar_igl9664d922008-03-03 22:23:53 +00002958 frame_addr = get_sigframe(env, sizeof *frame);
2959 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00002960 goto badframe;
2961
2962 /*
2963 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
2964 * use this trampoline anymore but it sets it up for GDB.
2965 * In QEMU, using the trampoline simplifies things a bit so we use it.
2966 *
2967 * This is movu.w __NR_sigreturn, r9; break 13;
2968 */
2969 err |= __put_user(0x9c5f, frame->retcode+0);
2970 err |= __put_user(TARGET_NR_sigreturn,
2971 frame->retcode+2);
2972 err |= __put_user(0xe93d, frame->retcode+4);
2973
2974 /* Save the mask. */
2975 err |= __put_user(set->sig[0], &frame->sc.oldmask);
2976 if (err)
2977 goto badframe;
2978
2979 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2980 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
2981 goto badframe;
2982 }
2983
2984 setup_sigcontext(&frame->sc, env);
2985
2986 /* Move the stack and setup the arguments for the handler. */
balrog526ccb72008-07-16 12:13:52 +00002987 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002988 env->regs[10] = sig;
pbrook624f7972008-05-31 16:11:38 +00002989 env->pc = (unsigned long) ka->_sa_handler;
edgar_iglb6d3abd2008-02-28 11:29:27 +00002990 /* Link SRP so the guest returns through the trampoline. */
balrog526ccb72008-07-16 12:13:52 +00002991 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
edgar_iglb6d3abd2008-02-28 11:29:27 +00002992
edgar_igl9664d922008-03-03 22:23:53 +00002993 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002994 return;
2995 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00002996 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00002997 force_sig(TARGET_SIGSEGV);
2998}
2999
pbrook624f7972008-05-31 16:11:38 +00003000static void setup_rt_frame(int sig, struct target_sigaction *ka,
edgar_iglb6d3abd2008-02-28 11:29:27 +00003001 target_siginfo_t *info,
3002 target_sigset_t *set, CPUState *env)
3003{
3004 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3005}
3006
3007long do_sigreturn(CPUState *env)
3008{
3009 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003010 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003011 target_sigset_t target_set;
3012 sigset_t set;
3013 int i;
3014
edgar_igl9664d922008-03-03 22:23:53 +00003015 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003016 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003017 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003018 goto badframe;
3019
3020 /* Restore blocked signals */
3021 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3022 goto badframe;
3023 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3024 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3025 goto badframe;
3026 }
3027 target_to_host_sigset_internal(&set, &target_set);
3028 sigprocmask(SIG_SETMASK, &set, NULL);
3029
3030 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003031 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003032 return env->regs[10];
3033 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003034 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003035 force_sig(TARGET_SIGSEGV);
3036}
3037
3038long do_rt_sigreturn(CPUState *env)
3039{
3040 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3041 return -TARGET_ENOSYS;
3042}
thsc3b5bc82007-12-02 06:31:25 +00003043
bellardb346ff42003-06-15 20:05:50 +00003044#else
3045
pbrook624f7972008-05-31 16:11:38 +00003046static void setup_frame(int sig, struct target_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00003047 target_sigset_t *set, CPUState *env)
3048{
3049 fprintf(stderr, "setup_frame: not implemented\n");
3050}
3051
pbrook624f7972008-05-31 16:11:38 +00003052static void setup_rt_frame(int sig, struct target_sigaction *ka,
bellardb346ff42003-06-15 20:05:50 +00003053 target_siginfo_t *info,
3054 target_sigset_t *set, CPUState *env)
3055{
3056 fprintf(stderr, "setup_rt_frame: not implemented\n");
3057}
3058
3059long do_sigreturn(CPUState *env)
3060{
3061 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003062 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003063}
3064
3065long do_rt_sigreturn(CPUState *env)
3066{
3067 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00003068 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00003069}
3070
bellard66fb9762003-03-23 01:06:05 +00003071#endif
3072
pbrook624f7972008-05-31 16:11:38 +00003073void process_pending_signals(CPUState *cpu_env)
bellard66fb9762003-03-23 01:06:05 +00003074{
3075 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00003076 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00003077 sigset_t set, old_set;
3078 target_sigset_t target_old_set;
pbrook624f7972008-05-31 16:11:38 +00003079 struct emulated_sigtable *k;
3080 struct target_sigaction *sa;
bellard66fb9762003-03-23 01:06:05 +00003081 struct sigqueue *q;
pbrook624f7972008-05-31 16:11:38 +00003082 TaskState *ts = cpu_env->opaque;
ths3b46e622007-09-17 08:09:54 +00003083
pbrook624f7972008-05-31 16:11:38 +00003084 if (!ts->signal_pending)
bellard31e31b82003-02-18 22:55:36 +00003085 return;
3086
pbrook624f7972008-05-31 16:11:38 +00003087 /* FIXME: This is not threadsafe. */
3088 k = ts->sigtab;
bellard66fb9762003-03-23 01:06:05 +00003089 for(sig = 1; sig <= TARGET_NSIG; sig++) {
3090 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00003091 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00003092 k++;
bellard31e31b82003-02-18 22:55:36 +00003093 }
3094 /* if no signal is pending, just return */
pbrook624f7972008-05-31 16:11:38 +00003095 ts->signal_pending = 0;
bellard31e31b82003-02-18 22:55:36 +00003096 return;
bellard66fb9762003-03-23 01:06:05 +00003097
bellard31e31b82003-02-18 22:55:36 +00003098 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00003099#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00003100 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00003101#endif
3102 /* dequeue signal */
3103 q = k->first;
3104 k->first = q->next;
3105 if (!k->first)
3106 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00003107
bellard1fddef42005-04-17 19:16:13 +00003108 sig = gdb_handlesig (cpu_env, sig);
3109 if (!sig) {
3110 fprintf (stderr, "Lost signal\n");
3111 abort();
3112 }
bellard66fb9762003-03-23 01:06:05 +00003113
pbrook624f7972008-05-31 16:11:38 +00003114 sa = &sigact_table[sig - 1];
3115 handler = sa->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +00003116 if (handler == TARGET_SIG_DFL) {
3117 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +00003118 if (sig != TARGET_SIGCHLD &&
3119 sig != TARGET_SIGURG &&
bellard66fb9762003-03-23 01:06:05 +00003120 sig != TARGET_SIGWINCH) {
3121 force_sig(sig);
3122 }
3123 } else if (handler == TARGET_SIG_IGN) {
3124 /* ignore sig */
3125 } else if (handler == TARGET_SIG_ERR) {
3126 force_sig(sig);
3127 } else {
bellard9de5e442003-03-23 16:49:39 +00003128 /* compute the blocked signals during the handler execution */
pbrook624f7972008-05-31 16:11:38 +00003129 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +00003130 /* SA_NODEFER indicates that the current signal should not be
3131 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +00003132 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +00003133 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00003134
bellard9de5e442003-03-23 16:49:39 +00003135 /* block signals in the handler using Linux */
3136 sigprocmask(SIG_BLOCK, &set, &old_set);
3137 /* save the previous blocked signal state to restore it at the
3138 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00003139 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00003140
bellardbc8a22c2003-03-30 21:02:40 +00003141 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00003142#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00003143 {
3144 CPUX86State *env = cpu_env;
3145 if (env->eflags & VM_MASK)
3146 save_v86_state(env);
3147 }
3148#endif
bellard9de5e442003-03-23 16:49:39 +00003149 /* prepare the stack frame of the virtual CPU */
pbrook624f7972008-05-31 16:11:38 +00003150 if (sa->sa_flags & TARGET_SA_SIGINFO)
3151 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00003152 else
pbrook624f7972008-05-31 16:11:38 +00003153 setup_frame(sig, sa, &target_old_set, cpu_env);
3154 if (sa->sa_flags & TARGET_SA_RESETHAND)
3155 sa->_sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00003156 }
bellard66fb9762003-03-23 01:06:05 +00003157 if (q != &k->info)
pbrook624f7972008-05-31 16:11:38 +00003158 free_sigqueue(cpu_env, q);
bellard31e31b82003-02-18 22:55:36 +00003159}