blob: 46491674fef36f7a6d29581a3be615cc6c9c674e [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 */
414 assert(0);
415
bellard66fb9762003-03-23 01:06:05 +0000416}
417
bellard9de5e442003-03-23 16:49:39 +0000418/* queue a signal so that it will be send to the virtual CPU as soon
419 as possible */
Anthony Liguoric227f092009-10-01 16:12:16 -0500420int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000421{
pbrook624f7972008-05-31 16:11:38 +0000422 TaskState *ts = env->opaque;
423 struct emulated_sigtable *k;
bellard9de5e442003-03-23 16:49:39 +0000424 struct sigqueue *q, **pq;
blueswir1992f48a2007-10-14 16:27:31 +0000425 abi_ulong handler;
aurel32ca587a82008-12-18 22:44:13 +0000426 int queue;
bellard66fb9762003-03-23 01:06:05 +0000427
bellard9de5e442003-03-23 16:49:39 +0000428#if defined(DEBUG_SIGNAL)
ths5fafdf22007-09-16 21:08:06 +0000429 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000430 sig);
bellard66fb9762003-03-23 01:06:05 +0000431#endif
pbrook624f7972008-05-31 16:11:38 +0000432 k = &ts->sigtab[sig - 1];
aurel32ca587a82008-12-18 22:44:13 +0000433 queue = gdb_queuesig ();
pbrook624f7972008-05-31 16:11:38 +0000434 handler = sigact_table[sig - 1]._sa_handler;
aurel32ca587a82008-12-18 22:44:13 +0000435 if (!queue && handler == TARGET_SIG_DFL) {
ths60b19692008-11-27 15:47:15 +0000436 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
437 kill(getpid(),SIGSTOP);
438 return 0;
439 } else
bellard66fb9762003-03-23 01:06:05 +0000440 /* default handler : ignore some signal. The other are fatal */
ths5fafdf22007-09-16 21:08:06 +0000441 if (sig != TARGET_SIGCHLD &&
442 sig != TARGET_SIGURG &&
ths60b19692008-11-27 15:47:15 +0000443 sig != TARGET_SIGWINCH &&
444 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +0000445 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000446 } else {
447 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000448 }
aurel32ca587a82008-12-18 22:44:13 +0000449 } else if (!queue && handler == TARGET_SIG_IGN) {
bellard66fb9762003-03-23 01:06:05 +0000450 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000451 return 0;
aurel32ca587a82008-12-18 22:44:13 +0000452 } else if (!queue && handler == TARGET_SIG_ERR) {
bellard66fb9762003-03-23 01:06:05 +0000453 force_sig(sig);
454 } else {
bellard9de5e442003-03-23 16:49:39 +0000455 pq = &k->first;
456 if (sig < TARGET_SIGRTMIN) {
457 /* if non real time signal, we queue exactly one signal */
458 if (!k->pending)
459 q = &k->info;
460 else
461 return 0;
462 } else {
463 if (!k->pending) {
464 /* first signal */
465 q = &k->info;
466 } else {
pbrook624f7972008-05-31 16:11:38 +0000467 q = alloc_sigqueue(env);
bellard9de5e442003-03-23 16:49:39 +0000468 if (!q)
469 return -EAGAIN;
470 while (*pq != NULL)
471 pq = &(*pq)->next;
472 }
473 }
474 *pq = q;
475 q->info = *info;
476 q->next = NULL;
477 k->pending = 1;
478 /* signal that a new signal is pending */
pbrook624f7972008-05-31 16:11:38 +0000479 ts->signal_pending = 1;
bellard9de5e442003-03-23 16:49:39 +0000480 return 1; /* indicates that the signal was queued */
481 }
482}
483
ths5fafdf22007-09-16 21:08:06 +0000484static void host_signal_handler(int host_signum, siginfo_t *info,
bellard9de5e442003-03-23 16:49:39 +0000485 void *puc)
486{
487 int sig;
Anthony Liguoric227f092009-10-01 16:12:16 -0500488 target_siginfo_t tinfo;
bellard9de5e442003-03-23 16:49:39 +0000489
490 /* the CPU emulator uses some host signals to detect exceptions,
aurel32eaa449b2009-01-03 13:14:52 +0000491 we forward to it some signals */
aurel32ca587a82008-12-18 22:44:13 +0000492 if ((host_signum == SIGSEGV || host_signum == SIGBUS)
aurel32eaa449b2009-01-03 13:14:52 +0000493 && info->si_code > 0) {
bellardb346ff42003-06-15 20:05:50 +0000494 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000495 return;
496 }
497
498 /* get target signal number */
499 sig = host_to_target_signal(host_signum);
500 if (sig < 1 || sig > TARGET_NSIG)
501 return;
502#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000503 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000504#endif
505 host_to_target_siginfo_noswap(&tinfo, info);
pbrookd5975362008-06-07 20:50:51 +0000506 if (queue_signal(thread_env, sig, &tinfo) == 1) {
bellard9de5e442003-03-23 16:49:39 +0000507 /* interrupt the virtual CPU as soon as possible */
aurel323098dba2009-03-07 21:28:24 +0000508 cpu_exit(thread_env);
bellard66fb9762003-03-23 01:06:05 +0000509 }
bellard31e31b82003-02-18 22:55:36 +0000510}
511
ths0da46a62007-10-20 20:23:07 +0000512/* do_sigaltstack() returns target values and errnos. */
bellard579a97f2007-11-11 14:26:47 +0000513/* compare linux/kernel/signal.c:do_sigaltstack() */
514abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
thsa04e1342007-09-27 13:57:58 +0000515{
516 int ret;
517 struct target_sigaltstack oss;
518
519 /* XXX: test errors */
bellard579a97f2007-11-11 14:26:47 +0000520 if(uoss_addr)
thsa04e1342007-09-27 13:57:58 +0000521 {
522 __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
523 __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
524 __put_user(sas_ss_flags(sp), &oss.ss_flags);
525 }
526
bellard579a97f2007-11-11 14:26:47 +0000527 if(uss_addr)
thsa04e1342007-09-27 13:57:58 +0000528 {
bellard579a97f2007-11-11 14:26:47 +0000529 struct target_sigaltstack *uss;
530 struct target_sigaltstack ss;
thsa04e1342007-09-27 13:57:58 +0000531
ths0da46a62007-10-20 20:23:07 +0000532 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000533 if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
thsa04e1342007-09-27 13:57:58 +0000534 || __get_user(ss.ss_sp, &uss->ss_sp)
535 || __get_user(ss.ss_size, &uss->ss_size)
536 || __get_user(ss.ss_flags, &uss->ss_flags))
537 goto out;
bellard579a97f2007-11-11 14:26:47 +0000538 unlock_user_struct(uss, uss_addr, 0);
thsa04e1342007-09-27 13:57:58 +0000539
ths0da46a62007-10-20 20:23:07 +0000540 ret = -TARGET_EPERM;
thsa04e1342007-09-27 13:57:58 +0000541 if (on_sig_stack(sp))
542 goto out;
543
ths0da46a62007-10-20 20:23:07 +0000544 ret = -TARGET_EINVAL;
thsa04e1342007-09-27 13:57:58 +0000545 if (ss.ss_flags != TARGET_SS_DISABLE
546 && ss.ss_flags != TARGET_SS_ONSTACK
547 && ss.ss_flags != 0)
548 goto out;
549
550 if (ss.ss_flags == TARGET_SS_DISABLE) {
551 ss.ss_size = 0;
552 ss.ss_sp = 0;
553 } else {
ths0da46a62007-10-20 20:23:07 +0000554 ret = -TARGET_ENOMEM;
thsa04e1342007-09-27 13:57:58 +0000555 if (ss.ss_size < MINSIGSTKSZ)
556 goto out;
557 }
558
559 target_sigaltstack_used.ss_sp = ss.ss_sp;
560 target_sigaltstack_used.ss_size = ss.ss_size;
561 }
562
bellard579a97f2007-11-11 14:26:47 +0000563 if (uoss_addr) {
ths0da46a62007-10-20 20:23:07 +0000564 ret = -TARGET_EFAULT;
bellard579a97f2007-11-11 14:26:47 +0000565 if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
thsa04e1342007-09-27 13:57:58 +0000566 goto out;
thsa04e1342007-09-27 13:57:58 +0000567 }
568
569 ret = 0;
570out:
571 return ret;
572}
573
ths0da46a62007-10-20 20:23:07 +0000574/* do_sigaction() return host values and errnos */
bellard66fb9762003-03-23 01:06:05 +0000575int do_sigaction(int sig, const struct target_sigaction *act,
576 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000577{
pbrook624f7972008-05-31 16:11:38 +0000578 struct target_sigaction *k;
bellard773b93e2004-01-04 17:15:59 +0000579 struct sigaction act1;
580 int host_sig;
ths0da46a62007-10-20 20:23:07 +0000581 int ret = 0;
bellard31e31b82003-02-18 22:55:36 +0000582
ths2a913eb2008-11-27 15:46:25 +0000583 if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
bellard66fb9762003-03-23 01:06:05 +0000584 return -EINVAL;
585 k = &sigact_table[sig - 1];
bellard773b93e2004-01-04 17:15:59 +0000586#if defined(DEBUG_SIGNAL)
Blue Swirl0bf9e312009-07-20 17:19:25 +0000587 fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
588 sig, act, oact);
bellard66fb9762003-03-23 01:06:05 +0000589#endif
590 if (oact) {
pbrook624f7972008-05-31 16:11:38 +0000591 oact->_sa_handler = tswapl(k->_sa_handler);
592 oact->sa_flags = tswapl(k->sa_flags);
ths388bb212007-05-13 13:58:00 +0000593#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000594 oact->sa_restorer = tswapl(k->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000595#endif
pbrook624f7972008-05-31 16:11:38 +0000596 oact->sa_mask = k->sa_mask;
bellard66fb9762003-03-23 01:06:05 +0000597 }
598 if (act) {
pbrook624f7972008-05-31 16:11:38 +0000599 /* FIXME: This is not threadsafe. */
600 k->_sa_handler = tswapl(act->_sa_handler);
601 k->sa_flags = tswapl(act->sa_flags);
ths388bb212007-05-13 13:58:00 +0000602#if !defined(TARGET_MIPS)
pbrook624f7972008-05-31 16:11:38 +0000603 k->sa_restorer = tswapl(act->sa_restorer);
ths388bb212007-05-13 13:58:00 +0000604#endif
pbrook624f7972008-05-31 16:11:38 +0000605 k->sa_mask = act->sa_mask;
bellard773b93e2004-01-04 17:15:59 +0000606
607 /* we update the host linux signal state */
608 host_sig = target_to_host_signal(sig);
609 if (host_sig != SIGSEGV && host_sig != SIGBUS) {
610 sigfillset(&act1.sa_mask);
611 act1.sa_flags = SA_SIGINFO;
pbrook624f7972008-05-31 16:11:38 +0000612 if (k->sa_flags & TARGET_SA_RESTART)
bellard773b93e2004-01-04 17:15:59 +0000613 act1.sa_flags |= SA_RESTART;
614 /* NOTE: it is important to update the host kernel signal
615 ignore state to avoid getting unexpected interrupted
616 syscalls */
pbrook624f7972008-05-31 16:11:38 +0000617 if (k->_sa_handler == TARGET_SIG_IGN) {
bellard773b93e2004-01-04 17:15:59 +0000618 act1.sa_sigaction = (void *)SIG_IGN;
pbrook624f7972008-05-31 16:11:38 +0000619 } else if (k->_sa_handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +0000620 if (fatal_signal (sig))
621 act1.sa_sigaction = host_signal_handler;
622 else
623 act1.sa_sigaction = (void *)SIG_DFL;
bellard773b93e2004-01-04 17:15:59 +0000624 } else {
625 act1.sa_sigaction = host_signal_handler;
626 }
ths0da46a62007-10-20 20:23:07 +0000627 ret = sigaction(host_sig, &act1, NULL);
bellard773b93e2004-01-04 17:15:59 +0000628 }
bellard66fb9762003-03-23 01:06:05 +0000629 }
ths0da46a62007-10-20 20:23:07 +0000630 return ret;
bellard66fb9762003-03-23 01:06:05 +0000631}
bellard31e31b82003-02-18 22:55:36 +0000632
Anthony Liguoric227f092009-10-01 16:12:16 -0500633static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
634 const target_siginfo_t *info)
bellard43fff232003-07-09 19:31:39 +0000635{
636 tswap_siginfo(tinfo, info);
637 return 0;
638}
639
thsc3b5bc82007-12-02 06:31:25 +0000640static inline int current_exec_domain_sig(int sig)
641{
642 return /* current->exec_domain && current->exec_domain->signal_invmap
643 && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
644}
645
bellard459a4012007-11-11 19:45:10 +0000646#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
bellard66fb9762003-03-23 01:06:05 +0000647
648/* from the Linux kernel */
649
650struct target_fpreg {
651 uint16_t significand[4];
652 uint16_t exponent;
653};
654
655struct target_fpxreg {
656 uint16_t significand[4];
657 uint16_t exponent;
658 uint16_t padding[3];
659};
660
661struct target_xmmreg {
blueswir1992f48a2007-10-14 16:27:31 +0000662 abi_ulong element[4];
bellard66fb9762003-03-23 01:06:05 +0000663};
664
665struct target_fpstate {
666 /* Regular FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000667 abi_ulong cw;
668 abi_ulong sw;
669 abi_ulong tag;
670 abi_ulong ipoff;
671 abi_ulong cssel;
672 abi_ulong dataoff;
673 abi_ulong datasel;
bellard66fb9762003-03-23 01:06:05 +0000674 struct target_fpreg _st[8];
675 uint16_t status;
676 uint16_t magic; /* 0xffff = regular FPU data only */
677
678 /* FXSR FPU environment */
blueswir1992f48a2007-10-14 16:27:31 +0000679 abi_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
680 abi_ulong mxcsr;
681 abi_ulong reserved;
bellard66fb9762003-03-23 01:06:05 +0000682 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
683 struct target_xmmreg _xmm[8];
blueswir1992f48a2007-10-14 16:27:31 +0000684 abi_ulong padding[56];
bellard66fb9762003-03-23 01:06:05 +0000685};
686
687#define X86_FXSR_MAGIC 0x0000
688
689struct target_sigcontext {
690 uint16_t gs, __gsh;
691 uint16_t fs, __fsh;
692 uint16_t es, __esh;
693 uint16_t ds, __dsh;
blueswir1992f48a2007-10-14 16:27:31 +0000694 abi_ulong edi;
695 abi_ulong esi;
696 abi_ulong ebp;
697 abi_ulong esp;
698 abi_ulong ebx;
699 abi_ulong edx;
700 abi_ulong ecx;
701 abi_ulong eax;
702 abi_ulong trapno;
703 abi_ulong err;
704 abi_ulong eip;
bellard66fb9762003-03-23 01:06:05 +0000705 uint16_t cs, __csh;
blueswir1992f48a2007-10-14 16:27:31 +0000706 abi_ulong eflags;
707 abi_ulong esp_at_signal;
bellard66fb9762003-03-23 01:06:05 +0000708 uint16_t ss, __ssh;
blueswir1992f48a2007-10-14 16:27:31 +0000709 abi_ulong fpstate; /* pointer */
710 abi_ulong oldmask;
711 abi_ulong cr2;
bellard66fb9762003-03-23 01:06:05 +0000712};
713
bellard66fb9762003-03-23 01:06:05 +0000714struct target_ucontext {
blueswir1992f48a2007-10-14 16:27:31 +0000715 abi_ulong tuc_flags;
716 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -0500717 target_stack_t tuc_stack;
bellardb8076a72005-04-07 22:20:31 +0000718 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -0500719 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard66fb9762003-03-23 01:06:05 +0000720};
721
722struct sigframe
723{
blueswir1992f48a2007-10-14 16:27:31 +0000724 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000725 int sig;
726 struct target_sigcontext sc;
727 struct target_fpstate fpstate;
blueswir1992f48a2007-10-14 16:27:31 +0000728 abi_ulong extramask[TARGET_NSIG_WORDS-1];
bellard66fb9762003-03-23 01:06:05 +0000729 char retcode[8];
730};
731
732struct rt_sigframe
733{
blueswir1992f48a2007-10-14 16:27:31 +0000734 abi_ulong pretcode;
bellard66fb9762003-03-23 01:06:05 +0000735 int sig;
blueswir1992f48a2007-10-14 16:27:31 +0000736 abi_ulong pinfo;
737 abi_ulong puc;
bellard66fb9762003-03-23 01:06:05 +0000738 struct target_siginfo info;
739 struct target_ucontext uc;
740 struct target_fpstate fpstate;
741 char retcode[8];
742};
743
744/*
745 * Set up a signal frame.
746 */
747
bellard66fb9762003-03-23 01:06:05 +0000748/* XXX: save x87 state */
749static int
750setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
bellard28be6232007-11-11 22:23:38 +0000751 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
bellard66fb9762003-03-23 01:06:05 +0000752{
753 int err = 0;
bellard775b58d2007-11-11 16:22:17 +0000754 uint16_t magic;
bellard66fb9762003-03-23 01:06:05 +0000755
bellard579a97f2007-11-11 14:26:47 +0000756 /* already locked in setup_frame() */
bellarda52c7572003-06-21 13:14:12 +0000757 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
758 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
759 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
760 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000761 err |= __put_user(env->regs[R_EDI], &sc->edi);
762 err |= __put_user(env->regs[R_ESI], &sc->esi);
763 err |= __put_user(env->regs[R_EBP], &sc->ebp);
764 err |= __put_user(env->regs[R_ESP], &sc->esp);
765 err |= __put_user(env->regs[R_EBX], &sc->ebx);
766 err |= __put_user(env->regs[R_EDX], &sc->edx);
767 err |= __put_user(env->regs[R_ECX], &sc->ecx);
768 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000769 err |= __put_user(env->exception_index, &sc->trapno);
770 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000771 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000772 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000773 err |= __put_user(env->eflags, &sc->eflags);
774 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000775 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000776
bellard28be6232007-11-11 22:23:38 +0000777 cpu_x86_fsave(env, fpstate_addr, 1);
bellarded2dcdf2003-05-29 20:06:27 +0000778 fpstate->status = fpstate->sw;
bellard775b58d2007-11-11 16:22:17 +0000779 magic = 0xffff;
780 err |= __put_user(magic, &fpstate->magic);
bellard28be6232007-11-11 22:23:38 +0000781 err |= __put_user(fpstate_addr, &sc->fpstate);
bellarded2dcdf2003-05-29 20:06:27 +0000782
bellard66fb9762003-03-23 01:06:05 +0000783 /* non-iBCS2 extensions.. */
784 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000785 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000786 return err;
787}
788
789/*
790 * Determine which stack to use..
791 */
792
bellard579a97f2007-11-11 14:26:47 +0000793static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +0000794get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
bellard66fb9762003-03-23 01:06:05 +0000795{
796 unsigned long esp;
797
798 /* Default to using normal stack */
799 esp = env->regs[R_ESP];
bellard66fb9762003-03-23 01:06:05 +0000800 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +0000801 if (ka->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +0000802 if (sas_ss_flags(esp) == 0)
803 esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
804 }
bellard66fb9762003-03-23 01:06:05 +0000805
806 /* This is the legacy signal stack switching. */
ths5fafdf22007-09-16 21:08:06 +0000807 else
bellarda52c7572003-06-21 13:14:12 +0000808 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
pbrook624f7972008-05-31 16:11:38 +0000809 !(ka->sa_flags & TARGET_SA_RESTORER) &&
810 ka->sa_restorer) {
811 esp = (unsigned long) ka->sa_restorer;
bellarda52c7572003-06-21 13:14:12 +0000812 }
bellard579a97f2007-11-11 14:26:47 +0000813 return (esp - frame_size) & -8ul;
bellard66fb9762003-03-23 01:06:05 +0000814}
815
bellard579a97f2007-11-11 14:26:47 +0000816/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +0000817static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500818 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000819{
bellard579a97f2007-11-11 14:26:47 +0000820 abi_ulong frame_addr;
bellard66fb9762003-03-23 01:06:05 +0000821 struct sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000822 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000823
bellard579a97f2007-11-11 14:26:47 +0000824 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000825
bellard579a97f2007-11-11 14:26:47 +0000826 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000827 goto give_sigsegv;
bellard579a97f2007-11-11 14:26:47 +0000828
thsc3b5bc82007-12-02 06:31:25 +0000829 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000830 &frame->sig);
831 if (err)
832 goto give_sigsegv;
833
bellard28be6232007-11-11 22:23:38 +0000834 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
835 frame_addr + offsetof(struct sigframe, fpstate));
bellard66fb9762003-03-23 01:06:05 +0000836 if (err)
837 goto give_sigsegv;
838
bellard92319442004-06-19 16:58:13 +0000839 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
840 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
841 goto give_sigsegv;
842 }
bellard66fb9762003-03-23 01:06:05 +0000843
844 /* Set up to return from userspace. If provided, use a stub
845 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000846 if (ka->sa_flags & TARGET_SA_RESTORER) {
847 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000848 } else {
bellard775b58d2007-11-11 16:22:17 +0000849 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000850 abi_ulong retcode_addr;
851 retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
852 err |= __put_user(retcode_addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000853 /* This is popl %eax ; movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000854 val16 = 0xb858;
855 err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000856 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
bellard775b58d2007-11-11 16:22:17 +0000857 val16 = 0x80cd;
858 err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
bellard66fb9762003-03-23 01:06:05 +0000859 }
860
861 if (err)
862 goto give_sigsegv;
863
864 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000865 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000866 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000867
868 cpu_x86_load_seg(env, R_DS, __USER_DS);
869 cpu_x86_load_seg(env, R_ES, __USER_DS);
870 cpu_x86_load_seg(env, R_SS, __USER_DS);
871 cpu_x86_load_seg(env, R_CS, __USER_CS);
872 env->eflags &= ~TF_MASK;
873
bellard579a97f2007-11-11 14:26:47 +0000874 unlock_user_struct(frame, frame_addr, 1);
875
bellard66fb9762003-03-23 01:06:05 +0000876 return;
877
878give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000879 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000880 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000881 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000882 force_sig(TARGET_SIGSEGV /* , current */);
883}
884
bellard579a97f2007-11-11 14:26:47 +0000885/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +0000886static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -0500887 target_siginfo_t *info,
888 target_sigset_t *set, CPUX86State *env)
bellard66fb9762003-03-23 01:06:05 +0000889{
bellard28be6232007-11-11 22:23:38 +0000890 abi_ulong frame_addr, addr;
bellard66fb9762003-03-23 01:06:05 +0000891 struct rt_sigframe *frame;
bellard92319442004-06-19 16:58:13 +0000892 int i, err = 0;
bellard66fb9762003-03-23 01:06:05 +0000893
bellard579a97f2007-11-11 14:26:47 +0000894 frame_addr = get_sigframe(ka, env, sizeof(*frame));
bellard66fb9762003-03-23 01:06:05 +0000895
bellard579a97f2007-11-11 14:26:47 +0000896 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard66fb9762003-03-23 01:06:05 +0000897 goto give_sigsegv;
bellard66fb9762003-03-23 01:06:05 +0000898
thsc3b5bc82007-12-02 06:31:25 +0000899 err |= __put_user(current_exec_domain_sig(sig),
bellard66fb9762003-03-23 01:06:05 +0000900 &frame->sig);
bellard28be6232007-11-11 22:23:38 +0000901 addr = frame_addr + offsetof(struct rt_sigframe, info);
902 err |= __put_user(addr, &frame->pinfo);
903 addr = frame_addr + offsetof(struct rt_sigframe, uc);
904 err |= __put_user(addr, &frame->puc);
bellard66fb9762003-03-23 01:06:05 +0000905 err |= copy_siginfo_to_user(&frame->info, info);
906 if (err)
907 goto give_sigsegv;
908
909 /* Create the ucontext. */
bellardb8076a72005-04-07 22:20:31 +0000910 err |= __put_user(0, &frame->uc.tuc_flags);
911 err |= __put_user(0, &frame->uc.tuc_link);
thsa04e1342007-09-27 13:57:58 +0000912 err |= __put_user(target_sigaltstack_used.ss_sp,
bellardb8076a72005-04-07 22:20:31 +0000913 &frame->uc.tuc_stack.ss_sp);
thsa04e1342007-09-27 13:57:58 +0000914 err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
bellardb8076a72005-04-07 22:20:31 +0000915 &frame->uc.tuc_stack.ss_flags);
thsa04e1342007-09-27 13:57:58 +0000916 err |= __put_user(target_sigaltstack_used.ss_size,
bellardb8076a72005-04-07 22:20:31 +0000917 &frame->uc.tuc_stack.ss_size);
918 err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
bellard28be6232007-11-11 22:23:38 +0000919 env, set->sig[0],
920 frame_addr + offsetof(struct rt_sigframe, fpstate));
bellard92319442004-06-19 16:58:13 +0000921 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +0000922 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard92319442004-06-19 16:58:13 +0000923 goto give_sigsegv;
924 }
bellard66fb9762003-03-23 01:06:05 +0000925
926 /* Set up to return from userspace. If provided, use a stub
927 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +0000928 if (ka->sa_flags & TARGET_SA_RESTORER) {
929 err |= __put_user(ka->sa_restorer, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000930 } else {
bellard775b58d2007-11-11 16:22:17 +0000931 uint16_t val16;
bellard28be6232007-11-11 22:23:38 +0000932 addr = frame_addr + offsetof(struct rt_sigframe, retcode);
933 err |= __put_user(addr, &frame->pretcode);
bellard66fb9762003-03-23 01:06:05 +0000934 /* This is movl $,%eax ; int $0x80 */
bellard775b58d2007-11-11 16:22:17 +0000935 err |= __put_user(0xb8, (char *)(frame->retcode+0));
bellard66fb9762003-03-23 01:06:05 +0000936 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
bellard775b58d2007-11-11 16:22:17 +0000937 val16 = 0x80cd;
938 err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
bellard66fb9762003-03-23 01:06:05 +0000939 }
940
941 if (err)
942 goto give_sigsegv;
943
944 /* Set up registers for signal handler */
bellard28be6232007-11-11 22:23:38 +0000945 env->regs[R_ESP] = frame_addr;
pbrook624f7972008-05-31 16:11:38 +0000946 env->eip = ka->_sa_handler;
bellard66fb9762003-03-23 01:06:05 +0000947
948 cpu_x86_load_seg(env, R_DS, __USER_DS);
949 cpu_x86_load_seg(env, R_ES, __USER_DS);
950 cpu_x86_load_seg(env, R_SS, __USER_DS);
951 cpu_x86_load_seg(env, R_CS, __USER_CS);
952 env->eflags &= ~TF_MASK;
953
bellard579a97f2007-11-11 14:26:47 +0000954 unlock_user_struct(frame, frame_addr, 1);
955
bellard66fb9762003-03-23 01:06:05 +0000956 return;
957
958give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +0000959 unlock_user_struct(frame, frame_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000960 if (sig == TARGET_SIGSEGV)
pbrook624f7972008-05-31 16:11:38 +0000961 ka->_sa_handler = TARGET_SIG_DFL;
bellard66fb9762003-03-23 01:06:05 +0000962 force_sig(TARGET_SIGSEGV /* , current */);
963}
964
965static int
966restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
967{
968 unsigned int err = 0;
bellard28be6232007-11-11 22:23:38 +0000969 abi_ulong fpstate_addr;
970 unsigned int tmpflags;
bellard66fb9762003-03-23 01:06:05 +0000971
bellard28be6232007-11-11 22:23:38 +0000972 cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
973 cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
974 cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
975 cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
bellard66fb9762003-03-23 01:06:05 +0000976
bellard28be6232007-11-11 22:23:38 +0000977 env->regs[R_EDI] = tswapl(sc->edi);
978 env->regs[R_ESI] = tswapl(sc->esi);
979 env->regs[R_EBP] = tswapl(sc->ebp);
980 env->regs[R_ESP] = tswapl(sc->esp);
981 env->regs[R_EBX] = tswapl(sc->ebx);
982 env->regs[R_EDX] = tswapl(sc->edx);
983 env->regs[R_ECX] = tswapl(sc->ecx);
984 env->eip = tswapl(sc->eip);
bellard66fb9762003-03-23 01:06:05 +0000985
986 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
987 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
ths5fafdf22007-09-16 21:08:06 +0000988
bellard28be6232007-11-11 22:23:38 +0000989 tmpflags = tswapl(sc->eflags);
990 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
991 // regs->orig_eax = -1; /* disable syscall checks */
992
993 fpstate_addr = tswapl(sc->fpstate);
994 if (fpstate_addr != 0) {
995 if (!access_ok(VERIFY_READ, fpstate_addr,
996 sizeof(struct target_fpstate)))
997 goto badframe;
998 cpu_x86_frstor(env, fpstate_addr, 1);
bellard66fb9762003-03-23 01:06:05 +0000999 }
1000
bellard28be6232007-11-11 22:23:38 +00001001 *peax = tswapl(sc->eax);
bellard66fb9762003-03-23 01:06:05 +00001002 return err;
bellard66fb9762003-03-23 01:06:05 +00001003badframe:
1004 return 1;
bellard66fb9762003-03-23 01:06:05 +00001005}
1006
1007long do_sigreturn(CPUX86State *env)
1008{
bellard579a97f2007-11-11 14:26:47 +00001009 struct sigframe *frame;
1010 abi_ulong frame_addr = env->regs[R_ESP] - 8;
Anthony Liguoric227f092009-10-01 16:12:16 -05001011 target_sigset_t target_set;
bellard66fb9762003-03-23 01:06:05 +00001012 sigset_t set;
1013 int eax, i;
1014
bellard447db212003-05-10 15:10:36 +00001015#if defined(DEBUG_SIGNAL)
1016 fprintf(stderr, "do_sigreturn\n");
1017#endif
bellard579a97f2007-11-11 14:26:47 +00001018 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1019 goto badframe;
bellard66fb9762003-03-23 01:06:05 +00001020 /* set blocked signals */
bellard92319442004-06-19 16:58:13 +00001021 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1022 goto badframe;
1023 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1024 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1025 goto badframe;
1026 }
bellard66fb9762003-03-23 01:06:05 +00001027
bellard92319442004-06-19 16:58:13 +00001028 target_to_host_sigset_internal(&set, &target_set);
bellard66fb9762003-03-23 01:06:05 +00001029 sigprocmask(SIG_SETMASK, &set, NULL);
ths3b46e622007-09-17 08:09:54 +00001030
bellard66fb9762003-03-23 01:06:05 +00001031 /* restore registers */
1032 if (restore_sigcontext(env, &frame->sc, &eax))
1033 goto badframe;
bellard579a97f2007-11-11 14:26:47 +00001034 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001035 return eax;
1036
1037badframe:
bellard579a97f2007-11-11 14:26:47 +00001038 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001039 force_sig(TARGET_SIGSEGV);
1040 return 0;
1041}
1042
1043long do_rt_sigreturn(CPUX86State *env)
1044{
bellard28be6232007-11-11 22:23:38 +00001045 abi_ulong frame_addr;
1046 struct rt_sigframe *frame;
bellard66fb9762003-03-23 01:06:05 +00001047 sigset_t set;
bellard66fb9762003-03-23 01:06:05 +00001048 int eax;
1049
bellard28be6232007-11-11 22:23:38 +00001050 frame_addr = env->regs[R_ESP] - 4;
1051 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1052 goto badframe;
bellardb8076a72005-04-07 22:20:31 +00001053 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
bellard66fb9762003-03-23 01:06:05 +00001054 sigprocmask(SIG_SETMASK, &set, NULL);
ths5fafdf22007-09-16 21:08:06 +00001055
bellardb8076a72005-04-07 22:20:31 +00001056 if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
bellard66fb9762003-03-23 01:06:05 +00001057 goto badframe;
1058
bellard28be6232007-11-11 22:23:38 +00001059 if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0,
1060 get_sp_from_cpustate(env)) == -EFAULT)
bellard66fb9762003-03-23 01:06:05 +00001061 goto badframe;
thsa04e1342007-09-27 13:57:58 +00001062
bellard28be6232007-11-11 22:23:38 +00001063 unlock_user_struct(frame, frame_addr, 0);
bellard66fb9762003-03-23 01:06:05 +00001064 return eax;
1065
1066badframe:
bellard28be6232007-11-11 22:23:38 +00001067 unlock_user_struct(frame, frame_addr, 0);
1068 force_sig(TARGET_SIGSEGV);
bellard66fb9762003-03-23 01:06:05 +00001069 return 0;
1070}
1071
bellard43fff232003-07-09 19:31:39 +00001072#elif defined(TARGET_ARM)
1073
1074struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001075 abi_ulong trap_no;
1076 abi_ulong error_code;
1077 abi_ulong oldmask;
1078 abi_ulong arm_r0;
1079 abi_ulong arm_r1;
1080 abi_ulong arm_r2;
1081 abi_ulong arm_r3;
1082 abi_ulong arm_r4;
1083 abi_ulong arm_r5;
1084 abi_ulong arm_r6;
1085 abi_ulong arm_r7;
1086 abi_ulong arm_r8;
1087 abi_ulong arm_r9;
1088 abi_ulong arm_r10;
1089 abi_ulong arm_fp;
1090 abi_ulong arm_ip;
1091 abi_ulong arm_sp;
1092 abi_ulong arm_lr;
1093 abi_ulong arm_pc;
1094 abi_ulong arm_cpsr;
1095 abi_ulong fault_address;
bellard43fff232003-07-09 19:31:39 +00001096};
1097
pbrooka745ec62008-05-06 15:36:17 +00001098struct target_ucontext_v1 {
blueswir1992f48a2007-10-14 16:27:31 +00001099 abi_ulong tuc_flags;
1100 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001101 target_stack_t tuc_stack;
bellardb8076a72005-04-07 22:20:31 +00001102 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001103 target_sigset_t tuc_sigmask; /* mask last for extensibility */
bellard43fff232003-07-09 19:31:39 +00001104};
1105
pbrooka745ec62008-05-06 15:36:17 +00001106struct target_ucontext_v2 {
1107 abi_ulong tuc_flags;
1108 abi_ulong tuc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05001109 target_stack_t tuc_stack;
pbrooka745ec62008-05-06 15:36:17 +00001110 struct target_sigcontext tuc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05001111 target_sigset_t tuc_sigmask; /* mask last for extensibility */
pbrooka745ec62008-05-06 15:36:17 +00001112 char __unused[128 - sizeof(sigset_t)];
1113 abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1114};
1115
pbrooka8c33202008-05-07 23:22:46 +00001116struct sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001117{
1118 struct target_sigcontext sc;
blueswir1992f48a2007-10-14 16:27:31 +00001119 abi_ulong extramask[TARGET_NSIG_WORDS-1];
1120 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001121};
1122
pbrooka8c33202008-05-07 23:22:46 +00001123struct sigframe_v2
1124{
1125 struct target_ucontext_v2 uc;
1126 abi_ulong retcode;
1127};
1128
pbrooka745ec62008-05-06 15:36:17 +00001129struct rt_sigframe_v1
bellard43fff232003-07-09 19:31:39 +00001130{
bellardf8b0aa22007-11-11 23:03:42 +00001131 abi_ulong pinfo;
1132 abi_ulong puc;
bellard43fff232003-07-09 19:31:39 +00001133 struct target_siginfo info;
pbrooka745ec62008-05-06 15:36:17 +00001134 struct target_ucontext_v1 uc;
1135 abi_ulong retcode;
1136};
1137
1138struct rt_sigframe_v2
1139{
1140 struct target_siginfo info;
1141 struct target_ucontext_v2 uc;
blueswir1992f48a2007-10-14 16:27:31 +00001142 abi_ulong retcode;
bellard43fff232003-07-09 19:31:39 +00001143};
1144
1145#define TARGET_CONFIG_CPU_32 1
1146
1147/*
1148 * For ARM syscalls, we encode the syscall number into the instruction.
1149 */
1150#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1151#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1152
1153/*
1154 * For Thumb syscalls, we pass the syscall number via r7. We therefore
1155 * need two 16-bit instructions.
1156 */
1157#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1158#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1159
blueswir1992f48a2007-10-14 16:27:31 +00001160static const abi_ulong retcodes[4] = {
bellard43fff232003-07-09 19:31:39 +00001161 SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN,
1162 SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN
1163};
1164
1165
bellard43fff232003-07-09 19:31:39 +00001166#define __get_user_error(x,p,e) __get_user(x, p)
1167
1168static inline int valid_user_regs(CPUState *regs)
1169{
1170 return 1;
1171}
1172
pbrooka8c33202008-05-07 23:22:46 +00001173static void
bellard43fff232003-07-09 19:31:39 +00001174setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
bellardf8b0aa22007-11-11 23:03:42 +00001175 CPUState *env, abi_ulong mask)
bellard43fff232003-07-09 19:31:39 +00001176{
pbrooka8c33202008-05-07 23:22:46 +00001177 __put_user(env->regs[0], &sc->arm_r0);
1178 __put_user(env->regs[1], &sc->arm_r1);
1179 __put_user(env->regs[2], &sc->arm_r2);
1180 __put_user(env->regs[3], &sc->arm_r3);
1181 __put_user(env->regs[4], &sc->arm_r4);
1182 __put_user(env->regs[5], &sc->arm_r5);
1183 __put_user(env->regs[6], &sc->arm_r6);
1184 __put_user(env->regs[7], &sc->arm_r7);
1185 __put_user(env->regs[8], &sc->arm_r8);
1186 __put_user(env->regs[9], &sc->arm_r9);
1187 __put_user(env->regs[10], &sc->arm_r10);
1188 __put_user(env->regs[11], &sc->arm_fp);
1189 __put_user(env->regs[12], &sc->arm_ip);
1190 __put_user(env->regs[13], &sc->arm_sp);
1191 __put_user(env->regs[14], &sc->arm_lr);
1192 __put_user(env->regs[15], &sc->arm_pc);
bellard43fff232003-07-09 19:31:39 +00001193#ifdef TARGET_CONFIG_CPU_32
pbrooka8c33202008-05-07 23:22:46 +00001194 __put_user(cpsr_read(env), &sc->arm_cpsr);
bellard43fff232003-07-09 19:31:39 +00001195#endif
1196
pbrooka8c33202008-05-07 23:22:46 +00001197 __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1198 __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1199 __put_user(/* current->thread.address */ 0, &sc->fault_address);
1200 __put_user(mask, &sc->oldmask);
bellard43fff232003-07-09 19:31:39 +00001201}
1202
bellard579a97f2007-11-11 14:26:47 +00001203static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00001204get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
bellard43fff232003-07-09 19:31:39 +00001205{
1206 unsigned long sp = regs->regs[13];
1207
bellard43fff232003-07-09 19:31:39 +00001208 /*
1209 * This is the X/Open sanctioned signal stack switching.
1210 */
pbrook624f7972008-05-31 16:11:38 +00001211 if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
thsa04e1342007-09-27 13:57:58 +00001212 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard43fff232003-07-09 19:31:39 +00001213 /*
1214 * ATPCS B01 mandates 8-byte alignment
1215 */
bellard579a97f2007-11-11 14:26:47 +00001216 return (sp - framesize) & ~7;
bellard43fff232003-07-09 19:31:39 +00001217}
1218
1219static int
pbrook624f7972008-05-31 16:11:38 +00001220setup_return(CPUState *env, struct target_sigaction *ka,
bellardf8b0aa22007-11-11 23:03:42 +00001221 abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
bellard43fff232003-07-09 19:31:39 +00001222{
pbrook624f7972008-05-31 16:11:38 +00001223 abi_ulong handler = ka->_sa_handler;
blueswir1992f48a2007-10-14 16:27:31 +00001224 abi_ulong retcode;
pbrook75b680e2008-03-21 16:07:30 +00001225 int thumb = handler & 1;
bellard43fff232003-07-09 19:31:39 +00001226
pbrook624f7972008-05-31 16:11:38 +00001227 if (ka->sa_flags & TARGET_SA_RESTORER) {
1228 retcode = ka->sa_restorer;
bellard43fff232003-07-09 19:31:39 +00001229 } else {
1230 unsigned int idx = thumb;
1231
pbrook624f7972008-05-31 16:11:38 +00001232 if (ka->sa_flags & TARGET_SA_SIGINFO)
bellard43fff232003-07-09 19:31:39 +00001233 idx += 2;
1234
1235 if (__put_user(retcodes[idx], rc))
1236 return 1;
1237#if 0
blueswir1992f48a2007-10-14 16:27:31 +00001238 flush_icache_range((abi_ulong)rc,
1239 (abi_ulong)(rc + 1));
bellard43fff232003-07-09 19:31:39 +00001240#endif
bellardf8b0aa22007-11-11 23:03:42 +00001241 retcode = rc_addr + thumb;
bellard43fff232003-07-09 19:31:39 +00001242 }
1243
1244 env->regs[0] = usig;
bellardf8b0aa22007-11-11 23:03:42 +00001245 env->regs[13] = frame_addr;
bellard43fff232003-07-09 19:31:39 +00001246 env->regs[14] = retcode;
1247 env->regs[15] = handler & (thumb ? ~1 : ~3);
pbrook75b680e2008-03-21 16:07:30 +00001248 env->thumb = thumb;
bellard43fff232003-07-09 19:31:39 +00001249
bellardb5ff1b32005-11-26 10:38:39 +00001250#if 0
bellard43fff232003-07-09 19:31:39 +00001251#ifdef TARGET_CONFIG_CPU_32
1252 env->cpsr = cpsr;
1253#endif
bellardb5ff1b32005-11-26 10:38:39 +00001254#endif
bellard43fff232003-07-09 19:31:39 +00001255
1256 return 0;
1257}
1258
pbrooka8c33202008-05-07 23:22:46 +00001259static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
Anthony Liguoric227f092009-10-01 16:12:16 -05001260 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001261{
pbrooka8c33202008-05-07 23:22:46 +00001262 struct target_sigaltstack stack;
1263 int i;
1264
1265 /* Clear all the bits of the ucontext we don't use. */
1266 memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1267
1268 memset(&stack, 0, sizeof(stack));
1269 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1270 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1271 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1272 memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1273
1274 setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1275 /* FIXME: Save coprocessor signal frame. */
1276 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1277 __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1278 }
1279}
1280
1281/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00001282static void setup_frame_v1(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001283 target_sigset_t *set, CPUState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001284{
1285 struct sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001286 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
pbrooka8c33202008-05-07 23:22:46 +00001287 int i;
bellard43fff232003-07-09 19:31:39 +00001288
bellard579a97f2007-11-11 14:26:47 +00001289 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1290 return;
1291
pbrooka8c33202008-05-07 23:22:46 +00001292 setup_sigcontext(&frame->sc, regs, set->sig[0]);
bellard43fff232003-07-09 19:31:39 +00001293
bellard92319442004-06-19 16:58:13 +00001294 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1295 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
bellard579a97f2007-11-11 14:26:47 +00001296 goto end;
bellard43fff232003-07-09 19:31:39 +00001297 }
1298
pbrooka8c33202008-05-07 23:22:46 +00001299 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1300 frame_addr + offsetof(struct sigframe_v1, retcode));
bellard579a97f2007-11-11 14:26:47 +00001301
1302end:
1303 unlock_user_struct(frame, frame_addr, 1);
pbrooka8c33202008-05-07 23:22:46 +00001304}
1305
pbrook624f7972008-05-31 16:11:38 +00001306static void setup_frame_v2(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001307 target_sigset_t *set, CPUState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001308{
1309 struct sigframe_v2 *frame;
1310 abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1311
1312 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1313 return;
1314
1315 setup_sigframe_v2(&frame->uc, set, regs);
1316
1317 setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1318 frame_addr + offsetof(struct sigframe_v2, retcode));
1319
1320 unlock_user_struct(frame, frame_addr, 1);
1321}
1322
pbrook624f7972008-05-31 16:11:38 +00001323static void setup_frame(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001324 target_sigset_t *set, CPUState *regs)
pbrooka8c33202008-05-07 23:22:46 +00001325{
1326 if (get_osversion() >= 0x020612) {
1327 setup_frame_v2(usig, ka, set, regs);
1328 } else {
1329 setup_frame_v1(usig, ka, set, regs);
1330 }
bellard43fff232003-07-09 19:31:39 +00001331}
1332
bellard579a97f2007-11-11 14:26:47 +00001333/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
pbrook624f7972008-05-31 16:11:38 +00001334static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001335 target_siginfo_t *info,
1336 target_sigset_t *set, CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001337{
pbrooka745ec62008-05-06 15:36:17 +00001338 struct rt_sigframe_v1 *frame;
bellard579a97f2007-11-11 14:26:47 +00001339 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
thsa04e1342007-09-27 13:57:58 +00001340 struct target_sigaltstack stack;
pbrooka8c33202008-05-07 23:22:46 +00001341 int i;
bellardf8b0aa22007-11-11 23:03:42 +00001342 abi_ulong info_addr, uc_addr;
bellard43fff232003-07-09 19:31:39 +00001343
bellard579a97f2007-11-11 14:26:47 +00001344 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellardedf779f2004-02-22 13:40:13 +00001345 return /* 1 */;
1346
pbrooka745ec62008-05-06 15:36:17 +00001347 info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
pbrooka8c33202008-05-07 23:22:46 +00001348 __put_user(info_addr, &frame->pinfo);
pbrooka745ec62008-05-06 15:36:17 +00001349 uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
pbrooka8c33202008-05-07 23:22:46 +00001350 __put_user(uc_addr, &frame->puc);
1351 copy_siginfo_to_user(&frame->info, info);
bellard43fff232003-07-09 19:31:39 +00001352
1353 /* Clear all the bits of the ucontext we don't use. */
pbrooka745ec62008-05-06 15:36:17 +00001354 memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
bellard43fff232003-07-09 19:31:39 +00001355
thsa04e1342007-09-27 13:57:58 +00001356 memset(&stack, 0, sizeof(stack));
1357 __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1358 __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1359 __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
bellard775b58d2007-11-11 16:22:17 +00001360 memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
thsa04e1342007-09-27 13:57:58 +00001361
pbrooka8c33202008-05-07 23:22:46 +00001362 setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
bellard92319442004-06-19 16:58:13 +00001363 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellardb8076a72005-04-07 22:20:31 +00001364 if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
bellard579a97f2007-11-11 14:26:47 +00001365 goto end;
bellard92319442004-06-19 16:58:13 +00001366 }
bellard43fff232003-07-09 19:31:39 +00001367
pbrooka8c33202008-05-07 23:22:46 +00001368 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1369 frame_addr + offsetof(struct rt_sigframe_v1, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001370
pbrooka8c33202008-05-07 23:22:46 +00001371 env->regs[1] = info_addr;
1372 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001373
1374end:
1375 unlock_user_struct(frame, frame_addr, 1);
pbrooka745ec62008-05-06 15:36:17 +00001376}
1377
pbrook624f7972008-05-31 16:11:38 +00001378static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001379 target_siginfo_t *info,
1380 target_sigset_t *set, CPUState *env)
pbrooka745ec62008-05-06 15:36:17 +00001381{
1382 struct rt_sigframe_v2 *frame;
1383 abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
pbrooka745ec62008-05-06 15:36:17 +00001384 abi_ulong info_addr, uc_addr;
1385
1386 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1387 return /* 1 */;
1388
1389 info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1390 uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
pbrooka8c33202008-05-07 23:22:46 +00001391 copy_siginfo_to_user(&frame->info, info);
pbrooka745ec62008-05-06 15:36:17 +00001392
pbrooka8c33202008-05-07 23:22:46 +00001393 setup_sigframe_v2(&frame->uc, set, env);
pbrooka745ec62008-05-06 15:36:17 +00001394
pbrooka8c33202008-05-07 23:22:46 +00001395 setup_return(env, ka, &frame->retcode, frame_addr, usig,
1396 frame_addr + offsetof(struct rt_sigframe_v2, retcode));
pbrooka745ec62008-05-06 15:36:17 +00001397
pbrooka8c33202008-05-07 23:22:46 +00001398 env->regs[1] = info_addr;
1399 env->regs[2] = uc_addr;
pbrooka745ec62008-05-06 15:36:17 +00001400
bellard579a97f2007-11-11 14:26:47 +00001401 unlock_user_struct(frame, frame_addr, 1);
bellard43fff232003-07-09 19:31:39 +00001402}
1403
pbrook624f7972008-05-31 16:11:38 +00001404static void setup_rt_frame(int usig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001405 target_siginfo_t *info,
1406 target_sigset_t *set, CPUState *env)
pbrooka745ec62008-05-06 15:36:17 +00001407{
1408 if (get_osversion() >= 0x020612) {
1409 setup_rt_frame_v2(usig, ka, info, set, env);
1410 } else {
1411 setup_rt_frame_v1(usig, ka, info, set, env);
1412 }
1413}
1414
bellard43fff232003-07-09 19:31:39 +00001415static int
1416restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1417{
1418 int err = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001419 uint32_t cpsr;
bellard43fff232003-07-09 19:31:39 +00001420
1421 __get_user_error(env->regs[0], &sc->arm_r0, err);
1422 __get_user_error(env->regs[1], &sc->arm_r1, err);
1423 __get_user_error(env->regs[2], &sc->arm_r2, err);
1424 __get_user_error(env->regs[3], &sc->arm_r3, err);
1425 __get_user_error(env->regs[4], &sc->arm_r4, err);
1426 __get_user_error(env->regs[5], &sc->arm_r5, err);
1427 __get_user_error(env->regs[6], &sc->arm_r6, err);
1428 __get_user_error(env->regs[7], &sc->arm_r7, err);
1429 __get_user_error(env->regs[8], &sc->arm_r8, err);
1430 __get_user_error(env->regs[9], &sc->arm_r9, err);
1431 __get_user_error(env->regs[10], &sc->arm_r10, err);
1432 __get_user_error(env->regs[11], &sc->arm_fp, err);
1433 __get_user_error(env->regs[12], &sc->arm_ip, err);
1434 __get_user_error(env->regs[13], &sc->arm_sp, err);
1435 __get_user_error(env->regs[14], &sc->arm_lr, err);
1436 __get_user_error(env->regs[15], &sc->arm_pc, err);
1437#ifdef TARGET_CONFIG_CPU_32
bellardb5ff1b32005-11-26 10:38:39 +00001438 __get_user_error(cpsr, &sc->arm_cpsr, err);
pbrook75b680e2008-03-21 16:07:30 +00001439 cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
bellard43fff232003-07-09 19:31:39 +00001440#endif
1441
1442 err |= !valid_user_regs(env);
1443
1444 return err;
1445}
1446
aurel32dc7eea62009-01-30 20:15:32 +00001447static long do_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001448{
bellardf8b0aa22007-11-11 23:03:42 +00001449 abi_ulong frame_addr;
pbrooka8c33202008-05-07 23:22:46 +00001450 struct sigframe_v1 *frame;
Anthony Liguoric227f092009-10-01 16:12:16 -05001451 target_sigset_t set;
bellard43fff232003-07-09 19:31:39 +00001452 sigset_t host_set;
bellard92319442004-06-19 16:58:13 +00001453 int i;
bellard43fff232003-07-09 19:31:39 +00001454
1455 /*
1456 * Since we stacked the signal on a 64-bit boundary,
1457 * then 'sp' should be word aligned here. If it's
1458 * not, then the user is trying to mess with us.
1459 */
1460 if (env->regs[13] & 7)
1461 goto badframe;
1462
bellardf8b0aa22007-11-11 23:03:42 +00001463 frame_addr = env->regs[13];
1464 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1465 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001466
bellard92319442004-06-19 16:58:13 +00001467 if (__get_user(set.sig[0], &frame->sc.oldmask))
1468 goto badframe;
1469 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1470 if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1471 goto badframe;
1472 }
bellard43fff232003-07-09 19:31:39 +00001473
bellard92319442004-06-19 16:58:13 +00001474 target_to_host_sigset_internal(&host_set, &set);
bellard43fff232003-07-09 19:31:39 +00001475 sigprocmask(SIG_SETMASK, &host_set, NULL);
1476
1477 if (restore_sigcontext(env, &frame->sc))
1478 goto badframe;
1479
1480#if 0
1481 /* Send SIGTRAP if we're single-stepping */
1482 if (ptrace_cancel_bpt(current))
1483 send_sig(SIGTRAP, current, 1);
1484#endif
bellardf8b0aa22007-11-11 23:03:42 +00001485 unlock_user_struct(frame, frame_addr, 0);
1486 return env->regs[0];
bellard43fff232003-07-09 19:31:39 +00001487
1488badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001489 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001490 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00001491 return 0;
1492}
1493
pbrooka8c33202008-05-07 23:22:46 +00001494static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1495 struct target_ucontext_v2 *uc)
1496{
1497 sigset_t host_set;
1498
1499 target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1500 sigprocmask(SIG_SETMASK, &host_set, NULL);
1501
1502 if (restore_sigcontext(env, &uc->tuc_mcontext))
1503 return 1;
1504
1505 if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1506 return 1;
1507
1508#if 0
1509 /* Send SIGTRAP if we're single-stepping */
1510 if (ptrace_cancel_bpt(current))
1511 send_sig(SIGTRAP, current, 1);
1512#endif
1513
1514 return 0;
1515}
1516
aurel32dc7eea62009-01-30 20:15:32 +00001517static long do_sigreturn_v2(CPUState *env)
pbrooka8c33202008-05-07 23:22:46 +00001518{
1519 abi_ulong frame_addr;
1520 struct sigframe_v2 *frame;
1521
1522 /*
1523 * Since we stacked the signal on a 64-bit boundary,
1524 * then 'sp' should be word aligned here. If it's
1525 * not, then the user is trying to mess with us.
1526 */
1527 if (env->regs[13] & 7)
1528 goto badframe;
1529
1530 frame_addr = env->regs[13];
1531 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1532 goto badframe;
1533
1534 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1535 goto badframe;
1536
1537 unlock_user_struct(frame, frame_addr, 0);
1538 return env->regs[0];
1539
1540badframe:
1541 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001542 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka8c33202008-05-07 23:22:46 +00001543 return 0;
1544}
1545
1546long do_sigreturn(CPUState *env)
1547{
1548 if (get_osversion() >= 0x020612) {
1549 return do_sigreturn_v2(env);
1550 } else {
1551 return do_sigreturn_v1(env);
1552 }
1553}
1554
aurel32dc7eea62009-01-30 20:15:32 +00001555static long do_rt_sigreturn_v1(CPUState *env)
bellard43fff232003-07-09 19:31:39 +00001556{
bellardf8b0aa22007-11-11 23:03:42 +00001557 abi_ulong frame_addr;
pbrooka745ec62008-05-06 15:36:17 +00001558 struct rt_sigframe_v1 *frame;
bellard43fff232003-07-09 19:31:39 +00001559 sigset_t host_set;
1560
1561 /*
1562 * Since we stacked the signal on a 64-bit boundary,
1563 * then 'sp' should be word aligned here. If it's
1564 * not, then the user is trying to mess with us.
1565 */
1566 if (env->regs[13] & 7)
1567 goto badframe;
1568
bellardf8b0aa22007-11-11 23:03:42 +00001569 frame_addr = env->regs[13];
1570 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1571 goto badframe;
bellard43fff232003-07-09 19:31:39 +00001572
bellardb8076a72005-04-07 22:20:31 +00001573 target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
bellard43fff232003-07-09 19:31:39 +00001574 sigprocmask(SIG_SETMASK, &host_set, NULL);
1575
bellardb8076a72005-04-07 22:20:31 +00001576 if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
bellard43fff232003-07-09 19:31:39 +00001577 goto badframe;
1578
pbrooka745ec62008-05-06 15:36:17 +00001579 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 +00001580 goto badframe;
1581
bellard43fff232003-07-09 19:31:39 +00001582#if 0
1583 /* Send SIGTRAP if we're single-stepping */
1584 if (ptrace_cancel_bpt(current))
1585 send_sig(SIGTRAP, current, 1);
1586#endif
bellardf8b0aa22007-11-11 23:03:42 +00001587 unlock_user_struct(frame, frame_addr, 0);
bellard43fff232003-07-09 19:31:39 +00001588 return env->regs[0];
1589
1590badframe:
bellardf8b0aa22007-11-11 23:03:42 +00001591 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001592 force_sig(TARGET_SIGSEGV /* , current */);
bellard43fff232003-07-09 19:31:39 +00001593 return 0;
1594}
1595
aurel32dc7eea62009-01-30 20:15:32 +00001596static long do_rt_sigreturn_v2(CPUState *env)
pbrooka745ec62008-05-06 15:36:17 +00001597{
1598 abi_ulong frame_addr;
1599 struct rt_sigframe_v2 *frame;
pbrooka745ec62008-05-06 15:36:17 +00001600
1601 /*
1602 * Since we stacked the signal on a 64-bit boundary,
1603 * then 'sp' should be word aligned here. If it's
1604 * not, then the user is trying to mess with us.
1605 */
1606 if (env->regs[13] & 7)
1607 goto badframe;
1608
1609 frame_addr = env->regs[13];
1610 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1611 goto badframe;
1612
pbrooka8c33202008-05-07 23:22:46 +00001613 if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1614 goto badframe;
pbrooka745ec62008-05-06 15:36:17 +00001615
pbrooka745ec62008-05-06 15:36:17 +00001616 unlock_user_struct(frame, frame_addr, 0);
1617 return env->regs[0];
1618
1619badframe:
1620 unlock_user_struct(frame, frame_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02001621 force_sig(TARGET_SIGSEGV /* , current */);
pbrooka745ec62008-05-06 15:36:17 +00001622 return 0;
1623}
1624
1625long do_rt_sigreturn(CPUState *env)
1626{
1627 if (get_osversion() >= 0x020612) {
1628 return do_rt_sigreturn_v2(env);
1629 } else {
1630 return do_rt_sigreturn_v1(env);
1631 }
1632}
1633
bellard6d5e2162004-09-30 22:04:13 +00001634#elif defined(TARGET_SPARC)
bellard80a9d032005-01-03 23:31:27 +00001635
bellard6d5e2162004-09-30 22:04:13 +00001636#define __SUNOS_MAXWIN 31
1637
1638/* This is what SunOS does, so shall I. */
1639struct target_sigcontext {
blueswir1992f48a2007-10-14 16:27:31 +00001640 abi_ulong sigc_onstack; /* state to restore */
bellard6d5e2162004-09-30 22:04:13 +00001641
blueswir1992f48a2007-10-14 16:27:31 +00001642 abi_ulong sigc_mask; /* sigmask to restore */
1643 abi_ulong sigc_sp; /* stack pointer */
1644 abi_ulong sigc_pc; /* program counter */
1645 abi_ulong sigc_npc; /* next program counter */
1646 abi_ulong sigc_psr; /* for condition codes etc */
1647 abi_ulong sigc_g1; /* User uses these two registers */
1648 abi_ulong sigc_o0; /* within the trampoline code. */
bellard6d5e2162004-09-30 22:04:13 +00001649
1650 /* Now comes information regarding the users window set
1651 * at the time of the signal.
1652 */
blueswir1992f48a2007-10-14 16:27:31 +00001653 abi_ulong sigc_oswins; /* outstanding windows */
bellard6d5e2162004-09-30 22:04:13 +00001654
1655 /* stack ptrs for each regwin buf */
1656 char *sigc_spbuf[__SUNOS_MAXWIN];
1657
1658 /* Windows to restore after signal */
1659 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001660 abi_ulong locals[8];
1661 abi_ulong ins[8];
bellard6d5e2162004-09-30 22:04:13 +00001662 } sigc_wbuf[__SUNOS_MAXWIN];
1663};
1664/* A Sparc stack frame */
1665struct sparc_stackf {
blueswir1992f48a2007-10-14 16:27:31 +00001666 abi_ulong locals[8];
1667 abi_ulong ins[6];
bellard6d5e2162004-09-30 22:04:13 +00001668 struct sparc_stackf *fp;
blueswir1992f48a2007-10-14 16:27:31 +00001669 abi_ulong callers_pc;
bellard6d5e2162004-09-30 22:04:13 +00001670 char *structptr;
blueswir1992f48a2007-10-14 16:27:31 +00001671 abi_ulong xargs[6];
1672 abi_ulong xxargs[1];
bellard6d5e2162004-09-30 22:04:13 +00001673};
1674
1675typedef struct {
1676 struct {
blueswir1992f48a2007-10-14 16:27:31 +00001677 abi_ulong psr;
1678 abi_ulong pc;
1679 abi_ulong npc;
1680 abi_ulong y;
1681 abi_ulong u_regs[16]; /* globals and ins */
bellard6d5e2162004-09-30 22:04:13 +00001682 } si_regs;
1683 int si_mask;
1684} __siginfo_t;
1685
1686typedef struct {
1687 unsigned long si_float_regs [32];
1688 unsigned long si_fsr;
1689 unsigned long si_fpqdepth;
1690 struct {
1691 unsigned long *insn_addr;
1692 unsigned long insn;
1693 } si_fpqueue [16];
Anthony Liguoric227f092009-10-01 16:12:16 -05001694} qemu_siginfo_fpu_t;
bellard6d5e2162004-09-30 22:04:13 +00001695
1696
1697struct target_signal_frame {
1698 struct sparc_stackf ss;
1699 __siginfo_t info;
bellardf8b0aa22007-11-11 23:03:42 +00001700 abi_ulong fpu_save;
blueswir1992f48a2007-10-14 16:27:31 +00001701 abi_ulong insns[2] __attribute__ ((aligned (8)));
1702 abi_ulong extramask[TARGET_NSIG_WORDS - 1];
1703 abi_ulong extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05001704 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001705};
1706struct target_rt_signal_frame {
1707 struct sparc_stackf ss;
1708 siginfo_t info;
blueswir1992f48a2007-10-14 16:27:31 +00001709 abi_ulong regs[20];
bellard6d5e2162004-09-30 22:04:13 +00001710 sigset_t mask;
bellardf8b0aa22007-11-11 23:03:42 +00001711 abi_ulong fpu_save;
bellard6d5e2162004-09-30 22:04:13 +00001712 unsigned int insns[2];
1713 stack_t stack;
1714 unsigned int extra_size; /* Should be 0 */
Anthony Liguoric227f092009-10-01 16:12:16 -05001715 qemu_siginfo_fpu_t fpu_state;
bellard6d5e2162004-09-30 22:04:13 +00001716};
1717
bellarde80cfcf2004-12-19 23:18:01 +00001718#define UREG_O0 16
1719#define UREG_O6 22
1720#define UREG_I0 0
1721#define UREG_I1 1
1722#define UREG_I2 2
blueswir15bfb56b2007-10-05 17:01:51 +00001723#define UREG_I3 3
1724#define UREG_I4 4
1725#define UREG_I5 5
bellarde80cfcf2004-12-19 23:18:01 +00001726#define UREG_I6 6
1727#define UREG_I7 7
1728#define UREG_L0 8
bellard6d5e2162004-09-30 22:04:13 +00001729#define UREG_FP UREG_I6
1730#define UREG_SP UREG_O6
1731
pbrook624f7972008-05-31 16:11:38 +00001732static inline abi_ulong get_sigframe(struct target_sigaction *sa,
bellard459a4012007-11-11 19:45:10 +00001733 CPUState *env, unsigned long framesize)
bellard6d5e2162004-09-30 22:04:13 +00001734{
bellard459a4012007-11-11 19:45:10 +00001735 abi_ulong sp;
bellard6d5e2162004-09-30 22:04:13 +00001736
1737 sp = env->regwptr[UREG_FP];
bellard6d5e2162004-09-30 22:04:13 +00001738
1739 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00001740 if (sa->sa_flags & TARGET_SA_ONSTACK) {
thsa04e1342007-09-27 13:57:58 +00001741 if (!on_sig_stack(sp)
1742 && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1743 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
bellard6d5e2162004-09-30 22:04:13 +00001744 }
bellard459a4012007-11-11 19:45:10 +00001745 return sp - framesize;
bellard6d5e2162004-09-30 22:04:13 +00001746}
1747
1748static int
blueswir1992f48a2007-10-14 16:27:31 +00001749setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
bellard6d5e2162004-09-30 22:04:13 +00001750{
1751 int err = 0, i;
1752
bellard6d5e2162004-09-30 22:04:13 +00001753 err |= __put_user(env->psr, &si->si_regs.psr);
bellard6d5e2162004-09-30 22:04:13 +00001754 err |= __put_user(env->pc, &si->si_regs.pc);
1755 err |= __put_user(env->npc, &si->si_regs.npc);
1756 err |= __put_user(env->y, &si->si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001757 for (i=0; i < 8; i++) {
bellard6d5e2162004-09-30 22:04:13 +00001758 err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1759 }
bellarda315a142005-01-30 22:59:18 +00001760 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001761 err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
bellard6d5e2162004-09-30 22:04:13 +00001762 }
bellard6d5e2162004-09-30 22:04:13 +00001763 err |= __put_user(mask, &si->si_mask);
1764 return err;
1765}
bellarde80cfcf2004-12-19 23:18:01 +00001766
bellard80a9d032005-01-03 23:31:27 +00001767#if 0
bellard6d5e2162004-09-30 22:04:13 +00001768static int
1769setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1770 CPUState *env, unsigned long mask)
1771{
1772 int err = 0;
1773
1774 err |= __put_user(mask, &sc->sigc_mask);
1775 err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1776 err |= __put_user(env->pc, &sc->sigc_pc);
1777 err |= __put_user(env->npc, &sc->sigc_npc);
1778 err |= __put_user(env->psr, &sc->sigc_psr);
1779 err |= __put_user(env->gregs[1], &sc->sigc_g1);
1780 err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1781
1782 return err;
1783}
bellard80a9d032005-01-03 23:31:27 +00001784#endif
bellard6d5e2162004-09-30 22:04:13 +00001785#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7)))
1786
pbrook624f7972008-05-31 16:11:38 +00001787static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001788 target_sigset_t *set, CPUState *env)
bellard6d5e2162004-09-30 22:04:13 +00001789{
bellard459a4012007-11-11 19:45:10 +00001790 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001791 struct target_signal_frame *sf;
1792 int sigframe_size, err, i;
1793
1794 /* 1. Make sure everything is clean */
1795 //synchronize_user_stack();
1796
1797 sigframe_size = NF_ALIGNEDSZ;
bellard459a4012007-11-11 19:45:10 +00001798 sf_addr = get_sigframe(ka, env, sigframe_size);
bellard6d5e2162004-09-30 22:04:13 +00001799
bellard459a4012007-11-11 19:45:10 +00001800 sf = lock_user(VERIFY_WRITE, sf_addr,
1801 sizeof(struct target_signal_frame), 0);
1802 if (!sf)
1803 goto sigsegv;
1804
bellarde80cfcf2004-12-19 23:18:01 +00001805 //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 +00001806#if 0
1807 if (invalid_frame_pointer(sf, sigframe_size))
1808 goto sigill_and_return;
1809#endif
1810 /* 2. Save the current process state */
1811 err = setup___siginfo(&sf->info, env, set->sig[0]);
1812 err |= __put_user(0, &sf->extra_size);
1813
1814 //err |= save_fpu_state(regs, &sf->fpu_state);
1815 //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1816
1817 err |= __put_user(set->sig[0], &sf->info.si_mask);
1818 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1819 err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1820 }
1821
bellarda315a142005-01-30 22:59:18 +00001822 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001823 err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
bellard6d5e2162004-09-30 22:04:13 +00001824 }
bellarda315a142005-01-30 22:59:18 +00001825 for (i = 0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001826 err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
bellard6d5e2162004-09-30 22:04:13 +00001827 }
bellard6d5e2162004-09-30 22:04:13 +00001828 if (err)
1829 goto sigsegv;
1830
1831 /* 3. signal handler back-trampoline and parameters */
bellard459a4012007-11-11 19:45:10 +00001832 env->regwptr[UREG_FP] = sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001833 env->regwptr[UREG_I0] = sig;
bellard459a4012007-11-11 19:45:10 +00001834 env->regwptr[UREG_I1] = sf_addr +
1835 offsetof(struct target_signal_frame, info);
1836 env->regwptr[UREG_I2] = sf_addr +
1837 offsetof(struct target_signal_frame, info);
bellard6d5e2162004-09-30 22:04:13 +00001838
1839 /* 4. signal handler */
pbrook624f7972008-05-31 16:11:38 +00001840 env->pc = ka->_sa_handler;
bellard6d5e2162004-09-30 22:04:13 +00001841 env->npc = (env->pc + 4);
1842 /* 5. return to kernel instructions */
pbrook624f7972008-05-31 16:11:38 +00001843 if (ka->sa_restorer)
1844 env->regwptr[UREG_I7] = ka->sa_restorer;
bellard6d5e2162004-09-30 22:04:13 +00001845 else {
bellard775b58d2007-11-11 16:22:17 +00001846 uint32_t val32;
bellard459a4012007-11-11 19:45:10 +00001847
1848 env->regwptr[UREG_I7] = sf_addr +
1849 offsetof(struct target_signal_frame, insns) - 2 * 4;
bellard6d5e2162004-09-30 22:04:13 +00001850
1851 /* mov __NR_sigreturn, %g1 */
bellard775b58d2007-11-11 16:22:17 +00001852 val32 = 0x821020d8;
1853 err |= __put_user(val32, &sf->insns[0]);
bellard6d5e2162004-09-30 22:04:13 +00001854
1855 /* t 0x10 */
bellard775b58d2007-11-11 16:22:17 +00001856 val32 = 0x91d02010;
1857 err |= __put_user(val32, &sf->insns[1]);
bellard6d5e2162004-09-30 22:04:13 +00001858 if (err)
1859 goto sigsegv;
1860
1861 /* Flush instruction space. */
1862 //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
bellard80a9d032005-01-03 23:31:27 +00001863 // tb_flush(env);
bellard6d5e2162004-09-30 22:04:13 +00001864 }
bellard459a4012007-11-11 19:45:10 +00001865 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001866 return;
bellard459a4012007-11-11 19:45:10 +00001867#if 0
1868sigill_and_return:
bellard6d5e2162004-09-30 22:04:13 +00001869 force_sig(TARGET_SIGILL);
bellard459a4012007-11-11 19:45:10 +00001870#endif
bellard6d5e2162004-09-30 22:04:13 +00001871sigsegv:
bellarde80cfcf2004-12-19 23:18:01 +00001872 //fprintf(stderr, "force_sig\n");
bellard459a4012007-11-11 19:45:10 +00001873 unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
bellard6d5e2162004-09-30 22:04:13 +00001874 force_sig(TARGET_SIGSEGV);
1875}
1876static inline int
Anthony Liguoric227f092009-10-01 16:12:16 -05001877restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
bellard6d5e2162004-09-30 22:04:13 +00001878{
1879 int err;
1880#if 0
1881#ifdef CONFIG_SMP
1882 if (current->flags & PF_USEDFPU)
1883 regs->psr &= ~PSR_EF;
1884#else
1885 if (current == last_task_used_math) {
1886 last_task_used_math = 0;
1887 regs->psr &= ~PSR_EF;
1888 }
1889#endif
1890 current->used_math = 1;
1891 current->flags &= ~PF_USEDFPU;
1892#endif
1893#if 0
1894 if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1895 return -EFAULT;
1896#endif
1897
bellardfafffae2006-10-28 12:09:16 +00001898#if 0
1899 /* XXX: incorrect */
bellard6d5e2162004-09-30 22:04:13 +00001900 err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1901 (sizeof(unsigned long) * 32));
bellardfafffae2006-10-28 12:09:16 +00001902#endif
bellard6d5e2162004-09-30 22:04:13 +00001903 err |= __get_user(env->fsr, &fpu->si_fsr);
1904#if 0
1905 err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1906 if (current->thread.fpqdepth != 0)
1907 err |= __copy_from_user(&current->thread.fpqueue[0],
1908 &fpu->si_fpqueue[0],
1909 ((sizeof(unsigned long) +
1910 (sizeof(unsigned long *)))*16));
1911#endif
1912 return err;
1913}
1914
1915
pbrook624f7972008-05-31 16:11:38 +00001916static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05001917 target_siginfo_t *info,
1918 target_sigset_t *set, CPUState *env)
bellard6d5e2162004-09-30 22:04:13 +00001919{
1920 fprintf(stderr, "setup_rt_frame: not implemented\n");
1921}
1922
1923long do_sigreturn(CPUState *env)
1924{
bellardf8b0aa22007-11-11 23:03:42 +00001925 abi_ulong sf_addr;
bellard6d5e2162004-09-30 22:04:13 +00001926 struct target_signal_frame *sf;
bellarde80cfcf2004-12-19 23:18:01 +00001927 uint32_t up_psr, pc, npc;
Anthony Liguoric227f092009-10-01 16:12:16 -05001928 target_sigset_t set;
bellarde80cfcf2004-12-19 23:18:01 +00001929 sigset_t host_set;
bellardf8b0aa22007-11-11 23:03:42 +00001930 abi_ulong fpu_save_addr;
bellarde80cfcf2004-12-19 23:18:01 +00001931 int err, i;
bellard6d5e2162004-09-30 22:04:13 +00001932
bellardf8b0aa22007-11-11 23:03:42 +00001933 sf_addr = env->regwptr[UREG_FP];
1934 if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1935 goto segv_and_exit;
bellard80a9d032005-01-03 23:31:27 +00001936#if 0
bellarde80cfcf2004-12-19 23:18:01 +00001937 fprintf(stderr, "sigreturn\n");
1938 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 +00001939#endif
bellarde80cfcf2004-12-19 23:18:01 +00001940 //cpu_dump_state(env, stderr, fprintf, 0);
bellard6d5e2162004-09-30 22:04:13 +00001941
1942 /* 1. Make sure we are not getting garbage from the user */
bellard6d5e2162004-09-30 22:04:13 +00001943
bellardf8b0aa22007-11-11 23:03:42 +00001944 if (sf_addr & 3)
bellard6d5e2162004-09-30 22:04:13 +00001945 goto segv_and_exit;
1946
1947 err = __get_user(pc, &sf->info.si_regs.pc);
1948 err |= __get_user(npc, &sf->info.si_regs.npc);
1949
bellard6d5e2162004-09-30 22:04:13 +00001950 if ((pc | npc) & 3)
1951 goto segv_and_exit;
1952
1953 /* 2. Restore the state */
bellarde80cfcf2004-12-19 23:18:01 +00001954 err |= __get_user(up_psr, &sf->info.si_regs.psr);
1955
bellard6d5e2162004-09-30 22:04:13 +00001956 /* User can only change condition codes and FPU enabling in %psr. */
bellarda315a142005-01-30 22:59:18 +00001957 env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1958 | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1959
1960 env->pc = pc;
1961 env->npc = npc;
bellarde80cfcf2004-12-19 23:18:01 +00001962 err |= __get_user(env->y, &sf->info.si_regs.y);
bellarda315a142005-01-30 22:59:18 +00001963 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001964 err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1965 }
bellarda315a142005-01-30 22:59:18 +00001966 for (i=0; i < 8; i++) {
bellarde80cfcf2004-12-19 23:18:01 +00001967 err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1968 }
bellard6d5e2162004-09-30 22:04:13 +00001969
bellardf8b0aa22007-11-11 23:03:42 +00001970 err |= __get_user(fpu_save_addr, &sf->fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001971
bellarde80cfcf2004-12-19 23:18:01 +00001972 //if (fpu_save)
1973 // err |= restore_fpu_state(env, fpu_save);
bellard6d5e2162004-09-30 22:04:13 +00001974
1975 /* This is pretty much atomic, no amount locking would prevent
1976 * the races which exist anyways.
1977 */
1978 err |= __get_user(set.sig[0], &sf->info.si_mask);
bellarde80cfcf2004-12-19 23:18:01 +00001979 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1980 err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1981 }
1982
1983 target_to_host_sigset_internal(&host_set, &set);
1984 sigprocmask(SIG_SETMASK, &host_set, NULL);
bellard6d5e2162004-09-30 22:04:13 +00001985
1986 if (err)
1987 goto segv_and_exit;
bellardf8b0aa22007-11-11 23:03:42 +00001988 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001989 return env->regwptr[0];
1990
1991segv_and_exit:
bellardf8b0aa22007-11-11 23:03:42 +00001992 unlock_user_struct(sf, sf_addr, 0);
bellard6d5e2162004-09-30 22:04:13 +00001993 force_sig(TARGET_SIGSEGV);
1994}
1995
1996long do_rt_sigreturn(CPUState *env)
1997{
1998 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00001999 return -TARGET_ENOSYS;
bellard6d5e2162004-09-30 22:04:13 +00002000}
2001
bellard459a4012007-11-11 19:45:10 +00002002#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
blueswir15bfb56b2007-10-05 17:01:51 +00002003#define MC_TSTATE 0
2004#define MC_PC 1
2005#define MC_NPC 2
2006#define MC_Y 3
2007#define MC_G1 4
2008#define MC_G2 5
2009#define MC_G3 6
2010#define MC_G4 7
2011#define MC_G5 8
2012#define MC_G6 9
2013#define MC_G7 10
2014#define MC_O0 11
2015#define MC_O1 12
2016#define MC_O2 13
2017#define MC_O3 14
2018#define MC_O4 15
2019#define MC_O5 16
2020#define MC_O6 17
2021#define MC_O7 18
2022#define MC_NGREG 19
2023
Anthony Liguoric227f092009-10-01 16:12:16 -05002024typedef abi_ulong target_mc_greg_t;
2025typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
blueswir15bfb56b2007-10-05 17:01:51 +00002026
2027struct target_mc_fq {
blueswir1992f48a2007-10-14 16:27:31 +00002028 abi_ulong *mcfq_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002029 uint32_t mcfq_insn;
2030};
2031
2032struct target_mc_fpu {
2033 union {
2034 uint32_t sregs[32];
2035 uint64_t dregs[32];
2036 //uint128_t qregs[16];
2037 } mcfpu_fregs;
blueswir1992f48a2007-10-14 16:27:31 +00002038 abi_ulong mcfpu_fsr;
2039 abi_ulong mcfpu_fprs;
2040 abi_ulong mcfpu_gsr;
blueswir15bfb56b2007-10-05 17:01:51 +00002041 struct target_mc_fq *mcfpu_fq;
2042 unsigned char mcfpu_qcnt;
2043 unsigned char mcfpu_qentsz;
2044 unsigned char mcfpu_enab;
2045};
Anthony Liguoric227f092009-10-01 16:12:16 -05002046typedef struct target_mc_fpu target_mc_fpu_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002047
2048typedef struct {
Anthony Liguoric227f092009-10-01 16:12:16 -05002049 target_mc_gregset_t mc_gregs;
2050 target_mc_greg_t mc_fp;
2051 target_mc_greg_t mc_i7;
2052 target_mc_fpu_t mc_fpregs;
2053} target_mcontext_t;
blueswir15bfb56b2007-10-05 17:01:51 +00002054
2055struct target_ucontext {
2056 struct target_ucontext *uc_link;
blueswir1992f48a2007-10-14 16:27:31 +00002057 abi_ulong uc_flags;
Anthony Liguoric227f092009-10-01 16:12:16 -05002058 target_sigset_t uc_sigmask;
2059 target_mcontext_t uc_mcontext;
blueswir15bfb56b2007-10-05 17:01:51 +00002060};
2061
2062/* A V9 register window */
2063struct target_reg_window {
blueswir1992f48a2007-10-14 16:27:31 +00002064 abi_ulong locals[8];
2065 abi_ulong ins[8];
blueswir15bfb56b2007-10-05 17:01:51 +00002066};
2067
2068#define TARGET_STACK_BIAS 2047
2069
2070/* {set, get}context() needed for 64-bit SparcLinux userland. */
2071void sparc64_set_context(CPUSPARCState *env)
2072{
bellard459a4012007-11-11 19:45:10 +00002073 abi_ulong ucp_addr;
2074 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002075 target_mc_gregset_t *grp;
blueswir1992f48a2007-10-14 16:27:31 +00002076 abi_ulong pc, npc, tstate;
bellard459a4012007-11-11 19:45:10 +00002077 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002078 unsigned char fenab;
2079 int err;
2080 unsigned int i;
blueswir15bfb56b2007-10-05 17:01:51 +00002081
bellard459a4012007-11-11 19:45:10 +00002082 ucp_addr = env->regwptr[UREG_I0];
2083 if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2084 goto do_sigsegv;
blueswir15bfb56b2007-10-05 17:01:51 +00002085 grp = &ucp->uc_mcontext.mc_gregs;
bellard579a97f2007-11-11 14:26:47 +00002086 err = __get_user(pc, &((*grp)[MC_PC]));
2087 err |= __get_user(npc, &((*grp)[MC_NPC]));
blueswir15bfb56b2007-10-05 17:01:51 +00002088 if (err || ((pc | npc) & 3))
2089 goto do_sigsegv;
2090 if (env->regwptr[UREG_I1]) {
Anthony Liguoric227f092009-10-01 16:12:16 -05002091 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002092 sigset_t set;
2093
2094 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002095 if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
blueswir15bfb56b2007-10-05 17:01:51 +00002096 goto do_sigsegv;
2097 } else {
bellard459a4012007-11-11 19:45:10 +00002098 abi_ulong *src, *dst;
2099 src = ucp->uc_sigmask.sig;
2100 dst = target_set.sig;
Anthony Liguoric227f092009-10-01 16:12:16 -05002101 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002102 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002103 err |= __get_user(*dst, src);
blueswir15bfb56b2007-10-05 17:01:51 +00002104 if (err)
2105 goto do_sigsegv;
2106 }
2107 target_to_host_sigset_internal(&set, &target_set);
2108 sigprocmask(SIG_SETMASK, &set, NULL);
2109 }
2110 env->pc = pc;
2111 env->npc = npc;
bellard579a97f2007-11-11 14:26:47 +00002112 err |= __get_user(env->y, &((*grp)[MC_Y]));
2113 err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
blueswir15bfb56b2007-10-05 17:01:51 +00002114 env->asi = (tstate >> 24) & 0xff;
2115 PUT_CCR(env, tstate >> 32);
2116 PUT_CWP64(env, tstate & 0x1f);
bellard579a97f2007-11-11 14:26:47 +00002117 err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2118 err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2119 err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2120 err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2121 err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2122 err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2123 err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2124 err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2125 err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2126 err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2127 err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2128 err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2129 err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2130 err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2131 err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002132
bellard579a97f2007-11-11 14:26:47 +00002133 err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2134 err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002135
bellard459a4012007-11-11 19:45:10 +00002136 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2137 if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2138 abi_ulong) != 0)
2139 goto do_sigsegv;
2140 if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2141 abi_ulong) != 0)
2142 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002143 err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2144 err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
bellard459a4012007-11-11 19:45:10 +00002145 {
2146 uint32_t *src, *dst;
2147 src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2148 dst = env->fpr;
2149 /* XXX: check that the CPU storage is the same as user context */
2150 for (i = 0; i < 64; i++, dst++, src++)
2151 err |= __get_user(*dst, src);
2152 }
bellard579a97f2007-11-11 14:26:47 +00002153 err |= __get_user(env->fsr,
2154 &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2155 err |= __get_user(env->gsr,
2156 &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
blueswir15bfb56b2007-10-05 17:01:51 +00002157 if (err)
2158 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002159 unlock_user_struct(ucp, ucp_addr, 0);
blueswir15bfb56b2007-10-05 17:01:51 +00002160 return;
2161 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002162 unlock_user_struct(ucp, ucp_addr, 0);
Riku Voipio66393fb2009-12-04 15:16:32 +02002163 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002164}
2165
2166void sparc64_get_context(CPUSPARCState *env)
2167{
bellard459a4012007-11-11 19:45:10 +00002168 abi_ulong ucp_addr;
2169 struct target_ucontext *ucp;
Anthony Liguoric227f092009-10-01 16:12:16 -05002170 target_mc_gregset_t *grp;
2171 target_mcontext_t *mcp;
bellard459a4012007-11-11 19:45:10 +00002172 abi_ulong fp, i7, w_addr;
blueswir15bfb56b2007-10-05 17:01:51 +00002173 int err;
2174 unsigned int i;
Anthony Liguoric227f092009-10-01 16:12:16 -05002175 target_sigset_t target_set;
blueswir15bfb56b2007-10-05 17:01:51 +00002176 sigset_t set;
2177
bellard459a4012007-11-11 19:45:10 +00002178 ucp_addr = env->regwptr[UREG_I0];
2179 if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2180 goto do_sigsegv;
2181
blueswir15bfb56b2007-10-05 17:01:51 +00002182 mcp = &ucp->uc_mcontext;
2183 grp = &mcp->mc_gregs;
2184
2185 /* Skip over the trap instruction, first. */
2186 env->pc = env->npc;
2187 env->npc += 4;
2188
2189 err = 0;
2190
2191 sigprocmask(0, NULL, &set);
2192 host_to_target_sigset_internal(&target_set, &set);
bellard459a4012007-11-11 19:45:10 +00002193 if (TARGET_NSIG_WORDS == 1) {
bellard579a97f2007-11-11 14:26:47 +00002194 err |= __put_user(target_set.sig[0],
2195 (abi_ulong *)&ucp->uc_sigmask);
bellard459a4012007-11-11 19:45:10 +00002196 } else {
2197 abi_ulong *src, *dst;
2198 src = target_set.sig;
2199 dst = ucp->uc_sigmask.sig;
Anthony Liguoric227f092009-10-01 16:12:16 -05002200 for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
blueswir15bfb56b2007-10-05 17:01:51 +00002201 i++, dst++, src++)
bellard459a4012007-11-11 19:45:10 +00002202 err |= __put_user(*src, dst);
blueswir15bfb56b2007-10-05 17:01:51 +00002203 if (err)
2204 goto do_sigsegv;
2205 }
2206
bellard459a4012007-11-11 19:45:10 +00002207 /* XXX: tstate must be saved properly */
2208 // err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
bellard579a97f2007-11-11 14:26:47 +00002209 err |= __put_user(env->pc, &((*grp)[MC_PC]));
2210 err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2211 err |= __put_user(env->y, &((*grp)[MC_Y]));
2212 err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2213 err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2214 err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2215 err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2216 err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2217 err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2218 err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2219 err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2220 err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2221 err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2222 err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2223 err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2224 err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2225 err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2226 err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
blueswir15bfb56b2007-10-05 17:01:51 +00002227
bellard459a4012007-11-11 19:45:10 +00002228 w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2229 fp = i7 = 0;
2230 if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]),
2231 abi_ulong) != 0)
2232 goto do_sigsegv;
2233 if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]),
2234 abi_ulong) != 0)
2235 goto do_sigsegv;
bellard579a97f2007-11-11 14:26:47 +00002236 err |= __put_user(fp, &(mcp->mc_fp));
2237 err |= __put_user(i7, &(mcp->mc_i7));
blueswir15bfb56b2007-10-05 17:01:51 +00002238
bellard459a4012007-11-11 19:45:10 +00002239 {
2240 uint32_t *src, *dst;
2241 src = env->fpr;
2242 dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2243 /* XXX: check that the CPU storage is the same as user context */
2244 for (i = 0; i < 64; i++, dst++, src++)
2245 err |= __put_user(*src, dst);
2246 }
bellard579a97f2007-11-11 14:26:47 +00002247 err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2248 err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2249 err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
blueswir15bfb56b2007-10-05 17:01:51 +00002250
2251 if (err)
2252 goto do_sigsegv;
bellard459a4012007-11-11 19:45:10 +00002253 unlock_user_struct(ucp, ucp_addr, 1);
blueswir15bfb56b2007-10-05 17:01:51 +00002254 return;
2255 do_sigsegv:
bellard459a4012007-11-11 19:45:10 +00002256 unlock_user_struct(ucp, ucp_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02002257 force_sig(TARGET_SIGSEGV);
blueswir15bfb56b2007-10-05 17:01:51 +00002258}
2259#endif
thsd26bc212007-11-08 18:05:37 +00002260#elif defined(TARGET_ABI_MIPSN64)
ths540635b2007-09-30 01:58:33 +00002261
2262# warning signal handling not implemented
2263
pbrook624f7972008-05-31 16:11:38 +00002264static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002265 target_sigset_t *set, CPUState *env)
ths540635b2007-09-30 01:58:33 +00002266{
2267 fprintf(stderr, "setup_frame: not implemented\n");
2268}
2269
pbrook624f7972008-05-31 16:11:38 +00002270static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002271 target_siginfo_t *info,
2272 target_sigset_t *set, CPUState *env)
ths540635b2007-09-30 01:58:33 +00002273{
2274 fprintf(stderr, "setup_rt_frame: not implemented\n");
2275}
2276
2277long do_sigreturn(CPUState *env)
2278{
2279 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002280 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002281}
2282
2283long do_rt_sigreturn(CPUState *env)
2284{
2285 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002286 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002287}
2288
thsd26bc212007-11-08 18:05:37 +00002289#elif defined(TARGET_ABI_MIPSN32)
ths540635b2007-09-30 01:58:33 +00002290
2291# warning signal handling not implemented
2292
pbrook624f7972008-05-31 16:11:38 +00002293static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002294 target_sigset_t *set, CPUState *env)
ths540635b2007-09-30 01:58:33 +00002295{
2296 fprintf(stderr, "setup_frame: not implemented\n");
2297}
2298
pbrook624f7972008-05-31 16:11:38 +00002299static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002300 target_siginfo_t *info,
2301 target_sigset_t *set, CPUState *env)
ths540635b2007-09-30 01:58:33 +00002302{
2303 fprintf(stderr, "setup_rt_frame: not implemented\n");
2304}
2305
2306long do_sigreturn(CPUState *env)
2307{
2308 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002309 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002310}
2311
2312long do_rt_sigreturn(CPUState *env)
2313{
2314 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00002315 return -TARGET_ENOSYS;
ths540635b2007-09-30 01:58:33 +00002316}
2317
thsd26bc212007-11-08 18:05:37 +00002318#elif defined(TARGET_ABI_MIPSO32)
bellard106ec872006-06-27 21:08:10 +00002319
2320struct target_sigcontext {
2321 uint32_t sc_regmask; /* Unused */
2322 uint32_t sc_status;
2323 uint64_t sc_pc;
2324 uint64_t sc_regs[32];
2325 uint64_t sc_fpregs[32];
2326 uint32_t sc_ownedfp; /* Unused */
2327 uint32_t sc_fpc_csr;
2328 uint32_t sc_fpc_eir; /* Unused */
2329 uint32_t sc_used_math;
2330 uint32_t sc_dsp; /* dsp status, was sc_ssflags */
Paul Brook94c54952009-07-09 18:40:15 +01002331 uint32_t pad0;
bellard106ec872006-06-27 21:08:10 +00002332 uint64_t sc_mdhi;
2333 uint64_t sc_mdlo;
2334 target_ulong sc_hi1; /* Was sc_cause */
2335 target_ulong sc_lo1; /* Was sc_badvaddr */
2336 target_ulong sc_hi2; /* Was sc_sigset[4] */
2337 target_ulong sc_lo2;
2338 target_ulong sc_hi3;
2339 target_ulong sc_lo3;
2340};
2341
2342struct sigframe {
2343 uint32_t sf_ass[4]; /* argument save space for o32 */
2344 uint32_t sf_code[2]; /* signal trampoline */
2345 struct target_sigcontext sf_sc;
Anthony Liguoric227f092009-10-01 16:12:16 -05002346 target_sigset_t sf_mask;
bellard106ec872006-06-27 21:08:10 +00002347};
2348
pbrook0b1bcb02009-04-21 01:41:10 +00002349struct target_ucontext {
2350 target_ulong uc_flags;
2351 target_ulong uc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05002352 target_stack_t uc_stack;
Paul Brook94c54952009-07-09 18:40:15 +01002353 target_ulong pad0;
pbrook0b1bcb02009-04-21 01:41:10 +00002354 struct target_sigcontext uc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05002355 target_sigset_t uc_sigmask;
pbrook0b1bcb02009-04-21 01:41:10 +00002356};
2357
2358struct target_rt_sigframe {
2359 uint32_t rs_ass[4]; /* argument save space for o32 */
2360 uint32_t rs_code[2]; /* signal trampoline */
2361 struct target_siginfo rs_info;
2362 struct target_ucontext rs_uc;
2363};
2364
bellard106ec872006-06-27 21:08:10 +00002365/* Install trampoline to jump back from signal handler */
2366static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall)
2367{
2368 int err;
2369
2370 /*
2371 * Set up the return code ...
2372 *
2373 * li v0, __NR__foo_sigreturn
2374 * syscall
2375 */
2376
2377 err = __put_user(0x24020000 + syscall, tramp + 0);
2378 err |= __put_user(0x0000000c , tramp + 1);
2379 /* flush_cache_sigtramp((unsigned long) tramp); */
2380 return err;
2381}
2382
2383static inline int
2384setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2385{
2386 int err = 0;
2387
thsb5dc7732008-06-27 10:02:35 +00002388 err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
bellard106ec872006-06-27 21:08:10 +00002389
thsb5dc7732008-06-27 10:02:35 +00002390#define save_gp_reg(i) do { \
2391 err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002392 } while(0)
2393 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2394 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2395 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2396 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2397 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2398 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2399 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2400 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2401 save_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002402#undef save_gp_reg
bellard106ec872006-06-27 21:08:10 +00002403
thsb5dc7732008-06-27 10:02:35 +00002404 err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2405 err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002406
2407 /* Not used yet, but might be useful if we ever have DSP suppport */
2408#if 0
2409 if (cpu_has_dsp) {
2410 err |= __put_user(mfhi1(), &sc->sc_hi1);
2411 err |= __put_user(mflo1(), &sc->sc_lo1);
2412 err |= __put_user(mfhi2(), &sc->sc_hi2);
2413 err |= __put_user(mflo2(), &sc->sc_lo2);
2414 err |= __put_user(mfhi3(), &sc->sc_hi3);
2415 err |= __put_user(mflo3(), &sc->sc_lo3);
2416 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2417 }
2418 /* same with 64 bit */
ths388bb212007-05-13 13:58:00 +00002419#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002420 err |= __put_user(regs->hi, &sc->sc_hi[0]);
2421 err |= __put_user(regs->lo, &sc->sc_lo[0]);
2422 if (cpu_has_dsp) {
2423 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2424 err |= __put_user(mflo1(), &sc->sc_lo[1]);
2425 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2426 err |= __put_user(mflo2(), &sc->sc_lo[2]);
2427 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2428 err |= __put_user(mflo3(), &sc->sc_lo[3]);
2429 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2430 }
ths388bb212007-05-13 13:58:00 +00002431#endif
2432#endif
bellard106ec872006-06-27 21:08:10 +00002433
ths388bb212007-05-13 13:58:00 +00002434#if 0
bellard106ec872006-06-27 21:08:10 +00002435 err |= __put_user(!!used_math(), &sc->sc_used_math);
2436
2437 if (!used_math())
2438 goto out;
2439
2440 /*
2441 * Save FPU state to signal context. Signal handler will "inherit"
2442 * current FPU state.
2443 */
2444 preempt_disable();
2445
2446 if (!is_fpu_owner()) {
2447 own_fpu();
2448 restore_fp(current);
2449 }
2450 err |= save_fp_context(sc);
2451
2452 preempt_enable();
2453 out:
2454#endif
2455 return err;
2456}
2457
2458static inline int
2459restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2460{
2461 int err = 0;
2462
2463 err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2464
thsb5dc7732008-06-27 10:02:35 +00002465 err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2466 err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
bellard106ec872006-06-27 21:08:10 +00002467
thsead93602007-09-06 00:18:15 +00002468#define restore_gp_reg(i) do { \
thsb5dc7732008-06-27 10:02:35 +00002469 err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]); \
bellard106ec872006-06-27 21:08:10 +00002470 } while(0)
2471 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2472 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2473 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2474 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2475 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2476 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2477 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2478 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2479 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2480 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2481 restore_gp_reg(31);
ths388bb212007-05-13 13:58:00 +00002482#undef restore_gp_reg
bellard106ec872006-06-27 21:08:10 +00002483
2484#if 0
2485 if (cpu_has_dsp) {
2486 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2487 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2488 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2489 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2490 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2491 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2492 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2493 }
ths388bb212007-05-13 13:58:00 +00002494#ifdef CONFIG_64BIT
bellard106ec872006-06-27 21:08:10 +00002495 err |= __get_user(regs->hi, &sc->sc_hi[0]);
2496 err |= __get_user(regs->lo, &sc->sc_lo[0]);
2497 if (cpu_has_dsp) {
2498 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2499 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2500 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2501 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2502 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2503 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2504 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2505 }
ths388bb212007-05-13 13:58:00 +00002506#endif
bellard106ec872006-06-27 21:08:10 +00002507
2508 err |= __get_user(used_math, &sc->sc_used_math);
2509 conditional_used_math(used_math);
2510
2511 preempt_disable();
2512
2513 if (used_math()) {
2514 /* restore fpu context if we have used it before */
2515 own_fpu();
2516 err |= restore_fp_context(sc);
2517 } else {
2518 /* signal handler may have used FPU. Give it up. */
2519 lose_fpu();
2520 }
2521
2522 preempt_enable();
2523#endif
2524 return err;
2525}
2526/*
2527 * Determine which stack to use..
2528 */
bellard579a97f2007-11-11 14:26:47 +00002529static inline abi_ulong
pbrook624f7972008-05-31 16:11:38 +00002530get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
bellard106ec872006-06-27 21:08:10 +00002531{
2532 unsigned long sp;
2533
2534 /* Default to using normal stack */
thsb5dc7732008-06-27 10:02:35 +00002535 sp = regs->active_tc.gpr[29];
bellard106ec872006-06-27 21:08:10 +00002536
2537 /*
2538 * FPU emulator may have it's own trampoline active just
2539 * above the user stack, 16-bytes before the next lowest
2540 * 16 byte boundary. Try to avoid trashing it.
2541 */
2542 sp -= 32;
2543
bellard106ec872006-06-27 21:08:10 +00002544 /* This is the X/Open sanctioned signal stack switching. */
pbrook624f7972008-05-31 16:11:38 +00002545 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
thsa04e1342007-09-27 13:57:58 +00002546 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2547 }
bellard106ec872006-06-27 21:08:10 +00002548
bellard579a97f2007-11-11 14:26:47 +00002549 return (sp - frame_size) & ~7;
bellard106ec872006-06-27 21:08:10 +00002550}
2551
bellard579a97f2007-11-11 14:26:47 +00002552/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
pbrook624f7972008-05-31 16:11:38 +00002553static void setup_frame(int sig, struct target_sigaction * ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002554 target_sigset_t *set, CPUState *regs)
bellard106ec872006-06-27 21:08:10 +00002555{
2556 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002557 abi_ulong frame_addr;
bellard106ec872006-06-27 21:08:10 +00002558 int i;
2559
bellard579a97f2007-11-11 14:26:47 +00002560 frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2561 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
bellard106ec872006-06-27 21:08:10 +00002562 goto give_sigsegv;
2563
2564 install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2565
2566 if(setup_sigcontext(regs, &frame->sf_sc))
2567 goto give_sigsegv;
2568
2569 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2570 if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2571 goto give_sigsegv;
2572 }
2573
2574 /*
2575 * Arguments to signal handler:
2576 *
2577 * a0 = signal number
2578 * a1 = 0 (should be cause)
2579 * a2 = pointer to struct sigcontext
2580 *
2581 * $25 and PC point to the signal handler, $29 points to the
2582 * struct sigframe.
2583 */
thsb5dc7732008-06-27 10:02:35 +00002584 regs->active_tc.gpr[ 4] = sig;
2585 regs->active_tc.gpr[ 5] = 0;
2586 regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2587 regs->active_tc.gpr[29] = frame_addr;
2588 regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
bellard106ec872006-06-27 21:08:10 +00002589 /* The original kernel code sets CP0_EPC to the handler
2590 * since it returns to userland using eret
2591 * we cannot do this here, and we must set PC directly */
thsb5dc7732008-06-27 10:02:35 +00002592 regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
bellard579a97f2007-11-11 14:26:47 +00002593 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002594 return;
2595
2596give_sigsegv:
bellard579a97f2007-11-11 14:26:47 +00002597 unlock_user_struct(frame, frame_addr, 1);
bellard106ec872006-06-27 21:08:10 +00002598 force_sig(TARGET_SIGSEGV/*, current*/);
ths5fafdf22007-09-16 21:08:06 +00002599 return;
bellard106ec872006-06-27 21:08:10 +00002600}
2601
2602long do_sigreturn(CPUState *regs)
2603{
ths388bb212007-05-13 13:58:00 +00002604 struct sigframe *frame;
bellard579a97f2007-11-11 14:26:47 +00002605 abi_ulong frame_addr;
ths388bb212007-05-13 13:58:00 +00002606 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05002607 target_sigset_t target_set;
ths388bb212007-05-13 13:58:00 +00002608 int i;
bellard106ec872006-06-27 21:08:10 +00002609
2610#if defined(DEBUG_SIGNAL)
ths388bb212007-05-13 13:58:00 +00002611 fprintf(stderr, "do_sigreturn\n");
bellard106ec872006-06-27 21:08:10 +00002612#endif
thsb5dc7732008-06-27 10:02:35 +00002613 frame_addr = regs->active_tc.gpr[29];
bellard579a97f2007-11-11 14:26:47 +00002614 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
bellard106ec872006-06-27 21:08:10 +00002615 goto badframe;
2616
ths388bb212007-05-13 13:58:00 +00002617 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
bellard106ec872006-06-27 21:08:10 +00002618 if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2619 goto badframe;
ths388bb212007-05-13 13:58:00 +00002620 }
bellard106ec872006-06-27 21:08:10 +00002621
ths388bb212007-05-13 13:58:00 +00002622 target_to_host_sigset_internal(&blocked, &target_set);
2623 sigprocmask(SIG_SETMASK, &blocked, NULL);
bellard106ec872006-06-27 21:08:10 +00002624
ths388bb212007-05-13 13:58:00 +00002625 if (restore_sigcontext(regs, &frame->sf_sc))
bellard106ec872006-06-27 21:08:10 +00002626 goto badframe;
2627
2628#if 0
ths388bb212007-05-13 13:58:00 +00002629 /*
2630 * Don't let your children do this ...
2631 */
2632 __asm__ __volatile__(
bellard106ec872006-06-27 21:08:10 +00002633 "move\t$29, %0\n\t"
2634 "j\tsyscall_exit"
2635 :/* no outputs */
2636 :"r" (&regs));
ths388bb212007-05-13 13:58:00 +00002637 /* Unreached */
bellard106ec872006-06-27 21:08:10 +00002638#endif
ths3b46e622007-09-17 08:09:54 +00002639
thsb5dc7732008-06-27 10:02:35 +00002640 regs->active_tc.PC = regs->CP0_EPC;
ths388bb212007-05-13 13:58:00 +00002641 /* I am not sure this is right, but it seems to work
bellard106ec872006-06-27 21:08:10 +00002642 * maybe a problem with nested signals ? */
2643 regs->CP0_EPC = 0;
pbrook0b1bcb02009-04-21 01:41:10 +00002644 return -TARGET_QEMU_ESIGRETURN;
bellard106ec872006-06-27 21:08:10 +00002645
2646badframe:
ths388bb212007-05-13 13:58:00 +00002647 force_sig(TARGET_SIGSEGV/*, current*/);
2648 return 0;
bellard106ec872006-06-27 21:08:10 +00002649}
2650
pbrook624f7972008-05-31 16:11:38 +00002651static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002652 target_siginfo_t *info,
2653 target_sigset_t *set, CPUState *env)
bellard106ec872006-06-27 21:08:10 +00002654{
pbrook0b1bcb02009-04-21 01:41:10 +00002655 struct target_rt_sigframe *frame;
2656 abi_ulong frame_addr;
2657 int i;
2658
2659 frame_addr = get_sigframe(ka, env, sizeof(*frame));
2660 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2661 goto give_sigsegv;
2662
2663 install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2664
2665 copy_siginfo_to_user(&frame->rs_info, info);
2666
2667 __put_user(0, &frame->rs_uc.uc_flags);
2668 __put_user(0, &frame->rs_uc.uc_link);
2669 __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.uc_stack.ss_sp);
2670 __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.uc_stack.ss_size);
2671 __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2672 &frame->rs_uc.uc_stack.ss_flags);
2673
2674 setup_sigcontext(env, &frame->rs_uc.uc_mcontext);
2675
2676 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2677 __put_user(set->sig[i], &frame->rs_uc.uc_sigmask.sig[i]);
2678 }
2679
2680 /*
2681 * Arguments to signal handler:
2682 *
2683 * a0 = signal number
2684 * a1 = pointer to struct siginfo
2685 * a2 = pointer to struct ucontext
2686 *
2687 * $25 and PC point to the signal handler, $29 points to the
2688 * struct sigframe.
2689 */
2690 env->active_tc.gpr[ 4] = sig;
2691 env->active_tc.gpr[ 5] = frame_addr
2692 + offsetof(struct target_rt_sigframe, rs_info);
2693 env->active_tc.gpr[ 6] = frame_addr
2694 + offsetof(struct target_rt_sigframe, rs_uc);
2695 env->active_tc.gpr[29] = frame_addr;
2696 env->active_tc.gpr[31] = frame_addr
2697 + offsetof(struct target_rt_sigframe, rs_code);
2698 /* The original kernel code sets CP0_EPC to the handler
2699 * since it returns to userland using eret
2700 * we cannot do this here, and we must set PC directly */
2701 env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2702 unlock_user_struct(frame, frame_addr, 1);
2703 return;
2704
2705give_sigsegv:
2706 unlock_user_struct(frame, frame_addr, 1);
2707 force_sig(TARGET_SIGSEGV/*, current*/);
2708 return;
bellard106ec872006-06-27 21:08:10 +00002709}
2710
2711long do_rt_sigreturn(CPUState *env)
2712{
pbrook0b1bcb02009-04-21 01:41:10 +00002713 struct target_rt_sigframe *frame;
2714 abi_ulong frame_addr;
2715 sigset_t blocked;
2716
2717#if defined(DEBUG_SIGNAL)
2718 fprintf(stderr, "do_rt_sigreturn\n");
2719#endif
2720 frame_addr = env->active_tc.gpr[29];
2721 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2722 goto badframe;
2723
2724 target_to_host_sigset(&blocked, &frame->rs_uc.uc_sigmask);
2725 sigprocmask(SIG_SETMASK, &blocked, NULL);
2726
2727 if (restore_sigcontext(env, &frame->rs_uc.uc_mcontext))
2728 goto badframe;
2729
2730 if (do_sigaltstack(frame_addr +
2731 offsetof(struct target_rt_sigframe, rs_uc.uc_stack),
2732 0, get_sp_from_cpustate(env)) == -EFAULT)
2733 goto badframe;
2734
2735 env->active_tc.PC = env->CP0_EPC;
2736 /* I am not sure this is right, but it seems to work
2737 * maybe a problem with nested signals ? */
2738 env->CP0_EPC = 0;
2739 return -TARGET_QEMU_ESIGRETURN;
2740
2741badframe:
2742 force_sig(TARGET_SIGSEGV/*, current*/);
2743 return 0;
bellard106ec872006-06-27 21:08:10 +00002744}
bellard6d5e2162004-09-30 22:04:13 +00002745
thsc3b5bc82007-12-02 06:31:25 +00002746#elif defined(TARGET_SH4)
2747
2748/*
2749 * code and data structures from linux kernel:
2750 * include/asm-sh/sigcontext.h
2751 * arch/sh/kernel/signal.c
2752 */
2753
2754struct target_sigcontext {
2755 target_ulong oldmask;
2756
2757 /* CPU registers */
2758 target_ulong sc_gregs[16];
2759 target_ulong sc_pc;
2760 target_ulong sc_pr;
2761 target_ulong sc_sr;
2762 target_ulong sc_gbr;
2763 target_ulong sc_mach;
2764 target_ulong sc_macl;
2765
2766 /* FPU registers */
2767 target_ulong sc_fpregs[16];
2768 target_ulong sc_xfpregs[16];
2769 unsigned int sc_fpscr;
2770 unsigned int sc_fpul;
2771 unsigned int sc_ownedfp;
2772};
2773
2774struct target_sigframe
2775{
2776 struct target_sigcontext sc;
2777 target_ulong extramask[TARGET_NSIG_WORDS-1];
2778 uint16_t retcode[3];
2779};
2780
2781
2782struct target_ucontext {
2783 target_ulong uc_flags;
2784 struct target_ucontext *uc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05002785 target_stack_t uc_stack;
thsc3b5bc82007-12-02 06:31:25 +00002786 struct target_sigcontext uc_mcontext;
Anthony Liguoric227f092009-10-01 16:12:16 -05002787 target_sigset_t uc_sigmask; /* mask last for extensibility */
thsc3b5bc82007-12-02 06:31:25 +00002788};
2789
2790struct target_rt_sigframe
2791{
2792 struct target_siginfo info;
2793 struct target_ucontext uc;
2794 uint16_t retcode[3];
2795};
2796
2797
2798#define MOVW(n) (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2799#define TRAP_NOARG 0xc310 /* Syscall w/no args (NR in R3) SH3/4 */
2800
pbrook624f7972008-05-31 16:11:38 +00002801static abi_ulong get_sigframe(struct target_sigaction *ka,
thsc3b5bc82007-12-02 06:31:25 +00002802 unsigned long sp, size_t frame_size)
2803{
pbrook624f7972008-05-31 16:11:38 +00002804 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
thsc3b5bc82007-12-02 06:31:25 +00002805 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2806 }
2807
2808 return (sp - frame_size) & -8ul;
2809}
2810
2811static int setup_sigcontext(struct target_sigcontext *sc,
2812 CPUState *regs, unsigned long mask)
2813{
2814 int err = 0;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002815 int i;
thsc3b5bc82007-12-02 06:31:25 +00002816
2817#define COPY(x) err |= __put_user(regs->x, &sc->sc_##x)
2818 COPY(gregs[0]); COPY(gregs[1]);
2819 COPY(gregs[2]); COPY(gregs[3]);
2820 COPY(gregs[4]); COPY(gregs[5]);
2821 COPY(gregs[6]); COPY(gregs[7]);
2822 COPY(gregs[8]); COPY(gregs[9]);
2823 COPY(gregs[10]); COPY(gregs[11]);
2824 COPY(gregs[12]); COPY(gregs[13]);
2825 COPY(gregs[14]); COPY(gregs[15]);
2826 COPY(gbr); COPY(mach);
2827 COPY(macl); COPY(pr);
2828 COPY(sr); COPY(pc);
2829#undef COPY
2830
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002831 for (i=0; i<16; i++) {
2832 err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2833 }
2834 err |= __put_user(regs->fpscr, &sc->sc_fpscr);
2835 err |= __put_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00002836
2837 /* non-iBCS2 extensions.. */
2838 err |= __put_user(mask, &sc->oldmask);
2839
2840 return err;
2841}
2842
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002843static int restore_sigcontext(CPUState *regs, struct target_sigcontext *sc,
2844 target_ulong *r0_p)
thsc3b5bc82007-12-02 06:31:25 +00002845{
2846 unsigned int err = 0;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002847 int i;
thsc3b5bc82007-12-02 06:31:25 +00002848
2849#define COPY(x) err |= __get_user(regs->x, &sc->sc_##x)
2850 COPY(gregs[1]);
2851 COPY(gregs[2]); COPY(gregs[3]);
2852 COPY(gregs[4]); COPY(gregs[5]);
2853 COPY(gregs[6]); COPY(gregs[7]);
2854 COPY(gregs[8]); COPY(gregs[9]);
2855 COPY(gregs[10]); COPY(gregs[11]);
2856 COPY(gregs[12]); COPY(gregs[13]);
2857 COPY(gregs[14]); COPY(gregs[15]);
2858 COPY(gbr); COPY(mach);
2859 COPY(macl); COPY(pr);
2860 COPY(sr); COPY(pc);
2861#undef COPY
2862
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002863 for (i=0; i<16; i++) {
2864 err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
2865 }
2866 err |= __get_user(regs->fpscr, &sc->sc_fpscr);
2867 err |= __get_user(regs->fpul, &sc->sc_fpul);
thsc3b5bc82007-12-02 06:31:25 +00002868
2869 regs->tra = -1; /* disable syscall checks */
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002870 err |= __get_user(*r0_p, &sc->sc_gregs[0]);
thsc3b5bc82007-12-02 06:31:25 +00002871 return err;
2872}
2873
pbrook624f7972008-05-31 16:11:38 +00002874static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002875 target_sigset_t *set, CPUState *regs)
thsc3b5bc82007-12-02 06:31:25 +00002876{
2877 struct target_sigframe *frame;
2878 abi_ulong frame_addr;
2879 int i;
2880 int err = 0;
2881 int signal;
2882
2883 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2884 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2885 goto give_sigsegv;
2886
2887 signal = current_exec_domain_sig(sig);
2888
2889 err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2890
2891 for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2892 err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2893 }
2894
2895 /* Set up to return from userspace. If provided, use a stub
2896 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002897 if (ka->sa_flags & TARGET_SA_RESTORER) {
2898 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002899 } else {
2900 /* Generate return code (system call to sigreturn) */
2901 err |= __put_user(MOVW(2), &frame->retcode[0]);
2902 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2903 err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2904 regs->pr = (unsigned long) frame->retcode;
2905 }
2906
2907 if (err)
2908 goto give_sigsegv;
2909
2910 /* Set up registers for signal handler */
2911 regs->gregs[15] = (unsigned long) frame;
2912 regs->gregs[4] = signal; /* Arg for signal handler */
2913 regs->gregs[5] = 0;
2914 regs->gregs[6] = (unsigned long) &frame->sc;
pbrook624f7972008-05-31 16:11:38 +00002915 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002916
2917 unlock_user_struct(frame, frame_addr, 1);
2918 return;
2919
2920give_sigsegv:
2921 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02002922 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00002923}
2924
pbrook624f7972008-05-31 16:11:38 +00002925static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05002926 target_siginfo_t *info,
2927 target_sigset_t *set, CPUState *regs)
thsc3b5bc82007-12-02 06:31:25 +00002928{
2929 struct target_rt_sigframe *frame;
2930 abi_ulong frame_addr;
2931 int i;
2932 int err = 0;
2933 int signal;
2934
2935 frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2936 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2937 goto give_sigsegv;
2938
2939 signal = current_exec_domain_sig(sig);
2940
2941 err |= copy_siginfo_to_user(&frame->info, info);
2942
2943 /* Create the ucontext. */
2944 err |= __put_user(0, &frame->uc.uc_flags);
2945 err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
balrog526ccb72008-07-16 12:13:52 +00002946 err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
thsc3b5bc82007-12-02 06:31:25 +00002947 &frame->uc.uc_stack.ss_sp);
2948 err |= __put_user(sas_ss_flags(regs->gregs[15]),
2949 &frame->uc.uc_stack.ss_flags);
2950 err |= __put_user(target_sigaltstack_used.ss_size,
2951 &frame->uc.uc_stack.ss_size);
2952 err |= setup_sigcontext(&frame->uc.uc_mcontext,
2953 regs, set->sig[0]);
2954 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2955 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2956 }
2957
2958 /* Set up to return from userspace. If provided, use a stub
2959 already in userspace. */
pbrook624f7972008-05-31 16:11:38 +00002960 if (ka->sa_flags & TARGET_SA_RESTORER) {
2961 regs->pr = (unsigned long) ka->sa_restorer;
thsc3b5bc82007-12-02 06:31:25 +00002962 } else {
2963 /* Generate return code (system call to sigreturn) */
2964 err |= __put_user(MOVW(2), &frame->retcode[0]);
2965 err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2966 err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2967 regs->pr = (unsigned long) frame->retcode;
2968 }
2969
2970 if (err)
2971 goto give_sigsegv;
2972
2973 /* Set up registers for signal handler */
2974 regs->gregs[15] = (unsigned long) frame;
2975 regs->gregs[4] = signal; /* Arg for signal handler */
2976 regs->gregs[5] = (unsigned long) &frame->info;
2977 regs->gregs[6] = (unsigned long) &frame->uc;
pbrook624f7972008-05-31 16:11:38 +00002978 regs->pc = (unsigned long) ka->_sa_handler;
thsc3b5bc82007-12-02 06:31:25 +00002979
2980 unlock_user_struct(frame, frame_addr, 1);
2981 return;
2982
2983give_sigsegv:
2984 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02002985 force_sig(TARGET_SIGSEGV);
thsc3b5bc82007-12-02 06:31:25 +00002986}
2987
2988long do_sigreturn(CPUState *regs)
2989{
2990 struct target_sigframe *frame;
2991 abi_ulong frame_addr;
2992 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05002993 target_sigset_t target_set;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09002994 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00002995 int i;
2996 int err = 0;
2997
2998#if defined(DEBUG_SIGNAL)
2999 fprintf(stderr, "do_sigreturn\n");
3000#endif
3001 frame_addr = regs->gregs[15];
3002 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3003 goto badframe;
3004
3005 err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3006 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3007 err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3008 }
3009
3010 if (err)
3011 goto badframe;
3012
3013 target_to_host_sigset_internal(&blocked, &target_set);
3014 sigprocmask(SIG_SETMASK, &blocked, NULL);
3015
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003016 if (restore_sigcontext(regs, &frame->sc, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003017 goto badframe;
3018
3019 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003020 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003021
3022badframe:
3023 unlock_user_struct(frame, frame_addr, 0);
3024 force_sig(TARGET_SIGSEGV);
3025 return 0;
3026}
3027
3028long do_rt_sigreturn(CPUState *regs)
3029{
3030 struct target_rt_sigframe *frame;
3031 abi_ulong frame_addr;
3032 sigset_t blocked;
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003033 target_ulong r0;
thsc3b5bc82007-12-02 06:31:25 +00003034
3035#if defined(DEBUG_SIGNAL)
3036 fprintf(stderr, "do_rt_sigreturn\n");
3037#endif
3038 frame_addr = regs->gregs[15];
3039 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3040 goto badframe;
3041
3042 target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
3043 sigprocmask(SIG_SETMASK, &blocked, NULL);
3044
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003045 if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &r0))
thsc3b5bc82007-12-02 06:31:25 +00003046 goto badframe;
3047
3048 if (do_sigaltstack(frame_addr +
3049 offsetof(struct target_rt_sigframe, uc.uc_stack),
3050 0, get_sp_from_cpustate(regs)) == -EFAULT)
3051 goto badframe;
3052
3053 unlock_user_struct(frame, frame_addr, 0);
takasi-y@ops.dti.ne.jpd8714432010-02-18 00:46:45 +09003054 return r0;
thsc3b5bc82007-12-02 06:31:25 +00003055
3056badframe:
3057 unlock_user_struct(frame, frame_addr, 0);
3058 force_sig(TARGET_SIGSEGV);
3059 return 0;
3060}
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003061#elif defined(TARGET_MICROBLAZE)
3062
3063struct target_sigcontext {
3064 struct target_pt_regs regs; /* needs to be first */
3065 uint32_t oldmask;
3066};
3067
3068/* Signal frames. */
3069struct target_signal_frame {
3070 struct target_sigcontext sc;
3071 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3072 uint32_t tramp[2];
3073};
3074
3075struct rt_signal_frame {
3076 struct siginfo info;
3077 struct ucontext uc;
3078 uint32_t tramp[2];
3079};
3080
3081static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3082{
3083 __put_user(env->regs[0], &sc->regs.r0);
3084 __put_user(env->regs[1], &sc->regs.r1);
3085 __put_user(env->regs[2], &sc->regs.r2);
3086 __put_user(env->regs[3], &sc->regs.r3);
3087 __put_user(env->regs[4], &sc->regs.r4);
3088 __put_user(env->regs[5], &sc->regs.r5);
3089 __put_user(env->regs[6], &sc->regs.r6);
3090 __put_user(env->regs[7], &sc->regs.r7);
3091 __put_user(env->regs[8], &sc->regs.r8);
3092 __put_user(env->regs[9], &sc->regs.r9);
3093 __put_user(env->regs[10], &sc->regs.r10);
3094 __put_user(env->regs[11], &sc->regs.r11);
3095 __put_user(env->regs[12], &sc->regs.r12);
3096 __put_user(env->regs[13], &sc->regs.r13);
3097 __put_user(env->regs[14], &sc->regs.r14);
3098 __put_user(env->regs[15], &sc->regs.r15);
3099 __put_user(env->regs[16], &sc->regs.r16);
3100 __put_user(env->regs[17], &sc->regs.r17);
3101 __put_user(env->regs[18], &sc->regs.r18);
3102 __put_user(env->regs[19], &sc->regs.r19);
3103 __put_user(env->regs[20], &sc->regs.r20);
3104 __put_user(env->regs[21], &sc->regs.r21);
3105 __put_user(env->regs[22], &sc->regs.r22);
3106 __put_user(env->regs[23], &sc->regs.r23);
3107 __put_user(env->regs[24], &sc->regs.r24);
3108 __put_user(env->regs[25], &sc->regs.r25);
3109 __put_user(env->regs[26], &sc->regs.r26);
3110 __put_user(env->regs[27], &sc->regs.r27);
3111 __put_user(env->regs[28], &sc->regs.r28);
3112 __put_user(env->regs[29], &sc->regs.r29);
3113 __put_user(env->regs[30], &sc->regs.r30);
3114 __put_user(env->regs[31], &sc->regs.r31);
3115 __put_user(env->sregs[SR_PC], &sc->regs.pc);
3116}
3117
3118static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3119{
3120 __get_user(env->regs[0], &sc->regs.r0);
3121 __get_user(env->regs[1], &sc->regs.r1);
3122 __get_user(env->regs[2], &sc->regs.r2);
3123 __get_user(env->regs[3], &sc->regs.r3);
3124 __get_user(env->regs[4], &sc->regs.r4);
3125 __get_user(env->regs[5], &sc->regs.r5);
3126 __get_user(env->regs[6], &sc->regs.r6);
3127 __get_user(env->regs[7], &sc->regs.r7);
3128 __get_user(env->regs[8], &sc->regs.r8);
3129 __get_user(env->regs[9], &sc->regs.r9);
3130 __get_user(env->regs[10], &sc->regs.r10);
3131 __get_user(env->regs[11], &sc->regs.r11);
3132 __get_user(env->regs[12], &sc->regs.r12);
3133 __get_user(env->regs[13], &sc->regs.r13);
3134 __get_user(env->regs[14], &sc->regs.r14);
3135 __get_user(env->regs[15], &sc->regs.r15);
3136 __get_user(env->regs[16], &sc->regs.r16);
3137 __get_user(env->regs[17], &sc->regs.r17);
3138 __get_user(env->regs[18], &sc->regs.r18);
3139 __get_user(env->regs[19], &sc->regs.r19);
3140 __get_user(env->regs[20], &sc->regs.r20);
3141 __get_user(env->regs[21], &sc->regs.r21);
3142 __get_user(env->regs[22], &sc->regs.r22);
3143 __get_user(env->regs[23], &sc->regs.r23);
3144 __get_user(env->regs[24], &sc->regs.r24);
3145 __get_user(env->regs[25], &sc->regs.r25);
3146 __get_user(env->regs[26], &sc->regs.r26);
3147 __get_user(env->regs[27], &sc->regs.r27);
3148 __get_user(env->regs[28], &sc->regs.r28);
3149 __get_user(env->regs[29], &sc->regs.r29);
3150 __get_user(env->regs[30], &sc->regs.r30);
3151 __get_user(env->regs[31], &sc->regs.r31);
3152 __get_user(env->sregs[SR_PC], &sc->regs.pc);
3153}
3154
3155static abi_ulong get_sigframe(struct target_sigaction *ka,
3156 CPUState *env, int frame_size)
3157{
3158 abi_ulong sp = env->regs[1];
3159
3160 if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3161 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3162
3163 return ((sp - frame_size) & -8UL);
3164}
3165
3166static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003167 target_sigset_t *set, CPUState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003168{
3169 struct target_signal_frame *frame;
3170 abi_ulong frame_addr;
3171 int err = 0;
3172 int i;
3173
3174 frame_addr = get_sigframe(ka, env, sizeof *frame);
3175 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3176 goto badframe;
3177
3178 /* Save the mask. */
3179 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3180 if (err)
3181 goto badframe;
3182
3183 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3184 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3185 goto badframe;
3186 }
3187
3188 setup_sigcontext(&frame->sc, env);
3189
3190 /* Set up to return from userspace. If provided, use a stub
3191 already in userspace. */
3192 /* minus 8 is offset to cater for "rtsd r15,8" offset */
3193 if (ka->sa_flags & TARGET_SA_RESTORER) {
3194 env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3195 } else {
3196 uint32_t t;
3197 /* Note, these encodings are _big endian_! */
3198 /* addi r12, r0, __NR_sigreturn */
3199 t = 0x31800000UL | TARGET_NR_sigreturn;
3200 err |= __put_user(t, frame->tramp + 0);
3201 /* brki r14, 0x8 */
3202 t = 0xb9cc0008UL;
3203 err |= __put_user(t, frame->tramp + 1);
3204
3205 /* Return from sighandler will jump to the tramp.
3206 Negative 8 offset because return is rtsd r15, 8 */
3207 env->regs[15] = ((unsigned long)frame->tramp) - 8;
3208 }
3209
3210 if (err)
3211 goto badframe;
3212
3213 /* Set up registers for signal handler */
3214 env->regs[1] = (unsigned long) frame;
3215 /* Signal handler args: */
3216 env->regs[5] = sig; /* Arg 0: signum */
3217 env->regs[6] = (unsigned long) &frame->sc; /* arg 1: sigcontext */
3218
3219 /* Offset of 4 to handle microblaze rtid r14, 0 */
3220 env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3221
3222 unlock_user_struct(frame, frame_addr, 1);
3223 return;
3224 badframe:
3225 unlock_user_struct(frame, frame_addr, 1);
3226 force_sig(TARGET_SIGSEGV);
3227}
3228
3229static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003230 target_siginfo_t *info,
3231 target_sigset_t *set, CPUState *env)
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003232{
3233 fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3234}
3235
3236long do_sigreturn(CPUState *env)
3237{
3238 struct target_signal_frame *frame;
3239 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003240 target_sigset_t target_set;
Edgar E. Iglesiasb779e292009-05-20 21:31:33 +02003241 sigset_t set;
3242 int i;
3243
3244 frame_addr = env->regs[R_SP];
3245 /* Make sure the guest isn't playing games. */
3246 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3247 goto badframe;
3248
3249 /* Restore blocked signals */
3250 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3251 goto badframe;
3252 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3253 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3254 goto badframe;
3255 }
3256 target_to_host_sigset_internal(&set, &target_set);
3257 sigprocmask(SIG_SETMASK, &set, NULL);
3258
3259 restore_sigcontext(&frame->sc, env);
3260 /* We got here through a sigreturn syscall, our path back is via an
3261 rtb insn so setup r14 for that. */
3262 env->regs[14] = env->sregs[SR_PC];
3263
3264 unlock_user_struct(frame, frame_addr, 0);
3265 return env->regs[10];
3266 badframe:
3267 unlock_user_struct(frame, frame_addr, 0);
3268 force_sig(TARGET_SIGSEGV);
3269}
3270
3271long do_rt_sigreturn(CPUState *env)
3272{
3273 fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3274 return -TARGET_ENOSYS;
3275}
3276
edgar_iglb6d3abd2008-02-28 11:29:27 +00003277#elif defined(TARGET_CRIS)
3278
3279struct target_sigcontext {
3280 struct target_pt_regs regs; /* needs to be first */
3281 uint32_t oldmask;
3282 uint32_t usp; /* usp before stacking this gunk on it */
3283};
3284
3285/* Signal frames. */
3286struct target_signal_frame {
3287 struct target_sigcontext sc;
3288 uint32_t extramask[TARGET_NSIG_WORDS - 1];
3289 uint8_t retcode[8]; /* Trampoline code. */
3290};
3291
3292struct rt_signal_frame {
3293 struct siginfo *pinfo;
3294 void *puc;
3295 struct siginfo info;
3296 struct ucontext uc;
3297 uint8_t retcode[8]; /* Trampoline code. */
3298};
3299
3300static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3301{
edgar_igl9664d922008-03-03 22:23:53 +00003302 __put_user(env->regs[0], &sc->regs.r0);
3303 __put_user(env->regs[1], &sc->regs.r1);
3304 __put_user(env->regs[2], &sc->regs.r2);
3305 __put_user(env->regs[3], &sc->regs.r3);
3306 __put_user(env->regs[4], &sc->regs.r4);
3307 __put_user(env->regs[5], &sc->regs.r5);
3308 __put_user(env->regs[6], &sc->regs.r6);
3309 __put_user(env->regs[7], &sc->regs.r7);
3310 __put_user(env->regs[8], &sc->regs.r8);
3311 __put_user(env->regs[9], &sc->regs.r9);
3312 __put_user(env->regs[10], &sc->regs.r10);
3313 __put_user(env->regs[11], &sc->regs.r11);
3314 __put_user(env->regs[12], &sc->regs.r12);
3315 __put_user(env->regs[13], &sc->regs.r13);
3316 __put_user(env->regs[14], &sc->usp);
3317 __put_user(env->regs[15], &sc->regs.acr);
3318 __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3319 __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3320 __put_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003321}
edgar_igl9664d922008-03-03 22:23:53 +00003322
edgar_iglb6d3abd2008-02-28 11:29:27 +00003323static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3324{
edgar_igl9664d922008-03-03 22:23:53 +00003325 __get_user(env->regs[0], &sc->regs.r0);
3326 __get_user(env->regs[1], &sc->regs.r1);
3327 __get_user(env->regs[2], &sc->regs.r2);
3328 __get_user(env->regs[3], &sc->regs.r3);
3329 __get_user(env->regs[4], &sc->regs.r4);
3330 __get_user(env->regs[5], &sc->regs.r5);
3331 __get_user(env->regs[6], &sc->regs.r6);
3332 __get_user(env->regs[7], &sc->regs.r7);
3333 __get_user(env->regs[8], &sc->regs.r8);
3334 __get_user(env->regs[9], &sc->regs.r9);
3335 __get_user(env->regs[10], &sc->regs.r10);
3336 __get_user(env->regs[11], &sc->regs.r11);
3337 __get_user(env->regs[12], &sc->regs.r12);
3338 __get_user(env->regs[13], &sc->regs.r13);
3339 __get_user(env->regs[14], &sc->usp);
3340 __get_user(env->regs[15], &sc->regs.acr);
3341 __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3342 __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3343 __get_user(env->pc, &sc->regs.erp);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003344}
3345
edgar_igl9664d922008-03-03 22:23:53 +00003346static abi_ulong get_sigframe(CPUState *env, int framesize)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003347{
edgar_igl9664d922008-03-03 22:23:53 +00003348 abi_ulong sp;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003349 /* Align the stack downwards to 4. */
edgar_igl9664d922008-03-03 22:23:53 +00003350 sp = (env->regs[R_SP] & ~3);
3351 return sp - framesize;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003352}
3353
pbrook624f7972008-05-31 16:11:38 +00003354static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003355 target_sigset_t *set, CPUState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003356{
3357 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003358 abi_ulong frame_addr;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003359 int err = 0;
3360 int i;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003361
edgar_igl9664d922008-03-03 22:23:53 +00003362 frame_addr = get_sigframe(env, sizeof *frame);
3363 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003364 goto badframe;
3365
3366 /*
3367 * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3368 * use this trampoline anymore but it sets it up for GDB.
3369 * In QEMU, using the trampoline simplifies things a bit so we use it.
3370 *
3371 * This is movu.w __NR_sigreturn, r9; break 13;
3372 */
3373 err |= __put_user(0x9c5f, frame->retcode+0);
3374 err |= __put_user(TARGET_NR_sigreturn,
3375 frame->retcode+2);
3376 err |= __put_user(0xe93d, frame->retcode+4);
3377
3378 /* Save the mask. */
3379 err |= __put_user(set->sig[0], &frame->sc.oldmask);
3380 if (err)
3381 goto badframe;
3382
3383 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3384 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3385 goto badframe;
3386 }
3387
3388 setup_sigcontext(&frame->sc, env);
3389
3390 /* Move the stack and setup the arguments for the handler. */
balrog526ccb72008-07-16 12:13:52 +00003391 env->regs[R_SP] = (uint32_t) (unsigned long) frame;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003392 env->regs[10] = sig;
pbrook624f7972008-05-31 16:11:38 +00003393 env->pc = (unsigned long) ka->_sa_handler;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003394 /* Link SRP so the guest returns through the trampoline. */
balrog526ccb72008-07-16 12:13:52 +00003395 env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003396
edgar_igl9664d922008-03-03 22:23:53 +00003397 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003398 return;
3399 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003400 unlock_user_struct(frame, frame_addr, 1);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003401 force_sig(TARGET_SIGSEGV);
3402}
3403
pbrook624f7972008-05-31 16:11:38 +00003404static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003405 target_siginfo_t *info,
3406 target_sigset_t *set, CPUState *env)
edgar_iglb6d3abd2008-02-28 11:29:27 +00003407{
3408 fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3409}
3410
3411long do_sigreturn(CPUState *env)
3412{
3413 struct target_signal_frame *frame;
edgar_igl9664d922008-03-03 22:23:53 +00003414 abi_ulong frame_addr;
Anthony Liguoric227f092009-10-01 16:12:16 -05003415 target_sigset_t target_set;
edgar_iglb6d3abd2008-02-28 11:29:27 +00003416 sigset_t set;
3417 int i;
3418
edgar_igl9664d922008-03-03 22:23:53 +00003419 frame_addr = env->regs[R_SP];
edgar_iglb6d3abd2008-02-28 11:29:27 +00003420 /* Make sure the guest isn't playing games. */
edgar_igl9664d922008-03-03 22:23:53 +00003421 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
edgar_iglb6d3abd2008-02-28 11:29:27 +00003422 goto badframe;
3423
3424 /* Restore blocked signals */
3425 if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3426 goto badframe;
3427 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3428 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3429 goto badframe;
3430 }
3431 target_to_host_sigset_internal(&set, &target_set);
3432 sigprocmask(SIG_SETMASK, &set, NULL);
3433
3434 restore_sigcontext(&frame->sc, env);
edgar_igl9664d922008-03-03 22:23:53 +00003435 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003436 return env->regs[10];
3437 badframe:
edgar_igl9664d922008-03-03 22:23:53 +00003438 unlock_user_struct(frame, frame_addr, 0);
edgar_iglb6d3abd2008-02-28 11:29:27 +00003439 force_sig(TARGET_SIGSEGV);
3440}
3441
3442long do_rt_sigreturn(CPUState *env)
3443{
3444 fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3445 return -TARGET_ENOSYS;
3446}
thsc3b5bc82007-12-02 06:31:25 +00003447
Nathan Froydbcd49332009-05-12 19:13:18 -07003448#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
3449
3450/* FIXME: Many of the structures are defined for both PPC and PPC64, but
3451 the signal handling is different enough that we haven't implemented
3452 support for PPC64 yet. Hence the restriction above.
3453
3454 There are various #if'd blocks for code for TARGET_PPC64. These
3455 blocks should go away so that we can successfully run 32-bit and
3456 64-bit binaries on a QEMU configured for PPC64. */
3457
3458/* Size of dummy stack frame allocated when calling signal handler.
3459 See arch/powerpc/include/asm/ptrace.h. */
3460#if defined(TARGET_PPC64)
3461#define SIGNAL_FRAMESIZE 128
3462#else
3463#define SIGNAL_FRAMESIZE 64
3464#endif
3465
3466/* See arch/powerpc/include/asm/sigcontext.h. */
3467struct target_sigcontext {
3468 target_ulong _unused[4];
3469 int32_t signal;
3470#if defined(TARGET_PPC64)
3471 int32_t pad0;
3472#endif
3473 target_ulong handler;
3474 target_ulong oldmask;
3475 target_ulong regs; /* struct pt_regs __user * */
3476 /* TODO: PPC64 includes extra bits here. */
3477};
3478
3479/* Indices for target_mcontext.mc_gregs, below.
3480 See arch/powerpc/include/asm/ptrace.h for details. */
3481enum {
3482 TARGET_PT_R0 = 0,
3483 TARGET_PT_R1 = 1,
3484 TARGET_PT_R2 = 2,
3485 TARGET_PT_R3 = 3,
3486 TARGET_PT_R4 = 4,
3487 TARGET_PT_R5 = 5,
3488 TARGET_PT_R6 = 6,
3489 TARGET_PT_R7 = 7,
3490 TARGET_PT_R8 = 8,
3491 TARGET_PT_R9 = 9,
3492 TARGET_PT_R10 = 10,
3493 TARGET_PT_R11 = 11,
3494 TARGET_PT_R12 = 12,
3495 TARGET_PT_R13 = 13,
3496 TARGET_PT_R14 = 14,
3497 TARGET_PT_R15 = 15,
3498 TARGET_PT_R16 = 16,
3499 TARGET_PT_R17 = 17,
3500 TARGET_PT_R18 = 18,
3501 TARGET_PT_R19 = 19,
3502 TARGET_PT_R20 = 20,
3503 TARGET_PT_R21 = 21,
3504 TARGET_PT_R22 = 22,
3505 TARGET_PT_R23 = 23,
3506 TARGET_PT_R24 = 24,
3507 TARGET_PT_R25 = 25,
3508 TARGET_PT_R26 = 26,
3509 TARGET_PT_R27 = 27,
3510 TARGET_PT_R28 = 28,
3511 TARGET_PT_R29 = 29,
3512 TARGET_PT_R30 = 30,
3513 TARGET_PT_R31 = 31,
3514 TARGET_PT_NIP = 32,
3515 TARGET_PT_MSR = 33,
3516 TARGET_PT_ORIG_R3 = 34,
3517 TARGET_PT_CTR = 35,
3518 TARGET_PT_LNK = 36,
3519 TARGET_PT_XER = 37,
3520 TARGET_PT_CCR = 38,
3521 /* Yes, there are two registers with #39. One is 64-bit only. */
3522 TARGET_PT_MQ = 39,
3523 TARGET_PT_SOFTE = 39,
3524 TARGET_PT_TRAP = 40,
3525 TARGET_PT_DAR = 41,
3526 TARGET_PT_DSISR = 42,
3527 TARGET_PT_RESULT = 43,
3528 TARGET_PT_REGS_COUNT = 44
3529};
3530
3531/* See arch/powerpc/include/asm/ucontext.h. Only used for 32-bit PPC;
3532 on 64-bit PPC, sigcontext and mcontext are one and the same. */
3533struct target_mcontext {
3534 target_ulong mc_gregs[48];
3535 /* Includes fpscr. */
3536 uint64_t mc_fregs[33];
3537 target_ulong mc_pad[2];
3538 /* We need to handle Altivec and SPE at the same time, which no
3539 kernel needs to do. Fortunately, the kernel defines this bit to
3540 be Altivec-register-large all the time, rather than trying to
3541 twiddle it based on the specific platform. */
3542 union {
3543 /* SPE vector registers. One extra for SPEFSCR. */
3544 uint32_t spe[33];
3545 /* Altivec vector registers. The packing of VSCR and VRSAVE
3546 varies depending on whether we're PPC64 or not: PPC64 splits
3547 them apart; PPC32 stuffs them together. */
3548#if defined(TARGET_PPC64)
malc3efa9a62009-07-18 13:10:12 +04003549#define QEMU_NVRREG 34
Nathan Froydbcd49332009-05-12 19:13:18 -07003550#else
malc3efa9a62009-07-18 13:10:12 +04003551#define QEMU_NVRREG 33
Nathan Froydbcd49332009-05-12 19:13:18 -07003552#endif
Anthony Liguoric227f092009-10-01 16:12:16 -05003553 ppc_avr_t altivec[QEMU_NVRREG];
malc3efa9a62009-07-18 13:10:12 +04003554#undef QEMU_NVRREG
Nathan Froydbcd49332009-05-12 19:13:18 -07003555 } mc_vregs __attribute__((__aligned__(16)));
3556};
3557
3558struct target_ucontext {
3559 target_ulong uc_flags;
3560 target_ulong uc_link; /* struct ucontext __user * */
3561 struct target_sigaltstack uc_stack;
3562#if !defined(TARGET_PPC64)
3563 int32_t uc_pad[7];
3564 target_ulong uc_regs; /* struct mcontext __user *
3565 points to uc_mcontext field */
3566#endif
Anthony Liguoric227f092009-10-01 16:12:16 -05003567 target_sigset_t uc_sigmask;
Nathan Froydbcd49332009-05-12 19:13:18 -07003568#if defined(TARGET_PPC64)
Anthony Liguoric227f092009-10-01 16:12:16 -05003569 target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
Nathan Froydbcd49332009-05-12 19:13:18 -07003570 struct target_sigcontext uc_mcontext;
3571#else
3572 int32_t uc_maskext[30];
3573 int32_t uc_pad2[3];
3574 struct target_mcontext uc_mcontext;
3575#endif
3576};
3577
3578/* See arch/powerpc/kernel/signal_32.c. */
3579struct target_sigframe {
3580 struct target_sigcontext sctx;
3581 struct target_mcontext mctx;
3582 int32_t abigap[56];
3583};
3584
3585struct target_rt_sigframe {
3586 struct target_siginfo info;
3587 struct target_ucontext uc;
3588 int32_t abigap[56];
3589};
3590
3591/* We use the mc_pad field for the signal return trampoline. */
3592#define tramp mc_pad
3593
3594/* See arch/powerpc/kernel/signal.c. */
3595static target_ulong get_sigframe(struct target_sigaction *ka,
3596 CPUState *env,
3597 int frame_size)
3598{
3599 target_ulong oldsp, newsp;
3600
3601 oldsp = env->gpr[1];
3602
3603 if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
3604 (sas_ss_flags(oldsp))) {
3605 oldsp = (target_sigaltstack_used.ss_sp
3606 + target_sigaltstack_used.ss_size);
3607 }
3608
3609 newsp = (oldsp - frame_size) & ~0xFUL;
3610
3611 return newsp;
3612}
3613
3614static int save_user_regs(CPUState *env, struct target_mcontext *frame,
3615 int sigret)
3616{
3617 target_ulong msr = env->msr;
3618 int i;
3619 target_ulong ccr = 0;
3620
3621 /* In general, the kernel attempts to be intelligent about what it
3622 needs to save for Altivec/FP/SPE registers. We don't care that
3623 much, so we just go ahead and save everything. */
3624
3625 /* Save general registers. */
3626 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3627 if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
3628 return 1;
3629 }
3630 }
3631 if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3632 || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3633 || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3634 || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3635 return 1;
3636
3637 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3638 ccr |= env->crf[i] << (32 - ((i + 1) * 4));
3639 }
3640 if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3641 return 1;
3642
3643 /* Save Altivec registers if necessary. */
3644 if (env->insns_flags & PPC_ALTIVEC) {
3645 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003646 ppc_avr_t *avr = &env->avr[i];
3647 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07003648
3649 if (__put_user(avr->u64[0], &vreg->u64[0]) ||
3650 __put_user(avr->u64[1], &vreg->u64[1])) {
3651 return 1;
3652 }
3653 }
3654 /* Set MSR_VR in the saved MSR value to indicate that
3655 frame->mc_vregs contains valid data. */
3656 msr |= MSR_VR;
3657 if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
3658 &frame->mc_vregs.altivec[32].u32[3]))
3659 return 1;
3660 }
3661
3662 /* Save floating point registers. */
3663 if (env->insns_flags & PPC_FLOAT) {
3664 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3665 if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
3666 return 1;
3667 }
3668 }
3669 if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
3670 return 1;
3671 }
3672
3673 /* Save SPE registers. The kernel only saves the high half. */
3674 if (env->insns_flags & PPC_SPE) {
3675#if defined(TARGET_PPC64)
3676 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3677 if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
3678 return 1;
3679 }
3680 }
3681#else
3682 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3683 if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3684 return 1;
3685 }
3686 }
3687#endif
3688 /* Set MSR_SPE in the saved MSR value to indicate that
3689 frame->mc_vregs contains valid data. */
3690 msr |= MSR_SPE;
3691 if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3692 return 1;
3693 }
3694
3695 /* Store MSR. */
3696 if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3697 return 1;
3698
3699 /* Set up the sigreturn trampoline: li r0,sigret; sc. */
3700 if (sigret) {
3701 if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
3702 __put_user(0x44000002UL, &frame->tramp[1])) {
3703 return 1;
3704 }
3705 }
3706
3707 return 0;
3708}
3709
3710static int restore_user_regs(CPUState *env,
3711 struct target_mcontext *frame, int sig)
3712{
3713 target_ulong save_r2 = 0;
3714 target_ulong msr;
3715 target_ulong ccr;
3716
3717 int i;
3718
3719 if (!sig) {
3720 save_r2 = env->gpr[2];
3721 }
3722
3723 /* Restore general registers. */
3724 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3725 if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
3726 return 1;
3727 }
3728 }
3729 if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
3730 || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
3731 || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
3732 || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
3733 return 1;
3734 if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
3735 return 1;
3736
3737 for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
3738 env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
3739 }
3740
3741 if (!sig) {
3742 env->gpr[2] = save_r2;
3743 }
3744 /* Restore MSR. */
3745 if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
3746 return 1;
3747
3748 /* If doing signal return, restore the previous little-endian mode. */
3749 if (sig)
3750 env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
3751
3752 /* Restore Altivec registers if necessary. */
3753 if (env->insns_flags & PPC_ALTIVEC) {
3754 for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
Anthony Liguoric227f092009-10-01 16:12:16 -05003755 ppc_avr_t *avr = &env->avr[i];
3756 ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
Nathan Froydbcd49332009-05-12 19:13:18 -07003757
3758 if (__get_user(avr->u64[0], &vreg->u64[0]) ||
3759 __get_user(avr->u64[1], &vreg->u64[1])) {
3760 return 1;
3761 }
3762 }
3763 /* Set MSR_VEC in the saved MSR value to indicate that
3764 frame->mc_vregs contains valid data. */
3765 if (__get_user(env->spr[SPR_VRSAVE],
3766 (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
3767 return 1;
3768 }
3769
3770 /* Restore floating point registers. */
3771 if (env->insns_flags & PPC_FLOAT) {
3772 uint64_t fpscr;
3773 for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
3774 if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
3775 return 1;
3776 }
3777 }
3778 if (__get_user(fpscr, &frame->mc_fregs[32]))
3779 return 1;
3780 env->fpscr = (uint32_t) fpscr;
3781 }
3782
3783 /* Save SPE registers. The kernel only saves the high half. */
3784 if (env->insns_flags & PPC_SPE) {
3785#if defined(TARGET_PPC64)
3786 for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
3787 uint32_t hi;
3788
3789 if (__get_user(hi, &frame->mc_vregs.spe[i])) {
3790 return 1;
3791 }
3792 env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
3793 }
3794#else
3795 for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
3796 if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
3797 return 1;
3798 }
3799 }
3800#endif
3801 if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
3802 return 1;
3803 }
3804
3805 return 0;
3806}
3807
3808static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003809 target_sigset_t *set, CPUState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07003810{
3811 struct target_sigframe *frame;
3812 struct target_sigcontext *sc;
3813 target_ulong frame_addr, newsp;
3814 int err = 0;
3815 int signal;
3816
3817 frame_addr = get_sigframe(ka, env, sizeof(*frame));
3818 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3819 goto sigsegv;
3820 sc = &frame->sctx;
3821
3822 signal = current_exec_domain_sig(sig);
3823
3824 err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
3825 err |= __put_user(set->sig[0], &sc->oldmask);
3826#if defined(TARGET_PPC64)
3827 err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
3828#else
3829 err |= __put_user(set->sig[1], &sc->_unused[3]);
3830#endif
3831 err |= __put_user(h2g(&frame->mctx), &sc->regs);
3832 err |= __put_user(sig, &sc->signal);
3833
3834 /* Save user regs. */
3835 err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
3836
3837 /* The kernel checks for the presence of a VDSO here. We don't
3838 emulate a vdso, so use a sigreturn system call. */
3839 env->lr = (target_ulong) h2g(frame->mctx.tramp);
3840
3841 /* Turn off all fp exceptions. */
3842 env->fpscr = 0;
3843
3844 /* Create a stack frame for the caller of the handler. */
3845 newsp = frame_addr - SIGNAL_FRAMESIZE;
3846 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3847
3848 if (err)
3849 goto sigsegv;
3850
3851 /* Set up registers for signal handler. */
3852 env->gpr[1] = newsp;
3853 env->gpr[3] = signal;
3854 env->gpr[4] = (target_ulong) h2g(sc);
3855 env->nip = (target_ulong) ka->_sa_handler;
3856 /* Signal handlers are entered in big-endian mode. */
3857 env->msr &= ~MSR_LE;
3858
3859 unlock_user_struct(frame, frame_addr, 1);
3860 return;
3861
3862sigsegv:
3863 unlock_user_struct(frame, frame_addr, 1);
3864 if (logfile)
3865 fprintf (logfile, "segfaulting from setup_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02003866 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07003867}
3868
3869static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05003870 target_siginfo_t *info,
3871 target_sigset_t *set, CPUState *env)
Nathan Froydbcd49332009-05-12 19:13:18 -07003872{
3873 struct target_rt_sigframe *rt_sf;
3874 struct target_mcontext *frame;
3875 target_ulong rt_sf_addr, newsp = 0;
3876 int i, err = 0;
3877 int signal;
3878
3879 rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
3880 if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
3881 goto sigsegv;
3882
3883 signal = current_exec_domain_sig(sig);
3884
3885 err |= copy_siginfo_to_user(&rt_sf->info, info);
3886
3887 err |= __put_user(0, &rt_sf->uc.uc_flags);
3888 err |= __put_user(0, &rt_sf->uc.uc_link);
3889 err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
3890 &rt_sf->uc.uc_stack.ss_sp);
3891 err |= __put_user(sas_ss_flags(env->gpr[1]),
3892 &rt_sf->uc.uc_stack.ss_flags);
3893 err |= __put_user(target_sigaltstack_used.ss_size,
3894 &rt_sf->uc.uc_stack.ss_size);
3895 err |= __put_user(h2g (&rt_sf->uc.uc_mcontext),
3896 &rt_sf->uc.uc_regs);
3897 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3898 err |= __put_user(set->sig[i], &rt_sf->uc.uc_sigmask.sig[i]);
3899 }
3900
3901 frame = &rt_sf->uc.uc_mcontext;
3902 err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
3903
3904 /* The kernel checks for the presence of a VDSO here. We don't
3905 emulate a vdso, so use a sigreturn system call. */
3906 env->lr = (target_ulong) h2g(frame->tramp);
3907
3908 /* Turn off all fp exceptions. */
3909 env->fpscr = 0;
3910
3911 /* Create a stack frame for the caller of the handler. */
3912 newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
3913 err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
3914
3915 if (err)
3916 goto sigsegv;
3917
3918 /* Set up registers for signal handler. */
3919 env->gpr[1] = newsp;
3920 env->gpr[3] = (target_ulong) signal;
3921 env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
3922 env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
3923 env->gpr[6] = (target_ulong) h2g(rt_sf);
3924 env->nip = (target_ulong) ka->_sa_handler;
3925 /* Signal handlers are entered in big-endian mode. */
3926 env->msr &= ~MSR_LE;
3927
3928 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3929 return;
3930
3931sigsegv:
3932 unlock_user_struct(rt_sf, rt_sf_addr, 1);
3933 if (logfile)
3934 fprintf (logfile, "segfaulting from setup_rt_frame\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02003935 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07003936
3937}
3938
3939long do_sigreturn(CPUState *env)
3940{
3941 struct target_sigcontext *sc = NULL;
3942 struct target_mcontext *sr = NULL;
3943 target_ulong sr_addr, sc_addr;
3944 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05003945 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07003946
3947 sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
3948 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
3949 goto sigsegv;
3950
3951#if defined(TARGET_PPC64)
3952 set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
3953#else
3954 if(__get_user(set.sig[0], &sc->oldmask) ||
3955 __get_user(set.sig[1], &sc->_unused[3]))
3956 goto sigsegv;
3957#endif
3958 target_to_host_sigset_internal(&blocked, &set);
3959 sigprocmask(SIG_SETMASK, &blocked, NULL);
3960
3961 if (__get_user(sr_addr, &sc->regs))
3962 goto sigsegv;
3963 if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
3964 goto sigsegv;
3965 if (restore_user_regs(env, sr, 1))
3966 goto sigsegv;
3967
3968 unlock_user_struct(sr, sr_addr, 1);
3969 unlock_user_struct(sc, sc_addr, 1);
3970 return -TARGET_QEMU_ESIGRETURN;
3971
3972sigsegv:
3973 unlock_user_struct(sr, sr_addr, 1);
3974 unlock_user_struct(sc, sc_addr, 1);
3975 if (logfile)
3976 fprintf (logfile, "segfaulting from do_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02003977 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07003978 return 0;
3979}
3980
3981/* See arch/powerpc/kernel/signal_32.c. */
3982static int do_setcontext(struct target_ucontext *ucp, CPUState *env, int sig)
3983{
3984 struct target_mcontext *mcp;
3985 target_ulong mcp_addr;
3986 sigset_t blocked;
Anthony Liguoric227f092009-10-01 16:12:16 -05003987 target_sigset_t set;
Nathan Froydbcd49332009-05-12 19:13:18 -07003988
3989 if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, uc_sigmask),
3990 sizeof (set)))
3991 return 1;
3992
3993#if defined(TARGET_PPC64)
3994 fprintf (stderr, "do_setcontext: not implemented\n");
3995 return 0;
3996#else
3997 if (__get_user(mcp_addr, &ucp->uc_regs))
3998 return 1;
3999
4000 if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4001 return 1;
4002
4003 target_to_host_sigset_internal(&blocked, &set);
4004 sigprocmask(SIG_SETMASK, &blocked, NULL);
4005 if (restore_user_regs(env, mcp, sig))
4006 goto sigsegv;
4007
4008 unlock_user_struct(mcp, mcp_addr, 1);
4009 return 0;
4010
4011sigsegv:
4012 unlock_user_struct(mcp, mcp_addr, 1);
4013 return 1;
4014#endif
4015}
4016
4017long do_rt_sigreturn(CPUState *env)
4018{
4019 struct target_rt_sigframe *rt_sf = NULL;
4020 target_ulong rt_sf_addr;
4021
4022 rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4023 if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4024 goto sigsegv;
4025
4026 if (do_setcontext(&rt_sf->uc, env, 1))
4027 goto sigsegv;
4028
4029 do_sigaltstack(rt_sf_addr
4030 + offsetof(struct target_rt_sigframe, uc.uc_stack),
4031 0, env->gpr[1]);
4032
4033 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4034 return -TARGET_QEMU_ESIGRETURN;
4035
4036sigsegv:
4037 unlock_user_struct(rt_sf, rt_sf_addr, 1);
4038 if (logfile)
4039 fprintf (logfile, "segfaulting from do_rt_sigreturn\n");
Riku Voipio66393fb2009-12-04 15:16:32 +02004040 force_sig(TARGET_SIGSEGV);
Nathan Froydbcd49332009-05-12 19:13:18 -07004041 return 0;
4042}
4043
Laurent Vivier492a8742009-08-03 16:12:17 +02004044#elif defined(TARGET_M68K)
4045
4046struct target_sigcontext {
4047 abi_ulong sc_mask;
4048 abi_ulong sc_usp;
4049 abi_ulong sc_d0;
4050 abi_ulong sc_d1;
4051 abi_ulong sc_a0;
4052 abi_ulong sc_a1;
4053 unsigned short sc_sr;
4054 abi_ulong sc_pc;
4055};
4056
4057struct target_sigframe
4058{
4059 abi_ulong pretcode;
4060 int sig;
4061 int code;
4062 abi_ulong psc;
4063 char retcode[8];
4064 abi_ulong extramask[TARGET_NSIG_WORDS-1];
4065 struct target_sigcontext sc;
4066};
Laurent Vivier71811552009-08-03 16:12:18 +02004067
Anthony Liguoric227f092009-10-01 16:12:16 -05004068typedef int target_greg_t;
Laurent Vivier71811552009-08-03 16:12:18 +02004069#define TARGET_NGREG 18
Anthony Liguoric227f092009-10-01 16:12:16 -05004070typedef target_greg_t target_gregset_t[TARGET_NGREG];
Laurent Vivier71811552009-08-03 16:12:18 +02004071
4072typedef struct target_fpregset {
4073 int f_fpcntl[3];
4074 int f_fpregs[8*3];
Anthony Liguoric227f092009-10-01 16:12:16 -05004075} target_fpregset_t;
Laurent Vivier71811552009-08-03 16:12:18 +02004076
4077struct target_mcontext {
4078 int version;
Anthony Liguoric227f092009-10-01 16:12:16 -05004079 target_gregset_t gregs;
4080 target_fpregset_t fpregs;
Laurent Vivier71811552009-08-03 16:12:18 +02004081};
4082
4083#define TARGET_MCONTEXT_VERSION 2
4084
4085struct target_ucontext {
4086 abi_ulong uc_flags;
4087 abi_ulong uc_link;
Anthony Liguoric227f092009-10-01 16:12:16 -05004088 target_stack_t uc_stack;
Laurent Vivier71811552009-08-03 16:12:18 +02004089 struct target_mcontext uc_mcontext;
4090 abi_long uc_filler[80];
Anthony Liguoric227f092009-10-01 16:12:16 -05004091 target_sigset_t uc_sigmask;
Laurent Vivier71811552009-08-03 16:12:18 +02004092};
4093
4094struct target_rt_sigframe
4095{
4096 abi_ulong pretcode;
4097 int sig;
4098 abi_ulong pinfo;
4099 abi_ulong puc;
4100 char retcode[8];
4101 struct target_siginfo info;
4102 struct target_ucontext uc;
4103};
Laurent Vivier492a8742009-08-03 16:12:17 +02004104
4105static int
4106setup_sigcontext(struct target_sigcontext *sc, CPUState *env, abi_ulong mask)
4107{
4108 int err = 0;
4109
4110 err |= __put_user(mask, &sc->sc_mask);
4111 err |= __put_user(env->aregs[7], &sc->sc_usp);
4112 err |= __put_user(env->dregs[0], &sc->sc_d0);
4113 err |= __put_user(env->dregs[1], &sc->sc_d1);
4114 err |= __put_user(env->aregs[0], &sc->sc_a0);
4115 err |= __put_user(env->aregs[1], &sc->sc_a1);
4116 err |= __put_user(env->sr, &sc->sc_sr);
4117 err |= __put_user(env->pc, &sc->sc_pc);
4118
4119 return err;
4120}
4121
4122static int
4123restore_sigcontext(CPUState *env, struct target_sigcontext *sc, int *pd0)
4124{
4125 int err = 0;
4126 int temp;
4127
4128 err |= __get_user(env->aregs[7], &sc->sc_usp);
4129 err |= __get_user(env->dregs[1], &sc->sc_d1);
4130 err |= __get_user(env->aregs[0], &sc->sc_a0);
4131 err |= __get_user(env->aregs[1], &sc->sc_a1);
4132 err |= __get_user(env->pc, &sc->sc_pc);
4133 err |= __get_user(temp, &sc->sc_sr);
4134 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4135
4136 *pd0 = tswapl(sc->sc_d0);
4137
4138 return err;
4139}
4140
4141/*
4142 * Determine which stack to use..
4143 */
4144static inline abi_ulong
4145get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
4146{
4147 unsigned long sp;
4148
4149 sp = regs->aregs[7];
4150
4151 /* This is the X/Open sanctioned signal stack switching. */
4152 if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4153 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4154 }
4155
4156 return ((sp - frame_size) & -8UL);
4157}
4158
4159static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004160 target_sigset_t *set, CPUState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02004161{
4162 struct target_sigframe *frame;
4163 abi_ulong frame_addr;
4164 abi_ulong retcode_addr;
4165 abi_ulong sc_addr;
4166 int err = 0;
4167 int i;
4168
4169 frame_addr = get_sigframe(ka, env, sizeof *frame);
4170 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4171 goto give_sigsegv;
4172
4173 err |= __put_user(sig, &frame->sig);
4174
4175 sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4176 err |= __put_user(sc_addr, &frame->psc);
4177
4178 err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4179 if (err)
4180 goto give_sigsegv;
4181
4182 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4183 if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4184 goto give_sigsegv;
4185 }
4186
4187 /* Set up to return from userspace. */
4188
4189 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4190 err |= __put_user(retcode_addr, &frame->pretcode);
4191
4192 /* moveq #,d0; trap #0 */
4193
4194 err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4195 (long *)(frame->retcode));
4196
4197 if (err)
4198 goto give_sigsegv;
4199
4200 /* Set up to return from userspace */
4201
4202 env->aregs[7] = frame_addr;
4203 env->pc = ka->_sa_handler;
4204
4205 unlock_user_struct(frame, frame_addr, 1);
4206 return;
4207
4208give_sigsegv:
4209 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02004210 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02004211}
4212
Laurent Vivier71811552009-08-03 16:12:18 +02004213static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4214 CPUState *env)
4215{
Anthony Liguoric227f092009-10-01 16:12:16 -05004216 target_greg_t *gregs = uc->uc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02004217 int err;
4218
4219 err = __put_user(TARGET_MCONTEXT_VERSION, &uc->uc_mcontext.version);
4220 err |= __put_user(env->dregs[0], &gregs[0]);
4221 err |= __put_user(env->dregs[1], &gregs[1]);
4222 err |= __put_user(env->dregs[2], &gregs[2]);
4223 err |= __put_user(env->dregs[3], &gregs[3]);
4224 err |= __put_user(env->dregs[4], &gregs[4]);
4225 err |= __put_user(env->dregs[5], &gregs[5]);
4226 err |= __put_user(env->dregs[6], &gregs[6]);
4227 err |= __put_user(env->dregs[7], &gregs[7]);
4228 err |= __put_user(env->aregs[0], &gregs[8]);
4229 err |= __put_user(env->aregs[1], &gregs[9]);
4230 err |= __put_user(env->aregs[2], &gregs[10]);
4231 err |= __put_user(env->aregs[3], &gregs[11]);
4232 err |= __put_user(env->aregs[4], &gregs[12]);
4233 err |= __put_user(env->aregs[5], &gregs[13]);
4234 err |= __put_user(env->aregs[6], &gregs[14]);
4235 err |= __put_user(env->aregs[7], &gregs[15]);
4236 err |= __put_user(env->pc, &gregs[16]);
4237 err |= __put_user(env->sr, &gregs[17]);
4238
4239 return err;
4240}
4241
4242static inline int target_rt_restore_ucontext(CPUState *env,
4243 struct target_ucontext *uc,
4244 int *pd0)
4245{
4246 int temp;
4247 int err;
Anthony Liguoric227f092009-10-01 16:12:16 -05004248 target_greg_t *gregs = uc->uc_mcontext.gregs;
Laurent Vivier71811552009-08-03 16:12:18 +02004249
4250 err = __get_user(temp, &uc->uc_mcontext.version);
4251 if (temp != TARGET_MCONTEXT_VERSION)
4252 goto badframe;
4253
4254 /* restore passed registers */
4255 err |= __get_user(env->dregs[0], &gregs[0]);
4256 err |= __get_user(env->dregs[1], &gregs[1]);
4257 err |= __get_user(env->dregs[2], &gregs[2]);
4258 err |= __get_user(env->dregs[3], &gregs[3]);
4259 err |= __get_user(env->dregs[4], &gregs[4]);
4260 err |= __get_user(env->dregs[5], &gregs[5]);
4261 err |= __get_user(env->dregs[6], &gregs[6]);
4262 err |= __get_user(env->dregs[7], &gregs[7]);
4263 err |= __get_user(env->aregs[0], &gregs[8]);
4264 err |= __get_user(env->aregs[1], &gregs[9]);
4265 err |= __get_user(env->aregs[2], &gregs[10]);
4266 err |= __get_user(env->aregs[3], &gregs[11]);
4267 err |= __get_user(env->aregs[4], &gregs[12]);
4268 err |= __get_user(env->aregs[5], &gregs[13]);
4269 err |= __get_user(env->aregs[6], &gregs[14]);
4270 err |= __get_user(env->aregs[7], &gregs[15]);
4271 err |= __get_user(env->pc, &gregs[16]);
4272 err |= __get_user(temp, &gregs[17]);
4273 env->sr = (env->sr & 0xff00) | (temp & 0xff);
4274
4275 *pd0 = env->dregs[0];
4276 return err;
4277
4278badframe:
4279 return 1;
4280}
4281
Laurent Vivier492a8742009-08-03 16:12:17 +02004282static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004283 target_siginfo_t *info,
4284 target_sigset_t *set, CPUState *env)
Laurent Vivier492a8742009-08-03 16:12:17 +02004285{
Laurent Vivier71811552009-08-03 16:12:18 +02004286 struct target_rt_sigframe *frame;
4287 abi_ulong frame_addr;
4288 abi_ulong retcode_addr;
4289 abi_ulong info_addr;
4290 abi_ulong uc_addr;
4291 int err = 0;
4292 int i;
4293
4294 frame_addr = get_sigframe(ka, env, sizeof *frame);
4295 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4296 goto give_sigsegv;
4297
4298 err |= __put_user(sig, &frame->sig);
4299
4300 info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4301 err |= __put_user(info_addr, &frame->pinfo);
4302
4303 uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4304 err |= __put_user(uc_addr, &frame->puc);
4305
4306 err |= copy_siginfo_to_user(&frame->info, info);
4307
4308 /* Create the ucontext */
4309
4310 err |= __put_user(0, &frame->uc.uc_flags);
4311 err |= __put_user(0, &frame->uc.uc_link);
4312 err |= __put_user(target_sigaltstack_used.ss_sp,
4313 &frame->uc.uc_stack.ss_sp);
4314 err |= __put_user(sas_ss_flags(env->aregs[7]),
4315 &frame->uc.uc_stack.ss_flags);
4316 err |= __put_user(target_sigaltstack_used.ss_size,
4317 &frame->uc.uc_stack.ss_size);
4318 err |= target_rt_setup_ucontext(&frame->uc, env);
4319
4320 if (err)
4321 goto give_sigsegv;
4322
4323 for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4324 if (__put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]))
4325 goto give_sigsegv;
4326 }
4327
4328 /* Set up to return from userspace. */
4329
4330 retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4331 err |= __put_user(retcode_addr, &frame->pretcode);
4332
4333 /* moveq #,d0; notb d0; trap #0 */
4334
4335 err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4336 (long *)(frame->retcode + 0));
4337 err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
4338
4339 if (err)
4340 goto give_sigsegv;
4341
4342 /* Set up to return from userspace */
4343
4344 env->aregs[7] = frame_addr;
4345 env->pc = ka->_sa_handler;
4346
4347 unlock_user_struct(frame, frame_addr, 1);
4348 return;
4349
4350give_sigsegv:
4351 unlock_user_struct(frame, frame_addr, 1);
Riku Voipio66393fb2009-12-04 15:16:32 +02004352 force_sig(TARGET_SIGSEGV);
Laurent Vivier492a8742009-08-03 16:12:17 +02004353}
4354
4355long do_sigreturn(CPUState *env)
4356{
4357 struct target_sigframe *frame;
4358 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05004359 target_sigset_t target_set;
Laurent Vivier492a8742009-08-03 16:12:17 +02004360 sigset_t set;
4361 int d0, i;
4362
4363 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4364 goto badframe;
4365
4366 /* set blocked signals */
4367
4368 if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
4369 goto badframe;
4370
4371 for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4372 if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
4373 goto badframe;
4374 }
4375
4376 target_to_host_sigset_internal(&set, &target_set);
4377 sigprocmask(SIG_SETMASK, &set, NULL);
4378
4379 /* restore registers */
4380
4381 if (restore_sigcontext(env, &frame->sc, &d0))
4382 goto badframe;
4383
4384 unlock_user_struct(frame, frame_addr, 0);
4385 return d0;
4386
4387badframe:
4388 unlock_user_struct(frame, frame_addr, 0);
4389 force_sig(TARGET_SIGSEGV);
4390 return 0;
4391}
4392
4393long do_rt_sigreturn(CPUState *env)
4394{
Laurent Vivier71811552009-08-03 16:12:18 +02004395 struct target_rt_sigframe *frame;
4396 abi_ulong frame_addr = env->aregs[7] - 4;
Anthony Liguoric227f092009-10-01 16:12:16 -05004397 target_sigset_t target_set;
Laurent Vivier71811552009-08-03 16:12:18 +02004398 sigset_t set;
4399 int d0;
4400
4401 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4402 goto badframe;
4403
4404 target_to_host_sigset_internal(&set, &target_set);
4405 sigprocmask(SIG_SETMASK, &set, NULL);
4406
4407 /* restore registers */
4408
4409 if (target_rt_restore_ucontext(env, &frame->uc, &d0))
4410 goto badframe;
4411
4412 if (do_sigaltstack(frame_addr +
4413 offsetof(struct target_rt_sigframe, uc.uc_stack),
4414 0, get_sp_from_cpustate(env)) == -EFAULT)
4415 goto badframe;
4416
4417 unlock_user_struct(frame, frame_addr, 0);
4418 return d0;
4419
4420badframe:
4421 unlock_user_struct(frame, frame_addr, 0);
4422 force_sig(TARGET_SIGSEGV);
4423 return 0;
Laurent Vivier492a8742009-08-03 16:12:17 +02004424}
4425
Richard Henderson6049f4f2009-12-27 18:30:03 -08004426#elif defined(TARGET_ALPHA)
4427
4428struct target_sigcontext {
4429 abi_long sc_onstack;
4430 abi_long sc_mask;
4431 abi_long sc_pc;
4432 abi_long sc_ps;
4433 abi_long sc_regs[32];
4434 abi_long sc_ownedfp;
4435 abi_long sc_fpregs[32];
4436 abi_ulong sc_fpcr;
4437 abi_ulong sc_fp_control;
4438 abi_ulong sc_reserved1;
4439 abi_ulong sc_reserved2;
4440 abi_ulong sc_ssize;
4441 abi_ulong sc_sbase;
4442 abi_ulong sc_traparg_a0;
4443 abi_ulong sc_traparg_a1;
4444 abi_ulong sc_traparg_a2;
4445 abi_ulong sc_fp_trap_pc;
4446 abi_ulong sc_fp_trigger_sum;
4447 abi_ulong sc_fp_trigger_inst;
4448};
4449
4450struct target_ucontext {
4451 abi_ulong uc_flags;
4452 abi_ulong uc_link;
4453 abi_ulong uc_osf_sigmask;
4454 target_stack_t uc_stack;
4455 struct target_sigcontext uc_mcontext;
4456 target_sigset_t uc_sigmask;
4457};
4458
4459struct target_sigframe {
4460 struct target_sigcontext sc;
4461 unsigned int retcode[3];
4462};
4463
4464struct target_rt_sigframe {
4465 target_siginfo_t info;
4466 struct target_ucontext uc;
4467 unsigned int retcode[3];
4468};
4469
4470#define INSN_MOV_R30_R16 0x47fe0410
4471#define INSN_LDI_R0 0x201f0000
4472#define INSN_CALLSYS 0x00000083
4473
4474static int setup_sigcontext(struct target_sigcontext *sc, CPUState *env,
4475 abi_ulong frame_addr, target_sigset_t *set)
4476{
4477 int i, err = 0;
4478
4479 err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
4480 err |= __put_user(set->sig[0], &sc->sc_mask);
4481 err |= __put_user(env->pc, &sc->sc_pc);
4482 err |= __put_user(8, &sc->sc_ps);
4483
4484 for (i = 0; i < 31; ++i) {
4485 err |= __put_user(env->ir[i], &sc->sc_regs[i]);
4486 }
4487 err |= __put_user(0, &sc->sc_regs[31]);
4488
4489 for (i = 0; i < 31; ++i) {
4490 err |= __put_user(env->fir[i], &sc->sc_fpregs[i]);
4491 }
4492 err |= __put_user(0, &sc->sc_fpregs[31]);
4493 err |= __put_user(cpu_alpha_load_fpcr(env), &sc->sc_fpcr);
4494
4495 err |= __put_user(0, &sc->sc_traparg_a0); /* FIXME */
4496 err |= __put_user(0, &sc->sc_traparg_a1); /* FIXME */
4497 err |= __put_user(0, &sc->sc_traparg_a2); /* FIXME */
4498
4499 return err;
4500}
4501
4502static int restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
4503{
4504 uint64_t fpcr;
4505 int i, err = 0;
4506
4507 err |= __get_user(env->pc, &sc->sc_pc);
4508
4509 for (i = 0; i < 31; ++i) {
4510 err |= __get_user(env->ir[i], &sc->sc_regs[i]);
4511 }
4512 for (i = 0; i < 31; ++i) {
4513 err |= __get_user(env->fir[i], &sc->sc_fpregs[i]);
4514 }
4515
4516 err |= __get_user(fpcr, &sc->sc_fpcr);
4517 cpu_alpha_store_fpcr(env, fpcr);
4518
4519 return err;
4520}
4521
4522static inline abi_ulong get_sigframe(struct target_sigaction *sa,
4523 CPUState *env, unsigned long framesize)
4524{
4525 abi_ulong sp = env->ir[IR_SP];
4526
4527 /* This is the X/Open sanctioned signal stack switching. */
4528 if ((sa->sa_flags & TARGET_SA_ONSTACK) != 0 && !sas_ss_flags(sp)) {
4529 sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4530 }
4531 return (sp - framesize) & -32;
4532}
4533
4534static void setup_frame(int sig, struct target_sigaction *ka,
4535 target_sigset_t *set, CPUState *env)
4536{
4537 abi_ulong frame_addr, r26;
4538 struct target_sigframe *frame;
4539 int err = 0;
4540
4541 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4542 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4543 goto give_sigsegv;
4544 }
4545
4546 err |= setup_sigcontext(&frame->sc, env, frame_addr, set);
4547
4548 if (ka->sa_restorer) {
4549 r26 = ka->sa_restorer;
4550 } else {
4551 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
4552 err |= __put_user(INSN_LDI_R0 + TARGET_NR_sigreturn,
4553 &frame->retcode[1]);
4554 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
4555 /* imb() */
4556 r26 = frame_addr;
4557 }
4558
4559 unlock_user_struct(frame, frame_addr, 1);
4560
4561 if (err) {
4562 give_sigsegv:
4563 if (sig == TARGET_SIGSEGV) {
4564 ka->_sa_handler = TARGET_SIG_DFL;
4565 }
4566 force_sig(TARGET_SIGSEGV);
4567 }
4568
4569 env->ir[IR_RA] = r26;
4570 env->ir[IR_PV] = env->pc = ka->_sa_handler;
4571 env->ir[IR_A0] = sig;
4572 env->ir[IR_A1] = 0;
4573 env->ir[IR_A2] = frame_addr + offsetof(struct target_sigframe, sc);
4574 env->ir[IR_SP] = frame_addr;
4575}
4576
4577static void setup_rt_frame(int sig, struct target_sigaction *ka,
4578 target_siginfo_t *info,
4579 target_sigset_t *set, CPUState *env)
4580{
4581 abi_ulong frame_addr, r26;
4582 struct target_rt_sigframe *frame;
4583 int i, err = 0;
4584
4585 frame_addr = get_sigframe(ka, env, sizeof(*frame));
4586 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
4587 goto give_sigsegv;
4588 }
4589
4590 err |= copy_siginfo_to_user(&frame->info, info);
4591
4592 err |= __put_user(0, &frame->uc.uc_flags);
4593 err |= __put_user(0, &frame->uc.uc_link);
4594 err |= __put_user(set->sig[0], &frame->uc.uc_osf_sigmask);
4595 err |= __put_user(target_sigaltstack_used.ss_sp,
4596 &frame->uc.uc_stack.ss_sp);
4597 err |= __put_user(sas_ss_flags(env->ir[IR_SP]),
4598 &frame->uc.uc_stack.ss_flags);
4599 err |= __put_user(target_sigaltstack_used.ss_size,
4600 &frame->uc.uc_stack.ss_size);
4601 err |= setup_sigcontext(&frame->uc.uc_mcontext, env, frame_addr, set);
4602 for (i = 0; i < TARGET_NSIG_WORDS; ++i) {
4603 err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
4604 }
4605
4606 if (ka->sa_restorer) {
4607 r26 = ka->sa_restorer;
4608 } else {
4609 err |= __put_user(INSN_MOV_R30_R16, &frame->retcode[0]);
4610 err |= __put_user(INSN_LDI_R0 + TARGET_NR_rt_sigreturn,
4611 &frame->retcode[1]);
4612 err |= __put_user(INSN_CALLSYS, &frame->retcode[2]);
4613 /* imb(); */
4614 r26 = frame_addr;
4615 }
4616
4617 if (err) {
4618 give_sigsegv:
4619 if (sig == TARGET_SIGSEGV) {
4620 ka->_sa_handler = TARGET_SIG_DFL;
4621 }
4622 force_sig(TARGET_SIGSEGV);
4623 }
4624
4625 env->ir[IR_RA] = r26;
4626 env->ir[IR_PV] = env->pc = ka->_sa_handler;
4627 env->ir[IR_A0] = sig;
4628 env->ir[IR_A1] = frame_addr + offsetof(struct target_rt_sigframe, info);
4629 env->ir[IR_A2] = frame_addr + offsetof(struct target_rt_sigframe, uc);
4630 env->ir[IR_SP] = frame_addr;
4631}
4632
4633long do_sigreturn(CPUState *env)
4634{
4635 struct target_sigcontext *sc;
4636 abi_ulong sc_addr = env->ir[IR_A0];
4637 target_sigset_t target_set;
4638 sigset_t set;
4639
4640 if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1)) {
4641 goto badframe;
4642 }
4643
4644 target_sigemptyset(&target_set);
4645 if (__get_user(target_set.sig[0], &sc->sc_mask)) {
4646 goto badframe;
4647 }
4648
4649 target_to_host_sigset_internal(&set, &target_set);
4650 sigprocmask(SIG_SETMASK, &set, NULL);
4651
4652 if (restore_sigcontext(env, sc)) {
4653 goto badframe;
4654 }
4655 unlock_user_struct(sc, sc_addr, 0);
4656 return env->ir[IR_V0];
4657
4658 badframe:
4659 unlock_user_struct(sc, sc_addr, 0);
4660 force_sig(TARGET_SIGSEGV);
4661}
4662
4663long do_rt_sigreturn(CPUState *env)
4664{
4665 abi_ulong frame_addr = env->ir[IR_A0];
4666 struct target_rt_sigframe *frame;
4667 sigset_t set;
4668
4669 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
4670 goto badframe;
4671 }
4672 target_to_host_sigset(&set, &frame->uc.uc_sigmask);
4673 sigprocmask(SIG_SETMASK, &set, NULL);
4674
4675 if (restore_sigcontext(env, &frame->uc.uc_mcontext)) {
4676 goto badframe;
4677 }
4678 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
4679 uc.uc_stack),
4680 0, env->ir[IR_SP]) == -EFAULT) {
4681 goto badframe;
4682 }
4683
4684 unlock_user_struct(frame, frame_addr, 0);
4685 return env->ir[IR_V0];
4686
4687
4688 badframe:
4689 unlock_user_struct(frame, frame_addr, 0);
4690 force_sig(TARGET_SIGSEGV);
4691}
4692
bellardb346ff42003-06-15 20:05:50 +00004693#else
4694
pbrook624f7972008-05-31 16:11:38 +00004695static void setup_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004696 target_sigset_t *set, CPUState *env)
bellardb346ff42003-06-15 20:05:50 +00004697{
4698 fprintf(stderr, "setup_frame: not implemented\n");
4699}
4700
pbrook624f7972008-05-31 16:11:38 +00004701static void setup_rt_frame(int sig, struct target_sigaction *ka,
Anthony Liguoric227f092009-10-01 16:12:16 -05004702 target_siginfo_t *info,
4703 target_sigset_t *set, CPUState *env)
bellardb346ff42003-06-15 20:05:50 +00004704{
4705 fprintf(stderr, "setup_rt_frame: not implemented\n");
4706}
4707
4708long do_sigreturn(CPUState *env)
4709{
4710 fprintf(stderr, "do_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00004711 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00004712}
4713
4714long do_rt_sigreturn(CPUState *env)
4715{
4716 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
bellardf8b0aa22007-11-11 23:03:42 +00004717 return -TARGET_ENOSYS;
bellardb346ff42003-06-15 20:05:50 +00004718}
4719
bellard66fb9762003-03-23 01:06:05 +00004720#endif
4721
pbrook624f7972008-05-31 16:11:38 +00004722void process_pending_signals(CPUState *cpu_env)
bellard66fb9762003-03-23 01:06:05 +00004723{
4724 int sig;
blueswir1992f48a2007-10-14 16:27:31 +00004725 abi_ulong handler;
bellard9de5e442003-03-23 16:49:39 +00004726 sigset_t set, old_set;
Anthony Liguoric227f092009-10-01 16:12:16 -05004727 target_sigset_t target_old_set;
pbrook624f7972008-05-31 16:11:38 +00004728 struct emulated_sigtable *k;
4729 struct target_sigaction *sa;
bellard66fb9762003-03-23 01:06:05 +00004730 struct sigqueue *q;
pbrook624f7972008-05-31 16:11:38 +00004731 TaskState *ts = cpu_env->opaque;
ths3b46e622007-09-17 08:09:54 +00004732
pbrook624f7972008-05-31 16:11:38 +00004733 if (!ts->signal_pending)
bellard31e31b82003-02-18 22:55:36 +00004734 return;
4735
pbrook624f7972008-05-31 16:11:38 +00004736 /* FIXME: This is not threadsafe. */
4737 k = ts->sigtab;
bellard66fb9762003-03-23 01:06:05 +00004738 for(sig = 1; sig <= TARGET_NSIG; sig++) {
4739 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +00004740 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +00004741 k++;
bellard31e31b82003-02-18 22:55:36 +00004742 }
4743 /* if no signal is pending, just return */
pbrook624f7972008-05-31 16:11:38 +00004744 ts->signal_pending = 0;
bellard31e31b82003-02-18 22:55:36 +00004745 return;
bellard66fb9762003-03-23 01:06:05 +00004746
bellard31e31b82003-02-18 22:55:36 +00004747 handle_signal:
bellard66fb9762003-03-23 01:06:05 +00004748#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +00004749 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +00004750#endif
4751 /* dequeue signal */
4752 q = k->first;
4753 k->first = q->next;
4754 if (!k->first)
4755 k->pending = 0;
ths3b46e622007-09-17 08:09:54 +00004756
bellard1fddef42005-04-17 19:16:13 +00004757 sig = gdb_handlesig (cpu_env, sig);
4758 if (!sig) {
aurel32ca587a82008-12-18 22:44:13 +00004759 sa = NULL;
4760 handler = TARGET_SIG_IGN;
4761 } else {
4762 sa = &sigact_table[sig - 1];
4763 handler = sa->_sa_handler;
bellard1fddef42005-04-17 19:16:13 +00004764 }
bellard66fb9762003-03-23 01:06:05 +00004765
bellard66fb9762003-03-23 01:06:05 +00004766 if (handler == TARGET_SIG_DFL) {
aurel32ca587a82008-12-18 22:44:13 +00004767 /* default handler : ignore some signal. The other are job control or fatal */
4768 if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
4769 kill(getpid(),SIGSTOP);
4770 } else if (sig != TARGET_SIGCHLD &&
4771 sig != TARGET_SIGURG &&
4772 sig != TARGET_SIGWINCH &&
4773 sig != TARGET_SIGCONT) {
bellard66fb9762003-03-23 01:06:05 +00004774 force_sig(sig);
4775 }
4776 } else if (handler == TARGET_SIG_IGN) {
4777 /* ignore sig */
4778 } else if (handler == TARGET_SIG_ERR) {
4779 force_sig(sig);
4780 } else {
bellard9de5e442003-03-23 16:49:39 +00004781 /* compute the blocked signals during the handler execution */
pbrook624f7972008-05-31 16:11:38 +00004782 target_to_host_sigset(&set, &sa->sa_mask);
bellard9de5e442003-03-23 16:49:39 +00004783 /* SA_NODEFER indicates that the current signal should not be
4784 blocked during the handler */
pbrook624f7972008-05-31 16:11:38 +00004785 if (!(sa->sa_flags & TARGET_SA_NODEFER))
bellard9de5e442003-03-23 16:49:39 +00004786 sigaddset(&set, target_to_host_signal(sig));
ths3b46e622007-09-17 08:09:54 +00004787
bellard9de5e442003-03-23 16:49:39 +00004788 /* block signals in the handler using Linux */
4789 sigprocmask(SIG_BLOCK, &set, &old_set);
4790 /* save the previous blocked signal state to restore it at the
4791 end of the signal execution (see do_sigreturn) */
bellard92319442004-06-19 16:58:13 +00004792 host_to_target_sigset_internal(&target_old_set, &old_set);
bellard9de5e442003-03-23 16:49:39 +00004793
bellardbc8a22c2003-03-30 21:02:40 +00004794 /* if the CPU is in VM86 mode, we restore the 32 bit values */
j_mayer84409dd2007-04-06 08:56:50 +00004795#if defined(TARGET_I386) && !defined(TARGET_X86_64)
bellardbc8a22c2003-03-30 21:02:40 +00004796 {
4797 CPUX86State *env = cpu_env;
4798 if (env->eflags & VM_MASK)
4799 save_v86_state(env);
4800 }
4801#endif
bellard9de5e442003-03-23 16:49:39 +00004802 /* prepare the stack frame of the virtual CPU */
pbrook624f7972008-05-31 16:11:38 +00004803 if (sa->sa_flags & TARGET_SA_SIGINFO)
4804 setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +00004805 else
pbrook624f7972008-05-31 16:11:38 +00004806 setup_frame(sig, sa, &target_old_set, cpu_env);
4807 if (sa->sa_flags & TARGET_SA_RESETHAND)
4808 sa->_sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +00004809 }
bellard66fb9762003-03-23 01:06:05 +00004810 if (q != &k->info)
pbrook624f7972008-05-31 16:11:38 +00004811 free_sigqueue(cpu_env, q);
bellard31e31b82003-02-18 22:55:36 +00004812}