]> git.proxmox.com Git - qemu.git/blame - linux-user/signal.c
basic signal handling
[qemu.git] / linux-user / signal.c
CommitLineData
31e31b8a 1/*
66fb9763 2 * Emulation of Linux signals
31e31b8a
FB
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>
66fb9763 22#include <string.h>
31e31b8a
FB
23#include <stdarg.h>
24#include <signal.h>
66fb9763 25#include <errno.h>
31e31b8a
FB
26#include <sys/ucontext.h>
27
66fb9763
FB
28#include "gemu.h"
29
30#include "syscall_defs.h"
31
32#ifdef TARGET_I386
33#include "cpu-i386.h"
34#include "syscall-i386.h"
35#endif
36
37/* signal handling inspired from em86. */
38
39//#define DEBUG_SIGNAL
40
41#define MAX_SIGQUEUE_SIZE 1024
42
43struct sigqueue {
44 struct sigqueue *next;
45 siginfo_t info;
46};
31e31b8a
FB
47
48struct emulated_sigaction {
49 struct target_sigaction sa;
66fb9763
FB
50 int pending; /* true if signal is pending */
51 struct sigqueue *first;
52 struct sigqueue info; /* in order to always have memory for the
53 first signal, we put it here */
31e31b8a
FB
54};
55
66fb9763
FB
56static struct emulated_sigaction sigact_table[TARGET_NSIG];
57static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
58static struct sigqueue *first_free; /* first free siginfo queue entry */
59static int signal_pending; /* non zero if a signal may be pending */
31e31b8a 60
66fb9763
FB
61static void host_signal_handler(int host_signum, siginfo_t *info,
62 void *puc);
63
64/* XXX: do it properly */
31e31b8a
FB
65static inline int host_to_target_signal(int sig)
66{
67 return sig;
68}
69
70static inline int target_to_host_signal(int sig)
71{
72 return sig;
73}
74
66fb9763
FB
75void host_to_target_sigset(target_sigset_t *d, sigset_t *s)
76{
77 int i;
78 for(i = 0;i < TARGET_NSIG_WORDS; i++) {
79 d->sig[i] = tswapl(((unsigned long *)s)[i]);
80 }
81}
82
83void target_to_host_sigset(sigset_t *d, target_sigset_t *s)
84{
85 int i;
86 for(i = 0;i < TARGET_NSIG_WORDS; i++) {
87 ((unsigned long *)d)[i] = tswapl(s->sig[i]);
88 }
89}
90
91void host_to_target_old_sigset(target_ulong *old_sigset,
92 const sigset_t *sigset)
93{
94 *old_sigset = tswap32(*(unsigned long *)sigset & 0xffffffff);
95}
96
97void target_to_host_old_sigset(sigset_t *sigset,
98 const target_ulong *old_sigset)
99{
100 sigemptyset(sigset);
101 *(unsigned long *)sigset = tswapl(*old_sigset);
102}
103
104/* XXX: finish it */
105void host_to_target_siginfo(target_siginfo_t *tinfo, siginfo_t *info)
106{
107 tinfo->si_signo = tswap32(info->si_signo);
108 tinfo->si_errno = tswap32(info->si_errno);
109 tinfo->si_code = tswap32(info->si_code);
110}
111
112/* XXX: finish it */
113void target_to_host_siginfo(siginfo_t *info, target_siginfo_t *tinfo)
114{
115 info->si_signo = tswap32(tinfo->si_signo);
116 info->si_errno = tswap32(tinfo->si_errno);
117 info->si_code = tswap32(tinfo->si_code);
118}
119
31e31b8a
FB
120void signal_init(void)
121{
122 struct sigaction act;
123 int i;
124
125 /* set all host signal handlers */
126 sigemptyset(&act.sa_mask);
127 act.sa_flags = SA_SIGINFO;
128 act.sa_sigaction = host_signal_handler;
129 for(i = 1; i < NSIG; i++) {
66fb9763 130 sigaction(i, &act, NULL);
31e31b8a
FB
131 }
132
133 memset(sigact_table, 0, sizeof(sigact_table));
66fb9763
FB
134
135 first_free = &sigqueue_table[0];
136 for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
137 sigqueue_table[i].next = &sigqueue_table[i + 1];
138 sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
139}
140
141/* signal queue handling */
142
143static inline struct sigqueue *alloc_sigqueue(void)
144{
145 struct sigqueue *q = first_free;
146 if (!q)
147 return NULL;
148 first_free = q->next;
149 return q;
31e31b8a
FB
150}
151
66fb9763
FB
152static inline void free_sigqueue(struct sigqueue *q)
153{
154 q->next = first_free;
155 first_free = q;
156}
157
158static int queue_signal(struct emulated_sigaction *k, int sig, siginfo_t *info)
159{
160 struct sigqueue *q, **pq;
161
162 pq = &k->first;
163 if (!k->pending || sig < TARGET_SIGRTMIN) {
164 /* first signal or non real time signal */
165 q = &k->info;
166 } else {
167 q = alloc_sigqueue();
168 if (!q)
169 return -EAGAIN;
170 while (*pq != NULL)
171 pq = &(*pq)->next;
172 }
173 *pq = q;
174 q->info = *info;
175 q->next = NULL;
176 k->pending = 1;
177 /* signal that a new signal is pending */
178 signal_pending = 1;
179 return 0;
180}
181
182void force_sig(int sig)
183{
184 int host_sig;
185 /* abort execution with signal */
186 host_sig = target_to_host_signal(sig);
187 fprintf(stderr, "gemu: uncaught target signal %d (%s) - exiting\n",
188 sig, strsignal(host_sig));
189 _exit(-host_sig);
190}
191
192
31e31b8a
FB
193static void host_signal_handler(int host_signum, siginfo_t *info,
194 void *puc)
195{
66fb9763
FB
196 struct emulated_sigaction *k;
197 int sig;
198 target_ulong handler;
199
31e31b8a 200 /* get target signal number */
66fb9763
FB
201 sig = host_to_target_signal(host_signum);
202 if (sig < 1 || sig > TARGET_NSIG)
31e31b8a 203 return;
66fb9763
FB
204 k = &sigact_table[sig - 1];
205#ifdef DEBUG_SIGNAL
206 fprintf(stderr, "gemu: got signal %d\n", sig);
207#endif
208 handler = k->sa._sa_handler;
209 if (handler == TARGET_SIG_DFL) {
210 /* default handler : ignore some signal. The other are fatal */
211 if (sig != TARGET_SIGCHLD &&
212 sig != TARGET_SIGURG &&
213 sig != TARGET_SIGWINCH) {
214 force_sig(sig);
215 }
216 } else if (handler == TARGET_SIG_IGN) {
217 /* ignore signal */
218 } else if (handler == TARGET_SIG_ERR) {
219 force_sig(sig);
220 } else {
221 queue_signal(k, sig, info);
222 }
223}
224
225int do_sigaction(int sig, const struct target_sigaction *act,
226 struct target_sigaction *oact)
227{
228 struct emulated_sigaction *k;
229
230 if (sig < 1 || sig > TARGET_NSIG)
231 return -EINVAL;
232 k = &sigact_table[sig - 1];
233#if defined(DEBUG_SIGNAL) && 0
234 fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
235 sig, (int)act, (int)oact);
236#endif
237 if (oact) {
238 oact->_sa_handler = tswapl(k->sa._sa_handler);
239 oact->sa_flags = tswapl(k->sa.sa_flags);
240 oact->sa_restorer = tswapl(k->sa.sa_restorer);
241 oact->sa_mask = k->sa.sa_mask;
242 }
243 if (act) {
244 k->sa._sa_handler = tswapl(act->_sa_handler);
245 k->sa.sa_flags = tswapl(act->sa_flags);
246 k->sa.sa_restorer = tswapl(act->sa_restorer);
247 k->sa.sa_mask = act->sa_mask;
248 }
249 return 0;
250}
251
252#ifdef TARGET_I386
253
254/* from the Linux kernel */
255
256struct target_fpreg {
257 uint16_t significand[4];
258 uint16_t exponent;
259};
260
261struct target_fpxreg {
262 uint16_t significand[4];
263 uint16_t exponent;
264 uint16_t padding[3];
265};
266
267struct target_xmmreg {
268 target_ulong element[4];
269};
270
271struct target_fpstate {
272 /* Regular FPU environment */
273 target_ulong cw;
274 target_ulong sw;
275 target_ulong tag;
276 target_ulong ipoff;
277 target_ulong cssel;
278 target_ulong dataoff;
279 target_ulong datasel;
280 struct target_fpreg _st[8];
281 uint16_t status;
282 uint16_t magic; /* 0xffff = regular FPU data only */
283
284 /* FXSR FPU environment */
285 target_ulong _fxsr_env[6]; /* FXSR FPU env is ignored */
286 target_ulong mxcsr;
287 target_ulong reserved;
288 struct target_fpxreg _fxsr_st[8]; /* FXSR FPU reg data is ignored */
289 struct target_xmmreg _xmm[8];
290 target_ulong padding[56];
291};
292
293#define X86_FXSR_MAGIC 0x0000
294
295struct target_sigcontext {
296 uint16_t gs, __gsh;
297 uint16_t fs, __fsh;
298 uint16_t es, __esh;
299 uint16_t ds, __dsh;
300 target_ulong edi;
301 target_ulong esi;
302 target_ulong ebp;
303 target_ulong esp;
304 target_ulong ebx;
305 target_ulong edx;
306 target_ulong ecx;
307 target_ulong eax;
308 target_ulong trapno;
309 target_ulong err;
310 target_ulong eip;
311 uint16_t cs, __csh;
312 target_ulong eflags;
313 target_ulong esp_at_signal;
314 uint16_t ss, __ssh;
315 target_ulong fpstate; /* pointer */
316 target_ulong oldmask;
317 target_ulong cr2;
318};
319
320typedef struct target_sigaltstack {
321 target_ulong ss_sp;
322 int ss_flags;
323 target_ulong ss_size;
324} target_stack_t;
325
326struct target_ucontext {
327 target_ulong uc_flags;
328 target_ulong uc_link;
329 target_stack_t uc_stack;
330 struct target_sigcontext uc_mcontext;
331 target_sigset_t uc_sigmask; /* mask last for extensibility */
332};
333
334struct sigframe
335{
336 target_ulong pretcode;
337 int sig;
338 struct target_sigcontext sc;
339 struct target_fpstate fpstate;
340 target_ulong extramask[TARGET_NSIG_WORDS-1];
341 char retcode[8];
342};
343
344struct rt_sigframe
345{
346 target_ulong pretcode;
347 int sig;
348 target_ulong pinfo;
349 target_ulong puc;
350 struct target_siginfo info;
351 struct target_ucontext uc;
352 struct target_fpstate fpstate;
353 char retcode[8];
354};
355
356/*
357 * Set up a signal frame.
358 */
359
360#define __put_user(x,ptr)\
361({\
362 int size = sizeof(*ptr);\
363 switch(size) {\
364 case 1:\
365 stb(ptr, (typeof(*ptr))(x));\
366 break;\
367 case 2:\
368 stw(ptr, (typeof(*ptr))(x));\
369 break;\
370 case 4:\
371 stl(ptr, (typeof(*ptr))(x));\
372 break;\
373 case 8:\
374 stq(ptr, (typeof(*ptr))(x));\
375 break;\
376 default:\
377 abort();\
378 }\
379 0;\
380})
381
382#define get_user(val, ptr) (typeof(*ptr))(*(ptr))
383
384
385#define __copy_to_user(dst, src, size)\
386({\
387 memcpy(dst, src, size);\
388 0;\
389})
390
391static inline int copy_siginfo_to_user(target_siginfo_t *tinfo, siginfo_t *info)
392{
393 host_to_target_siginfo(tinfo, info);
394 return 0;
395}
396
397/* XXX: save x87 state */
398static int
399setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
400 CPUX86State *env, unsigned long mask)
401{
402 int err = 0;
403
404 err |= __put_user(env->segs[R_GS], (unsigned int *)&sc->gs);
405 err |= __put_user(env->segs[R_FS], (unsigned int *)&sc->fs);
406 err |= __put_user(env->segs[R_ES], (unsigned int *)&sc->es);
407 err |= __put_user(env->segs[R_DS], (unsigned int *)&sc->ds);
408 err |= __put_user(env->regs[R_EDI], &sc->edi);
409 err |= __put_user(env->regs[R_ESI], &sc->esi);
410 err |= __put_user(env->regs[R_EBP], &sc->ebp);
411 err |= __put_user(env->regs[R_ESP], &sc->esp);
412 err |= __put_user(env->regs[R_EBX], &sc->ebx);
413 err |= __put_user(env->regs[R_EDX], &sc->edx);
414 err |= __put_user(env->regs[R_ECX], &sc->ecx);
415 err |= __put_user(env->regs[R_EAX], &sc->eax);
416 err |= __put_user(/*current->thread.trap_no*/ 0, &sc->trapno);
417 err |= __put_user(/*current->thread.error_code*/ 0, &sc->err);
418 err |= __put_user(env->eip, &sc->eip);
419 err |= __put_user(env->segs[R_CS], (unsigned int *)&sc->cs);
420 err |= __put_user(env->eflags, &sc->eflags);
421 err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
422 err |= __put_user(env->segs[R_SS], (unsigned int *)&sc->ss);
423#if 0
424 tmp = save_i387(fpstate);
425 if (tmp < 0)
426 err = 1;
427 else
428 err |= __put_user(tmp ? fpstate : NULL, &sc->fpstate);
429#else
430 err |= __put_user(0, &sc->fpstate);
431#endif
432 /* non-iBCS2 extensions.. */
433 err |= __put_user(mask, &sc->oldmask);
434 err |= __put_user(/*current->thread.cr2*/ 0, &sc->cr2);
435
436 return err;
31e31b8a
FB
437}
438
66fb9763
FB
439/*
440 * Determine which stack to use..
441 */
31e31b8a 442
66fb9763
FB
443static inline void *
444get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
31e31b8a 445{
66fb9763
FB
446 unsigned long esp;
447
448 /* Default to using normal stack */
449 esp = env->regs[R_ESP];
450#if 0
451 /* This is the X/Open sanctioned signal stack switching. */
452 if (ka->sa.sa_flags & SA_ONSTACK) {
453 if (sas_ss_flags(esp) == 0)
454 esp = current->sas_ss_sp + current->sas_ss_size;
455 }
456
457 /* This is the legacy signal stack switching. */
458 else if ((regs->xss & 0xffff) != __USER_DS &&
459 !(ka->sa.sa_flags & SA_RESTORER) &&
460 ka->sa.sa_restorer) {
461 esp = (unsigned long) ka->sa.sa_restorer;
462 }
463#endif
464 return (void *)((esp - frame_size) & -8ul);
465}
466
467#define TF_MASK TRAP_FLAG
468
469static void setup_frame(int sig, struct emulated_sigaction *ka,
470 target_sigset_t *set, CPUX86State *env)
471{
472 struct sigframe *frame;
473 int err = 0;
474
475 frame = get_sigframe(ka, env, sizeof(*frame));
476
477#if 0
478 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
479 goto give_sigsegv;
480#endif
481 err |= __put_user((/*current->exec_domain
482 && current->exec_domain->signal_invmap
483 && sig < 32
484 ? current->exec_domain->signal_invmap[sig]
485 : */ sig),
486 &frame->sig);
487 if (err)
488 goto give_sigsegv;
489
490 setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
491 if (err)
492 goto give_sigsegv;
493
494 if (TARGET_NSIG_WORDS > 1) {
495 err |= __copy_to_user(frame->extramask, &set->sig[1],
496 sizeof(frame->extramask));
497 }
498 if (err)
499 goto give_sigsegv;
500
501 /* Set up to return from userspace. If provided, use a stub
502 already in userspace. */
503 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
504 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
505 } else {
506 err |= __put_user(frame->retcode, &frame->pretcode);
507 /* This is popl %eax ; movl $,%eax ; int $0x80 */
508 err |= __put_user(0xb858, (short *)(frame->retcode+0));
509 err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
510 err |= __put_user(0x80cd, (short *)(frame->retcode+6));
511 }
512
513 if (err)
514 goto give_sigsegv;
515
516 /* Set up registers for signal handler */
517 env->regs[R_ESP] = (unsigned long) frame;
518 env->eip = (unsigned long) ka->sa._sa_handler;
519
520 cpu_x86_load_seg(env, R_DS, __USER_DS);
521 cpu_x86_load_seg(env, R_ES, __USER_DS);
522 cpu_x86_load_seg(env, R_SS, __USER_DS);
523 cpu_x86_load_seg(env, R_CS, __USER_CS);
524 env->eflags &= ~TF_MASK;
525
526 return;
527
528give_sigsegv:
529 if (sig == TARGET_SIGSEGV)
530 ka->sa._sa_handler = TARGET_SIG_DFL;
531 force_sig(TARGET_SIGSEGV /* , current */);
532}
533
534static void setup_rt_frame(int sig, struct emulated_sigaction *ka, siginfo_t *info,
535 target_sigset_t *set, CPUX86State *env)
536{
537 struct rt_sigframe *frame;
538 int err = 0;
539
540 frame = get_sigframe(ka, env, sizeof(*frame));
541
542#if 0
543 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
544 goto give_sigsegv;
545#endif
546
547 err |= __put_user((/*current->exec_domain
548 && current->exec_domain->signal_invmap
549 && sig < 32
550 ? current->exec_domain->signal_invmap[sig]
551 : */sig),
552 &frame->sig);
553 err |= __put_user((target_ulong)&frame->info, &frame->pinfo);
554 err |= __put_user((target_ulong)&frame->uc, &frame->puc);
555 err |= copy_siginfo_to_user(&frame->info, info);
556 if (err)
557 goto give_sigsegv;
31e31b8a 558
66fb9763
FB
559 /* Create the ucontext. */
560 err |= __put_user(0, &frame->uc.uc_flags);
561 err |= __put_user(0, &frame->uc.uc_link);
562 err |= __put_user(/*current->sas_ss_sp*/ 0, &frame->uc.uc_stack.ss_sp);
563 err |= __put_user(/* sas_ss_flags(regs->esp) */ 0,
564 &frame->uc.uc_stack.ss_flags);
565 err |= __put_user(/* current->sas_ss_size */ 0, &frame->uc.uc_stack.ss_size);
566 err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
567 env, set->sig[0]);
568 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
569 if (err)
570 goto give_sigsegv;
31e31b8a 571
66fb9763
FB
572 /* Set up to return from userspace. If provided, use a stub
573 already in userspace. */
574 if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
575 err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
576 } else {
577 err |= __put_user(frame->retcode, &frame->pretcode);
578 /* This is movl $,%eax ; int $0x80 */
579 err |= __put_user(0xb8, (char *)(frame->retcode+0));
580 err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
581 err |= __put_user(0x80cd, (short *)(frame->retcode+5));
582 }
583
584 if (err)
585 goto give_sigsegv;
586
587 /* Set up registers for signal handler */
588 env->regs[R_ESP] = (unsigned long) frame;
589 env->eip = (unsigned long) ka->sa._sa_handler;
590
591 cpu_x86_load_seg(env, R_DS, __USER_DS);
592 cpu_x86_load_seg(env, R_ES, __USER_DS);
593 cpu_x86_load_seg(env, R_SS, __USER_DS);
594 cpu_x86_load_seg(env, R_CS, __USER_CS);
595 env->eflags &= ~TF_MASK;
596
597 return;
598
599give_sigsegv:
600 if (sig == TARGET_SIGSEGV)
601 ka->sa._sa_handler = TARGET_SIG_DFL;
602 force_sig(TARGET_SIGSEGV /* , current */);
603}
604
605static int
606restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
607{
608 unsigned int err = 0;
609
610
611
612#define COPY(x) err |= __get_user(regs->x, &sc->x)
613
614#define COPY_SEG(seg) \
615 { unsigned short tmp; \
616 err |= __get_user(tmp, &sc->seg); \
617 regs->x##seg = tmp; }
618
619#define COPY_SEG_STRICT(seg) \
620 { unsigned short tmp; \
621 err |= __get_user(tmp, &sc->seg); \
622 regs->x##seg = tmp|3; }
623
624#define GET_SEG(seg) \
625 { unsigned short tmp; \
626 err |= __get_user(tmp, &sc->seg); \
627 loadsegment(seg,tmp); }
628
629 cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
630 cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
631 cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
632 cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
633
634 env->regs[R_EDI] = ldl(&sc->edi);
635 env->regs[R_ESI] = ldl(&sc->esi);
636 env->regs[R_EBP] = ldl(&sc->ebp);
637 env->regs[R_ESP] = ldl(&sc->esp);
638 env->regs[R_EBX] = ldl(&sc->ebx);
639 env->regs[R_EDX] = ldl(&sc->edx);
640 env->regs[R_ECX] = ldl(&sc->ecx);
641 env->eip = ldl(&sc->eip);
642
643 cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
644 cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
645
646 {
647 unsigned int tmpflags;
648 tmpflags = ldl(&sc->eflags);
649 env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
650 // regs->orig_eax = -1; /* disable syscall checks */
651 }
652
653#if 0
654 {
655 struct _fpstate * buf;
656 err |= __get_user(buf, &sc->fpstate);
657 if (buf) {
658 if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
659 goto badframe;
660 err |= restore_i387(buf);
661 }
662 }
663#endif
664 *peax = ldl(&sc->eax);
665 return err;
666#if 0
667badframe:
668 return 1;
669#endif
670}
671
672long do_sigreturn(CPUX86State *env)
673{
674 struct sigframe *frame = (struct sigframe *)(env->regs[R_ESP] - 8);
675 target_sigset_t target_set;
676 sigset_t set;
677 int eax, i;
678
679 /* set blocked signals */
680 target_set.sig[0] = frame->sc.oldmask;
681 for(i = 1; i < TARGET_NSIG_WORDS; i++)
682 target_set.sig[i] = frame->extramask[i - 1];
683
684 target_to_host_sigset(&set, &target_set);
685 sigprocmask(SIG_SETMASK, &set, NULL);
686
687 /* restore registers */
688 if (restore_sigcontext(env, &frame->sc, &eax))
689 goto badframe;
690 return eax;
691
692badframe:
693 force_sig(TARGET_SIGSEGV);
694 return 0;
695}
696
697long do_rt_sigreturn(CPUX86State *env)
698{
699 struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4);
700 target_sigset_t target_set;
701 sigset_t set;
702 // stack_t st;
703 int eax;
704
705#if 0
706 if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
707 goto badframe;
708#endif
709 memcpy(&target_set, &frame->uc.uc_sigmask, sizeof(target_sigset_t));
710
711 target_to_host_sigset(&set, &target_set);
712 sigprocmask(SIG_SETMASK, &set, NULL);
713
714 if (restore_sigcontext(env, &frame->uc.uc_mcontext, &eax))
715 goto badframe;
716
717#if 0
718 if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
719 goto badframe;
720 /* It is more difficult to avoid calling this function than to
721 call it and ignore errors. */
722 do_sigaltstack(&st, NULL, regs->esp);
723#endif
724 return eax;
725
726badframe:
727 force_sig(TARGET_SIGSEGV);
728 return 0;
729}
730
731#endif
732
733void process_pending_signals(void *cpu_env)
734{
735 int sig;
736 target_ulong handler;
737 target_sigset_t set;
738 struct emulated_sigaction *k;
739 struct sigqueue *q;
740
31e31b8a
FB
741 if (!signal_pending)
742 return;
743
66fb9763
FB
744 k = sigact_table;
745 for(sig = 1; sig <= TARGET_NSIG; sig++) {
746 if (k->pending)
31e31b8a 747 goto handle_signal;
66fb9763 748 k++;
31e31b8a
FB
749 }
750 /* if no signal is pending, just return */
751 signal_pending = 0;
752 return;
66fb9763 753
31e31b8a 754 handle_signal:
66fb9763
FB
755#ifdef DEBUG_SIGNAL
756 fprintf(stderr, "gemu: process signal %d\n", sig);
757#endif
758 /* dequeue signal */
759 q = k->first;
760 k->first = q->next;
761 if (!k->first)
762 k->pending = 0;
763
764 handler = k->sa._sa_handler;
765 if (handler == TARGET_SIG_DFL) {
766 /* default handler : ignore some signal. The other are fatal */
767 if (sig != TARGET_SIGCHLD &&
768 sig != TARGET_SIGURG &&
769 sig != TARGET_SIGWINCH) {
770 force_sig(sig);
771 }
772 } else if (handler == TARGET_SIG_IGN) {
773 /* ignore sig */
774 } else if (handler == TARGET_SIG_ERR) {
775 force_sig(sig);
776 } else {
777 set = k->sa.sa_mask;
778 /* send the signal to the CPU */
779 if (k->sa.sa_flags & TARGET_SA_SIGINFO)
780 setup_rt_frame(sig, k, &q->info, &set, cpu_env);
781 else
782 setup_frame(sig, k, &set, cpu_env);
783 if (k->sa.sa_flags & TARGET_SA_RESETHAND)
784 k->sa._sa_handler = TARGET_SIG_DFL;
31e31b8a 785 }
66fb9763
FB
786 if (q != &k->info)
787 free_sigqueue(q);
788}
31e31b8a
FB
789
790