aboutsummaryrefslogtreecommitdiff
path: root/linux-user/m68k/target_signal.c
blob: eb00532311153d28d45fb3d8473085460a5d3ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 *  Emulation of Linux signals : m68k specific code
 *
 *  Copyright (c) 2003 Fabrice Bellard
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <sys/ucontext.h>
#include <sys/resource.h>

#include "qemu.h"
#include "qemu-common.h"
#include "signal-common.h"
#include "target_signal.h"

struct target_sigcontext {
    abi_ulong  sc_mask;
    abi_ulong  sc_usp;
    abi_ulong  sc_d0;
    abi_ulong  sc_d1;
    abi_ulong  sc_a0;
    abi_ulong  sc_a1;
    unsigned short sc_sr;
    abi_ulong  sc_pc;
};

struct target_sigframe
{
    abi_ulong pretcode;
    int sig;
    int code;
    abi_ulong psc;
    char retcode[8];
    abi_ulong extramask[TARGET_NSIG_WORDS-1];
    struct target_sigcontext sc;
};
 
typedef int target_greg_t;
#define TARGET_NGREG 18
typedef target_greg_t target_gregset_t[TARGET_NGREG];

typedef struct target_fpregset {
    int f_fpcntl[3];
    int f_fpregs[8*3];
} target_fpregset_t;

struct target_mcontext {
    int version;
    target_gregset_t gregs;
    target_fpregset_t fpregs;
};

#define TARGET_MCONTEXT_VERSION 2

struct target_ucontext {
    abi_ulong tuc_flags;
    abi_ulong tuc_link;
    target_stack_t tuc_stack;
    struct target_mcontext tuc_mcontext;
    abi_long tuc_filler[80];
    target_sigset_t tuc_sigmask;
};

struct target_rt_sigframe
{
    abi_ulong pretcode;
    int sig;
    abi_ulong pinfo;
    abi_ulong puc;
    char retcode[8];
    struct target_siginfo info;
    struct target_ucontext uc;
};

static int
setup_sigcontext(struct target_sigcontext *sc, CPUState *env, abi_ulong mask)
{
    int err = 0;

    err |= __put_user(mask, &sc->sc_mask);
    err |= __put_user(env->aregs[7], &sc->sc_usp);
    err |= __put_user(env->dregs[0], &sc->sc_d0);
    err |= __put_user(env->dregs[1], &sc->sc_d1);
    err |= __put_user(env->aregs[0], &sc->sc_a0);
    err |= __put_user(env->aregs[1], &sc->sc_a1);
    err |= __put_user(env->sr, &sc->sc_sr);
    err |= __put_user(env->pc, &sc->sc_pc);

    return err;
}

static int
restore_sigcontext(CPUState *env, struct target_sigcontext *sc, int *pd0)
{
    int err = 0;
    int temp;

    err |= __get_user(env->aregs[7], &sc->sc_usp);
    err |= __get_user(env->dregs[1], &sc->sc_d1);
    err |= __get_user(env->aregs[0], &sc->sc_a0);
    err |= __get_user(env->aregs[1], &sc->sc_a1);
    err |= __get_user(env->pc, &sc->sc_pc);
    err |= __get_user(temp, &sc->sc_sr);
    env->sr = (env->sr & 0xff00) | (temp & 0xff);

    *pd0 = tswapl(sc->sc_d0);

    return err;
}

/*
 * Determine which stack to use..
 */
static inline abi_ulong
get_sigframe(struct target_sigaction *ka, CPUState *regs, size_t frame_size)
{
    unsigned long sp;

    sp = regs->aregs[7];

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

    return ((sp - frame_size) & -8UL);
}

void setup_frame(int sig, struct target_sigaction *ka,
                 target_sigset_t *set, CPUState *env)
{
    struct target_sigframe *frame;
    abi_ulong frame_addr;
    abi_ulong retcode_addr;
    abi_ulong sc_addr;
    int err = 0;
    int i;

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

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

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

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

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

    /* Set up to return from userspace.  */

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

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

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

    if (err)
        goto give_sigsegv;

    /* Set up to return from userspace */

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

    unlock_user_struct(frame, frame_addr, 1);
    return;

give_sigsegv:
    unlock_user_struct(frame, frame_addr, 1);
    force_sig(TARGET_SIGSEGV);
}

static inline int target_rt_setup_ucontext(struct target_ucontext *uc,
                                           CPUState *env)
{
    target_greg_t *gregs = uc->tuc_mcontext.gregs;
    int err;

    err = __put_user(TARGET_MCONTEXT_VERSION, &uc->tuc_mcontext.version);
    err |= __put_user(env->dregs[0], &gregs[0]);
    err |= __put_user(env->dregs[1], &gregs[1]);
    err |= __put_user(env->dregs[2], &gregs[2]);
    err |= __put_user(env->dregs[3], &gregs[3]);
    err |= __put_user(env->dregs[4], &gregs[4]);
    err |= __put_user(env->dregs[5], &gregs[5]);
    err |= __put_user(env->dregs[6], &gregs[6]);
    err |= __put_user(env->dregs[7], &gregs[7]);
    err |= __put_user(env->aregs[0], &gregs[8]);
    err |= __put_user(env->aregs[1], &gregs[9]);
    err |= __put_user(env->aregs[2], &gregs[10]);
    err |= __put_user(env->aregs[3], &gregs[11]);
    err |= __put_user(env->aregs[4], &gregs[12]);
    err |= __put_user(env->aregs[5], &gregs[13]);
    err |= __put_user(env->aregs[6], &gregs[14]);
    err |= __put_user(env->aregs[7], &gregs[15]);
    err |= __put_user(env->pc, &gregs[16]);
    err |= __put_user(env->sr, &gregs[17]);

    return err;
}
 
static inline int target_rt_restore_ucontext(CPUState *env,
                                             struct target_ucontext *uc,
                                             int *pd0)
{
    int temp;
    int err;
    target_greg_t *gregs = uc->tuc_mcontext.gregs;
    
    err = __get_user(temp, &uc->tuc_mcontext.version);
    if (temp != TARGET_MCONTEXT_VERSION)
        goto badframe;

    /* restore passed registers */
    err |= __get_user(env->dregs[0], &gregs[0]);
    err |= __get_user(env->dregs[1], &gregs[1]);
    err |= __get_user(env->dregs[2], &gregs[2]);
    err |= __get_user(env->dregs[3], &gregs[3]);
    err |= __get_user(env->dregs[4], &gregs[4]);
    err |= __get_user(env->dregs[5], &gregs[5]);
    err |= __get_user(env->dregs[6], &gregs[6]);
    err |= __get_user(env->dregs[7], &gregs[7]);
    err |= __get_user(env->aregs[0], &gregs[8]);
    err |= __get_user(env->aregs[1], &gregs[9]);
    err |= __get_user(env->aregs[2], &gregs[10]);
    err |= __get_user(env->aregs[3], &gregs[11]);
    err |= __get_user(env->aregs[4], &gregs[12]);
    err |= __get_user(env->aregs[5], &gregs[13]);
    err |= __get_user(env->aregs[6], &gregs[14]);
    err |= __get_user(env->aregs[7], &gregs[15]);
    err |= __get_user(env->pc, &gregs[16]);
    err |= __get_user(temp, &gregs[17]);
    env->sr = (env->sr & 0xff00) | (temp & 0xff);

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

badframe:
    return 1;
}

void setup_rt_frame(int sig, struct target_sigaction *ka,
                    target_siginfo_t *info,
                    target_sigset_t *set, CPUState *env)
{
    struct target_rt_sigframe *frame;
    abi_ulong frame_addr;
    abi_ulong retcode_addr;
    abi_ulong info_addr;
    abi_ulong uc_addr;
    int err = 0;
    int i;

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

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

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

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

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

    /* Create the ucontext */

    err |= __put_user(0, &frame->uc.tuc_flags);
    err |= __put_user(0, &frame->uc.tuc_link);
    err |= __put_user(target_sigaltstack_used.ss_sp,
                      &frame->uc.tuc_stack.ss_sp);
    err |= __put_user(sas_ss_flags(env->aregs[7]),
                      &frame->uc.tuc_stack.ss_flags);
    err |= __put_user(target_sigaltstack_used.ss_size,
                      &frame->uc.tuc_stack.ss_size);
    err |= target_rt_setup_ucontext(&frame->uc, env);

    if (err)
            goto give_sigsegv;

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

    /* Set up to return from userspace.  */

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

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

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

    if (err)
        goto give_sigsegv;

    /* Set up to return from userspace */

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

    unlock_user_struct(frame, frame_addr, 1);
    return;

give_sigsegv:
    unlock_user_struct(frame, frame_addr, 1);
    force_sig(TARGET_SIGSEGV);
}

long do_sigreturn(CPUState *env)
{
    struct target_sigframe *frame;
    abi_ulong frame_addr = env->aregs[7] - 4;
    target_sigset_t target_set;
    sigset_t set;
    int d0, i;

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

    /* set blocked signals */

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

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

    target_to_host_sigset_internal(&set, &target_set);
    sigprocmask(SIG_SETMASK, &set, NULL);

    /* restore registers */

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

    unlock_user_struct(frame, frame_addr, 0);
    return d0;

badframe:
    unlock_user_struct(frame, frame_addr, 0);
    force_sig(TARGET_SIGSEGV);
    return 0;
}

long do_rt_sigreturn(CPUState *env)
{
    struct target_rt_sigframe *frame;
    abi_ulong frame_addr = env->aregs[7] - 4;
    target_sigset_t target_set;
    sigset_t set;
    int d0;

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

    target_to_host_sigset_internal(&set, &target_set);
    sigprocmask(SIG_SETMASK, &set, NULL);

    /* restore registers */

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

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

    unlock_user_struct(frame, frame_addr, 0);
    return d0;

badframe:
    unlock_user_struct(frame, frame_addr, 0);
    force_sig(TARGET_SIGSEGV);
    return 0;
}