blob: 97f30d95479dd09616aa4e3ef5cdf959d295034a [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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard31e31b82003-02-18 22:55:36 +000018 */
19#include <stdlib.h>
20#include <stdio.h>
bellard66fb9762003-03-23 01:06:05 +000021#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000022#include <stdarg.h>
bellard2677e102003-04-10 00:03:27 +000023#include <unistd.h>
bellard66fb9762003-03-23 01:06:05 +000024#include <errno.h>
aurel32603e4fd2009-04-15 16:18:38 +000025#include <assert.h>
bellard31e31b82003-02-18 22:55:36 +000026#include <sys/ucontext.h>
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030027#include <sys/resource.h>
bellard31e31b82003-02-18 22:55:36 +000028
bellard3ef693a2003-03-23 20:17:16 +000029#include "qemu.h"
blueswir17d99a002009-01-14 19:00:36 +000030#include "qemu-common.h"
blueswir1992f48a2007-10-14 16:27:31 +000031#include "target_signal.h"
bellard66fb9762003-03-23 01:06:05 +000032
33//#define DEBUG_SIGNAL
34
blueswir1249c4c32008-10-05 11:09:37 +000035static struct target_sigaltstack target_sigaltstack_used = {
thsa04e1342007-09-27 13:57:58 +000036 .ss_sp = 0,
37 .ss_size = 0,
38 .ss_flags = TARGET_SS_DISABLE,
39};
40
pbrook624f7972008-05-31 16:11:38 +000041static struct target_sigaction sigact_table[TARGET_NSIG];
bellard31e31b82003-02-18 22:55:36 +000042
ths5fafdf22007-09-16 21:08:06 +000043static void host_signal_handler(int host_signum, siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +000044 void *puc);
45
Arnaud Patard3ca05582009-03-30 01:18:20 +020046static uint8_t host_to_target_signal_table[_NSIG] = {
bellard9e5f5282003-07-13 17:33:54 +000047 [SIGHUP] = TARGET_SIGHUP,
48 [SIGINT] = TARGET_SIGINT,
49 [SIGQUIT] = TARGET_SIGQUIT,
50 [SIGILL] = TARGET_SIGILL,
51 [SIGTRAP] = TARGET_SIGTRAP,
52 [SIGABRT] = TARGET_SIGABRT,
bellard01e3b762003-09-30 21:10:14 +000053/* [SIGIOT] = TARGET_SIGIOT,*/
bellard9e5f5282003-07-13 17:33:54 +000054 [SIGBUS] = TARGET_SIGBUS,
55 [SIGFPE] = TARGET_SIGFPE,
56 [SIGKILL] = TARGET_SIGKILL,
57 [SIGUSR1] = TARGET_SIGUSR1,
58 [SIGSEGV] = TARGET_SIGSEGV,
59 [SIGUSR2] = TARGET_SIGUSR2,
60 [SIGPIPE] = TARGET_SIGPIPE,
61 [SIGALRM] = TARGET_SIGALRM,
62 [SIGTERM] = TARGET_SIGTERM,
63#ifdef SIGSTKFLT
64 [SIGSTKFLT] = TARGET_SIGSTKFLT,
65#endif
66 [SIGCHLD] = TARGET_SIGCHLD,
67 [SIGCONT] = TARGET_SIGCONT,
68 [SIGSTOP] = TARGET_SIGSTOP,
69 [SIGTSTP] = TARGET_SIGTSTP,
70 [SIGTTIN] = TARGET_SIGTTIN,
71 [SIGTTOU] = TARGET_SIGTTOU,
72 [SIGURG] = TARGET_SIGURG,
73 [SIGXCPU] = TARGET_SIGXCPU,
74 [SIGXFSZ] = TARGET_SIGXFSZ,
75 [SIGVTALRM] = TARGET_SIGVTALRM,
76 [SIGPROF] = TARGET_SIGPROF,
77 [SIGWINCH] = TARGET_SIGWINCH,
78 [SIGIO] = TARGET_SIGIO,
79 [SIGPWR] = TARGET_SIGPWR,
80 [SIGSYS] = TARGET_SIGSYS,
81 /* next signals stay the same */
pbrook624f7972008-05-31 16:11:38 +000082 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
Dong Xu Wangb4916d72011-11-22 18:06:17 +080083 host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
pbrook624f7972008-05-31 16:11:38 +000084 To fix this properly we need to do manual signal delivery multiplexed
85 over a single host signal. */
86 [__SIGRTMIN] = __SIGRTMAX,
87 [__SIGRTMAX] = __SIGRTMIN,
bellard9e5f5282003-07-13 17:33:54 +000088};
Arnaud Patard3ca05582009-03-30 01:18:20 +020089static uint8_t target_to_host_signal_table[_NSIG];
bellard9e5f5282003-07-13 17:33:54 +000090
thsa04e1342007-09-27 13:57:58 +000091static inline int on_sig_stack(unsigned long sp)
92{
93 return (sp - target_sigaltstack_used.ss_sp
94 < target_sigaltstack_used.ss_size);
95}
96
97static inline int sas_ss_flags(unsigned long sp)
98{
99 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
100 : on_sig_stack(sp) ? SS_ONSTACK : 0);
101}
102
pbrook1d9d8b52009-04-16 15:17:02 +0000103int host_to_target_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +0000104{
Arnaud Patard3ca05582009-03-30 01:18:20 +0200105 if (sig >= _NSIG)
pbrook4cb05962008-05-30 18:05:19 +0000106 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000107 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000108}
109
pbrook4cb05962008-05-30 18:05:19 +0000110int target_to_host_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +0000111{
Arnaud Patard3ca05582009-03-30 01:18:20 +0200112 if (sig >= _NSIG)
pbrook4cb05962008-05-30 18:05:19 +0000113 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000114 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000115}
116
Anthony Liguoric227f092009-10-01 16:12:16 -0500117static inline void target_sigemptyset(target_sigset_t *set)
pbrookf5545b52008-05-30 22:37:07 +0000118{
119 memset(set, 0, sizeof(*set));
120}
121
Anthony Liguoric227f092009-10-01 16:12:16 -0500122static inline void target_sigaddset(target_sigset_t *set, int signum)
pbrookf5545b52008-05-30 22:37:07 +0000123{
124 signum--;
125 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
126 set->sig[signum / TARGET_NSIG_BPW] |= mask;
127}
128
Anthony Liguoric227f092009-10-01 16:12:16 -0500129static inline int target_sigismember(const target_sigset_t *set, int signum)
pbrookf5545b52008-05-30 22:37:07 +0000130{
131 signum--;
132 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
133 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
134}
135
Anthony Liguoric227f092009-10-01 16:12:16 -0500136static void host_to_target_sigset_internal(target_sigset_t *d,
bellard92319442004-06-19 16:58:13 +0000137 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000138{
139 int i;
pbrookf5545b52008-05-30 22:37:07 +0000140 target_sigemptyset(d);
141 for (i = 1; i <= TARGET_NSIG; i++) {
142 if (sigismember(s, i)) {
143 target_sigaddset(d, host_to_target_signal(i));
144 }
bellard9e5f5282003-07-13 17:33:54 +0000145 }
bellard66fb9762003-03-23 01:06:05 +0000146}
147
Anthony Liguoric227f092009-10-01 16:12:16 -0500148void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
bellard92319442004-06-19 16:58:13 +0000149{
Anthony Liguoric227f092009-10-01 16:12:16 -0500150 target_sigset_t d1;
bellard92319442004-06-19 16:58:13 +0000151 int i;
152
153 host_to_target_sigset_internal(&d1, s);
154 for(i = 0;i < TARGET_NSIG_WORDS; i++)
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200155 d->sig[i] = tswapal(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000156}
157
blueswir18fcd3692008-08-17 20:26:25 +0000158static void target_to_host_sigset_internal(sigset_t *d,
Anthony Liguoric227f092009-10-01 16:12:16 -0500159 const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000160{
161 int i;
pbrookf5545b52008-05-30 22:37:07 +0000162 sigemptyset(d);
163 for (i = 1; i <= TARGET_NSIG; i++) {
164 if (target_sigismember(s, i)) {
165 sigaddset(d, target_to_host_signal(i));
166 }
167 }
bellard66fb9762003-03-23 01:06:05 +0000168}
169
Anthony Liguoric227f092009-10-01 16:12:16 -0500170void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
bellard92319442004-06-19 16:58:13 +0000171{
Anthony Liguoric227f092009-10-01 16:12:16 -0500172 target_sigset_t s1;
bellard92319442004-06-19 16:58:13 +0000173 int i;
174
175 for(i = 0;i < TARGET_NSIG_WORDS; i++)
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200176 s1.sig[i] = tswapal(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000177 target_to_host_sigset_internal(d, &s1);
178}
ths3b46e622007-09-17 08:09:54 +0000179
blueswir1992f48a2007-10-14 16:27:31 +0000180void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000181 const sigset_t *sigset)
182{
Anthony Liguoric227f092009-10-01 16:12:16 -0500183 target_sigset_t d;
bellard9e5f5282003-07-13 17:33:54 +0000184 host_to_target_sigset(&d, sigset);
185 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000186}
187
ths5fafdf22007-09-16 21:08:06 +0000188void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000189 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000190{
Anthony Liguoric227f092009-10-01 16:12:16 -0500191 target_sigset_t d;
bellard9e5f5282003-07-13 17:33:54 +0000192 int i;
193
194 d.sig[0] = *old_sigset;
195 for(i = 1;i < TARGET_NSIG_WORDS; i++)
196 d.sig[i] = 0;
197 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000198}
199
bellard9de5e442003-03-23 16:49:39 +0000200/* siginfo conversion */
201
Anthony Liguoric227f092009-10-01 16:12:16 -0500202static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000203 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000204{
bellard9de5e442003-03-23 16:49:39 +0000205 int sig;
206 sig = host_to_target_signal(info->si_signo);
207 tinfo->si_signo = sig;
208 tinfo->si_errno = 0;
pbrookafd7cd92008-05-31 12:14:21 +0000209 tinfo->si_code = info->si_code;
ths5fafdf22007-09-16 21:08:06 +0000210 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000211 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000212 /* should never come here, but who knows. The information for
213 the target is irrelevant */
214 tinfo->_sifields._sigfault._addr = 0;
ths7f7f7c82007-07-12 11:02:46 +0000215 } else if (sig == SIGIO) {
216 tinfo->_sifields._sigpoll._fd = info->si_fd;
bellard9de5e442003-03-23 16:49:39 +0000217 } else if (sig >= TARGET_SIGRTMIN) {
218 tinfo->_sifields._rt._pid = info->si_pid;
219 tinfo->_sifields._rt._uid = info->si_uid;
220 /* XXX: potential problem if 64 bit */
ths5fafdf22007-09-16 21:08:06 +0000221 tinfo->_sifields._rt._sigval.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000222 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000223 }
bellard66fb9762003-03-23 01:06:05 +0000224}
225
Anthony Liguoric227f092009-10-01 16:12:16 -0500226static void tswap_siginfo(target_siginfo_t *tinfo,
227 const target_siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000228{
229 int sig;
230 sig = info->si_signo;
231 tinfo->si_signo = tswap32(sig);
232 tinfo->si_errno = tswap32(info->si_errno);
233 tinfo->si_code = tswap32(info->si_code);
ths5fafdf22007-09-16 21:08:06 +0000234 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000235 sig == SIGBUS || sig == SIGTRAP) {
ths5fafdf22007-09-16 21:08:06 +0000236 tinfo->_sifields._sigfault._addr =
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200237 tswapal(info->_sifields._sigfault._addr);
ths7f7f7c82007-07-12 11:02:46 +0000238 } else if (sig == SIGIO) {
239 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
bellard9de5e442003-03-23 16:49:39 +0000240 } else if (sig >= TARGET_SIGRTMIN) {
241 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
242 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000243 tinfo->_sifields._rt._sigval.sival_ptr =
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200244 tswapal(info->_sifields._rt._sigval.sival_ptr);
bellard9de5e442003-03-23 16:49:39 +0000245 }
246}
247
248
Anthony Liguoric227f092009-10-01 16:12:16 -0500249void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000250{
251 host_to_target_siginfo_noswap(tinfo, info);
252 tswap_siginfo(tinfo, tinfo);
253}
254
255/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000256/* XXX: find a solution for 64 bit (additional malloced data is needed) */
Anthony Liguoric227f092009-10-01 16:12:16 -0500257void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000258{
259 info->si_signo = tswap32(tinfo->si_signo);
260 info->si_errno = tswap32(tinfo->si_errno);
261 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000262 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
263 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000264 info->si_value.sival_ptr =
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200265 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000266}
267
aurel32ca587a82008-12-18 22:44:13 +0000268static int fatal_signal (int sig)
269{
270 switch (sig) {
271 case TARGET_SIGCHLD:
272 case TARGET_SIGURG:
273 case TARGET_SIGWINCH:
274 /* Ignored by default. */
275 return 0;
276 case TARGET_SIGCONT:
277 case TARGET_SIGSTOP:
278 case TARGET_SIGTSTP:
279 case TARGET_SIGTTIN:
280 case TARGET_SIGTTOU:
281 /* Job control signals. */
282 return 0;
283 default:
284 return 1;
285 }
286}
287
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300288/* returns 1 if given signal should dump core if not handled */
289static int core_dump_signal(int sig)
290{
291 switch (sig) {
292 case TARGET_SIGABRT:
293 case TARGET_SIGFPE:
294 case TARGET_SIGILL:
295 case TARGET_SIGQUIT:
296 case TARGET_SIGSEGV:
297 case TARGET_SIGTRAP:
298 case TARGET_SIGBUS:
299 return (1);
300 default:
301 return (0);
302 }
303}
304
bellard31e31b82003-02-18 22:55:36 +0000305void signal_init(void)
306{
307 struct sigaction act;
pbrook624f7972008-05-31 16:11:38 +0000308 struct sigaction oact;
bellard9e5f5282003-07-13 17:33:54 +0000309 int i, j;
pbrook624f7972008-05-31 16:11:38 +0000310 int host_sig;
bellard31e31b82003-02-18 22:55:36 +0000311
bellard9e5f5282003-07-13 17:33:54 +0000312 /* generate signal conversion tables */
Arnaud Patard3ca05582009-03-30 01:18:20 +0200313 for(i = 1; i < _NSIG; i++) {
bellard9e5f5282003-07-13 17:33:54 +0000314 if (host_to_target_signal_table[i] == 0)
315 host_to_target_signal_table[i] = i;
316 }
Arnaud Patard3ca05582009-03-30 01:18:20 +0200317 for(i = 1; i < _NSIG; i++) {
bellard9e5f5282003-07-13 17:33:54 +0000318 j = host_to_target_signal_table[i];
319 target_to_host_signal_table[j] = i;
320 }
ths3b46e622007-09-17 08:09:54 +0000321
bellard9de5e442003-03-23 16:49:39 +0000322 /* set all host signal handlers. ALL signals are blocked during
323 the handlers to serialize them. */
pbrook624f7972008-05-31 16:11:38 +0000324 memset(sigact_table, 0, sizeof(sigact_table));
325
bellard9de5e442003-03-23 16:49:39 +0000326 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000327 act.sa_flags = SA_SIGINFO;
328 act.sa_sigaction = host_signal_handler;
pbrook624f7972008-05-31 16:11:38 +0000329 for(i = 1; i <= TARGET_NSIG; i++) {
330 host_sig = target_to_host_signal(i);
331 sigaction(host_sig, NULL, &oact);
332 if (oact.sa_sigaction == (void *)SIG_IGN) {
333 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
334 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
335 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
336 }
337 /* If there's already a handler installed then something has
338 gone horribly wrong, so don't even try to handle that case. */
aurel32ca587a82008-12-18 22:44:13 +0000339 /* Install some handlers for our own use. We need at least
340 SIGSEGV and SIGBUS, to detect exceptions. We can not just
341 trap all signals because it affects syscall interrupt
342 behavior. But do trap all default-fatal signals. */
343 if (fatal_signal (i))
pbrook624f7972008-05-31 16:11:38 +0000344 sigaction(host_sig, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000345 }
bellard31e31b82003-02-18 22:55:36 +0000346}
347
bellard66fb9762003-03-23 01:06:05 +0000348/* signal queue handling */
349
Andreas Färber9349b4f2012-03-14 01:38:32 +0100350static inline struct sigqueue *alloc_sigqueue(CPUArchState *env)
bellard66fb9762003-03-23 01:06:05 +0000351{
pbrook624f7972008-05-31 16:11:38 +0000352 TaskState *ts = env->opaque;
353 struct sigqueue *q = ts->first_free;
bellard66fb9762003-03-23 01:06:05 +0000354 if (!q)
355 return NULL;
pbrook624f7972008-05-31 16:11:38 +0000356 ts->first_free = q->next;
bellard66fb9762003-03-23 01:06:05 +0000357 return q;
358}
359
Andreas Färber9349b4f2012-03-14 01:38:32 +0100360static inline void free_sigqueue(CPUArchState *env, struct sigqueue *q)
bellard66fb9762003-03-23 01:06:05 +0000361{
pbrook624f7972008-05-31 16:11:38 +0000362 TaskState *ts = env->opaque;
363 q->next = ts->first_free;
364 ts->first_free = q;
bellard66fb9762003-03-23 01:06:05 +0000365}
366
bellard9de5e442003-03-23 16:49:39 +0000367/* abort execution with signal */
Riku Voipio66393fb2009-12-04 15:16:32 +0200368static void QEMU_NORETURN force_sig(int target_sig)
bellard66fb9762003-03-23 01:06:05 +0000369{
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300370 TaskState *ts = (TaskState *)thread_env->opaque;
371 int host_sig, core_dumped = 0;
aurel32603e4fd2009-04-15 16:18:38 +0000372 struct sigaction act;
Riku Voipio66393fb2009-12-04 15:16:32 +0200373 host_sig = target_to_host_signal(target_sig);
374 gdb_signalled(thread_env, target_sig);
aurel32603e4fd2009-04-15 16:18:38 +0000375
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300376 /* dump core if supported by target binary format */
Riku Voipio66393fb2009-12-04 15:16:32 +0200377 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300378 stop_all_tasks();
379 core_dumped =
Riku Voipio66393fb2009-12-04 15:16:32 +0200380 ((*ts->bprm->core_dump)(target_sig, thread_env) == 0);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300381 }
382 if (core_dumped) {
383 /* we already dumped the core of target process, we don't want
384 * a coredump of qemu itself */
385 struct rlimit nodump;
386 getrlimit(RLIMIT_CORE, &nodump);
387 nodump.rlim_cur=0;
388 setrlimit(RLIMIT_CORE, &nodump);
389 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
Riku Voipio66393fb2009-12-04 15:16:32 +0200390 target_sig, strsignal(host_sig), "core dumped" );
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300391 }
392
Stefan Weil0c587512011-04-28 17:20:32 +0200393 /* The proper exit code for dying from an uncaught signal is
aurel32603e4fd2009-04-15 16:18:38 +0000394 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
395 * a negative value. To get the proper exit code we need to
396 * actually die from an uncaught signal. Here the default signal
397 * handler is installed, we send ourself a signal and we wait for
398 * it to arrive. */
399 sigfillset(&act.sa_mask);
400 act.sa_handler = SIG_DFL;
401 sigaction(host_sig, &act, NULL);
402
403 /* For some reason raise(host_sig) doesn't send the signal when
404 * statically linked on x86-64. */
405 kill(getpid(), host_sig);
406
407 /* Make sure the signal isn't masked (just reuse the mask inside
408 of act) */
409 sigdelset(&act.sa_mask, host_sig);
410 sigsuspend(&act.sa_mask);
411
412 /* unreachable */
Blue Swirla6c6f762010-03-13 14:18:50 +0000413 abort();
bellard66fb9762003-03-23 01:06:05 +0000414}
415
bellard9de5e442003-03-23 16:49:39 +0000416/* queue a signal so that it will be send to the virtual CPU as soon
417 as possible */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100418int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000419{
pbrook624f7972008-05-31 16:11:38 +0000420 TaskState *ts = env->opaque;
421 struct emulated_sigtable *k;
bellard9de5e442003-03-23 16:49:39 +0000422 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000423 abi_ulong handler;
aurel32ca587a82008-12-18 22:44:13 +0000424 int queue;
bellard66fb9762003-03-23 01:06:05 +0000425
bellard9de5e442003-03-23 16:49:39 +0000426#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000427 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000428 sig);
bellard66fb9762003-03-23 01:06:05 +0000429#endif
pbrook624f7972008-05-31 16:11:38 +0000430 k = &ts->sigtab[sig - 1];
aurel32ca587a82008-12-18 22:44:13 +0000431 queue = gdb_queuesig ();
pbrook624f7972008-05-31 16:11:38 +0000432 handler = sigact_table[sig - 1]._sa_handler;
aurel32ca587a82008-12-18 22:44:13 +0000433 if (!queue && handler == TARGET_SIG_DFL) {
ths60b19692008-11-27 15:47:15 +0000434 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
435 kill(getpid(),SIGSTOP);
436 return 0;
437 } else
bellard66fb9762003-03-23 01:06:05 +0000438 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000439 if (sig != TARGET_SIGCHLD &&
440 sig != TARGET_SIGURG &&
ths60b19692008-11-27 15:47:15 +0000441 sig != TARGET_SIGWINCH &&
442 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +0000443 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000444 } else {
445 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000446 }
aurel32ca587a82008-12-18 22:44:13 +0000447 } else if (!queue && handler == TARGET_SIG_IGN) {
bellard66fb9762003-03-23 01:06:05 +0000448 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000449 return 0;
aurel32ca587a82008-12-18 22:44:13 +0000450 } else if (!queue && handler == TARGET_SIG_ERR) {
bellard66fb9762003-03-23 01:06:05 +0000451 force_sig(sig);
452 } else {
bellard9de5e442003-03-23 16:49:39 +0000453 pq = &k->first;
454 if (sig < TARGET_SIGRTMIN) {
455 /* if non real time signal, we queue exactly one signal */
456 if (!k->pending)
457 q = &k->info;
458 else
459 return 0;
460 } else {
461 if (!k->pending) {
462 /* first signal */
463 q = &k->info;
464 } else {
pbrook624f7972008-05-31 16:11:38 +0000465 q = alloc_sigqueue(env);
bellard9de5e442003-03-23 16:49:39 +0000466 if (!q)
467 return -EAGAIN;
468 while (*pq != NULL)
469 pq = &(*pq)->next;
470 }
471 }
472 *pq = q;
473 q->info = *info;
474 q->next = NULL;
475 k->pending = 1;
476 /* signal that a new signal is pending */
pbrook624f7972008-05-31 16:11:38 +0000477 ts->signal_pending = 1;
bellard9de5e442003-03-23 16:49:39 +0000478 return 1; /* indicates that the signal was queued */
479 }
480}
481
ths5fafdf22007-09-16 21:08:06 +0000482static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000483 void *puc)
484{
485 int sig;
Anthony Liguoric227f092009-10-01 16:12:16 -0500486 target_siginfo_t tinfo;
bellard9de5e442003-03-23 16:49:39 +0000487
488 /* the CPU emulator uses some host signals to detect exceptions,
aurel32eaa449b2009-01-03 13:14:52 +0000489 we forward to it some signals */
aurel32ca587a82008-12-18 22:44:13 +0000490 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
aurel32eaa449b2009-01-03 13:14:52 +0000491 && info->si_code > 0) {
bellardb346ff42003-06-15 20:05:50 +0000492 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000493 return;
494 }
495
496 /* get target signal number */
497 sig = host_to_target_signal(host_signum);
498 if (sig < 1 || sig > TARGET_NSIG)
499 return;
500#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000501 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000502#endif
503 host_to_target_siginfo_noswap(&tinfo, info);
pbrookd5975362008-06-07 20:50:51 +0000504 if (queue_signal(thread_env, sig, &tinfo) == 1) {
bellard9de5e442003-03-23 16:49:39 +0000505 /* interrupt the virtual CPU as soon as possible */
aurel323098dba2009-03-07 21:28:24 +0000506 cpu_exit(thread_env);
bellard66fb9762003-03-23 01:06:05 +0000507 }
bellard31e31b82003-02-18 22:55:36 +0000508}
509
ths0da46a62007-10-20 20:23:07 +0000510/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000511/* compare linux/kernel/signal.c:do_sigaltstack() */
512abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000513{
514 int ret;
515 struct target_sigaltstack oss;
516
517 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000518 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000519 {
520 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
521 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
522 __put_user(sas_ss_flags(sp), &oss.ss_flags);
523 }
524
bellard579a97f2007-11-11 14:26:47 +0000525 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000526 {
bellard579a97f2007-11-11 14:26:47 +0000527 struct target_sigaltstack *uss;
528 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000529
ths0da46a62007-10-20 20:23:07 +0000530 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000531 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000532 || __get_user(ss.ss_sp, &uss->ss_sp)
533 || __get_user(ss.ss_size, &uss->ss_size)
534 || __get_user(ss.ss_flags, &uss->ss_flags))
535 goto out;
bellard579a97f2007-11-11 14:26:47 +0000536 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000537
ths0da46a62007-10-20 20:23:07 +0000538 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000539 if (on_sig_stack(sp))
540 goto out;
541
ths0da46a62007-10-20 20:23:07 +0000542 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000543 if (ss.ss_flags != TARGET_SS_DISABLE
544 && ss.ss_flags != TARGET_SS_ONSTACK
545 && ss.ss_flags != 0)
546 goto out;
547
548 if (ss.ss_flags == TARGET_SS_DISABLE) {
549 ss.ss_size = 0;
550 ss.ss_sp = 0;
551 } else {
ths0da46a62007-10-20 20:23:07 +0000552 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000553 if (ss.ss_size < MINSIGSTKSZ)
554 goto out;
555 }
556
557 target_sigaltstack_used.ss_sp = ss.ss_sp;
558 target_sigaltstack_used.ss_size = ss.ss_size;
559 }
560
bellard579a97f2007-11-11 14:26:47 +0000561 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000562 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000563 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000564 goto out;
thsa04e1342007-09-27 13:57:58 +0000565 }
566
567 ret = 0;
568out:
569 return ret;
570}
571
ths0da46a62007-10-20 20:23:07 +0000572/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000573int do_sigaction(int sig, const struct target_sigaction *act,
574 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000575{
pbrook624f7972008-05-31 16:11:38 +0000576 struct target_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000577 struct sigaction act1;
578 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000579 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000580
ths2a913eb2008-11-27 15:46:25 +0000581 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000582 return -EINVAL;
583 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000584#if defined(DEBUG_SIGNAL)
Blue Swirl0bf9e312009-07-20 17:19:25 +0000585 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
586 sig, act, oact);
bellard66fb9762003-03-23 01:06:05 +0000587#endif
588 if (oact) {
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200589 oact->_sa_handler = tswapal(k->_sa_handler);
Andreas Färberf78b0f02011-12-29 16:55:43 +0100590#if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
591 oact->sa_flags = bswap32(k->sa_flags);
592#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200593 oact->sa_flags = tswapal(k->sa_flags);
Andreas Färberf78b0f02011-12-29 16:55:43 +0100594#endif
ths388bb212007-05-13 13:58:00 +0000595#if !defined(TARGET_MIPS)
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200596 oact->sa_restorer = tswapal(k->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000597#endif
pbrook624f7972008-05-31 16:11:38 +0000598 oact->sa_mask = k->sa_mask;
bellard66fb9762003-03-23 01:06:05 +0000599 }
600 if (act) {
pbrook624f7972008-05-31 16:11:38 +0000601 /* FIXME: This is not threadsafe. */
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200602 k->_sa_handler = tswapal(act->_sa_handler);
Andreas Färberf78b0f02011-12-29 16:55:43 +0100603#if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
604 k->sa_flags = bswap32(act->sa_flags);
605#else
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200606 k->sa_flags = tswapal(act->sa_flags);
Andreas Färberf78b0f02011-12-29 16:55:43 +0100607#endif
ths388bb212007-05-13 13:58:00 +0000608#if !defined(TARGET_MIPS)
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200609 k->sa_restorer = tswapal(act->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000610#endif
pbrook624f7972008-05-31 16:11:38 +0000611 k->sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000612
613 /* we update the host linux signal state */
614 host_sig = target_to_host_signal(sig);
615 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
616 sigfillset(&act1.sa_mask);
617 act1.sa_flags = SA_SIGINFO;
pbrook624f7972008-05-31 16:11:38 +0000618 if (k->sa_flags & TARGET_SA_RESTART)
bellard773b93e2004-01-04 17:15:59 +0000619 act1.sa_flags |= SA_RESTART;
620 /* NOTE: it is important to update the host kernel signal
621 ignore state to avoid getting unexpected interrupted
622 syscalls */
pbrook624f7972008-05-31 16:11:38 +0000623 if (k->_sa_handler == TARGET_SIG_IGN) {
bellard773b93e2004-01-04 17:15:59 +0000624 act1.sa_sigaction = (void *)SIG_IGN;
pbrook624f7972008-05-31 16:11:38 +0000625 } else if (k->_sa_handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +0000626 if (fatal_signal (sig))
627 act1.sa_sigaction = host_signal_handler;
628 else
629 act1.sa_sigaction = (void *)SIG_DFL;
bellard773b93e2004-01-04 17:15:59 +0000630 } else {
631 act1.sa_sigaction = host_signal_handler;
632 }
ths0da46a62007-10-20 20:23:07 +0000633 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000634 }
bellard66fb9762003-03-23 01:06:05 +0000635 }
ths0da46a62007-10-20 20:23:07 +0000636 return ret;
bellard66fb9762003-03-23 01:06:05 +0000637}
bellard31e31b82003-02-18 22:55:36 +0000638
Anthony Liguoric227f092009-10-01 16:12:16 -0500639static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
640 const target_siginfo_t *info)
bellard43fff232003-07-09 19:31:39 +0000641{
642 tswap_siginfo(tinfo, info);
643 return 0;
644}
645
thsc3b5bc82007-12-02 06:31:25 +0000646static inline int current_exec_domain_sig(int sig)
647{
648 return /* current->exec_domain && current->exec_domain->signal_invmap
649 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
650}
651
bellard459a4012007-11-11 19:45:10 +0000652#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000653
654/* from the Linux kernel */
655
656struct target_fpreg {
657 uint16_t significand[4];
658 uint16_t exponent;
659};
660
661struct target_fpxreg {
662 uint16_t significand[4];
663 uint16_t exponent;
664 uint16_t padding[3];
665};
666
667struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000668 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000669};
670
671struct target_fpstate {
672 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000673 abi_ulong cw;
674 abi_ulong sw;
675 abi_ulong tag;
676 abi_ulong ipoff;
677 abi_ulong cssel;
678 abi_ulong dataoff;
679 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000680 struct target_fpreg _st[8];
681 uint16_t status;
682 uint16_t magic; /* 0xffff = regular FPU data only */
683
684 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000685 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
686 abi_ulong mxcsr;
687 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000688 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
689 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000690 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000691};
692
693#define X86_FXSR_MAGIC 0x0000
694
695struct target_sigcontext {
696 uint16_t gs, __gsh;
697 uint16_t fs, __fsh;
698 uint16_t es, __esh;
699 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000700 abi_ulong edi;
701 abi_ulong esi;
702 abi_ulong ebp;
703 abi_ulong esp;
704 abi_ulong ebx;
705 abi_ulong edx;
706 abi_ulong ecx;
707 abi_ulong eax;
708 abi_ulong trapno;
709 abi_ulong err;
710 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000711 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000712 abi_ulong eflags;
713 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000714 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000715 abi_ulong fpstate; /* pointer */
716 abi_ulong oldmask;
717 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000718};
719
bellard66fb9762003-03-23 01:06:05 +0000720struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000721 abi_ulong tuc_flags;
722 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -0500723 target_stack_t tuc_stack;
bellardb8076a72005-04-07 22:20:31 +0000724 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -0500725 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000726};
727
728struct sigframe
729{
blueswir1992f48a2007-10-14 16:27:31 +0000730 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000731 int sig;
732 struct target_sigcontext sc;
733 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000734 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000735 char retcode[8];
736};
737
738struct rt_sigframe
739{
blueswir1992f48a2007-10-14 16:27:31 +0000740 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000741 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000742 abi_ulong pinfo;
743 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000744 struct target_siginfo info;
745 struct target_ucontext uc;
746 struct target_fpstate fpstate;
747 char retcode[8];
748};
749
750/*
751 * Set up a signal frame.
752 */
753
bellard66fb9762003-03-23 01:06:05 +0000754/* XXX: save x87 state */
755static int
756setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
bellard28be6232007-11-11 22:23:38 +0000757 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000758{
759 int err = 0;
bellard775b58d2007-11-11 16:22:17 +0000760 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000761
bellard579a97f2007-11-11 14:26:47 +0000762 /* already locked in setup_frame() */
bellarda52c7572003-06-21 13:14:12 +0000763 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
764 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
765 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
766 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000767 err |= __put_user(env->regs[R_EDI], &sc->edi);
768 err |= __put_user(env->regs[R_ESI], &sc->esi);
769 err |= __put_user(env->regs[R_EBP], &sc->ebp);
770 err |= __put_user(env->regs[R_ESP], &sc->esp);
771 err |= __put_user(env->regs[R_EBX], &sc->ebx);
772 err |= __put_user(env->regs[R_EDX], &sc->edx);
773 err |= __put_user(env->regs[R_ECX], &sc->ecx);
774 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000775 err |= __put_user(env->exception_index, &sc->trapno);
776 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000777 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000778 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000779 err |= __put_user(env->eflags, &sc->eflags);
780 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000781 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000782
bellard28be6232007-11-11 22:23:38 +0000783 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000784 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000785 magic = 0xffff;
786 err |= __put_user(magic, &fpstate->magic);
bellard28be6232007-11-11 22:23:38 +0000787 err |= __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000788
bellard66fb9762003-03-23 01:06:05 +0000789 /* non-iBCS2 extensions.. */
790 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000791 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000792 return err;
793}
794
795/*
796 * Determine which stack to use..
797 */
798
bellard579a97f2007-11-11 14:26:47 +0000799static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +0000800get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
bellard66fb9762003-03-23 01:06:05 +0000801{
802 unsigned long esp;
803
804 /* Default to using normal stack */
805 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000806 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +0000807 if (ka->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +0000808 if (sas_ss_flags(esp) == 0)
809 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
810 }
bellard66fb9762003-03-23 01:06:05 +0000811
812 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000813 else
bellarda52c7572003-06-21 13:14:12 +0000814 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
pbrook624f7972008-05-31 16:11:38 +0000815 !(ka->sa_flags & TARGET_SA_RESTORER) &&
816 ka->sa_restorer) {
817 esp = (unsigned long) ka->sa_restorer;
bellarda52c7572003-06-21 13:14:12 +0000818 }
bellard579a97f2007-11-11 14:26:47 +0000819 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000820}
821
bellard579a97f2007-11-11 14:26:47 +0000822/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +0000823static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500824 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000825{
bellard579a97f2007-11-11 14:26:47 +0000826 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000827 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000828 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000829
bellard579a97f2007-11-11 14:26:47 +0000830 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000831
bellard579a97f2007-11-11 14:26:47 +0000832 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000833 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000834
thsc3b5bc82007-12-02 06:31:25 +0000835 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000836 &frame->sig);
837 if (err)
838 goto give_sigsegv;
839
bellard28be6232007-11-11 22:23:38 +0000840 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
841 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000842 if (err)
843 goto give_sigsegv;
844
bellard92319442004-06-19 16:58:13 +0000845 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
846 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
847 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 abi_ulong retcode_addr;
857 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
858 err |= __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000859 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000860 val16 = 0xb858;
861 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000862 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000863 val16 = 0x80cd;
864 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000865 }
866
867 if (err)
868 goto give_sigsegv;
869
870 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000871 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000872 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000873
874 cpu_x86_load_seg(env, R_DS, __USER_DS);
875 cpu_x86_load_seg(env, R_ES, __USER_DS);
876 cpu_x86_load_seg(env, R_SS, __USER_DS);
877 cpu_x86_load_seg(env, R_CS, __USER_CS);
878 env->eflags &= ~TF_MASK;
879
bellard579a97f2007-11-11 14:26:47 +0000880 unlock_user_struct(frame, frame_addr, 1);
881
bellard66fb9762003-03-23 01:06:05 +0000882 return;
883
884give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000885 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000886 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000887 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000888 force_sig(TARGET_SIGSEGV /* , current */);
889}
890
bellard579a97f2007-11-11 14:26:47 +0000891/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +0000892static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500893 target_siginfo_t *info,
894 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000895{
bellard28be6232007-11-11 22:23:38 +0000896 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000897 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000898 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000899
bellard579a97f2007-11-11 14:26:47 +0000900 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000901
bellard579a97f2007-11-11 14:26:47 +0000902 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000903 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000904
thsc3b5bc82007-12-02 06:31:25 +0000905 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000906 &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000907 addr = frame_addr + offsetof(struct rt_sigframe, info);
908 err |= __put_user(addr, &frame->pinfo);
909 addr = frame_addr + offsetof(struct rt_sigframe, uc);
910 err |= __put_user(addr, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000911 err |= copy_siginfo_to_user(&frame->info, info);
912 if (err)
913 goto give_sigsegv;
914
915 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000916 err |= __put_user(0, &frame->uc.tuc_flags);
917 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000918 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000919 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000920 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000921 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000922 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000923 &frame->uc.tuc_stack.ss_size);
924 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000925 env, set->sig[0],
926 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +0000927 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000928 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000929 goto give_sigsegv;
930 }
bellard66fb9762003-03-23 01:06:05 +0000931
932 /* Set up to return from userspace. If provided, use a stub
933 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000934 if (ka->sa_flags & TARGET_SA_RESTORER) {
935 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000936 } else {
bellard775b58d2007-11-11 16:22:17 +0000937 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000938 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
939 err |= __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000940 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000941 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000942 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000943 val16 = 0x80cd;
944 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000945 }
946
947 if (err)
948 goto give_sigsegv;
949
950 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000951 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000952 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000953
954 cpu_x86_load_seg(env, R_DS, __USER_DS);
955 cpu_x86_load_seg(env, R_ES, __USER_DS);
956 cpu_x86_load_seg(env, R_SS, __USER_DS);
957 cpu_x86_load_seg(env, R_CS, __USER_CS);
958 env->eflags &= ~TF_MASK;
959
bellard579a97f2007-11-11 14:26:47 +0000960 unlock_user_struct(frame, frame_addr, 1);
961
bellard66fb9762003-03-23 01:06:05 +0000962 return;
963
964give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000965 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000966 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000967 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000968 force_sig(TARGET_SIGSEGV /* , current */);
969}
970
971static int
972restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
973{
974 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +0000975 abi_ulong fpstate_addr;
976 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +0000977
bellard28be6232007-11-11 22:23:38 +0000978 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
979 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
980 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
981 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +0000982
bellard28be6232007-11-11 22:23:38 +0000983 env->regs[R_EDI] = tswapl(sc->edi);
984 env->regs[R_ESI] = tswapl(sc->esi);
985 env->regs[R_EBP] = tswapl(sc->ebp);
986 env->regs[R_ESP] = tswapl(sc->esp);
987 env->regs[R_EBX] = tswapl(sc->ebx);
988 env->regs[R_EDX] = tswapl(sc->edx);
989 env->regs[R_ECX] = tswapl(sc->ecx);
990 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +0000991
Mike McCormack9a826d72011-06-01 15:14:37 +0900992 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
993 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000994
bellard28be6232007-11-11 22:23:38 +0000995 tmpflags = tswapl(sc->eflags);
996 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
997 // regs->orig_eax = -1; /* disable syscall checks */
998
999 fpstate_addr = tswapl(sc->fpstate);
1000 if (fpstate_addr != 0) {
1001 if (!access_ok(VERIFY_READ, fpstate_addr,
1002 sizeof(struct target_fpstate)))
1003 goto badframe;
1004 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +00001005 }
1006
bellard28be6232007-11-11 22:23:38 +00001007 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +00001008 return err;
bellard66fb9762003-03-23 01:06:05 +00001009badframe:
1010 return 1;
bellard66fb9762003-03-23 01:06:05 +00001011}
1012
1013long do_sigreturn(CPUX86State *env)
1014{
bellard579a97f2007-11-11 14:26:47 +00001015 struct sigframe *frame;
1016 abi_ulong frame_addr = env->regs[R_ESP] - 8;
Anthony Liguoric227f092009-10-01 16:12:16 -05001017 target_sigset_t target_set;
bellard66fb9762003-03-23 01:06:05 +00001018 sigset_t set;
1019 int eax, i;
1020
bellard447db212003-05-10 15:10:36 +00001021#if defined(DEBUG_SIGNAL)
1022 fprintf(stderr, "do_sigreturn\n");
1023#endif
bellard579a97f2007-11-11 14:26:47 +00001024 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1025 goto badframe;
bellard66fb9762003-03-23 01:06:05 +00001026 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +00001027 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1028 goto badframe;
1029 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1030 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1031 goto badframe;
1032 }
bellard66fb9762003-03-23 01:06:05 +00001033
bellard92319442004-06-19 16:58:13 +00001034 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +00001035 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +00001036
bellard66fb9762003-03-23 01:06:05 +00001037 /* restore registers */
1038 if (restore_sigcontext(env, &frame->sc, &eax))
1039 goto badframe;
bellard579a97f2007-11-11 14:26:47 +00001040 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001041 return eax;
1042
1043badframe:
bellard579a97f2007-11-11 14:26:47 +00001044 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001045 force_sig(TARGET_SIGSEGV);
1046 return 0;
1047}
1048
1049long do_rt_sigreturn(CPUX86State *env)
1050{
bellard28be6232007-11-11 22:23:38 +00001051 abi_ulong frame_addr;
1052 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +00001053 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +00001054 int eax;
1055
bellard28be6232007-11-11 22:23:38 +00001056 frame_addr = env->regs[R_ESP] - 4;
1057 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1058 goto badframe;
bellardb8076a72005-04-07 22:20:31 +00001059 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +00001060 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +00001061
bellardb8076a72005-04-07 22:20:31 +00001062 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +00001063 goto badframe;
1064
bellard28be6232007-11-11 22:23:38 +00001065 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1066 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +00001067 goto badframe;
thsa04e1342007-09-27 13:57:58 +00001068
bellard28be6232007-11-11 22:23:38 +00001069 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001070 return eax;
1071
1072badframe:
bellard28be6232007-11-11 22:23:38 +00001073 unlock_user_struct(frame, frame_addr, 0);
1074 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +00001075 return 0;
1076}
1077
bellard43fff232003-07-09 19:31:39 +00001078#elif defined(TARGET_ARM)
1079
1080struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001081 abi_ulong trap_no;
1082 abi_ulong error_code;
1083 abi_ulong oldmask;
1084 abi_ulong arm_r0;
1085 abi_ulong arm_r1;
1086 abi_ulong arm_r2;
1087 abi_ulong arm_r3;
1088 abi_ulong arm_r4;
1089 abi_ulong arm_r5;
1090 abi_ulong arm_r6;
1091 abi_ulong arm_r7;
1092 abi_ulong arm_r8;
1093 abi_ulong arm_r9;
1094 abi_ulong arm_r10;
1095 abi_ulong arm_fp;
1096 abi_ulong arm_ip;
1097 abi_ulong arm_sp;
1098 abi_ulong arm_lr;
1099 abi_ulong arm_pc;
1100 abi_ulong arm_cpsr;
1101 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001102};
1103
pbrooka745ec62008-05-06 15:36:17 +00001104struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001105 abi_ulong tuc_flags;
1106 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001107 target_stack_t tuc_stack;
bellardb8076a72005-04-07 22:20:31 +00001108 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001109 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001110};
1111
pbrooka745ec62008-05-06 15:36:17 +00001112struct target_ucontext_v2 {
1113 abi_ulong tuc_flags;
1114 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001115 target_stack_t tuc_stack;
pbrooka745ec62008-05-06 15:36:17 +00001116 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001117 target_sigset_t tuc_sigmask; /* mask last for extensibility */
Peter Maydell5f0b7c82010-11-24 15:20:03 +00001118 char __unused[128 - sizeof(target_sigset_t)];
pbrooka745ec62008-05-06 15:36:17 +00001119 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1120};
1121
Peter Maydell0d871bd2010-11-24 15:20:05 +00001122struct target_user_vfp {
1123 uint64_t fpregs[32];
1124 abi_ulong fpscr;
1125};
1126
1127struct target_user_vfp_exc {
1128 abi_ulong fpexc;
1129 abi_ulong fpinst;
1130 abi_ulong fpinst2;
1131};
1132
1133struct target_vfp_sigframe {
1134 abi_ulong magic;
1135 abi_ulong size;
1136 struct target_user_vfp ufp;
1137 struct target_user_vfp_exc ufp_exc;
1138} __attribute__((__aligned__(8)));
1139
Peter Maydell08e11252010-11-24 15:20:07 +00001140struct target_iwmmxt_sigframe {
1141 abi_ulong magic;
1142 abi_ulong size;
1143 uint64_t regs[16];
1144 /* Note that not all the coprocessor control registers are stored here */
1145 uint32_t wcssf;
1146 uint32_t wcasf;
1147 uint32_t wcgr0;
1148 uint32_t wcgr1;
1149 uint32_t wcgr2;
1150 uint32_t wcgr3;
1151} __attribute__((__aligned__(8)));
1152
Peter Maydell0d871bd2010-11-24 15:20:05 +00001153#define TARGET_VFP_MAGIC 0x56465001
Peter Maydell08e11252010-11-24 15:20:07 +00001154#define TARGET_IWMMXT_MAGIC 0x12ef842a
Peter Maydell0d871bd2010-11-24 15:20:05 +00001155
pbrooka8c33202008-05-07 23:22:46 +00001156struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001157{
1158 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001159 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1160 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001161};
1162
pbrooka8c33202008-05-07 23:22:46 +00001163struct sigframe_v2
1164{
1165 struct target_ucontext_v2 uc;
1166 abi_ulong retcode;
1167};
1168
pbrooka745ec62008-05-06 15:36:17 +00001169struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001170{
bellardf8b0aa22007-11-11 23:03:42 +00001171 abi_ulong pinfo;
1172 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001173 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001174 struct target_ucontext_v1 uc;
1175 abi_ulong retcode;
1176};
1177
1178struct rt_sigframe_v2
1179{
1180 struct target_siginfo info;
1181 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001182 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001183};
1184
1185#define TARGET_CONFIG_CPU_32 1
1186
1187/*
1188 * For ARM syscalls, we encode the syscall number into the instruction.
1189 */
1190#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1191#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1192
1193/*
1194 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1195 * need two 16-bit instructions.
1196 */
1197#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1198#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1199
blueswir1992f48a2007-10-14 16:27:31 +00001200static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001201 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1202 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1203};
1204
1205
bellard43fff232003-07-09 19:31:39 +00001206#define __get_user_error(x,p,e) __get_user(x, p)
1207
Andreas Färber05390242012-02-25 03:37:53 +01001208static inline int valid_user_regs(CPUARMState *regs)
bellard43fff232003-07-09 19:31:39 +00001209{
1210 return 1;
1211}
1212
pbrooka8c33202008-05-07 23:22:46 +00001213static void
bellard43fff232003-07-09 19:31:39 +00001214setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
Andreas Färber05390242012-02-25 03:37:53 +01001215 CPUARMState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001216{
pbrooka8c33202008-05-07 23:22:46 +00001217 __put_user(env->regs[0], &sc->arm_r0);
1218 __put_user(env->regs[1], &sc->arm_r1);
1219 __put_user(env->regs[2], &sc->arm_r2);
1220 __put_user(env->regs[3], &sc->arm_r3);
1221 __put_user(env->regs[4], &sc->arm_r4);
1222 __put_user(env->regs[5], &sc->arm_r5);
1223 __put_user(env->regs[6], &sc->arm_r6);
1224 __put_user(env->regs[7], &sc->arm_r7);
1225 __put_user(env->regs[8], &sc->arm_r8);
1226 __put_user(env->regs[9], &sc->arm_r9);
1227 __put_user(env->regs[10], &sc->arm_r10);
1228 __put_user(env->regs[11], &sc->arm_fp);
1229 __put_user(env->regs[12], &sc->arm_ip);
1230 __put_user(env->regs[13], &sc->arm_sp);
1231 __put_user(env->regs[14], &sc->arm_lr);
1232 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001233#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001234 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001235#endif
1236
pbrooka8c33202008-05-07 23:22:46 +00001237 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1238 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1239 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1240 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001241}
1242
bellard579a97f2007-11-11 14:26:47 +00001243static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01001244get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
bellard43fff232003-07-09 19:31:39 +00001245{
1246 unsigned long sp = regs->regs[13];
1247
bellard43fff232003-07-09 19:31:39 +00001248 /*
1249 * This is the X/Open sanctioned signal stack switching.
1250 */
pbrook624f7972008-05-31 16:11:38 +00001251 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
thsa04e1342007-09-27 13:57:58 +00001252 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001253 /*
1254 * ATPCS B01 mandates 8-byte alignment
1255 */
bellard579a97f2007-11-11 14:26:47 +00001256 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001257}
1258
1259static int
Andreas Färber05390242012-02-25 03:37:53 +01001260setup_return(CPUARMState *env, struct target_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001261 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001262{
pbrook624f7972008-05-31 16:11:38 +00001263 abi_ulong handler = ka->_sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001264 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001265 int thumb = handler & 1;
Peter Maydell964413d2011-01-14 20:39:19 +01001266 uint32_t cpsr = cpsr_read(env);
1267
1268 cpsr &= ~CPSR_IT;
1269 if (thumb) {
1270 cpsr |= CPSR_T;
1271 } else {
1272 cpsr &= ~CPSR_T;
1273 }
bellard43fff232003-07-09 19:31:39 +00001274
pbrook624f7972008-05-31 16:11:38 +00001275 if (ka->sa_flags & TARGET_SA_RESTORER) {
1276 retcode = ka->sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001277 } else {
1278 unsigned int idx = thumb;
1279
pbrook624f7972008-05-31 16:11:38 +00001280 if (ka->sa_flags & TARGET_SA_SIGINFO)
bellard43fff232003-07-09 19:31:39 +00001281 idx += 2;
1282
1283 if (__put_user(retcodes[idx], rc))
1284 return 1;
Stefan Weilca8a2772011-10-03 22:43:19 +02001285
bellardf8b0aa22007-11-11 23:03:42 +00001286 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001287 }
1288
1289 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001290 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001291 env->regs[14] = retcode;
1292 env->regs[15] = handler & (thumb ? ~1 : ~3);
Peter Maydell964413d2011-01-14 20:39:19 +01001293 cpsr_write(env, cpsr, 0xffffffff);
bellard43fff232003-07-09 19:31:39 +00001294
1295 return 0;
1296}
1297
Andreas Färber05390242012-02-25 03:37:53 +01001298static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
Peter Maydell0d871bd2010-11-24 15:20:05 +00001299{
1300 int i;
1301 struct target_vfp_sigframe *vfpframe;
1302 vfpframe = (struct target_vfp_sigframe *)regspace;
1303 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1304 __put_user(sizeof(*vfpframe), &vfpframe->size);
1305 for (i = 0; i < 32; i++) {
Peter Maydell005e1a02011-02-10 13:59:35 +00001306 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
Peter Maydell0d871bd2010-11-24 15:20:05 +00001307 }
1308 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1309 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1310 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1311 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1312 return (abi_ulong*)(vfpframe+1);
1313}
1314
Andreas Färber05390242012-02-25 03:37:53 +01001315static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1316 CPUARMState *env)
Peter Maydell08e11252010-11-24 15:20:07 +00001317{
1318 int i;
1319 struct target_iwmmxt_sigframe *iwmmxtframe;
1320 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1321 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1322 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1323 for (i = 0; i < 16; i++) {
1324 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1325 }
1326 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1327 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1328 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1329 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1330 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1331 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1332 return (abi_ulong*)(iwmmxtframe+1);
1333}
1334
pbrooka8c33202008-05-07 23:22:46 +00001335static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
Andreas Färber05390242012-02-25 03:37:53 +01001336 target_sigset_t *set, CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001337{
pbrooka8c33202008-05-07 23:22:46 +00001338 struct target_sigaltstack stack;
1339 int i;
Peter Maydell0d871bd2010-11-24 15:20:05 +00001340 abi_ulong *regspace;
pbrooka8c33202008-05-07 23:22:46 +00001341
1342 /* Clear all the bits of the ucontext we don't use. */
1343 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1344
1345 memset(&stack, 0, sizeof(stack));
1346 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1347 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1348 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1349 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1350
1351 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
Peter Maydell0d871bd2010-11-24 15:20:05 +00001352 /* Save coprocessor signal frame. */
1353 regspace = uc->tuc_regspace;
1354 if (arm_feature(env, ARM_FEATURE_VFP)) {
1355 regspace = setup_sigframe_v2_vfp(regspace, env);
1356 }
Peter Maydell08e11252010-11-24 15:20:07 +00001357 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1358 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1359 }
1360
Peter Maydell0d871bd2010-11-24 15:20:05 +00001361 /* Write terminating magic word */
1362 __put_user(0, regspace);
1363
pbrooka8c33202008-05-07 23:22:46 +00001364 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1365 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1366 }
1367}
1368
1369/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00001370static void setup_frame_v1(int usig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001371 target_sigset_t *set, CPUARMState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001372{
1373 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001374 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001375 int i;
bellard43fff232003-07-09 19:31:39 +00001376
bellard579a97f2007-11-11 14:26:47 +00001377 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1378 return;
1379
pbrooka8c33202008-05-07 23:22:46 +00001380 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001381
bellard92319442004-06-19 16:58:13 +00001382 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1383 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001384 goto end;
bellard43fff232003-07-09 19:31:39 +00001385 }
1386
pbrooka8c33202008-05-07 23:22:46 +00001387 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1388 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001389
1390end:
1391 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001392}
1393
pbrook624f7972008-05-31 16:11:38 +00001394static void setup_frame_v2(int usig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001395 target_sigset_t *set, CPUARMState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001396{
1397 struct sigframe_v2 *frame;
1398 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1399
1400 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1401 return;
1402
1403 setup_sigframe_v2(&frame->uc, set, regs);
1404
1405 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1406 frame_addr + offsetof(struct sigframe_v2, retcode));
1407
1408 unlock_user_struct(frame, frame_addr, 1);
1409}
1410
pbrook624f7972008-05-31 16:11:38 +00001411static void setup_frame(int usig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001412 target_sigset_t *set, CPUARMState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001413{
1414 if (get_osversion() >= 0x020612) {
1415 setup_frame_v2(usig, ka, set, regs);
1416 } else {
1417 setup_frame_v1(usig, ka, set, regs);
1418 }
bellard43fff232003-07-09 19:31:39 +00001419}
1420
bellard579a97f2007-11-11 14:26:47 +00001421/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +00001422static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001423 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01001424 target_sigset_t *set, CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001425{
pbrooka745ec62008-05-06 15:36:17 +00001426 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001427 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001428 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001429 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001430 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001431
bellard579a97f2007-11-11 14:26:47 +00001432 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001433 return /* 1 */;
1434
pbrooka745ec62008-05-06 15:36:17 +00001435 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001436 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001437 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001438 __put_user(uc_addr, &frame->puc);
1439 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001440
1441 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001442 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001443
thsa04e1342007-09-27 13:57:58 +00001444 memset(&stack, 0, sizeof(stack));
1445 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1446 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1447 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001448 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001449
pbrooka8c33202008-05-07 23:22:46 +00001450 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001451 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001452 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001453 goto end;
bellard92319442004-06-19 16:58:13 +00001454 }
bellard43fff232003-07-09 19:31:39 +00001455
pbrooka8c33202008-05-07 23:22:46 +00001456 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1457 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001458
pbrooka8c33202008-05-07 23:22:46 +00001459 env->regs[1] = info_addr;
1460 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001461
1462end:
1463 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001464}
1465
pbrook624f7972008-05-31 16:11:38 +00001466static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001467 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01001468 target_sigset_t *set, CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00001469{
1470 struct rt_sigframe_v2 *frame;
1471 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001472 abi_ulong info_addr, uc_addr;
1473
1474 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1475 return /* 1 */;
1476
1477 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1478 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001479 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001480
pbrooka8c33202008-05-07 23:22:46 +00001481 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001482
pbrooka8c33202008-05-07 23:22:46 +00001483 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1484 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001485
pbrooka8c33202008-05-07 23:22:46 +00001486 env->regs[1] = info_addr;
1487 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001488
bellard579a97f2007-11-11 14:26:47 +00001489 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001490}
1491
pbrook624f7972008-05-31 16:11:38 +00001492static void setup_rt_frame(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001493 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01001494 target_sigset_t *set, CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00001495{
1496 if (get_osversion() >= 0x020612) {
1497 setup_rt_frame_v2(usig, ka, info, set, env);
1498 } else {
1499 setup_rt_frame_v1(usig, ka, info, set, env);
1500 }
1501}
1502
bellard43fff232003-07-09 19:31:39 +00001503static int
Andreas Färber05390242012-02-25 03:37:53 +01001504restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
bellard43fff232003-07-09 19:31:39 +00001505{
1506 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001507 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001508
1509 __get_user_error(env->regs[0], &sc->arm_r0, err);
1510 __get_user_error(env->regs[1], &sc->arm_r1, err);
1511 __get_user_error(env->regs[2], &sc->arm_r2, err);
1512 __get_user_error(env->regs[3], &sc->arm_r3, err);
1513 __get_user_error(env->regs[4], &sc->arm_r4, err);
1514 __get_user_error(env->regs[5], &sc->arm_r5, err);
1515 __get_user_error(env->regs[6], &sc->arm_r6, err);
1516 __get_user_error(env->regs[7], &sc->arm_r7, err);
1517 __get_user_error(env->regs[8], &sc->arm_r8, err);
1518 __get_user_error(env->regs[9], &sc->arm_r9, err);
1519 __get_user_error(env->regs[10], &sc->arm_r10, err);
1520 __get_user_error(env->regs[11], &sc->arm_fp, err);
1521 __get_user_error(env->regs[12], &sc->arm_ip, err);
1522 __get_user_error(env->regs[13], &sc->arm_sp, err);
1523 __get_user_error(env->regs[14], &sc->arm_lr, err);
1524 __get_user_error(env->regs[15], &sc->arm_pc, err);
1525#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001526 __get_user_error(cpsr, &sc->arm_cpsr, err);
pbrook75b680e2008-03-21 16:07:30 +00001527 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001528#endif
1529
1530 err |= !valid_user_regs(env);
1531
1532 return err;
1533}
1534
Andreas Färber05390242012-02-25 03:37:53 +01001535static long do_sigreturn_v1(CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001536{
bellardf8b0aa22007-11-11 23:03:42 +00001537 abi_ulong frame_addr;
pbrooka8c33202008-05-07 23:22:46 +00001538 struct sigframe_v1 *frame;
Anthony Liguoric227f092009-10-01 16:12:16 -05001539 target_sigset_t set;
bellard43fff232003-07-09 19:31:39 +00001540 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001541 int i;
bellard43fff232003-07-09 19:31:39 +00001542
1543 /*
1544 * Since we stacked the signal on a 64-bit boundary,
1545 * then 'sp' should be word aligned here. If it's
1546 * not, then the user is trying to mess with us.
1547 */
1548 if (env->regs[13] & 7)
1549 goto badframe;
1550
bellardf8b0aa22007-11-11 23:03:42 +00001551 frame_addr = env->regs[13];
1552 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1553 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001554
bellard92319442004-06-19 16:58:13 +00001555 if (__get_user(set.sig[0], &frame->sc.oldmask))
1556 goto badframe;
1557 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1558 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1559 goto badframe;
1560 }
bellard43fff232003-07-09 19:31:39 +00001561
bellard92319442004-06-19 16:58:13 +00001562 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001563 sigprocmask(SIG_SETMASK, &host_set, NULL);
1564
1565 if (restore_sigcontext(env, &frame->sc))
1566 goto badframe;
1567
1568#if 0
1569 /* Send SIGTRAP if we're single-stepping */
1570 if (ptrace_cancel_bpt(current))
1571 send_sig(SIGTRAP, current, 1);
1572#endif
bellardf8b0aa22007-11-11 23:03:42 +00001573 unlock_user_struct(frame, frame_addr, 0);
1574 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001575
1576badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001577 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001578 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00001579 return 0;
1580}
1581
Andreas Färber05390242012-02-25 03:37:53 +01001582static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
Peter Maydell5f9099d2010-11-24 15:20:06 +00001583{
1584 int i;
1585 abi_ulong magic, sz;
1586 uint32_t fpscr, fpexc;
1587 struct target_vfp_sigframe *vfpframe;
1588 vfpframe = (struct target_vfp_sigframe *)regspace;
1589
1590 __get_user(magic, &vfpframe->magic);
1591 __get_user(sz, &vfpframe->size);
1592 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1593 return 0;
1594 }
1595 for (i = 0; i < 32; i++) {
Peter Maydell005e1a02011-02-10 13:59:35 +00001596 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
Peter Maydell5f9099d2010-11-24 15:20:06 +00001597 }
1598 __get_user(fpscr, &vfpframe->ufp.fpscr);
1599 vfp_set_fpscr(env, fpscr);
1600 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1601 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1602 * and the exception flag is cleared
1603 */
1604 fpexc |= (1 << 30);
1605 fpexc &= ~((1 << 31) | (1 << 28));
1606 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1607 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1608 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1609 return (abi_ulong*)(vfpframe + 1);
1610}
1611
Andreas Färber05390242012-02-25 03:37:53 +01001612static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1613 abi_ulong *regspace)
Peter Maydella59d69d2010-11-24 15:20:08 +00001614{
1615 int i;
1616 abi_ulong magic, sz;
1617 struct target_iwmmxt_sigframe *iwmmxtframe;
1618 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1619
1620 __get_user(magic, &iwmmxtframe->magic);
1621 __get_user(sz, &iwmmxtframe->size);
1622 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1623 return 0;
1624 }
1625 for (i = 0; i < 16; i++) {
1626 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1627 }
1628 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1629 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1630 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1631 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1632 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1633 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1634 return (abi_ulong*)(iwmmxtframe + 1);
1635}
1636
Andreas Färber05390242012-02-25 03:37:53 +01001637static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
pbrooka8c33202008-05-07 23:22:46 +00001638 struct target_ucontext_v2 *uc)
1639{
1640 sigset_t host_set;
Peter Maydell5f9099d2010-11-24 15:20:06 +00001641 abi_ulong *regspace;
pbrooka8c33202008-05-07 23:22:46 +00001642
1643 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1644 sigprocmask(SIG_SETMASK, &host_set, NULL);
1645
1646 if (restore_sigcontext(env, &uc->tuc_mcontext))
1647 return 1;
1648
Peter Maydell5f9099d2010-11-24 15:20:06 +00001649 /* Restore coprocessor signal frame */
1650 regspace = uc->tuc_regspace;
1651 if (arm_feature(env, ARM_FEATURE_VFP)) {
1652 regspace = restore_sigframe_v2_vfp(env, regspace);
1653 if (!regspace) {
1654 return 1;
1655 }
1656 }
Peter Maydella59d69d2010-11-24 15:20:08 +00001657 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1658 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1659 if (!regspace) {
1660 return 1;
1661 }
1662 }
Peter Maydell5f9099d2010-11-24 15:20:06 +00001663
pbrooka8c33202008-05-07 23:22:46 +00001664 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1665 return 1;
1666
1667#if 0
1668 /* Send SIGTRAP if we're single-stepping */
1669 if (ptrace_cancel_bpt(current))
1670 send_sig(SIGTRAP, current, 1);
1671#endif
1672
1673 return 0;
1674}
1675
Andreas Färber05390242012-02-25 03:37:53 +01001676static long do_sigreturn_v2(CPUARMState *env)
pbrooka8c33202008-05-07 23:22:46 +00001677{
1678 abi_ulong frame_addr;
1679 struct sigframe_v2 *frame;
1680
1681 /*
1682 * Since we stacked the signal on a 64-bit boundary,
1683 * then 'sp' should be word aligned here. If it's
1684 * not, then the user is trying to mess with us.
1685 */
1686 if (env->regs[13] & 7)
1687 goto badframe;
1688
1689 frame_addr = env->regs[13];
1690 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1691 goto badframe;
1692
1693 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1694 goto badframe;
1695
1696 unlock_user_struct(frame, frame_addr, 0);
1697 return env->regs[0];
1698
1699badframe:
1700 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001701 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka8c33202008-05-07 23:22:46 +00001702 return 0;
1703}
1704
Andreas Färber05390242012-02-25 03:37:53 +01001705long do_sigreturn(CPUARMState *env)
pbrooka8c33202008-05-07 23:22:46 +00001706{
1707 if (get_osversion() >= 0x020612) {
1708 return do_sigreturn_v2(env);
1709 } else {
1710 return do_sigreturn_v1(env);
1711 }
1712}
1713
Andreas Färber05390242012-02-25 03:37:53 +01001714static long do_rt_sigreturn_v1(CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001715{
bellardf8b0aa22007-11-11 23:03:42 +00001716 abi_ulong frame_addr;
pbrooka745ec62008-05-06 15:36:17 +00001717 struct rt_sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001718 sigset_t host_set;
1719
1720 /*
1721 * Since we stacked the signal on a 64-bit boundary,
1722 * then 'sp' should be word aligned here. If it's
1723 * not, then the user is trying to mess with us.
1724 */
1725 if (env->regs[13] & 7)
1726 goto badframe;
1727
bellardf8b0aa22007-11-11 23:03:42 +00001728 frame_addr = env->regs[13];
1729 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1730 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001731
bellardb8076a72005-04-07 22:20:31 +00001732 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001733 sigprocmask(SIG_SETMASK, &host_set, NULL);
1734
bellardb8076a72005-04-07 22:20:31 +00001735 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001736 goto badframe;
1737
pbrooka745ec62008-05-06 15:36:17 +00001738 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 +00001739 goto badframe;
1740
bellard43fff232003-07-09 19:31:39 +00001741#if 0
1742 /* Send SIGTRAP if we're single-stepping */
1743 if (ptrace_cancel_bpt(current))
1744 send_sig(SIGTRAP, current, 1);
1745#endif
bellardf8b0aa22007-11-11 23:03:42 +00001746 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001747 return env->regs[0];
1748
1749badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001750 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001751 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00001752 return 0;
1753}
1754
Andreas Färber05390242012-02-25 03:37:53 +01001755static long do_rt_sigreturn_v2(CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00001756{
1757 abi_ulong frame_addr;
1758 struct rt_sigframe_v2 *frame;
pbrooka745ec62008-05-06 15:36:17 +00001759
1760 /*
1761 * Since we stacked the signal on a 64-bit boundary,
1762 * then 'sp' should be word aligned here. If it's
1763 * not, then the user is trying to mess with us.
1764 */
1765 if (env->regs[13] & 7)
1766 goto badframe;
1767
1768 frame_addr = env->regs[13];
1769 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1770 goto badframe;
1771
pbrooka8c33202008-05-07 23:22:46 +00001772 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1773 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00001774
pbrooka745ec62008-05-06 15:36:17 +00001775 unlock_user_struct(frame, frame_addr, 0);
1776 return env->regs[0];
1777
1778badframe:
1779 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001780 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka745ec62008-05-06 15:36:17 +00001781 return 0;
1782}
1783
Andreas Färber05390242012-02-25 03:37:53 +01001784long do_rt_sigreturn(CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00001785{
1786 if (get_osversion() >= 0x020612) {
1787 return do_rt_sigreturn_v2(env);
1788 } else {
1789 return do_rt_sigreturn_v1(env);
1790 }
1791}
1792
bellard6d5e2162004-09-30 22:04:13 +00001793#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001794
bellard6d5e2162004-09-30 22:04:13 +00001795#define __SUNOS_MAXWIN 31
1796
1797/* This is what SunOS does, so shall I. */
1798struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001799 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001800
blueswir1992f48a2007-10-14 16:27:31 +00001801 abi_ulong sigc_mask; /* sigmask to restore */
1802 abi_ulong sigc_sp; /* stack pointer */
1803 abi_ulong sigc_pc; /* program counter */
1804 abi_ulong sigc_npc; /* next program counter */
1805 abi_ulong sigc_psr; /* for condition codes etc */
1806 abi_ulong sigc_g1; /* User uses these two registers */
1807 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001808
1809 /* Now comes information regarding the users window set
1810 * at the time of the signal.
1811 */
blueswir1992f48a2007-10-14 16:27:31 +00001812 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001813
1814 /* stack ptrs for each regwin buf */
1815 char *sigc_spbuf[__SUNOS_MAXWIN];
1816
1817 /* Windows to restore after signal */
1818 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001819 abi_ulong locals[8];
1820 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001821 } sigc_wbuf[__SUNOS_MAXWIN];
1822};
1823/* A Sparc stack frame */
1824struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001825 abi_ulong locals[8];
Peter Maydelle321c342011-02-01 15:54:52 +00001826 abi_ulong ins[8];
1827 /* It's simpler to treat fp and callers_pc as elements of ins[]
1828 * since we never need to access them ourselves.
1829 */
bellard6d5e2162004-09-30 22:04:13 +00001830 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001831 abi_ulong xargs[6];
1832 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001833};
1834
1835typedef struct {
1836 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001837 abi_ulong psr;
1838 abi_ulong pc;
1839 abi_ulong npc;
1840 abi_ulong y;
1841 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001842 } si_regs;
1843 int si_mask;
1844} __siginfo_t;
1845
1846typedef struct {
1847 unsigned long si_float_regs [32];
1848 unsigned long si_fsr;
1849 unsigned long si_fpqdepth;
1850 struct {
1851 unsigned long *insn_addr;
1852 unsigned long insn;
1853 } si_fpqueue [16];
Anthony Liguoric227f092009-10-01 16:12:16 -05001854} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001855
1856
1857struct target_signal_frame {
1858 struct sparc_stackf ss;
1859 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00001860 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001861 abi_ulong insns[2] __attribute__ ((aligned (8)));
1862 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1863 abi_ulong extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05001864 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001865};
1866struct target_rt_signal_frame {
1867 struct sparc_stackf ss;
1868 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001869 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001870 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00001871 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001872 unsigned int insns[2];
1873 stack_t stack;
1874 unsigned int extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05001875 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001876};
1877
bellarde80cfcf2004-12-19 23:18:01 +00001878#define UREG_O0 16
1879#define UREG_O6 22
1880#define UREG_I0 0
1881#define UREG_I1 1
1882#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001883#define UREG_I3 3
1884#define UREG_I4 4
1885#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001886#define UREG_I6 6
1887#define UREG_I7 7
1888#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001889#define UREG_FP UREG_I6
1890#define UREG_SP UREG_O6
1891
pbrook624f7972008-05-31 16:11:38 +00001892static inline abi_ulong get_sigframe(struct target_sigaction *sa,
Andreas Färber05390242012-02-25 03:37:53 +01001893 CPUSPARCState *env,
1894 unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001895{
bellard459a4012007-11-11 19:45:10 +00001896 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001897
1898 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001899
1900 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00001901 if (sa->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +00001902 if (!on_sig_stack(sp)
1903 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1904 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001905 }
bellard459a4012007-11-11 19:45:10 +00001906 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001907}
1908
1909static int
Andreas Färber05390242012-02-25 03:37:53 +01001910setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001911{
1912 int err = 0, i;
1913
bellard6d5e2162004-09-30 22:04:13 +00001914 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001915 err |= __put_user(env->pc, &si->si_regs.pc);
1916 err |= __put_user(env->npc, &si->si_regs.npc);
1917 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001918 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001919 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1920 }
bellarda315a142005-01-30 22:59:18 +00001921 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001922 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001923 }
bellard6d5e2162004-09-30 22:04:13 +00001924 err |= __put_user(mask, &si->si_mask);
1925 return err;
1926}
bellarde80cfcf2004-12-19 23:18:01 +00001927
bellard80a9d032005-01-03 23:31:27 +00001928#if 0
bellard6d5e2162004-09-30 22:04:13 +00001929static int
1930setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
Andreas Färber05390242012-02-25 03:37:53 +01001931 CPUSPARCState *env, unsigned long mask)
bellard6d5e2162004-09-30 22:04:13 +00001932{
1933 int err = 0;
1934
1935 err |= __put_user(mask, &sc->sigc_mask);
1936 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1937 err |= __put_user(env->pc, &sc->sigc_pc);
1938 err |= __put_user(env->npc, &sc->sigc_npc);
1939 err |= __put_user(env->psr, &sc->sigc_psr);
1940 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1941 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1942
1943 return err;
1944}
bellard80a9d032005-01-03 23:31:27 +00001945#endif
bellard6d5e2162004-09-30 22:04:13 +00001946#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1947
pbrook624f7972008-05-31 16:11:38 +00001948static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001949 target_sigset_t *set, CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00001950{
bellard459a4012007-11-11 19:45:10 +00001951 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001952 struct target_signal_frame *sf;
1953 int sigframe_size, err, i;
1954
1955 /* 1. Make sure everything is clean */
1956 //synchronize_user_stack();
1957
1958 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001959 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001960
bellard459a4012007-11-11 19:45:10 +00001961 sf = lock_user(VERIFY_WRITE, sf_addr,
1962 sizeof(struct target_signal_frame), 0);
1963 if (!sf)
1964 goto sigsegv;
1965
bellarde80cfcf2004-12-19 23:18:01 +00001966 //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 +00001967#if 0
1968 if (invalid_frame_pointer(sf, sigframe_size))
1969 goto sigill_and_return;
1970#endif
1971 /* 2. Save the current process state */
1972 err = setup___siginfo(&sf->info, env, set->sig[0]);
1973 err |= __put_user(0, &sf->extra_size);
1974
1975 //err |= save_fpu_state(regs, &sf->fpu_state);
1976 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1977
1978 err |= __put_user(set->sig[0], &sf->info.si_mask);
1979 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1980 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1981 }
1982
bellarda315a142005-01-30 22:59:18 +00001983 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001984 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001985 }
bellarda315a142005-01-30 22:59:18 +00001986 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001987 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001988 }
bellard6d5e2162004-09-30 22:04:13 +00001989 if (err)
1990 goto sigsegv;
1991
1992 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001993 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001994 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001995 env->regwptr[UREG_I1] = sf_addr +
1996 offsetof(struct target_signal_frame, info);
1997 env->regwptr[UREG_I2] = sf_addr +
1998 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001999
2000 /* 4. signal handler */
pbrook624f7972008-05-31 16:11:38 +00002001 env->pc = ka->_sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00002002 env->npc = (env->pc + 4);
2003 /* 5. return to kernel instructions */
pbrook624f7972008-05-31 16:11:38 +00002004 if (ka->sa_restorer)
2005 env->regwptr[UREG_I7] = ka->sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00002006 else {
bellard775b58d2007-11-11 16:22:17 +00002007 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00002008
2009 env->regwptr[UREG_I7] = sf_addr +
2010 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00002011
2012 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00002013 val32 = 0x821020d8;
2014 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00002015
2016 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00002017 val32 = 0x91d02010;
2018 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00002019 if (err)
2020 goto sigsegv;
2021
2022 /* Flush instruction space. */
2023 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00002024 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00002025 }
bellard459a4012007-11-11 19:45:10 +00002026 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00002027 return;
bellard459a4012007-11-11 19:45:10 +00002028#if 0
2029sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00002030 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00002031#endif
bellard6d5e2162004-09-30 22:04:13 +00002032sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00002033 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00002034 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00002035 force_sig(TARGET_SIGSEGV);
2036}
2037static inline int
Andreas Färber05390242012-02-25 03:37:53 +01002038restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00002039{
2040 int err;
2041#if 0
2042#ifdef CONFIG_SMP
2043 if (current->flags & PF_USEDFPU)
2044 regs->psr &= ~PSR_EF;
2045#else
2046 if (current == last_task_used_math) {
2047 last_task_used_math = 0;
2048 regs->psr &= ~PSR_EF;
2049 }
2050#endif
2051 current->used_math = 1;
2052 current->flags &= ~PF_USEDFPU;
2053#endif
2054#if 0
2055 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2056 return -EFAULT;
2057#endif
2058
bellardfafffae2006-10-28 12:09:16 +00002059#if 0
2060 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00002061 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
2062 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00002063#endif
bellard6d5e2162004-09-30 22:04:13 +00002064 err |= __get_user(env->fsr, &fpu->si_fsr);
2065#if 0
2066 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2067 if (current->thread.fpqdepth != 0)
2068 err |= __copy_from_user(&current->thread.fpqueue[0],
2069 &fpu->si_fpqueue[0],
2070 ((sizeof(unsigned long) +
2071 (sizeof(unsigned long *)))*16));
2072#endif
2073 return err;
2074}
2075
2076
pbrook624f7972008-05-31 16:11:38 +00002077static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002078 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01002079 target_sigset_t *set, CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002080{
2081 fprintf(stderr, "setup_rt_frame: not implemented\n");
2082}
2083
Andreas Färber05390242012-02-25 03:37:53 +01002084long do_sigreturn(CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002085{
bellardf8b0aa22007-11-11 23:03:42 +00002086 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00002087 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00002088 uint32_t up_psr, pc, npc;
Anthony Liguoric227f092009-10-01 16:12:16 -05002089 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00002090 sigset_t host_set;
bellarde80cfcf2004-12-19 23:18:01 +00002091 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00002092
bellardf8b0aa22007-11-11 23:03:42 +00002093 sf_addr = env->regwptr[UREG_FP];
2094 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2095 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00002096#if 0
bellarde80cfcf2004-12-19 23:18:01 +00002097 fprintf(stderr, "sigreturn\n");
2098 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 +00002099#endif
bellarde80cfcf2004-12-19 23:18:01 +00002100 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00002101
2102 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00002103
bellardf8b0aa22007-11-11 23:03:42 +00002104 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00002105 goto segv_and_exit;
2106
2107 err = __get_user(pc, &sf->info.si_regs.pc);
2108 err |= __get_user(npc, &sf->info.si_regs.npc);
2109
bellard6d5e2162004-09-30 22:04:13 +00002110 if ((pc | npc) & 3)
2111 goto segv_and_exit;
2112
2113 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00002114 err |= __get_user(up_psr, &sf->info.si_regs.psr);
2115
bellard6d5e2162004-09-30 22:04:13 +00002116 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00002117 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2118 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2119
2120 env->pc = pc;
2121 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00002122 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00002123 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00002124 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2125 }
bellarda315a142005-01-30 22:59:18 +00002126 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00002127 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2128 }
bellard6d5e2162004-09-30 22:04:13 +00002129
Peter Maydell2aec3a22011-06-16 17:37:14 +01002130 /* FIXME: implement FPU save/restore:
2131 * __get_user(fpu_save, &sf->fpu_save);
2132 * if (fpu_save)
2133 * err |= restore_fpu_state(env, fpu_save);
2134 */
bellard6d5e2162004-09-30 22:04:13 +00002135
2136 /* This is pretty much atomic, no amount locking would prevent
2137 * the races which exist anyways.
2138 */
2139 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00002140 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2141 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2142 }
2143
2144 target_to_host_sigset_internal(&host_set, &set);
2145 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00002146
2147 if (err)
2148 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00002149 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00002150 return env->regwptr[0];
2151
2152segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00002153 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00002154 force_sig(TARGET_SIGSEGV);
2155}
2156
Andreas Färber05390242012-02-25 03:37:53 +01002157long do_rt_sigreturn(CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002158{
2159 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002160 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00002161}
2162
bellard459a4012007-11-11 19:45:10 +00002163#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00002164#define MC_TSTATE 0
2165#define MC_PC 1
2166#define MC_NPC 2
2167#define MC_Y 3
2168#define MC_G1 4
2169#define MC_G2 5
2170#define MC_G3 6
2171#define MC_G4 7
2172#define MC_G5 8
2173#define MC_G6 9
2174#define MC_G7 10
2175#define MC_O0 11
2176#define MC_O1 12
2177#define MC_O2 13
2178#define MC_O3 14
2179#define MC_O4 15
2180#define MC_O5 16
2181#define MC_O6 17
2182#define MC_O7 18
2183#define MC_NGREG 19
2184
Anthony Liguoric227f092009-10-01 16:12:16 -05002185typedef abi_ulong target_mc_greg_t;
2186typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
blueswir15bfb56b2007-10-05 17:01:51 +00002187
2188struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00002189 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002190 uint32_t mcfq_insn;
2191};
2192
2193struct target_mc_fpu {
2194 union {
2195 uint32_t sregs[32];
2196 uint64_t dregs[32];
2197 //uint128_t qregs[16];
2198 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00002199 abi_ulong mcfpu_fsr;
2200 abi_ulong mcfpu_fprs;
2201 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00002202 struct target_mc_fq *mcfpu_fq;
2203 unsigned char mcfpu_qcnt;
2204 unsigned char mcfpu_qentsz;
2205 unsigned char mcfpu_enab;
2206};
Anthony Liguoric227f092009-10-01 16:12:16 -05002207typedef struct target_mc_fpu target_mc_fpu_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002208
2209typedef struct {
Anthony Liguoric227f092009-10-01 16:12:16 -05002210 target_mc_gregset_t mc_gregs;
2211 target_mc_greg_t mc_fp;
2212 target_mc_greg_t mc_i7;
2213 target_mc_fpu_t mc_fpregs;
2214} target_mcontext_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002215
2216struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002217 struct target_ucontext *tuc_link;
2218 abi_ulong tuc_flags;
2219 target_sigset_t tuc_sigmask;
2220 target_mcontext_t tuc_mcontext;
blueswir15bfb56b2007-10-05 17:01:51 +00002221};
2222
2223/* A V9 register window */
2224struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00002225 abi_ulong locals[8];
2226 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00002227};
2228
2229#define TARGET_STACK_BIAS 2047
2230
2231/* {set, get}context() needed for 64-bit SparcLinux userland. */
2232void sparc64_set_context(CPUSPARCState *env)
2233{
bellard459a4012007-11-11 19:45:10 +00002234 abi_ulong ucp_addr;
2235 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002236 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00002237 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00002238 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002239 int err;
2240 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002241
bellard459a4012007-11-11 19:45:10 +00002242 ucp_addr = env->regwptr[UREG_I0];
2243 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2244 goto do_sigsegv;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002245 grp = &ucp->tuc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00002246 err = __get_user(pc, &((*grp)[MC_PC]));
2247 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002248 if (err || ((pc | npc) & 3))
2249 goto do_sigsegv;
2250 if (env->regwptr[UREG_I1]) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002251 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002252 sigset_t set;
2253
2254 if (TARGET_NSIG_WORDS == 1) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002255 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002256 goto do_sigsegv;
2257 } else {
bellard459a4012007-11-11 19:45:10 +00002258 abi_ulong *src, *dst;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002259 src = ucp->tuc_sigmask.sig;
bellard459a4012007-11-11 19:45:10 +00002260 dst = target_set.sig;
Anthony Liguoric227f092009-10-01 16:12:16 -05002261 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002262 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002263 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00002264 if (err)
2265 goto do_sigsegv;
2266 }
2267 target_to_host_sigset_internal(&set, &target_set);
2268 sigprocmask(SIG_SETMASK, &set, NULL);
2269 }
2270 env->pc = pc;
2271 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00002272 err |= __get_user(env->y, &((*grp)[MC_Y]));
2273 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002274 env->asi = (tstate >> 24) & 0xff;
Blue Swirl5a834bb2010-05-09 20:19:04 +00002275 cpu_put_ccr(env, tstate >> 32);
2276 cpu_put_cwp64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00002277 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2278 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2279 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2280 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2281 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2282 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2283 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2284 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2285 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2286 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2287 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2288 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2289 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2290 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2291 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002292
Aurelien Jarno60e99242010-03-29 02:12:51 +02002293 err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2294 err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002295
bellard459a4012007-11-11 19:45:10 +00002296 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2297 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2298 abi_ulong) != 0)
2299 goto do_sigsegv;
2300 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2301 abi_ulong) != 0)
2302 goto do_sigsegv;
Peter Maydellc7b016b2011-06-16 17:37:15 +01002303 /* FIXME this does not match how the kernel handles the FPU in
2304 * its sparc64_set_context implementation. In particular the FPU
2305 * is only restored if fenab is non-zero in:
2306 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2307 */
Aurelien Jarno60e99242010-03-29 02:12:51 +02002308 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002309 {
Richard Henderson30038fd2011-10-17 10:42:49 -07002310 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2311 for (i = 0; i < 64; i++, src++) {
2312 if (i & 1) {
2313 err |= __get_user(env->fpr[i/2].l.lower, src);
2314 } else {
2315 err |= __get_user(env->fpr[i/2].l.upper, src);
2316 }
2317 }
bellard459a4012007-11-11 19:45:10 +00002318 }
bellard579a97f2007-11-11 14:26:47 +00002319 err |= __get_user(env->fsr,
Aurelien Jarno60e99242010-03-29 02:12:51 +02002320 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
bellard579a97f2007-11-11 14:26:47 +00002321 err |= __get_user(env->gsr,
Aurelien Jarno60e99242010-03-29 02:12:51 +02002322 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002323 if (err)
2324 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002325 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002326 return;
2327 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002328 unlock_user_struct(ucp, ucp_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002329 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002330}
2331
2332void sparc64_get_context(CPUSPARCState *env)
2333{
bellard459a4012007-11-11 19:45:10 +00002334 abi_ulong ucp_addr;
2335 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002336 target_mc_gregset_t *grp;
2337 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002338 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002339 int err;
2340 unsigned int i;
Anthony Liguoric227f092009-10-01 16:12:16 -05002341 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002342 sigset_t set;
2343
bellard459a4012007-11-11 19:45:10 +00002344 ucp_addr = env->regwptr[UREG_I0];
2345 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2346 goto do_sigsegv;
2347
Aurelien Jarno60e99242010-03-29 02:12:51 +02002348 mcp = &ucp->tuc_mcontext;
blueswir15bfb56b2007-10-05 17:01:51 +00002349 grp = &mcp->mc_gregs;
2350
2351 /* Skip over the trap instruction, first. */
2352 env->pc = env->npc;
2353 env->npc += 4;
2354
2355 err = 0;
2356
2357 sigprocmask(0, NULL, &set);
2358 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002359 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002360 err |= __put_user(target_set.sig[0],
Aurelien Jarno60e99242010-03-29 02:12:51 +02002361 (abi_ulong *)&ucp->tuc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002362 } else {
2363 abi_ulong *src, *dst;
2364 src = target_set.sig;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002365 dst = ucp->tuc_sigmask.sig;
Anthony Liguoric227f092009-10-01 16:12:16 -05002366 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002367 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002368 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00002369 if (err)
2370 goto do_sigsegv;
2371 }
2372
bellard459a4012007-11-11 19:45:10 +00002373 /* XXX: tstate must be saved properly */
2374 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00002375 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2376 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2377 err |= __put_user(env->y, &((*grp)[MC_Y]));
2378 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2379 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2380 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2381 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2382 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2383 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2384 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2385 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2386 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2387 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2388 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2389 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2390 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2391 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2392 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002393
bellard459a4012007-11-11 19:45:10 +00002394 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2395 fp = i7 = 0;
2396 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2397 abi_ulong) != 0)
2398 goto do_sigsegv;
2399 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2400 abi_ulong) != 0)
2401 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002402 err |= __put_user(fp, &(mcp->mc_fp));
2403 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002404
bellard459a4012007-11-11 19:45:10 +00002405 {
Richard Henderson30038fd2011-10-17 10:42:49 -07002406 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2407 for (i = 0; i < 64; i++, dst++) {
2408 if (i & 1) {
2409 err |= __put_user(env->fpr[i/2].l.lower, dst);
2410 } else {
2411 err |= __put_user(env->fpr[i/2].l.upper, dst);
2412 }
2413 }
bellard459a4012007-11-11 19:45:10 +00002414 }
bellard579a97f2007-11-11 14:26:47 +00002415 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2416 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2417 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002418
2419 if (err)
2420 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002421 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002422 return;
2423 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002424 unlock_user_struct(ucp, ucp_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02002425 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002426}
2427#endif
thsd26bc212007-11-08 18:05:37 +00002428#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002429
2430# warning signal handling not implemented
2431
pbrook624f7972008-05-31 16:11:38 +00002432static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01002433 target_sigset_t *set, CPUMIPSState *env)
ths540635b2007-09-30 01:58:33 +00002434{
2435 fprintf(stderr, "setup_frame: not implemented\n");
2436}
2437
pbrook624f7972008-05-31 16:11:38 +00002438static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002439 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01002440 target_sigset_t *set, CPUMIPSState *env)
ths540635b2007-09-30 01:58:33 +00002441{
2442 fprintf(stderr, "setup_rt_frame: not implemented\n");
2443}
2444
Andreas Färber05390242012-02-25 03:37:53 +01002445long do_sigreturn(CPUMIPSState *env)
ths540635b2007-09-30 01:58:33 +00002446{
2447 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002448 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002449}
2450
Andreas Färber05390242012-02-25 03:37:53 +01002451long do_rt_sigreturn(CPUMIPSState *env)
ths540635b2007-09-30 01:58:33 +00002452{
2453 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002454 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002455}
2456
thsd26bc212007-11-08 18:05:37 +00002457#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002458
2459# warning signal handling not implemented
2460
pbrook624f7972008-05-31 16:11:38 +00002461static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01002462 target_sigset_t *set, CPUMIPSState *env)
ths540635b2007-09-30 01:58:33 +00002463{
2464 fprintf(stderr, "setup_frame: not implemented\n");
2465}
2466
pbrook624f7972008-05-31 16:11:38 +00002467static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002468 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01002469 target_sigset_t *set, CPUMIPSState *env)
ths540635b2007-09-30 01:58:33 +00002470{
2471 fprintf(stderr, "setup_rt_frame: not implemented\n");
2472}
2473
Andreas Färber05390242012-02-25 03:37:53 +01002474long do_sigreturn(CPUMIPSState *env)
ths540635b2007-09-30 01:58:33 +00002475{
2476 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002477 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002478}
2479
Andreas Färber05390242012-02-25 03:37:53 +01002480long do_rt_sigreturn(CPUMIPSState *env)
ths540635b2007-09-30 01:58:33 +00002481{
2482 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002483 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002484}
2485
thsd26bc212007-11-08 18:05:37 +00002486#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002487
2488struct target_sigcontext {
2489 uint32_t sc_regmask; /* Unused */
2490 uint32_t sc_status;
2491 uint64_t sc_pc;
2492 uint64_t sc_regs[32];
2493 uint64_t sc_fpregs[32];
2494 uint32_t sc_ownedfp; /* Unused */
2495 uint32_t sc_fpc_csr;
2496 uint32_t sc_fpc_eir; /* Unused */
2497 uint32_t sc_used_math;
2498 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
Paul Brook94c54952009-07-09 18:40:15 +01002499 uint32_t pad0;
bellard106ec872006-06-27 21:08:10 +00002500 uint64_t sc_mdhi;
2501 uint64_t sc_mdlo;
2502 target_ulong sc_hi1; /* Was sc_cause */
2503 target_ulong sc_lo1; /* Was sc_badvaddr */
2504 target_ulong sc_hi2; /* Was sc_sigset[4] */
2505 target_ulong sc_lo2;
2506 target_ulong sc_hi3;
2507 target_ulong sc_lo3;
2508};
2509
2510struct sigframe {
2511 uint32_t sf_ass[4]; /* argument save space for o32 */
2512 uint32_t sf_code[2]; /* signal trampoline */
2513 struct target_sigcontext sf_sc;
Anthony Liguoric227f092009-10-01 16:12:16 -05002514 target_sigset_t sf_mask;
bellard106ec872006-06-27 21:08:10 +00002515};
2516
pbrook0b1bcb02009-04-21 01:41:10 +00002517struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002518 target_ulong tuc_flags;
2519 target_ulong tuc_link;
2520 target_stack_t tuc_stack;
Paul Brook94c54952009-07-09 18:40:15 +01002521 target_ulong pad0;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002522 struct target_sigcontext tuc_mcontext;
2523 target_sigset_t tuc_sigmask;
pbrook0b1bcb02009-04-21 01:41:10 +00002524};
2525
2526struct target_rt_sigframe {
2527 uint32_t rs_ass[4]; /* argument save space for o32 */
2528 uint32_t rs_code[2]; /* signal trampoline */
2529 struct target_siginfo rs_info;
2530 struct target_ucontext rs_uc;
2531};
2532
bellard106ec872006-06-27 21:08:10 +00002533/* Install trampoline to jump back from signal handler */
2534static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2535{
2536 int err;
2537
2538 /*
2539 * Set up the return code ...
2540 *
2541 * li v0, __NR__foo_sigreturn
2542 * syscall
2543 */
2544
2545 err = __put_user(0x24020000 + syscall, tramp + 0);
2546 err |= __put_user(0x0000000c , tramp + 1);
2547 /* flush_cache_sigtramp((unsigned long) tramp); */
2548 return err;
2549}
2550
2551static inline int
Andreas Färber05390242012-02-25 03:37:53 +01002552setup_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
bellard106ec872006-06-27 21:08:10 +00002553{
2554 int err = 0;
2555
thsb5dc7732008-06-27 10:02:35 +00002556 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002557
thsb5dc7732008-06-27 10:02:35 +00002558#define save_gp_reg(i) do { \
2559 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002560 } while(0)
2561 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2562 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2563 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2564 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2565 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2566 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2567 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2568 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2569 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002570#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002571
thsb5dc7732008-06-27 10:02:35 +00002572 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2573 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002574
2575 /* Not used yet, but might be useful if we ever have DSP suppport */
2576#if 0
2577 if (cpu_has_dsp) {
2578 err |= __put_user(mfhi1(), &sc->sc_hi1);
2579 err |= __put_user(mflo1(), &sc->sc_lo1);
2580 err |= __put_user(mfhi2(), &sc->sc_hi2);
2581 err |= __put_user(mflo2(), &sc->sc_lo2);
2582 err |= __put_user(mfhi3(), &sc->sc_hi3);
2583 err |= __put_user(mflo3(), &sc->sc_lo3);
2584 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2585 }
2586 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002587#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002588 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2589 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2590 if (cpu_has_dsp) {
2591 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2592 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2593 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2594 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2595 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2596 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2597 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2598 }
ths388bb212007-05-13 13:58:00 +00002599#endif
2600#endif
bellard106ec872006-06-27 21:08:10 +00002601
ths388bb212007-05-13 13:58:00 +00002602#if 0
bellard106ec872006-06-27 21:08:10 +00002603 err |= __put_user(!!used_math(), &sc->sc_used_math);
2604
2605 if (!used_math())
2606 goto out;
2607
2608 /*
2609 * Save FPU state to signal context. Signal handler will "inherit"
2610 * current FPU state.
2611 */
2612 preempt_disable();
2613
2614 if (!is_fpu_owner()) {
2615 own_fpu();
2616 restore_fp(current);
2617 }
2618 err |= save_fp_context(sc);
2619
2620 preempt_enable();
2621 out:
2622#endif
2623 return err;
2624}
2625
2626static inline int
Andreas Färber05390242012-02-25 03:37:53 +01002627restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
bellard106ec872006-06-27 21:08:10 +00002628{
2629 int err = 0;
2630
2631 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2632
thsb5dc7732008-06-27 10:02:35 +00002633 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2634 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002635
thsead93602007-09-06 00:18:15 +00002636#define restore_gp_reg(i) do { \
thsb5dc7732008-06-27 10:02:35 +00002637 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002638 } while(0)
2639 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2640 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2641 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2642 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2643 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2644 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2645 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2646 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2647 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2648 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2649 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002650#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002651
2652#if 0
2653 if (cpu_has_dsp) {
2654 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2655 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2656 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2657 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2658 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2659 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2660 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2661 }
ths388bb212007-05-13 13:58:00 +00002662#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002663 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2664 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2665 if (cpu_has_dsp) {
2666 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2667 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2668 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2669 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2670 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2671 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2672 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2673 }
ths388bb212007-05-13 13:58:00 +00002674#endif
bellard106ec872006-06-27 21:08:10 +00002675
2676 err |= __get_user(used_math, &sc->sc_used_math);
2677 conditional_used_math(used_math);
2678
2679 preempt_disable();
2680
2681 if (used_math()) {
2682 /* restore fpu context if we have used it before */
2683 own_fpu();
2684 err |= restore_fp_context(sc);
2685 } else {
2686 /* signal handler may have used FPU. Give it up. */
2687 lose_fpu();
2688 }
2689
2690 preempt_enable();
2691#endif
2692 return err;
2693}
2694/*
2695 * Determine which stack to use..
2696 */
bellard579a97f2007-11-11 14:26:47 +00002697static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01002698get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
bellard106ec872006-06-27 21:08:10 +00002699{
2700 unsigned long sp;
2701
2702 /* Default to using normal stack */
thsb5dc7732008-06-27 10:02:35 +00002703 sp = regs->active_tc.gpr[29];
bellard106ec872006-06-27 21:08:10 +00002704
2705 /*
Stefan Weil93148aa2012-02-26 18:46:12 +01002706 * FPU emulator may have its own trampoline active just
bellard106ec872006-06-27 21:08:10 +00002707 * above the user stack, 16-bytes before the next lowest
2708 * 16 byte boundary. Try to avoid trashing it.
2709 */
2710 sp -= 32;
2711
bellard106ec872006-06-27 21:08:10 +00002712 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002713 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
thsa04e1342007-09-27 13:57:58 +00002714 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2715 }
bellard106ec872006-06-27 21:08:10 +00002716
bellard579a97f2007-11-11 14:26:47 +00002717 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002718}
2719
bellard579a97f2007-11-11 14:26:47 +00002720/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00002721static void setup_frame(int sig, struct target_sigaction * ka,
Andreas Färber05390242012-02-25 03:37:53 +01002722 target_sigset_t *set, CPUMIPSState *regs)
bellard106ec872006-06-27 21:08:10 +00002723{
2724 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002725 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002726 int i;
2727
bellard579a97f2007-11-11 14:26:47 +00002728 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2729 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002730 goto give_sigsegv;
2731
2732 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2733
2734 if(setup_sigcontext(regs, &frame->sf_sc))
2735 goto give_sigsegv;
2736
2737 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2738 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2739 goto give_sigsegv;
2740 }
2741
2742 /*
2743 * Arguments to signal handler:
2744 *
2745 * a0 = signal number
2746 * a1 = 0 (should be cause)
2747 * a2 = pointer to struct sigcontext
2748 *
2749 * $25 and PC point to the signal handler, $29 points to the
2750 * struct sigframe.
2751 */
thsb5dc7732008-06-27 10:02:35 +00002752 regs->active_tc.gpr[ 4] = sig;
2753 regs->active_tc.gpr[ 5] = 0;
2754 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2755 regs->active_tc.gpr[29] = frame_addr;
2756 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002757 /* The original kernel code sets CP0_EPC to the handler
2758 * since it returns to userland using eret
2759 * we cannot do this here, and we must set PC directly */
thsb5dc7732008-06-27 10:02:35 +00002760 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002761 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002762 return;
2763
2764give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002765 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002766 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002767 return;
bellard106ec872006-06-27 21:08:10 +00002768}
2769
Andreas Färber05390242012-02-25 03:37:53 +01002770long do_sigreturn(CPUMIPSState *regs)
bellard106ec872006-06-27 21:08:10 +00002771{
ths388bb212007-05-13 13:58:00 +00002772 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002773 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002774 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05002775 target_sigset_t target_set;
ths388bb212007-05-13 13:58:00 +00002776 int i;
bellard106ec872006-06-27 21:08:10 +00002777
2778#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002779 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002780#endif
thsb5dc7732008-06-27 10:02:35 +00002781 frame_addr = regs->active_tc.gpr[29];
bellard579a97f2007-11-11 14:26:47 +00002782 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002783 goto badframe;
2784
ths388bb212007-05-13 13:58:00 +00002785 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002786 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2787 goto badframe;
ths388bb212007-05-13 13:58:00 +00002788 }
bellard106ec872006-06-27 21:08:10 +00002789
ths388bb212007-05-13 13:58:00 +00002790 target_to_host_sigset_internal(&blocked, &target_set);
2791 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002792
ths388bb212007-05-13 13:58:00 +00002793 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002794 goto badframe;
2795
2796#if 0
ths388bb212007-05-13 13:58:00 +00002797 /*
2798 * Don't let your children do this ...
2799 */
2800 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002801 "move\t$29, %0\n\t"
2802 "j\tsyscall_exit"
2803 :/* no outputs */
2804 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002805 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002806#endif
ths3b46e622007-09-17 08:09:54 +00002807
thsb5dc7732008-06-27 10:02:35 +00002808 regs->active_tc.PC = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002809 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002810 * maybe a problem with nested signals ? */
2811 regs->CP0_EPC = 0;
pbrook0b1bcb02009-04-21 01:41:10 +00002812 return -TARGET_QEMU_ESIGRETURN;
bellard106ec872006-06-27 21:08:10 +00002813
2814badframe:
ths388bb212007-05-13 13:58:00 +00002815 force_sig(TARGET_SIGSEGV/*, current*/);
2816 return 0;
bellard106ec872006-06-27 21:08:10 +00002817}
2818
pbrook624f7972008-05-31 16:11:38 +00002819static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002820 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01002821 target_sigset_t *set, CPUMIPSState *env)
bellard106ec872006-06-27 21:08:10 +00002822{
pbrook0b1bcb02009-04-21 01:41:10 +00002823 struct target_rt_sigframe *frame;
2824 abi_ulong frame_addr;
2825 int i;
2826
2827 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2828 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2829 goto give_sigsegv;
2830
2831 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2832
2833 copy_siginfo_to_user(&frame->rs_info, info);
2834
Aurelien Jarno60e99242010-03-29 02:12:51 +02002835 __put_user(0, &frame->rs_uc.tuc_flags);
2836 __put_user(0, &frame->rs_uc.tuc_link);
2837 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2838 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
pbrook0b1bcb02009-04-21 01:41:10 +00002839 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
Aurelien Jarno60e99242010-03-29 02:12:51 +02002840 &frame->rs_uc.tuc_stack.ss_flags);
pbrook0b1bcb02009-04-21 01:41:10 +00002841
Aurelien Jarno60e99242010-03-29 02:12:51 +02002842 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
pbrook0b1bcb02009-04-21 01:41:10 +00002843
2844 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002845 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
pbrook0b1bcb02009-04-21 01:41:10 +00002846 }
2847
2848 /*
2849 * Arguments to signal handler:
2850 *
2851 * a0 = signal number
2852 * a1 = pointer to struct siginfo
2853 * a2 = pointer to struct ucontext
2854 *
2855 * $25 and PC point to the signal handler, $29 points to the
2856 * struct sigframe.
2857 */
2858 env->active_tc.gpr[ 4] = sig;
2859 env->active_tc.gpr[ 5] = frame_addr
2860 + offsetof(struct target_rt_sigframe, rs_info);
2861 env->active_tc.gpr[ 6] = frame_addr
2862 + offsetof(struct target_rt_sigframe, rs_uc);
2863 env->active_tc.gpr[29] = frame_addr;
2864 env->active_tc.gpr[31] = frame_addr
2865 + offsetof(struct target_rt_sigframe, rs_code);
2866 /* The original kernel code sets CP0_EPC to the handler
2867 * since it returns to userland using eret
2868 * we cannot do this here, and we must set PC directly */
2869 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2870 unlock_user_struct(frame, frame_addr, 1);
2871 return;
2872
2873give_sigsegv:
2874 unlock_user_struct(frame, frame_addr, 1);
2875 force_sig(TARGET_SIGSEGV/*, current*/);
2876 return;
bellard106ec872006-06-27 21:08:10 +00002877}
2878
Andreas Färber05390242012-02-25 03:37:53 +01002879long do_rt_sigreturn(CPUMIPSState *env)
bellard106ec872006-06-27 21:08:10 +00002880{
pbrook0b1bcb02009-04-21 01:41:10 +00002881 struct target_rt_sigframe *frame;
2882 abi_ulong frame_addr;
2883 sigset_t blocked;
2884
2885#if defined(DEBUG_SIGNAL)
2886 fprintf(stderr, "do_rt_sigreturn\n");
2887#endif
2888 frame_addr = env->active_tc.gpr[29];
2889 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2890 goto badframe;
2891
Aurelien Jarno60e99242010-03-29 02:12:51 +02002892 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
pbrook0b1bcb02009-04-21 01:41:10 +00002893 sigprocmask(SIG_SETMASK, &blocked, NULL);
2894
Aurelien Jarno60e99242010-03-29 02:12:51 +02002895 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
pbrook0b1bcb02009-04-21 01:41:10 +00002896 goto badframe;
2897
2898 if (do_sigaltstack(frame_addr +
Aurelien Jarno60e99242010-03-29 02:12:51 +02002899 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
pbrook0b1bcb02009-04-21 01:41:10 +00002900 0, get_sp_from_cpustate(env)) == -EFAULT)
2901 goto badframe;
2902
2903 env->active_tc.PC = env->CP0_EPC;
2904 /* I am not sure this is right, but it seems to work
2905 * maybe a problem with nested signals ? */
2906 env->CP0_EPC = 0;
2907 return -TARGET_QEMU_ESIGRETURN;
2908
2909badframe:
2910 force_sig(TARGET_SIGSEGV/*, current*/);
2911 return 0;
bellard106ec872006-06-27 21:08:10 +00002912}
bellard6d5e2162004-09-30 22:04:13 +00002913
thsc3b5bc82007-12-02 06:31:25 +00002914#elif defined(TARGET_SH4)
2915
2916/*
2917 * code and data structures from linux kernel:
2918 * include/asm-sh/sigcontext.h
2919 * arch/sh/kernel/signal.c
2920 */
2921
2922struct target_sigcontext {
2923 target_ulong oldmask;
2924
2925 /* CPU registers */
2926 target_ulong sc_gregs[16];
2927 target_ulong sc_pc;
2928 target_ulong sc_pr;
2929 target_ulong sc_sr;
2930 target_ulong sc_gbr;
2931 target_ulong sc_mach;
2932 target_ulong sc_macl;
2933
2934 /* FPU registers */
2935 target_ulong sc_fpregs[16];
2936 target_ulong sc_xfpregs[16];
2937 unsigned int sc_fpscr;
2938 unsigned int sc_fpul;
2939 unsigned int sc_ownedfp;
2940};
2941
2942struct target_sigframe
2943{
2944 struct target_sigcontext sc;
2945 target_ulong extramask[TARGET_NSIG_WORDS-1];
2946 uint16_t retcode[3];
2947};
2948
2949
2950struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002951 target_ulong tuc_flags;
2952 struct target_ucontext *tuc_link;
2953 target_stack_t tuc_stack;
2954 struct target_sigcontext tuc_mcontext;
2955 target_sigset_t tuc_sigmask; /* mask last for extensibility */
thsc3b5bc82007-12-02 06:31:25 +00002956};
2957
2958struct target_rt_sigframe
2959{
2960 struct target_siginfo info;
2961 struct target_ucontext uc;
2962 uint16_t retcode[3];
2963};
2964
2965
2966#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2967#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2968
pbrook624f7972008-05-31 16:11:38 +00002969static abi_ulong get_sigframe(struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002970 unsigned long sp, size_t frame_size)
2971{
pbrook624f7972008-05-31 16:11:38 +00002972 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
thsc3b5bc82007-12-02 06:31:25 +00002973 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2974 }
2975
2976 return (sp - frame_size) & -8ul;
2977}
2978
2979static int setup_sigcontext(struct target_sigcontext *sc,
Andreas Färber05390242012-02-25 03:37:53 +01002980 CPUSH4State *regs, unsigned long mask)
thsc3b5bc82007-12-02 06:31:25 +00002981{
2982 int err = 0;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002983 int i;
thsc3b5bc82007-12-02 06:31:25 +00002984
2985#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2986 COPY(gregs[0]); COPY(gregs[1]);
2987 COPY(gregs[2]); COPY(gregs[3]);
2988 COPY(gregs[4]); COPY(gregs[5]);
2989 COPY(gregs[6]); COPY(gregs[7]);
2990 COPY(gregs[8]); COPY(gregs[9]);
2991 COPY(gregs[10]); COPY(gregs[11]);
2992 COPY(gregs[12]); COPY(gregs[13]);
2993 COPY(gregs[14]); COPY(gregs[15]);
2994 COPY(gbr); COPY(mach);
2995 COPY(macl); COPY(pr);
2996 COPY(sr); COPY(pc);
2997#undef COPY
2998
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002999 for (i=0; i<16; i++) {
3000 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
3001 }
3002 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
3003 err |= __put_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00003004
3005 /* non-iBCS2 extensions.. */
3006 err |= __put_user(mask, &sc->oldmask);
3007
3008 return err;
3009}
3010
Andreas Färber05390242012-02-25 03:37:53 +01003011static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003012 target_ulong *r0_p)
thsc3b5bc82007-12-02 06:31:25 +00003013{
3014 unsigned int err = 0;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003015 int i;
thsc3b5bc82007-12-02 06:31:25 +00003016
3017#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
3018 COPY(gregs[1]);
3019 COPY(gregs[2]); COPY(gregs[3]);
3020 COPY(gregs[4]); COPY(gregs[5]);
3021 COPY(gregs[6]); COPY(gregs[7]);
3022 COPY(gregs[8]); COPY(gregs[9]);
3023 COPY(gregs[10]); COPY(gregs[11]);
3024 COPY(gregs[12]); COPY(gregs[13]);
3025 COPY(gregs[14]); COPY(gregs[15]);
3026 COPY(gbr); COPY(mach);
3027 COPY(macl); COPY(pr);
3028 COPY(sr); COPY(pc);
3029#undef COPY
3030
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003031 for (i=0; i<16; i++) {
3032 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3033 }
3034 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3035 err |= __get_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00003036
3037 regs->tra = -1; /* disable syscall checks */
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003038 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
thsc3b5bc82007-12-02 06:31:25 +00003039 return err;
3040}
3041
pbrook624f7972008-05-31 16:11:38 +00003042static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003043 target_sigset_t *set, CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003044{
3045 struct target_sigframe *frame;
3046 abi_ulong frame_addr;
3047 int i;
3048 int err = 0;
3049 int signal;
3050
3051 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3052 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3053 goto give_sigsegv;
3054
3055 signal = current_exec_domain_sig(sig);
3056
3057 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3058
3059 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3060 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3061 }
3062
3063 /* Set up to return from userspace. If provided, use a stub
3064 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00003065 if (ka->sa_flags & TARGET_SA_RESTORER) {
3066 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00003067 } else {
3068 /* Generate return code (system call to sigreturn) */
3069 err |= __put_user(MOVW(2), &frame->retcode[0]);
3070 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3071 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3072 regs->pr = (unsigned long) frame->retcode;
3073 }
3074
3075 if (err)
3076 goto give_sigsegv;
3077
3078 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003079 regs->gregs[15] = frame_addr;
thsc3b5bc82007-12-02 06:31:25 +00003080 regs->gregs[4] = signal; /* Arg for signal handler */
3081 regs->gregs[5] = 0;
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003082 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
pbrook624f7972008-05-31 16:11:38 +00003083 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00003084
3085 unlock_user_struct(frame, frame_addr, 1);
3086 return;
3087
3088give_sigsegv:
3089 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02003090 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00003091}
3092
pbrook624f7972008-05-31 16:11:38 +00003093static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003094 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003095 target_sigset_t *set, CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003096{
3097 struct target_rt_sigframe *frame;
3098 abi_ulong frame_addr;
3099 int i;
3100 int err = 0;
3101 int signal;
3102
3103 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3104 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3105 goto give_sigsegv;
3106
3107 signal = current_exec_domain_sig(sig);
3108
3109 err |= copy_siginfo_to_user(&frame->info, info);
3110
3111 /* Create the ucontext. */
Aurelien Jarno60e99242010-03-29 02:12:51 +02003112 err |= __put_user(0, &frame->uc.tuc_flags);
3113 err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
balrog526ccb72008-07-16 12:13:52 +00003114 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
Aurelien Jarno60e99242010-03-29 02:12:51 +02003115 &frame->uc.tuc_stack.ss_sp);
thsc3b5bc82007-12-02 06:31:25 +00003116 err |= __put_user(sas_ss_flags(regs->gregs[15]),
Aurelien Jarno60e99242010-03-29 02:12:51 +02003117 &frame->uc.tuc_stack.ss_flags);
thsc3b5bc82007-12-02 06:31:25 +00003118 err |= __put_user(target_sigaltstack_used.ss_size,
Aurelien Jarno60e99242010-03-29 02:12:51 +02003119 &frame->uc.tuc_stack.ss_size);
3120 err |= setup_sigcontext(&frame->uc.tuc_mcontext,
thsc3b5bc82007-12-02 06:31:25 +00003121 regs, set->sig[0]);
3122 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02003123 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
thsc3b5bc82007-12-02 06:31:25 +00003124 }
3125
3126 /* Set up to return from userspace. If provided, use a stub
3127 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00003128 if (ka->sa_flags & TARGET_SA_RESTORER) {
3129 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00003130 } else {
3131 /* Generate return code (system call to sigreturn) */
3132 err |= __put_user(MOVW(2), &frame->retcode[0]);
3133 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3134 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3135 regs->pr = (unsigned long) frame->retcode;
3136 }
3137
3138 if (err)
3139 goto give_sigsegv;
3140
3141 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003142 regs->gregs[15] = frame_addr;
thsc3b5bc82007-12-02 06:31:25 +00003143 regs->gregs[4] = signal; /* Arg for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003144 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3145 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
pbrook624f7972008-05-31 16:11:38 +00003146 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00003147
3148 unlock_user_struct(frame, frame_addr, 1);
3149 return;
3150
3151give_sigsegv:
3152 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02003153 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00003154}
3155
Andreas Färber05390242012-02-25 03:37:53 +01003156long do_sigreturn(CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003157{
3158 struct target_sigframe *frame;
3159 abi_ulong frame_addr;
3160 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05003161 target_sigset_t target_set;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003162 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00003163 int i;
3164 int err = 0;
3165
3166#if defined(DEBUG_SIGNAL)
3167 fprintf(stderr, "do_sigreturn\n");
3168#endif
3169 frame_addr = regs->gregs[15];
3170 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3171 goto badframe;
3172
3173 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3174 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3175 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3176 }
3177
3178 if (err)
3179 goto badframe;
3180
3181 target_to_host_sigset_internal(&blocked, &target_set);
3182 sigprocmask(SIG_SETMASK, &blocked, NULL);
3183
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003184 if (restore_sigcontext(regs, &frame->sc, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003185 goto badframe;
3186
3187 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003188 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003189
3190badframe:
3191 unlock_user_struct(frame, frame_addr, 0);
3192 force_sig(TARGET_SIGSEGV);
3193 return 0;
3194}
3195
Andreas Färber05390242012-02-25 03:37:53 +01003196long do_rt_sigreturn(CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003197{
3198 struct target_rt_sigframe *frame;
3199 abi_ulong frame_addr;
3200 sigset_t blocked;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003201 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00003202
3203#if defined(DEBUG_SIGNAL)
3204 fprintf(stderr, "do_rt_sigreturn\n");
3205#endif
3206 frame_addr = regs->gregs[15];
3207 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3208 goto badframe;
3209
Aurelien Jarno60e99242010-03-29 02:12:51 +02003210 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
thsc3b5bc82007-12-02 06:31:25 +00003211 sigprocmask(SIG_SETMASK, &blocked, NULL);
3212
Aurelien Jarno60e99242010-03-29 02:12:51 +02003213 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003214 goto badframe;
3215
3216 if (do_sigaltstack(frame_addr +
Aurelien Jarno60e99242010-03-29 02:12:51 +02003217 offsetof(struct target_rt_sigframe, uc.tuc_stack),
thsc3b5bc82007-12-02 06:31:25 +00003218 0, get_sp_from_cpustate(regs)) == -EFAULT)
3219 goto badframe;
3220
3221 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003222 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003223
3224badframe:
3225 unlock_user_struct(frame, frame_addr, 0);
3226 force_sig(TARGET_SIGSEGV);
3227 return 0;
3228}
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003229#elif defined(TARGET_MICROBLAZE)
3230
3231struct target_sigcontext {
3232 struct target_pt_regs regs; /* needs to be first */
3233 uint32_t oldmask;
3234};
3235
Edgar E. Iglesiasb2178702010-07-23 09:30:37 +02003236struct target_stack_t {
3237 abi_ulong ss_sp;
3238 int ss_flags;
3239 unsigned int ss_size;
3240};
3241
3242struct target_ucontext {
Richard Hendersonf711df62010-11-22 14:57:52 -08003243 abi_ulong tuc_flags;
3244 abi_ulong tuc_link;
3245 struct target_stack_t tuc_stack;
3246 struct target_sigcontext tuc_mcontext;
3247 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
Edgar E. Iglesiasb2178702010-07-23 09:30:37 +02003248};
3249
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003250/* Signal frames. */
3251struct target_signal_frame {
Edgar E. Iglesiasb2178702010-07-23 09:30:37 +02003252 struct target_ucontext uc;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003253 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3254 uint32_t tramp[2];
3255};
3256
3257struct rt_signal_frame {
3258 struct siginfo info;
3259 struct ucontext uc;
3260 uint32_t tramp[2];
3261};
3262
Andreas Färber05390242012-02-25 03:37:53 +01003263static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003264{
3265 __put_user(env->regs[0], &sc->regs.r0);
3266 __put_user(env->regs[1], &sc->regs.r1);
3267 __put_user(env->regs[2], &sc->regs.r2);
3268 __put_user(env->regs[3], &sc->regs.r3);
3269 __put_user(env->regs[4], &sc->regs.r4);
3270 __put_user(env->regs[5], &sc->regs.r5);
3271 __put_user(env->regs[6], &sc->regs.r6);
3272 __put_user(env->regs[7], &sc->regs.r7);
3273 __put_user(env->regs[8], &sc->regs.r8);
3274 __put_user(env->regs[9], &sc->regs.r9);
3275 __put_user(env->regs[10], &sc->regs.r10);
3276 __put_user(env->regs[11], &sc->regs.r11);
3277 __put_user(env->regs[12], &sc->regs.r12);
3278 __put_user(env->regs[13], &sc->regs.r13);
3279 __put_user(env->regs[14], &sc->regs.r14);
3280 __put_user(env->regs[15], &sc->regs.r15);
3281 __put_user(env->regs[16], &sc->regs.r16);
3282 __put_user(env->regs[17], &sc->regs.r17);
3283 __put_user(env->regs[18], &sc->regs.r18);
3284 __put_user(env->regs[19], &sc->regs.r19);
3285 __put_user(env->regs[20], &sc->regs.r20);
3286 __put_user(env->regs[21], &sc->regs.r21);
3287 __put_user(env->regs[22], &sc->regs.r22);
3288 __put_user(env->regs[23], &sc->regs.r23);
3289 __put_user(env->regs[24], &sc->regs.r24);
3290 __put_user(env->regs[25], &sc->regs.r25);
3291 __put_user(env->regs[26], &sc->regs.r26);
3292 __put_user(env->regs[27], &sc->regs.r27);
3293 __put_user(env->regs[28], &sc->regs.r28);
3294 __put_user(env->regs[29], &sc->regs.r29);
3295 __put_user(env->regs[30], &sc->regs.r30);
3296 __put_user(env->regs[31], &sc->regs.r31);
3297 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3298}
3299
Andreas Färber05390242012-02-25 03:37:53 +01003300static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003301{
3302 __get_user(env->regs[0], &sc->regs.r0);
3303 __get_user(env->regs[1], &sc->regs.r1);
3304 __get_user(env->regs[2], &sc->regs.r2);
3305 __get_user(env->regs[3], &sc->regs.r3);
3306 __get_user(env->regs[4], &sc->regs.r4);
3307 __get_user(env->regs[5], &sc->regs.r5);
3308 __get_user(env->regs[6], &sc->regs.r6);
3309 __get_user(env->regs[7], &sc->regs.r7);
3310 __get_user(env->regs[8], &sc->regs.r8);
3311 __get_user(env->regs[9], &sc->regs.r9);
3312 __get_user(env->regs[10], &sc->regs.r10);
3313 __get_user(env->regs[11], &sc->regs.r11);
3314 __get_user(env->regs[12], &sc->regs.r12);
3315 __get_user(env->regs[13], &sc->regs.r13);
3316 __get_user(env->regs[14], &sc->regs.r14);
3317 __get_user(env->regs[15], &sc->regs.r15);
3318 __get_user(env->regs[16], &sc->regs.r16);
3319 __get_user(env->regs[17], &sc->regs.r17);
3320 __get_user(env->regs[18], &sc->regs.r18);
3321 __get_user(env->regs[19], &sc->regs.r19);
3322 __get_user(env->regs[20], &sc->regs.r20);
3323 __get_user(env->regs[21], &sc->regs.r21);
3324 __get_user(env->regs[22], &sc->regs.r22);
3325 __get_user(env->regs[23], &sc->regs.r23);
3326 __get_user(env->regs[24], &sc->regs.r24);
3327 __get_user(env->regs[25], &sc->regs.r25);
3328 __get_user(env->regs[26], &sc->regs.r26);
3329 __get_user(env->regs[27], &sc->regs.r27);
3330 __get_user(env->regs[28], &sc->regs.r28);
3331 __get_user(env->regs[29], &sc->regs.r29);
3332 __get_user(env->regs[30], &sc->regs.r30);
3333 __get_user(env->regs[31], &sc->regs.r31);
3334 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3335}
3336
3337static abi_ulong get_sigframe(struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003338 CPUMBState *env, int frame_size)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003339{
3340 abi_ulong sp = env->regs[1];
3341
3342 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3343 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3344
3345 return ((sp - frame_size) & -8UL);
3346}
3347
3348static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003349 target_sigset_t *set, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003350{
3351 struct target_signal_frame *frame;
3352 abi_ulong frame_addr;
3353 int err = 0;
3354 int i;
3355
3356 frame_addr = get_sigframe(ka, env, sizeof *frame);
3357 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3358 goto badframe;
3359
3360 /* Save the mask. */
Richard Hendersonf711df62010-11-22 14:57:52 -08003361 err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003362 if (err)
3363 goto badframe;
3364
3365 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3366 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3367 goto badframe;
3368 }
3369
Richard Hendersonf711df62010-11-22 14:57:52 -08003370 setup_sigcontext(&frame->uc.tuc_mcontext, env);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003371
3372 /* Set up to return from userspace. If provided, use a stub
3373 already in userspace. */
3374 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3375 if (ka->sa_flags & TARGET_SA_RESTORER) {
3376 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3377 } else {
3378 uint32_t t;
3379 /* Note, these encodings are _big endian_! */
3380 /* addi r12, r0, __NR_sigreturn */
3381 t = 0x31800000UL | TARGET_NR_sigreturn;
3382 err |= __put_user(t, frame->tramp + 0);
3383 /* brki r14, 0x8 */
3384 t = 0xb9cc0008UL;
3385 err |= __put_user(t, frame->tramp + 1);
3386
3387 /* Return from sighandler will jump to the tramp.
3388 Negative 8 offset because return is rtsd r15, 8 */
3389 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3390 }
3391
3392 if (err)
3393 goto badframe;
3394
3395 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003396 env->regs[1] = frame_addr;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003397 /* Signal handler args: */
3398 env->regs[5] = sig; /* Arg 0: signum */
Edgar E. Iglesias187b4e02010-07-15 15:32:51 +02003399 env->regs[6] = 0;
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003400 /* arg 1: sigcontext */
3401 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003402
3403 /* Offset of 4 to handle microblaze rtid r14, 0 */
3404 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3405
3406 unlock_user_struct(frame, frame_addr, 1);
3407 return;
3408 badframe:
3409 unlock_user_struct(frame, frame_addr, 1);
3410 force_sig(TARGET_SIGSEGV);
3411}
3412
3413static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003414 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003415 target_sigset_t *set, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003416{
3417 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3418}
3419
Andreas Färber05390242012-02-25 03:37:53 +01003420long do_sigreturn(CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003421{
3422 struct target_signal_frame *frame;
3423 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003424 target_sigset_t target_set;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003425 sigset_t set;
3426 int i;
3427
3428 frame_addr = env->regs[R_SP];
3429 /* Make sure the guest isn't playing games. */
3430 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3431 goto badframe;
3432
3433 /* Restore blocked signals */
Richard Hendersonf711df62010-11-22 14:57:52 -08003434 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003435 goto badframe;
3436 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3437 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3438 goto badframe;
3439 }
3440 target_to_host_sigset_internal(&set, &target_set);
3441 sigprocmask(SIG_SETMASK, &set, NULL);
3442
Richard Hendersonf711df62010-11-22 14:57:52 -08003443 restore_sigcontext(&frame->uc.tuc_mcontext, env);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003444 /* We got here through a sigreturn syscall, our path back is via an
3445 rtb insn so setup r14 for that. */
3446 env->regs[14] = env->sregs[SR_PC];
3447
3448 unlock_user_struct(frame, frame_addr, 0);
3449 return env->regs[10];
3450 badframe:
3451 unlock_user_struct(frame, frame_addr, 0);
3452 force_sig(TARGET_SIGSEGV);
3453}
3454
Andreas Färber05390242012-02-25 03:37:53 +01003455long do_rt_sigreturn(CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003456{
3457 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3458 return -TARGET_ENOSYS;
3459}
3460
edgar_iglb6d3abd2008-02-28 11:29:27 +00003461#elif defined(TARGET_CRIS)
3462
3463struct target_sigcontext {
3464 struct target_pt_regs regs; /* needs to be first */
3465 uint32_t oldmask;
3466 uint32_t usp; /* usp before stacking this gunk on it */
3467};
3468
3469/* Signal frames. */
3470struct target_signal_frame {
3471 struct target_sigcontext sc;
3472 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3473 uint8_t retcode[8]; /* Trampoline code. */
3474};
3475
3476struct rt_signal_frame {
3477 struct siginfo *pinfo;
3478 void *puc;
3479 struct siginfo info;
3480 struct ucontext uc;
3481 uint8_t retcode[8]; /* Trampoline code. */
3482};
3483
Andreas Färber05390242012-02-25 03:37:53 +01003484static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003485{
edgar_igl9664d922008-03-03 22:23:53 +00003486 __put_user(env->regs[0], &sc->regs.r0);
3487 __put_user(env->regs[1], &sc->regs.r1);
3488 __put_user(env->regs[2], &sc->regs.r2);
3489 __put_user(env->regs[3], &sc->regs.r3);
3490 __put_user(env->regs[4], &sc->regs.r4);
3491 __put_user(env->regs[5], &sc->regs.r5);
3492 __put_user(env->regs[6], &sc->regs.r6);
3493 __put_user(env->regs[7], &sc->regs.r7);
3494 __put_user(env->regs[8], &sc->regs.r8);
3495 __put_user(env->regs[9], &sc->regs.r9);
3496 __put_user(env->regs[10], &sc->regs.r10);
3497 __put_user(env->regs[11], &sc->regs.r11);
3498 __put_user(env->regs[12], &sc->regs.r12);
3499 __put_user(env->regs[13], &sc->regs.r13);
3500 __put_user(env->regs[14], &sc->usp);
3501 __put_user(env->regs[15], &sc->regs.acr);
3502 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3503 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3504 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003505}
edgar_igl9664d922008-03-03 22:23:53 +00003506
Andreas Färber05390242012-02-25 03:37:53 +01003507static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003508{
edgar_igl9664d922008-03-03 22:23:53 +00003509 __get_user(env->regs[0], &sc->regs.r0);
3510 __get_user(env->regs[1], &sc->regs.r1);
3511 __get_user(env->regs[2], &sc->regs.r2);
3512 __get_user(env->regs[3], &sc->regs.r3);
3513 __get_user(env->regs[4], &sc->regs.r4);
3514 __get_user(env->regs[5], &sc->regs.r5);
3515 __get_user(env->regs[6], &sc->regs.r6);
3516 __get_user(env->regs[7], &sc->regs.r7);
3517 __get_user(env->regs[8], &sc->regs.r8);
3518 __get_user(env->regs[9], &sc->regs.r9);
3519 __get_user(env->regs[10], &sc->regs.r10);
3520 __get_user(env->regs[11], &sc->regs.r11);
3521 __get_user(env->regs[12], &sc->regs.r12);
3522 __get_user(env->regs[13], &sc->regs.r13);
3523 __get_user(env->regs[14], &sc->usp);
3524 __get_user(env->regs[15], &sc->regs.acr);
3525 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3526 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3527 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003528}
3529
Andreas Färber05390242012-02-25 03:37:53 +01003530static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003531{
edgar_igl9664d922008-03-03 22:23:53 +00003532 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003533 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00003534 sp = (env->regs[R_SP] & ~3);
3535 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003536}
3537
pbrook624f7972008-05-31 16:11:38 +00003538static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003539 target_sigset_t *set, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003540{
3541 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003542 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003543 int err = 0;
3544 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003545
edgar_igl9664d922008-03-03 22:23:53 +00003546 frame_addr = get_sigframe(env, sizeof *frame);
3547 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003548 goto badframe;
3549
3550 /*
3551 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3552 * use this trampoline anymore but it sets it up for GDB.
3553 * In QEMU, using the trampoline simplifies things a bit so we use it.
3554 *
3555 * This is movu.w __NR_sigreturn, r9; break 13;
3556 */
3557 err |= __put_user(0x9c5f, frame->retcode+0);
3558 err |= __put_user(TARGET_NR_sigreturn,
3559 frame->retcode+2);
3560 err |= __put_user(0xe93d, frame->retcode+4);
3561
3562 /* Save the mask. */
3563 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3564 if (err)
3565 goto badframe;
3566
3567 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3568 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3569 goto badframe;
3570 }
3571
3572 setup_sigcontext(&frame->sc, env);
3573
3574 /* Move the stack and setup the arguments for the handler. */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003575 env->regs[R_SP] = frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003576 env->regs[10] = sig;
pbrook624f7972008-05-31 16:11:38 +00003577 env->pc = (unsigned long) ka->_sa_handler;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003578 /* Link SRP so the guest returns through the trampoline. */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003579 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003580
edgar_igl9664d922008-03-03 22:23:53 +00003581 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003582 return;
3583 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003584 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003585 force_sig(TARGET_SIGSEGV);
3586}
3587
pbrook624f7972008-05-31 16:11:38 +00003588static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003589 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003590 target_sigset_t *set, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003591{
3592 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3593}
3594
Andreas Färber05390242012-02-25 03:37:53 +01003595long do_sigreturn(CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003596{
3597 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003598 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003599 target_sigset_t target_set;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003600 sigset_t set;
3601 int i;
3602
edgar_igl9664d922008-03-03 22:23:53 +00003603 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003604 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003605 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003606 goto badframe;
3607
3608 /* Restore blocked signals */
3609 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3610 goto badframe;
3611 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3612 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3613 goto badframe;
3614 }
3615 target_to_host_sigset_internal(&set, &target_set);
3616 sigprocmask(SIG_SETMASK, &set, NULL);
3617
3618 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003619 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003620 return env->regs[10];
3621 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003622 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003623 force_sig(TARGET_SIGSEGV);
3624}
3625
Andreas Färber05390242012-02-25 03:37:53 +01003626long do_rt_sigreturn(CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003627{
3628 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3629 return -TARGET_ENOSYS;
3630}
thsc3b5bc82007-12-02 06:31:25 +00003631
Jia Liud9627832012-07-20 15:50:52 +08003632#elif defined(TARGET_OPENRISC)
3633
3634struct target_sigcontext {
3635 struct target_pt_regs regs;
3636 abi_ulong oldmask;
3637 abi_ulong usp;
3638};
3639
3640struct target_ucontext {
3641 abi_ulong tuc_flags;
3642 abi_ulong tuc_link;
3643 target_stack_t tuc_stack;
3644 struct target_sigcontext tuc_mcontext;
3645 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3646};
3647
3648struct target_rt_sigframe {
3649 abi_ulong pinfo;
3650 uint64_t puc;
3651 struct target_siginfo info;
3652 struct target_sigcontext sc;
3653 struct target_ucontext uc;
3654 unsigned char retcode[16]; /* trampoline code */
3655};
3656
3657/* This is the asm-generic/ucontext.h version */
3658#if 0
3659static int restore_sigcontext(CPUOpenRISCState *regs,
3660 struct target_sigcontext *sc)
3661{
3662 unsigned int err = 0;
3663 unsigned long old_usp;
3664
3665 /* Alwys make any pending restarted system call return -EINTR */
3666 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3667
3668 /* restore the regs from &sc->regs (same as sc, since regs is first)
3669 * (sc is already checked for VERIFY_READ since the sigframe was
3670 * checked in sys_sigreturn previously)
3671 */
3672
3673 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3674 goto badframe;
3675 }
3676
3677 /* make sure the U-flag is set so user-mode cannot fool us */
3678
3679 regs->sr &= ~SR_SM;
3680
3681 /* restore the old USP as it was before we stacked the sc etc.
3682 * (we cannot just pop the sigcontext since we aligned the sp and
3683 * stuff after pushing it)
3684 */
3685
3686 err |= __get_user(old_usp, &sc->usp);
3687 phx_signal("old_usp 0x%lx", old_usp);
3688
3689 __PHX__ REALLY /* ??? */
3690 wrusp(old_usp);
3691 regs->gpr[1] = old_usp;
3692
3693 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3694 * after this completes, but we don't use that mechanism. maybe we can
3695 * use it now ?
3696 */
3697
3698 return err;
3699
3700badframe:
3701 return 1;
3702}
3703#endif
3704
3705/* Set up a signal frame. */
3706
3707static int setup_sigcontext(struct target_sigcontext *sc,
3708 CPUOpenRISCState *regs,
3709 unsigned long mask)
3710{
3711 int err = 0;
3712 unsigned long usp = regs->gpr[1];
3713
3714 /* copy the regs. they are first in sc so we can use sc directly */
3715
3716 /*err |= copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
3717
3718 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3719 the signal handler. The frametype will be restored to its previous
3720 value in restore_sigcontext. */
3721 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3722
3723 /* then some other stuff */
3724 err |= __put_user(mask, &sc->oldmask);
3725 err |= __put_user(usp, &sc->usp); return err;
3726}
3727
3728static inline unsigned long align_sigframe(unsigned long sp)
3729{
3730 unsigned long i;
3731 i = sp & ~3UL;
3732 return i;
3733}
3734
3735static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3736 CPUOpenRISCState *regs,
3737 size_t frame_size)
3738{
3739 unsigned long sp = regs->gpr[1];
3740 int onsigstack = on_sig_stack(sp);
3741
3742 /* redzone */
3743 /* This is the X/Open sanctioned signal stack switching. */
3744 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
3745 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3746 }
3747
3748 sp = align_sigframe(sp - frame_size);
3749
3750 /*
3751 * If we are on the alternate signal stack and would overflow it, don't.
3752 * Return an always-bogus address instead so we will die with SIGSEGV.
3753 */
3754
3755 if (onsigstack && !likely(on_sig_stack(sp))) {
3756 return -1L;
3757 }
3758
3759 return sp;
3760}
3761
3762static void setup_frame(int sig, struct target_sigaction *ka,
3763 target_sigset_t *set, CPUOpenRISCState *env)
3764{
3765 qemu_log("Not implement.\n");
3766}
3767
3768static void setup_rt_frame(int sig, struct target_sigaction *ka,
3769 target_siginfo_t *info,
3770 target_sigset_t *set, CPUOpenRISCState *env)
3771{
3772 int err = 0;
3773 abi_ulong frame_addr;
3774 unsigned long return_ip;
3775 struct target_rt_sigframe *frame;
3776 abi_ulong info_addr, uc_addr;
3777
3778 frame_addr = get_sigframe(ka, env, sizeof *frame);
3779
3780 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3781 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3782 goto give_sigsegv;
3783 }
3784
3785 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
3786 err |= __put_user(info_addr, &frame->pinfo);
3787 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
3788 err |= __put_user(uc_addr, &frame->puc);
3789
3790 if (ka->sa_flags & SA_SIGINFO) {
3791 err |= copy_siginfo_to_user(&frame->info, info);
3792 }
3793 if (err) {
3794 goto give_sigsegv;
3795 }
3796
3797 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
3798 err |= __put_user(0, &frame->uc.tuc_flags);
3799 err |= __put_user(0, &frame->uc.tuc_link);
3800 err |= __put_user(target_sigaltstack_used.ss_sp,
3801 &frame->uc.tuc_stack.ss_sp);
3802 err |= __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
3803 err |= __put_user(target_sigaltstack_used.ss_size,
3804 &frame->uc.tuc_stack.ss_size);
3805 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
3806
3807 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
3808
3809 if (err) {
3810 goto give_sigsegv;
3811 }
3812
3813 /* trampoline - the desired return ip is the retcode itself */
3814 return_ip = (unsigned long)&frame->retcode;
3815 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
3816 err |= __put_user(0xa960, (short *)(frame->retcode + 0));
3817 err |= __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
3818 err |= __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
3819 err |= __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
3820
3821 if (err) {
3822 goto give_sigsegv;
3823 }
3824
3825 /* TODO what is the current->exec_domain stuff and invmap ? */
3826
3827 /* Set up registers for signal handler */
3828 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
3829 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
3830 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
3831 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
3832 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
3833
3834 /* actually move the usp to reflect the stacked frame */
3835 env->gpr[1] = (unsigned long)frame;
3836
3837 return;
3838
3839give_sigsegv:
3840 unlock_user_struct(frame, frame_addr, 1);
3841 if (sig == TARGET_SIGSEGV) {
3842 ka->_sa_handler = TARGET_SIG_DFL;
3843 }
3844 force_sig(TARGET_SIGSEGV);
3845}
3846
3847long do_sigreturn(CPUOpenRISCState *env)
3848{
3849
3850 qemu_log("do_sigreturn: not implemented\n");
3851 return -TARGET_ENOSYS;
3852}
3853
3854long do_rt_sigreturn(CPUOpenRISCState *env)
3855{
3856 qemu_log("do_rt_sigreturn: not implemented\n");
3857 return -TARGET_ENOSYS;
3858}
3859/* TARGET_OPENRISC */
3860
Ulrich Hechta4c075f2009-07-24 16:57:31 +02003861#elif defined(TARGET_S390X)
3862
3863#define __NUM_GPRS 16
3864#define __NUM_FPRS 16
3865#define __NUM_ACRS 16
3866
3867#define S390_SYSCALL_SIZE 2
3868#define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
3869
3870#define _SIGCONTEXT_NSIG 64
3871#define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
3872#define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3873#define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3874#define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
3875#define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
3876
3877typedef struct {
3878 target_psw_t psw;
3879 target_ulong gprs[__NUM_GPRS];
3880 unsigned int acrs[__NUM_ACRS];
3881} target_s390_regs_common;
3882
3883typedef struct {
3884 unsigned int fpc;
3885 double fprs[__NUM_FPRS];
3886} target_s390_fp_regs;
3887
3888typedef struct {
3889 target_s390_regs_common regs;
3890 target_s390_fp_regs fpregs;
3891} target_sigregs;
3892
3893struct target_sigcontext {
3894 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
3895 target_sigregs *sregs;
3896};
3897
3898typedef struct {
3899 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3900 struct target_sigcontext sc;
3901 target_sigregs sregs;
3902 int signo;
3903 uint8_t retcode[S390_SYSCALL_SIZE];
3904} sigframe;
3905
3906struct target_ucontext {
Peter Maydell6fea2ea2011-07-12 21:27:15 +01003907 target_ulong tuc_flags;
3908 struct target_ucontext *tuc_link;
3909 target_stack_t tuc_stack;
3910 target_sigregs tuc_mcontext;
3911 target_sigset_t tuc_sigmask; /* mask last for extensibility */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02003912};
3913
3914typedef struct {
3915 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3916 uint8_t retcode[S390_SYSCALL_SIZE];
3917 struct target_siginfo info;
3918 struct target_ucontext uc;
3919} rt_sigframe;
3920
3921static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01003922get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02003923{
3924 abi_ulong sp;
3925
3926 /* Default to using normal stack */
3927 sp = env->regs[15];
3928
3929 /* This is the X/Open sanctioned signal stack switching. */
3930 if (ka->sa_flags & TARGET_SA_ONSTACK) {
3931 if (!sas_ss_flags(sp)) {
3932 sp = target_sigaltstack_used.ss_sp +
3933 target_sigaltstack_used.ss_size;
3934 }
3935 }
3936
3937 /* This is the legacy signal stack switching. */
3938 else if (/* FIXME !user_mode(regs) */ 0 &&
3939 !(ka->sa_flags & TARGET_SA_RESTORER) &&
3940 ka->sa_restorer) {
3941 sp = (abi_ulong) ka->sa_restorer;
3942 }
3943
3944 return (sp - frame_size) & -8ul;
3945}
3946
Andreas Färber05390242012-02-25 03:37:53 +01003947static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02003948{
3949 int i;
3950 //save_access_regs(current->thread.acrs); FIXME
3951
3952 /* Copy a 'clean' PSW mask to the user to avoid leaking
3953 information about whether PER is currently on. */
3954 __put_user(env->psw.mask, &sregs->regs.psw.mask);
3955 __put_user(env->psw.addr, &sregs->regs.psw.addr);
3956 for (i = 0; i < 16; i++) {
3957 __put_user(env->regs[i], &sregs->regs.gprs[i]);
3958 }
3959 for (i = 0; i < 16; i++) {
3960 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
3961 }
3962 /*
3963 * We have to store the fp registers to current->thread.fp_regs
3964 * to merge them with the emulated registers.
3965 */
3966 //save_fp_regs(&current->thread.fp_regs); FIXME
3967 for (i = 0; i < 16; i++) {
3968 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
3969 }
3970}
3971
3972static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003973 target_sigset_t *set, CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02003974{
3975 sigframe *frame;
3976 abi_ulong frame_addr;
3977
3978 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3979 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3980 (unsigned long long)frame_addr);
3981 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3982 goto give_sigsegv;
3983 }
3984
3985 qemu_log("%s: 1\n", __FUNCTION__);
3986 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
3987 goto give_sigsegv;
3988 }
3989
3990 save_sigregs(env, &frame->sregs);
3991
3992 __put_user((abi_ulong)(unsigned long)&frame->sregs,
3993 (abi_ulong *)&frame->sc.sregs);
3994
3995 /* Set up to return from userspace. If provided, use a stub
3996 already in userspace. */
3997 if (ka->sa_flags & TARGET_SA_RESTORER) {
3998 env->regs[14] = (unsigned long)
3999 ka->sa_restorer | PSW_ADDR_AMODE;
4000 } else {
4001 env->regs[14] = (unsigned long)
4002 frame->retcode | PSW_ADDR_AMODE;
4003 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4004 (uint16_t *)(frame->retcode)))
4005 goto give_sigsegv;
4006 }
4007
4008 /* Set up backchain. */
4009 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4010 goto give_sigsegv;
4011 }
4012
4013 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004014 env->regs[15] = frame_addr;
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004015 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4016
4017 env->regs[2] = sig; //map_signal(sig);
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004018 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004019
4020 /* We forgot to include these in the sigcontext.
4021 To avoid breaking binary compatibility, they are passed as args. */
4022 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4023 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4024
4025 /* Place signal number on stack to allow backtrace from handler. */
4026 if (__put_user(env->regs[2], (int *) &frame->signo)) {
4027 goto give_sigsegv;
4028 }
4029 unlock_user_struct(frame, frame_addr, 1);
4030 return;
4031
4032give_sigsegv:
4033 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4034 unlock_user_struct(frame, frame_addr, 1);
4035 force_sig(TARGET_SIGSEGV);
4036}
4037
4038static void setup_rt_frame(int sig, struct target_sigaction *ka,
4039 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01004040 target_sigset_t *set, CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004041{
4042 int i;
4043 rt_sigframe *frame;
4044 abi_ulong frame_addr;
4045
4046 frame_addr = get_sigframe(ka, env, sizeof *frame);
4047 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4048 (unsigned long long)frame_addr);
4049 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4050 goto give_sigsegv;
4051 }
4052
4053 qemu_log("%s: 1\n", __FUNCTION__);
4054 if (copy_siginfo_to_user(&frame->info, info)) {
4055 goto give_sigsegv;
4056 }
4057
4058 /* Create the ucontext. */
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004059 __put_user(0, &frame->uc.tuc_flags);
4060 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4061 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004062 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004063 &frame->uc.tuc_stack.ss_flags);
4064 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4065 save_sigregs(env, &frame->uc.tuc_mcontext);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004066 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4067 __put_user((abi_ulong)set->sig[i],
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004068 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004069 }
4070
4071 /* Set up to return from userspace. If provided, use a stub
4072 already in userspace. */
4073 if (ka->sa_flags & TARGET_SA_RESTORER) {
4074 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4075 } else {
4076 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4077 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4078 (uint16_t *)(frame->retcode))) {
4079 goto give_sigsegv;
4080 }
4081 }
4082
4083 /* Set up backchain. */
4084 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4085 goto give_sigsegv;
4086 }
4087
4088 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004089 env->regs[15] = frame_addr;
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004090 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4091
4092 env->regs[2] = sig; //map_signal(sig);
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004093 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4094 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004095 return;
4096
4097give_sigsegv:
4098 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4099 unlock_user_struct(frame, frame_addr, 1);
4100 force_sig(TARGET_SIGSEGV);
4101}
4102
4103static int
Andreas Färber05390242012-02-25 03:37:53 +01004104restore_sigregs(CPUS390XState *env, target_sigregs *sc)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004105{
4106 int err = 0;
4107 int i;
4108
4109 for (i = 0; i < 16; i++) {
4110 err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
4111 }
4112
4113 err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
4114 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4115 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4116 (unsigned long long)env->psw.addr);
4117 err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
4118 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4119
4120 for (i = 0; i < 16; i++) {
4121 err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
4122 }
4123 for (i = 0; i < 16; i++) {
4124 err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
4125 }
4126
4127 return err;
4128}
4129
Andreas Färber05390242012-02-25 03:37:53 +01004130long do_sigreturn(CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004131{
4132 sigframe *frame;
4133 abi_ulong frame_addr = env->regs[15];
4134 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4135 (unsigned long long)frame_addr);
4136 target_sigset_t target_set;
4137 sigset_t set;
4138
4139 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4140 goto badframe;
4141 }
4142 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4143 goto badframe;
4144 }
4145
4146 target_to_host_sigset_internal(&set, &target_set);
4147 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4148
4149 if (restore_sigregs(env, &frame->sregs)) {
4150 goto badframe;
4151 }
4152
4153 unlock_user_struct(frame, frame_addr, 0);
4154 return env->regs[2];
4155
4156badframe:
4157 unlock_user_struct(frame, frame_addr, 0);
4158 force_sig(TARGET_SIGSEGV);
4159 return 0;
4160}
4161
Andreas Färber05390242012-02-25 03:37:53 +01004162long do_rt_sigreturn(CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004163{
4164 rt_sigframe *frame;
4165 abi_ulong frame_addr = env->regs[15];
4166 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4167 (unsigned long long)frame_addr);
4168 sigset_t set;
4169
4170 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4171 goto badframe;
4172 }
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004173 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004174
4175 sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
4176
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004177 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004178 goto badframe;
4179 }
4180
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004181 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004182 get_sp_from_cpustate(env)) == -EFAULT) {
4183 goto badframe;
4184 }
4185 unlock_user_struct(frame, frame_addr, 0);
4186 return env->regs[2];
4187
4188badframe:
4189 unlock_user_struct(frame, frame_addr, 0);
4190 force_sig(TARGET_SIGSEGV);
4191 return 0;
4192}
4193
Nathan Froydbcd49332009-05-12 19:13:18 -07004194#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4195
4196/* FIXME: Many of the structures are defined for both PPC and PPC64, but
4197 the signal handling is different enough that we haven't implemented
4198 support for PPC64 yet. Hence the restriction above.
4199
4200 There are various #if'd blocks for code for TARGET_PPC64. These
4201 blocks should go away so that we can successfully run 32-bit and
4202 64-bit binaries on a QEMU configured for PPC64. */
4203
4204/* Size of dummy stack frame allocated when calling signal handler.
4205 See arch/powerpc/include/asm/ptrace.h. */
4206#if defined(TARGET_PPC64)
4207#define SIGNAL_FRAMESIZE 128
4208#else
4209#define SIGNAL_FRAMESIZE 64
4210#endif
4211
4212/* See arch/powerpc/include/asm/sigcontext.h. */
4213struct target_sigcontext {
4214 target_ulong _unused[4];
4215 int32_t signal;
4216#if defined(TARGET_PPC64)
4217 int32_t pad0;
4218#endif
4219 target_ulong handler;
4220 target_ulong oldmask;
4221 target_ulong regs; /* struct pt_regs __user * */
4222 /* TODO: PPC64 includes extra bits here. */
4223};
4224
4225/* Indices for target_mcontext.mc_gregs, below.
4226 See arch/powerpc/include/asm/ptrace.h for details. */
4227enum {
4228 TARGET_PT_R0 = 0,
4229 TARGET_PT_R1 = 1,
4230 TARGET_PT_R2 = 2,
4231 TARGET_PT_R3 = 3,
4232 TARGET_PT_R4 = 4,
4233 TARGET_PT_R5 = 5,
4234 TARGET_PT_R6 = 6,
4235 TARGET_PT_R7 = 7,
4236 TARGET_PT_R8 = 8,
4237 TARGET_PT_R9 = 9,
4238 TARGET_PT_R10 = 10,
4239 TARGET_PT_R11 = 11,
4240 TARGET_PT_R12 = 12,
4241 TARGET_PT_R13 = 13,
4242 TARGET_PT_R14 = 14,
4243 TARGET_PT_R15 = 15,
4244 TARGET_PT_R16 = 16,
4245 TARGET_PT_R17 = 17,
4246 TARGET_PT_R18 = 18,
4247 TARGET_PT_R19 = 19,
4248 TARGET_PT_R20 = 20,
4249 TARGET_PT_R21 = 21,
4250 TARGET_PT_R22 = 22,
4251 TARGET_PT_R23 = 23,
4252 TARGET_PT_R24 = 24,
4253 TARGET_PT_R25 = 25,
4254 TARGET_PT_R26 = 26,
4255 TARGET_PT_R27 = 27,
4256 TARGET_PT_R28 = 28,
4257 TARGET_PT_R29 = 29,
4258 TARGET_PT_R30 = 30,
4259 TARGET_PT_R31 = 31,
4260 TARGET_PT_NIP = 32,
4261 TARGET_PT_MSR = 33,
4262 TARGET_PT_ORIG_R3 = 34,
4263 TARGET_PT_CTR = 35,
4264 TARGET_PT_LNK = 36,
4265 TARGET_PT_XER = 37,
4266 TARGET_PT_CCR = 38,
4267 /* Yes, there are two registers with #39. One is 64-bit only. */
4268 TARGET_PT_MQ = 39,
4269 TARGET_PT_SOFTE = 39,
4270 TARGET_PT_TRAP = 40,
4271 TARGET_PT_DAR = 41,
4272 TARGET_PT_DSISR = 42,
4273 TARGET_PT_RESULT = 43,
4274 TARGET_PT_REGS_COUNT = 44
4275};
4276
4277/* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4278 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4279struct target_mcontext {
4280 target_ulong mc_gregs[48];
4281 /* Includes fpscr. */
4282 uint64_t mc_fregs[33];
4283 target_ulong mc_pad[2];
4284 /* We need to handle Altivec and SPE at the same time, which no
4285 kernel needs to do. Fortunately, the kernel defines this bit to
4286 be Altivec-register-large all the time, rather than trying to
4287 twiddle it based on the specific platform. */
4288 union {
4289 /* SPE vector registers. One extra for SPEFSCR. */
4290 uint32_t spe[33];
4291 /* Altivec vector registers. The packing of VSCR and VRSAVE
4292 varies depending on whether we're PPC64 or not: PPC64 splits
4293 them apart; PPC32 stuffs them together. */
4294#if defined(TARGET_PPC64)
malc3efa9a62009-07-18 13:10:12 +04004295#define QEMU_NVRREG 34
Nathan Froydbcd49332009-05-12 19:13:18 -07004296#else
malc3efa9a62009-07-18 13:10:12 +04004297#define QEMU_NVRREG 33
Nathan Froydbcd49332009-05-12 19:13:18 -07004298#endif
Anthony Liguoric227f092009-10-01 16:12:16 -05004299 ppc_avr_t altivec[QEMU_NVRREG];
malc3efa9a62009-07-18 13:10:12 +04004300#undef QEMU_NVRREG
Nathan Froydbcd49332009-05-12 19:13:18 -07004301 } mc_vregs __attribute__((__aligned__(16)));
4302};
4303
4304struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02004305 target_ulong tuc_flags;
4306 target_ulong tuc_link; /* struct ucontext __user * */
4307 struct target_sigaltstack tuc_stack;
Nathan Froydbcd49332009-05-12 19:13:18 -07004308#if !defined(TARGET_PPC64)
Aurelien Jarno60e99242010-03-29 02:12:51 +02004309 int32_t tuc_pad[7];
4310 target_ulong tuc_regs; /* struct mcontext __user *
Nathan Froydbcd49332009-05-12 19:13:18 -07004311 points to uc_mcontext field */
4312#endif
Aurelien Jarno60e99242010-03-29 02:12:51 +02004313 target_sigset_t tuc_sigmask;
Nathan Froydbcd49332009-05-12 19:13:18 -07004314#if defined(TARGET_PPC64)
Anthony Liguoric227f092009-10-01 16:12:16 -05004315 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
Aurelien Jarno60e99242010-03-29 02:12:51 +02004316 struct target_sigcontext tuc_mcontext;
Nathan Froydbcd49332009-05-12 19:13:18 -07004317#else
Aurelien Jarno60e99242010-03-29 02:12:51 +02004318 int32_t tuc_maskext[30];
4319 int32_t tuc_pad2[3];
4320 struct target_mcontext tuc_mcontext;
Nathan Froydbcd49332009-05-12 19:13:18 -07004321#endif
4322};
4323
4324/* See arch/powerpc/kernel/signal_32.c. */
4325struct target_sigframe {
4326 struct target_sigcontext sctx;
4327 struct target_mcontext mctx;
4328 int32_t abigap[56];
4329};
4330
4331struct target_rt_sigframe {
4332 struct target_siginfo info;
4333 struct target_ucontext uc;
4334 int32_t abigap[56];
4335};
4336
4337/* We use the mc_pad field for the signal return trampoline. */
4338#define tramp mc_pad
4339
4340/* See arch/powerpc/kernel/signal.c. */
4341static target_ulong get_sigframe(struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01004342 CPUPPCState *env,
Nathan Froydbcd49332009-05-12 19:13:18 -07004343 int frame_size)
4344{
4345 target_ulong oldsp, newsp;
4346
4347 oldsp = env->gpr[1];
4348
4349 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
Alex Barcelo32a20032012-02-09 23:55:46 +00004350 (sas_ss_flags(oldsp) == 0)) {
Nathan Froydbcd49332009-05-12 19:13:18 -07004351 oldsp = (target_sigaltstack_used.ss_sp
4352 + target_sigaltstack_used.ss_size);
4353 }
4354
4355 newsp = (oldsp - frame_size) & ~0xFUL;
4356
4357 return newsp;
4358}
4359
Andreas Färber05390242012-02-25 03:37:53 +01004360static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
Nathan Froydbcd49332009-05-12 19:13:18 -07004361 int sigret)
4362{
4363 target_ulong msr = env->msr;
4364 int i;
4365 target_ulong ccr = 0;
4366
4367 /* In general, the kernel attempts to be intelligent about what it
4368 needs to save for Altivec/FP/SPE registers. We don't care that
4369 much, so we just go ahead and save everything. */
4370
4371 /* Save general registers. */
4372 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4373 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4374 return 1;
4375 }
4376 }
4377 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4378 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4379 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4380 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4381 return 1;
4382
4383 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4384 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4385 }
4386 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4387 return 1;
4388
4389 /* Save Altivec registers if necessary. */
4390 if (env->insns_flags & PPC_ALTIVEC) {
4391 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004392 ppc_avr_t *avr = &env->avr[i];
4393 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07004394
4395 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4396 __put_user(avr->u64[1], &vreg->u64[1])) {
4397 return 1;
4398 }
4399 }
4400 /* Set MSR_VR in the saved MSR value to indicate that
4401 frame->mc_vregs contains valid data. */
4402 msr |= MSR_VR;
4403 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4404 &frame->mc_vregs.altivec[32].u32[3]))
4405 return 1;
4406 }
4407
4408 /* Save floating point registers. */
4409 if (env->insns_flags & PPC_FLOAT) {
4410 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4411 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4412 return 1;
4413 }
4414 }
4415 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4416 return 1;
4417 }
4418
4419 /* Save SPE registers. The kernel only saves the high half. */
4420 if (env->insns_flags & PPC_SPE) {
4421#if defined(TARGET_PPC64)
4422 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4423 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4424 return 1;
4425 }
4426 }
4427#else
4428 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4429 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4430 return 1;
4431 }
4432 }
4433#endif
4434 /* Set MSR_SPE in the saved MSR value to indicate that
4435 frame->mc_vregs contains valid data. */
4436 msr |= MSR_SPE;
4437 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4438 return 1;
4439 }
4440
4441 /* Store MSR. */
4442 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4443 return 1;
4444
4445 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4446 if (sigret) {
4447 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4448 __put_user(0x44000002UL, &frame->tramp[1])) {
4449 return 1;
4450 }
4451 }
4452
4453 return 0;
4454}
4455
Andreas Färber05390242012-02-25 03:37:53 +01004456static int restore_user_regs(CPUPPCState *env,
Nathan Froydbcd49332009-05-12 19:13:18 -07004457 struct target_mcontext *frame, int sig)
4458{
4459 target_ulong save_r2 = 0;
4460 target_ulong msr;
4461 target_ulong ccr;
4462
4463 int i;
4464
4465 if (!sig) {
4466 save_r2 = env->gpr[2];
4467 }
4468
4469 /* Restore general registers. */
4470 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4471 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4472 return 1;
4473 }
4474 }
4475 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4476 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4477 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4478 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4479 return 1;
4480 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4481 return 1;
4482
4483 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4484 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4485 }
4486
4487 if (!sig) {
4488 env->gpr[2] = save_r2;
4489 }
4490 /* Restore MSR. */
4491 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4492 return 1;
4493
4494 /* If doing signal return, restore the previous little-endian mode. */
4495 if (sig)
4496 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4497
4498 /* Restore Altivec registers if necessary. */
4499 if (env->insns_flags & PPC_ALTIVEC) {
4500 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004501 ppc_avr_t *avr = &env->avr[i];
4502 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07004503
4504 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4505 __get_user(avr->u64[1], &vreg->u64[1])) {
4506 return 1;
4507 }
4508 }
4509 /* Set MSR_VEC in the saved MSR value to indicate that
4510 frame->mc_vregs contains valid data. */
4511 if (__get_user(env->spr[SPR_VRSAVE],
4512 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4513 return 1;
4514 }
4515
4516 /* Restore floating point registers. */
4517 if (env->insns_flags & PPC_FLOAT) {
4518 uint64_t fpscr;
4519 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4520 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4521 return 1;
4522 }
4523 }
4524 if (__get_user(fpscr, &frame->mc_fregs[32]))
4525 return 1;
4526 env->fpscr = (uint32_t) fpscr;
4527 }
4528
4529 /* Save SPE registers. The kernel only saves the high half. */
4530 if (env->insns_flags & PPC_SPE) {
4531#if defined(TARGET_PPC64)
4532 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4533 uint32_t hi;
4534
4535 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4536 return 1;
4537 }
4538 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4539 }
4540#else
4541 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4542 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4543 return 1;
4544 }
4545 }
4546#endif
4547 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4548 return 1;
4549 }
4550
4551 return 0;
4552}
4553
4554static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01004555 target_sigset_t *set, CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004556{
4557 struct target_sigframe *frame;
4558 struct target_sigcontext *sc;
4559 target_ulong frame_addr, newsp;
4560 int err = 0;
4561 int signal;
4562
4563 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4564 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4565 goto sigsegv;
4566 sc = &frame->sctx;
4567
4568 signal = current_exec_domain_sig(sig);
4569
4570 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
4571 err |= __put_user(set->sig[0], &sc->oldmask);
4572#if defined(TARGET_PPC64)
4573 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4574#else
4575 err |= __put_user(set->sig[1], &sc->_unused[3]);
4576#endif
4577 err |= __put_user(h2g(&frame->mctx), &sc->regs);
4578 err |= __put_user(sig, &sc->signal);
4579
4580 /* Save user regs. */
4581 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4582
4583 /* The kernel checks for the presence of a VDSO here. We don't
4584 emulate a vdso, so use a sigreturn system call. */
4585 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4586
4587 /* Turn off all fp exceptions. */
4588 env->fpscr = 0;
4589
4590 /* Create a stack frame for the caller of the handler. */
4591 newsp = frame_addr - SIGNAL_FRAMESIZE;
4592 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4593
4594 if (err)
4595 goto sigsegv;
4596
4597 /* Set up registers for signal handler. */
4598 env->gpr[1] = newsp;
4599 env->gpr[3] = signal;
4600 env->gpr[4] = (target_ulong) h2g(sc);
4601 env->nip = (target_ulong) ka->_sa_handler;
4602 /* Signal handlers are entered in big-endian mode. */
4603 env->msr &= ~MSR_LE;
4604
4605 unlock_user_struct(frame, frame_addr, 1);
4606 return;
4607
4608sigsegv:
4609 unlock_user_struct(frame, frame_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004610 qemu_log("segfaulting from setup_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004611 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004612}
4613
4614static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004615 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01004616 target_sigset_t *set, CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004617{
4618 struct target_rt_sigframe *rt_sf;
4619 struct target_mcontext *frame;
4620 target_ulong rt_sf_addr, newsp = 0;
4621 int i, err = 0;
4622 int signal;
4623
4624 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4625 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4626 goto sigsegv;
4627
4628 signal = current_exec_domain_sig(sig);
4629
4630 err |= copy_siginfo_to_user(&rt_sf->info, info);
4631
Aurelien Jarno60e99242010-03-29 02:12:51 +02004632 err |= __put_user(0, &rt_sf->uc.tuc_flags);
4633 err |= __put_user(0, &rt_sf->uc.tuc_link);
Nathan Froydbcd49332009-05-12 19:13:18 -07004634 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
Aurelien Jarno60e99242010-03-29 02:12:51 +02004635 &rt_sf->uc.tuc_stack.ss_sp);
Nathan Froydbcd49332009-05-12 19:13:18 -07004636 err |= __put_user(sas_ss_flags(env->gpr[1]),
Aurelien Jarno60e99242010-03-29 02:12:51 +02004637 &rt_sf->uc.tuc_stack.ss_flags);
Nathan Froydbcd49332009-05-12 19:13:18 -07004638 err |= __put_user(target_sigaltstack_used.ss_size,
Aurelien Jarno60e99242010-03-29 02:12:51 +02004639 &rt_sf->uc.tuc_stack.ss_size);
4640 err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4641 &rt_sf->uc.tuc_regs);
Nathan Froydbcd49332009-05-12 19:13:18 -07004642 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02004643 err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
Nathan Froydbcd49332009-05-12 19:13:18 -07004644 }
4645
Aurelien Jarno60e99242010-03-29 02:12:51 +02004646 frame = &rt_sf->uc.tuc_mcontext;
Nathan Froydbcd49332009-05-12 19:13:18 -07004647 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4648
4649 /* The kernel checks for the presence of a VDSO here. We don't
4650 emulate a vdso, so use a sigreturn system call. */
4651 env->lr = (target_ulong) h2g(frame->tramp);
4652
4653 /* Turn off all fp exceptions. */
4654 env->fpscr = 0;
4655
4656 /* Create a stack frame for the caller of the handler. */
4657 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4658 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4659
4660 if (err)
4661 goto sigsegv;
4662
4663 /* Set up registers for signal handler. */
4664 env->gpr[1] = newsp;
4665 env->gpr[3] = (target_ulong) signal;
4666 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4667 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4668 env->gpr[6] = (target_ulong) h2g(rt_sf);
4669 env->nip = (target_ulong) ka->_sa_handler;
4670 /* Signal handlers are entered in big-endian mode. */
4671 env->msr &= ~MSR_LE;
4672
4673 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4674 return;
4675
4676sigsegv:
4677 unlock_user_struct(rt_sf, rt_sf_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004678 qemu_log("segfaulting from setup_rt_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004679 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004680
4681}
4682
Andreas Färber05390242012-02-25 03:37:53 +01004683long do_sigreturn(CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004684{
4685 struct target_sigcontext *sc = NULL;
4686 struct target_mcontext *sr = NULL;
4687 target_ulong sr_addr, sc_addr;
4688 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05004689 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07004690
4691 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4692 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4693 goto sigsegv;
4694
4695#if defined(TARGET_PPC64)
4696 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4697#else
4698 if(__get_user(set.sig[0], &sc->oldmask) ||
4699 __get_user(set.sig[1], &sc->_unused[3]))
4700 goto sigsegv;
4701#endif
4702 target_to_host_sigset_internal(&blocked, &set);
4703 sigprocmask(SIG_SETMASK, &blocked, NULL);
4704
4705 if (__get_user(sr_addr, &sc->regs))
4706 goto sigsegv;
4707 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4708 goto sigsegv;
4709 if (restore_user_regs(env, sr, 1))
4710 goto sigsegv;
4711
4712 unlock_user_struct(sr, sr_addr, 1);
4713 unlock_user_struct(sc, sc_addr, 1);
4714 return -TARGET_QEMU_ESIGRETURN;
4715
4716sigsegv:
4717 unlock_user_struct(sr, sr_addr, 1);
4718 unlock_user_struct(sc, sc_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004719 qemu_log("segfaulting from do_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004720 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004721 return 0;
4722}
4723
4724/* See arch/powerpc/kernel/signal_32.c. */
Andreas Färber05390242012-02-25 03:37:53 +01004725static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
Nathan Froydbcd49332009-05-12 19:13:18 -07004726{
4727 struct target_mcontext *mcp;
4728 target_ulong mcp_addr;
4729 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05004730 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07004731
Aurelien Jarno60e99242010-03-29 02:12:51 +02004732 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
Nathan Froydbcd49332009-05-12 19:13:18 -07004733 sizeof (set)))
4734 return 1;
4735
4736#if defined(TARGET_PPC64)
4737 fprintf (stderr, "do_setcontext: not implemented\n");
4738 return 0;
4739#else
Aurelien Jarno60e99242010-03-29 02:12:51 +02004740 if (__get_user(mcp_addr, &ucp->tuc_regs))
Nathan Froydbcd49332009-05-12 19:13:18 -07004741 return 1;
4742
4743 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4744 return 1;
4745
4746 target_to_host_sigset_internal(&blocked, &set);
4747 sigprocmask(SIG_SETMASK, &blocked, NULL);
4748 if (restore_user_regs(env, mcp, sig))
4749 goto sigsegv;
4750
4751 unlock_user_struct(mcp, mcp_addr, 1);
4752 return 0;
4753
4754sigsegv:
4755 unlock_user_struct(mcp, mcp_addr, 1);
4756 return 1;
4757#endif
4758}
4759
Andreas Färber05390242012-02-25 03:37:53 +01004760long do_rt_sigreturn(CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004761{
4762 struct target_rt_sigframe *rt_sf = NULL;
4763 target_ulong rt_sf_addr;
4764
4765 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4766 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4767 goto sigsegv;
4768
4769 if (do_setcontext(&rt_sf->uc, env, 1))
4770 goto sigsegv;
4771
4772 do_sigaltstack(rt_sf_addr
Aurelien Jarno60e99242010-03-29 02:12:51 +02004773 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
Nathan Froydbcd49332009-05-12 19:13:18 -07004774 0, env->gpr[1]);
4775
4776 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4777 return -TARGET_QEMU_ESIGRETURN;
4778
4779sigsegv:
4780 unlock_user_struct(rt_sf, rt_sf_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004781 qemu_log("segfaulting from do_rt_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004782 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004783 return 0;
4784}
4785
Laurent Vivier492a8742009-08-03 16:12:17 +02004786#elif defined(TARGET_M68K)
4787
4788struct target_sigcontext {
4789 abi_ulong sc_mask;
4790 abi_ulong sc_usp;
4791 abi_ulong sc_d0;
4792 abi_ulong sc_d1;
4793 abi_ulong sc_a0;
4794 abi_ulong sc_a1;
4795 unsigned short sc_sr;
4796 abi_ulong sc_pc;
4797};
4798
4799struct target_sigframe
4800{
4801 abi_ulong pretcode;
4802 int sig;
4803 int code;
4804 abi_ulong psc;
4805 char retcode[8];
4806 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4807 struct target_sigcontext sc;
4808};
Laurent Vivier71811552009-08-03 16:12:18 +02004809
Anthony Liguoric227f092009-10-01 16:12:16 -05004810typedef int target_greg_t;
Laurent Vivier71811552009-08-03 16:12:18 +02004811#define TARGET_NGREG 18
Anthony Liguoric227f092009-10-01 16:12:16 -05004812typedef target_greg_t target_gregset_t[TARGET_NGREG];
Laurent Vivier71811552009-08-03 16:12:18 +02004813
4814typedef struct target_fpregset {
4815 int f_fpcntl[3];
4816 int f_fpregs[8*3];
Anthony Liguoric227f092009-10-01 16:12:16 -05004817} target_fpregset_t;
Laurent Vivier71811552009-08-03 16:12:18 +02004818
4819struct target_mcontext {
4820 int version;
Anthony Liguoric227f092009-10-01 16:12:16 -05004821 target_gregset_t gregs;
4822 target_fpregset_t fpregs;
Laurent Vivier71811552009-08-03 16:12:18 +02004823};
4824
4825#define TARGET_MCONTEXT_VERSION 2
4826
4827struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02004828 abi_ulong tuc_flags;
4829 abi_ulong tuc_link;
4830 target_stack_t tuc_stack;
4831 struct target_mcontext tuc_mcontext;
4832 abi_long tuc_filler[80];
4833 target_sigset_t tuc_sigmask;
Laurent Vivier71811552009-08-03 16:12:18 +02004834};
4835
4836struct target_rt_sigframe
4837{
4838 abi_ulong pretcode;
4839 int sig;
4840 abi_ulong pinfo;
4841 abi_ulong puc;
4842 char retcode[8];
4843 struct target_siginfo info;
4844 struct target_ucontext uc;
4845};
Laurent Vivier492a8742009-08-03 16:12:17 +02004846
4847static int
Andreas Färber05390242012-02-25 03:37:53 +01004848setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
4849 abi_ulong mask)
Laurent Vivier492a8742009-08-03 16:12:17 +02004850{
4851 int err = 0;
4852
4853 err |= __put_user(mask, &sc->sc_mask);
4854 err |= __put_user(env->aregs[7], &sc->sc_usp);
4855 err |= __put_user(env->dregs[0], &sc->sc_d0);
4856 err |= __put_user(env->dregs[1], &sc->sc_d1);
4857 err |= __put_user(env->aregs[0], &sc->sc_a0);
4858 err |= __put_user(env->aregs[1], &sc->sc_a1);
4859 err |= __put_user(env->sr, &sc->sc_sr);
4860 err |= __put_user(env->pc, &sc->sc_pc);
4861
4862 return err;
4863}
4864
4865static int
Andreas Färber05390242012-02-25 03:37:53 +01004866restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
Laurent Vivier492a8742009-08-03 16:12:17 +02004867{
4868 int err = 0;
4869 int temp;
4870
4871 err |= __get_user(env->aregs[7], &sc->sc_usp);
4872 err |= __get_user(env->dregs[1], &sc->sc_d1);
4873 err |= __get_user(env->aregs[0], &sc->sc_a0);
4874 err |= __get_user(env->aregs[1], &sc->sc_a1);
4875 err |= __get_user(env->pc, &sc->sc_pc);
4876 err |= __get_user(temp, &sc->sc_sr);
4877 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4878
4879 *pd0 = tswapl(sc->sc_d0);
4880
4881 return err;
4882}
4883
4884/*
4885 * Determine which stack to use..
4886 */
4887static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01004888get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
4889 size_t frame_size)
Laurent Vivier492a8742009-08-03 16:12:17 +02004890{
4891 unsigned long sp;
4892
4893 sp = regs->aregs[7];
4894
4895 /* This is the X/Open sanctioned signal stack switching. */
4896 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4897 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4898 }
4899
4900 return ((sp - frame_size) & -8UL);
4901}
4902
4903static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01004904 target_sigset_t *set, CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02004905{
4906 struct target_sigframe *frame;
4907 abi_ulong frame_addr;
4908 abi_ulong retcode_addr;
4909 abi_ulong sc_addr;
4910 int err = 0;
4911 int i;
4912
4913 frame_addr = get_sigframe(ka, env, sizeof *frame);
4914 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4915 goto give_sigsegv;
4916
4917 err |= __put_user(sig, &frame->sig);
4918
4919 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4920 err |= __put_user(sc_addr, &frame->psc);
4921
4922 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4923 if (err)
4924 goto give_sigsegv;
4925
4926 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4927 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4928 goto give_sigsegv;
4929 }
4930
4931 /* Set up to return from userspace. */
4932
4933 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4934 err |= __put_user(retcode_addr, &frame->pretcode);
4935
4936 /* moveq #,d0; trap #0 */
4937
4938 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4939 (long *)(frame->retcode));
4940
4941 if (err)
4942 goto give_sigsegv;
4943
4944 /* Set up to return from userspace */
4945
4946 env->aregs[7] = frame_addr;
4947 env->pc = ka->_sa_handler;
4948
4949 unlock_user_struct(frame, frame_addr, 1);
4950 return;
4951
4952give_sigsegv:
4953 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02004954 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02004955}
4956
Laurent Vivier71811552009-08-03 16:12:18 +02004957static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
Andreas Färber05390242012-02-25 03:37:53 +01004958 CPUM68KState *env)
Laurent Vivier71811552009-08-03 16:12:18 +02004959{
Aurelien Jarno60e99242010-03-29 02:12:51 +02004960 target_greg_t *gregs = uc->tuc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02004961 int err;
4962
Aurelien Jarno60e99242010-03-29 02:12:51 +02004963 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
Laurent Vivier71811552009-08-03 16:12:18 +02004964 err |= __put_user(env->dregs[0], &gregs[0]);
4965 err |= __put_user(env->dregs[1], &gregs[1]);
4966 err |= __put_user(env->dregs[2], &gregs[2]);
4967 err |= __put_user(env->dregs[3], &gregs[3]);
4968 err |= __put_user(env->dregs[4], &gregs[4]);
4969 err |= __put_user(env->dregs[5], &gregs[5]);
4970 err |= __put_user(env->dregs[6], &gregs[6]);
4971 err |= __put_user(env->dregs[7], &gregs[7]);
4972 err |= __put_user(env->aregs[0], &gregs[8]);
4973 err |= __put_user(env->aregs[1], &gregs[9]);
4974 err |= __put_user(env->aregs[2], &gregs[10]);
4975 err |= __put_user(env->aregs[3], &gregs[11]);
4976 err |= __put_user(env->aregs[4], &gregs[12]);
4977 err |= __put_user(env->aregs[5], &gregs[13]);
4978 err |= __put_user(env->aregs[6], &gregs[14]);
4979 err |= __put_user(env->aregs[7], &gregs[15]);
4980 err |= __put_user(env->pc, &gregs[16]);
4981 err |= __put_user(env->sr, &gregs[17]);
4982
4983 return err;
4984}
4985
Andreas Färber05390242012-02-25 03:37:53 +01004986static inline int target_rt_restore_ucontext(CPUM68KState *env,
Laurent Vivier71811552009-08-03 16:12:18 +02004987 struct target_ucontext *uc,
4988 int *pd0)
4989{
4990 int temp;
4991 int err;
Aurelien Jarno60e99242010-03-29 02:12:51 +02004992 target_greg_t *gregs = uc->tuc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02004993
Aurelien Jarno60e99242010-03-29 02:12:51 +02004994 err = __get_user(temp, &uc->tuc_mcontext.version);
Laurent Vivier71811552009-08-03 16:12:18 +02004995 if (temp != TARGET_MCONTEXT_VERSION)
4996 goto badframe;
4997
4998 /* restore passed registers */
4999 err |= __get_user(env->dregs[0], &gregs[0]);
5000 err |= __get_user(env->dregs[1], &gregs[1]);
5001 err |= __get_user(env->dregs[2], &gregs[2]);
5002 err |= __get_user(env->dregs[3], &gregs[3]);
5003 err |= __get_user(env->dregs[4], &gregs[4]);
5004 err |= __get_user(env->dregs[5], &gregs[5]);
5005 err |= __get_user(env->dregs[6], &gregs[6]);
5006 err |= __get_user(env->dregs[7], &gregs[7]);
5007 err |= __get_user(env->aregs[0], &gregs[8]);
5008 err |= __get_user(env->aregs[1], &gregs[9]);
5009 err |= __get_user(env->aregs[2], &gregs[10]);
5010 err |= __get_user(env->aregs[3], &gregs[11]);
5011 err |= __get_user(env->aregs[4], &gregs[12]);
5012 err |= __get_user(env->aregs[5], &gregs[13]);
5013 err |= __get_user(env->aregs[6], &gregs[14]);
5014 err |= __get_user(env->aregs[7], &gregs[15]);
5015 err |= __get_user(env->pc, &gregs[16]);
5016 err |= __get_user(temp, &gregs[17]);
5017 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5018
5019 *pd0 = env->dregs[0];
5020 return err;
5021
5022badframe:
5023 return 1;
5024}
5025
Laurent Vivier492a8742009-08-03 16:12:17 +02005026static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05005027 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01005028 target_sigset_t *set, CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005029{
Laurent Vivier71811552009-08-03 16:12:18 +02005030 struct target_rt_sigframe *frame;
5031 abi_ulong frame_addr;
5032 abi_ulong retcode_addr;
5033 abi_ulong info_addr;
5034 abi_ulong uc_addr;
5035 int err = 0;
5036 int i;
5037
5038 frame_addr = get_sigframe(ka, env, sizeof *frame);
5039 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5040 goto give_sigsegv;
5041
5042 err |= __put_user(sig, &frame->sig);
5043
5044 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
5045 err |= __put_user(info_addr, &frame->pinfo);
5046
5047 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
5048 err |= __put_user(uc_addr, &frame->puc);
5049
5050 err |= copy_siginfo_to_user(&frame->info, info);
5051
5052 /* Create the ucontext */
5053
Aurelien Jarno60e99242010-03-29 02:12:51 +02005054 err |= __put_user(0, &frame->uc.tuc_flags);
5055 err |= __put_user(0, &frame->uc.tuc_link);
Laurent Vivier71811552009-08-03 16:12:18 +02005056 err |= __put_user(target_sigaltstack_used.ss_sp,
Aurelien Jarno60e99242010-03-29 02:12:51 +02005057 &frame->uc.tuc_stack.ss_sp);
Laurent Vivier71811552009-08-03 16:12:18 +02005058 err |= __put_user(sas_ss_flags(env->aregs[7]),
Aurelien Jarno60e99242010-03-29 02:12:51 +02005059 &frame->uc.tuc_stack.ss_flags);
Laurent Vivier71811552009-08-03 16:12:18 +02005060 err |= __put_user(target_sigaltstack_used.ss_size,
Aurelien Jarno60e99242010-03-29 02:12:51 +02005061 &frame->uc.tuc_stack.ss_size);
Laurent Vivier71811552009-08-03 16:12:18 +02005062 err |= target_rt_setup_ucontext(&frame->uc, env);
5063
5064 if (err)
5065 goto give_sigsegv;
5066
5067 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02005068 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
Laurent Vivier71811552009-08-03 16:12:18 +02005069 goto give_sigsegv;
5070 }
5071
5072 /* Set up to return from userspace. */
5073
5074 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
5075 err |= __put_user(retcode_addr, &frame->pretcode);
5076
5077 /* moveq #,d0; notb d0; trap #0 */
5078
5079 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5080 (long *)(frame->retcode + 0));
5081 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
5082
5083 if (err)
5084 goto give_sigsegv;
5085
5086 /* Set up to return from userspace */
5087
5088 env->aregs[7] = frame_addr;
5089 env->pc = ka->_sa_handler;
5090
5091 unlock_user_struct(frame, frame_addr, 1);
5092 return;
5093
5094give_sigsegv:
5095 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02005096 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02005097}
5098
Andreas Färber05390242012-02-25 03:37:53 +01005099long do_sigreturn(CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005100{
5101 struct target_sigframe *frame;
5102 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05005103 target_sigset_t target_set;
Laurent Vivier492a8742009-08-03 16:12:17 +02005104 sigset_t set;
5105 int d0, i;
5106
5107 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5108 goto badframe;
5109
5110 /* set blocked signals */
5111
5112 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5113 goto badframe;
5114
5115 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5116 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5117 goto badframe;
5118 }
5119
5120 target_to_host_sigset_internal(&set, &target_set);
5121 sigprocmask(SIG_SETMASK, &set, NULL);
5122
5123 /* restore registers */
5124
5125 if (restore_sigcontext(env, &frame->sc, &d0))
5126 goto badframe;
5127
5128 unlock_user_struct(frame, frame_addr, 0);
5129 return d0;
5130
5131badframe:
5132 unlock_user_struct(frame, frame_addr, 0);
5133 force_sig(TARGET_SIGSEGV);
5134 return 0;
5135}
5136
Andreas Färber05390242012-02-25 03:37:53 +01005137long do_rt_sigreturn(CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005138{
Laurent Vivier71811552009-08-03 16:12:18 +02005139 struct target_rt_sigframe *frame;
5140 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05005141 target_sigset_t target_set;
Laurent Vivier71811552009-08-03 16:12:18 +02005142 sigset_t set;
5143 int d0;
5144
5145 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5146 goto badframe;
5147
5148 target_to_host_sigset_internal(&set, &target_set);
5149 sigprocmask(SIG_SETMASK, &set, NULL);
5150
5151 /* restore registers */
5152
5153 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5154 goto badframe;
5155
5156 if (do_sigaltstack(frame_addr +
Aurelien Jarno60e99242010-03-29 02:12:51 +02005157 offsetof(struct target_rt_sigframe, uc.tuc_stack),
Laurent Vivier71811552009-08-03 16:12:18 +02005158 0, get_sp_from_cpustate(env)) == -EFAULT)
5159 goto badframe;
5160
5161 unlock_user_struct(frame, frame_addr, 0);
5162 return d0;
5163
5164badframe:
5165 unlock_user_struct(frame, frame_addr, 0);
5166 force_sig(TARGET_SIGSEGV);
5167 return 0;
Laurent Vivier492a8742009-08-03 16:12:17 +02005168}
5169
Richard Henderson6049f4f2009-12-27 18:30:03 -08005170#elif defined(TARGET_ALPHA)
5171
5172struct target_sigcontext {
5173 abi_long sc_onstack;
5174 abi_long sc_mask;
5175 abi_long sc_pc;
5176 abi_long sc_ps;
5177 abi_long sc_regs[32];
5178 abi_long sc_ownedfp;
5179 abi_long sc_fpregs[32];
5180 abi_ulong sc_fpcr;
5181 abi_ulong sc_fp_control;
5182 abi_ulong sc_reserved1;
5183 abi_ulong sc_reserved2;
5184 abi_ulong sc_ssize;
5185 abi_ulong sc_sbase;
5186 abi_ulong sc_traparg_a0;
5187 abi_ulong sc_traparg_a1;
5188 abi_ulong sc_traparg_a2;
5189 abi_ulong sc_fp_trap_pc;
5190 abi_ulong sc_fp_trigger_sum;
5191 abi_ulong sc_fp_trigger_inst;
5192};
5193
5194struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02005195 abi_ulong tuc_flags;
5196 abi_ulong tuc_link;
5197 abi_ulong tuc_osf_sigmask;
5198 target_stack_t tuc_stack;
5199 struct target_sigcontext tuc_mcontext;
5200 target_sigset_t tuc_sigmask;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005201};
5202
5203struct target_sigframe {
5204 struct target_sigcontext sc;
5205 unsigned int retcode[3];
5206};
5207
5208struct target_rt_sigframe {
5209 target_siginfo_t info;
5210 struct target_ucontext uc;
5211 unsigned int retcode[3];
5212};
5213
5214#define INSN_MOV_R30_R16 0x47fe0410
5215#define INSN_LDI_R0 0x201f0000
5216#define INSN_CALLSYS 0x00000083
5217
Andreas Färber05390242012-02-25 03:37:53 +01005218static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
Richard Henderson6049f4f2009-12-27 18:30:03 -08005219 abi_ulong frame_addr, target_sigset_t *set)
5220{
5221 int i, err = 0;
5222
5223 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5224 err |= __put_user(set->sig[0], &sc->sc_mask);
5225 err |= __put_user(env->pc, &sc->sc_pc);
5226 err |= __put_user(8, &sc->sc_ps);
5227
5228 for (i = 0; i < 31; ++i) {
5229 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
5230 }
5231 err |= __put_user(0, &sc->sc_regs[31]);
5232
5233 for (i = 0; i < 31; ++i) {
5234 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
5235 }
5236 err |= __put_user(0, &sc->sc_fpregs[31]);
5237 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
5238
5239 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5240 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5241 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
5242
5243 return err;
5244}
5245
Andreas Färber05390242012-02-25 03:37:53 +01005246static int restore_sigcontext(CPUAlphaState *env,
5247 struct target_sigcontext *sc)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005248{
5249 uint64_t fpcr;
5250 int i, err = 0;
5251
5252 err |= __get_user(env->pc, &sc->sc_pc);
5253
5254 for (i = 0; i < 31; ++i) {
5255 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
5256 }
5257 for (i = 0; i < 31; ++i) {
5258 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
5259 }
5260
5261 err |= __get_user(fpcr, &sc->sc_fpcr);
5262 cpu_alpha_store_fpcr(env, fpcr);
5263
5264 return err;
5265}
5266
5267static inline abi_ulong get_sigframe(struct target_sigaction *sa,
Andreas Färber05390242012-02-25 03:37:53 +01005268 CPUAlphaState *env,
5269 unsigned long framesize)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005270{
5271 abi_ulong sp = env->ir[IR_SP];
5272
5273 /* This is the X/Open sanctioned signal stack switching. */
5274 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5275 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5276 }
5277 return (sp - framesize) & -32;
5278}
5279
5280static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01005281 target_sigset_t *set, CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005282{
5283 abi_ulong frame_addr, r26;
5284 struct target_sigframe *frame;
5285 int err = 0;
5286
5287 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5288 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5289 goto give_sigsegv;
5290 }
5291
5292 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5293
5294 if (ka->sa_restorer) {
5295 r26 = ka->sa_restorer;
5296 } else {
5297 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5298 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5299 &frame->retcode[1]);
5300 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5301 /* imb() */
5302 r26 = frame_addr;
5303 }
5304
5305 unlock_user_struct(frame, frame_addr, 1);
5306
5307 if (err) {
5308 give_sigsegv:
5309 if (sig == TARGET_SIGSEGV) {
5310 ka->_sa_handler = TARGET_SIG_DFL;
5311 }
5312 force_sig(TARGET_SIGSEGV);
5313 }
5314
5315 env->ir[IR_RA] = r26;
5316 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5317 env->ir[IR_A0] = sig;
5318 env->ir[IR_A1] = 0;
5319 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5320 env->ir[IR_SP] = frame_addr;
5321}
5322
5323static void setup_rt_frame(int sig, struct target_sigaction *ka,
5324 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01005325 target_sigset_t *set, CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005326{
5327 abi_ulong frame_addr, r26;
5328 struct target_rt_sigframe *frame;
5329 int i, err = 0;
5330
5331 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5332 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5333 goto give_sigsegv;
5334 }
5335
5336 err |= copy_siginfo_to_user(&frame->info, info);
5337
Aurelien Jarno60e99242010-03-29 02:12:51 +02005338 err |= __put_user(0, &frame->uc.tuc_flags);
5339 err |= __put_user(0, &frame->uc.tuc_link);
5340 err |= __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005341 err |= __put_user(target_sigaltstack_used.ss_sp,
Aurelien Jarno60e99242010-03-29 02:12:51 +02005342 &frame->uc.tuc_stack.ss_sp);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005343 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
Aurelien Jarno60e99242010-03-29 02:12:51 +02005344 &frame->uc.tuc_stack.ss_flags);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005345 err |= __put_user(target_sigaltstack_used.ss_size,
Aurelien Jarno60e99242010-03-29 02:12:51 +02005346 &frame->uc.tuc_stack.ss_size);
5347 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005348 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02005349 err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005350 }
5351
5352 if (ka->sa_restorer) {
5353 r26 = ka->sa_restorer;
5354 } else {
5355 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5356 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5357 &frame->retcode[1]);
5358 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
5359 /* imb(); */
5360 r26 = frame_addr;
5361 }
5362
5363 if (err) {
5364 give_sigsegv:
5365 if (sig == TARGET_SIGSEGV) {
5366 ka->_sa_handler = TARGET_SIG_DFL;
5367 }
5368 force_sig(TARGET_SIGSEGV);
5369 }
5370
5371 env->ir[IR_RA] = r26;
5372 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5373 env->ir[IR_A0] = sig;
5374 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5375 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5376 env->ir[IR_SP] = frame_addr;
5377}
5378
Andreas Färber05390242012-02-25 03:37:53 +01005379long do_sigreturn(CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005380{
5381 struct target_sigcontext *sc;
5382 abi_ulong sc_addr = env->ir[IR_A0];
5383 target_sigset_t target_set;
5384 sigset_t set;
5385
5386 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5387 goto badframe;
5388 }
5389
5390 target_sigemptyset(&target_set);
5391 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5392 goto badframe;
5393 }
5394
5395 target_to_host_sigset_internal(&set, &target_set);
5396 sigprocmask(SIG_SETMASK, &set, NULL);
5397
5398 if (restore_sigcontext(env, sc)) {
5399 goto badframe;
5400 }
5401 unlock_user_struct(sc, sc_addr, 0);
5402 return env->ir[IR_V0];
5403
5404 badframe:
5405 unlock_user_struct(sc, sc_addr, 0);
5406 force_sig(TARGET_SIGSEGV);
5407}
5408
Andreas Färber05390242012-02-25 03:37:53 +01005409long do_rt_sigreturn(CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005410{
5411 abi_ulong frame_addr = env->ir[IR_A0];
5412 struct target_rt_sigframe *frame;
5413 sigset_t set;
5414
5415 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5416 goto badframe;
5417 }
Aurelien Jarno60e99242010-03-29 02:12:51 +02005418 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005419 sigprocmask(SIG_SETMASK, &set, NULL);
5420
Aurelien Jarno60e99242010-03-29 02:12:51 +02005421 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005422 goto badframe;
5423 }
5424 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
Aurelien Jarno60e99242010-03-29 02:12:51 +02005425 uc.tuc_stack),
Richard Henderson6049f4f2009-12-27 18:30:03 -08005426 0, env->ir[IR_SP]) == -EFAULT) {
5427 goto badframe;
5428 }
5429
5430 unlock_user_struct(frame, frame_addr, 0);
5431 return env->ir[IR_V0];
5432
5433
5434 badframe:
5435 unlock_user_struct(frame, frame_addr, 0);
5436 force_sig(TARGET_SIGSEGV);
5437}
5438
bellardb346ff42003-06-15 20:05:50 +00005439#else
5440
pbrook624f7972008-05-31 16:11:38 +00005441static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber9349b4f2012-03-14 01:38:32 +01005442 target_sigset_t *set, CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005443{
5444 fprintf(stderr, "setup_frame: not implemented\n");
5445}
5446
pbrook624f7972008-05-31 16:11:38 +00005447static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05005448 target_siginfo_t *info,
Andreas Färber9349b4f2012-03-14 01:38:32 +01005449 target_sigset_t *set, CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005450{
5451 fprintf(stderr, "setup_rt_frame: not implemented\n");
5452}
5453
Andreas Färber9349b4f2012-03-14 01:38:32 +01005454long do_sigreturn(CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005455{
5456 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00005457 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00005458}
5459
Andreas Färber9349b4f2012-03-14 01:38:32 +01005460long do_rt_sigreturn(CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005461{
5462 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00005463 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00005464}
5465
bellard66fb9762003-03-23 01:06:05 +00005466#endif
5467
Andreas Färber9349b4f2012-03-14 01:38:32 +01005468void process_pending_signals(CPUArchState *cpu_env)
bellard66fb9762003-03-23 01:06:05 +00005469{
5470 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00005471 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00005472 sigset_t set, old_set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005473 target_sigset_t target_old_set;
pbrook624f7972008-05-31 16:11:38 +00005474 struct emulated_sigtable *k;
5475 struct target_sigaction *sa;
bellard66fb9762003-03-23 01:06:05 +00005476 struct sigqueue *q;
pbrook624f7972008-05-31 16:11:38 +00005477 TaskState *ts = cpu_env->opaque;
ths3b46e622007-09-17 08:09:54 +00005478
pbrook624f7972008-05-31 16:11:38 +00005479 if (!ts->signal_pending)
bellard31e31b82003-02-18 22:55:36 +00005480 return;
5481
pbrook624f7972008-05-31 16:11:38 +00005482 /* FIXME: This is not threadsafe. */
5483 k = ts->sigtab;
bellard66fb9762003-03-23 01:06:05 +00005484 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5485 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00005486 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00005487 k++;
bellard31e31b82003-02-18 22:55:36 +00005488 }
5489 /* if no signal is pending, just return */
pbrook624f7972008-05-31 16:11:38 +00005490 ts->signal_pending = 0;
bellard31e31b82003-02-18 22:55:36 +00005491 return;
bellard66fb9762003-03-23 01:06:05 +00005492
bellard31e31b82003-02-18 22:55:36 +00005493 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00005494#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00005495 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00005496#endif
5497 /* dequeue signal */
5498 q = k->first;
5499 k->first = q->next;
5500 if (!k->first)
5501 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00005502
bellard1fddef42005-04-17 19:16:13 +00005503 sig = gdb_handlesig (cpu_env, sig);
5504 if (!sig) {
aurel32ca587a82008-12-18 22:44:13 +00005505 sa = NULL;
5506 handler = TARGET_SIG_IGN;
5507 } else {
5508 sa = &sigact_table[sig - 1];
5509 handler = sa->_sa_handler;
bellard1fddef42005-04-17 19:16:13 +00005510 }
bellard66fb9762003-03-23 01:06:05 +00005511
bellard66fb9762003-03-23 01:06:05 +00005512 if (handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +00005513 /* default handler : ignore some signal. The other are job control or fatal */
5514 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5515 kill(getpid(),SIGSTOP);
5516 } else if (sig != TARGET_SIGCHLD &&
5517 sig != TARGET_SIGURG &&
5518 sig != TARGET_SIGWINCH &&
5519 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +00005520 force_sig(sig);
5521 }
5522 } else if (handler == TARGET_SIG_IGN) {
5523 /* ignore sig */
5524 } else if (handler == TARGET_SIG_ERR) {
5525 force_sig(sig);
5526 } else {
bellard9de5e442003-03-23 16:49:39 +00005527 /* compute the blocked signals during the handler execution */
pbrook624f7972008-05-31 16:11:38 +00005528 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +00005529 /* SA_NODEFER indicates that the current signal should not be
5530 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +00005531 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +00005532 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00005533
bellard9de5e442003-03-23 16:49:39 +00005534 /* block signals in the handler using Linux */
5535 sigprocmask(SIG_BLOCK, &set, &old_set);
5536 /* save the previous blocked signal state to restore it at the
5537 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00005538 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00005539
bellardbc8a22c2003-03-30 21:02:40 +00005540 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00005541#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00005542 {
5543 CPUX86State *env = cpu_env;
5544 if (env->eflags & VM_MASK)
5545 save_v86_state(env);
5546 }
5547#endif
bellard9de5e442003-03-23 16:49:39 +00005548 /* prepare the stack frame of the virtual CPU */
pbrook624f7972008-05-31 16:11:38 +00005549 if (sa->sa_flags & TARGET_SA_SIGINFO)
5550 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00005551 else
pbrook624f7972008-05-31 16:11:38 +00005552 setup_frame(sig, sa, &target_old_set, cpu_env);
5553 if (sa->sa_flags & TARGET_SA_RESETHAND)
5554 sa->_sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00005555 }
bellard66fb9762003-03-23 01:06:05 +00005556 if (q != &k->info)
pbrook624f7972008-05-31 16:11:38 +00005557 free_sigqueue(cpu_env, q);
bellard31e31b82003-02-18 22:55:36 +00005558}