Statistics
| Branch: | Revision:

root / linux-user / signal.c @ 4cb05961

History | View | Annotate | Download (90.8 kB)

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
#include "target_signal.h"
31

    
32
//#define DEBUG_SIGNAL
33

    
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 = {
50
    .ss_sp = 0,
51
    .ss_size = 0,
52
    .ss_flags = TARGET_SS_DISABLE,
53
};
54

    
55
static struct emulated_sigaction sigact_table[TARGET_NSIG];
56
static struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
57
static struct sigqueue *first_free; /* first free siginfo queue entry */
58
static int signal_pending; /* non zero if a signal may be pending */
59

    
60
static void host_signal_handler(int host_signum, siginfo_t *info,
61
                                void *puc);
62

    
63
static uint8_t host_to_target_signal_table[65] = {
64
    [SIGHUP] = TARGET_SIGHUP,
65
    [SIGINT] = TARGET_SIGINT,
66
    [SIGQUIT] = TARGET_SIGQUIT,
67
    [SIGILL] = TARGET_SIGILL,
68
    [SIGTRAP] = TARGET_SIGTRAP,
69
    [SIGABRT] = TARGET_SIGABRT,
70
/*    [SIGIOT] = TARGET_SIGIOT,*/
71
    [SIGBUS] = TARGET_SIGBUS,
72
    [SIGFPE] = TARGET_SIGFPE,
73
    [SIGKILL] = TARGET_SIGKILL,
74
    [SIGUSR1] = TARGET_SIGUSR1,
75
    [SIGSEGV] = TARGET_SIGSEGV,
76
    [SIGUSR2] = TARGET_SIGUSR2,
77
    [SIGPIPE] = TARGET_SIGPIPE,
78
    [SIGALRM] = TARGET_SIGALRM,
79
    [SIGTERM] = TARGET_SIGTERM,
80
#ifdef SIGSTKFLT
81
    [SIGSTKFLT] = TARGET_SIGSTKFLT,
82
#endif
83
    [SIGCHLD] = TARGET_SIGCHLD,
84
    [SIGCONT] = TARGET_SIGCONT,
85
    [SIGSTOP] = TARGET_SIGSTOP,
86
    [SIGTSTP] = TARGET_SIGTSTP,
87
    [SIGTTIN] = TARGET_SIGTTIN,
88
    [SIGTTOU] = TARGET_SIGTTOU,
89
    [SIGURG] = TARGET_SIGURG,
90
    [SIGXCPU] = TARGET_SIGXCPU,
91
    [SIGXFSZ] = TARGET_SIGXFSZ,
92
    [SIGVTALRM] = TARGET_SIGVTALRM,
93
    [SIGPROF] = TARGET_SIGPROF,
94
    [SIGWINCH] = TARGET_SIGWINCH,
95
    [SIGIO] = TARGET_SIGIO,
96
    [SIGPWR] = TARGET_SIGPWR,
97
    [SIGSYS] = TARGET_SIGSYS,
98
    /* next signals stay the same */
99
};
100
static uint8_t target_to_host_signal_table[65];
101

    
102
static inline int on_sig_stack(unsigned long sp)
103
{
104
    return (sp - target_sigaltstack_used.ss_sp
105
            < target_sigaltstack_used.ss_size);
106
}
107

    
108
static inline int sas_ss_flags(unsigned long sp)
109
{
110
    return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
111
            : on_sig_stack(sp) ? SS_ONSTACK : 0);
112
}
113

    
114
static inline int host_to_target_signal(int sig)
115
{
116
    if (sig > 64)
117
        return sig;
118
    return host_to_target_signal_table[sig];
119
}
120

    
121
int target_to_host_signal(int sig)
122
{
123
    if (sig > 64)
124
        return sig;
125
    return target_to_host_signal_table[sig];
126
}
127

    
128
static void host_to_target_sigset_internal(target_sigset_t *d,
129
                                           const sigset_t *s)
130
{
131
    int i;
132
    unsigned long sigmask;
133
    uint32_t target_sigmask;
134

    
135
    sigmask = ((unsigned long *)s)[0];
136
    target_sigmask = 0;
137
    for(i = 0; i < 32; i++) {
138
        if (sigmask & (1 << i))
139
            target_sigmask |= 1 << (host_to_target_signal(i + 1) - 1);
140
    }
141
#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
142
    d->sig[0] = target_sigmask;
143
    for(i = 1;i < TARGET_NSIG_WORDS; i++) {
144
        d->sig[i] = ((unsigned long *)s)[i];
145
    }
146
#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
147
    d->sig[0] = target_sigmask;
148
    d->sig[1] = sigmask >> 32;
149
#else
150
    /* XXX: do it */
151
#endif
152
}
153

    
154
void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
155
{
156
    target_sigset_t d1;
157
    int i;
158

    
159
    host_to_target_sigset_internal(&d1, s);
160
    for(i = 0;i < TARGET_NSIG_WORDS; i++)
161
        d->sig[i] = tswapl(d1.sig[i]);
162
}
163

    
164
void target_to_host_sigset_internal(sigset_t *d, const target_sigset_t *s)
165
{
166
    int i;
167
    unsigned long sigmask;
168
    abi_ulong target_sigmask;
169

    
170
    target_sigmask = s->sig[0];
171
    sigmask = 0;
172
    for(i = 0; i < 32; i++) {
173
        if (target_sigmask & (1 << i))
174
            sigmask |= 1 << (target_to_host_signal(i + 1) - 1);
175
    }
176
#if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 32
177
    ((unsigned long *)d)[0] = sigmask;
178
    for(i = 1;i < TARGET_NSIG_WORDS; i++) {
179
        ((unsigned long *)d)[i] = s->sig[i];
180
    }
181
#elif TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64 && TARGET_NSIG_WORDS == 2
182
    ((unsigned long *)d)[0] = sigmask | ((unsigned long)(s->sig[1]) << 32);
183
#else
184
    /* XXX: do it */
185
#endif /* TARGET_ABI_BITS */
186
}
187

    
188
void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
189
{
190
    target_sigset_t s1;
191
    int i;
192

    
193
    for(i = 0;i < TARGET_NSIG_WORDS; i++)
194
        s1.sig[i] = tswapl(s->sig[i]);
195
    target_to_host_sigset_internal(d, &s1);
196
}
197

    
198
void host_to_target_old_sigset(abi_ulong *old_sigset,
199
                               const sigset_t *sigset)
200
{
201
    target_sigset_t d;
202
    host_to_target_sigset(&d, sigset);
203
    *old_sigset = d.sig[0];
204
}
205

    
206
void target_to_host_old_sigset(sigset_t *sigset,
207
                               const abi_ulong *old_sigset)
208
{
209
    target_sigset_t d;
210
    int i;
211

    
212
    d.sig[0] = *old_sigset;
213
    for(i = 1;i < TARGET_NSIG_WORDS; i++)
214
        d.sig[i] = 0;
215
    target_to_host_sigset(sigset, &d);
216
}
217

    
218
/* siginfo conversion */
219

    
220
static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
221
                                                 const siginfo_t *info)
222
{
223
    int sig;
224
    sig = host_to_target_signal(info->si_signo);
225
    tinfo->si_signo = sig;
226
    tinfo->si_errno = 0;
227
    tinfo->si_code = 0;
228
    if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
229
        sig == SIGBUS || sig == SIGTRAP) {
230
        /* should never come here, but who knows. The information for
231
           the target is irrelevant */
232
        tinfo->_sifields._sigfault._addr = 0;
233
    } else if (sig == SIGIO) {
234
        tinfo->_sifields._sigpoll._fd = info->si_fd;
235
    } else if (sig >= TARGET_SIGRTMIN) {
236
        tinfo->_sifields._rt._pid = info->si_pid;
237
        tinfo->_sifields._rt._uid = info->si_uid;
238
        /* XXX: potential problem if 64 bit */
239
        tinfo->_sifields._rt._sigval.sival_ptr =
240
            (abi_ulong)(unsigned long)info->si_value.sival_ptr;
241
    }
242
}
243

    
244
static void tswap_siginfo(target_siginfo_t *tinfo,
245
                          const target_siginfo_t *info)
246
{
247
    int sig;
248
    sig = info->si_signo;
249
    tinfo->si_signo = tswap32(sig);
250
    tinfo->si_errno = tswap32(info->si_errno);
251
    tinfo->si_code = tswap32(info->si_code);
252
    if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
253
        sig == SIGBUS || sig == SIGTRAP) {
254
        tinfo->_sifields._sigfault._addr =
255
            tswapl(info->_sifields._sigfault._addr);
256
    } else if (sig == SIGIO) {
257
        tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
258
    } else if (sig >= TARGET_SIGRTMIN) {
259
        tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
260
        tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
261
        tinfo->_sifields._rt._sigval.sival_ptr =
262
            tswapl(info->_sifields._rt._sigval.sival_ptr);
263
    }
264
}
265

    
266

    
267
void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
268
{
269
    host_to_target_siginfo_noswap(tinfo, info);
270
    tswap_siginfo(tinfo, tinfo);
271
}
272

    
273
/* XXX: we support only POSIX RT signals are used. */
274
/* XXX: find a solution for 64 bit (additional malloced data is needed) */
275
void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
276
{
277
    info->si_signo = tswap32(tinfo->si_signo);
278
    info->si_errno = tswap32(tinfo->si_errno);
279
    info->si_code = tswap32(tinfo->si_code);
280
    info->si_pid = tswap32(tinfo->_sifields._rt._pid);
281
    info->si_uid = tswap32(tinfo->_sifields._rt._uid);
282
    info->si_value.sival_ptr =
283
            (void *)(long)tswapl(tinfo->_sifields._rt._sigval.sival_ptr);
284
}
285

    
286
void signal_init(void)
287
{
288
    struct sigaction act;
289
    int i, j;
290

    
291
    /* generate signal conversion tables */
292
    for(i = 1; i <= 64; i++) {
293
        if (host_to_target_signal_table[i] == 0)
294
            host_to_target_signal_table[i] = i;
295
    }
296
    for(i = 1; i <= 64; i++) {
297
        j = host_to_target_signal_table[i];
298
        target_to_host_signal_table[j] = i;
299
    }
300

    
301
    /* set all host signal handlers. ALL signals are blocked during
302
       the handlers to serialize them. */
303
    sigfillset(&act.sa_mask);
304
    act.sa_flags = SA_SIGINFO;
305
    act.sa_sigaction = host_signal_handler;
306
    for(i = 1; i < NSIG; i++) {
307
        sigaction(i, &act, NULL);
308
    }
309

    
310
    memset(sigact_table, 0, sizeof(sigact_table));
311

    
312
    first_free = &sigqueue_table[0];
313
    for(i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++)
314
        sigqueue_table[i].next = &sigqueue_table[i + 1];
315
    sigqueue_table[MAX_SIGQUEUE_SIZE - 1].next = NULL;
316
}
317

    
318
/* signal queue handling */
319

    
320
static inline struct sigqueue *alloc_sigqueue(void)
321
{
322
    struct sigqueue *q = first_free;
323
    if (!q)
324
        return NULL;
325
    first_free = q->next;
326
    return q;
327
}
328

    
329
static inline void free_sigqueue(struct sigqueue *q)
330
{
331
    q->next = first_free;
332
    first_free = q;
333
}
334

    
335
/* abort execution with signal */
336
void __attribute((noreturn)) force_sig(int sig)
337
{
338
    int host_sig;
339
    host_sig = target_to_host_signal(sig);
340
    fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
341
            sig, strsignal(host_sig));
342
#if 1
343
    _exit(-host_sig);
344
#else
345
    {
346
        struct sigaction act;
347
        sigemptyset(&act.sa_mask);
348
        act.sa_flags = SA_SIGINFO;
349
        act.sa_sigaction = SIG_DFL;
350
        sigaction(SIGABRT, &act, NULL);
351
        abort();
352
    }
353
#endif
354
}
355

    
356
/* queue a signal so that it will be send to the virtual CPU as soon
357
   as possible */
358
int queue_signal(int sig, target_siginfo_t *info)
359
{
360
    struct emulated_sigaction *k;
361
    struct sigqueue *q, **pq;
362
    abi_ulong handler;
363

    
364
#if defined(DEBUG_SIGNAL)
365
    fprintf(stderr, "queue_signal: sig=%d\n",
366
            sig);
367
#endif
368
    k = &sigact_table[sig - 1];
369
    handler = k->sa._sa_handler;
370
    if (handler == TARGET_SIG_DFL) {
371
        /* default handler : ignore some signal. The other are fatal */
372
        if (sig != TARGET_SIGCHLD &&
373
            sig != TARGET_SIGURG &&
374
            sig != TARGET_SIGWINCH) {
375
            force_sig(sig);
376
        } else {
377
            return 0; /* indicate ignored */
378
        }
379
    } else if (handler == TARGET_SIG_IGN) {
380
        /* ignore signal */
381
        return 0;
382
    } else if (handler == TARGET_SIG_ERR) {
383
        force_sig(sig);
384
    } else {
385
        pq = &k->first;
386
        if (sig < TARGET_SIGRTMIN) {
387
            /* if non real time signal, we queue exactly one signal */
388
            if (!k->pending)
389
                q = &k->info;
390
            else
391
                return 0;
392
        } else {
393
            if (!k->pending) {
394
                /* first signal */
395
                q = &k->info;
396
            } else {
397
                q = alloc_sigqueue();
398
                if (!q)
399
                    return -EAGAIN;
400
                while (*pq != NULL)
401
                    pq = &(*pq)->next;
402
            }
403
        }
404
        *pq = q;
405
        q->info = *info;
406
        q->next = NULL;
407
        k->pending = 1;
408
        /* signal that a new signal is pending */
409
        signal_pending = 1;
410
        return 1; /* indicates that the signal was queued */
411
    }
412
}
413

    
414
static void host_signal_handler(int host_signum, siginfo_t *info,
415
                                void *puc)
416
{
417
    int sig;
418
    target_siginfo_t tinfo;
419

    
420
    /* the CPU emulator uses some host signals to detect exceptions,
421
       we we forward to it some signals */
422
    if (host_signum == SIGSEGV || host_signum == SIGBUS) {
423
        if (cpu_signal_handler(host_signum, info, puc))
424
            return;
425
    }
426

    
427
    /* get target signal number */
428
    sig = host_to_target_signal(host_signum);
429
    if (sig < 1 || sig > TARGET_NSIG)
430
        return;
431
#if defined(DEBUG_SIGNAL)
432
    fprintf(stderr, "qemu: got signal %d\n", sig);
433
#endif
434
    host_to_target_siginfo_noswap(&tinfo, info);
435
    if (queue_signal(sig, &tinfo) == 1) {
436
        /* interrupt the virtual CPU as soon as possible */
437
        cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
438
    }
439
}
440

    
441
/* do_sigaltstack() returns target values and errnos. */
442
/* compare linux/kernel/signal.c:do_sigaltstack() */
443
abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
444
{
445
    int ret;
446
    struct target_sigaltstack oss;
447

    
448
    /* XXX: test errors */
449
    if(uoss_addr)
450
    {
451
        __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
452
        __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
453
        __put_user(sas_ss_flags(sp), &oss.ss_flags);
454
    }
455

    
456
    if(uss_addr)
457
    {
458
        struct target_sigaltstack *uss;
459
        struct target_sigaltstack ss;
460

    
461
        ret = -TARGET_EFAULT;
462
        if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
463
            || __get_user(ss.ss_sp, &uss->ss_sp)
464
            || __get_user(ss.ss_size, &uss->ss_size)
465
            || __get_user(ss.ss_flags, &uss->ss_flags))
466
            goto out;
467
        unlock_user_struct(uss, uss_addr, 0);
468

    
469
        ret = -TARGET_EPERM;
470
        if (on_sig_stack(sp))
471
            goto out;
472

    
473
        ret = -TARGET_EINVAL;
474
        if (ss.ss_flags != TARGET_SS_DISABLE
475
            && ss.ss_flags != TARGET_SS_ONSTACK
476
            && ss.ss_flags != 0)
477
            goto out;
478

    
479
        if (ss.ss_flags == TARGET_SS_DISABLE) {
480
            ss.ss_size = 0;
481
            ss.ss_sp = 0;
482
        } else {
483
            ret = -TARGET_ENOMEM;
484
            if (ss.ss_size < MINSIGSTKSZ)
485
                goto out;
486
        }
487

    
488
        target_sigaltstack_used.ss_sp = ss.ss_sp;
489
        target_sigaltstack_used.ss_size = ss.ss_size;
490
    }
491

    
492
    if (uoss_addr) {
493
        ret = -TARGET_EFAULT;
494
        if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
495
            goto out;
496
    }
497

    
498
    ret = 0;
499
out:
500
    return ret;
501
}
502

    
503
/* do_sigaction() return host values and errnos */
504
int do_sigaction(int sig, const struct target_sigaction *act,
505
                 struct target_sigaction *oact)
506
{
507
    struct emulated_sigaction *k;
508
    struct sigaction act1;
509
    int host_sig;
510
    int ret = 0;
511

    
512
    if (sig < 1 || sig > TARGET_NSIG || sig == SIGKILL || sig == SIGSTOP)
513
        return -EINVAL;
514
    k = &sigact_table[sig - 1];
515
#if defined(DEBUG_SIGNAL)
516
    fprintf(stderr, "sigaction sig=%d act=0x%08x, oact=0x%08x\n",
517
            sig, (int)act, (int)oact);
518
#endif
519
    if (oact) {
520
        oact->_sa_handler = tswapl(k->sa._sa_handler);
521
        oact->sa_flags = tswapl(k->sa.sa_flags);
522
#if !defined(TARGET_MIPS)
523
        oact->sa_restorer = tswapl(k->sa.sa_restorer);
524
#endif
525
        oact->sa_mask = k->sa.sa_mask;
526
    }
527
    if (act) {
528
        k->sa._sa_handler = tswapl(act->_sa_handler);
529
        k->sa.sa_flags = tswapl(act->sa_flags);
530
#if !defined(TARGET_MIPS)
531
        k->sa.sa_restorer = tswapl(act->sa_restorer);
532
#endif
533
        k->sa.sa_mask = act->sa_mask;
534

    
535
        /* we update the host linux signal state */
536
        host_sig = target_to_host_signal(sig);
537
        if (host_sig != SIGSEGV && host_sig != SIGBUS) {
538
            sigfillset(&act1.sa_mask);
539
            act1.sa_flags = SA_SIGINFO;
540
            if (k->sa.sa_flags & TARGET_SA_RESTART)
541
                act1.sa_flags |= SA_RESTART;
542
            /* NOTE: it is important to update the host kernel signal
543
               ignore state to avoid getting unexpected interrupted
544
               syscalls */
545
            if (k->sa._sa_handler == TARGET_SIG_IGN) {
546
                act1.sa_sigaction = (void *)SIG_IGN;
547
            } else if (k->sa._sa_handler == TARGET_SIG_DFL) {
548
                act1.sa_sigaction = (void *)SIG_DFL;
549
            } else {
550
                act1.sa_sigaction = host_signal_handler;
551
            }
552
            ret = sigaction(host_sig, &act1, NULL);
553
        }
554
    }
555
    return ret;
556
}
557

    
558
#ifndef offsetof
559
#define offsetof(type, field) ((size_t) &((type *)0)->field)
560
#endif
561

    
562
static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
563
                                       const target_siginfo_t *info)
564
{
565
    tswap_siginfo(tinfo, info);
566
    return 0;
567
}
568

    
569
static inline int current_exec_domain_sig(int sig)
570
{
571
    return /* current->exec_domain && current->exec_domain->signal_invmap
572
              && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
573
}
574

    
575
#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
576

    
577
/* from the Linux kernel */
578

    
579
struct target_fpreg {
580
        uint16_t significand[4];
581
        uint16_t exponent;
582
};
583

    
584
struct target_fpxreg {
585
        uint16_t significand[4];
586
        uint16_t exponent;
587
        uint16_t padding[3];
588
};
589

    
590
struct target_xmmreg {
591
        abi_ulong element[4];
592
};
593

    
594
struct target_fpstate {
595
        /* Regular FPU environment */
596
        abi_ulong       cw;
597
        abi_ulong       sw;
598
        abi_ulong       tag;
599
        abi_ulong       ipoff;
600
        abi_ulong       cssel;
601
        abi_ulong       dataoff;
602
        abi_ulong       datasel;
603
        struct target_fpreg        _st[8];
604
        uint16_t        status;
605
        uint16_t        magic;                /* 0xffff = regular FPU data only */
606

    
607
        /* FXSR FPU environment */
608
        abi_ulong       _fxsr_env[6];   /* FXSR FPU env is ignored */
609
        abi_ulong       mxcsr;
610
        abi_ulong       reserved;
611
        struct target_fpxreg        _fxsr_st[8];        /* FXSR FPU reg data is ignored */
612
        struct target_xmmreg        _xmm[8];
613
        abi_ulong       padding[56];
614
};
615

    
616
#define X86_FXSR_MAGIC                0x0000
617

    
618
struct target_sigcontext {
619
        uint16_t gs, __gsh;
620
        uint16_t fs, __fsh;
621
        uint16_t es, __esh;
622
        uint16_t ds, __dsh;
623
        abi_ulong edi;
624
        abi_ulong esi;
625
        abi_ulong ebp;
626
        abi_ulong esp;
627
        abi_ulong ebx;
628
        abi_ulong edx;
629
        abi_ulong ecx;
630
        abi_ulong eax;
631
        abi_ulong trapno;
632
        abi_ulong err;
633
        abi_ulong eip;
634
        uint16_t cs, __csh;
635
        abi_ulong eflags;
636
        abi_ulong esp_at_signal;
637
        uint16_t ss, __ssh;
638
        abi_ulong fpstate; /* pointer */
639
        abi_ulong oldmask;
640
        abi_ulong cr2;
641
};
642

    
643
struct target_ucontext {
644
        abi_ulong         tuc_flags;
645
        abi_ulong         tuc_link;
646
        target_stack_t          tuc_stack;
647
        struct target_sigcontext tuc_mcontext;
648
        target_sigset_t          tuc_sigmask;        /* mask last for extensibility */
649
};
650

    
651
struct sigframe
652
{
653
    abi_ulong pretcode;
654
    int sig;
655
    struct target_sigcontext sc;
656
    struct target_fpstate fpstate;
657
    abi_ulong extramask[TARGET_NSIG_WORDS-1];
658
    char retcode[8];
659
};
660

    
661
struct rt_sigframe
662
{
663
    abi_ulong pretcode;
664
    int sig;
665
    abi_ulong pinfo;
666
    abi_ulong puc;
667
    struct target_siginfo info;
668
    struct target_ucontext uc;
669
    struct target_fpstate fpstate;
670
    char retcode[8];
671
};
672

    
673
/*
674
 * Set up a signal frame.
675
 */
676

    
677
/* XXX: save x87 state */
678
static int
679
setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
680
                 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
681
{
682
        int err = 0;
683
        uint16_t magic;
684

    
685
        /* already locked in setup_frame() */
686
        err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
687
        err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
688
        err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
689
        err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
690
        err |= __put_user(env->regs[R_EDI], &sc->edi);
691
        err |= __put_user(env->regs[R_ESI], &sc->esi);
692
        err |= __put_user(env->regs[R_EBP], &sc->ebp);
693
        err |= __put_user(env->regs[R_ESP], &sc->esp);
694
        err |= __put_user(env->regs[R_EBX], &sc->ebx);
695
        err |= __put_user(env->regs[R_EDX], &sc->edx);
696
        err |= __put_user(env->regs[R_ECX], &sc->ecx);
697
        err |= __put_user(env->regs[R_EAX], &sc->eax);
698
        err |= __put_user(env->exception_index, &sc->trapno);
699
        err |= __put_user(env->error_code, &sc->err);
700
        err |= __put_user(env->eip, &sc->eip);
701
        err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
702
        err |= __put_user(env->eflags, &sc->eflags);
703
        err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
704
        err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
705

    
706
        cpu_x86_fsave(env, fpstate_addr, 1);
707
        fpstate->status = fpstate->sw;
708
        magic = 0xffff;
709
        err |= __put_user(magic, &fpstate->magic);
710
        err |= __put_user(fpstate_addr, &sc->fpstate);
711

    
712
        /* non-iBCS2 extensions.. */
713
        err |= __put_user(mask, &sc->oldmask);
714
        err |= __put_user(env->cr[2], &sc->cr2);
715
        return err;
716
}
717

    
718
/*
719
 * Determine which stack to use..
720
 */
721

    
722
static inline abi_ulong
723
get_sigframe(struct emulated_sigaction *ka, CPUX86State *env, size_t frame_size)
724
{
725
        unsigned long esp;
726

    
727
        /* Default to using normal stack */
728
        esp = env->regs[R_ESP];
729
        /* This is the X/Open sanctioned signal stack switching.  */
730
        if (ka->sa.sa_flags & TARGET_SA_ONSTACK) {
731
            if (sas_ss_flags(esp) == 0)
732
                esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
733
        }
734

    
735
        /* This is the legacy signal stack switching. */
736
        else
737
        if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
738
            !(ka->sa.sa_flags & TARGET_SA_RESTORER) &&
739
            ka->sa.sa_restorer) {
740
            esp = (unsigned long) ka->sa.sa_restorer;
741
        }
742
        return (esp - frame_size) & -8ul;
743
}
744

    
745
/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
746
static void setup_frame(int sig, struct emulated_sigaction *ka,
747
                        target_sigset_t *set, CPUX86State *env)
748
{
749
        abi_ulong frame_addr;
750
        struct sigframe *frame;
751
        int i, err = 0;
752

    
753
        frame_addr = get_sigframe(ka, env, sizeof(*frame));
754

    
755
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
756
                goto give_sigsegv;
757

    
758
        err |= __put_user(current_exec_domain_sig(sig),
759
                          &frame->sig);
760
        if (err)
761
                goto give_sigsegv;
762

    
763
        setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
764
                         frame_addr + offsetof(struct sigframe, fpstate));
765
        if (err)
766
                goto give_sigsegv;
767

    
768
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
769
            if (__put_user(set->sig[i], &frame->extramask[i - 1]))
770
                goto give_sigsegv;
771
        }
772

    
773
        /* Set up to return from userspace.  If provided, use a stub
774
           already in userspace.  */
775
        if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
776
                err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
777
        } else {
778
                uint16_t val16;
779
                abi_ulong retcode_addr;
780
                retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
781
                err |= __put_user(retcode_addr, &frame->pretcode);
782
                /* This is popl %eax ; movl $,%eax ; int $0x80 */
783
                val16 = 0xb858;
784
                err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
785
                err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
786
                val16 = 0x80cd;
787
                err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
788
        }
789

    
790
        if (err)
791
                goto give_sigsegv;
792

    
793
        /* Set up registers for signal handler */
794
        env->regs[R_ESP] = frame_addr;
795
        env->eip = ka->sa._sa_handler;
796

    
797
        cpu_x86_load_seg(env, R_DS, __USER_DS);
798
        cpu_x86_load_seg(env, R_ES, __USER_DS);
799
        cpu_x86_load_seg(env, R_SS, __USER_DS);
800
        cpu_x86_load_seg(env, R_CS, __USER_CS);
801
        env->eflags &= ~TF_MASK;
802

    
803
        unlock_user_struct(frame, frame_addr, 1);
804

    
805
        return;
806

    
807
give_sigsegv:
808
        unlock_user_struct(frame, frame_addr, 1);
809
        if (sig == TARGET_SIGSEGV)
810
                ka->sa._sa_handler = TARGET_SIG_DFL;
811
        force_sig(TARGET_SIGSEGV /* , current */);
812
}
813

    
814
/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
815
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
816
                           target_siginfo_t *info,
817
                           target_sigset_t *set, CPUX86State *env)
818
{
819
        abi_ulong frame_addr, addr;
820
        struct rt_sigframe *frame;
821
        int i, err = 0;
822

    
823
        frame_addr = get_sigframe(ka, env, sizeof(*frame));
824

    
825
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
826
                goto give_sigsegv;
827

    
828
        err |= __put_user(current_exec_domain_sig(sig),
829
                          &frame->sig);
830
        addr = frame_addr + offsetof(struct rt_sigframe, info);
831
        err |= __put_user(addr, &frame->pinfo);
832
        addr = frame_addr + offsetof(struct rt_sigframe, uc);
833
        err |= __put_user(addr, &frame->puc);
834
        err |= copy_siginfo_to_user(&frame->info, info);
835
        if (err)
836
                goto give_sigsegv;
837

    
838
        /* Create the ucontext.  */
839
        err |= __put_user(0, &frame->uc.tuc_flags);
840
        err |= __put_user(0, &frame->uc.tuc_link);
841
        err |= __put_user(target_sigaltstack_used.ss_sp,
842
                          &frame->uc.tuc_stack.ss_sp);
843
        err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
844
                          &frame->uc.tuc_stack.ss_flags);
845
        err |= __put_user(target_sigaltstack_used.ss_size,
846
                          &frame->uc.tuc_stack.ss_size);
847
        err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
848
                                env, set->sig[0], 
849
                                frame_addr + offsetof(struct rt_sigframe, fpstate));
850
        for(i = 0; i < TARGET_NSIG_WORDS; i++) {
851
            if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
852
                goto give_sigsegv;
853
        }
854

    
855
        /* Set up to return from userspace.  If provided, use a stub
856
           already in userspace.  */
857
        if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
858
                err |= __put_user(ka->sa.sa_restorer, &frame->pretcode);
859
        } else {
860
                uint16_t val16;
861
                addr = frame_addr + offsetof(struct rt_sigframe, retcode);
862
                err |= __put_user(addr, &frame->pretcode);
863
                /* This is movl $,%eax ; int $0x80 */
864
                err |= __put_user(0xb8, (char *)(frame->retcode+0));
865
                err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
866
                val16 = 0x80cd;
867
                err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
868
        }
869

    
870
        if (err)
871
                goto give_sigsegv;
872

    
873
        /* Set up registers for signal handler */
874
        env->regs[R_ESP] = frame_addr;
875
        env->eip = ka->sa._sa_handler;
876

    
877
        cpu_x86_load_seg(env, R_DS, __USER_DS);
878
        cpu_x86_load_seg(env, R_ES, __USER_DS);
879
        cpu_x86_load_seg(env, R_SS, __USER_DS);
880
        cpu_x86_load_seg(env, R_CS, __USER_CS);
881
        env->eflags &= ~TF_MASK;
882

    
883
        unlock_user_struct(frame, frame_addr, 1);
884

    
885
        return;
886

    
887
give_sigsegv:
888
        unlock_user_struct(frame, frame_addr, 1);
889
        if (sig == TARGET_SIGSEGV)
890
                ka->sa._sa_handler = TARGET_SIG_DFL;
891
        force_sig(TARGET_SIGSEGV /* , current */);
892
}
893

    
894
static int
895
restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
896
{
897
        unsigned int err = 0;
898
        abi_ulong fpstate_addr;
899
        unsigned int tmpflags;
900

    
901
        cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
902
        cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
903
        cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
904
        cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
905

    
906
        env->regs[R_EDI] = tswapl(sc->edi);
907
        env->regs[R_ESI] = tswapl(sc->esi);
908
        env->regs[R_EBP] = tswapl(sc->ebp);
909
        env->regs[R_ESP] = tswapl(sc->esp);
910
        env->regs[R_EBX] = tswapl(sc->ebx);
911
        env->regs[R_EDX] = tswapl(sc->edx);
912
        env->regs[R_ECX] = tswapl(sc->ecx);
913
        env->eip = tswapl(sc->eip);
914

    
915
        cpu_x86_load_seg(env, R_CS, lduw(&sc->cs) | 3);
916
        cpu_x86_load_seg(env, R_SS, lduw(&sc->ss) | 3);
917

    
918
        tmpflags = tswapl(sc->eflags);
919
        env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
920
        //                regs->orig_eax = -1;                /* disable syscall checks */
921

    
922
        fpstate_addr = tswapl(sc->fpstate);
923
        if (fpstate_addr != 0) {
924
                if (!access_ok(VERIFY_READ, fpstate_addr, 
925
                               sizeof(struct target_fpstate)))
926
                        goto badframe;
927
                cpu_x86_frstor(env, fpstate_addr, 1);
928
        }
929

    
930
        *peax = tswapl(sc->eax);
931
        return err;
932
badframe:
933
        return 1;
934
}
935

    
936
long do_sigreturn(CPUX86State *env)
937
{
938
    struct sigframe *frame;
939
    abi_ulong frame_addr = env->regs[R_ESP] - 8;
940
    target_sigset_t target_set;
941
    sigset_t set;
942
    int eax, i;
943

    
944
#if defined(DEBUG_SIGNAL)
945
    fprintf(stderr, "do_sigreturn\n");
946
#endif
947
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
948
        goto badframe;
949
    /* set blocked signals */
950
    if (__get_user(target_set.sig[0], &frame->sc.oldmask))
951
        goto badframe;
952
    for(i = 1; i < TARGET_NSIG_WORDS; i++) {
953
        if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
954
            goto badframe;
955
    }
956

    
957
    target_to_host_sigset_internal(&set, &target_set);
958
    sigprocmask(SIG_SETMASK, &set, NULL);
959

    
960
    /* restore registers */
961
    if (restore_sigcontext(env, &frame->sc, &eax))
962
        goto badframe;
963
    unlock_user_struct(frame, frame_addr, 0);
964
    return eax;
965

    
966
badframe:
967
    unlock_user_struct(frame, frame_addr, 0);
968
    force_sig(TARGET_SIGSEGV);
969
    return 0;
970
}
971

    
972
long do_rt_sigreturn(CPUX86State *env)
973
{
974
        abi_ulong frame_addr;
975
        struct rt_sigframe *frame;
976
        sigset_t set;
977
        int eax;
978

    
979
        frame_addr = env->regs[R_ESP] - 4;
980
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
981
                goto badframe;
982
        target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
983
        sigprocmask(SIG_SETMASK, &set, NULL);
984

    
985
        if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
986
                goto badframe;
987

    
988
        if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0, 
989
                           get_sp_from_cpustate(env)) == -EFAULT)
990
                goto badframe;
991

    
992
        unlock_user_struct(frame, frame_addr, 0);
993
        return eax;
994

    
995
badframe:
996
        unlock_user_struct(frame, frame_addr, 0);
997
        force_sig(TARGET_SIGSEGV);
998
        return 0;
999
}
1000

    
1001
#elif defined(TARGET_ARM)
1002

    
1003
struct target_sigcontext {
1004
        abi_ulong trap_no;
1005
        abi_ulong error_code;
1006
        abi_ulong oldmask;
1007
        abi_ulong arm_r0;
1008
        abi_ulong arm_r1;
1009
        abi_ulong arm_r2;
1010
        abi_ulong arm_r3;
1011
        abi_ulong arm_r4;
1012
        abi_ulong arm_r5;
1013
        abi_ulong arm_r6;
1014
        abi_ulong arm_r7;
1015
        abi_ulong arm_r8;
1016
        abi_ulong arm_r9;
1017
        abi_ulong arm_r10;
1018
        abi_ulong arm_fp;
1019
        abi_ulong arm_ip;
1020
        abi_ulong arm_sp;
1021
        abi_ulong arm_lr;
1022
        abi_ulong arm_pc;
1023
        abi_ulong arm_cpsr;
1024
        abi_ulong fault_address;
1025
};
1026

    
1027
struct target_ucontext_v1 {
1028
    abi_ulong tuc_flags;
1029
    abi_ulong tuc_link;
1030
    target_stack_t tuc_stack;
1031
    struct target_sigcontext tuc_mcontext;
1032
    target_sigset_t  tuc_sigmask;        /* mask last for extensibility */
1033
};
1034

    
1035
struct target_ucontext_v2 {
1036
    abi_ulong tuc_flags;
1037
    abi_ulong tuc_link;
1038
    target_stack_t tuc_stack;
1039
    struct target_sigcontext tuc_mcontext;
1040
    target_sigset_t  tuc_sigmask;        /* mask last for extensibility */
1041
    char __unused[128 - sizeof(sigset_t)];
1042
    abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1043
};
1044

    
1045
struct sigframe_v1
1046
{
1047
    struct target_sigcontext sc;
1048
    abi_ulong extramask[TARGET_NSIG_WORDS-1];
1049
    abi_ulong retcode;
1050
};
1051

    
1052
struct sigframe_v2
1053
{
1054
    struct target_ucontext_v2 uc;
1055
    abi_ulong retcode;
1056
};
1057

    
1058
struct rt_sigframe_v1
1059
{
1060
    abi_ulong pinfo;
1061
    abi_ulong puc;
1062
    struct target_siginfo info;
1063
    struct target_ucontext_v1 uc;
1064
    abi_ulong retcode;
1065
};
1066

    
1067
struct rt_sigframe_v2
1068
{
1069
    struct target_siginfo info;
1070
    struct target_ucontext_v2 uc;
1071
    abi_ulong retcode;
1072
};
1073

    
1074
#define TARGET_CONFIG_CPU_32 1
1075

    
1076
/*
1077
 * For ARM syscalls, we encode the syscall number into the instruction.
1078
 */
1079
#define SWI_SYS_SIGRETURN        (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1080
#define SWI_SYS_RT_SIGRETURN        (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1081

    
1082
/*
1083
 * For Thumb syscalls, we pass the syscall number via r7.  We therefore
1084
 * need two 16-bit instructions.
1085
 */
1086
#define SWI_THUMB_SIGRETURN        (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1087
#define SWI_THUMB_RT_SIGRETURN        (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1088

    
1089
static const abi_ulong retcodes[4] = {
1090
        SWI_SYS_SIGRETURN,        SWI_THUMB_SIGRETURN,
1091
        SWI_SYS_RT_SIGRETURN,        SWI_THUMB_RT_SIGRETURN
1092
};
1093

    
1094

    
1095
#define __get_user_error(x,p,e) __get_user(x, p)
1096

    
1097
static inline int valid_user_regs(CPUState *regs)
1098
{
1099
    return 1;
1100
}
1101

    
1102
static void
1103
setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1104
                 CPUState *env, abi_ulong mask)
1105
{
1106
        __put_user(env->regs[0], &sc->arm_r0);
1107
        __put_user(env->regs[1], &sc->arm_r1);
1108
        __put_user(env->regs[2], &sc->arm_r2);
1109
        __put_user(env->regs[3], &sc->arm_r3);
1110
        __put_user(env->regs[4], &sc->arm_r4);
1111
        __put_user(env->regs[5], &sc->arm_r5);
1112
        __put_user(env->regs[6], &sc->arm_r6);
1113
        __put_user(env->regs[7], &sc->arm_r7);
1114
        __put_user(env->regs[8], &sc->arm_r8);
1115
        __put_user(env->regs[9], &sc->arm_r9);
1116
        __put_user(env->regs[10], &sc->arm_r10);
1117
        __put_user(env->regs[11], &sc->arm_fp);
1118
        __put_user(env->regs[12], &sc->arm_ip);
1119
        __put_user(env->regs[13], &sc->arm_sp);
1120
        __put_user(env->regs[14], &sc->arm_lr);
1121
        __put_user(env->regs[15], &sc->arm_pc);
1122
#ifdef TARGET_CONFIG_CPU_32
1123
        __put_user(cpsr_read(env), &sc->arm_cpsr);
1124
#endif
1125

    
1126
        __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1127
        __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1128
        __put_user(/* current->thread.address */ 0, &sc->fault_address);
1129
        __put_user(mask, &sc->oldmask);
1130
}
1131

    
1132
static inline abi_ulong
1133
get_sigframe(struct emulated_sigaction *ka, CPUState *regs, int framesize)
1134
{
1135
        unsigned long sp = regs->regs[13];
1136

    
1137
        /*
1138
         * This is the X/Open sanctioned signal stack switching.
1139
         */
1140
        if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1141
            sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1142
        /*
1143
         * ATPCS B01 mandates 8-byte alignment
1144
         */
1145
        return (sp - framesize) & ~7;
1146
}
1147

    
1148
static int
1149
setup_return(CPUState *env, struct emulated_sigaction *ka,
1150
             abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1151
{
1152
        abi_ulong handler = ka->sa._sa_handler;
1153
        abi_ulong retcode;
1154
        int thumb = handler & 1;
1155

    
1156
        if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
1157
                retcode = ka->sa.sa_restorer;
1158
        } else {
1159
                unsigned int idx = thumb;
1160

    
1161
                if (ka->sa.sa_flags & TARGET_SA_SIGINFO)
1162
                        idx += 2;
1163

    
1164
                if (__put_user(retcodes[idx], rc))
1165
                        return 1;
1166
#if 0
1167
                flush_icache_range((abi_ulong)rc,
1168
                                   (abi_ulong)(rc + 1));
1169
#endif
1170
                retcode = rc_addr + thumb;
1171
        }
1172

    
1173
        env->regs[0] = usig;
1174
        env->regs[13] = frame_addr;
1175
        env->regs[14] = retcode;
1176
        env->regs[15] = handler & (thumb ? ~1 : ~3);
1177
        env->thumb = thumb;
1178

    
1179
#if 0
1180
#ifdef TARGET_CONFIG_CPU_32
1181
        env->cpsr = cpsr;
1182
#endif
1183
#endif
1184

    
1185
        return 0;
1186
}
1187

    
1188
static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1189
                              target_sigset_t *set, CPUState *env)
1190
{
1191
    struct target_sigaltstack stack;
1192
    int i;
1193

    
1194
    /* Clear all the bits of the ucontext we don't use.  */
1195
    memset(uc, 0, offsetof(struct target_ucontext_v2, tuc_mcontext));
1196

    
1197
    memset(&stack, 0, sizeof(stack));
1198
    __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1199
    __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1200
    __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1201
    memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1202

    
1203
    setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1204
    /* FIXME: Save coprocessor signal frame.  */
1205
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1206
        __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1207
    }
1208
}
1209

    
1210
/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1211
static void setup_frame_v1(int usig, struct emulated_sigaction *ka,
1212
                           target_sigset_t *set, CPUState *regs)
1213
{
1214
        struct sigframe_v1 *frame;
1215
        abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1216
        int i;
1217

    
1218
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1219
                return;
1220

    
1221
        setup_sigcontext(&frame->sc, regs, set->sig[0]);
1222

    
1223
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1224
            if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1225
                goto end;
1226
        }
1227

    
1228
        setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1229
                     frame_addr + offsetof(struct sigframe_v1, retcode));
1230

    
1231
end:
1232
        unlock_user_struct(frame, frame_addr, 1);
1233
}
1234

    
1235
static void setup_frame_v2(int usig, struct emulated_sigaction *ka,
1236
                           target_sigset_t *set, CPUState *regs)
1237
{
1238
        struct sigframe_v2 *frame;
1239
        abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1240

    
1241
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1242
                return;
1243

    
1244
        setup_sigframe_v2(&frame->uc, set, regs);
1245

    
1246
        setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1247
                     frame_addr + offsetof(struct sigframe_v2, retcode));
1248

    
1249
        unlock_user_struct(frame, frame_addr, 1);
1250
}
1251

    
1252
static void setup_frame(int usig, struct emulated_sigaction *ka,
1253
                        target_sigset_t *set, CPUState *regs)
1254
{
1255
    if (get_osversion() >= 0x020612) {
1256
        setup_frame_v2(usig, ka, set, regs);
1257
    } else {
1258
        setup_frame_v1(usig, ka, set, regs);
1259
    }
1260
}
1261

    
1262
/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1263
static void setup_rt_frame_v1(int usig, struct emulated_sigaction *ka,
1264
                              target_siginfo_t *info,
1265
                              target_sigset_t *set, CPUState *env)
1266
{
1267
        struct rt_sigframe_v1 *frame;
1268
        abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1269
        struct target_sigaltstack stack;
1270
        int i;
1271
        abi_ulong info_addr, uc_addr;
1272

    
1273
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1274
            return /* 1 */;
1275

    
1276
        info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1277
        __put_user(info_addr, &frame->pinfo);
1278
        uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1279
        __put_user(uc_addr, &frame->puc);
1280
        copy_siginfo_to_user(&frame->info, info);
1281

    
1282
        /* Clear all the bits of the ucontext we don't use.  */
1283
        memset(&frame->uc, 0, offsetof(struct target_ucontext_v1, tuc_mcontext));
1284

    
1285
        memset(&stack, 0, sizeof(stack));
1286
        __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1287
        __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1288
        __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1289
        memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1290

    
1291
        setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1292
        for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1293
            if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1294
                goto end;
1295
        }
1296

    
1297
        setup_return(env, ka, &frame->retcode, frame_addr, usig,
1298
                     frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1299

    
1300
        env->regs[1] = info_addr;
1301
        env->regs[2] = uc_addr;
1302

    
1303
end:
1304
        unlock_user_struct(frame, frame_addr, 1);
1305
}
1306

    
1307
static void setup_rt_frame_v2(int usig, struct emulated_sigaction *ka,
1308
                              target_siginfo_t *info,
1309
                              target_sigset_t *set, CPUState *env)
1310
{
1311
        struct rt_sigframe_v2 *frame;
1312
        abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1313
        abi_ulong info_addr, uc_addr;
1314

    
1315
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1316
            return /* 1 */;
1317

    
1318
        info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1319
        uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1320
        copy_siginfo_to_user(&frame->info, info);
1321

    
1322
        setup_sigframe_v2(&frame->uc, set, env);
1323

    
1324
        setup_return(env, ka, &frame->retcode, frame_addr, usig,
1325
                     frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1326

    
1327
        env->regs[1] = info_addr;
1328
        env->regs[2] = uc_addr;
1329

    
1330
        unlock_user_struct(frame, frame_addr, 1);
1331
}
1332

    
1333
static void setup_rt_frame(int usig, struct emulated_sigaction *ka,
1334
                           target_siginfo_t *info,
1335
                           target_sigset_t *set, CPUState *env)
1336
{
1337
    if (get_osversion() >= 0x020612) {
1338
        setup_rt_frame_v2(usig, ka, info, set, env);
1339
    } else {
1340
        setup_rt_frame_v1(usig, ka, info, set, env);
1341
    }
1342
}
1343

    
1344
static int
1345
restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1346
{
1347
        int err = 0;
1348
        uint32_t cpsr;
1349

    
1350
        __get_user_error(env->regs[0], &sc->arm_r0, err);
1351
        __get_user_error(env->regs[1], &sc->arm_r1, err);
1352
        __get_user_error(env->regs[2], &sc->arm_r2, err);
1353
        __get_user_error(env->regs[3], &sc->arm_r3, err);
1354
        __get_user_error(env->regs[4], &sc->arm_r4, err);
1355
        __get_user_error(env->regs[5], &sc->arm_r5, err);
1356
        __get_user_error(env->regs[6], &sc->arm_r6, err);
1357
        __get_user_error(env->regs[7], &sc->arm_r7, err);
1358
        __get_user_error(env->regs[8], &sc->arm_r8, err);
1359
        __get_user_error(env->regs[9], &sc->arm_r9, err);
1360
        __get_user_error(env->regs[10], &sc->arm_r10, err);
1361
        __get_user_error(env->regs[11], &sc->arm_fp, err);
1362
        __get_user_error(env->regs[12], &sc->arm_ip, err);
1363
        __get_user_error(env->regs[13], &sc->arm_sp, err);
1364
        __get_user_error(env->regs[14], &sc->arm_lr, err);
1365
        __get_user_error(env->regs[15], &sc->arm_pc, err);
1366
#ifdef TARGET_CONFIG_CPU_32
1367
        __get_user_error(cpsr, &sc->arm_cpsr, err);
1368
        cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1369
#endif
1370

    
1371
        err |= !valid_user_regs(env);
1372

    
1373
        return err;
1374
}
1375

    
1376
long do_sigreturn_v1(CPUState *env)
1377
{
1378
        abi_ulong frame_addr;
1379
        struct sigframe_v1 *frame;
1380
        target_sigset_t set;
1381
        sigset_t host_set;
1382
        int i;
1383

    
1384
        /*
1385
         * Since we stacked the signal on a 64-bit boundary,
1386
         * then 'sp' should be word aligned here.  If it's
1387
         * not, then the user is trying to mess with us.
1388
         */
1389
        if (env->regs[13] & 7)
1390
                goto badframe;
1391

    
1392
        frame_addr = env->regs[13];
1393
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1394
                goto badframe;
1395

    
1396
        if (__get_user(set.sig[0], &frame->sc.oldmask))
1397
            goto badframe;
1398
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1399
            if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1400
                goto badframe;
1401
        }
1402

    
1403
        target_to_host_sigset_internal(&host_set, &set);
1404
        sigprocmask(SIG_SETMASK, &host_set, NULL);
1405

    
1406
        if (restore_sigcontext(env, &frame->sc))
1407
                goto badframe;
1408

    
1409
#if 0
1410
        /* Send SIGTRAP if we're single-stepping */
1411
        if (ptrace_cancel_bpt(current))
1412
                send_sig(SIGTRAP, current, 1);
1413
#endif
1414
        unlock_user_struct(frame, frame_addr, 0);
1415
        return env->regs[0];
1416

    
1417
badframe:
1418
        unlock_user_struct(frame, frame_addr, 0);
1419
        force_sig(SIGSEGV /* , current */);
1420
        return 0;
1421
}
1422

    
1423
static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1424
                                 struct target_ucontext_v2 *uc)
1425
{
1426
    sigset_t host_set;
1427

    
1428
    target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1429
    sigprocmask(SIG_SETMASK, &host_set, NULL);
1430

    
1431
    if (restore_sigcontext(env, &uc->tuc_mcontext))
1432
        return 1;
1433

    
1434
    if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1435
        return 1;
1436

    
1437
#if 0
1438
    /* Send SIGTRAP if we're single-stepping */
1439
    if (ptrace_cancel_bpt(current))
1440
            send_sig(SIGTRAP, current, 1);
1441
#endif
1442

    
1443
    return 0;
1444
}
1445

    
1446
long do_sigreturn_v2(CPUState *env)
1447
{
1448
        abi_ulong frame_addr;
1449
        struct sigframe_v2 *frame;
1450

    
1451
        /*
1452
         * Since we stacked the signal on a 64-bit boundary,
1453
         * then 'sp' should be word aligned here.  If it's
1454
         * not, then the user is trying to mess with us.
1455
         */
1456
        if (env->regs[13] & 7)
1457
                goto badframe;
1458

    
1459
        frame_addr = env->regs[13];
1460
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1461
                goto badframe;
1462

    
1463
        if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1464
                goto badframe;
1465

    
1466
        unlock_user_struct(frame, frame_addr, 0);
1467
        return env->regs[0];
1468

    
1469
badframe:
1470
        unlock_user_struct(frame, frame_addr, 0);
1471
        force_sig(SIGSEGV /* , current */);
1472
        return 0;
1473
}
1474

    
1475
long do_sigreturn(CPUState *env)
1476
{
1477
    if (get_osversion() >= 0x020612) {
1478
        return do_sigreturn_v2(env);
1479
    } else {
1480
        return do_sigreturn_v1(env);
1481
    }
1482
}
1483

    
1484
long do_rt_sigreturn_v1(CPUState *env)
1485
{
1486
        abi_ulong frame_addr;
1487
        struct rt_sigframe_v1 *frame;
1488
        sigset_t host_set;
1489

    
1490
        /*
1491
         * Since we stacked the signal on a 64-bit boundary,
1492
         * then 'sp' should be word aligned here.  If it's
1493
         * not, then the user is trying to mess with us.
1494
         */
1495
        if (env->regs[13] & 7)
1496
                goto badframe;
1497

    
1498
        frame_addr = env->regs[13];
1499
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1500
                goto badframe;
1501

    
1502
        target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1503
        sigprocmask(SIG_SETMASK, &host_set, NULL);
1504

    
1505
        if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1506
                goto badframe;
1507

    
1508
        if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1509
                goto badframe;
1510

    
1511
#if 0
1512
        /* Send SIGTRAP if we're single-stepping */
1513
        if (ptrace_cancel_bpt(current))
1514
                send_sig(SIGTRAP, current, 1);
1515
#endif
1516
        unlock_user_struct(frame, frame_addr, 0);
1517
        return env->regs[0];
1518

    
1519
badframe:
1520
        unlock_user_struct(frame, frame_addr, 0);
1521
        force_sig(SIGSEGV /* , current */);
1522
        return 0;
1523
}
1524

    
1525
long do_rt_sigreturn_v2(CPUState *env)
1526
{
1527
        abi_ulong frame_addr;
1528
        struct rt_sigframe_v2 *frame;
1529

    
1530
        /*
1531
         * Since we stacked the signal on a 64-bit boundary,
1532
         * then 'sp' should be word aligned here.  If it's
1533
         * not, then the user is trying to mess with us.
1534
         */
1535
        if (env->regs[13] & 7)
1536
                goto badframe;
1537

    
1538
        frame_addr = env->regs[13];
1539
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1540
                goto badframe;
1541

    
1542
        if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1543
                goto badframe;
1544

    
1545
        unlock_user_struct(frame, frame_addr, 0);
1546
        return env->regs[0];
1547

    
1548
badframe:
1549
        unlock_user_struct(frame, frame_addr, 0);
1550
        force_sig(SIGSEGV /* , current */);
1551
        return 0;
1552
}
1553

    
1554
long do_rt_sigreturn(CPUState *env)
1555
{
1556
    if (get_osversion() >= 0x020612) {
1557
        return do_rt_sigreturn_v2(env);
1558
    } else {
1559
        return do_rt_sigreturn_v1(env);
1560
    }
1561
}
1562

    
1563
#elif defined(TARGET_SPARC)
1564

    
1565
#define __SUNOS_MAXWIN   31
1566

    
1567
/* This is what SunOS does, so shall I. */
1568
struct target_sigcontext {
1569
        abi_ulong sigc_onstack;      /* state to restore */
1570

    
1571
        abi_ulong sigc_mask;         /* sigmask to restore */
1572
        abi_ulong sigc_sp;           /* stack pointer */
1573
        abi_ulong sigc_pc;           /* program counter */
1574
        abi_ulong sigc_npc;          /* next program counter */
1575
        abi_ulong sigc_psr;          /* for condition codes etc */
1576
        abi_ulong sigc_g1;           /* User uses these two registers */
1577
        abi_ulong sigc_o0;           /* within the trampoline code. */
1578

    
1579
        /* Now comes information regarding the users window set
1580
         * at the time of the signal.
1581
         */
1582
        abi_ulong sigc_oswins;       /* outstanding windows */
1583

    
1584
        /* stack ptrs for each regwin buf */
1585
        char *sigc_spbuf[__SUNOS_MAXWIN];
1586

    
1587
        /* Windows to restore after signal */
1588
        struct {
1589
                abi_ulong locals[8];
1590
                abi_ulong ins[8];
1591
        } sigc_wbuf[__SUNOS_MAXWIN];
1592
};
1593
/* A Sparc stack frame */
1594
struct sparc_stackf {
1595
        abi_ulong locals[8];
1596
        abi_ulong ins[6];
1597
        struct sparc_stackf *fp;
1598
        abi_ulong callers_pc;
1599
        char *structptr;
1600
        abi_ulong xargs[6];
1601
        abi_ulong xxargs[1];
1602
};
1603

    
1604
typedef struct {
1605
        struct {
1606
                abi_ulong psr;
1607
                abi_ulong pc;
1608
                abi_ulong npc;
1609
                abi_ulong y;
1610
                abi_ulong u_regs[16]; /* globals and ins */
1611
        }               si_regs;
1612
        int             si_mask;
1613
} __siginfo_t;
1614

    
1615
typedef struct {
1616
        unsigned   long si_float_regs [32];
1617
        unsigned   long si_fsr;
1618
        unsigned   long si_fpqdepth;
1619
        struct {
1620
                unsigned long *insn_addr;
1621
                unsigned long insn;
1622
        } si_fpqueue [16];
1623
} qemu_siginfo_fpu_t;
1624

    
1625

    
1626
struct target_signal_frame {
1627
        struct sparc_stackf        ss;
1628
        __siginfo_t                info;
1629
        abi_ulong               fpu_save;
1630
        abi_ulong                insns[2] __attribute__ ((aligned (8)));
1631
        abi_ulong                extramask[TARGET_NSIG_WORDS - 1];
1632
        abi_ulong                extra_size; /* Should be 0 */
1633
        qemu_siginfo_fpu_t        fpu_state;
1634
};
1635
struct target_rt_signal_frame {
1636
        struct sparc_stackf        ss;
1637
        siginfo_t                info;
1638
        abi_ulong                regs[20];
1639
        sigset_t                mask;
1640
        abi_ulong               fpu_save;
1641
        unsigned int                insns[2];
1642
        stack_t                        stack;
1643
        unsigned int                extra_size; /* Should be 0 */
1644
        qemu_siginfo_fpu_t        fpu_state;
1645
};
1646

    
1647
#define UREG_O0        16
1648
#define UREG_O6        22
1649
#define UREG_I0        0
1650
#define UREG_I1        1
1651
#define UREG_I2        2
1652
#define UREG_I3        3
1653
#define UREG_I4        4
1654
#define UREG_I5        5
1655
#define UREG_I6        6
1656
#define UREG_I7        7
1657
#define UREG_L0               8
1658
#define UREG_FP        UREG_I6
1659
#define UREG_SP        UREG_O6
1660

    
1661
static inline abi_ulong get_sigframe(struct emulated_sigaction *sa, 
1662
                                     CPUState *env, unsigned long framesize)
1663
{
1664
        abi_ulong sp;
1665

    
1666
        sp = env->regwptr[UREG_FP];
1667

    
1668
        /* This is the X/Open sanctioned signal stack switching.  */
1669
        if (sa->sa.sa_flags & TARGET_SA_ONSTACK) {
1670
            if (!on_sig_stack(sp)
1671
                && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1672
                sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1673
        }
1674
        return sp - framesize;
1675
}
1676

    
1677
static int
1678
setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1679
{
1680
        int err = 0, i;
1681

    
1682
        err |= __put_user(env->psr, &si->si_regs.psr);
1683
        err |= __put_user(env->pc, &si->si_regs.pc);
1684
        err |= __put_user(env->npc, &si->si_regs.npc);
1685
        err |= __put_user(env->y, &si->si_regs.y);
1686
        for (i=0; i < 8; i++) {
1687
                err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1688
        }
1689
        for (i=0; i < 8; i++) {
1690
                err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1691
        }
1692
        err |= __put_user(mask, &si->si_mask);
1693
        return err;
1694
}
1695

    
1696
#if 0
1697
static int
1698
setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1699
                 CPUState *env, unsigned long mask)
1700
{
1701
        int err = 0;
1702

1703
        err |= __put_user(mask, &sc->sigc_mask);
1704
        err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1705
        err |= __put_user(env->pc, &sc->sigc_pc);
1706
        err |= __put_user(env->npc, &sc->sigc_npc);
1707
        err |= __put_user(env->psr, &sc->sigc_psr);
1708
        err |= __put_user(env->gregs[1], &sc->sigc_g1);
1709
        err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1710

1711
        return err;
1712
}
1713
#endif
1714
#define NF_ALIGNEDSZ  (((sizeof(struct target_signal_frame) + 7) & (~7)))
1715

    
1716
static void setup_frame(int sig, struct emulated_sigaction *ka,
1717
                        target_sigset_t *set, CPUState *env)
1718
{
1719
        abi_ulong sf_addr;
1720
        struct target_signal_frame *sf;
1721
        int sigframe_size, err, i;
1722

    
1723
        /* 1. Make sure everything is clean */
1724
        //synchronize_user_stack();
1725

    
1726
        sigframe_size = NF_ALIGNEDSZ;
1727
        sf_addr = get_sigframe(ka, env, sigframe_size);
1728

    
1729
        sf = lock_user(VERIFY_WRITE, sf_addr, 
1730
                       sizeof(struct target_signal_frame), 0);
1731
        if (!sf)
1732
                goto sigsegv;
1733
                
1734
        //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1735
#if 0
1736
        if (invalid_frame_pointer(sf, sigframe_size))
1737
                goto sigill_and_return;
1738
#endif
1739
        /* 2. Save the current process state */
1740
        err = setup___siginfo(&sf->info, env, set->sig[0]);
1741
        err |= __put_user(0, &sf->extra_size);
1742

    
1743
        //err |= save_fpu_state(regs, &sf->fpu_state);
1744
        //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1745

    
1746
        err |= __put_user(set->sig[0], &sf->info.si_mask);
1747
        for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1748
                err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1749
        }
1750

    
1751
        for (i = 0; i < 8; i++) {
1752
                  err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1753
        }
1754
        for (i = 0; i < 8; i++) {
1755
                  err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1756
        }
1757
        if (err)
1758
                goto sigsegv;
1759

    
1760
        /* 3. signal handler back-trampoline and parameters */
1761
        env->regwptr[UREG_FP] = sf_addr;
1762
        env->regwptr[UREG_I0] = sig;
1763
        env->regwptr[UREG_I1] = sf_addr + 
1764
                offsetof(struct target_signal_frame, info);
1765
        env->regwptr[UREG_I2] = sf_addr + 
1766
                offsetof(struct target_signal_frame, info);
1767

    
1768
        /* 4. signal handler */
1769
        env->pc = ka->sa._sa_handler;
1770
        env->npc = (env->pc + 4);
1771
        /* 5. return to kernel instructions */
1772
        if (ka->sa.sa_restorer)
1773
                env->regwptr[UREG_I7] = ka->sa.sa_restorer;
1774
        else {
1775
                uint32_t val32;
1776

    
1777
                env->regwptr[UREG_I7] = sf_addr + 
1778
                        offsetof(struct target_signal_frame, insns) - 2 * 4;
1779

    
1780
                /* mov __NR_sigreturn, %g1 */
1781
                val32 = 0x821020d8;
1782
                err |= __put_user(val32, &sf->insns[0]);
1783

    
1784
                /* t 0x10 */
1785
                val32 = 0x91d02010;
1786
                err |= __put_user(val32, &sf->insns[1]);
1787
                if (err)
1788
                        goto sigsegv;
1789

    
1790
                /* Flush instruction space. */
1791
                //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
1792
                //                tb_flush(env);
1793
        }
1794
        unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1795
        return;
1796
#if 0
1797
sigill_and_return:
1798
        force_sig(TARGET_SIGILL);
1799
#endif
1800
sigsegv:
1801
        //fprintf(stderr, "force_sig\n");
1802
        unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
1803
        force_sig(TARGET_SIGSEGV);
1804
}
1805
static inline int
1806
restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
1807
{
1808
        int err;
1809
#if 0
1810
#ifdef CONFIG_SMP
1811
        if (current->flags & PF_USEDFPU)
1812
                regs->psr &= ~PSR_EF;
1813
#else
1814
        if (current == last_task_used_math) {
1815
                last_task_used_math = 0;
1816
                regs->psr &= ~PSR_EF;
1817
        }
1818
#endif
1819
        current->used_math = 1;
1820
        current->flags &= ~PF_USEDFPU;
1821
#endif
1822
#if 0
1823
        if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
1824
                return -EFAULT;
1825
#endif
1826

    
1827
#if 0
1828
        /* XXX: incorrect */
1829
        err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
1830
                                     (sizeof(unsigned long) * 32));
1831
#endif
1832
        err |= __get_user(env->fsr, &fpu->si_fsr);
1833
#if 0
1834
        err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
1835
        if (current->thread.fpqdepth != 0)
1836
                err |= __copy_from_user(&current->thread.fpqueue[0],
1837
                                        &fpu->si_fpqueue[0],
1838
                                        ((sizeof(unsigned long) +
1839
                                        (sizeof(unsigned long *)))*16));
1840
#endif
1841
        return err;
1842
}
1843

    
1844

    
1845
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
1846
                           target_siginfo_t *info,
1847
                           target_sigset_t *set, CPUState *env)
1848
{
1849
    fprintf(stderr, "setup_rt_frame: not implemented\n");
1850
}
1851

    
1852
long do_sigreturn(CPUState *env)
1853
{
1854
        abi_ulong sf_addr;
1855
        struct target_signal_frame *sf;
1856
        uint32_t up_psr, pc, npc;
1857
        target_sigset_t set;
1858
        sigset_t host_set;
1859
        abi_ulong fpu_save_addr;
1860
        int err, i;
1861

    
1862
        sf_addr = env->regwptr[UREG_FP];
1863
        if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
1864
                goto segv_and_exit;
1865
#if 0
1866
        fprintf(stderr, "sigreturn\n");
1867
        fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1868
#endif
1869
        //cpu_dump_state(env, stderr, fprintf, 0);
1870

    
1871
        /* 1. Make sure we are not getting garbage from the user */
1872

    
1873
        if (sf_addr & 3)
1874
                goto segv_and_exit;
1875

    
1876
        err = __get_user(pc,  &sf->info.si_regs.pc);
1877
        err |= __get_user(npc, &sf->info.si_regs.npc);
1878

    
1879
        if ((pc | npc) & 3)
1880
                goto segv_and_exit;
1881

    
1882
        /* 2. Restore the state */
1883
        err |= __get_user(up_psr, &sf->info.si_regs.psr);
1884

    
1885
        /* User can only change condition codes and FPU enabling in %psr. */
1886
        env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
1887
                  | (env->psr & ~(PSR_ICC /* | PSR_EF */));
1888

    
1889
        env->pc = pc;
1890
        env->npc = npc;
1891
        err |= __get_user(env->y, &sf->info.si_regs.y);
1892
        for (i=0; i < 8; i++) {
1893
                err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
1894
        }
1895
        for (i=0; i < 8; i++) {
1896
                err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
1897
        }
1898

    
1899
        err |= __get_user(fpu_save_addr, &sf->fpu_save);
1900

    
1901
        //if (fpu_save)
1902
        //        err |= restore_fpu_state(env, fpu_save);
1903

    
1904
        /* This is pretty much atomic, no amount locking would prevent
1905
         * the races which exist anyways.
1906
         */
1907
        err |= __get_user(set.sig[0], &sf->info.si_mask);
1908
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1909
            err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
1910
        }
1911

    
1912
        target_to_host_sigset_internal(&host_set, &set);
1913
        sigprocmask(SIG_SETMASK, &host_set, NULL);
1914

    
1915
        if (err)
1916
                goto segv_and_exit;
1917
        unlock_user_struct(sf, sf_addr, 0);
1918
        return env->regwptr[0];
1919

    
1920
segv_and_exit:
1921
        unlock_user_struct(sf, sf_addr, 0);
1922
        force_sig(TARGET_SIGSEGV);
1923
}
1924

    
1925
long do_rt_sigreturn(CPUState *env)
1926
{
1927
    fprintf(stderr, "do_rt_sigreturn: not implemented\n");
1928
    return -TARGET_ENOSYS;
1929
}
1930

    
1931
#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1932
#define MC_TSTATE 0
1933
#define MC_PC 1
1934
#define MC_NPC 2
1935
#define MC_Y 3
1936
#define MC_G1 4
1937
#define MC_G2 5
1938
#define MC_G3 6
1939
#define MC_G4 7
1940
#define MC_G5 8
1941
#define MC_G6 9
1942
#define MC_G7 10
1943
#define MC_O0 11
1944
#define MC_O1 12
1945
#define MC_O2 13
1946
#define MC_O3 14
1947
#define MC_O4 15
1948
#define MC_O5 16
1949
#define MC_O6 17
1950
#define MC_O7 18
1951
#define MC_NGREG 19
1952

    
1953
typedef abi_ulong target_mc_greg_t;
1954
typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
1955

    
1956
struct target_mc_fq {
1957
    abi_ulong *mcfq_addr;
1958
    uint32_t mcfq_insn;
1959
};
1960

    
1961
struct target_mc_fpu {
1962
    union {
1963
        uint32_t sregs[32];
1964
        uint64_t dregs[32];
1965
        //uint128_t qregs[16];
1966
    } mcfpu_fregs;
1967
    abi_ulong mcfpu_fsr;
1968
    abi_ulong mcfpu_fprs;
1969
    abi_ulong mcfpu_gsr;
1970
    struct target_mc_fq *mcfpu_fq;
1971
    unsigned char mcfpu_qcnt;
1972
    unsigned char mcfpu_qentsz;
1973
    unsigned char mcfpu_enab;
1974
};
1975
typedef struct target_mc_fpu target_mc_fpu_t;
1976

    
1977
typedef struct {
1978
    target_mc_gregset_t mc_gregs;
1979
    target_mc_greg_t mc_fp;
1980
    target_mc_greg_t mc_i7;
1981
    target_mc_fpu_t mc_fpregs;
1982
} target_mcontext_t;
1983

    
1984
struct target_ucontext {
1985
    struct target_ucontext *uc_link;
1986
    abi_ulong uc_flags;
1987
    target_sigset_t uc_sigmask;
1988
    target_mcontext_t uc_mcontext;
1989
};
1990

    
1991
/* A V9 register window */
1992
struct target_reg_window {
1993
    abi_ulong locals[8];
1994
    abi_ulong ins[8];
1995
};
1996

    
1997
#define TARGET_STACK_BIAS 2047
1998

    
1999
/* {set, get}context() needed for 64-bit SparcLinux userland. */
2000
void sparc64_set_context(CPUSPARCState *env)
2001
{
2002
    abi_ulong ucp_addr;
2003
    struct target_ucontext *ucp;
2004
    target_mc_gregset_t *grp;
2005
    abi_ulong pc, npc, tstate;
2006
    abi_ulong fp, i7, w_addr;
2007
    unsigned char fenab;
2008
    int err;
2009
    unsigned int i;
2010

    
2011
    ucp_addr = env->regwptr[UREG_I0];
2012
    if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2013
        goto do_sigsegv;
2014
    grp  = &ucp->uc_mcontext.mc_gregs;
2015
    err  = __get_user(pc, &((*grp)[MC_PC]));
2016
    err |= __get_user(npc, &((*grp)[MC_NPC]));
2017
    if (err || ((pc | npc) & 3))
2018
        goto do_sigsegv;
2019
    if (env->regwptr[UREG_I1]) {
2020
        target_sigset_t target_set;
2021
        sigset_t set;
2022

    
2023
        if (TARGET_NSIG_WORDS == 1) {
2024
            if (__get_user(target_set.sig[0], &ucp->uc_sigmask.sig[0]))
2025
                goto do_sigsegv;
2026
        } else {
2027
            abi_ulong *src, *dst;
2028
            src = ucp->uc_sigmask.sig;
2029
            dst = target_set.sig;
2030
            for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2031
                 i++, dst++, src++)
2032
                err |= __get_user(*dst, src);
2033
            if (err)
2034
                goto do_sigsegv;
2035
        }
2036
        target_to_host_sigset_internal(&set, &target_set);
2037
        sigprocmask(SIG_SETMASK, &set, NULL);
2038
    }
2039
    env->pc = pc;
2040
    env->npc = npc;
2041
    err |= __get_user(env->y, &((*grp)[MC_Y]));
2042
    err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2043
    env->asi = (tstate >> 24) & 0xff;
2044
    PUT_CCR(env, tstate >> 32);
2045
    PUT_CWP64(env, tstate & 0x1f);
2046
    err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2047
    err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2048
    err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2049
    err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2050
    err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2051
    err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2052
    err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2053
    err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2054
    err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2055
    err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2056
    err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2057
    err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2058
    err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2059
    err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2060
    err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2061

    
2062
    err |= __get_user(fp, &(ucp->uc_mcontext.mc_fp));
2063
    err |= __get_user(i7, &(ucp->uc_mcontext.mc_i7));
2064

    
2065
    w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2066
    if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]), 
2067
                 abi_ulong) != 0)
2068
        goto do_sigsegv;
2069
    if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]), 
2070
                 abi_ulong) != 0)
2071
        goto do_sigsegv;
2072
    err |= __get_user(fenab, &(ucp->uc_mcontext.mc_fpregs.mcfpu_enab));
2073
    err |= __get_user(env->fprs, &(ucp->uc_mcontext.mc_fpregs.mcfpu_fprs));
2074
    {
2075
        uint32_t *src, *dst;
2076
        src = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2077
        dst = env->fpr;
2078
        /* XXX: check that the CPU storage is the same as user context */
2079
        for (i = 0; i < 64; i++, dst++, src++)
2080
            err |= __get_user(*dst, src);
2081
    }
2082
    err |= __get_user(env->fsr,
2083
                      &(ucp->uc_mcontext.mc_fpregs.mcfpu_fsr));
2084
    err |= __get_user(env->gsr,
2085
                      &(ucp->uc_mcontext.mc_fpregs.mcfpu_gsr));
2086
    if (err)
2087
        goto do_sigsegv;
2088
    unlock_user_struct(ucp, ucp_addr, 0);
2089
    return;
2090
 do_sigsegv:
2091
    unlock_user_struct(ucp, ucp_addr, 0);
2092
    force_sig(SIGSEGV);
2093
}
2094

    
2095
void sparc64_get_context(CPUSPARCState *env)
2096
{
2097
    abi_ulong ucp_addr;
2098
    struct target_ucontext *ucp;
2099
    target_mc_gregset_t *grp;
2100
    target_mcontext_t *mcp;
2101
    abi_ulong fp, i7, w_addr;
2102
    int err;
2103
    unsigned int i;
2104
    target_sigset_t target_set;
2105
    sigset_t set;
2106

    
2107
    ucp_addr = env->regwptr[UREG_I0];
2108
    if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2109
        goto do_sigsegv;
2110
    
2111
    mcp = &ucp->uc_mcontext;
2112
    grp = &mcp->mc_gregs;
2113

    
2114
    /* Skip over the trap instruction, first. */
2115
    env->pc = env->npc;
2116
    env->npc += 4;
2117

    
2118
    err = 0;
2119

    
2120
    sigprocmask(0, NULL, &set);
2121
    host_to_target_sigset_internal(&target_set, &set);
2122
    if (TARGET_NSIG_WORDS == 1) {
2123
        err |= __put_user(target_set.sig[0],
2124
                          (abi_ulong *)&ucp->uc_sigmask);
2125
    } else {
2126
        abi_ulong *src, *dst;
2127
        src = target_set.sig;
2128
        dst = ucp->uc_sigmask.sig;
2129
        for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2130
             i++, dst++, src++)
2131
            err |= __put_user(*src, dst);
2132
        if (err)
2133
            goto do_sigsegv;
2134
    }
2135

    
2136
    /* XXX: tstate must be saved properly */
2137
    //    err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2138
    err |= __put_user(env->pc, &((*grp)[MC_PC]));
2139
    err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2140
    err |= __put_user(env->y, &((*grp)[MC_Y]));
2141
    err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2142
    err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2143
    err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2144
    err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2145
    err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2146
    err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2147
    err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2148
    err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2149
    err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2150
    err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2151
    err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2152
    err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2153
    err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2154
    err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2155
    err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2156

    
2157
    w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2158
    fp = i7 = 0;
2159
    if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]), 
2160
                 abi_ulong) != 0)
2161
        goto do_sigsegv;
2162
    if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]), 
2163
                 abi_ulong) != 0)
2164
        goto do_sigsegv;
2165
    err |= __put_user(fp, &(mcp->mc_fp));
2166
    err |= __put_user(i7, &(mcp->mc_i7));
2167

    
2168
    {
2169
        uint32_t *src, *dst;
2170
        src = env->fpr;
2171
        dst = ucp->uc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2172
        /* XXX: check that the CPU storage is the same as user context */
2173
        for (i = 0; i < 64; i++, dst++, src++)
2174
            err |= __put_user(*src, dst);
2175
    }
2176
    err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2177
    err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2178
    err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2179

    
2180
    if (err)
2181
        goto do_sigsegv;
2182
    unlock_user_struct(ucp, ucp_addr, 1);
2183
    return;
2184
 do_sigsegv:
2185
    unlock_user_struct(ucp, ucp_addr, 1);
2186
    force_sig(SIGSEGV);
2187
}
2188
#endif
2189
#elif defined(TARGET_ABI_MIPSN64)
2190

    
2191
# warning signal handling not implemented
2192

    
2193
static void setup_frame(int sig, struct emulated_sigaction *ka,
2194
                        target_sigset_t *set, CPUState *env)
2195
{
2196
    fprintf(stderr, "setup_frame: not implemented\n");
2197
}
2198

    
2199
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2200
                           target_siginfo_t *info,
2201
                           target_sigset_t *set, CPUState *env)
2202
{
2203
    fprintf(stderr, "setup_rt_frame: not implemented\n");
2204
}
2205

    
2206
long do_sigreturn(CPUState *env)
2207
{
2208
    fprintf(stderr, "do_sigreturn: not implemented\n");
2209
    return -TARGET_ENOSYS;
2210
}
2211

    
2212
long do_rt_sigreturn(CPUState *env)
2213
{
2214
    fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2215
    return -TARGET_ENOSYS;
2216
}
2217

    
2218
#elif defined(TARGET_ABI_MIPSN32)
2219

    
2220
# warning signal handling not implemented
2221

    
2222
static void setup_frame(int sig, struct emulated_sigaction *ka,
2223
                        target_sigset_t *set, CPUState *env)
2224
{
2225
    fprintf(stderr, "setup_frame: not implemented\n");
2226
}
2227

    
2228
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2229
                           target_siginfo_t *info,
2230
                           target_sigset_t *set, CPUState *env)
2231
{
2232
    fprintf(stderr, "setup_rt_frame: not implemented\n");
2233
}
2234

    
2235
long do_sigreturn(CPUState *env)
2236
{
2237
    fprintf(stderr, "do_sigreturn: not implemented\n");
2238
    return -TARGET_ENOSYS;
2239
}
2240

    
2241
long do_rt_sigreturn(CPUState *env)
2242
{
2243
    fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2244
    return -TARGET_ENOSYS;
2245
}
2246

    
2247
#elif defined(TARGET_ABI_MIPSO32)
2248

    
2249
struct target_sigcontext {
2250
    uint32_t   sc_regmask;     /* Unused */
2251
    uint32_t   sc_status;
2252
    uint64_t   sc_pc;
2253
    uint64_t   sc_regs[32];
2254
    uint64_t   sc_fpregs[32];
2255
    uint32_t   sc_ownedfp;     /* Unused */
2256
    uint32_t   sc_fpc_csr;
2257
    uint32_t   sc_fpc_eir;     /* Unused */
2258
    uint32_t   sc_used_math;
2259
    uint32_t   sc_dsp;         /* dsp status, was sc_ssflags */
2260
    uint64_t   sc_mdhi;
2261
    uint64_t   sc_mdlo;
2262
    target_ulong   sc_hi1;         /* Was sc_cause */
2263
    target_ulong   sc_lo1;         /* Was sc_badvaddr */
2264
    target_ulong   sc_hi2;         /* Was sc_sigset[4] */
2265
    target_ulong   sc_lo2;
2266
    target_ulong   sc_hi3;
2267
    target_ulong   sc_lo3;
2268
};
2269

    
2270
struct sigframe {
2271
    uint32_t sf_ass[4];                        /* argument save space for o32 */
2272
    uint32_t sf_code[2];                        /* signal trampoline */
2273
    struct target_sigcontext sf_sc;
2274
    target_sigset_t sf_mask;
2275
};
2276

    
2277
/* Install trampoline to jump back from signal handler */
2278
static inline int install_sigtramp(unsigned int *tramp,   unsigned int syscall)
2279
{
2280
    int err;
2281

    
2282
    /*
2283
    * Set up the return code ...
2284
    *
2285
    *         li      v0, __NR__foo_sigreturn
2286
    *         syscall
2287
    */
2288

    
2289
    err = __put_user(0x24020000 + syscall, tramp + 0);
2290
    err |= __put_user(0x0000000c          , tramp + 1);
2291
    /* flush_cache_sigtramp((unsigned long) tramp); */
2292
    return err;
2293
}
2294

    
2295
static inline int
2296
setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2297
{
2298
    int err = 0;
2299

    
2300
    err |= __put_user(regs->PC[regs->current_tc], &sc->sc_pc);
2301

    
2302
#define save_gp_reg(i) do {                                                           \
2303
        err |= __put_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]);        \
2304
    } while(0)
2305
    __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2306
    save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2307
    save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2308
    save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2309
    save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2310
    save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2311
    save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2312
    save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2313
    save_gp_reg(31);
2314
#undef save_gp_reg
2315

    
2316
    err |= __put_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2317
    err |= __put_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
2318

    
2319
    /* Not used yet, but might be useful if we ever have DSP suppport */
2320
#if 0
2321
    if (cpu_has_dsp) {
2322
        err |= __put_user(mfhi1(), &sc->sc_hi1);
2323
        err |= __put_user(mflo1(), &sc->sc_lo1);
2324
        err |= __put_user(mfhi2(), &sc->sc_hi2);
2325
        err |= __put_user(mflo2(), &sc->sc_lo2);
2326
        err |= __put_user(mfhi3(), &sc->sc_hi3);
2327
        err |= __put_user(mflo3(), &sc->sc_lo3);
2328
        err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2329
    }
2330
    /* same with 64 bit */
2331
#ifdef CONFIG_64BIT
2332
    err |= __put_user(regs->hi, &sc->sc_hi[0]);
2333
    err |= __put_user(regs->lo, &sc->sc_lo[0]);
2334
    if (cpu_has_dsp) {
2335
        err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2336
        err |= __put_user(mflo1(), &sc->sc_lo[1]);
2337
        err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2338
        err |= __put_user(mflo2(), &sc->sc_lo[2]);
2339
        err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2340
        err |= __put_user(mflo3(), &sc->sc_lo[3]);
2341
        err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2342
    }
2343
#endif
2344
#endif
2345

    
2346
#if 0
2347
    err |= __put_user(!!used_math(), &sc->sc_used_math);
2348

2349
    if (!used_math())
2350
        goto out;
2351

2352
    /*
2353
    * Save FPU state to signal context.  Signal handler will "inherit"
2354
    * current FPU state.
2355
    */
2356
    preempt_disable();
2357

2358
    if (!is_fpu_owner()) {
2359
        own_fpu();
2360
        restore_fp(current);
2361
    }
2362
    err |= save_fp_context(sc);
2363

2364
    preempt_enable();
2365
    out:
2366
#endif
2367
    return err;
2368
}
2369

    
2370
static inline int
2371
restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2372
{
2373
    int err = 0;
2374

    
2375
    err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2376

    
2377
    err |= __get_user(regs->HI[regs->current_tc][0], &sc->sc_mdhi);
2378
    err |= __get_user(regs->LO[regs->current_tc][0], &sc->sc_mdlo);
2379

    
2380
#define restore_gp_reg(i) do {                                                           \
2381
        err |= __get_user(regs->gpr[regs->current_tc][i], &sc->sc_regs[i]);                \
2382
    } while(0)
2383
    restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2384
    restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2385
    restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2386
    restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2387
    restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2388
    restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2389
    restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2390
    restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2391
    restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2392
    restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2393
    restore_gp_reg(31);
2394
#undef restore_gp_reg
2395

    
2396
#if 0
2397
    if (cpu_has_dsp) {
2398
        err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2399
        err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2400
        err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2401
        err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2402
        err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2403
        err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2404
        err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2405
    }
2406
#ifdef CONFIG_64BIT
2407
    err |= __get_user(regs->hi, &sc->sc_hi[0]);
2408
    err |= __get_user(regs->lo, &sc->sc_lo[0]);
2409
    if (cpu_has_dsp) {
2410
        err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2411
        err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2412
        err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2413
        err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2414
        err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2415
        err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2416
        err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2417
    }
2418
#endif
2419

    
2420
    err |= __get_user(used_math, &sc->sc_used_math);
2421
    conditional_used_math(used_math);
2422

    
2423
    preempt_disable();
2424

    
2425
    if (used_math()) {
2426
        /* restore fpu context if we have used it before */
2427
        own_fpu();
2428
        err |= restore_fp_context(sc);
2429
    } else {
2430
        /* signal handler may have used FPU.  Give it up. */
2431
        lose_fpu();
2432
    }
2433

    
2434
    preempt_enable();
2435
#endif
2436
    return err;
2437
}
2438
/*
2439
 * Determine which stack to use..
2440
 */
2441
static inline abi_ulong
2442
get_sigframe(struct emulated_sigaction *ka, CPUState *regs, size_t frame_size)
2443
{
2444
    unsigned long sp;
2445

    
2446
    /* Default to using normal stack */
2447
    sp = regs->gpr[regs->current_tc][29];
2448

    
2449
    /*
2450
     * FPU emulator may have it's own trampoline active just
2451
     * above the user stack, 16-bytes before the next lowest
2452
     * 16 byte boundary.  Try to avoid trashing it.
2453
     */
2454
    sp -= 32;
2455

    
2456
    /* This is the X/Open sanctioned signal stack switching.  */
2457
    if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2458
        sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2459
    }
2460

    
2461
    return (sp - frame_size) & ~7;
2462
}
2463

    
2464
/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2465
static void setup_frame(int sig, struct emulated_sigaction * ka,
2466
                        target_sigset_t *set, CPUState *regs)
2467
{
2468
    struct sigframe *frame;
2469
    abi_ulong frame_addr;
2470
    int i;
2471

    
2472
    frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2473
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2474
        goto give_sigsegv;
2475

    
2476
    install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2477

    
2478
    if(setup_sigcontext(regs, &frame->sf_sc))
2479
        goto give_sigsegv;
2480

    
2481
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2482
        if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2483
            goto give_sigsegv;
2484
    }
2485

    
2486
    /*
2487
    * Arguments to signal handler:
2488
    *
2489
    *   a0 = signal number
2490
    *   a1 = 0 (should be cause)
2491
    *   a2 = pointer to struct sigcontext
2492
    *
2493
    * $25 and PC point to the signal handler, $29 points to the
2494
    * struct sigframe.
2495
    */
2496
    regs->gpr[regs->current_tc][ 4] = sig;
2497
    regs->gpr[regs->current_tc][ 5] = 0;
2498
    regs->gpr[regs->current_tc][ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2499
    regs->gpr[regs->current_tc][29] = frame_addr;
2500
    regs->gpr[regs->current_tc][31] = frame_addr + offsetof(struct sigframe, sf_code);
2501
    /* The original kernel code sets CP0_EPC to the handler
2502
    * since it returns to userland using eret
2503
    * we cannot do this here, and we must set PC directly */
2504
    regs->PC[regs->current_tc] = regs->gpr[regs->current_tc][25] = ka->sa._sa_handler;
2505
    unlock_user_struct(frame, frame_addr, 1);
2506
    return;
2507

    
2508
give_sigsegv:
2509
    unlock_user_struct(frame, frame_addr, 1);
2510
    force_sig(TARGET_SIGSEGV/*, current*/);
2511
    return;
2512
}
2513

    
2514
long do_sigreturn(CPUState *regs)
2515
{
2516
    struct sigframe *frame;
2517
    abi_ulong frame_addr;
2518
    sigset_t blocked;
2519
    target_sigset_t target_set;
2520
    int i;
2521

    
2522
#if defined(DEBUG_SIGNAL)
2523
    fprintf(stderr, "do_sigreturn\n");
2524
#endif
2525
    frame_addr = regs->gpr[regs->current_tc][29];
2526
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2527
           goto badframe;
2528

    
2529
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2530
           if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2531
            goto badframe;
2532
    }
2533

    
2534
    target_to_host_sigset_internal(&blocked, &target_set);
2535
    sigprocmask(SIG_SETMASK, &blocked, NULL);
2536

    
2537
    if (restore_sigcontext(regs, &frame->sf_sc))
2538
           goto badframe;
2539

    
2540
#if 0
2541
    /*
2542
     * Don't let your children do this ...
2543
     */
2544
    __asm__ __volatile__(
2545
           "move\t$29, %0\n\t"
2546
           "j\tsyscall_exit"
2547
           :/* no outputs */
2548
           :"r" (&regs));
2549
    /* Unreached */
2550
#endif
2551

    
2552
    regs->PC[regs->current_tc] = regs->CP0_EPC;
2553
    /* I am not sure this is right, but it seems to work
2554
    * maybe a problem with nested signals ? */
2555
    regs->CP0_EPC = 0;
2556
    return 0;
2557

    
2558
badframe:
2559
    force_sig(TARGET_SIGSEGV/*, current*/);
2560
    return 0;
2561
}
2562

    
2563
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2564
                           target_siginfo_t *info,
2565
                           target_sigset_t *set, CPUState *env)
2566
{
2567
    fprintf(stderr, "setup_rt_frame: not implemented\n");
2568
}
2569

    
2570
long do_rt_sigreturn(CPUState *env)
2571
{
2572
    fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2573
    return -TARGET_ENOSYS;
2574
}
2575

    
2576
#elif defined(TARGET_SH4)
2577

    
2578
/*
2579
 * code and data structures from linux kernel:
2580
 * include/asm-sh/sigcontext.h
2581
 * arch/sh/kernel/signal.c
2582
 */
2583

    
2584
struct target_sigcontext {
2585
    target_ulong  oldmask;
2586

    
2587
    /* CPU registers */
2588
    target_ulong  sc_gregs[16];
2589
    target_ulong  sc_pc;
2590
    target_ulong  sc_pr;
2591
    target_ulong  sc_sr;
2592
    target_ulong  sc_gbr;
2593
    target_ulong  sc_mach;
2594
    target_ulong  sc_macl;
2595

    
2596
    /* FPU registers */
2597
    target_ulong  sc_fpregs[16];
2598
    target_ulong  sc_xfpregs[16];
2599
    unsigned int sc_fpscr;
2600
    unsigned int sc_fpul;
2601
    unsigned int sc_ownedfp;
2602
};
2603

    
2604
struct target_sigframe
2605
{
2606
    struct target_sigcontext sc;
2607
    target_ulong extramask[TARGET_NSIG_WORDS-1];
2608
    uint16_t retcode[3];
2609
};
2610

    
2611

    
2612
struct target_ucontext {
2613
    target_ulong uc_flags;
2614
    struct target_ucontext *uc_link;
2615
    target_stack_t uc_stack;
2616
    struct target_sigcontext uc_mcontext;
2617
    target_sigset_t uc_sigmask;        /* mask last for extensibility */
2618
};
2619

    
2620
struct target_rt_sigframe
2621
{
2622
    struct target_siginfo info;
2623
    struct target_ucontext uc;
2624
    uint16_t retcode[3];
2625
};
2626

    
2627

    
2628
#define MOVW(n)  (0x9300|((n)-2)) /* Move mem word at PC+n to R3 */
2629
#define TRAP_NOARG 0xc310         /* Syscall w/no args (NR in R3) SH3/4 */
2630

    
2631
static abi_ulong get_sigframe(struct emulated_sigaction *ka,
2632
                         unsigned long sp, size_t frame_size)
2633
{
2634
    if ((ka->sa.sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2635
        sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2636
    }
2637

    
2638
    return (sp - frame_size) & -8ul;
2639
}
2640

    
2641
static int setup_sigcontext(struct target_sigcontext *sc,
2642
                            CPUState *regs, unsigned long mask)
2643
{
2644
    int err = 0;
2645

    
2646
#define COPY(x)         err |= __put_user(regs->x, &sc->sc_##x)
2647
    COPY(gregs[0]); COPY(gregs[1]);
2648
    COPY(gregs[2]); COPY(gregs[3]);
2649
    COPY(gregs[4]); COPY(gregs[5]);
2650
    COPY(gregs[6]); COPY(gregs[7]);
2651
    COPY(gregs[8]); COPY(gregs[9]);
2652
    COPY(gregs[10]); COPY(gregs[11]);
2653
    COPY(gregs[12]); COPY(gregs[13]);
2654
    COPY(gregs[14]); COPY(gregs[15]);
2655
    COPY(gbr); COPY(mach);
2656
    COPY(macl); COPY(pr);
2657
    COPY(sr); COPY(pc);
2658
#undef COPY
2659

    
2660
    /* todo: save FPU registers here */
2661

    
2662
    /* non-iBCS2 extensions.. */
2663
    err |= __put_user(mask, &sc->oldmask);
2664

    
2665
    return err;
2666
}
2667

    
2668
static int restore_sigcontext(struct CPUState *regs,
2669
                              struct target_sigcontext *sc)
2670
{
2671
    unsigned int err = 0;
2672

    
2673
#define COPY(x)         err |= __get_user(regs->x, &sc->sc_##x)
2674
    COPY(gregs[1]);
2675
    COPY(gregs[2]); COPY(gregs[3]);
2676
    COPY(gregs[4]); COPY(gregs[5]);
2677
    COPY(gregs[6]); COPY(gregs[7]);
2678
    COPY(gregs[8]); COPY(gregs[9]);
2679
    COPY(gregs[10]); COPY(gregs[11]);
2680
    COPY(gregs[12]); COPY(gregs[13]);
2681
    COPY(gregs[14]); COPY(gregs[15]);
2682
    COPY(gbr); COPY(mach);
2683
    COPY(macl); COPY(pr);
2684
    COPY(sr); COPY(pc);
2685
#undef COPY
2686

    
2687
    /* todo: restore FPU registers here */
2688

    
2689
    regs->tra = -1;         /* disable syscall checks */
2690
    return err;
2691
}
2692

    
2693
static void setup_frame(int sig, struct emulated_sigaction *ka,
2694
                        target_sigset_t *set, CPUState *regs)
2695
{
2696
    struct target_sigframe *frame;
2697
    abi_ulong frame_addr;
2698
    int i;
2699
    int err = 0;
2700
    int signal;
2701

    
2702
    frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2703
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2704
        goto give_sigsegv;
2705

    
2706
    signal = current_exec_domain_sig(sig);
2707

    
2708
    err |= setup_sigcontext(&frame->sc, regs, set->sig[0]);
2709

    
2710
    for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
2711
        err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
2712
    }
2713

    
2714
    /* Set up to return from userspace.  If provided, use a stub
2715
       already in userspace.  */
2716
    if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2717
        regs->pr = (unsigned long) ka->sa.sa_restorer;
2718
    } else {
2719
        /* Generate return code (system call to sigreturn) */
2720
        err |= __put_user(MOVW(2), &frame->retcode[0]);
2721
        err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2722
        err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
2723
        regs->pr = (unsigned long) frame->retcode;
2724
    }
2725

    
2726
    if (err)
2727
        goto give_sigsegv;
2728

    
2729
    /* Set up registers for signal handler */
2730
    regs->gregs[15] = (unsigned long) frame;
2731
    regs->gregs[4] = signal; /* Arg for signal handler */
2732
    regs->gregs[5] = 0;
2733
    regs->gregs[6] = (unsigned long) &frame->sc;
2734
    regs->pc = (unsigned long) ka->sa._sa_handler;
2735

    
2736
    unlock_user_struct(frame, frame_addr, 1);
2737
    return;
2738

    
2739
give_sigsegv:
2740
    unlock_user_struct(frame, frame_addr, 1);
2741
    force_sig(SIGSEGV);
2742
}
2743

    
2744
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
2745
                           target_siginfo_t *info,
2746
                           target_sigset_t *set, CPUState *regs)
2747
{
2748
    struct target_rt_sigframe *frame;
2749
    abi_ulong frame_addr;
2750
    int i;
2751
    int err = 0;
2752
    int signal;
2753

    
2754
    frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
2755
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2756
        goto give_sigsegv;
2757

    
2758
    signal = current_exec_domain_sig(sig);
2759

    
2760
    err |= copy_siginfo_to_user(&frame->info, info);
2761

    
2762
    /* Create the ucontext.  */
2763
    err |= __put_user(0, &frame->uc.uc_flags);
2764
    err |= __put_user(0, (unsigned long *)&frame->uc.uc_link);
2765
    err |= __put_user((void *)target_sigaltstack_used.ss_sp,
2766
                      &frame->uc.uc_stack.ss_sp);
2767
    err |= __put_user(sas_ss_flags(regs->gregs[15]),
2768
                      &frame->uc.uc_stack.ss_flags);
2769
    err |= __put_user(target_sigaltstack_used.ss_size,
2770
                      &frame->uc.uc_stack.ss_size);
2771
    err |= setup_sigcontext(&frame->uc.uc_mcontext,
2772
                            regs, set->sig[0]);
2773
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2774
        err |= __put_user(set->sig[i], &frame->uc.uc_sigmask.sig[i]);
2775
    }
2776

    
2777
    /* Set up to return from userspace.  If provided, use a stub
2778
       already in userspace.  */
2779
    if (ka->sa.sa_flags & TARGET_SA_RESTORER) {
2780
        regs->pr = (unsigned long) ka->sa.sa_restorer;
2781
    } else {
2782
        /* Generate return code (system call to sigreturn) */
2783
        err |= __put_user(MOVW(2), &frame->retcode[0]);
2784
        err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
2785
        err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
2786
        regs->pr = (unsigned long) frame->retcode;
2787
    }
2788

    
2789
    if (err)
2790
        goto give_sigsegv;
2791

    
2792
    /* Set up registers for signal handler */
2793
    regs->gregs[15] = (unsigned long) frame;
2794
    regs->gregs[4] = signal; /* Arg for signal handler */
2795
    regs->gregs[5] = (unsigned long) &frame->info;
2796
    regs->gregs[6] = (unsigned long) &frame->uc;
2797
    regs->pc = (unsigned long) ka->sa._sa_handler;
2798

    
2799
    unlock_user_struct(frame, frame_addr, 1);
2800
    return;
2801

    
2802
give_sigsegv:
2803
    unlock_user_struct(frame, frame_addr, 1);
2804
    force_sig(SIGSEGV);
2805
}
2806

    
2807
long do_sigreturn(CPUState *regs)
2808
{
2809
    struct target_sigframe *frame;
2810
    abi_ulong frame_addr;
2811
    sigset_t blocked;
2812
    target_sigset_t target_set;
2813
    int i;
2814
    int err = 0;
2815

    
2816
#if defined(DEBUG_SIGNAL)
2817
    fprintf(stderr, "do_sigreturn\n");
2818
#endif
2819
    frame_addr = regs->gregs[15];
2820
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2821
           goto badframe;
2822

    
2823
    err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
2824
    for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2825
        err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
2826
    }
2827

    
2828
    if (err)
2829
        goto badframe;
2830

    
2831
    target_to_host_sigset_internal(&blocked, &target_set);
2832
    sigprocmask(SIG_SETMASK, &blocked, NULL);
2833

    
2834
    if (restore_sigcontext(regs, &frame->sc))
2835
        goto badframe;
2836

    
2837
    unlock_user_struct(frame, frame_addr, 0);
2838
    return regs->gregs[0];
2839

    
2840
badframe:
2841
    unlock_user_struct(frame, frame_addr, 0);
2842
    force_sig(TARGET_SIGSEGV);
2843
    return 0;
2844
}
2845

    
2846
long do_rt_sigreturn(CPUState *regs)
2847
{
2848
    struct target_rt_sigframe *frame;
2849
    abi_ulong frame_addr;
2850
    sigset_t blocked;
2851

    
2852
#if defined(DEBUG_SIGNAL)
2853
    fprintf(stderr, "do_rt_sigreturn\n");
2854
#endif
2855
    frame_addr = regs->gregs[15];
2856
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2857
           goto badframe;
2858

    
2859
    target_to_host_sigset(&blocked, &frame->uc.uc_sigmask);
2860
    sigprocmask(SIG_SETMASK, &blocked, NULL);
2861

    
2862
    if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
2863
        goto badframe;
2864

    
2865
    if (do_sigaltstack(frame_addr +
2866
                       offsetof(struct target_rt_sigframe, uc.uc_stack),
2867
                       0, get_sp_from_cpustate(regs)) == -EFAULT)
2868
        goto badframe;
2869

    
2870
    unlock_user_struct(frame, frame_addr, 0);
2871
    return regs->gregs[0];
2872

    
2873
badframe:
2874
    unlock_user_struct(frame, frame_addr, 0);
2875
    force_sig(TARGET_SIGSEGV);
2876
    return 0;
2877
}
2878
#elif defined(TARGET_CRIS)
2879

    
2880
struct target_sigcontext {
2881
        struct target_pt_regs regs;  /* needs to be first */
2882
        uint32_t oldmask;
2883
        uint32_t usp;    /* usp before stacking this gunk on it */
2884
};
2885

    
2886
/* Signal frames. */
2887
struct target_signal_frame {
2888
        struct target_sigcontext sc;
2889
        uint32_t extramask[TARGET_NSIG_WORDS - 1];
2890
        uint8_t retcode[8];       /* Trampoline code. */
2891
};
2892

    
2893
struct rt_signal_frame {
2894
        struct siginfo *pinfo;
2895
        void *puc;
2896
        struct siginfo info;
2897
        struct ucontext uc;
2898
        uint8_t retcode[8];       /* Trampoline code. */
2899
};
2900

    
2901
static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
2902
{
2903
        __put_user(env->regs[0], &sc->regs.r0);
2904
        __put_user(env->regs[1], &sc->regs.r1);
2905
        __put_user(env->regs[2], &sc->regs.r2);
2906
        __put_user(env->regs[3], &sc->regs.r3);
2907
        __put_user(env->regs[4], &sc->regs.r4);
2908
        __put_user(env->regs[5], &sc->regs.r5);
2909
        __put_user(env->regs[6], &sc->regs.r6);
2910
        __put_user(env->regs[7], &sc->regs.r7);
2911
        __put_user(env->regs[8], &sc->regs.r8);
2912
        __put_user(env->regs[9], &sc->regs.r9);
2913
        __put_user(env->regs[10], &sc->regs.r10);
2914
        __put_user(env->regs[11], &sc->regs.r11);
2915
        __put_user(env->regs[12], &sc->regs.r12);
2916
        __put_user(env->regs[13], &sc->regs.r13);
2917
        __put_user(env->regs[14], &sc->usp);
2918
        __put_user(env->regs[15], &sc->regs.acr);
2919
        __put_user(env->pregs[PR_MOF], &sc->regs.mof);
2920
        __put_user(env->pregs[PR_SRP], &sc->regs.srp);
2921
        __put_user(env->pc, &sc->regs.erp);
2922
}
2923

    
2924
static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
2925
{
2926
        __get_user(env->regs[0], &sc->regs.r0);
2927
        __get_user(env->regs[1], &sc->regs.r1);
2928
        __get_user(env->regs[2], &sc->regs.r2);
2929
        __get_user(env->regs[3], &sc->regs.r3);
2930
        __get_user(env->regs[4], &sc->regs.r4);
2931
        __get_user(env->regs[5], &sc->regs.r5);
2932
        __get_user(env->regs[6], &sc->regs.r6);
2933
        __get_user(env->regs[7], &sc->regs.r7);
2934
        __get_user(env->regs[8], &sc->regs.r8);
2935
        __get_user(env->regs[9], &sc->regs.r9);
2936
        __get_user(env->regs[10], &sc->regs.r10);
2937
        __get_user(env->regs[11], &sc->regs.r11);
2938
        __get_user(env->regs[12], &sc->regs.r12);
2939
        __get_user(env->regs[13], &sc->regs.r13);
2940
        __get_user(env->regs[14], &sc->usp);
2941
        __get_user(env->regs[15], &sc->regs.acr);
2942
        __get_user(env->pregs[PR_MOF], &sc->regs.mof);
2943
        __get_user(env->pregs[PR_SRP], &sc->regs.srp);
2944
        __get_user(env->pc, &sc->regs.erp);
2945
}
2946

    
2947
static abi_ulong get_sigframe(CPUState *env, int framesize)
2948
{
2949
        abi_ulong sp;
2950
        /* Align the stack downwards to 4.  */
2951
        sp = (env->regs[R_SP] & ~3);
2952
        return sp - framesize;
2953
}
2954

    
2955
static void setup_frame(int sig, struct emulated_sigaction *ka,
2956
                        target_sigset_t *set, CPUState *env)
2957
{
2958
        struct target_signal_frame *frame;
2959
        abi_ulong frame_addr;
2960
        int err = 0;
2961
        int i;
2962

    
2963
        frame_addr = get_sigframe(env, sizeof *frame);
2964
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2965
                goto badframe;
2966

    
2967
        /*
2968
         * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
2969
         * use this trampoline anymore but it sets it up for GDB.
2970
         * In QEMU, using the trampoline simplifies things a bit so we use it.
2971
         *
2972
         * This is movu.w __NR_sigreturn, r9; break 13;
2973
         */
2974
        err |= __put_user(0x9c5f, frame->retcode+0);
2975
        err |= __put_user(TARGET_NR_sigreturn, 
2976
                          frame->retcode+2);
2977
        err |= __put_user(0xe93d, frame->retcode+4);
2978

    
2979
        /* Save the mask.  */
2980
        err |= __put_user(set->sig[0], &frame->sc.oldmask);
2981
        if (err)
2982
                goto badframe;
2983

    
2984
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2985
                if (__put_user(set->sig[i], &frame->extramask[i - 1]))
2986
                        goto badframe;
2987
        }
2988

    
2989
        setup_sigcontext(&frame->sc, env);
2990

    
2991
        /* Move the stack and setup the arguments for the handler.  */
2992
        env->regs[R_SP] = (uint32_t) frame;
2993
        env->regs[10] = sig;
2994
        env->pc = (unsigned long) ka->sa._sa_handler;
2995
        /* Link SRP so the guest returns through the trampoline.  */
2996
        env->pregs[PR_SRP] = (uint32_t) &frame->retcode[0];
2997

    
2998
        unlock_user_struct(frame, frame_addr, 1);
2999
        return;
3000
  badframe:
3001
        unlock_user_struct(frame, frame_addr, 1);
3002
        force_sig(TARGET_SIGSEGV);
3003
}
3004

    
3005
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
3006
                           target_siginfo_t *info,
3007
                           target_sigset_t *set, CPUState *env)
3008
{
3009
    fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3010
}
3011

    
3012
long do_sigreturn(CPUState *env)
3013
{
3014
        struct target_signal_frame *frame;
3015
        abi_ulong frame_addr;
3016
        target_sigset_t target_set;
3017
        sigset_t set;
3018
        int i;
3019

    
3020
        frame_addr = env->regs[R_SP];
3021
        /* Make sure the guest isn't playing games.  */
3022
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3023
                goto badframe;
3024

    
3025
        /* Restore blocked signals */
3026
        if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3027
                goto badframe;
3028
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3029
                if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3030
                        goto badframe;
3031
        }
3032
        target_to_host_sigset_internal(&set, &target_set);
3033
        sigprocmask(SIG_SETMASK, &set, NULL);
3034

    
3035
        restore_sigcontext(&frame->sc, env);
3036
        /* Compensate for the syscall return path advancing brk.  */
3037
        env->pc -= 2;
3038

    
3039
        unlock_user_struct(frame, frame_addr, 0);
3040
        return env->regs[10];
3041
  badframe:
3042
        unlock_user_struct(frame, frame_addr, 0);
3043
        force_sig(TARGET_SIGSEGV);
3044
}
3045

    
3046
long do_rt_sigreturn(CPUState *env)
3047
{
3048
    fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3049
    return -TARGET_ENOSYS;
3050
}
3051

    
3052
#else
3053

    
3054
static void setup_frame(int sig, struct emulated_sigaction *ka,
3055
                        target_sigset_t *set, CPUState *env)
3056
{
3057
    fprintf(stderr, "setup_frame: not implemented\n");
3058
}
3059

    
3060
static void setup_rt_frame(int sig, struct emulated_sigaction *ka,
3061
                           target_siginfo_t *info,
3062
                           target_sigset_t *set, CPUState *env)
3063
{
3064
    fprintf(stderr, "setup_rt_frame: not implemented\n");
3065
}
3066

    
3067
long do_sigreturn(CPUState *env)
3068
{
3069
    fprintf(stderr, "do_sigreturn: not implemented\n");
3070
    return -TARGET_ENOSYS;
3071
}
3072

    
3073
long do_rt_sigreturn(CPUState *env)
3074
{
3075
    fprintf(stderr, "do_rt_sigreturn: not implemented\n");
3076
    return -TARGET_ENOSYS;
3077
}
3078

    
3079
#endif
3080

    
3081
void process_pending_signals(void *cpu_env)
3082
{
3083
    int sig;
3084
    abi_ulong handler;
3085
    sigset_t set, old_set;
3086
    target_sigset_t target_old_set;
3087
    struct emulated_sigaction *k;
3088
    struct sigqueue *q;
3089

    
3090
    if (!signal_pending)
3091
        return;
3092

    
3093
    k = sigact_table;
3094
    for(sig = 1; sig <= TARGET_NSIG; sig++) {
3095
        if (k->pending)
3096
            goto handle_signal;
3097
        k++;
3098
    }
3099
    /* if no signal is pending, just return */
3100
    signal_pending = 0;
3101
    return;
3102

    
3103
 handle_signal:
3104
#ifdef DEBUG_SIGNAL
3105
    fprintf(stderr, "qemu: process signal %d\n", sig);
3106
#endif
3107
    /* dequeue signal */
3108
    q = k->first;
3109
    k->first = q->next;
3110
    if (!k->first)
3111
        k->pending = 0;
3112

    
3113
    sig = gdb_handlesig (cpu_env, sig);
3114
    if (!sig) {
3115
        fprintf (stderr, "Lost signal\n");
3116
        abort();
3117
    }
3118

    
3119
    handler = k->sa._sa_handler;
3120
    if (handler == TARGET_SIG_DFL) {
3121
        /* default handler : ignore some signal. The other are fatal */
3122
        if (sig != TARGET_SIGCHLD &&
3123
            sig != TARGET_SIGURG &&
3124
            sig != TARGET_SIGWINCH) {
3125
            force_sig(sig);
3126
        }
3127
    } else if (handler == TARGET_SIG_IGN) {
3128
        /* ignore sig */
3129
    } else if (handler == TARGET_SIG_ERR) {
3130
        force_sig(sig);
3131
    } else {
3132
        /* compute the blocked signals during the handler execution */
3133
        target_to_host_sigset(&set, &k->sa.sa_mask);
3134
        /* SA_NODEFER indicates that the current signal should not be
3135
           blocked during the handler */
3136
        if (!(k->sa.sa_flags & TARGET_SA_NODEFER))
3137
            sigaddset(&set, target_to_host_signal(sig));
3138

    
3139
        /* block signals in the handler using Linux */
3140
        sigprocmask(SIG_BLOCK, &set, &old_set);
3141
        /* save the previous blocked signal state to restore it at the
3142
           end of the signal execution (see do_sigreturn) */
3143
        host_to_target_sigset_internal(&target_old_set, &old_set);
3144

    
3145
        /* if the CPU is in VM86 mode, we restore the 32 bit values */
3146
#if defined(TARGET_I386) && !defined(TARGET_X86_64)
3147
        {
3148
            CPUX86State *env = cpu_env;
3149
            if (env->eflags & VM_MASK)
3150
                save_v86_state(env);
3151
        }
3152
#endif
3153
        /* prepare the stack frame of the virtual CPU */
3154
        if (k->sa.sa_flags & TARGET_SA_SIGINFO)
3155
            setup_rt_frame(sig, k, &q->info, &target_old_set, cpu_env);
3156
        else
3157
            setup_frame(sig, k, &target_old_set, cpu_env);
3158
        if (k->sa.sa_flags & TARGET_SA_RESETHAND)
3159
            k->sa._sa_handler = TARGET_SIG_DFL;
3160
    }
3161
    if (q != &k->info)
3162
        free_sigqueue(q);
3163
}