blob: 74c54bfd35c174ba23a679364e989ca0cb938c32 [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{
Andreas Schwab167c50d2013-07-02 14:04:12 +0100105 if (sig < 0 || 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{
Andreas Schwab167c50d2013-07-02 14:04:12 +0100112 if (sig < 0 || 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
Alex Barcelo1c275922014-03-14 14:36:55 +0000200/* Wrapper for sigprocmask function
201 * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
202 * are host signal set, not guest ones. This wraps the sigprocmask host calls
203 * that should be protected (calls originated from guest)
204 */
205int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
206{
Peter Maydella7ec0f92014-03-14 14:36:56 +0000207 int ret;
208 sigset_t val;
209 sigset_t *temp = NULL;
210 CPUState *cpu = thread_cpu;
211 TaskState *ts = (TaskState *)cpu->opaque;
212 bool segv_was_blocked = ts->sigsegv_blocked;
213
214 if (set) {
215 bool has_sigsegv = sigismember(set, SIGSEGV);
216 val = *set;
217 temp = &val;
218
219 sigdelset(temp, SIGSEGV);
220
221 switch (how) {
222 case SIG_BLOCK:
223 if (has_sigsegv) {
224 ts->sigsegv_blocked = true;
225 }
226 break;
227 case SIG_UNBLOCK:
228 if (has_sigsegv) {
229 ts->sigsegv_blocked = false;
230 }
231 break;
232 case SIG_SETMASK:
233 ts->sigsegv_blocked = has_sigsegv;
234 break;
235 default:
236 g_assert_not_reached();
237 }
238 }
239
240 ret = sigprocmask(how, temp, oldset);
241
242 if (oldset && segv_was_blocked) {
243 sigaddset(oldset, SIGSEGV);
244 }
245
246 return ret;
Alex Barcelo1c275922014-03-14 14:36:55 +0000247}
248
bellard9de5e442003-03-23 16:49:39 +0000249/* siginfo conversion */
250
Anthony Liguoric227f092009-10-01 16:12:16 -0500251static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000252 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000253{
Richard Hendersona05c6402012-09-15 11:34:20 -0700254 int sig = host_to_target_signal(info->si_signo);
bellard9de5e442003-03-23 16:49:39 +0000255 tinfo->si_signo = sig;
256 tinfo->si_errno = 0;
pbrookafd7cd92008-05-31 12:14:21 +0000257 tinfo->si_code = info->si_code;
Richard Hendersona05c6402012-09-15 11:34:20 -0700258
259 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
260 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
261 /* Should never come here, but who knows. The information for
262 the target is irrelevant. */
bellard9de5e442003-03-23 16:49:39 +0000263 tinfo->_sifields._sigfault._addr = 0;
Richard Hendersona05c6402012-09-15 11:34:20 -0700264 } else if (sig == TARGET_SIGIO) {
265 tinfo->_sifields._sigpoll._band = info->si_band;
ths7f7f7c82007-07-12 11:02:46 +0000266 tinfo->_sifields._sigpoll._fd = info->si_fd;
Richard Hendersona05c6402012-09-15 11:34:20 -0700267 } else if (sig == TARGET_SIGCHLD) {
268 tinfo->_sifields._sigchld._pid = info->si_pid;
269 tinfo->_sifields._sigchld._uid = info->si_uid;
270 tinfo->_sifields._sigchld._status
271 = host_to_target_waitstatus(info->si_status);
272 tinfo->_sifields._sigchld._utime = info->si_utime;
273 tinfo->_sifields._sigchld._stime = info->si_stime;
bellard9de5e442003-03-23 16:49:39 +0000274 } else if (sig >= TARGET_SIGRTMIN) {
275 tinfo->_sifields._rt._pid = info->si_pid;
276 tinfo->_sifields._rt._uid = info->si_uid;
277 /* XXX: potential problem if 64 bit */
Richard Hendersona05c6402012-09-15 11:34:20 -0700278 tinfo->_sifields._rt._sigval.sival_ptr
279 = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000280 }
bellard66fb9762003-03-23 01:06:05 +0000281}
282
Anthony Liguoric227f092009-10-01 16:12:16 -0500283static void tswap_siginfo(target_siginfo_t *tinfo,
284 const target_siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000285{
Richard Hendersona05c6402012-09-15 11:34:20 -0700286 int sig = info->si_signo;
bellard9de5e442003-03-23 16:49:39 +0000287 tinfo->si_signo = tswap32(sig);
288 tinfo->si_errno = tswap32(info->si_errno);
289 tinfo->si_code = tswap32(info->si_code);
Richard Hendersona05c6402012-09-15 11:34:20 -0700290
291 if (sig == TARGET_SIGILL || sig == TARGET_SIGFPE || sig == TARGET_SIGSEGV
292 || sig == TARGET_SIGBUS || sig == TARGET_SIGTRAP) {
293 tinfo->_sifields._sigfault._addr
294 = tswapal(info->_sifields._sigfault._addr);
295 } else if (sig == TARGET_SIGIO) {
296 tinfo->_sifields._sigpoll._band
297 = tswap32(info->_sifields._sigpoll._band);
298 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
299 } else if (sig == TARGET_SIGCHLD) {
300 tinfo->_sifields._sigchld._pid
301 = tswap32(info->_sifields._sigchld._pid);
302 tinfo->_sifields._sigchld._uid
303 = tswap32(info->_sifields._sigchld._uid);
304 tinfo->_sifields._sigchld._status
305 = tswap32(info->_sifields._sigchld._status);
306 tinfo->_sifields._sigchld._utime
307 = tswapal(info->_sifields._sigchld._utime);
308 tinfo->_sifields._sigchld._stime
309 = tswapal(info->_sifields._sigchld._stime);
bellard9de5e442003-03-23 16:49:39 +0000310 } else if (sig >= TARGET_SIGRTMIN) {
311 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
312 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
Richard Hendersona05c6402012-09-15 11:34:20 -0700313 tinfo->_sifields._rt._sigval.sival_ptr
314 = tswapal(info->_sifields._rt._sigval.sival_ptr);
bellard9de5e442003-03-23 16:49:39 +0000315 }
316}
317
318
Anthony Liguoric227f092009-10-01 16:12:16 -0500319void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000320{
321 host_to_target_siginfo_noswap(tinfo, info);
322 tswap_siginfo(tinfo, tinfo);
323}
324
325/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000326/* XXX: find a solution for 64 bit (additional malloced data is needed) */
Anthony Liguoric227f092009-10-01 16:12:16 -0500327void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000328{
329 info->si_signo = tswap32(tinfo->si_signo);
330 info->si_errno = tswap32(tinfo->si_errno);
331 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000332 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
333 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000334 info->si_value.sival_ptr =
Matthias Brauncbb21ee2011-08-12 19:57:41 +0200335 (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000336}
337
aurel32ca587a82008-12-18 22:44:13 +0000338static int fatal_signal (int sig)
339{
340 switch (sig) {
341 case TARGET_SIGCHLD:
342 case TARGET_SIGURG:
343 case TARGET_SIGWINCH:
344 /* Ignored by default. */
345 return 0;
346 case TARGET_SIGCONT:
347 case TARGET_SIGSTOP:
348 case TARGET_SIGTSTP:
349 case TARGET_SIGTTIN:
350 case TARGET_SIGTTOU:
351 /* Job control signals. */
352 return 0;
353 default:
354 return 1;
355 }
356}
357
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300358/* returns 1 if given signal should dump core if not handled */
359static int core_dump_signal(int sig)
360{
361 switch (sig) {
362 case TARGET_SIGABRT:
363 case TARGET_SIGFPE:
364 case TARGET_SIGILL:
365 case TARGET_SIGQUIT:
366 case TARGET_SIGSEGV:
367 case TARGET_SIGTRAP:
368 case TARGET_SIGBUS:
369 return (1);
370 default:
371 return (0);
372 }
373}
374
bellard31e31b82003-02-18 22:55:36 +0000375void signal_init(void)
376{
377 struct sigaction act;
pbrook624f7972008-05-31 16:11:38 +0000378 struct sigaction oact;
bellard9e5f5282003-07-13 17:33:54 +0000379 int i, j;
pbrook624f7972008-05-31 16:11:38 +0000380 int host_sig;
bellard31e31b82003-02-18 22:55:36 +0000381
bellard9e5f5282003-07-13 17:33:54 +0000382 /* generate signal conversion tables */
Arnaud Patard3ca05582009-03-30 01:18:20 +0200383 for(i = 1; i < _NSIG; i++) {
bellard9e5f5282003-07-13 17:33:54 +0000384 if (host_to_target_signal_table[i] == 0)
385 host_to_target_signal_table[i] = i;
386 }
Arnaud Patard3ca05582009-03-30 01:18:20 +0200387 for(i = 1; i < _NSIG; i++) {
bellard9e5f5282003-07-13 17:33:54 +0000388 j = host_to_target_signal_table[i];
389 target_to_host_signal_table[j] = i;
390 }
ths3b46e622007-09-17 08:09:54 +0000391
bellard9de5e442003-03-23 16:49:39 +0000392 /* set all host signal handlers. ALL signals are blocked during
393 the handlers to serialize them. */
pbrook624f7972008-05-31 16:11:38 +0000394 memset(sigact_table, 0, sizeof(sigact_table));
395
bellard9de5e442003-03-23 16:49:39 +0000396 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000397 act.sa_flags = SA_SIGINFO;
398 act.sa_sigaction = host_signal_handler;
pbrook624f7972008-05-31 16:11:38 +0000399 for(i = 1; i <= TARGET_NSIG; i++) {
400 host_sig = target_to_host_signal(i);
401 sigaction(host_sig, NULL, &oact);
402 if (oact.sa_sigaction == (void *)SIG_IGN) {
403 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
404 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
405 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
406 }
407 /* If there's already a handler installed then something has
408 gone horribly wrong, so don't even try to handle that case. */
aurel32ca587a82008-12-18 22:44:13 +0000409 /* Install some handlers for our own use. We need at least
410 SIGSEGV and SIGBUS, to detect exceptions. We can not just
411 trap all signals because it affects syscall interrupt
412 behavior. But do trap all default-fatal signals. */
413 if (fatal_signal (i))
pbrook624f7972008-05-31 16:11:38 +0000414 sigaction(host_sig, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000415 }
bellard31e31b82003-02-18 22:55:36 +0000416}
417
bellard66fb9762003-03-23 01:06:05 +0000418/* signal queue handling */
419
Andreas Färber9349b4f2012-03-14 01:38:32 +0100420static inline struct sigqueue *alloc_sigqueue(CPUArchState *env)
bellard66fb9762003-03-23 01:06:05 +0000421{
Andreas Färber0429a972013-08-26 18:14:44 +0200422 CPUState *cpu = ENV_GET_CPU(env);
423 TaskState *ts = cpu->opaque;
pbrook624f7972008-05-31 16:11:38 +0000424 struct sigqueue *q = ts->first_free;
bellard66fb9762003-03-23 01:06:05 +0000425 if (!q)
426 return NULL;
pbrook624f7972008-05-31 16:11:38 +0000427 ts->first_free = q->next;
bellard66fb9762003-03-23 01:06:05 +0000428 return q;
429}
430
Andreas Färber9349b4f2012-03-14 01:38:32 +0100431static inline void free_sigqueue(CPUArchState *env, struct sigqueue *q)
bellard66fb9762003-03-23 01:06:05 +0000432{
Andreas Färber0429a972013-08-26 18:14:44 +0200433 CPUState *cpu = ENV_GET_CPU(env);
434 TaskState *ts = cpu->opaque;
435
pbrook624f7972008-05-31 16:11:38 +0000436 q->next = ts->first_free;
437 ts->first_free = q;
bellard66fb9762003-03-23 01:06:05 +0000438}
439
bellard9de5e442003-03-23 16:49:39 +0000440/* abort execution with signal */
Riku Voipio66393fb2009-12-04 15:16:32 +0200441static void QEMU_NORETURN force_sig(int target_sig)
bellard66fb9762003-03-23 01:06:05 +0000442{
Andreas Färber0429a972013-08-26 18:14:44 +0200443 CPUState *cpu = thread_cpu;
444 CPUArchState *env = cpu->env_ptr;
445 TaskState *ts = (TaskState *)cpu->opaque;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300446 int host_sig, core_dumped = 0;
aurel32603e4fd2009-04-15 16:18:38 +0000447 struct sigaction act;
Riku Voipio66393fb2009-12-04 15:16:32 +0200448 host_sig = target_to_host_signal(target_sig);
Andreas Färbera2247f82013-06-09 19:47:04 +0200449 gdb_signalled(env, target_sig);
aurel32603e4fd2009-04-15 16:18:38 +0000450
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300451 /* dump core if supported by target binary format */
Riku Voipio66393fb2009-12-04 15:16:32 +0200452 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300453 stop_all_tasks();
454 core_dumped =
Andreas Färbera2247f82013-06-09 19:47:04 +0200455 ((*ts->bprm->core_dump)(target_sig, env) == 0);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300456 }
457 if (core_dumped) {
458 /* we already dumped the core of target process, we don't want
459 * a coredump of qemu itself */
460 struct rlimit nodump;
461 getrlimit(RLIMIT_CORE, &nodump);
462 nodump.rlim_cur=0;
463 setrlimit(RLIMIT_CORE, &nodump);
464 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
Riku Voipio66393fb2009-12-04 15:16:32 +0200465 target_sig, strsignal(host_sig), "core dumped" );
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300466 }
467
Stefan Weil0c587512011-04-28 17:20:32 +0200468 /* The proper exit code for dying from an uncaught signal is
aurel32603e4fd2009-04-15 16:18:38 +0000469 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
470 * a negative value. To get the proper exit code we need to
471 * actually die from an uncaught signal. Here the default signal
472 * handler is installed, we send ourself a signal and we wait for
473 * it to arrive. */
474 sigfillset(&act.sa_mask);
475 act.sa_handler = SIG_DFL;
Peter Maydell3a5d30b2014-02-17 18:55:32 +0000476 act.sa_flags = 0;
aurel32603e4fd2009-04-15 16:18:38 +0000477 sigaction(host_sig, &act, NULL);
478
479 /* For some reason raise(host_sig) doesn't send the signal when
480 * statically linked on x86-64. */
481 kill(getpid(), host_sig);
482
483 /* Make sure the signal isn't masked (just reuse the mask inside
484 of act) */
485 sigdelset(&act.sa_mask, host_sig);
486 sigsuspend(&act.sa_mask);
487
488 /* unreachable */
Blue Swirla6c6f762010-03-13 14:18:50 +0000489 abort();
bellard66fb9762003-03-23 01:06:05 +0000490}
491
bellard9de5e442003-03-23 16:49:39 +0000492/* queue a signal so that it will be send to the virtual CPU as soon
493 as possible */
Andreas Färber9349b4f2012-03-14 01:38:32 +0100494int queue_signal(CPUArchState *env, int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000495{
Andreas Färber0429a972013-08-26 18:14:44 +0200496 CPUState *cpu = ENV_GET_CPU(env);
497 TaskState *ts = cpu->opaque;
pbrook624f7972008-05-31 16:11:38 +0000498 struct emulated_sigtable *k;
bellard9de5e442003-03-23 16:49:39 +0000499 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000500 abi_ulong handler;
aurel32ca587a82008-12-18 22:44:13 +0000501 int queue;
bellard66fb9762003-03-23 01:06:05 +0000502
bellard9de5e442003-03-23 16:49:39 +0000503#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000504 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000505 sig);
bellard66fb9762003-03-23 01:06:05 +0000506#endif
pbrook624f7972008-05-31 16:11:38 +0000507 k = &ts->sigtab[sig - 1];
aurel32ca587a82008-12-18 22:44:13 +0000508 queue = gdb_queuesig ();
pbrook624f7972008-05-31 16:11:38 +0000509 handler = sigact_table[sig - 1]._sa_handler;
Peter Maydella7ec0f92014-03-14 14:36:56 +0000510
511 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
512 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
513 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
514 * because it got a real MMU fault). A blocked SIGSEGV in that
515 * situation is treated as if using the default handler. This is
516 * not correct if some other process has randomly sent us a SIGSEGV
517 * via kill(), but that is not easy to distinguish at this point,
518 * so we assume it doesn't happen.
519 */
520 handler = TARGET_SIG_DFL;
521 }
522
aurel32ca587a82008-12-18 22:44:13 +0000523 if (!queue && handler == TARGET_SIG_DFL) {
ths60b19692008-11-27 15:47:15 +0000524 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
525 kill(getpid(),SIGSTOP);
526 return 0;
527 } else
bellard66fb9762003-03-23 01:06:05 +0000528 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000529 if (sig != TARGET_SIGCHLD &&
530 sig != TARGET_SIGURG &&
ths60b19692008-11-27 15:47:15 +0000531 sig != TARGET_SIGWINCH &&
532 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +0000533 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000534 } else {
535 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000536 }
aurel32ca587a82008-12-18 22:44:13 +0000537 } else if (!queue && handler == TARGET_SIG_IGN) {
bellard66fb9762003-03-23 01:06:05 +0000538 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000539 return 0;
aurel32ca587a82008-12-18 22:44:13 +0000540 } else if (!queue && handler == TARGET_SIG_ERR) {
bellard66fb9762003-03-23 01:06:05 +0000541 force_sig(sig);
542 } else {
bellard9de5e442003-03-23 16:49:39 +0000543 pq = &k->first;
544 if (sig < TARGET_SIGRTMIN) {
545 /* if non real time signal, we queue exactly one signal */
546 if (!k->pending)
547 q = &k->info;
548 else
549 return 0;
550 } else {
551 if (!k->pending) {
552 /* first signal */
553 q = &k->info;
554 } else {
pbrook624f7972008-05-31 16:11:38 +0000555 q = alloc_sigqueue(env);
bellard9de5e442003-03-23 16:49:39 +0000556 if (!q)
557 return -EAGAIN;
558 while (*pq != NULL)
559 pq = &(*pq)->next;
560 }
561 }
562 *pq = q;
563 q->info = *info;
564 q->next = NULL;
565 k->pending = 1;
566 /* signal that a new signal is pending */
pbrook624f7972008-05-31 16:11:38 +0000567 ts->signal_pending = 1;
bellard9de5e442003-03-23 16:49:39 +0000568 return 1; /* indicates that the signal was queued */
569 }
570}
571
ths5fafdf22007-09-16 21:08:06 +0000572static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000573 void *puc)
574{
Andreas Färbera2247f82013-06-09 19:47:04 +0200575 CPUArchState *env = thread_cpu->env_ptr;
bellard9de5e442003-03-23 16:49:39 +0000576 int sig;
Anthony Liguoric227f092009-10-01 16:12:16 -0500577 target_siginfo_t tinfo;
bellard9de5e442003-03-23 16:49:39 +0000578
579 /* the CPU emulator uses some host signals to detect exceptions,
aurel32eaa449b2009-01-03 13:14:52 +0000580 we forward to it some signals */
aurel32ca587a82008-12-18 22:44:13 +0000581 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
aurel32eaa449b2009-01-03 13:14:52 +0000582 && info->si_code > 0) {
bellardb346ff42003-06-15 20:05:50 +0000583 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000584 return;
585 }
586
587 /* get target signal number */
588 sig = host_to_target_signal(host_signum);
589 if (sig < 1 || sig > TARGET_NSIG)
590 return;
591#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000592 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000593#endif
594 host_to_target_siginfo_noswap(&tinfo, info);
Andreas Färbera2247f82013-06-09 19:47:04 +0200595 if (queue_signal(env, sig, &tinfo) == 1) {
bellard9de5e442003-03-23 16:49:39 +0000596 /* interrupt the virtual CPU as soon as possible */
Andreas Färbera2247f82013-06-09 19:47:04 +0200597 cpu_exit(thread_cpu);
bellard66fb9762003-03-23 01:06:05 +0000598 }
bellard31e31b82003-02-18 22:55:36 +0000599}
600
ths0da46a62007-10-20 20:23:07 +0000601/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000602/* compare linux/kernel/signal.c:do_sigaltstack() */
603abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000604{
605 int ret;
606 struct target_sigaltstack oss;
607
608 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000609 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000610 {
611 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
612 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
613 __put_user(sas_ss_flags(sp), &oss.ss_flags);
614 }
615
bellard579a97f2007-11-11 14:26:47 +0000616 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000617 {
bellard579a97f2007-11-11 14:26:47 +0000618 struct target_sigaltstack *uss;
619 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000620
ths0da46a62007-10-20 20:23:07 +0000621 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000622 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000623 || __get_user(ss.ss_sp, &uss->ss_sp)
624 || __get_user(ss.ss_size, &uss->ss_size)
625 || __get_user(ss.ss_flags, &uss->ss_flags))
626 goto out;
bellard579a97f2007-11-11 14:26:47 +0000627 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000628
ths0da46a62007-10-20 20:23:07 +0000629 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000630 if (on_sig_stack(sp))
631 goto out;
632
ths0da46a62007-10-20 20:23:07 +0000633 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000634 if (ss.ss_flags != TARGET_SS_DISABLE
635 && ss.ss_flags != TARGET_SS_ONSTACK
636 && ss.ss_flags != 0)
637 goto out;
638
639 if (ss.ss_flags == TARGET_SS_DISABLE) {
640 ss.ss_size = 0;
641 ss.ss_sp = 0;
642 } else {
ths0da46a62007-10-20 20:23:07 +0000643 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000644 if (ss.ss_size < MINSIGSTKSZ)
645 goto out;
646 }
647
648 target_sigaltstack_used.ss_sp = ss.ss_sp;
649 target_sigaltstack_used.ss_size = ss.ss_size;
650 }
651
bellard579a97f2007-11-11 14:26:47 +0000652 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000653 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000654 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000655 goto out;
thsa04e1342007-09-27 13:57:58 +0000656 }
657
658 ret = 0;
659out:
660 return ret;
661}
662
ths0da46a62007-10-20 20:23:07 +0000663/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000664int do_sigaction(int sig, const struct target_sigaction *act,
665 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000666{
pbrook624f7972008-05-31 16:11:38 +0000667 struct target_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000668 struct sigaction act1;
669 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000670 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000671
ths2a913eb2008-11-27 15:46:25 +0000672 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000673 return -EINVAL;
674 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000675#if defined(DEBUG_SIGNAL)
Blue Swirl0bf9e312009-07-20 17:19:25 +0000676 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
677 sig, act, oact);
bellard66fb9762003-03-23 01:06:05 +0000678#endif
679 if (oact) {
Richard Hendersond2565872013-01-04 16:39:32 -0800680 __put_user(k->_sa_handler, &oact->_sa_handler);
681 __put_user(k->sa_flags, &oact->sa_flags);
ths388bb212007-05-13 13:58:00 +0000682#if !defined(TARGET_MIPS)
Richard Hendersond2565872013-01-04 16:39:32 -0800683 __put_user(k->sa_restorer, &oact->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000684#endif
Richard Hendersond2565872013-01-04 16:39:32 -0800685 /* Not swapped. */
pbrook624f7972008-05-31 16:11:38 +0000686 oact->sa_mask = k->sa_mask;
bellard66fb9762003-03-23 01:06:05 +0000687 }
688 if (act) {
pbrook624f7972008-05-31 16:11:38 +0000689 /* FIXME: This is not threadsafe. */
Richard Hendersond2565872013-01-04 16:39:32 -0800690 __get_user(k->_sa_handler, &act->_sa_handler);
691 __get_user(k->sa_flags, &act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000692#if !defined(TARGET_MIPS)
Richard Hendersond2565872013-01-04 16:39:32 -0800693 __get_user(k->sa_restorer, &act->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000694#endif
Richard Hendersond2565872013-01-04 16:39:32 -0800695 /* To be swapped in target_to_host_sigset. */
pbrook624f7972008-05-31 16:11:38 +0000696 k->sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000697
698 /* we update the host linux signal state */
699 host_sig = target_to_host_signal(sig);
700 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
701 sigfillset(&act1.sa_mask);
702 act1.sa_flags = SA_SIGINFO;
pbrook624f7972008-05-31 16:11:38 +0000703 if (k->sa_flags & TARGET_SA_RESTART)
bellard773b93e2004-01-04 17:15:59 +0000704 act1.sa_flags |= SA_RESTART;
705 /* NOTE: it is important to update the host kernel signal
706 ignore state to avoid getting unexpected interrupted
707 syscalls */
pbrook624f7972008-05-31 16:11:38 +0000708 if (k->_sa_handler == TARGET_SIG_IGN) {
bellard773b93e2004-01-04 17:15:59 +0000709 act1.sa_sigaction = (void *)SIG_IGN;
pbrook624f7972008-05-31 16:11:38 +0000710 } else if (k->_sa_handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +0000711 if (fatal_signal (sig))
712 act1.sa_sigaction = host_signal_handler;
713 else
714 act1.sa_sigaction = (void *)SIG_DFL;
bellard773b93e2004-01-04 17:15:59 +0000715 } else {
716 act1.sa_sigaction = host_signal_handler;
717 }
ths0da46a62007-10-20 20:23:07 +0000718 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000719 }
bellard66fb9762003-03-23 01:06:05 +0000720 }
ths0da46a62007-10-20 20:23:07 +0000721 return ret;
bellard66fb9762003-03-23 01:06:05 +0000722}
bellard31e31b82003-02-18 22:55:36 +0000723
Riku Voipiob0fd8d12014-04-23 10:46:13 +0300724static inline void copy_siginfo_to_user(target_siginfo_t *tinfo,
Anthony Liguoric227f092009-10-01 16:12:16 -0500725 const target_siginfo_t *info)
bellard43fff232003-07-09 19:31:39 +0000726{
727 tswap_siginfo(tinfo, info);
bellard43fff232003-07-09 19:31:39 +0000728}
729
thsc3b5bc82007-12-02 06:31:25 +0000730static inline int current_exec_domain_sig(int sig)
731{
732 return /* current->exec_domain && current->exec_domain->signal_invmap
733 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
734}
735
bellard459a4012007-11-11 19:45:10 +0000736#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000737
738/* from the Linux kernel */
739
740struct target_fpreg {
741 uint16_t significand[4];
742 uint16_t exponent;
743};
744
745struct target_fpxreg {
746 uint16_t significand[4];
747 uint16_t exponent;
748 uint16_t padding[3];
749};
750
751struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000752 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000753};
754
755struct target_fpstate {
756 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000757 abi_ulong cw;
758 abi_ulong sw;
759 abi_ulong tag;
760 abi_ulong ipoff;
761 abi_ulong cssel;
762 abi_ulong dataoff;
763 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000764 struct target_fpreg _st[8];
765 uint16_t status;
766 uint16_t magic; /* 0xffff = regular FPU data only */
767
768 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000769 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
770 abi_ulong mxcsr;
771 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000772 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
773 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000774 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000775};
776
777#define X86_FXSR_MAGIC 0x0000
778
779struct target_sigcontext {
780 uint16_t gs, __gsh;
781 uint16_t fs, __fsh;
782 uint16_t es, __esh;
783 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000784 abi_ulong edi;
785 abi_ulong esi;
786 abi_ulong ebp;
787 abi_ulong esp;
788 abi_ulong ebx;
789 abi_ulong edx;
790 abi_ulong ecx;
791 abi_ulong eax;
792 abi_ulong trapno;
793 abi_ulong err;
794 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000795 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000796 abi_ulong eflags;
797 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000798 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000799 abi_ulong fpstate; /* pointer */
800 abi_ulong oldmask;
801 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000802};
803
bellard66fb9762003-03-23 01:06:05 +0000804struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000805 abi_ulong tuc_flags;
806 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -0500807 target_stack_t tuc_stack;
bellardb8076a72005-04-07 22:20:31 +0000808 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -0500809 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000810};
811
812struct sigframe
813{
blueswir1992f48a2007-10-14 16:27:31 +0000814 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000815 int sig;
816 struct target_sigcontext sc;
817 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000818 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000819 char retcode[8];
820};
821
822struct rt_sigframe
823{
blueswir1992f48a2007-10-14 16:27:31 +0000824 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000825 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000826 abi_ulong pinfo;
827 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000828 struct target_siginfo info;
829 struct target_ucontext uc;
830 struct target_fpstate fpstate;
831 char retcode[8];
832};
833
834/*
835 * Set up a signal frame.
836 */
837
bellard66fb9762003-03-23 01:06:05 +0000838/* XXX: save x87 state */
839static int
840setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
bellard28be6232007-11-11 22:23:38 +0000841 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000842{
Andreas Färber27103422013-08-26 08:31:06 +0200843 CPUState *cs = CPU(x86_env_get_cpu(env));
844 int err = 0;
845 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000846
bellard579a97f2007-11-11 14:26:47 +0000847 /* already locked in setup_frame() */
Riku Voipio1d8b5122014-04-23 10:26:05 +0300848 __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
849 __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
850 __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
851 __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
852 __put_user(env->regs[R_EDI], &sc->edi);
853 __put_user(env->regs[R_ESI], &sc->esi);
854 __put_user(env->regs[R_EBP], &sc->ebp);
855 __put_user(env->regs[R_ESP], &sc->esp);
856 __put_user(env->regs[R_EBX], &sc->ebx);
857 __put_user(env->regs[R_EDX], &sc->edx);
858 __put_user(env->regs[R_ECX], &sc->ecx);
859 __put_user(env->regs[R_EAX], &sc->eax);
860 __put_user(cs->exception_index, &sc->trapno);
861 __put_user(env->error_code, &sc->err);
862 __put_user(env->eip, &sc->eip);
863 __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
864 __put_user(env->eflags, &sc->eflags);
865 __put_user(env->regs[R_ESP], &sc->esp_at_signal);
866 __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000867
bellard28be6232007-11-11 22:23:38 +0000868 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000869 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000870 magic = 0xffff;
Riku Voipio1d8b5122014-04-23 10:26:05 +0300871 __put_user(magic, &fpstate->magic);
872 __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000873
bellard66fb9762003-03-23 01:06:05 +0000874 /* non-iBCS2 extensions.. */
Riku Voipio1d8b5122014-04-23 10:26:05 +0300875 __put_user(mask, &sc->oldmask);
876 __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000877 return err;
878}
879
880/*
881 * Determine which stack to use..
882 */
883
bellard579a97f2007-11-11 14:26:47 +0000884static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +0000885get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
bellard66fb9762003-03-23 01:06:05 +0000886{
887 unsigned long esp;
888
889 /* Default to using normal stack */
890 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000891 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +0000892 if (ka->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +0000893 if (sas_ss_flags(esp) == 0)
894 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
895 }
bellard66fb9762003-03-23 01:06:05 +0000896
897 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000898 else
bellarda52c7572003-06-21 13:14:12 +0000899 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
pbrook624f7972008-05-31 16:11:38 +0000900 !(ka->sa_flags & TARGET_SA_RESTORER) &&
901 ka->sa_restorer) {
902 esp = (unsigned long) ka->sa_restorer;
bellarda52c7572003-06-21 13:14:12 +0000903 }
bellard579a97f2007-11-11 14:26:47 +0000904 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000905}
906
bellard579a97f2007-11-11 14:26:47 +0000907/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +0000908static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500909 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000910{
bellard579a97f2007-11-11 14:26:47 +0000911 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000912 struct sigframe *frame;
Riku Voipio7df2fa32014-04-23 10:34:53 +0300913 int i;
bellard66fb9762003-03-23 01:06:05 +0000914
bellard579a97f2007-11-11 14:26:47 +0000915 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000916
bellard579a97f2007-11-11 14:26:47 +0000917 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000918 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000919
Riku Voipio1d8b5122014-04-23 10:26:05 +0300920 __put_user(current_exec_domain_sig(sig),
921 &frame->sig);
bellard66fb9762003-03-23 01:06:05 +0000922
bellard28be6232007-11-11 22:23:38 +0000923 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
924 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000925
Riku Voipio7df2fa32014-04-23 10:34:53 +0300926 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
927 __put_user(set->sig[i], &frame->extramask[i - 1]);
928 }
bellard66fb9762003-03-23 01:06:05 +0000929
930 /* Set up to return from userspace. If provided, use a stub
931 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000932 if (ka->sa_flags & TARGET_SA_RESTORER) {
Riku Voipio1d8b5122014-04-23 10:26:05 +0300933 __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000934 } else {
bellard775b58d2007-11-11 16:22:17 +0000935 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000936 abi_ulong retcode_addr;
937 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
Riku Voipio1d8b5122014-04-23 10:26:05 +0300938 __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000939 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000940 val16 = 0xb858;
Riku Voipio1d8b5122014-04-23 10:26:05 +0300941 __put_user(val16, (uint16_t *)(frame->retcode+0));
942 __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000943 val16 = 0x80cd;
Riku Voipio1d8b5122014-04-23 10:26:05 +0300944 __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000945 }
946
bellard66fb9762003-03-23 01:06:05 +0000947
948 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000949 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000950 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000951
952 cpu_x86_load_seg(env, R_DS, __USER_DS);
953 cpu_x86_load_seg(env, R_ES, __USER_DS);
954 cpu_x86_load_seg(env, R_SS, __USER_DS);
955 cpu_x86_load_seg(env, R_CS, __USER_CS);
956 env->eflags &= ~TF_MASK;
957
bellard579a97f2007-11-11 14:26:47 +0000958 unlock_user_struct(frame, frame_addr, 1);
959
bellard66fb9762003-03-23 01:06:05 +0000960 return;
961
962give_sigsegv:
963 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000964 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000965 force_sig(TARGET_SIGSEGV /* , current */);
966}
967
bellard579a97f2007-11-11 14:26:47 +0000968/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +0000969static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500970 target_siginfo_t *info,
971 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000972{
bellard28be6232007-11-11 22:23:38 +0000973 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000974 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000975 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000976
bellard579a97f2007-11-11 14:26:47 +0000977 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000978
bellard579a97f2007-11-11 14:26:47 +0000979 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000980 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000981
Riku Voipio1d8b5122014-04-23 10:26:05 +0300982 __put_user(current_exec_domain_sig(sig), &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000983 addr = frame_addr + offsetof(struct rt_sigframe, info);
Riku Voipio1d8b5122014-04-23 10:26:05 +0300984 __put_user(addr, &frame->pinfo);
bellard28be6232007-11-11 22:23:38 +0000985 addr = frame_addr + offsetof(struct rt_sigframe, uc);
Riku Voipio1d8b5122014-04-23 10:26:05 +0300986 __put_user(addr, &frame->puc);
Riku Voipiob0fd8d12014-04-23 10:46:13 +0300987 copy_siginfo_to_user(&frame->info, info);
bellard66fb9762003-03-23 01:06:05 +0000988
989 /* Create the ucontext. */
Riku Voipio1d8b5122014-04-23 10:26:05 +0300990 __put_user(0, &frame->uc.tuc_flags);
991 __put_user(0, &frame->uc.tuc_link);
992 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
993 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
994 &frame->uc.tuc_stack.ss_flags);
995 __put_user(target_sigaltstack_used.ss_size,
996 &frame->uc.tuc_stack.ss_size);
bellardb8076a72005-04-07 22:20:31 +0000997 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000998 env, set->sig[0],
999 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +00001000 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001001 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +00001002 goto give_sigsegv;
1003 }
bellard66fb9762003-03-23 01:06:05 +00001004
1005 /* Set up to return from userspace. If provided, use a stub
1006 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00001007 if (ka->sa_flags & TARGET_SA_RESTORER) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03001008 __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +00001009 } else {
bellard775b58d2007-11-11 16:22:17 +00001010 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +00001011 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
Riku Voipio1d8b5122014-04-23 10:26:05 +03001012 __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +00001013 /* This is movl $,%eax ; int $0x80 */
Riku Voipio1d8b5122014-04-23 10:26:05 +03001014 __put_user(0xb8, (char *)(frame->retcode+0));
1015 __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +00001016 val16 = 0x80cd;
Riku Voipio1d8b5122014-04-23 10:26:05 +03001017 __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +00001018 }
1019
1020 if (err)
1021 goto give_sigsegv;
1022
1023 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +00001024 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +00001025 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +00001026
1027 cpu_x86_load_seg(env, R_DS, __USER_DS);
1028 cpu_x86_load_seg(env, R_ES, __USER_DS);
1029 cpu_x86_load_seg(env, R_SS, __USER_DS);
1030 cpu_x86_load_seg(env, R_CS, __USER_CS);
1031 env->eflags &= ~TF_MASK;
1032
bellard579a97f2007-11-11 14:26:47 +00001033 unlock_user_struct(frame, frame_addr, 1);
1034
bellard66fb9762003-03-23 01:06:05 +00001035 return;
1036
1037give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00001038 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +00001039 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +00001040 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +00001041 force_sig(TARGET_SIGSEGV /* , current */);
1042}
1043
1044static int
1045restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
1046{
1047 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +00001048 abi_ulong fpstate_addr;
1049 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +00001050
bellard28be6232007-11-11 22:23:38 +00001051 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
1052 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
1053 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
1054 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +00001055
bellard28be6232007-11-11 22:23:38 +00001056 env->regs[R_EDI] = tswapl(sc->edi);
1057 env->regs[R_ESI] = tswapl(sc->esi);
1058 env->regs[R_EBP] = tswapl(sc->ebp);
1059 env->regs[R_ESP] = tswapl(sc->esp);
1060 env->regs[R_EBX] = tswapl(sc->ebx);
1061 env->regs[R_EDX] = tswapl(sc->edx);
1062 env->regs[R_ECX] = tswapl(sc->ecx);
1063 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +00001064
Mike McCormack9a826d72011-06-01 15:14:37 +09001065 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1066 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +00001067
bellard28be6232007-11-11 22:23:38 +00001068 tmpflags = tswapl(sc->eflags);
1069 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1070 // regs->orig_eax = -1; /* disable syscall checks */
1071
1072 fpstate_addr = tswapl(sc->fpstate);
1073 if (fpstate_addr != 0) {
1074 if (!access_ok(VERIFY_READ, fpstate_addr,
1075 sizeof(struct target_fpstate)))
1076 goto badframe;
1077 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +00001078 }
1079
bellard28be6232007-11-11 22:23:38 +00001080 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +00001081 return err;
bellard66fb9762003-03-23 01:06:05 +00001082badframe:
1083 return 1;
bellard66fb9762003-03-23 01:06:05 +00001084}
1085
1086long do_sigreturn(CPUX86State *env)
1087{
bellard579a97f2007-11-11 14:26:47 +00001088 struct sigframe *frame;
1089 abi_ulong frame_addr = env->regs[R_ESP] - 8;
Anthony Liguoric227f092009-10-01 16:12:16 -05001090 target_sigset_t target_set;
bellard66fb9762003-03-23 01:06:05 +00001091 sigset_t set;
1092 int eax, i;
1093
bellard447db212003-05-10 15:10:36 +00001094#if defined(DEBUG_SIGNAL)
1095 fprintf(stderr, "do_sigreturn\n");
1096#endif
bellard579a97f2007-11-11 14:26:47 +00001097 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1098 goto badframe;
bellard66fb9762003-03-23 01:06:05 +00001099 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +00001100 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1101 goto badframe;
1102 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1103 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1104 goto badframe;
1105 }
bellard66fb9762003-03-23 01:06:05 +00001106
bellard92319442004-06-19 16:58:13 +00001107 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00001108 do_sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +00001109
bellard66fb9762003-03-23 01:06:05 +00001110 /* restore registers */
1111 if (restore_sigcontext(env, &frame->sc, &eax))
1112 goto badframe;
bellard579a97f2007-11-11 14:26:47 +00001113 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001114 return eax;
1115
1116badframe:
bellard579a97f2007-11-11 14:26:47 +00001117 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001118 force_sig(TARGET_SIGSEGV);
1119 return 0;
1120}
1121
1122long do_rt_sigreturn(CPUX86State *env)
1123{
bellard28be6232007-11-11 22:23:38 +00001124 abi_ulong frame_addr;
1125 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +00001126 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +00001127 int eax;
1128
bellard28be6232007-11-11 22:23:38 +00001129 frame_addr = env->regs[R_ESP] - 4;
1130 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1131 goto badframe;
bellardb8076a72005-04-07 22:20:31 +00001132 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00001133 do_sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +00001134
bellardb8076a72005-04-07 22:20:31 +00001135 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +00001136 goto badframe;
1137
bellard28be6232007-11-11 22:23:38 +00001138 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1139 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +00001140 goto badframe;
thsa04e1342007-09-27 13:57:58 +00001141
bellard28be6232007-11-11 22:23:38 +00001142 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001143 return eax;
1144
1145badframe:
bellard28be6232007-11-11 22:23:38 +00001146 unlock_user_struct(frame, frame_addr, 0);
1147 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +00001148 return 0;
1149}
1150
Andreas Schwab1744aea2013-09-03 20:12:16 +01001151#elif defined(TARGET_AARCH64)
1152
1153struct target_sigcontext {
1154 uint64_t fault_address;
1155 /* AArch64 registers */
1156 uint64_t regs[31];
1157 uint64_t sp;
1158 uint64_t pc;
1159 uint64_t pstate;
1160 /* 4K reserved for FP/SIMD state and future expansion */
1161 char __reserved[4096] __attribute__((__aligned__(16)));
1162};
1163
1164struct target_ucontext {
1165 abi_ulong tuc_flags;
1166 abi_ulong tuc_link;
1167 target_stack_t tuc_stack;
1168 target_sigset_t tuc_sigmask;
1169 /* glibc uses a 1024-bit sigset_t */
1170 char __unused[1024 / 8 - sizeof(target_sigset_t)];
1171 /* last for future expansion */
1172 struct target_sigcontext tuc_mcontext;
1173};
1174
1175/*
1176 * Header to be used at the beginning of structures extending the user
1177 * context. Such structures must be placed after the rt_sigframe on the stack
1178 * and be 16-byte aligned. The last structure must be a dummy one with the
1179 * magic and size set to 0.
1180 */
1181struct target_aarch64_ctx {
1182 uint32_t magic;
1183 uint32_t size;
1184};
1185
1186#define TARGET_FPSIMD_MAGIC 0x46508001
1187
1188struct target_fpsimd_context {
1189 struct target_aarch64_ctx head;
1190 uint32_t fpsr;
1191 uint32_t fpcr;
1192 uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
1193};
1194
1195/*
1196 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1197 * user space as it will change with the addition of new context. User space
1198 * should check the magic/size information.
1199 */
1200struct target_aux_context {
1201 struct target_fpsimd_context fpsimd;
1202 /* additional context to be added before "end" */
1203 struct target_aarch64_ctx end;
1204};
1205
1206struct target_rt_sigframe {
1207 struct target_siginfo info;
1208 struct target_ucontext uc;
1209 uint64_t fp;
1210 uint64_t lr;
1211 uint32_t tramp[2];
1212};
1213
1214static int target_setup_sigframe(struct target_rt_sigframe *sf,
1215 CPUARMState *env, target_sigset_t *set)
1216{
1217 int i;
1218 struct target_aux_context *aux =
1219 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1220
1221 /* set up the stack frame for unwinding */
1222 __put_user(env->xregs[29], &sf->fp);
1223 __put_user(env->xregs[30], &sf->lr);
1224
1225 for (i = 0; i < 31; i++) {
1226 __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1227 }
1228 __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1229 __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
Peter Maydelld3563122013-12-17 19:42:30 +00001230 __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001231
Peter Maydell7af03922014-05-01 18:36:17 +01001232 __put_user(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001233
1234 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1235 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1236 }
1237
1238 for (i = 0; i < 32; i++) {
1239#ifdef TARGET_WORDS_BIGENDIAN
1240 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1241 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1242#else
1243 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1244 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1245#endif
1246 }
Will Newtone0ee1382014-01-04 22:15:48 +00001247 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1248 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001249 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1250 __put_user(sizeof(struct target_fpsimd_context),
1251 &aux->fpsimd.head.size);
1252
1253 /* set the "end" magic */
1254 __put_user(0, &aux->end.magic);
1255 __put_user(0, &aux->end.size);
1256
1257 return 0;
1258}
1259
1260static int target_restore_sigframe(CPUARMState *env,
1261 struct target_rt_sigframe *sf)
1262{
1263 sigset_t set;
1264 int i;
1265 struct target_aux_context *aux =
1266 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
Will Newtone0ee1382014-01-04 22:15:48 +00001267 uint32_t magic, size, fpsr, fpcr;
Peter Maydelld3563122013-12-17 19:42:30 +00001268 uint64_t pstate;
Andreas Schwab1744aea2013-09-03 20:12:16 +01001269
1270 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00001271 do_sigprocmask(SIG_SETMASK, &set, NULL);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001272
1273 for (i = 0; i < 31; i++) {
1274 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1275 }
1276
1277 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1278 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
Peter Maydelld3563122013-12-17 19:42:30 +00001279 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1280 pstate_write(env, pstate);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001281
1282 __get_user(magic, &aux->fpsimd.head.magic);
1283 __get_user(size, &aux->fpsimd.head.size);
1284
1285 if (magic != TARGET_FPSIMD_MAGIC
1286 || size != sizeof(struct target_fpsimd_context)) {
1287 return 1;
1288 }
1289
Peter Maydell4cf23482014-03-02 19:36:38 +00001290 for (i = 0; i < 32; i++) {
1291#ifdef TARGET_WORDS_BIGENDIAN
1292 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1293 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1294#else
1295 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1296 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1297#endif
Andreas Schwab1744aea2013-09-03 20:12:16 +01001298 }
Will Newtone0ee1382014-01-04 22:15:48 +00001299 __get_user(fpsr, &aux->fpsimd.fpsr);
1300 vfp_set_fpsr(env, fpsr);
1301 __get_user(fpcr, &aux->fpsimd.fpcr);
1302 vfp_set_fpcr(env, fpcr);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001303
1304 return 0;
1305}
1306
1307static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1308{
1309 abi_ulong sp;
1310
1311 sp = env->xregs[31];
1312
1313 /*
1314 * This is the X/Open sanctioned signal stack switching.
1315 */
1316 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
1317 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1318 }
1319
1320 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1321
1322 return sp;
1323}
1324
1325static void target_setup_frame(int usig, struct target_sigaction *ka,
1326 target_siginfo_t *info, target_sigset_t *set,
1327 CPUARMState *env)
1328{
1329 struct target_rt_sigframe *frame;
Michael Matz8a3ae912014-03-02 19:36:39 +00001330 abi_ulong frame_addr, return_addr;
Andreas Schwab1744aea2013-09-03 20:12:16 +01001331
1332 frame_addr = get_sigframe(ka, env);
1333 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1334 goto give_sigsegv;
1335 }
1336
1337 __put_user(0, &frame->uc.tuc_flags);
1338 __put_user(0, &frame->uc.tuc_link);
1339
1340 __put_user(target_sigaltstack_used.ss_sp,
1341 &frame->uc.tuc_stack.ss_sp);
1342 __put_user(sas_ss_flags(env->xregs[31]),
1343 &frame->uc.tuc_stack.ss_flags);
1344 __put_user(target_sigaltstack_used.ss_size,
1345 &frame->uc.tuc_stack.ss_size);
1346 target_setup_sigframe(frame, env, set);
Michael Matz8a3ae912014-03-02 19:36:39 +00001347 if (ka->sa_flags & TARGET_SA_RESTORER) {
1348 return_addr = ka->sa_restorer;
1349 } else {
1350 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1351 __put_user(0xd2801168, &frame->tramp[0]);
1352 __put_user(0xd4000001, &frame->tramp[1]);
1353 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1354 }
Andreas Schwab1744aea2013-09-03 20:12:16 +01001355 env->xregs[0] = usig;
1356 env->xregs[31] = frame_addr;
1357 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1358 env->pc = ka->_sa_handler;
Michael Matz8a3ae912014-03-02 19:36:39 +00001359 env->xregs[30] = return_addr;
Andreas Schwab1744aea2013-09-03 20:12:16 +01001360 if (info) {
Riku Voipiob0fd8d12014-04-23 10:46:13 +03001361 copy_siginfo_to_user(&frame->info, info);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001362 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1363 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1364 }
1365
1366 unlock_user_struct(frame, frame_addr, 1);
1367 return;
1368
1369 give_sigsegv:
1370 unlock_user_struct(frame, frame_addr, 1);
1371 force_sig(TARGET_SIGSEGV);
1372}
1373
1374static void setup_rt_frame(int sig, struct target_sigaction *ka,
1375 target_siginfo_t *info, target_sigset_t *set,
1376 CPUARMState *env)
1377{
1378 target_setup_frame(sig, ka, info, set, env);
1379}
1380
1381static void setup_frame(int sig, struct target_sigaction *ka,
1382 target_sigset_t *set, CPUARMState *env)
1383{
1384 target_setup_frame(sig, ka, 0, set, env);
1385}
1386
1387long do_rt_sigreturn(CPUARMState *env)
1388{
Peter Maydell7f72cd22014-03-12 13:06:00 +00001389 struct target_rt_sigframe *frame = NULL;
Andreas Schwab1744aea2013-09-03 20:12:16 +01001390 abi_ulong frame_addr = env->xregs[31];
1391
1392 if (frame_addr & 15) {
1393 goto badframe;
1394 }
1395
1396 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1397 goto badframe;
1398 }
1399
1400 if (target_restore_sigframe(env, frame)) {
1401 goto badframe;
1402 }
1403
1404 if (do_sigaltstack(frame_addr +
1405 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1406 0, get_sp_from_cpustate(env)) == -EFAULT) {
1407 goto badframe;
1408 }
1409
1410 unlock_user_struct(frame, frame_addr, 0);
1411 return env->xregs[0];
1412
1413 badframe:
1414 unlock_user_struct(frame, frame_addr, 0);
1415 force_sig(TARGET_SIGSEGV);
1416 return 0;
1417}
1418
1419long do_sigreturn(CPUARMState *env)
1420{
1421 return do_rt_sigreturn(env);
1422}
1423
bellard43fff232003-07-09 19:31:39 +00001424#elif defined(TARGET_ARM)
1425
1426struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001427 abi_ulong trap_no;
1428 abi_ulong error_code;
1429 abi_ulong oldmask;
1430 abi_ulong arm_r0;
1431 abi_ulong arm_r1;
1432 abi_ulong arm_r2;
1433 abi_ulong arm_r3;
1434 abi_ulong arm_r4;
1435 abi_ulong arm_r5;
1436 abi_ulong arm_r6;
1437 abi_ulong arm_r7;
1438 abi_ulong arm_r8;
1439 abi_ulong arm_r9;
1440 abi_ulong arm_r10;
1441 abi_ulong arm_fp;
1442 abi_ulong arm_ip;
1443 abi_ulong arm_sp;
1444 abi_ulong arm_lr;
1445 abi_ulong arm_pc;
1446 abi_ulong arm_cpsr;
1447 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001448};
1449
pbrooka745ec62008-05-06 15:36:17 +00001450struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001451 abi_ulong tuc_flags;
1452 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001453 target_stack_t tuc_stack;
bellardb8076a72005-04-07 22:20:31 +00001454 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001455 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001456};
1457
pbrooka745ec62008-05-06 15:36:17 +00001458struct target_ucontext_v2 {
1459 abi_ulong tuc_flags;
1460 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001461 target_stack_t tuc_stack;
pbrooka745ec62008-05-06 15:36:17 +00001462 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001463 target_sigset_t tuc_sigmask; /* mask last for extensibility */
Peter Maydell5f0b7c82010-11-24 15:20:03 +00001464 char __unused[128 - sizeof(target_sigset_t)];
pbrooka745ec62008-05-06 15:36:17 +00001465 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1466};
1467
Peter Maydell0d871bd2010-11-24 15:20:05 +00001468struct target_user_vfp {
1469 uint64_t fpregs[32];
1470 abi_ulong fpscr;
1471};
1472
1473struct target_user_vfp_exc {
1474 abi_ulong fpexc;
1475 abi_ulong fpinst;
1476 abi_ulong fpinst2;
1477};
1478
1479struct target_vfp_sigframe {
1480 abi_ulong magic;
1481 abi_ulong size;
1482 struct target_user_vfp ufp;
1483 struct target_user_vfp_exc ufp_exc;
1484} __attribute__((__aligned__(8)));
1485
Peter Maydell08e11252010-11-24 15:20:07 +00001486struct target_iwmmxt_sigframe {
1487 abi_ulong magic;
1488 abi_ulong size;
1489 uint64_t regs[16];
1490 /* Note that not all the coprocessor control registers are stored here */
1491 uint32_t wcssf;
1492 uint32_t wcasf;
1493 uint32_t wcgr0;
1494 uint32_t wcgr1;
1495 uint32_t wcgr2;
1496 uint32_t wcgr3;
1497} __attribute__((__aligned__(8)));
1498
Peter Maydell0d871bd2010-11-24 15:20:05 +00001499#define TARGET_VFP_MAGIC 0x56465001
Peter Maydell08e11252010-11-24 15:20:07 +00001500#define TARGET_IWMMXT_MAGIC 0x12ef842a
Peter Maydell0d871bd2010-11-24 15:20:05 +00001501
pbrooka8c33202008-05-07 23:22:46 +00001502struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001503{
1504 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001505 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1506 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001507};
1508
pbrooka8c33202008-05-07 23:22:46 +00001509struct sigframe_v2
1510{
1511 struct target_ucontext_v2 uc;
1512 abi_ulong retcode;
1513};
1514
pbrooka745ec62008-05-06 15:36:17 +00001515struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001516{
bellardf8b0aa22007-11-11 23:03:42 +00001517 abi_ulong pinfo;
1518 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001519 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001520 struct target_ucontext_v1 uc;
1521 abi_ulong retcode;
1522};
1523
1524struct rt_sigframe_v2
1525{
1526 struct target_siginfo info;
1527 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001528 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001529};
1530
1531#define TARGET_CONFIG_CPU_32 1
1532
1533/*
1534 * For ARM syscalls, we encode the syscall number into the instruction.
1535 */
1536#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1537#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1538
1539/*
1540 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1541 * need two 16-bit instructions.
1542 */
1543#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1544#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1545
blueswir1992f48a2007-10-14 16:27:31 +00001546static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001547 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1548 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1549};
1550
1551
Andreas Färber05390242012-02-25 03:37:53 +01001552static inline int valid_user_regs(CPUARMState *regs)
bellard43fff232003-07-09 19:31:39 +00001553{
1554 return 1;
1555}
1556
pbrooka8c33202008-05-07 23:22:46 +00001557static void
bellard43fff232003-07-09 19:31:39 +00001558setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
Andreas Färber05390242012-02-25 03:37:53 +01001559 CPUARMState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001560{
pbrooka8c33202008-05-07 23:22:46 +00001561 __put_user(env->regs[0], &sc->arm_r0);
1562 __put_user(env->regs[1], &sc->arm_r1);
1563 __put_user(env->regs[2], &sc->arm_r2);
1564 __put_user(env->regs[3], &sc->arm_r3);
1565 __put_user(env->regs[4], &sc->arm_r4);
1566 __put_user(env->regs[5], &sc->arm_r5);
1567 __put_user(env->regs[6], &sc->arm_r6);
1568 __put_user(env->regs[7], &sc->arm_r7);
1569 __put_user(env->regs[8], &sc->arm_r8);
1570 __put_user(env->regs[9], &sc->arm_r9);
1571 __put_user(env->regs[10], &sc->arm_r10);
1572 __put_user(env->regs[11], &sc->arm_fp);
1573 __put_user(env->regs[12], &sc->arm_ip);
1574 __put_user(env->regs[13], &sc->arm_sp);
1575 __put_user(env->regs[14], &sc->arm_lr);
1576 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001577#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001578 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001579#endif
1580
pbrooka8c33202008-05-07 23:22:46 +00001581 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1582 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1583 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1584 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001585}
1586
bellard579a97f2007-11-11 14:26:47 +00001587static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01001588get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
bellard43fff232003-07-09 19:31:39 +00001589{
1590 unsigned long sp = regs->regs[13];
1591
bellard43fff232003-07-09 19:31:39 +00001592 /*
1593 * This is the X/Open sanctioned signal stack switching.
1594 */
pbrook624f7972008-05-31 16:11:38 +00001595 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
thsa04e1342007-09-27 13:57:58 +00001596 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001597 /*
1598 * ATPCS B01 mandates 8-byte alignment
1599 */
bellard579a97f2007-11-11 14:26:47 +00001600 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001601}
1602
1603static int
Andreas Färber05390242012-02-25 03:37:53 +01001604setup_return(CPUARMState *env, struct target_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001605 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001606{
pbrook624f7972008-05-31 16:11:38 +00001607 abi_ulong handler = ka->_sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001608 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001609 int thumb = handler & 1;
Peter Maydell964413d2011-01-14 20:39:19 +01001610 uint32_t cpsr = cpsr_read(env);
1611
1612 cpsr &= ~CPSR_IT;
1613 if (thumb) {
1614 cpsr |= CPSR_T;
1615 } else {
1616 cpsr &= ~CPSR_T;
1617 }
bellard43fff232003-07-09 19:31:39 +00001618
pbrook624f7972008-05-31 16:11:38 +00001619 if (ka->sa_flags & TARGET_SA_RESTORER) {
1620 retcode = ka->sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001621 } else {
1622 unsigned int idx = thumb;
1623
pbrook624f7972008-05-31 16:11:38 +00001624 if (ka->sa_flags & TARGET_SA_SIGINFO)
bellard43fff232003-07-09 19:31:39 +00001625 idx += 2;
1626
1627 if (__put_user(retcodes[idx], rc))
1628 return 1;
Stefan Weilca8a2772011-10-03 22:43:19 +02001629
bellardf8b0aa22007-11-11 23:03:42 +00001630 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001631 }
1632
1633 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001634 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001635 env->regs[14] = retcode;
1636 env->regs[15] = handler & (thumb ? ~1 : ~3);
Peter Maydell964413d2011-01-14 20:39:19 +01001637 cpsr_write(env, cpsr, 0xffffffff);
bellard43fff232003-07-09 19:31:39 +00001638
1639 return 0;
1640}
1641
Andreas Färber05390242012-02-25 03:37:53 +01001642static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
Peter Maydell0d871bd2010-11-24 15:20:05 +00001643{
1644 int i;
1645 struct target_vfp_sigframe *vfpframe;
1646 vfpframe = (struct target_vfp_sigframe *)regspace;
1647 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1648 __put_user(sizeof(*vfpframe), &vfpframe->size);
1649 for (i = 0; i < 32; i++) {
Peter Maydell005e1a02011-02-10 13:59:35 +00001650 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
Peter Maydell0d871bd2010-11-24 15:20:05 +00001651 }
1652 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1653 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1654 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1655 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1656 return (abi_ulong*)(vfpframe+1);
1657}
1658
Andreas Färber05390242012-02-25 03:37:53 +01001659static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1660 CPUARMState *env)
Peter Maydell08e11252010-11-24 15:20:07 +00001661{
1662 int i;
1663 struct target_iwmmxt_sigframe *iwmmxtframe;
1664 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1665 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1666 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1667 for (i = 0; i < 16; i++) {
1668 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1669 }
1670 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1671 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1672 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1673 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1674 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1675 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1676 return (abi_ulong*)(iwmmxtframe+1);
1677}
1678
pbrooka8c33202008-05-07 23:22:46 +00001679static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
Andreas Färber05390242012-02-25 03:37:53 +01001680 target_sigset_t *set, CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001681{
pbrooka8c33202008-05-07 23:22:46 +00001682 struct target_sigaltstack stack;
1683 int i;
Peter Maydell0d871bd2010-11-24 15:20:05 +00001684 abi_ulong *regspace;
pbrooka8c33202008-05-07 23:22:46 +00001685
1686 /* Clear all the bits of the ucontext we don't use. */
1687 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1688
1689 memset(&stack, 0, sizeof(stack));
1690 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1691 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1692 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1693 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1694
1695 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
Peter Maydell0d871bd2010-11-24 15:20:05 +00001696 /* Save coprocessor signal frame. */
1697 regspace = uc->tuc_regspace;
1698 if (arm_feature(env, ARM_FEATURE_VFP)) {
1699 regspace = setup_sigframe_v2_vfp(regspace, env);
1700 }
Peter Maydell08e11252010-11-24 15:20:07 +00001701 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1702 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1703 }
1704
Peter Maydell0d871bd2010-11-24 15:20:05 +00001705 /* Write terminating magic word */
1706 __put_user(0, regspace);
1707
pbrooka8c33202008-05-07 23:22:46 +00001708 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1709 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1710 }
1711}
1712
1713/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00001714static void setup_frame_v1(int usig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001715 target_sigset_t *set, CPUARMState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001716{
1717 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001718 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001719 int i;
bellard43fff232003-07-09 19:31:39 +00001720
bellard579a97f2007-11-11 14:26:47 +00001721 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1722 return;
1723
pbrooka8c33202008-05-07 23:22:46 +00001724 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001725
bellard92319442004-06-19 16:58:13 +00001726 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1727 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001728 goto end;
bellard43fff232003-07-09 19:31:39 +00001729 }
1730
pbrooka8c33202008-05-07 23:22:46 +00001731 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1732 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001733
1734end:
1735 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001736}
1737
pbrook624f7972008-05-31 16:11:38 +00001738static void setup_frame_v2(int usig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001739 target_sigset_t *set, CPUARMState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001740{
1741 struct sigframe_v2 *frame;
1742 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1743
1744 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1745 return;
1746
1747 setup_sigframe_v2(&frame->uc, set, regs);
1748
1749 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1750 frame_addr + offsetof(struct sigframe_v2, retcode));
1751
1752 unlock_user_struct(frame, frame_addr, 1);
1753}
1754
pbrook624f7972008-05-31 16:11:38 +00001755static void setup_frame(int usig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001756 target_sigset_t *set, CPUARMState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001757{
1758 if (get_osversion() >= 0x020612) {
1759 setup_frame_v2(usig, ka, set, regs);
1760 } else {
1761 setup_frame_v1(usig, ka, set, regs);
1762 }
bellard43fff232003-07-09 19:31:39 +00001763}
1764
bellard579a97f2007-11-11 14:26:47 +00001765/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +00001766static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001767 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01001768 target_sigset_t *set, CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001769{
pbrooka745ec62008-05-06 15:36:17 +00001770 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001771 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001772 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001773 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001774 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001775
bellard579a97f2007-11-11 14:26:47 +00001776 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001777 return /* 1 */;
1778
pbrooka745ec62008-05-06 15:36:17 +00001779 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001780 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001781 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001782 __put_user(uc_addr, &frame->puc);
1783 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001784
1785 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001786 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001787
thsa04e1342007-09-27 13:57:58 +00001788 memset(&stack, 0, sizeof(stack));
1789 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1790 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1791 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001792 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001793
pbrooka8c33202008-05-07 23:22:46 +00001794 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001795 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001796 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001797 goto end;
bellard92319442004-06-19 16:58:13 +00001798 }
bellard43fff232003-07-09 19:31:39 +00001799
pbrooka8c33202008-05-07 23:22:46 +00001800 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1801 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001802
pbrooka8c33202008-05-07 23:22:46 +00001803 env->regs[1] = info_addr;
1804 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001805
1806end:
1807 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001808}
1809
pbrook624f7972008-05-31 16:11:38 +00001810static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001811 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01001812 target_sigset_t *set, CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00001813{
1814 struct rt_sigframe_v2 *frame;
1815 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001816 abi_ulong info_addr, uc_addr;
1817
1818 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1819 return /* 1 */;
1820
1821 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1822 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001823 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001824
pbrooka8c33202008-05-07 23:22:46 +00001825 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001826
pbrooka8c33202008-05-07 23:22:46 +00001827 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1828 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001829
pbrooka8c33202008-05-07 23:22:46 +00001830 env->regs[1] = info_addr;
1831 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001832
bellard579a97f2007-11-11 14:26:47 +00001833 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001834}
1835
pbrook624f7972008-05-31 16:11:38 +00001836static void setup_rt_frame(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001837 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01001838 target_sigset_t *set, CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00001839{
1840 if (get_osversion() >= 0x020612) {
1841 setup_rt_frame_v2(usig, ka, info, set, env);
1842 } else {
1843 setup_rt_frame_v1(usig, ka, info, set, env);
1844 }
1845}
1846
bellard43fff232003-07-09 19:31:39 +00001847static int
Andreas Färber05390242012-02-25 03:37:53 +01001848restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
bellard43fff232003-07-09 19:31:39 +00001849{
1850 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001851 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001852
Riku Voipio1d8b5122014-04-23 10:26:05 +03001853 __get_user(env->regs[0], &sc->arm_r0);
1854 __get_user(env->regs[1], &sc->arm_r1);
1855 __get_user(env->regs[2], &sc->arm_r2);
1856 __get_user(env->regs[3], &sc->arm_r3);
1857 __get_user(env->regs[4], &sc->arm_r4);
1858 __get_user(env->regs[5], &sc->arm_r5);
1859 __get_user(env->regs[6], &sc->arm_r6);
1860 __get_user(env->regs[7], &sc->arm_r7);
1861 __get_user(env->regs[8], &sc->arm_r8);
1862 __get_user(env->regs[9], &sc->arm_r9);
1863 __get_user(env->regs[10], &sc->arm_r10);
1864 __get_user(env->regs[11], &sc->arm_fp);
1865 __get_user(env->regs[12], &sc->arm_ip);
1866 __get_user(env->regs[13], &sc->arm_sp);
1867 __get_user(env->regs[14], &sc->arm_lr);
1868 __get_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001869#ifdef TARGET_CONFIG_CPU_32
Riku Voipio1d8b5122014-04-23 10:26:05 +03001870 __get_user(cpsr, &sc->arm_cpsr);
pbrook75b680e2008-03-21 16:07:30 +00001871 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001872#endif
1873
1874 err |= !valid_user_regs(env);
1875
1876 return err;
1877}
1878
Andreas Färber05390242012-02-25 03:37:53 +01001879static long do_sigreturn_v1(CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001880{
bellardf8b0aa22007-11-11 23:03:42 +00001881 abi_ulong frame_addr;
Peter Maydell978fae92013-07-29 12:00:32 +01001882 struct sigframe_v1 *frame = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -05001883 target_sigset_t set;
bellard43fff232003-07-09 19:31:39 +00001884 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001885 int i;
bellard43fff232003-07-09 19:31:39 +00001886
1887 /*
1888 * Since we stacked the signal on a 64-bit boundary,
1889 * then 'sp' should be word aligned here. If it's
1890 * not, then the user is trying to mess with us.
1891 */
bellardf8b0aa22007-11-11 23:03:42 +00001892 frame_addr = env->regs[13];
Peter Maydell978fae92013-07-29 12:00:32 +01001893 if (frame_addr & 7) {
1894 goto badframe;
1895 }
1896
bellardf8b0aa22007-11-11 23:03:42 +00001897 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1898 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001899
bellard92319442004-06-19 16:58:13 +00001900 if (__get_user(set.sig[0], &frame->sc.oldmask))
1901 goto badframe;
1902 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1903 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1904 goto badframe;
1905 }
bellard43fff232003-07-09 19:31:39 +00001906
bellard92319442004-06-19 16:58:13 +00001907 target_to_host_sigset_internal(&host_set, &set);
Alex Barcelo1c275922014-03-14 14:36:55 +00001908 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard43fff232003-07-09 19:31:39 +00001909
1910 if (restore_sigcontext(env, &frame->sc))
1911 goto badframe;
1912
1913#if 0
1914 /* Send SIGTRAP if we're single-stepping */
1915 if (ptrace_cancel_bpt(current))
1916 send_sig(SIGTRAP, current, 1);
1917#endif
bellardf8b0aa22007-11-11 23:03:42 +00001918 unlock_user_struct(frame, frame_addr, 0);
1919 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001920
1921badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001922 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001923 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00001924 return 0;
1925}
1926
Andreas Färber05390242012-02-25 03:37:53 +01001927static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
Peter Maydell5f9099d2010-11-24 15:20:06 +00001928{
1929 int i;
1930 abi_ulong magic, sz;
1931 uint32_t fpscr, fpexc;
1932 struct target_vfp_sigframe *vfpframe;
1933 vfpframe = (struct target_vfp_sigframe *)regspace;
1934
1935 __get_user(magic, &vfpframe->magic);
1936 __get_user(sz, &vfpframe->size);
1937 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1938 return 0;
1939 }
1940 for (i = 0; i < 32; i++) {
Peter Maydell005e1a02011-02-10 13:59:35 +00001941 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
Peter Maydell5f9099d2010-11-24 15:20:06 +00001942 }
1943 __get_user(fpscr, &vfpframe->ufp.fpscr);
1944 vfp_set_fpscr(env, fpscr);
1945 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1946 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1947 * and the exception flag is cleared
1948 */
1949 fpexc |= (1 << 30);
1950 fpexc &= ~((1 << 31) | (1 << 28));
1951 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1952 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1953 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1954 return (abi_ulong*)(vfpframe + 1);
1955}
1956
Andreas Färber05390242012-02-25 03:37:53 +01001957static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1958 abi_ulong *regspace)
Peter Maydella59d69d2010-11-24 15:20:08 +00001959{
1960 int i;
1961 abi_ulong magic, sz;
1962 struct target_iwmmxt_sigframe *iwmmxtframe;
1963 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1964
1965 __get_user(magic, &iwmmxtframe->magic);
1966 __get_user(sz, &iwmmxtframe->size);
1967 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1968 return 0;
1969 }
1970 for (i = 0; i < 16; i++) {
1971 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1972 }
1973 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1974 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1975 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1976 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1977 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1978 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1979 return (abi_ulong*)(iwmmxtframe + 1);
1980}
1981
Andreas Färber05390242012-02-25 03:37:53 +01001982static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
pbrooka8c33202008-05-07 23:22:46 +00001983 struct target_ucontext_v2 *uc)
1984{
1985 sigset_t host_set;
Peter Maydell5f9099d2010-11-24 15:20:06 +00001986 abi_ulong *regspace;
pbrooka8c33202008-05-07 23:22:46 +00001987
1988 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00001989 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
pbrooka8c33202008-05-07 23:22:46 +00001990
1991 if (restore_sigcontext(env, &uc->tuc_mcontext))
1992 return 1;
1993
Peter Maydell5f9099d2010-11-24 15:20:06 +00001994 /* Restore coprocessor signal frame */
1995 regspace = uc->tuc_regspace;
1996 if (arm_feature(env, ARM_FEATURE_VFP)) {
1997 regspace = restore_sigframe_v2_vfp(env, regspace);
1998 if (!regspace) {
1999 return 1;
2000 }
2001 }
Peter Maydella59d69d2010-11-24 15:20:08 +00002002 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2003 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
2004 if (!regspace) {
2005 return 1;
2006 }
2007 }
Peter Maydell5f9099d2010-11-24 15:20:06 +00002008
pbrooka8c33202008-05-07 23:22:46 +00002009 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2010 return 1;
2011
2012#if 0
2013 /* Send SIGTRAP if we're single-stepping */
2014 if (ptrace_cancel_bpt(current))
2015 send_sig(SIGTRAP, current, 1);
2016#endif
2017
2018 return 0;
2019}
2020
Andreas Färber05390242012-02-25 03:37:53 +01002021static long do_sigreturn_v2(CPUARMState *env)
pbrooka8c33202008-05-07 23:22:46 +00002022{
2023 abi_ulong frame_addr;
Peter Maydell978fae92013-07-29 12:00:32 +01002024 struct sigframe_v2 *frame = NULL;
pbrooka8c33202008-05-07 23:22:46 +00002025
2026 /*
2027 * Since we stacked the signal on a 64-bit boundary,
2028 * then 'sp' should be word aligned here. If it's
2029 * not, then the user is trying to mess with us.
2030 */
pbrooka8c33202008-05-07 23:22:46 +00002031 frame_addr = env->regs[13];
Peter Maydell978fae92013-07-29 12:00:32 +01002032 if (frame_addr & 7) {
2033 goto badframe;
2034 }
2035
pbrooka8c33202008-05-07 23:22:46 +00002036 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2037 goto badframe;
2038
2039 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2040 goto badframe;
2041
2042 unlock_user_struct(frame, frame_addr, 0);
2043 return env->regs[0];
2044
2045badframe:
2046 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002047 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka8c33202008-05-07 23:22:46 +00002048 return 0;
2049}
2050
Andreas Färber05390242012-02-25 03:37:53 +01002051long do_sigreturn(CPUARMState *env)
pbrooka8c33202008-05-07 23:22:46 +00002052{
2053 if (get_osversion() >= 0x020612) {
2054 return do_sigreturn_v2(env);
2055 } else {
2056 return do_sigreturn_v1(env);
2057 }
2058}
2059
Andreas Färber05390242012-02-25 03:37:53 +01002060static long do_rt_sigreturn_v1(CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00002061{
bellardf8b0aa22007-11-11 23:03:42 +00002062 abi_ulong frame_addr;
Peter Maydell978fae92013-07-29 12:00:32 +01002063 struct rt_sigframe_v1 *frame = NULL;
bellard43fff232003-07-09 19:31:39 +00002064 sigset_t host_set;
2065
2066 /*
2067 * Since we stacked the signal on a 64-bit boundary,
2068 * then 'sp' should be word aligned here. If it's
2069 * not, then the user is trying to mess with us.
2070 */
bellardf8b0aa22007-11-11 23:03:42 +00002071 frame_addr = env->regs[13];
Peter Maydell978fae92013-07-29 12:00:32 +01002072 if (frame_addr & 7) {
2073 goto badframe;
2074 }
2075
bellardf8b0aa22007-11-11 23:03:42 +00002076 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2077 goto badframe;
bellard43fff232003-07-09 19:31:39 +00002078
bellardb8076a72005-04-07 22:20:31 +00002079 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00002080 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard43fff232003-07-09 19:31:39 +00002081
bellardb8076a72005-04-07 22:20:31 +00002082 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00002083 goto badframe;
2084
pbrooka745ec62008-05-06 15:36:17 +00002085 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 +00002086 goto badframe;
2087
bellard43fff232003-07-09 19:31:39 +00002088#if 0
2089 /* Send SIGTRAP if we're single-stepping */
2090 if (ptrace_cancel_bpt(current))
2091 send_sig(SIGTRAP, current, 1);
2092#endif
bellardf8b0aa22007-11-11 23:03:42 +00002093 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00002094 return env->regs[0];
2095
2096badframe:
bellardf8b0aa22007-11-11 23:03:42 +00002097 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002098 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00002099 return 0;
2100}
2101
Andreas Färber05390242012-02-25 03:37:53 +01002102static long do_rt_sigreturn_v2(CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00002103{
2104 abi_ulong frame_addr;
Peter Maydell978fae92013-07-29 12:00:32 +01002105 struct rt_sigframe_v2 *frame = NULL;
pbrooka745ec62008-05-06 15:36:17 +00002106
2107 /*
2108 * Since we stacked the signal on a 64-bit boundary,
2109 * then 'sp' should be word aligned here. If it's
2110 * not, then the user is trying to mess with us.
2111 */
pbrooka745ec62008-05-06 15:36:17 +00002112 frame_addr = env->regs[13];
Peter Maydell978fae92013-07-29 12:00:32 +01002113 if (frame_addr & 7) {
2114 goto badframe;
2115 }
2116
pbrooka745ec62008-05-06 15:36:17 +00002117 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2118 goto badframe;
2119
pbrooka8c33202008-05-07 23:22:46 +00002120 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2121 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00002122
pbrooka745ec62008-05-06 15:36:17 +00002123 unlock_user_struct(frame, frame_addr, 0);
2124 return env->regs[0];
2125
2126badframe:
2127 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002128 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka745ec62008-05-06 15:36:17 +00002129 return 0;
2130}
2131
Andreas Färber05390242012-02-25 03:37:53 +01002132long do_rt_sigreturn(CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00002133{
2134 if (get_osversion() >= 0x020612) {
2135 return do_rt_sigreturn_v2(env);
2136 } else {
2137 return do_rt_sigreturn_v1(env);
2138 }
2139}
2140
bellard6d5e2162004-09-30 22:04:13 +00002141#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00002142
bellard6d5e2162004-09-30 22:04:13 +00002143#define __SUNOS_MAXWIN 31
2144
2145/* This is what SunOS does, so shall I. */
2146struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00002147 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00002148
blueswir1992f48a2007-10-14 16:27:31 +00002149 abi_ulong sigc_mask; /* sigmask to restore */
2150 abi_ulong sigc_sp; /* stack pointer */
2151 abi_ulong sigc_pc; /* program counter */
2152 abi_ulong sigc_npc; /* next program counter */
2153 abi_ulong sigc_psr; /* for condition codes etc */
2154 abi_ulong sigc_g1; /* User uses these two registers */
2155 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00002156
2157 /* Now comes information regarding the users window set
2158 * at the time of the signal.
2159 */
blueswir1992f48a2007-10-14 16:27:31 +00002160 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00002161
2162 /* stack ptrs for each regwin buf */
2163 char *sigc_spbuf[__SUNOS_MAXWIN];
2164
2165 /* Windows to restore after signal */
2166 struct {
blueswir1992f48a2007-10-14 16:27:31 +00002167 abi_ulong locals[8];
2168 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00002169 } sigc_wbuf[__SUNOS_MAXWIN];
2170};
2171/* A Sparc stack frame */
2172struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00002173 abi_ulong locals[8];
Peter Maydelle321c342011-02-01 15:54:52 +00002174 abi_ulong ins[8];
2175 /* It's simpler to treat fp and callers_pc as elements of ins[]
2176 * since we never need to access them ourselves.
2177 */
bellard6d5e2162004-09-30 22:04:13 +00002178 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00002179 abi_ulong xargs[6];
2180 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00002181};
2182
2183typedef struct {
2184 struct {
blueswir1992f48a2007-10-14 16:27:31 +00002185 abi_ulong psr;
2186 abi_ulong pc;
2187 abi_ulong npc;
2188 abi_ulong y;
2189 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00002190 } si_regs;
2191 int si_mask;
2192} __siginfo_t;
2193
2194typedef struct {
Blue Swirl8954bae2012-07-30 15:29:11 +00002195 abi_ulong si_float_regs[32];
bellard6d5e2162004-09-30 22:04:13 +00002196 unsigned long si_fsr;
2197 unsigned long si_fpqdepth;
2198 struct {
2199 unsigned long *insn_addr;
2200 unsigned long insn;
2201 } si_fpqueue [16];
Anthony Liguoric227f092009-10-01 16:12:16 -05002202} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00002203
2204
2205struct target_signal_frame {
2206 struct sparc_stackf ss;
2207 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00002208 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00002209 abi_ulong insns[2] __attribute__ ((aligned (8)));
2210 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2211 abi_ulong extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002212 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00002213};
2214struct target_rt_signal_frame {
2215 struct sparc_stackf ss;
2216 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00002217 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00002218 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00002219 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00002220 unsigned int insns[2];
2221 stack_t stack;
2222 unsigned int extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002223 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00002224};
2225
bellarde80cfcf2004-12-19 23:18:01 +00002226#define UREG_O0 16
2227#define UREG_O6 22
2228#define UREG_I0 0
2229#define UREG_I1 1
2230#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00002231#define UREG_I3 3
2232#define UREG_I4 4
2233#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00002234#define UREG_I6 6
2235#define UREG_I7 7
2236#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00002237#define UREG_FP UREG_I6
2238#define UREG_SP UREG_O6
2239
pbrook624f7972008-05-31 16:11:38 +00002240static inline abi_ulong get_sigframe(struct target_sigaction *sa,
Andreas Färber05390242012-02-25 03:37:53 +01002241 CPUSPARCState *env,
2242 unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00002243{
bellard459a4012007-11-11 19:45:10 +00002244 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00002245
2246 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00002247
2248 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002249 if (sa->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +00002250 if (!on_sig_stack(sp)
2251 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
2252 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00002253 }
bellard459a4012007-11-11 19:45:10 +00002254 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00002255}
2256
2257static int
Andreas Färber05390242012-02-25 03:37:53 +01002258setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00002259{
2260 int err = 0, i;
2261
Riku Voipio1d8b5122014-04-23 10:26:05 +03002262 __put_user(env->psr, &si->si_regs.psr);
2263 __put_user(env->pc, &si->si_regs.pc);
2264 __put_user(env->npc, &si->si_regs.npc);
2265 __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00002266 for (i=0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002267 __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
bellard6d5e2162004-09-30 22:04:13 +00002268 }
bellarda315a142005-01-30 22:59:18 +00002269 for (i=0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002270 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00002271 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03002272 __put_user(mask, &si->si_mask);
bellard6d5e2162004-09-30 22:04:13 +00002273 return err;
2274}
bellarde80cfcf2004-12-19 23:18:01 +00002275
bellard80a9d032005-01-03 23:31:27 +00002276#if 0
bellard6d5e2162004-09-30 22:04:13 +00002277static int
2278setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
Andreas Färber05390242012-02-25 03:37:53 +01002279 CPUSPARCState *env, unsigned long mask)
bellard6d5e2162004-09-30 22:04:13 +00002280{
2281 int err = 0;
2282
Riku Voipio1d8b5122014-04-23 10:26:05 +03002283 __put_user(mask, &sc->sigc_mask);
2284 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2285 __put_user(env->pc, &sc->sigc_pc);
2286 __put_user(env->npc, &sc->sigc_npc);
2287 __put_user(env->psr, &sc->sigc_psr);
2288 __put_user(env->gregs[1], &sc->sigc_g1);
2289 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
bellard6d5e2162004-09-30 22:04:13 +00002290
2291 return err;
2292}
bellard80a9d032005-01-03 23:31:27 +00002293#endif
bellard6d5e2162004-09-30 22:04:13 +00002294#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2295
pbrook624f7972008-05-31 16:11:38 +00002296static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01002297 target_sigset_t *set, CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002298{
bellard459a4012007-11-11 19:45:10 +00002299 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00002300 struct target_signal_frame *sf;
2301 int sigframe_size, err, i;
2302
2303 /* 1. Make sure everything is clean */
2304 //synchronize_user_stack();
2305
2306 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00002307 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00002308
bellard459a4012007-11-11 19:45:10 +00002309 sf = lock_user(VERIFY_WRITE, sf_addr,
2310 sizeof(struct target_signal_frame), 0);
2311 if (!sf)
2312 goto sigsegv;
2313
bellarde80cfcf2004-12-19 23:18:01 +00002314 //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 +00002315#if 0
2316 if (invalid_frame_pointer(sf, sigframe_size))
2317 goto sigill_and_return;
2318#endif
2319 /* 2. Save the current process state */
2320 err = setup___siginfo(&sf->info, env, set->sig[0]);
Riku Voipio1d8b5122014-04-23 10:26:05 +03002321 __put_user(0, &sf->extra_size);
bellard6d5e2162004-09-30 22:04:13 +00002322
Riku Voipio1d8b5122014-04-23 10:26:05 +03002323 //save_fpu_state(regs, &sf->fpu_state);
2324 //__put_user(&sf->fpu_state, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00002325
Riku Voipio1d8b5122014-04-23 10:26:05 +03002326 __put_user(set->sig[0], &sf->info.si_mask);
bellard6d5e2162004-09-30 22:04:13 +00002327 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002328 __put_user(set->sig[i + 1], &sf->extramask[i]);
bellard6d5e2162004-09-30 22:04:13 +00002329 }
2330
bellarda315a142005-01-30 22:59:18 +00002331 for (i = 0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002332 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00002333 }
bellarda315a142005-01-30 22:59:18 +00002334 for (i = 0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002335 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00002336 }
bellard6d5e2162004-09-30 22:04:13 +00002337 if (err)
2338 goto sigsegv;
2339
2340 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00002341 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00002342 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00002343 env->regwptr[UREG_I1] = sf_addr +
2344 offsetof(struct target_signal_frame, info);
2345 env->regwptr[UREG_I2] = sf_addr +
2346 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00002347
2348 /* 4. signal handler */
pbrook624f7972008-05-31 16:11:38 +00002349 env->pc = ka->_sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00002350 env->npc = (env->pc + 4);
2351 /* 5. return to kernel instructions */
pbrook624f7972008-05-31 16:11:38 +00002352 if (ka->sa_restorer)
2353 env->regwptr[UREG_I7] = ka->sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00002354 else {
bellard775b58d2007-11-11 16:22:17 +00002355 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00002356
2357 env->regwptr[UREG_I7] = sf_addr +
2358 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00002359
2360 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00002361 val32 = 0x821020d8;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002362 __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00002363
2364 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00002365 val32 = 0x91d02010;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002366 __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00002367 if (err)
2368 goto sigsegv;
2369
2370 /* Flush instruction space. */
2371 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00002372 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00002373 }
bellard459a4012007-11-11 19:45:10 +00002374 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00002375 return;
bellard459a4012007-11-11 19:45:10 +00002376#if 0
2377sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00002378 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00002379#endif
bellard6d5e2162004-09-30 22:04:13 +00002380sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00002381 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00002382 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00002383 force_sig(TARGET_SIGSEGV);
2384}
2385static inline int
Andreas Färber05390242012-02-25 03:37:53 +01002386restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00002387{
2388 int err;
2389#if 0
2390#ifdef CONFIG_SMP
2391 if (current->flags & PF_USEDFPU)
2392 regs->psr &= ~PSR_EF;
2393#else
2394 if (current == last_task_used_math) {
2395 last_task_used_math = 0;
2396 regs->psr &= ~PSR_EF;
2397 }
2398#endif
2399 current->used_math = 1;
2400 current->flags &= ~PF_USEDFPU;
2401#endif
2402#if 0
2403 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2404 return -EFAULT;
2405#endif
2406
bellardfafffae2006-10-28 12:09:16 +00002407 /* XXX: incorrect */
Blue Swirl8954bae2012-07-30 15:29:11 +00002408 err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0],
2409 (sizeof(abi_ulong) * 32));
bellard6d5e2162004-09-30 22:04:13 +00002410 err |= __get_user(env->fsr, &fpu->si_fsr);
2411#if 0
2412 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2413 if (current->thread.fpqdepth != 0)
2414 err |= __copy_from_user(&current->thread.fpqueue[0],
2415 &fpu->si_fpqueue[0],
2416 ((sizeof(unsigned long) +
2417 (sizeof(unsigned long *)))*16));
2418#endif
2419 return err;
2420}
2421
2422
pbrook624f7972008-05-31 16:11:38 +00002423static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002424 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01002425 target_sigset_t *set, CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002426{
2427 fprintf(stderr, "setup_rt_frame: not implemented\n");
2428}
2429
Andreas Färber05390242012-02-25 03:37:53 +01002430long do_sigreturn(CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002431{
bellardf8b0aa22007-11-11 23:03:42 +00002432 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00002433 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00002434 uint32_t up_psr, pc, npc;
Anthony Liguoric227f092009-10-01 16:12:16 -05002435 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00002436 sigset_t host_set;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002437 int err=0, i;
bellard6d5e2162004-09-30 22:04:13 +00002438
bellardf8b0aa22007-11-11 23:03:42 +00002439 sf_addr = env->regwptr[UREG_FP];
2440 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2441 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00002442#if 0
bellarde80cfcf2004-12-19 23:18:01 +00002443 fprintf(stderr, "sigreturn\n");
2444 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 +00002445#endif
bellarde80cfcf2004-12-19 23:18:01 +00002446 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00002447
2448 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00002449
bellardf8b0aa22007-11-11 23:03:42 +00002450 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00002451 goto segv_and_exit;
2452
Riku Voipio1d8b5122014-04-23 10:26:05 +03002453 __get_user(pc, &sf->info.si_regs.pc);
2454 __get_user(npc, &sf->info.si_regs.npc);
bellard6d5e2162004-09-30 22:04:13 +00002455
bellard6d5e2162004-09-30 22:04:13 +00002456 if ((pc | npc) & 3)
2457 goto segv_and_exit;
2458
2459 /* 2. Restore the state */
Riku Voipio1d8b5122014-04-23 10:26:05 +03002460 __get_user(up_psr, &sf->info.si_regs.psr);
bellarde80cfcf2004-12-19 23:18:01 +00002461
bellard6d5e2162004-09-30 22:04:13 +00002462 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00002463 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2464 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2465
2466 env->pc = pc;
2467 env->npc = npc;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002468 __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00002469 for (i=0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002470 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
bellarde80cfcf2004-12-19 23:18:01 +00002471 }
bellarda315a142005-01-30 22:59:18 +00002472 for (i=0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002473 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
bellarde80cfcf2004-12-19 23:18:01 +00002474 }
bellard6d5e2162004-09-30 22:04:13 +00002475
Peter Maydell2aec3a22011-06-16 17:37:14 +01002476 /* FIXME: implement FPU save/restore:
2477 * __get_user(fpu_save, &sf->fpu_save);
2478 * if (fpu_save)
2479 * err |= restore_fpu_state(env, fpu_save);
2480 */
bellard6d5e2162004-09-30 22:04:13 +00002481
2482 /* This is pretty much atomic, no amount locking would prevent
2483 * the races which exist anyways.
2484 */
Riku Voipio1d8b5122014-04-23 10:26:05 +03002485 __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00002486 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002487 __get_user(set.sig[i], &sf->extramask[i - 1]);
bellarde80cfcf2004-12-19 23:18:01 +00002488 }
2489
2490 target_to_host_sigset_internal(&host_set, &set);
Alex Barcelo1c275922014-03-14 14:36:55 +00002491 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00002492
2493 if (err)
2494 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00002495 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00002496 return env->regwptr[0];
2497
2498segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00002499 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00002500 force_sig(TARGET_SIGSEGV);
2501}
2502
Andreas Färber05390242012-02-25 03:37:53 +01002503long do_rt_sigreturn(CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002504{
2505 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002506 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00002507}
2508
bellard459a4012007-11-11 19:45:10 +00002509#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00002510#define MC_TSTATE 0
2511#define MC_PC 1
2512#define MC_NPC 2
2513#define MC_Y 3
2514#define MC_G1 4
2515#define MC_G2 5
2516#define MC_G3 6
2517#define MC_G4 7
2518#define MC_G5 8
2519#define MC_G6 9
2520#define MC_G7 10
2521#define MC_O0 11
2522#define MC_O1 12
2523#define MC_O2 13
2524#define MC_O3 14
2525#define MC_O4 15
2526#define MC_O5 16
2527#define MC_O6 17
2528#define MC_O7 18
2529#define MC_NGREG 19
2530
Anthony Liguoric227f092009-10-01 16:12:16 -05002531typedef abi_ulong target_mc_greg_t;
2532typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
blueswir15bfb56b2007-10-05 17:01:51 +00002533
2534struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00002535 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002536 uint32_t mcfq_insn;
2537};
2538
2539struct target_mc_fpu {
2540 union {
2541 uint32_t sregs[32];
2542 uint64_t dregs[32];
2543 //uint128_t qregs[16];
2544 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00002545 abi_ulong mcfpu_fsr;
2546 abi_ulong mcfpu_fprs;
2547 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00002548 struct target_mc_fq *mcfpu_fq;
2549 unsigned char mcfpu_qcnt;
2550 unsigned char mcfpu_qentsz;
2551 unsigned char mcfpu_enab;
2552};
Anthony Liguoric227f092009-10-01 16:12:16 -05002553typedef struct target_mc_fpu target_mc_fpu_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002554
2555typedef struct {
Anthony Liguoric227f092009-10-01 16:12:16 -05002556 target_mc_gregset_t mc_gregs;
2557 target_mc_greg_t mc_fp;
2558 target_mc_greg_t mc_i7;
2559 target_mc_fpu_t mc_fpregs;
2560} target_mcontext_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002561
2562struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002563 struct target_ucontext *tuc_link;
2564 abi_ulong tuc_flags;
2565 target_sigset_t tuc_sigmask;
2566 target_mcontext_t tuc_mcontext;
blueswir15bfb56b2007-10-05 17:01:51 +00002567};
2568
2569/* A V9 register window */
2570struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00002571 abi_ulong locals[8];
2572 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00002573};
2574
2575#define TARGET_STACK_BIAS 2047
2576
2577/* {set, get}context() needed for 64-bit SparcLinux userland. */
2578void sparc64_set_context(CPUSPARCState *env)
2579{
bellard459a4012007-11-11 19:45:10 +00002580 abi_ulong ucp_addr;
2581 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002582 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00002583 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00002584 abi_ulong fp, i7, w_addr;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002585 int err = 0;
blueswir15bfb56b2007-10-05 17:01:51 +00002586 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002587
bellard459a4012007-11-11 19:45:10 +00002588 ucp_addr = env->regwptr[UREG_I0];
2589 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2590 goto do_sigsegv;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002591 grp = &ucp->tuc_mcontext.mc_gregs;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002592 __get_user(pc, &((*grp)[MC_PC]));
2593 __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002594 if (err || ((pc | npc) & 3))
2595 goto do_sigsegv;
2596 if (env->regwptr[UREG_I1]) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002597 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002598 sigset_t set;
2599
2600 if (TARGET_NSIG_WORDS == 1) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002601 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002602 goto do_sigsegv;
2603 } else {
bellard459a4012007-11-11 19:45:10 +00002604 abi_ulong *src, *dst;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002605 src = ucp->tuc_sigmask.sig;
bellard459a4012007-11-11 19:45:10 +00002606 dst = target_set.sig;
Stefan Weil0d9e61c2013-12-07 14:48:08 +01002607 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002608 __get_user(*dst, src);
Stefan Weil0d9e61c2013-12-07 14:48:08 +01002609 }
blueswir15bfb56b2007-10-05 17:01:51 +00002610 if (err)
2611 goto do_sigsegv;
2612 }
2613 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00002614 do_sigprocmask(SIG_SETMASK, &set, NULL);
blueswir15bfb56b2007-10-05 17:01:51 +00002615 }
2616 env->pc = pc;
2617 env->npc = npc;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002618 __get_user(env->y, &((*grp)[MC_Y]));
2619 __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002620 env->asi = (tstate >> 24) & 0xff;
Blue Swirl5a834bb2010-05-09 20:19:04 +00002621 cpu_put_ccr(env, tstate >> 32);
2622 cpu_put_cwp64(env, tstate & 0x1f);
Riku Voipio1d8b5122014-04-23 10:26:05 +03002623 __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2624 __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2625 __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2626 __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2627 __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2628 __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2629 __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2630 __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2631 __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2632 __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2633 __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2634 __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2635 __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2636 __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2637 __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002638
Riku Voipio1d8b5122014-04-23 10:26:05 +03002639 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2640 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002641
bellard459a4012007-11-11 19:45:10 +00002642 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2643 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2644 abi_ulong) != 0)
2645 goto do_sigsegv;
2646 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2647 abi_ulong) != 0)
2648 goto do_sigsegv;
Peter Maydellc7b016b2011-06-16 17:37:15 +01002649 /* FIXME this does not match how the kernel handles the FPU in
2650 * its sparc64_set_context implementation. In particular the FPU
2651 * is only restored if fenab is non-zero in:
2652 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2653 */
Aurelien Jarno60e99242010-03-29 02:12:51 +02002654 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002655 {
Richard Henderson30038fd2011-10-17 10:42:49 -07002656 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2657 for (i = 0; i < 64; i++, src++) {
2658 if (i & 1) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002659 __get_user(env->fpr[i/2].l.lower, src);
Richard Henderson30038fd2011-10-17 10:42:49 -07002660 } else {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002661 __get_user(env->fpr[i/2].l.upper, src);
Richard Henderson30038fd2011-10-17 10:42:49 -07002662 }
2663 }
bellard459a4012007-11-11 19:45:10 +00002664 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03002665 __get_user(env->fsr,
2666 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2667 __get_user(env->gsr,
2668 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002669 if (err)
2670 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002671 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002672 return;
2673 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002674 unlock_user_struct(ucp, ucp_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002675 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002676}
2677
2678void sparc64_get_context(CPUSPARCState *env)
2679{
bellard459a4012007-11-11 19:45:10 +00002680 abi_ulong ucp_addr;
2681 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002682 target_mc_gregset_t *grp;
2683 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002684 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002685 int err;
2686 unsigned int i;
Anthony Liguoric227f092009-10-01 16:12:16 -05002687 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002688 sigset_t set;
2689
bellard459a4012007-11-11 19:45:10 +00002690 ucp_addr = env->regwptr[UREG_I0];
2691 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2692 goto do_sigsegv;
2693
Aurelien Jarno60e99242010-03-29 02:12:51 +02002694 mcp = &ucp->tuc_mcontext;
blueswir15bfb56b2007-10-05 17:01:51 +00002695 grp = &mcp->mc_gregs;
2696
2697 /* Skip over the trap instruction, first. */
2698 env->pc = env->npc;
2699 env->npc += 4;
2700
2701 err = 0;
2702
Alex Barcelo1c275922014-03-14 14:36:55 +00002703 do_sigprocmask(0, NULL, &set);
blueswir15bfb56b2007-10-05 17:01:51 +00002704 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002705 if (TARGET_NSIG_WORDS == 1) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002706 __put_user(target_set.sig[0],
2707 (abi_ulong *)&ucp->tuc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002708 } else {
2709 abi_ulong *src, *dst;
2710 src = target_set.sig;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002711 dst = ucp->tuc_sigmask.sig;
Stefan Weil0d9e61c2013-12-07 14:48:08 +01002712 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002713 __put_user(*src, dst);
Stefan Weil0d9e61c2013-12-07 14:48:08 +01002714 }
blueswir15bfb56b2007-10-05 17:01:51 +00002715 if (err)
2716 goto do_sigsegv;
2717 }
2718
bellard459a4012007-11-11 19:45:10 +00002719 /* XXX: tstate must be saved properly */
Riku Voipio1d8b5122014-04-23 10:26:05 +03002720 // __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2721 __put_user(env->pc, &((*grp)[MC_PC]));
2722 __put_user(env->npc, &((*grp)[MC_NPC]));
2723 __put_user(env->y, &((*grp)[MC_Y]));
2724 __put_user(env->gregs[1], &((*grp)[MC_G1]));
2725 __put_user(env->gregs[2], &((*grp)[MC_G2]));
2726 __put_user(env->gregs[3], &((*grp)[MC_G3]));
2727 __put_user(env->gregs[4], &((*grp)[MC_G4]));
2728 __put_user(env->gregs[5], &((*grp)[MC_G5]));
2729 __put_user(env->gregs[6], &((*grp)[MC_G6]));
2730 __put_user(env->gregs[7], &((*grp)[MC_G7]));
2731 __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2732 __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2733 __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2734 __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2735 __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2736 __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2737 __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2738 __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002739
bellard459a4012007-11-11 19:45:10 +00002740 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2741 fp = i7 = 0;
2742 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2743 abi_ulong) != 0)
2744 goto do_sigsegv;
2745 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2746 abi_ulong) != 0)
2747 goto do_sigsegv;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002748 __put_user(fp, &(mcp->mc_fp));
2749 __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002750
bellard459a4012007-11-11 19:45:10 +00002751 {
Richard Henderson30038fd2011-10-17 10:42:49 -07002752 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2753 for (i = 0; i < 64; i++, dst++) {
2754 if (i & 1) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002755 __put_user(env->fpr[i/2].l.lower, dst);
Richard Henderson30038fd2011-10-17 10:42:49 -07002756 } else {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002757 __put_user(env->fpr[i/2].l.upper, dst);
Richard Henderson30038fd2011-10-17 10:42:49 -07002758 }
2759 }
bellard459a4012007-11-11 19:45:10 +00002760 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03002761 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2762 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2763 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002764
2765 if (err)
2766 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002767 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002768 return;
2769 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002770 unlock_user_struct(ucp, ucp_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02002771 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002772}
2773#endif
Richard Hendersonff970902013-02-10 10:30:42 -08002774#elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
bellard106ec872006-06-27 21:08:10 +00002775
Richard Hendersonff970902013-02-10 10:30:42 -08002776# if defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002777struct target_sigcontext {
2778 uint32_t sc_regmask; /* Unused */
2779 uint32_t sc_status;
2780 uint64_t sc_pc;
2781 uint64_t sc_regs[32];
2782 uint64_t sc_fpregs[32];
2783 uint32_t sc_ownedfp; /* Unused */
2784 uint32_t sc_fpc_csr;
2785 uint32_t sc_fpc_eir; /* Unused */
2786 uint32_t sc_used_math;
2787 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
Paul Brook94c54952009-07-09 18:40:15 +01002788 uint32_t pad0;
bellard106ec872006-06-27 21:08:10 +00002789 uint64_t sc_mdhi;
2790 uint64_t sc_mdlo;
2791 target_ulong sc_hi1; /* Was sc_cause */
2792 target_ulong sc_lo1; /* Was sc_badvaddr */
2793 target_ulong sc_hi2; /* Was sc_sigset[4] */
2794 target_ulong sc_lo2;
2795 target_ulong sc_hi3;
2796 target_ulong sc_lo3;
2797};
Richard Hendersonff970902013-02-10 10:30:42 -08002798# else /* N32 || N64 */
2799struct target_sigcontext {
2800 uint64_t sc_regs[32];
2801 uint64_t sc_fpregs[32];
2802 uint64_t sc_mdhi;
2803 uint64_t sc_hi1;
2804 uint64_t sc_hi2;
2805 uint64_t sc_hi3;
2806 uint64_t sc_mdlo;
2807 uint64_t sc_lo1;
2808 uint64_t sc_lo2;
2809 uint64_t sc_lo3;
2810 uint64_t sc_pc;
2811 uint32_t sc_fpc_csr;
2812 uint32_t sc_used_math;
2813 uint32_t sc_dsp;
2814 uint32_t sc_reserved;
2815};
2816# endif /* O32 */
bellard106ec872006-06-27 21:08:10 +00002817
2818struct sigframe {
2819 uint32_t sf_ass[4]; /* argument save space for o32 */
2820 uint32_t sf_code[2]; /* signal trampoline */
2821 struct target_sigcontext sf_sc;
Anthony Liguoric227f092009-10-01 16:12:16 -05002822 target_sigset_t sf_mask;
bellard106ec872006-06-27 21:08:10 +00002823};
2824
pbrook0b1bcb02009-04-21 01:41:10 +00002825struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002826 target_ulong tuc_flags;
2827 target_ulong tuc_link;
2828 target_stack_t tuc_stack;
Paul Brook94c54952009-07-09 18:40:15 +01002829 target_ulong pad0;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002830 struct target_sigcontext tuc_mcontext;
2831 target_sigset_t tuc_sigmask;
pbrook0b1bcb02009-04-21 01:41:10 +00002832};
2833
2834struct target_rt_sigframe {
2835 uint32_t rs_ass[4]; /* argument save space for o32 */
2836 uint32_t rs_code[2]; /* signal trampoline */
2837 struct target_siginfo rs_info;
2838 struct target_ucontext rs_uc;
2839};
2840
bellard106ec872006-06-27 21:08:10 +00002841/* Install trampoline to jump back from signal handler */
2842static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2843{
Richard Henderson084d0492013-02-10 10:30:44 -08002844 int err = 0;
bellard106ec872006-06-27 21:08:10 +00002845
2846 /*
Richard Henderson084d0492013-02-10 10:30:44 -08002847 * Set up the return code ...
2848 *
2849 * li v0, __NR__foo_sigreturn
2850 * syscall
2851 */
bellard106ec872006-06-27 21:08:10 +00002852
Riku Voipio1d8b5122014-04-23 10:26:05 +03002853 __put_user(0x24020000 + syscall, tramp + 0);
2854 __put_user(0x0000000c , tramp + 1);
bellard106ec872006-06-27 21:08:10 +00002855 return err;
2856}
2857
2858static inline int
Andreas Färber05390242012-02-25 03:37:53 +01002859setup_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
bellard106ec872006-06-27 21:08:10 +00002860{
2861 int err = 0;
Richard Henderson084d0492013-02-10 10:30:44 -08002862 int i;
bellard106ec872006-06-27 21:08:10 +00002863
Riku Voipio1d8b5122014-04-23 10:26:05 +03002864 __put_user(exception_resume_pc(regs), &sc->sc_pc);
Kwok Cheung Yeung1239b472013-05-17 14:51:21 -07002865 regs->hflags &= ~MIPS_HFLAG_BMASK;
bellard106ec872006-06-27 21:08:10 +00002866
Richard Henderson084d0492013-02-10 10:30:44 -08002867 __put_user(0, &sc->sc_regs[0]);
2868 for (i = 1; i < 32; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002869 __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
Richard Henderson084d0492013-02-10 10:30:44 -08002870 }
bellard106ec872006-06-27 21:08:10 +00002871
Riku Voipio1d8b5122014-04-23 10:26:05 +03002872 __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2873 __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002874
Richard Henderson084d0492013-02-10 10:30:44 -08002875 /* Rather than checking for dsp existence, always copy. The storage
2876 would just be garbage otherwise. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03002877 __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2878 __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2879 __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2880 __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2881 __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2882 __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
Richard Henderson084d0492013-02-10 10:30:44 -08002883 {
2884 uint32_t dsp = cpu_rddsp(0x3ff, regs);
Riku Voipio1d8b5122014-04-23 10:26:05 +03002885 __put_user(dsp, &sc->sc_dsp);
bellard106ec872006-06-27 21:08:10 +00002886 }
Richard Henderson084d0492013-02-10 10:30:44 -08002887
Riku Voipio1d8b5122014-04-23 10:26:05 +03002888 __put_user(1, &sc->sc_used_math);
Richard Henderson084d0492013-02-10 10:30:44 -08002889
2890 for (i = 0; i < 32; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002891 __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
bellard106ec872006-06-27 21:08:10 +00002892 }
bellard106ec872006-06-27 21:08:10 +00002893
bellard106ec872006-06-27 21:08:10 +00002894 return err;
2895}
2896
2897static inline int
Andreas Färber05390242012-02-25 03:37:53 +01002898restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
bellard106ec872006-06-27 21:08:10 +00002899{
2900 int err = 0;
Richard Henderson084d0492013-02-10 10:30:44 -08002901 int i;
bellard106ec872006-06-27 21:08:10 +00002902
Riku Voipio1d8b5122014-04-23 10:26:05 +03002903 __get_user(regs->CP0_EPC, &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002904
Riku Voipio1d8b5122014-04-23 10:26:05 +03002905 __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2906 __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002907
Richard Henderson084d0492013-02-10 10:30:44 -08002908 for (i = 1; i < 32; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002909 __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
bellard106ec872006-06-27 21:08:10 +00002910 }
2911
Riku Voipio1d8b5122014-04-23 10:26:05 +03002912 __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2913 __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2914 __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2915 __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2916 __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2917 __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
Richard Henderson084d0492013-02-10 10:30:44 -08002918 {
2919 uint32_t dsp;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002920 __get_user(dsp, &sc->sc_dsp);
Richard Henderson084d0492013-02-10 10:30:44 -08002921 cpu_wrdsp(dsp, 0x3ff, regs);
2922 }
2923
2924 for (i = 0; i < 32; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002925 __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
Richard Henderson084d0492013-02-10 10:30:44 -08002926 }
2927
bellard106ec872006-06-27 21:08:10 +00002928 return err;
2929}
Richard Hendersonff970902013-02-10 10:30:42 -08002930
bellard106ec872006-06-27 21:08:10 +00002931/*
2932 * Determine which stack to use..
2933 */
bellard579a97f2007-11-11 14:26:47 +00002934static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01002935get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
bellard106ec872006-06-27 21:08:10 +00002936{
2937 unsigned long sp;
2938
2939 /* Default to using normal stack */
thsb5dc7732008-06-27 10:02:35 +00002940 sp = regs->active_tc.gpr[29];
bellard106ec872006-06-27 21:08:10 +00002941
2942 /*
Stefan Weil93148aa2012-02-26 18:46:12 +01002943 * FPU emulator may have its own trampoline active just
bellard106ec872006-06-27 21:08:10 +00002944 * above the user stack, 16-bytes before the next lowest
2945 * 16 byte boundary. Try to avoid trashing it.
2946 */
2947 sp -= 32;
2948
bellard106ec872006-06-27 21:08:10 +00002949 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002950 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
thsa04e1342007-09-27 13:57:58 +00002951 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2952 }
bellard106ec872006-06-27 21:08:10 +00002953
bellard579a97f2007-11-11 14:26:47 +00002954 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002955}
2956
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07002957static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2958{
2959 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2960 env->hflags &= ~MIPS_HFLAG_M16;
2961 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2962 env->active_tc.PC &= ~(target_ulong) 1;
2963 }
2964}
2965
Richard Hendersonff970902013-02-10 10:30:42 -08002966# if defined(TARGET_ABI_MIPSO32)
bellard579a97f2007-11-11 14:26:47 +00002967/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00002968static void setup_frame(int sig, struct target_sigaction * ka,
Andreas Färber05390242012-02-25 03:37:53 +01002969 target_sigset_t *set, CPUMIPSState *regs)
bellard106ec872006-06-27 21:08:10 +00002970{
2971 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002972 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002973 int i;
2974
bellard579a97f2007-11-11 14:26:47 +00002975 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2976 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002977 goto give_sigsegv;
2978
2979 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2980
2981 if(setup_sigcontext(regs, &frame->sf_sc))
2982 goto give_sigsegv;
2983
2984 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2985 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2986 goto give_sigsegv;
2987 }
2988
2989 /*
2990 * Arguments to signal handler:
2991 *
2992 * a0 = signal number
2993 * a1 = 0 (should be cause)
2994 * a2 = pointer to struct sigcontext
2995 *
2996 * $25 and PC point to the signal handler, $29 points to the
2997 * struct sigframe.
2998 */
thsb5dc7732008-06-27 10:02:35 +00002999 regs->active_tc.gpr[ 4] = sig;
3000 regs->active_tc.gpr[ 5] = 0;
3001 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
3002 regs->active_tc.gpr[29] = frame_addr;
3003 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00003004 /* The original kernel code sets CP0_EPC to the handler
3005 * since it returns to userland using eret
3006 * we cannot do this here, and we must set PC directly */
thsb5dc7732008-06-27 10:02:35 +00003007 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07003008 mips_set_hflags_isa_mode_from_pc(regs);
bellard579a97f2007-11-11 14:26:47 +00003009 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00003010 return;
3011
3012give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00003013 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00003014 force_sig(TARGET_SIGSEGV/*, current*/);
bellard106ec872006-06-27 21:08:10 +00003015}
3016
Andreas Färber05390242012-02-25 03:37:53 +01003017long do_sigreturn(CPUMIPSState *regs)
bellard106ec872006-06-27 21:08:10 +00003018{
ths388bb212007-05-13 13:58:00 +00003019 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00003020 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00003021 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05003022 target_sigset_t target_set;
ths388bb212007-05-13 13:58:00 +00003023 int i;
bellard106ec872006-06-27 21:08:10 +00003024
3025#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00003026 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00003027#endif
thsb5dc7732008-06-27 10:02:35 +00003028 frame_addr = regs->active_tc.gpr[29];
bellard579a97f2007-11-11 14:26:47 +00003029 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00003030 goto badframe;
3031
ths388bb212007-05-13 13:58:00 +00003032 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00003033 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
3034 goto badframe;
ths388bb212007-05-13 13:58:00 +00003035 }
bellard106ec872006-06-27 21:08:10 +00003036
ths388bb212007-05-13 13:58:00 +00003037 target_to_host_sigset_internal(&blocked, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00003038 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00003039
ths388bb212007-05-13 13:58:00 +00003040 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00003041 goto badframe;
3042
3043#if 0
ths388bb212007-05-13 13:58:00 +00003044 /*
3045 * Don't let your children do this ...
3046 */
3047 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00003048 "move\t$29, %0\n\t"
3049 "j\tsyscall_exit"
3050 :/* no outputs */
3051 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00003052 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00003053#endif
ths3b46e622007-09-17 08:09:54 +00003054
thsb5dc7732008-06-27 10:02:35 +00003055 regs->active_tc.PC = regs->CP0_EPC;
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07003056 mips_set_hflags_isa_mode_from_pc(regs);
ths388bb212007-05-13 13:58:00 +00003057 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00003058 * maybe a problem with nested signals ? */
3059 regs->CP0_EPC = 0;
pbrook0b1bcb02009-04-21 01:41:10 +00003060 return -TARGET_QEMU_ESIGRETURN;
bellard106ec872006-06-27 21:08:10 +00003061
3062badframe:
ths388bb212007-05-13 13:58:00 +00003063 force_sig(TARGET_SIGSEGV/*, current*/);
3064 return 0;
bellard106ec872006-06-27 21:08:10 +00003065}
Richard Hendersonff970902013-02-10 10:30:42 -08003066# endif /* O32 */
bellard106ec872006-06-27 21:08:10 +00003067
pbrook624f7972008-05-31 16:11:38 +00003068static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003069 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003070 target_sigset_t *set, CPUMIPSState *env)
bellard106ec872006-06-27 21:08:10 +00003071{
pbrook0b1bcb02009-04-21 01:41:10 +00003072 struct target_rt_sigframe *frame;
3073 abi_ulong frame_addr;
3074 int i;
3075
3076 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3077 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3078 goto give_sigsegv;
3079
3080 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3081
3082 copy_siginfo_to_user(&frame->rs_info, info);
3083
Aurelien Jarno60e99242010-03-29 02:12:51 +02003084 __put_user(0, &frame->rs_uc.tuc_flags);
3085 __put_user(0, &frame->rs_uc.tuc_link);
3086 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3087 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
pbrook0b1bcb02009-04-21 01:41:10 +00003088 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
Aurelien Jarno60e99242010-03-29 02:12:51 +02003089 &frame->rs_uc.tuc_stack.ss_flags);
pbrook0b1bcb02009-04-21 01:41:10 +00003090
Aurelien Jarno60e99242010-03-29 02:12:51 +02003091 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
pbrook0b1bcb02009-04-21 01:41:10 +00003092
3093 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02003094 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
pbrook0b1bcb02009-04-21 01:41:10 +00003095 }
3096
3097 /*
3098 * Arguments to signal handler:
3099 *
3100 * a0 = signal number
Richard W.M. Jones02d2bd52012-07-05 03:32:44 +00003101 * a1 = pointer to siginfo_t
pbrook0b1bcb02009-04-21 01:41:10 +00003102 * a2 = pointer to struct ucontext
3103 *
3104 * $25 and PC point to the signal handler, $29 points to the
3105 * struct sigframe.
3106 */
3107 env->active_tc.gpr[ 4] = sig;
3108 env->active_tc.gpr[ 5] = frame_addr
3109 + offsetof(struct target_rt_sigframe, rs_info);
3110 env->active_tc.gpr[ 6] = frame_addr
3111 + offsetof(struct target_rt_sigframe, rs_uc);
3112 env->active_tc.gpr[29] = frame_addr;
3113 env->active_tc.gpr[31] = frame_addr
3114 + offsetof(struct target_rt_sigframe, rs_code);
3115 /* The original kernel code sets CP0_EPC to the handler
3116 * since it returns to userland using eret
3117 * we cannot do this here, and we must set PC directly */
3118 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07003119 mips_set_hflags_isa_mode_from_pc(env);
pbrook0b1bcb02009-04-21 01:41:10 +00003120 unlock_user_struct(frame, frame_addr, 1);
3121 return;
3122
3123give_sigsegv:
3124 unlock_user_struct(frame, frame_addr, 1);
3125 force_sig(TARGET_SIGSEGV/*, current*/);
bellard106ec872006-06-27 21:08:10 +00003126}
3127
Andreas Färber05390242012-02-25 03:37:53 +01003128long do_rt_sigreturn(CPUMIPSState *env)
bellard106ec872006-06-27 21:08:10 +00003129{
pbrook0b1bcb02009-04-21 01:41:10 +00003130 struct target_rt_sigframe *frame;
3131 abi_ulong frame_addr;
3132 sigset_t blocked;
3133
3134#if defined(DEBUG_SIGNAL)
3135 fprintf(stderr, "do_rt_sigreturn\n");
3136#endif
3137 frame_addr = env->active_tc.gpr[29];
3138 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3139 goto badframe;
3140
Aurelien Jarno60e99242010-03-29 02:12:51 +02003141 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00003142 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
pbrook0b1bcb02009-04-21 01:41:10 +00003143
Aurelien Jarno60e99242010-03-29 02:12:51 +02003144 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
pbrook0b1bcb02009-04-21 01:41:10 +00003145 goto badframe;
3146
3147 if (do_sigaltstack(frame_addr +
Aurelien Jarno60e99242010-03-29 02:12:51 +02003148 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
pbrook0b1bcb02009-04-21 01:41:10 +00003149 0, get_sp_from_cpustate(env)) == -EFAULT)
3150 goto badframe;
3151
3152 env->active_tc.PC = env->CP0_EPC;
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07003153 mips_set_hflags_isa_mode_from_pc(env);
pbrook0b1bcb02009-04-21 01:41:10 +00003154 /* I am not sure this is right, but it seems to work
3155 * maybe a problem with nested signals ? */
3156 env->CP0_EPC = 0;
3157 return -TARGET_QEMU_ESIGRETURN;
3158
3159badframe:
3160 force_sig(TARGET_SIGSEGV/*, current*/);
3161 return 0;
bellard106ec872006-06-27 21:08:10 +00003162}
bellard6d5e2162004-09-30 22:04:13 +00003163
thsc3b5bc82007-12-02 06:31:25 +00003164#elif defined(TARGET_SH4)
3165
3166/*
3167 * code and data structures from linux kernel:
3168 * include/asm-sh/sigcontext.h
3169 * arch/sh/kernel/signal.c
3170 */
3171
3172struct target_sigcontext {
3173 target_ulong oldmask;
3174
3175 /* CPU registers */
3176 target_ulong sc_gregs[16];
3177 target_ulong sc_pc;
3178 target_ulong sc_pr;
3179 target_ulong sc_sr;
3180 target_ulong sc_gbr;
3181 target_ulong sc_mach;
3182 target_ulong sc_macl;
3183
3184 /* FPU registers */
3185 target_ulong sc_fpregs[16];
3186 target_ulong sc_xfpregs[16];
3187 unsigned int sc_fpscr;
3188 unsigned int sc_fpul;
3189 unsigned int sc_ownedfp;
3190};
3191
3192struct target_sigframe
3193{
3194 struct target_sigcontext sc;
3195 target_ulong extramask[TARGET_NSIG_WORDS-1];
3196 uint16_t retcode[3];
3197};
3198
3199
3200struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02003201 target_ulong tuc_flags;
3202 struct target_ucontext *tuc_link;
3203 target_stack_t tuc_stack;
3204 struct target_sigcontext tuc_mcontext;
3205 target_sigset_t tuc_sigmask; /* mask last for extensibility */
thsc3b5bc82007-12-02 06:31:25 +00003206};
3207
3208struct target_rt_sigframe
3209{
3210 struct target_siginfo info;
3211 struct target_ucontext uc;
3212 uint16_t retcode[3];
3213};
3214
3215
3216#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3217#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3218
pbrook624f7972008-05-31 16:11:38 +00003219static abi_ulong get_sigframe(struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00003220 unsigned long sp, size_t frame_size)
3221{
pbrook624f7972008-05-31 16:11:38 +00003222 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
thsc3b5bc82007-12-02 06:31:25 +00003223 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3224 }
3225
3226 return (sp - frame_size) & -8ul;
3227}
3228
3229static int setup_sigcontext(struct target_sigcontext *sc,
Andreas Färber05390242012-02-25 03:37:53 +01003230 CPUSH4State *regs, unsigned long mask)
thsc3b5bc82007-12-02 06:31:25 +00003231{
3232 int err = 0;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003233 int i;
thsc3b5bc82007-12-02 06:31:25 +00003234
Riku Voipio1d8b5122014-04-23 10:26:05 +03003235#define COPY(x) __put_user(regs->x, &sc->sc_##x)
thsc3b5bc82007-12-02 06:31:25 +00003236 COPY(gregs[0]); COPY(gregs[1]);
3237 COPY(gregs[2]); COPY(gregs[3]);
3238 COPY(gregs[4]); COPY(gregs[5]);
3239 COPY(gregs[6]); COPY(gregs[7]);
3240 COPY(gregs[8]); COPY(gregs[9]);
3241 COPY(gregs[10]); COPY(gregs[11]);
3242 COPY(gregs[12]); COPY(gregs[13]);
3243 COPY(gregs[14]); COPY(gregs[15]);
3244 COPY(gbr); COPY(mach);
3245 COPY(macl); COPY(pr);
3246 COPY(sr); COPY(pc);
3247#undef COPY
3248
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003249 for (i=0; i<16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003250 __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003251 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03003252 __put_user(regs->fpscr, &sc->sc_fpscr);
3253 __put_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00003254
3255 /* non-iBCS2 extensions.. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003256 __put_user(mask, &sc->oldmask);
thsc3b5bc82007-12-02 06:31:25 +00003257
3258 return err;
3259}
3260
Andreas Färber05390242012-02-25 03:37:53 +01003261static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003262 target_ulong *r0_p)
thsc3b5bc82007-12-02 06:31:25 +00003263{
3264 unsigned int err = 0;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003265 int i;
thsc3b5bc82007-12-02 06:31:25 +00003266
Riku Voipio1d8b5122014-04-23 10:26:05 +03003267#define COPY(x) __get_user(regs->x, &sc->sc_##x)
thsc3b5bc82007-12-02 06:31:25 +00003268 COPY(gregs[1]);
3269 COPY(gregs[2]); COPY(gregs[3]);
3270 COPY(gregs[4]); COPY(gregs[5]);
3271 COPY(gregs[6]); COPY(gregs[7]);
3272 COPY(gregs[8]); COPY(gregs[9]);
3273 COPY(gregs[10]); COPY(gregs[11]);
3274 COPY(gregs[12]); COPY(gregs[13]);
3275 COPY(gregs[14]); COPY(gregs[15]);
3276 COPY(gbr); COPY(mach);
3277 COPY(macl); COPY(pr);
3278 COPY(sr); COPY(pc);
3279#undef COPY
3280
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003281 for (i=0; i<16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003282 __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003283 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03003284 __get_user(regs->fpscr, &sc->sc_fpscr);
3285 __get_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00003286
3287 regs->tra = -1; /* disable syscall checks */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003288 __get_user(*r0_p, &sc->sc_gregs[0]);
thsc3b5bc82007-12-02 06:31:25 +00003289 return err;
3290}
3291
pbrook624f7972008-05-31 16:11:38 +00003292static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003293 target_sigset_t *set, CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003294{
3295 struct target_sigframe *frame;
3296 abi_ulong frame_addr;
3297 int i;
3298 int err = 0;
3299 int signal;
3300
3301 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3302 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3303 goto give_sigsegv;
3304
3305 signal = current_exec_domain_sig(sig);
3306
3307 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
3308
3309 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003310 __put_user(set->sig[i + 1], &frame->extramask[i]);
thsc3b5bc82007-12-02 06:31:25 +00003311 }
3312
3313 /* Set up to return from userspace. If provided, use a stub
3314 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00003315 if (ka->sa_flags & TARGET_SA_RESTORER) {
3316 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00003317 } else {
3318 /* Generate return code (system call to sigreturn) */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003319 __put_user(MOVW(2), &frame->retcode[0]);
3320 __put_user(TRAP_NOARG, &frame->retcode[1]);
3321 __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
thsc3b5bc82007-12-02 06:31:25 +00003322 regs->pr = (unsigned long) frame->retcode;
3323 }
3324
3325 if (err)
3326 goto give_sigsegv;
3327
3328 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003329 regs->gregs[15] = frame_addr;
thsc3b5bc82007-12-02 06:31:25 +00003330 regs->gregs[4] = signal; /* Arg for signal handler */
3331 regs->gregs[5] = 0;
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003332 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
pbrook624f7972008-05-31 16:11:38 +00003333 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00003334
3335 unlock_user_struct(frame, frame_addr, 1);
3336 return;
3337
3338give_sigsegv:
3339 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02003340 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00003341}
3342
pbrook624f7972008-05-31 16:11:38 +00003343static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003344 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003345 target_sigset_t *set, CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003346{
3347 struct target_rt_sigframe *frame;
3348 abi_ulong frame_addr;
3349 int i;
3350 int err = 0;
3351 int signal;
3352
3353 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3354 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3355 goto give_sigsegv;
3356
3357 signal = current_exec_domain_sig(sig);
3358
Riku Voipiob0fd8d12014-04-23 10:46:13 +03003359 copy_siginfo_to_user(&frame->info, info);
thsc3b5bc82007-12-02 06:31:25 +00003360
3361 /* Create the ucontext. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003362 __put_user(0, &frame->uc.tuc_flags);
3363 __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3364 __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3365 &frame->uc.tuc_stack.ss_sp);
3366 __put_user(sas_ss_flags(regs->gregs[15]),
3367 &frame->uc.tuc_stack.ss_flags);
3368 __put_user(target_sigaltstack_used.ss_size,
3369 &frame->uc.tuc_stack.ss_size);
3370 setup_sigcontext(&frame->uc.tuc_mcontext,
thsc3b5bc82007-12-02 06:31:25 +00003371 regs, set->sig[0]);
3372 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003373 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
thsc3b5bc82007-12-02 06:31:25 +00003374 }
3375
3376 /* Set up to return from userspace. If provided, use a stub
3377 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00003378 if (ka->sa_flags & TARGET_SA_RESTORER) {
3379 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00003380 } else {
3381 /* Generate return code (system call to sigreturn) */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003382 __put_user(MOVW(2), &frame->retcode[0]);
3383 __put_user(TRAP_NOARG, &frame->retcode[1]);
3384 __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
thsc3b5bc82007-12-02 06:31:25 +00003385 regs->pr = (unsigned long) frame->retcode;
3386 }
3387
3388 if (err)
3389 goto give_sigsegv;
3390
3391 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003392 regs->gregs[15] = frame_addr;
thsc3b5bc82007-12-02 06:31:25 +00003393 regs->gregs[4] = signal; /* Arg for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003394 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3395 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
pbrook624f7972008-05-31 16:11:38 +00003396 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00003397
3398 unlock_user_struct(frame, frame_addr, 1);
3399 return;
3400
3401give_sigsegv:
3402 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02003403 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00003404}
3405
Andreas Färber05390242012-02-25 03:37:53 +01003406long do_sigreturn(CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003407{
3408 struct target_sigframe *frame;
3409 abi_ulong frame_addr;
3410 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05003411 target_sigset_t target_set;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003412 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00003413 int i;
3414 int err = 0;
3415
3416#if defined(DEBUG_SIGNAL)
3417 fprintf(stderr, "do_sigreturn\n");
3418#endif
3419 frame_addr = regs->gregs[15];
3420 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3421 goto badframe;
3422
Riku Voipio1d8b5122014-04-23 10:26:05 +03003423 __get_user(target_set.sig[0], &frame->sc.oldmask);
thsc3b5bc82007-12-02 06:31:25 +00003424 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003425 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
thsc3b5bc82007-12-02 06:31:25 +00003426 }
3427
3428 if (err)
3429 goto badframe;
3430
3431 target_to_host_sigset_internal(&blocked, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00003432 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
thsc3b5bc82007-12-02 06:31:25 +00003433
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003434 if (restore_sigcontext(regs, &frame->sc, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003435 goto badframe;
3436
3437 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003438 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003439
3440badframe:
3441 unlock_user_struct(frame, frame_addr, 0);
3442 force_sig(TARGET_SIGSEGV);
3443 return 0;
3444}
3445
Andreas Färber05390242012-02-25 03:37:53 +01003446long do_rt_sigreturn(CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003447{
3448 struct target_rt_sigframe *frame;
3449 abi_ulong frame_addr;
3450 sigset_t blocked;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003451 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00003452
3453#if defined(DEBUG_SIGNAL)
3454 fprintf(stderr, "do_rt_sigreturn\n");
3455#endif
3456 frame_addr = regs->gregs[15];
3457 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3458 goto badframe;
3459
Aurelien Jarno60e99242010-03-29 02:12:51 +02003460 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00003461 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
thsc3b5bc82007-12-02 06:31:25 +00003462
Aurelien Jarno60e99242010-03-29 02:12:51 +02003463 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003464 goto badframe;
3465
3466 if (do_sigaltstack(frame_addr +
Aurelien Jarno60e99242010-03-29 02:12:51 +02003467 offsetof(struct target_rt_sigframe, uc.tuc_stack),
thsc3b5bc82007-12-02 06:31:25 +00003468 0, get_sp_from_cpustate(regs)) == -EFAULT)
3469 goto badframe;
3470
3471 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003472 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003473
3474badframe:
3475 unlock_user_struct(frame, frame_addr, 0);
3476 force_sig(TARGET_SIGSEGV);
3477 return 0;
3478}
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003479#elif defined(TARGET_MICROBLAZE)
3480
3481struct target_sigcontext {
3482 struct target_pt_regs regs; /* needs to be first */
3483 uint32_t oldmask;
3484};
3485
Edgar E. Iglesiasb2178702010-07-23 09:30:37 +02003486struct target_stack_t {
3487 abi_ulong ss_sp;
3488 int ss_flags;
3489 unsigned int ss_size;
3490};
3491
3492struct target_ucontext {
Richard Hendersonf711df62010-11-22 14:57:52 -08003493 abi_ulong tuc_flags;
3494 abi_ulong tuc_link;
3495 struct target_stack_t tuc_stack;
3496 struct target_sigcontext tuc_mcontext;
3497 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
Edgar E. Iglesiasb2178702010-07-23 09:30:37 +02003498};
3499
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003500/* Signal frames. */
3501struct target_signal_frame {
Edgar E. Iglesiasb2178702010-07-23 09:30:37 +02003502 struct target_ucontext uc;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003503 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3504 uint32_t tramp[2];
3505};
3506
3507struct rt_signal_frame {
Richard W.M. Jones02d2bd52012-07-05 03:32:44 +00003508 siginfo_t info;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003509 struct ucontext uc;
3510 uint32_t tramp[2];
3511};
3512
Andreas Färber05390242012-02-25 03:37:53 +01003513static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003514{
3515 __put_user(env->regs[0], &sc->regs.r0);
3516 __put_user(env->regs[1], &sc->regs.r1);
3517 __put_user(env->regs[2], &sc->regs.r2);
3518 __put_user(env->regs[3], &sc->regs.r3);
3519 __put_user(env->regs[4], &sc->regs.r4);
3520 __put_user(env->regs[5], &sc->regs.r5);
3521 __put_user(env->regs[6], &sc->regs.r6);
3522 __put_user(env->regs[7], &sc->regs.r7);
3523 __put_user(env->regs[8], &sc->regs.r8);
3524 __put_user(env->regs[9], &sc->regs.r9);
3525 __put_user(env->regs[10], &sc->regs.r10);
3526 __put_user(env->regs[11], &sc->regs.r11);
3527 __put_user(env->regs[12], &sc->regs.r12);
3528 __put_user(env->regs[13], &sc->regs.r13);
3529 __put_user(env->regs[14], &sc->regs.r14);
3530 __put_user(env->regs[15], &sc->regs.r15);
3531 __put_user(env->regs[16], &sc->regs.r16);
3532 __put_user(env->regs[17], &sc->regs.r17);
3533 __put_user(env->regs[18], &sc->regs.r18);
3534 __put_user(env->regs[19], &sc->regs.r19);
3535 __put_user(env->regs[20], &sc->regs.r20);
3536 __put_user(env->regs[21], &sc->regs.r21);
3537 __put_user(env->regs[22], &sc->regs.r22);
3538 __put_user(env->regs[23], &sc->regs.r23);
3539 __put_user(env->regs[24], &sc->regs.r24);
3540 __put_user(env->regs[25], &sc->regs.r25);
3541 __put_user(env->regs[26], &sc->regs.r26);
3542 __put_user(env->regs[27], &sc->regs.r27);
3543 __put_user(env->regs[28], &sc->regs.r28);
3544 __put_user(env->regs[29], &sc->regs.r29);
3545 __put_user(env->regs[30], &sc->regs.r30);
3546 __put_user(env->regs[31], &sc->regs.r31);
3547 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3548}
3549
Andreas Färber05390242012-02-25 03:37:53 +01003550static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003551{
3552 __get_user(env->regs[0], &sc->regs.r0);
3553 __get_user(env->regs[1], &sc->regs.r1);
3554 __get_user(env->regs[2], &sc->regs.r2);
3555 __get_user(env->regs[3], &sc->regs.r3);
3556 __get_user(env->regs[4], &sc->regs.r4);
3557 __get_user(env->regs[5], &sc->regs.r5);
3558 __get_user(env->regs[6], &sc->regs.r6);
3559 __get_user(env->regs[7], &sc->regs.r7);
3560 __get_user(env->regs[8], &sc->regs.r8);
3561 __get_user(env->regs[9], &sc->regs.r9);
3562 __get_user(env->regs[10], &sc->regs.r10);
3563 __get_user(env->regs[11], &sc->regs.r11);
3564 __get_user(env->regs[12], &sc->regs.r12);
3565 __get_user(env->regs[13], &sc->regs.r13);
3566 __get_user(env->regs[14], &sc->regs.r14);
3567 __get_user(env->regs[15], &sc->regs.r15);
3568 __get_user(env->regs[16], &sc->regs.r16);
3569 __get_user(env->regs[17], &sc->regs.r17);
3570 __get_user(env->regs[18], &sc->regs.r18);
3571 __get_user(env->regs[19], &sc->regs.r19);
3572 __get_user(env->regs[20], &sc->regs.r20);
3573 __get_user(env->regs[21], &sc->regs.r21);
3574 __get_user(env->regs[22], &sc->regs.r22);
3575 __get_user(env->regs[23], &sc->regs.r23);
3576 __get_user(env->regs[24], &sc->regs.r24);
3577 __get_user(env->regs[25], &sc->regs.r25);
3578 __get_user(env->regs[26], &sc->regs.r26);
3579 __get_user(env->regs[27], &sc->regs.r27);
3580 __get_user(env->regs[28], &sc->regs.r28);
3581 __get_user(env->regs[29], &sc->regs.r29);
3582 __get_user(env->regs[30], &sc->regs.r30);
3583 __get_user(env->regs[31], &sc->regs.r31);
3584 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3585}
3586
3587static abi_ulong get_sigframe(struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003588 CPUMBState *env, int frame_size)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003589{
3590 abi_ulong sp = env->regs[1];
3591
3592 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3593 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3594
3595 return ((sp - frame_size) & -8UL);
3596}
3597
3598static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003599 target_sigset_t *set, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003600{
3601 struct target_signal_frame *frame;
3602 abi_ulong frame_addr;
3603 int err = 0;
3604 int i;
3605
3606 frame_addr = get_sigframe(ka, env, sizeof *frame);
3607 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3608 goto badframe;
3609
3610 /* Save the mask. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003611 __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003612 if (err)
3613 goto badframe;
3614
3615 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3616 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3617 goto badframe;
3618 }
3619
Richard Hendersonf711df62010-11-22 14:57:52 -08003620 setup_sigcontext(&frame->uc.tuc_mcontext, env);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003621
3622 /* Set up to return from userspace. If provided, use a stub
3623 already in userspace. */
3624 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3625 if (ka->sa_flags & TARGET_SA_RESTORER) {
3626 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3627 } else {
3628 uint32_t t;
3629 /* Note, these encodings are _big endian_! */
3630 /* addi r12, r0, __NR_sigreturn */
3631 t = 0x31800000UL | TARGET_NR_sigreturn;
Riku Voipio1d8b5122014-04-23 10:26:05 +03003632 __put_user(t, frame->tramp + 0);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003633 /* brki r14, 0x8 */
3634 t = 0xb9cc0008UL;
Riku Voipio1d8b5122014-04-23 10:26:05 +03003635 __put_user(t, frame->tramp + 1);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003636
3637 /* Return from sighandler will jump to the tramp.
3638 Negative 8 offset because return is rtsd r15, 8 */
3639 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3640 }
3641
3642 if (err)
3643 goto badframe;
3644
3645 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003646 env->regs[1] = frame_addr;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003647 /* Signal handler args: */
3648 env->regs[5] = sig; /* Arg 0: signum */
Edgar E. Iglesias187b4e02010-07-15 15:32:51 +02003649 env->regs[6] = 0;
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003650 /* arg 1: sigcontext */
3651 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003652
3653 /* Offset of 4 to handle microblaze rtid r14, 0 */
3654 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3655
3656 unlock_user_struct(frame, frame_addr, 1);
3657 return;
3658 badframe:
3659 unlock_user_struct(frame, frame_addr, 1);
3660 force_sig(TARGET_SIGSEGV);
3661}
3662
3663static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003664 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003665 target_sigset_t *set, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003666{
3667 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3668}
3669
Andreas Färber05390242012-02-25 03:37:53 +01003670long do_sigreturn(CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003671{
3672 struct target_signal_frame *frame;
3673 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003674 target_sigset_t target_set;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003675 sigset_t set;
3676 int i;
3677
3678 frame_addr = env->regs[R_SP];
3679 /* Make sure the guest isn't playing games. */
3680 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3681 goto badframe;
3682
3683 /* Restore blocked signals */
Richard Hendersonf711df62010-11-22 14:57:52 -08003684 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003685 goto badframe;
3686 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3687 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3688 goto badframe;
3689 }
3690 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00003691 do_sigprocmask(SIG_SETMASK, &set, NULL);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003692
Richard Hendersonf711df62010-11-22 14:57:52 -08003693 restore_sigcontext(&frame->uc.tuc_mcontext, env);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003694 /* We got here through a sigreturn syscall, our path back is via an
3695 rtb insn so setup r14 for that. */
3696 env->regs[14] = env->sregs[SR_PC];
3697
3698 unlock_user_struct(frame, frame_addr, 0);
3699 return env->regs[10];
3700 badframe:
3701 unlock_user_struct(frame, frame_addr, 0);
3702 force_sig(TARGET_SIGSEGV);
3703}
3704
Andreas Färber05390242012-02-25 03:37:53 +01003705long do_rt_sigreturn(CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003706{
3707 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3708 return -TARGET_ENOSYS;
3709}
3710
edgar_iglb6d3abd2008-02-28 11:29:27 +00003711#elif defined(TARGET_CRIS)
3712
3713struct target_sigcontext {
3714 struct target_pt_regs regs; /* needs to be first */
3715 uint32_t oldmask;
3716 uint32_t usp; /* usp before stacking this gunk on it */
3717};
3718
3719/* Signal frames. */
3720struct target_signal_frame {
3721 struct target_sigcontext sc;
3722 uint32_t extramask[TARGET_NSIG_WORDS - 1];
Stefan Weil8cfc1142014-02-01 09:41:09 +01003723 uint16_t retcode[4]; /* Trampoline code. */
edgar_iglb6d3abd2008-02-28 11:29:27 +00003724};
3725
3726struct rt_signal_frame {
Richard W.M. Jones02d2bd52012-07-05 03:32:44 +00003727 siginfo_t *pinfo;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003728 void *puc;
Richard W.M. Jones02d2bd52012-07-05 03:32:44 +00003729 siginfo_t info;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003730 struct ucontext uc;
Stefan Weil8cfc1142014-02-01 09:41:09 +01003731 uint16_t retcode[4]; /* Trampoline code. */
edgar_iglb6d3abd2008-02-28 11:29:27 +00003732};
3733
Andreas Färber05390242012-02-25 03:37:53 +01003734static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003735{
edgar_igl9664d922008-03-03 22:23:53 +00003736 __put_user(env->regs[0], &sc->regs.r0);
3737 __put_user(env->regs[1], &sc->regs.r1);
3738 __put_user(env->regs[2], &sc->regs.r2);
3739 __put_user(env->regs[3], &sc->regs.r3);
3740 __put_user(env->regs[4], &sc->regs.r4);
3741 __put_user(env->regs[5], &sc->regs.r5);
3742 __put_user(env->regs[6], &sc->regs.r6);
3743 __put_user(env->regs[7], &sc->regs.r7);
3744 __put_user(env->regs[8], &sc->regs.r8);
3745 __put_user(env->regs[9], &sc->regs.r9);
3746 __put_user(env->regs[10], &sc->regs.r10);
3747 __put_user(env->regs[11], &sc->regs.r11);
3748 __put_user(env->regs[12], &sc->regs.r12);
3749 __put_user(env->regs[13], &sc->regs.r13);
3750 __put_user(env->regs[14], &sc->usp);
3751 __put_user(env->regs[15], &sc->regs.acr);
3752 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3753 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3754 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003755}
edgar_igl9664d922008-03-03 22:23:53 +00003756
Andreas Färber05390242012-02-25 03:37:53 +01003757static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003758{
edgar_igl9664d922008-03-03 22:23:53 +00003759 __get_user(env->regs[0], &sc->regs.r0);
3760 __get_user(env->regs[1], &sc->regs.r1);
3761 __get_user(env->regs[2], &sc->regs.r2);
3762 __get_user(env->regs[3], &sc->regs.r3);
3763 __get_user(env->regs[4], &sc->regs.r4);
3764 __get_user(env->regs[5], &sc->regs.r5);
3765 __get_user(env->regs[6], &sc->regs.r6);
3766 __get_user(env->regs[7], &sc->regs.r7);
3767 __get_user(env->regs[8], &sc->regs.r8);
3768 __get_user(env->regs[9], &sc->regs.r9);
3769 __get_user(env->regs[10], &sc->regs.r10);
3770 __get_user(env->regs[11], &sc->regs.r11);
3771 __get_user(env->regs[12], &sc->regs.r12);
3772 __get_user(env->regs[13], &sc->regs.r13);
3773 __get_user(env->regs[14], &sc->usp);
3774 __get_user(env->regs[15], &sc->regs.acr);
3775 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3776 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3777 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003778}
3779
Andreas Färber05390242012-02-25 03:37:53 +01003780static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003781{
edgar_igl9664d922008-03-03 22:23:53 +00003782 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003783 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00003784 sp = (env->regs[R_SP] & ~3);
3785 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003786}
3787
pbrook624f7972008-05-31 16:11:38 +00003788static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003789 target_sigset_t *set, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003790{
3791 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003792 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003793 int err = 0;
3794 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003795
edgar_igl9664d922008-03-03 22:23:53 +00003796 frame_addr = get_sigframe(env, sizeof *frame);
3797 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003798 goto badframe;
3799
3800 /*
3801 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3802 * use this trampoline anymore but it sets it up for GDB.
3803 * In QEMU, using the trampoline simplifies things a bit so we use it.
3804 *
3805 * This is movu.w __NR_sigreturn, r9; break 13;
3806 */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003807 __put_user(0x9c5f, frame->retcode+0);
3808 __put_user(TARGET_NR_sigreturn,
3809 frame->retcode + 1);
3810 __put_user(0xe93d, frame->retcode + 2);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003811
3812 /* Save the mask. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003813 __put_user(set->sig[0], &frame->sc.oldmask);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003814 if (err)
3815 goto badframe;
3816
3817 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3818 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3819 goto badframe;
3820 }
3821
3822 setup_sigcontext(&frame->sc, env);
3823
3824 /* Move the stack and setup the arguments for the handler. */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003825 env->regs[R_SP] = frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003826 env->regs[10] = sig;
pbrook624f7972008-05-31 16:11:38 +00003827 env->pc = (unsigned long) ka->_sa_handler;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003828 /* Link SRP so the guest returns through the trampoline. */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003829 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003830
edgar_igl9664d922008-03-03 22:23:53 +00003831 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003832 return;
3833 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003834 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003835 force_sig(TARGET_SIGSEGV);
3836}
3837
pbrook624f7972008-05-31 16:11:38 +00003838static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003839 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003840 target_sigset_t *set, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003841{
3842 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3843}
3844
Andreas Färber05390242012-02-25 03:37:53 +01003845long do_sigreturn(CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003846{
3847 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003848 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003849 target_sigset_t target_set;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003850 sigset_t set;
3851 int i;
3852
edgar_igl9664d922008-03-03 22:23:53 +00003853 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003854 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003855 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003856 goto badframe;
3857
3858 /* Restore blocked signals */
3859 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3860 goto badframe;
3861 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3862 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3863 goto badframe;
3864 }
3865 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00003866 do_sigprocmask(SIG_SETMASK, &set, NULL);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003867
3868 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003869 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003870 return env->regs[10];
3871 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003872 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003873 force_sig(TARGET_SIGSEGV);
3874}
3875
Andreas Färber05390242012-02-25 03:37:53 +01003876long do_rt_sigreturn(CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003877{
3878 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3879 return -TARGET_ENOSYS;
3880}
thsc3b5bc82007-12-02 06:31:25 +00003881
Jia Liud9627832012-07-20 15:50:52 +08003882#elif defined(TARGET_OPENRISC)
3883
3884struct target_sigcontext {
3885 struct target_pt_regs regs;
3886 abi_ulong oldmask;
3887 abi_ulong usp;
3888};
3889
3890struct target_ucontext {
3891 abi_ulong tuc_flags;
3892 abi_ulong tuc_link;
3893 target_stack_t tuc_stack;
3894 struct target_sigcontext tuc_mcontext;
3895 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3896};
3897
3898struct target_rt_sigframe {
3899 abi_ulong pinfo;
3900 uint64_t puc;
3901 struct target_siginfo info;
3902 struct target_sigcontext sc;
3903 struct target_ucontext uc;
3904 unsigned char retcode[16]; /* trampoline code */
3905};
3906
3907/* This is the asm-generic/ucontext.h version */
3908#if 0
3909static int restore_sigcontext(CPUOpenRISCState *regs,
3910 struct target_sigcontext *sc)
3911{
3912 unsigned int err = 0;
3913 unsigned long old_usp;
3914
3915 /* Alwys make any pending restarted system call return -EINTR */
3916 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3917
3918 /* restore the regs from &sc->regs (same as sc, since regs is first)
3919 * (sc is already checked for VERIFY_READ since the sigframe was
3920 * checked in sys_sigreturn previously)
3921 */
3922
3923 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3924 goto badframe;
3925 }
3926
3927 /* make sure the U-flag is set so user-mode cannot fool us */
3928
3929 regs->sr &= ~SR_SM;
3930
3931 /* restore the old USP as it was before we stacked the sc etc.
3932 * (we cannot just pop the sigcontext since we aligned the sp and
3933 * stuff after pushing it)
3934 */
3935
Riku Voipio1d8b5122014-04-23 10:26:05 +03003936 __get_user(old_usp, &sc->usp);
Jia Liud9627832012-07-20 15:50:52 +08003937 phx_signal("old_usp 0x%lx", old_usp);
3938
3939 __PHX__ REALLY /* ??? */
3940 wrusp(old_usp);
3941 regs->gpr[1] = old_usp;
3942
3943 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3944 * after this completes, but we don't use that mechanism. maybe we can
3945 * use it now ?
3946 */
3947
3948 return err;
3949
3950badframe:
3951 return 1;
3952}
3953#endif
3954
3955/* Set up a signal frame. */
3956
3957static int setup_sigcontext(struct target_sigcontext *sc,
3958 CPUOpenRISCState *regs,
3959 unsigned long mask)
3960{
3961 int err = 0;
3962 unsigned long usp = regs->gpr[1];
3963
3964 /* copy the regs. they are first in sc so we can use sc directly */
3965
Riku Voipio1d8b5122014-04-23 10:26:05 +03003966 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
Jia Liud9627832012-07-20 15:50:52 +08003967
3968 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3969 the signal handler. The frametype will be restored to its previous
3970 value in restore_sigcontext. */
3971 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3972
3973 /* then some other stuff */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003974 __put_user(mask, &sc->oldmask);
3975 __put_user(usp, &sc->usp); return err;
Jia Liud9627832012-07-20 15:50:52 +08003976}
3977
3978static inline unsigned long align_sigframe(unsigned long sp)
3979{
3980 unsigned long i;
3981 i = sp & ~3UL;
3982 return i;
3983}
3984
3985static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3986 CPUOpenRISCState *regs,
3987 size_t frame_size)
3988{
3989 unsigned long sp = regs->gpr[1];
3990 int onsigstack = on_sig_stack(sp);
3991
3992 /* redzone */
3993 /* This is the X/Open sanctioned signal stack switching. */
3994 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
3995 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3996 }
3997
3998 sp = align_sigframe(sp - frame_size);
3999
4000 /*
4001 * If we are on the alternate signal stack and would overflow it, don't.
4002 * Return an always-bogus address instead so we will die with SIGSEGV.
4003 */
4004
4005 if (onsigstack && !likely(on_sig_stack(sp))) {
4006 return -1L;
4007 }
4008
4009 return sp;
4010}
4011
4012static void setup_frame(int sig, struct target_sigaction *ka,
4013 target_sigset_t *set, CPUOpenRISCState *env)
4014{
4015 qemu_log("Not implement.\n");
4016}
4017
4018static void setup_rt_frame(int sig, struct target_sigaction *ka,
4019 target_siginfo_t *info,
4020 target_sigset_t *set, CPUOpenRISCState *env)
4021{
4022 int err = 0;
4023 abi_ulong frame_addr;
4024 unsigned long return_ip;
4025 struct target_rt_sigframe *frame;
4026 abi_ulong info_addr, uc_addr;
4027
Jia Liud9627832012-07-20 15:50:52 +08004028 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4029 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4030 goto give_sigsegv;
4031 }
4032
4033 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
Riku Voipio1d8b5122014-04-23 10:26:05 +03004034 __put_user(info_addr, &frame->pinfo);
Jia Liud9627832012-07-20 15:50:52 +08004035 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
Riku Voipio1d8b5122014-04-23 10:26:05 +03004036 __put_user(uc_addr, &frame->puc);
Jia Liud9627832012-07-20 15:50:52 +08004037
4038 if (ka->sa_flags & SA_SIGINFO) {
Riku Voipiob0fd8d12014-04-23 10:46:13 +03004039 copy_siginfo_to_user(&frame->info, info);
Jia Liud9627832012-07-20 15:50:52 +08004040 }
4041
4042 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
Riku Voipio1d8b5122014-04-23 10:26:05 +03004043 __put_user(0, &frame->uc.tuc_flags);
4044 __put_user(0, &frame->uc.tuc_link);
4045 __put_user(target_sigaltstack_used.ss_sp,
4046 &frame->uc.tuc_stack.ss_sp);
4047 __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
4048 __put_user(target_sigaltstack_used.ss_size,
4049 &frame->uc.tuc_stack.ss_size);
Jia Liud9627832012-07-20 15:50:52 +08004050 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4051
4052 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
4053
4054 if (err) {
4055 goto give_sigsegv;
4056 }
4057
4058 /* trampoline - the desired return ip is the retcode itself */
4059 return_ip = (unsigned long)&frame->retcode;
4060 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
Riku Voipio1d8b5122014-04-23 10:26:05 +03004061 __put_user(0xa960, (short *)(frame->retcode + 0));
4062 __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
4063 __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
4064 __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
Jia Liud9627832012-07-20 15:50:52 +08004065
4066 if (err) {
4067 goto give_sigsegv;
4068 }
4069
4070 /* TODO what is the current->exec_domain stuff and invmap ? */
4071
4072 /* Set up registers for signal handler */
4073 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
4074 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
4075 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
4076 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
4077 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
4078
4079 /* actually move the usp to reflect the stacked frame */
4080 env->gpr[1] = (unsigned long)frame;
4081
4082 return;
4083
4084give_sigsegv:
4085 unlock_user_struct(frame, frame_addr, 1);
4086 if (sig == TARGET_SIGSEGV) {
4087 ka->_sa_handler = TARGET_SIG_DFL;
4088 }
4089 force_sig(TARGET_SIGSEGV);
4090}
4091
4092long do_sigreturn(CPUOpenRISCState *env)
4093{
4094
4095 qemu_log("do_sigreturn: not implemented\n");
4096 return -TARGET_ENOSYS;
4097}
4098
4099long do_rt_sigreturn(CPUOpenRISCState *env)
4100{
4101 qemu_log("do_rt_sigreturn: not implemented\n");
4102 return -TARGET_ENOSYS;
4103}
4104/* TARGET_OPENRISC */
4105
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004106#elif defined(TARGET_S390X)
4107
4108#define __NUM_GPRS 16
4109#define __NUM_FPRS 16
4110#define __NUM_ACRS 16
4111
4112#define S390_SYSCALL_SIZE 2
4113#define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4114
4115#define _SIGCONTEXT_NSIG 64
4116#define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4117#define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4118#define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4119#define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4120#define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4121
4122typedef struct {
4123 target_psw_t psw;
4124 target_ulong gprs[__NUM_GPRS];
4125 unsigned int acrs[__NUM_ACRS];
4126} target_s390_regs_common;
4127
4128typedef struct {
4129 unsigned int fpc;
4130 double fprs[__NUM_FPRS];
4131} target_s390_fp_regs;
4132
4133typedef struct {
4134 target_s390_regs_common regs;
4135 target_s390_fp_regs fpregs;
4136} target_sigregs;
4137
4138struct target_sigcontext {
4139 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4140 target_sigregs *sregs;
4141};
4142
4143typedef struct {
4144 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4145 struct target_sigcontext sc;
4146 target_sigregs sregs;
4147 int signo;
4148 uint8_t retcode[S390_SYSCALL_SIZE];
4149} sigframe;
4150
4151struct target_ucontext {
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004152 target_ulong tuc_flags;
4153 struct target_ucontext *tuc_link;
4154 target_stack_t tuc_stack;
4155 target_sigregs tuc_mcontext;
4156 target_sigset_t tuc_sigmask; /* mask last for extensibility */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004157};
4158
4159typedef struct {
4160 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4161 uint8_t retcode[S390_SYSCALL_SIZE];
4162 struct target_siginfo info;
4163 struct target_ucontext uc;
4164} rt_sigframe;
4165
4166static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01004167get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004168{
4169 abi_ulong sp;
4170
4171 /* Default to using normal stack */
4172 sp = env->regs[15];
4173
4174 /* This is the X/Open sanctioned signal stack switching. */
4175 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4176 if (!sas_ss_flags(sp)) {
4177 sp = target_sigaltstack_used.ss_sp +
4178 target_sigaltstack_used.ss_size;
4179 }
4180 }
4181
4182 /* This is the legacy signal stack switching. */
4183 else if (/* FIXME !user_mode(regs) */ 0 &&
4184 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4185 ka->sa_restorer) {
4186 sp = (abi_ulong) ka->sa_restorer;
4187 }
4188
4189 return (sp - frame_size) & -8ul;
4190}
4191
Andreas Färber05390242012-02-25 03:37:53 +01004192static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004193{
4194 int i;
4195 //save_access_regs(current->thread.acrs); FIXME
4196
4197 /* Copy a 'clean' PSW mask to the user to avoid leaking
4198 information about whether PER is currently on. */
4199 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4200 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4201 for (i = 0; i < 16; i++) {
4202 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4203 }
4204 for (i = 0; i < 16; i++) {
4205 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4206 }
4207 /*
4208 * We have to store the fp registers to current->thread.fp_regs
4209 * to merge them with the emulated registers.
4210 */
4211 //save_fp_regs(&current->thread.fp_regs); FIXME
4212 for (i = 0; i < 16; i++) {
4213 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
4214 }
4215}
4216
4217static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01004218 target_sigset_t *set, CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004219{
4220 sigframe *frame;
4221 abi_ulong frame_addr;
4222
4223 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4224 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4225 (unsigned long long)frame_addr);
4226 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4227 goto give_sigsegv;
4228 }
4229
4230 qemu_log("%s: 1\n", __FUNCTION__);
4231 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
4232 goto give_sigsegv;
4233 }
4234
4235 save_sigregs(env, &frame->sregs);
4236
4237 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4238 (abi_ulong *)&frame->sc.sregs);
4239
4240 /* Set up to return from userspace. If provided, use a stub
4241 already in userspace. */
4242 if (ka->sa_flags & TARGET_SA_RESTORER) {
4243 env->regs[14] = (unsigned long)
4244 ka->sa_restorer | PSW_ADDR_AMODE;
4245 } else {
4246 env->regs[14] = (unsigned long)
4247 frame->retcode | PSW_ADDR_AMODE;
4248 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4249 (uint16_t *)(frame->retcode)))
4250 goto give_sigsegv;
4251 }
4252
4253 /* Set up backchain. */
4254 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4255 goto give_sigsegv;
4256 }
4257
4258 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004259 env->regs[15] = frame_addr;
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004260 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4261
4262 env->regs[2] = sig; //map_signal(sig);
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004263 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004264
4265 /* We forgot to include these in the sigcontext.
4266 To avoid breaking binary compatibility, they are passed as args. */
4267 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4268 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4269
4270 /* Place signal number on stack to allow backtrace from handler. */
4271 if (__put_user(env->regs[2], (int *) &frame->signo)) {
4272 goto give_sigsegv;
4273 }
4274 unlock_user_struct(frame, frame_addr, 1);
4275 return;
4276
4277give_sigsegv:
4278 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4279 unlock_user_struct(frame, frame_addr, 1);
4280 force_sig(TARGET_SIGSEGV);
4281}
4282
4283static void setup_rt_frame(int sig, struct target_sigaction *ka,
4284 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01004285 target_sigset_t *set, CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004286{
4287 int i;
4288 rt_sigframe *frame;
4289 abi_ulong frame_addr;
4290
4291 frame_addr = get_sigframe(ka, env, sizeof *frame);
4292 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4293 (unsigned long long)frame_addr);
4294 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4295 goto give_sigsegv;
4296 }
4297
4298 qemu_log("%s: 1\n", __FUNCTION__);
Riku Voipiob0fd8d12014-04-23 10:46:13 +03004299 copy_siginfo_to_user(&frame->info, info);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004300
4301 /* Create the ucontext. */
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004302 __put_user(0, &frame->uc.tuc_flags);
4303 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4304 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004305 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004306 &frame->uc.tuc_stack.ss_flags);
4307 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4308 save_sigregs(env, &frame->uc.tuc_mcontext);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004309 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4310 __put_user((abi_ulong)set->sig[i],
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004311 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004312 }
4313
4314 /* Set up to return from userspace. If provided, use a stub
4315 already in userspace. */
4316 if (ka->sa_flags & TARGET_SA_RESTORER) {
4317 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4318 } else {
4319 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4320 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4321 (uint16_t *)(frame->retcode))) {
4322 goto give_sigsegv;
4323 }
4324 }
4325
4326 /* Set up backchain. */
4327 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4328 goto give_sigsegv;
4329 }
4330
4331 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004332 env->regs[15] = frame_addr;
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004333 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4334
4335 env->regs[2] = sig; //map_signal(sig);
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004336 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4337 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004338 return;
4339
4340give_sigsegv:
4341 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4342 unlock_user_struct(frame, frame_addr, 1);
4343 force_sig(TARGET_SIGSEGV);
4344}
4345
4346static int
Andreas Färber05390242012-02-25 03:37:53 +01004347restore_sigregs(CPUS390XState *env, target_sigregs *sc)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004348{
4349 int err = 0;
4350 int i;
4351
4352 for (i = 0; i < 16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03004353 __get_user(env->regs[i], &sc->regs.gprs[i]);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004354 }
4355
Riku Voipio1d8b5122014-04-23 10:26:05 +03004356 __get_user(env->psw.mask, &sc->regs.psw.mask);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004357 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4358 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4359 (unsigned long long)env->psw.addr);
Riku Voipio1d8b5122014-04-23 10:26:05 +03004360 __get_user(env->psw.addr, &sc->regs.psw.addr);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004361 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4362
4363 for (i = 0; i < 16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03004364 __get_user(env->aregs[i], &sc->regs.acrs[i]);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004365 }
4366 for (i = 0; i < 16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03004367 __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004368 }
4369
4370 return err;
4371}
4372
Andreas Färber05390242012-02-25 03:37:53 +01004373long do_sigreturn(CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004374{
4375 sigframe *frame;
4376 abi_ulong frame_addr = env->regs[15];
4377 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4378 (unsigned long long)frame_addr);
4379 target_sigset_t target_set;
4380 sigset_t set;
4381
4382 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4383 goto badframe;
4384 }
4385 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4386 goto badframe;
4387 }
4388
4389 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00004390 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004391
4392 if (restore_sigregs(env, &frame->sregs)) {
4393 goto badframe;
4394 }
4395
4396 unlock_user_struct(frame, frame_addr, 0);
4397 return env->regs[2];
4398
4399badframe:
4400 unlock_user_struct(frame, frame_addr, 0);
4401 force_sig(TARGET_SIGSEGV);
4402 return 0;
4403}
4404
Andreas Färber05390242012-02-25 03:37:53 +01004405long do_rt_sigreturn(CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004406{
4407 rt_sigframe *frame;
4408 abi_ulong frame_addr = env->regs[15];
4409 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4410 (unsigned long long)frame_addr);
4411 sigset_t set;
4412
4413 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4414 goto badframe;
4415 }
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004416 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004417
Alex Barcelo1c275922014-03-14 14:36:55 +00004418 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004419
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004420 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004421 goto badframe;
4422 }
4423
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004424 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004425 get_sp_from_cpustate(env)) == -EFAULT) {
4426 goto badframe;
4427 }
4428 unlock_user_struct(frame, frame_addr, 0);
4429 return env->regs[2];
4430
4431badframe:
4432 unlock_user_struct(frame, frame_addr, 0);
4433 force_sig(TARGET_SIGSEGV);
4434 return 0;
4435}
4436
Nathan Froydbcd49332009-05-12 19:13:18 -07004437#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4438
4439/* FIXME: Many of the structures are defined for both PPC and PPC64, but
4440 the signal handling is different enough that we haven't implemented
4441 support for PPC64 yet. Hence the restriction above.
4442
4443 There are various #if'd blocks for code for TARGET_PPC64. These
4444 blocks should go away so that we can successfully run 32-bit and
4445 64-bit binaries on a QEMU configured for PPC64. */
4446
4447/* Size of dummy stack frame allocated when calling signal handler.
4448 See arch/powerpc/include/asm/ptrace.h. */
4449#if defined(TARGET_PPC64)
4450#define SIGNAL_FRAMESIZE 128
4451#else
4452#define SIGNAL_FRAMESIZE 64
4453#endif
4454
4455/* See arch/powerpc/include/asm/sigcontext.h. */
4456struct target_sigcontext {
4457 target_ulong _unused[4];
4458 int32_t signal;
4459#if defined(TARGET_PPC64)
4460 int32_t pad0;
4461#endif
4462 target_ulong handler;
4463 target_ulong oldmask;
4464 target_ulong regs; /* struct pt_regs __user * */
4465 /* TODO: PPC64 includes extra bits here. */
4466};
4467
4468/* Indices for target_mcontext.mc_gregs, below.
4469 See arch/powerpc/include/asm/ptrace.h for details. */
4470enum {
4471 TARGET_PT_R0 = 0,
4472 TARGET_PT_R1 = 1,
4473 TARGET_PT_R2 = 2,
4474 TARGET_PT_R3 = 3,
4475 TARGET_PT_R4 = 4,
4476 TARGET_PT_R5 = 5,
4477 TARGET_PT_R6 = 6,
4478 TARGET_PT_R7 = 7,
4479 TARGET_PT_R8 = 8,
4480 TARGET_PT_R9 = 9,
4481 TARGET_PT_R10 = 10,
4482 TARGET_PT_R11 = 11,
4483 TARGET_PT_R12 = 12,
4484 TARGET_PT_R13 = 13,
4485 TARGET_PT_R14 = 14,
4486 TARGET_PT_R15 = 15,
4487 TARGET_PT_R16 = 16,
4488 TARGET_PT_R17 = 17,
4489 TARGET_PT_R18 = 18,
4490 TARGET_PT_R19 = 19,
4491 TARGET_PT_R20 = 20,
4492 TARGET_PT_R21 = 21,
4493 TARGET_PT_R22 = 22,
4494 TARGET_PT_R23 = 23,
4495 TARGET_PT_R24 = 24,
4496 TARGET_PT_R25 = 25,
4497 TARGET_PT_R26 = 26,
4498 TARGET_PT_R27 = 27,
4499 TARGET_PT_R28 = 28,
4500 TARGET_PT_R29 = 29,
4501 TARGET_PT_R30 = 30,
4502 TARGET_PT_R31 = 31,
4503 TARGET_PT_NIP = 32,
4504 TARGET_PT_MSR = 33,
4505 TARGET_PT_ORIG_R3 = 34,
4506 TARGET_PT_CTR = 35,
4507 TARGET_PT_LNK = 36,
4508 TARGET_PT_XER = 37,
4509 TARGET_PT_CCR = 38,
4510 /* Yes, there are two registers with #39. One is 64-bit only. */
4511 TARGET_PT_MQ = 39,
4512 TARGET_PT_SOFTE = 39,
4513 TARGET_PT_TRAP = 40,
4514 TARGET_PT_DAR = 41,
4515 TARGET_PT_DSISR = 42,
4516 TARGET_PT_RESULT = 43,
4517 TARGET_PT_REGS_COUNT = 44
4518};
4519
4520/* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4521 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4522struct target_mcontext {
4523 target_ulong mc_gregs[48];
4524 /* Includes fpscr. */
4525 uint64_t mc_fregs[33];
4526 target_ulong mc_pad[2];
4527 /* We need to handle Altivec and SPE at the same time, which no
4528 kernel needs to do. Fortunately, the kernel defines this bit to
4529 be Altivec-register-large all the time, rather than trying to
4530 twiddle it based on the specific platform. */
4531 union {
4532 /* SPE vector registers. One extra for SPEFSCR. */
4533 uint32_t spe[33];
4534 /* Altivec vector registers. The packing of VSCR and VRSAVE
4535 varies depending on whether we're PPC64 or not: PPC64 splits
4536 them apart; PPC32 stuffs them together. */
4537#if defined(TARGET_PPC64)
malc3efa9a62009-07-18 13:10:12 +04004538#define QEMU_NVRREG 34
Nathan Froydbcd49332009-05-12 19:13:18 -07004539#else
malc3efa9a62009-07-18 13:10:12 +04004540#define QEMU_NVRREG 33
Nathan Froydbcd49332009-05-12 19:13:18 -07004541#endif
Anthony Liguoric227f092009-10-01 16:12:16 -05004542 ppc_avr_t altivec[QEMU_NVRREG];
malc3efa9a62009-07-18 13:10:12 +04004543#undef QEMU_NVRREG
Nathan Froydbcd49332009-05-12 19:13:18 -07004544 } mc_vregs __attribute__((__aligned__(16)));
4545};
4546
4547struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02004548 target_ulong tuc_flags;
4549 target_ulong tuc_link; /* struct ucontext __user * */
4550 struct target_sigaltstack tuc_stack;
Nathan Froydbcd49332009-05-12 19:13:18 -07004551#if !defined(TARGET_PPC64)
Aurelien Jarno60e99242010-03-29 02:12:51 +02004552 int32_t tuc_pad[7];
4553 target_ulong tuc_regs; /* struct mcontext __user *
Nathan Froydbcd49332009-05-12 19:13:18 -07004554 points to uc_mcontext field */
4555#endif
Aurelien Jarno60e99242010-03-29 02:12:51 +02004556 target_sigset_t tuc_sigmask;
Nathan Froydbcd49332009-05-12 19:13:18 -07004557#if defined(TARGET_PPC64)
Anthony Liguoric227f092009-10-01 16:12:16 -05004558 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
Aurelien Jarno60e99242010-03-29 02:12:51 +02004559 struct target_sigcontext tuc_mcontext;
Nathan Froydbcd49332009-05-12 19:13:18 -07004560#else
Aurelien Jarno60e99242010-03-29 02:12:51 +02004561 int32_t tuc_maskext[30];
4562 int32_t tuc_pad2[3];
4563 struct target_mcontext tuc_mcontext;
Nathan Froydbcd49332009-05-12 19:13:18 -07004564#endif
4565};
4566
4567/* See arch/powerpc/kernel/signal_32.c. */
4568struct target_sigframe {
4569 struct target_sigcontext sctx;
4570 struct target_mcontext mctx;
4571 int32_t abigap[56];
4572};
4573
4574struct target_rt_sigframe {
4575 struct target_siginfo info;
4576 struct target_ucontext uc;
4577 int32_t abigap[56];
4578};
4579
4580/* We use the mc_pad field for the signal return trampoline. */
4581#define tramp mc_pad
4582
4583/* See arch/powerpc/kernel/signal.c. */
4584static target_ulong get_sigframe(struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01004585 CPUPPCState *env,
Nathan Froydbcd49332009-05-12 19:13:18 -07004586 int frame_size)
4587{
4588 target_ulong oldsp, newsp;
4589
4590 oldsp = env->gpr[1];
4591
4592 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
Alex Barcelo32a20032012-02-09 23:55:46 +00004593 (sas_ss_flags(oldsp) == 0)) {
Nathan Froydbcd49332009-05-12 19:13:18 -07004594 oldsp = (target_sigaltstack_used.ss_sp
4595 + target_sigaltstack_used.ss_size);
4596 }
4597
4598 newsp = (oldsp - frame_size) & ~0xFUL;
4599
4600 return newsp;
4601}
4602
Andreas Färber05390242012-02-25 03:37:53 +01004603static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
Nathan Froydbcd49332009-05-12 19:13:18 -07004604 int sigret)
4605{
4606 target_ulong msr = env->msr;
4607 int i;
4608 target_ulong ccr = 0;
4609
4610 /* In general, the kernel attempts to be intelligent about what it
4611 needs to save for Altivec/FP/SPE registers. We don't care that
4612 much, so we just go ahead and save everything. */
4613
4614 /* Save general registers. */
4615 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4616 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4617 return 1;
4618 }
4619 }
4620 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4621 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4622 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4623 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4624 return 1;
4625
4626 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4627 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4628 }
4629 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4630 return 1;
4631
4632 /* Save Altivec registers if necessary. */
4633 if (env->insns_flags & PPC_ALTIVEC) {
4634 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004635 ppc_avr_t *avr = &env->avr[i];
4636 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07004637
4638 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4639 __put_user(avr->u64[1], &vreg->u64[1])) {
4640 return 1;
4641 }
4642 }
4643 /* Set MSR_VR in the saved MSR value to indicate that
4644 frame->mc_vregs contains valid data. */
4645 msr |= MSR_VR;
4646 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4647 &frame->mc_vregs.altivec[32].u32[3]))
4648 return 1;
4649 }
4650
4651 /* Save floating point registers. */
4652 if (env->insns_flags & PPC_FLOAT) {
4653 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4654 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4655 return 1;
4656 }
4657 }
4658 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4659 return 1;
4660 }
4661
4662 /* Save SPE registers. The kernel only saves the high half. */
4663 if (env->insns_flags & PPC_SPE) {
4664#if defined(TARGET_PPC64)
4665 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4666 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4667 return 1;
4668 }
4669 }
4670#else
4671 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4672 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4673 return 1;
4674 }
4675 }
4676#endif
4677 /* Set MSR_SPE in the saved MSR value to indicate that
4678 frame->mc_vregs contains valid data. */
4679 msr |= MSR_SPE;
4680 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4681 return 1;
4682 }
4683
4684 /* Store MSR. */
4685 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4686 return 1;
4687
4688 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4689 if (sigret) {
4690 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4691 __put_user(0x44000002UL, &frame->tramp[1])) {
4692 return 1;
4693 }
4694 }
4695
4696 return 0;
4697}
4698
Andreas Färber05390242012-02-25 03:37:53 +01004699static int restore_user_regs(CPUPPCState *env,
Nathan Froydbcd49332009-05-12 19:13:18 -07004700 struct target_mcontext *frame, int sig)
4701{
4702 target_ulong save_r2 = 0;
4703 target_ulong msr;
4704 target_ulong ccr;
4705
4706 int i;
4707
4708 if (!sig) {
4709 save_r2 = env->gpr[2];
4710 }
4711
4712 /* Restore general registers. */
4713 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4714 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4715 return 1;
4716 }
4717 }
4718 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4719 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4720 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4721 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4722 return 1;
4723 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4724 return 1;
4725
4726 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4727 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4728 }
4729
4730 if (!sig) {
4731 env->gpr[2] = save_r2;
4732 }
4733 /* Restore MSR. */
4734 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4735 return 1;
4736
4737 /* If doing signal return, restore the previous little-endian mode. */
4738 if (sig)
4739 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4740
4741 /* Restore Altivec registers if necessary. */
4742 if (env->insns_flags & PPC_ALTIVEC) {
4743 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004744 ppc_avr_t *avr = &env->avr[i];
4745 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07004746
4747 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4748 __get_user(avr->u64[1], &vreg->u64[1])) {
4749 return 1;
4750 }
4751 }
4752 /* Set MSR_VEC in the saved MSR value to indicate that
4753 frame->mc_vregs contains valid data. */
4754 if (__get_user(env->spr[SPR_VRSAVE],
4755 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4756 return 1;
4757 }
4758
4759 /* Restore floating point registers. */
4760 if (env->insns_flags & PPC_FLOAT) {
4761 uint64_t fpscr;
4762 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4763 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4764 return 1;
4765 }
4766 }
4767 if (__get_user(fpscr, &frame->mc_fregs[32]))
4768 return 1;
4769 env->fpscr = (uint32_t) fpscr;
4770 }
4771
4772 /* Save SPE registers. The kernel only saves the high half. */
4773 if (env->insns_flags & PPC_SPE) {
4774#if defined(TARGET_PPC64)
4775 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4776 uint32_t hi;
4777
4778 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4779 return 1;
4780 }
4781 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4782 }
4783#else
4784 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4785 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4786 return 1;
4787 }
4788 }
4789#endif
4790 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4791 return 1;
4792 }
4793
4794 return 0;
4795}
4796
4797static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01004798 target_sigset_t *set, CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004799{
4800 struct target_sigframe *frame;
4801 struct target_sigcontext *sc;
4802 target_ulong frame_addr, newsp;
4803 int err = 0;
4804 int signal;
4805
4806 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4807 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4808 goto sigsegv;
4809 sc = &frame->sctx;
4810
4811 signal = current_exec_domain_sig(sig);
4812
Riku Voipio1d8b5122014-04-23 10:26:05 +03004813 __put_user(ka->_sa_handler, &sc->handler);
4814 __put_user(set->sig[0], &sc->oldmask);
Nathan Froydbcd49332009-05-12 19:13:18 -07004815#if defined(TARGET_PPC64)
Riku Voipio1d8b5122014-04-23 10:26:05 +03004816 __put_user(set->sig[0] >> 32, &sc->_unused[3]);
Nathan Froydbcd49332009-05-12 19:13:18 -07004817#else
Riku Voipio1d8b5122014-04-23 10:26:05 +03004818 __put_user(set->sig[1], &sc->_unused[3]);
Nathan Froydbcd49332009-05-12 19:13:18 -07004819#endif
Riku Voipio1d8b5122014-04-23 10:26:05 +03004820 __put_user(h2g(&frame->mctx), &sc->regs);
4821 __put_user(sig, &sc->signal);
Nathan Froydbcd49332009-05-12 19:13:18 -07004822
4823 /* Save user regs. */
4824 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4825
4826 /* The kernel checks for the presence of a VDSO here. We don't
4827 emulate a vdso, so use a sigreturn system call. */
4828 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4829
4830 /* Turn off all fp exceptions. */
4831 env->fpscr = 0;
4832
4833 /* Create a stack frame for the caller of the handler. */
4834 newsp = frame_addr - SIGNAL_FRAMESIZE;
Samuel Seaybeb526b2013-01-02 10:53:46 +00004835 err |= put_user(env->gpr[1], newsp, target_ulong);
Nathan Froydbcd49332009-05-12 19:13:18 -07004836
4837 if (err)
4838 goto sigsegv;
4839
4840 /* Set up registers for signal handler. */
4841 env->gpr[1] = newsp;
4842 env->gpr[3] = signal;
Samuel Seay61993a62013-01-04 14:35:48 +00004843 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
Nathan Froydbcd49332009-05-12 19:13:18 -07004844 env->nip = (target_ulong) ka->_sa_handler;
4845 /* Signal handlers are entered in big-endian mode. */
4846 env->msr &= ~MSR_LE;
4847
4848 unlock_user_struct(frame, frame_addr, 1);
4849 return;
4850
4851sigsegv:
4852 unlock_user_struct(frame, frame_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004853 qemu_log("segfaulting from setup_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004854 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004855}
4856
4857static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004858 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01004859 target_sigset_t *set, CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004860{
4861 struct target_rt_sigframe *rt_sf;
4862 struct target_mcontext *frame;
4863 target_ulong rt_sf_addr, newsp = 0;
4864 int i, err = 0;
4865 int signal;
4866
4867 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4868 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4869 goto sigsegv;
4870
4871 signal = current_exec_domain_sig(sig);
4872
Riku Voipiob0fd8d12014-04-23 10:46:13 +03004873 copy_siginfo_to_user(&rt_sf->info, info);
Nathan Froydbcd49332009-05-12 19:13:18 -07004874
Riku Voipio1d8b5122014-04-23 10:26:05 +03004875 __put_user(0, &rt_sf->uc.tuc_flags);
4876 __put_user(0, &rt_sf->uc.tuc_link);
4877 __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4878 &rt_sf->uc.tuc_stack.ss_sp);
4879 __put_user(sas_ss_flags(env->gpr[1]),
4880 &rt_sf->uc.tuc_stack.ss_flags);
4881 __put_user(target_sigaltstack_used.ss_size,
4882 &rt_sf->uc.tuc_stack.ss_size);
4883 __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4884 &rt_sf->uc.tuc_regs);
Nathan Froydbcd49332009-05-12 19:13:18 -07004885 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03004886 __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
Nathan Froydbcd49332009-05-12 19:13:18 -07004887 }
4888
Aurelien Jarno60e99242010-03-29 02:12:51 +02004889 frame = &rt_sf->uc.tuc_mcontext;
Nathan Froydbcd49332009-05-12 19:13:18 -07004890 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4891
4892 /* The kernel checks for the presence of a VDSO here. We don't
4893 emulate a vdso, so use a sigreturn system call. */
4894 env->lr = (target_ulong) h2g(frame->tramp);
4895
4896 /* Turn off all fp exceptions. */
4897 env->fpscr = 0;
4898
4899 /* Create a stack frame for the caller of the handler. */
4900 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
Riku Voipio1d8b5122014-04-23 10:26:05 +03004901 __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
Nathan Froydbcd49332009-05-12 19:13:18 -07004902
4903 if (err)
4904 goto sigsegv;
4905
4906 /* Set up registers for signal handler. */
4907 env->gpr[1] = newsp;
4908 env->gpr[3] = (target_ulong) signal;
4909 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4910 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4911 env->gpr[6] = (target_ulong) h2g(rt_sf);
4912 env->nip = (target_ulong) ka->_sa_handler;
4913 /* Signal handlers are entered in big-endian mode. */
4914 env->msr &= ~MSR_LE;
4915
4916 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4917 return;
4918
4919sigsegv:
4920 unlock_user_struct(rt_sf, rt_sf_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004921 qemu_log("segfaulting from setup_rt_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004922 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004923
4924}
4925
Andreas Färber05390242012-02-25 03:37:53 +01004926long do_sigreturn(CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004927{
4928 struct target_sigcontext *sc = NULL;
4929 struct target_mcontext *sr = NULL;
Peter Maydellb04636f2013-07-29 12:00:31 +01004930 target_ulong sr_addr = 0, sc_addr;
Nathan Froydbcd49332009-05-12 19:13:18 -07004931 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05004932 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07004933
4934 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4935 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4936 goto sigsegv;
4937
4938#if defined(TARGET_PPC64)
4939 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4940#else
4941 if(__get_user(set.sig[0], &sc->oldmask) ||
4942 __get_user(set.sig[1], &sc->_unused[3]))
4943 goto sigsegv;
4944#endif
4945 target_to_host_sigset_internal(&blocked, &set);
Alex Barcelo1c275922014-03-14 14:36:55 +00004946 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
Nathan Froydbcd49332009-05-12 19:13:18 -07004947
4948 if (__get_user(sr_addr, &sc->regs))
4949 goto sigsegv;
4950 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4951 goto sigsegv;
4952 if (restore_user_regs(env, sr, 1))
4953 goto sigsegv;
4954
4955 unlock_user_struct(sr, sr_addr, 1);
4956 unlock_user_struct(sc, sc_addr, 1);
4957 return -TARGET_QEMU_ESIGRETURN;
4958
4959sigsegv:
4960 unlock_user_struct(sr, sr_addr, 1);
4961 unlock_user_struct(sc, sc_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004962 qemu_log("segfaulting from do_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004963 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004964 return 0;
4965}
4966
4967/* See arch/powerpc/kernel/signal_32.c. */
Andreas Färber05390242012-02-25 03:37:53 +01004968static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
Nathan Froydbcd49332009-05-12 19:13:18 -07004969{
4970 struct target_mcontext *mcp;
4971 target_ulong mcp_addr;
4972 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05004973 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07004974
Aurelien Jarno60e99242010-03-29 02:12:51 +02004975 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
Nathan Froydbcd49332009-05-12 19:13:18 -07004976 sizeof (set)))
4977 return 1;
4978
4979#if defined(TARGET_PPC64)
4980 fprintf (stderr, "do_setcontext: not implemented\n");
4981 return 0;
4982#else
Aurelien Jarno60e99242010-03-29 02:12:51 +02004983 if (__get_user(mcp_addr, &ucp->tuc_regs))
Nathan Froydbcd49332009-05-12 19:13:18 -07004984 return 1;
4985
4986 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4987 return 1;
4988
4989 target_to_host_sigset_internal(&blocked, &set);
Alex Barcelo1c275922014-03-14 14:36:55 +00004990 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
Nathan Froydbcd49332009-05-12 19:13:18 -07004991 if (restore_user_regs(env, mcp, sig))
4992 goto sigsegv;
4993
4994 unlock_user_struct(mcp, mcp_addr, 1);
4995 return 0;
4996
4997sigsegv:
4998 unlock_user_struct(mcp, mcp_addr, 1);
4999 return 1;
5000#endif
5001}
5002
Andreas Färber05390242012-02-25 03:37:53 +01005003long do_rt_sigreturn(CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07005004{
5005 struct target_rt_sigframe *rt_sf = NULL;
5006 target_ulong rt_sf_addr;
5007
5008 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
5009 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
5010 goto sigsegv;
5011
5012 if (do_setcontext(&rt_sf->uc, env, 1))
5013 goto sigsegv;
5014
5015 do_sigaltstack(rt_sf_addr
Aurelien Jarno60e99242010-03-29 02:12:51 +02005016 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
Nathan Froydbcd49332009-05-12 19:13:18 -07005017 0, env->gpr[1]);
5018
5019 unlock_user_struct(rt_sf, rt_sf_addr, 1);
5020 return -TARGET_QEMU_ESIGRETURN;
5021
5022sigsegv:
5023 unlock_user_struct(rt_sf, rt_sf_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00005024 qemu_log("segfaulting from do_rt_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02005025 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07005026 return 0;
5027}
5028
Laurent Vivier492a8742009-08-03 16:12:17 +02005029#elif defined(TARGET_M68K)
5030
5031struct target_sigcontext {
5032 abi_ulong sc_mask;
5033 abi_ulong sc_usp;
5034 abi_ulong sc_d0;
5035 abi_ulong sc_d1;
5036 abi_ulong sc_a0;
5037 abi_ulong sc_a1;
5038 unsigned short sc_sr;
5039 abi_ulong sc_pc;
5040};
5041
5042struct target_sigframe
5043{
5044 abi_ulong pretcode;
5045 int sig;
5046 int code;
5047 abi_ulong psc;
5048 char retcode[8];
5049 abi_ulong extramask[TARGET_NSIG_WORDS-1];
5050 struct target_sigcontext sc;
5051};
Laurent Vivier71811552009-08-03 16:12:18 +02005052
Anthony Liguoric227f092009-10-01 16:12:16 -05005053typedef int target_greg_t;
Laurent Vivier71811552009-08-03 16:12:18 +02005054#define TARGET_NGREG 18
Anthony Liguoric227f092009-10-01 16:12:16 -05005055typedef target_greg_t target_gregset_t[TARGET_NGREG];
Laurent Vivier71811552009-08-03 16:12:18 +02005056
5057typedef struct target_fpregset {
5058 int f_fpcntl[3];
5059 int f_fpregs[8*3];
Anthony Liguoric227f092009-10-01 16:12:16 -05005060} target_fpregset_t;
Laurent Vivier71811552009-08-03 16:12:18 +02005061
5062struct target_mcontext {
5063 int version;
Anthony Liguoric227f092009-10-01 16:12:16 -05005064 target_gregset_t gregs;
5065 target_fpregset_t fpregs;
Laurent Vivier71811552009-08-03 16:12:18 +02005066};
5067
5068#define TARGET_MCONTEXT_VERSION 2
5069
5070struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02005071 abi_ulong tuc_flags;
5072 abi_ulong tuc_link;
5073 target_stack_t tuc_stack;
5074 struct target_mcontext tuc_mcontext;
5075 abi_long tuc_filler[80];
5076 target_sigset_t tuc_sigmask;
Laurent Vivier71811552009-08-03 16:12:18 +02005077};
5078
5079struct target_rt_sigframe
5080{
5081 abi_ulong pretcode;
5082 int sig;
5083 abi_ulong pinfo;
5084 abi_ulong puc;
5085 char retcode[8];
5086 struct target_siginfo info;
5087 struct target_ucontext uc;
5088};
Laurent Vivier492a8742009-08-03 16:12:17 +02005089
5090static int
Andreas Färber05390242012-02-25 03:37:53 +01005091setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
5092 abi_ulong mask)
Laurent Vivier492a8742009-08-03 16:12:17 +02005093{
5094 int err = 0;
5095
Riku Voipio1d8b5122014-04-23 10:26:05 +03005096 __put_user(mask, &sc->sc_mask);
5097 __put_user(env->aregs[7], &sc->sc_usp);
5098 __put_user(env->dregs[0], &sc->sc_d0);
5099 __put_user(env->dregs[1], &sc->sc_d1);
5100 __put_user(env->aregs[0], &sc->sc_a0);
5101 __put_user(env->aregs[1], &sc->sc_a1);
5102 __put_user(env->sr, &sc->sc_sr);
5103 __put_user(env->pc, &sc->sc_pc);
Laurent Vivier492a8742009-08-03 16:12:17 +02005104
5105 return err;
5106}
5107
5108static int
Andreas Färber05390242012-02-25 03:37:53 +01005109restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
Laurent Vivier492a8742009-08-03 16:12:17 +02005110{
5111 int err = 0;
5112 int temp;
5113
Riku Voipio1d8b5122014-04-23 10:26:05 +03005114 __get_user(env->aregs[7], &sc->sc_usp);
5115 __get_user(env->dregs[1], &sc->sc_d1);
5116 __get_user(env->aregs[0], &sc->sc_a0);
5117 __get_user(env->aregs[1], &sc->sc_a1);
5118 __get_user(env->pc, &sc->sc_pc);
5119 __get_user(temp, &sc->sc_sr);
Laurent Vivier492a8742009-08-03 16:12:17 +02005120 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5121
5122 *pd0 = tswapl(sc->sc_d0);
5123
5124 return err;
5125}
5126
5127/*
5128 * Determine which stack to use..
5129 */
5130static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01005131get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5132 size_t frame_size)
Laurent Vivier492a8742009-08-03 16:12:17 +02005133{
5134 unsigned long sp;
5135
5136 sp = regs->aregs[7];
5137
5138 /* This is the X/Open sanctioned signal stack switching. */
5139 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5140 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5141 }
5142
5143 return ((sp - frame_size) & -8UL);
5144}
5145
5146static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01005147 target_sigset_t *set, CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005148{
5149 struct target_sigframe *frame;
5150 abi_ulong frame_addr;
5151 abi_ulong retcode_addr;
5152 abi_ulong sc_addr;
5153 int err = 0;
5154 int i;
5155
5156 frame_addr = get_sigframe(ka, env, sizeof *frame);
5157 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5158 goto give_sigsegv;
5159
Riku Voipio1d8b5122014-04-23 10:26:05 +03005160 __put_user(sig, &frame->sig);
Laurent Vivier492a8742009-08-03 16:12:17 +02005161
5162 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005163 __put_user(sc_addr, &frame->psc);
Laurent Vivier492a8742009-08-03 16:12:17 +02005164
5165 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
5166 if (err)
5167 goto give_sigsegv;
5168
5169 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5170 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
5171 goto give_sigsegv;
5172 }
5173
5174 /* Set up to return from userspace. */
5175
5176 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005177 __put_user(retcode_addr, &frame->pretcode);
Laurent Vivier492a8742009-08-03 16:12:17 +02005178
5179 /* moveq #,d0; trap #0 */
5180
Riku Voipio1d8b5122014-04-23 10:26:05 +03005181 __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
Laurent Vivier492a8742009-08-03 16:12:17 +02005182 (long *)(frame->retcode));
5183
5184 if (err)
5185 goto give_sigsegv;
5186
5187 /* Set up to return from userspace */
5188
5189 env->aregs[7] = frame_addr;
5190 env->pc = ka->_sa_handler;
5191
5192 unlock_user_struct(frame, frame_addr, 1);
5193 return;
5194
5195give_sigsegv:
5196 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02005197 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02005198}
5199
Laurent Vivier71811552009-08-03 16:12:18 +02005200static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
Andreas Färber05390242012-02-25 03:37:53 +01005201 CPUM68KState *env)
Laurent Vivier71811552009-08-03 16:12:18 +02005202{
Aurelien Jarno60e99242010-03-29 02:12:51 +02005203 target_greg_t *gregs = uc->tuc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02005204
Riku Voipio1d8b5122014-04-23 10:26:05 +03005205 __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5206 __put_user(env->dregs[0], &gregs[0]);
5207 __put_user(env->dregs[1], &gregs[1]);
5208 __put_user(env->dregs[2], &gregs[2]);
5209 __put_user(env->dregs[3], &gregs[3]);
5210 __put_user(env->dregs[4], &gregs[4]);
5211 __put_user(env->dregs[5], &gregs[5]);
5212 __put_user(env->dregs[6], &gregs[6]);
5213 __put_user(env->dregs[7], &gregs[7]);
5214 __put_user(env->aregs[0], &gregs[8]);
5215 __put_user(env->aregs[1], &gregs[9]);
5216 __put_user(env->aregs[2], &gregs[10]);
5217 __put_user(env->aregs[3], &gregs[11]);
5218 __put_user(env->aregs[4], &gregs[12]);
5219 __put_user(env->aregs[5], &gregs[13]);
5220 __put_user(env->aregs[6], &gregs[14]);
5221 __put_user(env->aregs[7], &gregs[15]);
5222 __put_user(env->pc, &gregs[16]);
5223 __put_user(env->sr, &gregs[17]);
Laurent Vivier71811552009-08-03 16:12:18 +02005224
Riku Voipio1d8b5122014-04-23 10:26:05 +03005225 return 0;
Laurent Vivier71811552009-08-03 16:12:18 +02005226}
5227
Andreas Färber05390242012-02-25 03:37:53 +01005228static inline int target_rt_restore_ucontext(CPUM68KState *env,
Laurent Vivier71811552009-08-03 16:12:18 +02005229 struct target_ucontext *uc,
5230 int *pd0)
5231{
5232 int temp;
Aurelien Jarno60e99242010-03-29 02:12:51 +02005233 target_greg_t *gregs = uc->tuc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02005234
Riku Voipio1d8b5122014-04-23 10:26:05 +03005235 __get_user(temp, &uc->tuc_mcontext.version);
Laurent Vivier71811552009-08-03 16:12:18 +02005236 if (temp != TARGET_MCONTEXT_VERSION)
5237 goto badframe;
5238
5239 /* restore passed registers */
Riku Voipio1d8b5122014-04-23 10:26:05 +03005240 __get_user(env->dregs[0], &gregs[0]);
5241 __get_user(env->dregs[1], &gregs[1]);
5242 __get_user(env->dregs[2], &gregs[2]);
5243 __get_user(env->dregs[3], &gregs[3]);
5244 __get_user(env->dregs[4], &gregs[4]);
5245 __get_user(env->dregs[5], &gregs[5]);
5246 __get_user(env->dregs[6], &gregs[6]);
5247 __get_user(env->dregs[7], &gregs[7]);
5248 __get_user(env->aregs[0], &gregs[8]);
5249 __get_user(env->aregs[1], &gregs[9]);
5250 __get_user(env->aregs[2], &gregs[10]);
5251 __get_user(env->aregs[3], &gregs[11]);
5252 __get_user(env->aregs[4], &gregs[12]);
5253 __get_user(env->aregs[5], &gregs[13]);
5254 __get_user(env->aregs[6], &gregs[14]);
5255 __get_user(env->aregs[7], &gregs[15]);
5256 __get_user(env->pc, &gregs[16]);
5257 __get_user(temp, &gregs[17]);
Laurent Vivier71811552009-08-03 16:12:18 +02005258 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5259
5260 *pd0 = env->dregs[0];
Riku Voipio1d8b5122014-04-23 10:26:05 +03005261 return 0;
Laurent Vivier71811552009-08-03 16:12:18 +02005262
5263badframe:
5264 return 1;
5265}
5266
Laurent Vivier492a8742009-08-03 16:12:17 +02005267static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05005268 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01005269 target_sigset_t *set, CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005270{
Laurent Vivier71811552009-08-03 16:12:18 +02005271 struct target_rt_sigframe *frame;
5272 abi_ulong frame_addr;
5273 abi_ulong retcode_addr;
5274 abi_ulong info_addr;
5275 abi_ulong uc_addr;
5276 int err = 0;
5277 int i;
5278
5279 frame_addr = get_sigframe(ka, env, sizeof *frame);
5280 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5281 goto give_sigsegv;
5282
Riku Voipio1d8b5122014-04-23 10:26:05 +03005283 __put_user(sig, &frame->sig);
Laurent Vivier71811552009-08-03 16:12:18 +02005284
5285 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005286 __put_user(info_addr, &frame->pinfo);
Laurent Vivier71811552009-08-03 16:12:18 +02005287
5288 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005289 __put_user(uc_addr, &frame->puc);
Laurent Vivier71811552009-08-03 16:12:18 +02005290
Riku Voipiob0fd8d12014-04-23 10:46:13 +03005291 copy_siginfo_to_user(&frame->info, info);
Laurent Vivier71811552009-08-03 16:12:18 +02005292
5293 /* Create the ucontext */
5294
Riku Voipio1d8b5122014-04-23 10:26:05 +03005295 __put_user(0, &frame->uc.tuc_flags);
5296 __put_user(0, &frame->uc.tuc_link);
5297 __put_user(target_sigaltstack_used.ss_sp,
5298 &frame->uc.tuc_stack.ss_sp);
5299 __put_user(sas_ss_flags(env->aregs[7]),
5300 &frame->uc.tuc_stack.ss_flags);
5301 __put_user(target_sigaltstack_used.ss_size,
5302 &frame->uc.tuc_stack.ss_size);
Laurent Vivier71811552009-08-03 16:12:18 +02005303 err |= target_rt_setup_ucontext(&frame->uc, env);
5304
5305 if (err)
5306 goto give_sigsegv;
5307
5308 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02005309 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
Laurent Vivier71811552009-08-03 16:12:18 +02005310 goto give_sigsegv;
5311 }
5312
5313 /* Set up to return from userspace. */
5314
5315 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005316 __put_user(retcode_addr, &frame->pretcode);
Laurent Vivier71811552009-08-03 16:12:18 +02005317
5318 /* moveq #,d0; notb d0; trap #0 */
5319
Riku Voipio1d8b5122014-04-23 10:26:05 +03005320 __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5321 (long *)(frame->retcode + 0));
5322 __put_user(0x4e40, (short *)(frame->retcode + 4));
Laurent Vivier71811552009-08-03 16:12:18 +02005323
5324 if (err)
5325 goto give_sigsegv;
5326
5327 /* Set up to return from userspace */
5328
5329 env->aregs[7] = frame_addr;
5330 env->pc = ka->_sa_handler;
5331
5332 unlock_user_struct(frame, frame_addr, 1);
5333 return;
5334
5335give_sigsegv:
5336 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02005337 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02005338}
5339
Andreas Färber05390242012-02-25 03:37:53 +01005340long do_sigreturn(CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005341{
5342 struct target_sigframe *frame;
5343 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05005344 target_sigset_t target_set;
Laurent Vivier492a8742009-08-03 16:12:17 +02005345 sigset_t set;
5346 int d0, i;
5347
5348 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5349 goto badframe;
5350
5351 /* set blocked signals */
5352
5353 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5354 goto badframe;
5355
5356 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5357 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5358 goto badframe;
5359 }
5360
5361 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00005362 do_sigprocmask(SIG_SETMASK, &set, NULL);
Laurent Vivier492a8742009-08-03 16:12:17 +02005363
5364 /* restore registers */
5365
5366 if (restore_sigcontext(env, &frame->sc, &d0))
5367 goto badframe;
5368
5369 unlock_user_struct(frame, frame_addr, 0);
5370 return d0;
5371
5372badframe:
5373 unlock_user_struct(frame, frame_addr, 0);
5374 force_sig(TARGET_SIGSEGV);
5375 return 0;
5376}
5377
Andreas Färber05390242012-02-25 03:37:53 +01005378long do_rt_sigreturn(CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005379{
Laurent Vivier71811552009-08-03 16:12:18 +02005380 struct target_rt_sigframe *frame;
5381 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05005382 target_sigset_t target_set;
Laurent Vivier71811552009-08-03 16:12:18 +02005383 sigset_t set;
5384 int d0;
5385
5386 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5387 goto badframe;
5388
5389 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00005390 do_sigprocmask(SIG_SETMASK, &set, NULL);
Laurent Vivier71811552009-08-03 16:12:18 +02005391
5392 /* restore registers */
5393
5394 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5395 goto badframe;
5396
5397 if (do_sigaltstack(frame_addr +
Aurelien Jarno60e99242010-03-29 02:12:51 +02005398 offsetof(struct target_rt_sigframe, uc.tuc_stack),
Laurent Vivier71811552009-08-03 16:12:18 +02005399 0, get_sp_from_cpustate(env)) == -EFAULT)
5400 goto badframe;
5401
5402 unlock_user_struct(frame, frame_addr, 0);
5403 return d0;
5404
5405badframe:
5406 unlock_user_struct(frame, frame_addr, 0);
5407 force_sig(TARGET_SIGSEGV);
5408 return 0;
Laurent Vivier492a8742009-08-03 16:12:17 +02005409}
5410
Richard Henderson6049f4f2009-12-27 18:30:03 -08005411#elif defined(TARGET_ALPHA)
5412
5413struct target_sigcontext {
5414 abi_long sc_onstack;
5415 abi_long sc_mask;
5416 abi_long sc_pc;
5417 abi_long sc_ps;
5418 abi_long sc_regs[32];
5419 abi_long sc_ownedfp;
5420 abi_long sc_fpregs[32];
5421 abi_ulong sc_fpcr;
5422 abi_ulong sc_fp_control;
5423 abi_ulong sc_reserved1;
5424 abi_ulong sc_reserved2;
5425 abi_ulong sc_ssize;
5426 abi_ulong sc_sbase;
5427 abi_ulong sc_traparg_a0;
5428 abi_ulong sc_traparg_a1;
5429 abi_ulong sc_traparg_a2;
5430 abi_ulong sc_fp_trap_pc;
5431 abi_ulong sc_fp_trigger_sum;
5432 abi_ulong sc_fp_trigger_inst;
5433};
5434
5435struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02005436 abi_ulong tuc_flags;
5437 abi_ulong tuc_link;
5438 abi_ulong tuc_osf_sigmask;
5439 target_stack_t tuc_stack;
5440 struct target_sigcontext tuc_mcontext;
5441 target_sigset_t tuc_sigmask;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005442};
5443
5444struct target_sigframe {
5445 struct target_sigcontext sc;
5446 unsigned int retcode[3];
5447};
5448
5449struct target_rt_sigframe {
5450 target_siginfo_t info;
5451 struct target_ucontext uc;
5452 unsigned int retcode[3];
5453};
5454
5455#define INSN_MOV_R30_R16 0x47fe0410
5456#define INSN_LDI_R0 0x201f0000
5457#define INSN_CALLSYS 0x00000083
5458
Andreas Färber05390242012-02-25 03:37:53 +01005459static int setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
Richard Henderson6049f4f2009-12-27 18:30:03 -08005460 abi_ulong frame_addr, target_sigset_t *set)
5461{
5462 int i, err = 0;
5463
Riku Voipio1d8b5122014-04-23 10:26:05 +03005464 __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5465 __put_user(set->sig[0], &sc->sc_mask);
5466 __put_user(env->pc, &sc->sc_pc);
5467 __put_user(8, &sc->sc_ps);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005468
5469 for (i = 0; i < 31; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005470 __put_user(env->ir[i], &sc->sc_regs[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005471 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03005472 __put_user(0, &sc->sc_regs[31]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005473
5474 for (i = 0; i < 31; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005475 __put_user(env->fir[i], &sc->sc_fpregs[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005476 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03005477 __put_user(0, &sc->sc_fpregs[31]);
5478 __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005479
Riku Voipio1d8b5122014-04-23 10:26:05 +03005480 __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5481 __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5482 __put_user(0, &sc->sc_traparg_a2); /* FIXME */
Richard Henderson6049f4f2009-12-27 18:30:03 -08005483
5484 return err;
5485}
5486
Andreas Färber05390242012-02-25 03:37:53 +01005487static int restore_sigcontext(CPUAlphaState *env,
5488 struct target_sigcontext *sc)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005489{
5490 uint64_t fpcr;
5491 int i, err = 0;
5492
Riku Voipio1d8b5122014-04-23 10:26:05 +03005493 __get_user(env->pc, &sc->sc_pc);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005494
5495 for (i = 0; i < 31; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005496 __get_user(env->ir[i], &sc->sc_regs[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005497 }
5498 for (i = 0; i < 31; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005499 __get_user(env->fir[i], &sc->sc_fpregs[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005500 }
5501
Riku Voipio1d8b5122014-04-23 10:26:05 +03005502 __get_user(fpcr, &sc->sc_fpcr);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005503 cpu_alpha_store_fpcr(env, fpcr);
5504
5505 return err;
5506}
5507
5508static inline abi_ulong get_sigframe(struct target_sigaction *sa,
Andreas Färber05390242012-02-25 03:37:53 +01005509 CPUAlphaState *env,
5510 unsigned long framesize)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005511{
5512 abi_ulong sp = env->ir[IR_SP];
5513
5514 /* This is the X/Open sanctioned signal stack switching. */
5515 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5516 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5517 }
5518 return (sp - framesize) & -32;
5519}
5520
5521static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01005522 target_sigset_t *set, CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005523{
5524 abi_ulong frame_addr, r26;
5525 struct target_sigframe *frame;
5526 int err = 0;
5527
5528 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5529 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5530 goto give_sigsegv;
5531 }
5532
5533 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
5534
5535 if (ka->sa_restorer) {
5536 r26 = ka->sa_restorer;
5537 } else {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005538 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5539 __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5540 &frame->retcode[1]);
5541 __put_user(INSN_CALLSYS, &frame->retcode[2]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005542 /* imb() */
5543 r26 = frame_addr;
5544 }
5545
5546 unlock_user_struct(frame, frame_addr, 1);
5547
5548 if (err) {
5549 give_sigsegv:
5550 if (sig == TARGET_SIGSEGV) {
5551 ka->_sa_handler = TARGET_SIG_DFL;
5552 }
5553 force_sig(TARGET_SIGSEGV);
5554 }
5555
5556 env->ir[IR_RA] = r26;
5557 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5558 env->ir[IR_A0] = sig;
5559 env->ir[IR_A1] = 0;
5560 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5561 env->ir[IR_SP] = frame_addr;
5562}
5563
5564static void setup_rt_frame(int sig, struct target_sigaction *ka,
5565 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01005566 target_sigset_t *set, CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005567{
5568 abi_ulong frame_addr, r26;
5569 struct target_rt_sigframe *frame;
5570 int i, err = 0;
5571
5572 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5573 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5574 goto give_sigsegv;
5575 }
5576
Riku Voipiob0fd8d12014-04-23 10:46:13 +03005577 copy_siginfo_to_user(&frame->info, info);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005578
Riku Voipio1d8b5122014-04-23 10:26:05 +03005579 __put_user(0, &frame->uc.tuc_flags);
5580 __put_user(0, &frame->uc.tuc_link);
5581 __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5582 __put_user(target_sigaltstack_used.ss_sp,
5583 &frame->uc.tuc_stack.ss_sp);
5584 __put_user(sas_ss_flags(env->ir[IR_SP]),
5585 &frame->uc.tuc_stack.ss_flags);
5586 __put_user(target_sigaltstack_used.ss_size,
5587 &frame->uc.tuc_stack.ss_size);
Aurelien Jarno60e99242010-03-29 02:12:51 +02005588 err |= setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005589 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005590 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005591 }
5592
5593 if (ka->sa_restorer) {
5594 r26 = ka->sa_restorer;
5595 } else {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005596 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5597 __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5598 &frame->retcode[1]);
5599 __put_user(INSN_CALLSYS, &frame->retcode[2]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005600 /* imb(); */
5601 r26 = frame_addr;
5602 }
5603
5604 if (err) {
5605 give_sigsegv:
5606 if (sig == TARGET_SIGSEGV) {
5607 ka->_sa_handler = TARGET_SIG_DFL;
5608 }
5609 force_sig(TARGET_SIGSEGV);
5610 }
5611
5612 env->ir[IR_RA] = r26;
5613 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5614 env->ir[IR_A0] = sig;
5615 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5616 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5617 env->ir[IR_SP] = frame_addr;
5618}
5619
Andreas Färber05390242012-02-25 03:37:53 +01005620long do_sigreturn(CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005621{
5622 struct target_sigcontext *sc;
5623 abi_ulong sc_addr = env->ir[IR_A0];
5624 target_sigset_t target_set;
5625 sigset_t set;
5626
5627 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5628 goto badframe;
5629 }
5630
5631 target_sigemptyset(&target_set);
5632 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5633 goto badframe;
5634 }
5635
5636 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00005637 do_sigprocmask(SIG_SETMASK, &set, NULL);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005638
5639 if (restore_sigcontext(env, sc)) {
5640 goto badframe;
5641 }
5642 unlock_user_struct(sc, sc_addr, 0);
5643 return env->ir[IR_V0];
5644
5645 badframe:
5646 unlock_user_struct(sc, sc_addr, 0);
5647 force_sig(TARGET_SIGSEGV);
5648}
5649
Andreas Färber05390242012-02-25 03:37:53 +01005650long do_rt_sigreturn(CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005651{
5652 abi_ulong frame_addr = env->ir[IR_A0];
5653 struct target_rt_sigframe *frame;
5654 sigset_t set;
5655
5656 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5657 goto badframe;
5658 }
Aurelien Jarno60e99242010-03-29 02:12:51 +02005659 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00005660 do_sigprocmask(SIG_SETMASK, &set, NULL);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005661
Aurelien Jarno60e99242010-03-29 02:12:51 +02005662 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005663 goto badframe;
5664 }
5665 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
Aurelien Jarno60e99242010-03-29 02:12:51 +02005666 uc.tuc_stack),
Richard Henderson6049f4f2009-12-27 18:30:03 -08005667 0, env->ir[IR_SP]) == -EFAULT) {
5668 goto badframe;
5669 }
5670
5671 unlock_user_struct(frame, frame_addr, 0);
5672 return env->ir[IR_V0];
5673
5674
5675 badframe:
5676 unlock_user_struct(frame, frame_addr, 0);
5677 force_sig(TARGET_SIGSEGV);
5678}
5679
bellardb346ff42003-06-15 20:05:50 +00005680#else
5681
pbrook624f7972008-05-31 16:11:38 +00005682static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber9349b4f2012-03-14 01:38:32 +01005683 target_sigset_t *set, CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005684{
5685 fprintf(stderr, "setup_frame: not implemented\n");
5686}
5687
pbrook624f7972008-05-31 16:11:38 +00005688static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05005689 target_siginfo_t *info,
Andreas Färber9349b4f2012-03-14 01:38:32 +01005690 target_sigset_t *set, CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005691{
5692 fprintf(stderr, "setup_rt_frame: not implemented\n");
5693}
5694
Andreas Färber9349b4f2012-03-14 01:38:32 +01005695long do_sigreturn(CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005696{
5697 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00005698 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00005699}
5700
Andreas Färber9349b4f2012-03-14 01:38:32 +01005701long do_rt_sigreturn(CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005702{
5703 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00005704 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00005705}
5706
bellard66fb9762003-03-23 01:06:05 +00005707#endif
5708
Andreas Färber9349b4f2012-03-14 01:38:32 +01005709void process_pending_signals(CPUArchState *cpu_env)
bellard66fb9762003-03-23 01:06:05 +00005710{
Andreas Färberdb6b81d2013-06-27 19:49:31 +02005711 CPUState *cpu = ENV_GET_CPU(cpu_env);
bellard66fb9762003-03-23 01:06:05 +00005712 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00005713 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00005714 sigset_t set, old_set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005715 target_sigset_t target_old_set;
pbrook624f7972008-05-31 16:11:38 +00005716 struct emulated_sigtable *k;
5717 struct target_sigaction *sa;
bellard66fb9762003-03-23 01:06:05 +00005718 struct sigqueue *q;
Andreas Färber0429a972013-08-26 18:14:44 +02005719 TaskState *ts = cpu->opaque;
ths3b46e622007-09-17 08:09:54 +00005720
pbrook624f7972008-05-31 16:11:38 +00005721 if (!ts->signal_pending)
bellard31e31b82003-02-18 22:55:36 +00005722 return;
5723
pbrook624f7972008-05-31 16:11:38 +00005724 /* FIXME: This is not threadsafe. */
5725 k = ts->sigtab;
bellard66fb9762003-03-23 01:06:05 +00005726 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5727 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00005728 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00005729 k++;
bellard31e31b82003-02-18 22:55:36 +00005730 }
5731 /* if no signal is pending, just return */
pbrook624f7972008-05-31 16:11:38 +00005732 ts->signal_pending = 0;
bellard31e31b82003-02-18 22:55:36 +00005733 return;
bellard66fb9762003-03-23 01:06:05 +00005734
bellard31e31b82003-02-18 22:55:36 +00005735 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00005736#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00005737 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00005738#endif
5739 /* dequeue signal */
5740 q = k->first;
5741 k->first = q->next;
5742 if (!k->first)
5743 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00005744
Andreas Färberdb6b81d2013-06-27 19:49:31 +02005745 sig = gdb_handlesig(cpu, sig);
bellard1fddef42005-04-17 19:16:13 +00005746 if (!sig) {
aurel32ca587a82008-12-18 22:44:13 +00005747 sa = NULL;
5748 handler = TARGET_SIG_IGN;
5749 } else {
5750 sa = &sigact_table[sig - 1];
5751 handler = sa->_sa_handler;
bellard1fddef42005-04-17 19:16:13 +00005752 }
bellard66fb9762003-03-23 01:06:05 +00005753
Peter Maydella7ec0f92014-03-14 14:36:56 +00005754 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
5755 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5756 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5757 * because it got a real MMU fault), and treat as if default handler.
5758 */
5759 handler = TARGET_SIG_DFL;
5760 }
5761
bellard66fb9762003-03-23 01:06:05 +00005762 if (handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +00005763 /* default handler : ignore some signal. The other are job control or fatal */
5764 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5765 kill(getpid(),SIGSTOP);
5766 } else if (sig != TARGET_SIGCHLD &&
5767 sig != TARGET_SIGURG &&
5768 sig != TARGET_SIGWINCH &&
5769 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +00005770 force_sig(sig);
5771 }
5772 } else if (handler == TARGET_SIG_IGN) {
5773 /* ignore sig */
5774 } else if (handler == TARGET_SIG_ERR) {
5775 force_sig(sig);
5776 } else {
bellard9de5e442003-03-23 16:49:39 +00005777 /* compute the blocked signals during the handler execution */
pbrook624f7972008-05-31 16:11:38 +00005778 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +00005779 /* SA_NODEFER indicates that the current signal should not be
5780 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +00005781 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +00005782 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00005783
bellard9de5e442003-03-23 16:49:39 +00005784 /* block signals in the handler using Linux */
Alex Barcelo1c275922014-03-14 14:36:55 +00005785 do_sigprocmask(SIG_BLOCK, &set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00005786 /* save the previous blocked signal state to restore it at the
5787 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00005788 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00005789
bellardbc8a22c2003-03-30 21:02:40 +00005790 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00005791#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00005792 {
5793 CPUX86State *env = cpu_env;
5794 if (env->eflags & VM_MASK)
5795 save_v86_state(env);
5796 }
5797#endif
bellard9de5e442003-03-23 16:49:39 +00005798 /* prepare the stack frame of the virtual CPU */
Richard Hendersonff970902013-02-10 10:30:42 -08005799#if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5800 /* These targets do not have traditional signals. */
5801 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5802#else
pbrook624f7972008-05-31 16:11:38 +00005803 if (sa->sa_flags & TARGET_SA_SIGINFO)
5804 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00005805 else
pbrook624f7972008-05-31 16:11:38 +00005806 setup_frame(sig, sa, &target_old_set, cpu_env);
Richard Hendersonff970902013-02-10 10:30:42 -08005807#endif
pbrook624f7972008-05-31 16:11:38 +00005808 if (sa->sa_flags & TARGET_SA_RESETHAND)
5809 sa->_sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00005810 }
bellard66fb9762003-03-23 01:06:05 +00005811 if (q != &k->info)
pbrook624f7972008-05-31 16:11:38 +00005812 free_sigqueue(cpu_env, q);
bellard31e31b82003-02-18 22:55:36 +00005813}