blob: b59ec3d38046f467b1cfc1b891f53cdec5b32b21 [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 */
Riku Voipio41ecc722014-04-23 11:01:00 +0300839static void setup_sigcontext(struct target_sigcontext *sc,
840 struct target_fpstate *fpstate, CPUX86State *env, abi_ulong mask,
841 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));
Andreas Färber27103422013-08-26 08:31:06 +0200844 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000845
bellard579a97f2007-11-11 14:26:47 +0000846 /* already locked in setup_frame() */
Riku Voipio1d8b5122014-04-23 10:26:05 +0300847 __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
848 __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
849 __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
850 __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
851 __put_user(env->regs[R_EDI], &sc->edi);
852 __put_user(env->regs[R_ESI], &sc->esi);
853 __put_user(env->regs[R_EBP], &sc->ebp);
854 __put_user(env->regs[R_ESP], &sc->esp);
855 __put_user(env->regs[R_EBX], &sc->ebx);
856 __put_user(env->regs[R_EDX], &sc->edx);
857 __put_user(env->regs[R_ECX], &sc->ecx);
858 __put_user(env->regs[R_EAX], &sc->eax);
859 __put_user(cs->exception_index, &sc->trapno);
860 __put_user(env->error_code, &sc->err);
861 __put_user(env->eip, &sc->eip);
862 __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
863 __put_user(env->eflags, &sc->eflags);
864 __put_user(env->regs[R_ESP], &sc->esp_at_signal);
865 __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000866
bellard28be6232007-11-11 22:23:38 +0000867 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000868 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000869 magic = 0xffff;
Riku Voipio1d8b5122014-04-23 10:26:05 +0300870 __put_user(magic, &fpstate->magic);
871 __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000872
bellard66fb9762003-03-23 01:06:05 +0000873 /* non-iBCS2 extensions.. */
Riku Voipio1d8b5122014-04-23 10:26:05 +0300874 __put_user(mask, &sc->oldmask);
875 __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000876}
877
878/*
879 * Determine which stack to use..
880 */
881
bellard579a97f2007-11-11 14:26:47 +0000882static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +0000883get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
bellard66fb9762003-03-23 01:06:05 +0000884{
885 unsigned long esp;
886
887 /* Default to using normal stack */
888 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000889 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +0000890 if (ka->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +0000891 if (sas_ss_flags(esp) == 0)
892 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
893 }
bellard66fb9762003-03-23 01:06:05 +0000894
895 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000896 else
bellarda52c7572003-06-21 13:14:12 +0000897 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
pbrook624f7972008-05-31 16:11:38 +0000898 !(ka->sa_flags & TARGET_SA_RESTORER) &&
899 ka->sa_restorer) {
900 esp = (unsigned long) ka->sa_restorer;
bellarda52c7572003-06-21 13:14:12 +0000901 }
bellard579a97f2007-11-11 14:26:47 +0000902 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000903}
904
bellard579a97f2007-11-11 14:26:47 +0000905/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +0000906static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500907 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000908{
bellard579a97f2007-11-11 14:26:47 +0000909 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000910 struct sigframe *frame;
Riku Voipio7df2fa32014-04-23 10:34:53 +0300911 int i;
bellard66fb9762003-03-23 01:06:05 +0000912
bellard579a97f2007-11-11 14:26:47 +0000913 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000914
bellard579a97f2007-11-11 14:26:47 +0000915 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000916 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000917
Riku Voipio1d8b5122014-04-23 10:26:05 +0300918 __put_user(current_exec_domain_sig(sig),
919 &frame->sig);
bellard66fb9762003-03-23 01:06:05 +0000920
bellard28be6232007-11-11 22:23:38 +0000921 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
922 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000923
Riku Voipio7df2fa32014-04-23 10:34:53 +0300924 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
925 __put_user(set->sig[i], &frame->extramask[i - 1]);
926 }
bellard66fb9762003-03-23 01:06:05 +0000927
928 /* Set up to return from userspace. If provided, use a stub
929 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000930 if (ka->sa_flags & TARGET_SA_RESTORER) {
Riku Voipio1d8b5122014-04-23 10:26:05 +0300931 __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000932 } else {
bellard775b58d2007-11-11 16:22:17 +0000933 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000934 abi_ulong retcode_addr;
935 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
Riku Voipio1d8b5122014-04-23 10:26:05 +0300936 __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000937 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000938 val16 = 0xb858;
Riku Voipio1d8b5122014-04-23 10:26:05 +0300939 __put_user(val16, (uint16_t *)(frame->retcode+0));
940 __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000941 val16 = 0x80cd;
Riku Voipio1d8b5122014-04-23 10:26:05 +0300942 __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000943 }
944
bellard66fb9762003-03-23 01:06:05 +0000945
946 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000947 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000948 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000949
950 cpu_x86_load_seg(env, R_DS, __USER_DS);
951 cpu_x86_load_seg(env, R_ES, __USER_DS);
952 cpu_x86_load_seg(env, R_SS, __USER_DS);
953 cpu_x86_load_seg(env, R_CS, __USER_CS);
954 env->eflags &= ~TF_MASK;
955
bellard579a97f2007-11-11 14:26:47 +0000956 unlock_user_struct(frame, frame_addr, 1);
957
bellard66fb9762003-03-23 01:06:05 +0000958 return;
959
960give_sigsegv:
961 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000962 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000963 force_sig(TARGET_SIGSEGV /* , current */);
964}
965
bellard579a97f2007-11-11 14:26:47 +0000966/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +0000967static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500968 target_siginfo_t *info,
969 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000970{
bellard28be6232007-11-11 22:23:38 +0000971 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000972 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000973 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000974
bellard579a97f2007-11-11 14:26:47 +0000975 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000976
bellard579a97f2007-11-11 14:26:47 +0000977 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000978 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000979
Riku Voipio1d8b5122014-04-23 10:26:05 +0300980 __put_user(current_exec_domain_sig(sig), &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000981 addr = frame_addr + offsetof(struct rt_sigframe, info);
Riku Voipio1d8b5122014-04-23 10:26:05 +0300982 __put_user(addr, &frame->pinfo);
bellard28be6232007-11-11 22:23:38 +0000983 addr = frame_addr + offsetof(struct rt_sigframe, uc);
Riku Voipio1d8b5122014-04-23 10:26:05 +0300984 __put_user(addr, &frame->puc);
Riku Voipiob0fd8d12014-04-23 10:46:13 +0300985 copy_siginfo_to_user(&frame->info, info);
bellard66fb9762003-03-23 01:06:05 +0000986
987 /* Create the ucontext. */
Riku Voipio1d8b5122014-04-23 10:26:05 +0300988 __put_user(0, &frame->uc.tuc_flags);
989 __put_user(0, &frame->uc.tuc_link);
990 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
991 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
992 &frame->uc.tuc_stack.ss_flags);
993 __put_user(target_sigaltstack_used.ss_size,
994 &frame->uc.tuc_stack.ss_size);
Riku Voipio41ecc722014-04-23 11:01:00 +0300995 setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate, env,
996 set->sig[0], frame_addr + offsetof(struct rt_sigframe, fpstate));
997
bellard92319442004-06-19 16:58:13 +0000998 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000999 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +00001000 goto give_sigsegv;
1001 }
bellard66fb9762003-03-23 01:06:05 +00001002
1003 /* Set up to return from userspace. If provided, use a stub
1004 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00001005 if (ka->sa_flags & TARGET_SA_RESTORER) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03001006 __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +00001007 } else {
bellard775b58d2007-11-11 16:22:17 +00001008 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +00001009 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
Riku Voipio1d8b5122014-04-23 10:26:05 +03001010 __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +00001011 /* This is movl $,%eax ; int $0x80 */
Riku Voipio1d8b5122014-04-23 10:26:05 +03001012 __put_user(0xb8, (char *)(frame->retcode+0));
1013 __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +00001014 val16 = 0x80cd;
Riku Voipio1d8b5122014-04-23 10:26:05 +03001015 __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +00001016 }
1017
1018 if (err)
1019 goto give_sigsegv;
1020
1021 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +00001022 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +00001023 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +00001024
1025 cpu_x86_load_seg(env, R_DS, __USER_DS);
1026 cpu_x86_load_seg(env, R_ES, __USER_DS);
1027 cpu_x86_load_seg(env, R_SS, __USER_DS);
1028 cpu_x86_load_seg(env, R_CS, __USER_CS);
1029 env->eflags &= ~TF_MASK;
1030
bellard579a97f2007-11-11 14:26:47 +00001031 unlock_user_struct(frame, frame_addr, 1);
1032
bellard66fb9762003-03-23 01:06:05 +00001033 return;
1034
1035give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00001036 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +00001037 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +00001038 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +00001039 force_sig(TARGET_SIGSEGV /* , current */);
1040}
1041
1042static int
1043restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
1044{
1045 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +00001046 abi_ulong fpstate_addr;
1047 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +00001048
bellard28be6232007-11-11 22:23:38 +00001049 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
1050 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
1051 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
1052 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +00001053
bellard28be6232007-11-11 22:23:38 +00001054 env->regs[R_EDI] = tswapl(sc->edi);
1055 env->regs[R_ESI] = tswapl(sc->esi);
1056 env->regs[R_EBP] = tswapl(sc->ebp);
1057 env->regs[R_ESP] = tswapl(sc->esp);
1058 env->regs[R_EBX] = tswapl(sc->ebx);
1059 env->regs[R_EDX] = tswapl(sc->edx);
1060 env->regs[R_ECX] = tswapl(sc->ecx);
1061 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +00001062
Mike McCormack9a826d72011-06-01 15:14:37 +09001063 cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
1064 cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +00001065
bellard28be6232007-11-11 22:23:38 +00001066 tmpflags = tswapl(sc->eflags);
1067 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
1068 // regs->orig_eax = -1; /* disable syscall checks */
1069
1070 fpstate_addr = tswapl(sc->fpstate);
1071 if (fpstate_addr != 0) {
1072 if (!access_ok(VERIFY_READ, fpstate_addr,
1073 sizeof(struct target_fpstate)))
1074 goto badframe;
1075 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +00001076 }
1077
bellard28be6232007-11-11 22:23:38 +00001078 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +00001079 return err;
bellard66fb9762003-03-23 01:06:05 +00001080badframe:
1081 return 1;
bellard66fb9762003-03-23 01:06:05 +00001082}
1083
1084long do_sigreturn(CPUX86State *env)
1085{
bellard579a97f2007-11-11 14:26:47 +00001086 struct sigframe *frame;
1087 abi_ulong frame_addr = env->regs[R_ESP] - 8;
Anthony Liguoric227f092009-10-01 16:12:16 -05001088 target_sigset_t target_set;
bellard66fb9762003-03-23 01:06:05 +00001089 sigset_t set;
1090 int eax, i;
1091
bellard447db212003-05-10 15:10:36 +00001092#if defined(DEBUG_SIGNAL)
1093 fprintf(stderr, "do_sigreturn\n");
1094#endif
bellard579a97f2007-11-11 14:26:47 +00001095 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1096 goto badframe;
bellard66fb9762003-03-23 01:06:05 +00001097 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +00001098 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1099 goto badframe;
1100 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1101 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1102 goto badframe;
1103 }
bellard66fb9762003-03-23 01:06:05 +00001104
bellard92319442004-06-19 16:58:13 +00001105 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00001106 do_sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +00001107
bellard66fb9762003-03-23 01:06:05 +00001108 /* restore registers */
1109 if (restore_sigcontext(env, &frame->sc, &eax))
1110 goto badframe;
bellard579a97f2007-11-11 14:26:47 +00001111 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001112 return eax;
1113
1114badframe:
bellard579a97f2007-11-11 14:26:47 +00001115 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001116 force_sig(TARGET_SIGSEGV);
1117 return 0;
1118}
1119
1120long do_rt_sigreturn(CPUX86State *env)
1121{
bellard28be6232007-11-11 22:23:38 +00001122 abi_ulong frame_addr;
1123 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +00001124 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +00001125 int eax;
1126
bellard28be6232007-11-11 22:23:38 +00001127 frame_addr = env->regs[R_ESP] - 4;
1128 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1129 goto badframe;
bellardb8076a72005-04-07 22:20:31 +00001130 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00001131 do_sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +00001132
bellardb8076a72005-04-07 22:20:31 +00001133 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +00001134 goto badframe;
1135
bellard28be6232007-11-11 22:23:38 +00001136 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1137 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +00001138 goto badframe;
thsa04e1342007-09-27 13:57:58 +00001139
bellard28be6232007-11-11 22:23:38 +00001140 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001141 return eax;
1142
1143badframe:
bellard28be6232007-11-11 22:23:38 +00001144 unlock_user_struct(frame, frame_addr, 0);
1145 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +00001146 return 0;
1147}
1148
Andreas Schwab1744aea2013-09-03 20:12:16 +01001149#elif defined(TARGET_AARCH64)
1150
1151struct target_sigcontext {
1152 uint64_t fault_address;
1153 /* AArch64 registers */
1154 uint64_t regs[31];
1155 uint64_t sp;
1156 uint64_t pc;
1157 uint64_t pstate;
1158 /* 4K reserved for FP/SIMD state and future expansion */
1159 char __reserved[4096] __attribute__((__aligned__(16)));
1160};
1161
1162struct target_ucontext {
1163 abi_ulong tuc_flags;
1164 abi_ulong tuc_link;
1165 target_stack_t tuc_stack;
1166 target_sigset_t tuc_sigmask;
1167 /* glibc uses a 1024-bit sigset_t */
1168 char __unused[1024 / 8 - sizeof(target_sigset_t)];
1169 /* last for future expansion */
1170 struct target_sigcontext tuc_mcontext;
1171};
1172
1173/*
1174 * Header to be used at the beginning of structures extending the user
1175 * context. Such structures must be placed after the rt_sigframe on the stack
1176 * and be 16-byte aligned. The last structure must be a dummy one with the
1177 * magic and size set to 0.
1178 */
1179struct target_aarch64_ctx {
1180 uint32_t magic;
1181 uint32_t size;
1182};
1183
1184#define TARGET_FPSIMD_MAGIC 0x46508001
1185
1186struct target_fpsimd_context {
1187 struct target_aarch64_ctx head;
1188 uint32_t fpsr;
1189 uint32_t fpcr;
1190 uint64_t vregs[32 * 2]; /* really uint128_t vregs[32] */
1191};
1192
1193/*
1194 * Auxiliary context saved in the sigcontext.__reserved array. Not exported to
1195 * user space as it will change with the addition of new context. User space
1196 * should check the magic/size information.
1197 */
1198struct target_aux_context {
1199 struct target_fpsimd_context fpsimd;
1200 /* additional context to be added before "end" */
1201 struct target_aarch64_ctx end;
1202};
1203
1204struct target_rt_sigframe {
1205 struct target_siginfo info;
1206 struct target_ucontext uc;
1207 uint64_t fp;
1208 uint64_t lr;
1209 uint32_t tramp[2];
1210};
1211
1212static int target_setup_sigframe(struct target_rt_sigframe *sf,
1213 CPUARMState *env, target_sigset_t *set)
1214{
1215 int i;
1216 struct target_aux_context *aux =
1217 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
1218
1219 /* set up the stack frame for unwinding */
1220 __put_user(env->xregs[29], &sf->fp);
1221 __put_user(env->xregs[30], &sf->lr);
1222
1223 for (i = 0; i < 31; i++) {
1224 __put_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1225 }
1226 __put_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1227 __put_user(env->pc, &sf->uc.tuc_mcontext.pc);
Peter Maydelld3563122013-12-17 19:42:30 +00001228 __put_user(pstate_read(env), &sf->uc.tuc_mcontext.pstate);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001229
Peter Maydell7af03922014-05-01 18:36:17 +01001230 __put_user(env->exception.vaddress, &sf->uc.tuc_mcontext.fault_address);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001231
1232 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
1233 __put_user(set->sig[i], &sf->uc.tuc_sigmask.sig[i]);
1234 }
1235
1236 for (i = 0; i < 32; i++) {
1237#ifdef TARGET_WORDS_BIGENDIAN
1238 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1239 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1240#else
1241 __put_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1242 __put_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1243#endif
1244 }
Will Newtone0ee1382014-01-04 22:15:48 +00001245 __put_user(vfp_get_fpsr(env), &aux->fpsimd.fpsr);
1246 __put_user(vfp_get_fpcr(env), &aux->fpsimd.fpcr);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001247 __put_user(TARGET_FPSIMD_MAGIC, &aux->fpsimd.head.magic);
1248 __put_user(sizeof(struct target_fpsimd_context),
1249 &aux->fpsimd.head.size);
1250
1251 /* set the "end" magic */
1252 __put_user(0, &aux->end.magic);
1253 __put_user(0, &aux->end.size);
1254
1255 return 0;
1256}
1257
1258static int target_restore_sigframe(CPUARMState *env,
1259 struct target_rt_sigframe *sf)
1260{
1261 sigset_t set;
1262 int i;
1263 struct target_aux_context *aux =
1264 (struct target_aux_context *)sf->uc.tuc_mcontext.__reserved;
Will Newtone0ee1382014-01-04 22:15:48 +00001265 uint32_t magic, size, fpsr, fpcr;
Peter Maydelld3563122013-12-17 19:42:30 +00001266 uint64_t pstate;
Andreas Schwab1744aea2013-09-03 20:12:16 +01001267
1268 target_to_host_sigset(&set, &sf->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00001269 do_sigprocmask(SIG_SETMASK, &set, NULL);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001270
1271 for (i = 0; i < 31; i++) {
1272 __get_user(env->xregs[i], &sf->uc.tuc_mcontext.regs[i]);
1273 }
1274
1275 __get_user(env->xregs[31], &sf->uc.tuc_mcontext.sp);
1276 __get_user(env->pc, &sf->uc.tuc_mcontext.pc);
Peter Maydelld3563122013-12-17 19:42:30 +00001277 __get_user(pstate, &sf->uc.tuc_mcontext.pstate);
1278 pstate_write(env, pstate);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001279
1280 __get_user(magic, &aux->fpsimd.head.magic);
1281 __get_user(size, &aux->fpsimd.head.size);
1282
1283 if (magic != TARGET_FPSIMD_MAGIC
1284 || size != sizeof(struct target_fpsimd_context)) {
1285 return 1;
1286 }
1287
Peter Maydell4cf23482014-03-02 19:36:38 +00001288 for (i = 0; i < 32; i++) {
1289#ifdef TARGET_WORDS_BIGENDIAN
1290 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2 + 1]);
1291 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2]);
1292#else
1293 __get_user(env->vfp.regs[i * 2], &aux->fpsimd.vregs[i * 2]);
1294 __get_user(env->vfp.regs[i * 2 + 1], &aux->fpsimd.vregs[i * 2 + 1]);
1295#endif
Andreas Schwab1744aea2013-09-03 20:12:16 +01001296 }
Will Newtone0ee1382014-01-04 22:15:48 +00001297 __get_user(fpsr, &aux->fpsimd.fpsr);
1298 vfp_set_fpsr(env, fpsr);
1299 __get_user(fpcr, &aux->fpsimd.fpcr);
1300 vfp_set_fpcr(env, fpcr);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001301
1302 return 0;
1303}
1304
1305static abi_ulong get_sigframe(struct target_sigaction *ka, CPUARMState *env)
1306{
1307 abi_ulong sp;
1308
1309 sp = env->xregs[31];
1310
1311 /*
1312 * This is the X/Open sanctioned signal stack switching.
1313 */
1314 if ((ka->sa_flags & SA_ONSTACK) && !sas_ss_flags(sp)) {
1315 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1316 }
1317
1318 sp = (sp - sizeof(struct target_rt_sigframe)) & ~15;
1319
1320 return sp;
1321}
1322
1323static void target_setup_frame(int usig, struct target_sigaction *ka,
1324 target_siginfo_t *info, target_sigset_t *set,
1325 CPUARMState *env)
1326{
1327 struct target_rt_sigframe *frame;
Michael Matz8a3ae912014-03-02 19:36:39 +00001328 abi_ulong frame_addr, return_addr;
Andreas Schwab1744aea2013-09-03 20:12:16 +01001329
1330 frame_addr = get_sigframe(ka, env);
1331 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
1332 goto give_sigsegv;
1333 }
1334
1335 __put_user(0, &frame->uc.tuc_flags);
1336 __put_user(0, &frame->uc.tuc_link);
1337
1338 __put_user(target_sigaltstack_used.ss_sp,
1339 &frame->uc.tuc_stack.ss_sp);
1340 __put_user(sas_ss_flags(env->xregs[31]),
1341 &frame->uc.tuc_stack.ss_flags);
1342 __put_user(target_sigaltstack_used.ss_size,
1343 &frame->uc.tuc_stack.ss_size);
1344 target_setup_sigframe(frame, env, set);
Michael Matz8a3ae912014-03-02 19:36:39 +00001345 if (ka->sa_flags & TARGET_SA_RESTORER) {
1346 return_addr = ka->sa_restorer;
1347 } else {
1348 /* mov x8,#__NR_rt_sigreturn; svc #0 */
1349 __put_user(0xd2801168, &frame->tramp[0]);
1350 __put_user(0xd4000001, &frame->tramp[1]);
1351 return_addr = frame_addr + offsetof(struct target_rt_sigframe, tramp);
1352 }
Andreas Schwab1744aea2013-09-03 20:12:16 +01001353 env->xregs[0] = usig;
1354 env->xregs[31] = frame_addr;
1355 env->xregs[29] = env->xregs[31] + offsetof(struct target_rt_sigframe, fp);
1356 env->pc = ka->_sa_handler;
Michael Matz8a3ae912014-03-02 19:36:39 +00001357 env->xregs[30] = return_addr;
Andreas Schwab1744aea2013-09-03 20:12:16 +01001358 if (info) {
Riku Voipiob0fd8d12014-04-23 10:46:13 +03001359 copy_siginfo_to_user(&frame->info, info);
Andreas Schwab1744aea2013-09-03 20:12:16 +01001360 env->xregs[1] = frame_addr + offsetof(struct target_rt_sigframe, info);
1361 env->xregs[2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
1362 }
1363
1364 unlock_user_struct(frame, frame_addr, 1);
1365 return;
1366
1367 give_sigsegv:
1368 unlock_user_struct(frame, frame_addr, 1);
1369 force_sig(TARGET_SIGSEGV);
1370}
1371
1372static void setup_rt_frame(int sig, struct target_sigaction *ka,
1373 target_siginfo_t *info, target_sigset_t *set,
1374 CPUARMState *env)
1375{
1376 target_setup_frame(sig, ka, info, set, env);
1377}
1378
1379static void setup_frame(int sig, struct target_sigaction *ka,
1380 target_sigset_t *set, CPUARMState *env)
1381{
1382 target_setup_frame(sig, ka, 0, set, env);
1383}
1384
1385long do_rt_sigreturn(CPUARMState *env)
1386{
Peter Maydell7f72cd22014-03-12 13:06:00 +00001387 struct target_rt_sigframe *frame = NULL;
Andreas Schwab1744aea2013-09-03 20:12:16 +01001388 abi_ulong frame_addr = env->xregs[31];
1389
1390 if (frame_addr & 15) {
1391 goto badframe;
1392 }
1393
1394 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
1395 goto badframe;
1396 }
1397
1398 if (target_restore_sigframe(env, frame)) {
1399 goto badframe;
1400 }
1401
1402 if (do_sigaltstack(frame_addr +
1403 offsetof(struct target_rt_sigframe, uc.tuc_stack),
1404 0, get_sp_from_cpustate(env)) == -EFAULT) {
1405 goto badframe;
1406 }
1407
1408 unlock_user_struct(frame, frame_addr, 0);
1409 return env->xregs[0];
1410
1411 badframe:
1412 unlock_user_struct(frame, frame_addr, 0);
1413 force_sig(TARGET_SIGSEGV);
1414 return 0;
1415}
1416
1417long do_sigreturn(CPUARMState *env)
1418{
1419 return do_rt_sigreturn(env);
1420}
1421
bellard43fff232003-07-09 19:31:39 +00001422#elif defined(TARGET_ARM)
1423
1424struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001425 abi_ulong trap_no;
1426 abi_ulong error_code;
1427 abi_ulong oldmask;
1428 abi_ulong arm_r0;
1429 abi_ulong arm_r1;
1430 abi_ulong arm_r2;
1431 abi_ulong arm_r3;
1432 abi_ulong arm_r4;
1433 abi_ulong arm_r5;
1434 abi_ulong arm_r6;
1435 abi_ulong arm_r7;
1436 abi_ulong arm_r8;
1437 abi_ulong arm_r9;
1438 abi_ulong arm_r10;
1439 abi_ulong arm_fp;
1440 abi_ulong arm_ip;
1441 abi_ulong arm_sp;
1442 abi_ulong arm_lr;
1443 abi_ulong arm_pc;
1444 abi_ulong arm_cpsr;
1445 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001446};
1447
pbrooka745ec62008-05-06 15:36:17 +00001448struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001449 abi_ulong tuc_flags;
1450 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001451 target_stack_t tuc_stack;
bellardb8076a72005-04-07 22:20:31 +00001452 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001453 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001454};
1455
pbrooka745ec62008-05-06 15:36:17 +00001456struct target_ucontext_v2 {
1457 abi_ulong tuc_flags;
1458 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001459 target_stack_t tuc_stack;
pbrooka745ec62008-05-06 15:36:17 +00001460 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001461 target_sigset_t tuc_sigmask; /* mask last for extensibility */
Peter Maydell5f0b7c82010-11-24 15:20:03 +00001462 char __unused[128 - sizeof(target_sigset_t)];
pbrooka745ec62008-05-06 15:36:17 +00001463 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1464};
1465
Peter Maydell0d871bd2010-11-24 15:20:05 +00001466struct target_user_vfp {
1467 uint64_t fpregs[32];
1468 abi_ulong fpscr;
1469};
1470
1471struct target_user_vfp_exc {
1472 abi_ulong fpexc;
1473 abi_ulong fpinst;
1474 abi_ulong fpinst2;
1475};
1476
1477struct target_vfp_sigframe {
1478 abi_ulong magic;
1479 abi_ulong size;
1480 struct target_user_vfp ufp;
1481 struct target_user_vfp_exc ufp_exc;
1482} __attribute__((__aligned__(8)));
1483
Peter Maydell08e11252010-11-24 15:20:07 +00001484struct target_iwmmxt_sigframe {
1485 abi_ulong magic;
1486 abi_ulong size;
1487 uint64_t regs[16];
1488 /* Note that not all the coprocessor control registers are stored here */
1489 uint32_t wcssf;
1490 uint32_t wcasf;
1491 uint32_t wcgr0;
1492 uint32_t wcgr1;
1493 uint32_t wcgr2;
1494 uint32_t wcgr3;
1495} __attribute__((__aligned__(8)));
1496
Peter Maydell0d871bd2010-11-24 15:20:05 +00001497#define TARGET_VFP_MAGIC 0x56465001
Peter Maydell08e11252010-11-24 15:20:07 +00001498#define TARGET_IWMMXT_MAGIC 0x12ef842a
Peter Maydell0d871bd2010-11-24 15:20:05 +00001499
pbrooka8c33202008-05-07 23:22:46 +00001500struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001501{
1502 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001503 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1504 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001505};
1506
pbrooka8c33202008-05-07 23:22:46 +00001507struct sigframe_v2
1508{
1509 struct target_ucontext_v2 uc;
1510 abi_ulong retcode;
1511};
1512
pbrooka745ec62008-05-06 15:36:17 +00001513struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001514{
bellardf8b0aa22007-11-11 23:03:42 +00001515 abi_ulong pinfo;
1516 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001517 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001518 struct target_ucontext_v1 uc;
1519 abi_ulong retcode;
1520};
1521
1522struct rt_sigframe_v2
1523{
1524 struct target_siginfo info;
1525 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001526 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001527};
1528
1529#define TARGET_CONFIG_CPU_32 1
1530
1531/*
1532 * For ARM syscalls, we encode the syscall number into the instruction.
1533 */
1534#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1535#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1536
1537/*
1538 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1539 * need two 16-bit instructions.
1540 */
1541#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1542#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1543
blueswir1992f48a2007-10-14 16:27:31 +00001544static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001545 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1546 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1547};
1548
1549
Andreas Färber05390242012-02-25 03:37:53 +01001550static inline int valid_user_regs(CPUARMState *regs)
bellard43fff232003-07-09 19:31:39 +00001551{
1552 return 1;
1553}
1554
pbrooka8c33202008-05-07 23:22:46 +00001555static void
bellard43fff232003-07-09 19:31:39 +00001556setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
Andreas Färber05390242012-02-25 03:37:53 +01001557 CPUARMState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001558{
pbrooka8c33202008-05-07 23:22:46 +00001559 __put_user(env->regs[0], &sc->arm_r0);
1560 __put_user(env->regs[1], &sc->arm_r1);
1561 __put_user(env->regs[2], &sc->arm_r2);
1562 __put_user(env->regs[3], &sc->arm_r3);
1563 __put_user(env->regs[4], &sc->arm_r4);
1564 __put_user(env->regs[5], &sc->arm_r5);
1565 __put_user(env->regs[6], &sc->arm_r6);
1566 __put_user(env->regs[7], &sc->arm_r7);
1567 __put_user(env->regs[8], &sc->arm_r8);
1568 __put_user(env->regs[9], &sc->arm_r9);
1569 __put_user(env->regs[10], &sc->arm_r10);
1570 __put_user(env->regs[11], &sc->arm_fp);
1571 __put_user(env->regs[12], &sc->arm_ip);
1572 __put_user(env->regs[13], &sc->arm_sp);
1573 __put_user(env->regs[14], &sc->arm_lr);
1574 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001575#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001576 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001577#endif
1578
pbrooka8c33202008-05-07 23:22:46 +00001579 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1580 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1581 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1582 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001583}
1584
bellard579a97f2007-11-11 14:26:47 +00001585static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01001586get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
bellard43fff232003-07-09 19:31:39 +00001587{
1588 unsigned long sp = regs->regs[13];
1589
bellard43fff232003-07-09 19:31:39 +00001590 /*
1591 * This is the X/Open sanctioned signal stack switching.
1592 */
pbrook624f7972008-05-31 16:11:38 +00001593 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
thsa04e1342007-09-27 13:57:58 +00001594 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001595 /*
1596 * ATPCS B01 mandates 8-byte alignment
1597 */
bellard579a97f2007-11-11 14:26:47 +00001598 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001599}
1600
1601static int
Andreas Färber05390242012-02-25 03:37:53 +01001602setup_return(CPUARMState *env, struct target_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001603 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001604{
pbrook624f7972008-05-31 16:11:38 +00001605 abi_ulong handler = ka->_sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001606 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001607 int thumb = handler & 1;
Peter Maydell964413d2011-01-14 20:39:19 +01001608 uint32_t cpsr = cpsr_read(env);
1609
1610 cpsr &= ~CPSR_IT;
1611 if (thumb) {
1612 cpsr |= CPSR_T;
1613 } else {
1614 cpsr &= ~CPSR_T;
1615 }
bellard43fff232003-07-09 19:31:39 +00001616
pbrook624f7972008-05-31 16:11:38 +00001617 if (ka->sa_flags & TARGET_SA_RESTORER) {
1618 retcode = ka->sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001619 } else {
1620 unsigned int idx = thumb;
1621
pbrook624f7972008-05-31 16:11:38 +00001622 if (ka->sa_flags & TARGET_SA_SIGINFO)
bellard43fff232003-07-09 19:31:39 +00001623 idx += 2;
1624
1625 if (__put_user(retcodes[idx], rc))
1626 return 1;
Stefan Weilca8a2772011-10-03 22:43:19 +02001627
bellardf8b0aa22007-11-11 23:03:42 +00001628 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001629 }
1630
1631 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001632 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001633 env->regs[14] = retcode;
1634 env->regs[15] = handler & (thumb ? ~1 : ~3);
Peter Maydell964413d2011-01-14 20:39:19 +01001635 cpsr_write(env, cpsr, 0xffffffff);
bellard43fff232003-07-09 19:31:39 +00001636
1637 return 0;
1638}
1639
Andreas Färber05390242012-02-25 03:37:53 +01001640static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUARMState *env)
Peter Maydell0d871bd2010-11-24 15:20:05 +00001641{
1642 int i;
1643 struct target_vfp_sigframe *vfpframe;
1644 vfpframe = (struct target_vfp_sigframe *)regspace;
1645 __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1646 __put_user(sizeof(*vfpframe), &vfpframe->size);
1647 for (i = 0; i < 32; i++) {
Peter Maydell005e1a02011-02-10 13:59:35 +00001648 __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
Peter Maydell0d871bd2010-11-24 15:20:05 +00001649 }
1650 __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1651 __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1652 __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1653 __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1654 return (abi_ulong*)(vfpframe+1);
1655}
1656
Andreas Färber05390242012-02-25 03:37:53 +01001657static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace,
1658 CPUARMState *env)
Peter Maydell08e11252010-11-24 15:20:07 +00001659{
1660 int i;
1661 struct target_iwmmxt_sigframe *iwmmxtframe;
1662 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1663 __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1664 __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1665 for (i = 0; i < 16; i++) {
1666 __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1667 }
1668 __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1669 __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1670 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1671 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1672 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1673 __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1674 return (abi_ulong*)(iwmmxtframe+1);
1675}
1676
pbrooka8c33202008-05-07 23:22:46 +00001677static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
Andreas Färber05390242012-02-25 03:37:53 +01001678 target_sigset_t *set, CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001679{
pbrooka8c33202008-05-07 23:22:46 +00001680 struct target_sigaltstack stack;
1681 int i;
Peter Maydell0d871bd2010-11-24 15:20:05 +00001682 abi_ulong *regspace;
pbrooka8c33202008-05-07 23:22:46 +00001683
1684 /* Clear all the bits of the ucontext we don't use. */
1685 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1686
1687 memset(&stack, 0, sizeof(stack));
1688 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1689 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1690 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1691 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1692
1693 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
Peter Maydell0d871bd2010-11-24 15:20:05 +00001694 /* Save coprocessor signal frame. */
1695 regspace = uc->tuc_regspace;
1696 if (arm_feature(env, ARM_FEATURE_VFP)) {
1697 regspace = setup_sigframe_v2_vfp(regspace, env);
1698 }
Peter Maydell08e11252010-11-24 15:20:07 +00001699 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1700 regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1701 }
1702
Peter Maydell0d871bd2010-11-24 15:20:05 +00001703 /* Write terminating magic word */
1704 __put_user(0, regspace);
1705
pbrooka8c33202008-05-07 23:22:46 +00001706 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1707 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1708 }
1709}
1710
1711/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00001712static void setup_frame_v1(int usig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001713 target_sigset_t *set, CPUARMState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001714{
1715 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001716 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001717 int i;
bellard43fff232003-07-09 19:31:39 +00001718
bellard579a97f2007-11-11 14:26:47 +00001719 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1720 return;
1721
pbrooka8c33202008-05-07 23:22:46 +00001722 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001723
bellard92319442004-06-19 16:58:13 +00001724 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1725 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001726 goto end;
bellard43fff232003-07-09 19:31:39 +00001727 }
1728
pbrooka8c33202008-05-07 23:22:46 +00001729 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1730 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001731
1732end:
1733 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001734}
1735
pbrook624f7972008-05-31 16:11:38 +00001736static void setup_frame_v2(int usig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001737 target_sigset_t *set, CPUARMState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001738{
1739 struct sigframe_v2 *frame;
1740 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1741
1742 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1743 return;
1744
1745 setup_sigframe_v2(&frame->uc, set, regs);
1746
1747 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1748 frame_addr + offsetof(struct sigframe_v2, retcode));
1749
1750 unlock_user_struct(frame, frame_addr, 1);
1751}
1752
pbrook624f7972008-05-31 16:11:38 +00001753static void setup_frame(int usig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01001754 target_sigset_t *set, CPUARMState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001755{
1756 if (get_osversion() >= 0x020612) {
1757 setup_frame_v2(usig, ka, set, regs);
1758 } else {
1759 setup_frame_v1(usig, ka, set, regs);
1760 }
bellard43fff232003-07-09 19:31:39 +00001761}
1762
bellard579a97f2007-11-11 14:26:47 +00001763/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +00001764static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001765 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01001766 target_sigset_t *set, CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001767{
pbrooka745ec62008-05-06 15:36:17 +00001768 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001769 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001770 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001771 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001772 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001773
bellard579a97f2007-11-11 14:26:47 +00001774 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001775 return /* 1 */;
1776
pbrooka745ec62008-05-06 15:36:17 +00001777 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001778 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001779 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001780 __put_user(uc_addr, &frame->puc);
1781 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001782
1783 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001784 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001785
thsa04e1342007-09-27 13:57:58 +00001786 memset(&stack, 0, sizeof(stack));
1787 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1788 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1789 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001790 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001791
pbrooka8c33202008-05-07 23:22:46 +00001792 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001793 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001794 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001795 goto end;
bellard92319442004-06-19 16:58:13 +00001796 }
bellard43fff232003-07-09 19:31:39 +00001797
pbrooka8c33202008-05-07 23:22:46 +00001798 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1799 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001800
pbrooka8c33202008-05-07 23:22:46 +00001801 env->regs[1] = info_addr;
1802 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001803
1804end:
1805 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001806}
1807
pbrook624f7972008-05-31 16:11:38 +00001808static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001809 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01001810 target_sigset_t *set, CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00001811{
1812 struct rt_sigframe_v2 *frame;
1813 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001814 abi_ulong info_addr, uc_addr;
1815
1816 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1817 return /* 1 */;
1818
1819 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1820 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001821 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001822
pbrooka8c33202008-05-07 23:22:46 +00001823 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001824
pbrooka8c33202008-05-07 23:22:46 +00001825 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1826 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001827
pbrooka8c33202008-05-07 23:22:46 +00001828 env->regs[1] = info_addr;
1829 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001830
bellard579a97f2007-11-11 14:26:47 +00001831 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001832}
1833
pbrook624f7972008-05-31 16:11:38 +00001834static void setup_rt_frame(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001835 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01001836 target_sigset_t *set, CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00001837{
1838 if (get_osversion() >= 0x020612) {
1839 setup_rt_frame_v2(usig, ka, info, set, env);
1840 } else {
1841 setup_rt_frame_v1(usig, ka, info, set, env);
1842 }
1843}
1844
bellard43fff232003-07-09 19:31:39 +00001845static int
Andreas Färber05390242012-02-25 03:37:53 +01001846restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
bellard43fff232003-07-09 19:31:39 +00001847{
1848 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001849 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001850
Riku Voipio1d8b5122014-04-23 10:26:05 +03001851 __get_user(env->regs[0], &sc->arm_r0);
1852 __get_user(env->regs[1], &sc->arm_r1);
1853 __get_user(env->regs[2], &sc->arm_r2);
1854 __get_user(env->regs[3], &sc->arm_r3);
1855 __get_user(env->regs[4], &sc->arm_r4);
1856 __get_user(env->regs[5], &sc->arm_r5);
1857 __get_user(env->regs[6], &sc->arm_r6);
1858 __get_user(env->regs[7], &sc->arm_r7);
1859 __get_user(env->regs[8], &sc->arm_r8);
1860 __get_user(env->regs[9], &sc->arm_r9);
1861 __get_user(env->regs[10], &sc->arm_r10);
1862 __get_user(env->regs[11], &sc->arm_fp);
1863 __get_user(env->regs[12], &sc->arm_ip);
1864 __get_user(env->regs[13], &sc->arm_sp);
1865 __get_user(env->regs[14], &sc->arm_lr);
1866 __get_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001867#ifdef TARGET_CONFIG_CPU_32
Riku Voipio1d8b5122014-04-23 10:26:05 +03001868 __get_user(cpsr, &sc->arm_cpsr);
pbrook75b680e2008-03-21 16:07:30 +00001869 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001870#endif
1871
1872 err |= !valid_user_regs(env);
1873
1874 return err;
1875}
1876
Andreas Färber05390242012-02-25 03:37:53 +01001877static long do_sigreturn_v1(CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00001878{
bellardf8b0aa22007-11-11 23:03:42 +00001879 abi_ulong frame_addr;
Peter Maydell978fae92013-07-29 12:00:32 +01001880 struct sigframe_v1 *frame = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -05001881 target_sigset_t set;
bellard43fff232003-07-09 19:31:39 +00001882 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001883 int i;
bellard43fff232003-07-09 19:31:39 +00001884
1885 /*
1886 * Since we stacked the signal on a 64-bit boundary,
1887 * then 'sp' should be word aligned here. If it's
1888 * not, then the user is trying to mess with us.
1889 */
bellardf8b0aa22007-11-11 23:03:42 +00001890 frame_addr = env->regs[13];
Peter Maydell978fae92013-07-29 12:00:32 +01001891 if (frame_addr & 7) {
1892 goto badframe;
1893 }
1894
bellardf8b0aa22007-11-11 23:03:42 +00001895 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1896 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001897
bellard92319442004-06-19 16:58:13 +00001898 if (__get_user(set.sig[0], &frame->sc.oldmask))
1899 goto badframe;
1900 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1901 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1902 goto badframe;
1903 }
bellard43fff232003-07-09 19:31:39 +00001904
bellard92319442004-06-19 16:58:13 +00001905 target_to_host_sigset_internal(&host_set, &set);
Alex Barcelo1c275922014-03-14 14:36:55 +00001906 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard43fff232003-07-09 19:31:39 +00001907
1908 if (restore_sigcontext(env, &frame->sc))
1909 goto badframe;
1910
1911#if 0
1912 /* Send SIGTRAP if we're single-stepping */
1913 if (ptrace_cancel_bpt(current))
1914 send_sig(SIGTRAP, current, 1);
1915#endif
bellardf8b0aa22007-11-11 23:03:42 +00001916 unlock_user_struct(frame, frame_addr, 0);
1917 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001918
1919badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001920 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001921 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00001922 return 0;
1923}
1924
Andreas Färber05390242012-02-25 03:37:53 +01001925static abi_ulong *restore_sigframe_v2_vfp(CPUARMState *env, abi_ulong *regspace)
Peter Maydell5f9099d2010-11-24 15:20:06 +00001926{
1927 int i;
1928 abi_ulong magic, sz;
1929 uint32_t fpscr, fpexc;
1930 struct target_vfp_sigframe *vfpframe;
1931 vfpframe = (struct target_vfp_sigframe *)regspace;
1932
1933 __get_user(magic, &vfpframe->magic);
1934 __get_user(sz, &vfpframe->size);
1935 if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1936 return 0;
1937 }
1938 for (i = 0; i < 32; i++) {
Peter Maydell005e1a02011-02-10 13:59:35 +00001939 __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
Peter Maydell5f9099d2010-11-24 15:20:06 +00001940 }
1941 __get_user(fpscr, &vfpframe->ufp.fpscr);
1942 vfp_set_fpscr(env, fpscr);
1943 __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1944 /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1945 * and the exception flag is cleared
1946 */
1947 fpexc |= (1 << 30);
1948 fpexc &= ~((1 << 31) | (1 << 28));
1949 env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1950 __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1951 __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1952 return (abi_ulong*)(vfpframe + 1);
1953}
1954
Andreas Färber05390242012-02-25 03:37:53 +01001955static abi_ulong *restore_sigframe_v2_iwmmxt(CPUARMState *env,
1956 abi_ulong *regspace)
Peter Maydella59d69d2010-11-24 15:20:08 +00001957{
1958 int i;
1959 abi_ulong magic, sz;
1960 struct target_iwmmxt_sigframe *iwmmxtframe;
1961 iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1962
1963 __get_user(magic, &iwmmxtframe->magic);
1964 __get_user(sz, &iwmmxtframe->size);
1965 if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1966 return 0;
1967 }
1968 for (i = 0; i < 16; i++) {
1969 __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1970 }
1971 __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1972 __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1973 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1974 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1975 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1976 __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1977 return (abi_ulong*)(iwmmxtframe + 1);
1978}
1979
Andreas Färber05390242012-02-25 03:37:53 +01001980static int do_sigframe_return_v2(CPUARMState *env, target_ulong frame_addr,
pbrooka8c33202008-05-07 23:22:46 +00001981 struct target_ucontext_v2 *uc)
1982{
1983 sigset_t host_set;
Peter Maydell5f9099d2010-11-24 15:20:06 +00001984 abi_ulong *regspace;
pbrooka8c33202008-05-07 23:22:46 +00001985
1986 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00001987 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
pbrooka8c33202008-05-07 23:22:46 +00001988
1989 if (restore_sigcontext(env, &uc->tuc_mcontext))
1990 return 1;
1991
Peter Maydell5f9099d2010-11-24 15:20:06 +00001992 /* Restore coprocessor signal frame */
1993 regspace = uc->tuc_regspace;
1994 if (arm_feature(env, ARM_FEATURE_VFP)) {
1995 regspace = restore_sigframe_v2_vfp(env, regspace);
1996 if (!regspace) {
1997 return 1;
1998 }
1999 }
Peter Maydella59d69d2010-11-24 15:20:08 +00002000 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
2001 regspace = restore_sigframe_v2_iwmmxt(env, regspace);
2002 if (!regspace) {
2003 return 1;
2004 }
2005 }
Peter Maydell5f9099d2010-11-24 15:20:06 +00002006
pbrooka8c33202008-05-07 23:22:46 +00002007 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
2008 return 1;
2009
2010#if 0
2011 /* Send SIGTRAP if we're single-stepping */
2012 if (ptrace_cancel_bpt(current))
2013 send_sig(SIGTRAP, current, 1);
2014#endif
2015
2016 return 0;
2017}
2018
Andreas Färber05390242012-02-25 03:37:53 +01002019static long do_sigreturn_v2(CPUARMState *env)
pbrooka8c33202008-05-07 23:22:46 +00002020{
2021 abi_ulong frame_addr;
Peter Maydell978fae92013-07-29 12:00:32 +01002022 struct sigframe_v2 *frame = NULL;
pbrooka8c33202008-05-07 23:22:46 +00002023
2024 /*
2025 * Since we stacked the signal on a 64-bit boundary,
2026 * then 'sp' should be word aligned here. If it's
2027 * not, then the user is trying to mess with us.
2028 */
pbrooka8c33202008-05-07 23:22:46 +00002029 frame_addr = env->regs[13];
Peter Maydell978fae92013-07-29 12:00:32 +01002030 if (frame_addr & 7) {
2031 goto badframe;
2032 }
2033
pbrooka8c33202008-05-07 23:22:46 +00002034 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2035 goto badframe;
2036
2037 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2038 goto badframe;
2039
2040 unlock_user_struct(frame, frame_addr, 0);
2041 return env->regs[0];
2042
2043badframe:
2044 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002045 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka8c33202008-05-07 23:22:46 +00002046 return 0;
2047}
2048
Andreas Färber05390242012-02-25 03:37:53 +01002049long do_sigreturn(CPUARMState *env)
pbrooka8c33202008-05-07 23:22:46 +00002050{
2051 if (get_osversion() >= 0x020612) {
2052 return do_sigreturn_v2(env);
2053 } else {
2054 return do_sigreturn_v1(env);
2055 }
2056}
2057
Andreas Färber05390242012-02-25 03:37:53 +01002058static long do_rt_sigreturn_v1(CPUARMState *env)
bellard43fff232003-07-09 19:31:39 +00002059{
bellardf8b0aa22007-11-11 23:03:42 +00002060 abi_ulong frame_addr;
Peter Maydell978fae92013-07-29 12:00:32 +01002061 struct rt_sigframe_v1 *frame = NULL;
bellard43fff232003-07-09 19:31:39 +00002062 sigset_t host_set;
2063
2064 /*
2065 * Since we stacked the signal on a 64-bit boundary,
2066 * then 'sp' should be word aligned here. If it's
2067 * not, then the user is trying to mess with us.
2068 */
bellardf8b0aa22007-11-11 23:03:42 +00002069 frame_addr = env->regs[13];
Peter Maydell978fae92013-07-29 12:00:32 +01002070 if (frame_addr & 7) {
2071 goto badframe;
2072 }
2073
bellardf8b0aa22007-11-11 23:03:42 +00002074 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2075 goto badframe;
bellard43fff232003-07-09 19:31:39 +00002076
bellardb8076a72005-04-07 22:20:31 +00002077 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00002078 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard43fff232003-07-09 19:31:39 +00002079
bellardb8076a72005-04-07 22:20:31 +00002080 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00002081 goto badframe;
2082
pbrooka745ec62008-05-06 15:36:17 +00002083 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 +00002084 goto badframe;
2085
bellard43fff232003-07-09 19:31:39 +00002086#if 0
2087 /* Send SIGTRAP if we're single-stepping */
2088 if (ptrace_cancel_bpt(current))
2089 send_sig(SIGTRAP, current, 1);
2090#endif
bellardf8b0aa22007-11-11 23:03:42 +00002091 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00002092 return env->regs[0];
2093
2094badframe:
bellardf8b0aa22007-11-11 23:03:42 +00002095 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002096 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00002097 return 0;
2098}
2099
Andreas Färber05390242012-02-25 03:37:53 +01002100static long do_rt_sigreturn_v2(CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00002101{
2102 abi_ulong frame_addr;
Peter Maydell978fae92013-07-29 12:00:32 +01002103 struct rt_sigframe_v2 *frame = NULL;
pbrooka745ec62008-05-06 15:36:17 +00002104
2105 /*
2106 * Since we stacked the signal on a 64-bit boundary,
2107 * then 'sp' should be word aligned here. If it's
2108 * not, then the user is trying to mess with us.
2109 */
pbrooka745ec62008-05-06 15:36:17 +00002110 frame_addr = env->regs[13];
Peter Maydell978fae92013-07-29 12:00:32 +01002111 if (frame_addr & 7) {
2112 goto badframe;
2113 }
2114
pbrooka745ec62008-05-06 15:36:17 +00002115 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2116 goto badframe;
2117
pbrooka8c33202008-05-07 23:22:46 +00002118 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
2119 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00002120
pbrooka745ec62008-05-06 15:36:17 +00002121 unlock_user_struct(frame, frame_addr, 0);
2122 return env->regs[0];
2123
2124badframe:
2125 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002126 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka745ec62008-05-06 15:36:17 +00002127 return 0;
2128}
2129
Andreas Färber05390242012-02-25 03:37:53 +01002130long do_rt_sigreturn(CPUARMState *env)
pbrooka745ec62008-05-06 15:36:17 +00002131{
2132 if (get_osversion() >= 0x020612) {
2133 return do_rt_sigreturn_v2(env);
2134 } else {
2135 return do_rt_sigreturn_v1(env);
2136 }
2137}
2138
bellard6d5e2162004-09-30 22:04:13 +00002139#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00002140
bellard6d5e2162004-09-30 22:04:13 +00002141#define __SUNOS_MAXWIN 31
2142
2143/* This is what SunOS does, so shall I. */
2144struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00002145 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00002146
blueswir1992f48a2007-10-14 16:27:31 +00002147 abi_ulong sigc_mask; /* sigmask to restore */
2148 abi_ulong sigc_sp; /* stack pointer */
2149 abi_ulong sigc_pc; /* program counter */
2150 abi_ulong sigc_npc; /* next program counter */
2151 abi_ulong sigc_psr; /* for condition codes etc */
2152 abi_ulong sigc_g1; /* User uses these two registers */
2153 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00002154
2155 /* Now comes information regarding the users window set
2156 * at the time of the signal.
2157 */
blueswir1992f48a2007-10-14 16:27:31 +00002158 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00002159
2160 /* stack ptrs for each regwin buf */
2161 char *sigc_spbuf[__SUNOS_MAXWIN];
2162
2163 /* Windows to restore after signal */
2164 struct {
blueswir1992f48a2007-10-14 16:27:31 +00002165 abi_ulong locals[8];
2166 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00002167 } sigc_wbuf[__SUNOS_MAXWIN];
2168};
2169/* A Sparc stack frame */
2170struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00002171 abi_ulong locals[8];
Peter Maydelle321c342011-02-01 15:54:52 +00002172 abi_ulong ins[8];
2173 /* It's simpler to treat fp and callers_pc as elements of ins[]
2174 * since we never need to access them ourselves.
2175 */
bellard6d5e2162004-09-30 22:04:13 +00002176 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00002177 abi_ulong xargs[6];
2178 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00002179};
2180
2181typedef struct {
2182 struct {
blueswir1992f48a2007-10-14 16:27:31 +00002183 abi_ulong psr;
2184 abi_ulong pc;
2185 abi_ulong npc;
2186 abi_ulong y;
2187 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00002188 } si_regs;
2189 int si_mask;
2190} __siginfo_t;
2191
2192typedef struct {
Blue Swirl8954bae2012-07-30 15:29:11 +00002193 abi_ulong si_float_regs[32];
bellard6d5e2162004-09-30 22:04:13 +00002194 unsigned long si_fsr;
2195 unsigned long si_fpqdepth;
2196 struct {
2197 unsigned long *insn_addr;
2198 unsigned long insn;
2199 } si_fpqueue [16];
Anthony Liguoric227f092009-10-01 16:12:16 -05002200} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00002201
2202
2203struct target_signal_frame {
2204 struct sparc_stackf ss;
2205 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00002206 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00002207 abi_ulong insns[2] __attribute__ ((aligned (8)));
2208 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
2209 abi_ulong extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002210 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00002211};
2212struct target_rt_signal_frame {
2213 struct sparc_stackf ss;
2214 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00002215 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00002216 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00002217 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00002218 unsigned int insns[2];
2219 stack_t stack;
2220 unsigned int extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05002221 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00002222};
2223
bellarde80cfcf2004-12-19 23:18:01 +00002224#define UREG_O0 16
2225#define UREG_O6 22
2226#define UREG_I0 0
2227#define UREG_I1 1
2228#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00002229#define UREG_I3 3
2230#define UREG_I4 4
2231#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00002232#define UREG_I6 6
2233#define UREG_I7 7
2234#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00002235#define UREG_FP UREG_I6
2236#define UREG_SP UREG_O6
2237
pbrook624f7972008-05-31 16:11:38 +00002238static inline abi_ulong get_sigframe(struct target_sigaction *sa,
Andreas Färber05390242012-02-25 03:37:53 +01002239 CPUSPARCState *env,
2240 unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00002241{
bellard459a4012007-11-11 19:45:10 +00002242 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00002243
2244 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00002245
2246 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002247 if (sa->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +00002248 if (!on_sig_stack(sp)
2249 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
2250 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00002251 }
bellard459a4012007-11-11 19:45:10 +00002252 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00002253}
2254
2255static int
Andreas Färber05390242012-02-25 03:37:53 +01002256setup___siginfo(__siginfo_t *si, CPUSPARCState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00002257{
2258 int err = 0, i;
2259
Riku Voipio1d8b5122014-04-23 10:26:05 +03002260 __put_user(env->psr, &si->si_regs.psr);
2261 __put_user(env->pc, &si->si_regs.pc);
2262 __put_user(env->npc, &si->si_regs.npc);
2263 __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00002264 for (i=0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002265 __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
bellard6d5e2162004-09-30 22:04:13 +00002266 }
bellarda315a142005-01-30 22:59:18 +00002267 for (i=0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002268 __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00002269 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03002270 __put_user(mask, &si->si_mask);
bellard6d5e2162004-09-30 22:04:13 +00002271 return err;
2272}
bellarde80cfcf2004-12-19 23:18:01 +00002273
bellard80a9d032005-01-03 23:31:27 +00002274#if 0
bellard6d5e2162004-09-30 22:04:13 +00002275static int
2276setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
Andreas Färber05390242012-02-25 03:37:53 +01002277 CPUSPARCState *env, unsigned long mask)
bellard6d5e2162004-09-30 22:04:13 +00002278{
2279 int err = 0;
2280
Riku Voipio1d8b5122014-04-23 10:26:05 +03002281 __put_user(mask, &sc->sigc_mask);
2282 __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
2283 __put_user(env->pc, &sc->sigc_pc);
2284 __put_user(env->npc, &sc->sigc_npc);
2285 __put_user(env->psr, &sc->sigc_psr);
2286 __put_user(env->gregs[1], &sc->sigc_g1);
2287 __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
bellard6d5e2162004-09-30 22:04:13 +00002288
2289 return err;
2290}
bellard80a9d032005-01-03 23:31:27 +00002291#endif
bellard6d5e2162004-09-30 22:04:13 +00002292#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
2293
pbrook624f7972008-05-31 16:11:38 +00002294static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01002295 target_sigset_t *set, CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002296{
bellard459a4012007-11-11 19:45:10 +00002297 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00002298 struct target_signal_frame *sf;
2299 int sigframe_size, err, i;
2300
2301 /* 1. Make sure everything is clean */
2302 //synchronize_user_stack();
2303
2304 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00002305 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00002306
bellard459a4012007-11-11 19:45:10 +00002307 sf = lock_user(VERIFY_WRITE, sf_addr,
2308 sizeof(struct target_signal_frame), 0);
2309 if (!sf)
2310 goto sigsegv;
2311
bellarde80cfcf2004-12-19 23:18:01 +00002312 //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 +00002313#if 0
2314 if (invalid_frame_pointer(sf, sigframe_size))
2315 goto sigill_and_return;
2316#endif
2317 /* 2. Save the current process state */
2318 err = setup___siginfo(&sf->info, env, set->sig[0]);
Riku Voipio1d8b5122014-04-23 10:26:05 +03002319 __put_user(0, &sf->extra_size);
bellard6d5e2162004-09-30 22:04:13 +00002320
Riku Voipio1d8b5122014-04-23 10:26:05 +03002321 //save_fpu_state(regs, &sf->fpu_state);
2322 //__put_user(&sf->fpu_state, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00002323
Riku Voipio1d8b5122014-04-23 10:26:05 +03002324 __put_user(set->sig[0], &sf->info.si_mask);
bellard6d5e2162004-09-30 22:04:13 +00002325 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002326 __put_user(set->sig[i + 1], &sf->extramask[i]);
bellard6d5e2162004-09-30 22:04:13 +00002327 }
2328
bellarda315a142005-01-30 22:59:18 +00002329 for (i = 0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002330 __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00002331 }
bellarda315a142005-01-30 22:59:18 +00002332 for (i = 0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002333 __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00002334 }
bellard6d5e2162004-09-30 22:04:13 +00002335 if (err)
2336 goto sigsegv;
2337
2338 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00002339 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00002340 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00002341 env->regwptr[UREG_I1] = sf_addr +
2342 offsetof(struct target_signal_frame, info);
2343 env->regwptr[UREG_I2] = sf_addr +
2344 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00002345
2346 /* 4. signal handler */
pbrook624f7972008-05-31 16:11:38 +00002347 env->pc = ka->_sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00002348 env->npc = (env->pc + 4);
2349 /* 5. return to kernel instructions */
pbrook624f7972008-05-31 16:11:38 +00002350 if (ka->sa_restorer)
2351 env->regwptr[UREG_I7] = ka->sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00002352 else {
bellard775b58d2007-11-11 16:22:17 +00002353 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00002354
2355 env->regwptr[UREG_I7] = sf_addr +
2356 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00002357
2358 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00002359 val32 = 0x821020d8;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002360 __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00002361
2362 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00002363 val32 = 0x91d02010;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002364 __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00002365 if (err)
2366 goto sigsegv;
2367
2368 /* Flush instruction space. */
2369 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00002370 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00002371 }
bellard459a4012007-11-11 19:45:10 +00002372 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00002373 return;
bellard459a4012007-11-11 19:45:10 +00002374#if 0
2375sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00002376 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00002377#endif
bellard6d5e2162004-09-30 22:04:13 +00002378sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00002379 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00002380 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00002381 force_sig(TARGET_SIGSEGV);
2382}
2383static inline int
Andreas Färber05390242012-02-25 03:37:53 +01002384restore_fpu_state(CPUSPARCState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00002385{
2386 int err;
2387#if 0
2388#ifdef CONFIG_SMP
2389 if (current->flags & PF_USEDFPU)
2390 regs->psr &= ~PSR_EF;
2391#else
2392 if (current == last_task_used_math) {
2393 last_task_used_math = 0;
2394 regs->psr &= ~PSR_EF;
2395 }
2396#endif
2397 current->used_math = 1;
2398 current->flags &= ~PF_USEDFPU;
2399#endif
2400#if 0
2401 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2402 return -EFAULT;
2403#endif
2404
bellardfafffae2006-10-28 12:09:16 +00002405 /* XXX: incorrect */
Blue Swirl8954bae2012-07-30 15:29:11 +00002406 err = copy_from_user(&env->fpr[0], fpu->si_float_regs[0],
2407 (sizeof(abi_ulong) * 32));
bellard6d5e2162004-09-30 22:04:13 +00002408 err |= __get_user(env->fsr, &fpu->si_fsr);
2409#if 0
2410 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2411 if (current->thread.fpqdepth != 0)
2412 err |= __copy_from_user(&current->thread.fpqueue[0],
2413 &fpu->si_fpqueue[0],
2414 ((sizeof(unsigned long) +
2415 (sizeof(unsigned long *)))*16));
2416#endif
2417 return err;
2418}
2419
2420
pbrook624f7972008-05-31 16:11:38 +00002421static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002422 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01002423 target_sigset_t *set, CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002424{
2425 fprintf(stderr, "setup_rt_frame: not implemented\n");
2426}
2427
Andreas Färber05390242012-02-25 03:37:53 +01002428long do_sigreturn(CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002429{
bellardf8b0aa22007-11-11 23:03:42 +00002430 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00002431 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00002432 uint32_t up_psr, pc, npc;
Anthony Liguoric227f092009-10-01 16:12:16 -05002433 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00002434 sigset_t host_set;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002435 int err=0, i;
bellard6d5e2162004-09-30 22:04:13 +00002436
bellardf8b0aa22007-11-11 23:03:42 +00002437 sf_addr = env->regwptr[UREG_FP];
2438 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2439 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00002440#if 0
bellarde80cfcf2004-12-19 23:18:01 +00002441 fprintf(stderr, "sigreturn\n");
2442 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 +00002443#endif
bellarde80cfcf2004-12-19 23:18:01 +00002444 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00002445
2446 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00002447
bellardf8b0aa22007-11-11 23:03:42 +00002448 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00002449 goto segv_and_exit;
2450
Riku Voipio1d8b5122014-04-23 10:26:05 +03002451 __get_user(pc, &sf->info.si_regs.pc);
2452 __get_user(npc, &sf->info.si_regs.npc);
bellard6d5e2162004-09-30 22:04:13 +00002453
bellard6d5e2162004-09-30 22:04:13 +00002454 if ((pc | npc) & 3)
2455 goto segv_and_exit;
2456
2457 /* 2. Restore the state */
Riku Voipio1d8b5122014-04-23 10:26:05 +03002458 __get_user(up_psr, &sf->info.si_regs.psr);
bellarde80cfcf2004-12-19 23:18:01 +00002459
bellard6d5e2162004-09-30 22:04:13 +00002460 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00002461 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2462 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2463
2464 env->pc = pc;
2465 env->npc = npc;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002466 __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00002467 for (i=0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002468 __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
bellarde80cfcf2004-12-19 23:18:01 +00002469 }
bellarda315a142005-01-30 22:59:18 +00002470 for (i=0; i < 8; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002471 __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
bellarde80cfcf2004-12-19 23:18:01 +00002472 }
bellard6d5e2162004-09-30 22:04:13 +00002473
Peter Maydell2aec3a22011-06-16 17:37:14 +01002474 /* FIXME: implement FPU save/restore:
2475 * __get_user(fpu_save, &sf->fpu_save);
2476 * if (fpu_save)
2477 * err |= restore_fpu_state(env, fpu_save);
2478 */
bellard6d5e2162004-09-30 22:04:13 +00002479
2480 /* This is pretty much atomic, no amount locking would prevent
2481 * the races which exist anyways.
2482 */
Riku Voipio1d8b5122014-04-23 10:26:05 +03002483 __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00002484 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002485 __get_user(set.sig[i], &sf->extramask[i - 1]);
bellarde80cfcf2004-12-19 23:18:01 +00002486 }
2487
2488 target_to_host_sigset_internal(&host_set, &set);
Alex Barcelo1c275922014-03-14 14:36:55 +00002489 do_sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00002490
2491 if (err)
2492 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00002493 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00002494 return env->regwptr[0];
2495
2496segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00002497 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00002498 force_sig(TARGET_SIGSEGV);
2499}
2500
Andreas Färber05390242012-02-25 03:37:53 +01002501long do_rt_sigreturn(CPUSPARCState *env)
bellard6d5e2162004-09-30 22:04:13 +00002502{
2503 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002504 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00002505}
2506
bellard459a4012007-11-11 19:45:10 +00002507#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00002508#define MC_TSTATE 0
2509#define MC_PC 1
2510#define MC_NPC 2
2511#define MC_Y 3
2512#define MC_G1 4
2513#define MC_G2 5
2514#define MC_G3 6
2515#define MC_G4 7
2516#define MC_G5 8
2517#define MC_G6 9
2518#define MC_G7 10
2519#define MC_O0 11
2520#define MC_O1 12
2521#define MC_O2 13
2522#define MC_O3 14
2523#define MC_O4 15
2524#define MC_O5 16
2525#define MC_O6 17
2526#define MC_O7 18
2527#define MC_NGREG 19
2528
Anthony Liguoric227f092009-10-01 16:12:16 -05002529typedef abi_ulong target_mc_greg_t;
2530typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
blueswir15bfb56b2007-10-05 17:01:51 +00002531
2532struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00002533 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002534 uint32_t mcfq_insn;
2535};
2536
2537struct target_mc_fpu {
2538 union {
2539 uint32_t sregs[32];
2540 uint64_t dregs[32];
2541 //uint128_t qregs[16];
2542 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00002543 abi_ulong mcfpu_fsr;
2544 abi_ulong mcfpu_fprs;
2545 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00002546 struct target_mc_fq *mcfpu_fq;
2547 unsigned char mcfpu_qcnt;
2548 unsigned char mcfpu_qentsz;
2549 unsigned char mcfpu_enab;
2550};
Anthony Liguoric227f092009-10-01 16:12:16 -05002551typedef struct target_mc_fpu target_mc_fpu_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002552
2553typedef struct {
Anthony Liguoric227f092009-10-01 16:12:16 -05002554 target_mc_gregset_t mc_gregs;
2555 target_mc_greg_t mc_fp;
2556 target_mc_greg_t mc_i7;
2557 target_mc_fpu_t mc_fpregs;
2558} target_mcontext_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002559
2560struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002561 struct target_ucontext *tuc_link;
2562 abi_ulong tuc_flags;
2563 target_sigset_t tuc_sigmask;
2564 target_mcontext_t tuc_mcontext;
blueswir15bfb56b2007-10-05 17:01:51 +00002565};
2566
2567/* A V9 register window */
2568struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00002569 abi_ulong locals[8];
2570 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00002571};
2572
2573#define TARGET_STACK_BIAS 2047
2574
2575/* {set, get}context() needed for 64-bit SparcLinux userland. */
2576void sparc64_set_context(CPUSPARCState *env)
2577{
bellard459a4012007-11-11 19:45:10 +00002578 abi_ulong ucp_addr;
2579 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002580 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00002581 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00002582 abi_ulong fp, i7, w_addr;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002583 int err = 0;
blueswir15bfb56b2007-10-05 17:01:51 +00002584 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002585
bellard459a4012007-11-11 19:45:10 +00002586 ucp_addr = env->regwptr[UREG_I0];
2587 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2588 goto do_sigsegv;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002589 grp = &ucp->tuc_mcontext.mc_gregs;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002590 __get_user(pc, &((*grp)[MC_PC]));
2591 __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002592 if (err || ((pc | npc) & 3))
2593 goto do_sigsegv;
2594 if (env->regwptr[UREG_I1]) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002595 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002596 sigset_t set;
2597
2598 if (TARGET_NSIG_WORDS == 1) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002599 if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002600 goto do_sigsegv;
2601 } else {
bellard459a4012007-11-11 19:45:10 +00002602 abi_ulong *src, *dst;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002603 src = ucp->tuc_sigmask.sig;
bellard459a4012007-11-11 19:45:10 +00002604 dst = target_set.sig;
Stefan Weil0d9e61c2013-12-07 14:48:08 +01002605 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002606 __get_user(*dst, src);
Stefan Weil0d9e61c2013-12-07 14:48:08 +01002607 }
blueswir15bfb56b2007-10-05 17:01:51 +00002608 if (err)
2609 goto do_sigsegv;
2610 }
2611 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00002612 do_sigprocmask(SIG_SETMASK, &set, NULL);
blueswir15bfb56b2007-10-05 17:01:51 +00002613 }
2614 env->pc = pc;
2615 env->npc = npc;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002616 __get_user(env->y, &((*grp)[MC_Y]));
2617 __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002618 env->asi = (tstate >> 24) & 0xff;
Blue Swirl5a834bb2010-05-09 20:19:04 +00002619 cpu_put_ccr(env, tstate >> 32);
2620 cpu_put_cwp64(env, tstate & 0x1f);
Riku Voipio1d8b5122014-04-23 10:26:05 +03002621 __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2622 __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2623 __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2624 __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2625 __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2626 __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2627 __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2628 __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2629 __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2630 __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2631 __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2632 __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2633 __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2634 __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2635 __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002636
Riku Voipio1d8b5122014-04-23 10:26:05 +03002637 __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2638 __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002639
bellard459a4012007-11-11 19:45:10 +00002640 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2641 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2642 abi_ulong) != 0)
2643 goto do_sigsegv;
2644 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2645 abi_ulong) != 0)
2646 goto do_sigsegv;
Peter Maydellc7b016b2011-06-16 17:37:15 +01002647 /* FIXME this does not match how the kernel handles the FPU in
2648 * its sparc64_set_context implementation. In particular the FPU
2649 * is only restored if fenab is non-zero in:
2650 * __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2651 */
Aurelien Jarno60e99242010-03-29 02:12:51 +02002652 err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002653 {
Richard Henderson30038fd2011-10-17 10:42:49 -07002654 uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2655 for (i = 0; i < 64; i++, src++) {
2656 if (i & 1) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002657 __get_user(env->fpr[i/2].l.lower, src);
Richard Henderson30038fd2011-10-17 10:42:49 -07002658 } else {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002659 __get_user(env->fpr[i/2].l.upper, src);
Richard Henderson30038fd2011-10-17 10:42:49 -07002660 }
2661 }
bellard459a4012007-11-11 19:45:10 +00002662 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03002663 __get_user(env->fsr,
2664 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2665 __get_user(env->gsr,
2666 &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002667 if (err)
2668 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002669 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002670 return;
2671 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002672 unlock_user_struct(ucp, ucp_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002673 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002674}
2675
2676void sparc64_get_context(CPUSPARCState *env)
2677{
bellard459a4012007-11-11 19:45:10 +00002678 abi_ulong ucp_addr;
2679 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002680 target_mc_gregset_t *grp;
2681 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002682 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002683 int err;
2684 unsigned int i;
Anthony Liguoric227f092009-10-01 16:12:16 -05002685 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002686 sigset_t set;
2687
bellard459a4012007-11-11 19:45:10 +00002688 ucp_addr = env->regwptr[UREG_I0];
2689 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2690 goto do_sigsegv;
2691
Aurelien Jarno60e99242010-03-29 02:12:51 +02002692 mcp = &ucp->tuc_mcontext;
blueswir15bfb56b2007-10-05 17:01:51 +00002693 grp = &mcp->mc_gregs;
2694
2695 /* Skip over the trap instruction, first. */
2696 env->pc = env->npc;
2697 env->npc += 4;
2698
2699 err = 0;
2700
Alex Barcelo1c275922014-03-14 14:36:55 +00002701 do_sigprocmask(0, NULL, &set);
blueswir15bfb56b2007-10-05 17:01:51 +00002702 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002703 if (TARGET_NSIG_WORDS == 1) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002704 __put_user(target_set.sig[0],
2705 (abi_ulong *)&ucp->tuc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002706 } else {
2707 abi_ulong *src, *dst;
2708 src = target_set.sig;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002709 dst = ucp->tuc_sigmask.sig;
Stefan Weil0d9e61c2013-12-07 14:48:08 +01002710 for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002711 __put_user(*src, dst);
Stefan Weil0d9e61c2013-12-07 14:48:08 +01002712 }
blueswir15bfb56b2007-10-05 17:01:51 +00002713 if (err)
2714 goto do_sigsegv;
2715 }
2716
bellard459a4012007-11-11 19:45:10 +00002717 /* XXX: tstate must be saved properly */
Riku Voipio1d8b5122014-04-23 10:26:05 +03002718 // __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2719 __put_user(env->pc, &((*grp)[MC_PC]));
2720 __put_user(env->npc, &((*grp)[MC_NPC]));
2721 __put_user(env->y, &((*grp)[MC_Y]));
2722 __put_user(env->gregs[1], &((*grp)[MC_G1]));
2723 __put_user(env->gregs[2], &((*grp)[MC_G2]));
2724 __put_user(env->gregs[3], &((*grp)[MC_G3]));
2725 __put_user(env->gregs[4], &((*grp)[MC_G4]));
2726 __put_user(env->gregs[5], &((*grp)[MC_G5]));
2727 __put_user(env->gregs[6], &((*grp)[MC_G6]));
2728 __put_user(env->gregs[7], &((*grp)[MC_G7]));
2729 __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2730 __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2731 __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2732 __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2733 __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2734 __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2735 __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2736 __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002737
bellard459a4012007-11-11 19:45:10 +00002738 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2739 fp = i7 = 0;
2740 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2741 abi_ulong) != 0)
2742 goto do_sigsegv;
2743 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2744 abi_ulong) != 0)
2745 goto do_sigsegv;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002746 __put_user(fp, &(mcp->mc_fp));
2747 __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002748
bellard459a4012007-11-11 19:45:10 +00002749 {
Richard Henderson30038fd2011-10-17 10:42:49 -07002750 uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2751 for (i = 0; i < 64; i++, dst++) {
2752 if (i & 1) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002753 __put_user(env->fpr[i/2].l.lower, dst);
Richard Henderson30038fd2011-10-17 10:42:49 -07002754 } else {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002755 __put_user(env->fpr[i/2].l.upper, dst);
Richard Henderson30038fd2011-10-17 10:42:49 -07002756 }
2757 }
bellard459a4012007-11-11 19:45:10 +00002758 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03002759 __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2760 __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2761 __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002762
2763 if (err)
2764 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002765 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002766 return;
2767 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002768 unlock_user_struct(ucp, ucp_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02002769 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002770}
2771#endif
Richard Hendersonff970902013-02-10 10:30:42 -08002772#elif defined(TARGET_MIPS) || defined(TARGET_MIPS64)
bellard106ec872006-06-27 21:08:10 +00002773
Richard Hendersonff970902013-02-10 10:30:42 -08002774# if defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002775struct target_sigcontext {
2776 uint32_t sc_regmask; /* Unused */
2777 uint32_t sc_status;
2778 uint64_t sc_pc;
2779 uint64_t sc_regs[32];
2780 uint64_t sc_fpregs[32];
2781 uint32_t sc_ownedfp; /* Unused */
2782 uint32_t sc_fpc_csr;
2783 uint32_t sc_fpc_eir; /* Unused */
2784 uint32_t sc_used_math;
2785 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
Paul Brook94c54952009-07-09 18:40:15 +01002786 uint32_t pad0;
bellard106ec872006-06-27 21:08:10 +00002787 uint64_t sc_mdhi;
2788 uint64_t sc_mdlo;
2789 target_ulong sc_hi1; /* Was sc_cause */
2790 target_ulong sc_lo1; /* Was sc_badvaddr */
2791 target_ulong sc_hi2; /* Was sc_sigset[4] */
2792 target_ulong sc_lo2;
2793 target_ulong sc_hi3;
2794 target_ulong sc_lo3;
2795};
Richard Hendersonff970902013-02-10 10:30:42 -08002796# else /* N32 || N64 */
2797struct target_sigcontext {
2798 uint64_t sc_regs[32];
2799 uint64_t sc_fpregs[32];
2800 uint64_t sc_mdhi;
2801 uint64_t sc_hi1;
2802 uint64_t sc_hi2;
2803 uint64_t sc_hi3;
2804 uint64_t sc_mdlo;
2805 uint64_t sc_lo1;
2806 uint64_t sc_lo2;
2807 uint64_t sc_lo3;
2808 uint64_t sc_pc;
2809 uint32_t sc_fpc_csr;
2810 uint32_t sc_used_math;
2811 uint32_t sc_dsp;
2812 uint32_t sc_reserved;
2813};
2814# endif /* O32 */
bellard106ec872006-06-27 21:08:10 +00002815
2816struct sigframe {
2817 uint32_t sf_ass[4]; /* argument save space for o32 */
2818 uint32_t sf_code[2]; /* signal trampoline */
2819 struct target_sigcontext sf_sc;
Anthony Liguoric227f092009-10-01 16:12:16 -05002820 target_sigset_t sf_mask;
bellard106ec872006-06-27 21:08:10 +00002821};
2822
pbrook0b1bcb02009-04-21 01:41:10 +00002823struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02002824 target_ulong tuc_flags;
2825 target_ulong tuc_link;
2826 target_stack_t tuc_stack;
Paul Brook94c54952009-07-09 18:40:15 +01002827 target_ulong pad0;
Aurelien Jarno60e99242010-03-29 02:12:51 +02002828 struct target_sigcontext tuc_mcontext;
2829 target_sigset_t tuc_sigmask;
pbrook0b1bcb02009-04-21 01:41:10 +00002830};
2831
2832struct target_rt_sigframe {
2833 uint32_t rs_ass[4]; /* argument save space for o32 */
2834 uint32_t rs_code[2]; /* signal trampoline */
2835 struct target_siginfo rs_info;
2836 struct target_ucontext rs_uc;
2837};
2838
bellard106ec872006-06-27 21:08:10 +00002839/* Install trampoline to jump back from signal handler */
2840static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2841{
Richard Henderson084d0492013-02-10 10:30:44 -08002842 int err = 0;
bellard106ec872006-06-27 21:08:10 +00002843
2844 /*
Richard Henderson084d0492013-02-10 10:30:44 -08002845 * Set up the return code ...
2846 *
2847 * li v0, __NR__foo_sigreturn
2848 * syscall
2849 */
bellard106ec872006-06-27 21:08:10 +00002850
Riku Voipio1d8b5122014-04-23 10:26:05 +03002851 __put_user(0x24020000 + syscall, tramp + 0);
2852 __put_user(0x0000000c , tramp + 1);
bellard106ec872006-06-27 21:08:10 +00002853 return err;
2854}
2855
Riku Voipio41ecc722014-04-23 11:01:00 +03002856static inline void setup_sigcontext(CPUMIPSState *regs,
2857 struct target_sigcontext *sc)
bellard106ec872006-06-27 21:08:10 +00002858{
Richard Henderson084d0492013-02-10 10:30:44 -08002859 int i;
bellard106ec872006-06-27 21:08:10 +00002860
Riku Voipio1d8b5122014-04-23 10:26:05 +03002861 __put_user(exception_resume_pc(regs), &sc->sc_pc);
Kwok Cheung Yeung1239b472013-05-17 14:51:21 -07002862 regs->hflags &= ~MIPS_HFLAG_BMASK;
bellard106ec872006-06-27 21:08:10 +00002863
Richard Henderson084d0492013-02-10 10:30:44 -08002864 __put_user(0, &sc->sc_regs[0]);
2865 for (i = 1; i < 32; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002866 __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
Richard Henderson084d0492013-02-10 10:30:44 -08002867 }
bellard106ec872006-06-27 21:08:10 +00002868
Riku Voipio1d8b5122014-04-23 10:26:05 +03002869 __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2870 __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002871
Richard Henderson084d0492013-02-10 10:30:44 -08002872 /* Rather than checking for dsp existence, always copy. The storage
2873 would just be garbage otherwise. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03002874 __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
2875 __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
2876 __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
2877 __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
2878 __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
2879 __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
Richard Henderson084d0492013-02-10 10:30:44 -08002880 {
2881 uint32_t dsp = cpu_rddsp(0x3ff, regs);
Riku Voipio1d8b5122014-04-23 10:26:05 +03002882 __put_user(dsp, &sc->sc_dsp);
bellard106ec872006-06-27 21:08:10 +00002883 }
Richard Henderson084d0492013-02-10 10:30:44 -08002884
Riku Voipio1d8b5122014-04-23 10:26:05 +03002885 __put_user(1, &sc->sc_used_math);
Richard Henderson084d0492013-02-10 10:30:44 -08002886
2887 for (i = 0; i < 32; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002888 __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
bellard106ec872006-06-27 21:08:10 +00002889 }
bellard106ec872006-06-27 21:08:10 +00002890}
2891
2892static inline int
Andreas Färber05390242012-02-25 03:37:53 +01002893restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
bellard106ec872006-06-27 21:08:10 +00002894{
2895 int err = 0;
Richard Henderson084d0492013-02-10 10:30:44 -08002896 int i;
bellard106ec872006-06-27 21:08:10 +00002897
Riku Voipio1d8b5122014-04-23 10:26:05 +03002898 __get_user(regs->CP0_EPC, &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002899
Riku Voipio1d8b5122014-04-23 10:26:05 +03002900 __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2901 __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002902
Richard Henderson084d0492013-02-10 10:30:44 -08002903 for (i = 1; i < 32; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002904 __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
bellard106ec872006-06-27 21:08:10 +00002905 }
2906
Riku Voipio1d8b5122014-04-23 10:26:05 +03002907 __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
2908 __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
2909 __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
2910 __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
2911 __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
2912 __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
Richard Henderson084d0492013-02-10 10:30:44 -08002913 {
2914 uint32_t dsp;
Riku Voipio1d8b5122014-04-23 10:26:05 +03002915 __get_user(dsp, &sc->sc_dsp);
Richard Henderson084d0492013-02-10 10:30:44 -08002916 cpu_wrdsp(dsp, 0x3ff, regs);
2917 }
2918
2919 for (i = 0; i < 32; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03002920 __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
Richard Henderson084d0492013-02-10 10:30:44 -08002921 }
2922
bellard106ec872006-06-27 21:08:10 +00002923 return err;
2924}
Richard Hendersonff970902013-02-10 10:30:42 -08002925
bellard106ec872006-06-27 21:08:10 +00002926/*
2927 * Determine which stack to use..
2928 */
bellard579a97f2007-11-11 14:26:47 +00002929static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01002930get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
bellard106ec872006-06-27 21:08:10 +00002931{
2932 unsigned long sp;
2933
2934 /* Default to using normal stack */
thsb5dc7732008-06-27 10:02:35 +00002935 sp = regs->active_tc.gpr[29];
bellard106ec872006-06-27 21:08:10 +00002936
2937 /*
Stefan Weil93148aa2012-02-26 18:46:12 +01002938 * FPU emulator may have its own trampoline active just
bellard106ec872006-06-27 21:08:10 +00002939 * above the user stack, 16-bytes before the next lowest
2940 * 16 byte boundary. Try to avoid trashing it.
2941 */
2942 sp -= 32;
2943
bellard106ec872006-06-27 21:08:10 +00002944 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002945 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
thsa04e1342007-09-27 13:57:58 +00002946 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2947 }
bellard106ec872006-06-27 21:08:10 +00002948
bellard579a97f2007-11-11 14:26:47 +00002949 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002950}
2951
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07002952static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
2953{
2954 if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
2955 env->hflags &= ~MIPS_HFLAG_M16;
2956 env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
2957 env->active_tc.PC &= ~(target_ulong) 1;
2958 }
2959}
2960
Richard Hendersonff970902013-02-10 10:30:42 -08002961# if defined(TARGET_ABI_MIPSO32)
bellard579a97f2007-11-11 14:26:47 +00002962/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00002963static void setup_frame(int sig, struct target_sigaction * ka,
Andreas Färber05390242012-02-25 03:37:53 +01002964 target_sigset_t *set, CPUMIPSState *regs)
bellard106ec872006-06-27 21:08:10 +00002965{
2966 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002967 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002968 int i;
2969
bellard579a97f2007-11-11 14:26:47 +00002970 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2971 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002972 goto give_sigsegv;
2973
2974 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2975
Riku Voipio41ecc722014-04-23 11:01:00 +03002976 setup_sigcontext(regs, &frame->sf_sc);
bellard106ec872006-06-27 21:08:10 +00002977
2978 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2979 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2980 goto give_sigsegv;
2981 }
2982
2983 /*
2984 * Arguments to signal handler:
2985 *
2986 * a0 = signal number
2987 * a1 = 0 (should be cause)
2988 * a2 = pointer to struct sigcontext
2989 *
2990 * $25 and PC point to the signal handler, $29 points to the
2991 * struct sigframe.
2992 */
thsb5dc7732008-06-27 10:02:35 +00002993 regs->active_tc.gpr[ 4] = sig;
2994 regs->active_tc.gpr[ 5] = 0;
2995 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2996 regs->active_tc.gpr[29] = frame_addr;
2997 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002998 /* The original kernel code sets CP0_EPC to the handler
2999 * since it returns to userland using eret
3000 * we cannot do this here, and we must set PC directly */
thsb5dc7732008-06-27 10:02:35 +00003001 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07003002 mips_set_hflags_isa_mode_from_pc(regs);
bellard579a97f2007-11-11 14:26:47 +00003003 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00003004 return;
3005
3006give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00003007 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00003008 force_sig(TARGET_SIGSEGV/*, current*/);
bellard106ec872006-06-27 21:08:10 +00003009}
3010
Andreas Färber05390242012-02-25 03:37:53 +01003011long do_sigreturn(CPUMIPSState *regs)
bellard106ec872006-06-27 21:08:10 +00003012{
ths388bb212007-05-13 13:58:00 +00003013 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00003014 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00003015 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05003016 target_sigset_t target_set;
ths388bb212007-05-13 13:58:00 +00003017 int i;
bellard106ec872006-06-27 21:08:10 +00003018
3019#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00003020 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00003021#endif
thsb5dc7732008-06-27 10:02:35 +00003022 frame_addr = regs->active_tc.gpr[29];
bellard579a97f2007-11-11 14:26:47 +00003023 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00003024 goto badframe;
3025
ths388bb212007-05-13 13:58:00 +00003026 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00003027 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
3028 goto badframe;
ths388bb212007-05-13 13:58:00 +00003029 }
bellard106ec872006-06-27 21:08:10 +00003030
ths388bb212007-05-13 13:58:00 +00003031 target_to_host_sigset_internal(&blocked, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00003032 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00003033
ths388bb212007-05-13 13:58:00 +00003034 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00003035 goto badframe;
3036
3037#if 0
ths388bb212007-05-13 13:58:00 +00003038 /*
3039 * Don't let your children do this ...
3040 */
3041 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00003042 "move\t$29, %0\n\t"
3043 "j\tsyscall_exit"
3044 :/* no outputs */
3045 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00003046 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00003047#endif
ths3b46e622007-09-17 08:09:54 +00003048
thsb5dc7732008-06-27 10:02:35 +00003049 regs->active_tc.PC = regs->CP0_EPC;
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07003050 mips_set_hflags_isa_mode_from_pc(regs);
ths388bb212007-05-13 13:58:00 +00003051 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00003052 * maybe a problem with nested signals ? */
3053 regs->CP0_EPC = 0;
pbrook0b1bcb02009-04-21 01:41:10 +00003054 return -TARGET_QEMU_ESIGRETURN;
bellard106ec872006-06-27 21:08:10 +00003055
3056badframe:
ths388bb212007-05-13 13:58:00 +00003057 force_sig(TARGET_SIGSEGV/*, current*/);
3058 return 0;
bellard106ec872006-06-27 21:08:10 +00003059}
Richard Hendersonff970902013-02-10 10:30:42 -08003060# endif /* O32 */
bellard106ec872006-06-27 21:08:10 +00003061
pbrook624f7972008-05-31 16:11:38 +00003062static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003063 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003064 target_sigset_t *set, CPUMIPSState *env)
bellard106ec872006-06-27 21:08:10 +00003065{
pbrook0b1bcb02009-04-21 01:41:10 +00003066 struct target_rt_sigframe *frame;
3067 abi_ulong frame_addr;
3068 int i;
3069
3070 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3071 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3072 goto give_sigsegv;
3073
3074 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
3075
3076 copy_siginfo_to_user(&frame->rs_info, info);
3077
Aurelien Jarno60e99242010-03-29 02:12:51 +02003078 __put_user(0, &frame->rs_uc.tuc_flags);
3079 __put_user(0, &frame->rs_uc.tuc_link);
3080 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
3081 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
pbrook0b1bcb02009-04-21 01:41:10 +00003082 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
Aurelien Jarno60e99242010-03-29 02:12:51 +02003083 &frame->rs_uc.tuc_stack.ss_flags);
pbrook0b1bcb02009-04-21 01:41:10 +00003084
Aurelien Jarno60e99242010-03-29 02:12:51 +02003085 setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
pbrook0b1bcb02009-04-21 01:41:10 +00003086
3087 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02003088 __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
pbrook0b1bcb02009-04-21 01:41:10 +00003089 }
3090
3091 /*
3092 * Arguments to signal handler:
3093 *
3094 * a0 = signal number
Richard W.M. Jones02d2bd52012-07-05 03:32:44 +00003095 * a1 = pointer to siginfo_t
pbrook0b1bcb02009-04-21 01:41:10 +00003096 * a2 = pointer to struct ucontext
3097 *
3098 * $25 and PC point to the signal handler, $29 points to the
3099 * struct sigframe.
3100 */
3101 env->active_tc.gpr[ 4] = sig;
3102 env->active_tc.gpr[ 5] = frame_addr
3103 + offsetof(struct target_rt_sigframe, rs_info);
3104 env->active_tc.gpr[ 6] = frame_addr
3105 + offsetof(struct target_rt_sigframe, rs_uc);
3106 env->active_tc.gpr[29] = frame_addr;
3107 env->active_tc.gpr[31] = frame_addr
3108 + offsetof(struct target_rt_sigframe, rs_code);
3109 /* The original kernel code sets CP0_EPC to the handler
3110 * since it returns to userland using eret
3111 * we cannot do this here, and we must set PC directly */
3112 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07003113 mips_set_hflags_isa_mode_from_pc(env);
pbrook0b1bcb02009-04-21 01:41:10 +00003114 unlock_user_struct(frame, frame_addr, 1);
3115 return;
3116
3117give_sigsegv:
3118 unlock_user_struct(frame, frame_addr, 1);
3119 force_sig(TARGET_SIGSEGV/*, current*/);
bellard106ec872006-06-27 21:08:10 +00003120}
3121
Andreas Färber05390242012-02-25 03:37:53 +01003122long do_rt_sigreturn(CPUMIPSState *env)
bellard106ec872006-06-27 21:08:10 +00003123{
pbrook0b1bcb02009-04-21 01:41:10 +00003124 struct target_rt_sigframe *frame;
3125 abi_ulong frame_addr;
3126 sigset_t blocked;
3127
3128#if defined(DEBUG_SIGNAL)
3129 fprintf(stderr, "do_rt_sigreturn\n");
3130#endif
3131 frame_addr = env->active_tc.gpr[29];
3132 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3133 goto badframe;
3134
Aurelien Jarno60e99242010-03-29 02:12:51 +02003135 target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00003136 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
pbrook0b1bcb02009-04-21 01:41:10 +00003137
Aurelien Jarno60e99242010-03-29 02:12:51 +02003138 if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
pbrook0b1bcb02009-04-21 01:41:10 +00003139 goto badframe;
3140
3141 if (do_sigaltstack(frame_addr +
Aurelien Jarno60e99242010-03-29 02:12:51 +02003142 offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
pbrook0b1bcb02009-04-21 01:41:10 +00003143 0, get_sp_from_cpustate(env)) == -EFAULT)
3144 goto badframe;
3145
3146 env->active_tc.PC = env->CP0_EPC;
Kwok Cheung Yeungea3164a2013-05-17 14:51:20 -07003147 mips_set_hflags_isa_mode_from_pc(env);
pbrook0b1bcb02009-04-21 01:41:10 +00003148 /* I am not sure this is right, but it seems to work
3149 * maybe a problem with nested signals ? */
3150 env->CP0_EPC = 0;
3151 return -TARGET_QEMU_ESIGRETURN;
3152
3153badframe:
3154 force_sig(TARGET_SIGSEGV/*, current*/);
3155 return 0;
bellard106ec872006-06-27 21:08:10 +00003156}
bellard6d5e2162004-09-30 22:04:13 +00003157
thsc3b5bc82007-12-02 06:31:25 +00003158#elif defined(TARGET_SH4)
3159
3160/*
3161 * code and data structures from linux kernel:
3162 * include/asm-sh/sigcontext.h
3163 * arch/sh/kernel/signal.c
3164 */
3165
3166struct target_sigcontext {
3167 target_ulong oldmask;
3168
3169 /* CPU registers */
3170 target_ulong sc_gregs[16];
3171 target_ulong sc_pc;
3172 target_ulong sc_pr;
3173 target_ulong sc_sr;
3174 target_ulong sc_gbr;
3175 target_ulong sc_mach;
3176 target_ulong sc_macl;
3177
3178 /* FPU registers */
3179 target_ulong sc_fpregs[16];
3180 target_ulong sc_xfpregs[16];
3181 unsigned int sc_fpscr;
3182 unsigned int sc_fpul;
3183 unsigned int sc_ownedfp;
3184};
3185
3186struct target_sigframe
3187{
3188 struct target_sigcontext sc;
3189 target_ulong extramask[TARGET_NSIG_WORDS-1];
3190 uint16_t retcode[3];
3191};
3192
3193
3194struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02003195 target_ulong tuc_flags;
3196 struct target_ucontext *tuc_link;
3197 target_stack_t tuc_stack;
3198 struct target_sigcontext tuc_mcontext;
3199 target_sigset_t tuc_sigmask; /* mask last for extensibility */
thsc3b5bc82007-12-02 06:31:25 +00003200};
3201
3202struct target_rt_sigframe
3203{
3204 struct target_siginfo info;
3205 struct target_ucontext uc;
3206 uint16_t retcode[3];
3207};
3208
3209
3210#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
3211#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
3212
pbrook624f7972008-05-31 16:11:38 +00003213static abi_ulong get_sigframe(struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00003214 unsigned long sp, size_t frame_size)
3215{
pbrook624f7972008-05-31 16:11:38 +00003216 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
thsc3b5bc82007-12-02 06:31:25 +00003217 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3218 }
3219
3220 return (sp - frame_size) & -8ul;
3221}
3222
Riku Voipio41ecc722014-04-23 11:01:00 +03003223static void setup_sigcontext(struct target_sigcontext *sc,
Andreas Färber05390242012-02-25 03:37:53 +01003224 CPUSH4State *regs, unsigned long mask)
thsc3b5bc82007-12-02 06:31:25 +00003225{
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003226 int i;
thsc3b5bc82007-12-02 06:31:25 +00003227
Riku Voipio1d8b5122014-04-23 10:26:05 +03003228#define COPY(x) __put_user(regs->x, &sc->sc_##x)
thsc3b5bc82007-12-02 06:31:25 +00003229 COPY(gregs[0]); COPY(gregs[1]);
3230 COPY(gregs[2]); COPY(gregs[3]);
3231 COPY(gregs[4]); COPY(gregs[5]);
3232 COPY(gregs[6]); COPY(gregs[7]);
3233 COPY(gregs[8]); COPY(gregs[9]);
3234 COPY(gregs[10]); COPY(gregs[11]);
3235 COPY(gregs[12]); COPY(gregs[13]);
3236 COPY(gregs[14]); COPY(gregs[15]);
3237 COPY(gbr); COPY(mach);
3238 COPY(macl); COPY(pr);
3239 COPY(sr); COPY(pc);
3240#undef COPY
3241
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003242 for (i=0; i<16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003243 __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003244 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03003245 __put_user(regs->fpscr, &sc->sc_fpscr);
3246 __put_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00003247
3248 /* non-iBCS2 extensions.. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003249 __put_user(mask, &sc->oldmask);
thsc3b5bc82007-12-02 06:31:25 +00003250}
3251
Andreas Färber05390242012-02-25 03:37:53 +01003252static int restore_sigcontext(CPUSH4State *regs, struct target_sigcontext *sc,
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003253 target_ulong *r0_p)
thsc3b5bc82007-12-02 06:31:25 +00003254{
3255 unsigned int err = 0;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003256 int i;
thsc3b5bc82007-12-02 06:31:25 +00003257
Riku Voipio1d8b5122014-04-23 10:26:05 +03003258#define COPY(x) __get_user(regs->x, &sc->sc_##x)
thsc3b5bc82007-12-02 06:31:25 +00003259 COPY(gregs[1]);
3260 COPY(gregs[2]); COPY(gregs[3]);
3261 COPY(gregs[4]); COPY(gregs[5]);
3262 COPY(gregs[6]); COPY(gregs[7]);
3263 COPY(gregs[8]); COPY(gregs[9]);
3264 COPY(gregs[10]); COPY(gregs[11]);
3265 COPY(gregs[12]); COPY(gregs[13]);
3266 COPY(gregs[14]); COPY(gregs[15]);
3267 COPY(gbr); COPY(mach);
3268 COPY(macl); COPY(pr);
3269 COPY(sr); COPY(pc);
3270#undef COPY
3271
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003272 for (i=0; i<16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003273 __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003274 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03003275 __get_user(regs->fpscr, &sc->sc_fpscr);
3276 __get_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00003277
3278 regs->tra = -1; /* disable syscall checks */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003279 __get_user(*r0_p, &sc->sc_gregs[0]);
thsc3b5bc82007-12-02 06:31:25 +00003280 return err;
3281}
3282
pbrook624f7972008-05-31 16:11:38 +00003283static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003284 target_sigset_t *set, CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003285{
3286 struct target_sigframe *frame;
3287 abi_ulong frame_addr;
3288 int i;
3289 int err = 0;
3290 int signal;
3291
3292 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3293 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3294 goto give_sigsegv;
3295
3296 signal = current_exec_domain_sig(sig);
3297
Riku Voipio41ecc722014-04-23 11:01:00 +03003298 setup_sigcontext(&frame->sc, regs, set->sig[0]);
thsc3b5bc82007-12-02 06:31:25 +00003299
3300 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003301 __put_user(set->sig[i + 1], &frame->extramask[i]);
thsc3b5bc82007-12-02 06:31:25 +00003302 }
3303
3304 /* Set up to return from userspace. If provided, use a stub
3305 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00003306 if (ka->sa_flags & TARGET_SA_RESTORER) {
3307 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00003308 } else {
3309 /* Generate return code (system call to sigreturn) */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003310 __put_user(MOVW(2), &frame->retcode[0]);
3311 __put_user(TRAP_NOARG, &frame->retcode[1]);
3312 __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
thsc3b5bc82007-12-02 06:31:25 +00003313 regs->pr = (unsigned long) frame->retcode;
3314 }
3315
3316 if (err)
3317 goto give_sigsegv;
3318
3319 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003320 regs->gregs[15] = frame_addr;
thsc3b5bc82007-12-02 06:31:25 +00003321 regs->gregs[4] = signal; /* Arg for signal handler */
3322 regs->gregs[5] = 0;
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003323 regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
pbrook624f7972008-05-31 16:11:38 +00003324 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00003325
3326 unlock_user_struct(frame, frame_addr, 1);
3327 return;
3328
3329give_sigsegv:
3330 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02003331 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00003332}
3333
pbrook624f7972008-05-31 16:11:38 +00003334static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003335 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003336 target_sigset_t *set, CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003337{
3338 struct target_rt_sigframe *frame;
3339 abi_ulong frame_addr;
3340 int i;
3341 int err = 0;
3342 int signal;
3343
3344 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3345 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3346 goto give_sigsegv;
3347
3348 signal = current_exec_domain_sig(sig);
3349
Riku Voipiob0fd8d12014-04-23 10:46:13 +03003350 copy_siginfo_to_user(&frame->info, info);
thsc3b5bc82007-12-02 06:31:25 +00003351
3352 /* Create the ucontext. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003353 __put_user(0, &frame->uc.tuc_flags);
3354 __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3355 __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3356 &frame->uc.tuc_stack.ss_sp);
3357 __put_user(sas_ss_flags(regs->gregs[15]),
3358 &frame->uc.tuc_stack.ss_flags);
3359 __put_user(target_sigaltstack_used.ss_size,
3360 &frame->uc.tuc_stack.ss_size);
3361 setup_sigcontext(&frame->uc.tuc_mcontext,
thsc3b5bc82007-12-02 06:31:25 +00003362 regs, set->sig[0]);
3363 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003364 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
thsc3b5bc82007-12-02 06:31:25 +00003365 }
3366
3367 /* Set up to return from userspace. If provided, use a stub
3368 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00003369 if (ka->sa_flags & TARGET_SA_RESTORER) {
3370 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00003371 } else {
3372 /* Generate return code (system call to sigreturn) */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003373 __put_user(MOVW(2), &frame->retcode[0]);
3374 __put_user(TRAP_NOARG, &frame->retcode[1]);
3375 __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
thsc3b5bc82007-12-02 06:31:25 +00003376 regs->pr = (unsigned long) frame->retcode;
3377 }
3378
3379 if (err)
3380 goto give_sigsegv;
3381
3382 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003383 regs->gregs[15] = frame_addr;
thsc3b5bc82007-12-02 06:31:25 +00003384 regs->gregs[4] = signal; /* Arg for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003385 regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3386 regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
pbrook624f7972008-05-31 16:11:38 +00003387 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00003388
3389 unlock_user_struct(frame, frame_addr, 1);
3390 return;
3391
3392give_sigsegv:
3393 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02003394 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00003395}
3396
Andreas Färber05390242012-02-25 03:37:53 +01003397long do_sigreturn(CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003398{
3399 struct target_sigframe *frame;
3400 abi_ulong frame_addr;
3401 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05003402 target_sigset_t target_set;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003403 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00003404 int i;
3405 int err = 0;
3406
3407#if defined(DEBUG_SIGNAL)
3408 fprintf(stderr, "do_sigreturn\n");
3409#endif
3410 frame_addr = regs->gregs[15];
3411 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3412 goto badframe;
3413
Riku Voipio1d8b5122014-04-23 10:26:05 +03003414 __get_user(target_set.sig[0], &frame->sc.oldmask);
thsc3b5bc82007-12-02 06:31:25 +00003415 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03003416 __get_user(target_set.sig[i], &frame->extramask[i - 1]);
thsc3b5bc82007-12-02 06:31:25 +00003417 }
3418
3419 if (err)
3420 goto badframe;
3421
3422 target_to_host_sigset_internal(&blocked, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00003423 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
thsc3b5bc82007-12-02 06:31:25 +00003424
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003425 if (restore_sigcontext(regs, &frame->sc, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003426 goto badframe;
3427
3428 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003429 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003430
3431badframe:
3432 unlock_user_struct(frame, frame_addr, 0);
3433 force_sig(TARGET_SIGSEGV);
3434 return 0;
3435}
3436
Andreas Färber05390242012-02-25 03:37:53 +01003437long do_rt_sigreturn(CPUSH4State *regs)
thsc3b5bc82007-12-02 06:31:25 +00003438{
3439 struct target_rt_sigframe *frame;
3440 abi_ulong frame_addr;
3441 sigset_t blocked;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003442 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00003443
3444#if defined(DEBUG_SIGNAL)
3445 fprintf(stderr, "do_rt_sigreturn\n");
3446#endif
3447 frame_addr = regs->gregs[15];
3448 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3449 goto badframe;
3450
Aurelien Jarno60e99242010-03-29 02:12:51 +02003451 target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00003452 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
thsc3b5bc82007-12-02 06:31:25 +00003453
Aurelien Jarno60e99242010-03-29 02:12:51 +02003454 if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003455 goto badframe;
3456
3457 if (do_sigaltstack(frame_addr +
Aurelien Jarno60e99242010-03-29 02:12:51 +02003458 offsetof(struct target_rt_sigframe, uc.tuc_stack),
thsc3b5bc82007-12-02 06:31:25 +00003459 0, get_sp_from_cpustate(regs)) == -EFAULT)
3460 goto badframe;
3461
3462 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003463 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003464
3465badframe:
3466 unlock_user_struct(frame, frame_addr, 0);
3467 force_sig(TARGET_SIGSEGV);
3468 return 0;
3469}
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003470#elif defined(TARGET_MICROBLAZE)
3471
3472struct target_sigcontext {
3473 struct target_pt_regs regs; /* needs to be first */
3474 uint32_t oldmask;
3475};
3476
Edgar E. Iglesiasb2178702010-07-23 09:30:37 +02003477struct target_stack_t {
3478 abi_ulong ss_sp;
3479 int ss_flags;
3480 unsigned int ss_size;
3481};
3482
3483struct target_ucontext {
Richard Hendersonf711df62010-11-22 14:57:52 -08003484 abi_ulong tuc_flags;
3485 abi_ulong tuc_link;
3486 struct target_stack_t tuc_stack;
3487 struct target_sigcontext tuc_mcontext;
3488 uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
Edgar E. Iglesiasb2178702010-07-23 09:30:37 +02003489};
3490
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003491/* Signal frames. */
3492struct target_signal_frame {
Edgar E. Iglesiasb2178702010-07-23 09:30:37 +02003493 struct target_ucontext uc;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003494 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3495 uint32_t tramp[2];
3496};
3497
3498struct rt_signal_frame {
Richard W.M. Jones02d2bd52012-07-05 03:32:44 +00003499 siginfo_t info;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003500 struct ucontext uc;
3501 uint32_t tramp[2];
3502};
3503
Andreas Färber05390242012-02-25 03:37:53 +01003504static void setup_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003505{
3506 __put_user(env->regs[0], &sc->regs.r0);
3507 __put_user(env->regs[1], &sc->regs.r1);
3508 __put_user(env->regs[2], &sc->regs.r2);
3509 __put_user(env->regs[3], &sc->regs.r3);
3510 __put_user(env->regs[4], &sc->regs.r4);
3511 __put_user(env->regs[5], &sc->regs.r5);
3512 __put_user(env->regs[6], &sc->regs.r6);
3513 __put_user(env->regs[7], &sc->regs.r7);
3514 __put_user(env->regs[8], &sc->regs.r8);
3515 __put_user(env->regs[9], &sc->regs.r9);
3516 __put_user(env->regs[10], &sc->regs.r10);
3517 __put_user(env->regs[11], &sc->regs.r11);
3518 __put_user(env->regs[12], &sc->regs.r12);
3519 __put_user(env->regs[13], &sc->regs.r13);
3520 __put_user(env->regs[14], &sc->regs.r14);
3521 __put_user(env->regs[15], &sc->regs.r15);
3522 __put_user(env->regs[16], &sc->regs.r16);
3523 __put_user(env->regs[17], &sc->regs.r17);
3524 __put_user(env->regs[18], &sc->regs.r18);
3525 __put_user(env->regs[19], &sc->regs.r19);
3526 __put_user(env->regs[20], &sc->regs.r20);
3527 __put_user(env->regs[21], &sc->regs.r21);
3528 __put_user(env->regs[22], &sc->regs.r22);
3529 __put_user(env->regs[23], &sc->regs.r23);
3530 __put_user(env->regs[24], &sc->regs.r24);
3531 __put_user(env->regs[25], &sc->regs.r25);
3532 __put_user(env->regs[26], &sc->regs.r26);
3533 __put_user(env->regs[27], &sc->regs.r27);
3534 __put_user(env->regs[28], &sc->regs.r28);
3535 __put_user(env->regs[29], &sc->regs.r29);
3536 __put_user(env->regs[30], &sc->regs.r30);
3537 __put_user(env->regs[31], &sc->regs.r31);
3538 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3539}
3540
Andreas Färber05390242012-02-25 03:37:53 +01003541static void restore_sigcontext(struct target_sigcontext *sc, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003542{
3543 __get_user(env->regs[0], &sc->regs.r0);
3544 __get_user(env->regs[1], &sc->regs.r1);
3545 __get_user(env->regs[2], &sc->regs.r2);
3546 __get_user(env->regs[3], &sc->regs.r3);
3547 __get_user(env->regs[4], &sc->regs.r4);
3548 __get_user(env->regs[5], &sc->regs.r5);
3549 __get_user(env->regs[6], &sc->regs.r6);
3550 __get_user(env->regs[7], &sc->regs.r7);
3551 __get_user(env->regs[8], &sc->regs.r8);
3552 __get_user(env->regs[9], &sc->regs.r9);
3553 __get_user(env->regs[10], &sc->regs.r10);
3554 __get_user(env->regs[11], &sc->regs.r11);
3555 __get_user(env->regs[12], &sc->regs.r12);
3556 __get_user(env->regs[13], &sc->regs.r13);
3557 __get_user(env->regs[14], &sc->regs.r14);
3558 __get_user(env->regs[15], &sc->regs.r15);
3559 __get_user(env->regs[16], &sc->regs.r16);
3560 __get_user(env->regs[17], &sc->regs.r17);
3561 __get_user(env->regs[18], &sc->regs.r18);
3562 __get_user(env->regs[19], &sc->regs.r19);
3563 __get_user(env->regs[20], &sc->regs.r20);
3564 __get_user(env->regs[21], &sc->regs.r21);
3565 __get_user(env->regs[22], &sc->regs.r22);
3566 __get_user(env->regs[23], &sc->regs.r23);
3567 __get_user(env->regs[24], &sc->regs.r24);
3568 __get_user(env->regs[25], &sc->regs.r25);
3569 __get_user(env->regs[26], &sc->regs.r26);
3570 __get_user(env->regs[27], &sc->regs.r27);
3571 __get_user(env->regs[28], &sc->regs.r28);
3572 __get_user(env->regs[29], &sc->regs.r29);
3573 __get_user(env->regs[30], &sc->regs.r30);
3574 __get_user(env->regs[31], &sc->regs.r31);
3575 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3576}
3577
3578static abi_ulong get_sigframe(struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003579 CPUMBState *env, int frame_size)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003580{
3581 abi_ulong sp = env->regs[1];
3582
3583 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3584 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3585
3586 return ((sp - frame_size) & -8UL);
3587}
3588
3589static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003590 target_sigset_t *set, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003591{
3592 struct target_signal_frame *frame;
3593 abi_ulong frame_addr;
3594 int err = 0;
3595 int i;
3596
3597 frame_addr = get_sigframe(ka, env, sizeof *frame);
3598 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3599 goto badframe;
3600
3601 /* Save the mask. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003602 __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003603 if (err)
3604 goto badframe;
3605
3606 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3607 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3608 goto badframe;
3609 }
3610
Richard Hendersonf711df62010-11-22 14:57:52 -08003611 setup_sigcontext(&frame->uc.tuc_mcontext, env);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003612
3613 /* Set up to return from userspace. If provided, use a stub
3614 already in userspace. */
3615 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3616 if (ka->sa_flags & TARGET_SA_RESTORER) {
3617 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3618 } else {
3619 uint32_t t;
3620 /* Note, these encodings are _big endian_! */
3621 /* addi r12, r0, __NR_sigreturn */
3622 t = 0x31800000UL | TARGET_NR_sigreturn;
Riku Voipio1d8b5122014-04-23 10:26:05 +03003623 __put_user(t, frame->tramp + 0);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003624 /* brki r14, 0x8 */
3625 t = 0xb9cc0008UL;
Riku Voipio1d8b5122014-04-23 10:26:05 +03003626 __put_user(t, frame->tramp + 1);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003627
3628 /* Return from sighandler will jump to the tramp.
3629 Negative 8 offset because return is rtsd r15, 8 */
3630 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3631 }
3632
3633 if (err)
3634 goto badframe;
3635
3636 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003637 env->regs[1] = frame_addr;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003638 /* Signal handler args: */
3639 env->regs[5] = sig; /* Arg 0: signum */
Edgar E. Iglesias187b4e02010-07-15 15:32:51 +02003640 env->regs[6] = 0;
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003641 /* arg 1: sigcontext */
3642 env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003643
3644 /* Offset of 4 to handle microblaze rtid r14, 0 */
3645 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3646
3647 unlock_user_struct(frame, frame_addr, 1);
3648 return;
3649 badframe:
3650 unlock_user_struct(frame, frame_addr, 1);
3651 force_sig(TARGET_SIGSEGV);
3652}
3653
3654static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003655 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003656 target_sigset_t *set, CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003657{
3658 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3659}
3660
Andreas Färber05390242012-02-25 03:37:53 +01003661long do_sigreturn(CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003662{
3663 struct target_signal_frame *frame;
3664 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003665 target_sigset_t target_set;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003666 sigset_t set;
3667 int i;
3668
3669 frame_addr = env->regs[R_SP];
3670 /* Make sure the guest isn't playing games. */
3671 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3672 goto badframe;
3673
3674 /* Restore blocked signals */
Richard Hendersonf711df62010-11-22 14:57:52 -08003675 if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003676 goto badframe;
3677 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3678 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3679 goto badframe;
3680 }
3681 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00003682 do_sigprocmask(SIG_SETMASK, &set, NULL);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003683
Richard Hendersonf711df62010-11-22 14:57:52 -08003684 restore_sigcontext(&frame->uc.tuc_mcontext, env);
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003685 /* We got here through a sigreturn syscall, our path back is via an
3686 rtb insn so setup r14 for that. */
3687 env->regs[14] = env->sregs[SR_PC];
3688
3689 unlock_user_struct(frame, frame_addr, 0);
3690 return env->regs[10];
3691 badframe:
3692 unlock_user_struct(frame, frame_addr, 0);
3693 force_sig(TARGET_SIGSEGV);
3694}
3695
Andreas Färber05390242012-02-25 03:37:53 +01003696long do_rt_sigreturn(CPUMBState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003697{
3698 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3699 return -TARGET_ENOSYS;
3700}
3701
edgar_iglb6d3abd2008-02-28 11:29:27 +00003702#elif defined(TARGET_CRIS)
3703
3704struct target_sigcontext {
3705 struct target_pt_regs regs; /* needs to be first */
3706 uint32_t oldmask;
3707 uint32_t usp; /* usp before stacking this gunk on it */
3708};
3709
3710/* Signal frames. */
3711struct target_signal_frame {
3712 struct target_sigcontext sc;
3713 uint32_t extramask[TARGET_NSIG_WORDS - 1];
Stefan Weil8cfc1142014-02-01 09:41:09 +01003714 uint16_t retcode[4]; /* Trampoline code. */
edgar_iglb6d3abd2008-02-28 11:29:27 +00003715};
3716
3717struct rt_signal_frame {
Richard W.M. Jones02d2bd52012-07-05 03:32:44 +00003718 siginfo_t *pinfo;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003719 void *puc;
Richard W.M. Jones02d2bd52012-07-05 03:32:44 +00003720 siginfo_t info;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003721 struct ucontext uc;
Stefan Weil8cfc1142014-02-01 09:41:09 +01003722 uint16_t retcode[4]; /* Trampoline code. */
edgar_iglb6d3abd2008-02-28 11:29:27 +00003723};
3724
Andreas Färber05390242012-02-25 03:37:53 +01003725static void setup_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003726{
edgar_igl9664d922008-03-03 22:23:53 +00003727 __put_user(env->regs[0], &sc->regs.r0);
3728 __put_user(env->regs[1], &sc->regs.r1);
3729 __put_user(env->regs[2], &sc->regs.r2);
3730 __put_user(env->regs[3], &sc->regs.r3);
3731 __put_user(env->regs[4], &sc->regs.r4);
3732 __put_user(env->regs[5], &sc->regs.r5);
3733 __put_user(env->regs[6], &sc->regs.r6);
3734 __put_user(env->regs[7], &sc->regs.r7);
3735 __put_user(env->regs[8], &sc->regs.r8);
3736 __put_user(env->regs[9], &sc->regs.r9);
3737 __put_user(env->regs[10], &sc->regs.r10);
3738 __put_user(env->regs[11], &sc->regs.r11);
3739 __put_user(env->regs[12], &sc->regs.r12);
3740 __put_user(env->regs[13], &sc->regs.r13);
3741 __put_user(env->regs[14], &sc->usp);
3742 __put_user(env->regs[15], &sc->regs.acr);
3743 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3744 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3745 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003746}
edgar_igl9664d922008-03-03 22:23:53 +00003747
Andreas Färber05390242012-02-25 03:37:53 +01003748static void restore_sigcontext(struct target_sigcontext *sc, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003749{
edgar_igl9664d922008-03-03 22:23:53 +00003750 __get_user(env->regs[0], &sc->regs.r0);
3751 __get_user(env->regs[1], &sc->regs.r1);
3752 __get_user(env->regs[2], &sc->regs.r2);
3753 __get_user(env->regs[3], &sc->regs.r3);
3754 __get_user(env->regs[4], &sc->regs.r4);
3755 __get_user(env->regs[5], &sc->regs.r5);
3756 __get_user(env->regs[6], &sc->regs.r6);
3757 __get_user(env->regs[7], &sc->regs.r7);
3758 __get_user(env->regs[8], &sc->regs.r8);
3759 __get_user(env->regs[9], &sc->regs.r9);
3760 __get_user(env->regs[10], &sc->regs.r10);
3761 __get_user(env->regs[11], &sc->regs.r11);
3762 __get_user(env->regs[12], &sc->regs.r12);
3763 __get_user(env->regs[13], &sc->regs.r13);
3764 __get_user(env->regs[14], &sc->usp);
3765 __get_user(env->regs[15], &sc->regs.acr);
3766 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3767 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3768 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003769}
3770
Andreas Färber05390242012-02-25 03:37:53 +01003771static abi_ulong get_sigframe(CPUCRISState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003772{
edgar_igl9664d922008-03-03 22:23:53 +00003773 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003774 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00003775 sp = (env->regs[R_SP] & ~3);
3776 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003777}
3778
pbrook624f7972008-05-31 16:11:38 +00003779static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01003780 target_sigset_t *set, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003781{
3782 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003783 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003784 int err = 0;
3785 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003786
edgar_igl9664d922008-03-03 22:23:53 +00003787 frame_addr = get_sigframe(env, sizeof *frame);
3788 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003789 goto badframe;
3790
3791 /*
3792 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3793 * use this trampoline anymore but it sets it up for GDB.
3794 * In QEMU, using the trampoline simplifies things a bit so we use it.
3795 *
3796 * This is movu.w __NR_sigreturn, r9; break 13;
3797 */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003798 __put_user(0x9c5f, frame->retcode+0);
3799 __put_user(TARGET_NR_sigreturn,
3800 frame->retcode + 1);
3801 __put_user(0xe93d, frame->retcode + 2);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003802
3803 /* Save the mask. */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003804 __put_user(set->sig[0], &frame->sc.oldmask);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003805 if (err)
3806 goto badframe;
3807
3808 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3809 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3810 goto badframe;
3811 }
3812
3813 setup_sigcontext(&frame->sc, env);
3814
3815 /* Move the stack and setup the arguments for the handler. */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003816 env->regs[R_SP] = frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003817 env->regs[10] = sig;
pbrook624f7972008-05-31 16:11:38 +00003818 env->pc = (unsigned long) ka->_sa_handler;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003819 /* Link SRP so the guest returns through the trampoline. */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02003820 env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003821
edgar_igl9664d922008-03-03 22:23:53 +00003822 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003823 return;
3824 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003825 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003826 force_sig(TARGET_SIGSEGV);
3827}
3828
pbrook624f7972008-05-31 16:11:38 +00003829static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003830 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01003831 target_sigset_t *set, CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003832{
3833 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3834}
3835
Andreas Färber05390242012-02-25 03:37:53 +01003836long do_sigreturn(CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003837{
3838 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003839 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003840 target_sigset_t target_set;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003841 sigset_t set;
3842 int i;
3843
edgar_igl9664d922008-03-03 22:23:53 +00003844 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003845 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003846 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003847 goto badframe;
3848
3849 /* Restore blocked signals */
3850 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3851 goto badframe;
3852 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3853 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3854 goto badframe;
3855 }
3856 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00003857 do_sigprocmask(SIG_SETMASK, &set, NULL);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003858
3859 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003860 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003861 return env->regs[10];
3862 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003863 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003864 force_sig(TARGET_SIGSEGV);
3865}
3866
Andreas Färber05390242012-02-25 03:37:53 +01003867long do_rt_sigreturn(CPUCRISState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003868{
3869 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3870 return -TARGET_ENOSYS;
3871}
thsc3b5bc82007-12-02 06:31:25 +00003872
Jia Liud9627832012-07-20 15:50:52 +08003873#elif defined(TARGET_OPENRISC)
3874
3875struct target_sigcontext {
3876 struct target_pt_regs regs;
3877 abi_ulong oldmask;
3878 abi_ulong usp;
3879};
3880
3881struct target_ucontext {
3882 abi_ulong tuc_flags;
3883 abi_ulong tuc_link;
3884 target_stack_t tuc_stack;
3885 struct target_sigcontext tuc_mcontext;
3886 target_sigset_t tuc_sigmask; /* mask last for extensibility */
3887};
3888
3889struct target_rt_sigframe {
3890 abi_ulong pinfo;
3891 uint64_t puc;
3892 struct target_siginfo info;
3893 struct target_sigcontext sc;
3894 struct target_ucontext uc;
3895 unsigned char retcode[16]; /* trampoline code */
3896};
3897
3898/* This is the asm-generic/ucontext.h version */
3899#if 0
3900static int restore_sigcontext(CPUOpenRISCState *regs,
3901 struct target_sigcontext *sc)
3902{
3903 unsigned int err = 0;
3904 unsigned long old_usp;
3905
3906 /* Alwys make any pending restarted system call return -EINTR */
3907 current_thread_info()->restart_block.fn = do_no_restart_syscall;
3908
3909 /* restore the regs from &sc->regs (same as sc, since regs is first)
3910 * (sc is already checked for VERIFY_READ since the sigframe was
3911 * checked in sys_sigreturn previously)
3912 */
3913
3914 if (copy_from_user(regs, &sc, sizeof(struct target_pt_regs))) {
3915 goto badframe;
3916 }
3917
3918 /* make sure the U-flag is set so user-mode cannot fool us */
3919
3920 regs->sr &= ~SR_SM;
3921
3922 /* restore the old USP as it was before we stacked the sc etc.
3923 * (we cannot just pop the sigcontext since we aligned the sp and
3924 * stuff after pushing it)
3925 */
3926
Riku Voipio1d8b5122014-04-23 10:26:05 +03003927 __get_user(old_usp, &sc->usp);
Jia Liud9627832012-07-20 15:50:52 +08003928 phx_signal("old_usp 0x%lx", old_usp);
3929
3930 __PHX__ REALLY /* ??? */
3931 wrusp(old_usp);
3932 regs->gpr[1] = old_usp;
3933
3934 /* TODO: the other ports use regs->orig_XX to disable syscall checks
3935 * after this completes, but we don't use that mechanism. maybe we can
3936 * use it now ?
3937 */
3938
3939 return err;
3940
3941badframe:
3942 return 1;
3943}
3944#endif
3945
3946/* Set up a signal frame. */
3947
Riku Voipio41ecc722014-04-23 11:01:00 +03003948static void setup_sigcontext(struct target_sigcontext *sc,
Jia Liud9627832012-07-20 15:50:52 +08003949 CPUOpenRISCState *regs,
3950 unsigned long mask)
3951{
Jia Liud9627832012-07-20 15:50:52 +08003952 unsigned long usp = regs->gpr[1];
3953
3954 /* copy the regs. they are first in sc so we can use sc directly */
3955
Riku Voipio1d8b5122014-04-23 10:26:05 +03003956 /*copy_to_user(&sc, regs, sizeof(struct target_pt_regs));*/
Jia Liud9627832012-07-20 15:50:52 +08003957
3958 /* Set the frametype to CRIS_FRAME_NORMAL for the execution of
3959 the signal handler. The frametype will be restored to its previous
3960 value in restore_sigcontext. */
3961 /*regs->frametype = CRIS_FRAME_NORMAL;*/
3962
3963 /* then some other stuff */
Riku Voipio1d8b5122014-04-23 10:26:05 +03003964 __put_user(mask, &sc->oldmask);
Riku Voipio41ecc722014-04-23 11:01:00 +03003965 __put_user(usp, &sc->usp);
Jia Liud9627832012-07-20 15:50:52 +08003966}
3967
3968static inline unsigned long align_sigframe(unsigned long sp)
3969{
3970 unsigned long i;
3971 i = sp & ~3UL;
3972 return i;
3973}
3974
3975static inline abi_ulong get_sigframe(struct target_sigaction *ka,
3976 CPUOpenRISCState *regs,
3977 size_t frame_size)
3978{
3979 unsigned long sp = regs->gpr[1];
3980 int onsigstack = on_sig_stack(sp);
3981
3982 /* redzone */
3983 /* This is the X/Open sanctioned signal stack switching. */
3984 if ((ka->sa_flags & SA_ONSTACK) != 0 && !onsigstack) {
3985 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3986 }
3987
3988 sp = align_sigframe(sp - frame_size);
3989
3990 /*
3991 * If we are on the alternate signal stack and would overflow it, don't.
3992 * Return an always-bogus address instead so we will die with SIGSEGV.
3993 */
3994
3995 if (onsigstack && !likely(on_sig_stack(sp))) {
3996 return -1L;
3997 }
3998
3999 return sp;
4000}
4001
4002static void setup_frame(int sig, struct target_sigaction *ka,
4003 target_sigset_t *set, CPUOpenRISCState *env)
4004{
4005 qemu_log("Not implement.\n");
4006}
4007
4008static void setup_rt_frame(int sig, struct target_sigaction *ka,
4009 target_siginfo_t *info,
4010 target_sigset_t *set, CPUOpenRISCState *env)
4011{
4012 int err = 0;
4013 abi_ulong frame_addr;
4014 unsigned long return_ip;
4015 struct target_rt_sigframe *frame;
4016 abi_ulong info_addr, uc_addr;
4017
Jia Liud9627832012-07-20 15:50:52 +08004018 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4019 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4020 goto give_sigsegv;
4021 }
4022
4023 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
Riku Voipio1d8b5122014-04-23 10:26:05 +03004024 __put_user(info_addr, &frame->pinfo);
Jia Liud9627832012-07-20 15:50:52 +08004025 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
Riku Voipio1d8b5122014-04-23 10:26:05 +03004026 __put_user(uc_addr, &frame->puc);
Jia Liud9627832012-07-20 15:50:52 +08004027
4028 if (ka->sa_flags & SA_SIGINFO) {
Riku Voipiob0fd8d12014-04-23 10:46:13 +03004029 copy_siginfo_to_user(&frame->info, info);
Jia Liud9627832012-07-20 15:50:52 +08004030 }
4031
4032 /*err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));*/
Riku Voipio1d8b5122014-04-23 10:26:05 +03004033 __put_user(0, &frame->uc.tuc_flags);
4034 __put_user(0, &frame->uc.tuc_link);
4035 __put_user(target_sigaltstack_used.ss_sp,
4036 &frame->uc.tuc_stack.ss_sp);
4037 __put_user(sas_ss_flags(env->gpr[1]), &frame->uc.tuc_stack.ss_flags);
4038 __put_user(target_sigaltstack_used.ss_size,
4039 &frame->uc.tuc_stack.ss_size);
Riku Voipio41ecc722014-04-23 11:01:00 +03004040 setup_sigcontext(&frame->sc, env, set->sig[0]);
Jia Liud9627832012-07-20 15:50:52 +08004041
4042 /*err |= copy_to_user(frame->uc.tuc_sigmask, set, sizeof(*set));*/
4043
Jia Liud9627832012-07-20 15:50:52 +08004044 /* trampoline - the desired return ip is the retcode itself */
4045 return_ip = (unsigned long)&frame->retcode;
4046 /* This is l.ori r11,r0,__NR_sigreturn, l.sys 1 */
Riku Voipio1d8b5122014-04-23 10:26:05 +03004047 __put_user(0xa960, (short *)(frame->retcode + 0));
4048 __put_user(TARGET_NR_rt_sigreturn, (short *)(frame->retcode + 2));
4049 __put_user(0x20000001, (unsigned long *)(frame->retcode + 4));
4050 __put_user(0x15000000, (unsigned long *)(frame->retcode + 8));
Jia Liud9627832012-07-20 15:50:52 +08004051
4052 if (err) {
4053 goto give_sigsegv;
4054 }
4055
4056 /* TODO what is the current->exec_domain stuff and invmap ? */
4057
4058 /* Set up registers for signal handler */
4059 env->pc = (unsigned long)ka->_sa_handler; /* what we enter NOW */
4060 env->gpr[9] = (unsigned long)return_ip; /* what we enter LATER */
4061 env->gpr[3] = (unsigned long)sig; /* arg 1: signo */
4062 env->gpr[4] = (unsigned long)&frame->info; /* arg 2: (siginfo_t*) */
4063 env->gpr[5] = (unsigned long)&frame->uc; /* arg 3: ucontext */
4064
4065 /* actually move the usp to reflect the stacked frame */
4066 env->gpr[1] = (unsigned long)frame;
4067
4068 return;
4069
4070give_sigsegv:
4071 unlock_user_struct(frame, frame_addr, 1);
4072 if (sig == TARGET_SIGSEGV) {
4073 ka->_sa_handler = TARGET_SIG_DFL;
4074 }
4075 force_sig(TARGET_SIGSEGV);
4076}
4077
4078long do_sigreturn(CPUOpenRISCState *env)
4079{
4080
4081 qemu_log("do_sigreturn: not implemented\n");
4082 return -TARGET_ENOSYS;
4083}
4084
4085long do_rt_sigreturn(CPUOpenRISCState *env)
4086{
4087 qemu_log("do_rt_sigreturn: not implemented\n");
4088 return -TARGET_ENOSYS;
4089}
4090/* TARGET_OPENRISC */
4091
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004092#elif defined(TARGET_S390X)
4093
4094#define __NUM_GPRS 16
4095#define __NUM_FPRS 16
4096#define __NUM_ACRS 16
4097
4098#define S390_SYSCALL_SIZE 2
4099#define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
4100
4101#define _SIGCONTEXT_NSIG 64
4102#define _SIGCONTEXT_NSIG_BPW 64 /* FIXME: 31-bit mode -> 32 */
4103#define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
4104#define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
4105#define PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
4106#define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
4107
4108typedef struct {
4109 target_psw_t psw;
4110 target_ulong gprs[__NUM_GPRS];
4111 unsigned int acrs[__NUM_ACRS];
4112} target_s390_regs_common;
4113
4114typedef struct {
4115 unsigned int fpc;
4116 double fprs[__NUM_FPRS];
4117} target_s390_fp_regs;
4118
4119typedef struct {
4120 target_s390_regs_common regs;
4121 target_s390_fp_regs fpregs;
4122} target_sigregs;
4123
4124struct target_sigcontext {
4125 target_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
4126 target_sigregs *sregs;
4127};
4128
4129typedef struct {
4130 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4131 struct target_sigcontext sc;
4132 target_sigregs sregs;
4133 int signo;
4134 uint8_t retcode[S390_SYSCALL_SIZE];
4135} sigframe;
4136
4137struct target_ucontext {
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004138 target_ulong tuc_flags;
4139 struct target_ucontext *tuc_link;
4140 target_stack_t tuc_stack;
4141 target_sigregs tuc_mcontext;
4142 target_sigset_t tuc_sigmask; /* mask last for extensibility */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004143};
4144
4145typedef struct {
4146 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
4147 uint8_t retcode[S390_SYSCALL_SIZE];
4148 struct target_siginfo info;
4149 struct target_ucontext uc;
4150} rt_sigframe;
4151
4152static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01004153get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004154{
4155 abi_ulong sp;
4156
4157 /* Default to using normal stack */
4158 sp = env->regs[15];
4159
4160 /* This is the X/Open sanctioned signal stack switching. */
4161 if (ka->sa_flags & TARGET_SA_ONSTACK) {
4162 if (!sas_ss_flags(sp)) {
4163 sp = target_sigaltstack_used.ss_sp +
4164 target_sigaltstack_used.ss_size;
4165 }
4166 }
4167
4168 /* This is the legacy signal stack switching. */
4169 else if (/* FIXME !user_mode(regs) */ 0 &&
4170 !(ka->sa_flags & TARGET_SA_RESTORER) &&
4171 ka->sa_restorer) {
4172 sp = (abi_ulong) ka->sa_restorer;
4173 }
4174
4175 return (sp - frame_size) & -8ul;
4176}
4177
Andreas Färber05390242012-02-25 03:37:53 +01004178static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004179{
4180 int i;
4181 //save_access_regs(current->thread.acrs); FIXME
4182
4183 /* Copy a 'clean' PSW mask to the user to avoid leaking
4184 information about whether PER is currently on. */
4185 __put_user(env->psw.mask, &sregs->regs.psw.mask);
4186 __put_user(env->psw.addr, &sregs->regs.psw.addr);
4187 for (i = 0; i < 16; i++) {
4188 __put_user(env->regs[i], &sregs->regs.gprs[i]);
4189 }
4190 for (i = 0; i < 16; i++) {
4191 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
4192 }
4193 /*
4194 * We have to store the fp registers to current->thread.fp_regs
4195 * to merge them with the emulated registers.
4196 */
4197 //save_fp_regs(&current->thread.fp_regs); FIXME
4198 for (i = 0; i < 16; i++) {
4199 __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
4200 }
4201}
4202
4203static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01004204 target_sigset_t *set, CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004205{
4206 sigframe *frame;
4207 abi_ulong frame_addr;
4208
4209 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4210 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4211 (unsigned long long)frame_addr);
4212 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4213 goto give_sigsegv;
4214 }
4215
4216 qemu_log("%s: 1\n", __FUNCTION__);
4217 if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
4218 goto give_sigsegv;
4219 }
4220
4221 save_sigregs(env, &frame->sregs);
4222
4223 __put_user((abi_ulong)(unsigned long)&frame->sregs,
4224 (abi_ulong *)&frame->sc.sregs);
4225
4226 /* Set up to return from userspace. If provided, use a stub
4227 already in userspace. */
4228 if (ka->sa_flags & TARGET_SA_RESTORER) {
4229 env->regs[14] = (unsigned long)
4230 ka->sa_restorer | PSW_ADDR_AMODE;
4231 } else {
4232 env->regs[14] = (unsigned long)
4233 frame->retcode | PSW_ADDR_AMODE;
4234 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
4235 (uint16_t *)(frame->retcode)))
4236 goto give_sigsegv;
4237 }
4238
4239 /* Set up backchain. */
4240 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4241 goto give_sigsegv;
4242 }
4243
4244 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004245 env->regs[15] = frame_addr;
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004246 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4247
4248 env->regs[2] = sig; //map_signal(sig);
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004249 env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004250
4251 /* We forgot to include these in the sigcontext.
4252 To avoid breaking binary compatibility, they are passed as args. */
4253 env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
4254 env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
4255
4256 /* Place signal number on stack to allow backtrace from handler. */
4257 if (__put_user(env->regs[2], (int *) &frame->signo)) {
4258 goto give_sigsegv;
4259 }
4260 unlock_user_struct(frame, frame_addr, 1);
4261 return;
4262
4263give_sigsegv:
4264 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4265 unlock_user_struct(frame, frame_addr, 1);
4266 force_sig(TARGET_SIGSEGV);
4267}
4268
4269static void setup_rt_frame(int sig, struct target_sigaction *ka,
4270 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01004271 target_sigset_t *set, CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004272{
4273 int i;
4274 rt_sigframe *frame;
4275 abi_ulong frame_addr;
4276
4277 frame_addr = get_sigframe(ka, env, sizeof *frame);
4278 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4279 (unsigned long long)frame_addr);
4280 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4281 goto give_sigsegv;
4282 }
4283
4284 qemu_log("%s: 1\n", __FUNCTION__);
Riku Voipiob0fd8d12014-04-23 10:46:13 +03004285 copy_siginfo_to_user(&frame->info, info);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004286
4287 /* Create the ucontext. */
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004288 __put_user(0, &frame->uc.tuc_flags);
4289 __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
4290 __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004291 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004292 &frame->uc.tuc_stack.ss_flags);
4293 __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
4294 save_sigregs(env, &frame->uc.tuc_mcontext);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004295 for (i = 0; i < TARGET_NSIG_WORDS; i++) {
4296 __put_user((abi_ulong)set->sig[i],
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004297 (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004298 }
4299
4300 /* Set up to return from userspace. If provided, use a stub
4301 already in userspace. */
4302 if (ka->sa_flags & TARGET_SA_RESTORER) {
4303 env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
4304 } else {
4305 env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
4306 if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
4307 (uint16_t *)(frame->retcode))) {
4308 goto give_sigsegv;
4309 }
4310 }
4311
4312 /* Set up backchain. */
4313 if (__put_user(env->regs[15], (abi_ulong *) frame)) {
4314 goto give_sigsegv;
4315 }
4316
4317 /* Set up registers for signal handler */
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004318 env->regs[15] = frame_addr;
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004319 env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
4320
4321 env->regs[2] = sig; //map_signal(sig);
Edgar E. Iglesiascb9c6262011-08-22 18:44:58 +02004322 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
4323 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004324 return;
4325
4326give_sigsegv:
4327 qemu_log("%s: give_sigsegv\n", __FUNCTION__);
4328 unlock_user_struct(frame, frame_addr, 1);
4329 force_sig(TARGET_SIGSEGV);
4330}
4331
4332static int
Andreas Färber05390242012-02-25 03:37:53 +01004333restore_sigregs(CPUS390XState *env, target_sigregs *sc)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004334{
4335 int err = 0;
4336 int i;
4337
4338 for (i = 0; i < 16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03004339 __get_user(env->regs[i], &sc->regs.gprs[i]);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004340 }
4341
Riku Voipio1d8b5122014-04-23 10:26:05 +03004342 __get_user(env->psw.mask, &sc->regs.psw.mask);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004343 qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
4344 __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
4345 (unsigned long long)env->psw.addr);
Riku Voipio1d8b5122014-04-23 10:26:05 +03004346 __get_user(env->psw.addr, &sc->regs.psw.addr);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004347 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
4348
4349 for (i = 0; i < 16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03004350 __get_user(env->aregs[i], &sc->regs.acrs[i]);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004351 }
4352 for (i = 0; i < 16; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03004353 __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004354 }
4355
4356 return err;
4357}
4358
Andreas Färber05390242012-02-25 03:37:53 +01004359long do_sigreturn(CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004360{
4361 sigframe *frame;
4362 abi_ulong frame_addr = env->regs[15];
4363 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4364 (unsigned long long)frame_addr);
4365 target_sigset_t target_set;
4366 sigset_t set;
4367
4368 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4369 goto badframe;
4370 }
4371 if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
4372 goto badframe;
4373 }
4374
4375 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00004376 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004377
4378 if (restore_sigregs(env, &frame->sregs)) {
4379 goto badframe;
4380 }
4381
4382 unlock_user_struct(frame, frame_addr, 0);
4383 return env->regs[2];
4384
4385badframe:
4386 unlock_user_struct(frame, frame_addr, 0);
4387 force_sig(TARGET_SIGSEGV);
4388 return 0;
4389}
4390
Andreas Färber05390242012-02-25 03:37:53 +01004391long do_rt_sigreturn(CPUS390XState *env)
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004392{
4393 rt_sigframe *frame;
4394 abi_ulong frame_addr = env->regs[15];
4395 qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
4396 (unsigned long long)frame_addr);
4397 sigset_t set;
4398
4399 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4400 goto badframe;
4401 }
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004402 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004403
Alex Barcelo1c275922014-03-14 14:36:55 +00004404 do_sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004405
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004406 if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004407 goto badframe;
4408 }
4409
Peter Maydell6fea2ea2011-07-12 21:27:15 +01004410 if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
Ulrich Hechta4c075f2009-07-24 16:57:31 +02004411 get_sp_from_cpustate(env)) == -EFAULT) {
4412 goto badframe;
4413 }
4414 unlock_user_struct(frame, frame_addr, 0);
4415 return env->regs[2];
4416
4417badframe:
4418 unlock_user_struct(frame, frame_addr, 0);
4419 force_sig(TARGET_SIGSEGV);
4420 return 0;
4421}
4422
Nathan Froydbcd49332009-05-12 19:13:18 -07004423#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
4424
4425/* FIXME: Many of the structures are defined for both PPC and PPC64, but
4426 the signal handling is different enough that we haven't implemented
4427 support for PPC64 yet. Hence the restriction above.
4428
4429 There are various #if'd blocks for code for TARGET_PPC64. These
4430 blocks should go away so that we can successfully run 32-bit and
4431 64-bit binaries on a QEMU configured for PPC64. */
4432
4433/* Size of dummy stack frame allocated when calling signal handler.
4434 See arch/powerpc/include/asm/ptrace.h. */
4435#if defined(TARGET_PPC64)
4436#define SIGNAL_FRAMESIZE 128
4437#else
4438#define SIGNAL_FRAMESIZE 64
4439#endif
4440
4441/* See arch/powerpc/include/asm/sigcontext.h. */
4442struct target_sigcontext {
4443 target_ulong _unused[4];
4444 int32_t signal;
4445#if defined(TARGET_PPC64)
4446 int32_t pad0;
4447#endif
4448 target_ulong handler;
4449 target_ulong oldmask;
4450 target_ulong regs; /* struct pt_regs __user * */
4451 /* TODO: PPC64 includes extra bits here. */
4452};
4453
4454/* Indices for target_mcontext.mc_gregs, below.
4455 See arch/powerpc/include/asm/ptrace.h for details. */
4456enum {
4457 TARGET_PT_R0 = 0,
4458 TARGET_PT_R1 = 1,
4459 TARGET_PT_R2 = 2,
4460 TARGET_PT_R3 = 3,
4461 TARGET_PT_R4 = 4,
4462 TARGET_PT_R5 = 5,
4463 TARGET_PT_R6 = 6,
4464 TARGET_PT_R7 = 7,
4465 TARGET_PT_R8 = 8,
4466 TARGET_PT_R9 = 9,
4467 TARGET_PT_R10 = 10,
4468 TARGET_PT_R11 = 11,
4469 TARGET_PT_R12 = 12,
4470 TARGET_PT_R13 = 13,
4471 TARGET_PT_R14 = 14,
4472 TARGET_PT_R15 = 15,
4473 TARGET_PT_R16 = 16,
4474 TARGET_PT_R17 = 17,
4475 TARGET_PT_R18 = 18,
4476 TARGET_PT_R19 = 19,
4477 TARGET_PT_R20 = 20,
4478 TARGET_PT_R21 = 21,
4479 TARGET_PT_R22 = 22,
4480 TARGET_PT_R23 = 23,
4481 TARGET_PT_R24 = 24,
4482 TARGET_PT_R25 = 25,
4483 TARGET_PT_R26 = 26,
4484 TARGET_PT_R27 = 27,
4485 TARGET_PT_R28 = 28,
4486 TARGET_PT_R29 = 29,
4487 TARGET_PT_R30 = 30,
4488 TARGET_PT_R31 = 31,
4489 TARGET_PT_NIP = 32,
4490 TARGET_PT_MSR = 33,
4491 TARGET_PT_ORIG_R3 = 34,
4492 TARGET_PT_CTR = 35,
4493 TARGET_PT_LNK = 36,
4494 TARGET_PT_XER = 37,
4495 TARGET_PT_CCR = 38,
4496 /* Yes, there are two registers with #39. One is 64-bit only. */
4497 TARGET_PT_MQ = 39,
4498 TARGET_PT_SOFTE = 39,
4499 TARGET_PT_TRAP = 40,
4500 TARGET_PT_DAR = 41,
4501 TARGET_PT_DSISR = 42,
4502 TARGET_PT_RESULT = 43,
4503 TARGET_PT_REGS_COUNT = 44
4504};
4505
4506/* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
4507 on 64-bit PPC, sigcontext and mcontext are one and the same. */
4508struct target_mcontext {
4509 target_ulong mc_gregs[48];
4510 /* Includes fpscr. */
4511 uint64_t mc_fregs[33];
4512 target_ulong mc_pad[2];
4513 /* We need to handle Altivec and SPE at the same time, which no
4514 kernel needs to do. Fortunately, the kernel defines this bit to
4515 be Altivec-register-large all the time, rather than trying to
4516 twiddle it based on the specific platform. */
4517 union {
4518 /* SPE vector registers. One extra for SPEFSCR. */
4519 uint32_t spe[33];
4520 /* Altivec vector registers. The packing of VSCR and VRSAVE
4521 varies depending on whether we're PPC64 or not: PPC64 splits
4522 them apart; PPC32 stuffs them together. */
4523#if defined(TARGET_PPC64)
malc3efa9a62009-07-18 13:10:12 +04004524#define QEMU_NVRREG 34
Nathan Froydbcd49332009-05-12 19:13:18 -07004525#else
malc3efa9a62009-07-18 13:10:12 +04004526#define QEMU_NVRREG 33
Nathan Froydbcd49332009-05-12 19:13:18 -07004527#endif
Anthony Liguoric227f092009-10-01 16:12:16 -05004528 ppc_avr_t altivec[QEMU_NVRREG];
malc3efa9a62009-07-18 13:10:12 +04004529#undef QEMU_NVRREG
Nathan Froydbcd49332009-05-12 19:13:18 -07004530 } mc_vregs __attribute__((__aligned__(16)));
4531};
4532
4533struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02004534 target_ulong tuc_flags;
4535 target_ulong tuc_link; /* struct ucontext __user * */
4536 struct target_sigaltstack tuc_stack;
Nathan Froydbcd49332009-05-12 19:13:18 -07004537#if !defined(TARGET_PPC64)
Aurelien Jarno60e99242010-03-29 02:12:51 +02004538 int32_t tuc_pad[7];
4539 target_ulong tuc_regs; /* struct mcontext __user *
Nathan Froydbcd49332009-05-12 19:13:18 -07004540 points to uc_mcontext field */
4541#endif
Aurelien Jarno60e99242010-03-29 02:12:51 +02004542 target_sigset_t tuc_sigmask;
Nathan Froydbcd49332009-05-12 19:13:18 -07004543#if defined(TARGET_PPC64)
Anthony Liguoric227f092009-10-01 16:12:16 -05004544 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
Aurelien Jarno60e99242010-03-29 02:12:51 +02004545 struct target_sigcontext tuc_mcontext;
Nathan Froydbcd49332009-05-12 19:13:18 -07004546#else
Aurelien Jarno60e99242010-03-29 02:12:51 +02004547 int32_t tuc_maskext[30];
4548 int32_t tuc_pad2[3];
4549 struct target_mcontext tuc_mcontext;
Nathan Froydbcd49332009-05-12 19:13:18 -07004550#endif
4551};
4552
4553/* See arch/powerpc/kernel/signal_32.c. */
4554struct target_sigframe {
4555 struct target_sigcontext sctx;
4556 struct target_mcontext mctx;
4557 int32_t abigap[56];
4558};
4559
4560struct target_rt_sigframe {
4561 struct target_siginfo info;
4562 struct target_ucontext uc;
4563 int32_t abigap[56];
4564};
4565
4566/* We use the mc_pad field for the signal return trampoline. */
4567#define tramp mc_pad
4568
4569/* See arch/powerpc/kernel/signal.c. */
4570static target_ulong get_sigframe(struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01004571 CPUPPCState *env,
Nathan Froydbcd49332009-05-12 19:13:18 -07004572 int frame_size)
4573{
4574 target_ulong oldsp, newsp;
4575
4576 oldsp = env->gpr[1];
4577
4578 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
Alex Barcelo32a20032012-02-09 23:55:46 +00004579 (sas_ss_flags(oldsp) == 0)) {
Nathan Froydbcd49332009-05-12 19:13:18 -07004580 oldsp = (target_sigaltstack_used.ss_sp
4581 + target_sigaltstack_used.ss_size);
4582 }
4583
4584 newsp = (oldsp - frame_size) & ~0xFUL;
4585
4586 return newsp;
4587}
4588
Andreas Färber05390242012-02-25 03:37:53 +01004589static int save_user_regs(CPUPPCState *env, struct target_mcontext *frame,
Nathan Froydbcd49332009-05-12 19:13:18 -07004590 int sigret)
4591{
4592 target_ulong msr = env->msr;
4593 int i;
4594 target_ulong ccr = 0;
4595
4596 /* In general, the kernel attempts to be intelligent about what it
4597 needs to save for Altivec/FP/SPE registers. We don't care that
4598 much, so we just go ahead and save everything. */
4599
4600 /* Save general registers. */
4601 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4602 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4603 return 1;
4604 }
4605 }
4606 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4607 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4608 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4609 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4610 return 1;
4611
4612 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4613 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4614 }
4615 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4616 return 1;
4617
4618 /* Save Altivec registers if necessary. */
4619 if (env->insns_flags & PPC_ALTIVEC) {
4620 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004621 ppc_avr_t *avr = &env->avr[i];
4622 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07004623
4624 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4625 __put_user(avr->u64[1], &vreg->u64[1])) {
4626 return 1;
4627 }
4628 }
4629 /* Set MSR_VR in the saved MSR value to indicate that
4630 frame->mc_vregs contains valid data. */
4631 msr |= MSR_VR;
4632 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4633 &frame->mc_vregs.altivec[32].u32[3]))
4634 return 1;
4635 }
4636
4637 /* Save floating point registers. */
4638 if (env->insns_flags & PPC_FLOAT) {
4639 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4640 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4641 return 1;
4642 }
4643 }
4644 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4645 return 1;
4646 }
4647
4648 /* Save SPE registers. The kernel only saves the high half. */
4649 if (env->insns_flags & PPC_SPE) {
4650#if defined(TARGET_PPC64)
4651 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4652 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4653 return 1;
4654 }
4655 }
4656#else
4657 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4658 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4659 return 1;
4660 }
4661 }
4662#endif
4663 /* Set MSR_SPE in the saved MSR value to indicate that
4664 frame->mc_vregs contains valid data. */
4665 msr |= MSR_SPE;
4666 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4667 return 1;
4668 }
4669
4670 /* Store MSR. */
4671 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4672 return 1;
4673
4674 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
4675 if (sigret) {
4676 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4677 __put_user(0x44000002UL, &frame->tramp[1])) {
4678 return 1;
4679 }
4680 }
4681
4682 return 0;
4683}
4684
Andreas Färber05390242012-02-25 03:37:53 +01004685static int restore_user_regs(CPUPPCState *env,
Nathan Froydbcd49332009-05-12 19:13:18 -07004686 struct target_mcontext *frame, int sig)
4687{
4688 target_ulong save_r2 = 0;
4689 target_ulong msr;
4690 target_ulong ccr;
4691
4692 int i;
4693
4694 if (!sig) {
4695 save_r2 = env->gpr[2];
4696 }
4697
4698 /* Restore general registers. */
4699 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4700 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4701 return 1;
4702 }
4703 }
4704 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4705 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4706 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4707 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4708 return 1;
4709 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4710 return 1;
4711
4712 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4713 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4714 }
4715
4716 if (!sig) {
4717 env->gpr[2] = save_r2;
4718 }
4719 /* Restore MSR. */
4720 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4721 return 1;
4722
4723 /* If doing signal return, restore the previous little-endian mode. */
4724 if (sig)
4725 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4726
4727 /* Restore Altivec registers if necessary. */
4728 if (env->insns_flags & PPC_ALTIVEC) {
4729 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05004730 ppc_avr_t *avr = &env->avr[i];
4731 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07004732
4733 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4734 __get_user(avr->u64[1], &vreg->u64[1])) {
4735 return 1;
4736 }
4737 }
4738 /* Set MSR_VEC in the saved MSR value to indicate that
4739 frame->mc_vregs contains valid data. */
4740 if (__get_user(env->spr[SPR_VRSAVE],
4741 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4742 return 1;
4743 }
4744
4745 /* Restore floating point registers. */
4746 if (env->insns_flags & PPC_FLOAT) {
4747 uint64_t fpscr;
4748 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4749 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4750 return 1;
4751 }
4752 }
4753 if (__get_user(fpscr, &frame->mc_fregs[32]))
4754 return 1;
4755 env->fpscr = (uint32_t) fpscr;
4756 }
4757
4758 /* Save SPE registers. The kernel only saves the high half. */
4759 if (env->insns_flags & PPC_SPE) {
4760#if defined(TARGET_PPC64)
4761 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4762 uint32_t hi;
4763
4764 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4765 return 1;
4766 }
4767 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4768 }
4769#else
4770 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4771 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4772 return 1;
4773 }
4774 }
4775#endif
4776 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4777 return 1;
4778 }
4779
4780 return 0;
4781}
4782
4783static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01004784 target_sigset_t *set, CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004785{
4786 struct target_sigframe *frame;
4787 struct target_sigcontext *sc;
4788 target_ulong frame_addr, newsp;
4789 int err = 0;
4790 int signal;
4791
4792 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4793 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4794 goto sigsegv;
4795 sc = &frame->sctx;
4796
4797 signal = current_exec_domain_sig(sig);
4798
Riku Voipio1d8b5122014-04-23 10:26:05 +03004799 __put_user(ka->_sa_handler, &sc->handler);
4800 __put_user(set->sig[0], &sc->oldmask);
Nathan Froydbcd49332009-05-12 19:13:18 -07004801#if defined(TARGET_PPC64)
Riku Voipio1d8b5122014-04-23 10:26:05 +03004802 __put_user(set->sig[0] >> 32, &sc->_unused[3]);
Nathan Froydbcd49332009-05-12 19:13:18 -07004803#else
Riku Voipio1d8b5122014-04-23 10:26:05 +03004804 __put_user(set->sig[1], &sc->_unused[3]);
Nathan Froydbcd49332009-05-12 19:13:18 -07004805#endif
Riku Voipio1d8b5122014-04-23 10:26:05 +03004806 __put_user(h2g(&frame->mctx), &sc->regs);
4807 __put_user(sig, &sc->signal);
Nathan Froydbcd49332009-05-12 19:13:18 -07004808
4809 /* Save user regs. */
4810 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4811
4812 /* The kernel checks for the presence of a VDSO here. We don't
4813 emulate a vdso, so use a sigreturn system call. */
4814 env->lr = (target_ulong) h2g(frame->mctx.tramp);
4815
4816 /* Turn off all fp exceptions. */
4817 env->fpscr = 0;
4818
4819 /* Create a stack frame for the caller of the handler. */
4820 newsp = frame_addr - SIGNAL_FRAMESIZE;
Samuel Seaybeb526b2013-01-02 10:53:46 +00004821 err |= put_user(env->gpr[1], newsp, target_ulong);
Nathan Froydbcd49332009-05-12 19:13:18 -07004822
4823 if (err)
4824 goto sigsegv;
4825
4826 /* Set up registers for signal handler. */
4827 env->gpr[1] = newsp;
4828 env->gpr[3] = signal;
Samuel Seay61993a62013-01-04 14:35:48 +00004829 env->gpr[4] = frame_addr + offsetof(struct target_sigframe, sctx);
Nathan Froydbcd49332009-05-12 19:13:18 -07004830 env->nip = (target_ulong) ka->_sa_handler;
4831 /* Signal handlers are entered in big-endian mode. */
4832 env->msr &= ~MSR_LE;
4833
4834 unlock_user_struct(frame, frame_addr, 1);
4835 return;
4836
4837sigsegv:
4838 unlock_user_struct(frame, frame_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004839 qemu_log("segfaulting from setup_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004840 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004841}
4842
4843static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004844 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01004845 target_sigset_t *set, CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004846{
4847 struct target_rt_sigframe *rt_sf;
4848 struct target_mcontext *frame;
4849 target_ulong rt_sf_addr, newsp = 0;
4850 int i, err = 0;
4851 int signal;
4852
4853 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4854 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4855 goto sigsegv;
4856
4857 signal = current_exec_domain_sig(sig);
4858
Riku Voipiob0fd8d12014-04-23 10:46:13 +03004859 copy_siginfo_to_user(&rt_sf->info, info);
Nathan Froydbcd49332009-05-12 19:13:18 -07004860
Riku Voipio1d8b5122014-04-23 10:26:05 +03004861 __put_user(0, &rt_sf->uc.tuc_flags);
4862 __put_user(0, &rt_sf->uc.tuc_link);
4863 __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4864 &rt_sf->uc.tuc_stack.ss_sp);
4865 __put_user(sas_ss_flags(env->gpr[1]),
4866 &rt_sf->uc.tuc_stack.ss_flags);
4867 __put_user(target_sigaltstack_used.ss_size,
4868 &rt_sf->uc.tuc_stack.ss_size);
4869 __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4870 &rt_sf->uc.tuc_regs);
Nathan Froydbcd49332009-05-12 19:13:18 -07004871 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03004872 __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
Nathan Froydbcd49332009-05-12 19:13:18 -07004873 }
4874
Aurelien Jarno60e99242010-03-29 02:12:51 +02004875 frame = &rt_sf->uc.tuc_mcontext;
Nathan Froydbcd49332009-05-12 19:13:18 -07004876 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4877
4878 /* The kernel checks for the presence of a VDSO here. We don't
4879 emulate a vdso, so use a sigreturn system call. */
4880 env->lr = (target_ulong) h2g(frame->tramp);
4881
4882 /* Turn off all fp exceptions. */
4883 env->fpscr = 0;
4884
4885 /* Create a stack frame for the caller of the handler. */
4886 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
Riku Voipio1d8b5122014-04-23 10:26:05 +03004887 __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
Nathan Froydbcd49332009-05-12 19:13:18 -07004888
4889 if (err)
4890 goto sigsegv;
4891
4892 /* Set up registers for signal handler. */
4893 env->gpr[1] = newsp;
4894 env->gpr[3] = (target_ulong) signal;
4895 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4896 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4897 env->gpr[6] = (target_ulong) h2g(rt_sf);
4898 env->nip = (target_ulong) ka->_sa_handler;
4899 /* Signal handlers are entered in big-endian mode. */
4900 env->msr &= ~MSR_LE;
4901
4902 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4903 return;
4904
4905sigsegv:
4906 unlock_user_struct(rt_sf, rt_sf_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004907 qemu_log("segfaulting from setup_rt_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004908 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004909
4910}
4911
Andreas Färber05390242012-02-25 03:37:53 +01004912long do_sigreturn(CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004913{
4914 struct target_sigcontext *sc = NULL;
4915 struct target_mcontext *sr = NULL;
Peter Maydellb04636f2013-07-29 12:00:31 +01004916 target_ulong sr_addr = 0, sc_addr;
Nathan Froydbcd49332009-05-12 19:13:18 -07004917 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05004918 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07004919
4920 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4921 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4922 goto sigsegv;
4923
4924#if defined(TARGET_PPC64)
4925 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4926#else
4927 if(__get_user(set.sig[0], &sc->oldmask) ||
4928 __get_user(set.sig[1], &sc->_unused[3]))
4929 goto sigsegv;
4930#endif
4931 target_to_host_sigset_internal(&blocked, &set);
Alex Barcelo1c275922014-03-14 14:36:55 +00004932 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
Nathan Froydbcd49332009-05-12 19:13:18 -07004933
4934 if (__get_user(sr_addr, &sc->regs))
4935 goto sigsegv;
4936 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4937 goto sigsegv;
4938 if (restore_user_regs(env, sr, 1))
4939 goto sigsegv;
4940
4941 unlock_user_struct(sr, sr_addr, 1);
4942 unlock_user_struct(sc, sc_addr, 1);
4943 return -TARGET_QEMU_ESIGRETURN;
4944
4945sigsegv:
4946 unlock_user_struct(sr, sr_addr, 1);
4947 unlock_user_struct(sc, sc_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00004948 qemu_log("segfaulting from do_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004949 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004950 return 0;
4951}
4952
4953/* See arch/powerpc/kernel/signal_32.c. */
Andreas Färber05390242012-02-25 03:37:53 +01004954static int do_setcontext(struct target_ucontext *ucp, CPUPPCState *env, int sig)
Nathan Froydbcd49332009-05-12 19:13:18 -07004955{
4956 struct target_mcontext *mcp;
4957 target_ulong mcp_addr;
4958 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05004959 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07004960
Aurelien Jarno60e99242010-03-29 02:12:51 +02004961 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
Nathan Froydbcd49332009-05-12 19:13:18 -07004962 sizeof (set)))
4963 return 1;
4964
4965#if defined(TARGET_PPC64)
4966 fprintf (stderr, "do_setcontext: not implemented\n");
4967 return 0;
4968#else
Aurelien Jarno60e99242010-03-29 02:12:51 +02004969 if (__get_user(mcp_addr, &ucp->tuc_regs))
Nathan Froydbcd49332009-05-12 19:13:18 -07004970 return 1;
4971
4972 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4973 return 1;
4974
4975 target_to_host_sigset_internal(&blocked, &set);
Alex Barcelo1c275922014-03-14 14:36:55 +00004976 do_sigprocmask(SIG_SETMASK, &blocked, NULL);
Nathan Froydbcd49332009-05-12 19:13:18 -07004977 if (restore_user_regs(env, mcp, sig))
4978 goto sigsegv;
4979
4980 unlock_user_struct(mcp, mcp_addr, 1);
4981 return 0;
4982
4983sigsegv:
4984 unlock_user_struct(mcp, mcp_addr, 1);
4985 return 1;
4986#endif
4987}
4988
Andreas Färber05390242012-02-25 03:37:53 +01004989long do_rt_sigreturn(CPUPPCState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07004990{
4991 struct target_rt_sigframe *rt_sf = NULL;
4992 target_ulong rt_sf_addr;
4993
4994 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4995 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4996 goto sigsegv;
4997
4998 if (do_setcontext(&rt_sf->uc, env, 1))
4999 goto sigsegv;
5000
5001 do_sigaltstack(rt_sf_addr
Aurelien Jarno60e99242010-03-29 02:12:51 +02005002 + offsetof(struct target_rt_sigframe, uc.tuc_stack),
Nathan Froydbcd49332009-05-12 19:13:18 -07005003 0, env->gpr[1]);
5004
5005 unlock_user_struct(rt_sf, rt_sf_addr, 1);
5006 return -TARGET_QEMU_ESIGRETURN;
5007
5008sigsegv:
5009 unlock_user_struct(rt_sf, rt_sf_addr, 1);
Blue Swirleeacee42012-06-03 16:35:32 +00005010 qemu_log("segfaulting from do_rt_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02005011 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07005012 return 0;
5013}
5014
Laurent Vivier492a8742009-08-03 16:12:17 +02005015#elif defined(TARGET_M68K)
5016
5017struct target_sigcontext {
5018 abi_ulong sc_mask;
5019 abi_ulong sc_usp;
5020 abi_ulong sc_d0;
5021 abi_ulong sc_d1;
5022 abi_ulong sc_a0;
5023 abi_ulong sc_a1;
5024 unsigned short sc_sr;
5025 abi_ulong sc_pc;
5026};
5027
5028struct target_sigframe
5029{
5030 abi_ulong pretcode;
5031 int sig;
5032 int code;
5033 abi_ulong psc;
5034 char retcode[8];
5035 abi_ulong extramask[TARGET_NSIG_WORDS-1];
5036 struct target_sigcontext sc;
5037};
Laurent Vivier71811552009-08-03 16:12:18 +02005038
Anthony Liguoric227f092009-10-01 16:12:16 -05005039typedef int target_greg_t;
Laurent Vivier71811552009-08-03 16:12:18 +02005040#define TARGET_NGREG 18
Anthony Liguoric227f092009-10-01 16:12:16 -05005041typedef target_greg_t target_gregset_t[TARGET_NGREG];
Laurent Vivier71811552009-08-03 16:12:18 +02005042
5043typedef struct target_fpregset {
5044 int f_fpcntl[3];
5045 int f_fpregs[8*3];
Anthony Liguoric227f092009-10-01 16:12:16 -05005046} target_fpregset_t;
Laurent Vivier71811552009-08-03 16:12:18 +02005047
5048struct target_mcontext {
5049 int version;
Anthony Liguoric227f092009-10-01 16:12:16 -05005050 target_gregset_t gregs;
5051 target_fpregset_t fpregs;
Laurent Vivier71811552009-08-03 16:12:18 +02005052};
5053
5054#define TARGET_MCONTEXT_VERSION 2
5055
5056struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02005057 abi_ulong tuc_flags;
5058 abi_ulong tuc_link;
5059 target_stack_t tuc_stack;
5060 struct target_mcontext tuc_mcontext;
5061 abi_long tuc_filler[80];
5062 target_sigset_t tuc_sigmask;
Laurent Vivier71811552009-08-03 16:12:18 +02005063};
5064
5065struct target_rt_sigframe
5066{
5067 abi_ulong pretcode;
5068 int sig;
5069 abi_ulong pinfo;
5070 abi_ulong puc;
5071 char retcode[8];
5072 struct target_siginfo info;
5073 struct target_ucontext uc;
5074};
Laurent Vivier492a8742009-08-03 16:12:17 +02005075
Riku Voipio41ecc722014-04-23 11:01:00 +03005076static void setup_sigcontext(struct target_sigcontext *sc, CPUM68KState *env,
5077 abi_ulong mask)
Laurent Vivier492a8742009-08-03 16:12:17 +02005078{
Riku Voipio1d8b5122014-04-23 10:26:05 +03005079 __put_user(mask, &sc->sc_mask);
5080 __put_user(env->aregs[7], &sc->sc_usp);
5081 __put_user(env->dregs[0], &sc->sc_d0);
5082 __put_user(env->dregs[1], &sc->sc_d1);
5083 __put_user(env->aregs[0], &sc->sc_a0);
5084 __put_user(env->aregs[1], &sc->sc_a1);
5085 __put_user(env->sr, &sc->sc_sr);
5086 __put_user(env->pc, &sc->sc_pc);
Laurent Vivier492a8742009-08-03 16:12:17 +02005087}
5088
5089static int
Andreas Färber05390242012-02-25 03:37:53 +01005090restore_sigcontext(CPUM68KState *env, struct target_sigcontext *sc, int *pd0)
Laurent Vivier492a8742009-08-03 16:12:17 +02005091{
5092 int err = 0;
5093 int temp;
5094
Riku Voipio1d8b5122014-04-23 10:26:05 +03005095 __get_user(env->aregs[7], &sc->sc_usp);
5096 __get_user(env->dregs[1], &sc->sc_d1);
5097 __get_user(env->aregs[0], &sc->sc_a0);
5098 __get_user(env->aregs[1], &sc->sc_a1);
5099 __get_user(env->pc, &sc->sc_pc);
5100 __get_user(temp, &sc->sc_sr);
Laurent Vivier492a8742009-08-03 16:12:17 +02005101 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5102
5103 *pd0 = tswapl(sc->sc_d0);
5104
5105 return err;
5106}
5107
5108/*
5109 * Determine which stack to use..
5110 */
5111static inline abi_ulong
Andreas Färber05390242012-02-25 03:37:53 +01005112get_sigframe(struct target_sigaction *ka, CPUM68KState *regs,
5113 size_t frame_size)
Laurent Vivier492a8742009-08-03 16:12:17 +02005114{
5115 unsigned long sp;
5116
5117 sp = regs->aregs[7];
5118
5119 /* This is the X/Open sanctioned signal stack switching. */
5120 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
5121 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5122 }
5123
5124 return ((sp - frame_size) & -8UL);
5125}
5126
5127static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01005128 target_sigset_t *set, CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005129{
5130 struct target_sigframe *frame;
5131 abi_ulong frame_addr;
5132 abi_ulong retcode_addr;
5133 abi_ulong sc_addr;
5134 int err = 0;
5135 int i;
5136
5137 frame_addr = get_sigframe(ka, env, sizeof *frame);
5138 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5139 goto give_sigsegv;
5140
Riku Voipio1d8b5122014-04-23 10:26:05 +03005141 __put_user(sig, &frame->sig);
Laurent Vivier492a8742009-08-03 16:12:17 +02005142
5143 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005144 __put_user(sc_addr, &frame->psc);
Laurent Vivier492a8742009-08-03 16:12:17 +02005145
Riku Voipio41ecc722014-04-23 11:01:00 +03005146 setup_sigcontext(&frame->sc, env, set->sig[0]);
Laurent Vivier492a8742009-08-03 16:12:17 +02005147
5148 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5149 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
5150 goto give_sigsegv;
5151 }
5152
5153 /* Set up to return from userspace. */
5154
5155 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005156 __put_user(retcode_addr, &frame->pretcode);
Laurent Vivier492a8742009-08-03 16:12:17 +02005157
5158 /* moveq #,d0; trap #0 */
5159
Riku Voipio1d8b5122014-04-23 10:26:05 +03005160 __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
Laurent Vivier492a8742009-08-03 16:12:17 +02005161 (long *)(frame->retcode));
5162
5163 if (err)
5164 goto give_sigsegv;
5165
5166 /* Set up to return from userspace */
5167
5168 env->aregs[7] = frame_addr;
5169 env->pc = ka->_sa_handler;
5170
5171 unlock_user_struct(frame, frame_addr, 1);
5172 return;
5173
5174give_sigsegv:
5175 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02005176 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02005177}
5178
Laurent Vivier71811552009-08-03 16:12:18 +02005179static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
Andreas Färber05390242012-02-25 03:37:53 +01005180 CPUM68KState *env)
Laurent Vivier71811552009-08-03 16:12:18 +02005181{
Aurelien Jarno60e99242010-03-29 02:12:51 +02005182 target_greg_t *gregs = uc->tuc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02005183
Riku Voipio1d8b5122014-04-23 10:26:05 +03005184 __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
5185 __put_user(env->dregs[0], &gregs[0]);
5186 __put_user(env->dregs[1], &gregs[1]);
5187 __put_user(env->dregs[2], &gregs[2]);
5188 __put_user(env->dregs[3], &gregs[3]);
5189 __put_user(env->dregs[4], &gregs[4]);
5190 __put_user(env->dregs[5], &gregs[5]);
5191 __put_user(env->dregs[6], &gregs[6]);
5192 __put_user(env->dregs[7], &gregs[7]);
5193 __put_user(env->aregs[0], &gregs[8]);
5194 __put_user(env->aregs[1], &gregs[9]);
5195 __put_user(env->aregs[2], &gregs[10]);
5196 __put_user(env->aregs[3], &gregs[11]);
5197 __put_user(env->aregs[4], &gregs[12]);
5198 __put_user(env->aregs[5], &gregs[13]);
5199 __put_user(env->aregs[6], &gregs[14]);
5200 __put_user(env->aregs[7], &gregs[15]);
5201 __put_user(env->pc, &gregs[16]);
5202 __put_user(env->sr, &gregs[17]);
Laurent Vivier71811552009-08-03 16:12:18 +02005203
Riku Voipio1d8b5122014-04-23 10:26:05 +03005204 return 0;
Laurent Vivier71811552009-08-03 16:12:18 +02005205}
5206
Andreas Färber05390242012-02-25 03:37:53 +01005207static inline int target_rt_restore_ucontext(CPUM68KState *env,
Laurent Vivier71811552009-08-03 16:12:18 +02005208 struct target_ucontext *uc,
5209 int *pd0)
5210{
5211 int temp;
Aurelien Jarno60e99242010-03-29 02:12:51 +02005212 target_greg_t *gregs = uc->tuc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02005213
Riku Voipio1d8b5122014-04-23 10:26:05 +03005214 __get_user(temp, &uc->tuc_mcontext.version);
Laurent Vivier71811552009-08-03 16:12:18 +02005215 if (temp != TARGET_MCONTEXT_VERSION)
5216 goto badframe;
5217
5218 /* restore passed registers */
Riku Voipio1d8b5122014-04-23 10:26:05 +03005219 __get_user(env->dregs[0], &gregs[0]);
5220 __get_user(env->dregs[1], &gregs[1]);
5221 __get_user(env->dregs[2], &gregs[2]);
5222 __get_user(env->dregs[3], &gregs[3]);
5223 __get_user(env->dregs[4], &gregs[4]);
5224 __get_user(env->dregs[5], &gregs[5]);
5225 __get_user(env->dregs[6], &gregs[6]);
5226 __get_user(env->dregs[7], &gregs[7]);
5227 __get_user(env->aregs[0], &gregs[8]);
5228 __get_user(env->aregs[1], &gregs[9]);
5229 __get_user(env->aregs[2], &gregs[10]);
5230 __get_user(env->aregs[3], &gregs[11]);
5231 __get_user(env->aregs[4], &gregs[12]);
5232 __get_user(env->aregs[5], &gregs[13]);
5233 __get_user(env->aregs[6], &gregs[14]);
5234 __get_user(env->aregs[7], &gregs[15]);
5235 __get_user(env->pc, &gregs[16]);
5236 __get_user(temp, &gregs[17]);
Laurent Vivier71811552009-08-03 16:12:18 +02005237 env->sr = (env->sr & 0xff00) | (temp & 0xff);
5238
5239 *pd0 = env->dregs[0];
Riku Voipio1d8b5122014-04-23 10:26:05 +03005240 return 0;
Laurent Vivier71811552009-08-03 16:12:18 +02005241
5242badframe:
5243 return 1;
5244}
5245
Laurent Vivier492a8742009-08-03 16:12:17 +02005246static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05005247 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01005248 target_sigset_t *set, CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005249{
Laurent Vivier71811552009-08-03 16:12:18 +02005250 struct target_rt_sigframe *frame;
5251 abi_ulong frame_addr;
5252 abi_ulong retcode_addr;
5253 abi_ulong info_addr;
5254 abi_ulong uc_addr;
5255 int err = 0;
5256 int i;
5257
5258 frame_addr = get_sigframe(ka, env, sizeof *frame);
5259 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
5260 goto give_sigsegv;
5261
Riku Voipio1d8b5122014-04-23 10:26:05 +03005262 __put_user(sig, &frame->sig);
Laurent Vivier71811552009-08-03 16:12:18 +02005263
5264 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005265 __put_user(info_addr, &frame->pinfo);
Laurent Vivier71811552009-08-03 16:12:18 +02005266
5267 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005268 __put_user(uc_addr, &frame->puc);
Laurent Vivier71811552009-08-03 16:12:18 +02005269
Riku Voipiob0fd8d12014-04-23 10:46:13 +03005270 copy_siginfo_to_user(&frame->info, info);
Laurent Vivier71811552009-08-03 16:12:18 +02005271
5272 /* Create the ucontext */
5273
Riku Voipio1d8b5122014-04-23 10:26:05 +03005274 __put_user(0, &frame->uc.tuc_flags);
5275 __put_user(0, &frame->uc.tuc_link);
5276 __put_user(target_sigaltstack_used.ss_sp,
5277 &frame->uc.tuc_stack.ss_sp);
5278 __put_user(sas_ss_flags(env->aregs[7]),
5279 &frame->uc.tuc_stack.ss_flags);
5280 __put_user(target_sigaltstack_used.ss_size,
5281 &frame->uc.tuc_stack.ss_size);
Laurent Vivier71811552009-08-03 16:12:18 +02005282 err |= target_rt_setup_ucontext(&frame->uc, env);
5283
5284 if (err)
5285 goto give_sigsegv;
5286
5287 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
Aurelien Jarno60e99242010-03-29 02:12:51 +02005288 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
Laurent Vivier71811552009-08-03 16:12:18 +02005289 goto give_sigsegv;
5290 }
5291
5292 /* Set up to return from userspace. */
5293
5294 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
Riku Voipio1d8b5122014-04-23 10:26:05 +03005295 __put_user(retcode_addr, &frame->pretcode);
Laurent Vivier71811552009-08-03 16:12:18 +02005296
5297 /* moveq #,d0; notb d0; trap #0 */
5298
Riku Voipio1d8b5122014-04-23 10:26:05 +03005299 __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
5300 (long *)(frame->retcode + 0));
5301 __put_user(0x4e40, (short *)(frame->retcode + 4));
Laurent Vivier71811552009-08-03 16:12:18 +02005302
5303 if (err)
5304 goto give_sigsegv;
5305
5306 /* Set up to return from userspace */
5307
5308 env->aregs[7] = frame_addr;
5309 env->pc = ka->_sa_handler;
5310
5311 unlock_user_struct(frame, frame_addr, 1);
5312 return;
5313
5314give_sigsegv:
5315 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02005316 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02005317}
5318
Andreas Färber05390242012-02-25 03:37:53 +01005319long do_sigreturn(CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005320{
5321 struct target_sigframe *frame;
5322 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05005323 target_sigset_t target_set;
Laurent Vivier492a8742009-08-03 16:12:17 +02005324 sigset_t set;
5325 int d0, i;
5326
5327 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5328 goto badframe;
5329
5330 /* set blocked signals */
5331
5332 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
5333 goto badframe;
5334
5335 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
5336 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
5337 goto badframe;
5338 }
5339
5340 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00005341 do_sigprocmask(SIG_SETMASK, &set, NULL);
Laurent Vivier492a8742009-08-03 16:12:17 +02005342
5343 /* restore registers */
5344
5345 if (restore_sigcontext(env, &frame->sc, &d0))
5346 goto badframe;
5347
5348 unlock_user_struct(frame, frame_addr, 0);
5349 return d0;
5350
5351badframe:
5352 unlock_user_struct(frame, frame_addr, 0);
5353 force_sig(TARGET_SIGSEGV);
5354 return 0;
5355}
5356
Andreas Färber05390242012-02-25 03:37:53 +01005357long do_rt_sigreturn(CPUM68KState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02005358{
Laurent Vivier71811552009-08-03 16:12:18 +02005359 struct target_rt_sigframe *frame;
5360 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05005361 target_sigset_t target_set;
Laurent Vivier71811552009-08-03 16:12:18 +02005362 sigset_t set;
5363 int d0;
5364
5365 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
5366 goto badframe;
5367
5368 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00005369 do_sigprocmask(SIG_SETMASK, &set, NULL);
Laurent Vivier71811552009-08-03 16:12:18 +02005370
5371 /* restore registers */
5372
5373 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
5374 goto badframe;
5375
5376 if (do_sigaltstack(frame_addr +
Aurelien Jarno60e99242010-03-29 02:12:51 +02005377 offsetof(struct target_rt_sigframe, uc.tuc_stack),
Laurent Vivier71811552009-08-03 16:12:18 +02005378 0, get_sp_from_cpustate(env)) == -EFAULT)
5379 goto badframe;
5380
5381 unlock_user_struct(frame, frame_addr, 0);
5382 return d0;
5383
5384badframe:
5385 unlock_user_struct(frame, frame_addr, 0);
5386 force_sig(TARGET_SIGSEGV);
5387 return 0;
Laurent Vivier492a8742009-08-03 16:12:17 +02005388}
5389
Richard Henderson6049f4f2009-12-27 18:30:03 -08005390#elif defined(TARGET_ALPHA)
5391
5392struct target_sigcontext {
5393 abi_long sc_onstack;
5394 abi_long sc_mask;
5395 abi_long sc_pc;
5396 abi_long sc_ps;
5397 abi_long sc_regs[32];
5398 abi_long sc_ownedfp;
5399 abi_long sc_fpregs[32];
5400 abi_ulong sc_fpcr;
5401 abi_ulong sc_fp_control;
5402 abi_ulong sc_reserved1;
5403 abi_ulong sc_reserved2;
5404 abi_ulong sc_ssize;
5405 abi_ulong sc_sbase;
5406 abi_ulong sc_traparg_a0;
5407 abi_ulong sc_traparg_a1;
5408 abi_ulong sc_traparg_a2;
5409 abi_ulong sc_fp_trap_pc;
5410 abi_ulong sc_fp_trigger_sum;
5411 abi_ulong sc_fp_trigger_inst;
5412};
5413
5414struct target_ucontext {
Aurelien Jarno60e99242010-03-29 02:12:51 +02005415 abi_ulong tuc_flags;
5416 abi_ulong tuc_link;
5417 abi_ulong tuc_osf_sigmask;
5418 target_stack_t tuc_stack;
5419 struct target_sigcontext tuc_mcontext;
5420 target_sigset_t tuc_sigmask;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005421};
5422
5423struct target_sigframe {
5424 struct target_sigcontext sc;
5425 unsigned int retcode[3];
5426};
5427
5428struct target_rt_sigframe {
5429 target_siginfo_t info;
5430 struct target_ucontext uc;
5431 unsigned int retcode[3];
5432};
5433
5434#define INSN_MOV_R30_R16 0x47fe0410
5435#define INSN_LDI_R0 0x201f0000
5436#define INSN_CALLSYS 0x00000083
5437
Riku Voipio41ecc722014-04-23 11:01:00 +03005438static void setup_sigcontext(struct target_sigcontext *sc, CPUAlphaState *env,
Richard Henderson6049f4f2009-12-27 18:30:03 -08005439 abi_ulong frame_addr, target_sigset_t *set)
5440{
Riku Voipio41ecc722014-04-23 11:01:00 +03005441 int i;
Richard Henderson6049f4f2009-12-27 18:30:03 -08005442
Riku Voipio1d8b5122014-04-23 10:26:05 +03005443 __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
5444 __put_user(set->sig[0], &sc->sc_mask);
5445 __put_user(env->pc, &sc->sc_pc);
5446 __put_user(8, &sc->sc_ps);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005447
5448 for (i = 0; i < 31; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005449 __put_user(env->ir[i], &sc->sc_regs[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005450 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03005451 __put_user(0, &sc->sc_regs[31]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005452
5453 for (i = 0; i < 31; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005454 __put_user(env->fir[i], &sc->sc_fpregs[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005455 }
Riku Voipio1d8b5122014-04-23 10:26:05 +03005456 __put_user(0, &sc->sc_fpregs[31]);
5457 __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005458
Riku Voipio1d8b5122014-04-23 10:26:05 +03005459 __put_user(0, &sc->sc_traparg_a0); /* FIXME */
5460 __put_user(0, &sc->sc_traparg_a1); /* FIXME */
5461 __put_user(0, &sc->sc_traparg_a2); /* FIXME */
Richard Henderson6049f4f2009-12-27 18:30:03 -08005462}
5463
Andreas Färber05390242012-02-25 03:37:53 +01005464static int restore_sigcontext(CPUAlphaState *env,
5465 struct target_sigcontext *sc)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005466{
5467 uint64_t fpcr;
5468 int i, err = 0;
5469
Riku Voipio1d8b5122014-04-23 10:26:05 +03005470 __get_user(env->pc, &sc->sc_pc);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005471
5472 for (i = 0; i < 31; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005473 __get_user(env->ir[i], &sc->sc_regs[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005474 }
5475 for (i = 0; i < 31; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005476 __get_user(env->fir[i], &sc->sc_fpregs[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005477 }
5478
Riku Voipio1d8b5122014-04-23 10:26:05 +03005479 __get_user(fpcr, &sc->sc_fpcr);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005480 cpu_alpha_store_fpcr(env, fpcr);
5481
5482 return err;
5483}
5484
5485static inline abi_ulong get_sigframe(struct target_sigaction *sa,
Andreas Färber05390242012-02-25 03:37:53 +01005486 CPUAlphaState *env,
5487 unsigned long framesize)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005488{
5489 abi_ulong sp = env->ir[IR_SP];
5490
5491 /* This is the X/Open sanctioned signal stack switching. */
5492 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
5493 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
5494 }
5495 return (sp - framesize) & -32;
5496}
5497
5498static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber05390242012-02-25 03:37:53 +01005499 target_sigset_t *set, CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005500{
5501 abi_ulong frame_addr, r26;
5502 struct target_sigframe *frame;
5503 int err = 0;
5504
5505 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5506 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5507 goto give_sigsegv;
5508 }
5509
Riku Voipio41ecc722014-04-23 11:01:00 +03005510 setup_sigcontext(&frame->sc, env, frame_addr, set);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005511
5512 if (ka->sa_restorer) {
5513 r26 = ka->sa_restorer;
5514 } else {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005515 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5516 __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
5517 &frame->retcode[1]);
5518 __put_user(INSN_CALLSYS, &frame->retcode[2]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005519 /* imb() */
5520 r26 = frame_addr;
5521 }
5522
5523 unlock_user_struct(frame, frame_addr, 1);
5524
5525 if (err) {
5526 give_sigsegv:
5527 if (sig == TARGET_SIGSEGV) {
5528 ka->_sa_handler = TARGET_SIG_DFL;
5529 }
5530 force_sig(TARGET_SIGSEGV);
5531 }
5532
5533 env->ir[IR_RA] = r26;
5534 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5535 env->ir[IR_A0] = sig;
5536 env->ir[IR_A1] = 0;
5537 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
5538 env->ir[IR_SP] = frame_addr;
5539}
5540
5541static void setup_rt_frame(int sig, struct target_sigaction *ka,
5542 target_siginfo_t *info,
Andreas Färber05390242012-02-25 03:37:53 +01005543 target_sigset_t *set, CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005544{
5545 abi_ulong frame_addr, r26;
5546 struct target_rt_sigframe *frame;
5547 int i, err = 0;
5548
5549 frame_addr = get_sigframe(ka, env, sizeof(*frame));
5550 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
5551 goto give_sigsegv;
5552 }
5553
Riku Voipiob0fd8d12014-04-23 10:46:13 +03005554 copy_siginfo_to_user(&frame->info, info);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005555
Riku Voipio1d8b5122014-04-23 10:26:05 +03005556 __put_user(0, &frame->uc.tuc_flags);
5557 __put_user(0, &frame->uc.tuc_link);
5558 __put_user(set->sig[0], &frame->uc.tuc_osf_sigmask);
5559 __put_user(target_sigaltstack_used.ss_sp,
5560 &frame->uc.tuc_stack.ss_sp);
5561 __put_user(sas_ss_flags(env->ir[IR_SP]),
5562 &frame->uc.tuc_stack.ss_flags);
5563 __put_user(target_sigaltstack_used.ss_size,
5564 &frame->uc.tuc_stack.ss_size);
Riku Voipio41ecc722014-04-23 11:01:00 +03005565 setup_sigcontext(&frame->uc.tuc_mcontext, env, frame_addr, set);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005566 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005567 __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005568 }
5569
5570 if (ka->sa_restorer) {
5571 r26 = ka->sa_restorer;
5572 } else {
Riku Voipio1d8b5122014-04-23 10:26:05 +03005573 __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
5574 __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
5575 &frame->retcode[1]);
5576 __put_user(INSN_CALLSYS, &frame->retcode[2]);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005577 /* imb(); */
5578 r26 = frame_addr;
5579 }
5580
5581 if (err) {
5582 give_sigsegv:
5583 if (sig == TARGET_SIGSEGV) {
5584 ka->_sa_handler = TARGET_SIG_DFL;
5585 }
5586 force_sig(TARGET_SIGSEGV);
5587 }
5588
5589 env->ir[IR_RA] = r26;
5590 env->ir[IR_PV] = env->pc = ka->_sa_handler;
5591 env->ir[IR_A0] = sig;
5592 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
5593 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
5594 env->ir[IR_SP] = frame_addr;
5595}
5596
Andreas Färber05390242012-02-25 03:37:53 +01005597long do_sigreturn(CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005598{
5599 struct target_sigcontext *sc;
5600 abi_ulong sc_addr = env->ir[IR_A0];
5601 target_sigset_t target_set;
5602 sigset_t set;
5603
5604 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
5605 goto badframe;
5606 }
5607
5608 target_sigemptyset(&target_set);
5609 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
5610 goto badframe;
5611 }
5612
5613 target_to_host_sigset_internal(&set, &target_set);
Alex Barcelo1c275922014-03-14 14:36:55 +00005614 do_sigprocmask(SIG_SETMASK, &set, NULL);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005615
5616 if (restore_sigcontext(env, sc)) {
5617 goto badframe;
5618 }
5619 unlock_user_struct(sc, sc_addr, 0);
5620 return env->ir[IR_V0];
5621
5622 badframe:
5623 unlock_user_struct(sc, sc_addr, 0);
5624 force_sig(TARGET_SIGSEGV);
5625}
5626
Andreas Färber05390242012-02-25 03:37:53 +01005627long do_rt_sigreturn(CPUAlphaState *env)
Richard Henderson6049f4f2009-12-27 18:30:03 -08005628{
5629 abi_ulong frame_addr = env->ir[IR_A0];
5630 struct target_rt_sigframe *frame;
5631 sigset_t set;
5632
5633 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
5634 goto badframe;
5635 }
Aurelien Jarno60e99242010-03-29 02:12:51 +02005636 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
Alex Barcelo1c275922014-03-14 14:36:55 +00005637 do_sigprocmask(SIG_SETMASK, &set, NULL);
Richard Henderson6049f4f2009-12-27 18:30:03 -08005638
Aurelien Jarno60e99242010-03-29 02:12:51 +02005639 if (restore_sigcontext(env, &frame->uc.tuc_mcontext)) {
Richard Henderson6049f4f2009-12-27 18:30:03 -08005640 goto badframe;
5641 }
5642 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
Aurelien Jarno60e99242010-03-29 02:12:51 +02005643 uc.tuc_stack),
Richard Henderson6049f4f2009-12-27 18:30:03 -08005644 0, env->ir[IR_SP]) == -EFAULT) {
5645 goto badframe;
5646 }
5647
5648 unlock_user_struct(frame, frame_addr, 0);
5649 return env->ir[IR_V0];
5650
5651
5652 badframe:
5653 unlock_user_struct(frame, frame_addr, 0);
5654 force_sig(TARGET_SIGSEGV);
5655}
5656
bellardb346ff42003-06-15 20:05:50 +00005657#else
5658
pbrook624f7972008-05-31 16:11:38 +00005659static void setup_frame(int sig, struct target_sigaction *ka,
Andreas Färber9349b4f2012-03-14 01:38:32 +01005660 target_sigset_t *set, CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005661{
5662 fprintf(stderr, "setup_frame: not implemented\n");
5663}
5664
pbrook624f7972008-05-31 16:11:38 +00005665static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05005666 target_siginfo_t *info,
Andreas Färber9349b4f2012-03-14 01:38:32 +01005667 target_sigset_t *set, CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005668{
5669 fprintf(stderr, "setup_rt_frame: not implemented\n");
5670}
5671
Andreas Färber9349b4f2012-03-14 01:38:32 +01005672long do_sigreturn(CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005673{
5674 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00005675 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00005676}
5677
Andreas Färber9349b4f2012-03-14 01:38:32 +01005678long do_rt_sigreturn(CPUArchState *env)
bellardb346ff42003-06-15 20:05:50 +00005679{
5680 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00005681 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00005682}
5683
bellard66fb9762003-03-23 01:06:05 +00005684#endif
5685
Andreas Färber9349b4f2012-03-14 01:38:32 +01005686void process_pending_signals(CPUArchState *cpu_env)
bellard66fb9762003-03-23 01:06:05 +00005687{
Andreas Färberdb6b81d2013-06-27 19:49:31 +02005688 CPUState *cpu = ENV_GET_CPU(cpu_env);
bellard66fb9762003-03-23 01:06:05 +00005689 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00005690 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00005691 sigset_t set, old_set;
Anthony Liguoric227f092009-10-01 16:12:16 -05005692 target_sigset_t target_old_set;
pbrook624f7972008-05-31 16:11:38 +00005693 struct emulated_sigtable *k;
5694 struct target_sigaction *sa;
bellard66fb9762003-03-23 01:06:05 +00005695 struct sigqueue *q;
Andreas Färber0429a972013-08-26 18:14:44 +02005696 TaskState *ts = cpu->opaque;
ths3b46e622007-09-17 08:09:54 +00005697
pbrook624f7972008-05-31 16:11:38 +00005698 if (!ts->signal_pending)
bellard31e31b82003-02-18 22:55:36 +00005699 return;
5700
pbrook624f7972008-05-31 16:11:38 +00005701 /* FIXME: This is not threadsafe. */
5702 k = ts->sigtab;
bellard66fb9762003-03-23 01:06:05 +00005703 for(sig = 1; sig <= TARGET_NSIG; sig++) {
5704 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00005705 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00005706 k++;
bellard31e31b82003-02-18 22:55:36 +00005707 }
5708 /* if no signal is pending, just return */
pbrook624f7972008-05-31 16:11:38 +00005709 ts->signal_pending = 0;
bellard31e31b82003-02-18 22:55:36 +00005710 return;
bellard66fb9762003-03-23 01:06:05 +00005711
bellard31e31b82003-02-18 22:55:36 +00005712 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00005713#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00005714 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00005715#endif
5716 /* dequeue signal */
5717 q = k->first;
5718 k->first = q->next;
5719 if (!k->first)
5720 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00005721
Andreas Färberdb6b81d2013-06-27 19:49:31 +02005722 sig = gdb_handlesig(cpu, sig);
bellard1fddef42005-04-17 19:16:13 +00005723 if (!sig) {
aurel32ca587a82008-12-18 22:44:13 +00005724 sa = NULL;
5725 handler = TARGET_SIG_IGN;
5726 } else {
5727 sa = &sigact_table[sig - 1];
5728 handler = sa->_sa_handler;
bellard1fddef42005-04-17 19:16:13 +00005729 }
bellard66fb9762003-03-23 01:06:05 +00005730
Peter Maydella7ec0f92014-03-14 14:36:56 +00005731 if (ts->sigsegv_blocked && sig == TARGET_SIGSEGV) {
5732 /* Guest has blocked SIGSEGV but we got one anyway. Assume this
5733 * is a forced SIGSEGV (ie one the kernel handles via force_sig_info
5734 * because it got a real MMU fault), and treat as if default handler.
5735 */
5736 handler = TARGET_SIG_DFL;
5737 }
5738
bellard66fb9762003-03-23 01:06:05 +00005739 if (handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +00005740 /* default handler : ignore some signal. The other are job control or fatal */
5741 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
5742 kill(getpid(),SIGSTOP);
5743 } else if (sig != TARGET_SIGCHLD &&
5744 sig != TARGET_SIGURG &&
5745 sig != TARGET_SIGWINCH &&
5746 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +00005747 force_sig(sig);
5748 }
5749 } else if (handler == TARGET_SIG_IGN) {
5750 /* ignore sig */
5751 } else if (handler == TARGET_SIG_ERR) {
5752 force_sig(sig);
5753 } else {
bellard9de5e442003-03-23 16:49:39 +00005754 /* compute the blocked signals during the handler execution */
pbrook624f7972008-05-31 16:11:38 +00005755 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +00005756 /* SA_NODEFER indicates that the current signal should not be
5757 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +00005758 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +00005759 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00005760
bellard9de5e442003-03-23 16:49:39 +00005761 /* block signals in the handler using Linux */
Alex Barcelo1c275922014-03-14 14:36:55 +00005762 do_sigprocmask(SIG_BLOCK, &set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00005763 /* save the previous blocked signal state to restore it at the
5764 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00005765 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00005766
bellardbc8a22c2003-03-30 21:02:40 +00005767 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00005768#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00005769 {
5770 CPUX86State *env = cpu_env;
5771 if (env->eflags & VM_MASK)
5772 save_v86_state(env);
5773 }
5774#endif
bellard9de5e442003-03-23 16:49:39 +00005775 /* prepare the stack frame of the virtual CPU */
Richard Hendersonff970902013-02-10 10:30:42 -08005776#if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
5777 /* These targets do not have traditional signals. */
5778 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
5779#else
pbrook624f7972008-05-31 16:11:38 +00005780 if (sa->sa_flags & TARGET_SA_SIGINFO)
5781 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00005782 else
pbrook624f7972008-05-31 16:11:38 +00005783 setup_frame(sig, sa, &target_old_set, cpu_env);
Richard Hendersonff970902013-02-10 10:30:42 -08005784#endif
pbrook624f7972008-05-31 16:11:38 +00005785 if (sa->sa_flags & TARGET_SA_RESETHAND)
5786 sa->_sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00005787 }
bellard66fb9762003-03-23 01:06:05 +00005788 if (q != &k->info)
pbrook624f7972008-05-31 16:11:38 +00005789 free_sigqueue(cpu_env, q);
bellard31e31b82003-02-18 22:55:36 +00005790}