blob: 8c757dd4a1fcad4461fc09ab79b1acac4dbd21ec [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard66fb9762003-03-23 01:06:05 +00002 * Emulation of Linux signals
bellard31e31b82003-02-18 22:55:36 +00003 *
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>
bellard66fb9762003-03-23 01:06:05 +000022#include <string.h>
bellard31e31b82003-02-18 22:55:36 +000023#include <stdarg.h>
bellard2677e102003-04-10 00:03:27 +000024#include <unistd.h>
bellard31e31b82003-02-18 22:55:36 +000025#include <signal.h>
bellard66fb9762003-03-23 01:06:05 +000026#include <errno.h>
bellard31e31b82003-02-18 22:55:36 +000027#include <sys/ucontext.h>
28
bellard0d330192003-04-29 21:10:09 +000029#ifdef __ia64__
30#undef uc_mcontext
31#undef uc_sigmask
32#undef uc_stack
33#undef uc_link
34#endif
35
bellard3ef693a2003-03-23 20:17:16 +000036#include "qemu.h"
bellard66fb9762003-03-23 01:06:05 +000037
38//#define DEBUG_SIGNAL
39
40#define MAX_SIGQUEUE_SIZE 1024
41
42struct sigqueue {
43 struct sigqueue *next;
bellard9de5e442003-03-23 16:49:39 +000044 target_siginfo_t info;
bellard66fb9762003-03-23 01:06:05 +000045};
bellard31e31b82003-02-18 22:55:36 +000046
47struct emulated_sigaction {
48 struct target_sigaction sa;
bellard66fb9762003-03-23 01:06:05 +000049 int pending; /* true if signal is pending */
50 struct sigqueue *first;
51 struct sigqueue info; /* in order to always have memory for the
52 first signal, we put it here */
bellard31e31b82003-02-18 22:55:36 +000053};
54
bellard66fb9762003-03-23 01:06:05 +000055static struct emulated_sigaction sigact_table[TARGET_NSIG];
56static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57static struct sigqueue *first_free; /* first free siginfo queue entry */
58static int signal_pending; /* non zero if a signal may be pending */
bellard31e31b82003-02-18 22:55:36 +000059
bellard66fb9762003-03-23 01:06:05 +000060static void host_signal_handler(int host_signum, siginfo_t *info,
61 void *puc);
62
63/* XXX: do it properly */
bellard31e31b82003-02-18 22:55:36 +000064static inline int host_to_target_signal(int sig)
65{
66 return sig;
67}
68
69static inline int target_to_host_signal(int sig)
70{
71 return sig;
72}
73
bellard66fb9762003-03-23 01:06:05 +000074void host_to_target_sigset(target_sigset_t *d, sigset_t *s)
75{
76 int i;
77 for(i = 0;i < TARGET_NSIG_WORDS; i++) {
78 d->sig[i] = tswapl(((unsigned long *)s)[i]);
79 }
80}
81
82void target_to_host_sigset(sigset_t *d, target_sigset_t *s)
83{
84 int i;
85 for(i = 0;i < TARGET_NSIG_WORDS; i++) {
86 ((unsigned long *)d)[i] = tswapl(s->sig[i]);
87 }
88}
89
90void host_to_target_old_sigset(target_ulong *old_sigset,
91 const sigset_t *sigset)
92{
93 *old_sigset = tswap32(*(unsigned long *)sigset & 0xffffffff);
94}
95
96void target_to_host_old_sigset(sigset_t *sigset,
97 const target_ulong *old_sigset)
98{
99 sigemptyset(sigset);
100 *(unsigned long *)sigset = tswapl(*old_sigset);
101}
102
bellard9de5e442003-03-23 16:49:39 +0000103/* siginfo conversion */
104
105static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
106 const siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000107{
bellard9de5e442003-03-23 16:49:39 +0000108 int sig;
109 sig = host_to_target_signal(info->si_signo);
110 tinfo->si_signo = sig;
111 tinfo->si_errno = 0;
112 tinfo->si_code = 0;
bellard447db212003-05-10 15:10:36 +0000113 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
114 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000115 /* should never come here, but who knows. The information for
116 the target is irrelevant */
117 tinfo->_sifields._sigfault._addr = 0;
118 } else if (sig >= TARGET_SIGRTMIN) {
119 tinfo->_sifields._rt._pid = info->si_pid;
120 tinfo->_sifields._rt._uid = info->si_uid;
121 /* XXX: potential problem if 64 bit */
122 tinfo->_sifields._rt._sigval.sival_ptr =
123 (target_ulong)info->si_value.sival_ptr;
124 }
bellard66fb9762003-03-23 01:06:05 +0000125}
126
bellard9de5e442003-03-23 16:49:39 +0000127static void tswap_siginfo(target_siginfo_t *tinfo,
128 const target_siginfo_t *info)
129{
130 int sig;
131 sig = info->si_signo;
132 tinfo->si_signo = tswap32(sig);
133 tinfo->si_errno = tswap32(info->si_errno);
134 tinfo->si_code = tswap32(info->si_code);
bellard447db212003-05-10 15:10:36 +0000135 if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
136 sig == SIGBUS || sig == SIGTRAP) {
bellard9de5e442003-03-23 16:49:39 +0000137 tinfo->_sifields._sigfault._addr =
138 tswapl(info->_sifields._sigfault._addr);
139 } else if (sig >= TARGET_SIGRTMIN) {
140 tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
141 tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
142 tinfo->_sifields._rt._sigval.sival_ptr =
143 tswapl(info->_sifields._rt._sigval.sival_ptr);
144 }
145}
146
147
148void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
149{
150 host_to_target_siginfo_noswap(tinfo, info);
151 tswap_siginfo(tinfo, tinfo);
152}
153
154/* XXX: we support only POSIX RT signals are used. */
155/* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
156void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
bellard66fb9762003-03-23 01:06:05 +0000157{
158 info->si_signo = tswap32(tinfo->si_signo);
159 info->si_errno = tswap32(tinfo->si_errno);
160 info->si_code = tswap32(tinfo->si_code);
bellard9de5e442003-03-23 16:49:39 +0000161 info->si_pid = tswap32(tinfo->_sifields._rt._pid);
162 info->si_uid = tswap32(tinfo->_sifields._rt._uid);
163 info->si_value.sival_ptr =
164 (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
bellard66fb9762003-03-23 01:06:05 +0000165}
166
bellard31e31b82003-02-18 22:55:36 +0000167void signal_init(void)
168{
169 struct sigaction act;
170 int i;
171
bellard9de5e442003-03-23 16:49:39 +0000172 /* set all host signal handlers. ALL signals are blocked during
173 the handlers to serialize them. */
174 sigfillset(&act.sa_mask);
bellard31e31b82003-02-18 22:55:36 +0000175 act.sa_flags = SA_SIGINFO;
176 act.sa_sigaction = host_signal_handler;
177 for(i = 1; i < NSIG; i++) {
bellardc9087c22003-05-27 23:25:41 +0000178 sigaction(i, &act, NULL);
bellard31e31b82003-02-18 22:55:36 +0000179 }
180
181 memset(sigact_table, 0, sizeof(sigact_table));
bellard66fb9762003-03-23 01:06:05 +0000182
183 first_free = &sigqueue_table[0];
184 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
185 sigqueue_table[i].next = &sigqueue_table[i + 1];
186 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
bellard31e31b82003-02-18 22:55:36 +0000187}
188
bellard66fb9762003-03-23 01:06:05 +0000189/* signal queue handling */
190
191static inline struct sigqueue *alloc_sigqueue(void)
192{
193 struct sigqueue *q = first_free;
194 if (!q)
195 return NULL;
196 first_free = q->next;
197 return q;
198}
199
200static inline void free_sigqueue(struct sigqueue *q)
201{
202 q->next = first_free;
203 first_free = q;
204}
205
bellard9de5e442003-03-23 16:49:39 +0000206/* abort execution with signal */
207void __attribute((noreturn)) force_sig(int sig)
bellard66fb9762003-03-23 01:06:05 +0000208{
209 int host_sig;
bellard66fb9762003-03-23 01:06:05 +0000210 host_sig = target_to_host_signal(sig);
bellardbc8a22c2003-03-30 21:02:40 +0000211 fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
bellard66fb9762003-03-23 01:06:05 +0000212 sig, strsignal(host_sig));
bellard9de5e442003-03-23 16:49:39 +0000213#if 1
bellard66fb9762003-03-23 01:06:05 +0000214 _exit(-host_sig);
bellard9de5e442003-03-23 16:49:39 +0000215#else
216 {
217 struct sigaction act;
218 sigemptyset(&act.sa_mask);
219 act.sa_flags = SA_SIGINFO;
220 act.sa_sigaction = SIG_DFL;
221 sigaction(SIGABRT, &act, NULL);
222 abort();
223 }
224#endif
bellard66fb9762003-03-23 01:06:05 +0000225}
226
bellard9de5e442003-03-23 16:49:39 +0000227/* queue a signal so that it will be send to the virtual CPU as soon
228 as possible */
229int queue_signal(int sig, target_siginfo_t *info)
bellard31e31b82003-02-18 22:55:36 +0000230{
bellard66fb9762003-03-23 01:06:05 +0000231 struct emulated_sigaction *k;
bellard9de5e442003-03-23 16:49:39 +0000232 struct sigqueue *q, **pq;
bellard66fb9762003-03-23 01:06:05 +0000233 target_ulong handler;
234
bellard9de5e442003-03-23 16:49:39 +0000235#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000236 fprintf(stderr, "queue_signal: sig=%d\n",
bellard9de5e442003-03-23 16:49:39 +0000237 sig);
bellard66fb9762003-03-23 01:06:05 +0000238#endif
bellard9de5e442003-03-23 16:49:39 +0000239 k = &sigact_table[sig - 1];
bellard66fb9762003-03-23 01:06:05 +0000240 handler = k->sa._sa_handler;
241 if (handler == TARGET_SIG_DFL) {
242 /* default handler : ignore some signal. The other are fatal */
243 if (sig != TARGET_SIGCHLD &&
244 sig != TARGET_SIGURG &&
245 sig != TARGET_SIGWINCH) {
246 force_sig(sig);
bellard9de5e442003-03-23 16:49:39 +0000247 } else {
248 return 0; /* indicate ignored */
bellard66fb9762003-03-23 01:06:05 +0000249 }
250 } else if (handler == TARGET_SIG_IGN) {
251 /* ignore signal */
bellard9de5e442003-03-23 16:49:39 +0000252 return 0;
bellard66fb9762003-03-23 01:06:05 +0000253 } else if (handler == TARGET_SIG_ERR) {
254 force_sig(sig);
255 } else {
bellard9de5e442003-03-23 16:49:39 +0000256 pq = &k->first;
257 if (sig < TARGET_SIGRTMIN) {
258 /* if non real time signal, we queue exactly one signal */
259 if (!k->pending)
260 q = &k->info;
261 else
262 return 0;
263 } else {
264 if (!k->pending) {
265 /* first signal */
266 q = &k->info;
267 } else {
268 q = alloc_sigqueue();
269 if (!q)
270 return -EAGAIN;
271 while (*pq != NULL)
272 pq = &(*pq)->next;
273 }
274 }
275 *pq = q;
276 q->info = *info;
277 q->next = NULL;
278 k->pending = 1;
279 /* signal that a new signal is pending */
280 signal_pending = 1;
281 return 1; /* indicates that the signal was queued */
282 }
283}
284
285#if defined(DEBUG_SIGNAL)
286#ifdef __i386__
287static void dump_regs(struct ucontext *uc)
288{
289 fprintf(stderr,
290 "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
291 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
292 "EFL=%08x EIP=%08x\n",
293 uc->uc_mcontext.gregs[EAX],
294 uc->uc_mcontext.gregs[EBX],
295 uc->uc_mcontext.gregs[ECX],
296 uc->uc_mcontext.gregs[EDX],
297 uc->uc_mcontext.gregs[ESI],
298 uc->uc_mcontext.gregs[EDI],
299 uc->uc_mcontext.gregs[EBP],
300 uc->uc_mcontext.gregs[ESP],
301 uc->uc_mcontext.gregs[EFL],
302 uc->uc_mcontext.gregs[EIP]);
303}
304#else
305static void dump_regs(struct ucontext *uc)
306{
307}
308#endif
309
310#endif
311
312static void host_signal_handler(int host_signum, siginfo_t *info,
313 void *puc)
314{
315 int sig;
316 target_siginfo_t tinfo;
317
318 /* the CPU emulator uses some host signals to detect exceptions,
319 we we forward to it some signals */
320 if (host_signum == SIGSEGV || host_signum == SIGBUS) {
bellardb346ff42003-06-15 20:05:50 +0000321 if (cpu_signal_handler(host_signum, info, puc))
bellard9de5e442003-03-23 16:49:39 +0000322 return;
323 }
324
325 /* get target signal number */
326 sig = host_to_target_signal(host_signum);
327 if (sig < 1 || sig > TARGET_NSIG)
328 return;
329#if defined(DEBUG_SIGNAL)
bellardbc8a22c2003-03-30 21:02:40 +0000330 fprintf(stderr, "qemu: got signal %d\n", sig);
bellard9de5e442003-03-23 16:49:39 +0000331 dump_regs(puc);
332#endif
333 host_to_target_siginfo_noswap(&tinfo, info);
334 if (queue_signal(sig, &tinfo) == 1) {
335 /* interrupt the virtual CPU as soon as possible */
bellardb346ff42003-06-15 20:05:50 +0000336 cpu_interrupt(global_env);
bellard66fb9762003-03-23 01:06:05 +0000337 }
bellard31e31b82003-02-18 22:55:36 +0000338}
339
bellard66fb9762003-03-23 01:06:05 +0000340int do_sigaction(int sig, const struct target_sigaction *act,
341 struct target_sigaction *oact)
bellard31e31b82003-02-18 22:55:36 +0000342{
bellard66fb9762003-03-23 01:06:05 +0000343 struct emulated_sigaction *k;
bellard31e31b82003-02-18 22:55:36 +0000344
bellard66fb9762003-03-23 01:06:05 +0000345 if (sig < 1 || sig > TARGET_NSIG)
346 return -EINVAL;
347 k = &sigact_table[sig - 1];
348#if defined(DEBUG_SIGNAL) && 0
349 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
350 sig, (int)act, (int)oact);
351#endif
352 if (oact) {
353 oact->_sa_handler = tswapl(k->sa._sa_handler);
354 oact->sa_flags = tswapl(k->sa.sa_flags);
355 oact->sa_restorer = tswapl(k->sa.sa_restorer);
356 oact->sa_mask = k->sa.sa_mask;
357 }
358 if (act) {
359 k->sa._sa_handler = tswapl(act->_sa_handler);
360 k->sa.sa_flags = tswapl(act->sa_flags);
361 k->sa.sa_restorer = tswapl(act->sa_restorer);
362 k->sa.sa_mask = act->sa_mask;
363 }
364 return 0;
365}
bellard31e31b82003-02-18 22:55:36 +0000366
bellard66fb9762003-03-23 01:06:05 +0000367#ifdef TARGET_I386
368
369/* from the Linux kernel */
370
371struct target_fpreg {
372 uint16_t significand[4];
373 uint16_t exponent;
374};
375
376struct target_fpxreg {
377 uint16_t significand[4];
378 uint16_t exponent;
379 uint16_t padding[3];
380};
381
382struct target_xmmreg {
383 target_ulong element[4];
384};
385
386struct target_fpstate {
387 /* Regular FPU environment */
388 target_ulong cw;
389 target_ulong sw;
390 target_ulong tag;
391 target_ulong ipoff;
392 target_ulong cssel;
393 target_ulong dataoff;
394 target_ulong datasel;
395 struct target_fpreg _st[8];
396 uint16_t status;
397 uint16_t magic; /* 0xffff = regular FPU data only */
398
399 /* FXSR FPU environment */
400 target_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
401 target_ulong mxcsr;
402 target_ulong reserved;
403 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
404 struct target_xmmreg _xmm[8];
405 target_ulong padding[56];
406};
407
408#define X86_FXSR_MAGIC 0x0000
409
410struct target_sigcontext {
411 uint16_t gs, __gsh;
412 uint16_t fs, __fsh;
413 uint16_t es, __esh;
414 uint16_t ds, __dsh;
415 target_ulong edi;
416 target_ulong esi;
417 target_ulong ebp;
418 target_ulong esp;
419 target_ulong ebx;
420 target_ulong edx;
421 target_ulong ecx;
422 target_ulong eax;
423 target_ulong trapno;
424 target_ulong err;
425 target_ulong eip;
426 uint16_t cs, __csh;
427 target_ulong eflags;
428 target_ulong esp_at_signal;
429 uint16_t ss, __ssh;
430 target_ulong fpstate; /* pointer */
431 target_ulong oldmask;
432 target_ulong cr2;
433};
434
435typedef struct target_sigaltstack {
436 target_ulong ss_sp;
437 int ss_flags;
438 target_ulong ss_size;
439} target_stack_t;
440
441struct target_ucontext {
442 target_ulong uc_flags;
443 target_ulong uc_link;
444 target_stack_t uc_stack;
445 struct target_sigcontext uc_mcontext;
446 target_sigset_t uc_sigmask; /* mask last for extensibility */
447};
448
449struct sigframe
450{
451 target_ulong pretcode;
452 int sig;
453 struct target_sigcontext sc;
454 struct target_fpstate fpstate;
455 target_ulong extramask[TARGET_NSIG_WORDS-1];
456 char retcode[8];
457};
458
459struct rt_sigframe
460{
461 target_ulong pretcode;
462 int sig;
463 target_ulong pinfo;
464 target_ulong puc;
465 struct target_siginfo info;
466 struct target_ucontext uc;
467 struct target_fpstate fpstate;
468 char retcode[8];
469};
470
471/*
472 * Set up a signal frame.
473 */
474
475#define __put_user(x,ptr)\
476({\
477 int size = sizeof(*ptr);\
478 switch(size) {\
479 case 1:\
480 stb(ptr, (typeof(*ptr))(x));\
481 break;\
482 case 2:\
483 stw(ptr, (typeof(*ptr))(x));\
484 break;\
485 case 4:\
486 stl(ptr, (typeof(*ptr))(x));\
487 break;\
488 case 8:\
489 stq(ptr, (typeof(*ptr))(x));\
490 break;\
491 default:\
492 abort();\
493 }\
494 0;\
495})
496
497#define get_user(val, ptr) (typeof(*ptr))(*(ptr))
498
499
500#define __copy_to_user(dst, src, size)\
501({\
502 memcpy(dst, src, size);\
503 0;\
504})
505
bellard9de5e442003-03-23 16:49:39 +0000506static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
507 const target_siginfo_t *info)
bellard66fb9762003-03-23 01:06:05 +0000508{
bellard9de5e442003-03-23 16:49:39 +0000509 tswap_siginfo(tinfo, info);
bellard66fb9762003-03-23 01:06:05 +0000510 return 0;
511}
512
513/* XXX: save x87 state */
514static int
515setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
516 CPUX86State *env, unsigned long mask)
517{
518 int err = 0;
519
bellarda52c7572003-06-21 13:14:12 +0000520 err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
521 err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
522 err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
523 err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
bellard66fb9762003-03-23 01:06:05 +0000524 err |= __put_user(env->regs[R_EDI], &sc->edi);
525 err |= __put_user(env->regs[R_ESI], &sc->esi);
526 err |= __put_user(env->regs[R_EBP], &sc->ebp);
527 err |= __put_user(env->regs[R_ESP], &sc->esp);
528 err |= __put_user(env->regs[R_EBX], &sc->ebx);
529 err |= __put_user(env->regs[R_EDX], &sc->edx);
530 err |= __put_user(env->regs[R_ECX], &sc->ecx);
531 err |= __put_user(env->regs[R_EAX], &sc->eax);
bellard66099dd2003-05-08 15:34:02 +0000532 err |= __put_user(env->exception_index, &sc->trapno);
533 err |= __put_user(env->error_code, &sc->err);
bellard66fb9762003-03-23 01:06:05 +0000534 err |= __put_user(env->eip, &sc->eip);
bellarda52c7572003-06-21 13:14:12 +0000535 err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
bellard66fb9762003-03-23 01:06:05 +0000536 err |= __put_user(env->eflags, &sc->eflags);
537 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
bellarda52c7572003-06-21 13:14:12 +0000538 err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
bellarded2dcdf2003-05-29 20:06:27 +0000539
540 cpu_x86_fsave(env, (void *)fpstate, 1);
541 fpstate->status = fpstate->sw;
542 err |= __put_user(0xffff, &fpstate->magic);
543 err |= __put_user(fpstate, &sc->fpstate);
544
bellard66fb9762003-03-23 01:06:05 +0000545 /* non-iBCS2 extensions.. */
546 err |= __put_user(mask, &sc->oldmask);
bellarda52c7572003-06-21 13:14:12 +0000547 err |= __put_user(env->cr[2], &sc->cr2);
bellard66fb9762003-03-23 01:06:05 +0000548 return err;
549}
550
551/*
552 * Determine which stack to use..
553 */
554
555static inline void *
556get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
557{
558 unsigned long esp;
559
560 /* Default to using normal stack */
561 esp = env->regs[R_ESP];
562#if 0
563 /* This is the X/Open sanctioned signal stack switching. */
564 if (ka->sa.sa_flags & SA_ONSTACK) {
565 if (sas_ss_flags(esp) == 0)
566 esp = current->sas_ss_sp + current->sas_ss_size;
567 }
568
569 /* This is the legacy signal stack switching. */
bellarda52c7572003-06-21 13:14:12 +0000570 else
bellard66fb9762003-03-23 01:06:05 +0000571#endif
bellarda52c7572003-06-21 13:14:12 +0000572 if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
573 !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
574 ka->sa.sa_restorer) {
575 esp = (unsigned long) ka->sa.sa_restorer;
576 }
577 return (void *)((esp - frame_size) & -8ul);
bellard66fb9762003-03-23 01:06:05 +0000578}
579
bellard66fb9762003-03-23 01:06:05 +0000580static void setup_frame(int sig, struct emulated_sigaction *ka,
581 target_sigset_t *set, CPUX86State *env)
582{
583 struct sigframe *frame;
584 int err = 0;
585
586 frame = get_sigframe(ka, env, sizeof(*frame));
587
588#if 0
589 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
590 goto give_sigsegv;
591#endif
592 err |= __put_user((/*current->exec_domain
593 && current->exec_domain->signal_invmap
594 && sig < 32
595 ? current->exec_domain->signal_invmap[sig]
596 : */ sig),
597 &frame->sig);
598 if (err)
599 goto give_sigsegv;
600
601 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
602 if (err)
603 goto give_sigsegv;
604
605 if (TARGET_NSIG_WORDS > 1) {
606 err |= __copy_to_user(frame->extramask, &set->sig[1],
607 sizeof(frame->extramask));
608 }
609 if (err)
610 goto give_sigsegv;
611
612 /* Set up to return from userspace. If provided, use a stub
613 already in userspace. */
614 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
615 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
616 } else {
617 err |= __put_user(frame->retcode, &frame->pretcode);
618 /* This is popl %eax ; movl $,%eax ; int $0x80 */
619 err |= __put_user(0xb858, (short *)(frame->retcode+0));
620 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
621 err |= __put_user(0x80cd, (short *)(frame->retcode+6));
622 }
623
624 if (err)
625 goto give_sigsegv;
626
627 /* Set up registers for signal handler */
628 env->regs[R_ESP] = (unsigned long) frame;
629 env->eip = (unsigned long) ka->sa._sa_handler;
630
631 cpu_x86_load_seg(env, R_DS, __USER_DS);
632 cpu_x86_load_seg(env, R_ES, __USER_DS);
633 cpu_x86_load_seg(env, R_SS, __USER_DS);
634 cpu_x86_load_seg(env, R_CS, __USER_CS);
635 env->eflags &= ~TF_MASK;
636
637 return;
638
639give_sigsegv:
640 if (sig == TARGET_SIGSEGV)
641 ka->sa._sa_handler = TARGET_SIG_DFL;
642 force_sig(TARGET_SIGSEGV /* , current */);
643}
644
bellard9de5e442003-03-23 16:49:39 +0000645static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
646 target_siginfo_t *info,
bellard66fb9762003-03-23 01:06:05 +0000647 target_sigset_t *set, CPUX86State *env)
648{
649 struct rt_sigframe *frame;
650 int err = 0;
651
652 frame = get_sigframe(ka, env, sizeof(*frame));
653
654#if 0
655 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
656 goto give_sigsegv;
657#endif
658
659 err |= __put_user((/*current->exec_domain
660 && current->exec_domain->signal_invmap
661 && sig < 32
662 ? current->exec_domain->signal_invmap[sig]
663 : */sig),
664 &frame->sig);
665 err |= __put_user((target_ulong)&frame->info, &frame->pinfo);
666 err |= __put_user((target_ulong)&frame->uc, &frame->puc);
667 err |= copy_siginfo_to_user(&frame->info, info);
668 if (err)
669 goto give_sigsegv;
670
671 /* Create the ucontext. */
672 err |= __put_user(0, &frame->uc.uc_flags);
673 err |= __put_user(0, &frame->uc.uc_link);
674 err |= __put_user(/*current->sas_ss_sp*/ 0, &frame->uc.uc_stack.ss_sp);
675 err |= __put_user(/* sas_ss_flags(regs->esp) */ 0,
676 &frame->uc.uc_stack.ss_flags);
677 err |= __put_user(/* current->sas_ss_size */ 0, &frame->uc.uc_stack.ss_size);
678 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
679 env, set->sig[0]);
680 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
681 if (err)
682 goto give_sigsegv;
683
684 /* Set up to return from userspace. If provided, use a stub
685 already in userspace. */
686 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
687 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
688 } else {
689 err |= __put_user(frame->retcode, &frame->pretcode);
690 /* This is movl $,%eax ; int $0x80 */
691 err |= __put_user(0xb8, (char *)(frame->retcode+0));
692 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
693 err |= __put_user(0x80cd, (short *)(frame->retcode+5));
694 }
695
696 if (err)
697 goto give_sigsegv;
698
699 /* Set up registers for signal handler */
700 env->regs[R_ESP] = (unsigned long) frame;
701 env->eip = (unsigned long) ka->sa._sa_handler;
702
703 cpu_x86_load_seg(env, R_DS, __USER_DS);
704 cpu_x86_load_seg(env, R_ES, __USER_DS);
705 cpu_x86_load_seg(env, R_SS, __USER_DS);
706 cpu_x86_load_seg(env, R_CS, __USER_CS);
707 env->eflags &= ~TF_MASK;
708
709 return;
710
711give_sigsegv:
712 if (sig == TARGET_SIGSEGV)
713 ka->sa._sa_handler = TARGET_SIG_DFL;
714 force_sig(TARGET_SIGSEGV /* , current */);
715}
716
717static int
718restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
719{
720 unsigned int err = 0;
721
bellard66fb9762003-03-23 01:06:05 +0000722 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
723 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
724 cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
725 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
726
727 env->regs[R_EDI] = ldl(&sc->edi);
728 env->regs[R_ESI] = ldl(&sc->esi);
729 env->regs[R_EBP] = ldl(&sc->ebp);
730 env->regs[R_ESP] = ldl(&sc->esp);
731 env->regs[R_EBX] = ldl(&sc->ebx);
732 env->regs[R_EDX] = ldl(&sc->edx);
733 env->regs[R_ECX] = ldl(&sc->ecx);
734 env->eip = ldl(&sc->eip);
735
736 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
737 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
738
739 {
740 unsigned int tmpflags;
741 tmpflags = ldl(&sc->eflags);
742 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
743 // regs->orig_eax = -1; /* disable syscall checks */
744 }
745
bellard66fb9762003-03-23 01:06:05 +0000746 {
747 struct _fpstate * buf;
bellarded2dcdf2003-05-29 20:06:27 +0000748 buf = (void *)ldl(&sc->fpstate);
bellard66fb9762003-03-23 01:06:05 +0000749 if (buf) {
bellarded2dcdf2003-05-29 20:06:27 +0000750#if 0
bellard66fb9762003-03-23 01:06:05 +0000751 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
752 goto badframe;
bellarded2dcdf2003-05-29 20:06:27 +0000753#endif
754 cpu_x86_frstor(env, (void *)buf, 1);
bellard66fb9762003-03-23 01:06:05 +0000755 }
756 }
bellarded2dcdf2003-05-29 20:06:27 +0000757
bellard66fb9762003-03-23 01:06:05 +0000758 *peax = ldl(&sc->eax);
759 return err;
760#if 0
761badframe:
762 return 1;
763#endif
764}
765
766long do_sigreturn(CPUX86State *env)
767{
768 struct sigframe *frame = (struct sigframe *)(env->regs[R_ESP] - 8);
769 target_sigset_t target_set;
770 sigset_t set;
771 int eax, i;
772
bellard447db212003-05-10 15:10:36 +0000773#if defined(DEBUG_SIGNAL)
774 fprintf(stderr, "do_sigreturn\n");
775#endif
bellard66fb9762003-03-23 01:06:05 +0000776 /* set blocked signals */
777 target_set.sig[0] = frame->sc.oldmask;
778 for(i = 1; i < TARGET_NSIG_WORDS; i++)
779 target_set.sig[i] = frame->extramask[i - 1];
780
781 target_to_host_sigset(&set, &target_set);
782 sigprocmask(SIG_SETMASK, &set, NULL);
783
784 /* restore registers */
785 if (restore_sigcontext(env, &frame->sc, &eax))
786 goto badframe;
787 return eax;
788
789badframe:
790 force_sig(TARGET_SIGSEGV);
791 return 0;
792}
793
794long do_rt_sigreturn(CPUX86State *env)
795{
796 struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4);
797 target_sigset_t target_set;
798 sigset_t set;
799 // stack_t st;
800 int eax;
801
802#if 0
803 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
804 goto badframe;
805#endif
806 memcpy(&target_set, &frame->uc.uc_sigmask, sizeof(target_sigset_t));
807
808 target_to_host_sigset(&set, &target_set);
809 sigprocmask(SIG_SETMASK, &set, NULL);
810
811 if (restore_sigcontext(env, &frame->uc.uc_mcontext, &eax))
812 goto badframe;
813
814#if 0
815 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
816 goto badframe;
817 /* It is more difficult to avoid calling this function than to
818 call it and ignore errors. */
819 do_sigaltstack(&st, NULL, regs->esp);
820#endif
821 return eax;
822
823badframe:
824 force_sig(TARGET_SIGSEGV);
825 return 0;
826}
827
bellardb346ff42003-06-15 20:05:50 +0000828#else
829
830static void setup_frame(int sig, struct emulated_sigaction *ka,
831 target_sigset_t *set, CPUState *env)
832{
833 fprintf(stderr, "setup_frame: not implemented\n");
834}
835
836static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
837 target_siginfo_t *info,
838 target_sigset_t *set, CPUState *env)
839{
840 fprintf(stderr, "setup_rt_frame: not implemented\n");
841}
842
843long do_sigreturn(CPUState *env)
844{
845 fprintf(stderr, "do_sigreturn: not implemented\n");
846 return -ENOSYS;
847}
848
849long do_rt_sigreturn(CPUState *env)
850{
851 fprintf(stderr, "do_rt_sigreturn: not implemented\n");
852 return -ENOSYS;
853}
854
bellard66fb9762003-03-23 01:06:05 +0000855#endif
856
857void process_pending_signals(void *cpu_env)
858{
859 int sig;
860 target_ulong handler;
bellard9de5e442003-03-23 16:49:39 +0000861 sigset_t set, old_set;
862 target_sigset_t target_old_set;
bellard66fb9762003-03-23 01:06:05 +0000863 struct emulated_sigaction *k;
864 struct sigqueue *q;
865
bellard31e31b82003-02-18 22:55:36 +0000866 if (!signal_pending)
867 return;
868
bellard66fb9762003-03-23 01:06:05 +0000869 k = sigact_table;
870 for(sig = 1; sig <= TARGET_NSIG; sig++) {
871 if (k->pending)
bellard31e31b82003-02-18 22:55:36 +0000872 goto handle_signal;
bellard66fb9762003-03-23 01:06:05 +0000873 k++;
bellard31e31b82003-02-18 22:55:36 +0000874 }
875 /* if no signal is pending, just return */
876 signal_pending = 0;
877 return;
bellard66fb9762003-03-23 01:06:05 +0000878
bellard31e31b82003-02-18 22:55:36 +0000879 handle_signal:
bellard66fb9762003-03-23 01:06:05 +0000880#ifdef DEBUG_SIGNAL
bellardbc8a22c2003-03-30 21:02:40 +0000881 fprintf(stderr, "qemu: process signal %d\n", sig);
bellard66fb9762003-03-23 01:06:05 +0000882#endif
883 /* dequeue signal */
884 q = k->first;
885 k->first = q->next;
886 if (!k->first)
887 k->pending = 0;
888
889 handler = k->sa._sa_handler;
890 if (handler == TARGET_SIG_DFL) {
891 /* default handler : ignore some signal. The other are fatal */
892 if (sig != TARGET_SIGCHLD &&
893 sig != TARGET_SIGURG &&
894 sig != TARGET_SIGWINCH) {
895 force_sig(sig);
896 }
897 } else if (handler == TARGET_SIG_IGN) {
898 /* ignore sig */
899 } else if (handler == TARGET_SIG_ERR) {
900 force_sig(sig);
901 } else {
bellard9de5e442003-03-23 16:49:39 +0000902 /* compute the blocked signals during the handler execution */
903 target_to_host_sigset(&set, &k->sa.sa_mask);
904 /* SA_NODEFER indicates that the current signal should not be
905 blocked during the handler */
906 if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
907 sigaddset(&set, target_to_host_signal(sig));
908
909 /* block signals in the handler using Linux */
910 sigprocmask(SIG_BLOCK, &set, &old_set);
911 /* save the previous blocked signal state to restore it at the
912 end of the signal execution (see do_sigreturn) */
913 host_to_target_sigset(&target_old_set, &old_set);
914
bellardbc8a22c2003-03-30 21:02:40 +0000915 /* if the CPU is in VM86 mode, we restore the 32 bit values */
916#ifdef TARGET_I386
917 {
918 CPUX86State *env = cpu_env;
919 if (env->eflags & VM_MASK)
920 save_v86_state(env);
921 }
922#endif
bellard9de5e442003-03-23 16:49:39 +0000923 /* prepare the stack frame of the virtual CPU */
bellard66fb9762003-03-23 01:06:05 +0000924 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
bellard9de5e442003-03-23 16:49:39 +0000925 setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +0000926 else
bellard9de5e442003-03-23 16:49:39 +0000927 setup_frame(sig, k, &target_old_set, cpu_env);
bellard66fb9762003-03-23 01:06:05 +0000928 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
929 k->sa._sa_handler = TARGET_SIG_DFL;
bellard31e31b82003-02-18 22:55:36 +0000930 }
bellard66fb9762003-03-23 01:06:05 +0000931 if (q != &k->info)
932 free_sigqueue(q);
bellard31e31b82003-02-18 22:55:36 +0000933}
bellard66fb9762003-03-23 01:06:05 +0000934
935