Statistics
| Branch: | Revision:

root / linux-user / signal.c @ 93148aa5

History | View | Annotate | Download (155.4 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, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <stdlib.h>
20
#include <stdio.h>
21
#include <string.h>
22
#include <stdarg.h>
23
#include <unistd.h>
24
#include <errno.h>
25
#include <assert.h>
26
#include <sys/ucontext.h>
27
#include <sys/resource.h>
28

    
29
#include "qemu.h"
30
#include "qemu-common.h"
31
#include "target_signal.h"
32

    
33
//#define DEBUG_SIGNAL
34

    
35
static struct target_sigaltstack target_sigaltstack_used = {
36
    .ss_sp = 0,
37
    .ss_size = 0,
38
    .ss_flags = TARGET_SS_DISABLE,
39
};
40

    
41
static struct target_sigaction sigact_table[TARGET_NSIG];
42

    
43
static void host_signal_handler(int host_signum, siginfo_t *info,
44
                                void *puc);
45

    
46
static uint8_t host_to_target_signal_table[_NSIG] = {
47
    [SIGHUP] = TARGET_SIGHUP,
48
    [SIGINT] = TARGET_SIGINT,
49
    [SIGQUIT] = TARGET_SIGQUIT,
50
    [SIGILL] = TARGET_SIGILL,
51
    [SIGTRAP] = TARGET_SIGTRAP,
52
    [SIGABRT] = TARGET_SIGABRT,
53
/*    [SIGIOT] = TARGET_SIGIOT,*/
54
    [SIGBUS] = TARGET_SIGBUS,
55
    [SIGFPE] = TARGET_SIGFPE,
56
    [SIGKILL] = TARGET_SIGKILL,
57
    [SIGUSR1] = TARGET_SIGUSR1,
58
    [SIGSEGV] = TARGET_SIGSEGV,
59
    [SIGUSR2] = TARGET_SIGUSR2,
60
    [SIGPIPE] = TARGET_SIGPIPE,
61
    [SIGALRM] = TARGET_SIGALRM,
62
    [SIGTERM] = TARGET_SIGTERM,
63
#ifdef SIGSTKFLT
64
    [SIGSTKFLT] = TARGET_SIGSTKFLT,
65
#endif
66
    [SIGCHLD] = TARGET_SIGCHLD,
67
    [SIGCONT] = TARGET_SIGCONT,
68
    [SIGSTOP] = TARGET_SIGSTOP,
69
    [SIGTSTP] = TARGET_SIGTSTP,
70
    [SIGTTIN] = TARGET_SIGTTIN,
71
    [SIGTTOU] = TARGET_SIGTTOU,
72
    [SIGURG] = TARGET_SIGURG,
73
    [SIGXCPU] = TARGET_SIGXCPU,
74
    [SIGXFSZ] = TARGET_SIGXFSZ,
75
    [SIGVTALRM] = TARGET_SIGVTALRM,
76
    [SIGPROF] = TARGET_SIGPROF,
77
    [SIGWINCH] = TARGET_SIGWINCH,
78
    [SIGIO] = TARGET_SIGIO,
79
    [SIGPWR] = TARGET_SIGPWR,
80
    [SIGSYS] = TARGET_SIGSYS,
81
    /* next signals stay the same */
82
    /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
83
       host libpthread signals.  This assumes no one actually uses SIGRTMAX :-/
84
       To fix this properly we need to do manual signal delivery multiplexed
85
       over a single host signal.  */
86
    [__SIGRTMIN] = __SIGRTMAX,
87
    [__SIGRTMAX] = __SIGRTMIN,
88
};
89
static uint8_t target_to_host_signal_table[_NSIG];
90

    
91
static inline int on_sig_stack(unsigned long sp)
92
{
93
    return (sp - target_sigaltstack_used.ss_sp
94
            < target_sigaltstack_used.ss_size);
95
}
96

    
97
static inline int sas_ss_flags(unsigned long sp)
98
{
99
    return (target_sigaltstack_used.ss_size == 0 ? SS_DISABLE
100
            : on_sig_stack(sp) ? SS_ONSTACK : 0);
101
}
102

    
103
int host_to_target_signal(int sig)
104
{
105
    if (sig >= _NSIG)
106
        return sig;
107
    return host_to_target_signal_table[sig];
108
}
109

    
110
int target_to_host_signal(int sig)
111
{
112
    if (sig >= _NSIG)
113
        return sig;
114
    return target_to_host_signal_table[sig];
115
}
116

    
117
static inline void target_sigemptyset(target_sigset_t *set)
118
{
119
    memset(set, 0, sizeof(*set));
120
}
121

    
122
static inline void target_sigaddset(target_sigset_t *set, int signum)
123
{
124
    signum--;
125
    abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
126
    set->sig[signum / TARGET_NSIG_BPW] |= mask;
127
}
128

    
129
static inline int target_sigismember(const target_sigset_t *set, int signum)
130
{
131
    signum--;
132
    abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
133
    return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
134
}
135

    
136
static void host_to_target_sigset_internal(target_sigset_t *d,
137
                                           const sigset_t *s)
138
{
139
    int i;
140
    target_sigemptyset(d);
141
    for (i = 1; i <= TARGET_NSIG; i++) {
142
        if (sigismember(s, i)) {
143
            target_sigaddset(d, host_to_target_signal(i));
144
        }
145
    }
146
}
147

    
148
void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
149
{
150
    target_sigset_t d1;
151
    int i;
152

    
153
    host_to_target_sigset_internal(&d1, s);
154
    for(i = 0;i < TARGET_NSIG_WORDS; i++)
155
        d->sig[i] = tswapal(d1.sig[i]);
156
}
157

    
158
static void target_to_host_sigset_internal(sigset_t *d,
159
                                           const target_sigset_t *s)
160
{
161
    int i;
162
    sigemptyset(d);
163
    for (i = 1; i <= TARGET_NSIG; i++) {
164
        if (target_sigismember(s, i)) {
165
            sigaddset(d, target_to_host_signal(i));
166
        }
167
     }
168
}
169

    
170
void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
171
{
172
    target_sigset_t s1;
173
    int i;
174

    
175
    for(i = 0;i < TARGET_NSIG_WORDS; i++)
176
        s1.sig[i] = tswapal(s->sig[i]);
177
    target_to_host_sigset_internal(d, &s1);
178
}
179

    
180
void host_to_target_old_sigset(abi_ulong *old_sigset,
181
                               const sigset_t *sigset)
182
{
183
    target_sigset_t d;
184
    host_to_target_sigset(&d, sigset);
185
    *old_sigset = d.sig[0];
186
}
187

    
188
void target_to_host_old_sigset(sigset_t *sigset,
189
                               const abi_ulong *old_sigset)
190
{
191
    target_sigset_t d;
192
    int i;
193

    
194
    d.sig[0] = *old_sigset;
195
    for(i = 1;i < TARGET_NSIG_WORDS; i++)
196
        d.sig[i] = 0;
197
    target_to_host_sigset(sigset, &d);
198
}
199

    
200
/* siginfo conversion */
201

    
202
static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
203
                                                 const siginfo_t *info)
204
{
205
    int sig;
206
    sig = host_to_target_signal(info->si_signo);
207
    tinfo->si_signo = sig;
208
    tinfo->si_errno = 0;
209
    tinfo->si_code = info->si_code;
210
    if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
211
        sig == SIGBUS || sig == SIGTRAP) {
212
        /* should never come here, but who knows. The information for
213
           the target is irrelevant */
214
        tinfo->_sifields._sigfault._addr = 0;
215
    } else if (sig == SIGIO) {
216
        tinfo->_sifields._sigpoll._fd = info->si_fd;
217
    } else if (sig >= TARGET_SIGRTMIN) {
218
        tinfo->_sifields._rt._pid = info->si_pid;
219
        tinfo->_sifields._rt._uid = info->si_uid;
220
        /* XXX: potential problem if 64 bit */
221
        tinfo->_sifields._rt._sigval.sival_ptr =
222
            (abi_ulong)(unsigned long)info->si_value.sival_ptr;
223
    }
224
}
225

    
226
static void tswap_siginfo(target_siginfo_t *tinfo,
227
                          const target_siginfo_t *info)
228
{
229
    int sig;
230
    sig = info->si_signo;
231
    tinfo->si_signo = tswap32(sig);
232
    tinfo->si_errno = tswap32(info->si_errno);
233
    tinfo->si_code = tswap32(info->si_code);
234
    if (sig == SIGILL || sig == SIGFPE || sig == SIGSEGV ||
235
        sig == SIGBUS || sig == SIGTRAP) {
236
        tinfo->_sifields._sigfault._addr =
237
            tswapal(info->_sifields._sigfault._addr);
238
    } else if (sig == SIGIO) {
239
        tinfo->_sifields._sigpoll._fd = tswap32(info->_sifields._sigpoll._fd);
240
    } else if (sig >= TARGET_SIGRTMIN) {
241
        tinfo->_sifields._rt._pid = tswap32(info->_sifields._rt._pid);
242
        tinfo->_sifields._rt._uid = tswap32(info->_sifields._rt._uid);
243
        tinfo->_sifields._rt._sigval.sival_ptr =
244
            tswapal(info->_sifields._rt._sigval.sival_ptr);
245
    }
246
}
247

    
248

    
249
void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
250
{
251
    host_to_target_siginfo_noswap(tinfo, info);
252
    tswap_siginfo(tinfo, tinfo);
253
}
254

    
255
/* XXX: we support only POSIX RT signals are used. */
256
/* XXX: find a solution for 64 bit (additional malloced data is needed) */
257
void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
258
{
259
    info->si_signo = tswap32(tinfo->si_signo);
260
    info->si_errno = tswap32(tinfo->si_errno);
261
    info->si_code = tswap32(tinfo->si_code);
262
    info->si_pid = tswap32(tinfo->_sifields._rt._pid);
263
    info->si_uid = tswap32(tinfo->_sifields._rt._uid);
264
    info->si_value.sival_ptr =
265
            (void *)(long)tswapal(tinfo->_sifields._rt._sigval.sival_ptr);
266
}
267

    
268
static int fatal_signal (int sig)
269
{
270
    switch (sig) {
271
    case TARGET_SIGCHLD:
272
    case TARGET_SIGURG:
273
    case TARGET_SIGWINCH:
274
        /* Ignored by default.  */
275
        return 0;
276
    case TARGET_SIGCONT:
277
    case TARGET_SIGSTOP:
278
    case TARGET_SIGTSTP:
279
    case TARGET_SIGTTIN:
280
    case TARGET_SIGTTOU:
281
        /* Job control signals.  */
282
        return 0;
283
    default:
284
        return 1;
285
    }
286
}
287

    
288
/* returns 1 if given signal should dump core if not handled */
289
static int core_dump_signal(int sig)
290
{
291
    switch (sig) {
292
    case TARGET_SIGABRT:
293
    case TARGET_SIGFPE:
294
    case TARGET_SIGILL:
295
    case TARGET_SIGQUIT:
296
    case TARGET_SIGSEGV:
297
    case TARGET_SIGTRAP:
298
    case TARGET_SIGBUS:
299
        return (1);
300
    default:
301
        return (0);
302
    }
303
}
304

    
305
void signal_init(void)
306
{
307
    struct sigaction act;
308
    struct sigaction oact;
309
    int i, j;
310
    int host_sig;
311

    
312
    /* generate signal conversion tables */
313
    for(i = 1; i < _NSIG; i++) {
314
        if (host_to_target_signal_table[i] == 0)
315
            host_to_target_signal_table[i] = i;
316
    }
317
    for(i = 1; i < _NSIG; i++) {
318
        j = host_to_target_signal_table[i];
319
        target_to_host_signal_table[j] = i;
320
    }
321

    
322
    /* set all host signal handlers. ALL signals are blocked during
323
       the handlers to serialize them. */
324
    memset(sigact_table, 0, sizeof(sigact_table));
325

    
326
    sigfillset(&act.sa_mask);
327
    act.sa_flags = SA_SIGINFO;
328
    act.sa_sigaction = host_signal_handler;
329
    for(i = 1; i <= TARGET_NSIG; i++) {
330
        host_sig = target_to_host_signal(i);
331
        sigaction(host_sig, NULL, &oact);
332
        if (oact.sa_sigaction == (void *)SIG_IGN) {
333
            sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
334
        } else if (oact.sa_sigaction == (void *)SIG_DFL) {
335
            sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
336
        }
337
        /* If there's already a handler installed then something has
338
           gone horribly wrong, so don't even try to handle that case.  */
339
        /* Install some handlers for our own use.  We need at least
340
           SIGSEGV and SIGBUS, to detect exceptions.  We can not just
341
           trap all signals because it affects syscall interrupt
342
           behavior.  But do trap all default-fatal signals.  */
343
        if (fatal_signal (i))
344
            sigaction(host_sig, &act, NULL);
345
    }
346
}
347

    
348
/* signal queue handling */
349

    
350
static inline struct sigqueue *alloc_sigqueue(CPUState *env)
351
{
352
    TaskState *ts = env->opaque;
353
    struct sigqueue *q = ts->first_free;
354
    if (!q)
355
        return NULL;
356
    ts->first_free = q->next;
357
    return q;
358
}
359

    
360
static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
361
{
362
    TaskState *ts = env->opaque;
363
    q->next = ts->first_free;
364
    ts->first_free = q;
365
}
366

    
367
/* abort execution with signal */
368
static void QEMU_NORETURN force_sig(int target_sig)
369
{
370
    TaskState *ts = (TaskState *)thread_env->opaque;
371
    int host_sig, core_dumped = 0;
372
    struct sigaction act;
373
    host_sig = target_to_host_signal(target_sig);
374
    gdb_signalled(thread_env, target_sig);
375

    
376
    /* dump core if supported by target binary format */
377
    if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
378
        stop_all_tasks();
379
        core_dumped =
380
            ((*ts->bprm->core_dump)(target_sig, thread_env) == 0);
381
    }
382
    if (core_dumped) {
383
        /* we already dumped the core of target process, we don't want
384
         * a coredump of qemu itself */
385
        struct rlimit nodump;
386
        getrlimit(RLIMIT_CORE, &nodump);
387
        nodump.rlim_cur=0;
388
        setrlimit(RLIMIT_CORE, &nodump);
389
        (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
390
            target_sig, strsignal(host_sig), "core dumped" );
391
    }
392

    
393
    /* The proper exit code for dying from an uncaught signal is
394
     * -<signal>.  The kernel doesn't allow exit() or _exit() to pass
395
     * a negative value.  To get the proper exit code we need to
396
     * actually die from an uncaught signal.  Here the default signal
397
     * handler is installed, we send ourself a signal and we wait for
398
     * it to arrive. */
399
    sigfillset(&act.sa_mask);
400
    act.sa_handler = SIG_DFL;
401
    sigaction(host_sig, &act, NULL);
402

    
403
    /* For some reason raise(host_sig) doesn't send the signal when
404
     * statically linked on x86-64. */
405
    kill(getpid(), host_sig);
406

    
407
    /* Make sure the signal isn't masked (just reuse the mask inside
408
    of act) */
409
    sigdelset(&act.sa_mask, host_sig);
410
    sigsuspend(&act.sa_mask);
411

    
412
    /* unreachable */
413
    abort();
414
}
415

    
416
/* queue a signal so that it will be send to the virtual CPU as soon
417
   as possible */
418
int queue_signal(CPUState *env, int sig, target_siginfo_t *info)
419
{
420
    TaskState *ts = env->opaque;
421
    struct emulated_sigtable *k;
422
    struct sigqueue *q, **pq;
423
    abi_ulong handler;
424
    int queue;
425

    
426
#if defined(DEBUG_SIGNAL)
427
    fprintf(stderr, "queue_signal: sig=%d\n",
428
            sig);
429
#endif
430
    k = &ts->sigtab[sig - 1];
431
    queue = gdb_queuesig ();
432
    handler = sigact_table[sig - 1]._sa_handler;
433
    if (!queue && handler == TARGET_SIG_DFL) {
434
        if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
435
            kill(getpid(),SIGSTOP);
436
            return 0;
437
        } else
438
        /* default handler : ignore some signal. The other are fatal */
439
        if (sig != TARGET_SIGCHLD &&
440
            sig != TARGET_SIGURG &&
441
            sig != TARGET_SIGWINCH &&
442
            sig != TARGET_SIGCONT) {
443
            force_sig(sig);
444
        } else {
445
            return 0; /* indicate ignored */
446
        }
447
    } else if (!queue && handler == TARGET_SIG_IGN) {
448
        /* ignore signal */
449
        return 0;
450
    } else if (!queue && handler == TARGET_SIG_ERR) {
451
        force_sig(sig);
452
    } else {
453
        pq = &k->first;
454
        if (sig < TARGET_SIGRTMIN) {
455
            /* if non real time signal, we queue exactly one signal */
456
            if (!k->pending)
457
                q = &k->info;
458
            else
459
                return 0;
460
        } else {
461
            if (!k->pending) {
462
                /* first signal */
463
                q = &k->info;
464
            } else {
465
                q = alloc_sigqueue(env);
466
                if (!q)
467
                    return -EAGAIN;
468
                while (*pq != NULL)
469
                    pq = &(*pq)->next;
470
            }
471
        }
472
        *pq = q;
473
        q->info = *info;
474
        q->next = NULL;
475
        k->pending = 1;
476
        /* signal that a new signal is pending */
477
        ts->signal_pending = 1;
478
        return 1; /* indicates that the signal was queued */
479
    }
480
}
481

    
482
static void host_signal_handler(int host_signum, siginfo_t *info,
483
                                void *puc)
484
{
485
    int sig;
486
    target_siginfo_t tinfo;
487

    
488
    /* the CPU emulator uses some host signals to detect exceptions,
489
       we forward to it some signals */
490
    if ((host_signum == SIGSEGV || host_signum == SIGBUS)
491
        && info->si_code > 0) {
492
        if (cpu_signal_handler(host_signum, info, puc))
493
            return;
494
    }
495

    
496
    /* get target signal number */
497
    sig = host_to_target_signal(host_signum);
498
    if (sig < 1 || sig > TARGET_NSIG)
499
        return;
500
#if defined(DEBUG_SIGNAL)
501
    fprintf(stderr, "qemu: got signal %d\n", sig);
502
#endif
503
    host_to_target_siginfo_noswap(&tinfo, info);
504
    if (queue_signal(thread_env, sig, &tinfo) == 1) {
505
        /* interrupt the virtual CPU as soon as possible */
506
        cpu_exit(thread_env);
507
    }
508
}
509

    
510
/* do_sigaltstack() returns target values and errnos. */
511
/* compare linux/kernel/signal.c:do_sigaltstack() */
512
abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
513
{
514
    int ret;
515
    struct target_sigaltstack oss;
516

    
517
    /* XXX: test errors */
518
    if(uoss_addr)
519
    {
520
        __put_user(target_sigaltstack_used.ss_sp, &oss.ss_sp);
521
        __put_user(target_sigaltstack_used.ss_size, &oss.ss_size);
522
        __put_user(sas_ss_flags(sp), &oss.ss_flags);
523
    }
524

    
525
    if(uss_addr)
526
    {
527
        struct target_sigaltstack *uss;
528
        struct target_sigaltstack ss;
529

    
530
        ret = -TARGET_EFAULT;
531
        if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)
532
            || __get_user(ss.ss_sp, &uss->ss_sp)
533
            || __get_user(ss.ss_size, &uss->ss_size)
534
            || __get_user(ss.ss_flags, &uss->ss_flags))
535
            goto out;
536
        unlock_user_struct(uss, uss_addr, 0);
537

    
538
        ret = -TARGET_EPERM;
539
        if (on_sig_stack(sp))
540
            goto out;
541

    
542
        ret = -TARGET_EINVAL;
543
        if (ss.ss_flags != TARGET_SS_DISABLE
544
            && ss.ss_flags != TARGET_SS_ONSTACK
545
            && ss.ss_flags != 0)
546
            goto out;
547

    
548
        if (ss.ss_flags == TARGET_SS_DISABLE) {
549
            ss.ss_size = 0;
550
            ss.ss_sp = 0;
551
        } else {
552
            ret = -TARGET_ENOMEM;
553
            if (ss.ss_size < MINSIGSTKSZ)
554
                goto out;
555
        }
556

    
557
        target_sigaltstack_used.ss_sp = ss.ss_sp;
558
        target_sigaltstack_used.ss_size = ss.ss_size;
559
    }
560

    
561
    if (uoss_addr) {
562
        ret = -TARGET_EFAULT;
563
        if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
564
            goto out;
565
    }
566

    
567
    ret = 0;
568
out:
569
    return ret;
570
}
571

    
572
/* do_sigaction() return host values and errnos */
573
int do_sigaction(int sig, const struct target_sigaction *act,
574
                 struct target_sigaction *oact)
575
{
576
    struct target_sigaction *k;
577
    struct sigaction act1;
578
    int host_sig;
579
    int ret = 0;
580

    
581
    if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)
582
        return -EINVAL;
583
    k = &sigact_table[sig - 1];
584
#if defined(DEBUG_SIGNAL)
585
    fprintf(stderr, "sigaction sig=%d act=0x%p, oact=0x%p\n",
586
            sig, act, oact);
587
#endif
588
    if (oact) {
589
        oact->_sa_handler = tswapal(k->_sa_handler);
590
#if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
591
        oact->sa_flags = bswap32(k->sa_flags);
592
#else
593
        oact->sa_flags = tswapal(k->sa_flags);
594
#endif
595
#if !defined(TARGET_MIPS)
596
        oact->sa_restorer = tswapal(k->sa_restorer);
597
#endif
598
        oact->sa_mask = k->sa_mask;
599
    }
600
    if (act) {
601
        /* FIXME: This is not threadsafe.  */
602
        k->_sa_handler = tswapal(act->_sa_handler);
603
#if defined(TARGET_MIPS) || defined (TARGET_ALPHA)
604
        k->sa_flags = bswap32(act->sa_flags);
605
#else
606
        k->sa_flags = tswapal(act->sa_flags);
607
#endif
608
#if !defined(TARGET_MIPS)
609
        k->sa_restorer = tswapal(act->sa_restorer);
610
#endif
611
        k->sa_mask = act->sa_mask;
612

    
613
        /* we update the host linux signal state */
614
        host_sig = target_to_host_signal(sig);
615
        if (host_sig != SIGSEGV && host_sig != SIGBUS) {
616
            sigfillset(&act1.sa_mask);
617
            act1.sa_flags = SA_SIGINFO;
618
            if (k->sa_flags & TARGET_SA_RESTART)
619
                act1.sa_flags |= SA_RESTART;
620
            /* NOTE: it is important to update the host kernel signal
621
               ignore state to avoid getting unexpected interrupted
622
               syscalls */
623
            if (k->_sa_handler == TARGET_SIG_IGN) {
624
                act1.sa_sigaction = (void *)SIG_IGN;
625
            } else if (k->_sa_handler == TARGET_SIG_DFL) {
626
                if (fatal_signal (sig))
627
                    act1.sa_sigaction = host_signal_handler;
628
                else
629
                    act1.sa_sigaction = (void *)SIG_DFL;
630
            } else {
631
                act1.sa_sigaction = host_signal_handler;
632
            }
633
            ret = sigaction(host_sig, &act1, NULL);
634
        }
635
    }
636
    return ret;
637
}
638

    
639
static inline int copy_siginfo_to_user(target_siginfo_t *tinfo,
640
                                       const target_siginfo_t *info)
641
{
642
    tswap_siginfo(tinfo, info);
643
    return 0;
644
}
645

    
646
static inline int current_exec_domain_sig(int sig)
647
{
648
    return /* current->exec_domain && current->exec_domain->signal_invmap
649
              && sig < 32 ? current->exec_domain->signal_invmap[sig] : */ sig;
650
}
651

    
652
#if defined(TARGET_I386) && TARGET_ABI_BITS == 32
653

    
654
/* from the Linux kernel */
655

    
656
struct target_fpreg {
657
        uint16_t significand[4];
658
        uint16_t exponent;
659
};
660

    
661
struct target_fpxreg {
662
        uint16_t significand[4];
663
        uint16_t exponent;
664
        uint16_t padding[3];
665
};
666

    
667
struct target_xmmreg {
668
        abi_ulong element[4];
669
};
670

    
671
struct target_fpstate {
672
        /* Regular FPU environment */
673
        abi_ulong       cw;
674
        abi_ulong       sw;
675
        abi_ulong       tag;
676
        abi_ulong       ipoff;
677
        abi_ulong       cssel;
678
        abi_ulong       dataoff;
679
        abi_ulong       datasel;
680
        struct target_fpreg        _st[8];
681
        uint16_t        status;
682
        uint16_t        magic;                /* 0xffff = regular FPU data only */
683

    
684
        /* FXSR FPU environment */
685
        abi_ulong       _fxsr_env[6];   /* FXSR FPU env is ignored */
686
        abi_ulong       mxcsr;
687
        abi_ulong       reserved;
688
        struct target_fpxreg        _fxsr_st[8];        /* FXSR FPU reg data is ignored */
689
        struct target_xmmreg        _xmm[8];
690
        abi_ulong       padding[56];
691
};
692

    
693
#define X86_FXSR_MAGIC                0x0000
694

    
695
struct target_sigcontext {
696
        uint16_t gs, __gsh;
697
        uint16_t fs, __fsh;
698
        uint16_t es, __esh;
699
        uint16_t ds, __dsh;
700
        abi_ulong edi;
701
        abi_ulong esi;
702
        abi_ulong ebp;
703
        abi_ulong esp;
704
        abi_ulong ebx;
705
        abi_ulong edx;
706
        abi_ulong ecx;
707
        abi_ulong eax;
708
        abi_ulong trapno;
709
        abi_ulong err;
710
        abi_ulong eip;
711
        uint16_t cs, __csh;
712
        abi_ulong eflags;
713
        abi_ulong esp_at_signal;
714
        uint16_t ss, __ssh;
715
        abi_ulong fpstate; /* pointer */
716
        abi_ulong oldmask;
717
        abi_ulong cr2;
718
};
719

    
720
struct target_ucontext {
721
        abi_ulong         tuc_flags;
722
        abi_ulong         tuc_link;
723
        target_stack_t          tuc_stack;
724
        struct target_sigcontext tuc_mcontext;
725
        target_sigset_t          tuc_sigmask;        /* mask last for extensibility */
726
};
727

    
728
struct sigframe
729
{
730
    abi_ulong pretcode;
731
    int sig;
732
    struct target_sigcontext sc;
733
    struct target_fpstate fpstate;
734
    abi_ulong extramask[TARGET_NSIG_WORDS-1];
735
    char retcode[8];
736
};
737

    
738
struct rt_sigframe
739
{
740
    abi_ulong pretcode;
741
    int sig;
742
    abi_ulong pinfo;
743
    abi_ulong puc;
744
    struct target_siginfo info;
745
    struct target_ucontext uc;
746
    struct target_fpstate fpstate;
747
    char retcode[8];
748
};
749

    
750
/*
751
 * Set up a signal frame.
752
 */
753

    
754
/* XXX: save x87 state */
755
static int
756
setup_sigcontext(struct target_sigcontext *sc, struct target_fpstate *fpstate,
757
                 CPUX86State *env, abi_ulong mask, abi_ulong fpstate_addr)
758
{
759
        int err = 0;
760
        uint16_t magic;
761

    
762
        /* already locked in setup_frame() */
763
        err |= __put_user(env->segs[R_GS].selector, (unsigned int *)&sc->gs);
764
        err |= __put_user(env->segs[R_FS].selector, (unsigned int *)&sc->fs);
765
        err |= __put_user(env->segs[R_ES].selector, (unsigned int *)&sc->es);
766
        err |= __put_user(env->segs[R_DS].selector, (unsigned int *)&sc->ds);
767
        err |= __put_user(env->regs[R_EDI], &sc->edi);
768
        err |= __put_user(env->regs[R_ESI], &sc->esi);
769
        err |= __put_user(env->regs[R_EBP], &sc->ebp);
770
        err |= __put_user(env->regs[R_ESP], &sc->esp);
771
        err |= __put_user(env->regs[R_EBX], &sc->ebx);
772
        err |= __put_user(env->regs[R_EDX], &sc->edx);
773
        err |= __put_user(env->regs[R_ECX], &sc->ecx);
774
        err |= __put_user(env->regs[R_EAX], &sc->eax);
775
        err |= __put_user(env->exception_index, &sc->trapno);
776
        err |= __put_user(env->error_code, &sc->err);
777
        err |= __put_user(env->eip, &sc->eip);
778
        err |= __put_user(env->segs[R_CS].selector, (unsigned int *)&sc->cs);
779
        err |= __put_user(env->eflags, &sc->eflags);
780
        err |= __put_user(env->regs[R_ESP], &sc->esp_at_signal);
781
        err |= __put_user(env->segs[R_SS].selector, (unsigned int *)&sc->ss);
782

    
783
        cpu_x86_fsave(env, fpstate_addr, 1);
784
        fpstate->status = fpstate->sw;
785
        magic = 0xffff;
786
        err |= __put_user(magic, &fpstate->magic);
787
        err |= __put_user(fpstate_addr, &sc->fpstate);
788

    
789
        /* non-iBCS2 extensions.. */
790
        err |= __put_user(mask, &sc->oldmask);
791
        err |= __put_user(env->cr[2], &sc->cr2);
792
        return err;
793
}
794

    
795
/*
796
 * Determine which stack to use..
797
 */
798

    
799
static inline abi_ulong
800
get_sigframe(struct target_sigaction *ka, CPUX86State *env, size_t frame_size)
801
{
802
        unsigned long esp;
803

    
804
        /* Default to using normal stack */
805
        esp = env->regs[R_ESP];
806
        /* This is the X/Open sanctioned signal stack switching.  */
807
        if (ka->sa_flags & TARGET_SA_ONSTACK) {
808
            if (sas_ss_flags(esp) == 0)
809
                esp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
810
        }
811

    
812
        /* This is the legacy signal stack switching. */
813
        else
814
        if ((env->segs[R_SS].selector & 0xffff) != __USER_DS &&
815
            !(ka->sa_flags & TARGET_SA_RESTORER) &&
816
            ka->sa_restorer) {
817
            esp = (unsigned long) ka->sa_restorer;
818
        }
819
        return (esp - frame_size) & -8ul;
820
}
821

    
822
/* compare linux/arch/i386/kernel/signal.c:setup_frame() */
823
static void setup_frame(int sig, struct target_sigaction *ka,
824
                        target_sigset_t *set, CPUX86State *env)
825
{
826
        abi_ulong frame_addr;
827
        struct sigframe *frame;
828
        int i, err = 0;
829

    
830
        frame_addr = get_sigframe(ka, env, sizeof(*frame));
831

    
832
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
833
                goto give_sigsegv;
834

    
835
        err |= __put_user(current_exec_domain_sig(sig),
836
                          &frame->sig);
837
        if (err)
838
                goto give_sigsegv;
839

    
840
        setup_sigcontext(&frame->sc, &frame->fpstate, env, set->sig[0],
841
                         frame_addr + offsetof(struct sigframe, fpstate));
842
        if (err)
843
                goto give_sigsegv;
844

    
845
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
846
            if (__put_user(set->sig[i], &frame->extramask[i - 1]))
847
                goto give_sigsegv;
848
        }
849

    
850
        /* Set up to return from userspace.  If provided, use a stub
851
           already in userspace.  */
852
        if (ka->sa_flags & TARGET_SA_RESTORER) {
853
                err |= __put_user(ka->sa_restorer, &frame->pretcode);
854
        } else {
855
                uint16_t val16;
856
                abi_ulong retcode_addr;
857
                retcode_addr = frame_addr + offsetof(struct sigframe, retcode);
858
                err |= __put_user(retcode_addr, &frame->pretcode);
859
                /* This is popl %eax ; movl $,%eax ; int $0x80 */
860
                val16 = 0xb858;
861
                err |= __put_user(val16, (uint16_t *)(frame->retcode+0));
862
                err |= __put_user(TARGET_NR_sigreturn, (int *)(frame->retcode+2));
863
                val16 = 0x80cd;
864
                err |= __put_user(val16, (uint16_t *)(frame->retcode+6));
865
        }
866

    
867
        if (err)
868
                goto give_sigsegv;
869

    
870
        /* Set up registers for signal handler */
871
        env->regs[R_ESP] = frame_addr;
872
        env->eip = ka->_sa_handler;
873

    
874
        cpu_x86_load_seg(env, R_DS, __USER_DS);
875
        cpu_x86_load_seg(env, R_ES, __USER_DS);
876
        cpu_x86_load_seg(env, R_SS, __USER_DS);
877
        cpu_x86_load_seg(env, R_CS, __USER_CS);
878
        env->eflags &= ~TF_MASK;
879

    
880
        unlock_user_struct(frame, frame_addr, 1);
881

    
882
        return;
883

    
884
give_sigsegv:
885
        unlock_user_struct(frame, frame_addr, 1);
886
        if (sig == TARGET_SIGSEGV)
887
                ka->_sa_handler = TARGET_SIG_DFL;
888
        force_sig(TARGET_SIGSEGV /* , current */);
889
}
890

    
891
/* compare linux/arch/i386/kernel/signal.c:setup_rt_frame() */
892
static void setup_rt_frame(int sig, struct target_sigaction *ka,
893
                           target_siginfo_t *info,
894
                           target_sigset_t *set, CPUX86State *env)
895
{
896
        abi_ulong frame_addr, addr;
897
        struct rt_sigframe *frame;
898
        int i, err = 0;
899

    
900
        frame_addr = get_sigframe(ka, env, sizeof(*frame));
901

    
902
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
903
                goto give_sigsegv;
904

    
905
        err |= __put_user(current_exec_domain_sig(sig),
906
                          &frame->sig);
907
        addr = frame_addr + offsetof(struct rt_sigframe, info);
908
        err |= __put_user(addr, &frame->pinfo);
909
        addr = frame_addr + offsetof(struct rt_sigframe, uc);
910
        err |= __put_user(addr, &frame->puc);
911
        err |= copy_siginfo_to_user(&frame->info, info);
912
        if (err)
913
                goto give_sigsegv;
914

    
915
        /* Create the ucontext.  */
916
        err |= __put_user(0, &frame->uc.tuc_flags);
917
        err |= __put_user(0, &frame->uc.tuc_link);
918
        err |= __put_user(target_sigaltstack_used.ss_sp,
919
                          &frame->uc.tuc_stack.ss_sp);
920
        err |= __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
921
                          &frame->uc.tuc_stack.ss_flags);
922
        err |= __put_user(target_sigaltstack_used.ss_size,
923
                          &frame->uc.tuc_stack.ss_size);
924
        err |= setup_sigcontext(&frame->uc.tuc_mcontext, &frame->fpstate,
925
                                env, set->sig[0], 
926
                                frame_addr + offsetof(struct rt_sigframe, fpstate));
927
        for(i = 0; i < TARGET_NSIG_WORDS; i++) {
928
            if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
929
                goto give_sigsegv;
930
        }
931

    
932
        /* Set up to return from userspace.  If provided, use a stub
933
           already in userspace.  */
934
        if (ka->sa_flags & TARGET_SA_RESTORER) {
935
                err |= __put_user(ka->sa_restorer, &frame->pretcode);
936
        } else {
937
                uint16_t val16;
938
                addr = frame_addr + offsetof(struct rt_sigframe, retcode);
939
                err |= __put_user(addr, &frame->pretcode);
940
                /* This is movl $,%eax ; int $0x80 */
941
                err |= __put_user(0xb8, (char *)(frame->retcode+0));
942
                err |= __put_user(TARGET_NR_rt_sigreturn, (int *)(frame->retcode+1));
943
                val16 = 0x80cd;
944
                err |= __put_user(val16, (uint16_t *)(frame->retcode+5));
945
        }
946

    
947
        if (err)
948
                goto give_sigsegv;
949

    
950
        /* Set up registers for signal handler */
951
        env->regs[R_ESP] = frame_addr;
952
        env->eip = ka->_sa_handler;
953

    
954
        cpu_x86_load_seg(env, R_DS, __USER_DS);
955
        cpu_x86_load_seg(env, R_ES, __USER_DS);
956
        cpu_x86_load_seg(env, R_SS, __USER_DS);
957
        cpu_x86_load_seg(env, R_CS, __USER_CS);
958
        env->eflags &= ~TF_MASK;
959

    
960
        unlock_user_struct(frame, frame_addr, 1);
961

    
962
        return;
963

    
964
give_sigsegv:
965
        unlock_user_struct(frame, frame_addr, 1);
966
        if (sig == TARGET_SIGSEGV)
967
                ka->_sa_handler = TARGET_SIG_DFL;
968
        force_sig(TARGET_SIGSEGV /* , current */);
969
}
970

    
971
static int
972
restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)
973
{
974
        unsigned int err = 0;
975
        abi_ulong fpstate_addr;
976
        unsigned int tmpflags;
977

    
978
        cpu_x86_load_seg(env, R_GS, tswap16(sc->gs));
979
        cpu_x86_load_seg(env, R_FS, tswap16(sc->fs));
980
        cpu_x86_load_seg(env, R_ES, tswap16(sc->es));
981
        cpu_x86_load_seg(env, R_DS, tswap16(sc->ds));
982

    
983
        env->regs[R_EDI] = tswapl(sc->edi);
984
        env->regs[R_ESI] = tswapl(sc->esi);
985
        env->regs[R_EBP] = tswapl(sc->ebp);
986
        env->regs[R_ESP] = tswapl(sc->esp);
987
        env->regs[R_EBX] = tswapl(sc->ebx);
988
        env->regs[R_EDX] = tswapl(sc->edx);
989
        env->regs[R_ECX] = tswapl(sc->ecx);
990
        env->eip = tswapl(sc->eip);
991

    
992
        cpu_x86_load_seg(env, R_CS, lduw_p(&sc->cs) | 3);
993
        cpu_x86_load_seg(env, R_SS, lduw_p(&sc->ss) | 3);
994

    
995
        tmpflags = tswapl(sc->eflags);
996
        env->eflags = (env->eflags & ~0x40DD5) | (tmpflags & 0x40DD5);
997
        //                regs->orig_eax = -1;                /* disable syscall checks */
998

    
999
        fpstate_addr = tswapl(sc->fpstate);
1000
        if (fpstate_addr != 0) {
1001
                if (!access_ok(VERIFY_READ, fpstate_addr, 
1002
                               sizeof(struct target_fpstate)))
1003
                        goto badframe;
1004
                cpu_x86_frstor(env, fpstate_addr, 1);
1005
        }
1006

    
1007
        *peax = tswapl(sc->eax);
1008
        return err;
1009
badframe:
1010
        return 1;
1011
}
1012

    
1013
long do_sigreturn(CPUX86State *env)
1014
{
1015
    struct sigframe *frame;
1016
    abi_ulong frame_addr = env->regs[R_ESP] - 8;
1017
    target_sigset_t target_set;
1018
    sigset_t set;
1019
    int eax, i;
1020

    
1021
#if defined(DEBUG_SIGNAL)
1022
    fprintf(stderr, "do_sigreturn\n");
1023
#endif
1024
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1025
        goto badframe;
1026
    /* set blocked signals */
1027
    if (__get_user(target_set.sig[0], &frame->sc.oldmask))
1028
        goto badframe;
1029
    for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1030
        if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
1031
            goto badframe;
1032
    }
1033

    
1034
    target_to_host_sigset_internal(&set, &target_set);
1035
    sigprocmask(SIG_SETMASK, &set, NULL);
1036

    
1037
    /* restore registers */
1038
    if (restore_sigcontext(env, &frame->sc, &eax))
1039
        goto badframe;
1040
    unlock_user_struct(frame, frame_addr, 0);
1041
    return eax;
1042

    
1043
badframe:
1044
    unlock_user_struct(frame, frame_addr, 0);
1045
    force_sig(TARGET_SIGSEGV);
1046
    return 0;
1047
}
1048

    
1049
long do_rt_sigreturn(CPUX86State *env)
1050
{
1051
        abi_ulong frame_addr;
1052
        struct rt_sigframe *frame;
1053
        sigset_t set;
1054
        int eax;
1055

    
1056
        frame_addr = env->regs[R_ESP] - 4;
1057
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1058
                goto badframe;
1059
        target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
1060
        sigprocmask(SIG_SETMASK, &set, NULL);
1061

    
1062
        if (restore_sigcontext(env, &frame->uc.tuc_mcontext, &eax))
1063
                goto badframe;
1064

    
1065
        if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe, uc.tuc_stack), 0, 
1066
                           get_sp_from_cpustate(env)) == -EFAULT)
1067
                goto badframe;
1068

    
1069
        unlock_user_struct(frame, frame_addr, 0);
1070
        return eax;
1071

    
1072
badframe:
1073
        unlock_user_struct(frame, frame_addr, 0);
1074
        force_sig(TARGET_SIGSEGV);
1075
        return 0;
1076
}
1077

    
1078
#elif defined(TARGET_ARM)
1079

    
1080
struct target_sigcontext {
1081
        abi_ulong trap_no;
1082
        abi_ulong error_code;
1083
        abi_ulong oldmask;
1084
        abi_ulong arm_r0;
1085
        abi_ulong arm_r1;
1086
        abi_ulong arm_r2;
1087
        abi_ulong arm_r3;
1088
        abi_ulong arm_r4;
1089
        abi_ulong arm_r5;
1090
        abi_ulong arm_r6;
1091
        abi_ulong arm_r7;
1092
        abi_ulong arm_r8;
1093
        abi_ulong arm_r9;
1094
        abi_ulong arm_r10;
1095
        abi_ulong arm_fp;
1096
        abi_ulong arm_ip;
1097
        abi_ulong arm_sp;
1098
        abi_ulong arm_lr;
1099
        abi_ulong arm_pc;
1100
        abi_ulong arm_cpsr;
1101
        abi_ulong fault_address;
1102
};
1103

    
1104
struct target_ucontext_v1 {
1105
    abi_ulong tuc_flags;
1106
    abi_ulong tuc_link;
1107
    target_stack_t tuc_stack;
1108
    struct target_sigcontext tuc_mcontext;
1109
    target_sigset_t  tuc_sigmask;        /* mask last for extensibility */
1110
};
1111

    
1112
struct target_ucontext_v2 {
1113
    abi_ulong tuc_flags;
1114
    abi_ulong tuc_link;
1115
    target_stack_t tuc_stack;
1116
    struct target_sigcontext tuc_mcontext;
1117
    target_sigset_t  tuc_sigmask;        /* mask last for extensibility */
1118
    char __unused[128 - sizeof(target_sigset_t)];
1119
    abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
1120
};
1121

    
1122
struct target_user_vfp {
1123
    uint64_t fpregs[32];
1124
    abi_ulong fpscr;
1125
};
1126

    
1127
struct target_user_vfp_exc {
1128
    abi_ulong fpexc;
1129
    abi_ulong fpinst;
1130
    abi_ulong fpinst2;
1131
};
1132

    
1133
struct target_vfp_sigframe {
1134
    abi_ulong magic;
1135
    abi_ulong size;
1136
    struct target_user_vfp ufp;
1137
    struct target_user_vfp_exc ufp_exc;
1138
} __attribute__((__aligned__(8)));
1139

    
1140
struct target_iwmmxt_sigframe {
1141
    abi_ulong magic;
1142
    abi_ulong size;
1143
    uint64_t regs[16];
1144
    /* Note that not all the coprocessor control registers are stored here */
1145
    uint32_t wcssf;
1146
    uint32_t wcasf;
1147
    uint32_t wcgr0;
1148
    uint32_t wcgr1;
1149
    uint32_t wcgr2;
1150
    uint32_t wcgr3;
1151
} __attribute__((__aligned__(8)));
1152

    
1153
#define TARGET_VFP_MAGIC 0x56465001
1154
#define TARGET_IWMMXT_MAGIC 0x12ef842a
1155

    
1156
struct sigframe_v1
1157
{
1158
    struct target_sigcontext sc;
1159
    abi_ulong extramask[TARGET_NSIG_WORDS-1];
1160
    abi_ulong retcode;
1161
};
1162

    
1163
struct sigframe_v2
1164
{
1165
    struct target_ucontext_v2 uc;
1166
    abi_ulong retcode;
1167
};
1168

    
1169
struct rt_sigframe_v1
1170
{
1171
    abi_ulong pinfo;
1172
    abi_ulong puc;
1173
    struct target_siginfo info;
1174
    struct target_ucontext_v1 uc;
1175
    abi_ulong retcode;
1176
};
1177

    
1178
struct rt_sigframe_v2
1179
{
1180
    struct target_siginfo info;
1181
    struct target_ucontext_v2 uc;
1182
    abi_ulong retcode;
1183
};
1184

    
1185
#define TARGET_CONFIG_CPU_32 1
1186

    
1187
/*
1188
 * For ARM syscalls, we encode the syscall number into the instruction.
1189
 */
1190
#define SWI_SYS_SIGRETURN        (0xef000000|(TARGET_NR_sigreturn + ARM_SYSCALL_BASE))
1191
#define SWI_SYS_RT_SIGRETURN        (0xef000000|(TARGET_NR_rt_sigreturn + ARM_SYSCALL_BASE))
1192

    
1193
/*
1194
 * For Thumb syscalls, we pass the syscall number via r7.  We therefore
1195
 * need two 16-bit instructions.
1196
 */
1197
#define SWI_THUMB_SIGRETURN        (0xdf00 << 16 | 0x2700 | (TARGET_NR_sigreturn))
1198
#define SWI_THUMB_RT_SIGRETURN        (0xdf00 << 16 | 0x2700 | (TARGET_NR_rt_sigreturn))
1199

    
1200
static const abi_ulong retcodes[4] = {
1201
        SWI_SYS_SIGRETURN,        SWI_THUMB_SIGRETURN,
1202
        SWI_SYS_RT_SIGRETURN,        SWI_THUMB_RT_SIGRETURN
1203
};
1204

    
1205

    
1206
#define __get_user_error(x,p,e) __get_user(x, p)
1207

    
1208
static inline int valid_user_regs(CPUState *regs)
1209
{
1210
    return 1;
1211
}
1212

    
1213
static void
1214
setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1215
                 CPUState *env, abi_ulong mask)
1216
{
1217
        __put_user(env->regs[0], &sc->arm_r0);
1218
        __put_user(env->regs[1], &sc->arm_r1);
1219
        __put_user(env->regs[2], &sc->arm_r2);
1220
        __put_user(env->regs[3], &sc->arm_r3);
1221
        __put_user(env->regs[4], &sc->arm_r4);
1222
        __put_user(env->regs[5], &sc->arm_r5);
1223
        __put_user(env->regs[6], &sc->arm_r6);
1224
        __put_user(env->regs[7], &sc->arm_r7);
1225
        __put_user(env->regs[8], &sc->arm_r8);
1226
        __put_user(env->regs[9], &sc->arm_r9);
1227
        __put_user(env->regs[10], &sc->arm_r10);
1228
        __put_user(env->regs[11], &sc->arm_fp);
1229
        __put_user(env->regs[12], &sc->arm_ip);
1230
        __put_user(env->regs[13], &sc->arm_sp);
1231
        __put_user(env->regs[14], &sc->arm_lr);
1232
        __put_user(env->regs[15], &sc->arm_pc);
1233
#ifdef TARGET_CONFIG_CPU_32
1234
        __put_user(cpsr_read(env), &sc->arm_cpsr);
1235
#endif
1236

    
1237
        __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
1238
        __put_user(/* current->thread.error_code */ 0, &sc->error_code);
1239
        __put_user(/* current->thread.address */ 0, &sc->fault_address);
1240
        __put_user(mask, &sc->oldmask);
1241
}
1242

    
1243
static inline abi_ulong
1244
get_sigframe(struct target_sigaction *ka, CPUState *regs, int framesize)
1245
{
1246
        unsigned long sp = regs->regs[13];
1247

    
1248
        /*
1249
         * This is the X/Open sanctioned signal stack switching.
1250
         */
1251
        if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp))
1252
            sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1253
        /*
1254
         * ATPCS B01 mandates 8-byte alignment
1255
         */
1256
        return (sp - framesize) & ~7;
1257
}
1258

    
1259
static int
1260
setup_return(CPUState *env, struct target_sigaction *ka,
1261
             abi_ulong *rc, abi_ulong frame_addr, int usig, abi_ulong rc_addr)
1262
{
1263
        abi_ulong handler = ka->_sa_handler;
1264
        abi_ulong retcode;
1265
        int thumb = handler & 1;
1266
        uint32_t cpsr = cpsr_read(env);
1267

    
1268
        cpsr &= ~CPSR_IT;
1269
        if (thumb) {
1270
                cpsr |= CPSR_T;
1271
        } else {
1272
                cpsr &= ~CPSR_T;
1273
        }
1274

    
1275
        if (ka->sa_flags & TARGET_SA_RESTORER) {
1276
                retcode = ka->sa_restorer;
1277
        } else {
1278
                unsigned int idx = thumb;
1279

    
1280
                if (ka->sa_flags & TARGET_SA_SIGINFO)
1281
                        idx += 2;
1282

    
1283
                if (__put_user(retcodes[idx], rc))
1284
                        return 1;
1285

    
1286
                retcode = rc_addr + thumb;
1287
        }
1288

    
1289
        env->regs[0] = usig;
1290
        env->regs[13] = frame_addr;
1291
        env->regs[14] = retcode;
1292
        env->regs[15] = handler & (thumb ? ~1 : ~3);
1293
        cpsr_write(env, cpsr, 0xffffffff);
1294

    
1295
        return 0;
1296
}
1297

    
1298
static abi_ulong *setup_sigframe_v2_vfp(abi_ulong *regspace, CPUState *env)
1299
{
1300
    int i;
1301
    struct target_vfp_sigframe *vfpframe;
1302
    vfpframe = (struct target_vfp_sigframe *)regspace;
1303
    __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
1304
    __put_user(sizeof(*vfpframe), &vfpframe->size);
1305
    for (i = 0; i < 32; i++) {
1306
        __put_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1307
    }
1308
    __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
1309
    __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
1310
    __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1311
    __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1312
    return (abi_ulong*)(vfpframe+1);
1313
}
1314

    
1315
static abi_ulong *setup_sigframe_v2_iwmmxt(abi_ulong *regspace, CPUState *env)
1316
{
1317
    int i;
1318
    struct target_iwmmxt_sigframe *iwmmxtframe;
1319
    iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1320
    __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
1321
    __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
1322
    for (i = 0; i < 16; i++) {
1323
        __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1324
    }
1325
    __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1326
    __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1327
    __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1328
    __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1329
    __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1330
    __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1331
    return (abi_ulong*)(iwmmxtframe+1);
1332
}
1333

    
1334
static void setup_sigframe_v2(struct target_ucontext_v2 *uc,
1335
                              target_sigset_t *set, CPUState *env)
1336
{
1337
    struct target_sigaltstack stack;
1338
    int i;
1339
    abi_ulong *regspace;
1340

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

    
1344
    memset(&stack, 0, sizeof(stack));
1345
    __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1346
    __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1347
    __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1348
    memcpy(&uc->tuc_stack, &stack, sizeof(stack));
1349

    
1350
    setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
1351
    /* Save coprocessor signal frame.  */
1352
    regspace = uc->tuc_regspace;
1353
    if (arm_feature(env, ARM_FEATURE_VFP)) {
1354
        regspace = setup_sigframe_v2_vfp(regspace, env);
1355
    }
1356
    if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1357
        regspace = setup_sigframe_v2_iwmmxt(regspace, env);
1358
    }
1359

    
1360
    /* Write terminating magic word */
1361
    __put_user(0, regspace);
1362

    
1363
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1364
        __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
1365
    }
1366
}
1367

    
1368
/* compare linux/arch/arm/kernel/signal.c:setup_frame() */
1369
static void setup_frame_v1(int usig, struct target_sigaction *ka,
1370
                           target_sigset_t *set, CPUState *regs)
1371
{
1372
        struct sigframe_v1 *frame;
1373
        abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1374
        int i;
1375

    
1376
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1377
                return;
1378

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

    
1381
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1382
            if (__put_user(set->sig[i], &frame->extramask[i - 1]))
1383
                goto end;
1384
        }
1385

    
1386
        setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1387
                     frame_addr + offsetof(struct sigframe_v1, retcode));
1388

    
1389
end:
1390
        unlock_user_struct(frame, frame_addr, 1);
1391
}
1392

    
1393
static void setup_frame_v2(int usig, struct target_sigaction *ka,
1394
                           target_sigset_t *set, CPUState *regs)
1395
{
1396
        struct sigframe_v2 *frame;
1397
        abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
1398

    
1399
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1400
                return;
1401

    
1402
        setup_sigframe_v2(&frame->uc, set, regs);
1403

    
1404
        setup_return(regs, ka, &frame->retcode, frame_addr, usig,
1405
                     frame_addr + offsetof(struct sigframe_v2, retcode));
1406

    
1407
        unlock_user_struct(frame, frame_addr, 1);
1408
}
1409

    
1410
static void setup_frame(int usig, struct target_sigaction *ka,
1411
                        target_sigset_t *set, CPUState *regs)
1412
{
1413
    if (get_osversion() >= 0x020612) {
1414
        setup_frame_v2(usig, ka, set, regs);
1415
    } else {
1416
        setup_frame_v1(usig, ka, set, regs);
1417
    }
1418
}
1419

    
1420
/* compare linux/arch/arm/kernel/signal.c:setup_rt_frame() */
1421
static void setup_rt_frame_v1(int usig, struct target_sigaction *ka,
1422
                              target_siginfo_t *info,
1423
                              target_sigset_t *set, CPUState *env)
1424
{
1425
        struct rt_sigframe_v1 *frame;
1426
        abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1427
        struct target_sigaltstack stack;
1428
        int i;
1429
        abi_ulong info_addr, uc_addr;
1430

    
1431
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1432
            return /* 1 */;
1433

    
1434
        info_addr = frame_addr + offsetof(struct rt_sigframe_v1, info);
1435
        __put_user(info_addr, &frame->pinfo);
1436
        uc_addr = frame_addr + offsetof(struct rt_sigframe_v1, uc);
1437
        __put_user(uc_addr, &frame->puc);
1438
        copy_siginfo_to_user(&frame->info, info);
1439

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

    
1443
        memset(&stack, 0, sizeof(stack));
1444
        __put_user(target_sigaltstack_used.ss_sp, &stack.ss_sp);
1445
        __put_user(target_sigaltstack_used.ss_size, &stack.ss_size);
1446
        __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &stack.ss_flags);
1447
        memcpy(&frame->uc.tuc_stack, &stack, sizeof(stack));
1448

    
1449
        setup_sigcontext(&frame->uc.tuc_mcontext, env, set->sig[0]);
1450
        for(i = 0; i < TARGET_NSIG_WORDS; i++) {
1451
            if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
1452
                goto end;
1453
        }
1454

    
1455
        setup_return(env, ka, &frame->retcode, frame_addr, usig,
1456
                     frame_addr + offsetof(struct rt_sigframe_v1, retcode));
1457

    
1458
        env->regs[1] = info_addr;
1459
        env->regs[2] = uc_addr;
1460

    
1461
end:
1462
        unlock_user_struct(frame, frame_addr, 1);
1463
}
1464

    
1465
static void setup_rt_frame_v2(int usig, struct target_sigaction *ka,
1466
                              target_siginfo_t *info,
1467
                              target_sigset_t *set, CPUState *env)
1468
{
1469
        struct rt_sigframe_v2 *frame;
1470
        abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
1471
        abi_ulong info_addr, uc_addr;
1472

    
1473
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
1474
            return /* 1 */;
1475

    
1476
        info_addr = frame_addr + offsetof(struct rt_sigframe_v2, info);
1477
        uc_addr = frame_addr + offsetof(struct rt_sigframe_v2, uc);
1478
        copy_siginfo_to_user(&frame->info, info);
1479

    
1480
        setup_sigframe_v2(&frame->uc, set, env);
1481

    
1482
        setup_return(env, ka, &frame->retcode, frame_addr, usig,
1483
                     frame_addr + offsetof(struct rt_sigframe_v2, retcode));
1484

    
1485
        env->regs[1] = info_addr;
1486
        env->regs[2] = uc_addr;
1487

    
1488
        unlock_user_struct(frame, frame_addr, 1);
1489
}
1490

    
1491
static void setup_rt_frame(int usig, struct target_sigaction *ka,
1492
                           target_siginfo_t *info,
1493
                           target_sigset_t *set, CPUState *env)
1494
{
1495
    if (get_osversion() >= 0x020612) {
1496
        setup_rt_frame_v2(usig, ka, info, set, env);
1497
    } else {
1498
        setup_rt_frame_v1(usig, ka, info, set, env);
1499
    }
1500
}
1501

    
1502
static int
1503
restore_sigcontext(CPUState *env, struct target_sigcontext *sc)
1504
{
1505
        int err = 0;
1506
        uint32_t cpsr;
1507

    
1508
        __get_user_error(env->regs[0], &sc->arm_r0, err);
1509
        __get_user_error(env->regs[1], &sc->arm_r1, err);
1510
        __get_user_error(env->regs[2], &sc->arm_r2, err);
1511
        __get_user_error(env->regs[3], &sc->arm_r3, err);
1512
        __get_user_error(env->regs[4], &sc->arm_r4, err);
1513
        __get_user_error(env->regs[5], &sc->arm_r5, err);
1514
        __get_user_error(env->regs[6], &sc->arm_r6, err);
1515
        __get_user_error(env->regs[7], &sc->arm_r7, err);
1516
        __get_user_error(env->regs[8], &sc->arm_r8, err);
1517
        __get_user_error(env->regs[9], &sc->arm_r9, err);
1518
        __get_user_error(env->regs[10], &sc->arm_r10, err);
1519
        __get_user_error(env->regs[11], &sc->arm_fp, err);
1520
        __get_user_error(env->regs[12], &sc->arm_ip, err);
1521
        __get_user_error(env->regs[13], &sc->arm_sp, err);
1522
        __get_user_error(env->regs[14], &sc->arm_lr, err);
1523
        __get_user_error(env->regs[15], &sc->arm_pc, err);
1524
#ifdef TARGET_CONFIG_CPU_32
1525
        __get_user_error(cpsr, &sc->arm_cpsr, err);
1526
        cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC);
1527
#endif
1528

    
1529
        err |= !valid_user_regs(env);
1530

    
1531
        return err;
1532
}
1533

    
1534
static long do_sigreturn_v1(CPUState *env)
1535
{
1536
        abi_ulong frame_addr;
1537
        struct sigframe_v1 *frame;
1538
        target_sigset_t set;
1539
        sigset_t host_set;
1540
        int i;
1541

    
1542
        /*
1543
         * Since we stacked the signal on a 64-bit boundary,
1544
         * then 'sp' should be word aligned here.  If it's
1545
         * not, then the user is trying to mess with us.
1546
         */
1547
        if (env->regs[13] & 7)
1548
                goto badframe;
1549

    
1550
        frame_addr = env->regs[13];
1551
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1552
                goto badframe;
1553

    
1554
        if (__get_user(set.sig[0], &frame->sc.oldmask))
1555
            goto badframe;
1556
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
1557
            if (__get_user(set.sig[i], &frame->extramask[i - 1]))
1558
                goto badframe;
1559
        }
1560

    
1561
        target_to_host_sigset_internal(&host_set, &set);
1562
        sigprocmask(SIG_SETMASK, &host_set, NULL);
1563

    
1564
        if (restore_sigcontext(env, &frame->sc))
1565
                goto badframe;
1566

    
1567
#if 0
1568
        /* Send SIGTRAP if we're single-stepping */
1569
        if (ptrace_cancel_bpt(current))
1570
                send_sig(SIGTRAP, current, 1);
1571
#endif
1572
        unlock_user_struct(frame, frame_addr, 0);
1573
        return env->regs[0];
1574

    
1575
badframe:
1576
        unlock_user_struct(frame, frame_addr, 0);
1577
        force_sig(TARGET_SIGSEGV /* , current */);
1578
        return 0;
1579
}
1580

    
1581
static abi_ulong *restore_sigframe_v2_vfp(CPUState *env, abi_ulong *regspace)
1582
{
1583
    int i;
1584
    abi_ulong magic, sz;
1585
    uint32_t fpscr, fpexc;
1586
    struct target_vfp_sigframe *vfpframe;
1587
    vfpframe = (struct target_vfp_sigframe *)regspace;
1588

    
1589
    __get_user(magic, &vfpframe->magic);
1590
    __get_user(sz, &vfpframe->size);
1591
    if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
1592
        return 0;
1593
    }
1594
    for (i = 0; i < 32; i++) {
1595
        __get_user(float64_val(env->vfp.regs[i]), &vfpframe->ufp.fpregs[i]);
1596
    }
1597
    __get_user(fpscr, &vfpframe->ufp.fpscr);
1598
    vfp_set_fpscr(env, fpscr);
1599
    __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
1600
    /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
1601
     * and the exception flag is cleared
1602
     */
1603
    fpexc |= (1 << 30);
1604
    fpexc &= ~((1 << 31) | (1 << 28));
1605
    env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
1606
    __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
1607
    __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
1608
    return (abi_ulong*)(vfpframe + 1);
1609
}
1610

    
1611
static abi_ulong *restore_sigframe_v2_iwmmxt(CPUState *env, abi_ulong *regspace)
1612
{
1613
    int i;
1614
    abi_ulong magic, sz;
1615
    struct target_iwmmxt_sigframe *iwmmxtframe;
1616
    iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
1617

    
1618
    __get_user(magic, &iwmmxtframe->magic);
1619
    __get_user(sz, &iwmmxtframe->size);
1620
    if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
1621
        return 0;
1622
    }
1623
    for (i = 0; i < 16; i++) {
1624
        __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
1625
    }
1626
    __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
1627
    __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
1628
    __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
1629
    __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
1630
    __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
1631
    __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
1632
    return (abi_ulong*)(iwmmxtframe + 1);
1633
}
1634

    
1635
static int do_sigframe_return_v2(CPUState *env, target_ulong frame_addr,
1636
                                 struct target_ucontext_v2 *uc)
1637
{
1638
    sigset_t host_set;
1639
    abi_ulong *regspace;
1640

    
1641
    target_to_host_sigset(&host_set, &uc->tuc_sigmask);
1642
    sigprocmask(SIG_SETMASK, &host_set, NULL);
1643

    
1644
    if (restore_sigcontext(env, &uc->tuc_mcontext))
1645
        return 1;
1646

    
1647
    /* Restore coprocessor signal frame */
1648
    regspace = uc->tuc_regspace;
1649
    if (arm_feature(env, ARM_FEATURE_VFP)) {
1650
        regspace = restore_sigframe_v2_vfp(env, regspace);
1651
        if (!regspace) {
1652
            return 1;
1653
        }
1654
    }
1655
    if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
1656
        regspace = restore_sigframe_v2_iwmmxt(env, regspace);
1657
        if (!regspace) {
1658
            return 1;
1659
        }
1660
    }
1661

    
1662
    if (do_sigaltstack(frame_addr + offsetof(struct target_ucontext_v2, tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1663
        return 1;
1664

    
1665
#if 0
1666
    /* Send SIGTRAP if we're single-stepping */
1667
    if (ptrace_cancel_bpt(current))
1668
            send_sig(SIGTRAP, current, 1);
1669
#endif
1670

    
1671
    return 0;
1672
}
1673

    
1674
static long do_sigreturn_v2(CPUState *env)
1675
{
1676
        abi_ulong frame_addr;
1677
        struct sigframe_v2 *frame;
1678

    
1679
        /*
1680
         * Since we stacked the signal on a 64-bit boundary,
1681
         * then 'sp' should be word aligned here.  If it's
1682
         * not, then the user is trying to mess with us.
1683
         */
1684
        if (env->regs[13] & 7)
1685
                goto badframe;
1686

    
1687
        frame_addr = env->regs[13];
1688
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1689
                goto badframe;
1690

    
1691
        if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1692
                goto badframe;
1693

    
1694
        unlock_user_struct(frame, frame_addr, 0);
1695
        return env->regs[0];
1696

    
1697
badframe:
1698
        unlock_user_struct(frame, frame_addr, 0);
1699
        force_sig(TARGET_SIGSEGV /* , current */);
1700
        return 0;
1701
}
1702

    
1703
long do_sigreturn(CPUState *env)
1704
{
1705
    if (get_osversion() >= 0x020612) {
1706
        return do_sigreturn_v2(env);
1707
    } else {
1708
        return do_sigreturn_v1(env);
1709
    }
1710
}
1711

    
1712
static long do_rt_sigreturn_v1(CPUState *env)
1713
{
1714
        abi_ulong frame_addr;
1715
        struct rt_sigframe_v1 *frame;
1716
        sigset_t host_set;
1717

    
1718
        /*
1719
         * Since we stacked the signal on a 64-bit boundary,
1720
         * then 'sp' should be word aligned here.  If it's
1721
         * not, then the user is trying to mess with us.
1722
         */
1723
        if (env->regs[13] & 7)
1724
                goto badframe;
1725

    
1726
        frame_addr = env->regs[13];
1727
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1728
                goto badframe;
1729

    
1730
        target_to_host_sigset(&host_set, &frame->uc.tuc_sigmask);
1731
        sigprocmask(SIG_SETMASK, &host_set, NULL);
1732

    
1733
        if (restore_sigcontext(env, &frame->uc.tuc_mcontext))
1734
                goto badframe;
1735

    
1736
        if (do_sigaltstack(frame_addr + offsetof(struct rt_sigframe_v1, uc.tuc_stack), 0, get_sp_from_cpustate(env)) == -EFAULT)
1737
                goto badframe;
1738

    
1739
#if 0
1740
        /* Send SIGTRAP if we're single-stepping */
1741
        if (ptrace_cancel_bpt(current))
1742
                send_sig(SIGTRAP, current, 1);
1743
#endif
1744
        unlock_user_struct(frame, frame_addr, 0);
1745
        return env->regs[0];
1746

    
1747
badframe:
1748
        unlock_user_struct(frame, frame_addr, 0);
1749
        force_sig(TARGET_SIGSEGV /* , current */);
1750
        return 0;
1751
}
1752

    
1753
static long do_rt_sigreturn_v2(CPUState *env)
1754
{
1755
        abi_ulong frame_addr;
1756
        struct rt_sigframe_v2 *frame;
1757

    
1758
        /*
1759
         * Since we stacked the signal on a 64-bit boundary,
1760
         * then 'sp' should be word aligned here.  If it's
1761
         * not, then the user is trying to mess with us.
1762
         */
1763
        if (env->regs[13] & 7)
1764
                goto badframe;
1765

    
1766
        frame_addr = env->regs[13];
1767
        if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
1768
                goto badframe;
1769

    
1770
        if (do_sigframe_return_v2(env, frame_addr, &frame->uc))
1771
                goto badframe;
1772

    
1773
        unlock_user_struct(frame, frame_addr, 0);
1774
        return env->regs[0];
1775

    
1776
badframe:
1777
        unlock_user_struct(frame, frame_addr, 0);
1778
        force_sig(TARGET_SIGSEGV /* , current */);
1779
        return 0;
1780
}
1781

    
1782
long do_rt_sigreturn(CPUState *env)
1783
{
1784
    if (get_osversion() >= 0x020612) {
1785
        return do_rt_sigreturn_v2(env);
1786
    } else {
1787
        return do_rt_sigreturn_v1(env);
1788
    }
1789
}
1790

    
1791
#elif defined(TARGET_SPARC)
1792

    
1793
#define __SUNOS_MAXWIN   31
1794

    
1795
/* This is what SunOS does, so shall I. */
1796
struct target_sigcontext {
1797
        abi_ulong sigc_onstack;      /* state to restore */
1798

    
1799
        abi_ulong sigc_mask;         /* sigmask to restore */
1800
        abi_ulong sigc_sp;           /* stack pointer */
1801
        abi_ulong sigc_pc;           /* program counter */
1802
        abi_ulong sigc_npc;          /* next program counter */
1803
        abi_ulong sigc_psr;          /* for condition codes etc */
1804
        abi_ulong sigc_g1;           /* User uses these two registers */
1805
        abi_ulong sigc_o0;           /* within the trampoline code. */
1806

    
1807
        /* Now comes information regarding the users window set
1808
         * at the time of the signal.
1809
         */
1810
        abi_ulong sigc_oswins;       /* outstanding windows */
1811

    
1812
        /* stack ptrs for each regwin buf */
1813
        char *sigc_spbuf[__SUNOS_MAXWIN];
1814

    
1815
        /* Windows to restore after signal */
1816
        struct {
1817
                abi_ulong locals[8];
1818
                abi_ulong ins[8];
1819
        } sigc_wbuf[__SUNOS_MAXWIN];
1820
};
1821
/* A Sparc stack frame */
1822
struct sparc_stackf {
1823
        abi_ulong locals[8];
1824
        abi_ulong ins[8];
1825
        /* It's simpler to treat fp and callers_pc as elements of ins[]
1826
         * since we never need to access them ourselves.
1827
         */
1828
        char *structptr;
1829
        abi_ulong xargs[6];
1830
        abi_ulong xxargs[1];
1831
};
1832

    
1833
typedef struct {
1834
        struct {
1835
                abi_ulong psr;
1836
                abi_ulong pc;
1837
                abi_ulong npc;
1838
                abi_ulong y;
1839
                abi_ulong u_regs[16]; /* globals and ins */
1840
        }               si_regs;
1841
        int             si_mask;
1842
} __siginfo_t;
1843

    
1844
typedef struct {
1845
        unsigned   long si_float_regs [32];
1846
        unsigned   long si_fsr;
1847
        unsigned   long si_fpqdepth;
1848
        struct {
1849
                unsigned long *insn_addr;
1850
                unsigned long insn;
1851
        } si_fpqueue [16];
1852
} qemu_siginfo_fpu_t;
1853

    
1854

    
1855
struct target_signal_frame {
1856
        struct sparc_stackf        ss;
1857
        __siginfo_t                info;
1858
        abi_ulong               fpu_save;
1859
        abi_ulong                insns[2] __attribute__ ((aligned (8)));
1860
        abi_ulong                extramask[TARGET_NSIG_WORDS - 1];
1861
        abi_ulong                extra_size; /* Should be 0 */
1862
        qemu_siginfo_fpu_t        fpu_state;
1863
};
1864
struct target_rt_signal_frame {
1865
        struct sparc_stackf        ss;
1866
        siginfo_t                info;
1867
        abi_ulong                regs[20];
1868
        sigset_t                mask;
1869
        abi_ulong               fpu_save;
1870
        unsigned int                insns[2];
1871
        stack_t                        stack;
1872
        unsigned int                extra_size; /* Should be 0 */
1873
        qemu_siginfo_fpu_t        fpu_state;
1874
};
1875

    
1876
#define UREG_O0        16
1877
#define UREG_O6        22
1878
#define UREG_I0        0
1879
#define UREG_I1        1
1880
#define UREG_I2        2
1881
#define UREG_I3        3
1882
#define UREG_I4        4
1883
#define UREG_I5        5
1884
#define UREG_I6        6
1885
#define UREG_I7        7
1886
#define UREG_L0               8
1887
#define UREG_FP        UREG_I6
1888
#define UREG_SP        UREG_O6
1889

    
1890
static inline abi_ulong get_sigframe(struct target_sigaction *sa, 
1891
                                     CPUState *env, unsigned long framesize)
1892
{
1893
        abi_ulong sp;
1894

    
1895
        sp = env->regwptr[UREG_FP];
1896

    
1897
        /* This is the X/Open sanctioned signal stack switching.  */
1898
        if (sa->sa_flags & TARGET_SA_ONSTACK) {
1899
            if (!on_sig_stack(sp)
1900
                && !((target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size) & 7))
1901
                sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
1902
        }
1903
        return sp - framesize;
1904
}
1905

    
1906
static int
1907
setup___siginfo(__siginfo_t *si, CPUState *env, abi_ulong mask)
1908
{
1909
        int err = 0, i;
1910

    
1911
        err |= __put_user(env->psr, &si->si_regs.psr);
1912
        err |= __put_user(env->pc, &si->si_regs.pc);
1913
        err |= __put_user(env->npc, &si->si_regs.npc);
1914
        err |= __put_user(env->y, &si->si_regs.y);
1915
        for (i=0; i < 8; i++) {
1916
                err |= __put_user(env->gregs[i], &si->si_regs.u_regs[i]);
1917
        }
1918
        for (i=0; i < 8; i++) {
1919
                err |= __put_user(env->regwptr[UREG_I0 + i], &si->si_regs.u_regs[i+8]);
1920
        }
1921
        err |= __put_user(mask, &si->si_mask);
1922
        return err;
1923
}
1924

    
1925
#if 0
1926
static int
1927
setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
1928
                 CPUState *env, unsigned long mask)
1929
{
1930
        int err = 0;
1931

1932
        err |= __put_user(mask, &sc->sigc_mask);
1933
        err |= __put_user(env->regwptr[UREG_SP], &sc->sigc_sp);
1934
        err |= __put_user(env->pc, &sc->sigc_pc);
1935
        err |= __put_user(env->npc, &sc->sigc_npc);
1936
        err |= __put_user(env->psr, &sc->sigc_psr);
1937
        err |= __put_user(env->gregs[1], &sc->sigc_g1);
1938
        err |= __put_user(env->regwptr[UREG_O0], &sc->sigc_o0);
1939

1940
        return err;
1941
}
1942
#endif
1943
#define NF_ALIGNEDSZ  (((sizeof(struct target_signal_frame) + 7) & (~7)))
1944

    
1945
static void setup_frame(int sig, struct target_sigaction *ka,
1946
                        target_sigset_t *set, CPUState *env)
1947
{
1948
        abi_ulong sf_addr;
1949
        struct target_signal_frame *sf;
1950
        int sigframe_size, err, i;
1951

    
1952
        /* 1. Make sure everything is clean */
1953
        //synchronize_user_stack();
1954

    
1955
        sigframe_size = NF_ALIGNEDSZ;
1956
        sf_addr = get_sigframe(ka, env, sigframe_size);
1957

    
1958
        sf = lock_user(VERIFY_WRITE, sf_addr, 
1959
                       sizeof(struct target_signal_frame), 0);
1960
        if (!sf)
1961
                goto sigsegv;
1962
                
1963
        //fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
1964
#if 0
1965
        if (invalid_frame_pointer(sf, sigframe_size))
1966
                goto sigill_and_return;
1967
#endif
1968
        /* 2. Save the current process state */
1969
        err = setup___siginfo(&sf->info, env, set->sig[0]);
1970
        err |= __put_user(0, &sf->extra_size);
1971

    
1972
        //err |= save_fpu_state(regs, &sf->fpu_state);
1973
        //err |= __put_user(&sf->fpu_state, &sf->fpu_save);
1974

    
1975
        err |= __put_user(set->sig[0], &sf->info.si_mask);
1976
        for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
1977
                err |= __put_user(set->sig[i + 1], &sf->extramask[i]);
1978
        }
1979

    
1980
        for (i = 0; i < 8; i++) {
1981
                  err |= __put_user(env->regwptr[i + UREG_L0], &sf->ss.locals[i]);
1982
        }
1983
        for (i = 0; i < 8; i++) {
1984
                  err |= __put_user(env->regwptr[i + UREG_I0], &sf->ss.ins[i]);
1985
        }
1986
        if (err)
1987
                goto sigsegv;
1988

    
1989
        /* 3. signal handler back-trampoline and parameters */
1990
        env->regwptr[UREG_FP] = sf_addr;
1991
        env->regwptr[UREG_I0] = sig;
1992
        env->regwptr[UREG_I1] = sf_addr + 
1993
                offsetof(struct target_signal_frame, info);
1994
        env->regwptr[UREG_I2] = sf_addr + 
1995
                offsetof(struct target_signal_frame, info);
1996

    
1997
        /* 4. signal handler */
1998
        env->pc = ka->_sa_handler;
1999
        env->npc = (env->pc + 4);
2000
        /* 5. return to kernel instructions */
2001
        if (ka->sa_restorer)
2002
                env->regwptr[UREG_I7] = ka->sa_restorer;
2003
        else {
2004
                uint32_t val32;
2005

    
2006
                env->regwptr[UREG_I7] = sf_addr + 
2007
                        offsetof(struct target_signal_frame, insns) - 2 * 4;
2008

    
2009
                /* mov __NR_sigreturn, %g1 */
2010
                val32 = 0x821020d8;
2011
                err |= __put_user(val32, &sf->insns[0]);
2012

    
2013
                /* t 0x10 */
2014
                val32 = 0x91d02010;
2015
                err |= __put_user(val32, &sf->insns[1]);
2016
                if (err)
2017
                        goto sigsegv;
2018

    
2019
                /* Flush instruction space. */
2020
                //flush_sig_insns(current->mm, (unsigned long) &(sf->insns[0]));
2021
                //                tb_flush(env);
2022
        }
2023
        unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2024
        return;
2025
#if 0
2026
sigill_and_return:
2027
        force_sig(TARGET_SIGILL);
2028
#endif
2029
sigsegv:
2030
        //fprintf(stderr, "force_sig\n");
2031
        unlock_user(sf, sf_addr, sizeof(struct target_signal_frame));
2032
        force_sig(TARGET_SIGSEGV);
2033
}
2034
static inline int
2035
restore_fpu_state(CPUState *env, qemu_siginfo_fpu_t *fpu)
2036
{
2037
        int err;
2038
#if 0
2039
#ifdef CONFIG_SMP
2040
        if (current->flags & PF_USEDFPU)
2041
                regs->psr &= ~PSR_EF;
2042
#else
2043
        if (current == last_task_used_math) {
2044
                last_task_used_math = 0;
2045
                regs->psr &= ~PSR_EF;
2046
        }
2047
#endif
2048
        current->used_math = 1;
2049
        current->flags &= ~PF_USEDFPU;
2050
#endif
2051
#if 0
2052
        if (verify_area (VERIFY_READ, fpu, sizeof(*fpu)))
2053
                return -EFAULT;
2054
#endif
2055

    
2056
#if 0
2057
        /* XXX: incorrect */
2058
        err = __copy_from_user(&env->fpr[0], &fpu->si_float_regs[0],
2059
                                     (sizeof(unsigned long) * 32));
2060
#endif
2061
        err |= __get_user(env->fsr, &fpu->si_fsr);
2062
#if 0
2063
        err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
2064
        if (current->thread.fpqdepth != 0)
2065
                err |= __copy_from_user(&current->thread.fpqueue[0],
2066
                                        &fpu->si_fpqueue[0],
2067
                                        ((sizeof(unsigned long) +
2068
                                        (sizeof(unsigned long *)))*16));
2069
#endif
2070
        return err;
2071
}
2072

    
2073

    
2074
static void setup_rt_frame(int sig, struct target_sigaction *ka,
2075
                           target_siginfo_t *info,
2076
                           target_sigset_t *set, CPUState *env)
2077
{
2078
    fprintf(stderr, "setup_rt_frame: not implemented\n");
2079
}
2080

    
2081
long do_sigreturn(CPUState *env)
2082
{
2083
        abi_ulong sf_addr;
2084
        struct target_signal_frame *sf;
2085
        uint32_t up_psr, pc, npc;
2086
        target_sigset_t set;
2087
        sigset_t host_set;
2088
        int err, i;
2089

    
2090
        sf_addr = env->regwptr[UREG_FP];
2091
        if (!lock_user_struct(VERIFY_READ, sf, sf_addr, 1))
2092
                goto segv_and_exit;
2093
#if 0
2094
        fprintf(stderr, "sigreturn\n");
2095
        fprintf(stderr, "sf: %x pc %x fp %x sp %x\n", sf, env->pc, env->regwptr[UREG_FP], env->regwptr[UREG_SP]);
2096
#endif
2097
        //cpu_dump_state(env, stderr, fprintf, 0);
2098

    
2099
        /* 1. Make sure we are not getting garbage from the user */
2100

    
2101
        if (sf_addr & 3)
2102
                goto segv_and_exit;
2103

    
2104
        err = __get_user(pc,  &sf->info.si_regs.pc);
2105
        err |= __get_user(npc, &sf->info.si_regs.npc);
2106

    
2107
        if ((pc | npc) & 3)
2108
                goto segv_and_exit;
2109

    
2110
        /* 2. Restore the state */
2111
        err |= __get_user(up_psr, &sf->info.si_regs.psr);
2112

    
2113
        /* User can only change condition codes and FPU enabling in %psr. */
2114
        env->psr = (up_psr & (PSR_ICC /* | PSR_EF */))
2115
                  | (env->psr & ~(PSR_ICC /* | PSR_EF */));
2116

    
2117
        env->pc = pc;
2118
        env->npc = npc;
2119
        err |= __get_user(env->y, &sf->info.si_regs.y);
2120
        for (i=0; i < 8; i++) {
2121
                err |= __get_user(env->gregs[i], &sf->info.si_regs.u_regs[i]);
2122
        }
2123
        for (i=0; i < 8; i++) {
2124
                err |= __get_user(env->regwptr[i + UREG_I0], &sf->info.si_regs.u_regs[i+8]);
2125
        }
2126

    
2127
        /* FIXME: implement FPU save/restore:
2128
         * __get_user(fpu_save, &sf->fpu_save);
2129
         * if (fpu_save)
2130
         *        err |= restore_fpu_state(env, fpu_save);
2131
         */
2132

    
2133
        /* This is pretty much atomic, no amount locking would prevent
2134
         * the races which exist anyways.
2135
         */
2136
        err |= __get_user(set.sig[0], &sf->info.si_mask);
2137
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
2138
            err |= (__get_user(set.sig[i], &sf->extramask[i - 1]));
2139
        }
2140

    
2141
        target_to_host_sigset_internal(&host_set, &set);
2142
        sigprocmask(SIG_SETMASK, &host_set, NULL);
2143

    
2144
        if (err)
2145
                goto segv_and_exit;
2146
        unlock_user_struct(sf, sf_addr, 0);
2147
        return env->regwptr[0];
2148

    
2149
segv_and_exit:
2150
        unlock_user_struct(sf, sf_addr, 0);
2151
        force_sig(TARGET_SIGSEGV);
2152
}
2153

    
2154
long do_rt_sigreturn(CPUState *env)
2155
{
2156
    fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2157
    return -TARGET_ENOSYS;
2158
}
2159

    
2160
#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
2161
#define MC_TSTATE 0
2162
#define MC_PC 1
2163
#define MC_NPC 2
2164
#define MC_Y 3
2165
#define MC_G1 4
2166
#define MC_G2 5
2167
#define MC_G3 6
2168
#define MC_G4 7
2169
#define MC_G5 8
2170
#define MC_G6 9
2171
#define MC_G7 10
2172
#define MC_O0 11
2173
#define MC_O1 12
2174
#define MC_O2 13
2175
#define MC_O3 14
2176
#define MC_O4 15
2177
#define MC_O5 16
2178
#define MC_O6 17
2179
#define MC_O7 18
2180
#define MC_NGREG 19
2181

    
2182
typedef abi_ulong target_mc_greg_t;
2183
typedef target_mc_greg_t target_mc_gregset_t[MC_NGREG];
2184

    
2185
struct target_mc_fq {
2186
    abi_ulong *mcfq_addr;
2187
    uint32_t mcfq_insn;
2188
};
2189

    
2190
struct target_mc_fpu {
2191
    union {
2192
        uint32_t sregs[32];
2193
        uint64_t dregs[32];
2194
        //uint128_t qregs[16];
2195
    } mcfpu_fregs;
2196
    abi_ulong mcfpu_fsr;
2197
    abi_ulong mcfpu_fprs;
2198
    abi_ulong mcfpu_gsr;
2199
    struct target_mc_fq *mcfpu_fq;
2200
    unsigned char mcfpu_qcnt;
2201
    unsigned char mcfpu_qentsz;
2202
    unsigned char mcfpu_enab;
2203
};
2204
typedef struct target_mc_fpu target_mc_fpu_t;
2205

    
2206
typedef struct {
2207
    target_mc_gregset_t mc_gregs;
2208
    target_mc_greg_t mc_fp;
2209
    target_mc_greg_t mc_i7;
2210
    target_mc_fpu_t mc_fpregs;
2211
} target_mcontext_t;
2212

    
2213
struct target_ucontext {
2214
    struct target_ucontext *tuc_link;
2215
    abi_ulong tuc_flags;
2216
    target_sigset_t tuc_sigmask;
2217
    target_mcontext_t tuc_mcontext;
2218
};
2219

    
2220
/* A V9 register window */
2221
struct target_reg_window {
2222
    abi_ulong locals[8];
2223
    abi_ulong ins[8];
2224
};
2225

    
2226
#define TARGET_STACK_BIAS 2047
2227

    
2228
/* {set, get}context() needed for 64-bit SparcLinux userland. */
2229
void sparc64_set_context(CPUSPARCState *env)
2230
{
2231
    abi_ulong ucp_addr;
2232
    struct target_ucontext *ucp;
2233
    target_mc_gregset_t *grp;
2234
    abi_ulong pc, npc, tstate;
2235
    abi_ulong fp, i7, w_addr;
2236
    int err;
2237
    unsigned int i;
2238

    
2239
    ucp_addr = env->regwptr[UREG_I0];
2240
    if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1))
2241
        goto do_sigsegv;
2242
    grp  = &ucp->tuc_mcontext.mc_gregs;
2243
    err  = __get_user(pc, &((*grp)[MC_PC]));
2244
    err |= __get_user(npc, &((*grp)[MC_NPC]));
2245
    if (err || ((pc | npc) & 3))
2246
        goto do_sigsegv;
2247
    if (env->regwptr[UREG_I1]) {
2248
        target_sigset_t target_set;
2249
        sigset_t set;
2250

    
2251
        if (TARGET_NSIG_WORDS == 1) {
2252
            if (__get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]))
2253
                goto do_sigsegv;
2254
        } else {
2255
            abi_ulong *src, *dst;
2256
            src = ucp->tuc_sigmask.sig;
2257
            dst = target_set.sig;
2258
            for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2259
                 i++, dst++, src++)
2260
                err |= __get_user(*dst, src);
2261
            if (err)
2262
                goto do_sigsegv;
2263
        }
2264
        target_to_host_sigset_internal(&set, &target_set);
2265
        sigprocmask(SIG_SETMASK, &set, NULL);
2266
    }
2267
    env->pc = pc;
2268
    env->npc = npc;
2269
    err |= __get_user(env->y, &((*grp)[MC_Y]));
2270
    err |= __get_user(tstate, &((*grp)[MC_TSTATE]));
2271
    env->asi = (tstate >> 24) & 0xff;
2272
    cpu_put_ccr(env, tstate >> 32);
2273
    cpu_put_cwp64(env, tstate & 0x1f);
2274
    err |= __get_user(env->gregs[1], (&(*grp)[MC_G1]));
2275
    err |= __get_user(env->gregs[2], (&(*grp)[MC_G2]));
2276
    err |= __get_user(env->gregs[3], (&(*grp)[MC_G3]));
2277
    err |= __get_user(env->gregs[4], (&(*grp)[MC_G4]));
2278
    err |= __get_user(env->gregs[5], (&(*grp)[MC_G5]));
2279
    err |= __get_user(env->gregs[6], (&(*grp)[MC_G6]));
2280
    err |= __get_user(env->gregs[7], (&(*grp)[MC_G7]));
2281
    err |= __get_user(env->regwptr[UREG_I0], (&(*grp)[MC_O0]));
2282
    err |= __get_user(env->regwptr[UREG_I1], (&(*grp)[MC_O1]));
2283
    err |= __get_user(env->regwptr[UREG_I2], (&(*grp)[MC_O2]));
2284
    err |= __get_user(env->regwptr[UREG_I3], (&(*grp)[MC_O3]));
2285
    err |= __get_user(env->regwptr[UREG_I4], (&(*grp)[MC_O4]));
2286
    err |= __get_user(env->regwptr[UREG_I5], (&(*grp)[MC_O5]));
2287
    err |= __get_user(env->regwptr[UREG_I6], (&(*grp)[MC_O6]));
2288
    err |= __get_user(env->regwptr[UREG_I7], (&(*grp)[MC_O7]));
2289

    
2290
    err |= __get_user(fp, &(ucp->tuc_mcontext.mc_fp));
2291
    err |= __get_user(i7, &(ucp->tuc_mcontext.mc_i7));
2292

    
2293
    w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2294
    if (put_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]), 
2295
                 abi_ulong) != 0)
2296
        goto do_sigsegv;
2297
    if (put_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]), 
2298
                 abi_ulong) != 0)
2299
        goto do_sigsegv;
2300
    /* FIXME this does not match how the kernel handles the FPU in
2301
     * its sparc64_set_context implementation. In particular the FPU
2302
     * is only restored if fenab is non-zero in:
2303
     *   __get_user(fenab, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_enab));
2304
     */
2305
    err |= __get_user(env->fprs, &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fprs));
2306
    {
2307
        uint32_t *src = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2308
        for (i = 0; i < 64; i++, src++) {
2309
            if (i & 1) {
2310
                err |= __get_user(env->fpr[i/2].l.lower, src);
2311
            } else {
2312
                err |= __get_user(env->fpr[i/2].l.upper, src);
2313
            }
2314
        }
2315
    }
2316
    err |= __get_user(env->fsr,
2317
                      &(ucp->tuc_mcontext.mc_fpregs.mcfpu_fsr));
2318
    err |= __get_user(env->gsr,
2319
                      &(ucp->tuc_mcontext.mc_fpregs.mcfpu_gsr));
2320
    if (err)
2321
        goto do_sigsegv;
2322
    unlock_user_struct(ucp, ucp_addr, 0);
2323
    return;
2324
 do_sigsegv:
2325
    unlock_user_struct(ucp, ucp_addr, 0);
2326
    force_sig(TARGET_SIGSEGV);
2327
}
2328

    
2329
void sparc64_get_context(CPUSPARCState *env)
2330
{
2331
    abi_ulong ucp_addr;
2332
    struct target_ucontext *ucp;
2333
    target_mc_gregset_t *grp;
2334
    target_mcontext_t *mcp;
2335
    abi_ulong fp, i7, w_addr;
2336
    int err;
2337
    unsigned int i;
2338
    target_sigset_t target_set;
2339
    sigset_t set;
2340

    
2341
    ucp_addr = env->regwptr[UREG_I0];
2342
    if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0))
2343
        goto do_sigsegv;
2344
    
2345
    mcp = &ucp->tuc_mcontext;
2346
    grp = &mcp->mc_gregs;
2347

    
2348
    /* Skip over the trap instruction, first. */
2349
    env->pc = env->npc;
2350
    env->npc += 4;
2351

    
2352
    err = 0;
2353

    
2354
    sigprocmask(0, NULL, &set);
2355
    host_to_target_sigset_internal(&target_set, &set);
2356
    if (TARGET_NSIG_WORDS == 1) {
2357
        err |= __put_user(target_set.sig[0],
2358
                          (abi_ulong *)&ucp->tuc_sigmask);
2359
    } else {
2360
        abi_ulong *src, *dst;
2361
        src = target_set.sig;
2362
        dst = ucp->tuc_sigmask.sig;
2363
        for (i = 0; i < sizeof(target_sigset_t) / sizeof(abi_ulong);
2364
             i++, dst++, src++)
2365
            err |= __put_user(*src, dst);
2366
        if (err)
2367
            goto do_sigsegv;
2368
    }
2369

    
2370
    /* XXX: tstate must be saved properly */
2371
    //    err |= __put_user(env->tstate, &((*grp)[MC_TSTATE]));
2372
    err |= __put_user(env->pc, &((*grp)[MC_PC]));
2373
    err |= __put_user(env->npc, &((*grp)[MC_NPC]));
2374
    err |= __put_user(env->y, &((*grp)[MC_Y]));
2375
    err |= __put_user(env->gregs[1], &((*grp)[MC_G1]));
2376
    err |= __put_user(env->gregs[2], &((*grp)[MC_G2]));
2377
    err |= __put_user(env->gregs[3], &((*grp)[MC_G3]));
2378
    err |= __put_user(env->gregs[4], &((*grp)[MC_G4]));
2379
    err |= __put_user(env->gregs[5], &((*grp)[MC_G5]));
2380
    err |= __put_user(env->gregs[6], &((*grp)[MC_G6]));
2381
    err |= __put_user(env->gregs[7], &((*grp)[MC_G7]));
2382
    err |= __put_user(env->regwptr[UREG_I0], &((*grp)[MC_O0]));
2383
    err |= __put_user(env->regwptr[UREG_I1], &((*grp)[MC_O1]));
2384
    err |= __put_user(env->regwptr[UREG_I2], &((*grp)[MC_O2]));
2385
    err |= __put_user(env->regwptr[UREG_I3], &((*grp)[MC_O3]));
2386
    err |= __put_user(env->regwptr[UREG_I4], &((*grp)[MC_O4]));
2387
    err |= __put_user(env->regwptr[UREG_I5], &((*grp)[MC_O5]));
2388
    err |= __put_user(env->regwptr[UREG_I6], &((*grp)[MC_O6]));
2389
    err |= __put_user(env->regwptr[UREG_I7], &((*grp)[MC_O7]));
2390

    
2391
    w_addr = TARGET_STACK_BIAS+env->regwptr[UREG_I6];
2392
    fp = i7 = 0;
2393
    if (get_user(fp, w_addr + offsetof(struct target_reg_window, ins[6]), 
2394
                 abi_ulong) != 0)
2395
        goto do_sigsegv;
2396
    if (get_user(i7, w_addr + offsetof(struct target_reg_window, ins[7]), 
2397
                 abi_ulong) != 0)
2398
        goto do_sigsegv;
2399
    err |= __put_user(fp, &(mcp->mc_fp));
2400
    err |= __put_user(i7, &(mcp->mc_i7));
2401

    
2402
    {
2403
        uint32_t *dst = ucp->tuc_mcontext.mc_fpregs.mcfpu_fregs.sregs;
2404
        for (i = 0; i < 64; i++, dst++) {
2405
            if (i & 1) {
2406
                err |= __put_user(env->fpr[i/2].l.lower, dst);
2407
            } else {
2408
                err |= __put_user(env->fpr[i/2].l.upper, dst);
2409
            }
2410
        }
2411
    }
2412
    err |= __put_user(env->fsr, &(mcp->mc_fpregs.mcfpu_fsr));
2413
    err |= __put_user(env->gsr, &(mcp->mc_fpregs.mcfpu_gsr));
2414
    err |= __put_user(env->fprs, &(mcp->mc_fpregs.mcfpu_fprs));
2415

    
2416
    if (err)
2417
        goto do_sigsegv;
2418
    unlock_user_struct(ucp, ucp_addr, 1);
2419
    return;
2420
 do_sigsegv:
2421
    unlock_user_struct(ucp, ucp_addr, 1);
2422
    force_sig(TARGET_SIGSEGV);
2423
}
2424
#endif
2425
#elif defined(TARGET_ABI_MIPSN64)
2426

    
2427
# warning signal handling not implemented
2428

    
2429
static void setup_frame(int sig, struct target_sigaction *ka,
2430
                        target_sigset_t *set, CPUState *env)
2431
{
2432
    fprintf(stderr, "setup_frame: not implemented\n");
2433
}
2434

    
2435
static void setup_rt_frame(int sig, struct target_sigaction *ka,
2436
                           target_siginfo_t *info,
2437
                           target_sigset_t *set, CPUState *env)
2438
{
2439
    fprintf(stderr, "setup_rt_frame: not implemented\n");
2440
}
2441

    
2442
long do_sigreturn(CPUState *env)
2443
{
2444
    fprintf(stderr, "do_sigreturn: not implemented\n");
2445
    return -TARGET_ENOSYS;
2446
}
2447

    
2448
long do_rt_sigreturn(CPUState *env)
2449
{
2450
    fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2451
    return -TARGET_ENOSYS;
2452
}
2453

    
2454
#elif defined(TARGET_ABI_MIPSN32)
2455

    
2456
# warning signal handling not implemented
2457

    
2458
static void setup_frame(int sig, struct target_sigaction *ka,
2459
                        target_sigset_t *set, CPUState *env)
2460
{
2461
    fprintf(stderr, "setup_frame: not implemented\n");
2462
}
2463

    
2464
static void setup_rt_frame(int sig, struct target_sigaction *ka,
2465
                           target_siginfo_t *info,
2466
                           target_sigset_t *set, CPUState *env)
2467
{
2468
    fprintf(stderr, "setup_rt_frame: not implemented\n");
2469
}
2470

    
2471
long do_sigreturn(CPUState *env)
2472
{
2473
    fprintf(stderr, "do_sigreturn: not implemented\n");
2474
    return -TARGET_ENOSYS;
2475
}
2476

    
2477
long do_rt_sigreturn(CPUState *env)
2478
{
2479
    fprintf(stderr, "do_rt_sigreturn: not implemented\n");
2480
    return -TARGET_ENOSYS;
2481
}
2482

    
2483
#elif defined(TARGET_ABI_MIPSO32)
2484

    
2485
struct target_sigcontext {
2486
    uint32_t   sc_regmask;     /* Unused */
2487
    uint32_t   sc_status;
2488
    uint64_t   sc_pc;
2489
    uint64_t   sc_regs[32];
2490
    uint64_t   sc_fpregs[32];
2491
    uint32_t   sc_ownedfp;     /* Unused */
2492
    uint32_t   sc_fpc_csr;
2493
    uint32_t   sc_fpc_eir;     /* Unused */
2494
    uint32_t   sc_used_math;
2495
    uint32_t   sc_dsp;         /* dsp status, was sc_ssflags */
2496
    uint32_t   pad0;
2497
    uint64_t   sc_mdhi;
2498
    uint64_t   sc_mdlo;
2499
    target_ulong   sc_hi1;         /* Was sc_cause */
2500
    target_ulong   sc_lo1;         /* Was sc_badvaddr */
2501
    target_ulong   sc_hi2;         /* Was sc_sigset[4] */
2502
    target_ulong   sc_lo2;
2503
    target_ulong   sc_hi3;
2504
    target_ulong   sc_lo3;
2505
};
2506

    
2507
struct sigframe {
2508
    uint32_t sf_ass[4];                        /* argument save space for o32 */
2509
    uint32_t sf_code[2];                        /* signal trampoline */
2510
    struct target_sigcontext sf_sc;
2511
    target_sigset_t sf_mask;
2512
};
2513

    
2514
struct target_ucontext {
2515
    target_ulong tuc_flags;
2516
    target_ulong tuc_link;
2517
    target_stack_t tuc_stack;
2518
    target_ulong pad0;
2519
    struct target_sigcontext tuc_mcontext;
2520
    target_sigset_t tuc_sigmask;
2521
};
2522

    
2523
struct target_rt_sigframe {
2524
    uint32_t rs_ass[4];               /* argument save space for o32 */
2525
    uint32_t rs_code[2];              /* signal trampoline */
2526
    struct target_siginfo rs_info;
2527
    struct target_ucontext rs_uc;
2528
};
2529

    
2530
/* Install trampoline to jump back from signal handler */
2531
static inline int install_sigtramp(unsigned int *tramp,   unsigned int syscall)
2532
{
2533
    int err;
2534

    
2535
    /*
2536
    * Set up the return code ...
2537
    *
2538
    *         li      v0, __NR__foo_sigreturn
2539
    *         syscall
2540
    */
2541

    
2542
    err = __put_user(0x24020000 + syscall, tramp + 0);
2543
    err |= __put_user(0x0000000c          , tramp + 1);
2544
    /* flush_cache_sigtramp((unsigned long) tramp); */
2545
    return err;
2546
}
2547

    
2548
static inline int
2549
setup_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2550
{
2551
    int err = 0;
2552

    
2553
    err |= __put_user(regs->active_tc.PC, &sc->sc_pc);
2554

    
2555
#define save_gp_reg(i) do {                                                   \
2556
        err |= __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);        \
2557
    } while(0)
2558
    __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
2559
    save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
2560
    save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
2561
    save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
2562
    save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
2563
    save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
2564
    save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
2565
    save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
2566
    save_gp_reg(31);
2567
#undef save_gp_reg
2568

    
2569
    err |= __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2570
    err |= __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2571

    
2572
    /* Not used yet, but might be useful if we ever have DSP suppport */
2573
#if 0
2574
    if (cpu_has_dsp) {
2575
        err |= __put_user(mfhi1(), &sc->sc_hi1);
2576
        err |= __put_user(mflo1(), &sc->sc_lo1);
2577
        err |= __put_user(mfhi2(), &sc->sc_hi2);
2578
        err |= __put_user(mflo2(), &sc->sc_lo2);
2579
        err |= __put_user(mfhi3(), &sc->sc_hi3);
2580
        err |= __put_user(mflo3(), &sc->sc_lo3);
2581
        err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2582
    }
2583
    /* same with 64 bit */
2584
#ifdef CONFIG_64BIT
2585
    err |= __put_user(regs->hi, &sc->sc_hi[0]);
2586
    err |= __put_user(regs->lo, &sc->sc_lo[0]);
2587
    if (cpu_has_dsp) {
2588
        err |= __put_user(mfhi1(), &sc->sc_hi[1]);
2589
        err |= __put_user(mflo1(), &sc->sc_lo[1]);
2590
        err |= __put_user(mfhi2(), &sc->sc_hi[2]);
2591
        err |= __put_user(mflo2(), &sc->sc_lo[2]);
2592
        err |= __put_user(mfhi3(), &sc->sc_hi[3]);
2593
        err |= __put_user(mflo3(), &sc->sc_lo[3]);
2594
        err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
2595
    }
2596
#endif
2597
#endif
2598

    
2599
#if 0
2600
    err |= __put_user(!!used_math(), &sc->sc_used_math);
2601

2602
    if (!used_math())
2603
        goto out;
2604

2605
    /*
2606
    * Save FPU state to signal context.  Signal handler will "inherit"
2607
    * current FPU state.
2608
    */
2609
    preempt_disable();
2610

2611
    if (!is_fpu_owner()) {
2612
        own_fpu();
2613
        restore_fp(current);
2614
    }
2615
    err |= save_fp_context(sc);
2616

2617
    preempt_enable();
2618
    out:
2619
#endif
2620
    return err;
2621
}
2622

    
2623
static inline int
2624
restore_sigcontext(CPUState *regs, struct target_sigcontext *sc)
2625
{
2626
    int err = 0;
2627

    
2628
    err |= __get_user(regs->CP0_EPC, &sc->sc_pc);
2629

    
2630
    err |= __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
2631
    err |= __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
2632

    
2633
#define restore_gp_reg(i) do {                                                           \
2634
        err |= __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);                \
2635
    } while(0)
2636
    restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
2637
    restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
2638
    restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
2639
    restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
2640
    restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
2641
    restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
2642
    restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
2643
    restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
2644
    restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
2645
    restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
2646
    restore_gp_reg(31);
2647
#undef restore_gp_reg
2648

    
2649
#if 0
2650
    if (cpu_has_dsp) {
2651
        err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
2652
        err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
2653
        err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
2654
        err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
2655
        err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
2656
        err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
2657
        err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2658
    }
2659
#ifdef CONFIG_64BIT
2660
    err |= __get_user(regs->hi, &sc->sc_hi[0]);
2661
    err |= __get_user(regs->lo, &sc->sc_lo[0]);
2662
    if (cpu_has_dsp) {
2663
        err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
2664
        err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
2665
        err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
2666
        err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
2667
        err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
2668
        err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
2669
        err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
2670
    }
2671
#endif
2672

    
2673
    err |= __get_user(used_math, &sc->sc_used_math);
2674
    conditional_used_math(used_math);
2675

    
2676
    preempt_disable();
2677

    
2678
    if (used_math()) {
2679
        /* restore fpu context if we have used it before */
2680
        own_fpu();
2681
        err |= restore_fp_context(sc);
2682
    } else {
2683
        /* signal handler may have used FPU.  Give it up. */
2684
        lose_fpu();
2685
    }
2686

    
2687
    preempt_enable();
2688
#endif
2689
    return err;
2690
}
2691
/*
2692
 * Determine which stack to use..
2693
 */
2694
static inline abi_ulong
2695
get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
2696
{
2697
    unsigned long sp;
2698

    
2699
    /* Default to using normal stack */
2700
    sp = regs->active_tc.gpr[29];
2701

    
2702
    /*
2703
     * FPU emulator may have its own trampoline active just
2704
     * above the user stack, 16-bytes before the next lowest
2705
     * 16 byte boundary.  Try to avoid trashing it.
2706
     */
2707
    sp -= 32;
2708

    
2709
    /* This is the X/Open sanctioned signal stack switching.  */
2710
    if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
2711
        sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2712
    }
2713

    
2714
    return (sp - frame_size) & ~7;
2715
}
2716

    
2717
/* compare linux/arch/mips/kernel/signal.c:setup_frame() */
2718
static void setup_frame(int sig, struct target_sigaction * ka,
2719
                        target_sigset_t *set, CPUState *regs)
2720
{
2721
    struct sigframe *frame;
2722
    abi_ulong frame_addr;
2723
    int i;
2724

    
2725
    frame_addr = get_sigframe(ka, regs, sizeof(*frame));
2726
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2727
        goto give_sigsegv;
2728

    
2729
    install_sigtramp(frame->sf_code, TARGET_NR_sigreturn);
2730

    
2731
    if(setup_sigcontext(regs, &frame->sf_sc))
2732
        goto give_sigsegv;
2733

    
2734
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2735
        if(__put_user(set->sig[i], &frame->sf_mask.sig[i]))
2736
            goto give_sigsegv;
2737
    }
2738

    
2739
    /*
2740
    * Arguments to signal handler:
2741
    *
2742
    *   a0 = signal number
2743
    *   a1 = 0 (should be cause)
2744
    *   a2 = pointer to struct sigcontext
2745
    *
2746
    * $25 and PC point to the signal handler, $29 points to the
2747
    * struct sigframe.
2748
    */
2749
    regs->active_tc.gpr[ 4] = sig;
2750
    regs->active_tc.gpr[ 5] = 0;
2751
    regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
2752
    regs->active_tc.gpr[29] = frame_addr;
2753
    regs->active_tc.gpr[31] = frame_addr + offsetof(struct sigframe, sf_code);
2754
    /* The original kernel code sets CP0_EPC to the handler
2755
    * since it returns to userland using eret
2756
    * we cannot do this here, and we must set PC directly */
2757
    regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
2758
    unlock_user_struct(frame, frame_addr, 1);
2759
    return;
2760

    
2761
give_sigsegv:
2762
    unlock_user_struct(frame, frame_addr, 1);
2763
    force_sig(TARGET_SIGSEGV/*, current*/);
2764
    return;
2765
}
2766

    
2767
long do_sigreturn(CPUState *regs)
2768
{
2769
    struct sigframe *frame;
2770
    abi_ulong frame_addr;
2771
    sigset_t blocked;
2772
    target_sigset_t target_set;
2773
    int i;
2774

    
2775
#if defined(DEBUG_SIGNAL)
2776
    fprintf(stderr, "do_sigreturn\n");
2777
#endif
2778
    frame_addr = regs->active_tc.gpr[29];
2779
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2780
           goto badframe;
2781

    
2782
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2783
           if(__get_user(target_set.sig[i], &frame->sf_mask.sig[i]))
2784
            goto badframe;
2785
    }
2786

    
2787
    target_to_host_sigset_internal(&blocked, &target_set);
2788
    sigprocmask(SIG_SETMASK, &blocked, NULL);
2789

    
2790
    if (restore_sigcontext(regs, &frame->sf_sc))
2791
           goto badframe;
2792

    
2793
#if 0
2794
    /*
2795
     * Don't let your children do this ...
2796
     */
2797
    __asm__ __volatile__(
2798
           "move\t$29, %0\n\t"
2799
           "j\tsyscall_exit"
2800
           :/* no outputs */
2801
           :"r" (&regs));
2802
    /* Unreached */
2803
#endif
2804

    
2805
    regs->active_tc.PC = regs->CP0_EPC;
2806
    /* I am not sure this is right, but it seems to work
2807
    * maybe a problem with nested signals ? */
2808
    regs->CP0_EPC = 0;
2809
    return -TARGET_QEMU_ESIGRETURN;
2810

    
2811
badframe:
2812
    force_sig(TARGET_SIGSEGV/*, current*/);
2813
    return 0;
2814
}
2815

    
2816
static void setup_rt_frame(int sig, struct target_sigaction *ka,
2817
                           target_siginfo_t *info,
2818
                           target_sigset_t *set, CPUState *env)
2819
{
2820
    struct target_rt_sigframe *frame;
2821
    abi_ulong frame_addr;
2822
    int i;
2823

    
2824
    frame_addr = get_sigframe(ka, env, sizeof(*frame));
2825
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
2826
        goto give_sigsegv;
2827

    
2828
    install_sigtramp(frame->rs_code, TARGET_NR_rt_sigreturn);
2829

    
2830
    copy_siginfo_to_user(&frame->rs_info, info);
2831

    
2832
    __put_user(0, &frame->rs_uc.tuc_flags);
2833
    __put_user(0, &frame->rs_uc.tuc_link);
2834
    __put_user(target_sigaltstack_used.ss_sp, &frame->rs_uc.tuc_stack.ss_sp);
2835
    __put_user(target_sigaltstack_used.ss_size, &frame->rs_uc.tuc_stack.ss_size);
2836
    __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
2837
               &frame->rs_uc.tuc_stack.ss_flags);
2838

    
2839
    setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
2840

    
2841
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
2842
        __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
2843
    }
2844

    
2845
    /*
2846
    * Arguments to signal handler:
2847
    *
2848
    *   a0 = signal number
2849
    *   a1 = pointer to struct siginfo
2850
    *   a2 = pointer to struct ucontext
2851
    *
2852
    * $25 and PC point to the signal handler, $29 points to the
2853
    * struct sigframe.
2854
    */
2855
    env->active_tc.gpr[ 4] = sig;
2856
    env->active_tc.gpr[ 5] = frame_addr
2857
                             + offsetof(struct target_rt_sigframe, rs_info);
2858
    env->active_tc.gpr[ 6] = frame_addr
2859
                             + offsetof(struct target_rt_sigframe, rs_uc);
2860
    env->active_tc.gpr[29] = frame_addr;
2861
    env->active_tc.gpr[31] = frame_addr
2862
                             + offsetof(struct target_rt_sigframe, rs_code);
2863
    /* The original kernel code sets CP0_EPC to the handler
2864
    * since it returns to userland using eret
2865
    * we cannot do this here, and we must set PC directly */
2866
    env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
2867
    unlock_user_struct(frame, frame_addr, 1);
2868
    return;
2869

    
2870
give_sigsegv:
2871
    unlock_user_struct(frame, frame_addr, 1);
2872
    force_sig(TARGET_SIGSEGV/*, current*/);
2873
    return;
2874
}
2875

    
2876
long do_rt_sigreturn(CPUState *env)
2877
{
2878
    struct target_rt_sigframe *frame;
2879
    abi_ulong frame_addr;
2880
    sigset_t blocked;
2881

    
2882
#if defined(DEBUG_SIGNAL)
2883
    fprintf(stderr, "do_rt_sigreturn\n");
2884
#endif
2885
    frame_addr = env->active_tc.gpr[29];
2886
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
2887
           goto badframe;
2888

    
2889
    target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
2890
    sigprocmask(SIG_SETMASK, &blocked, NULL);
2891

    
2892
    if (restore_sigcontext(env, &frame->rs_uc.tuc_mcontext))
2893
        goto badframe;
2894

    
2895
    if (do_sigaltstack(frame_addr +
2896
                       offsetof(struct target_rt_sigframe, rs_uc.tuc_stack),
2897
                       0, get_sp_from_cpustate(env)) == -EFAULT)
2898
        goto badframe;
2899

    
2900
    env->active_tc.PC = env->CP0_EPC;
2901
    /* I am not sure this is right, but it seems to work
2902
    * maybe a problem with nested signals ? */
2903
    env->CP0_EPC = 0;
2904
    return -TARGET_QEMU_ESIGRETURN;
2905

    
2906
badframe:
2907
    force_sig(TARGET_SIGSEGV/*, current*/);
2908
    return 0;
2909
}
2910

    
2911
#elif defined(TARGET_SH4)
2912

    
2913
/*
2914
 * code and data structures from linux kernel:
2915
 * include/asm-sh/sigcontext.h
2916
 * arch/sh/kernel/signal.c
2917
 */
2918

    
2919
struct target_sigcontext {
2920
    target_ulong  oldmask;
2921

    
2922
    /* CPU registers */
2923
    target_ulong  sc_gregs[16];
2924
    target_ulong  sc_pc;
2925
    target_ulong  sc_pr;
2926
    target_ulong  sc_sr;
2927
    target_ulong  sc_gbr;
2928
    target_ulong  sc_mach;
2929
    target_ulong  sc_macl;
2930

    
2931
    /* FPU registers */
2932
    target_ulong  sc_fpregs[16];
2933
    target_ulong  sc_xfpregs[16];
2934
    unsigned int sc_fpscr;
2935
    unsigned int sc_fpul;
2936
    unsigned int sc_ownedfp;
2937
};
2938

    
2939
struct target_sigframe
2940
{
2941
    struct target_sigcontext sc;
2942
    target_ulong extramask[TARGET_NSIG_WORDS-1];
2943
    uint16_t retcode[3];
2944
};
2945

    
2946

    
2947
struct target_ucontext {
2948
    target_ulong tuc_flags;
2949
    struct target_ucontext *tuc_link;
2950
    target_stack_t tuc_stack;
2951
    struct target_sigcontext tuc_mcontext;
2952
    target_sigset_t tuc_sigmask;        /* mask last for extensibility */
2953
};
2954

    
2955
struct target_rt_sigframe
2956
{
2957
    struct target_siginfo info;
2958
    struct target_ucontext uc;
2959
    uint16_t retcode[3];
2960
};
2961

    
2962

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

    
2966
static abi_ulong get_sigframe(struct target_sigaction *ka,
2967
                         unsigned long sp, size_t frame_size)
2968
{
2969
    if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags(sp) == 0)) {
2970
        sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
2971
    }
2972

    
2973
    return (sp - frame_size) & -8ul;
2974
}
2975

    
2976
static int setup_sigcontext(struct target_sigcontext *sc,
2977
                            CPUState *regs, unsigned long mask)
2978
{
2979
    int err = 0;
2980
    int i;
2981

    
2982
#define COPY(x)         err |= __put_user(regs->x, &sc->sc_##x)
2983
    COPY(gregs[0]); COPY(gregs[1]);
2984
    COPY(gregs[2]); COPY(gregs[3]);
2985
    COPY(gregs[4]); COPY(gregs[5]);
2986
    COPY(gregs[6]); COPY(gregs[7]);
2987
    COPY(gregs[8]); COPY(gregs[9]);
2988
    COPY(gregs[10]); COPY(gregs[11]);
2989
    COPY(gregs[12]); COPY(gregs[13]);
2990
    COPY(gregs[14]); COPY(gregs[15]);
2991
    COPY(gbr); COPY(mach);
2992
    COPY(macl); COPY(pr);
2993
    COPY(sr); COPY(pc);
2994
#undef COPY
2995

    
2996
    for (i=0; i<16; i++) {
2997
        err |= __put_user(regs->fregs[i], &sc->sc_fpregs[i]);
2998
    }
2999
    err |= __put_user(regs->fpscr, &sc->sc_fpscr);
3000
    err |= __put_user(regs->fpul, &sc->sc_fpul);
3001

    
3002
    /* non-iBCS2 extensions.. */
3003
    err |= __put_user(mask, &sc->oldmask);
3004

    
3005
    return err;
3006
}
3007

    
3008
static int restore_sigcontext(CPUState *regs, struct target_sigcontext *sc,
3009
                              target_ulong *r0_p)
3010
{
3011
    unsigned int err = 0;
3012
    int i;
3013

    
3014
#define COPY(x)         err |= __get_user(regs->x, &sc->sc_##x)
3015
    COPY(gregs[1]);
3016
    COPY(gregs[2]); COPY(gregs[3]);
3017
    COPY(gregs[4]); COPY(gregs[5]);
3018
    COPY(gregs[6]); COPY(gregs[7]);
3019
    COPY(gregs[8]); COPY(gregs[9]);
3020
    COPY(gregs[10]); COPY(gregs[11]);
3021
    COPY(gregs[12]); COPY(gregs[13]);
3022
    COPY(gregs[14]); COPY(gregs[15]);
3023
    COPY(gbr); COPY(mach);
3024
    COPY(macl); COPY(pr);
3025
    COPY(sr); COPY(pc);
3026
#undef COPY
3027

    
3028
    for (i=0; i<16; i++) {
3029
        err |= __get_user(regs->fregs[i], &sc->sc_fpregs[i]);
3030
    }
3031
    err |= __get_user(regs->fpscr, &sc->sc_fpscr);
3032
    err |= __get_user(regs->fpul, &sc->sc_fpul);
3033

    
3034
    regs->tra = -1;         /* disable syscall checks */
3035
    err |= __get_user(*r0_p, &sc->sc_gregs[0]);
3036
    return err;
3037
}
3038

    
3039
static void setup_frame(int sig, struct target_sigaction *ka,
3040
                        target_sigset_t *set, CPUState *regs)
3041
{
3042
    struct target_sigframe *frame;
3043
    abi_ulong frame_addr;
3044
    int i;
3045
    int err = 0;
3046
    int signal;
3047

    
3048
    frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3049
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3050
        goto give_sigsegv;
3051

    
3052
    signal = current_exec_domain_sig(sig);
3053

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

    
3056
    for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
3057
        err |= __put_user(set->sig[i + 1], &frame->extramask[i]);
3058
    }
3059

    
3060
    /* Set up to return from userspace.  If provided, use a stub
3061
       already in userspace.  */
3062
    if (ka->sa_flags & TARGET_SA_RESTORER) {
3063
        regs->pr = (unsigned long) ka->sa_restorer;
3064
    } else {
3065
        /* Generate return code (system call to sigreturn) */
3066
        err |= __put_user(MOVW(2), &frame->retcode[0]);
3067
        err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3068
        err |= __put_user((TARGET_NR_sigreturn), &frame->retcode[2]);
3069
        regs->pr = (unsigned long) frame->retcode;
3070
    }
3071

    
3072
    if (err)
3073
        goto give_sigsegv;
3074

    
3075
    /* Set up registers for signal handler */
3076
    regs->gregs[15] = frame_addr;
3077
    regs->gregs[4] = signal; /* Arg for signal handler */
3078
    regs->gregs[5] = 0;
3079
    regs->gregs[6] = frame_addr += offsetof(typeof(*frame), sc);
3080
    regs->pc = (unsigned long) ka->_sa_handler;
3081

    
3082
    unlock_user_struct(frame, frame_addr, 1);
3083
    return;
3084

    
3085
give_sigsegv:
3086
    unlock_user_struct(frame, frame_addr, 1);
3087
    force_sig(TARGET_SIGSEGV);
3088
}
3089

    
3090
static void setup_rt_frame(int sig, struct target_sigaction *ka,
3091
                           target_siginfo_t *info,
3092
                           target_sigset_t *set, CPUState *regs)
3093
{
3094
    struct target_rt_sigframe *frame;
3095
    abi_ulong frame_addr;
3096
    int i;
3097
    int err = 0;
3098
    int signal;
3099

    
3100
    frame_addr = get_sigframe(ka, regs->gregs[15], sizeof(*frame));
3101
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3102
        goto give_sigsegv;
3103

    
3104
    signal = current_exec_domain_sig(sig);
3105

    
3106
    err |= copy_siginfo_to_user(&frame->info, info);
3107

    
3108
    /* Create the ucontext.  */
3109
    err |= __put_user(0, &frame->uc.tuc_flags);
3110
    err |= __put_user(0, (unsigned long *)&frame->uc.tuc_link);
3111
    err |= __put_user((unsigned long)target_sigaltstack_used.ss_sp,
3112
                      &frame->uc.tuc_stack.ss_sp);
3113
    err |= __put_user(sas_ss_flags(regs->gregs[15]),
3114
                      &frame->uc.tuc_stack.ss_flags);
3115
    err |= __put_user(target_sigaltstack_used.ss_size,
3116
                      &frame->uc.tuc_stack.ss_size);
3117
    err |= setup_sigcontext(&frame->uc.tuc_mcontext,
3118
                            regs, set->sig[0]);
3119
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
3120
        err |= __put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]);
3121
    }
3122

    
3123
    /* Set up to return from userspace.  If provided, use a stub
3124
       already in userspace.  */
3125
    if (ka->sa_flags & TARGET_SA_RESTORER) {
3126
        regs->pr = (unsigned long) ka->sa_restorer;
3127
    } else {
3128
        /* Generate return code (system call to sigreturn) */
3129
        err |= __put_user(MOVW(2), &frame->retcode[0]);
3130
        err |= __put_user(TRAP_NOARG, &frame->retcode[1]);
3131
        err |= __put_user((TARGET_NR_rt_sigreturn), &frame->retcode[2]);
3132
        regs->pr = (unsigned long) frame->retcode;
3133
    }
3134

    
3135
    if (err)
3136
        goto give_sigsegv;
3137

    
3138
    /* Set up registers for signal handler */
3139
    regs->gregs[15] = frame_addr;
3140
    regs->gregs[4] = signal; /* Arg for signal handler */
3141
    regs->gregs[5] = frame_addr + offsetof(typeof(*frame), info);
3142
    regs->gregs[6] = frame_addr + offsetof(typeof(*frame), uc);
3143
    regs->pc = (unsigned long) ka->_sa_handler;
3144

    
3145
    unlock_user_struct(frame, frame_addr, 1);
3146
    return;
3147

    
3148
give_sigsegv:
3149
    unlock_user_struct(frame, frame_addr, 1);
3150
    force_sig(TARGET_SIGSEGV);
3151
}
3152

    
3153
long do_sigreturn(CPUState *regs)
3154
{
3155
    struct target_sigframe *frame;
3156
    abi_ulong frame_addr;
3157
    sigset_t blocked;
3158
    target_sigset_t target_set;
3159
    target_ulong r0;
3160
    int i;
3161
    int err = 0;
3162

    
3163
#if defined(DEBUG_SIGNAL)
3164
    fprintf(stderr, "do_sigreturn\n");
3165
#endif
3166
    frame_addr = regs->gregs[15];
3167
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3168
           goto badframe;
3169

    
3170
    err |= __get_user(target_set.sig[0], &frame->sc.oldmask);
3171
    for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3172
        err |= (__get_user(target_set.sig[i], &frame->extramask[i - 1]));
3173
    }
3174

    
3175
    if (err)
3176
        goto badframe;
3177

    
3178
    target_to_host_sigset_internal(&blocked, &target_set);
3179
    sigprocmask(SIG_SETMASK, &blocked, NULL);
3180

    
3181
    if (restore_sigcontext(regs, &frame->sc, &r0))
3182
        goto badframe;
3183

    
3184
    unlock_user_struct(frame, frame_addr, 0);
3185
    return r0;
3186

    
3187
badframe:
3188
    unlock_user_struct(frame, frame_addr, 0);
3189
    force_sig(TARGET_SIGSEGV);
3190
    return 0;
3191
}
3192

    
3193
long do_rt_sigreturn(CPUState *regs)
3194
{
3195
    struct target_rt_sigframe *frame;
3196
    abi_ulong frame_addr;
3197
    sigset_t blocked;
3198
    target_ulong r0;
3199

    
3200
#if defined(DEBUG_SIGNAL)
3201
    fprintf(stderr, "do_rt_sigreturn\n");
3202
#endif
3203
    frame_addr = regs->gregs[15];
3204
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
3205
           goto badframe;
3206

    
3207
    target_to_host_sigset(&blocked, &frame->uc.tuc_sigmask);
3208
    sigprocmask(SIG_SETMASK, &blocked, NULL);
3209

    
3210
    if (restore_sigcontext(regs, &frame->uc.tuc_mcontext, &r0))
3211
        goto badframe;
3212

    
3213
    if (do_sigaltstack(frame_addr +
3214
                       offsetof(struct target_rt_sigframe, uc.tuc_stack),
3215
                       0, get_sp_from_cpustate(regs)) == -EFAULT)
3216
        goto badframe;
3217

    
3218
    unlock_user_struct(frame, frame_addr, 0);
3219
    return r0;
3220

    
3221
badframe:
3222
    unlock_user_struct(frame, frame_addr, 0);
3223
    force_sig(TARGET_SIGSEGV);
3224
    return 0;
3225
}
3226
#elif defined(TARGET_MICROBLAZE)
3227

    
3228
struct target_sigcontext {
3229
    struct target_pt_regs regs;  /* needs to be first */
3230
    uint32_t oldmask;
3231
};
3232

    
3233
struct target_stack_t {
3234
    abi_ulong ss_sp;
3235
    int ss_flags;
3236
    unsigned int ss_size;
3237
};
3238

    
3239
struct target_ucontext {
3240
    abi_ulong tuc_flags;
3241
    abi_ulong tuc_link;
3242
    struct target_stack_t tuc_stack;
3243
    struct target_sigcontext tuc_mcontext;
3244
    uint32_t tuc_extramask[TARGET_NSIG_WORDS - 1];
3245
};
3246

    
3247
/* Signal frames. */
3248
struct target_signal_frame {
3249
    struct target_ucontext uc;
3250
    uint32_t extramask[TARGET_NSIG_WORDS - 1];
3251
    uint32_t tramp[2];
3252
};
3253

    
3254
struct rt_signal_frame {
3255
    struct siginfo info;
3256
    struct ucontext uc;
3257
    uint32_t tramp[2];
3258
};
3259

    
3260
static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3261
{
3262
    __put_user(env->regs[0], &sc->regs.r0);
3263
    __put_user(env->regs[1], &sc->regs.r1);
3264
    __put_user(env->regs[2], &sc->regs.r2);
3265
    __put_user(env->regs[3], &sc->regs.r3);
3266
    __put_user(env->regs[4], &sc->regs.r4);
3267
    __put_user(env->regs[5], &sc->regs.r5);
3268
    __put_user(env->regs[6], &sc->regs.r6);
3269
    __put_user(env->regs[7], &sc->regs.r7);
3270
    __put_user(env->regs[8], &sc->regs.r8);
3271
    __put_user(env->regs[9], &sc->regs.r9);
3272
    __put_user(env->regs[10], &sc->regs.r10);
3273
    __put_user(env->regs[11], &sc->regs.r11);
3274
    __put_user(env->regs[12], &sc->regs.r12);
3275
    __put_user(env->regs[13], &sc->regs.r13);
3276
    __put_user(env->regs[14], &sc->regs.r14);
3277
    __put_user(env->regs[15], &sc->regs.r15);
3278
    __put_user(env->regs[16], &sc->regs.r16);
3279
    __put_user(env->regs[17], &sc->regs.r17);
3280
    __put_user(env->regs[18], &sc->regs.r18);
3281
    __put_user(env->regs[19], &sc->regs.r19);
3282
    __put_user(env->regs[20], &sc->regs.r20);
3283
    __put_user(env->regs[21], &sc->regs.r21);
3284
    __put_user(env->regs[22], &sc->regs.r22);
3285
    __put_user(env->regs[23], &sc->regs.r23);
3286
    __put_user(env->regs[24], &sc->regs.r24);
3287
    __put_user(env->regs[25], &sc->regs.r25);
3288
    __put_user(env->regs[26], &sc->regs.r26);
3289
    __put_user(env->regs[27], &sc->regs.r27);
3290
    __put_user(env->regs[28], &sc->regs.r28);
3291
    __put_user(env->regs[29], &sc->regs.r29);
3292
    __put_user(env->regs[30], &sc->regs.r30);
3293
    __put_user(env->regs[31], &sc->regs.r31);
3294
    __put_user(env->sregs[SR_PC], &sc->regs.pc);
3295
}
3296

    
3297
static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3298
{
3299
    __get_user(env->regs[0], &sc->regs.r0);
3300
    __get_user(env->regs[1], &sc->regs.r1);
3301
    __get_user(env->regs[2], &sc->regs.r2);
3302
    __get_user(env->regs[3], &sc->regs.r3);
3303
    __get_user(env->regs[4], &sc->regs.r4);
3304
    __get_user(env->regs[5], &sc->regs.r5);
3305
    __get_user(env->regs[6], &sc->regs.r6);
3306
    __get_user(env->regs[7], &sc->regs.r7);
3307
    __get_user(env->regs[8], &sc->regs.r8);
3308
    __get_user(env->regs[9], &sc->regs.r9);
3309
    __get_user(env->regs[10], &sc->regs.r10);
3310
    __get_user(env->regs[11], &sc->regs.r11);
3311
    __get_user(env->regs[12], &sc->regs.r12);
3312
    __get_user(env->regs[13], &sc->regs.r13);
3313
    __get_user(env->regs[14], &sc->regs.r14);
3314
    __get_user(env->regs[15], &sc->regs.r15);
3315
    __get_user(env->regs[16], &sc->regs.r16);
3316
    __get_user(env->regs[17], &sc->regs.r17);
3317
    __get_user(env->regs[18], &sc->regs.r18);
3318
    __get_user(env->regs[19], &sc->regs.r19);
3319
    __get_user(env->regs[20], &sc->regs.r20);
3320
    __get_user(env->regs[21], &sc->regs.r21);
3321
    __get_user(env->regs[22], &sc->regs.r22);
3322
    __get_user(env->regs[23], &sc->regs.r23);
3323
    __get_user(env->regs[24], &sc->regs.r24);
3324
    __get_user(env->regs[25], &sc->regs.r25);
3325
    __get_user(env->regs[26], &sc->regs.r26);
3326
    __get_user(env->regs[27], &sc->regs.r27);
3327
    __get_user(env->regs[28], &sc->regs.r28);
3328
    __get_user(env->regs[29], &sc->regs.r29);
3329
    __get_user(env->regs[30], &sc->regs.r30);
3330
    __get_user(env->regs[31], &sc->regs.r31);
3331
    __get_user(env->sregs[SR_PC], &sc->regs.pc);
3332
}
3333

    
3334
static abi_ulong get_sigframe(struct target_sigaction *ka,
3335
                              CPUState *env, int frame_size)
3336
{
3337
    abi_ulong sp = env->regs[1];
3338

    
3339
    if ((ka->sa_flags & SA_ONSTACK) != 0 && !on_sig_stack(sp))
3340
        sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
3341

    
3342
    return ((sp - frame_size) & -8UL);
3343
}
3344

    
3345
static void setup_frame(int sig, struct target_sigaction *ka,
3346
                        target_sigset_t *set, CPUState *env)
3347
{
3348
    struct target_signal_frame *frame;
3349
    abi_ulong frame_addr;
3350
    int err = 0;
3351
    int i;
3352

    
3353
    frame_addr = get_sigframe(ka, env, sizeof *frame);
3354
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3355
        goto badframe;
3356

    
3357
    /* Save the mask.  */
3358
    err |= __put_user(set->sig[0], &frame->uc.tuc_mcontext.oldmask);
3359
    if (err)
3360
        goto badframe;
3361

    
3362
    for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3363
        if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3364
            goto badframe;
3365
    }
3366

    
3367
    setup_sigcontext(&frame->uc.tuc_mcontext, env);
3368

    
3369
    /* Set up to return from userspace. If provided, use a stub
3370
       already in userspace. */
3371
    /* minus 8 is offset to cater for "rtsd r15,8" offset */
3372
    if (ka->sa_flags & TARGET_SA_RESTORER) {
3373
        env->regs[15] = ((unsigned long)ka->sa_restorer)-8;
3374
    } else {
3375
        uint32_t t;
3376
        /* Note, these encodings are _big endian_! */
3377
        /* addi r12, r0, __NR_sigreturn */
3378
        t = 0x31800000UL | TARGET_NR_sigreturn;
3379
        err |= __put_user(t, frame->tramp + 0);
3380
        /* brki r14, 0x8 */
3381
        t = 0xb9cc0008UL;
3382
        err |= __put_user(t, frame->tramp + 1);
3383

    
3384
        /* Return from sighandler will jump to the tramp.
3385
           Negative 8 offset because return is rtsd r15, 8 */
3386
        env->regs[15] = ((unsigned long)frame->tramp) - 8;
3387
    }
3388

    
3389
    if (err)
3390
        goto badframe;
3391

    
3392
    /* Set up registers for signal handler */
3393
    env->regs[1] = frame_addr;
3394
    /* Signal handler args: */
3395
    env->regs[5] = sig; /* Arg 0: signum */
3396
    env->regs[6] = 0;
3397
    /* arg 1: sigcontext */
3398
    env->regs[7] = frame_addr += offsetof(typeof(*frame), uc);
3399

    
3400
    /* Offset of 4 to handle microblaze rtid r14, 0 */
3401
    env->sregs[SR_PC] = (unsigned long)ka->_sa_handler;
3402

    
3403
    unlock_user_struct(frame, frame_addr, 1);
3404
    return;
3405
  badframe:
3406
    unlock_user_struct(frame, frame_addr, 1);
3407
    force_sig(TARGET_SIGSEGV);
3408
}
3409

    
3410
static void setup_rt_frame(int sig, struct target_sigaction *ka,
3411
                           target_siginfo_t *info,
3412
                           target_sigset_t *set, CPUState *env)
3413
{
3414
    fprintf(stderr, "Microblaze setup_rt_frame: not implemented\n");
3415
}
3416

    
3417
long do_sigreturn(CPUState *env)
3418
{
3419
    struct target_signal_frame *frame;
3420
    abi_ulong frame_addr;
3421
    target_sigset_t target_set;
3422
    sigset_t set;
3423
    int i;
3424

    
3425
    frame_addr = env->regs[R_SP];
3426
    /* Make sure the guest isn't playing games.  */
3427
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3428
        goto badframe;
3429

    
3430
    /* Restore blocked signals */
3431
    if (__get_user(target_set.sig[0], &frame->uc.tuc_mcontext.oldmask))
3432
        goto badframe;
3433
    for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3434
        if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3435
            goto badframe;
3436
    }
3437
    target_to_host_sigset_internal(&set, &target_set);
3438
    sigprocmask(SIG_SETMASK, &set, NULL);
3439

    
3440
    restore_sigcontext(&frame->uc.tuc_mcontext, env);
3441
    /* We got here through a sigreturn syscall, our path back is via an
3442
       rtb insn so setup r14 for that.  */
3443
    env->regs[14] = env->sregs[SR_PC];
3444
 
3445
    unlock_user_struct(frame, frame_addr, 0);
3446
    return env->regs[10];
3447
  badframe:
3448
    unlock_user_struct(frame, frame_addr, 0);
3449
    force_sig(TARGET_SIGSEGV);
3450
}
3451

    
3452
long do_rt_sigreturn(CPUState *env)
3453
{
3454
    fprintf(stderr, "Microblaze do_rt_sigreturn: not implemented\n");
3455
    return -TARGET_ENOSYS;
3456
}
3457

    
3458
#elif defined(TARGET_CRIS)
3459

    
3460
struct target_sigcontext {
3461
        struct target_pt_regs regs;  /* needs to be first */
3462
        uint32_t oldmask;
3463
        uint32_t usp;    /* usp before stacking this gunk on it */
3464
};
3465

    
3466
/* Signal frames. */
3467
struct target_signal_frame {
3468
        struct target_sigcontext sc;
3469
        uint32_t extramask[TARGET_NSIG_WORDS - 1];
3470
        uint8_t retcode[8];       /* Trampoline code. */
3471
};
3472

    
3473
struct rt_signal_frame {
3474
        struct siginfo *pinfo;
3475
        void *puc;
3476
        struct siginfo info;
3477
        struct ucontext uc;
3478
        uint8_t retcode[8];       /* Trampoline code. */
3479
};
3480

    
3481
static void setup_sigcontext(struct target_sigcontext *sc, CPUState *env)
3482
{
3483
        __put_user(env->regs[0], &sc->regs.r0);
3484
        __put_user(env->regs[1], &sc->regs.r1);
3485
        __put_user(env->regs[2], &sc->regs.r2);
3486
        __put_user(env->regs[3], &sc->regs.r3);
3487
        __put_user(env->regs[4], &sc->regs.r4);
3488
        __put_user(env->regs[5], &sc->regs.r5);
3489
        __put_user(env->regs[6], &sc->regs.r6);
3490
        __put_user(env->regs[7], &sc->regs.r7);
3491
        __put_user(env->regs[8], &sc->regs.r8);
3492
        __put_user(env->regs[9], &sc->regs.r9);
3493
        __put_user(env->regs[10], &sc->regs.r10);
3494
        __put_user(env->regs[11], &sc->regs.r11);
3495
        __put_user(env->regs[12], &sc->regs.r12);
3496
        __put_user(env->regs[13], &sc->regs.r13);
3497
        __put_user(env->regs[14], &sc->usp);
3498
        __put_user(env->regs[15], &sc->regs.acr);
3499
        __put_user(env->pregs[PR_MOF], &sc->regs.mof);
3500
        __put_user(env->pregs[PR_SRP], &sc->regs.srp);
3501
        __put_user(env->pc, &sc->regs.erp);
3502
}
3503

    
3504
static void restore_sigcontext(struct target_sigcontext *sc, CPUState *env)
3505
{
3506
        __get_user(env->regs[0], &sc->regs.r0);
3507
        __get_user(env->regs[1], &sc->regs.r1);
3508
        __get_user(env->regs[2], &sc->regs.r2);
3509
        __get_user(env->regs[3], &sc->regs.r3);
3510
        __get_user(env->regs[4], &sc->regs.r4);
3511
        __get_user(env->regs[5], &sc->regs.r5);
3512
        __get_user(env->regs[6], &sc->regs.r6);
3513
        __get_user(env->regs[7], &sc->regs.r7);
3514
        __get_user(env->regs[8], &sc->regs.r8);
3515
        __get_user(env->regs[9], &sc->regs.r9);
3516
        __get_user(env->regs[10], &sc->regs.r10);
3517
        __get_user(env->regs[11], &sc->regs.r11);
3518
        __get_user(env->regs[12], &sc->regs.r12);
3519
        __get_user(env->regs[13], &sc->regs.r13);
3520
        __get_user(env->regs[14], &sc->usp);
3521
        __get_user(env->regs[15], &sc->regs.acr);
3522
        __get_user(env->pregs[PR_MOF], &sc->regs.mof);
3523
        __get_user(env->pregs[PR_SRP], &sc->regs.srp);
3524
        __get_user(env->pc, &sc->regs.erp);
3525
}
3526

    
3527
static abi_ulong get_sigframe(CPUState *env, int framesize)
3528
{
3529
        abi_ulong sp;
3530
        /* Align the stack downwards to 4.  */
3531
        sp = (env->regs[R_SP] & ~3);
3532
        return sp - framesize;
3533
}
3534

    
3535
static void setup_frame(int sig, struct target_sigaction *ka,
3536
                        target_sigset_t *set, CPUState *env)
3537
{
3538
        struct target_signal_frame *frame;
3539
        abi_ulong frame_addr;
3540
        int err = 0;
3541
        int i;
3542

    
3543
        frame_addr = get_sigframe(env, sizeof *frame);
3544
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
3545
                goto badframe;
3546

    
3547
        /*
3548
         * The CRIS signal return trampoline. A real linux/CRIS kernel doesn't
3549
         * use this trampoline anymore but it sets it up for GDB.
3550
         * In QEMU, using the trampoline simplifies things a bit so we use it.
3551
         *
3552
         * This is movu.w __NR_sigreturn, r9; break 13;
3553
         */
3554
        err |= __put_user(0x9c5f, frame->retcode+0);
3555
        err |= __put_user(TARGET_NR_sigreturn, 
3556
                          frame->retcode+2);
3557
        err |= __put_user(0xe93d, frame->retcode+4);
3558

    
3559
        /* Save the mask.  */
3560
        err |= __put_user(set->sig[0], &frame->sc.oldmask);
3561
        if (err)
3562
                goto badframe;
3563

    
3564
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3565
                if (__put_user(set->sig[i], &frame->extramask[i - 1]))
3566
                        goto badframe;
3567
        }
3568

    
3569
        setup_sigcontext(&frame->sc, env);
3570

    
3571
        /* Move the stack and setup the arguments for the handler.  */
3572
        env->regs[R_SP] = frame_addr;
3573
        env->regs[10] = sig;
3574
        env->pc = (unsigned long) ka->_sa_handler;
3575
        /* Link SRP so the guest returns through the trampoline.  */
3576
        env->pregs[PR_SRP] = frame_addr + offsetof(typeof(*frame), retcode);
3577

    
3578
        unlock_user_struct(frame, frame_addr, 1);
3579
        return;
3580
  badframe:
3581
        unlock_user_struct(frame, frame_addr, 1);
3582
        force_sig(TARGET_SIGSEGV);
3583
}
3584

    
3585
static void setup_rt_frame(int sig, struct target_sigaction *ka,
3586
                           target_siginfo_t *info,
3587
                           target_sigset_t *set, CPUState *env)
3588
{
3589
    fprintf(stderr, "CRIS setup_rt_frame: not implemented\n");
3590
}
3591

    
3592
long do_sigreturn(CPUState *env)
3593
{
3594
        struct target_signal_frame *frame;
3595
        abi_ulong frame_addr;
3596
        target_sigset_t target_set;
3597
        sigset_t set;
3598
        int i;
3599

    
3600
        frame_addr = env->regs[R_SP];
3601
        /* Make sure the guest isn't playing games.  */
3602
        if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
3603
                goto badframe;
3604

    
3605
        /* Restore blocked signals */
3606
        if (__get_user(target_set.sig[0], &frame->sc.oldmask))
3607
                goto badframe;
3608
        for(i = 1; i < TARGET_NSIG_WORDS; i++) {
3609
                if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
3610
                        goto badframe;
3611
        }
3612
        target_to_host_sigset_internal(&set, &target_set);
3613
        sigprocmask(SIG_SETMASK, &set, NULL);
3614

    
3615
        restore_sigcontext(&frame->sc, env);
3616
        unlock_user_struct(frame, frame_addr, 0);
3617
        return env->regs[10];
3618
  badframe:
3619
        unlock_user_struct(frame, frame_addr, 0);
3620
        force_sig(TARGET_SIGSEGV);
3621
}
3622

    
3623
long do_rt_sigreturn(CPUState *env)
3624
{
3625
    fprintf(stderr, "CRIS do_rt_sigreturn: not implemented\n");
3626
    return -TARGET_ENOSYS;
3627
}
3628

    
3629
#elif defined(TARGET_S390X)
3630

    
3631
#define __NUM_GPRS 16
3632
#define __NUM_FPRS 16
3633
#define __NUM_ACRS 16
3634

    
3635
#define S390_SYSCALL_SIZE   2
3636
#define __SIGNAL_FRAMESIZE      160 /* FIXME: 31-bit mode -> 96 */
3637

    
3638
#define _SIGCONTEXT_NSIG        64
3639
#define _SIGCONTEXT_NSIG_BPW    64 /* FIXME: 31-bit mode -> 32 */
3640
#define _SIGCONTEXT_NSIG_WORDS  (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
3641
#define _SIGMASK_COPY_SIZE    (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
3642
#define PSW_ADDR_AMODE            0x0000000000000000UL /* 0x80000000UL for 31-bit */
3643
#define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
3644

    
3645
typedef struct {
3646
    target_psw_t psw;
3647
    target_ulong gprs[__NUM_GPRS];
3648
    unsigned int acrs[__NUM_ACRS];
3649
} target_s390_regs_common;
3650

    
3651
typedef struct {
3652
    unsigned int fpc;
3653
    double   fprs[__NUM_FPRS];
3654
} target_s390_fp_regs;
3655

    
3656
typedef struct {
3657
    target_s390_regs_common regs;
3658
    target_s390_fp_regs     fpregs;
3659
} target_sigregs;
3660

    
3661
struct target_sigcontext {
3662
    target_ulong   oldmask[_SIGCONTEXT_NSIG_WORDS];
3663
    target_sigregs *sregs;
3664
};
3665

    
3666
typedef struct {
3667
    uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3668
    struct target_sigcontext sc;
3669
    target_sigregs sregs;
3670
    int signo;
3671
    uint8_t retcode[S390_SYSCALL_SIZE];
3672
} sigframe;
3673

    
3674
struct target_ucontext {
3675
    target_ulong tuc_flags;
3676
    struct target_ucontext *tuc_link;
3677
    target_stack_t tuc_stack;
3678
    target_sigregs tuc_mcontext;
3679
    target_sigset_t tuc_sigmask;   /* mask last for extensibility */
3680
};
3681

    
3682
typedef struct {
3683
    uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
3684
    uint8_t retcode[S390_SYSCALL_SIZE];
3685
    struct target_siginfo info;
3686
    struct target_ucontext uc;
3687
} rt_sigframe;
3688

    
3689
static inline abi_ulong
3690
get_sigframe(struct target_sigaction *ka, CPUState *env, size_t frame_size)
3691
{
3692
    abi_ulong sp;
3693

    
3694
    /* Default to using normal stack */
3695
    sp = env->regs[15];
3696

    
3697
    /* This is the X/Open sanctioned signal stack switching.  */
3698
    if (ka->sa_flags & TARGET_SA_ONSTACK) {
3699
        if (!sas_ss_flags(sp)) {
3700
            sp = target_sigaltstack_used.ss_sp +
3701
                 target_sigaltstack_used.ss_size;
3702
        }
3703
    }
3704

    
3705
    /* This is the legacy signal stack switching. */
3706
    else if (/* FIXME !user_mode(regs) */ 0 &&
3707
             !(ka->sa_flags & TARGET_SA_RESTORER) &&
3708
             ka->sa_restorer) {
3709
        sp = (abi_ulong) ka->sa_restorer;
3710
    }
3711

    
3712
    return (sp - frame_size) & -8ul;
3713
}
3714

    
3715
static void save_sigregs(CPUState *env, target_sigregs *sregs)
3716
{
3717
    int i;
3718
    //save_access_regs(current->thread.acrs); FIXME
3719

    
3720
    /* Copy a 'clean' PSW mask to the user to avoid leaking
3721
       information about whether PER is currently on.  */
3722
    __put_user(env->psw.mask, &sregs->regs.psw.mask);
3723
    __put_user(env->psw.addr, &sregs->regs.psw.addr);
3724
    for (i = 0; i < 16; i++) {
3725
        __put_user(env->regs[i], &sregs->regs.gprs[i]);
3726
    }
3727
    for (i = 0; i < 16; i++) {
3728
        __put_user(env->aregs[i], &sregs->regs.acrs[i]);
3729
    }
3730
    /*
3731
     * We have to store the fp registers to current->thread.fp_regs
3732
     * to merge them with the emulated registers.
3733
     */
3734
    //save_fp_regs(&current->thread.fp_regs); FIXME
3735
    for (i = 0; i < 16; i++) {
3736
        __put_user(env->fregs[i].ll, &sregs->fpregs.fprs[i]);
3737
    }
3738
}
3739

    
3740
static void setup_frame(int sig, struct target_sigaction *ka,
3741
                        target_sigset_t *set, CPUState *env)
3742
{
3743
    sigframe *frame;
3744
    abi_ulong frame_addr;
3745

    
3746
    frame_addr = get_sigframe(ka, env, sizeof(*frame));
3747
    qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3748
             (unsigned long long)frame_addr);
3749
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3750
            goto give_sigsegv;
3751
    }
3752

    
3753
    qemu_log("%s: 1\n", __FUNCTION__);
3754
    if (__put_user(set->sig[0], &frame->sc.oldmask[0])) {
3755
              goto give_sigsegv;
3756
    }
3757

    
3758
    save_sigregs(env, &frame->sregs);
3759

    
3760
    __put_user((abi_ulong)(unsigned long)&frame->sregs,
3761
               (abi_ulong *)&frame->sc.sregs);
3762

    
3763
    /* Set up to return from userspace.  If provided, use a stub
3764
       already in userspace.  */
3765
    if (ka->sa_flags & TARGET_SA_RESTORER) {
3766
            env->regs[14] = (unsigned long)
3767
                    ka->sa_restorer | PSW_ADDR_AMODE;
3768
    } else {
3769
            env->regs[14] = (unsigned long)
3770
                    frame->retcode | PSW_ADDR_AMODE;
3771
            if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
3772
                           (uint16_t *)(frame->retcode)))
3773
                    goto give_sigsegv;
3774
    }
3775

    
3776
    /* Set up backchain. */
3777
    if (__put_user(env->regs[15], (abi_ulong *) frame)) {
3778
            goto give_sigsegv;
3779
    }
3780

    
3781
    /* Set up registers for signal handler */
3782
    env->regs[15] = frame_addr;
3783
    env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
3784

    
3785
    env->regs[2] = sig; //map_signal(sig);
3786
    env->regs[3] = frame_addr += offsetof(typeof(*frame), sc);
3787

    
3788
    /* We forgot to include these in the sigcontext.
3789
       To avoid breaking binary compatibility, they are passed as args. */
3790
    env->regs[4] = 0; // FIXME: no clue... current->thread.trap_no;
3791
    env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
3792

    
3793
    /* Place signal number on stack to allow backtrace from handler.  */
3794
    if (__put_user(env->regs[2], (int *) &frame->signo)) {
3795
            goto give_sigsegv;
3796
    }
3797
    unlock_user_struct(frame, frame_addr, 1);
3798
    return;
3799

    
3800
give_sigsegv:
3801
    qemu_log("%s: give_sigsegv\n", __FUNCTION__);
3802
    unlock_user_struct(frame, frame_addr, 1);
3803
    force_sig(TARGET_SIGSEGV);
3804
}
3805

    
3806
static void setup_rt_frame(int sig, struct target_sigaction *ka,
3807
                           target_siginfo_t *info,
3808
                           target_sigset_t *set, CPUState *env)
3809
{
3810
    int i;
3811
    rt_sigframe *frame;
3812
    abi_ulong frame_addr;
3813

    
3814
    frame_addr = get_sigframe(ka, env, sizeof *frame);
3815
    qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3816
             (unsigned long long)frame_addr);
3817
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
3818
        goto give_sigsegv;
3819
    }
3820

    
3821
    qemu_log("%s: 1\n", __FUNCTION__);
3822
    if (copy_siginfo_to_user(&frame->info, info)) {
3823
        goto give_sigsegv;
3824
    }
3825

    
3826
    /* Create the ucontext.  */
3827
    __put_user(0, &frame->uc.tuc_flags);
3828
    __put_user((abi_ulong)0, (abi_ulong *)&frame->uc.tuc_link);
3829
    __put_user(target_sigaltstack_used.ss_sp, &frame->uc.tuc_stack.ss_sp);
3830
    __put_user(sas_ss_flags(get_sp_from_cpustate(env)),
3831
                      &frame->uc.tuc_stack.ss_flags);
3832
    __put_user(target_sigaltstack_used.ss_size, &frame->uc.tuc_stack.ss_size);
3833
    save_sigregs(env, &frame->uc.tuc_mcontext);
3834
    for (i = 0; i < TARGET_NSIG_WORDS; i++) {
3835
        __put_user((abi_ulong)set->sig[i],
3836
        (abi_ulong *)&frame->uc.tuc_sigmask.sig[i]);
3837
    }
3838

    
3839
    /* Set up to return from userspace.  If provided, use a stub
3840
       already in userspace.  */
3841
    if (ka->sa_flags & TARGET_SA_RESTORER) {
3842
        env->regs[14] = (unsigned long) ka->sa_restorer | PSW_ADDR_AMODE;
3843
    } else {
3844
        env->regs[14] = (unsigned long) frame->retcode | PSW_ADDR_AMODE;
3845
        if (__put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
3846
                       (uint16_t *)(frame->retcode))) {
3847
            goto give_sigsegv;
3848
        }
3849
    }
3850

    
3851
    /* Set up backchain. */
3852
    if (__put_user(env->regs[15], (abi_ulong *) frame)) {
3853
        goto give_sigsegv;
3854
    }
3855

    
3856
    /* Set up registers for signal handler */
3857
    env->regs[15] = frame_addr;
3858
    env->psw.addr = (target_ulong) ka->_sa_handler | PSW_ADDR_AMODE;
3859

    
3860
    env->regs[2] = sig; //map_signal(sig);
3861
    env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
3862
    env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
3863
    return;
3864

    
3865
give_sigsegv:
3866
    qemu_log("%s: give_sigsegv\n", __FUNCTION__);
3867
    unlock_user_struct(frame, frame_addr, 1);
3868
    force_sig(TARGET_SIGSEGV);
3869
}
3870

    
3871
static int
3872
restore_sigregs(CPUState *env, target_sigregs *sc)
3873
{
3874
    int err = 0;
3875
    int i;
3876

    
3877
    for (i = 0; i < 16; i++) {
3878
        err |= __get_user(env->regs[i], &sc->regs.gprs[i]);
3879
    }
3880

    
3881
    err |= __get_user(env->psw.mask, &sc->regs.psw.mask);
3882
    qemu_log("%s: sc->regs.psw.addr 0x%llx env->psw.addr 0x%llx\n",
3883
             __FUNCTION__, (unsigned long long)sc->regs.psw.addr,
3884
             (unsigned long long)env->psw.addr);
3885
    err |= __get_user(env->psw.addr, &sc->regs.psw.addr);
3886
    /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
3887

    
3888
    for (i = 0; i < 16; i++) {
3889
        err |= __get_user(env->aregs[i], &sc->regs.acrs[i]);
3890
    }
3891
    for (i = 0; i < 16; i++) {
3892
        err |= __get_user(env->fregs[i].ll, &sc->fpregs.fprs[i]);
3893
    }
3894

    
3895
    return err;
3896
}
3897

    
3898
long do_sigreturn(CPUState *env)
3899
{
3900
    sigframe *frame;
3901
    abi_ulong frame_addr = env->regs[15];
3902
    qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3903
             (unsigned long long)frame_addr);
3904
    target_sigset_t target_set;
3905
    sigset_t set;
3906

    
3907
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3908
        goto badframe;
3909
    }
3910
    if (__get_user(target_set.sig[0], &frame->sc.oldmask[0])) {
3911
        goto badframe;
3912
    }
3913

    
3914
    target_to_host_sigset_internal(&set, &target_set);
3915
    sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
3916

    
3917
    if (restore_sigregs(env, &frame->sregs)) {
3918
        goto badframe;
3919
    }
3920

    
3921
    unlock_user_struct(frame, frame_addr, 0);
3922
    return env->regs[2];
3923

    
3924
badframe:
3925
    unlock_user_struct(frame, frame_addr, 0);
3926
    force_sig(TARGET_SIGSEGV);
3927
    return 0;
3928
}
3929

    
3930
long do_rt_sigreturn(CPUState *env)
3931
{
3932
    rt_sigframe *frame;
3933
    abi_ulong frame_addr = env->regs[15];
3934
    qemu_log("%s: frame_addr 0x%llx\n", __FUNCTION__,
3935
             (unsigned long long)frame_addr);
3936
    sigset_t set;
3937

    
3938
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
3939
        goto badframe;
3940
    }
3941
    target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
3942

    
3943
    sigprocmask(SIG_SETMASK, &set, NULL); /* ~_BLOCKABLE? */
3944

    
3945
    if (restore_sigregs(env, &frame->uc.tuc_mcontext)) {
3946
        goto badframe;
3947
    }
3948

    
3949
    if (do_sigaltstack(frame_addr + offsetof(rt_sigframe, uc.tuc_stack), 0,
3950
                       get_sp_from_cpustate(env)) == -EFAULT) {
3951
        goto badframe;
3952
    }
3953
    unlock_user_struct(frame, frame_addr, 0);
3954
    return env->regs[2];
3955

    
3956
badframe:
3957
    unlock_user_struct(frame, frame_addr, 0);
3958
    force_sig(TARGET_SIGSEGV);
3959
    return 0;
3960
}
3961

    
3962
#elif defined(TARGET_PPC) && !defined(TARGET_PPC64)
3963

    
3964
/* FIXME: Many of the structures are defined for both PPC and PPC64, but
3965
   the signal handling is different enough that we haven't implemented
3966
   support for PPC64 yet.  Hence the restriction above.
3967

3968
   There are various #if'd blocks for code for TARGET_PPC64.  These
3969
   blocks should go away so that we can successfully run 32-bit and
3970
   64-bit binaries on a QEMU configured for PPC64.  */
3971

    
3972
/* Size of dummy stack frame allocated when calling signal handler.
3973
   See arch/powerpc/include/asm/ptrace.h.  */
3974
#if defined(TARGET_PPC64)
3975
#define SIGNAL_FRAMESIZE 128
3976
#else
3977
#define SIGNAL_FRAMESIZE 64
3978
#endif
3979

    
3980
/* See arch/powerpc/include/asm/sigcontext.h.  */
3981
struct target_sigcontext {
3982
    target_ulong _unused[4];
3983
    int32_t signal;
3984
#if defined(TARGET_PPC64)
3985
    int32_t pad0;
3986
#endif
3987
    target_ulong handler;
3988
    target_ulong oldmask;
3989
    target_ulong regs;      /* struct pt_regs __user * */
3990
    /* TODO: PPC64 includes extra bits here.  */
3991
};
3992

    
3993
/* Indices for target_mcontext.mc_gregs, below.
3994
   See arch/powerpc/include/asm/ptrace.h for details.  */
3995
enum {
3996
    TARGET_PT_R0 = 0,
3997
    TARGET_PT_R1 = 1,
3998
    TARGET_PT_R2 = 2,
3999
    TARGET_PT_R3 = 3,
4000
    TARGET_PT_R4 = 4,
4001
    TARGET_PT_R5 = 5,
4002
    TARGET_PT_R6 = 6,
4003
    TARGET_PT_R7 = 7,
4004
    TARGET_PT_R8 = 8,
4005
    TARGET_PT_R9 = 9,
4006
    TARGET_PT_R10 = 10,
4007
    TARGET_PT_R11 = 11,
4008
    TARGET_PT_R12 = 12,
4009
    TARGET_PT_R13 = 13,
4010
    TARGET_PT_R14 = 14,
4011
    TARGET_PT_R15 = 15,
4012
    TARGET_PT_R16 = 16,
4013
    TARGET_PT_R17 = 17,
4014
    TARGET_PT_R18 = 18,
4015
    TARGET_PT_R19 = 19,
4016
    TARGET_PT_R20 = 20,
4017
    TARGET_PT_R21 = 21,
4018
    TARGET_PT_R22 = 22,
4019
    TARGET_PT_R23 = 23,
4020
    TARGET_PT_R24 = 24,
4021
    TARGET_PT_R25 = 25,
4022
    TARGET_PT_R26 = 26,
4023
    TARGET_PT_R27 = 27,
4024
    TARGET_PT_R28 = 28,
4025
    TARGET_PT_R29 = 29,
4026
    TARGET_PT_R30 = 30,
4027
    TARGET_PT_R31 = 31,
4028
    TARGET_PT_NIP = 32,
4029
    TARGET_PT_MSR = 33,
4030
    TARGET_PT_ORIG_R3 = 34,
4031
    TARGET_PT_CTR = 35,
4032
    TARGET_PT_LNK = 36,
4033
    TARGET_PT_XER = 37,
4034
    TARGET_PT_CCR = 38,
4035
    /* Yes, there are two registers with #39.  One is 64-bit only.  */
4036
    TARGET_PT_MQ = 39,
4037
    TARGET_PT_SOFTE = 39,
4038
    TARGET_PT_TRAP = 40,
4039
    TARGET_PT_DAR = 41,
4040
    TARGET_PT_DSISR = 42,
4041
    TARGET_PT_RESULT = 43,
4042
    TARGET_PT_REGS_COUNT = 44
4043
};
4044

    
4045
/* See arch/powerpc/include/asm/ucontext.h.  Only used for 32-bit PPC;
4046
   on 64-bit PPC, sigcontext and mcontext are one and the same.  */
4047
struct target_mcontext {
4048
    target_ulong mc_gregs[48];
4049
    /* Includes fpscr.  */
4050
    uint64_t mc_fregs[33];
4051
    target_ulong mc_pad[2];
4052
    /* We need to handle Altivec and SPE at the same time, which no
4053
       kernel needs to do.  Fortunately, the kernel defines this bit to
4054
       be Altivec-register-large all the time, rather than trying to
4055
       twiddle it based on the specific platform.  */
4056
    union {
4057
        /* SPE vector registers.  One extra for SPEFSCR.  */
4058
        uint32_t spe[33];
4059
        /* Altivec vector registers.  The packing of VSCR and VRSAVE
4060
           varies depending on whether we're PPC64 or not: PPC64 splits
4061
           them apart; PPC32 stuffs them together.  */
4062
#if defined(TARGET_PPC64)
4063
#define QEMU_NVRREG 34
4064
#else
4065
#define QEMU_NVRREG 33
4066
#endif
4067
        ppc_avr_t altivec[QEMU_NVRREG];
4068
#undef QEMU_NVRREG
4069
    } mc_vregs __attribute__((__aligned__(16)));
4070
};
4071

    
4072
struct target_ucontext {
4073
    target_ulong tuc_flags;
4074
    target_ulong tuc_link;    /* struct ucontext __user * */
4075
    struct target_sigaltstack tuc_stack;
4076
#if !defined(TARGET_PPC64)
4077
    int32_t tuc_pad[7];
4078
    target_ulong tuc_regs;    /* struct mcontext __user *
4079
                                points to uc_mcontext field */
4080
#endif
4081
    target_sigset_t tuc_sigmask;
4082
#if defined(TARGET_PPC64)
4083
    target_sigset_t unused[15]; /* Allow for uc_sigmask growth */
4084
    struct target_sigcontext tuc_mcontext;
4085
#else
4086
    int32_t tuc_maskext[30];
4087
    int32_t tuc_pad2[3];
4088
    struct target_mcontext tuc_mcontext;
4089
#endif
4090
};
4091

    
4092
/* See arch/powerpc/kernel/signal_32.c.  */
4093
struct target_sigframe {
4094
    struct target_sigcontext sctx;
4095
    struct target_mcontext mctx;
4096
    int32_t abigap[56];
4097
};
4098

    
4099
struct target_rt_sigframe {
4100
    struct target_siginfo info;
4101
    struct target_ucontext uc;
4102
    int32_t abigap[56];
4103
};
4104

    
4105
/* We use the mc_pad field for the signal return trampoline.  */
4106
#define tramp mc_pad
4107

    
4108
/* See arch/powerpc/kernel/signal.c.  */
4109
static target_ulong get_sigframe(struct target_sigaction *ka,
4110
                                 CPUState *env,
4111
                                 int frame_size)
4112
{
4113
    target_ulong oldsp, newsp;
4114

    
4115
    oldsp = env->gpr[1];
4116

    
4117
    if ((ka->sa_flags & TARGET_SA_ONSTACK) &&
4118
        (sas_ss_flags(oldsp))) {
4119
        oldsp = (target_sigaltstack_used.ss_sp
4120
                 + target_sigaltstack_used.ss_size);
4121
    }
4122

    
4123
    newsp = (oldsp - frame_size) & ~0xFUL;
4124

    
4125
    return newsp;
4126
}
4127

    
4128
static int save_user_regs(CPUState *env, struct target_mcontext *frame,
4129
                          int sigret)
4130
{
4131
    target_ulong msr = env->msr;
4132
    int i;
4133
    target_ulong ccr = 0;
4134

    
4135
    /* In general, the kernel attempts to be intelligent about what it
4136
       needs to save for Altivec/FP/SPE registers.  We don't care that
4137
       much, so we just go ahead and save everything.  */
4138

    
4139
    /* Save general registers.  */
4140
    for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4141
        if (__put_user(env->gpr[i], &frame->mc_gregs[i])) {
4142
            return 1;
4143
        }
4144
    }
4145
    if (__put_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4146
        || __put_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4147
        || __put_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4148
        || __put_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4149
        return 1;
4150

    
4151
    for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4152
        ccr |= env->crf[i] << (32 - ((i + 1) * 4));
4153
    }
4154
    if (__put_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4155
        return 1;
4156

    
4157
    /* Save Altivec registers if necessary.  */
4158
    if (env->insns_flags & PPC_ALTIVEC) {
4159
        for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4160
            ppc_avr_t *avr = &env->avr[i];
4161
            ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4162

    
4163
            if (__put_user(avr->u64[0], &vreg->u64[0]) ||
4164
                __put_user(avr->u64[1], &vreg->u64[1])) {
4165
                return 1;
4166
            }
4167
        }
4168
        /* Set MSR_VR in the saved MSR value to indicate that
4169
           frame->mc_vregs contains valid data.  */
4170
        msr |= MSR_VR;
4171
        if (__put_user((uint32_t)env->spr[SPR_VRSAVE],
4172
                       &frame->mc_vregs.altivec[32].u32[3]))
4173
            return 1;
4174
    }
4175

    
4176
    /* Save floating point registers.  */
4177
    if (env->insns_flags & PPC_FLOAT) {
4178
        for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4179
            if (__put_user(env->fpr[i], &frame->mc_fregs[i])) {
4180
                return 1;
4181
            }
4182
        }
4183
        if (__put_user((uint64_t) env->fpscr, &frame->mc_fregs[32]))
4184
            return 1;
4185
    }
4186

    
4187
    /* Save SPE registers.  The kernel only saves the high half.  */
4188
    if (env->insns_flags & PPC_SPE) {
4189
#if defined(TARGET_PPC64)
4190
        for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4191
            if (__put_user(env->gpr[i] >> 32, &frame->mc_vregs.spe[i])) {
4192
                return 1;
4193
            }
4194
        }
4195
#else
4196
        for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4197
            if (__put_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4198
                return 1;
4199
            }
4200
        }
4201
#endif
4202
        /* Set MSR_SPE in the saved MSR value to indicate that
4203
           frame->mc_vregs contains valid data.  */
4204
        msr |= MSR_SPE;
4205
        if (__put_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4206
            return 1;
4207
    }
4208

    
4209
    /* Store MSR.  */
4210
    if (__put_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4211
        return 1;
4212

    
4213
    /* Set up the sigreturn trampoline: li r0,sigret; sc.  */
4214
    if (sigret) {
4215
        if (__put_user(0x38000000UL | sigret, &frame->tramp[0]) ||
4216
            __put_user(0x44000002UL, &frame->tramp[1])) {
4217
            return 1;
4218
        }
4219
    }
4220

    
4221
    return 0;
4222
}
4223

    
4224
static int restore_user_regs(CPUState *env,
4225
                             struct target_mcontext *frame, int sig)
4226
{
4227
    target_ulong save_r2 = 0;
4228
    target_ulong msr;
4229
    target_ulong ccr;
4230

    
4231
    int i;
4232

    
4233
    if (!sig) {
4234
        save_r2 = env->gpr[2];
4235
    }
4236

    
4237
    /* Restore general registers.  */
4238
    for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4239
        if (__get_user(env->gpr[i], &frame->mc_gregs[i])) {
4240
            return 1;
4241
        }
4242
    }
4243
    if (__get_user(env->nip, &frame->mc_gregs[TARGET_PT_NIP])
4244
        || __get_user(env->ctr, &frame->mc_gregs[TARGET_PT_CTR])
4245
        || __get_user(env->lr, &frame->mc_gregs[TARGET_PT_LNK])
4246
        || __get_user(env->xer, &frame->mc_gregs[TARGET_PT_XER]))
4247
        return 1;
4248
    if (__get_user(ccr, &frame->mc_gregs[TARGET_PT_CCR]))
4249
        return 1;
4250

    
4251
    for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
4252
        env->crf[i] = (ccr >> (32 - ((i + 1) * 4))) & 0xf;
4253
    }
4254

    
4255
    if (!sig) {
4256
        env->gpr[2] = save_r2;
4257
    }
4258
    /* Restore MSR.  */
4259
    if (__get_user(msr, &frame->mc_gregs[TARGET_PT_MSR]))
4260
        return 1;
4261

    
4262
    /* If doing signal return, restore the previous little-endian mode.  */
4263
    if (sig)
4264
        env->msr = (env->msr & ~MSR_LE) | (msr & MSR_LE);
4265

    
4266
    /* Restore Altivec registers if necessary.  */
4267
    if (env->insns_flags & PPC_ALTIVEC) {
4268
        for (i = 0; i < ARRAY_SIZE(env->avr); i++) {
4269
            ppc_avr_t *avr = &env->avr[i];
4270
            ppc_avr_t *vreg = &frame->mc_vregs.altivec[i];
4271

    
4272
            if (__get_user(avr->u64[0], &vreg->u64[0]) ||
4273
                __get_user(avr->u64[1], &vreg->u64[1])) {
4274
                return 1;
4275
            }
4276
        }
4277
        /* Set MSR_VEC in the saved MSR value to indicate that
4278
           frame->mc_vregs contains valid data.  */
4279
        if (__get_user(env->spr[SPR_VRSAVE],
4280
                       (target_ulong *)(&frame->mc_vregs.altivec[32].u32[3])))
4281
            return 1;
4282
    }
4283

    
4284
    /* Restore floating point registers.  */
4285
    if (env->insns_flags & PPC_FLOAT) {
4286
        uint64_t fpscr;
4287
        for (i = 0; i < ARRAY_SIZE(env->fpr); i++) {
4288
            if (__get_user(env->fpr[i], &frame->mc_fregs[i])) {
4289
                return 1;
4290
            }
4291
        }
4292
        if (__get_user(fpscr, &frame->mc_fregs[32]))
4293
            return 1;
4294
        env->fpscr = (uint32_t) fpscr;
4295
    }
4296

    
4297
    /* Save SPE registers.  The kernel only saves the high half.  */
4298
    if (env->insns_flags & PPC_SPE) {
4299
#if defined(TARGET_PPC64)
4300
        for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
4301
            uint32_t hi;
4302

    
4303
            if (__get_user(hi, &frame->mc_vregs.spe[i])) {
4304
                return 1;
4305
            }
4306
            env->gpr[i] = ((uint64_t)hi << 32) | ((uint32_t) env->gpr[i]);
4307
        }
4308
#else
4309
        for (i = 0; i < ARRAY_SIZE(env->gprh); i++) {
4310
            if (__get_user(env->gprh[i], &frame->mc_vregs.spe[i])) {
4311
                return 1;
4312
            }
4313
        }
4314
#endif
4315
        if (__get_user(env->spe_fscr, &frame->mc_vregs.spe[32]))
4316
            return 1;
4317
    }
4318

    
4319
    return 0;
4320
}
4321

    
4322
static void setup_frame(int sig, struct target_sigaction *ka,
4323
                        target_sigset_t *set, CPUState *env)
4324
{
4325
    struct target_sigframe *frame;
4326
    struct target_sigcontext *sc;
4327
    target_ulong frame_addr, newsp;
4328
    int err = 0;
4329
    int signal;
4330

    
4331
    frame_addr = get_sigframe(ka, env, sizeof(*frame));
4332
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 1))
4333
        goto sigsegv;
4334
    sc = &frame->sctx;
4335

    
4336
    signal = current_exec_domain_sig(sig);
4337

    
4338
    err |= __put_user(h2g(ka->_sa_handler), &sc->handler);
4339
    err |= __put_user(set->sig[0], &sc->oldmask);
4340
#if defined(TARGET_PPC64)
4341
    err |= __put_user(set->sig[0] >> 32, &sc->_unused[3]);
4342
#else
4343
    err |= __put_user(set->sig[1], &sc->_unused[3]);
4344
#endif
4345
    err |= __put_user(h2g(&frame->mctx), &sc->regs);
4346
    err |= __put_user(sig, &sc->signal);
4347

    
4348
    /* Save user regs.  */
4349
    err |= save_user_regs(env, &frame->mctx, TARGET_NR_sigreturn);
4350

    
4351
    /* The kernel checks for the presence of a VDSO here.  We don't
4352
       emulate a vdso, so use a sigreturn system call.  */
4353
    env->lr = (target_ulong) h2g(frame->mctx.tramp);
4354

    
4355
    /* Turn off all fp exceptions.  */
4356
    env->fpscr = 0;
4357

    
4358
    /* Create a stack frame for the caller of the handler.  */
4359
    newsp = frame_addr - SIGNAL_FRAMESIZE;
4360
    err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4361

    
4362
    if (err)
4363
        goto sigsegv;
4364

    
4365
    /* Set up registers for signal handler.  */
4366
    env->gpr[1] = newsp;
4367
    env->gpr[3] = signal;
4368
    env->gpr[4] = (target_ulong) h2g(sc);
4369
    env->nip = (target_ulong) ka->_sa_handler;
4370
    /* Signal handlers are entered in big-endian mode.  */
4371
    env->msr &= ~MSR_LE;
4372

    
4373
    unlock_user_struct(frame, frame_addr, 1);
4374
    return;
4375

    
4376
sigsegv:
4377
    unlock_user_struct(frame, frame_addr, 1);
4378
    if (logfile)
4379
        fprintf (logfile, "segfaulting from setup_frame\n");
4380
    force_sig(TARGET_SIGSEGV);
4381
}
4382

    
4383
static void setup_rt_frame(int sig, struct target_sigaction *ka,
4384
                           target_siginfo_t *info,
4385
                           target_sigset_t *set, CPUState *env)
4386
{
4387
    struct target_rt_sigframe *rt_sf;
4388
    struct target_mcontext *frame;
4389
    target_ulong rt_sf_addr, newsp = 0;
4390
    int i, err = 0;
4391
    int signal;
4392

    
4393
    rt_sf_addr = get_sigframe(ka, env, sizeof(*rt_sf));
4394
    if (!lock_user_struct(VERIFY_WRITE, rt_sf, rt_sf_addr, 1))
4395
        goto sigsegv;
4396

    
4397
    signal = current_exec_domain_sig(sig);
4398

    
4399
    err |= copy_siginfo_to_user(&rt_sf->info, info);
4400

    
4401
    err |= __put_user(0, &rt_sf->uc.tuc_flags);
4402
    err |= __put_user(0, &rt_sf->uc.tuc_link);
4403
    err |= __put_user((target_ulong)target_sigaltstack_used.ss_sp,
4404
                      &rt_sf->uc.tuc_stack.ss_sp);
4405
    err |= __put_user(sas_ss_flags(env->gpr[1]),
4406
                      &rt_sf->uc.tuc_stack.ss_flags);
4407
    err |= __put_user(target_sigaltstack_used.ss_size,
4408
                      &rt_sf->uc.tuc_stack.ss_size);
4409
    err |= __put_user(h2g (&rt_sf->uc.tuc_mcontext),
4410
                      &rt_sf->uc.tuc_regs);
4411
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4412
        err |= __put_user(set->sig[i], &rt_sf->uc.tuc_sigmask.sig[i]);
4413
    }
4414

    
4415
    frame = &rt_sf->uc.tuc_mcontext;
4416
    err |= save_user_regs(env, frame, TARGET_NR_rt_sigreturn);
4417

    
4418
    /* The kernel checks for the presence of a VDSO here.  We don't
4419
       emulate a vdso, so use a sigreturn system call.  */
4420
    env->lr = (target_ulong) h2g(frame->tramp);
4421

    
4422
    /* Turn off all fp exceptions.  */
4423
    env->fpscr = 0;
4424

    
4425
    /* Create a stack frame for the caller of the handler.  */
4426
    newsp = rt_sf_addr - (SIGNAL_FRAMESIZE + 16);
4427
    err |= __put_user(env->gpr[1], (target_ulong *)(uintptr_t) newsp);
4428

    
4429
    if (err)
4430
        goto sigsegv;
4431

    
4432
    /* Set up registers for signal handler.  */
4433
    env->gpr[1] = newsp;
4434
    env->gpr[3] = (target_ulong) signal;
4435
    env->gpr[4] = (target_ulong) h2g(&rt_sf->info);
4436
    env->gpr[5] = (target_ulong) h2g(&rt_sf->uc);
4437
    env->gpr[6] = (target_ulong) h2g(rt_sf);
4438
    env->nip = (target_ulong) ka->_sa_handler;
4439
    /* Signal handlers are entered in big-endian mode.  */
4440
    env->msr &= ~MSR_LE;
4441

    
4442
    unlock_user_struct(rt_sf, rt_sf_addr, 1);
4443
    return;
4444

    
4445
sigsegv:
4446
    unlock_user_struct(rt_sf, rt_sf_addr, 1);
4447
    if (logfile)
4448
        fprintf (logfile, "segfaulting from setup_rt_frame\n");
4449
    force_sig(TARGET_SIGSEGV);
4450

    
4451
}
4452

    
4453
long do_sigreturn(CPUState *env)
4454
{
4455
    struct target_sigcontext *sc = NULL;
4456
    struct target_mcontext *sr = NULL;
4457
    target_ulong sr_addr, sc_addr;
4458
    sigset_t blocked;
4459
    target_sigset_t set;
4460

    
4461
    sc_addr = env->gpr[1] + SIGNAL_FRAMESIZE;
4462
    if (!lock_user_struct(VERIFY_READ, sc, sc_addr, 1))
4463
        goto sigsegv;
4464

    
4465
#if defined(TARGET_PPC64)
4466
    set.sig[0] = sc->oldmask + ((long)(sc->_unused[3]) << 32);
4467
#else
4468
    if(__get_user(set.sig[0], &sc->oldmask) ||
4469
       __get_user(set.sig[1], &sc->_unused[3]))
4470
       goto sigsegv;
4471
#endif
4472
    target_to_host_sigset_internal(&blocked, &set);
4473
    sigprocmask(SIG_SETMASK, &blocked, NULL);
4474

    
4475
    if (__get_user(sr_addr, &sc->regs))
4476
        goto sigsegv;
4477
    if (!lock_user_struct(VERIFY_READ, sr, sr_addr, 1))
4478
        goto sigsegv;
4479
    if (restore_user_regs(env, sr, 1))
4480
        goto sigsegv;
4481

    
4482
    unlock_user_struct(sr, sr_addr, 1);
4483
    unlock_user_struct(sc, sc_addr, 1);
4484
    return -TARGET_QEMU_ESIGRETURN;
4485

    
4486
sigsegv:
4487
    unlock_user_struct(sr, sr_addr, 1);
4488
    unlock_user_struct(sc, sc_addr, 1);
4489
    if (logfile)
4490
        fprintf (logfile, "segfaulting from do_sigreturn\n");
4491
    force_sig(TARGET_SIGSEGV);
4492
    return 0;
4493
}
4494

    
4495
/* See arch/powerpc/kernel/signal_32.c.  */
4496
static int do_setcontext(struct target_ucontext *ucp, CPUState *env, int sig)
4497
{
4498
    struct target_mcontext *mcp;
4499
    target_ulong mcp_addr;
4500
    sigset_t blocked;
4501
    target_sigset_t set;
4502

    
4503
    if (copy_from_user(&set, h2g(ucp) + offsetof(struct target_ucontext, tuc_sigmask),
4504
                       sizeof (set)))
4505
        return 1;
4506

    
4507
#if defined(TARGET_PPC64)
4508
    fprintf (stderr, "do_setcontext: not implemented\n");
4509
    return 0;
4510
#else
4511
    if (__get_user(mcp_addr, &ucp->tuc_regs))
4512
        return 1;
4513

    
4514
    if (!lock_user_struct(VERIFY_READ, mcp, mcp_addr, 1))
4515
        return 1;
4516

    
4517
    target_to_host_sigset_internal(&blocked, &set);
4518
    sigprocmask(SIG_SETMASK, &blocked, NULL);
4519
    if (restore_user_regs(env, mcp, sig))
4520
        goto sigsegv;
4521

    
4522
    unlock_user_struct(mcp, mcp_addr, 1);
4523
    return 0;
4524

    
4525
sigsegv:
4526
    unlock_user_struct(mcp, mcp_addr, 1);
4527
    return 1;
4528
#endif
4529
}
4530

    
4531
long do_rt_sigreturn(CPUState *env)
4532
{
4533
    struct target_rt_sigframe *rt_sf = NULL;
4534
    target_ulong rt_sf_addr;
4535

    
4536
    rt_sf_addr = env->gpr[1] + SIGNAL_FRAMESIZE + 16;
4537
    if (!lock_user_struct(VERIFY_READ, rt_sf, rt_sf_addr, 1))
4538
        goto sigsegv;
4539

    
4540
    if (do_setcontext(&rt_sf->uc, env, 1))
4541
        goto sigsegv;
4542

    
4543
    do_sigaltstack(rt_sf_addr
4544
                   + offsetof(struct target_rt_sigframe, uc.tuc_stack),
4545
                   0, env->gpr[1]);
4546

    
4547
    unlock_user_struct(rt_sf, rt_sf_addr, 1);
4548
    return -TARGET_QEMU_ESIGRETURN;
4549

    
4550
sigsegv:
4551
    unlock_user_struct(rt_sf, rt_sf_addr, 1);
4552
    if (logfile)
4553
        fprintf (logfile, "segfaulting from do_rt_sigreturn\n");
4554
    force_sig(TARGET_SIGSEGV);
4555
    return 0;
4556
}
4557

    
4558
#elif defined(TARGET_M68K)
4559

    
4560
struct target_sigcontext {
4561
    abi_ulong  sc_mask;
4562
    abi_ulong  sc_usp;
4563
    abi_ulong  sc_d0;
4564
    abi_ulong  sc_d1;
4565
    abi_ulong  sc_a0;
4566
    abi_ulong  sc_a1;
4567
    unsigned short sc_sr;
4568
    abi_ulong  sc_pc;
4569
};
4570

    
4571
struct target_sigframe
4572
{
4573
    abi_ulong pretcode;
4574
    int sig;
4575
    int code;
4576
    abi_ulong psc;
4577
    char retcode[8];
4578
    abi_ulong extramask[TARGET_NSIG_WORDS-1];
4579
    struct target_sigcontext sc;
4580
};
4581
 
4582
typedef int target_greg_t;
4583
#define TARGET_NGREG 18
4584
typedef target_greg_t target_gregset_t[TARGET_NGREG];
4585

    
4586
typedef struct target_fpregset {
4587
    int f_fpcntl[3];
4588
    int f_fpregs[8*3];
4589
} target_fpregset_t;
4590

    
4591
struct target_mcontext {
4592
    int version;
4593
    target_gregset_t gregs;
4594
    target_fpregset_t fpregs;
4595
};
4596

    
4597
#define TARGET_MCONTEXT_VERSION 2
4598

    
4599
struct target_ucontext {
4600
    abi_ulong tuc_flags;
4601
    abi_ulong tuc_link;
4602
    target_stack_t tuc_stack;
4603
    struct target_mcontext tuc_mcontext;
4604
    abi_long tuc_filler[80];
4605
    target_sigset_t tuc_sigmask;
4606
};
4607

    
4608
struct target_rt_sigframe
4609
{
4610
    abi_ulong pretcode;
4611
    int sig;
4612
    abi_ulong pinfo;
4613
    abi_ulong puc;
4614
    char retcode[8];
4615
    struct target_siginfo info;
4616
    struct target_ucontext uc;
4617
};
4618

    
4619
static int
4620
setup_sigcontext(struct target_sigcontext *sc, CPUState *env, abi_ulong mask)
4621
{
4622
    int err = 0;
4623

    
4624
    err |= __put_user(mask, &sc->sc_mask);
4625
    err |= __put_user(env->aregs[7], &sc->sc_usp);
4626
    err |= __put_user(env->dregs[0], &sc->sc_d0);
4627
    err |= __put_user(env->dregs[1], &sc->sc_d1);
4628
    err |= __put_user(env->aregs[0], &sc->sc_a0);
4629
    err |= __put_user(env->aregs[1], &sc->sc_a1);
4630
    err |= __put_user(env->sr, &sc->sc_sr);
4631
    err |= __put_user(env->pc, &sc->sc_pc);
4632

    
4633
    return err;
4634
}
4635

    
4636
static int
4637
restore_sigcontext(CPUState *env, struct target_sigcontext *sc, int *pd0)
4638
{
4639
    int err = 0;
4640
    int temp;
4641

    
4642
    err |= __get_user(env->aregs[7], &sc->sc_usp);
4643
    err |= __get_user(env->dregs[1], &sc->sc_d1);
4644
    err |= __get_user(env->aregs[0], &sc->sc_a0);
4645
    err |= __get_user(env->aregs[1], &sc->sc_a1);
4646
    err |= __get_user(env->pc, &sc->sc_pc);
4647
    err |= __get_user(temp, &sc->sc_sr);
4648
    env->sr = (env->sr & 0xff00) | (temp & 0xff);
4649

    
4650
    *pd0 = tswapl(sc->sc_d0);
4651

    
4652
    return err;
4653
}
4654

    
4655
/*
4656
 * Determine which stack to use..
4657
 */
4658
static inline abi_ulong
4659
get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
4660
{
4661
    unsigned long sp;
4662

    
4663
    sp = regs->aregs[7];
4664

    
4665
    /* This is the X/Open sanctioned signal stack switching.  */
4666
    if ((ka->sa_flags & TARGET_SA_ONSTACK) && (sas_ss_flags (sp) == 0)) {
4667
        sp = target_sigaltstack_used.ss_sp + target_sigaltstack_used.ss_size;
4668
    }
4669

    
4670
    return ((sp - frame_size) & -8UL);
4671
}
4672

    
4673
static void setup_frame(int sig, struct target_sigaction *ka,
4674
                        target_sigset_t *set, CPUState *env)
4675
{
4676
    struct target_sigframe *frame;
4677
    abi_ulong frame_addr;
4678
    abi_ulong retcode_addr;
4679
    abi_ulong sc_addr;
4680
    int err = 0;
4681
    int i;
4682

    
4683
    frame_addr = get_sigframe(ka, env, sizeof *frame);
4684
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4685
        goto give_sigsegv;
4686

    
4687
    err |= __put_user(sig, &frame->sig);
4688

    
4689
    sc_addr = frame_addr + offsetof(struct target_sigframe, sc);
4690
    err |= __put_user(sc_addr, &frame->psc);
4691

    
4692
    err |= setup_sigcontext(&frame->sc, env, set->sig[0]);
4693
    if (err)
4694
        goto give_sigsegv;
4695

    
4696
    for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4697
        if (__put_user(set->sig[i], &frame->extramask[i - 1]))
4698
            goto give_sigsegv;
4699
    }
4700

    
4701
    /* Set up to return from userspace.  */
4702

    
4703
    retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4704
    err |= __put_user(retcode_addr, &frame->pretcode);
4705

    
4706
    /* moveq #,d0; trap #0 */
4707

    
4708
    err |= __put_user(0x70004e40 + (TARGET_NR_sigreturn << 16),
4709
                      (long *)(frame->retcode));
4710

    
4711
    if (err)
4712
        goto give_sigsegv;
4713

    
4714
    /* Set up to return from userspace */
4715

    
4716
    env->aregs[7] = frame_addr;
4717
    env->pc = ka->_sa_handler;
4718

    
4719
    unlock_user_struct(frame, frame_addr, 1);
4720
    return;
4721

    
4722
give_sigsegv:
4723
    unlock_user_struct(frame, frame_addr, 1);
4724
    force_sig(TARGET_SIGSEGV);
4725
}
4726

    
4727
static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
4728
                                           CPUState *env)
4729
{
4730
    target_greg_t *gregs = uc->tuc_mcontext.gregs;
4731
    int err;
4732

    
4733
    err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
4734
    err |= __put_user(env->dregs[0], &gregs[0]);
4735
    err |= __put_user(env->dregs[1], &gregs[1]);
4736
    err |= __put_user(env->dregs[2], &gregs[2]);
4737
    err |= __put_user(env->dregs[3], &gregs[3]);
4738
    err |= __put_user(env->dregs[4], &gregs[4]);
4739
    err |= __put_user(env->dregs[5], &gregs[5]);
4740
    err |= __put_user(env->dregs[6], &gregs[6]);
4741
    err |= __put_user(env->dregs[7], &gregs[7]);
4742
    err |= __put_user(env->aregs[0], &gregs[8]);
4743
    err |= __put_user(env->aregs[1], &gregs[9]);
4744
    err |= __put_user(env->aregs[2], &gregs[10]);
4745
    err |= __put_user(env->aregs[3], &gregs[11]);
4746
    err |= __put_user(env->aregs[4], &gregs[12]);
4747
    err |= __put_user(env->aregs[5], &gregs[13]);
4748
    err |= __put_user(env->aregs[6], &gregs[14]);
4749
    err |= __put_user(env->aregs[7], &gregs[15]);
4750
    err |= __put_user(env->pc, &gregs[16]);
4751
    err |= __put_user(env->sr, &gregs[17]);
4752

    
4753
    return err;
4754
}
4755
 
4756
static inline int target_rt_restore_ucontext(CPUState *env,
4757
                                             struct target_ucontext *uc,
4758
                                             int *pd0)
4759
{
4760
    int temp;
4761
    int err;
4762
    target_greg_t *gregs = uc->tuc_mcontext.gregs;
4763
    
4764
    err = __get_user(temp, &uc->tuc_mcontext.version);
4765
    if (temp != TARGET_MCONTEXT_VERSION)
4766
        goto badframe;
4767

    
4768
    /* restore passed registers */
4769
    err |= __get_user(env->dregs[0], &gregs[0]);
4770
    err |= __get_user(env->dregs[1], &gregs[1]);
4771
    err |= __get_user(env->dregs[2], &gregs[2]);
4772
    err |= __get_user(env->dregs[3], &gregs[3]);
4773
    err |= __get_user(env->dregs[4], &gregs[4]);
4774
    err |= __get_user(env->dregs[5], &gregs[5]);
4775
    err |= __get_user(env->dregs[6], &gregs[6]);
4776
    err |= __get_user(env->dregs[7], &gregs[7]);
4777
    err |= __get_user(env->aregs[0], &gregs[8]);
4778
    err |= __get_user(env->aregs[1], &gregs[9]);
4779
    err |= __get_user(env->aregs[2], &gregs[10]);
4780
    err |= __get_user(env->aregs[3], &gregs[11]);
4781
    err |= __get_user(env->aregs[4], &gregs[12]);
4782
    err |= __get_user(env->aregs[5], &gregs[13]);
4783
    err |= __get_user(env->aregs[6], &gregs[14]);
4784
    err |= __get_user(env->aregs[7], &gregs[15]);
4785
    err |= __get_user(env->pc, &gregs[16]);
4786
    err |= __get_user(temp, &gregs[17]);
4787
    env->sr = (env->sr & 0xff00) | (temp & 0xff);
4788

    
4789
    *pd0 = env->dregs[0];
4790
    return err;
4791

    
4792
badframe:
4793
    return 1;
4794
}
4795

    
4796
static void setup_rt_frame(int sig, struct target_sigaction *ka,
4797
                           target_siginfo_t *info,
4798
                           target_sigset_t *set, CPUState *env)
4799
{
4800
    struct target_rt_sigframe *frame;
4801
    abi_ulong frame_addr;
4802
    abi_ulong retcode_addr;
4803
    abi_ulong info_addr;
4804
    abi_ulong uc_addr;
4805
    int err = 0;
4806
    int i;
4807

    
4808
    frame_addr = get_sigframe(ka, env, sizeof *frame);
4809
    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0))
4810
        goto give_sigsegv;
4811

    
4812
    err |= __put_user(sig, &frame->sig);
4813

    
4814
    info_addr = frame_addr + offsetof(struct target_rt_sigframe, info);
4815
    err |= __put_user(info_addr, &frame->pinfo);
4816

    
4817
    uc_addr = frame_addr + offsetof(struct target_rt_sigframe, uc);
4818
    err |= __put_user(uc_addr, &frame->puc);
4819

    
4820
    err |= copy_siginfo_to_user(&frame->info, info);
4821

    
4822
    /* Create the ucontext */
4823

    
4824
    err |= __put_user(0, &frame->uc.tuc_flags);
4825
    err |= __put_user(0, &frame->uc.tuc_link);
4826
    err |= __put_user(target_sigaltstack_used.ss_sp,
4827
                      &frame->uc.tuc_stack.ss_sp);
4828
    err |= __put_user(sas_ss_flags(env->aregs[7]),
4829
                      &frame->uc.tuc_stack.ss_flags);
4830
    err |= __put_user(target_sigaltstack_used.ss_size,
4831
                      &frame->uc.tuc_stack.ss_size);
4832
    err |= target_rt_setup_ucontext(&frame->uc, env);
4833

    
4834
    if (err)
4835
            goto give_sigsegv;
4836

    
4837
    for(i = 0; i < TARGET_NSIG_WORDS; i++) {
4838
        if (__put_user(set->sig[i], &frame->uc.tuc_sigmask.sig[i]))
4839
            goto give_sigsegv;
4840
    }
4841

    
4842
    /* Set up to return from userspace.  */
4843

    
4844
    retcode_addr = frame_addr + offsetof(struct target_sigframe, retcode);
4845
    err |= __put_user(retcode_addr, &frame->pretcode);
4846

    
4847
    /* moveq #,d0; notb d0; trap #0 */
4848

    
4849
    err |= __put_user(0x70004600 + ((TARGET_NR_rt_sigreturn ^ 0xff) << 16),
4850
                      (long *)(frame->retcode + 0));
4851
    err |= __put_user(0x4e40, (short *)(frame->retcode + 4));
4852

    
4853
    if (err)
4854
        goto give_sigsegv;
4855

    
4856
    /* Set up to return from userspace */
4857

    
4858
    env->aregs[7] = frame_addr;
4859
    env->pc = ka->_sa_handler;
4860

    
4861
    unlock_user_struct(frame, frame_addr, 1);
4862
    return;
4863

    
4864
give_sigsegv:
4865
    unlock_user_struct(frame, frame_addr, 1);
4866
    force_sig(TARGET_SIGSEGV);
4867
}
4868

    
4869
long do_sigreturn(CPUState *env)
4870
{
4871
    struct target_sigframe *frame;
4872
    abi_ulong frame_addr = env->aregs[7] - 4;
4873
    target_sigset_t target_set;
4874
    sigset_t set;
4875
    int d0, i;
4876

    
4877
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4878
        goto badframe;
4879

    
4880
    /* set blocked signals */
4881

    
4882
    if (__get_user(target_set.sig[0], &frame->sc.sc_mask))
4883
        goto badframe;
4884

    
4885
    for(i = 1; i < TARGET_NSIG_WORDS; i++) {
4886
        if (__get_user(target_set.sig[i], &frame->extramask[i - 1]))
4887
            goto badframe;
4888
    }
4889

    
4890
    target_to_host_sigset_internal(&set, &target_set);
4891
    sigprocmask(SIG_SETMASK, &set, NULL);
4892

    
4893
    /* restore registers */
4894

    
4895
    if (restore_sigcontext(env, &frame->sc, &d0))
4896
        goto badframe;
4897

    
4898
    unlock_user_struct(frame, frame_addr, 0);
4899
    return d0;
4900

    
4901
badframe:
4902
    unlock_user_struct(frame, frame_addr, 0);
4903
    force_sig(TARGET_SIGSEGV);
4904
    return 0;
4905
}
4906

    
4907
long do_rt_sigreturn(CPUState *env)
4908
{
4909
    struct target_rt_sigframe *frame;
4910
    abi_ulong frame_addr = env->aregs[7] - 4;
4911
    target_sigset_t target_set;
4912
    sigset_t set;
4913
    int d0;
4914

    
4915
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
4916
        goto badframe;
4917

    
4918
    target_to_host_sigset_internal(&set, &target_set);
4919
    sigprocmask(SIG_SETMASK, &set, NULL);
4920

    
4921
    /* restore registers */
4922

    
4923
    if (target_rt_restore_ucontext(env, &frame->uc, &d0))
4924
        goto badframe;
4925

    
4926
    if (do_sigaltstack(frame_addr +
4927
                       offsetof(struct target_rt_sigframe, uc.tuc_stack),
4928
                       0, get_sp_from_cpustate(env)) == -EFAULT)
4929
        goto badframe;
4930

    
4931
    unlock_user_struct(frame, frame_addr, 0);
4932
    return d0;
4933

    
4934
badframe:
4935
    unlock_user_struct(frame, frame_addr, 0);
4936
    force_sig(TARGET_SIGSEGV);
4937
    return 0;
4938
}
4939

    
4940
#elif defined(TARGET_ALPHA)
4941

    
4942
struct target_sigcontext {
4943
    abi_long sc_onstack;
4944
    abi_long sc_mask;
4945
    abi_long sc_pc;
4946
    abi_long sc_ps;
4947
    abi_long sc_regs[32];
4948
    abi_long sc_ownedfp;
4949
    abi_long sc_fpregs[32];
4950
    abi_ulong sc_fpcr;
4951
    abi_ulong sc_fp_control;
4952
    abi_ulong sc_reserved1;
4953
    abi_ulong sc_reserved2;
4954
    abi_ulong sc_ssize;
4955
    abi_ulong sc_sbase;
4956
    abi_ulong sc_traparg_a0;
4957
    abi_ulong sc_traparg_a1;
4958
    abi_ulong sc_traparg_a2;
4959
    abi_ulong sc_fp_trap_pc;
4960
    abi_ulong sc_fp_trigger_sum;
4961
    abi_ulong sc_fp_trigger_inst;
4962
};
4963

    
4964
struct target_ucontext {
4965
    abi_ulong tuc_flags;
4966
    abi_ulong tuc_link;
4967
    abi_ulong tuc_osf_sigmask;
4968
    target_stack_t tuc_stack;
4969
    struct target_sigcontext tuc_mcontext;
4970
    target_sigset_t tuc_sigmask;
4971
};
4972

    
4973
struct target_sigframe {
4974
    struct target_sigcontext sc;
4975
    unsigned int retcode[3];
4976
};
4977

    
4978
struct target_rt_sigframe {
4979
    target_siginfo_t info;
4980
    struct target_ucontext uc;
4981
    unsigned int retcode[3];
4982
};
4983

    
4984
#define INSN_MOV_R30_R16        0x47fe0410
4985
#define INSN_LDI_R0             0x201f0000
4986
#define INSN_CALLSYS            0x00000083
4987

    
4988
static int setup_sigcontext(struct target_sigcontext *sc, CPUState *env,
4989
                            abi_ulong frame_addr, target_sigset_t *set)
4990
{
4991
    int i, err = 0;
4992

    
4993
    err |= __put_user(on_sig_stack(frame_addr), &sc->sc_onstack);
4994
    err |= __put_user(set->sig[0], &sc->sc_mask);
4995
    err |= __put_user(env->pc, &sc->sc_pc);
4996