xref: /qemu/linux-user/signal.c (revision 773b93ee0684a9b9d1f0029a936a251411289027)
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 #ifdef __ia64__
30 #undef uc_mcontext
31 #undef uc_sigmask
32 #undef uc_stack
33 #undef uc_link
34 #endif
35 
36 #include "qemu.h"
37 
38 //#define DEBUG_SIGNAL
39 
40 #define MAX_SIGQUEUE_SIZE 1024
41 
42 struct sigqueue {
43     struct sigqueue *next;
44     target_siginfo_t info;
45 };
46 
47 struct emulated_sigaction {
48     struct target_sigaction sa;
49     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 */
53 };
54 
55 static struct emulated_sigaction sigact_table[TARGET_NSIG];
56 static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57 static struct sigqueue *first_free; /* first free siginfo queue entry */
58 static int signal_pending; /* non zero if a signal may be pending */
59 
60 static void host_signal_handler(int host_signum, siginfo_t *info,
61                                 void *puc);
62 
63 static uint8_t host_to_target_signal_table[65] = {
64     [SIGHUP] = TARGET_SIGHUP,
65     [SIGINT] = TARGET_SIGINT,
66     [SIGQUIT] = TARGET_SIGQUIT,
67     [SIGILL] = TARGET_SIGILL,
68     [SIGTRAP] = TARGET_SIGTRAP,
69     [SIGABRT] = TARGET_SIGABRT,
70 /*    [SIGIOT] = TARGET_SIGIOT,*/
71     [SIGBUS] = TARGET_SIGBUS,
72     [SIGFPE] = TARGET_SIGFPE,
73     [SIGKILL] = TARGET_SIGKILL,
74     [SIGUSR1] = TARGET_SIGUSR1,
75     [SIGSEGV] = TARGET_SIGSEGV,
76     [SIGUSR2] = TARGET_SIGUSR2,
77     [SIGPIPE] = TARGET_SIGPIPE,
78     [SIGALRM] = TARGET_SIGALRM,
79     [SIGTERM] = TARGET_SIGTERM,
80 #ifdef SIGSTKFLT
81     [SIGSTKFLT] = TARGET_SIGSTKFLT,
82 #endif
83     [SIGCHLD] = TARGET_SIGCHLD,
84     [SIGCONT] = TARGET_SIGCONT,
85     [SIGSTOP] = TARGET_SIGSTOP,
86     [SIGTSTP] = TARGET_SIGTSTP,
87     [SIGTTIN] = TARGET_SIGTTIN,
88     [SIGTTOU] = TARGET_SIGTTOU,
89     [SIGURG] = TARGET_SIGURG,
90     [SIGXCPU] = TARGET_SIGXCPU,
91     [SIGXFSZ] = TARGET_SIGXFSZ,
92     [SIGVTALRM] = TARGET_SIGVTALRM,
93     [SIGPROF] = TARGET_SIGPROF,
94     [SIGWINCH] = TARGET_SIGWINCH,
95     [SIGIO] = TARGET_SIGIO,
96     [SIGPWR] = TARGET_SIGPWR,
97     [SIGSYS] = TARGET_SIGSYS,
98     /* next signals stay the same */
99 };
100 static uint8_t target_to_host_signal_table[65];
101 
102 static inline int host_to_target_signal(int sig)
103 {
104     return host_to_target_signal_table[sig];
105 }
106 
107 static inline int target_to_host_signal(int sig)
108 {
109     return target_to_host_signal_table[sig];
110 }
111 
112 void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
113 {
114     int i;
115     unsigned long sigmask;
116     uint32_t target_sigmask;
117 
118     sigmask = ((unsigned long *)s)[0];
119     target_sigmask = 0;
120     for(i = 0; i < 32; i++) {
121         if (sigmask & (1 << i))
122             target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
123     }
124 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
125     d->sig[0] = tswapl(target_sigmask);
126     for(i = 1;i < TARGET_NSIG_WORDS; i++) {
127         d->sig[i] = tswapl(((unsigned long *)s)[i]);
128     }
129 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
130     d->sig[0] = tswapl(target_sigmask);
131     d->sig[1] = tswapl(sigmask >> 32);
132 #else
133 #error host_to_target_sigset
134 #endif
135 }
136 
137 void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
138 {
139     int i;
140     unsigned long sigmask;
141     target_ulong target_sigmask;
142 
143     target_sigmask = tswapl(s->sig[0]);
144     sigmask = 0;
145     for(i = 0; i < 32; i++) {
146         if (target_sigmask & (1 << i))
147             sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
148     }
149 #if TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 32
150     ((unsigned long *)d)[0] = sigmask;
151     for(i = 1;i < TARGET_NSIG_WORDS; i++) {
152         ((unsigned long *)d)[i] = tswapl(s->sig[i]);
153     }
154 #elif TARGET_LONG_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
155     ((unsigned long *)d)[0] = sigmask | ((unsigned long)tswapl(s->sig[1]) << 32);
156 #else
157 #error target_to_host_sigset
158 #endif /* TARGET_LONG_BITS */
159 }
160 
161 void host_to_target_old_sigset(target_ulong *old_sigset,
162                                const sigset_t *sigset)
163 {
164     target_sigset_t d;
165     host_to_target_sigset(&d, sigset);
166     *old_sigset = d.sig[0];
167 }
168 
169 void target_to_host_old_sigset(sigset_t *sigset,
170                                const target_ulong *old_sigset)
171 {
172     target_sigset_t d;
173     int i;
174 
175     d.sig[0] = *old_sigset;
176     for(i = 1;i < TARGET_NSIG_WORDS; i++)
177         d.sig[i] = 0;
178     target_to_host_sigset(sigset, &d);
179 }
180 
181 /* siginfo conversion */
182 
183 static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
184                                                  const siginfo_t *info)
185 {
186     int sig;
187     sig = host_to_target_signal(info->si_signo);
188     tinfo->si_signo = sig;
189     tinfo->si_errno = 0;
190     tinfo->si_code = 0;
191     if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
192         sig == SIGBUS || sig == SIGTRAP) {
193         /* should never come here, but who knows. The information for
194            the target is irrelevant */
195         tinfo->_sifields._sigfault._addr = 0;
196     } else if (sig >= TARGET_SIGRTMIN) {
197         tinfo->_sifields._rt._pid = info->si_pid;
198         tinfo->_sifields._rt._uid = info->si_uid;
199         /* XXX: potential problem if 64 bit */
200         tinfo->_sifields._rt._sigval.sival_ptr =
201             (target_ulong)info->si_value.sival_ptr;
202     }
203 }
204 
205 static void tswap_siginfo(target_siginfo_t *tinfo,
206                           const target_siginfo_t *info)
207 {
208     int sig;
209     sig = info->si_signo;
210     tinfo->si_signo = tswap32(sig);
211     tinfo->si_errno = tswap32(info->si_errno);
212     tinfo->si_code = tswap32(info->si_code);
213     if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
214         sig == SIGBUS || sig == SIGTRAP) {
215         tinfo->_sifields._sigfault._addr =
216             tswapl(info->_sifields._sigfault._addr);
217     } else if (sig >= TARGET_SIGRTMIN) {
218         tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
219         tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
220         tinfo->_sifields._rt._sigval.sival_ptr =
221             tswapl(info->_sifields._rt._sigval.sival_ptr);
222     }
223 }
224 
225 
226 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
227 {
228     host_to_target_siginfo_noswap(tinfo, info);
229     tswap_siginfo(tinfo, tinfo);
230 }
231 
232 /* XXX: we support only POSIX RT signals are used. */
233 /* XXX: find a solution for 64 bit (additionnal malloced data is needed) */
234 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
235 {
236     info->si_signo = tswap32(tinfo->si_signo);
237     info->si_errno = tswap32(tinfo->si_errno);
238     info->si_code = tswap32(tinfo->si_code);
239     info->si_pid = tswap32(tinfo->_sifields._rt._pid);
240     info->si_uid = tswap32(tinfo->_sifields._rt._uid);
241     info->si_value.sival_ptr =
242         (void *)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
243 }
244 
245 void signal_init(void)
246 {
247     struct sigaction act;
248     int i, j;
249 
250     /* generate signal conversion tables */
251     for(i = 1; i <= 64; i++) {
252         if (host_to_target_signal_table[i] == 0)
253             host_to_target_signal_table[i] = i;
254     }
255     for(i = 1; i <= 64; i++) {
256         j = host_to_target_signal_table[i];
257         target_to_host_signal_table[j] = i;
258     }
259 
260     /* set all host signal handlers. ALL signals are blocked during
261        the handlers to serialize them. */
262     sigfillset(&act.sa_mask);
263     act.sa_flags = SA_SIGINFO;
264     act.sa_sigaction = host_signal_handler;
265     for(i = 1; i < NSIG; i++) {
266         sigaction(i, &act, NULL);
267     }
268 
269     memset(sigact_table, 0, sizeof(sigact_table));
270 
271     first_free = &sigqueue_table[0];
272     for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
273         sigqueue_table[i].next = &sigqueue_table[i + 1];
274     sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
275 }
276 
277 /* signal queue handling */
278 
279 static inline struct sigqueue *alloc_sigqueue(void)
280 {
281     struct sigqueue *q = first_free;
282     if (!q)
283         return NULL;
284     first_free = q->next;
285     return q;
286 }
287 
288 static inline void free_sigqueue(struct sigqueue *q)
289 {
290     q->next = first_free;
291     first_free = q;
292 }
293 
294 /* abort execution with signal */
295 void __attribute((noreturn)) force_sig(int sig)
296 {
297     int host_sig;
298     host_sig = target_to_host_signal(sig);
299     fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
300             sig, strsignal(host_sig));
301 #if 1
302     _exit(-host_sig);
303 #else
304     {
305         struct sigaction act;
306         sigemptyset(&act.sa_mask);
307         act.sa_flags = SA_SIGINFO;
308         act.sa_sigaction = SIG_DFL;
309         sigaction(SIGABRT, &act, NULL);
310         abort();
311     }
312 #endif
313 }
314 
315 /* queue a signal so that it will be send to the virtual CPU as soon
316    as possible */
317 int queue_signal(int sig, target_siginfo_t *info)
318 {
319     struct emulated_sigaction *k;
320     struct sigqueue *q, **pq;
321     target_ulong handler;
322 
323 #if defined(DEBUG_SIGNAL)
324     fprintf(stderr, "queue_signal: sig=%d\n",
325             sig);
326 #endif
327     k = &sigact_table[sig - 1];
328     handler = k->sa._sa_handler;
329     if (handler == TARGET_SIG_DFL) {
330         /* default handler : ignore some signal. The other are fatal */
331         if (sig != TARGET_SIGCHLD &&
332             sig != TARGET_SIGURG &&
333             sig != TARGET_SIGWINCH) {
334             force_sig(sig);
335         } else {
336             return 0; /* indicate ignored */
337         }
338     } else if (handler == TARGET_SIG_IGN) {
339         /* ignore signal */
340         return 0;
341     } else if (handler == TARGET_SIG_ERR) {
342         force_sig(sig);
343     } else {
344         pq = &k->first;
345         if (sig < TARGET_SIGRTMIN) {
346             /* if non real time signal, we queue exactly one signal */
347             if (!k->pending)
348                 q = &k->info;
349             else
350                 return 0;
351         } else {
352             if (!k->pending) {
353                 /* first signal */
354                 q = &k->info;
355             } else {
356                 q = alloc_sigqueue();
357                 if (!q)
358                     return -EAGAIN;
359                 while (*pq != NULL)
360                     pq = &(*pq)->next;
361             }
362         }
363         *pq = q;
364         q->info = *info;
365         q->next = NULL;
366         k->pending = 1;
367         /* signal that a new signal is pending */
368         signal_pending = 1;
369         return 1; /* indicates that the signal was queued */
370     }
371 }
372 
373 static void host_signal_handler(int host_signum, siginfo_t *info,
374                                 void *puc)
375 {
376     int sig;
377     target_siginfo_t tinfo;
378 
379     /* the CPU emulator uses some host signals to detect exceptions,
380        we we forward to it some signals */
381     if (host_signum == SIGSEGV || host_signum == SIGBUS) {
382         if (cpu_signal_handler(host_signum, info, puc))
383             return;
384     }
385 
386     /* get target signal number */
387     sig = host_to_target_signal(host_signum);
388     if (sig < 1 || sig > TARGET_NSIG)
389         return;
390 #if defined(DEBUG_SIGNAL)
391     fprintf(stderr, "qemu: got signal %d\n", sig);
392 #endif
393     host_to_target_siginfo_noswap(&tinfo, info);
394     if (queue_signal(sig, &tinfo) == 1) {
395         /* interrupt the virtual CPU as soon as possible */
396         cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
397     }
398 }
399 
400 int do_sigaction(int sig, const struct target_sigaction *act,
401                  struct target_sigaction *oact)
402 {
403     struct emulated_sigaction *k;
404     struct sigaction act1;
405     int host_sig;
406 
407     if (sig < 1 || sig > TARGET_NSIG)
408         return -EINVAL;
409     k = &sigact_table[sig - 1];
410 #if defined(DEBUG_SIGNAL)
411     fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
412             sig, (int)act, (int)oact);
413 #endif
414     if (oact) {
415         oact->_sa_handler = tswapl(k->sa._sa_handler);
416         oact->sa_flags = tswapl(k->sa.sa_flags);
417         oact->sa_restorer = tswapl(k->sa.sa_restorer);
418         oact->sa_mask = k->sa.sa_mask;
419     }
420     if (act) {
421         k->sa._sa_handler = tswapl(act->_sa_handler);
422         k->sa.sa_flags = tswapl(act->sa_flags);
423         k->sa.sa_restorer = tswapl(act->sa_restorer);
424         k->sa.sa_mask = act->sa_mask;
425 
426         /* we update the host linux signal state */
427         host_sig = target_to_host_signal(sig);
428         if (host_sig != SIGSEGV && host_sig != SIGBUS) {
429             sigfillset(&act1.sa_mask);
430             act1.sa_flags = SA_SIGINFO;
431             if (k->sa.sa_flags & TARGET_SA_RESTART)
432                 act1.sa_flags |= SA_RESTART;
433             /* NOTE: it is important to update the host kernel signal
434                ignore state to avoid getting unexpected interrupted
435                syscalls */
436             if (k->sa._sa_handler == TARGET_SIG_IGN) {
437                 act1.sa_sigaction = (void *)SIG_IGN;
438             } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
439                 act1.sa_sigaction = (void *)SIG_DFL;
440             } else {
441                 act1.sa_sigaction = host_signal_handler;
442             }
443             sigaction(host_sig, &act1, NULL);
444         }
445     }
446     return 0;
447 }
448 
449 #define __put_user(x,ptr)\
450 ({\
451     int size = sizeof(*ptr);\
452     switch(size) {\
453     case 1:\
454         stb(ptr, (typeof(*ptr))(x));\
455         break;\
456     case 2:\
457         stw(ptr, (typeof(*ptr))(x));\
458         break;\
459     case 4:\
460         stl(ptr, (typeof(*ptr))(x));\
461         break;\
462     case 8:\
463         stq(ptr, (typeof(*ptr))(x));\
464         break;\
465     default:\
466         abort();\
467     }\
468     0;\
469 })
470 
471 #define __get_user(x, ptr) \
472 ({\
473     int size = sizeof(*ptr);\
474     switch(size) {\
475     case 1:\
476         x = (typeof(*ptr))ldub(ptr);\
477         break;\
478     case 2:\
479         x = (typeof(*ptr))lduw(ptr);\
480         break;\
481     case 4:\
482         x = (typeof(*ptr))ldl(ptr);\
483         break;\
484     case 8:\
485         x = (typeof(*ptr))ldq(ptr);\
486         break;\
487     default:\
488         abort();\
489     }\
490     0;\
491 })
492 
493 
494 #define __copy_to_user(dst, src, size)\
495 ({\
496     memcpy(dst, src, size);\
497     0;\
498 })
499 
500 #define __copy_from_user(dst, src, size)\
501 ({\
502     memcpy(dst, src, size);\
503     0;\
504 })
505 
506 #define __clear_user(dst, size)\
507 ({\
508     memset(dst, 0, size);\
509     0;\
510 })
511 
512 #ifndef offsetof
513 #define offsetof(type, field) ((size_t) &((type *)0)->field)
514 #endif
515 
516 static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
517                                        const target_siginfo_t *info)
518 {
519     tswap_siginfo(tinfo, info);
520     return 0;
521 }
522 
523 #ifdef TARGET_I386
524 
525 /* from the Linux kernel */
526 
527 struct target_fpreg {
528 	uint16_t significand[4];
529 	uint16_t exponent;
530 };
531 
532 struct target_fpxreg {
533 	uint16_t significand[4];
534 	uint16_t exponent;
535 	uint16_t padding[3];
536 };
537 
538 struct target_xmmreg {
539 	target_ulong element[4];
540 };
541 
542 struct target_fpstate {
543 	/* Regular FPU environment */
544 	target_ulong 	cw;
545 	target_ulong	sw;
546 	target_ulong	tag;
547 	target_ulong	ipoff;
548 	target_ulong	cssel;
549 	target_ulong	dataoff;
550 	target_ulong	datasel;
551 	struct target_fpreg	_st[8];
552 	uint16_t	status;
553 	uint16_t	magic;		/* 0xffff = regular FPU data only */
554 
555 	/* FXSR FPU environment */
556 	target_ulong	_fxsr_env[6];	/* FXSR FPU env is ignored */
557 	target_ulong	mxcsr;
558 	target_ulong	reserved;
559 	struct target_fpxreg	_fxsr_st[8];	/* FXSR FPU reg data is ignored */
560 	struct target_xmmreg	_xmm[8];
561 	target_ulong	padding[56];
562 };
563 
564 #define X86_FXSR_MAGIC		0x0000
565 
566 struct target_sigcontext {
567 	uint16_t gs, __gsh;
568 	uint16_t fs, __fsh;
569 	uint16_t es, __esh;
570 	uint16_t ds, __dsh;
571 	target_ulong edi;
572 	target_ulong esi;
573 	target_ulong ebp;
574 	target_ulong esp;
575 	target_ulong ebx;
576 	target_ulong edx;
577 	target_ulong ecx;
578 	target_ulong eax;
579 	target_ulong trapno;
580 	target_ulong err;
581 	target_ulong eip;
582 	uint16_t cs, __csh;
583 	target_ulong eflags;
584 	target_ulong esp_at_signal;
585 	uint16_t ss, __ssh;
586         target_ulong fpstate; /* pointer */
587 	target_ulong oldmask;
588 	target_ulong cr2;
589 };
590 
591 typedef struct target_sigaltstack {
592 	target_ulong ss_sp;
593 	int ss_flags;
594 	target_ulong ss_size;
595 } target_stack_t;
596 
597 struct target_ucontext {
598         target_ulong	  uc_flags;
599 	target_ulong      uc_link;
600 	target_stack_t	  uc_stack;
601 	struct target_sigcontext uc_mcontext;
602 	target_sigset_t	  uc_sigmask;	/* mask last for extensibility */
603 };
604 
605 struct sigframe
606 {
607     target_ulong pretcode;
608     int sig;
609     struct target_sigcontext sc;
610     struct target_fpstate fpstate;
611     target_ulong extramask[TARGET_NSIG_WORDS-1];
612     char retcode[8];
613 };
614 
615 struct rt_sigframe
616 {
617     target_ulong pretcode;
618     int sig;
619     target_ulong pinfo;
620     target_ulong puc;
621     struct target_siginfo info;
622     struct target_ucontext uc;
623     struct target_fpstate fpstate;
624     char retcode[8];
625 };
626 
627 /*
628  * Set up a signal frame.
629  */
630 
631 /* XXX: save x87 state */
632 static int
633 setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
634 		 CPUX86State *env, unsigned long mask)
635 {
636 	int err = 0;
637 
638 	err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
639 	err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
640 	err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
641 	err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
642 	err |= __put_user(env->regs[R_EDI], &sc->edi);
643 	err |= __put_user(env->regs[R_ESI], &sc->esi);
644 	err |= __put_user(env->regs[R_EBP], &sc->ebp);
645 	err |= __put_user(env->regs[R_ESP], &sc->esp);
646 	err |= __put_user(env->regs[R_EBX], &sc->ebx);
647 	err |= __put_user(env->regs[R_EDX], &sc->edx);
648 	err |= __put_user(env->regs[R_ECX], &sc->ecx);
649 	err |= __put_user(env->regs[R_EAX], &sc->eax);
650 	err |= __put_user(env->exception_index, &sc->trapno);
651 	err |= __put_user(env->error_code, &sc->err);
652 	err |= __put_user(env->eip, &sc->eip);
653 	err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
654 	err |= __put_user(env->eflags, &sc->eflags);
655 	err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
656 	err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
657 
658         cpu_x86_fsave(env, (void *)fpstate, 1);
659         fpstate->status = fpstate->sw;
660         err |= __put_user(0xffff, &fpstate->magic);
661         err |= __put_user(fpstate, &sc->fpstate);
662 
663 	/* non-iBCS2 extensions.. */
664 	err |= __put_user(mask, &sc->oldmask);
665 	err |= __put_user(env->cr[2], &sc->cr2);
666 	return err;
667 }
668 
669 /*
670  * Determine which stack to use..
671  */
672 
673 static inline void *
674 get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
675 {
676 	unsigned long esp;
677 
678 	/* Default to using normal stack */
679 	esp = env->regs[R_ESP];
680 #if 0
681 	/* This is the X/Open sanctioned signal stack switching.  */
682 	if (ka->sa.sa_flags & SA_ONSTACK) {
683 		if (sas_ss_flags(esp) == 0)
684 			esp = current->sas_ss_sp + current->sas_ss_size;
685 	}
686 
687 	/* This is the legacy signal stack switching. */
688 	else
689 #endif
690         if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
691             !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
692             ka->sa.sa_restorer) {
693             esp = (unsigned long) ka->sa.sa_restorer;
694 	}
695         return (void *)((esp - frame_size) & -8ul);
696 }
697 
698 static void setup_frame(int sig, struct emulated_sigaction *ka,
699 			target_sigset_t *set, CPUX86State *env)
700 {
701 	struct sigframe *frame;
702 	int err = 0;
703 
704 	frame = get_sigframe(ka, env, sizeof(*frame));
705 
706 #if 0
707 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
708 		goto give_sigsegv;
709 #endif
710 	err |= __put_user((/*current->exec_domain
711 		           && current->exec_domain->signal_invmap
712 		           && sig < 32
713 		           ? current->exec_domain->signal_invmap[sig]
714 		           : */ sig),
715 		          &frame->sig);
716 	if (err)
717 		goto give_sigsegv;
718 
719 	setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0]);
720 	if (err)
721 		goto give_sigsegv;
722 
723 	if (TARGET_NSIG_WORDS > 1) {
724 		err |= __copy_to_user(frame->extramask, &set->sig[1],
725 				      sizeof(frame->extramask));
726 	}
727 	if (err)
728 		goto give_sigsegv;
729 
730 	/* Set up to return from userspace.  If provided, use a stub
731 	   already in userspace.  */
732 	if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
733 		err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
734 	} else {
735 		err |= __put_user(frame->retcode, &frame->pretcode);
736 		/* This is popl %eax ; movl $,%eax ; int $0x80 */
737 		err |= __put_user(0xb858, (short *)(frame->retcode+0));
738 		err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
739 		err |= __put_user(0x80cd, (short *)(frame->retcode+6));
740 	}
741 
742 	if (err)
743 		goto give_sigsegv;
744 
745 	/* Set up registers for signal handler */
746 	env->regs[R_ESP] = (unsigned long) frame;
747 	env->eip = (unsigned long) ka->sa._sa_handler;
748 
749         cpu_x86_load_seg(env, R_DS, __USER_DS);
750         cpu_x86_load_seg(env, R_ES, __USER_DS);
751         cpu_x86_load_seg(env, R_SS, __USER_DS);
752         cpu_x86_load_seg(env, R_CS, __USER_CS);
753 	env->eflags &= ~TF_MASK;
754 
755 	return;
756 
757 give_sigsegv:
758 	if (sig == TARGET_SIGSEGV)
759 		ka->sa._sa_handler = TARGET_SIG_DFL;
760 	force_sig(TARGET_SIGSEGV /* , current */);
761 }
762 
763 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
764                            target_siginfo_t *info,
765 			   target_sigset_t *set, CPUX86State *env)
766 {
767 	struct rt_sigframe *frame;
768 	int err = 0;
769 
770 	frame = get_sigframe(ka, env, sizeof(*frame));
771 
772 #if 0
773 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
774 		goto give_sigsegv;
775 #endif
776 
777 	err |= __put_user((/*current->exec_domain
778 		    	   && current->exec_domain->signal_invmap
779 		    	   && sig < 32
780 		    	   ? current->exec_domain->signal_invmap[sig]
781 			   : */sig),
782 			  &frame->sig);
783 	err |= __put_user((target_ulong)&frame->info, &frame->pinfo);
784 	err |= __put_user((target_ulong)&frame->uc, &frame->puc);
785 	err |= copy_siginfo_to_user(&frame->info, info);
786 	if (err)
787 		goto give_sigsegv;
788 
789 	/* Create the ucontext.  */
790 	err |= __put_user(0, &frame->uc.uc_flags);
791 	err |= __put_user(0, &frame->uc.uc_link);
792 	err |= __put_user(/*current->sas_ss_sp*/ 0, &frame->uc.uc_stack.ss_sp);
793 	err |= __put_user(/* sas_ss_flags(regs->esp) */ 0,
794 			  &frame->uc.uc_stack.ss_flags);
795 	err |= __put_user(/* current->sas_ss_size */ 0, &frame->uc.uc_stack.ss_size);
796 	err |= setup_sigcontext(&frame->uc.uc_mcontext, &frame->fpstate,
797 			        env, set->sig[0]);
798 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
799 	if (err)
800 		goto give_sigsegv;
801 
802 	/* Set up to return from userspace.  If provided, use a stub
803 	   already in userspace.  */
804 	if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
805 		err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
806 	} else {
807 		err |= __put_user(frame->retcode, &frame->pretcode);
808 		/* This is movl $,%eax ; int $0x80 */
809 		err |= __put_user(0xb8, (char *)(frame->retcode+0));
810 		err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
811 		err |= __put_user(0x80cd, (short *)(frame->retcode+5));
812 	}
813 
814 	if (err)
815 		goto give_sigsegv;
816 
817 	/* Set up registers for signal handler */
818 	env->regs[R_ESP] = (unsigned long) frame;
819 	env->eip = (unsigned long) ka->sa._sa_handler;
820 
821         cpu_x86_load_seg(env, R_DS, __USER_DS);
822         cpu_x86_load_seg(env, R_ES, __USER_DS);
823         cpu_x86_load_seg(env, R_SS, __USER_DS);
824         cpu_x86_load_seg(env, R_CS, __USER_CS);
825 	env->eflags &= ~TF_MASK;
826 
827 	return;
828 
829 give_sigsegv:
830 	if (sig == TARGET_SIGSEGV)
831 		ka->sa._sa_handler = TARGET_SIG_DFL;
832 	force_sig(TARGET_SIGSEGV /* , current */);
833 }
834 
835 static int
836 restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
837 {
838 	unsigned int err = 0;
839 
840         cpu_x86_load_seg(env, R_GS, lduw(&sc->gs));
841         cpu_x86_load_seg(env, R_FS, lduw(&sc->fs));
842         cpu_x86_load_seg(env, R_ES, lduw(&sc->es));
843         cpu_x86_load_seg(env, R_DS, lduw(&sc->ds));
844 
845         env->regs[R_EDI] = ldl(&sc->edi);
846         env->regs[R_ESI] = ldl(&sc->esi);
847         env->regs[R_EBP] = ldl(&sc->ebp);
848         env->regs[R_ESP] = ldl(&sc->esp);
849         env->regs[R_EBX] = ldl(&sc->ebx);
850         env->regs[R_EDX] = ldl(&sc->edx);
851         env->regs[R_ECX] = ldl(&sc->ecx);
852         env->eip = ldl(&sc->eip);
853 
854         cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
855         cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
856 
857 	{
858 		unsigned int tmpflags;
859                 tmpflags = ldl(&sc->eflags);
860 		env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
861                 //		regs->orig_eax = -1;		/* disable syscall checks */
862 	}
863 
864 	{
865 		struct _fpstate * buf;
866                 buf = (void *)ldl(&sc->fpstate);
867 		if (buf) {
868 #if 0
869 			if (verify_area(VERIFY_READ, buf, sizeof(*buf)))
870 				goto badframe;
871 #endif
872                         cpu_x86_frstor(env, (void *)buf, 1);
873 		}
874 	}
875 
876         *peax = ldl(&sc->eax);
877 	return err;
878 #if 0
879 badframe:
880 	return 1;
881 #endif
882 }
883 
884 long do_sigreturn(CPUX86State *env)
885 {
886     struct sigframe *frame = (struct sigframe *)(env->regs[R_ESP] - 8);
887     target_sigset_t target_set;
888     sigset_t set;
889     int eax, i;
890 
891 #if defined(DEBUG_SIGNAL)
892     fprintf(stderr, "do_sigreturn\n");
893 #endif
894     /* set blocked signals */
895     target_set.sig[0] = frame->sc.oldmask;
896     for(i = 1; i < TARGET_NSIG_WORDS; i++)
897         target_set.sig[i] = frame->extramask[i - 1];
898 
899     target_to_host_sigset(&set, &target_set);
900     sigprocmask(SIG_SETMASK, &set, NULL);
901 
902     /* restore registers */
903     if (restore_sigcontext(env, &frame->sc, &eax))
904         goto badframe;
905     return eax;
906 
907 badframe:
908     force_sig(TARGET_SIGSEGV);
909     return 0;
910 }
911 
912 long do_rt_sigreturn(CPUX86State *env)
913 {
914 	struct rt_sigframe *frame = (struct rt_sigframe *)(env->regs[R_ESP] - 4);
915 	target_sigset_t target_set;
916         sigset_t set;
917         //	stack_t st;
918 	int eax;
919 
920 #if 0
921 	if (verify_area(VERIFY_READ, frame, sizeof(*frame)))
922 		goto badframe;
923 #endif
924         memcpy(&target_set, &frame->uc.uc_sigmask, sizeof(target_sigset_t));
925 
926         target_to_host_sigset(&set, &target_set);
927         sigprocmask(SIG_SETMASK, &set, NULL);
928 
929 	if (restore_sigcontext(env, &frame->uc.uc_mcontext, &eax))
930 		goto badframe;
931 
932 #if 0
933 	if (__copy_from_user(&st, &frame->uc.uc_stack, sizeof(st)))
934 		goto badframe;
935 	/* It is more difficult to avoid calling this function than to
936 	   call it and ignore errors.  */
937 	do_sigaltstack(&st, NULL, regs->esp);
938 #endif
939 	return eax;
940 
941 badframe:
942 	force_sig(TARGET_SIGSEGV);
943 	return 0;
944 }
945 
946 #elif defined(TARGET_ARM)
947 
948 struct target_sigcontext {
949 	target_ulong trap_no;
950 	target_ulong error_code;
951 	target_ulong oldmask;
952 	target_ulong arm_r0;
953 	target_ulong arm_r1;
954 	target_ulong arm_r2;
955 	target_ulong arm_r3;
956 	target_ulong arm_r4;
957 	target_ulong arm_r5;
958 	target_ulong arm_r6;
959 	target_ulong arm_r7;
960 	target_ulong arm_r8;
961 	target_ulong arm_r9;
962 	target_ulong arm_r10;
963 	target_ulong arm_fp;
964 	target_ulong arm_ip;
965 	target_ulong arm_sp;
966 	target_ulong arm_lr;
967 	target_ulong arm_pc;
968 	target_ulong arm_cpsr;
969 	target_ulong fault_address;
970 };
971 
972 typedef struct target_sigaltstack {
973 	target_ulong ss_sp;
974 	int ss_flags;
975 	target_ulong ss_size;
976 } target_stack_t;
977 
978 struct target_ucontext {
979     target_ulong uc_flags;
980     target_ulong uc_link;
981     target_stack_t uc_stack;
982     struct target_sigcontext uc_mcontext;
983     target_sigset_t  uc_sigmask;	/* mask last for extensibility */
984 };
985 
986 struct sigframe
987 {
988     struct target_sigcontext sc;
989     target_ulong extramask[TARGET_NSIG_WORDS-1];
990     target_ulong retcode;
991 };
992 
993 struct rt_sigframe
994 {
995     struct target_siginfo *pinfo;
996     void *puc;
997     struct target_siginfo info;
998     struct target_ucontext uc;
999     target_ulong retcode;
1000 };
1001 
1002 #define TARGET_CONFIG_CPU_32 1
1003 
1004 /*
1005  * For ARM syscalls, we encode the syscall number into the instruction.
1006  */
1007 #define SWI_SYS_SIGRETURN	(0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1008 #define SWI_SYS_RT_SIGRETURN	(0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1009 
1010 /*
1011  * For Thumb syscalls, we pass the syscall number via r7.  We therefore
1012  * need two 16-bit instructions.
1013  */
1014 #define SWI_THUMB_SIGRETURN	(0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1015 #define SWI_THUMB_RT_SIGRETURN	(0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1016 
1017 static const target_ulong retcodes[4] = {
1018 	SWI_SYS_SIGRETURN,	SWI_THUMB_SIGRETURN,
1019 	SWI_SYS_RT_SIGRETURN,	SWI_THUMB_RT_SIGRETURN
1020 };
1021 
1022 
1023 #define __put_user_error(x,p,e) __put_user(x, p)
1024 #define __get_user_error(x,p,e) __get_user(x, p)
1025 
1026 static inline int valid_user_regs(CPUState *regs)
1027 {
1028     return 1;
1029 }
1030 
1031 static int
1032 setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1033 		 CPUState *env, unsigned long mask)
1034 {
1035 	int err = 0;
1036 
1037 	__put_user_error(env->regs[0], &sc->arm_r0, err);
1038 	__put_user_error(env->regs[1], &sc->arm_r1, err);
1039 	__put_user_error(env->regs[2], &sc->arm_r2, err);
1040 	__put_user_error(env->regs[3], &sc->arm_r3, err);
1041 	__put_user_error(env->regs[4], &sc->arm_r4, err);
1042 	__put_user_error(env->regs[5], &sc->arm_r5, err);
1043 	__put_user_error(env->regs[6], &sc->arm_r6, err);
1044 	__put_user_error(env->regs[7], &sc->arm_r7, err);
1045 	__put_user_error(env->regs[8], &sc->arm_r8, err);
1046 	__put_user_error(env->regs[9], &sc->arm_r9, err);
1047 	__put_user_error(env->regs[10], &sc->arm_r10, err);
1048 	__put_user_error(env->regs[11], &sc->arm_fp, err);
1049 	__put_user_error(env->regs[12], &sc->arm_ip, err);
1050 	__put_user_error(env->regs[13], &sc->arm_sp, err);
1051 	__put_user_error(env->regs[14], &sc->arm_lr, err);
1052 	__put_user_error(env->regs[15], &sc->arm_pc, err);
1053 #ifdef TARGET_CONFIG_CPU_32
1054 	__put_user_error(env->cpsr, &sc->arm_cpsr, err);
1055 #endif
1056 
1057 	__put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1058 	__put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1059 	__put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1060 	__put_user_error(mask, &sc->oldmask, err);
1061 
1062 	return err;
1063 }
1064 
1065 static inline void *
1066 get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1067 {
1068 	unsigned long sp = regs->regs[13];
1069 
1070 #if 0
1071 	/*
1072 	 * This is the X/Open sanctioned signal stack switching.
1073 	 */
1074 	if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
1075 		sp = current->sas_ss_sp + current->sas_ss_size;
1076 #endif
1077 	/*
1078 	 * ATPCS B01 mandates 8-byte alignment
1079 	 */
1080 	return (void *)((sp - framesize) & ~7);
1081 }
1082 
1083 static int
1084 setup_return(CPUState *env, struct emulated_sigaction *ka,
1085 	     target_ulong *rc, void *frame, int usig)
1086 {
1087 	target_ulong handler = (target_ulong)ka->sa._sa_handler;
1088 	target_ulong retcode;
1089 	int thumb = 0;
1090 #if defined(TARGET_CONFIG_CPU_32)
1091 	target_ulong cpsr = env->cpsr;
1092 
1093 #if 0
1094 	/*
1095 	 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1096 	 */
1097 	if (ka->sa.sa_flags & SA_THIRTYTWO)
1098 		cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1099 
1100 #ifdef CONFIG_ARM_THUMB
1101 	if (elf_hwcap & HWCAP_THUMB) {
1102 		/*
1103 		 * The LSB of the handler determines if we're going to
1104 		 * be using THUMB or ARM mode for this signal handler.
1105 		 */
1106 		thumb = handler & 1;
1107 
1108 		if (thumb)
1109 			cpsr |= T_BIT;
1110 		else
1111 			cpsr &= ~T_BIT;
1112 	}
1113 #endif
1114 #endif
1115 #endif /* TARGET_CONFIG_CPU_32 */
1116 
1117 	if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
1118 		retcode = (target_ulong)ka->sa.sa_restorer;
1119 	} else {
1120 		unsigned int idx = thumb;
1121 
1122 		if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1123 			idx += 2;
1124 
1125 		if (__put_user(retcodes[idx], rc))
1126 			return 1;
1127 #if 0
1128 		flush_icache_range((target_ulong)rc,
1129 				   (target_ulong)(rc + 1));
1130 #endif
1131 		retcode = ((target_ulong)rc) + thumb;
1132 	}
1133 
1134 	env->regs[0] = usig;
1135 	env->regs[13] = (target_ulong)frame;
1136 	env->regs[14] = retcode;
1137 	env->regs[15] = handler & (thumb ? ~1 : ~3);
1138 
1139 #ifdef TARGET_CONFIG_CPU_32
1140 	env->cpsr = cpsr;
1141 #endif
1142 
1143 	return 0;
1144 }
1145 
1146 static void setup_frame(int usig, struct emulated_sigaction *ka,
1147 			target_sigset_t *set, CPUState *regs)
1148 {
1149 	struct sigframe *frame = get_sigframe(ka, regs, sizeof(*frame));
1150 	int err = 0;
1151 
1152 	err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
1153 
1154 	if (TARGET_NSIG_WORDS > 1) {
1155 		err |= __copy_to_user(frame->extramask, &set->sig[1],
1156 				      sizeof(frame->extramask));
1157 	}
1158 
1159 	if (err == 0)
1160             err = setup_return(regs, ka, &frame->retcode, frame, usig);
1161         //	return err;
1162 }
1163 
1164 static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1165                            target_siginfo_t *info,
1166 			   target_sigset_t *set, CPUState *env)
1167 {
1168 	struct rt_sigframe *frame = get_sigframe(ka, env, sizeof(*frame));
1169 	int err = 0;
1170 
1171 #if 0
1172 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
1173             return 1;
1174 #endif
1175 	__put_user_error(&frame->info, (target_ulong *)&frame->pinfo, err);
1176 	__put_user_error(&frame->uc, (target_ulong *)&frame->puc, err);
1177 	err |= copy_siginfo_to_user(&frame->info, info);
1178 
1179 	/* Clear all the bits of the ucontext we don't use.  */
1180 	err |= __clear_user(&frame->uc, offsetof(struct ucontext, uc_mcontext));
1181 
1182 	err |= setup_sigcontext(&frame->uc.uc_mcontext, /*&frame->fpstate,*/
1183 				env, set->sig[0]);
1184 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
1185 
1186 	if (err == 0)
1187 		err = setup_return(env, ka, &frame->retcode, frame, usig);
1188 
1189 	if (err == 0) {
1190 		/*
1191 		 * For realtime signals we must also set the second and third
1192 		 * arguments for the signal handler.
1193 		 *   -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1194 		 */
1195             env->regs[1] = (target_ulong)frame->pinfo;
1196             env->regs[2] = (target_ulong)frame->puc;
1197 	}
1198 
1199         //	return err;
1200 }
1201 
1202 static int
1203 restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1204 {
1205 	int err = 0;
1206 
1207 	__get_user_error(env->regs[0], &sc->arm_r0, err);
1208 	__get_user_error(env->regs[1], &sc->arm_r1, err);
1209 	__get_user_error(env->regs[2], &sc->arm_r2, err);
1210 	__get_user_error(env->regs[3], &sc->arm_r3, err);
1211 	__get_user_error(env->regs[4], &sc->arm_r4, err);
1212 	__get_user_error(env->regs[5], &sc->arm_r5, err);
1213 	__get_user_error(env->regs[6], &sc->arm_r6, err);
1214 	__get_user_error(env->regs[7], &sc->arm_r7, err);
1215 	__get_user_error(env->regs[8], &sc->arm_r8, err);
1216 	__get_user_error(env->regs[9], &sc->arm_r9, err);
1217 	__get_user_error(env->regs[10], &sc->arm_r10, err);
1218 	__get_user_error(env->regs[11], &sc->arm_fp, err);
1219 	__get_user_error(env->regs[12], &sc->arm_ip, err);
1220 	__get_user_error(env->regs[13], &sc->arm_sp, err);
1221 	__get_user_error(env->regs[14], &sc->arm_lr, err);
1222 	__get_user_error(env->regs[15], &sc->arm_pc, err);
1223 #ifdef TARGET_CONFIG_CPU_32
1224 	__get_user_error(env->cpsr, &sc->arm_cpsr, err);
1225 #endif
1226 
1227 	err |= !valid_user_regs(env);
1228 
1229 	return err;
1230 }
1231 
1232 long do_sigreturn(CPUState *env)
1233 {
1234 	struct sigframe *frame;
1235 	target_sigset_t set;
1236         sigset_t host_set;
1237 
1238 	/*
1239 	 * Since we stacked the signal on a 64-bit boundary,
1240 	 * then 'sp' should be word aligned here.  If it's
1241 	 * not, then the user is trying to mess with us.
1242 	 */
1243 	if (env->regs[13] & 7)
1244 		goto badframe;
1245 
1246 	frame = (struct sigframe *)env->regs[13];
1247 
1248 #if 0
1249 	if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1250 		goto badframe;
1251 #endif
1252 	if (__get_user(set.sig[0], &frame->sc.oldmask)
1253 	    || (TARGET_NSIG_WORDS > 1
1254 	        && __copy_from_user(&set.sig[1], &frame->extramask,
1255 				    sizeof(frame->extramask))))
1256 		goto badframe;
1257 
1258         target_to_host_sigset(&host_set, &set);
1259         sigprocmask(SIG_SETMASK, &host_set, NULL);
1260 
1261 	if (restore_sigcontext(env, &frame->sc))
1262 		goto badframe;
1263 
1264 #if 0
1265 	/* Send SIGTRAP if we're single-stepping */
1266 	if (ptrace_cancel_bpt(current))
1267 		send_sig(SIGTRAP, current, 1);
1268 #endif
1269 	return env->regs[0];
1270 
1271 badframe:
1272         force_sig(SIGSEGV /* , current */);
1273 	return 0;
1274 }
1275 
1276 long do_rt_sigreturn(CPUState *env)
1277 {
1278 	struct rt_sigframe *frame;
1279 	target_sigset_t set;
1280         sigset_t host_set;
1281 
1282 	/*
1283 	 * Since we stacked the signal on a 64-bit boundary,
1284 	 * then 'sp' should be word aligned here.  If it's
1285 	 * not, then the user is trying to mess with us.
1286 	 */
1287 	if (env->regs[13] & 7)
1288 		goto badframe;
1289 
1290 	frame = (struct rt_sigframe *)env->regs[13];
1291 
1292 #if 0
1293 	if (verify_area(VERIFY_READ, frame, sizeof (*frame)))
1294 		goto badframe;
1295 #endif
1296 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
1297 		goto badframe;
1298 
1299         target_to_host_sigset(&host_set, &set);
1300         sigprocmask(SIG_SETMASK, &host_set, NULL);
1301 
1302 	if (restore_sigcontext(env, &frame->uc.uc_mcontext))
1303 		goto badframe;
1304 
1305 #if 0
1306 	/* Send SIGTRAP if we're single-stepping */
1307 	if (ptrace_cancel_bpt(current))
1308 		send_sig(SIGTRAP, current, 1);
1309 #endif
1310 	return env->regs[0];
1311 
1312 badframe:
1313         force_sig(SIGSEGV /* , current */);
1314 	return 0;
1315 }
1316 
1317 #else
1318 
1319 static void setup_frame(int sig, struct emulated_sigaction *ka,
1320 			target_sigset_t *set, CPUState *env)
1321 {
1322     fprintf(stderr, "setup_frame: not implemented\n");
1323 }
1324 
1325 static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1326                            target_siginfo_t *info,
1327 			   target_sigset_t *set, CPUState *env)
1328 {
1329     fprintf(stderr, "setup_rt_frame: not implemented\n");
1330 }
1331 
1332 long do_sigreturn(CPUState *env)
1333 {
1334     fprintf(stderr, "do_sigreturn: not implemented\n");
1335     return -ENOSYS;
1336 }
1337 
1338 long do_rt_sigreturn(CPUState *env)
1339 {
1340     fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1341     return -ENOSYS;
1342 }
1343 
1344 #endif
1345 
1346 void process_pending_signals(void *cpu_env)
1347 {
1348     int sig;
1349     target_ulong handler;
1350     sigset_t set, old_set;
1351     target_sigset_t target_old_set;
1352     struct emulated_sigaction *k;
1353     struct sigqueue *q;
1354 
1355     if (!signal_pending)
1356         return;
1357 
1358     k = sigact_table;
1359     for(sig = 1; sig <= TARGET_NSIG; sig++) {
1360         if (k->pending)
1361             goto handle_signal;
1362         k++;
1363     }
1364     /* if no signal is pending, just return */
1365     signal_pending = 0;
1366     return;
1367 
1368  handle_signal:
1369 #ifdef DEBUG_SIGNAL
1370     fprintf(stderr, "qemu: process signal %d\n", sig);
1371 #endif
1372     /* dequeue signal */
1373     q = k->first;
1374     k->first = q->next;
1375     if (!k->first)
1376         k->pending = 0;
1377 
1378     handler = k->sa._sa_handler;
1379     if (handler == TARGET_SIG_DFL) {
1380         /* default handler : ignore some signal. The other are fatal */
1381         if (sig != TARGET_SIGCHLD &&
1382             sig != TARGET_SIGURG &&
1383             sig != TARGET_SIGWINCH) {
1384             force_sig(sig);
1385         }
1386     } else if (handler == TARGET_SIG_IGN) {
1387         /* ignore sig */
1388     } else if (handler == TARGET_SIG_ERR) {
1389         force_sig(sig);
1390     } else {
1391         /* compute the blocked signals during the handler execution */
1392         target_to_host_sigset(&set, &k->sa.sa_mask);
1393         /* SA_NODEFER indicates that the current signal should not be
1394            blocked during the handler */
1395         if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
1396             sigaddset(&set, target_to_host_signal(sig));
1397 
1398         /* block signals in the handler using Linux */
1399         sigprocmask(SIG_BLOCK, &set, &old_set);
1400         /* save the previous blocked signal state to restore it at the
1401            end of the signal execution (see do_sigreturn) */
1402         host_to_target_sigset(&target_old_set, &old_set);
1403 
1404         /* if the CPU is in VM86 mode, we restore the 32 bit values */
1405 #ifdef TARGET_I386
1406         {
1407             CPUX86State *env = cpu_env;
1408             if (env->eflags & VM_MASK)
1409                 save_v86_state(env);
1410         }
1411 #endif
1412         /* prepare the stack frame of the virtual CPU */
1413         if (k->sa.sa_flags & TARGET_SA_SIGINFO)
1414             setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
1415         else
1416             setup_frame(sig, k, &target_old_set, cpu_env);
1417 	if (k->sa.sa_flags & TARGET_SA_RESETHAND)
1418             k->sa._sa_handler = TARGET_SIG_DFL;
1419     }
1420     if (q != &k->info)
1421         free_sigqueue(q);
1422 }
1423 
1424 
1425