Annotation of qemu/linux-user/signal.c, revision 1.1.1.5

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.