blob: e327c3dc164ecd0c520676c9922b0995d2e7744a [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>
bellard31e31b82003-02-18 22:55:36 +000024#include <signal.h>
bellard66fb9762003-03-23 01:06:05 +000025#include <errno.h>
aurel32603e4fd2009-04-15 16:18:38 +000026#include <assert.h>
bellard31e31b82003-02-18 22:55:36 +000027#include <sys/ucontext.h>
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030028#include <sys/resource.h>
bellard31e31b82003-02-18 22:55:36 +000029
bellard3ef693a2003-03-23 20:17:16 +000030#include "qemu.h"
blueswir17d99a002009-01-14 19:00:36 +000031#include "qemu-common.h"
blueswir1992f48a2007-10-14 16:27:31 +000032#include "target_signal.h"
bellard66fb9762003-03-23 01:06:05 +000033
34//#define DEBUG_SIGNAL
35
blueswir1249c4c32008-10-05 11:09:37 +000036static struct target_sigaltstack target_sigaltstack_used = {
thsa04e1342007-09-27 13:57:58 +000037 .ss_sp = 0,
38 .ss_size = 0,
39 .ss_flags = TARGET_SS_DISABLE,
40};
41
pbrook624f7972008-05-31 16:11:38 +000042static struct target_sigaction sigact_table[TARGET_NSIG];
bellard31e31b82003-02-18 22:55:36 +000043
ths5fafdf22007-09-16 21:08:06 +000044static void host_signal_handler(int host_signum, siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +000045 void *puc);
46
Arnaud Patard3ca05582009-03-30 01:18:20 +020047static uint8_t host_to_target_signal_table[_NSIG] = {
bellard9e5f5282003-07-13 17:33:54 +000048 [SIGHUP] = TARGET_SIGHUP,
49 [SIGINT] = TARGET_SIGINT,
50 [SIGQUIT] = TARGET_SIGQUIT,
51 [SIGILL] = TARGET_SIGILL,
52 [SIGTRAP] = TARGET_SIGTRAP,
53 [SIGABRT] = TARGET_SIGABRT,
bellard01e3b762003-09-30 21:10:14 +000054/* [SIGIOT] = TARGET_SIGIOT,*/
bellard9e5f5282003-07-13 17:33:54 +000055 [SIGBUS] = TARGET_SIGBUS,
56 [SIGFPE] = TARGET_SIGFPE,
57 [SIGKILL] = TARGET_SIGKILL,
58 [SIGUSR1] = TARGET_SIGUSR1,
59 [SIGSEGV] = TARGET_SIGSEGV,
60 [SIGUSR2] = TARGET_SIGUSR2,
61 [SIGPIPE] = TARGET_SIGPIPE,
62 [SIGALRM] = TARGET_SIGALRM,
63 [SIGTERM] = TARGET_SIGTERM,
64#ifdef SIGSTKFLT
65 [SIGSTKFLT] = TARGET_SIGSTKFLT,
66#endif
67 [SIGCHLD] = TARGET_SIGCHLD,
68 [SIGCONT] = TARGET_SIGCONT,
69 [SIGSTOP] = TARGET_SIGSTOP,
70 [SIGTSTP] = TARGET_SIGTSTP,
71 [SIGTTIN] = TARGET_SIGTTIN,
72 [SIGTTOU] = TARGET_SIGTTOU,
73 [SIGURG] = TARGET_SIGURG,
74 [SIGXCPU] = TARGET_SIGXCPU,
75 [SIGXFSZ] = TARGET_SIGXFSZ,
76 [SIGVTALRM] = TARGET_SIGVTALRM,
77 [SIGPROF] = TARGET_SIGPROF,
78 [SIGWINCH] = TARGET_SIGWINCH,
79 [SIGIO] = TARGET_SIGIO,
80 [SIGPWR] = TARGET_SIGPWR,
81 [SIGSYS] = TARGET_SIGSYS,
82 /* next signals stay the same */
pbrook624f7972008-05-31 16:11:38 +000083 /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
84 host libpthread signals. This assumes noone actually uses SIGRTMAX :-/
85 To fix this properly we need to do manual signal delivery multiplexed
86 over a single host signal. */
87 [__SIGRTMIN] = __SIGRTMAX,
88 [__SIGRTMAX] = __SIGRTMIN,
bellard9e5f5282003-07-13 17:33:54 +000089};
Arnaud Patard3ca05582009-03-30 01:18:20 +020090static uint8_t target_to_host_signal_table[_NSIG];
bellard9e5f5282003-07-13 17:33:54 +000091
thsa04e1342007-09-27 13:57:58 +000092static inline int on_sig_stack(unsigned long sp)
93{
94 return (sp - target_sigaltstack_used.ss_sp
95 < target_sigaltstack_used.ss_size);
96}
97
98static inline int sas_ss_flags(unsigned long sp)
99{
100 return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
101 : on_sig_stack(sp) ? SS_ONSTACK : 0);
102}
103
pbrook1d9d8b52009-04-16 15:17:02 +0000104int host_to_target_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +0000105{
Arnaud Patard3ca05582009-03-30 01:18:20 +0200106 if (sig >= _NSIG)
pbrook4cb05962008-05-30 18:05:19 +0000107 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000108 return host_to_target_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000109}
110
pbrook4cb05962008-05-30 18:05:19 +0000111int target_to_host_signal(int sig)
bellard31e31b82003-02-18 22:55:36 +0000112{
Arnaud Patard3ca05582009-03-30 01:18:20 +0200113 if (sig >= _NSIG)
pbrook4cb05962008-05-30 18:05:19 +0000114 return sig;
bellard9e5f5282003-07-13 17:33:54 +0000115 return target_to_host_signal_table[sig];
bellard31e31b82003-02-18 22:55:36 +0000116}
117
Anthony Liguoric227f092009-10-01 16:12:16 -0500118static inline void target_sigemptyset(target_sigset_t *set)
pbrookf5545b52008-05-30 22:37:07 +0000119{
120 memset(set, 0, sizeof(*set));
121}
122
Anthony Liguoric227f092009-10-01 16:12:16 -0500123static inline void target_sigaddset(target_sigset_t *set, int signum)
pbrookf5545b52008-05-30 22:37:07 +0000124{
125 signum--;
126 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
127 set->sig[signum / TARGET_NSIG_BPW] |= mask;
128}
129
Anthony Liguoric227f092009-10-01 16:12:16 -0500130static inline int target_sigismember(const target_sigset_t *set, int signum)
pbrookf5545b52008-05-30 22:37:07 +0000131{
132 signum--;
133 abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
134 return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
135}
136
Anthony Liguoric227f092009-10-01 16:12:16 -0500137static void host_to_target_sigset_internal(target_sigset_t *d,
bellard92319442004-06-19 16:58:13 +0000138 const sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000139{
140 int i;
pbrookf5545b52008-05-30 22:37:07 +0000141 target_sigemptyset(d);
142 for (i = 1; i <= TARGET_NSIG; i++) {
143 if (sigismember(s, i)) {
144 target_sigaddset(d, host_to_target_signal(i));
145 }
bellard9e5f5282003-07-13 17:33:54 +0000146 }
bellard66fb9762003-03-23 01:06:05 +0000147}
148
Anthony Liguoric227f092009-10-01 16:12:16 -0500149void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
bellard92319442004-06-19 16:58:13 +0000150{
Anthony Liguoric227f092009-10-01 16:12:16 -0500151 target_sigset_t d1;
bellard92319442004-06-19 16:58:13 +0000152 int i;
153
154 host_to_target_sigset_internal(&d1, s);
155 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000156 d->sig[i] = tswapl(d1.sig[i]);
bellard92319442004-06-19 16:58:13 +0000157}
158
blueswir18fcd3692008-08-17 20:26:25 +0000159static void target_to_host_sigset_internal(sigset_t *d,
Anthony Liguoric227f092009-10-01 16:12:16 -0500160 const target_sigset_t *s)
bellard66fb9762003-03-23 01:06:05 +0000161{
162 int i;
pbrookf5545b52008-05-30 22:37:07 +0000163 sigemptyset(d);
164 for (i = 1; i <= TARGET_NSIG; i++) {
165 if (target_sigismember(s, i)) {
166 sigaddset(d, target_to_host_signal(i));
167 }
168 }
bellard66fb9762003-03-23 01:06:05 +0000169}
170
Anthony Liguoric227f092009-10-01 16:12:16 -0500171void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
bellard92319442004-06-19 16:58:13 +0000172{
Anthony Liguoric227f092009-10-01 16:12:16 -0500173 target_sigset_t s1;
bellard92319442004-06-19 16:58:13 +0000174 int i;
175
176 for(i = 0;i < TARGET_NSIG_WORDS; i++)
pbrook53a59602006-03-25 19:31:22 +0000177 s1.sig[i] = tswapl(s->sig[i]);
bellard92319442004-06-19 16:58:13 +0000178 target_to_host_sigset_internal(d, &s1);
179}
ths3b46e622007-09-17 08:09:54 +0000180
blueswir1992f48a2007-10-14 16:27:31 +0000181void host_to_target_old_sigset(abi_ulong *old_sigset,
bellard66fb9762003-03-23 01:06:05 +0000182 const sigset_t *sigset)
183{
Anthony Liguoric227f092009-10-01 16:12:16 -0500184 target_sigset_t d;
bellard9e5f5282003-07-13 17:33:54 +0000185 host_to_target_sigset(&d, sigset);
186 *old_sigset = d.sig[0];
bellard66fb9762003-03-23 01:06:05 +0000187}
188
ths5fafdf22007-09-16 21:08:06 +0000189void target_to_host_old_sigset(sigset_t *sigset,
blueswir1992f48a2007-10-14 16:27:31 +0000190 const abi_ulong *old_sigset)
bellard66fb9762003-03-23 01:06:05 +0000191{
Anthony Liguoric227f092009-10-01 16:12:16 -0500192 target_sigset_t d;
bellard9e5f5282003-07-13 17:33:54 +0000193 int i;
194
195 d.sig[0] = *old_sigset;
196 for(i = 1;i < TARGET_NSIG_WORDS; i++)
197 d.sig[i] = 0;
198 target_to_host_sigset(sigset, &d);
bellard66fb9762003-03-23 01:06:05 +0000199}
200
bellard9de5e442003-03-23 16:49:39 +0000201/* siginfo conversion */
202
Anthony Liguoric227f092009-10-01 16:12:16 -0500203static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
bellard9de5e442003-03-23 16:49:39 +0000204 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000205{
bellard9de5e442003-03-23 16:49:39 +0000206 int sig;
207 sig = host_to_target_signal(info->si_signo);
208 tinfo->si_signo = sig;
209 tinfo->si_errno = 0;
pbrookafd7cd92008-05-31 12:14:21 +0000210 tinfo->si_code = info->si_code;
ths5fafdf22007-09-16 21:08:06 +0000211 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000212 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000213 /* should never come here, but who knows. The information for
214 the target is irrelevant */
215 tinfo->_sifields._sigfault._addr = 0;
ths7f7f7c82007-07-12 11:02:46 +0000216 } else if (sig == SIGIO) {
217 tinfo->_sifields._sigpoll._fd = info->si_fd;
bellard9de5e442003-03-23 16:49:39 +0000218 } else if (sig >= TARGET_SIGRTMIN) {
219 tinfo->_sifields._rt._pid = info->si_pid;
220 tinfo->_sifields._rt._uid = info->si_uid;
221 /* XXX: potential problem if 64 bit */
ths5fafdf22007-09-16 21:08:06 +0000222 tinfo->_sifields._rt._sigval.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000223 (abi_ulong)(unsigned long)info->si_value.sival_ptr;
bellard9de5e442003-03-23 16:49:39 +0000224 }
bellard66fb9762003-03-23 01:06:05 +0000225}
226
Anthony Liguoric227f092009-10-01 16:12:16 -0500227static void tswap_siginfo(target_siginfo_t *tinfo,
228 const target_siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000229{
230 int sig;
231 sig = info->si_signo;
232 tinfo->si_signo = tswap32(sig);
233 tinfo->si_errno = tswap32(info->si_errno);
234 tinfo->si_code = tswap32(info->si_code);
ths5fafdf22007-09-16 21:08:06 +0000235 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
bellard447db212003-05-10 15:10:36 +0000236 sig == SIGBUS || sig == SIGTRAP) {
ths5fafdf22007-09-16 21:08:06 +0000237 tinfo->_sifields._sigfault._addr =
bellard9de5e442003-03-23 16:49:39 +0000238 tswapl(info->_sifields._sigfault._addr);
ths7f7f7c82007-07-12 11:02:46 +0000239 } else if (sig == SIGIO) {
240 tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
bellard9de5e442003-03-23 16:49:39 +0000241 } else if (sig >= TARGET_SIGRTMIN) {
242 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
243 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000244 tinfo->_sifields._rt._sigval.sival_ptr =
bellard9de5e442003-03-23 16:49:39 +0000245 tswapl(info->_sifields._rt._sigval.sival_ptr);
246 }
247}
248
249
Anthony Liguoric227f092009-10-01 16:12:16 -0500250void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
bellard9de5e442003-03-23 16:49:39 +0000251{
252 host_to_target_siginfo_noswap(tinfo, info);
253 tswap_siginfo(tinfo, tinfo);
254}
255
256/* XXX: we support only POSIX RT signals are used. */
thsaa1f17c2007-07-11 22:48:58 +0000257/* XXX: find a solution for 64 bit (additional malloced data is needed) */
Anthony Liguoric227f092009-10-01 16:12:16 -0500258void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000259{
260 info->si_signo = tswap32(tinfo->si_signo);
261 info->si_errno = tswap32(tinfo->si_errno);
262 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000263 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
264 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
ths5fafdf22007-09-16 21:08:06 +0000265 info->si_value.sival_ptr =
bellard459a4012007-11-11 19:45:10 +0000266 (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000267}
268
aurel32ca587a82008-12-18 22:44:13 +0000269static int fatal_signal (int sig)
270{
271 switch (sig) {
272 case TARGET_SIGCHLD:
273 case TARGET_SIGURG:
274 case TARGET_SIGWINCH:
275 /* Ignored by default. */
276 return 0;
277 case TARGET_SIGCONT:
278 case TARGET_SIGSTOP:
279 case TARGET_SIGTSTP:
280 case TARGET_SIGTTIN:
281 case TARGET_SIGTTOU:
282 /* Job control signals. */
283 return 0;
284 default:
285 return 1;
286 }
287}
288
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300289/* returns 1 if given signal should dump core if not handled */
290static int core_dump_signal(int sig)
291{
292 switch (sig) {
293 case TARGET_SIGABRT:
294 case TARGET_SIGFPE:
295 case TARGET_SIGILL:
296 case TARGET_SIGQUIT:
297 case TARGET_SIGSEGV:
298 case TARGET_SIGTRAP:
299 case TARGET_SIGBUS:
300 return (1);
301 default:
302 return (0);
303 }
304}
305
bellard31e31b82003-02-18 22:55:36 +0000306void signal_init(void)
307{
308 struct sigaction act;
pbrook624f7972008-05-31 16:11:38 +0000309 struct sigaction oact;
bellard9e5f5282003-07-13 17:33:54 +0000310 int i, j;
pbrook624f7972008-05-31 16:11:38 +0000311 int host_sig;
bellard31e31b82003-02-18 22:55:36 +0000312
bellard9e5f5282003-07-13 17:33:54 +0000313 /* generate signal conversion tables */
Arnaud Patard3ca05582009-03-30 01:18:20 +0200314 for(i = 1; i < _NSIG; i++) {
bellard9e5f5282003-07-13 17:33:54 +0000315 if (host_to_target_signal_table[i] == 0)
316 host_to_target_signal_table[i] = i;
317 }
Arnaud Patard3ca05582009-03-30 01:18:20 +0200318 for(i = 1; i < _NSIG; i++) {
bellard9e5f5282003-07-13 17:33:54 +0000319 j = host_to_target_signal_table[i];
320 target_to_host_signal_table[j] = i;
321 }
ths3b46e622007-09-17 08:09:54 +0000322
bellard9de5e442003-03-23 16:49:39 +0000323 /* set all host signal handlers. ALL signals are blocked during
324 the handlers to serialize them. */
pbrook624f7972008-05-31 16:11:38 +0000325 memset(sigact_table, 0, sizeof(sigact_table));
326
bellard9de5e442003-03-23 16:49:39 +0000327 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000328 act.sa_flags = SA_SIGINFO;
329 act.sa_sigaction = host_signal_handler;
pbrook624f7972008-05-31 16:11:38 +0000330 for(i = 1; i <= TARGET_NSIG; i++) {
331 host_sig = target_to_host_signal(i);
332 sigaction(host_sig, NULL, &oact);
333 if (oact.sa_sigaction == (void *)SIG_IGN) {
334 sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
335 } else if (oact.sa_sigaction == (void *)SIG_DFL) {
336 sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
337 }
338 /* If there's already a handler installed then something has
339 gone horribly wrong, so don't even try to handle that case. */
aurel32ca587a82008-12-18 22:44:13 +0000340 /* Install some handlers for our own use. We need at least
341 SIGSEGV and SIGBUS, to detect exceptions. We can not just
342 trap all signals because it affects syscall interrupt
343 behavior. But do trap all default-fatal signals. */
344 if (fatal_signal (i))
pbrook624f7972008-05-31 16:11:38 +0000345 sigaction(host_sig, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000346 }
bellard31e31b82003-02-18 22:55:36 +0000347}
348
bellard66fb9762003-03-23 01:06:05 +0000349/* signal queue handling */
350
pbrook624f7972008-05-31 16:11:38 +0000351static inline struct sigqueue *alloc_sigqueue(CPUState *env)
bellard66fb9762003-03-23 01:06:05 +0000352{
pbrook624f7972008-05-31 16:11:38 +0000353 TaskState *ts = env->opaque;
354 struct sigqueue *q = ts->first_free;
bellard66fb9762003-03-23 01:06:05 +0000355 if (!q)
356 return NULL;
pbrook624f7972008-05-31 16:11:38 +0000357 ts->first_free = q->next;
bellard66fb9762003-03-23 01:06:05 +0000358 return q;
359}
360
pbrook624f7972008-05-31 16:11:38 +0000361static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
bellard66fb9762003-03-23 01:06:05 +0000362{
pbrook624f7972008-05-31 16:11:38 +0000363 TaskState *ts = env->opaque;
364 q->next = ts->first_free;
365 ts->first_free = q;
bellard66fb9762003-03-23 01:06:05 +0000366}
367
bellard9de5e442003-03-23 16:49:39 +0000368/* abort execution with signal */
Riku Voipio66393fb2009-12-04 15:16:32 +0200369static void QEMU_NORETURN force_sig(int target_sig)
bellard66fb9762003-03-23 01:06:05 +0000370{
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300371 TaskState *ts = (TaskState *)thread_env->opaque;
372 int host_sig, core_dumped = 0;
aurel32603e4fd2009-04-15 16:18:38 +0000373 struct sigaction act;
Riku Voipio66393fb2009-12-04 15:16:32 +0200374 host_sig = target_to_host_signal(target_sig);
375 gdb_signalled(thread_env, target_sig);
aurel32603e4fd2009-04-15 16:18:38 +0000376
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300377 /* dump core if supported by target binary format */
Riku Voipio66393fb2009-12-04 15:16:32 +0200378 if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300379 stop_all_tasks();
380 core_dumped =
Riku Voipio66393fb2009-12-04 15:16:32 +0200381 ((*ts->bprm->core_dump)(target_sig, thread_env) == 0);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300382 }
383 if (core_dumped) {
384 /* we already dumped the core of target process, we don't want
385 * a coredump of qemu itself */
386 struct rlimit nodump;
387 getrlimit(RLIMIT_CORE, &nodump);
388 nodump.rlim_cur=0;
389 setrlimit(RLIMIT_CORE, &nodump);
390 (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
Riku Voipio66393fb2009-12-04 15:16:32 +0200391 target_sig, strsignal(host_sig), "core dumped" );
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300392 }
393
aurel32603e4fd2009-04-15 16:18:38 +0000394 /* The proper exit code for dieing from an uncaught signal is
395 * -<signal>. The kernel doesn't allow exit() or _exit() to pass
396 * a negative value. To get the proper exit code we need to
397 * actually die from an uncaught signal. Here the default signal
398 * handler is installed, we send ourself a signal and we wait for
399 * it to arrive. */
400 sigfillset(&act.sa_mask);
401 act.sa_handler = SIG_DFL;
402 sigaction(host_sig, &act, NULL);
403
404 /* For some reason raise(host_sig) doesn't send the signal when
405 * statically linked on x86-64. */
406 kill(getpid(), host_sig);
407
408 /* Make sure the signal isn't masked (just reuse the mask inside
409 of act) */
410 sigdelset(&act.sa_mask, host_sig);
411 sigsuspend(&act.sa_mask);
412
413 /* unreachable */
Blue Swirla6c6f762010-03-13 14:18:50 +0000414 abort();
bellard66fb9762003-03-23 01:06:05 +0000415}
416
bellard9de5e442003-03-23 16:49:39 +0000417/* queue a signal so that it will be send to the virtual CPU as soon
418 as possible */
Anthony Liguoric227f092009-10-01 16:12:16 -0500419int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000420{
pbrook624f7972008-05-31 16:11:38 +0000421 TaskState *ts = env->opaque;
422 struct emulated_sigtable *k;
bellard9de5e442003-03-23 16:49:39 +0000423 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000424 abi_ulong handler;
aurel32ca587a82008-12-18 22:44:13 +0000425 int queue;
bellard66fb9762003-03-23 01:06:05 +0000426
bellard9de5e442003-03-23 16:49:39 +0000427#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000428 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000429 sig);
bellard66fb9762003-03-23 01:06:05 +0000430#endif
pbrook624f7972008-05-31 16:11:38 +0000431 k = &ts->sigtab[sig - 1];
aurel32ca587a82008-12-18 22:44:13 +0000432 queue = gdb_queuesig ();
pbrook624f7972008-05-31 16:11:38 +0000433 handler = sigact_table[sig - 1]._sa_handler;
aurel32ca587a82008-12-18 22:44:13 +0000434 if (!queue && handler == TARGET_SIG_DFL) {
ths60b19692008-11-27 15:47:15 +0000435 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
436 kill(getpid(),SIGSTOP);
437 return 0;
438 } else
bellard66fb9762003-03-23 01:06:05 +0000439 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000440 if (sig != TARGET_SIGCHLD &&
441 sig != TARGET_SIGURG &&
ths60b19692008-11-27 15:47:15 +0000442 sig != TARGET_SIGWINCH &&
443 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +0000444 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000445 } else {
446 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000447 }
aurel32ca587a82008-12-18 22:44:13 +0000448 } else if (!queue && handler == TARGET_SIG_IGN) {
bellard66fb9762003-03-23 01:06:05 +0000449 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000450 return 0;
aurel32ca587a82008-12-18 22:44:13 +0000451 } else if (!queue && handler == TARGET_SIG_ERR) {
bellard66fb9762003-03-23 01:06:05 +0000452 force_sig(sig);
453 } else {
bellard9de5e442003-03-23 16:49:39 +0000454 pq = &k->first;
455 if (sig < TARGET_SIGRTMIN) {
456 /* if non real time signal, we queue exactly one signal */
457 if (!k->pending)
458 q = &k->info;
459 else
460 return 0;
461 } else {
462 if (!k->pending) {
463 /* first signal */
464 q = &k->info;
465 } else {
pbrook624f7972008-05-31 16:11:38 +0000466 q = alloc_sigqueue(env);
bellard9de5e442003-03-23 16:49:39 +0000467 if (!q)
468 return -EAGAIN;
469 while (*pq != NULL)
470 pq = &(*pq)->next;
471 }
472 }
473 *pq = q;
474 q->info = *info;
475 q->next = NULL;
476 k->pending = 1;
477 /* signal that a new signal is pending */
pbrook624f7972008-05-31 16:11:38 +0000478 ts->signal_pending = 1;
bellard9de5e442003-03-23 16:49:39 +0000479 return 1; /* indicates that the signal was queued */
480 }
481}
482
ths5fafdf22007-09-16 21:08:06 +0000483static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000484 void *puc)
485{
486 int sig;
Anthony Liguoric227f092009-10-01 16:12:16 -0500487 target_siginfo_t tinfo;
bellard9de5e442003-03-23 16:49:39 +0000488
489 /* the CPU emulator uses some host signals to detect exceptions,
aurel32eaa449b2009-01-03 13:14:52 +0000490 we forward to it some signals */
aurel32ca587a82008-12-18 22:44:13 +0000491 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
aurel32eaa449b2009-01-03 13:14:52 +0000492 && info->si_code > 0) {
bellardb346ff42003-06-15 20:05:50 +0000493 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000494 return;
495 }
496
497 /* get target signal number */
498 sig = host_to_target_signal(host_signum);
499 if (sig < 1 || sig > TARGET_NSIG)
500 return;
501#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000502 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000503#endif
504 host_to_target_siginfo_noswap(&tinfo, info);
pbrookd5975362008-06-07 20:50:51 +0000505 if (queue_signal(thread_env, sig, &tinfo) == 1) {
bellard9de5e442003-03-23 16:49:39 +0000506 /* interrupt the virtual CPU as soon as possible */
aurel323098dba2009-03-07 21:28:24 +0000507 cpu_exit(thread_env);
bellard66fb9762003-03-23 01:06:05 +0000508 }
bellard31e31b82003-02-18 22:55:36 +0000509}
510
ths0da46a62007-10-20 20:23:07 +0000511/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000512/* compare linux/kernel/signal.c:do_sigaltstack() */
513abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000514{
515 int ret;
516 struct target_sigaltstack oss;
517
518 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000519 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000520 {
521 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
522 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
523 __put_user(sas_ss_flags(sp), &oss.ss_flags);
524 }
525
bellard579a97f2007-11-11 14:26:47 +0000526 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000527 {
bellard579a97f2007-11-11 14:26:47 +0000528 struct target_sigaltstack *uss;
529 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000530
ths0da46a62007-10-20 20:23:07 +0000531 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000532 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000533 || __get_user(ss.ss_sp, &uss->ss_sp)
534 || __get_user(ss.ss_size, &uss->ss_size)
535 || __get_user(ss.ss_flags, &uss->ss_flags))
536 goto out;
bellard579a97f2007-11-11 14:26:47 +0000537 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000538
ths0da46a62007-10-20 20:23:07 +0000539 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000540 if (on_sig_stack(sp))
541 goto out;
542
ths0da46a62007-10-20 20:23:07 +0000543 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000544 if (ss.ss_flags != TARGET_SS_DISABLE
545 && ss.ss_flags != TARGET_SS_ONSTACK
546 && ss.ss_flags != 0)
547 goto out;
548
549 if (ss.ss_flags == TARGET_SS_DISABLE) {
550 ss.ss_size = 0;
551 ss.ss_sp = 0;
552 } else {
ths0da46a62007-10-20 20:23:07 +0000553 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000554 if (ss.ss_size < MINSIGSTKSZ)
555 goto out;
556 }
557
558 target_sigaltstack_used.ss_sp = ss.ss_sp;
559 target_sigaltstack_used.ss_size = ss.ss_size;
560 }
561
bellard579a97f2007-11-11 14:26:47 +0000562 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000563 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000564 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000565 goto out;
thsa04e1342007-09-27 13:57:58 +0000566 }
567
568 ret = 0;
569out:
570 return ret;
571}
572
ths0da46a62007-10-20 20:23:07 +0000573/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000574int do_sigaction(int sig, const struct target_sigaction *act,
575 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000576{
pbrook624f7972008-05-31 16:11:38 +0000577 struct target_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000578 struct sigaction act1;
579 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000580 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000581
ths2a913eb2008-11-27 15:46:25 +0000582 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000583 return -EINVAL;
584 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000585#if defined(DEBUG_SIGNAL)
Blue Swirl0bf9e312009-07-20 17:19:25 +0000586 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
587 sig, act, oact);
bellard66fb9762003-03-23 01:06:05 +0000588#endif
589 if (oact) {
pbrook624f7972008-05-31 16:11:38 +0000590 oact->_sa_handler = tswapl(k->_sa_handler);
591 oact->sa_flags = tswapl(k->sa_flags);
ths388bb212007-05-13 13:58:00 +0000592#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000593 oact->sa_restorer = tswapl(k->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000594#endif
pbrook624f7972008-05-31 16:11:38 +0000595 oact->sa_mask = k->sa_mask;
bellard66fb9762003-03-23 01:06:05 +0000596 }
597 if (act) {
pbrook624f7972008-05-31 16:11:38 +0000598 /* FIXME: This is not threadsafe. */
599 k->_sa_handler = tswapl(act->_sa_handler);
600 k->sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000601#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000602 k->sa_restorer = tswapl(act->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000603#endif
pbrook624f7972008-05-31 16:11:38 +0000604 k->sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000605
606 /* we update the host linux signal state */
607 host_sig = target_to_host_signal(sig);
608 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
609 sigfillset(&act1.sa_mask);
610 act1.sa_flags = SA_SIGINFO;
pbrook624f7972008-05-31 16:11:38 +0000611 if (k->sa_flags & TARGET_SA_RESTART)
bellard773b93e2004-01-04 17:15:59 +0000612 act1.sa_flags |= SA_RESTART;
613 /* NOTE: it is important to update the host kernel signal
614 ignore state to avoid getting unexpected interrupted
615 syscalls */
pbrook624f7972008-05-31 16:11:38 +0000616 if (k->_sa_handler == TARGET_SIG_IGN) {
bellard773b93e2004-01-04 17:15:59 +0000617 act1.sa_sigaction = (void *)SIG_IGN;
pbrook624f7972008-05-31 16:11:38 +0000618 } else if (k->_sa_handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +0000619 if (fatal_signal (sig))
620 act1.sa_sigaction = host_signal_handler;
621 else
622 act1.sa_sigaction = (void *)SIG_DFL;
bellard773b93e2004-01-04 17:15:59 +0000623 } else {
624 act1.sa_sigaction = host_signal_handler;
625 }
ths0da46a62007-10-20 20:23:07 +0000626 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000627 }
bellard66fb9762003-03-23 01:06:05 +0000628 }
ths0da46a62007-10-20 20:23:07 +0000629 return ret;
bellard66fb9762003-03-23 01:06:05 +0000630}
bellard31e31b82003-02-18 22:55:36 +0000631
Anthony Liguoric227f092009-10-01 16:12:16 -0500632static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
633 const target_siginfo_t *info)
bellard43fff232003-07-09 19:31:39 +0000634{
635 tswap_siginfo(tinfo, info);
636 return 0;
637}
638
thsc3b5bc82007-12-02 06:31:25 +0000639static inline int current_exec_domain_sig(int sig)
640{
641 return /* current->exec_domain && current->exec_domain->signal_invmap
642 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
643}
644
bellard459a4012007-11-11 19:45:10 +0000645#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000646
647/* from the Linux kernel */
648
649struct target_fpreg {
650 uint16_t significand[4];
651 uint16_t exponent;
652};
653
654struct target_fpxreg {
655 uint16_t significand[4];
656 uint16_t exponent;
657 uint16_t padding[3];
658};
659
660struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000661 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000662};
663
664struct target_fpstate {
665 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000666 abi_ulong cw;
667 abi_ulong sw;
668 abi_ulong tag;
669 abi_ulong ipoff;
670 abi_ulong cssel;
671 abi_ulong dataoff;
672 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000673 struct target_fpreg _st[8];
674 uint16_t status;
675 uint16_t magic; /* 0xffff = regular FPU data only */
676
677 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000678 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
679 abi_ulong mxcsr;
680 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000681 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
682 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000683 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000684};
685
686#define X86_FXSR_MAGIC 0x0000
687
688struct target_sigcontext {
689 uint16_t gs, __gsh;
690 uint16_t fs, __fsh;
691 uint16_t es, __esh;
692 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000693 abi_ulong edi;
694 abi_ulong esi;
695 abi_ulong ebp;
696 abi_ulong esp;
697 abi_ulong ebx;
698 abi_ulong edx;
699 abi_ulong ecx;
700 abi_ulong eax;
701 abi_ulong trapno;
702 abi_ulong err;
703 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000704 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000705 abi_ulong eflags;
706 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000707 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000708 abi_ulong fpstate; /* pointer */
709 abi_ulong oldmask;
710 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000711};
712
bellard66fb9762003-03-23 01:06:05 +0000713struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000714 abi_ulong tuc_flags;
715 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -0500716 target_stack_t tuc_stack;
bellardb8076a72005-04-07 22:20:31 +0000717 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -0500718 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000719};
720
721struct sigframe
722{
blueswir1992f48a2007-10-14 16:27:31 +0000723 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000724 int sig;
725 struct target_sigcontext sc;
726 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000727 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000728 char retcode[8];
729};
730
731struct rt_sigframe
732{
blueswir1992f48a2007-10-14 16:27:31 +0000733 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000734 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000735 abi_ulong pinfo;
736 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000737 struct target_siginfo info;
738 struct target_ucontext uc;
739 struct target_fpstate fpstate;
740 char retcode[8];
741};
742
743/*
744 * Set up a signal frame.
745 */
746
bellard66fb9762003-03-23 01:06:05 +0000747/* XXX: save x87 state */
748static int
749setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
bellard28be6232007-11-11 22:23:38 +0000750 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000751{
752 int err = 0;
bellard775b58d2007-11-11 16:22:17 +0000753 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000754
bellard579a97f2007-11-11 14:26:47 +0000755 /* already locked in setup_frame() */
bellarda52c7572003-06-21 13:14:12 +0000756 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
757 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
758 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
759 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000760 err |= __put_user(env->regs[R_EDI], &sc->edi);
761 err |= __put_user(env->regs[R_ESI], &sc->esi);
762 err |= __put_user(env->regs[R_EBP], &sc->ebp);
763 err |= __put_user(env->regs[R_ESP], &sc->esp);
764 err |= __put_user(env->regs[R_EBX], &sc->ebx);
765 err |= __put_user(env->regs[R_EDX], &sc->edx);
766 err |= __put_user(env->regs[R_ECX], &sc->ecx);
767 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000768 err |= __put_user(env->exception_index, &sc->trapno);
769 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000770 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000771 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000772 err |= __put_user(env->eflags, &sc->eflags);
773 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000774 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000775
bellard28be6232007-11-11 22:23:38 +0000776 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000777 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000778 magic = 0xffff;
779 err |= __put_user(magic, &fpstate->magic);
bellard28be6232007-11-11 22:23:38 +0000780 err |= __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000781
bellard66fb9762003-03-23 01:06:05 +0000782 /* non-iBCS2 extensions.. */
783 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000784 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000785 return err;
786}
787
788/*
789 * Determine which stack to use..
790 */
791
bellard579a97f2007-11-11 14:26:47 +0000792static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +0000793get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
bellard66fb9762003-03-23 01:06:05 +0000794{
795 unsigned long esp;
796
797 /* Default to using normal stack */
798 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000799 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +0000800 if (ka->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +0000801 if (sas_ss_flags(esp) == 0)
802 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
803 }
bellard66fb9762003-03-23 01:06:05 +0000804
805 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000806 else
bellarda52c7572003-06-21 13:14:12 +0000807 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
pbrook624f7972008-05-31 16:11:38 +0000808 !(ka->sa_flags & TARGET_SA_RESTORER) &&
809 ka->sa_restorer) {
810 esp = (unsigned long) ka->sa_restorer;
bellarda52c7572003-06-21 13:14:12 +0000811 }
bellard579a97f2007-11-11 14:26:47 +0000812 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000813}
814
bellard579a97f2007-11-11 14:26:47 +0000815/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +0000816static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500817 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000818{
bellard579a97f2007-11-11 14:26:47 +0000819 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000820 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000821 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000822
bellard579a97f2007-11-11 14:26:47 +0000823 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000824
bellard579a97f2007-11-11 14:26:47 +0000825 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000826 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000827
thsc3b5bc82007-12-02 06:31:25 +0000828 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000829 &frame->sig);
830 if (err)
831 goto give_sigsegv;
832
bellard28be6232007-11-11 22:23:38 +0000833 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
834 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000835 if (err)
836 goto give_sigsegv;
837
bellard92319442004-06-19 16:58:13 +0000838 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
839 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
840 goto give_sigsegv;
841 }
bellard66fb9762003-03-23 01:06:05 +0000842
843 /* Set up to return from userspace. If provided, use a stub
844 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000845 if (ka->sa_flags & TARGET_SA_RESTORER) {
846 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000847 } else {
bellard775b58d2007-11-11 16:22:17 +0000848 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000849 abi_ulong retcode_addr;
850 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
851 err |= __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000852 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000853 val16 = 0xb858;
854 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000855 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000856 val16 = 0x80cd;
857 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000858 }
859
860 if (err)
861 goto give_sigsegv;
862
863 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000864 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000865 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000866
867 cpu_x86_load_seg(env, R_DS, __USER_DS);
868 cpu_x86_load_seg(env, R_ES, __USER_DS);
869 cpu_x86_load_seg(env, R_SS, __USER_DS);
870 cpu_x86_load_seg(env, R_CS, __USER_CS);
871 env->eflags &= ~TF_MASK;
872
bellard579a97f2007-11-11 14:26:47 +0000873 unlock_user_struct(frame, frame_addr, 1);
874
bellard66fb9762003-03-23 01:06:05 +0000875 return;
876
877give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000878 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000879 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000880 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000881 force_sig(TARGET_SIGSEGV /* , current */);
882}
883
bellard579a97f2007-11-11 14:26:47 +0000884/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +0000885static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500886 target_siginfo_t *info,
887 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000888{
bellard28be6232007-11-11 22:23:38 +0000889 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000890 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000891 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000892
bellard579a97f2007-11-11 14:26:47 +0000893 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000894
bellard579a97f2007-11-11 14:26:47 +0000895 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000896 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000897
thsc3b5bc82007-12-02 06:31:25 +0000898 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000899 &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000900 addr = frame_addr + offsetof(struct rt_sigframe, info);
901 err |= __put_user(addr, &frame->pinfo);
902 addr = frame_addr + offsetof(struct rt_sigframe, uc);
903 err |= __put_user(addr, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000904 err |= copy_siginfo_to_user(&frame->info, info);
905 if (err)
906 goto give_sigsegv;
907
908 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000909 err |= __put_user(0, &frame->uc.tuc_flags);
910 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000911 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000912 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000913 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000914 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000915 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000916 &frame->uc.tuc_stack.ss_size);
917 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000918 env, set->sig[0],
919 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +0000920 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000921 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000922 goto give_sigsegv;
923 }
bellard66fb9762003-03-23 01:06:05 +0000924
925 /* Set up to return from userspace. If provided, use a stub
926 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000927 if (ka->sa_flags & TARGET_SA_RESTORER) {
928 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000929 } else {
bellard775b58d2007-11-11 16:22:17 +0000930 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000931 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
932 err |= __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000933 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000934 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000935 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000936 val16 = 0x80cd;
937 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000938 }
939
940 if (err)
941 goto give_sigsegv;
942
943 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000944 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000945 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000946
947 cpu_x86_load_seg(env, R_DS, __USER_DS);
948 cpu_x86_load_seg(env, R_ES, __USER_DS);
949 cpu_x86_load_seg(env, R_SS, __USER_DS);
950 cpu_x86_load_seg(env, R_CS, __USER_CS);
951 env->eflags &= ~TF_MASK;
952
bellard579a97f2007-11-11 14:26:47 +0000953 unlock_user_struct(frame, frame_addr, 1);
954
bellard66fb9762003-03-23 01:06:05 +0000955 return;
956
957give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000958 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000959 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000960 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000961 force_sig(TARGET_SIGSEGV /* , current */);
962}
963
964static int
965restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
966{
967 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +0000968 abi_ulong fpstate_addr;
969 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +0000970
bellard28be6232007-11-11 22:23:38 +0000971 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
972 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
973 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
974 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +0000975
bellard28be6232007-11-11 22:23:38 +0000976 env->regs[R_EDI] = tswapl(sc->edi);
977 env->regs[R_ESI] = tswapl(sc->esi);
978 env->regs[R_EBP] = tswapl(sc->ebp);
979 env->regs[R_ESP] = tswapl(sc->esp);
980 env->regs[R_EBX] = tswapl(sc->ebx);
981 env->regs[R_EDX] = tswapl(sc->edx);
982 env->regs[R_ECX] = tswapl(sc->ecx);
983 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +0000984
985 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
986 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000987
bellard28be6232007-11-11 22:23:38 +0000988 tmpflags = tswapl(sc->eflags);
989 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
990 // regs->orig_eax = -1; /* disable syscall checks */
991
992 fpstate_addr = tswapl(sc->fpstate);
993 if (fpstate_addr != 0) {
994 if (!access_ok(VERIFY_READ, fpstate_addr,
995 sizeof(struct target_fpstate)))
996 goto badframe;
997 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000998 }
999
bellard28be6232007-11-11 22:23:38 +00001000 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +00001001 return err;
bellard66fb9762003-03-23 01:06:05 +00001002badframe:
1003 return 1;
bellard66fb9762003-03-23 01:06:05 +00001004}
1005
1006long do_sigreturn(CPUX86State *env)
1007{
bellard579a97f2007-11-11 14:26:47 +00001008 struct sigframe *frame;
1009 abi_ulong frame_addr = env->regs[R_ESP] - 8;
Anthony Liguoric227f092009-10-01 16:12:16 -05001010 target_sigset_t target_set;
bellard66fb9762003-03-23 01:06:05 +00001011 sigset_t set;
1012 int eax, i;
1013
bellard447db212003-05-10 15:10:36 +00001014#if defined(DEBUG_SIGNAL)
1015 fprintf(stderr, "do_sigreturn\n");
1016#endif
bellard579a97f2007-11-11 14:26:47 +00001017 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1018 goto badframe;
bellard66fb9762003-03-23 01:06:05 +00001019 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +00001020 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1021 goto badframe;
1022 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1023 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1024 goto badframe;
1025 }
bellard66fb9762003-03-23 01:06:05 +00001026
bellard92319442004-06-19 16:58:13 +00001027 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +00001028 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +00001029
bellard66fb9762003-03-23 01:06:05 +00001030 /* restore registers */
1031 if (restore_sigcontext(env, &frame->sc, &eax))
1032 goto badframe;
bellard579a97f2007-11-11 14:26:47 +00001033 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001034 return eax;
1035
1036badframe:
bellard579a97f2007-11-11 14:26:47 +00001037 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001038 force_sig(TARGET_SIGSEGV);
1039 return 0;
1040}
1041
1042long do_rt_sigreturn(CPUX86State *env)
1043{
bellard28be6232007-11-11 22:23:38 +00001044 abi_ulong frame_addr;
1045 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +00001046 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +00001047 int eax;
1048
bellard28be6232007-11-11 22:23:38 +00001049 frame_addr = env->regs[R_ESP] - 4;
1050 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1051 goto badframe;
bellardb8076a72005-04-07 22:20:31 +00001052 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +00001053 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +00001054
bellardb8076a72005-04-07 22:20:31 +00001055 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +00001056 goto badframe;
1057
bellard28be6232007-11-11 22:23:38 +00001058 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1059 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +00001060 goto badframe;
thsa04e1342007-09-27 13:57:58 +00001061
bellard28be6232007-11-11 22:23:38 +00001062 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001063 return eax;
1064
1065badframe:
bellard28be6232007-11-11 22:23:38 +00001066 unlock_user_struct(frame, frame_addr, 0);
1067 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +00001068 return 0;
1069}
1070
bellard43fff232003-07-09 19:31:39 +00001071#elif defined(TARGET_ARM)
1072
1073struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001074 abi_ulong trap_no;
1075 abi_ulong error_code;
1076 abi_ulong oldmask;
1077 abi_ulong arm_r0;
1078 abi_ulong arm_r1;
1079 abi_ulong arm_r2;
1080 abi_ulong arm_r3;
1081 abi_ulong arm_r4;
1082 abi_ulong arm_r5;
1083 abi_ulong arm_r6;
1084 abi_ulong arm_r7;
1085 abi_ulong arm_r8;
1086 abi_ulong arm_r9;
1087 abi_ulong arm_r10;
1088 abi_ulong arm_fp;
1089 abi_ulong arm_ip;
1090 abi_ulong arm_sp;
1091 abi_ulong arm_lr;
1092 abi_ulong arm_pc;
1093 abi_ulong arm_cpsr;
1094 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001095};
1096
pbrooka745ec62008-05-06 15:36:17 +00001097struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001098 abi_ulong tuc_flags;
1099 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001100 target_stack_t tuc_stack;
bellardb8076a72005-04-07 22:20:31 +00001101 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001102 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001103};
1104
pbrooka745ec62008-05-06 15:36:17 +00001105struct target_ucontext_v2 {
1106 abi_ulong tuc_flags;
1107 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001108 target_stack_t tuc_stack;
pbrooka745ec62008-05-06 15:36:17 +00001109 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001110 target_sigset_t tuc_sigmask; /* mask last for extensibility */
pbrooka745ec62008-05-06 15:36:17 +00001111 char __unused[128 - sizeof(sigset_t)];
1112 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1113};
1114
pbrooka8c33202008-05-07 23:22:46 +00001115struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001116{
1117 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001118 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1119 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001120};
1121
pbrooka8c33202008-05-07 23:22:46 +00001122struct sigframe_v2
1123{
1124 struct target_ucontext_v2 uc;
1125 abi_ulong retcode;
1126};
1127
pbrooka745ec62008-05-06 15:36:17 +00001128struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001129{
bellardf8b0aa22007-11-11 23:03:42 +00001130 abi_ulong pinfo;
1131 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001132 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001133 struct target_ucontext_v1 uc;
1134 abi_ulong retcode;
1135};
1136
1137struct rt_sigframe_v2
1138{
1139 struct target_siginfo info;
1140 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001141 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001142};
1143
1144#define TARGET_CONFIG_CPU_32 1
1145
1146/*
1147 * For ARM syscalls, we encode the syscall number into the instruction.
1148 */
1149#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1150#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1151
1152/*
1153 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1154 * need two 16-bit instructions.
1155 */
1156#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1157#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1158
blueswir1992f48a2007-10-14 16:27:31 +00001159static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001160 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1161 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1162};
1163
1164
bellard43fff232003-07-09 19:31:39 +00001165#define __get_user_error(x,p,e) __get_user(x, p)
1166
1167static inline int valid_user_regs(CPUState *regs)
1168{
1169 return 1;
1170}
1171
pbrooka8c33202008-05-07 23:22:46 +00001172static void
bellard43fff232003-07-09 19:31:39 +00001173setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
bellardf8b0aa22007-11-11 23:03:42 +00001174 CPUState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001175{
pbrooka8c33202008-05-07 23:22:46 +00001176 __put_user(env->regs[0], &sc->arm_r0);
1177 __put_user(env->regs[1], &sc->arm_r1);
1178 __put_user(env->regs[2], &sc->arm_r2);
1179 __put_user(env->regs[3], &sc->arm_r3);
1180 __put_user(env->regs[4], &sc->arm_r4);
1181 __put_user(env->regs[5], &sc->arm_r5);
1182 __put_user(env->regs[6], &sc->arm_r6);
1183 __put_user(env->regs[7], &sc->arm_r7);
1184 __put_user(env->regs[8], &sc->arm_r8);
1185 __put_user(env->regs[9], &sc->arm_r9);
1186 __put_user(env->regs[10], &sc->arm_r10);
1187 __put_user(env->regs[11], &sc->arm_fp);
1188 __put_user(env->regs[12], &sc->arm_ip);
1189 __put_user(env->regs[13], &sc->arm_sp);
1190 __put_user(env->regs[14], &sc->arm_lr);
1191 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001192#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001193 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001194#endif
1195
pbrooka8c33202008-05-07 23:22:46 +00001196 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1197 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1198 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1199 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001200}
1201
bellard579a97f2007-11-11 14:26:47 +00001202static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00001203get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
bellard43fff232003-07-09 19:31:39 +00001204{
1205 unsigned long sp = regs->regs[13];
1206
bellard43fff232003-07-09 19:31:39 +00001207 /*
1208 * This is the X/Open sanctioned signal stack switching.
1209 */
pbrook624f7972008-05-31 16:11:38 +00001210 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
thsa04e1342007-09-27 13:57:58 +00001211 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001212 /*
1213 * ATPCS B01 mandates 8-byte alignment
1214 */
bellard579a97f2007-11-11 14:26:47 +00001215 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001216}
1217
1218static int
pbrook624f7972008-05-31 16:11:38 +00001219setup_return(CPUState *env, struct target_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001220 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001221{
pbrook624f7972008-05-31 16:11:38 +00001222 abi_ulong handler = ka->_sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001223 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001224 int thumb = handler & 1;
bellard43fff232003-07-09 19:31:39 +00001225
pbrook624f7972008-05-31 16:11:38 +00001226 if (ka->sa_flags & TARGET_SA_RESTORER) {
1227 retcode = ka->sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001228 } else {
1229 unsigned int idx = thumb;
1230
pbrook624f7972008-05-31 16:11:38 +00001231 if (ka->sa_flags & TARGET_SA_SIGINFO)
bellard43fff232003-07-09 19:31:39 +00001232 idx += 2;
1233
1234 if (__put_user(retcodes[idx], rc))
1235 return 1;
1236#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001237 flush_icache_range((abi_ulong)rc,
1238 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001239#endif
bellardf8b0aa22007-11-11 23:03:42 +00001240 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001241 }
1242
1243 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001244 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001245 env->regs[14] = retcode;
1246 env->regs[15] = handler & (thumb ? ~1 : ~3);
pbrook75b680e2008-03-21 16:07:30 +00001247 env->thumb = thumb;
bellard43fff232003-07-09 19:31:39 +00001248
bellardb5ff1b32005-11-26 10:38:39 +00001249#if 0
bellard43fff232003-07-09 19:31:39 +00001250#ifdef TARGET_CONFIG_CPU_32
1251 env->cpsr = cpsr;
1252#endif
bellardb5ff1b32005-11-26 10:38:39 +00001253#endif
bellard43fff232003-07-09 19:31:39 +00001254
1255 return 0;
1256}
1257
pbrooka8c33202008-05-07 23:22:46 +00001258static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
Anthony Liguoric227f092009-10-01 16:12:16 -05001259 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001260{
pbrooka8c33202008-05-07 23:22:46 +00001261 struct target_sigaltstack stack;
1262 int i;
1263
1264 /* Clear all the bits of the ucontext we don't use. */
1265 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1266
1267 memset(&stack, 0, sizeof(stack));
1268 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1269 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1270 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1271 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1272
1273 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1274 /* FIXME: Save coprocessor signal frame. */
1275 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1276 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1277 }
1278}
1279
1280/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00001281static void setup_frame_v1(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001282 target_sigset_t *set, CPUState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001283{
1284 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001285 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001286 int i;
bellard43fff232003-07-09 19:31:39 +00001287
bellard579a97f2007-11-11 14:26:47 +00001288 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1289 return;
1290
pbrooka8c33202008-05-07 23:22:46 +00001291 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001292
bellard92319442004-06-19 16:58:13 +00001293 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1294 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001295 goto end;
bellard43fff232003-07-09 19:31:39 +00001296 }
1297
pbrooka8c33202008-05-07 23:22:46 +00001298 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1299 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001300
1301end:
1302 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001303}
1304
pbrook624f7972008-05-31 16:11:38 +00001305static void setup_frame_v2(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001306 target_sigset_t *set, CPUState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001307{
1308 struct sigframe_v2 *frame;
1309 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1310
1311 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1312 return;
1313
1314 setup_sigframe_v2(&frame->uc, set, regs);
1315
1316 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1317 frame_addr + offsetof(struct sigframe_v2, retcode));
1318
1319 unlock_user_struct(frame, frame_addr, 1);
1320}
1321
pbrook624f7972008-05-31 16:11:38 +00001322static void setup_frame(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001323 target_sigset_t *set, CPUState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001324{
1325 if (get_osversion() >= 0x020612) {
1326 setup_frame_v2(usig, ka, set, regs);
1327 } else {
1328 setup_frame_v1(usig, ka, set, regs);
1329 }
bellard43fff232003-07-09 19:31:39 +00001330}
1331
bellard579a97f2007-11-11 14:26:47 +00001332/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +00001333static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001334 target_siginfo_t *info,
1335 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001336{
pbrooka745ec62008-05-06 15:36:17 +00001337 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001338 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001339 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001340 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001341 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001342
bellard579a97f2007-11-11 14:26:47 +00001343 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001344 return /* 1 */;
1345
pbrooka745ec62008-05-06 15:36:17 +00001346 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001347 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001348 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001349 __put_user(uc_addr, &frame->puc);
1350 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001351
1352 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001353 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001354
thsa04e1342007-09-27 13:57:58 +00001355 memset(&stack, 0, sizeof(stack));
1356 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1357 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1358 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001359 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001360
pbrooka8c33202008-05-07 23:22:46 +00001361 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001362 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001363 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001364 goto end;
bellard92319442004-06-19 16:58:13 +00001365 }
bellard43fff232003-07-09 19:31:39 +00001366
pbrooka8c33202008-05-07 23:22:46 +00001367 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1368 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001369
pbrooka8c33202008-05-07 23:22:46 +00001370 env->regs[1] = info_addr;
1371 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001372
1373end:
1374 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001375}
1376
pbrook624f7972008-05-31 16:11:38 +00001377static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001378 target_siginfo_t *info,
1379 target_sigset_t *set, CPUState *env)
pbrooka745ec62008-05-06 15:36:17 +00001380{
1381 struct rt_sigframe_v2 *frame;
1382 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001383 abi_ulong info_addr, uc_addr;
1384
1385 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1386 return /* 1 */;
1387
1388 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1389 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001390 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001391
pbrooka8c33202008-05-07 23:22:46 +00001392 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001393
pbrooka8c33202008-05-07 23:22:46 +00001394 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1395 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001396
pbrooka8c33202008-05-07 23:22:46 +00001397 env->regs[1] = info_addr;
1398 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001399
bellard579a97f2007-11-11 14:26:47 +00001400 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001401}
1402
pbrook624f7972008-05-31 16:11:38 +00001403static void setup_rt_frame(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001404 target_siginfo_t *info,
1405 target_sigset_t *set, CPUState *env)
pbrooka745ec62008-05-06 15:36:17 +00001406{
1407 if (get_osversion() >= 0x020612) {
1408 setup_rt_frame_v2(usig, ka, info, set, env);
1409 } else {
1410 setup_rt_frame_v1(usig, ka, info, set, env);
1411 }
1412}
1413
bellard43fff232003-07-09 19:31:39 +00001414static int
1415restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1416{
1417 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001418 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001419
1420 __get_user_error(env->regs[0], &sc->arm_r0, err);
1421 __get_user_error(env->regs[1], &sc->arm_r1, err);
1422 __get_user_error(env->regs[2], &sc->arm_r2, err);
1423 __get_user_error(env->regs[3], &sc->arm_r3, err);
1424 __get_user_error(env->regs[4], &sc->arm_r4, err);
1425 __get_user_error(env->regs[5], &sc->arm_r5, err);
1426 __get_user_error(env->regs[6], &sc->arm_r6, err);
1427 __get_user_error(env->regs[7], &sc->arm_r7, err);
1428 __get_user_error(env->regs[8], &sc->arm_r8, err);
1429 __get_user_error(env->regs[9], &sc->arm_r9, err);
1430 __get_user_error(env->regs[10], &sc->arm_r10, err);
1431 __get_user_error(env->regs[11], &sc->arm_fp, err);
1432 __get_user_error(env->regs[12], &sc->arm_ip, err);
1433 __get_user_error(env->regs[13], &sc->arm_sp, err);
1434 __get_user_error(env->regs[14], &sc->arm_lr, err);
1435 __get_user_error(env->regs[15], &sc->arm_pc, err);
1436#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001437 __get_user_error(cpsr, &sc->arm_cpsr, err);
pbrook75b680e2008-03-21 16:07:30 +00001438 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001439#endif
1440
1441 err |= !valid_user_regs(env);
1442
1443 return err;
1444}
1445
aurel32dc7eea62009-01-30 20:15:32 +00001446static long do_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001447{
bellardf8b0aa22007-11-11 23:03:42 +00001448 abi_ulong frame_addr;
pbrooka8c33202008-05-07 23:22:46 +00001449 struct sigframe_v1 *frame;
Anthony Liguoric227f092009-10-01 16:12:16 -05001450 target_sigset_t set;
bellard43fff232003-07-09 19:31:39 +00001451 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001452 int i;
bellard43fff232003-07-09 19:31:39 +00001453
1454 /*
1455 * Since we stacked the signal on a 64-bit boundary,
1456 * then 'sp' should be word aligned here. If it's
1457 * not, then the user is trying to mess with us.
1458 */
1459 if (env->regs[13] & 7)
1460 goto badframe;
1461
bellardf8b0aa22007-11-11 23:03:42 +00001462 frame_addr = env->regs[13];
1463 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1464 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001465
bellard92319442004-06-19 16:58:13 +00001466 if (__get_user(set.sig[0], &frame->sc.oldmask))
1467 goto badframe;
1468 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1469 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1470 goto badframe;
1471 }
bellard43fff232003-07-09 19:31:39 +00001472
bellard92319442004-06-19 16:58:13 +00001473 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001474 sigprocmask(SIG_SETMASK, &host_set, NULL);
1475
1476 if (restore_sigcontext(env, &frame->sc))
1477 goto badframe;
1478
1479#if 0
1480 /* Send SIGTRAP if we're single-stepping */
1481 if (ptrace_cancel_bpt(current))
1482 send_sig(SIGTRAP, current, 1);
1483#endif
bellardf8b0aa22007-11-11 23:03:42 +00001484 unlock_user_struct(frame, frame_addr, 0);
1485 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001486
1487badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001488 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001489 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00001490 return 0;
1491}
1492
pbrooka8c33202008-05-07 23:22:46 +00001493static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1494 struct target_ucontext_v2 *uc)
1495{
1496 sigset_t host_set;
1497
1498 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1499 sigprocmask(SIG_SETMASK, &host_set, NULL);
1500
1501 if (restore_sigcontext(env, &uc->tuc_mcontext))
1502 return 1;
1503
1504 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1505 return 1;
1506
1507#if 0
1508 /* Send SIGTRAP if we're single-stepping */
1509 if (ptrace_cancel_bpt(current))
1510 send_sig(SIGTRAP, current, 1);
1511#endif
1512
1513 return 0;
1514}
1515
aurel32dc7eea62009-01-30 20:15:32 +00001516static long do_sigreturn_v2(CPUState *env)
pbrooka8c33202008-05-07 23:22:46 +00001517{
1518 abi_ulong frame_addr;
1519 struct sigframe_v2 *frame;
1520
1521 /*
1522 * Since we stacked the signal on a 64-bit boundary,
1523 * then 'sp' should be word aligned here. If it's
1524 * not, then the user is trying to mess with us.
1525 */
1526 if (env->regs[13] & 7)
1527 goto badframe;
1528
1529 frame_addr = env->regs[13];
1530 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1531 goto badframe;
1532
1533 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1534 goto badframe;
1535
1536 unlock_user_struct(frame, frame_addr, 0);
1537 return env->regs[0];
1538
1539badframe:
1540 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001541 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka8c33202008-05-07 23:22:46 +00001542 return 0;
1543}
1544
1545long do_sigreturn(CPUState *env)
1546{
1547 if (get_osversion() >= 0x020612) {
1548 return do_sigreturn_v2(env);
1549 } else {
1550 return do_sigreturn_v1(env);
1551 }
1552}
1553
aurel32dc7eea62009-01-30 20:15:32 +00001554static long do_rt_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001555{
bellardf8b0aa22007-11-11 23:03:42 +00001556 abi_ulong frame_addr;
pbrooka745ec62008-05-06 15:36:17 +00001557 struct rt_sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001558 sigset_t host_set;
1559
1560 /*
1561 * Since we stacked the signal on a 64-bit boundary,
1562 * then 'sp' should be word aligned here. If it's
1563 * not, then the user is trying to mess with us.
1564 */
1565 if (env->regs[13] & 7)
1566 goto badframe;
1567
bellardf8b0aa22007-11-11 23:03:42 +00001568 frame_addr = env->regs[13];
1569 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1570 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001571
bellardb8076a72005-04-07 22:20:31 +00001572 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001573 sigprocmask(SIG_SETMASK, &host_set, NULL);
1574
bellardb8076a72005-04-07 22:20:31 +00001575 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001576 goto badframe;
1577
pbrooka745ec62008-05-06 15:36:17 +00001578 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 +00001579 goto badframe;
1580
bellard43fff232003-07-09 19:31:39 +00001581#if 0
1582 /* Send SIGTRAP if we're single-stepping */
1583 if (ptrace_cancel_bpt(current))
1584 send_sig(SIGTRAP, current, 1);
1585#endif
bellardf8b0aa22007-11-11 23:03:42 +00001586 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001587 return env->regs[0];
1588
1589badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001590 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001591 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00001592 return 0;
1593}
1594
aurel32dc7eea62009-01-30 20:15:32 +00001595static long do_rt_sigreturn_v2(CPUState *env)
pbrooka745ec62008-05-06 15:36:17 +00001596{
1597 abi_ulong frame_addr;
1598 struct rt_sigframe_v2 *frame;
pbrooka745ec62008-05-06 15:36:17 +00001599
1600 /*
1601 * Since we stacked the signal on a 64-bit boundary,
1602 * then 'sp' should be word aligned here. If it's
1603 * not, then the user is trying to mess with us.
1604 */
1605 if (env->regs[13] & 7)
1606 goto badframe;
1607
1608 frame_addr = env->regs[13];
1609 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1610 goto badframe;
1611
pbrooka8c33202008-05-07 23:22:46 +00001612 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1613 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00001614
pbrooka745ec62008-05-06 15:36:17 +00001615 unlock_user_struct(frame, frame_addr, 0);
1616 return env->regs[0];
1617
1618badframe:
1619 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001620 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka745ec62008-05-06 15:36:17 +00001621 return 0;
1622}
1623
1624long do_rt_sigreturn(CPUState *env)
1625{
1626 if (get_osversion() >= 0x020612) {
1627 return do_rt_sigreturn_v2(env);
1628 } else {
1629 return do_rt_sigreturn_v1(env);
1630 }
1631}
1632
bellard6d5e2162004-09-30 22:04:13 +00001633#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001634
bellard6d5e2162004-09-30 22:04:13 +00001635#define __SUNOS_MAXWIN 31
1636
1637/* This is what SunOS does, so shall I. */
1638struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001639 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001640
blueswir1992f48a2007-10-14 16:27:31 +00001641 abi_ulong sigc_mask; /* sigmask to restore */
1642 abi_ulong sigc_sp; /* stack pointer */
1643 abi_ulong sigc_pc; /* program counter */
1644 abi_ulong sigc_npc; /* next program counter */
1645 abi_ulong sigc_psr; /* for condition codes etc */
1646 abi_ulong sigc_g1; /* User uses these two registers */
1647 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001648
1649 /* Now comes information regarding the users window set
1650 * at the time of the signal.
1651 */
blueswir1992f48a2007-10-14 16:27:31 +00001652 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001653
1654 /* stack ptrs for each regwin buf */
1655 char *sigc_spbuf[__SUNOS_MAXWIN];
1656
1657 /* Windows to restore after signal */
1658 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001659 abi_ulong locals[8];
1660 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001661 } sigc_wbuf[__SUNOS_MAXWIN];
1662};
1663/* A Sparc stack frame */
1664struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001665 abi_ulong locals[8];
1666 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001667 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001668 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001669 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001670 abi_ulong xargs[6];
1671 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001672};
1673
1674typedef struct {
1675 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001676 abi_ulong psr;
1677 abi_ulong pc;
1678 abi_ulong npc;
1679 abi_ulong y;
1680 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001681 } si_regs;
1682 int si_mask;
1683} __siginfo_t;
1684
1685typedef struct {
1686 unsigned long si_float_regs [32];
1687 unsigned long si_fsr;
1688 unsigned long si_fpqdepth;
1689 struct {
1690 unsigned long *insn_addr;
1691 unsigned long insn;
1692 } si_fpqueue [16];
Anthony Liguoric227f092009-10-01 16:12:16 -05001693} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001694
1695
1696struct target_signal_frame {
1697 struct sparc_stackf ss;
1698 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00001699 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001700 abi_ulong insns[2] __attribute__ ((aligned (8)));
1701 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1702 abi_ulong extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05001703 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001704};
1705struct target_rt_signal_frame {
1706 struct sparc_stackf ss;
1707 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001708 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001709 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00001710 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001711 unsigned int insns[2];
1712 stack_t stack;
1713 unsigned int extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05001714 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001715};
1716
bellarde80cfcf2004-12-19 23:18:01 +00001717#define UREG_O0 16
1718#define UREG_O6 22
1719#define UREG_I0 0
1720#define UREG_I1 1
1721#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001722#define UREG_I3 3
1723#define UREG_I4 4
1724#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001725#define UREG_I6 6
1726#define UREG_I7 7
1727#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001728#define UREG_FP UREG_I6
1729#define UREG_SP UREG_O6
1730
pbrook624f7972008-05-31 16:11:38 +00001731static inline abi_ulong get_sigframe(struct target_sigaction *sa,
bellard459a4012007-11-11 19:45:10 +00001732 CPUState *env, unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001733{
bellard459a4012007-11-11 19:45:10 +00001734 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001735
1736 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001737
1738 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00001739 if (sa->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +00001740 if (!on_sig_stack(sp)
1741 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1742 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001743 }
bellard459a4012007-11-11 19:45:10 +00001744 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001745}
1746
1747static int
blueswir1992f48a2007-10-14 16:27:31 +00001748setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001749{
1750 int err = 0, i;
1751
bellard6d5e2162004-09-30 22:04:13 +00001752 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001753 err |= __put_user(env->pc, &si->si_regs.pc);
1754 err |= __put_user(env->npc, &si->si_regs.npc);
1755 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001756 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001757 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1758 }
bellarda315a142005-01-30 22:59:18 +00001759 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001760 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001761 }
bellard6d5e2162004-09-30 22:04:13 +00001762 err |= __put_user(mask, &si->si_mask);
1763 return err;
1764}
bellarde80cfcf2004-12-19 23:18:01 +00001765
bellard80a9d032005-01-03 23:31:27 +00001766#if 0
bellard6d5e2162004-09-30 22:04:13 +00001767static int
1768setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1769 CPUState *env, unsigned long mask)
1770{
1771 int err = 0;
1772
1773 err |= __put_user(mask, &sc->sigc_mask);
1774 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1775 err |= __put_user(env->pc, &sc->sigc_pc);
1776 err |= __put_user(env->npc, &sc->sigc_npc);
1777 err |= __put_user(env->psr, &sc->sigc_psr);
1778 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1779 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1780
1781 return err;
1782}
bellard80a9d032005-01-03 23:31:27 +00001783#endif
bellard6d5e2162004-09-30 22:04:13 +00001784#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1785
pbrook624f7972008-05-31 16:11:38 +00001786static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001787 target_sigset_t *set, CPUState *env)
bellard6d5e2162004-09-30 22:04:13 +00001788{
bellard459a4012007-11-11 19:45:10 +00001789 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001790 struct target_signal_frame *sf;
1791 int sigframe_size, err, i;
1792
1793 /* 1. Make sure everything is clean */
1794 //synchronize_user_stack();
1795
1796 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001797 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001798
bellard459a4012007-11-11 19:45:10 +00001799 sf = lock_user(VERIFY_WRITE, sf_addr,
1800 sizeof(struct target_signal_frame), 0);
1801 if (!sf)
1802 goto sigsegv;
1803
bellarde80cfcf2004-12-19 23:18:01 +00001804 //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 +00001805#if 0
1806 if (invalid_frame_pointer(sf, sigframe_size))
1807 goto sigill_and_return;
1808#endif
1809 /* 2. Save the current process state */
1810 err = setup___siginfo(&sf->info, env, set->sig[0]);
1811 err |= __put_user(0, &sf->extra_size);
1812
1813 //err |= save_fpu_state(regs, &sf->fpu_state);
1814 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1815
1816 err |= __put_user(set->sig[0], &sf->info.si_mask);
1817 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1818 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1819 }
1820
bellarda315a142005-01-30 22:59:18 +00001821 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001822 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001823 }
bellarda315a142005-01-30 22:59:18 +00001824 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001825 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001826 }
bellard6d5e2162004-09-30 22:04:13 +00001827 if (err)
1828 goto sigsegv;
1829
1830 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001831 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001832 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001833 env->regwptr[UREG_I1] = sf_addr +
1834 offsetof(struct target_signal_frame, info);
1835 env->regwptr[UREG_I2] = sf_addr +
1836 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001837
1838 /* 4. signal handler */
pbrook624f7972008-05-31 16:11:38 +00001839 env->pc = ka->_sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00001840 env->npc = (env->pc + 4);
1841 /* 5. return to kernel instructions */
pbrook624f7972008-05-31 16:11:38 +00001842 if (ka->sa_restorer)
1843 env->regwptr[UREG_I7] = ka->sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00001844 else {
bellard775b58d2007-11-11 16:22:17 +00001845 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00001846
1847 env->regwptr[UREG_I7] = sf_addr +
1848 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00001849
1850 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001851 val32 = 0x821020d8;
1852 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001853
1854 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001855 val32 = 0x91d02010;
1856 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001857 if (err)
1858 goto sigsegv;
1859
1860 /* Flush instruction space. */
1861 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001862 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001863 }
bellard459a4012007-11-11 19:45:10 +00001864 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001865 return;
bellard459a4012007-11-11 19:45:10 +00001866#if 0
1867sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001868 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00001869#endif
bellard6d5e2162004-09-30 22:04:13 +00001870sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001871 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00001872 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001873 force_sig(TARGET_SIGSEGV);
1874}
1875static inline int
Anthony Liguoric227f092009-10-01 16:12:16 -05001876restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001877{
1878 int err;
1879#if 0
1880#ifdef CONFIG_SMP
1881 if (current->flags & PF_USEDFPU)
1882 regs->psr &= ~PSR_EF;
1883#else
1884 if (current == last_task_used_math) {
1885 last_task_used_math = 0;
1886 regs->psr &= ~PSR_EF;
1887 }
1888#endif
1889 current->used_math = 1;
1890 current->flags &= ~PF_USEDFPU;
1891#endif
1892#if 0
1893 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1894 return -EFAULT;
1895#endif
1896
bellardfafffae2006-10-28 12:09:16 +00001897#if 0
1898 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001899 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1900 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001901#endif
bellard6d5e2162004-09-30 22:04:13 +00001902 err |= __get_user(env->fsr, &fpu->si_fsr);
1903#if 0
1904 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1905 if (current->thread.fpqdepth != 0)
1906 err |= __copy_from_user(&current->thread.fpqueue[0],
1907 &fpu->si_fpqueue[0],
1908 ((sizeof(unsigned long) +
1909 (sizeof(unsigned long *)))*16));
1910#endif
1911 return err;
1912}
1913
1914
pbrook624f7972008-05-31 16:11:38 +00001915static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001916 target_siginfo_t *info,
1917 target_sigset_t *set, CPUState *env)
bellard6d5e2162004-09-30 22:04:13 +00001918{
1919 fprintf(stderr, "setup_rt_frame: not implemented\n");
1920}
1921
1922long do_sigreturn(CPUState *env)
1923{
bellardf8b0aa22007-11-11 23:03:42 +00001924 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001925 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001926 uint32_t up_psr, pc, npc;
Anthony Liguoric227f092009-10-01 16:12:16 -05001927 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001928 sigset_t host_set;
bellardf8b0aa22007-11-11 23:03:42 +00001929 abi_ulong fpu_save_addr;
bellarde80cfcf2004-12-19 23:18:01 +00001930 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001931
bellardf8b0aa22007-11-11 23:03:42 +00001932 sf_addr = env->regwptr[UREG_FP];
1933 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1934 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00001935#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001936 fprintf(stderr, "sigreturn\n");
1937 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 +00001938#endif
bellarde80cfcf2004-12-19 23:18:01 +00001939 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001940
1941 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00001942
bellardf8b0aa22007-11-11 23:03:42 +00001943 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00001944 goto segv_and_exit;
1945
1946 err = __get_user(pc, &sf->info.si_regs.pc);
1947 err |= __get_user(npc, &sf->info.si_regs.npc);
1948
bellard6d5e2162004-09-30 22:04:13 +00001949 if ((pc | npc) & 3)
1950 goto segv_and_exit;
1951
1952 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001953 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1954
bellard6d5e2162004-09-30 22:04:13 +00001955 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001956 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1957 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1958
1959 env->pc = pc;
1960 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001961 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001962 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001963 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1964 }
bellarda315a142005-01-30 22:59:18 +00001965 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001966 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1967 }
bellard6d5e2162004-09-30 22:04:13 +00001968
bellardf8b0aa22007-11-11 23:03:42 +00001969 err |= __get_user(fpu_save_addr, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001970
bellarde80cfcf2004-12-19 23:18:01 +00001971 //if (fpu_save)
1972 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001973
1974 /* This is pretty much atomic, no amount locking would prevent
1975 * the races which exist anyways.
1976 */
1977 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001978 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1979 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1980 }
1981
1982 target_to_host_sigset_internal(&host_set, &set);
1983 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001984
1985 if (err)
1986 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00001987 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001988 return env->regwptr[0];
1989
1990segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00001991 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001992 force_sig(TARGET_SIGSEGV);
1993}
1994
1995long do_rt_sigreturn(CPUState *env)
1996{
1997 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00001998 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00001999}
2000
bellard459a4012007-11-11 19:45:10 +00002001#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00002002#define MC_TSTATE 0
2003#define MC_PC 1
2004#define MC_NPC 2
2005#define MC_Y 3
2006#define MC_G1 4
2007#define MC_G2 5
2008#define MC_G3 6
2009#define MC_G4 7
2010#define MC_G5 8
2011#define MC_G6 9
2012#define MC_G7 10
2013#define MC_O0 11
2014#define MC_O1 12
2015#define MC_O2 13
2016#define MC_O3 14
2017#define MC_O4 15
2018#define MC_O5 16
2019#define MC_O6 17
2020#define MC_O7 18
2021#define MC_NGREG 19
2022
Anthony Liguoric227f092009-10-01 16:12:16 -05002023typedef abi_ulong target_mc_greg_t;
2024typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
blueswir15bfb56b2007-10-05 17:01:51 +00002025
2026struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00002027 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002028 uint32_t mcfq_insn;
2029};
2030
2031struct target_mc_fpu {
2032 union {
2033 uint32_t sregs[32];
2034 uint64_t dregs[32];
2035 //uint128_t qregs[16];
2036 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00002037 abi_ulong mcfpu_fsr;
2038 abi_ulong mcfpu_fprs;
2039 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00002040 struct target_mc_fq *mcfpu_fq;
2041 unsigned char mcfpu_qcnt;
2042 unsigned char mcfpu_qentsz;
2043 unsigned char mcfpu_enab;
2044};
Anthony Liguoric227f092009-10-01 16:12:16 -05002045typedef struct target_mc_fpu target_mc_fpu_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002046
2047typedef struct {
Anthony Liguoric227f092009-10-01 16:12:16 -05002048 target_mc_gregset_t mc_gregs;
2049 target_mc_greg_t mc_fp;
2050 target_mc_greg_t mc_i7;
2051 target_mc_fpu_t mc_fpregs;
2052} target_mcontext_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002053
2054struct target_ucontext {
2055 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00002056 abi_ulong uc_flags;
Anthony Liguoric227f092009-10-01 16:12:16 -05002057 target_sigset_t uc_sigmask;
2058 target_mcontext_t uc_mcontext;
blueswir15bfb56b2007-10-05 17:01:51 +00002059};
2060
2061/* A V9 register window */
2062struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00002063 abi_ulong locals[8];
2064 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00002065};
2066
2067#define TARGET_STACK_BIAS 2047
2068
2069/* {set, get}context() needed for 64-bit SparcLinux userland. */
2070void sparc64_set_context(CPUSPARCState *env)
2071{
bellard459a4012007-11-11 19:45:10 +00002072 abi_ulong ucp_addr;
2073 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002074 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00002075 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00002076 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002077 unsigned char fenab;
2078 int err;
2079 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002080
bellard459a4012007-11-11 19:45:10 +00002081 ucp_addr = env->regwptr[UREG_I0];
2082 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2083 goto do_sigsegv;
blueswir15bfb56b2007-10-05 17:01:51 +00002084 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00002085 err = __get_user(pc, &((*grp)[MC_PC]));
2086 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002087 if (err || ((pc | npc) & 3))
2088 goto do_sigsegv;
2089 if (env->regwptr[UREG_I1]) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002090 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002091 sigset_t set;
2092
2093 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002094 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002095 goto do_sigsegv;
2096 } else {
bellard459a4012007-11-11 19:45:10 +00002097 abi_ulong *src, *dst;
2098 src = ucp->uc_sigmask.sig;
2099 dst = target_set.sig;
Anthony Liguoric227f092009-10-01 16:12:16 -05002100 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002101 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002102 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00002103 if (err)
2104 goto do_sigsegv;
2105 }
2106 target_to_host_sigset_internal(&set, &target_set);
2107 sigprocmask(SIG_SETMASK, &set, NULL);
2108 }
2109 env->pc = pc;
2110 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00002111 err |= __get_user(env->y, &((*grp)[MC_Y]));
2112 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002113 env->asi = (tstate >> 24) & 0xff;
2114 PUT_CCR(env, tstate >> 32);
2115 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00002116 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2117 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2118 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2119 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2120 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2121 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2122 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2123 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2124 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2125 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2126 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2127 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2128 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2129 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2130 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002131
bellard579a97f2007-11-11 14:26:47 +00002132 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2133 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002134
bellard459a4012007-11-11 19:45:10 +00002135 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2136 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2137 abi_ulong) != 0)
2138 goto do_sigsegv;
2139 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2140 abi_ulong) != 0)
2141 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002142 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2143 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002144 {
2145 uint32_t *src, *dst;
2146 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2147 dst = env->fpr;
2148 /* XXX: check that the CPU storage is the same as user context */
2149 for (i = 0; i < 64; i++, dst++, src++)
2150 err |= __get_user(*dst, src);
2151 }
bellard579a97f2007-11-11 14:26:47 +00002152 err |= __get_user(env->fsr,
2153 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2154 err |= __get_user(env->gsr,
2155 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002156 if (err)
2157 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002158 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002159 return;
2160 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002161 unlock_user_struct(ucp, ucp_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002162 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002163}
2164
2165void sparc64_get_context(CPUSPARCState *env)
2166{
bellard459a4012007-11-11 19:45:10 +00002167 abi_ulong ucp_addr;
2168 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002169 target_mc_gregset_t *grp;
2170 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002171 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002172 int err;
2173 unsigned int i;
Anthony Liguoric227f092009-10-01 16:12:16 -05002174 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002175 sigset_t set;
2176
bellard459a4012007-11-11 19:45:10 +00002177 ucp_addr = env->regwptr[UREG_I0];
2178 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2179 goto do_sigsegv;
2180
blueswir15bfb56b2007-10-05 17:01:51 +00002181 mcp = &ucp->uc_mcontext;
2182 grp = &mcp->mc_gregs;
2183
2184 /* Skip over the trap instruction, first. */
2185 env->pc = env->npc;
2186 env->npc += 4;
2187
2188 err = 0;
2189
2190 sigprocmask(0, NULL, &set);
2191 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002192 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002193 err |= __put_user(target_set.sig[0],
2194 (abi_ulong *)&ucp->uc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002195 } else {
2196 abi_ulong *src, *dst;
2197 src = target_set.sig;
2198 dst = ucp->uc_sigmask.sig;
Anthony Liguoric227f092009-10-01 16:12:16 -05002199 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002200 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002201 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00002202 if (err)
2203 goto do_sigsegv;
2204 }
2205
bellard459a4012007-11-11 19:45:10 +00002206 /* XXX: tstate must be saved properly */
2207 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00002208 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2209 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2210 err |= __put_user(env->y, &((*grp)[MC_Y]));
2211 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2212 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2213 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2214 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2215 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2216 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2217 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2218 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2219 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2220 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2221 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2222 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2223 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2224 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2225 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002226
bellard459a4012007-11-11 19:45:10 +00002227 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2228 fp = i7 = 0;
2229 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2230 abi_ulong) != 0)
2231 goto do_sigsegv;
2232 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2233 abi_ulong) != 0)
2234 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002235 err |= __put_user(fp, &(mcp->mc_fp));
2236 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002237
bellard459a4012007-11-11 19:45:10 +00002238 {
2239 uint32_t *src, *dst;
2240 src = env->fpr;
2241 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2242 /* XXX: check that the CPU storage is the same as user context */
2243 for (i = 0; i < 64; i++, dst++, src++)
2244 err |= __put_user(*src, dst);
2245 }
bellard579a97f2007-11-11 14:26:47 +00002246 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2247 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2248 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002249
2250 if (err)
2251 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002252 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002253 return;
2254 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002255 unlock_user_struct(ucp, ucp_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02002256 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002257}
2258#endif
thsd26bc212007-11-08 18:05:37 +00002259#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002260
2261# warning signal handling not implemented
2262
pbrook624f7972008-05-31 16:11:38 +00002263static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002264 target_sigset_t *set, CPUState *env)
ths540635b2007-09-30 01:58:33 +00002265{
2266 fprintf(stderr, "setup_frame: not implemented\n");
2267}
2268
pbrook624f7972008-05-31 16:11:38 +00002269static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002270 target_siginfo_t *info,
2271 target_sigset_t *set, CPUState *env)
ths540635b2007-09-30 01:58:33 +00002272{
2273 fprintf(stderr, "setup_rt_frame: not implemented\n");
2274}
2275
2276long do_sigreturn(CPUState *env)
2277{
2278 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002279 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002280}
2281
2282long do_rt_sigreturn(CPUState *env)
2283{
2284 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002285 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002286}
2287
thsd26bc212007-11-08 18:05:37 +00002288#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002289
2290# warning signal handling not implemented
2291
pbrook624f7972008-05-31 16:11:38 +00002292static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002293 target_sigset_t *set, CPUState *env)
ths540635b2007-09-30 01:58:33 +00002294{
2295 fprintf(stderr, "setup_frame: not implemented\n");
2296}
2297
pbrook624f7972008-05-31 16:11:38 +00002298static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002299 target_siginfo_t *info,
2300 target_sigset_t *set, CPUState *env)
ths540635b2007-09-30 01:58:33 +00002301{
2302 fprintf(stderr, "setup_rt_frame: not implemented\n");
2303}
2304
2305long do_sigreturn(CPUState *env)
2306{
2307 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002308 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002309}
2310
2311long do_rt_sigreturn(CPUState *env)
2312{
2313 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002314 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002315}
2316
thsd26bc212007-11-08 18:05:37 +00002317#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002318
2319struct target_sigcontext {
2320 uint32_t sc_regmask; /* Unused */
2321 uint32_t sc_status;
2322 uint64_t sc_pc;
2323 uint64_t sc_regs[32];
2324 uint64_t sc_fpregs[32];
2325 uint32_t sc_ownedfp; /* Unused */
2326 uint32_t sc_fpc_csr;
2327 uint32_t sc_fpc_eir; /* Unused */
2328 uint32_t sc_used_math;
2329 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
Paul Brook94c54952009-07-09 18:40:15 +01002330 uint32_t pad0;
bellard106ec872006-06-27 21:08:10 +00002331 uint64_t sc_mdhi;
2332 uint64_t sc_mdlo;
2333 target_ulong sc_hi1; /* Was sc_cause */
2334 target_ulong sc_lo1; /* Was sc_badvaddr */
2335 target_ulong sc_hi2; /* Was sc_sigset[4] */
2336 target_ulong sc_lo2;
2337 target_ulong sc_hi3;
2338 target_ulong sc_lo3;
2339};
2340
2341struct sigframe {
2342 uint32_t sf_ass[4]; /* argument save space for o32 */
2343 uint32_t sf_code[2]; /* signal trampoline */
2344 struct target_sigcontext sf_sc;
Anthony Liguoric227f092009-10-01 16:12:16 -05002345 target_sigset_t sf_mask;
bellard106ec872006-06-27 21:08:10 +00002346};
2347
pbrook0b1bcb02009-04-21 01:41:10 +00002348struct target_ucontext {
2349 target_ulong uc_flags;
2350 target_ulong uc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05002351 target_stack_t uc_stack;
Paul Brook94c54952009-07-09 18:40:15 +01002352 target_ulong pad0;
pbrook0b1bcb02009-04-21 01:41:10 +00002353 struct target_sigcontext uc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05002354 target_sigset_t uc_sigmask;
pbrook0b1bcb02009-04-21 01:41:10 +00002355};
2356
2357struct target_rt_sigframe {
2358 uint32_t rs_ass[4]; /* argument save space for o32 */
2359 uint32_t rs_code[2]; /* signal trampoline */
2360 struct target_siginfo rs_info;
2361 struct target_ucontext rs_uc;
2362};
2363
bellard106ec872006-06-27 21:08:10 +00002364/* Install trampoline to jump back from signal handler */
2365static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2366{
2367 int err;
2368
2369 /*
2370 * Set up the return code ...
2371 *
2372 * li v0, __NR__foo_sigreturn
2373 * syscall
2374 */
2375
2376 err = __put_user(0x24020000 + syscall, tramp + 0);
2377 err |= __put_user(0x0000000c , tramp + 1);
2378 /* flush_cache_sigtramp((unsigned long) tramp); */
2379 return err;
2380}
2381
2382static inline int
2383setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2384{
2385 int err = 0;
2386
thsb5dc7732008-06-27 10:02:35 +00002387 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002388
thsb5dc7732008-06-27 10:02:35 +00002389#define save_gp_reg(i) do { \
2390 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002391 } while(0)
2392 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2393 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2394 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2395 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2396 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2397 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2398 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2399 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2400 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002401#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002402
thsb5dc7732008-06-27 10:02:35 +00002403 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2404 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002405
2406 /* Not used yet, but might be useful if we ever have DSP suppport */
2407#if 0
2408 if (cpu_has_dsp) {
2409 err |= __put_user(mfhi1(), &sc->sc_hi1);
2410 err |= __put_user(mflo1(), &sc->sc_lo1);
2411 err |= __put_user(mfhi2(), &sc->sc_hi2);
2412 err |= __put_user(mflo2(), &sc->sc_lo2);
2413 err |= __put_user(mfhi3(), &sc->sc_hi3);
2414 err |= __put_user(mflo3(), &sc->sc_lo3);
2415 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2416 }
2417 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002418#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002419 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2420 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2421 if (cpu_has_dsp) {
2422 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2423 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2424 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2425 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2426 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2427 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2428 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2429 }
ths388bb212007-05-13 13:58:00 +00002430#endif
2431#endif
bellard106ec872006-06-27 21:08:10 +00002432
ths388bb212007-05-13 13:58:00 +00002433#if 0
bellard106ec872006-06-27 21:08:10 +00002434 err |= __put_user(!!used_math(), &sc->sc_used_math);
2435
2436 if (!used_math())
2437 goto out;
2438
2439 /*
2440 * Save FPU state to signal context. Signal handler will "inherit"
2441 * current FPU state.
2442 */
2443 preempt_disable();
2444
2445 if (!is_fpu_owner()) {
2446 own_fpu();
2447 restore_fp(current);
2448 }
2449 err |= save_fp_context(sc);
2450
2451 preempt_enable();
2452 out:
2453#endif
2454 return err;
2455}
2456
2457static inline int
2458restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2459{
2460 int err = 0;
2461
2462 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2463
thsb5dc7732008-06-27 10:02:35 +00002464 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2465 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002466
thsead93602007-09-06 00:18:15 +00002467#define restore_gp_reg(i) do { \
thsb5dc7732008-06-27 10:02:35 +00002468 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002469 } while(0)
2470 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2471 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2472 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2473 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2474 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2475 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2476 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2477 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2478 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2479 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2480 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002481#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002482
2483#if 0
2484 if (cpu_has_dsp) {
2485 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2486 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2487 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2488 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2489 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2490 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2491 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2492 }
ths388bb212007-05-13 13:58:00 +00002493#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002494 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2495 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2496 if (cpu_has_dsp) {
2497 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2498 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2499 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2500 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2501 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2502 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2503 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2504 }
ths388bb212007-05-13 13:58:00 +00002505#endif
bellard106ec872006-06-27 21:08:10 +00002506
2507 err |= __get_user(used_math, &sc->sc_used_math);
2508 conditional_used_math(used_math);
2509
2510 preempt_disable();
2511
2512 if (used_math()) {
2513 /* restore fpu context if we have used it before */
2514 own_fpu();
2515 err |= restore_fp_context(sc);
2516 } else {
2517 /* signal handler may have used FPU. Give it up. */
2518 lose_fpu();
2519 }
2520
2521 preempt_enable();
2522#endif
2523 return err;
2524}
2525/*
2526 * Determine which stack to use..
2527 */
bellard579a97f2007-11-11 14:26:47 +00002528static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00002529get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
bellard106ec872006-06-27 21:08:10 +00002530{
2531 unsigned long sp;
2532
2533 /* Default to using normal stack */
thsb5dc7732008-06-27 10:02:35 +00002534 sp = regs->active_tc.gpr[29];
bellard106ec872006-06-27 21:08:10 +00002535
2536 /*
2537 * FPU emulator may have it's own trampoline active just
2538 * above the user stack, 16-bytes before the next lowest
2539 * 16 byte boundary. Try to avoid trashing it.
2540 */
2541 sp -= 32;
2542
bellard106ec872006-06-27 21:08:10 +00002543 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002544 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
thsa04e1342007-09-27 13:57:58 +00002545 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2546 }
bellard106ec872006-06-27 21:08:10 +00002547
bellard579a97f2007-11-11 14:26:47 +00002548 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002549}
2550
bellard579a97f2007-11-11 14:26:47 +00002551/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00002552static void setup_frame(int sig, struct target_sigaction * ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002553 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002554{
2555 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002556 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002557 int i;
2558
bellard579a97f2007-11-11 14:26:47 +00002559 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2560 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002561 goto give_sigsegv;
2562
2563 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2564
2565 if(setup_sigcontext(regs, &frame->sf_sc))
2566 goto give_sigsegv;
2567
2568 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2569 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2570 goto give_sigsegv;
2571 }
2572
2573 /*
2574 * Arguments to signal handler:
2575 *
2576 * a0 = signal number
2577 * a1 = 0 (should be cause)
2578 * a2 = pointer to struct sigcontext
2579 *
2580 * $25 and PC point to the signal handler, $29 points to the
2581 * struct sigframe.
2582 */
thsb5dc7732008-06-27 10:02:35 +00002583 regs->active_tc.gpr[ 4] = sig;
2584 regs->active_tc.gpr[ 5] = 0;
2585 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2586 regs->active_tc.gpr[29] = frame_addr;
2587 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002588 /* The original kernel code sets CP0_EPC to the handler
2589 * since it returns to userland using eret
2590 * we cannot do this here, and we must set PC directly */
thsb5dc7732008-06-27 10:02:35 +00002591 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002592 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002593 return;
2594
2595give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002596 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002597 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002598 return;
bellard106ec872006-06-27 21:08:10 +00002599}
2600
2601long do_sigreturn(CPUState *regs)
2602{
ths388bb212007-05-13 13:58:00 +00002603 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002604 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002605 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05002606 target_sigset_t target_set;
ths388bb212007-05-13 13:58:00 +00002607 int i;
bellard106ec872006-06-27 21:08:10 +00002608
2609#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002610 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002611#endif
thsb5dc7732008-06-27 10:02:35 +00002612 frame_addr = regs->active_tc.gpr[29];
bellard579a97f2007-11-11 14:26:47 +00002613 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002614 goto badframe;
2615
ths388bb212007-05-13 13:58:00 +00002616 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002617 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2618 goto badframe;
ths388bb212007-05-13 13:58:00 +00002619 }
bellard106ec872006-06-27 21:08:10 +00002620
ths388bb212007-05-13 13:58:00 +00002621 target_to_host_sigset_internal(&blocked, &target_set);
2622 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002623
ths388bb212007-05-13 13:58:00 +00002624 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002625 goto badframe;
2626
2627#if 0
ths388bb212007-05-13 13:58:00 +00002628 /*
2629 * Don't let your children do this ...
2630 */
2631 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002632 "move\t$29, %0\n\t"
2633 "j\tsyscall_exit"
2634 :/* no outputs */
2635 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002636 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002637#endif
ths3b46e622007-09-17 08:09:54 +00002638
thsb5dc7732008-06-27 10:02:35 +00002639 regs->active_tc.PC = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002640 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002641 * maybe a problem with nested signals ? */
2642 regs->CP0_EPC = 0;
pbrook0b1bcb02009-04-21 01:41:10 +00002643 return -TARGET_QEMU_ESIGRETURN;
bellard106ec872006-06-27 21:08:10 +00002644
2645badframe:
ths388bb212007-05-13 13:58:00 +00002646 force_sig(TARGET_SIGSEGV/*, current*/);
2647 return 0;
bellard106ec872006-06-27 21:08:10 +00002648}
2649
pbrook624f7972008-05-31 16:11:38 +00002650static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002651 target_siginfo_t *info,
2652 target_sigset_t *set, CPUState *env)
bellard106ec872006-06-27 21:08:10 +00002653{
pbrook0b1bcb02009-04-21 01:41:10 +00002654 struct target_rt_sigframe *frame;
2655 abi_ulong frame_addr;
2656 int i;
2657
2658 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2659 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2660 goto give_sigsegv;
2661
2662 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2663
2664 copy_siginfo_to_user(&frame->rs_info, info);
2665
2666 __put_user(0, &frame->rs_uc.uc_flags);
2667 __put_user(0, &frame->rs_uc.uc_link);
2668 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.uc_stack.ss_sp);
2669 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.uc_stack.ss_size);
2670 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2671 &frame->rs_uc.uc_stack.ss_flags);
2672
2673 setup_sigcontext(env, &frame->rs_uc.uc_mcontext);
2674
2675 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2676 __put_user(set->sig[i], &frame->rs_uc.uc_sigmask.sig[i]);
2677 }
2678
2679 /*
2680 * Arguments to signal handler:
2681 *
2682 * a0 = signal number
2683 * a1 = pointer to struct siginfo
2684 * a2 = pointer to struct ucontext
2685 *
2686 * $25 and PC point to the signal handler, $29 points to the
2687 * struct sigframe.
2688 */
2689 env->active_tc.gpr[ 4] = sig;
2690 env->active_tc.gpr[ 5] = frame_addr
2691 + offsetof(struct target_rt_sigframe, rs_info);
2692 env->active_tc.gpr[ 6] = frame_addr
2693 + offsetof(struct target_rt_sigframe, rs_uc);
2694 env->active_tc.gpr[29] = frame_addr;
2695 env->active_tc.gpr[31] = frame_addr
2696 + offsetof(struct target_rt_sigframe, rs_code);
2697 /* The original kernel code sets CP0_EPC to the handler
2698 * since it returns to userland using eret
2699 * we cannot do this here, and we must set PC directly */
2700 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2701 unlock_user_struct(frame, frame_addr, 1);
2702 return;
2703
2704give_sigsegv:
2705 unlock_user_struct(frame, frame_addr, 1);
2706 force_sig(TARGET_SIGSEGV/*, current*/);
2707 return;
bellard106ec872006-06-27 21:08:10 +00002708}
2709
2710long do_rt_sigreturn(CPUState *env)
2711{
pbrook0b1bcb02009-04-21 01:41:10 +00002712 struct target_rt_sigframe *frame;
2713 abi_ulong frame_addr;
2714 sigset_t blocked;
2715
2716#if defined(DEBUG_SIGNAL)
2717 fprintf(stderr, "do_rt_sigreturn\n");
2718#endif
2719 frame_addr = env->active_tc.gpr[29];
2720 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2721 goto badframe;
2722
2723 target_to_host_sigset(&blocked, &frame->rs_uc.uc_sigmask);
2724 sigprocmask(SIG_SETMASK, &blocked, NULL);
2725
2726 if (restore_sigcontext(env, &frame->rs_uc.uc_mcontext))
2727 goto badframe;
2728
2729 if (do_sigaltstack(frame_addr +
2730 offsetof(struct target_rt_sigframe, rs_uc.uc_stack),
2731 0, get_sp_from_cpustate(env)) == -EFAULT)
2732 goto badframe;
2733
2734 env->active_tc.PC = env->CP0_EPC;
2735 /* I am not sure this is right, but it seems to work
2736 * maybe a problem with nested signals ? */
2737 env->CP0_EPC = 0;
2738 return -TARGET_QEMU_ESIGRETURN;
2739
2740badframe:
2741 force_sig(TARGET_SIGSEGV/*, current*/);
2742 return 0;
bellard106ec872006-06-27 21:08:10 +00002743}
bellard6d5e2162004-09-30 22:04:13 +00002744
thsc3b5bc82007-12-02 06:31:25 +00002745#elif defined(TARGET_SH4)
2746
2747/*
2748 * code and data structures from linux kernel:
2749 * include/asm-sh/sigcontext.h
2750 * arch/sh/kernel/signal.c
2751 */
2752
2753struct target_sigcontext {
2754 target_ulong oldmask;
2755
2756 /* CPU registers */
2757 target_ulong sc_gregs[16];
2758 target_ulong sc_pc;
2759 target_ulong sc_pr;
2760 target_ulong sc_sr;
2761 target_ulong sc_gbr;
2762 target_ulong sc_mach;
2763 target_ulong sc_macl;
2764
2765 /* FPU registers */
2766 target_ulong sc_fpregs[16];
2767 target_ulong sc_xfpregs[16];
2768 unsigned int sc_fpscr;
2769 unsigned int sc_fpul;
2770 unsigned int sc_ownedfp;
2771};
2772
2773struct target_sigframe
2774{
2775 struct target_sigcontext sc;
2776 target_ulong extramask[TARGET_NSIG_WORDS-1];
2777 uint16_t retcode[3];
2778};
2779
2780
2781struct target_ucontext {
2782 target_ulong uc_flags;
2783 struct target_ucontext *uc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05002784 target_stack_t uc_stack;
thsc3b5bc82007-12-02 06:31:25 +00002785 struct target_sigcontext uc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05002786 target_sigset_t uc_sigmask; /* mask last for extensibility */
thsc3b5bc82007-12-02 06:31:25 +00002787};
2788
2789struct target_rt_sigframe
2790{
2791 struct target_siginfo info;
2792 struct target_ucontext uc;
2793 uint16_t retcode[3];
2794};
2795
2796
2797#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2798#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2799
pbrook624f7972008-05-31 16:11:38 +00002800static abi_ulong get_sigframe(struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002801 unsigned long sp, size_t frame_size)
2802{
pbrook624f7972008-05-31 16:11:38 +00002803 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
thsc3b5bc82007-12-02 06:31:25 +00002804 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2805 }
2806
2807 return (sp - frame_size) & -8ul;
2808}
2809
2810static int setup_sigcontext(struct target_sigcontext *sc,
2811 CPUState *regs, unsigned long mask)
2812{
2813 int err = 0;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002814 int i;
thsc3b5bc82007-12-02 06:31:25 +00002815
2816#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2817 COPY(gregs[0]); COPY(gregs[1]);
2818 COPY(gregs[2]); COPY(gregs[3]);
2819 COPY(gregs[4]); COPY(gregs[5]);
2820 COPY(gregs[6]); COPY(gregs[7]);
2821 COPY(gregs[8]); COPY(gregs[9]);
2822 COPY(gregs[10]); COPY(gregs[11]);
2823 COPY(gregs[12]); COPY(gregs[13]);
2824 COPY(gregs[14]); COPY(gregs[15]);
2825 COPY(gbr); COPY(mach);
2826 COPY(macl); COPY(pr);
2827 COPY(sr); COPY(pc);
2828#undef COPY
2829
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002830 for (i=0; i<16; i++) {
2831 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2832 }
2833 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2834 err |= __put_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00002835
2836 /* non-iBCS2 extensions.. */
2837 err |= __put_user(mask, &sc->oldmask);
2838
2839 return err;
2840}
2841
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002842static int restore_sigcontext(CPUState *regs, struct target_sigcontext *sc,
2843 target_ulong *r0_p)
thsc3b5bc82007-12-02 06:31:25 +00002844{
2845 unsigned int err = 0;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002846 int i;
thsc3b5bc82007-12-02 06:31:25 +00002847
2848#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2849 COPY(gregs[1]);
2850 COPY(gregs[2]); COPY(gregs[3]);
2851 COPY(gregs[4]); COPY(gregs[5]);
2852 COPY(gregs[6]); COPY(gregs[7]);
2853 COPY(gregs[8]); COPY(gregs[9]);
2854 COPY(gregs[10]); COPY(gregs[11]);
2855 COPY(gregs[12]); COPY(gregs[13]);
2856 COPY(gregs[14]); COPY(gregs[15]);
2857 COPY(gbr); COPY(mach);
2858 COPY(macl); COPY(pr);
2859 COPY(sr); COPY(pc);
2860#undef COPY
2861
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002862 for (i=0; i<16; i++) {
2863 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
2864 }
2865 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
2866 err |= __get_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00002867
2868 regs->tra = -1; /* disable syscall checks */
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002869 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
thsc3b5bc82007-12-02 06:31:25 +00002870 return err;
2871}
2872
pbrook624f7972008-05-31 16:11:38 +00002873static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002874 target_sigset_t *set, CPUState *regs)
thsc3b5bc82007-12-02 06:31:25 +00002875{
2876 struct target_sigframe *frame;
2877 abi_ulong frame_addr;
2878 int i;
2879 int err = 0;
2880 int signal;
2881
2882 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2883 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2884 goto give_sigsegv;
2885
2886 signal = current_exec_domain_sig(sig);
2887
2888 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2889
2890 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2891 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2892 }
2893
2894 /* Set up to return from userspace. If provided, use a stub
2895 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002896 if (ka->sa_flags & TARGET_SA_RESTORER) {
2897 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002898 } else {
2899 /* Generate return code (system call to sigreturn) */
2900 err |= __put_user(MOVW(2), &frame->retcode[0]);
2901 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2902 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2903 regs->pr = (unsigned long) frame->retcode;
2904 }
2905
2906 if (err)
2907 goto give_sigsegv;
2908
2909 /* Set up registers for signal handler */
2910 regs->gregs[15] = (unsigned long) frame;
2911 regs->gregs[4] = signal; /* Arg for signal handler */
2912 regs->gregs[5] = 0;
2913 regs->gregs[6] = (unsigned long) &frame->sc;
pbrook624f7972008-05-31 16:11:38 +00002914 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002915
2916 unlock_user_struct(frame, frame_addr, 1);
2917 return;
2918
2919give_sigsegv:
2920 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02002921 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00002922}
2923
pbrook624f7972008-05-31 16:11:38 +00002924static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002925 target_siginfo_t *info,
2926 target_sigset_t *set, CPUState *regs)
thsc3b5bc82007-12-02 06:31:25 +00002927{
2928 struct target_rt_sigframe *frame;
2929 abi_ulong frame_addr;
2930 int i;
2931 int err = 0;
2932 int signal;
2933
2934 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2935 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2936 goto give_sigsegv;
2937
2938 signal = current_exec_domain_sig(sig);
2939
2940 err |= copy_siginfo_to_user(&frame->info, info);
2941
2942 /* Create the ucontext. */
2943 err |= __put_user(0, &frame->uc.uc_flags);
2944 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
balrog526ccb72008-07-16 12:13:52 +00002945 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
thsc3b5bc82007-12-02 06:31:25 +00002946 &frame->uc.uc_stack.ss_sp);
2947 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2948 &frame->uc.uc_stack.ss_flags);
2949 err |= __put_user(target_sigaltstack_used.ss_size,
2950 &frame->uc.uc_stack.ss_size);
2951 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2952 regs, set->sig[0]);
2953 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2954 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2955 }
2956
2957 /* Set up to return from userspace. If provided, use a stub
2958 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002959 if (ka->sa_flags & TARGET_SA_RESTORER) {
2960 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002961 } else {
2962 /* Generate return code (system call to sigreturn) */
2963 err |= __put_user(MOVW(2), &frame->retcode[0]);
2964 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2965 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2966 regs->pr = (unsigned long) frame->retcode;
2967 }
2968
2969 if (err)
2970 goto give_sigsegv;
2971
2972 /* Set up registers for signal handler */
2973 regs->gregs[15] = (unsigned long) frame;
2974 regs->gregs[4] = signal; /* Arg for signal handler */
2975 regs->gregs[5] = (unsigned long) &frame->info;
2976 regs->gregs[6] = (unsigned long) &frame->uc;
pbrook624f7972008-05-31 16:11:38 +00002977 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002978
2979 unlock_user_struct(frame, frame_addr, 1);
2980 return;
2981
2982give_sigsegv:
2983 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02002984 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00002985}
2986
2987long do_sigreturn(CPUState *regs)
2988{
2989 struct target_sigframe *frame;
2990 abi_ulong frame_addr;
2991 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05002992 target_sigset_t target_set;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002993 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00002994 int i;
2995 int err = 0;
2996
2997#if defined(DEBUG_SIGNAL)
2998 fprintf(stderr, "do_sigreturn\n");
2999#endif
3000 frame_addr = regs->gregs[15];
3001 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3002 goto badframe;
3003
3004 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3005 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3006 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3007 }
3008
3009 if (err)
3010 goto badframe;
3011
3012 target_to_host_sigset_internal(&blocked, &target_set);
3013 sigprocmask(SIG_SETMASK, &blocked, NULL);
3014
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003015 if (restore_sigcontext(regs, &frame->sc, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003016 goto badframe;
3017
3018 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003019 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003020
3021badframe:
3022 unlock_user_struct(frame, frame_addr, 0);
3023 force_sig(TARGET_SIGSEGV);
3024 return 0;
3025}
3026
3027long do_rt_sigreturn(CPUState *regs)
3028{
3029 struct target_rt_sigframe *frame;
3030 abi_ulong frame_addr;
3031 sigset_t blocked;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003032 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00003033
3034#if defined(DEBUG_SIGNAL)
3035 fprintf(stderr, "do_rt_sigreturn\n");
3036#endif
3037 frame_addr = regs->gregs[15];
3038 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3039 goto badframe;
3040
3041 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
3042 sigprocmask(SIG_SETMASK, &blocked, NULL);
3043
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003044 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003045 goto badframe;
3046
3047 if (do_sigaltstack(frame_addr +
3048 offsetof(struct target_rt_sigframe, uc.uc_stack),
3049 0, get_sp_from_cpustate(regs)) == -EFAULT)
3050 goto badframe;
3051
3052 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003053 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003054
3055badframe:
3056 unlock_user_struct(frame, frame_addr, 0);
3057 force_sig(TARGET_SIGSEGV);
3058 return 0;
3059}
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003060#elif defined(TARGET_MICROBLAZE)
3061
3062struct target_sigcontext {
3063 struct target_pt_regs regs; /* needs to be first */
3064 uint32_t oldmask;
3065};
3066
3067/* Signal frames. */
3068struct target_signal_frame {
3069 struct target_sigcontext sc;
3070 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3071 uint32_t tramp[2];
3072};
3073
3074struct rt_signal_frame {
3075 struct siginfo info;
3076 struct ucontext uc;
3077 uint32_t tramp[2];
3078};
3079
3080static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3081{
3082 __put_user(env->regs[0], &sc->regs.r0);
3083 __put_user(env->regs[1], &sc->regs.r1);
3084 __put_user(env->regs[2], &sc->regs.r2);
3085 __put_user(env->regs[3], &sc->regs.r3);
3086 __put_user(env->regs[4], &sc->regs.r4);
3087 __put_user(env->regs[5], &sc->regs.r5);
3088 __put_user(env->regs[6], &sc->regs.r6);
3089 __put_user(env->regs[7], &sc->regs.r7);
3090 __put_user(env->regs[8], &sc->regs.r8);
3091 __put_user(env->regs[9], &sc->regs.r9);
3092 __put_user(env->regs[10], &sc->regs.r10);
3093 __put_user(env->regs[11], &sc->regs.r11);
3094 __put_user(env->regs[12], &sc->regs.r12);
3095 __put_user(env->regs[13], &sc->regs.r13);
3096 __put_user(env->regs[14], &sc->regs.r14);
3097 __put_user(env->regs[15], &sc->regs.r15);
3098 __put_user(env->regs[16], &sc->regs.r16);
3099 __put_user(env->regs[17], &sc->regs.r17);
3100 __put_user(env->regs[18], &sc->regs.r18);
3101 __put_user(env->regs[19], &sc->regs.r19);
3102 __put_user(env->regs[20], &sc->regs.r20);
3103 __put_user(env->regs[21], &sc->regs.r21);
3104 __put_user(env->regs[22], &sc->regs.r22);
3105 __put_user(env->regs[23], &sc->regs.r23);
3106 __put_user(env->regs[24], &sc->regs.r24);
3107 __put_user(env->regs[25], &sc->regs.r25);
3108 __put_user(env->regs[26], &sc->regs.r26);
3109 __put_user(env->regs[27], &sc->regs.r27);
3110 __put_user(env->regs[28], &sc->regs.r28);
3111 __put_user(env->regs[29], &sc->regs.r29);
3112 __put_user(env->regs[30], &sc->regs.r30);
3113 __put_user(env->regs[31], &sc->regs.r31);
3114 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3115}
3116
3117static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3118{
3119 __get_user(env->regs[0], &sc->regs.r0);
3120 __get_user(env->regs[1], &sc->regs.r1);
3121 __get_user(env->regs[2], &sc->regs.r2);
3122 __get_user(env->regs[3], &sc->regs.r3);
3123 __get_user(env->regs[4], &sc->regs.r4);
3124 __get_user(env->regs[5], &sc->regs.r5);
3125 __get_user(env->regs[6], &sc->regs.r6);
3126 __get_user(env->regs[7], &sc->regs.r7);
3127 __get_user(env->regs[8], &sc->regs.r8);
3128 __get_user(env->regs[9], &sc->regs.r9);
3129 __get_user(env->regs[10], &sc->regs.r10);
3130 __get_user(env->regs[11], &sc->regs.r11);
3131 __get_user(env->regs[12], &sc->regs.r12);
3132 __get_user(env->regs[13], &sc->regs.r13);
3133 __get_user(env->regs[14], &sc->regs.r14);
3134 __get_user(env->regs[15], &sc->regs.r15);
3135 __get_user(env->regs[16], &sc->regs.r16);
3136 __get_user(env->regs[17], &sc->regs.r17);
3137 __get_user(env->regs[18], &sc->regs.r18);
3138 __get_user(env->regs[19], &sc->regs.r19);
3139 __get_user(env->regs[20], &sc->regs.r20);
3140 __get_user(env->regs[21], &sc->regs.r21);
3141 __get_user(env->regs[22], &sc->regs.r22);
3142 __get_user(env->regs[23], &sc->regs.r23);
3143 __get_user(env->regs[24], &sc->regs.r24);
3144 __get_user(env->regs[25], &sc->regs.r25);
3145 __get_user(env->regs[26], &sc->regs.r26);
3146 __get_user(env->regs[27], &sc->regs.r27);
3147 __get_user(env->regs[28], &sc->regs.r28);
3148 __get_user(env->regs[29], &sc->regs.r29);
3149 __get_user(env->regs[30], &sc->regs.r30);
3150 __get_user(env->regs[31], &sc->regs.r31);
3151 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3152}
3153
3154static abi_ulong get_sigframe(struct target_sigaction *ka,
3155 CPUState *env, int frame_size)
3156{
3157 abi_ulong sp = env->regs[1];
3158
3159 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3160 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3161
3162 return ((sp - frame_size) & -8UL);
3163}
3164
3165static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003166 target_sigset_t *set, CPUState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003167{
3168 struct target_signal_frame *frame;
3169 abi_ulong frame_addr;
3170 int err = 0;
3171 int i;
3172
3173 frame_addr = get_sigframe(ka, env, sizeof *frame);
3174 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3175 goto badframe;
3176
3177 /* Save the mask. */
3178 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3179 if (err)
3180 goto badframe;
3181
3182 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3183 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3184 goto badframe;
3185 }
3186
3187 setup_sigcontext(&frame->sc, env);
3188
3189 /* Set up to return from userspace. If provided, use a stub
3190 already in userspace. */
3191 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3192 if (ka->sa_flags & TARGET_SA_RESTORER) {
3193 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3194 } else {
3195 uint32_t t;
3196 /* Note, these encodings are _big endian_! */
3197 /* addi r12, r0, __NR_sigreturn */
3198 t = 0x31800000UL | TARGET_NR_sigreturn;
3199 err |= __put_user(t, frame->tramp + 0);
3200 /* brki r14, 0x8 */
3201 t = 0xb9cc0008UL;
3202 err |= __put_user(t, frame->tramp + 1);
3203
3204 /* Return from sighandler will jump to the tramp.
3205 Negative 8 offset because return is rtsd r15, 8 */
3206 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3207 }
3208
3209 if (err)
3210 goto badframe;
3211
3212 /* Set up registers for signal handler */
3213 env->regs[1] = (unsigned long) frame;
3214 /* Signal handler args: */
3215 env->regs[5] = sig; /* Arg 0: signum */
3216 env->regs[6] = (unsigned long) &frame->sc; /* arg 1: sigcontext */
3217
3218 /* Offset of 4 to handle microblaze rtid r14, 0 */
3219 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3220
3221 unlock_user_struct(frame, frame_addr, 1);
3222 return;
3223 badframe:
3224 unlock_user_struct(frame, frame_addr, 1);
3225 force_sig(TARGET_SIGSEGV);
3226}
3227
3228static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003229 target_siginfo_t *info,
3230 target_sigset_t *set, CPUState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003231{
3232 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3233}
3234
3235long do_sigreturn(CPUState *env)
3236{
3237 struct target_signal_frame *frame;
3238 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003239 target_sigset_t target_set;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003240 sigset_t set;
3241 int i;
3242
3243 frame_addr = env->regs[R_SP];
3244 /* Make sure the guest isn't playing games. */
3245 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3246 goto badframe;
3247
3248 /* Restore blocked signals */
3249 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3250 goto badframe;
3251 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3252 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3253 goto badframe;
3254 }
3255 target_to_host_sigset_internal(&set, &target_set);
3256 sigprocmask(SIG_SETMASK, &set, NULL);
3257
3258 restore_sigcontext(&frame->sc, env);
3259 /* We got here through a sigreturn syscall, our path back is via an
3260 rtb insn so setup r14 for that. */
3261 env->regs[14] = env->sregs[SR_PC];
3262
3263 unlock_user_struct(frame, frame_addr, 0);
3264 return env->regs[10];
3265 badframe:
3266 unlock_user_struct(frame, frame_addr, 0);
3267 force_sig(TARGET_SIGSEGV);
3268}
3269
3270long do_rt_sigreturn(CPUState *env)
3271{
3272 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3273 return -TARGET_ENOSYS;
3274}
3275
edgar_iglb6d3abd2008-02-28 11:29:27 +00003276#elif defined(TARGET_CRIS)
3277
3278struct target_sigcontext {
3279 struct target_pt_regs regs; /* needs to be first */
3280 uint32_t oldmask;
3281 uint32_t usp; /* usp before stacking this gunk on it */
3282};
3283
3284/* Signal frames. */
3285struct target_signal_frame {
3286 struct target_sigcontext sc;
3287 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3288 uint8_t retcode[8]; /* Trampoline code. */
3289};
3290
3291struct rt_signal_frame {
3292 struct siginfo *pinfo;
3293 void *puc;
3294 struct siginfo info;
3295 struct ucontext uc;
3296 uint8_t retcode[8]; /* Trampoline code. */
3297};
3298
3299static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3300{
edgar_igl9664d922008-03-03 22:23:53 +00003301 __put_user(env->regs[0], &sc->regs.r0);
3302 __put_user(env->regs[1], &sc->regs.r1);
3303 __put_user(env->regs[2], &sc->regs.r2);
3304 __put_user(env->regs[3], &sc->regs.r3);
3305 __put_user(env->regs[4], &sc->regs.r4);
3306 __put_user(env->regs[5], &sc->regs.r5);
3307 __put_user(env->regs[6], &sc->regs.r6);
3308 __put_user(env->regs[7], &sc->regs.r7);
3309 __put_user(env->regs[8], &sc->regs.r8);
3310 __put_user(env->regs[9], &sc->regs.r9);
3311 __put_user(env->regs[10], &sc->regs.r10);
3312 __put_user(env->regs[11], &sc->regs.r11);
3313 __put_user(env->regs[12], &sc->regs.r12);
3314 __put_user(env->regs[13], &sc->regs.r13);
3315 __put_user(env->regs[14], &sc->usp);
3316 __put_user(env->regs[15], &sc->regs.acr);
3317 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3318 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3319 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003320}
edgar_igl9664d922008-03-03 22:23:53 +00003321
edgar_iglb6d3abd2008-02-28 11:29:27 +00003322static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3323{
edgar_igl9664d922008-03-03 22:23:53 +00003324 __get_user(env->regs[0], &sc->regs.r0);
3325 __get_user(env->regs[1], &sc->regs.r1);
3326 __get_user(env->regs[2], &sc->regs.r2);
3327 __get_user(env->regs[3], &sc->regs.r3);
3328 __get_user(env->regs[4], &sc->regs.r4);
3329 __get_user(env->regs[5], &sc->regs.r5);
3330 __get_user(env->regs[6], &sc->regs.r6);
3331 __get_user(env->regs[7], &sc->regs.r7);
3332 __get_user(env->regs[8], &sc->regs.r8);
3333 __get_user(env->regs[9], &sc->regs.r9);
3334 __get_user(env->regs[10], &sc->regs.r10);
3335 __get_user(env->regs[11], &sc->regs.r11);
3336 __get_user(env->regs[12], &sc->regs.r12);
3337 __get_user(env->regs[13], &sc->regs.r13);
3338 __get_user(env->regs[14], &sc->usp);
3339 __get_user(env->regs[15], &sc->regs.acr);
3340 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3341 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3342 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003343}
3344
edgar_igl9664d922008-03-03 22:23:53 +00003345static abi_ulong get_sigframe(CPUState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003346{
edgar_igl9664d922008-03-03 22:23:53 +00003347 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003348 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00003349 sp = (env->regs[R_SP] & ~3);
3350 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003351}
3352
pbrook624f7972008-05-31 16:11:38 +00003353static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003354 target_sigset_t *set, CPUState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003355{
3356 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003357 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003358 int err = 0;
3359 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003360
edgar_igl9664d922008-03-03 22:23:53 +00003361 frame_addr = get_sigframe(env, sizeof *frame);
3362 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003363 goto badframe;
3364
3365 /*
3366 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3367 * use this trampoline anymore but it sets it up for GDB.
3368 * In QEMU, using the trampoline simplifies things a bit so we use it.
3369 *
3370 * This is movu.w __NR_sigreturn, r9; break 13;
3371 */
3372 err |= __put_user(0x9c5f, frame->retcode+0);
3373 err |= __put_user(TARGET_NR_sigreturn,
3374 frame->retcode+2);
3375 err |= __put_user(0xe93d, frame->retcode+4);
3376
3377 /* Save the mask. */
3378 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3379 if (err)
3380 goto badframe;
3381
3382 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3383 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3384 goto badframe;
3385 }
3386
3387 setup_sigcontext(&frame->sc, env);
3388
3389 /* Move the stack and setup the arguments for the handler. */
balrog526ccb72008-07-16 12:13:52 +00003390 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003391 env->regs[10] = sig;
pbrook624f7972008-05-31 16:11:38 +00003392 env->pc = (unsigned long) ka->_sa_handler;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003393 /* Link SRP so the guest returns through the trampoline. */
balrog526ccb72008-07-16 12:13:52 +00003394 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003395
edgar_igl9664d922008-03-03 22:23:53 +00003396 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003397 return;
3398 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003399 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003400 force_sig(TARGET_SIGSEGV);
3401}
3402
pbrook624f7972008-05-31 16:11:38 +00003403static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003404 target_siginfo_t *info,
3405 target_sigset_t *set, CPUState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003406{
3407 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3408}
3409
3410long do_sigreturn(CPUState *env)
3411{
3412 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003413 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003414 target_sigset_t target_set;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003415 sigset_t set;
3416 int i;
3417
edgar_igl9664d922008-03-03 22:23:53 +00003418 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003419 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003420 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003421 goto badframe;
3422
3423 /* Restore blocked signals */
3424 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3425 goto badframe;
3426 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3427 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3428 goto badframe;
3429 }
3430 target_to_host_sigset_internal(&set, &target_set);
3431 sigprocmask(SIG_SETMASK, &set, NULL);
3432
3433 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003434 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003435 return env->regs[10];
3436 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003437 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003438 force_sig(TARGET_SIGSEGV);
3439}
3440
3441long do_rt_sigreturn(CPUState *env)
3442{
3443 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3444 return -TARGET_ENOSYS;
3445}
thsc3b5bc82007-12-02 06:31:25 +00003446
Nathan Froydbcd49332009-05-12 19:13:18 -07003447#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
3448
3449/* FIXME: Many of the structures are defined for both PPC and PPC64, but
3450 the signal handling is different enough that we haven't implemented
3451 support for PPC64 yet. Hence the restriction above.
3452
3453 There are various #if'd blocks for code for TARGET_PPC64. These
3454 blocks should go away so that we can successfully run 32-bit and
3455 64-bit binaries on a QEMU configured for PPC64. */
3456
3457/* Size of dummy stack frame allocated when calling signal handler.
3458 See arch/powerpc/include/asm/ptrace.h. */
3459#if defined(TARGET_PPC64)
3460#define SIGNAL_FRAMESIZE 128
3461#else
3462#define SIGNAL_FRAMESIZE 64
3463#endif
3464
3465/* See arch/powerpc/include/asm/sigcontext.h. */
3466struct target_sigcontext {
3467 target_ulong _unused[4];
3468 int32_t signal;
3469#if defined(TARGET_PPC64)
3470 int32_t pad0;
3471#endif
3472 target_ulong handler;
3473 target_ulong oldmask;
3474 target_ulong regs; /* struct pt_regs __user * */
3475 /* TODO: PPC64 includes extra bits here. */
3476};
3477
3478/* Indices for target_mcontext.mc_gregs, below.
3479 See arch/powerpc/include/asm/ptrace.h for details. */
3480enum {
3481 TARGET_PT_R0 = 0,
3482 TARGET_PT_R1 = 1,
3483 TARGET_PT_R2 = 2,
3484 TARGET_PT_R3 = 3,
3485 TARGET_PT_R4 = 4,
3486 TARGET_PT_R5 = 5,
3487 TARGET_PT_R6 = 6,
3488 TARGET_PT_R7 = 7,
3489 TARGET_PT_R8 = 8,
3490 TARGET_PT_R9 = 9,
3491 TARGET_PT_R10 = 10,
3492 TARGET_PT_R11 = 11,
3493 TARGET_PT_R12 = 12,
3494 TARGET_PT_R13 = 13,
3495 TARGET_PT_R14 = 14,
3496 TARGET_PT_R15 = 15,
3497 TARGET_PT_R16 = 16,
3498 TARGET_PT_R17 = 17,
3499 TARGET_PT_R18 = 18,
3500 TARGET_PT_R19 = 19,
3501 TARGET_PT_R20 = 20,
3502 TARGET_PT_R21 = 21,
3503 TARGET_PT_R22 = 22,
3504 TARGET_PT_R23 = 23,
3505 TARGET_PT_R24 = 24,
3506 TARGET_PT_R25 = 25,
3507 TARGET_PT_R26 = 26,
3508 TARGET_PT_R27 = 27,
3509 TARGET_PT_R28 = 28,
3510 TARGET_PT_R29 = 29,
3511 TARGET_PT_R30 = 30,
3512 TARGET_PT_R31 = 31,
3513 TARGET_PT_NIP = 32,
3514 TARGET_PT_MSR = 33,
3515 TARGET_PT_ORIG_R3 = 34,
3516 TARGET_PT_CTR = 35,
3517 TARGET_PT_LNK = 36,
3518 TARGET_PT_XER = 37,
3519 TARGET_PT_CCR = 38,
3520 /* Yes, there are two registers with #39. One is 64-bit only. */
3521 TARGET_PT_MQ = 39,
3522 TARGET_PT_SOFTE = 39,
3523 TARGET_PT_TRAP = 40,
3524 TARGET_PT_DAR = 41,
3525 TARGET_PT_DSISR = 42,
3526 TARGET_PT_RESULT = 43,
3527 TARGET_PT_REGS_COUNT = 44
3528};
3529
3530/* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
3531 on 64-bit PPC, sigcontext and mcontext are one and the same. */
3532struct target_mcontext {
3533 target_ulong mc_gregs[48];
3534 /* Includes fpscr. */
3535 uint64_t mc_fregs[33];
3536 target_ulong mc_pad[2];
3537 /* We need to handle Altivec and SPE at the same time, which no
3538 kernel needs to do. Fortunately, the kernel defines this bit to
3539 be Altivec-register-large all the time, rather than trying to
3540 twiddle it based on the specific platform. */
3541 union {
3542 /* SPE vector registers. One extra for SPEFSCR. */
3543 uint32_t spe[33];
3544 /* Altivec vector registers. The packing of VSCR and VRSAVE
3545 varies depending on whether we're PPC64 or not: PPC64 splits
3546 them apart; PPC32 stuffs them together. */
3547#if defined(TARGET_PPC64)
malc3efa9a62009-07-18 13:10:12 +04003548#define QEMU_NVRREG 34
Nathan Froydbcd49332009-05-12 19:13:18 -07003549#else
malc3efa9a62009-07-18 13:10:12 +04003550#define QEMU_NVRREG 33
Nathan Froydbcd49332009-05-12 19:13:18 -07003551#endif
Anthony Liguoric227f092009-10-01 16:12:16 -05003552 ppc_avr_t altivec[QEMU_NVRREG];
malc3efa9a62009-07-18 13:10:12 +04003553#undef QEMU_NVRREG
Nathan Froydbcd49332009-05-12 19:13:18 -07003554 } mc_vregs __attribute__((__aligned__(16)));
3555};
3556
3557struct target_ucontext {
3558 target_ulong uc_flags;
3559 target_ulong uc_link; /* struct ucontext __user * */
3560 struct target_sigaltstack uc_stack;
3561#if !defined(TARGET_PPC64)
3562 int32_t uc_pad[7];
3563 target_ulong uc_regs; /* struct mcontext __user *
3564 points to uc_mcontext field */
3565#endif
Anthony Liguoric227f092009-10-01 16:12:16 -05003566 target_sigset_t uc_sigmask;
Nathan Froydbcd49332009-05-12 19:13:18 -07003567#if defined(TARGET_PPC64)
Anthony Liguoric227f092009-10-01 16:12:16 -05003568 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
Nathan Froydbcd49332009-05-12 19:13:18 -07003569 struct target_sigcontext uc_mcontext;
3570#else
3571 int32_t uc_maskext[30];
3572 int32_t uc_pad2[3];
3573 struct target_mcontext uc_mcontext;
3574#endif
3575};
3576
3577/* See arch/powerpc/kernel/signal_32.c. */
3578struct target_sigframe {
3579 struct target_sigcontext sctx;
3580 struct target_mcontext mctx;
3581 int32_t abigap[56];
3582};
3583
3584struct target_rt_sigframe {
3585 struct target_siginfo info;
3586 struct target_ucontext uc;
3587 int32_t abigap[56];
3588};
3589
3590/* We use the mc_pad field for the signal return trampoline. */
3591#define tramp mc_pad
3592
3593/* See arch/powerpc/kernel/signal.c. */
3594static target_ulong get_sigframe(struct target_sigaction *ka,
3595 CPUState *env,
3596 int frame_size)
3597{
3598 target_ulong oldsp, newsp;
3599
3600 oldsp = env->gpr[1];
3601
3602 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
3603 (sas_ss_flags(oldsp))) {
3604 oldsp = (target_sigaltstack_used.ss_sp
3605 + target_sigaltstack_used.ss_size);
3606 }
3607
3608 newsp = (oldsp - frame_size) & ~0xFUL;
3609
3610 return newsp;
3611}
3612
3613static int save_user_regs(CPUState *env, struct target_mcontext *frame,
3614 int sigret)
3615{
3616 target_ulong msr = env->msr;
3617 int i;
3618 target_ulong ccr = 0;
3619
3620 /* In general, the kernel attempts to be intelligent about what it
3621 needs to save for Altivec/FP/SPE registers. We don't care that
3622 much, so we just go ahead and save everything. */
3623
3624 /* Save general registers. */
3625 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3626 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
3627 return 1;
3628 }
3629 }
3630 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3631 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3632 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3633 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3634 return 1;
3635
3636 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3637 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
3638 }
3639 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3640 return 1;
3641
3642 /* Save Altivec registers if necessary. */
3643 if (env->insns_flags & PPC_ALTIVEC) {
3644 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003645 ppc_avr_t *avr = &env->avr[i];
3646 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07003647
3648 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
3649 __put_user(avr->u64[1], &vreg->u64[1])) {
3650 return 1;
3651 }
3652 }
3653 /* Set MSR_VR in the saved MSR value to indicate that
3654 frame->mc_vregs contains valid data. */
3655 msr |= MSR_VR;
3656 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
3657 &frame->mc_vregs.altivec[32].u32[3]))
3658 return 1;
3659 }
3660
3661 /* Save floating point registers. */
3662 if (env->insns_flags & PPC_FLOAT) {
3663 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3664 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
3665 return 1;
3666 }
3667 }
3668 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
3669 return 1;
3670 }
3671
3672 /* Save SPE registers. The kernel only saves the high half. */
3673 if (env->insns_flags & PPC_SPE) {
3674#if defined(TARGET_PPC64)
3675 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3676 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
3677 return 1;
3678 }
3679 }
3680#else
3681 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3682 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3683 return 1;
3684 }
3685 }
3686#endif
3687 /* Set MSR_SPE in the saved MSR value to indicate that
3688 frame->mc_vregs contains valid data. */
3689 msr |= MSR_SPE;
3690 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3691 return 1;
3692 }
3693
3694 /* Store MSR. */
3695 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3696 return 1;
3697
3698 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
3699 if (sigret) {
3700 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
3701 __put_user(0x44000002UL, &frame->tramp[1])) {
3702 return 1;
3703 }
3704 }
3705
3706 return 0;
3707}
3708
3709static int restore_user_regs(CPUState *env,
3710 struct target_mcontext *frame, int sig)
3711{
3712 target_ulong save_r2 = 0;
3713 target_ulong msr;
3714 target_ulong ccr;
3715
3716 int i;
3717
3718 if (!sig) {
3719 save_r2 = env->gpr[2];
3720 }
3721
3722 /* Restore general registers. */
3723 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3724 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
3725 return 1;
3726 }
3727 }
3728 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3729 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3730 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3731 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3732 return 1;
3733 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3734 return 1;
3735
3736 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3737 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
3738 }
3739
3740 if (!sig) {
3741 env->gpr[2] = save_r2;
3742 }
3743 /* Restore MSR. */
3744 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3745 return 1;
3746
3747 /* If doing signal return, restore the previous little-endian mode. */
3748 if (sig)
3749 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
3750
3751 /* Restore Altivec registers if necessary. */
3752 if (env->insns_flags & PPC_ALTIVEC) {
3753 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003754 ppc_avr_t *avr = &env->avr[i];
3755 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07003756
3757 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
3758 __get_user(avr->u64[1], &vreg->u64[1])) {
3759 return 1;
3760 }
3761 }
3762 /* Set MSR_VEC in the saved MSR value to indicate that
3763 frame->mc_vregs contains valid data. */
3764 if (__get_user(env->spr[SPR_VRSAVE],
3765 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
3766 return 1;
3767 }
3768
3769 /* Restore floating point registers. */
3770 if (env->insns_flags & PPC_FLOAT) {
3771 uint64_t fpscr;
3772 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3773 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
3774 return 1;
3775 }
3776 }
3777 if (__get_user(fpscr, &frame->mc_fregs[32]))
3778 return 1;
3779 env->fpscr = (uint32_t) fpscr;
3780 }
3781
3782 /* Save SPE registers. The kernel only saves the high half. */
3783 if (env->insns_flags & PPC_SPE) {
3784#if defined(TARGET_PPC64)
3785 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3786 uint32_t hi;
3787
3788 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
3789 return 1;
3790 }
3791 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
3792 }
3793#else
3794 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3795 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3796 return 1;
3797 }
3798 }
3799#endif
3800 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3801 return 1;
3802 }
3803
3804 return 0;
3805}
3806
3807static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003808 target_sigset_t *set, CPUState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07003809{
3810 struct target_sigframe *frame;
3811 struct target_sigcontext *sc;
3812 target_ulong frame_addr, newsp;
3813 int err = 0;
3814 int signal;
3815
3816 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3817 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3818 goto sigsegv;
3819 sc = &frame->sctx;
3820
3821 signal = current_exec_domain_sig(sig);
3822
3823 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
3824 err |= __put_user(set->sig[0], &sc->oldmask);
3825#if defined(TARGET_PPC64)
3826 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
3827#else
3828 err |= __put_user(set->sig[1], &sc->_unused[3]);
3829#endif
3830 err |= __put_user(h2g(&frame->mctx), &sc->regs);
3831 err |= __put_user(sig, &sc->signal);
3832
3833 /* Save user regs. */
3834 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
3835
3836 /* The kernel checks for the presence of a VDSO here. We don't
3837 emulate a vdso, so use a sigreturn system call. */
3838 env->lr = (target_ulong) h2g(frame->mctx.tramp);
3839
3840 /* Turn off all fp exceptions. */
3841 env->fpscr = 0;
3842
3843 /* Create a stack frame for the caller of the handler. */
3844 newsp = frame_addr - SIGNAL_FRAMESIZE;
3845 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3846
3847 if (err)
3848 goto sigsegv;
3849
3850 /* Set up registers for signal handler. */
3851 env->gpr[1] = newsp;
3852 env->gpr[3] = signal;
3853 env->gpr[4] = (target_ulong) h2g(sc);
3854 env->nip = (target_ulong) ka->_sa_handler;
3855 /* Signal handlers are entered in big-endian mode. */
3856 env->msr &= ~MSR_LE;
3857
3858 unlock_user_struct(frame, frame_addr, 1);
3859 return;
3860
3861sigsegv:
3862 unlock_user_struct(frame, frame_addr, 1);
3863 if (logfile)
3864 fprintf (logfile, "segfaulting from setup_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02003865 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07003866}
3867
3868static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003869 target_siginfo_t *info,
3870 target_sigset_t *set, CPUState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07003871{
3872 struct target_rt_sigframe *rt_sf;
3873 struct target_mcontext *frame;
3874 target_ulong rt_sf_addr, newsp = 0;
3875 int i, err = 0;
3876 int signal;
3877
3878 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
3879 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
3880 goto sigsegv;
3881
3882 signal = current_exec_domain_sig(sig);
3883
3884 err |= copy_siginfo_to_user(&rt_sf->info, info);
3885
3886 err |= __put_user(0, &rt_sf->uc.uc_flags);
3887 err |= __put_user(0, &rt_sf->uc.uc_link);
3888 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
3889 &rt_sf->uc.uc_stack.ss_sp);
3890 err |= __put_user(sas_ss_flags(env->gpr[1]),
3891 &rt_sf->uc.uc_stack.ss_flags);
3892 err |= __put_user(target_sigaltstack_used.ss_size,
3893 &rt_sf->uc.uc_stack.ss_size);
3894 err |= __put_user(h2g (&rt_sf->uc.uc_mcontext),
3895 &rt_sf->uc.uc_regs);
3896 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3897 err |= __put_user(set->sig[i], &rt_sf->uc.uc_sigmask.sig[i]);
3898 }
3899
3900 frame = &rt_sf->uc.uc_mcontext;
3901 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
3902
3903 /* The kernel checks for the presence of a VDSO here. We don't
3904 emulate a vdso, so use a sigreturn system call. */
3905 env->lr = (target_ulong) h2g(frame->tramp);
3906
3907 /* Turn off all fp exceptions. */
3908 env->fpscr = 0;
3909
3910 /* Create a stack frame for the caller of the handler. */
3911 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
3912 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3913
3914 if (err)
3915 goto sigsegv;
3916
3917 /* Set up registers for signal handler. */
3918 env->gpr[1] = newsp;
3919 env->gpr[3] = (target_ulong) signal;
3920 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
3921 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
3922 env->gpr[6] = (target_ulong) h2g(rt_sf);
3923 env->nip = (target_ulong) ka->_sa_handler;
3924 /* Signal handlers are entered in big-endian mode. */
3925 env->msr &= ~MSR_LE;
3926
3927 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3928 return;
3929
3930sigsegv:
3931 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3932 if (logfile)
3933 fprintf (logfile, "segfaulting from setup_rt_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02003934 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07003935
3936}
3937
3938long do_sigreturn(CPUState *env)
3939{
3940 struct target_sigcontext *sc = NULL;
3941 struct target_mcontext *sr = NULL;
3942 target_ulong sr_addr, sc_addr;
3943 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05003944 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07003945
3946 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
3947 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
3948 goto sigsegv;
3949
3950#if defined(TARGET_PPC64)
3951 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
3952#else
3953 if(__get_user(set.sig[0], &sc->oldmask) ||
3954 __get_user(set.sig[1], &sc->_unused[3]))
3955 goto sigsegv;
3956#endif
3957 target_to_host_sigset_internal(&blocked, &set);
3958 sigprocmask(SIG_SETMASK, &blocked, NULL);
3959
3960 if (__get_user(sr_addr, &sc->regs))
3961 goto sigsegv;
3962 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
3963 goto sigsegv;
3964 if (restore_user_regs(env, sr, 1))
3965 goto sigsegv;
3966
3967 unlock_user_struct(sr, sr_addr, 1);
3968 unlock_user_struct(sc, sc_addr, 1);
3969 return -TARGET_QEMU_ESIGRETURN;
3970
3971sigsegv:
3972 unlock_user_struct(sr, sr_addr, 1);
3973 unlock_user_struct(sc, sc_addr, 1);
3974 if (logfile)
3975 fprintf (logfile, "segfaulting from do_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02003976 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07003977 return 0;
3978}
3979
3980/* See arch/powerpc/kernel/signal_32.c. */
3981static int do_setcontext(struct target_ucontext *ucp, CPUState *env, int sig)
3982{
3983 struct target_mcontext *mcp;
3984 target_ulong mcp_addr;
3985 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05003986 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07003987
3988 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, uc_sigmask),
3989 sizeof (set)))
3990 return 1;
3991
3992#if defined(TARGET_PPC64)
3993 fprintf (stderr, "do_setcontext: not implemented\n");
3994 return 0;
3995#else
3996 if (__get_user(mcp_addr, &ucp->uc_regs))
3997 return 1;
3998
3999 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4000 return 1;
4001
4002 target_to_host_sigset_internal(&blocked, &set);
4003 sigprocmask(SIG_SETMASK, &blocked, NULL);
4004 if (restore_user_regs(env, mcp, sig))
4005 goto sigsegv;
4006
4007 unlock_user_struct(mcp, mcp_addr, 1);
4008 return 0;
4009
4010sigsegv:
4011 unlock_user_struct(mcp, mcp_addr, 1);
4012 return 1;
4013#endif
4014}
4015
4016long do_rt_sigreturn(CPUState *env)
4017{
4018 struct target_rt_sigframe *rt_sf = NULL;
4019 target_ulong rt_sf_addr;
4020
4021 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4022 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4023 goto sigsegv;
4024
4025 if (do_setcontext(&rt_sf->uc, env, 1))
4026 goto sigsegv;
4027
4028 do_sigaltstack(rt_sf_addr
4029 + offsetof(struct target_rt_sigframe, uc.uc_stack),
4030 0, env->gpr[1]);
4031
4032 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4033 return -TARGET_QEMU_ESIGRETURN;
4034
4035sigsegv:
4036 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4037 if (logfile)
4038 fprintf (logfile, "segfaulting from do_rt_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004039 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004040 return 0;
4041}
4042
Laurent Vivier492a8742009-08-03 16:12:17 +02004043#elif defined(TARGET_M68K)
4044
4045struct target_sigcontext {
4046 abi_ulong sc_mask;
4047 abi_ulong sc_usp;
4048 abi_ulong sc_d0;
4049 abi_ulong sc_d1;
4050 abi_ulong sc_a0;
4051 abi_ulong sc_a1;
4052 unsigned short sc_sr;
4053 abi_ulong sc_pc;
4054};
4055
4056struct target_sigframe
4057{
4058 abi_ulong pretcode;
4059 int sig;
4060 int code;
4061 abi_ulong psc;
4062 char retcode[8];
4063 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4064 struct target_sigcontext sc;
4065};
Laurent Vivier71811552009-08-03 16:12:18 +02004066
Anthony Liguoric227f092009-10-01 16:12:16 -05004067typedef int target_greg_t;
Laurent Vivier71811552009-08-03 16:12:18 +02004068#define TARGET_NGREG 18
Anthony Liguoric227f092009-10-01 16:12:16 -05004069typedef target_greg_t target_gregset_t[TARGET_NGREG];
Laurent Vivier71811552009-08-03 16:12:18 +02004070
4071typedef struct target_fpregset {
4072 int f_fpcntl[3];
4073 int f_fpregs[8*3];
Anthony Liguoric227f092009-10-01 16:12:16 -05004074} target_fpregset_t;
Laurent Vivier71811552009-08-03 16:12:18 +02004075
4076struct target_mcontext {
4077 int version;
Anthony Liguoric227f092009-10-01 16:12:16 -05004078 target_gregset_t gregs;
4079 target_fpregset_t fpregs;
Laurent Vivier71811552009-08-03 16:12:18 +02004080};
4081
4082#define TARGET_MCONTEXT_VERSION 2
4083
4084struct target_ucontext {
4085 abi_ulong uc_flags;
4086 abi_ulong uc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05004087 target_stack_t uc_stack;
Laurent Vivier71811552009-08-03 16:12:18 +02004088 struct target_mcontext uc_mcontext;
4089 abi_long uc_filler[80];
Anthony Liguoric227f092009-10-01 16:12:16 -05004090 target_sigset_t uc_sigmask;
Laurent Vivier71811552009-08-03 16:12:18 +02004091};
4092
4093struct target_rt_sigframe
4094{
4095 abi_ulong pretcode;
4096 int sig;
4097 abi_ulong pinfo;
4098 abi_ulong puc;
4099 char retcode[8];
4100 struct target_siginfo info;
4101 struct target_ucontext uc;
4102};
Laurent Vivier492a8742009-08-03 16:12:17 +02004103
4104static int
4105setup_sigcontext(struct target_sigcontext *sc, CPUState *env, abi_ulong mask)
4106{
4107 int err = 0;
4108
4109 err |= __put_user(mask, &sc->sc_mask);
4110 err |= __put_user(env->aregs[7], &sc->sc_usp);
4111 err |= __put_user(env->dregs[0], &sc->sc_d0);
4112 err |= __put_user(env->dregs[1], &sc->sc_d1);
4113 err |= __put_user(env->aregs[0], &sc->sc_a0);
4114 err |= __put_user(env->aregs[1], &sc->sc_a1);
4115 err |= __put_user(env->sr, &sc->sc_sr);
4116 err |= __put_user(env->pc, &sc->sc_pc);
4117
4118 return err;
4119}
4120
4121static int
4122restore_sigcontext(CPUState *env, struct target_sigcontext *sc, int *pd0)
4123{
4124 int err = 0;
4125 int temp;
4126
4127 err |= __get_user(env->aregs[7], &sc->sc_usp);
4128 err |= __get_user(env->dregs[1], &sc->sc_d1);
4129 err |= __get_user(env->aregs[0], &sc->sc_a0);
4130 err |= __get_user(env->aregs[1], &sc->sc_a1);
4131 err |= __get_user(env->pc, &sc->sc_pc);
4132 err |= __get_user(temp, &sc->sc_sr);
4133 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4134
4135 *pd0 = tswapl(sc->sc_d0);
4136
4137 return err;
4138}
4139
4140/*
4141 * Determine which stack to use..
4142 */
4143static inline abi_ulong
4144get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
4145{
4146 unsigned long sp;
4147
4148 sp = regs->aregs[7];
4149
4150 /* This is the X/Open sanctioned signal stack switching. */
4151 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4152 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4153 }
4154
4155 return ((sp - frame_size) & -8UL);
4156}
4157
4158static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004159 target_sigset_t *set, CPUState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02004160{
4161 struct target_sigframe *frame;
4162 abi_ulong frame_addr;
4163 abi_ulong retcode_addr;
4164 abi_ulong sc_addr;
4165 int err = 0;
4166 int i;
4167
4168 frame_addr = get_sigframe(ka, env, sizeof *frame);
4169 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4170 goto give_sigsegv;
4171
4172 err |= __put_user(sig, &frame->sig);
4173
4174 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4175 err |= __put_user(sc_addr, &frame->psc);
4176
4177 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4178 if (err)
4179 goto give_sigsegv;
4180
4181 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4182 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4183 goto give_sigsegv;
4184 }
4185
4186 /* Set up to return from userspace. */
4187
4188 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4189 err |= __put_user(retcode_addr, &frame->pretcode);
4190
4191 /* moveq #,d0; trap #0 */
4192
4193 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4194 (long *)(frame->retcode));
4195
4196 if (err)
4197 goto give_sigsegv;
4198
4199 /* Set up to return from userspace */
4200
4201 env->aregs[7] = frame_addr;
4202 env->pc = ka->_sa_handler;
4203
4204 unlock_user_struct(frame, frame_addr, 1);
4205 return;
4206
4207give_sigsegv:
4208 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02004209 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02004210}
4211
Laurent Vivier71811552009-08-03 16:12:18 +02004212static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4213 CPUState *env)
4214{
Anthony Liguoric227f092009-10-01 16:12:16 -05004215 target_greg_t *gregs = uc->uc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02004216 int err;
4217
4218 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->uc_mcontext.version);
4219 err |= __put_user(env->dregs[0], &gregs[0]);
4220 err |= __put_user(env->dregs[1], &gregs[1]);
4221 err |= __put_user(env->dregs[2], &gregs[2]);
4222 err |= __put_user(env->dregs[3], &gregs[3]);
4223 err |= __put_user(env->dregs[4], &gregs[4]);
4224 err |= __put_user(env->dregs[5], &gregs[5]);
4225 err |= __put_user(env->dregs[6], &gregs[6]);
4226 err |= __put_user(env->dregs[7], &gregs[7]);
4227 err |= __put_user(env->aregs[0], &gregs[8]);
4228 err |= __put_user(env->aregs[1], &gregs[9]);
4229 err |= __put_user(env->aregs[2], &gregs[10]);
4230 err |= __put_user(env->aregs[3], &gregs[11]);
4231 err |= __put_user(env->aregs[4], &gregs[12]);
4232 err |= __put_user(env->aregs[5], &gregs[13]);
4233 err |= __put_user(env->aregs[6], &gregs[14]);
4234 err |= __put_user(env->aregs[7], &gregs[15]);
4235 err |= __put_user(env->pc, &gregs[16]);
4236 err |= __put_user(env->sr, &gregs[17]);
4237
4238 return err;
4239}
4240
4241static inline int target_rt_restore_ucontext(CPUState *env,
4242 struct target_ucontext *uc,
4243 int *pd0)
4244{
4245 int temp;
4246 int err;
Anthony Liguoric227f092009-10-01 16:12:16 -05004247 target_greg_t *gregs = uc->uc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02004248
4249 err = __get_user(temp, &uc->uc_mcontext.version);
4250 if (temp != TARGET_MCONTEXT_VERSION)
4251 goto badframe;
4252
4253 /* restore passed registers */
4254 err |= __get_user(env->dregs[0], &gregs[0]);
4255 err |= __get_user(env->dregs[1], &gregs[1]);
4256 err |= __get_user(env->dregs[2], &gregs[2]);
4257 err |= __get_user(env->dregs[3], &gregs[3]);
4258 err |= __get_user(env->dregs[4], &gregs[4]);
4259 err |= __get_user(env->dregs[5], &gregs[5]);
4260 err |= __get_user(env->dregs[6], &gregs[6]);
4261 err |= __get_user(env->dregs[7], &gregs[7]);
4262 err |= __get_user(env->aregs[0], &gregs[8]);
4263 err |= __get_user(env->aregs[1], &gregs[9]);
4264 err |= __get_user(env->aregs[2], &gregs[10]);
4265 err |= __get_user(env->aregs[3], &gregs[11]);
4266 err |= __get_user(env->aregs[4], &gregs[12]);
4267 err |= __get_user(env->aregs[5], &gregs[13]);
4268 err |= __get_user(env->aregs[6], &gregs[14]);
4269 err |= __get_user(env->aregs[7], &gregs[15]);
4270 err |= __get_user(env->pc, &gregs[16]);
4271 err |= __get_user(temp, &gregs[17]);
4272 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4273
4274 *pd0 = env->dregs[0];
4275 return err;
4276
4277badframe:
4278 return 1;
4279}
4280
Laurent Vivier492a8742009-08-03 16:12:17 +02004281static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004282 target_siginfo_t *info,
4283 target_sigset_t *set, CPUState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02004284{
Laurent Vivier71811552009-08-03 16:12:18 +02004285 struct target_rt_sigframe *frame;
4286 abi_ulong frame_addr;
4287 abi_ulong retcode_addr;
4288 abi_ulong info_addr;
4289 abi_ulong uc_addr;
4290 int err = 0;
4291 int i;
4292
4293 frame_addr = get_sigframe(ka, env, sizeof *frame);
4294 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4295 goto give_sigsegv;
4296
4297 err |= __put_user(sig, &frame->sig);
4298
4299 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4300 err |= __put_user(info_addr, &frame->pinfo);
4301
4302 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4303 err |= __put_user(uc_addr, &frame->puc);
4304
4305 err |= copy_siginfo_to_user(&frame->info, info);
4306
4307 /* Create the ucontext */
4308
4309 err |= __put_user(0, &frame->uc.uc_flags);
4310 err |= __put_user(0, &frame->uc.uc_link);
4311 err |= __put_user(target_sigaltstack_used.ss_sp,
4312 &frame->uc.uc_stack.ss_sp);
4313 err |= __put_user(sas_ss_flags(env->aregs[7]),
4314 &frame->uc.uc_stack.ss_flags);
4315 err |= __put_user(target_sigaltstack_used.ss_size,
4316 &frame->uc.uc_stack.ss_size);
4317 err |= target_rt_setup_ucontext(&frame->uc, env);
4318
4319 if (err)
4320 goto give_sigsegv;
4321
4322 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4323 if (__put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]))
4324 goto give_sigsegv;
4325 }
4326
4327 /* Set up to return from userspace. */
4328
4329 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4330 err |= __put_user(retcode_addr, &frame->pretcode);
4331
4332 /* moveq #,d0; notb d0; trap #0 */
4333
4334 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4335 (long *)(frame->retcode + 0));
4336 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
4337
4338 if (err)
4339 goto give_sigsegv;
4340
4341 /* Set up to return from userspace */
4342
4343 env->aregs[7] = frame_addr;
4344 env->pc = ka->_sa_handler;
4345
4346 unlock_user_struct(frame, frame_addr, 1);
4347 return;
4348
4349give_sigsegv:
4350 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02004351 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02004352}
4353
4354long do_sigreturn(CPUState *env)
4355{
4356 struct target_sigframe *frame;
4357 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05004358 target_sigset_t target_set;
Laurent Vivier492a8742009-08-03 16:12:17 +02004359 sigset_t set;
4360 int d0, i;
4361
4362 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4363 goto badframe;
4364
4365 /* set blocked signals */
4366
4367 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
4368 goto badframe;
4369
4370 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4371 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
4372 goto badframe;
4373 }
4374
4375 target_to_host_sigset_internal(&set, &target_set);
4376 sigprocmask(SIG_SETMASK, &set, NULL);
4377
4378 /* restore registers */
4379
4380 if (restore_sigcontext(env, &frame->sc, &d0))
4381 goto badframe;
4382
4383 unlock_user_struct(frame, frame_addr, 0);
4384 return d0;
4385
4386badframe:
4387 unlock_user_struct(frame, frame_addr, 0);
4388 force_sig(TARGET_SIGSEGV);
4389 return 0;
4390}
4391
4392long do_rt_sigreturn(CPUState *env)
4393{
Laurent Vivier71811552009-08-03 16:12:18 +02004394 struct target_rt_sigframe *frame;
4395 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05004396 target_sigset_t target_set;
Laurent Vivier71811552009-08-03 16:12:18 +02004397 sigset_t set;
4398 int d0;
4399
4400 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4401 goto badframe;
4402
4403 target_to_host_sigset_internal(&set, &target_set);
4404 sigprocmask(SIG_SETMASK, &set, NULL);
4405
4406 /* restore registers */
4407
4408 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
4409 goto badframe;
4410
4411 if (do_sigaltstack(frame_addr +
4412 offsetof(struct target_rt_sigframe, uc.uc_stack),
4413 0, get_sp_from_cpustate(env)) == -EFAULT)
4414 goto badframe;
4415
4416 unlock_user_struct(frame, frame_addr, 0);
4417 return d0;
4418
4419badframe:
4420 unlock_user_struct(frame, frame_addr, 0);
4421 force_sig(TARGET_SIGSEGV);
4422 return 0;
Laurent Vivier492a8742009-08-03 16:12:17 +02004423}
4424
Richard Henderson6049f4f2009-12-27 18:30:03 -08004425#elif defined(TARGET_ALPHA)
4426
4427struct target_sigcontext {
4428 abi_long sc_onstack;
4429 abi_long sc_mask;
4430 abi_long sc_pc;
4431 abi_long sc_ps;
4432 abi_long sc_regs[32];
4433 abi_long sc_ownedfp;
4434 abi_long sc_fpregs[32];
4435 abi_ulong sc_fpcr;
4436 abi_ulong sc_fp_control;
4437 abi_ulong sc_reserved1;
4438 abi_ulong sc_reserved2;
4439 abi_ulong sc_ssize;
4440 abi_ulong sc_sbase;
4441 abi_ulong sc_traparg_a0;
4442 abi_ulong sc_traparg_a1;
4443 abi_ulong sc_traparg_a2;
4444 abi_ulong sc_fp_trap_pc;
4445 abi_ulong sc_fp_trigger_sum;
4446 abi_ulong sc_fp_trigger_inst;
4447};
4448
4449struct target_ucontext {
4450 abi_ulong uc_flags;
4451 abi_ulong uc_link;
4452 abi_ulong uc_osf_sigmask;
4453 target_stack_t uc_stack;
4454 struct target_sigcontext uc_mcontext;
4455 target_sigset_t uc_sigmask;
4456};
4457
4458struct target_sigframe {
4459 struct target_sigcontext sc;
4460 unsigned int retcode[3];
4461};
4462
4463struct target_rt_sigframe {
4464 target_siginfo_t info;
4465 struct target_ucontext uc;
4466 unsigned int retcode[3];
4467};
4468
4469#define INSN_MOV_R30_R16 0x47fe0410
4470#define INSN_LDI_R0 0x201f0000
4471#define INSN_CALLSYS 0x00000083
4472
4473static int setup_sigcontext(struct target_sigcontext *sc, CPUState *env,
4474 abi_ulong frame_addr, target_sigset_t *set)
4475{
4476 int i, err = 0;
4477
4478 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
4479 err |= __put_user(set->sig[0], &sc->sc_mask);
4480 err |= __put_user(env->pc, &sc->sc_pc);
4481 err |= __put_user(8, &sc->sc_ps);
4482
4483 for (i = 0; i < 31; ++i) {
4484 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
4485 }
4486 err |= __put_user(0, &sc->sc_regs[31]);
4487
4488 for (i = 0; i < 31; ++i) {
4489 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
4490 }
4491 err |= __put_user(0, &sc->sc_fpregs[31]);
4492 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
4493
4494 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
4495 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
4496 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
4497
4498 return err;
4499}
4500
4501static int restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
4502{
4503 uint64_t fpcr;
4504 int i, err = 0;
4505
4506 err |= __get_user(env->pc, &sc->sc_pc);
4507
4508 for (i = 0; i < 31; ++i) {
4509 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
4510 }
4511 for (i = 0; i < 31; ++i) {
4512 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
4513 }
4514
4515 err |= __get_user(fpcr, &sc->sc_fpcr);
4516 cpu_alpha_store_fpcr(env, fpcr);
4517
4518 return err;
4519}
4520
4521static inline abi_ulong get_sigframe(struct target_sigaction *sa,
4522 CPUState *env, unsigned long framesize)
4523{
4524 abi_ulong sp = env->ir[IR_SP];
4525
4526 /* This is the X/Open sanctioned signal stack switching. */
4527 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
4528 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4529 }
4530 return (sp - framesize) & -32;
4531}
4532
4533static void setup_frame(int sig, struct target_sigaction *ka,
4534 target_sigset_t *set, CPUState *env)
4535{
4536 abi_ulong frame_addr, r26;
4537 struct target_sigframe *frame;
4538 int err = 0;
4539
4540 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4541 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4542 goto give_sigsegv;
4543 }
4544
4545 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
4546
4547 if (ka->sa_restorer) {
4548 r26 = ka->sa_restorer;
4549 } else {
4550 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
4551 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
4552 &frame->retcode[1]);
4553 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
4554 /* imb() */
4555 r26 = frame_addr;
4556 }
4557
4558 unlock_user_struct(frame, frame_addr, 1);
4559
4560 if (err) {
4561 give_sigsegv:
4562 if (sig == TARGET_SIGSEGV) {
4563 ka->_sa_handler = TARGET_SIG_DFL;
4564 }
4565 force_sig(TARGET_SIGSEGV);
4566 }
4567
4568 env->ir[IR_RA] = r26;
4569 env->ir[IR_PV] = env->pc = ka->_sa_handler;
4570 env->ir[IR_A0] = sig;
4571 env->ir[IR_A1] = 0;
4572 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
4573 env->ir[IR_SP] = frame_addr;
4574}
4575
4576static void setup_rt_frame(int sig, struct target_sigaction *ka,
4577 target_siginfo_t *info,
4578 target_sigset_t *set, CPUState *env)
4579{
4580 abi_ulong frame_addr, r26;
4581 struct target_rt_sigframe *frame;
4582 int i, err = 0;
4583
4584 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4585 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4586 goto give_sigsegv;
4587 }
4588
4589 err |= copy_siginfo_to_user(&frame->info, info);
4590
4591 err |= __put_user(0, &frame->uc.uc_flags);
4592 err |= __put_user(0, &frame->uc.uc_link);
4593 err |= __put_user(set->sig[0], &frame->uc.uc_osf_sigmask);
4594 err |= __put_user(target_sigaltstack_used.ss_sp,
4595 &frame->uc.uc_stack.ss_sp);
4596 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
4597 &frame->uc.uc_stack.ss_flags);
4598 err |= __put_user(target_sigaltstack_used.ss_size,
4599 &frame->uc.uc_stack.ss_size);
4600 err |= setup_sigcontext(&frame->uc.uc_mcontext, env, frame_addr, set);
4601 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
4602 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
4603 }
4604
4605 if (ka->sa_restorer) {
4606 r26 = ka->sa_restorer;
4607 } else {
4608 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
4609 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
4610 &frame->retcode[1]);
4611 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
4612 /* imb(); */
4613 r26 = frame_addr;
4614 }
4615
4616 if (err) {
4617 give_sigsegv:
4618 if (sig == TARGET_SIGSEGV) {
4619 ka->_sa_handler = TARGET_SIG_DFL;
4620 }
4621 force_sig(TARGET_SIGSEGV);
4622 }
4623
4624 env->ir[IR_RA] = r26;
4625 env->ir[IR_PV] = env->pc = ka->_sa_handler;
4626 env->ir[IR_A0] = sig;
4627 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
4628 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
4629 env->ir[IR_SP] = frame_addr;
4630}
4631
4632long do_sigreturn(CPUState *env)
4633{
4634 struct target_sigcontext *sc;
4635 abi_ulong sc_addr = env->ir[IR_A0];
4636 target_sigset_t target_set;
4637 sigset_t set;
4638
4639 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
4640 goto badframe;
4641 }
4642
4643 target_sigemptyset(&target_set);
4644 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
4645 goto badframe;
4646 }
4647
4648 target_to_host_sigset_internal(&set, &target_set);
4649 sigprocmask(SIG_SETMASK, &set, NULL);
4650
4651 if (restore_sigcontext(env, sc)) {
4652 goto badframe;
4653 }
4654 unlock_user_struct(sc, sc_addr, 0);
4655 return env->ir[IR_V0];
4656
4657 badframe:
4658 unlock_user_struct(sc, sc_addr, 0);
4659 force_sig(TARGET_SIGSEGV);
4660}
4661
4662long do_rt_sigreturn(CPUState *env)
4663{
4664 abi_ulong frame_addr = env->ir[IR_A0];
4665 struct target_rt_sigframe *frame;
4666 sigset_t set;
4667
4668 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4669 goto badframe;
4670 }
4671 target_to_host_sigset(&set, &frame->uc.uc_sigmask);
4672 sigprocmask(SIG_SETMASK, &set, NULL);
4673
4674 if (restore_sigcontext(env, &frame->uc.uc_mcontext)) {
4675 goto badframe;
4676 }
4677 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
4678 uc.uc_stack),
4679 0, env->ir[IR_SP]) == -EFAULT) {
4680 goto badframe;
4681 }
4682
4683 unlock_user_struct(frame, frame_addr, 0);
4684 return env->ir[IR_V0];
4685
4686
4687 badframe:
4688 unlock_user_struct(frame, frame_addr, 0);
4689 force_sig(TARGET_SIGSEGV);
4690}
4691
bellardb346ff42003-06-15 20:05:50 +00004692#else
4693
pbrook624f7972008-05-31 16:11:38 +00004694static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004695 target_sigset_t *set, CPUState *env)
bellardb346ff42003-06-15 20:05:50 +00004696{
4697 fprintf(stderr, "setup_frame: not implemented\n");
4698}
4699
pbrook624f7972008-05-31 16:11:38 +00004700static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004701 target_siginfo_t *info,
4702 target_sigset_t *set, CPUState *env)
bellardb346ff42003-06-15 20:05:50 +00004703{
4704 fprintf(stderr, "setup_rt_frame: not implemented\n");
4705}
4706
4707long do_sigreturn(CPUState *env)
4708{
4709 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00004710 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00004711}
4712
4713long do_rt_sigreturn(CPUState *env)
4714{
4715 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00004716 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00004717}
4718
bellard66fb9762003-03-23 01:06:05 +00004719#endif
4720
pbrook624f7972008-05-31 16:11:38 +00004721void process_pending_signals(CPUState *cpu_env)
bellard66fb9762003-03-23 01:06:05 +00004722{
4723 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00004724 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00004725 sigset_t set, old_set;
Anthony Liguoric227f092009-10-01 16:12:16 -05004726 target_sigset_t target_old_set;
pbrook624f7972008-05-31 16:11:38 +00004727 struct emulated_sigtable *k;
4728 struct target_sigaction *sa;
bellard66fb9762003-03-23 01:06:05 +00004729 struct sigqueue *q;
pbrook624f7972008-05-31 16:11:38 +00004730 TaskState *ts = cpu_env->opaque;
ths3b46e622007-09-17 08:09:54 +00004731
pbrook624f7972008-05-31 16:11:38 +00004732 if (!ts->signal_pending)
bellard31e31b82003-02-18 22:55:36 +00004733 return;
4734
pbrook624f7972008-05-31 16:11:38 +00004735 /* FIXME: This is not threadsafe. */
4736 k = ts->sigtab;
bellard66fb9762003-03-23 01:06:05 +00004737 for(sig = 1; sig <= TARGET_NSIG; sig++) {
4738 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00004739 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00004740 k++;
bellard31e31b82003-02-18 22:55:36 +00004741 }
4742 /* if no signal is pending, just return */
pbrook624f7972008-05-31 16:11:38 +00004743 ts->signal_pending = 0;
bellard31e31b82003-02-18 22:55:36 +00004744 return;
bellard66fb9762003-03-23 01:06:05 +00004745
bellard31e31b82003-02-18 22:55:36 +00004746 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00004747#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00004748 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00004749#endif
4750 /* dequeue signal */
4751 q = k->first;
4752 k->first = q->next;
4753 if (!k->first)
4754 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00004755
bellard1fddef42005-04-17 19:16:13 +00004756 sig = gdb_handlesig (cpu_env, sig);
4757 if (!sig) {
aurel32ca587a82008-12-18 22:44:13 +00004758 sa = NULL;
4759 handler = TARGET_SIG_IGN;
4760 } else {
4761 sa = &sigact_table[sig - 1];
4762 handler = sa->_sa_handler;
bellard1fddef42005-04-17 19:16:13 +00004763 }
bellard66fb9762003-03-23 01:06:05 +00004764
bellard66fb9762003-03-23 01:06:05 +00004765 if (handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +00004766 /* default handler : ignore some signal. The other are job control or fatal */
4767 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
4768 kill(getpid(),SIGSTOP);
4769 } else if (sig != TARGET_SIGCHLD &&
4770 sig != TARGET_SIGURG &&
4771 sig != TARGET_SIGWINCH &&
4772 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +00004773 force_sig(sig);
4774 }
4775 } else if (handler == TARGET_SIG_IGN) {
4776 /* ignore sig */
4777 } else if (handler == TARGET_SIG_ERR) {
4778 force_sig(sig);
4779 } else {
bellard9de5e442003-03-23 16:49:39 +00004780 /* compute the blocked signals during the handler execution */
pbrook624f7972008-05-31 16:11:38 +00004781 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +00004782 /* SA_NODEFER indicates that the current signal should not be
4783 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +00004784 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +00004785 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00004786
bellard9de5e442003-03-23 16:49:39 +00004787 /* block signals in the handler using Linux */
4788 sigprocmask(SIG_BLOCK, &set, &old_set);
4789 /* save the previous blocked signal state to restore it at the
4790 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00004791 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00004792
bellardbc8a22c2003-03-30 21:02:40 +00004793 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00004794#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00004795 {
4796 CPUX86State *env = cpu_env;
4797 if (env->eflags & VM_MASK)
4798 save_v86_state(env);
4799 }
4800#endif
bellard9de5e442003-03-23 16:49:39 +00004801 /* prepare the stack frame of the virtual CPU */
pbrook624f7972008-05-31 16:11:38 +00004802 if (sa->sa_flags & TARGET_SA_SIGINFO)
4803 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00004804 else
pbrook624f7972008-05-31 16:11:38 +00004805 setup_frame(sig, sa, &target_old_set, cpu_env);
4806 if (sa->sa_flags & TARGET_SA_RESETHAND)
4807 sa->_sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00004808 }
bellard66fb9762003-03-23 01:06:05 +00004809 if (q != &k->info)
pbrook624f7972008-05-31 16:11:38 +00004810 free_sigqueue(cpu_env, q);
bellard31e31b82003-02-18 22:55:36 +00004811}