root / linux-user / signal.c @ cbeb0857
History | View | Annotate | Download (57.4 kB)
1 |
/*
|
---|---|
2 |
* Emulation of Linux signals
|
3 |
*
|
4 |
* 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
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
19 |
*/
|
20 |
#include <stdlib.h> |
21 |
#include <stdio.h> |
22 |
#include <string.h> |
23 |
#include <stdarg.h> |
24 |
#include <unistd.h> |
25 |
#include <signal.h> |
26 |
#include <errno.h> |
27 |
#include <sys/ucontext.h> |
28 |
|
29 |
#include "qemu.h" |
30 |
|
31 |
//#define DEBUG_SIGNAL
|
32 |
|
33 |
#define MAX_SIGQUEUE_SIZE 1024 |
34 |
|
35 |
struct sigqueue {
|
36 |
struct sigqueue *next;
|
37 |
target_siginfo_t info; |
38 |
}; |
39 |
|
40 |
struct emulated_sigaction {
|
41 |
struct target_sigaction sa;
|
42 |
int pending; /* true if signal is pending */ |
43 |
struct sigqueue *first;
|
44 |
struct sigqueue info; /* in order to always have memory for the |
45 |
first signal, we put it here */
|
46 |
}; |
47 |
|
48 |
static struct emulated_sigaction sigact_table[TARGET_NSIG]; |
49 |
static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */ |
50 |
static struct sigqueue *first_free; /* first free siginfo queue entry */ |
51 |
static int signal_pending; /* non zero if a signal may be pending */ |
52 |
|
53 |
static void host_signal_handler(int host_signum, siginfo_t *info, |
54 |
void *puc);
|
55 |
|
56 |
static uint8_t host_to_target_signal_table[65] = { |
57 |
[SIGHUP] = TARGET_SIGHUP, |
58 |
[SIGINT] = TARGET_SIGINT, |
59 |
[SIGQUIT] = TARGET_SIGQUIT, |
60 |
[SIGILL] = TARGET_SIGILL, |
61 |
[SIGTRAP] = TARGET_SIGTRAP, |
62 |
[SIGABRT] = TARGET_SIGABRT, |
63 |
/* [SIGIOT] = TARGET_SIGIOT,*/
|
64 |
[SIGBUS] = TARGET_SIGBUS, |
65 |
[SIGFPE] = TARGET_SIGFPE, |
66 |
[SIGKILL] = TARGET_SIGKILL, |
67 |
[SIGUSR1] = TARGET_SIGUSR1, |
68 |
[SIGSEGV] = TARGET_SIGSEGV, |
69 |
[SIGUSR2] = TARGET_SIGUSR2, |
70 |
[SIGPIPE] = TARGET_SIGPIPE, |
71 |
[SIGALRM] = TARGET_SIGALRM, |
72 |
[SIGTERM] = TARGET_SIGTERM, |
73 |
#ifdef SIGSTKFLT
|
74 |
[SIGSTKFLT] = TARGET_SIGSTKFLT, |
75 |
#endif
|
76 |
[SIGCHLD] = TARGET_SIGCHLD, |
77 |
[SIGCONT] = TARGET_SIGCONT, |
78 |
[SIGSTOP] = TARGET_SIGSTOP, |
79 |
[SIGTSTP] = TARGET_SIGTSTP, |
80 |
[SIGTTIN] = TARGET_SIGTTIN, |
81 |
[SIGTTOU] = TARGET_SIGTTOU, |
82 |
[SIGURG] = TARGET_SIGURG, |
83 |
[SIGXCPU] = TARGET_SIGXCPU, |
84 |
[SIGXFSZ] = TARGET_SIGXFSZ, |
85 |
[SIGVTALRM] = TARGET_SIGVTALRM, |
86 |
[SIGPROF] = TARGET_SIGPROF, |
87 |
[SIGWINCH] = TARGET_SIGWINCH, |
88 |
[SIGIO] = TARGET_SIGIO, |
89 |
[SIGPWR] = TARGET_SIGPWR, |
90 |
[SIGSYS] = TARGET_SIGSYS, |
91 |
/* next signals stay the same */
|
92 |
}; |
93 |
static uint8_t target_to_host_signal_table[65]; |
94 |
|
95 |
static inline int host_to_target_signal(int sig) |
96 |
{ |
97 |
return host_to_target_signal_table[sig];
|
98 |
} |
99 |
|
100 |
static inline int target_to_host_signal(int sig) |
101 |
{ |
102 |
return target_to_host_signal_table[sig];
|
103 |
} |
104 |
|
105 |
static void host_to_target_sigset_internal(target_sigset_t *d, |
106 |
const sigset_t *s)
|
107 |
{ |
108 |
int i;
|
109 |
unsigned long sigmask; |
110 |
uint32_t target_sigmask; |
111 |
|
112 |
sigmask = ((unsigned long *)s)[0]; |
113 |
target_sigmask = 0;
|
114 |
for(i = 0; i < 32; i++) { |
115 |
if (sigmask & (1 << i)) |
116 |
target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1); |
117 |
} |
118 |
#if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32 |
119 |
d->sig[0] = target_sigmask;
|
120 |
for(i = 1;i < TARGET_NSIG_WORDS; i++) { |
121 |
d->sig[i] = ((unsigned long *)s)[i]; |
122 |
} |
123 |
#elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2 |
124 |
d->sig[0] = target_sigmask;
|
125 |
d->sig[1] = sigmask >> 32; |
126 |
#else
|
127 |
#warning host_to_target_sigset
|
128 |
#endif
|
129 |
} |
130 |
|
131 |
void host_to_target_sigset(target_sigset_t *d, const sigset_t *s) |
132 |
{ |
133 |
target_sigset_t d1; |
134 |
int i;
|
135 |
|
136 |
host_to_target_sigset_internal(&d1, s); |
137 |
for(i = 0;i < TARGET_NSIG_WORDS; i++) |
138 |
d->sig[i] = tswapl(d1.sig[i]); |
139 |
} |
140 |
|
141 |
void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s) |
142 |
{ |
143 |
int i;
|
144 |
unsigned long sigmask; |
145 |
target_ulong target_sigmask; |
146 |
|
147 |
target_sigmask = s->sig[0];
|
148 |
sigmask = 0;
|
149 |
for(i = 0; i < 32; i++) { |
150 |
if (target_sigmask & (1 << i)) |
151 |
sigmask |= 1 << (target_to_host_signal(i + 1) - 1); |
152 |
} |
153 |
#if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32 |
154 |
((unsigned long *)d)[0] = sigmask; |
155 |
for(i = 1;i < TARGET_NSIG_WORDS; i++) { |
156 |
((unsigned long *)d)[i] = s->sig[i]; |
157 |
} |
158 |
#elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2 |
159 |
((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32); |
160 |
#else
|
161 |
#warning target_to_host_sigset
|
162 |
#endif /* TARGET_LONG_BITS */ |
163 |
} |
164 |
|
165 |
void target_to_host_sigset(sigset_t *d, const target_sigset_t *s) |
166 |
{ |
167 |
target_sigset_t s1; |
168 |
int i;
|
169 |
|
170 |
for(i = 0;i < TARGET_NSIG_WORDS; i++) |
171 |
s1.sig[i] = tswapl(s->sig[i]); |
172 |
target_to_host_sigset_internal(d, &s1); |
173 |
} |
174 |
|
175 |
void host_to_target_old_sigset(target_ulong *old_sigset,
|
176 |
const sigset_t *sigset)
|
177 |
{ |
178 |
target_sigset_t d; |
179 |
host_to_target_sigset(&d, sigset); |
180 |
*old_sigset = d.sig[0];
|
181 |
} |
182 |
|
183 |
void target_to_host_old_sigset(sigset_t *sigset,
|
184 |
const target_ulong *old_sigset)
|
185 |
{ |
186 |
target_sigset_t d; |
187 |
int i;
|
188 |
|
189 |
d.sig[0] = *old_sigset;
|
190 |
for(i = 1;i < TARGET_NSIG_WORDS; i++) |
191 |
d.sig[i] = 0;
|
192 |
target_to_host_sigset(sigset, &d); |
193 |
} |
194 |
|
195 |
/* siginfo conversion */
|
196 |
|
197 |
static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo, |
198 |
const siginfo_t *info)
|
199 |
{ |
200 |
int sig;
|
201 |
sig = host_to_target_signal(info->si_signo); |
202 |
tinfo->si_signo = sig; |
203 |
tinfo->si_errno = 0;
|
204 |
tinfo->si_code = 0;
|
205 |
if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
|
206 |
sig == SIGBUS || sig == SIGTRAP) { |
207 |
/* should never come here, but who knows. The information for
|
208 |
the target is irrelevant */
|
209 |
tinfo->_sifields._sigfault._addr = 0;
|
210 |
} else if (sig >= TARGET_SIGRTMIN) { |
211 |
tinfo->_sifields._rt._pid = info->si_pid; |
212 |
tinfo->_sifields._rt._uid = info->si_uid; |
213 |
/* XXX: potential problem if 64 bit */
|
214 |
tinfo->_sifields._rt._sigval.sival_ptr = |
215 |
(target_ulong)info->si_value.sival_ptr; |
216 |
} |
217 |
} |
218 |
|
219 |
static void tswap_siginfo(target_siginfo_t *tinfo, |
220 |
const target_siginfo_t *info)
|
221 |
{ |
222 |
int sig;
|
223 |
sig = info->si_signo; |
224 |
tinfo->si_signo = tswap32(sig); |
225 |
tinfo->si_errno = tswap32(info->si_errno); |
226 |
tinfo->si_code = tswap32(info->si_code); |
227 |
if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
|
228 |
sig == SIGBUS || sig == SIGTRAP) { |
229 |
tinfo->_sifields._sigfault._addr = |
230 |
tswapl(info->_sifields._sigfault._addr); |
231 |
} else if (sig >= TARGET_SIGRTMIN) { |
232 |
tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid); |
233 |
tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid); |
234 |
tinfo->_sifields._rt._sigval.sival_ptr = |
235 |
tswapl(info->_sifields._rt._sigval.sival_ptr); |
236 |
} |
237 |
} |
238 |
|
239 |
|
240 |
void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info) |
241 |
{ |
242 |
host_to_target_siginfo_noswap(tinfo, info); |
243 |
tswap_siginfo(tinfo, tinfo); |
244 |
} |
245 |
|
246 |
/* XXX: we support only POSIX RT signals are used. */
|
247 |
/* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
|
248 |
void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo) |
249 |
{ |
250 |
info->si_signo = tswap32(tinfo->si_signo); |
251 |
info->si_errno = tswap32(tinfo->si_errno); |
252 |
info->si_code = tswap32(tinfo->si_code); |
253 |
info->si_pid = tswap32(tinfo->_sifields._rt._pid); |
254 |
info->si_uid = tswap32(tinfo->_sifields._rt._uid); |
255 |
info->si_value.sival_ptr = |
256 |
(void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
|
257 |
} |
258 |
|
259 |
void signal_init(void) |
260 |
{ |
261 |
struct sigaction act;
|
262 |
int i, j;
|
263 |
|
264 |
/* generate signal conversion tables */
|
265 |
for(i = 1; i <= 64; i++) { |
266 |
if (host_to_target_signal_table[i] == 0) |
267 |
host_to_target_signal_table[i] = i; |
268 |
} |
269 |
for(i = 1; i <= 64; i++) { |
270 |
j = host_to_target_signal_table[i]; |
271 |
target_to_host_signal_table[j] = i; |
272 |
} |
273 |
|
274 |
/* set all host signal handlers. ALL signals are blocked during
|
275 |
the handlers to serialize them. */
|
276 |
sigfillset(&act.sa_mask); |
277 |
act.sa_flags = SA_SIGINFO; |
278 |
act.sa_sigaction = host_signal_handler; |
279 |
for(i = 1; i < NSIG; i++) { |
280 |
sigaction(i, &act, NULL);
|
281 |
} |
282 |
|
283 |
memset(sigact_table, 0, sizeof(sigact_table)); |
284 |
|
285 |
first_free = &sigqueue_table[0];
|
286 |
for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) |
287 |
sigqueue_table[i].next = &sigqueue_table[i + 1];
|
288 |
sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL; |
289 |
} |
290 |
|
291 |
/* signal queue handling */
|
292 |
|
293 |
static inline struct sigqueue *alloc_sigqueue(void) |
294 |
{ |
295 |
struct sigqueue *q = first_free;
|
296 |
if (!q)
|
297 |
return NULL; |
298 |
first_free = q->next; |
299 |
return q;
|
300 |
} |
301 |
|
302 |
static inline void free_sigqueue(struct sigqueue *q) |
303 |
{ |
304 |
q->next = first_free; |
305 |
first_free = q; |
306 |
} |
307 |
|
308 |
/* abort execution with signal */
|
309 |
void __attribute((noreturn)) force_sig(int sig) |
310 |
{ |
311 |
int host_sig;
|
312 |
host_sig = target_to_host_signal(sig); |
313 |
fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
|
314 |
sig, strsignal(host_sig)); |
315 |
#if 1 |
316 |
_exit(-host_sig); |
317 |
#else
|
318 |
{ |
319 |
struct sigaction act;
|
320 |
sigemptyset(&act.sa_mask); |
321 |
act.sa_flags = SA_SIGINFO; |
322 |
act.sa_sigaction = SIG_DFL; |
323 |
sigaction(SIGABRT, &act, NULL);
|
324 |
abort(); |
325 |
} |
326 |
#endif
|
327 |
} |
328 |
|
329 |
/* queue a signal so that it will be send to the virtual CPU as soon
|
330 |
as possible */
|
331 |
int queue_signal(int sig, target_siginfo_t *info) |
332 |
{ |
333 |
struct emulated_sigaction *k;
|
334 |
struct sigqueue *q, **pq;
|
335 |
target_ulong handler; |
336 |
|
337 |
#if defined(DEBUG_SIGNAL)
|
338 |
fprintf(stderr, "queue_signal: sig=%d\n",
|
339 |
sig); |
340 |
#endif
|
341 |
k = &sigact_table[sig - 1];
|
342 |
handler = k->sa._sa_handler; |
343 |
if (handler == TARGET_SIG_DFL) {
|
344 |
/* default handler : ignore some signal. The other are fatal */
|
345 |
if (sig != TARGET_SIGCHLD &&
|
346 |
sig != TARGET_SIGURG && |
347 |
sig != TARGET_SIGWINCH) { |
348 |
force_sig(sig); |
349 |
} else {
|
350 |
return 0; /* indicate ignored */ |
351 |
} |
352 |
} else if (handler == TARGET_SIG_IGN) { |
353 |
/* ignore signal */
|
354 |
return 0; |
355 |
} else if (handler == TARGET_SIG_ERR) { |
356 |
force_sig(sig); |
357 |
} else {
|
358 |
pq = &k->first; |
359 |
if (sig < TARGET_SIGRTMIN) {
|
360 |
/* if non real time signal, we queue exactly one signal */
|
361 |
if (!k->pending)
|
362 |
q = &k->info; |
363 |
else
|
364 |
return 0; |
365 |
} else {
|
366 |
if (!k->pending) {
|
367 |
/* first signal */
|
368 |
q = &k->info; |
369 |
} else {
|
370 |
q = alloc_sigqueue(); |
371 |
if (!q)
|
372 |
return -EAGAIN;
|
373 |
while (*pq != NULL) |
374 |
pq = &(*pq)->next; |
375 |
} |
376 |
} |
377 |
*pq = q; |
378 |
q->info = *info; |
379 |
q->next = NULL;
|
380 |
k->pending = 1;
|
381 |
/* signal that a new signal is pending */
|
382 |
signal_pending = 1;
|
383 |
return 1; /* indicates that the signal was queued */ |
384 |
} |
385 |
} |
386 |
|
387 |
static void host_signal_handler(int host_signum, siginfo_t *info, |
388 |
void *puc)
|
389 |
{ |
390 |
int sig;
|
391 |
target_siginfo_t tinfo; |
392 |
|
393 |
/* the CPU emulator uses some host signals to detect exceptions,
|
394 |
we we forward to it some signals */
|
395 |
if (host_signum == SIGSEGV || host_signum == SIGBUS
|
396 |
#if defined(TARGET_I386) && defined(USE_CODE_COPY)
|
397 |
|| host_signum == SIGFPE |
398 |
#endif
|
399 |
) { |
400 |
if (cpu_signal_handler(host_signum, info, puc))
|
401 |
return;
|
402 |
} |
403 |
|
404 |
/* get target signal number */
|
405 |
sig = host_to_target_signal(host_signum); |
406 |
if (sig < 1 || sig > TARGET_NSIG) |
407 |
return;
|
408 |
#if defined(DEBUG_SIGNAL)
|
409 |
fprintf(stderr, "qemu: got signal %d\n", sig);
|
410 |
#endif
|
411 |
host_to_target_siginfo_noswap(&tinfo, info); |
412 |
if (queue_signal(sig, &tinfo) == 1) { |
413 |
/* interrupt the virtual CPU as soon as possible */
|
414 |
cpu_interrupt(global_env, CPU_INTERRUPT_EXIT); |
415 |
} |
416 |
} |
417 |
|
418 |
int do_sigaction(int sig, const struct target_sigaction *act, |
419 |
struct target_sigaction *oact)
|
420 |
{ |
421 |
struct emulated_sigaction *k;
|
422 |
struct sigaction act1;
|
423 |
int host_sig;
|
424 |
|
425 |
if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP) |
426 |
return -EINVAL;
|
427 |
k = &sigact_table[sig - 1];
|
428 |
#if defined(DEBUG_SIGNAL)
|
429 |
fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
|
430 |
sig, (int)act, (int)oact); |
431 |
#endif
|
432 |
if (oact) {
|
433 |
oact->_sa_handler = tswapl(k->sa._sa_handler); |
434 |
oact->sa_flags = tswapl(k->sa.sa_flags); |
435 |
#if !defined(TARGET_MIPS)
|
436 |
oact->sa_restorer = tswapl(k->sa.sa_restorer); |
437 |
#endif
|
438 |
oact->sa_mask = k->sa.sa_mask; |
439 |
} |
440 |
if (act) {
|
441 |
k->sa._sa_handler = tswapl(act->_sa_handler); |
442 |
k->sa.sa_flags = tswapl(act->sa_flags); |
443 |
#if !defined(TARGET_MIPS)
|
444 |
k->sa.sa_restorer = tswapl(act->sa_restorer); |
445 |
#endif
|
446 |
k->sa.sa_mask = act->sa_mask; |
447 |
|
448 |
/* we update the host linux signal state */
|
449 |
host_sig = target_to_host_signal(sig); |
450 |
if (host_sig != SIGSEGV && host_sig != SIGBUS) {
|
451 |
sigfillset(&act1.sa_mask); |
452 |
act1.sa_flags = SA_SIGINFO; |
453 |
if (k->sa.sa_flags & TARGET_SA_RESTART)
|
454 |
act1.sa_flags |= SA_RESTART; |
455 |
/* NOTE: it is important to update the host kernel signal
|
456 |
ignore state to avoid getting unexpected interrupted
|
457 |
syscalls */
|
458 |
if (k->sa._sa_handler == TARGET_SIG_IGN) {
|
459 |
act1.sa_sigaction = (void *)SIG_IGN;
|
460 |
} else if (k->sa._sa_handler == TARGET_SIG_DFL) { |
461 |
act1.sa_sigaction = (void *)SIG_DFL;
|
462 |
} else {
|
463 |
act1.sa_sigaction = host_signal_handler; |
464 |
} |
465 |
sigaction(host_sig, &act1, NULL);
|
466 |
} |
467 |
} |
468 |
return 0; |
469 |
} |
470 |
|
471 |
#ifndef offsetof
|
472 |
#define offsetof(type, field) ((size_t) &((type *)0)->field) |
473 |
#endif
|
474 |
|
475 |
static inline int copy_siginfo_to_user(target_siginfo_t *tinfo, |
476 |
const target_siginfo_t *info)
|
477 |
{ |
478 |
tswap_siginfo(tinfo, info); |
479 |
return 0; |
480 |
} |
481 |
|
482 |
#ifdef TARGET_I386
|
483 |
|
484 |
/* from the Linux kernel */
|
485 |
|
486 |
struct target_fpreg {
|
487 |
uint16_t significand[4];
|
488 |
uint16_t exponent; |
489 |
}; |
490 |
|
491 |
struct target_fpxreg {
|
492 |
uint16_t significand[4];
|
493 |
uint16_t exponent; |
494 |
uint16_t padding[3];
|
495 |
}; |
496 |
|
497 |
struct target_xmmreg {
|
498 |
target_ulong element[4];
|
499 |
}; |
500 |
|
501 |
struct target_fpstate {
|
502 |
/* Regular FPU environment */
|
503 |
target_ulong cw; |
504 |
target_ulong sw; |
505 |
target_ulong tag; |
506 |
target_ulong ipoff; |
507 |
target_ulong cssel; |
508 |
target_ulong dataoff; |
509 |
target_ulong datasel; |
510 |
struct target_fpreg _st[8]; |
511 |
uint16_t status; |
512 |
uint16_t magic; /* 0xffff = regular FPU data only */
|
513 |
|
514 |
/* FXSR FPU environment */
|
515 |
target_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */ |
516 |
target_ulong mxcsr; |
517 |
target_ulong reserved; |
518 |
struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */ |
519 |
struct target_xmmreg _xmm[8]; |
520 |
target_ulong padding[56];
|
521 |
}; |
522 |
|
523 |
#define X86_FXSR_MAGIC 0x0000 |
524 |
|
525 |
struct target_sigcontext {
|
526 |
uint16_t gs, __gsh; |
527 |
uint16_t fs, __fsh; |
528 |
uint16_t es, __esh; |
529 |
uint16_t ds, __dsh; |
530 |
target_ulong edi; |
531 |
target_ulong esi; |
532 |
target_ulong ebp; |
533 |
target_ulong esp; |
534 |
target_ulong ebx; |
535 |
target_ulong edx; |
536 |
target_ulong ecx; |
537 |
target_ulong eax; |
538 |
target_ulong trapno; |
539 |
target_ulong err; |
540 |
target_ulong eip; |
541 |
uint16_t cs, __csh; |
542 |
target_ulong eflags; |
543 |
target_ulong esp_at_signal; |
544 |
uint16_t ss, __ssh; |
545 |
target_ulong fpstate; /* pointer */
|
546 |
target_ulong oldmask; |
547 |
target_ulong cr2; |
548 |
}; |
549 |
|
550 |
typedef struct target_sigaltstack { |
551 |
target_ulong ss_sp; |
552 |
int ss_flags;
|
553 |
target_ulong ss_size; |
554 |
} target_stack_t; |
555 |
|
556 |
struct target_ucontext {
|
557 |
target_ulong tuc_flags; |
558 |
target_ulong tuc_link; |
559 |
target_stack_t tuc_stack; |
560 |
struct target_sigcontext tuc_mcontext;
|
561 |
target_sigset_t tuc_sigmask; /* mask last for extensibility */
|
562 |
}; |
563 |
|
564 |
struct sigframe
|
565 |
{ |
566 |
target_ulong pretcode; |
567 |
int sig;
|
568 |
struct target_sigcontext sc;
|
569 |
struct target_fpstate fpstate;
|
570 |
target_ulong extramask[TARGET_NSIG_WORDS-1];
|
571 |
char retcode[8]; |
572 |
}; |
573 |
|
574 |
struct rt_sigframe
|
575 |
{ |
576 |
target_ulong pretcode; |
577 |
int sig;
|
578 |
target_ulong pinfo; |
579 |
target_ulong puc; |
580 |
struct target_siginfo info;
|
581 |
struct target_ucontext uc;
|
582 |
struct target_fpstate fpstate;
|
583 |
char retcode[8]; |
584 |
}; |
585 |
|
586 |
/*
|
587 |
* Set up a signal frame.
|
588 |
*/
|
589 |
|
590 |
/* XXX: save x87 state */
|
591 |
static int |
592 |
setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate, |
593 |
CPUX86State *env, unsigned long mask) |
594 |
{ |
595 |
int err = 0; |
596 |
|
597 |
err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs); |
598 |
err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs); |
599 |
err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es); |
600 |
err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds); |
601 |
err |= __put_user(env->regs[R_EDI], &sc->edi); |
602 |
err |= __put_user(env->regs[R_ESI], &sc->esi); |
603 |
err |= __put_user(env->regs[R_EBP], &sc->ebp); |
604 |
err |= __put_user(env->regs[R_ESP], &sc->esp); |
605 |
err |= __put_user(env->regs[R_EBX], &sc->ebx); |
606 |
err |= __put_user(env->regs[R_EDX], &sc->edx); |
607 |
err |= __put_user(env->regs[R_ECX], &sc->ecx); |
608 |
err |= __put_user(env->regs[R_EAX], &sc->eax); |
609 |
err |= __put_user(env->exception_index, &sc->trapno); |
610 |
err |= __put_user(env->error_code, &sc->err); |
611 |
err |= __put_user(env->eip, &sc->eip); |
612 |
err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs); |
613 |
err |= __put_user(env->eflags, &sc->eflags); |
614 |
err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal); |
615 |
err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss); |
616 |
|
617 |
cpu_x86_fsave(env, (void *)fpstate, 1); |
618 |
fpstate->status = fpstate->sw; |
619 |
err |= __put_user(0xffff, &fpstate->magic);
|
620 |
err |= __put_user(fpstate, &sc->fpstate); |
621 |
|
622 |
/* non-iBCS2 extensions.. */
|
623 |
err |= __put_user(mask, &sc->oldmask); |
624 |
err |= __put_user(env->cr[2], &sc->cr2);
|
625 |
return err;
|
626 |
} |
627 |
|
628 |
/*
|
629 |
* Determine which stack to use..
|
630 |
*/
|
631 |
|
632 |
static inline void * |
633 |
get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
|
634 |
{ |
635 |
unsigned long esp; |
636 |
|
637 |
/* Default to using normal stack */
|
638 |
esp = env->regs[R_ESP]; |
639 |
#if 0
|
640 |
/* This is the X/Open sanctioned signal stack switching. */
|
641 |
if (ka->sa.sa_flags & SA_ONSTACK) {
|
642 |
if (sas_ss_flags(esp) == 0)
|
643 |
esp = current->sas_ss_sp + current->sas_ss_size;
|
644 |
}
|
645 |
|
646 |
/* This is the legacy signal stack switching. */
|
647 |
else
|
648 |
#endif
|
649 |
if ((env->segs[R_SS].selector & 0xffff) != __USER_DS && |
650 |
!(ka->sa.sa_flags & TARGET_SA_RESTORER) && |
651 |
ka->sa.sa_restorer) { |
652 |
esp = (unsigned long) ka->sa.sa_restorer; |
653 |
} |
654 |
return g2h((esp - frame_size) & -8ul); |
655 |
} |
656 |
|
657 |
static void setup_frame(int sig, struct emulated_sigaction *ka, |
658 |
target_sigset_t *set, CPUX86State *env) |
659 |
{ |
660 |
struct sigframe *frame;
|
661 |
int i, err = 0; |
662 |
|
663 |
frame = get_sigframe(ka, env, sizeof(*frame));
|
664 |
|
665 |
if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
666 |
goto give_sigsegv;
|
667 |
err |= __put_user((/*current->exec_domain
|
668 |
&& current->exec_domain->signal_invmap
|
669 |
&& sig < 32
|
670 |
? current->exec_domain->signal_invmap[sig]
|
671 |
: */ sig),
|
672 |
&frame->sig); |
673 |
if (err)
|
674 |
goto give_sigsegv;
|
675 |
|
676 |
setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
|
677 |
if (err)
|
678 |
goto give_sigsegv;
|
679 |
|
680 |
for(i = 1; i < TARGET_NSIG_WORDS; i++) { |
681 |
if (__put_user(set->sig[i], &frame->extramask[i - 1])) |
682 |
goto give_sigsegv;
|
683 |
} |
684 |
|
685 |
/* Set up to return from userspace. If provided, use a stub
|
686 |
already in userspace. */
|
687 |
if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
|
688 |
err |= __put_user(ka->sa.sa_restorer, &frame->pretcode); |
689 |
} else {
|
690 |
err |= __put_user(frame->retcode, &frame->pretcode); |
691 |
/* This is popl %eax ; movl $,%eax ; int $0x80 */
|
692 |
err |= __put_user(0xb858, (short *)(frame->retcode+0)); |
693 |
#if defined(TARGET_X86_64)
|
694 |
#warning "Fix this !" |
695 |
#else
|
696 |
err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2)); |
697 |
#endif
|
698 |
err |= __put_user(0x80cd, (short *)(frame->retcode+6)); |
699 |
} |
700 |
|
701 |
if (err)
|
702 |
goto give_sigsegv;
|
703 |
|
704 |
/* Set up registers for signal handler */
|
705 |
env->regs[R_ESP] = h2g(frame); |
706 |
env->eip = (unsigned long) ka->sa._sa_handler; |
707 |
|
708 |
cpu_x86_load_seg(env, R_DS, __USER_DS); |
709 |
cpu_x86_load_seg(env, R_ES, __USER_DS); |
710 |
cpu_x86_load_seg(env, R_SS, __USER_DS); |
711 |
cpu_x86_load_seg(env, R_CS, __USER_CS); |
712 |
env->eflags &= ~TF_MASK; |
713 |
|
714 |
return;
|
715 |
|
716 |
give_sigsegv:
|
717 |
if (sig == TARGET_SIGSEGV)
|
718 |
ka->sa._sa_handler = TARGET_SIG_DFL; |
719 |
force_sig(TARGET_SIGSEGV /* , current */);
|
720 |
} |
721 |
|
722 |
static void setup_rt_frame(int sig, struct emulated_sigaction *ka, |
723 |
target_siginfo_t *info, |
724 |
target_sigset_t *set, CPUX86State *env) |
725 |
{ |
726 |
struct rt_sigframe *frame;
|
727 |
int i, err = 0; |
728 |
|
729 |
frame = get_sigframe(ka, env, sizeof(*frame));
|
730 |
|
731 |
if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame))) |
732 |
goto give_sigsegv;
|
733 |
|
734 |
err |= __put_user((/*current->exec_domain
|
735 |
&& current->exec_domain->signal_invmap
|
736 |
&& sig < 32
|
737 |
? current->exec_domain->signal_invmap[sig]
|
738 |
: */sig),
|
739 |
&frame->sig); |
740 |
err |= __put_user((target_ulong)&frame->info, &frame->pinfo); |
741 |
err |= __put_user((target_ulong)&frame->uc, &frame->puc); |
742 |
err |= copy_siginfo_to_user(&frame->info, info); |
743 |
if (err)
|
744 |
goto give_sigsegv;
|
745 |
|
746 |
/* Create the ucontext. */
|
747 |
err |= __put_user(0, &frame->uc.tuc_flags);
|
748 |
err |= __put_user(0, &frame->uc.tuc_link);
|
749 |
err |= __put_user(/*current->sas_ss_sp*/ 0, |
750 |
&frame->uc.tuc_stack.ss_sp); |
751 |
err |= __put_user(/* sas_ss_flags(regs->esp) */ 0, |
752 |
&frame->uc.tuc_stack.ss_flags); |
753 |
err |= __put_user(/* current->sas_ss_size */ 0, |
754 |
&frame->uc.tuc_stack.ss_size); |
755 |
err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate, |
756 |
env, set->sig[0]);
|
757 |
for(i = 0; i < TARGET_NSIG_WORDS; i++) { |
758 |
if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
|
759 |
goto give_sigsegv;
|
760 |
} |
761 |
|
762 |
/* Set up to return from userspace. If provided, use a stub
|
763 |
already in userspace. */
|
764 |
if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
|
765 |
err |= __put_user(ka->sa.sa_restorer, &frame->pretcode); |
766 |
} else {
|
767 |
err |= __put_user(frame->retcode, &frame->pretcode); |
768 |
/* This is movl $,%eax ; int $0x80 */
|
769 |
err |= __put_user(0xb8, (char *)(frame->retcode+0)); |
770 |
err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1)); |
771 |
err |= __put_user(0x80cd, (short *)(frame->retcode+5)); |
772 |
} |
773 |
|
774 |
if (err)
|
775 |
goto give_sigsegv;
|
776 |
|
777 |
/* Set up registers for signal handler */
|
778 |
env->regs[R_ESP] = (unsigned long) frame; |
779 |
env->eip = (unsigned long) ka->sa._sa_handler; |
780 |
|
781 |
cpu_x86_load_seg(env, R_DS, __USER_DS); |
782 |
cpu_x86_load_seg(env, R_ES, __USER_DS); |
783 |
cpu_x86_load_seg(env, R_SS, __USER_DS); |
784 |
cpu_x86_load_seg(env, R_CS, __USER_CS); |
785 |
env->eflags &= ~TF_MASK; |
786 |
|
787 |
return;
|
788 |
|
789 |
give_sigsegv:
|
790 |
if (sig == TARGET_SIGSEGV)
|
791 |
ka->sa._sa_handler = TARGET_SIG_DFL; |
792 |
force_sig(TARGET_SIGSEGV /* , current */);
|
793 |
} |
794 |
|
795 |
static int |
796 |
restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax) |
797 |
{ |
798 |
unsigned int err = 0; |
799 |
|
800 |
cpu_x86_load_seg(env, R_GS, lduw(&sc->gs)); |
801 |
cpu_x86_load_seg(env, R_FS, lduw(&sc->fs)); |
802 |
cpu_x86_load_seg(env, R_ES, lduw(&sc->es)); |
803 |
cpu_x86_load_seg(env, R_DS, lduw(&sc->ds)); |
804 |
|
805 |
env->regs[R_EDI] = ldl(&sc->edi); |
806 |
env->regs[R_ESI] = ldl(&sc->esi); |
807 |
env->regs[R_EBP] = ldl(&sc->ebp); |
808 |
env->regs[R_ESP] = ldl(&sc->esp); |
809 |
env->regs[R_EBX] = ldl(&sc->ebx); |
810 |
env->regs[R_EDX] = ldl(&sc->edx); |
811 |
env->regs[R_ECX] = ldl(&sc->ecx); |
812 |
env->eip = ldl(&sc->eip); |
813 |
|
814 |
cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
|
815 |
cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
|
816 |
|
817 |
{ |
818 |
unsigned int tmpflags; |
819 |
tmpflags = ldl(&sc->eflags); |
820 |
env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5); |
821 |
// regs->orig_eax = -1; /* disable syscall checks */
|
822 |
} |
823 |
|
824 |
{ |
825 |
struct _fpstate * buf;
|
826 |
buf = (void *)ldl(&sc->fpstate);
|
827 |
if (buf) {
|
828 |
#if 0
|
829 |
if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
|
830 |
goto badframe;
|
831 |
#endif
|
832 |
cpu_x86_frstor(env, (void *)buf, 1); |
833 |
} |
834 |
} |
835 |
|
836 |
*peax = ldl(&sc->eax); |
837 |
return err;
|
838 |
#if 0
|
839 |
badframe:
|
840 |
return 1;
|
841 |
#endif
|
842 |
} |
843 |
|
844 |
long do_sigreturn(CPUX86State *env)
|
845 |
{ |
846 |
struct sigframe *frame = (struct sigframe *)g2h(env->regs[R_ESP] - 8); |
847 |
target_sigset_t target_set; |
848 |
sigset_t set; |
849 |
int eax, i;
|
850 |
|
851 |
#if defined(DEBUG_SIGNAL)
|
852 |
fprintf(stderr, "do_sigreturn\n");
|
853 |
#endif
|
854 |
/* set blocked signals */
|
855 |
if (__get_user(target_set.sig[0], &frame->sc.oldmask)) |
856 |
goto badframe;
|
857 |
for(i = 1; i < TARGET_NSIG_WORDS; i++) { |
858 |
if (__get_user(target_set.sig[i], &frame->extramask[i - 1])) |
859 |
goto badframe;
|
860 |
} |
861 |
|
862 |
target_to_host_sigset_internal(&set, &target_set); |
863 |
sigprocmask(SIG_SETMASK, &set, NULL);
|
864 |
|
865 |
/* restore registers */
|
866 |
if (restore_sigcontext(env, &frame->sc, &eax))
|
867 |
goto badframe;
|
868 |
return eax;
|
869 |
|
870 |
badframe:
|
871 |
force_sig(TARGET_SIGSEGV); |
872 |
return 0; |
873 |
} |
874 |
|
875 |
long do_rt_sigreturn(CPUX86State *env)
|
876 |
{ |
877 |
struct rt_sigframe *frame = (struct rt_sigframe *)g2h(env->regs[R_ESP] - 4); |
878 |
sigset_t set; |
879 |
// stack_t st;
|
880 |
int eax;
|
881 |
|
882 |
#if 0
|
883 |
if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
|
884 |
goto badframe;
|
885 |
#endif
|
886 |
target_to_host_sigset(&set, &frame->uc.tuc_sigmask); |
887 |
sigprocmask(SIG_SETMASK, &set, NULL);
|
888 |
|
889 |
if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
|
890 |
goto badframe;
|
891 |
|
892 |
#if 0
|
893 |
if (__copy_from_user(&st, &frame->uc.tuc_stack, sizeof(st)))
|
894 |
goto badframe;
|
895 |
/* It is more difficult to avoid calling this function than to
|
896 |
call it and ignore errors. */
|
897 |
do_sigaltstack(&st, NULL, regs->esp);
|
898 |
#endif
|
899 |
return eax;
|
900 |
|
901 |
badframe:
|
902 |
force_sig(TARGET_SIGSEGV); |
903 |
return 0; |
904 |
} |
905 |
|
906 |
#elif defined(TARGET_ARM)
|
907 |
|
908 |
struct target_sigcontext {
|
909 |
target_ulong trap_no; |
910 |
target_ulong error_code; |
911 |
target_ulong oldmask; |
912 |
target_ulong arm_r0; |
913 |
target_ulong arm_r1; |
914 |
target_ulong arm_r2; |
915 |
target_ulong arm_r3; |
916 |
target_ulong arm_r4; |
917 |
target_ulong arm_r5; |
918 |
target_ulong arm_r6; |
919 |
target_ulong arm_r7; |
920 |
target_ulong arm_r8; |
921 |
target_ulong arm_r9; |
922 |
target_ulong arm_r10; |
923 |
target_ulong arm_fp; |
924 |
target_ulong arm_ip; |
925 |
target_ulong arm_sp; |
926 |
target_ulong arm_lr; |
927 |
target_ulong arm_pc; |
928 |
target_ulong arm_cpsr; |
929 |
target_ulong fault_address; |
930 |
}; |
931 |
|
932 |
typedef struct target_sigaltstack { |
933 |
target_ulong ss_sp; |
934 |
int ss_flags;
|
935 |
target_ulong ss_size; |
936 |
} target_stack_t; |
937 |
|
938 |
struct target_ucontext {
|
939 |
target_ulong tuc_flags; |
940 |
target_ulong tuc_link; |
941 |
target_stack_t tuc_stack; |
942 |
struct target_sigcontext tuc_mcontext;
|
943 |
target_sigset_t tuc_sigmask; /* mask last for extensibility */
|
944 |
}; |
945 |
|
946 |
struct sigframe
|
947 |
{ |
948 |
struct target_sigcontext sc;
|
949 |
target_ulong extramask[TARGET_NSIG_WORDS-1];
|
950 |
target_ulong retcode; |
951 |
}; |
952 |
|
953 |
struct rt_sigframe
|
954 |
{ |
955 |
struct target_siginfo *pinfo;
|
956 |
void *puc;
|
957 |
struct target_siginfo info;
|
958 |
struct target_ucontext uc;
|
959 |
target_ulong retcode; |
960 |
}; |
961 |
|
962 |
#define TARGET_CONFIG_CPU_32 1 |
963 |
|
964 |
/*
|
965 |
* For ARM syscalls, we encode the syscall number into the instruction.
|
966 |
*/
|
967 |
#define SWI_SYS_SIGRETURN (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE)) |
968 |
#define SWI_SYS_RT_SIGRETURN (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE)) |
969 |
|
970 |
/*
|
971 |
* For Thumb syscalls, we pass the syscall number via r7. We therefore
|
972 |
* need two 16-bit instructions.
|
973 |
*/
|
974 |
#define SWI_THUMB_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn)) |
975 |
#define SWI_THUMB_RT_SIGRETURN (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn)) |
976 |
|
977 |
static const target_ulong retcodes[4] = { |
978 |
SWI_SYS_SIGRETURN, SWI_THUMB_SIGRETURN, |
979 |
SWI_SYS_RT_SIGRETURN, SWI_THUMB_RT_SIGRETURN |
980 |
}; |
981 |
|
982 |
|
983 |
#define __put_user_error(x,p,e) __put_user(x, p)
|
984 |
#define __get_user_error(x,p,e) __get_user(x, p)
|
985 |
|
986 |
static inline int valid_user_regs(CPUState *regs) |
987 |
{ |
988 |
return 1; |
989 |
} |
990 |
|
991 |
static int |
992 |
setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/ |
993 |
CPUState *env, unsigned long mask) |
994 |
{ |
995 |
int err = 0; |
996 |
|
997 |
__put_user_error(env->regs[0], &sc->arm_r0, err);
|
998 |
__put_user_error(env->regs[1], &sc->arm_r1, err);
|
999 |
__put_user_error(env->regs[2], &sc->arm_r2, err);
|
1000 |
__put_user_error(env->regs[3], &sc->arm_r3, err);
|
1001 |
__put_user_error(env->regs[4], &sc->arm_r4, err);
|
1002 |
__put_user_error(env->regs[5], &sc->arm_r5, err);
|
1003 |
__put_user_error(env->regs[6], &sc->arm_r6, err);
|
1004 |
__put_user_error(env->regs[7], &sc->arm_r7, err);
|
1005 |
__put_user_error(env->regs[8], &sc->arm_r8, err);
|
1006 |
__put_user_error(env->regs[9], &sc->arm_r9, err);
|
1007 |
__put_user_error(env->regs[10], &sc->arm_r10, err);
|
1008 |
__put_user_error(env->regs[11], &sc->arm_fp, err);
|
1009 |
__put_user_error(env->regs[12], &sc->arm_ip, err);
|
1010 |
__put_user_error(env->regs[13], &sc->arm_sp, err);
|
1011 |
__put_user_error(env->regs[14], &sc->arm_lr, err);
|
1012 |
__put_user_error(env->regs[15], &sc->arm_pc, err);
|
1013 |
#ifdef TARGET_CONFIG_CPU_32
|
1014 |
__put_user_error(cpsr_read(env), &sc->arm_cpsr, err); |
1015 |
#endif
|
1016 |
|
1017 |
__put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err); |
1018 |
__put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err); |
1019 |
__put_user_error(/* current->thread.address */ 0, &sc->fault_address, err); |
1020 |
__put_user_error(mask, &sc->oldmask, err); |
1021 |
|
1022 |
return err;
|
1023 |
} |
1024 |
|
1025 |
static inline void * |
1026 |
get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize) |
1027 |
{ |
1028 |
unsigned long sp = regs->regs[13]; |
1029 |
|
1030 |
#if 0
|
1031 |
/*
|
1032 |
* This is the X/Open sanctioned signal stack switching.
|
1033 |
*/
|
1034 |
if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
|
1035 |
sp = current->sas_ss_sp + current->sas_ss_size;
|
1036 |
#endif
|
1037 |
/*
|
1038 |
* ATPCS B01 mandates 8-byte alignment
|
1039 |
*/
|
1040 |
return g2h((sp - framesize) & ~7); |
1041 |
} |
1042 |
|
1043 |
static int |
1044 |
setup_return(CPUState *env, struct emulated_sigaction *ka,
|
1045 |
target_ulong *rc, void *frame, int usig) |
1046 |
{ |
1047 |
target_ulong handler = (target_ulong)ka->sa._sa_handler; |
1048 |
target_ulong retcode; |
1049 |
int thumb = 0; |
1050 |
#if defined(TARGET_CONFIG_CPU_32)
|
1051 |
#if 0
|
1052 |
target_ulong cpsr = env->cpsr;
|
1053 |
|
1054 |
/*
|
1055 |
* Maybe we need to deliver a 32-bit signal to a 26-bit task.
|
1056 |
*/
|
1057 |
if (ka->sa.sa_flags & SA_THIRTYTWO)
|
1058 |
cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
|
1059 |
|
1060 |
#ifdef CONFIG_ARM_THUMB
|
1061 |
if (elf_hwcap & HWCAP_THUMB) {
|
1062 |
/*
|
1063 |
* The LSB of the handler determines if we're going to
|
1064 |
* be using THUMB or ARM mode for this signal handler.
|
1065 |
*/
|
1066 |
thumb = handler & 1;
|
1067 |
|
1068 |
if (thumb)
|
1069 |
cpsr |= T_BIT;
|
1070 |
else
|
1071 |
cpsr &= ~T_BIT;
|
1072 |
}
|
1073 |
#endif
|
1074 |
#endif
|
1075 |
#endif /* TARGET_CONFIG_CPU_32 */ |
1076 |
|
1077 |
if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
|
1078 |
retcode = (target_ulong)ka->sa.sa_restorer; |
1079 |
} else {
|
1080 |
unsigned int idx = thumb; |
1081 |
|
1082 |
if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
|
1083 |
idx += 2;
|
1084 |
|
1085 |
if (__put_user(retcodes[idx], rc))
|
1086 |
return 1; |
1087 |
#if 0
|
1088 |
flush_icache_range((target_ulong)rc,
|
1089 |
(target_ulong)(rc + 1));
|
1090 |
#endif
|
1091 |
retcode = ((target_ulong)rc) + thumb; |
1092 |
} |
1093 |
|
1094 |
env->regs[0] = usig;
|
1095 |
env->regs[13] = h2g(frame);
|
1096 |
env->regs[14] = retcode;
|
1097 |
env->regs[15] = handler & (thumb ? ~1 : ~3); |
1098 |
|
1099 |
#if 0
|
1100 |
#ifdef TARGET_CONFIG_CPU_32
|
1101 |
env->cpsr = cpsr;
|
1102 |
#endif
|
1103 |
#endif
|
1104 |
|
1105 |
return 0; |
1106 |
} |
1107 |
|
1108 |
static void setup_frame(int usig, struct emulated_sigaction *ka, |
1109 |
target_sigset_t *set, CPUState *regs) |
1110 |
{ |
1111 |
struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame)); |
1112 |
int i, err = 0; |
1113 |
|
1114 |
err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]); |
1115 |
|
1116 |
for(i = 1; i < TARGET_NSIG_WORDS; i++) { |
1117 |
if (__put_user(set->sig[i], &frame->extramask[i - 1])) |
1118 |
return;
|
1119 |
} |
1120 |
|
1121 |
if (err == 0) |
1122 |
err = setup_return(regs, ka, &frame->retcode, frame, usig); |
1123 |
// return err;
|
1124 |
} |
1125 |
|
1126 |
static void setup_rt_frame(int usig, struct emulated_sigaction *ka, |
1127 |
target_siginfo_t *info, |
1128 |
target_sigset_t *set, CPUState *env) |
1129 |
{ |
1130 |
struct rt_sigframe *frame = get_sigframe(ka, env, sizeof(*frame)); |
1131 |
int i, err = 0; |
1132 |
|
1133 |
if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame))) |
1134 |
return /* 1 */; |
1135 |
|
1136 |
__put_user_error(&frame->info, (target_ulong *)&frame->pinfo, err); |
1137 |
__put_user_error(&frame->uc, (target_ulong *)&frame->puc, err); |
1138 |
err |= copy_siginfo_to_user(&frame->info, info); |
1139 |
|
1140 |
/* Clear all the bits of the ucontext we don't use. */
|
1141 |
memset(&frame->uc, 0, offsetof(struct target_ucontext, tuc_mcontext)); |
1142 |
|
1143 |
err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
|
1144 |
env, set->sig[0]);
|
1145 |
for(i = 0; i < TARGET_NSIG_WORDS; i++) { |
1146 |
if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
|
1147 |
return;
|
1148 |
} |
1149 |
|
1150 |
if (err == 0) |
1151 |
err = setup_return(env, ka, &frame->retcode, frame, usig); |
1152 |
|
1153 |
if (err == 0) { |
1154 |
/*
|
1155 |
* For realtime signals we must also set the second and third
|
1156 |
* arguments for the signal handler.
|
1157 |
* -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
|
1158 |
*/
|
1159 |
env->regs[1] = (target_ulong)frame->pinfo;
|
1160 |
env->regs[2] = (target_ulong)frame->puc;
|
1161 |
} |
1162 |
|
1163 |
// return err;
|
1164 |
} |
1165 |
|
1166 |
static int |
1167 |
restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
|
1168 |
{ |
1169 |
int err = 0; |
1170 |
uint32_t cpsr; |
1171 |
|
1172 |
__get_user_error(env->regs[0], &sc->arm_r0, err);
|
1173 |
__get_user_error(env->regs[1], &sc->arm_r1, err);
|
1174 |
__get_user_error(env->regs[2], &sc->arm_r2, err);
|
1175 |
__get_user_error(env->regs[3], &sc->arm_r3, err);
|
1176 |
__get_user_error(env->regs[4], &sc->arm_r4, err);
|
1177 |
__get_user_error(env->regs[5], &sc->arm_r5, err);
|
1178 |
__get_user_error(env->regs[6], &sc->arm_r6, err);
|
1179 |
__get_user_error(env->regs[7], &sc->arm_r7, err);
|
1180 |
__get_user_error(env->regs[8], &sc->arm_r8, err);
|
1181 |
__get_user_error(env->regs[9], &sc->arm_r9, err);
|
1182 |
__get_user_error(env->regs[10], &sc->arm_r10, err);
|
1183 |
__get_user_error(env->regs[11], &sc->arm_fp, err);
|
1184 |
__get_user_error(env->regs[12], &sc->arm_ip, err);
|
1185 |
__get_user_error(env->regs[13], &sc->arm_sp, err);
|
1186 |
__get_user_error(env->regs[14], &sc->arm_lr, err);
|
1187 |
__get_user_error(env->regs[15], &sc->arm_pc, err);
|
1188 |
#ifdef TARGET_CONFIG_CPU_32
|
1189 |
__get_user_error(cpsr, &sc->arm_cpsr, err); |
1190 |
cpsr_write(env, cpsr, 0xffffffff);
|
1191 |
#endif
|
1192 |
|
1193 |
err |= !valid_user_regs(env); |
1194 |
|
1195 |
return err;
|
1196 |
} |
1197 |
|
1198 |
long do_sigreturn(CPUState *env)
|
1199 |
{ |
1200 |
struct sigframe *frame;
|
1201 |
target_sigset_t set; |
1202 |
sigset_t host_set; |
1203 |
int i;
|
1204 |
|
1205 |
/*
|
1206 |
* Since we stacked the signal on a 64-bit boundary,
|
1207 |
* then 'sp' should be word aligned here. If it's
|
1208 |
* not, then the user is trying to mess with us.
|
1209 |
*/
|
1210 |
if (env->regs[13] & 7) |
1211 |
goto badframe;
|
1212 |
|
1213 |
frame = (struct sigframe *)g2h(env->regs[13]); |
1214 |
|
1215 |
#if 0
|
1216 |
if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
|
1217 |
goto badframe;
|
1218 |
#endif
|
1219 |
if (__get_user(set.sig[0], &frame->sc.oldmask)) |
1220 |
goto badframe;
|
1221 |
for(i = 1; i < TARGET_NSIG_WORDS; i++) { |
1222 |
if (__get_user(set.sig[i], &frame->extramask[i - 1])) |
1223 |
goto badframe;
|
1224 |
} |
1225 |
|
1226 |
target_to_host_sigset_internal(&host_set, &set); |
1227 |
sigprocmask(SIG_SETMASK, &host_set, NULL);
|
1228 |
|
1229 |
if (restore_sigcontext(env, &frame->sc))
|
1230 |
goto badframe;
|
1231 |
|
1232 |
#if 0
|
1233 |
/* Send SIGTRAP if we're single-stepping */
|
1234 |
if (ptrace_cancel_bpt(current))
|
1235 |
send_sig(SIGTRAP, current, 1);
|
1236 |
#endif
|
1237 |
return env->regs[0]; |
1238 |
|
1239 |
badframe:
|
1240 |
force_sig(SIGSEGV /* , current */);
|
1241 |
return 0; |
1242 |
} |
1243 |
|
1244 |
long do_rt_sigreturn(CPUState *env)
|
1245 |
{ |
1246 |
struct rt_sigframe *frame;
|
1247 |
sigset_t host_set; |
1248 |
|
1249 |
/*
|
1250 |
* Since we stacked the signal on a 64-bit boundary,
|
1251 |
* then 'sp' should be word aligned here. If it's
|
1252 |
* not, then the user is trying to mess with us.
|
1253 |
*/
|
1254 |
if (env->regs[13] & 7) |
1255 |
goto badframe;
|
1256 |
|
1257 |
frame = (struct rt_sigframe *)env->regs[13]; |
1258 |
|
1259 |
#if 0
|
1260 |
if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
|
1261 |
goto badframe;
|
1262 |
#endif
|
1263 |
target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask); |
1264 |
sigprocmask(SIG_SETMASK, &host_set, NULL);
|
1265 |
|
1266 |
if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
|
1267 |
goto badframe;
|
1268 |
|
1269 |
#if 0
|
1270 |
/* Send SIGTRAP if we're single-stepping */
|
1271 |
if (ptrace_cancel_bpt(current))
|
1272 |
send_sig(SIGTRAP, current, 1);
|
1273 |
#endif
|
1274 |
return env->regs[0]; |
1275 |
|
1276 |
badframe:
|
1277 |
force_sig(SIGSEGV /* , current */);
|
1278 |
return 0; |
1279 |
} |
1280 |
|
1281 |
#elif defined(TARGET_SPARC)
|
1282 |
|
1283 |
#define __SUNOS_MAXWIN 31 |
1284 |
|
1285 |
/* This is what SunOS does, so shall I. */
|
1286 |
struct target_sigcontext {
|
1287 |
target_ulong sigc_onstack; /* state to restore */
|
1288 |
|
1289 |
target_ulong sigc_mask; /* sigmask to restore */
|
1290 |
target_ulong sigc_sp; /* stack pointer */
|
1291 |
target_ulong sigc_pc; /* program counter */
|
1292 |
target_ulong sigc_npc; /* next program counter */
|
1293 |
target_ulong sigc_psr; /* for condition codes etc */
|
1294 |
target_ulong sigc_g1; /* User uses these two registers */
|
1295 |
target_ulong sigc_o0; /* within the trampoline code. */
|
1296 |
|
1297 |
/* Now comes information regarding the users window set
|
1298 |
* at the time of the signal.
|
1299 |
*/
|
1300 |
target_ulong sigc_oswins; /* outstanding windows */
|
1301 |
|
1302 |
/* stack ptrs for each regwin buf */
|
1303 |
char *sigc_spbuf[__SUNOS_MAXWIN];
|
1304 |
|
1305 |
/* Windows to restore after signal */
|
1306 |
struct {
|
1307 |
target_ulong locals[8];
|
1308 |
target_ulong ins[8];
|
1309 |
} sigc_wbuf[__SUNOS_MAXWIN]; |
1310 |
}; |
1311 |
/* A Sparc stack frame */
|
1312 |
struct sparc_stackf {
|
1313 |
target_ulong locals[8];
|
1314 |
target_ulong ins[6];
|
1315 |
struct sparc_stackf *fp;
|
1316 |
target_ulong callers_pc; |
1317 |
char *structptr;
|
1318 |
target_ulong xargs[6];
|
1319 |
target_ulong xxargs[1];
|
1320 |
}; |
1321 |
|
1322 |
typedef struct { |
1323 |
struct {
|
1324 |
target_ulong psr; |
1325 |
target_ulong pc; |
1326 |
target_ulong npc; |
1327 |
target_ulong y; |
1328 |
target_ulong u_regs[16]; /* globals and ins */ |
1329 |
} si_regs; |
1330 |
int si_mask;
|
1331 |
} __siginfo_t; |
1332 |
|
1333 |
typedef struct { |
1334 |
unsigned long si_float_regs [32]; |
1335 |
unsigned long si_fsr; |
1336 |
unsigned long si_fpqdepth; |
1337 |
struct {
|
1338 |
unsigned long *insn_addr; |
1339 |
unsigned long insn; |
1340 |
} si_fpqueue [16];
|
1341 |
} qemu_siginfo_fpu_t; |
1342 |
|
1343 |
|
1344 |
struct target_signal_frame {
|
1345 |
struct sparc_stackf ss;
|
1346 |
__siginfo_t info; |
1347 |
qemu_siginfo_fpu_t *fpu_save; |
1348 |
target_ulong insns[2] __attribute__ ((aligned (8))); |
1349 |
target_ulong extramask[TARGET_NSIG_WORDS - 1];
|
1350 |
target_ulong extra_size; /* Should be 0 */
|
1351 |
qemu_siginfo_fpu_t fpu_state; |
1352 |
}; |
1353 |
struct target_rt_signal_frame {
|
1354 |
struct sparc_stackf ss;
|
1355 |
siginfo_t info; |
1356 |
target_ulong regs[20];
|
1357 |
sigset_t mask; |
1358 |
qemu_siginfo_fpu_t *fpu_save; |
1359 |
unsigned int insns[2]; |
1360 |
stack_t stack; |
1361 |
unsigned int extra_size; /* Should be 0 */ |
1362 |
qemu_siginfo_fpu_t fpu_state; |
1363 |
}; |
1364 |
|
1365 |
#define UREG_O0 16 |
1366 |
#define UREG_O6 22 |
1367 |
#define UREG_I0 0 |
1368 |
#define UREG_I1 1 |
1369 |
#define UREG_I2 2 |
1370 |
#define UREG_I6 6 |
1371 |
#define UREG_I7 7 |
1372 |
#define UREG_L0 8 |
1373 |
#define UREG_FP UREG_I6
|
1374 |
#define UREG_SP UREG_O6
|
1375 |
|
1376 |
static inline void *get_sigframe(struct emulated_sigaction *sa, CPUState *env, unsigned long framesize) |
1377 |
{ |
1378 |
unsigned long sp; |
1379 |
|
1380 |
sp = env->regwptr[UREG_FP]; |
1381 |
#if 0
|
1382 |
|
1383 |
/* This is the X/Open sanctioned signal stack switching. */
|
1384 |
if (sa->sa_flags & TARGET_SA_ONSTACK) {
|
1385 |
if (!on_sig_stack(sp) && !((current->sas_ss_sp + current->sas_ss_size) & 7))
|
1386 |
sp = current->sas_ss_sp + current->sas_ss_size;
|
1387 |
}
|
1388 |
#endif
|
1389 |
return g2h(sp - framesize);
|
1390 |
} |
1391 |
|
1392 |
static int |
1393 |
setup___siginfo(__siginfo_t *si, CPUState *env, target_ulong mask) |
1394 |
{ |
1395 |
int err = 0, i; |
1396 |
|
1397 |
err |= __put_user(env->psr, &si->si_regs.psr); |
1398 |
err |= __put_user(env->pc, &si->si_regs.pc); |
1399 |
err |= __put_user(env->npc, &si->si_regs.npc); |
1400 |
err |= __put_user(env->y, &si->si_regs.y); |
1401 |
for (i=0; i < 8; i++) { |
1402 |
err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]); |
1403 |
} |
1404 |
for (i=0; i < 8; i++) { |
1405 |
err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
|
1406 |
} |
1407 |
err |= __put_user(mask, &si->si_mask); |
1408 |
return err;
|
1409 |
} |
1410 |
|
1411 |
#if 0
|
1412 |
static int
|
1413 |
setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
|
1414 |
CPUState *env, unsigned long mask)
|
1415 |
{
|
1416 |
int err = 0;
|
1417 |
|
1418 |
err |= __put_user(mask, &sc->sigc_mask);
|
1419 |
err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
|
1420 |
err |= __put_user(env->pc, &sc->sigc_pc);
|
1421 |
err |= __put_user(env->npc, &sc->sigc_npc);
|
1422 |
err |= __put_user(env->psr, &sc->sigc_psr);
|
1423 |
err |= __put_user(env->gregs[1], &sc->sigc_g1);
|
1424 |
err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
|
1425 |
|
1426 |
return err;
|
1427 |
}
|
1428 |
#endif
|
1429 |
#define NF_ALIGNEDSZ (((sizeof(struct target_signal_frame) + 7) & (~7))) |
1430 |
|
1431 |
static void setup_frame(int sig, struct emulated_sigaction *ka, |
1432 |
target_sigset_t *set, CPUState *env) |
1433 |
{ |
1434 |
struct target_signal_frame *sf;
|
1435 |
int sigframe_size, err, i;
|
1436 |
|
1437 |
/* 1. Make sure everything is clean */
|
1438 |
//synchronize_user_stack();
|
1439 |
|
1440 |
sigframe_size = NF_ALIGNEDSZ; |
1441 |
|
1442 |
sf = (struct target_signal_frame *)
|
1443 |
get_sigframe(ka, env, sigframe_size); |
1444 |
|
1445 |
//fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
|
1446 |
#if 0
|
1447 |
if (invalid_frame_pointer(sf, sigframe_size))
|
1448 |
goto sigill_and_return;
|
1449 |
#endif
|
1450 |
/* 2. Save the current process state */
|
1451 |
err = setup___siginfo(&sf->info, env, set->sig[0]);
|
1452 |
err |= __put_user(0, &sf->extra_size);
|
1453 |
|
1454 |
//err |= save_fpu_state(regs, &sf->fpu_state);
|
1455 |
//err |= __put_user(&sf->fpu_state, &sf->fpu_save);
|
1456 |
|
1457 |
err |= __put_user(set->sig[0], &sf->info.si_mask);
|
1458 |
for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) { |
1459 |
err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
|
1460 |
} |
1461 |
|
1462 |
for (i = 0; i < 8; i++) { |
1463 |
err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]); |
1464 |
} |
1465 |
for (i = 0; i < 8; i++) { |
1466 |
err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]); |
1467 |
} |
1468 |
if (err)
|
1469 |
goto sigsegv;
|
1470 |
|
1471 |
/* 3. signal handler back-trampoline and parameters */
|
1472 |
env->regwptr[UREG_FP] = h2g(sf); |
1473 |
env->regwptr[UREG_I0] = sig; |
1474 |
env->regwptr[UREG_I1] = h2g(&sf->info); |
1475 |
env->regwptr[UREG_I2] = h2g(&sf->info); |
1476 |
|
1477 |
/* 4. signal handler */
|
1478 |
env->pc = (unsigned long) ka->sa._sa_handler; |
1479 |
env->npc = (env->pc + 4);
|
1480 |
/* 5. return to kernel instructions */
|
1481 |
if (ka->sa.sa_restorer)
|
1482 |
env->regwptr[UREG_I7] = (unsigned long)ka->sa.sa_restorer; |
1483 |
else {
|
1484 |
env->regwptr[UREG_I7] = h2g(&(sf->insns[0]) - 2); |
1485 |
|
1486 |
/* mov __NR_sigreturn, %g1 */
|
1487 |
err |= __put_user(0x821020d8, &sf->insns[0]); |
1488 |
|
1489 |
/* t 0x10 */
|
1490 |
err |= __put_user(0x91d02010, &sf->insns[1]); |
1491 |
if (err)
|
1492 |
goto sigsegv;
|
1493 |
|
1494 |
/* Flush instruction space. */
|
1495 |
//flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
|
1496 |
// tb_flush(env);
|
1497 |
} |
1498 |
return;
|
1499 |
|
1500 |
//sigill_and_return:
|
1501 |
force_sig(TARGET_SIGILL); |
1502 |
sigsegv:
|
1503 |
//fprintf(stderr, "force_sig\n");
|
1504 |
force_sig(TARGET_SIGSEGV); |
1505 |
} |
1506 |
static inline int |
1507 |
restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu) |
1508 |
{ |
1509 |
int err;
|
1510 |
#if 0
|
1511 |
#ifdef CONFIG_SMP
|
1512 |
if (current->flags & PF_USEDFPU)
|
1513 |
regs->psr &= ~PSR_EF;
|
1514 |
#else
|
1515 |
if (current == last_task_used_math) {
|
1516 |
last_task_used_math = 0;
|
1517 |
regs->psr &= ~PSR_EF; |
1518 |
} |
1519 |
#endif
|
1520 |
current->used_math = 1;
|
1521 |
current->flags &= ~PF_USEDFPU; |
1522 |
#endif
|
1523 |
#if 0
|
1524 |
if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
|
1525 |
return -EFAULT;
|
1526 |
#endif
|
1527 |
|
1528 |
#if 0
|
1529 |
/* XXX: incorrect */
|
1530 |
err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
|
1531 |
(sizeof(unsigned long) * 32));
|
1532 |
#endif
|
1533 |
err |= __get_user(env->fsr, &fpu->si_fsr); |
1534 |
#if 0
|
1535 |
err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
|
1536 |
if (current->thread.fpqdepth != 0)
|
1537 |
err |= __copy_from_user(¤t->thread.fpqueue[0],
|
1538 |
&fpu->si_fpqueue[0],
|
1539 |
((sizeof(unsigned long) +
|
1540 |
(sizeof(unsigned long *)))*16));
|
1541 |
#endif
|
1542 |
return err;
|
1543 |
} |
1544 |
|
1545 |
|
1546 |
static void setup_rt_frame(int sig, struct emulated_sigaction *ka, |
1547 |
target_siginfo_t *info, |
1548 |
target_sigset_t *set, CPUState *env) |
1549 |
{ |
1550 |
fprintf(stderr, "setup_rt_frame: not implemented\n");
|
1551 |
} |
1552 |
|
1553 |
long do_sigreturn(CPUState *env)
|
1554 |
{ |
1555 |
struct target_signal_frame *sf;
|
1556 |
uint32_t up_psr, pc, npc; |
1557 |
target_sigset_t set; |
1558 |
sigset_t host_set; |
1559 |
target_ulong fpu_save; |
1560 |
int err, i;
|
1561 |
|
1562 |
sf = (struct target_signal_frame *)g2h(env->regwptr[UREG_FP]);
|
1563 |
#if 0
|
1564 |
fprintf(stderr, "sigreturn\n");
|
1565 |
fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
|
1566 |
#endif
|
1567 |
//cpu_dump_state(env, stderr, fprintf, 0);
|
1568 |
|
1569 |
/* 1. Make sure we are not getting garbage from the user */
|
1570 |
#if 0
|
1571 |
if (verify_area (VERIFY_READ, sf, sizeof (*sf)))
|
1572 |
goto segv_and_exit;
|
1573 |
#endif
|
1574 |
|
1575 |
if (((uint) sf) & 3) |
1576 |
goto segv_and_exit;
|
1577 |
|
1578 |
err = __get_user(pc, &sf->info.si_regs.pc); |
1579 |
err |= __get_user(npc, &sf->info.si_regs.npc); |
1580 |
|
1581 |
if ((pc | npc) & 3) |
1582 |
goto segv_and_exit;
|
1583 |
|
1584 |
/* 2. Restore the state */
|
1585 |
err |= __get_user(up_psr, &sf->info.si_regs.psr); |
1586 |
|
1587 |
/* User can only change condition codes and FPU enabling in %psr. */
|
1588 |
env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
|
1589 |
| (env->psr & ~(PSR_ICC /* | PSR_EF */));
|
1590 |
|
1591 |
env->pc = pc; |
1592 |
env->npc = npc; |
1593 |
err |= __get_user(env->y, &sf->info.si_regs.y); |
1594 |
for (i=0; i < 8; i++) { |
1595 |
err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]); |
1596 |
} |
1597 |
for (i=0; i < 8; i++) { |
1598 |
err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
|
1599 |
} |
1600 |
|
1601 |
err |= __get_user(fpu_save, (target_ulong *)&sf->fpu_save); |
1602 |
|
1603 |
//if (fpu_save)
|
1604 |
// err |= restore_fpu_state(env, fpu_save);
|
1605 |
|
1606 |
/* This is pretty much atomic, no amount locking would prevent
|
1607 |
* the races which exist anyways.
|
1608 |
*/
|
1609 |
err |= __get_user(set.sig[0], &sf->info.si_mask);
|
1610 |
for(i = 1; i < TARGET_NSIG_WORDS; i++) { |
1611 |
err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
|
1612 |
} |
1613 |
|
1614 |
target_to_host_sigset_internal(&host_set, &set); |
1615 |
sigprocmask(SIG_SETMASK, &host_set, NULL);
|
1616 |
|
1617 |
if (err)
|
1618 |
goto segv_and_exit;
|
1619 |
|
1620 |
return env->regwptr[0]; |
1621 |
|
1622 |
segv_and_exit:
|
1623 |
force_sig(TARGET_SIGSEGV); |
1624 |
} |
1625 |
|
1626 |
long do_rt_sigreturn(CPUState *env)
|
1627 |
{ |
1628 |
fprintf(stderr, "do_rt_sigreturn: not implemented\n");
|
1629 |
return -ENOSYS;
|
1630 |
} |
1631 |
|
1632 |
#elif defined(TARGET_MIPS)
|
1633 |
|
1634 |
struct target_sigcontext {
|
1635 |
uint32_t sc_regmask; /* Unused */
|
1636 |
uint32_t sc_status; |
1637 |
uint64_t sc_pc; |
1638 |
uint64_t sc_regs[32];
|
1639 |
uint64_t sc_fpregs[32];
|
1640 |
uint32_t sc_ownedfp; /* Unused */
|
1641 |
uint32_t sc_fpc_csr; |
1642 |
uint32_t sc_fpc_eir; /* Unused */
|
1643 |
uint32_t sc_used_math; |
1644 |
uint32_t sc_dsp; /* dsp status, was sc_ssflags */
|
1645 |
uint64_t sc_mdhi; |
1646 |
uint64_t sc_mdlo; |
1647 |
target_ulong sc_hi1; /* Was sc_cause */
|
1648 |
target_ulong sc_lo1; /* Was sc_badvaddr */
|
1649 |
target_ulong sc_hi2; /* Was sc_sigset[4] */
|
1650 |
target_ulong sc_lo2; |
1651 |
target_ulong sc_hi3; |
1652 |
target_ulong sc_lo3; |
1653 |
}; |
1654 |
|
1655 |
struct sigframe {
|
1656 |
uint32_t sf_ass[4]; /* argument save space for o32 */ |
1657 |
uint32_t sf_code[2]; /* signal trampoline */ |
1658 |
struct target_sigcontext sf_sc;
|
1659 |
target_sigset_t sf_mask; |
1660 |
}; |
1661 |
|
1662 |
/* Install trampoline to jump back from signal handler */
|
1663 |
static inline int install_sigtramp(unsigned int *tramp, unsigned int syscall) |
1664 |
{ |
1665 |
int err;
|
1666 |
|
1667 |
/*
|
1668 |
* Set up the return code ...
|
1669 |
*
|
1670 |
* li v0, __NR__foo_sigreturn
|
1671 |
* syscall
|
1672 |
*/
|
1673 |
|
1674 |
err = __put_user(0x24020000 + syscall, tramp + 0); |
1675 |
err |= __put_user(0x0000000c , tramp + 1); |
1676 |
/* flush_cache_sigtramp((unsigned long) tramp); */
|
1677 |
return err;
|
1678 |
} |
1679 |
|
1680 |
static inline int |
1681 |
setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
|
1682 |
{ |
1683 |
int err = 0; |
1684 |
|
1685 |
err |= __put_user(regs->PC, &sc->sc_pc); |
1686 |
|
1687 |
#define save_gp_reg(i) do { \ |
1688 |
err |= __put_user(regs->gpr[i], &sc->sc_regs[i]); \ |
1689 |
} while(0) |
1690 |
__put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2); |
1691 |
save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6); |
1692 |
save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10); |
1693 |
save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14); |
1694 |
save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18); |
1695 |
save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22); |
1696 |
save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26); |
1697 |
save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30); |
1698 |
save_gp_reg(31);
|
1699 |
#undef save_gp_reg
|
1700 |
|
1701 |
err |= __put_user(regs->HI, &sc->sc_mdhi); |
1702 |
err |= __put_user(regs->LO, &sc->sc_mdlo); |
1703 |
|
1704 |
/* Not used yet, but might be useful if we ever have DSP suppport */
|
1705 |
#if 0
|
1706 |
if (cpu_has_dsp) {
|
1707 |
err |= __put_user(mfhi1(), &sc->sc_hi1);
|
1708 |
err |= __put_user(mflo1(), &sc->sc_lo1);
|
1709 |
err |= __put_user(mfhi2(), &sc->sc_hi2);
|
1710 |
err |= __put_user(mflo2(), &sc->sc_lo2);
|
1711 |
err |= __put_user(mfhi3(), &sc->sc_hi3);
|
1712 |
err |= __put_user(mflo3(), &sc->sc_lo3);
|
1713 |
err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
|
1714 |
}
|
1715 |
/* same with 64 bit */
|
1716 |
#ifdef CONFIG_64BIT
|
1717 |
err |= __put_user(regs->hi, &sc->sc_hi[0]);
|
1718 |
err |= __put_user(regs->lo, &sc->sc_lo[0]);
|
1719 |
if (cpu_has_dsp) {
|
1720 |
err |= __put_user(mfhi1(), &sc->sc_hi[1]);
|
1721 |
err |= __put_user(mflo1(), &sc->sc_lo[1]);
|
1722 |
err |= __put_user(mfhi2(), &sc->sc_hi[2]);
|
1723 |
err |= __put_user(mflo2(), &sc->sc_lo[2]);
|
1724 |
err |= __put_user(mfhi3(), &sc->sc_hi[3]);
|
1725 |
err |= __put_user(mflo3(), &sc->sc_lo[3]);
|
1726 |
err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
|
1727 |
}
|
1728 |
#endif
|
1729 |
|
1730 |
|
1731 |
#endif
|
1732 |
|
1733 |
|
1734 |
#if 0
|
1735 |
err |= __put_user(!!used_math(), &sc->sc_used_math);
|
1736 |
|
1737 |
if (!used_math())
|
1738 |
goto out;
|
1739 |
|
1740 |
/*
|
1741 |
* Save FPU state to signal context. Signal handler will "inherit"
|
1742 |
* current FPU state.
|
1743 |
*/
|
1744 |
preempt_disable();
|
1745 |
|
1746 |
if (!is_fpu_owner()) {
|
1747 |
own_fpu();
|
1748 |
restore_fp(current);
|
1749 |
}
|
1750 |
err |= save_fp_context(sc);
|
1751 |
|
1752 |
preempt_enable();
|
1753 |
out:
|
1754 |
#endif
|
1755 |
return err;
|
1756 |
} |
1757 |
|
1758 |
static inline int |
1759 |
restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
|
1760 |
{ |
1761 |
int err = 0; |
1762 |
|
1763 |
err |= __get_user(regs->CP0_EPC, &sc->sc_pc); |
1764 |
|
1765 |
err |= __get_user(regs->HI, &sc->sc_mdhi); |
1766 |
err |= __get_user(regs->LO, &sc->sc_mdlo); |
1767 |
|
1768 |
#define restore_gp_reg(i) do { \ |
1769 |
err |= __get_user(regs->gpr[i], &sc->sc_regs[i]); \ |
1770 |
} while(0) |
1771 |
restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3); |
1772 |
restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6); |
1773 |
restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9); |
1774 |
restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12); |
1775 |
restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15); |
1776 |
restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18); |
1777 |
restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21); |
1778 |
restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24); |
1779 |
restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27); |
1780 |
restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30); |
1781 |
restore_gp_reg(31);
|
1782 |
#undef restore_gp_reg
|
1783 |
|
1784 |
#if 0
|
1785 |
if (cpu_has_dsp) {
|
1786 |
err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
|
1787 |
err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
|
1788 |
err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
|
1789 |
err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
|
1790 |
err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
|
1791 |
err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
|
1792 |
err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
|
1793 |
}
|
1794 |
#ifdef CONFIG_64BIT
|
1795 |
err |= __get_user(regs->hi, &sc->sc_hi[0]);
|
1796 |
err |= __get_user(regs->lo, &sc->sc_lo[0]);
|
1797 |
if (cpu_has_dsp) {
|
1798 |
err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
|
1799 |
err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
|
1800 |
err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
|
1801 |
err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
|
1802 |
err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
|
1803 |
err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
|
1804 |
err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
|
1805 |
}
|
1806 |
#endif
|
1807 |
|
1808 |
err |= __get_user(used_math, &sc->sc_used_math);
|
1809 |
conditional_used_math(used_math);
|
1810 |
|
1811 |
preempt_disable();
|
1812 |
|
1813 |
if (used_math()) {
|
1814 |
/* restore fpu context if we have used it before */
|
1815 |
own_fpu();
|
1816 |
err |= restore_fp_context(sc);
|
1817 |
} else {
|
1818 |
/* signal handler may have used FPU. Give it up. */
|
1819 |
lose_fpu();
|
1820 |
}
|
1821 |
|
1822 |
preempt_enable();
|
1823 |
#endif
|
1824 |
return err;
|
1825 |
} |
1826 |
/*
|
1827 |
* Determine which stack to use..
|
1828 |
*/
|
1829 |
static inline void * |
1830 |
get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
|
1831 |
{ |
1832 |
unsigned long sp; |
1833 |
|
1834 |
/* Default to using normal stack */
|
1835 |
sp = regs->gpr[29];
|
1836 |
|
1837 |
/*
|
1838 |
* FPU emulator may have it's own trampoline active just
|
1839 |
* above the user stack, 16-bytes before the next lowest
|
1840 |
* 16 byte boundary. Try to avoid trashing it.
|
1841 |
*/
|
1842 |
sp -= 32;
|
1843 |
|
1844 |
#if 0
|
1845 |
/* This is the X/Open sanctioned signal stack switching. */
|
1846 |
if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
|
1847 |
sp = current->sas_ss_sp + current->sas_ss_size;
|
1848 |
#endif
|
1849 |
|
1850 |
return g2h((sp - frame_size) & ~7); |
1851 |
} |
1852 |
|
1853 |
static void setup_frame(int sig, struct emulated_sigaction * ka, |
1854 |
target_sigset_t *set, CPUState *regs) |
1855 |
{ |
1856 |
struct sigframe *frame;
|
1857 |
int i;
|
1858 |
|
1859 |
frame = get_sigframe(ka, regs, sizeof(*frame));
|
1860 |
if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame))) |
1861 |
goto give_sigsegv;
|
1862 |
|
1863 |
install_sigtramp(frame->sf_code, TARGET_NR_sigreturn); |
1864 |
|
1865 |
if(setup_sigcontext(regs, &frame->sf_sc))
|
1866 |
goto give_sigsegv;
|
1867 |
|
1868 |
for(i = 0; i < TARGET_NSIG_WORDS; i++) { |
1869 |
if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
|
1870 |
goto give_sigsegv;
|
1871 |
} |
1872 |
|
1873 |
/*
|
1874 |
* Arguments to signal handler:
|
1875 |
*
|
1876 |
* a0 = signal number
|
1877 |
* a1 = 0 (should be cause)
|
1878 |
* a2 = pointer to struct sigcontext
|
1879 |
*
|
1880 |
* $25 and PC point to the signal handler, $29 points to the
|
1881 |
* struct sigframe.
|
1882 |
*/
|
1883 |
regs->gpr[ 4] = sig;
|
1884 |
regs->gpr[ 5] = 0; |
1885 |
regs->gpr[ 6] = h2g(&frame->sf_sc);
|
1886 |
regs->gpr[29] = h2g(frame);
|
1887 |
regs->gpr[31] = h2g(frame->sf_code);
|
1888 |
/* The original kernel code sets CP0_EPC to the handler
|
1889 |
* since it returns to userland using eret
|
1890 |
* we cannot do this here, and we must set PC directly */
|
1891 |
regs->PC = regs->gpr[25] = ka->sa._sa_handler;
|
1892 |
return;
|
1893 |
|
1894 |
give_sigsegv:
|
1895 |
force_sig(TARGET_SIGSEGV/*, current*/);
|
1896 |
return;
|
1897 |
} |
1898 |
|
1899 |
long do_sigreturn(CPUState *regs)
|
1900 |
{ |
1901 |
struct sigframe *frame;
|
1902 |
sigset_t blocked; |
1903 |
target_sigset_t target_set; |
1904 |
int i;
|
1905 |
|
1906 |
#if defined(DEBUG_SIGNAL)
|
1907 |
fprintf(stderr, "do_sigreturn\n");
|
1908 |
#endif
|
1909 |
frame = (struct sigframe *) regs->gpr[29]; |
1910 |
if (!access_ok(VERIFY_READ, frame, sizeof(*frame))) |
1911 |
goto badframe;
|
1912 |
|
1913 |
for(i = 0; i < TARGET_NSIG_WORDS; i++) { |
1914 |
if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
|
1915 |
goto badframe;
|
1916 |
} |
1917 |
|
1918 |
target_to_host_sigset_internal(&blocked, &target_set); |
1919 |
sigprocmask(SIG_SETMASK, &blocked, NULL);
|
1920 |
|
1921 |
if (restore_sigcontext(regs, &frame->sf_sc))
|
1922 |
goto badframe;
|
1923 |
|
1924 |
#if 0
|
1925 |
/*
|
1926 |
* Don't let your children do this ...
|
1927 |
*/
|
1928 |
__asm__ __volatile__(
|
1929 |
"move\t$29, %0\n\t"
|
1930 |
"j\tsyscall_exit"
|
1931 |
:/* no outputs */
|
1932 |
:"r" (®s));
|
1933 |
/* Unreached */
|
1934 |
#endif
|
1935 |
|
1936 |
regs->PC = regs->CP0_EPC; |
1937 |
/* I am not sure this is right, but it seems to work
|
1938 |
* maybe a problem with nested signals ? */
|
1939 |
regs->CP0_EPC = 0;
|
1940 |
return 0; |
1941 |
|
1942 |
badframe:
|
1943 |
force_sig(TARGET_SIGSEGV/*, current*/);
|
1944 |
return 0; |
1945 |
|
1946 |
} |
1947 |
|
1948 |
static void setup_rt_frame(int sig, struct emulated_sigaction *ka, |
1949 |
target_siginfo_t *info, |
1950 |
target_sigset_t *set, CPUState *env) |
1951 |
{ |
1952 |
fprintf(stderr, "setup_rt_frame: not implemented\n");
|
1953 |
} |
1954 |
|
1955 |
long do_rt_sigreturn(CPUState *env)
|
1956 |
{ |
1957 |
fprintf(stderr, "do_rt_sigreturn: not implemented\n");
|
1958 |
return -ENOSYS;
|
1959 |
} |
1960 |
|
1961 |
#else
|
1962 |
|
1963 |
static void setup_frame(int sig, struct emulated_sigaction *ka, |
1964 |
target_sigset_t *set, CPUState *env) |
1965 |
{ |
1966 |
fprintf(stderr, "setup_frame: not implemented\n");
|
1967 |
} |
1968 |
|
1969 |
static void setup_rt_frame(int sig, struct emulated_sigaction *ka, |
1970 |
target_siginfo_t *info, |
1971 |
target_sigset_t *set, CPUState *env) |
1972 |
{ |
1973 |
fprintf(stderr, "setup_rt_frame: not implemented\n");
|
1974 |
} |
1975 |
|
1976 |
long do_sigreturn(CPUState *env)
|
1977 |
{ |
1978 |
fprintf(stderr, "do_sigreturn: not implemented\n");
|
1979 |
return -ENOSYS;
|
1980 |
} |
1981 |
|
1982 |
long do_rt_sigreturn(CPUState *env)
|
1983 |
{ |
1984 |
fprintf(stderr, "do_rt_sigreturn: not implemented\n");
|
1985 |
return -ENOSYS;
|
1986 |
} |
1987 |
|
1988 |
#endif
|
1989 |
|
1990 |
void process_pending_signals(void *cpu_env) |
1991 |
{ |
1992 |
int sig;
|
1993 |
target_ulong handler; |
1994 |
sigset_t set, old_set; |
1995 |
target_sigset_t target_old_set; |
1996 |
struct emulated_sigaction *k;
|
1997 |
struct sigqueue *q;
|
1998 |
|
1999 |
if (!signal_pending)
|
2000 |
return;
|
2001 |
|
2002 |
k = sigact_table; |
2003 |
for(sig = 1; sig <= TARGET_NSIG; sig++) { |
2004 |
if (k->pending)
|
2005 |
goto handle_signal;
|
2006 |
k++; |
2007 |
} |
2008 |
/* if no signal is pending, just return */
|
2009 |
signal_pending = 0;
|
2010 |
return;
|
2011 |
|
2012 |
handle_signal:
|
2013 |
#ifdef DEBUG_SIGNAL
|
2014 |
fprintf(stderr, "qemu: process signal %d\n", sig);
|
2015 |
#endif
|
2016 |
/* dequeue signal */
|
2017 |
q = k->first; |
2018 |
k->first = q->next; |
2019 |
if (!k->first)
|
2020 |
k->pending = 0;
|
2021 |
|
2022 |
sig = gdb_handlesig (cpu_env, sig); |
2023 |
if (!sig) {
|
2024 |
fprintf (stderr, "Lost signal\n");
|
2025 |
abort(); |
2026 |
} |
2027 |
|
2028 |
handler = k->sa._sa_handler; |
2029 |
if (handler == TARGET_SIG_DFL) {
|
2030 |
/* default handler : ignore some signal. The other are fatal */
|
2031 |
if (sig != TARGET_SIGCHLD &&
|
2032 |
sig != TARGET_SIGURG && |
2033 |
sig != TARGET_SIGWINCH) { |
2034 |
force_sig(sig); |
2035 |
} |
2036 |
} else if (handler == TARGET_SIG_IGN) { |
2037 |
/* ignore sig */
|
2038 |
} else if (handler == TARGET_SIG_ERR) { |
2039 |
force_sig(sig); |
2040 |
} else {
|
2041 |
/* compute the blocked signals during the handler execution */
|
2042 |
target_to_host_sigset(&set, &k->sa.sa_mask); |
2043 |
/* SA_NODEFER indicates that the current signal should not be
|
2044 |
blocked during the handler */
|
2045 |
if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
|
2046 |
sigaddset(&set, target_to_host_signal(sig)); |
2047 |
|
2048 |
/* block signals in the handler using Linux */
|
2049 |
sigprocmask(SIG_BLOCK, &set, &old_set); |
2050 |
/* save the previous blocked signal state to restore it at the
|
2051 |
end of the signal execution (see do_sigreturn) */
|
2052 |
host_to_target_sigset_internal(&target_old_set, &old_set); |
2053 |
|
2054 |
/* if the CPU is in VM86 mode, we restore the 32 bit values */
|
2055 |
#if defined(TARGET_I386) && !defined(TARGET_X86_64)
|
2056 |
{ |
2057 |
CPUX86State *env = cpu_env; |
2058 |
if (env->eflags & VM_MASK)
|
2059 |
save_v86_state(env); |
2060 |
} |
2061 |
#endif
|
2062 |
/* prepare the stack frame of the virtual CPU */
|
2063 |
if (k->sa.sa_flags & TARGET_SA_SIGINFO)
|
2064 |
setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env); |
2065 |
else
|
2066 |
setup_frame(sig, k, &target_old_set, cpu_env); |
2067 |
if (k->sa.sa_flags & TARGET_SA_RESETHAND)
|
2068 |
k->sa._sa_handler = TARGET_SIG_DFL; |
2069 |
} |
2070 |
if (q != &k->info)
|
2071 |
free_sigqueue(q); |
2072 |
} |
2073 |
|
2074 |
|