~jderose/ubuntu/raring/qemu/vde-again

« back to all changes in this revision

Viewing changes to linux-user/signal.c

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno, Aurelien Jarno
  • Date: 2009-03-22 10:13:17 UTC
  • mfrom: (1.2.1 upstream) (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090322101317-iigjtnu5qil35dtb
Tags: 0.10.1-1
[ Aurelien Jarno ]
* New upstream stable release:
  - patches/80_stable-branch.patch: remove.
* debian/control: 
  - Remove depends on proll.
  - Move depends on device-tree-compiler to build-depends.
  - Bump Standards-Version to 3.8.1 (no changes).
* patches/82_qemu-img_decimal.patch: new patch from upstream to make
  qemu-img accept sizes with decimal values (closes: bug#501400).

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 *
16
16
 *  You should have received a copy of the GNU General Public License
17
17
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 *  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
 
19
 *  MA 02110-1301, USA.
19
20
 */
20
21
#include <stdlib.h>
21
22
#include <stdio.h>
27
28
#include <sys/ucontext.h>
28
29
 
29
30
#include "qemu.h"
 
31
#include "qemu-common.h"
30
32
#include "target_signal.h"
31
33
 
32
34
//#define DEBUG_SIGNAL
33
35
 
34
 
#define MAX_SIGQUEUE_SIZE 1024
35
 
 
36
 
struct sigqueue {
37
 
    struct sigqueue *next;
38
 
    target_siginfo_t info;
39
 
};
40
 
 
41
 
struct emulated_sigaction {
42
 
    struct target_sigaction sa;
43
 
    int pending; /* true if signal is pending */
44
 
    struct sigqueue *first;
45
 
    struct sigqueue info; /* in order to always have memory for the
46
 
                             first signal, we put it here */
47
 
};
48
 
 
49
 
struct target_sigaltstack target_sigaltstack_used = {
 
36
static struct target_sigaltstack target_sigaltstack_used = {
50
37
    .ss_sp = 0,
51
38
    .ss_size = 0,
52
39
    .ss_flags = TARGET_SS_DISABLE,
53
40
};
54
41
 
55
 
static struct emulated_sigaction sigact_table[TARGET_NSIG];
56
 
static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57
 
static struct sigqueue *first_free; /* first free siginfo queue entry */
58
 
static int signal_pending; /* non zero if a signal may be pending */
 
42
static struct target_sigaction sigact_table[TARGET_NSIG];
59
43
 
60
44
static void host_signal_handler(int host_signum, siginfo_t *info,
61
45
                                void *puc);
96
80
    [SIGPWR] = TARGET_SIGPWR,
97
81
    [SIGSYS] = TARGET_SIGSYS,
98
82
    /* next signals stay the same */
 
83
    /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
 
84
       host libpthread signals.  This assumes noone actually uses SIGRTMAX :-/
 
85
       To fix this properly we need to do manual signal delivery multiplexed
 
86
       over a single host signal.  */
 
87
    [__SIGRTMIN] = __SIGRTMAX,
 
88
    [__SIGRTMAX] = __SIGRTMIN,
99
89
};
100
90
static uint8_t target_to_host_signal_table[65];
101
91
 
113
103
 
114
104
static inline int host_to_target_signal(int sig)
115
105
{
 
106
    if (sig > 64)
 
107
        return sig;
116
108
    return host_to_target_signal_table[sig];
117
109
}
118
110
 
119
 
static inline int target_to_host_signal(int sig)
 
111
int target_to_host_signal(int sig)
120
112
{
 
113
    if (sig > 64)
 
114
        return sig;
121
115
    return target_to_host_signal_table[sig];
122
116
}
123
117
 
 
118
static inline void target_sigemptyset(target_sigset_t *set)
 
119
{
 
120
    memset(set, 0, sizeof(*set));
 
121
}
 
122
 
 
123
static inline void target_sigaddset(target_sigset_t *set, int signum)
 
124
{
 
125
    signum--;
 
126
    abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
 
127
    set->sig[signum / TARGET_NSIG_BPW] |= mask;
 
128
}
 
129
 
 
130
static inline int target_sigismember(const target_sigset_t *set, int signum)
 
131
{
 
132
    signum--;
 
133
    abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
 
134
    return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
 
135
}
 
136
 
124
137
static void host_to_target_sigset_internal(target_sigset_t *d,
125
138
                                           const sigset_t *s)
126
139
{
127
140
    int i;
128
 
    unsigned long sigmask;
129
 
    uint32_t target_sigmask;
130
 
 
131
 
    sigmask = ((unsigned long *)s)[0];
132
 
    target_sigmask = 0;
133
 
    for(i = 0; i < 32; i++) {
134
 
        if (sigmask & (1 << i))
135
 
            target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
136
 
    }
137
 
#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
138
 
    d->sig[0] = target_sigmask;
139
 
    for(i = 1;i < TARGET_NSIG_WORDS; i++) {
140
 
        d->sig[i] = ((unsigned long *)s)[i];
141
 
    }
142
 
#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
143
 
    d->sig[0] = target_sigmask;
144
 
    d->sig[1] = sigmask >> 32;
145
 
#else
146
 
    /* XXX: do it */
147
 
#endif
 
141
    target_sigemptyset(d);
 
142
    for (i = 1; i <= TARGET_NSIG; i++) {
 
143
        if (sigismember(s, i)) {
 
144
            target_sigaddset(d, host_to_target_signal(i));
 
145
        }
 
146
    }
148
147
}
149
148
 
150
149
void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
157
156
        d->sig[i] = tswapl(d1.sig[i]);
158
157
}
159
158
 
160
 
void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
 
159
static void target_to_host_sigset_internal(sigset_t *d,
 
160
                                           const target_sigset_t *s)
161
161
{
162
162
    int i;
163
 
    unsigned long sigmask;
164
 
    abi_ulong target_sigmask;
165
 
 
166
 
    target_sigmask = s->sig[0];
167
 
    sigmask = 0;
168
 
    for(i = 0; i < 32; i++) {
169
 
        if (target_sigmask & (1 << i))
170
 
            sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
171
 
    }
172
 
#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
173
 
    ((unsigned long *)d)[0] = sigmask;
174
 
    for(i = 1;i < TARGET_NSIG_WORDS; i++) {
175
 
        ((unsigned long *)d)[i] = s->sig[i];
176
 
    }
177
 
#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
178
 
    ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
179
 
#else
180
 
    /* XXX: do it */
181
 
#endif /* TARGET_ABI_BITS */
 
163
    sigemptyset(d);
 
164
    for (i = 1; i <= TARGET_NSIG; i++) {
 
165
        if (target_sigismember(s, i)) {
 
166
            sigaddset(d, target_to_host_signal(i));
 
167
        }
 
168
     }
182
169
}
183
170
 
184
171
void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
220
207
    sig = host_to_target_signal(info->si_signo);
221
208
    tinfo->si_signo = sig;
222
209
    tinfo->si_errno = 0;
223
 
    tinfo->si_code = 0;
 
210
    tinfo->si_code = info->si_code;
224
211
    if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
225
212
        sig == SIGBUS || sig == SIGTRAP) {
226
213
        /* should never come here, but who knows. The information for
279
266
            (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
280
267
}
281
268
 
 
269
static int fatal_signal (int sig)
 
270
{
 
271
    switch (sig) {
 
272
    case TARGET_SIGCHLD:
 
273
    case TARGET_SIGURG:
 
274
    case TARGET_SIGWINCH:
 
275
        /* Ignored by default.  */
 
276
        return 0;
 
277
    case TARGET_SIGCONT:
 
278
    case TARGET_SIGSTOP:
 
279
    case TARGET_SIGTSTP:
 
280
    case TARGET_SIGTTIN:
 
281
    case TARGET_SIGTTOU:
 
282
        /* Job control signals.  */
 
283
        return 0;
 
284
    default:
 
285
        return 1;
 
286
    }
 
287
}
 
288
 
282
289
void signal_init(void)
283
290
{
284
291
    struct sigaction act;
 
292
    struct sigaction oact;
285
293
    int i, j;
 
294
    int host_sig;
286
295
 
287
296
    /* generate signal conversion tables */
288
297
    for(i = 1; i <= 64; i++) {
296
305
 
297
306
    /* set all host signal handlers. ALL signals are blocked during
298
307
       the handlers to serialize them. */
 
308
    memset(sigact_table, 0, sizeof(sigact_table));
 
309
 
299
310
    sigfillset(&act.sa_mask);
300
311
    act.sa_flags = SA_SIGINFO;
301
312
    act.sa_sigaction = host_signal_handler;
302
 
    for(i = 1; i < NSIG; i++) {
303
 
        sigaction(i, &act, NULL);
 
313
    for(i = 1; i <= TARGET_NSIG; i++) {
 
314
        host_sig = target_to_host_signal(i);
 
315
        sigaction(host_sig, NULL, &oact);
 
316
        if (oact.sa_sigaction == (void *)SIG_IGN) {
 
317
            sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
 
318
        } else if (oact.sa_sigaction == (void *)SIG_DFL) {
 
319
            sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
 
320
        }
 
321
        /* If there's already a handler installed then something has
 
322
           gone horribly wrong, so don't even try to handle that case.  */
 
323
        /* Install some handlers for our own use.  We need at least
 
324
           SIGSEGV and SIGBUS, to detect exceptions.  We can not just
 
325
           trap all signals because it affects syscall interrupt
 
326
           behavior.  But do trap all default-fatal signals.  */
 
327
        if (fatal_signal (i))
 
328
            sigaction(host_sig, &act, NULL);
304
329
    }
305
 
 
306
 
    memset(sigact_table, 0, sizeof(sigact_table));
307
 
 
308
 
    first_free = &sigqueue_table[0];
309
 
    for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
310
 
        sigqueue_table[i].next = &sigqueue_table[i + 1];
311
 
    sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
312
330
}
313
331
 
314
332
/* signal queue handling */
315
333
 
316
 
static inline struct sigqueue *alloc_sigqueue(void)
 
334
static inline struct sigqueue *alloc_sigqueue(CPUState *env)
317
335
{
318
 
    struct sigqueue *q = first_free;
 
336
    TaskState *ts = env->opaque;
 
337
    struct sigqueue *q = ts->first_free;
319
338
    if (!q)
320
339
        return NULL;
321
 
    first_free = q->next;
 
340
    ts->first_free = q->next;
322
341
    return q;
323
342
}
324
343
 
325
 
static inline void free_sigqueue(struct sigqueue *q)
 
344
static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
326
345
{
327
 
    q->next = first_free;
328
 
    first_free = q;
 
346
    TaskState *ts = env->opaque;
 
347
    q->next = ts->first_free;
 
348
    ts->first_free = q;
329
349
}
330
350
 
331
351
/* abort execution with signal */
332
 
void __attribute((noreturn)) force_sig(int sig)
 
352
static void QEMU_NORETURN force_sig(int sig)
333
353
{
334
354
    int host_sig;
335
355
    host_sig = target_to_host_signal(sig);
336
356
    fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
337
357
            sig, strsignal(host_sig));
338
358
#if 1
 
359
    gdb_signalled(thread_env, sig);
339
360
    _exit(-host_sig);
340
361
#else
341
362
    {
351
372
 
352
373
/* queue a signal so that it will be send to the virtual CPU as soon
353
374
   as possible */
354
 
int queue_signal(int sig, target_siginfo_t *info)
 
375
int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
355
376
{
356
 
    struct emulated_sigaction *k;
 
377
    TaskState *ts = env->opaque;
 
378
    struct emulated_sigtable *k;
357
379
    struct sigqueue *q, **pq;
358
380
    abi_ulong handler;
 
381
    int queue;
359
382
 
360
383
#if defined(DEBUG_SIGNAL)
361
384
    fprintf(stderr, "queue_signal: sig=%d\n",
362
385
            sig);
363
386
#endif
364
 
    k = &sigact_table[sig - 1];
365
 
    handler = k->sa._sa_handler;
366
 
    if (handler == TARGET_SIG_DFL) {
 
387
    k = &ts->sigtab[sig - 1];
 
388
    queue = gdb_queuesig ();
 
389
    handler = sigact_table[sig - 1]._sa_handler;
 
390
    if (!queue && handler == TARGET_SIG_DFL) {
 
391
        if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
 
392
            kill(getpid(),SIGSTOP);
 
393
            return 0;
 
394
        } else
367
395
        /* default handler : ignore some signal. The other are fatal */
368
396
        if (sig != TARGET_SIGCHLD &&
369
397
            sig != TARGET_SIGURG &&
370
 
            sig != TARGET_SIGWINCH) {
 
398
            sig != TARGET_SIGWINCH &&
 
399
            sig != TARGET_SIGCONT) {
371
400
            force_sig(sig);
372
401
        } else {
373
402
            return 0; /* indicate ignored */
374
403
        }
375
 
    } else if (handler == TARGET_SIG_IGN) {
 
404
    } else if (!queue && handler == TARGET_SIG_IGN) {
376
405
        /* ignore signal */
377
406
        return 0;
378
 
    } else if (handler == TARGET_SIG_ERR) {
 
407
    } else if (!queue && handler == TARGET_SIG_ERR) {
379
408
        force_sig(sig);
380
409
    } else {
381
410
        pq = &k->first;
390
419
                /* first signal */
391
420
                q = &k->info;
392
421
            } else {
393
 
                q = alloc_sigqueue();
 
422
                q = alloc_sigqueue(env);
394
423
                if (!q)
395
424
                    return -EAGAIN;
396
425
                while (*pq != NULL)
402
431
        q->next = NULL;
403
432
        k->pending = 1;
404
433
        /* signal that a new signal is pending */
405
 
        signal_pending = 1;
 
434
        ts->signal_pending = 1;
406
435
        return 1; /* indicates that the signal was queued */
407
436
    }
408
437
}
414
443
    target_siginfo_t tinfo;
415
444
 
416
445
    /* the CPU emulator uses some host signals to detect exceptions,
417
 
       we we forward to it some signals */
418
 
    if (host_signum == SIGSEGV || host_signum == SIGBUS) {
 
446
       we forward to it some signals */
 
447
    if ((host_signum == SIGSEGV || host_signum == SIGBUS)
 
448
        && info->si_code > 0) {
419
449
        if (cpu_signal_handler(host_signum, info, puc))
420
450
            return;
421
451
    }
428
458
    fprintf(stderr, "qemu: got signal %d\n", sig);
429
459
#endif
430
460
    host_to_target_siginfo_noswap(&tinfo, info);
431
 
    if (queue_signal(sig, &tinfo) == 1) {
 
461
    if (queue_signal(thread_env, sig, &tinfo) == 1) {
432
462
        /* interrupt the virtual CPU as soon as possible */
433
 
        cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
 
463
        cpu_interrupt(thread_env, CPU_INTERRUPT_EXIT);
434
464
    }
435
465
}
436
466
 
500
530
int do_sigaction(int sig, const struct target_sigaction *act,
501
531
                 struct target_sigaction *oact)
502
532
{
503
 
    struct emulated_sigaction *k;
 
533
    struct target_sigaction *k;
504
534
    struct sigaction act1;
505
535
    int host_sig;
506
536
    int ret = 0;
507
537
 
508
 
    if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
 
538
    if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
509
539
        return -EINVAL;
510
540
    k = &sigact_table[sig - 1];
511
541
#if defined(DEBUG_SIGNAL)
513
543
            sig, (int)act, (int)oact);
514
544
#endif
515
545
    if (oact) {
516
 
        oact->_sa_handler = tswapl(k->sa._sa_handler);
517
 
        oact->sa_flags = tswapl(k->sa.sa_flags);
 
546
        oact->_sa_handler = tswapl(k->_sa_handler);
 
547
        oact->sa_flags = tswapl(k->sa_flags);
518
548
#if !defined(TARGET_MIPS)
519
 
        oact->sa_restorer = tswapl(k->sa.sa_restorer);
 
549
        oact->sa_restorer = tswapl(k->sa_restorer);
520
550
#endif
521
 
        oact->sa_mask = k->sa.sa_mask;
 
551
        oact->sa_mask = k->sa_mask;
522
552
    }
523
553
    if (act) {
524
 
        k->sa._sa_handler = tswapl(act->_sa_handler);
525
 
        k->sa.sa_flags = tswapl(act->sa_flags);
 
554
        /* FIXME: This is not threadsafe.  */
 
555
        k->_sa_handler = tswapl(act->_sa_handler);
 
556
        k->sa_flags = tswapl(act->sa_flags);
526
557
#if !defined(TARGET_MIPS)
527
 
        k->sa.sa_restorer = tswapl(act->sa_restorer);
 
558
        k->sa_restorer = tswapl(act->sa_restorer);
528
559
#endif
529
 
        k->sa.sa_mask = act->sa_mask;
 
560
        k->sa_mask = act->sa_mask;
530
561
 
531
562
        /* we update the host linux signal state */
532
563
        host_sig = target_to_host_signal(sig);
533
564
        if (host_sig != SIGSEGV && host_sig != SIGBUS) {
534
565
            sigfillset(&act1.sa_mask);
535
566
            act1.sa_flags = SA_SIGINFO;
536
 
            if (k->sa.sa_flags & TARGET_SA_RESTART)
 
567
            if (k->sa_flags & TARGET_SA_RESTART)
537
568
                act1.sa_flags |= SA_RESTART;
538
569
            /* NOTE: it is important to update the host kernel signal
539
570
               ignore state to avoid getting unexpected interrupted
540
571
               syscalls */
541
 
            if (k->sa._sa_handler == TARGET_SIG_IGN) {
 
572
            if (k->_sa_handler == TARGET_SIG_IGN) {
542
573
                act1.sa_sigaction = (void *)SIG_IGN;
543
 
            } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
544
 
                act1.sa_sigaction = (void *)SIG_DFL;
 
574
            } else if (k->_sa_handler == TARGET_SIG_DFL) {
 
575
                if (fatal_signal (sig))
 
576
                    act1.sa_sigaction = host_signal_handler;
 
577
                else
 
578
                    act1.sa_sigaction = (void *)SIG_DFL;
545
579
            } else {
546
580
                act1.sa_sigaction = host_signal_handler;
547
581
            }
551
585
    return ret;
552
586
}
553
587
 
554
 
#ifndef offsetof
555
 
#define offsetof(type, field) ((size_t) &((type *)0)->field)
556
 
#endif
557
 
 
558
588
static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
559
589
                                       const target_siginfo_t *info)
560
590
{
716
746
 */
717
747
 
718
748
static inline abi_ulong
719
 
get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
 
749
get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
720
750
{
721
751
        unsigned long esp;
722
752
 
723
753
        /* Default to using normal stack */
724
754
        esp = env->regs[R_ESP];
725
755
        /* This is the X/Open sanctioned signal stack switching.  */
726
 
        if (ka->sa.sa_flags & TARGET_SA_ONSTACK) {
 
756
        if (ka->sa_flags & TARGET_SA_ONSTACK) {
727
757
            if (sas_ss_flags(esp) == 0)
728
758
                esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
729
759
        }
731
761
        /* This is the legacy signal stack switching. */
732
762
        else
733
763
        if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
734
 
            !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
735
 
            ka->sa.sa_restorer) {
736
 
            esp = (unsigned long) ka->sa.sa_restorer;
 
764
            !(ka->sa_flags & TARGET_SA_RESTORER) &&
 
765
            ka->sa_restorer) {
 
766
            esp = (unsigned long) ka->sa_restorer;
737
767
        }
738
768
        return (esp - frame_size) & -8ul;
739
769
}
740
770
 
741
771
/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
742
 
static void setup_frame(int sig, struct emulated_sigaction *ka,
 
772
static void setup_frame(int sig, struct target_sigaction *ka,
743
773
                        target_sigset_t *set, CPUX86State *env)
744
774
{
745
775
        abi_ulong frame_addr;
768
798
 
769
799
        /* Set up to return from userspace.  If provided, use a stub
770
800
           already in userspace.  */
771
 
        if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
772
 
                err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
 
801
        if (ka->sa_flags & TARGET_SA_RESTORER) {
 
802
                err |= __put_user(ka->sa_restorer, &frame->pretcode);
773
803
        } else {
774
804
                uint16_t val16;
775
805
                abi_ulong retcode_addr;
788
818
 
789
819
        /* Set up registers for signal handler */
790
820
        env->regs[R_ESP] = frame_addr;
791
 
        env->eip = ka->sa._sa_handler;
 
821
        env->eip = ka->_sa_handler;
792
822
 
793
823
        cpu_x86_load_seg(env, R_DS, __USER_DS);
794
824
        cpu_x86_load_seg(env, R_ES, __USER_DS);
803
833
give_sigsegv:
804
834
        unlock_user_struct(frame, frame_addr, 1);
805
835
        if (sig == TARGET_SIGSEGV)
806
 
                ka->sa._sa_handler = TARGET_SIG_DFL;
 
836
                ka->_sa_handler = TARGET_SIG_DFL;
807
837
        force_sig(TARGET_SIGSEGV /* , current */);
808
838
}
809
839
 
810
840
/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
811
 
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
 
841
static void setup_rt_frame(int sig, struct target_sigaction *ka,
812
842
                           target_siginfo_t *info,
813
843
                           target_sigset_t *set, CPUX86State *env)
814
844
{
850
880
 
851
881
        /* Set up to return from userspace.  If provided, use a stub
852
882
           already in userspace.  */
853
 
        if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
854
 
                err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
 
883
        if (ka->sa_flags & TARGET_SA_RESTORER) {
 
884
                err |= __put_user(ka->sa_restorer, &frame->pretcode);
855
885
        } else {
856
886
                uint16_t val16;
857
887
                addr = frame_addr + offsetof(struct rt_sigframe, retcode);
868
898
 
869
899
        /* Set up registers for signal handler */
870
900
        env->regs[R_ESP] = frame_addr;
871
 
        env->eip = ka->sa._sa_handler;
 
901
        env->eip = ka->_sa_handler;
872
902
 
873
903
        cpu_x86_load_seg(env, R_DS, __USER_DS);
874
904
        cpu_x86_load_seg(env, R_ES, __USER_DS);
883
913
give_sigsegv:
884
914
        unlock_user_struct(frame, frame_addr, 1);
885
915
        if (sig == TARGET_SIGSEGV)
886
 
                ka->sa._sa_handler = TARGET_SIG_DFL;
 
916
                ka->_sa_handler = TARGET_SIG_DFL;
887
917
        force_sig(TARGET_SIGSEGV /* , current */);
888
918
}
889
919
 
1020
1050
        abi_ulong fault_address;
1021
1051
};
1022
1052
 
1023
 
struct target_ucontext {
1024
 
    abi_ulong tuc_flags;
1025
 
    abi_ulong tuc_link;
1026
 
    target_stack_t tuc_stack;
1027
 
    struct target_sigcontext tuc_mcontext;
1028
 
    target_sigset_t  tuc_sigmask;       /* mask last for extensibility */
1029
 
};
1030
 
 
1031
 
struct sigframe
 
1053
struct target_ucontext_v1 {
 
1054
    abi_ulong tuc_flags;
 
1055
    abi_ulong tuc_link;
 
1056
    target_stack_t tuc_stack;
 
1057
    struct target_sigcontext tuc_mcontext;
 
1058
    target_sigset_t  tuc_sigmask;       /* mask last for extensibility */
 
1059
};
 
1060
 
 
1061
struct target_ucontext_v2 {
 
1062
    abi_ulong tuc_flags;
 
1063
    abi_ulong tuc_link;
 
1064
    target_stack_t tuc_stack;
 
1065
    struct target_sigcontext tuc_mcontext;
 
1066
    target_sigset_t  tuc_sigmask;       /* mask last for extensibility */
 
1067
    char __unused[128 - sizeof(sigset_t)];
 
1068
    abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
 
1069
};
 
1070
 
 
1071
struct sigframe_v1
1032
1072
{
1033
1073
    struct target_sigcontext sc;
1034
1074
    abi_ulong extramask[TARGET_NSIG_WORDS-1];
1035
1075
    abi_ulong retcode;
1036
1076
};
1037
1077
 
1038
 
struct rt_sigframe
 
1078
struct sigframe_v2
 
1079
{
 
1080
    struct target_ucontext_v2 uc;
 
1081
    abi_ulong retcode;
 
1082
};
 
1083
 
 
1084
struct rt_sigframe_v1
1039
1085
{
1040
1086
    abi_ulong pinfo;
1041
1087
    abi_ulong puc;
1042
1088
    struct target_siginfo info;
1043
 
    struct target_ucontext uc;
 
1089
    struct target_ucontext_v1 uc;
 
1090
    abi_ulong retcode;
 
1091
};
 
1092
 
 
1093
struct rt_sigframe_v2
 
1094
{
 
1095
    struct target_siginfo info;
 
1096
    struct target_ucontext_v2 uc;
1044
1097
    abi_ulong retcode;
1045
1098
};
1046
1099
 
1065
1118
};
1066
1119
 
1067
1120
 
1068
 
#define __put_user_error(x,p,e) __put_user(x, p)
1069
1121
#define __get_user_error(x,p,e) __get_user(x, p)
1070
1122
 
1071
1123
static inline int valid_user_regs(CPUState *regs)
1073
1125
    return 1;
1074
1126
}
1075
1127
 
1076
 
static int
 
1128
static void
1077
1129
setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1078
1130
                 CPUState *env, abi_ulong mask)
1079
1131
{
1080
 
        int err = 0;
1081
 
 
1082
 
        __put_user_error(env->regs[0], &sc->arm_r0, err);
1083
 
        __put_user_error(env->regs[1], &sc->arm_r1, err);
1084
 
        __put_user_error(env->regs[2], &sc->arm_r2, err);
1085
 
        __put_user_error(env->regs[3], &sc->arm_r3, err);
1086
 
        __put_user_error(env->regs[4], &sc->arm_r4, err);
1087
 
        __put_user_error(env->regs[5], &sc->arm_r5, err);
1088
 
        __put_user_error(env->regs[6], &sc->arm_r6, err);
1089
 
        __put_user_error(env->regs[7], &sc->arm_r7, err);
1090
 
        __put_user_error(env->regs[8], &sc->arm_r8, err);
1091
 
        __put_user_error(env->regs[9], &sc->arm_r9, err);
1092
 
        __put_user_error(env->regs[10], &sc->arm_r10, err);
1093
 
        __put_user_error(env->regs[11], &sc->arm_fp, err);
1094
 
        __put_user_error(env->regs[12], &sc->arm_ip, err);
1095
 
        __put_user_error(env->regs[13], &sc->arm_sp, err);
1096
 
        __put_user_error(env->regs[14], &sc->arm_lr, err);
1097
 
        __put_user_error(env->regs[15], &sc->arm_pc, err);
 
1132
        __put_user(env->regs[0], &sc->arm_r0);
 
1133
        __put_user(env->regs[1], &sc->arm_r1);
 
1134
        __put_user(env->regs[2], &sc->arm_r2);
 
1135
        __put_user(env->regs[3], &sc->arm_r3);
 
1136
        __put_user(env->regs[4], &sc->arm_r4);
 
1137
        __put_user(env->regs[5], &sc->arm_r5);
 
1138
        __put_user(env->regs[6], &sc->arm_r6);
 
1139
        __put_user(env->regs[7], &sc->arm_r7);
 
1140
        __put_user(env->regs[8], &sc->arm_r8);
 
1141
        __put_user(env->regs[9], &sc->arm_r9);
 
1142
        __put_user(env->regs[10], &sc->arm_r10);
 
1143
        __put_user(env->regs[11], &sc->arm_fp);
 
1144
        __put_user(env->regs[12], &sc->arm_ip);
 
1145
        __put_user(env->regs[13], &sc->arm_sp);
 
1146
        __put_user(env->regs[14], &sc->arm_lr);
 
1147
        __put_user(env->regs[15], &sc->arm_pc);
1098
1148
#ifdef TARGET_CONFIG_CPU_32
1099
 
        __put_user_error(cpsr_read(env), &sc->arm_cpsr, err);
 
1149
        __put_user(cpsr_read(env), &sc->arm_cpsr);
1100
1150
#endif
1101
1151
 
1102
 
        __put_user_error(/* current->thread.trap_no */ 0, &sc->trap_no, err);
1103
 
        __put_user_error(/* current->thread.error_code */ 0, &sc->error_code, err);
1104
 
        __put_user_error(/* current->thread.address */ 0, &sc->fault_address, err);
1105
 
        __put_user_error(mask, &sc->oldmask, err);
1106
 
 
1107
 
        return err;
 
1152
        __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
 
1153
        __put_user(/* current->thread.error_code */ 0, &sc->error_code);
 
1154
        __put_user(/* current->thread.address */ 0, &sc->fault_address);
 
1155
        __put_user(mask, &sc->oldmask);
1108
1156
}
1109
1157
 
1110
1158
static inline abi_ulong
1111
 
get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
 
1159
get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
1112
1160
{
1113
1161
        unsigned long sp = regs->regs[13];
1114
1162
 
1115
1163
        /*
1116
1164
         * This is the X/Open sanctioned signal stack switching.
1117
1165
         */
1118
 
        if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
 
1166
        if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1119
1167
            sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1120
1168
        /*
1121
1169
         * ATPCS B01 mandates 8-byte alignment
1124
1172
}
1125
1173
 
1126
1174
static int
1127
 
setup_return(CPUState *env, struct emulated_sigaction *ka,
 
1175
setup_return(CPUState *env, struct target_sigaction *ka,
1128
1176
             abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1129
1177
{
1130
 
        abi_ulong handler = ka->sa._sa_handler;
 
1178
        abi_ulong handler = ka->_sa_handler;
1131
1179
        abi_ulong retcode;
1132
 
        int thumb = 0;
1133
 
#if defined(TARGET_CONFIG_CPU_32)
1134
 
#if 0
1135
 
        abi_ulong cpsr = env->cpsr;
1136
 
 
1137
 
        /*
1138
 
         * Maybe we need to deliver a 32-bit signal to a 26-bit task.
1139
 
         */
1140
 
        if (ka->sa.sa_flags & SA_THIRTYTWO)
1141
 
                cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
1142
 
 
1143
 
#ifdef CONFIG_ARM_THUMB
1144
 
        if (elf_hwcap & HWCAP_THUMB) {
1145
 
                /*
1146
 
                 * The LSB of the handler determines if we're going to
1147
 
                 * be using THUMB or ARM mode for this signal handler.
1148
 
                 */
1149
 
                thumb = handler & 1;
1150
 
 
1151
 
                if (thumb)
1152
 
                        cpsr |= T_BIT;
1153
 
                else
1154
 
                        cpsr &= ~T_BIT;
1155
 
        }
1156
 
#endif /* CONFIG_ARM_THUMB */
1157
 
#endif /* 0 */
1158
 
#endif /* TARGET_CONFIG_CPU_32 */
1159
 
 
1160
 
        if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
1161
 
                retcode = ka->sa.sa_restorer;
 
1180
        int thumb = handler & 1;
 
1181
 
 
1182
        if (ka->sa_flags & TARGET_SA_RESTORER) {
 
1183
                retcode = ka->sa_restorer;
1162
1184
        } else {
1163
1185
                unsigned int idx = thumb;
1164
1186
 
1165
 
                if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
 
1187
                if (ka->sa_flags & TARGET_SA_SIGINFO)
1166
1188
                        idx += 2;
1167
1189
 
1168
1190
                if (__put_user(retcodes[idx], rc))
1178
1200
        env->regs[13] = frame_addr;
1179
1201
        env->regs[14] = retcode;
1180
1202
        env->regs[15] = handler & (thumb ? ~1 : ~3);
 
1203
        env->thumb = thumb;
1181
1204
 
1182
1205
#if 0
1183
1206
#ifdef TARGET_CONFIG_CPU_32
1188
1211
        return 0;
1189
1212
}
1190
1213
 
 
1214
static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
 
1215
                              target_sigset_t *set, CPUState *env)
 
1216
{
 
1217
    struct target_sigaltstack stack;
 
1218
    int i;
 
1219
 
 
1220
    /* Clear all the bits of the ucontext we don't use.  */
 
1221
    memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
 
1222
 
 
1223
    memset(&stack, 0, sizeof(stack));
 
1224
    __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
 
1225
    __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
 
1226
    __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
 
1227
    memcpy(&uc->tuc_stack, &stack, sizeof(stack));
 
1228
 
 
1229
    setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
 
1230
    /* FIXME: Save coprocessor signal frame.  */
 
1231
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
 
1232
        __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
 
1233
    }
 
1234
}
 
1235
 
1191
1236
/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1192
 
static void setup_frame(int usig, struct emulated_sigaction *ka,
1193
 
                        target_sigset_t *set, CPUState *regs)
 
1237
static void setup_frame_v1(int usig, struct target_sigaction *ka,
 
1238
                           target_sigset_t *set, CPUState *regs)
1194
1239
{
1195
 
        struct sigframe *frame;
 
1240
        struct sigframe_v1 *frame;
1196
1241
        abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1197
 
        int i, err = 0;
 
1242
        int i;
1198
1243
 
1199
1244
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1200
1245
                return;
1201
1246
 
1202
 
        err |= setup_sigcontext(&frame->sc, /*&frame->fpstate,*/ regs, set->sig[0]);
 
1247
        setup_sigcontext(&frame->sc, regs, set->sig[0]);
1203
1248
 
1204
1249
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1205
1250
            if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1206
1251
                goto end;
1207
1252
        }
1208
1253
 
1209
 
        if (err == 0)
1210
 
                err = setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1211
 
                                   frame_addr + offsetof(struct sigframe, retcode));
 
1254
        setup_return(regs, ka, &frame->retcode, frame_addr, usig,
 
1255
                     frame_addr + offsetof(struct sigframe_v1, retcode));
1212
1256
 
1213
1257
end:
1214
1258
        unlock_user_struct(frame, frame_addr, 1);
1215
 
        //      return err;
 
1259
}
 
1260
 
 
1261
static void setup_frame_v2(int usig, struct target_sigaction *ka,
 
1262
                           target_sigset_t *set, CPUState *regs)
 
1263
{
 
1264
        struct sigframe_v2 *frame;
 
1265
        abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
 
1266
 
 
1267
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
 
1268
                return;
 
1269
 
 
1270
        setup_sigframe_v2(&frame->uc, set, regs);
 
1271
 
 
1272
        setup_return(regs, ka, &frame->retcode, frame_addr, usig,
 
1273
                     frame_addr + offsetof(struct sigframe_v2, retcode));
 
1274
 
 
1275
        unlock_user_struct(frame, frame_addr, 1);
 
1276
}
 
1277
 
 
1278
static void setup_frame(int usig, struct target_sigaction *ka,
 
1279
                        target_sigset_t *set, CPUState *regs)
 
1280
{
 
1281
    if (get_osversion() >= 0x020612) {
 
1282
        setup_frame_v2(usig, ka, set, regs);
 
1283
    } else {
 
1284
        setup_frame_v1(usig, ka, set, regs);
 
1285
    }
1216
1286
}
1217
1287
 
1218
1288
/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1219
 
static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1220
 
                           target_siginfo_t *info,
1221
 
                           target_sigset_t *set, CPUState *env)
 
1289
static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
 
1290
                              target_siginfo_t *info,
 
1291
                              target_sigset_t *set, CPUState *env)
1222
1292
{
1223
 
        struct rt_sigframe *frame;
 
1293
        struct rt_sigframe_v1 *frame;
1224
1294
        abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1225
1295
        struct target_sigaltstack stack;
1226
 
        int i, err = 0;
 
1296
        int i;
1227
1297
        abi_ulong info_addr, uc_addr;
1228
1298
 
1229
1299
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1230
1300
            return /* 1 */;
1231
1301
 
1232
 
        info_addr = frame_addr + offsetof(struct rt_sigframe, info);
1233
 
        __put_user_error(info_addr, &frame->pinfo, err);
1234
 
        uc_addr = frame_addr + offsetof(struct rt_sigframe, uc);
1235
 
        __put_user_error(uc_addr, &frame->puc, err);
1236
 
        err |= copy_siginfo_to_user(&frame->info, info);
 
1302
        info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
 
1303
        __put_user(info_addr, &frame->pinfo);
 
1304
        uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
 
1305
        __put_user(uc_addr, &frame->puc);
 
1306
        copy_siginfo_to_user(&frame->info, info);
1237
1307
 
1238
1308
        /* Clear all the bits of the ucontext we don't use.  */
1239
 
        memset(&frame->uc, 0, offsetof(struct target_ucontext, tuc_mcontext));
 
1309
        memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1240
1310
 
1241
1311
        memset(&stack, 0, sizeof(stack));
1242
1312
        __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1244
1314
        __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1245
1315
        memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1246
1316
 
1247
 
        err |= setup_sigcontext(&frame->uc.tuc_mcontext, /*&frame->fpstate,*/
1248
 
                                env, set->sig[0]);
 
1317
        setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1249
1318
        for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1250
1319
            if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1251
1320
                goto end;
1252
1321
        }
1253
1322
 
1254
 
        if (err == 0)
1255
 
                err = setup_return(env, ka, &frame->retcode, frame_addr, usig,
1256
 
                                   frame_addr + offsetof(struct rt_sigframe, retcode));
 
1323
        setup_return(env, ka, &frame->retcode, frame_addr, usig,
 
1324
                     frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1257
1325
 
1258
 
        if (err == 0) {
1259
 
                /*
1260
 
                 * For realtime signals we must also set the second and third
1261
 
                 * arguments for the signal handler.
1262
 
                 *   -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
1263
 
                 */
1264
 
            env->regs[1] = info_addr;
1265
 
            env->regs[2] = uc_addr;
1266
 
        }
 
1326
        env->regs[1] = info_addr;
 
1327
        env->regs[2] = uc_addr;
1267
1328
 
1268
1329
end:
1269
1330
        unlock_user_struct(frame, frame_addr, 1);
1270
 
 
1271
 
        //      return err;
 
1331
}
 
1332
 
 
1333
static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
 
1334
                              target_siginfo_t *info,
 
1335
                              target_sigset_t *set, CPUState *env)
 
1336
{
 
1337
        struct rt_sigframe_v2 *frame;
 
1338
        abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
 
1339
        abi_ulong info_addr, uc_addr;
 
1340
 
 
1341
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
 
1342
            return /* 1 */;
 
1343
 
 
1344
        info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
 
1345
        uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
 
1346
        copy_siginfo_to_user(&frame->info, info);
 
1347
 
 
1348
        setup_sigframe_v2(&frame->uc, set, env);
 
1349
 
 
1350
        setup_return(env, ka, &frame->retcode, frame_addr, usig,
 
1351
                     frame_addr + offsetof(struct rt_sigframe_v2, retcode));
 
1352
 
 
1353
        env->regs[1] = info_addr;
 
1354
        env->regs[2] = uc_addr;
 
1355
 
 
1356
        unlock_user_struct(frame, frame_addr, 1);
 
1357
}
 
1358
 
 
1359
static void setup_rt_frame(int usig, struct target_sigaction *ka,
 
1360
                           target_siginfo_t *info,
 
1361
                           target_sigset_t *set, CPUState *env)
 
1362
{
 
1363
    if (get_osversion() >= 0x020612) {
 
1364
        setup_rt_frame_v2(usig, ka, info, set, env);
 
1365
    } else {
 
1366
        setup_rt_frame_v1(usig, ka, info, set, env);
 
1367
    }
1272
1368
}
1273
1369
 
1274
1370
static int
1295
1391
        __get_user_error(env->regs[15], &sc->arm_pc, err);
1296
1392
#ifdef TARGET_CONFIG_CPU_32
1297
1393
        __get_user_error(cpsr, &sc->arm_cpsr, err);
1298
 
        cpsr_write(env, cpsr, 0xffffffff);
 
1394
        cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1299
1395
#endif
1300
1396
 
1301
1397
        err |= !valid_user_regs(env);
1303
1399
        return err;
1304
1400
}
1305
1401
 
1306
 
long do_sigreturn(CPUState *env)
 
1402
static long do_sigreturn_v1(CPUState *env)
1307
1403
{
1308
1404
        abi_ulong frame_addr;
1309
 
        struct sigframe *frame;
 
1405
        struct sigframe_v1 *frame;
1310
1406
        target_sigset_t set;
1311
1407
        sigset_t host_set;
1312
1408
        int i;
1350
1446
        return 0;
1351
1447
}
1352
1448
 
1353
 
long do_rt_sigreturn(CPUState *env)
1354
 
{
1355
 
        abi_ulong frame_addr;
1356
 
        struct rt_sigframe *frame;
 
1449
static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
 
1450
                                 struct target_ucontext_v2 *uc)
 
1451
{
 
1452
    sigset_t host_set;
 
1453
 
 
1454
    target_to_host_sigset(&host_set, &uc->tuc_sigmask);
 
1455
    sigprocmask(SIG_SETMASK, &host_set, NULL);
 
1456
 
 
1457
    if (restore_sigcontext(env, &uc->tuc_mcontext))
 
1458
        return 1;
 
1459
 
 
1460
    if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
 
1461
        return 1;
 
1462
 
 
1463
#if 0
 
1464
    /* Send SIGTRAP if we're single-stepping */
 
1465
    if (ptrace_cancel_bpt(current))
 
1466
            send_sig(SIGTRAP, current, 1);
 
1467
#endif
 
1468
 
 
1469
    return 0;
 
1470
}
 
1471
 
 
1472
static long do_sigreturn_v2(CPUState *env)
 
1473
{
 
1474
        abi_ulong frame_addr;
 
1475
        struct sigframe_v2 *frame;
 
1476
 
 
1477
        /*
 
1478
         * Since we stacked the signal on a 64-bit boundary,
 
1479
         * then 'sp' should be word aligned here.  If it's
 
1480
         * not, then the user is trying to mess with us.
 
1481
         */
 
1482
        if (env->regs[13] & 7)
 
1483
                goto badframe;
 
1484
 
 
1485
        frame_addr = env->regs[13];
 
1486
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
 
1487
                goto badframe;
 
1488
 
 
1489
        if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
 
1490
                goto badframe;
 
1491
 
 
1492
        unlock_user_struct(frame, frame_addr, 0);
 
1493
        return env->regs[0];
 
1494
 
 
1495
badframe:
 
1496
        unlock_user_struct(frame, frame_addr, 0);
 
1497
        force_sig(SIGSEGV /* , current */);
 
1498
        return 0;
 
1499
}
 
1500
 
 
1501
long do_sigreturn(CPUState *env)
 
1502
{
 
1503
    if (get_osversion() >= 0x020612) {
 
1504
        return do_sigreturn_v2(env);
 
1505
    } else {
 
1506
        return do_sigreturn_v1(env);
 
1507
    }
 
1508
}
 
1509
 
 
1510
static long do_rt_sigreturn_v1(CPUState *env)
 
1511
{
 
1512
        abi_ulong frame_addr;
 
1513
        struct rt_sigframe_v1 *frame;
1357
1514
        sigset_t host_set;
1358
1515
 
1359
1516
        /*
1374
1531
        if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1375
1532
                goto badframe;
1376
1533
 
1377
 
        if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
 
1534
        if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1378
1535
                goto badframe;
1379
1536
 
1380
1537
#if 0
1391
1548
        return 0;
1392
1549
}
1393
1550
 
 
1551
static long do_rt_sigreturn_v2(CPUState *env)
 
1552
{
 
1553
        abi_ulong frame_addr;
 
1554
        struct rt_sigframe_v2 *frame;
 
1555
 
 
1556
        /*
 
1557
         * Since we stacked the signal on a 64-bit boundary,
 
1558
         * then 'sp' should be word aligned here.  If it's
 
1559
         * not, then the user is trying to mess with us.
 
1560
         */
 
1561
        if (env->regs[13] & 7)
 
1562
                goto badframe;
 
1563
 
 
1564
        frame_addr = env->regs[13];
 
1565
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
 
1566
                goto badframe;
 
1567
 
 
1568
        if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
 
1569
                goto badframe;
 
1570
 
 
1571
        unlock_user_struct(frame, frame_addr, 0);
 
1572
        return env->regs[0];
 
1573
 
 
1574
badframe:
 
1575
        unlock_user_struct(frame, frame_addr, 0);
 
1576
        force_sig(SIGSEGV /* , current */);
 
1577
        return 0;
 
1578
}
 
1579
 
 
1580
long do_rt_sigreturn(CPUState *env)
 
1581
{
 
1582
    if (get_osversion() >= 0x020612) {
 
1583
        return do_rt_sigreturn_v2(env);
 
1584
    } else {
 
1585
        return do_rt_sigreturn_v1(env);
 
1586
    }
 
1587
}
 
1588
 
1394
1589
#elif defined(TARGET_SPARC)
1395
1590
 
1396
1591
#define __SUNOS_MAXWIN   31
1489
1684
#define UREG_FP        UREG_I6
1490
1685
#define UREG_SP        UREG_O6
1491
1686
 
1492
 
static inline abi_ulong get_sigframe(struct emulated_sigaction *sa, 
 
1687
static inline abi_ulong get_sigframe(struct target_sigaction *sa, 
1493
1688
                                     CPUState *env, unsigned long framesize)
1494
1689
{
1495
1690
        abi_ulong sp;
1497
1692
        sp = env->regwptr[UREG_FP];
1498
1693
 
1499
1694
        /* This is the X/Open sanctioned signal stack switching.  */
1500
 
        if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
 
1695
        if (sa->sa_flags & TARGET_SA_ONSTACK) {
1501
1696
            if (!on_sig_stack(sp)
1502
1697
                && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1503
1698
                sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1544
1739
#endif
1545
1740
#define NF_ALIGNEDSZ  (((sizeof(struct target_signal_frame) + 7) & (~7)))
1546
1741
 
1547
 
static void setup_frame(int sig, struct emulated_sigaction *ka,
 
1742
static void setup_frame(int sig, struct target_sigaction *ka,
1548
1743
                        target_sigset_t *set, CPUState *env)
1549
1744
{
1550
1745
        abi_ulong sf_addr;
1597
1792
                offsetof(struct target_signal_frame, info);
1598
1793
 
1599
1794
        /* 4. signal handler */
1600
 
        env->pc = ka->sa._sa_handler;
 
1795
        env->pc = ka->_sa_handler;
1601
1796
        env->npc = (env->pc + 4);
1602
1797
        /* 5. return to kernel instructions */
1603
 
        if (ka->sa.sa_restorer)
1604
 
                env->regwptr[UREG_I7] = ka->sa.sa_restorer;
 
1798
        if (ka->sa_restorer)
 
1799
                env->regwptr[UREG_I7] = ka->sa_restorer;
1605
1800
        else {
1606
1801
                uint32_t val32;
1607
1802
 
1673
1868
}
1674
1869
 
1675
1870
 
1676
 
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
 
1871
static void setup_rt_frame(int sig, struct target_sigaction *ka,
1677
1872
                           target_siginfo_t *info,
1678
1873
                           target_sigset_t *set, CPUState *env)
1679
1874
{
2021
2216
 
2022
2217
# warning signal handling not implemented
2023
2218
 
2024
 
static void setup_frame(int sig, struct emulated_sigaction *ka,
 
2219
static void setup_frame(int sig, struct target_sigaction *ka,
2025
2220
                        target_sigset_t *set, CPUState *env)
2026
2221
{
2027
2222
    fprintf(stderr, "setup_frame: not implemented\n");
2028
2223
}
2029
2224
 
2030
 
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
 
2225
static void setup_rt_frame(int sig, struct target_sigaction *ka,
2031
2226
                           target_siginfo_t *info,
2032
2227
                           target_sigset_t *set, CPUState *env)
2033
2228
{
2050
2245
 
2051
2246
# warning signal handling not implemented
2052
2247
 
2053
 
static void setup_frame(int sig, struct emulated_sigaction *ka,
 
2248
static void setup_frame(int sig, struct target_sigaction *ka,
2054
2249
                        target_sigset_t *set, CPUState *env)
2055
2250
{
2056
2251
    fprintf(stderr, "setup_frame: not implemented\n");
2057
2252
}
2058
2253
 
2059
 
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
 
2254
static void setup_rt_frame(int sig, struct target_sigaction *ka,
2060
2255
                           target_siginfo_t *info,
2061
2256
                           target_sigset_t *set, CPUState *env)
2062
2257
{
2128
2323
{
2129
2324
    int err = 0;
2130
2325
 
2131
 
    err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
 
2326
    err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2132
2327
 
2133
 
#define save_gp_reg(i) do {                                                     \
2134
 
        err |= __put_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]);     \
 
2328
#define save_gp_reg(i) do {                                             \
 
2329
        err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);     \
2135
2330
    } while(0)
2136
2331
    __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2137
2332
    save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2144
2339
    save_gp_reg(31);
2145
2340
#undef save_gp_reg
2146
2341
 
2147
 
    err |= __put_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
2148
 
    err |= __put_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
 
2342
    err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
 
2343
    err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2149
2344
 
2150
2345
    /* Not used yet, but might be useful if we ever have DSP suppport */
2151
2346
#if 0
2205
2400
 
2206
2401
    err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2207
2402
 
2208
 
    err |= __get_user(regs->HI[0][regs->current_tc], &sc->sc_mdhi);
2209
 
    err |= __get_user(regs->LO[0][regs->current_tc], &sc->sc_mdlo);
 
2403
    err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
 
2404
    err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2210
2405
 
2211
2406
#define restore_gp_reg(i) do {                                                          \
2212
 
        err |= __get_user(regs->gpr[i][regs->current_tc], &sc->sc_regs[i]);             \
 
2407
        err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);             \
2213
2408
    } while(0)
2214
2409
    restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2215
2410
    restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2270
2465
 * Determine which stack to use..
2271
2466
 */
2272
2467
static inline abi_ulong
2273
 
get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
 
2468
get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
2274
2469
{
2275
2470
    unsigned long sp;
2276
2471
 
2277
2472
    /* Default to using normal stack */
2278
 
    sp = regs->gpr[29][regs->current_tc];
 
2473
    sp = regs->active_tc.gpr[29];
2279
2474
 
2280
2475
    /*
2281
2476
     * FPU emulator may have it's own trampoline active just
2285
2480
    sp -= 32;
2286
2481
 
2287
2482
    /* This is the X/Open sanctioned signal stack switching.  */
2288
 
    if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
 
2483
    if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2289
2484
        sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2290
2485
    }
2291
2486
 
2293
2488
}
2294
2489
 
2295
2490
/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2296
 
static void setup_frame(int sig, struct emulated_sigaction * ka,
 
2491
static void setup_frame(int sig, struct target_sigaction * ka,
2297
2492
                        target_sigset_t *set, CPUState *regs)
2298
2493
{
2299
2494
    struct sigframe *frame;
2324
2519
    * $25 and PC point to the signal handler, $29 points to the
2325
2520
    * struct sigframe.
2326
2521
    */
2327
 
    regs->gpr[ 4][regs->current_tc] = sig;
2328
 
    regs->gpr[ 5][regs->current_tc] = 0;
2329
 
    regs->gpr[ 6][regs->current_tc] = frame_addr + offsetof(struct sigframe, sf_sc);
2330
 
    regs->gpr[29][regs->current_tc] = frame_addr;
2331
 
    regs->gpr[31][regs->current_tc] = frame_addr + offsetof(struct sigframe, sf_code);
 
2522
    regs->active_tc.gpr[ 4] = sig;
 
2523
    regs->active_tc.gpr[ 5] = 0;
 
2524
    regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
 
2525
    regs->active_tc.gpr[29] = frame_addr;
 
2526
    regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2332
2527
    /* The original kernel code sets CP0_EPC to the handler
2333
2528
    * since it returns to userland using eret
2334
2529
    * we cannot do this here, and we must set PC directly */
2335
 
    regs->PC[regs->current_tc] = regs->gpr[25][regs->current_tc] = ka->sa._sa_handler;
 
2530
    regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2336
2531
    unlock_user_struct(frame, frame_addr, 1);
2337
2532
    return;
2338
2533
 
2353
2548
#if defined(DEBUG_SIGNAL)
2354
2549
    fprintf(stderr, "do_sigreturn\n");
2355
2550
#endif
2356
 
    frame_addr = regs->gpr[29][regs->current_tc];
 
2551
    frame_addr = regs->active_tc.gpr[29];
2357
2552
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2358
2553
        goto badframe;
2359
2554
 
2380
2575
    /* Unreached */
2381
2576
#endif
2382
2577
 
2383
 
    regs->PC[regs->current_tc] = regs->CP0_EPC;
 
2578
    regs->active_tc.PC = regs->CP0_EPC;
2384
2579
    /* I am not sure this is right, but it seems to work
2385
2580
    * maybe a problem with nested signals ? */
2386
2581
    regs->CP0_EPC = 0;
2391
2586
    return 0;
2392
2587
}
2393
2588
 
2394
 
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
 
2589
static void setup_rt_frame(int sig, struct target_sigaction *ka,
2395
2590
                           target_siginfo_t *info,
2396
2591
                           target_sigset_t *set, CPUState *env)
2397
2592
{
2459
2654
#define MOVW(n)  (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2460
2655
#define TRAP_NOARG 0xc310         /* Syscall w/no args (NR in R3) SH3/4 */
2461
2656
 
2462
 
static abi_ulong get_sigframe(struct emulated_sigaction *ka,
 
2657
static abi_ulong get_sigframe(struct target_sigaction *ka,
2463
2658
                         unsigned long sp, size_t frame_size)
2464
2659
{
2465
 
    if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
 
2660
    if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2466
2661
        sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2467
2662
    }
2468
2663
 
2521
2716
    return err;
2522
2717
}
2523
2718
 
2524
 
static void setup_frame(int sig, struct emulated_sigaction *ka,
 
2719
static void setup_frame(int sig, struct target_sigaction *ka,
2525
2720
                        target_sigset_t *set, CPUState *regs)
2526
2721
{
2527
2722
    struct target_sigframe *frame;
2544
2739
 
2545
2740
    /* Set up to return from userspace.  If provided, use a stub
2546
2741
       already in userspace.  */
2547
 
    if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2548
 
        regs->pr = (unsigned long) ka->sa.sa_restorer;
 
2742
    if (ka->sa_flags & TARGET_SA_RESTORER) {
 
2743
        regs->pr = (unsigned long) ka->sa_restorer;
2549
2744
    } else {
2550
2745
        /* Generate return code (system call to sigreturn) */
2551
2746
        err |= __put_user(MOVW(2), &frame->retcode[0]);
2562
2757
    regs->gregs[4] = signal; /* Arg for signal handler */
2563
2758
    regs->gregs[5] = 0;
2564
2759
    regs->gregs[6] = (unsigned long) &frame->sc;
2565
 
    regs->pc = (unsigned long) ka->sa._sa_handler;
 
2760
    regs->pc = (unsigned long) ka->_sa_handler;
2566
2761
 
2567
2762
    unlock_user_struct(frame, frame_addr, 1);
2568
2763
    return;
2572
2767
    force_sig(SIGSEGV);
2573
2768
}
2574
2769
 
2575
 
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
 
2770
static void setup_rt_frame(int sig, struct target_sigaction *ka,
2576
2771
                           target_siginfo_t *info,
2577
2772
                           target_sigset_t *set, CPUState *regs)
2578
2773
{
2593
2788
    /* Create the ucontext.  */
2594
2789
    err |= __put_user(0, &frame->uc.uc_flags);
2595
2790
    err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
2596
 
    err |= __put_user((void *)target_sigaltstack_used.ss_sp,
 
2791
    err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
2597
2792
                      &frame->uc.uc_stack.ss_sp);
2598
2793
    err |= __put_user(sas_ss_flags(regs->gregs[15]),
2599
2794
                      &frame->uc.uc_stack.ss_flags);
2607
2802
 
2608
2803
    /* Set up to return from userspace.  If provided, use a stub
2609
2804
       already in userspace.  */
2610
 
    if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2611
 
        regs->pr = (unsigned long) ka->sa.sa_restorer;
 
2805
    if (ka->sa_flags & TARGET_SA_RESTORER) {
 
2806
        regs->pr = (unsigned long) ka->sa_restorer;
2612
2807
    } else {
2613
2808
        /* Generate return code (system call to sigreturn) */
2614
2809
        err |= __put_user(MOVW(2), &frame->retcode[0]);
2625
2820
    regs->gregs[4] = signal; /* Arg for signal handler */
2626
2821
    regs->gregs[5] = (unsigned long) &frame->info;
2627
2822
    regs->gregs[6] = (unsigned long) &frame->uc;
2628
 
    regs->pc = (unsigned long) ka->sa._sa_handler;
 
2823
    regs->pc = (unsigned long) ka->_sa_handler;
2629
2824
 
2630
2825
    unlock_user_struct(frame, frame_addr, 1);
2631
2826
    return;
2706
2901
    force_sig(TARGET_SIGSEGV);
2707
2902
    return 0;
2708
2903
}
 
2904
#elif defined(TARGET_CRIS)
 
2905
 
 
2906
struct target_sigcontext {
 
2907
        struct target_pt_regs regs;  /* needs to be first */
 
2908
        uint32_t oldmask;
 
2909
        uint32_t usp;    /* usp before stacking this gunk on it */
 
2910
};
 
2911
 
 
2912
/* Signal frames. */
 
2913
struct target_signal_frame {
 
2914
        struct target_sigcontext sc;
 
2915
        uint32_t extramask[TARGET_NSIG_WORDS - 1];
 
2916
        uint8_t retcode[8];       /* Trampoline code. */
 
2917
};
 
2918
 
 
2919
struct rt_signal_frame {
 
2920
        struct siginfo *pinfo;
 
2921
        void *puc;
 
2922
        struct siginfo info;
 
2923
        struct ucontext uc;
 
2924
        uint8_t retcode[8];       /* Trampoline code. */
 
2925
};
 
2926
 
 
2927
static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
 
2928
{
 
2929
        __put_user(env->regs[0], &sc->regs.r0);
 
2930
        __put_user(env->regs[1], &sc->regs.r1);
 
2931
        __put_user(env->regs[2], &sc->regs.r2);
 
2932
        __put_user(env->regs[3], &sc->regs.r3);
 
2933
        __put_user(env->regs[4], &sc->regs.r4);
 
2934
        __put_user(env->regs[5], &sc->regs.r5);
 
2935
        __put_user(env->regs[6], &sc->regs.r6);
 
2936
        __put_user(env->regs[7], &sc->regs.r7);
 
2937
        __put_user(env->regs[8], &sc->regs.r8);
 
2938
        __put_user(env->regs[9], &sc->regs.r9);
 
2939
        __put_user(env->regs[10], &sc->regs.r10);
 
2940
        __put_user(env->regs[11], &sc->regs.r11);
 
2941
        __put_user(env->regs[12], &sc->regs.r12);
 
2942
        __put_user(env->regs[13], &sc->regs.r13);
 
2943
        __put_user(env->regs[14], &sc->usp);
 
2944
        __put_user(env->regs[15], &sc->regs.acr);
 
2945
        __put_user(env->pregs[PR_MOF], &sc->regs.mof);
 
2946
        __put_user(env->pregs[PR_SRP], &sc->regs.srp);
 
2947
        __put_user(env->pc, &sc->regs.erp);
 
2948
}
 
2949
 
 
2950
static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
 
2951
{
 
2952
        __get_user(env->regs[0], &sc->regs.r0);
 
2953
        __get_user(env->regs[1], &sc->regs.r1);
 
2954
        __get_user(env->regs[2], &sc->regs.r2);
 
2955
        __get_user(env->regs[3], &sc->regs.r3);
 
2956
        __get_user(env->regs[4], &sc->regs.r4);
 
2957
        __get_user(env->regs[5], &sc->regs.r5);
 
2958
        __get_user(env->regs[6], &sc->regs.r6);
 
2959
        __get_user(env->regs[7], &sc->regs.r7);
 
2960
        __get_user(env->regs[8], &sc->regs.r8);
 
2961
        __get_user(env->regs[9], &sc->regs.r9);
 
2962
        __get_user(env->regs[10], &sc->regs.r10);
 
2963
        __get_user(env->regs[11], &sc->regs.r11);
 
2964
        __get_user(env->regs[12], &sc->regs.r12);
 
2965
        __get_user(env->regs[13], &sc->regs.r13);
 
2966
        __get_user(env->regs[14], &sc->usp);
 
2967
        __get_user(env->regs[15], &sc->regs.acr);
 
2968
        __get_user(env->pregs[PR_MOF], &sc->regs.mof);
 
2969
        __get_user(env->pregs[PR_SRP], &sc->regs.srp);
 
2970
        __get_user(env->pc, &sc->regs.erp);
 
2971
}
 
2972
 
 
2973
static abi_ulong get_sigframe(CPUState *env, int framesize)
 
2974
{
 
2975
        abi_ulong sp;
 
2976
        /* Align the stack downwards to 4.  */
 
2977
        sp = (env->regs[R_SP] & ~3);
 
2978
        return sp - framesize;
 
2979
}
 
2980
 
 
2981
static void setup_frame(int sig, struct target_sigaction *ka,
 
2982
                        target_sigset_t *set, CPUState *env)
 
2983
{
 
2984
        struct target_signal_frame *frame;
 
2985
        abi_ulong frame_addr;
 
2986
        int err = 0;
 
2987
        int i;
 
2988
 
 
2989
        frame_addr = get_sigframe(env, sizeof *frame);
 
2990
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
 
2991
                goto badframe;
 
2992
 
 
2993
        /*
 
2994
         * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
 
2995
         * use this trampoline anymore but it sets it up for GDB.
 
2996
         * In QEMU, using the trampoline simplifies things a bit so we use it.
 
2997
         *
 
2998
         * This is movu.w __NR_sigreturn, r9; break 13;
 
2999
         */
 
3000
        err |= __put_user(0x9c5f, frame->retcode+0);
 
3001
        err |= __put_user(TARGET_NR_sigreturn, 
 
3002
                          frame->retcode+2);
 
3003
        err |= __put_user(0xe93d, frame->retcode+4);
 
3004
 
 
3005
        /* Save the mask.  */
 
3006
        err |= __put_user(set->sig[0], &frame->sc.oldmask);
 
3007
        if (err)
 
3008
                goto badframe;
 
3009
 
 
3010
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
 
3011
                if (__put_user(set->sig[i], &frame->extramask[i - 1]))
 
3012
                        goto badframe;
 
3013
        }
 
3014
 
 
3015
        setup_sigcontext(&frame->sc, env);
 
3016
 
 
3017
        /* Move the stack and setup the arguments for the handler.  */
 
3018
        env->regs[R_SP] = (uint32_t) (unsigned long) frame;
 
3019
        env->regs[10] = sig;
 
3020
        env->pc = (unsigned long) ka->_sa_handler;
 
3021
        /* Link SRP so the guest returns through the trampoline.  */
 
3022
        env->pregs[PR_SRP] = (uint32_t) (unsigned long) &frame->retcode[0];
 
3023
 
 
3024
        unlock_user_struct(frame, frame_addr, 1);
 
3025
        return;
 
3026
  badframe:
 
3027
        unlock_user_struct(frame, frame_addr, 1);
 
3028
        force_sig(TARGET_SIGSEGV);
 
3029
}
 
3030
 
 
3031
static void setup_rt_frame(int sig, struct target_sigaction *ka,
 
3032
                           target_siginfo_t *info,
 
3033
                           target_sigset_t *set, CPUState *env)
 
3034
{
 
3035
    fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
 
3036
}
 
3037
 
 
3038
long do_sigreturn(CPUState *env)
 
3039
{
 
3040
        struct target_signal_frame *frame;
 
3041
        abi_ulong frame_addr;
 
3042
        target_sigset_t target_set;
 
3043
        sigset_t set;
 
3044
        int i;
 
3045
 
 
3046
        frame_addr = env->regs[R_SP];
 
3047
        /* Make sure the guest isn't playing games.  */
 
3048
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
 
3049
                goto badframe;
 
3050
 
 
3051
        /* Restore blocked signals */
 
3052
        if (__get_user(target_set.sig[0], &frame->sc.oldmask))
 
3053
                goto badframe;
 
3054
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
 
3055
                if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
 
3056
                        goto badframe;
 
3057
        }
 
3058
        target_to_host_sigset_internal(&set, &target_set);
 
3059
        sigprocmask(SIG_SETMASK, &set, NULL);
 
3060
 
 
3061
        restore_sigcontext(&frame->sc, env);
 
3062
        unlock_user_struct(frame, frame_addr, 0);
 
3063
        return env->regs[10];
 
3064
  badframe:
 
3065
        unlock_user_struct(frame, frame_addr, 0);
 
3066
        force_sig(TARGET_SIGSEGV);
 
3067
}
 
3068
 
 
3069
long do_rt_sigreturn(CPUState *env)
 
3070
{
 
3071
    fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
 
3072
    return -TARGET_ENOSYS;
 
3073
}
2709
3074
 
2710
3075
#else
2711
3076
 
2712
 
static void setup_frame(int sig, struct emulated_sigaction *ka,
 
3077
static void setup_frame(int sig, struct target_sigaction *ka,
2713
3078
                        target_sigset_t *set, CPUState *env)
2714
3079
{
2715
3080
    fprintf(stderr, "setup_frame: not implemented\n");
2716
3081
}
2717
3082
 
2718
 
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
 
3083
static void setup_rt_frame(int sig, struct target_sigaction *ka,
2719
3084
                           target_siginfo_t *info,
2720
3085
                           target_sigset_t *set, CPUState *env)
2721
3086
{
2736
3101
 
2737
3102
#endif
2738
3103
 
2739
 
void process_pending_signals(void *cpu_env)
 
3104
void process_pending_signals(CPUState *cpu_env)
2740
3105
{
2741
3106
    int sig;
2742
3107
    abi_ulong handler;
2743
3108
    sigset_t set, old_set;
2744
3109
    target_sigset_t target_old_set;
2745
 
    struct emulated_sigaction *k;
 
3110
    struct emulated_sigtable *k;
 
3111
    struct target_sigaction *sa;
2746
3112
    struct sigqueue *q;
 
3113
    TaskState *ts = cpu_env->opaque;
2747
3114
 
2748
 
    if (!signal_pending)
 
3115
    if (!ts->signal_pending)
2749
3116
        return;
2750
3117
 
2751
 
    k = sigact_table;
 
3118
    /* FIXME: This is not threadsafe.  */
 
3119
    k = ts->sigtab;
2752
3120
    for(sig = 1; sig <= TARGET_NSIG; sig++) {
2753
3121
        if (k->pending)
2754
3122
            goto handle_signal;
2755
3123
        k++;
2756
3124
    }
2757
3125
    /* if no signal is pending, just return */
2758
 
    signal_pending = 0;
 
3126
    ts->signal_pending = 0;
2759
3127
    return;
2760
3128
 
2761
3129
 handle_signal:
2770
3138
 
2771
3139
    sig = gdb_handlesig (cpu_env, sig);
2772
3140
    if (!sig) {
2773
 
        fprintf (stderr, "Lost signal\n");
2774
 
        abort();
 
3141
        sa = NULL;
 
3142
        handler = TARGET_SIG_IGN;
 
3143
    } else {
 
3144
        sa = &sigact_table[sig - 1];
 
3145
        handler = sa->_sa_handler;
2775
3146
    }
2776
3147
 
2777
 
    handler = k->sa._sa_handler;
2778
3148
    if (handler == TARGET_SIG_DFL) {
2779
 
        /* default handler : ignore some signal. The other are fatal */
2780
 
        if (sig != TARGET_SIGCHLD &&
2781
 
            sig != TARGET_SIGURG &&
2782
 
            sig != TARGET_SIGWINCH) {
 
3149
        /* default handler : ignore some signal. The other are job control or fatal */
 
3150
        if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
 
3151
            kill(getpid(),SIGSTOP);
 
3152
        } else if (sig != TARGET_SIGCHLD &&
 
3153
                   sig != TARGET_SIGURG &&
 
3154
                   sig != TARGET_SIGWINCH &&
 
3155
                   sig != TARGET_SIGCONT) {
2783
3156
            force_sig(sig);
2784
3157
        }
2785
3158
    } else if (handler == TARGET_SIG_IGN) {
2788
3161
        force_sig(sig);
2789
3162
    } else {
2790
3163
        /* compute the blocked signals during the handler execution */
2791
 
        target_to_host_sigset(&set, &k->sa.sa_mask);
 
3164
        target_to_host_sigset(&set, &sa->sa_mask);
2792
3165
        /* SA_NODEFER indicates that the current signal should not be
2793
3166
           blocked during the handler */
2794
 
        if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
 
3167
        if (!(sa->sa_flags & TARGET_SA_NODEFER))
2795
3168
            sigaddset(&set, target_to_host_signal(sig));
2796
3169
 
2797
3170
        /* block signals in the handler using Linux */
2809
3182
        }
2810
3183
#endif
2811
3184
        /* prepare the stack frame of the virtual CPU */
2812
 
        if (k->sa.sa_flags & TARGET_SA_SIGINFO)
2813
 
            setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
 
3185
        if (sa->sa_flags & TARGET_SA_SIGINFO)
 
3186
            setup_rt_frame(sig, sa, &q->info, &target_old_set, cpu_env);
2814
3187
        else
2815
 
            setup_frame(sig, k, &target_old_set, cpu_env);
2816
 
        if (k->sa.sa_flags & TARGET_SA_RESETHAND)
2817
 
            k->sa._sa_handler = TARGET_SIG_DFL;
 
3188
            setup_frame(sig, sa, &target_old_set, cpu_env);
 
3189
        if (sa->sa_flags & TARGET_SA_RESETHAND)
 
3190
            sa->_sa_handler = TARGET_SIG_DFL;
2818
3191
    }
2819
3192
    if (q != &k->info)
2820
 
        free_sigqueue(q);
 
3193
        free_sigqueue(cpu_env, q);
2821
3194
}