summaryrefslogtreecommitdiff
path: root/linux-user/hexagon/signal.c
blob: 85eab5e94382d5d9a238aaf67ebce58847a7bb59 (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
/*
 *  Emulation of Linux signals
 *
 *  Copyright (c) 2003 Fabrice Bellard
 *  Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
 *
 *  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 "qemu/osdep.h"
#include "qemu.h"
#include "signal-common.h"
#include "linux-user/trace.h"

struct target_sigcontext {
    target_ulong r0,  r1,  r2,  r3;
    target_ulong r4,  r5,  r6,  r7;
    target_ulong r8,  r9, r10, r11;
    target_ulong r12, r13, r14, r15;
    target_ulong r16, r17, r18, r19;
    target_ulong r20, r21, r22, r23;
    target_ulong r24, r25, r26, r27;
    target_ulong r28, r29, r30, r31;
    target_ulong sa0;
    target_ulong lc0;
    target_ulong sa1;
    target_ulong lc1;
    target_ulong m0;
    target_ulong m1;
    target_ulong usr;
    target_ulong p3_0;
    target_ulong gp;
    target_ulong ugp;
    target_ulong pc;
    target_ulong cause;
    target_ulong badva;
    target_ulong pad1;
    target_ulong pad2;
    target_ulong pad3;
};

struct target_ucontext {
    unsigned long uc_flags;
    target_ulong uc_link; /* target pointer */
    target_stack_t uc_stack;
    struct target_sigcontext uc_mcontext;
    target_sigset_t uc_sigmask;
};

struct target_rt_sigframe {
    uint32_t tramp[2];
    struct target_siginfo info;
    struct target_ucontext uc;
};

static abi_ulong get_sigframe(struct target_sigaction *ka,
                              CPUHexagonState *regs, size_t framesize)
{
    abi_ulong sp = get_sp_from_cpustate(regs);

    /* This is the X/Open sanctioned signal stack switching.  */
    sp = target_sigsp(sp, ka) - framesize;

    sp = QEMU_ALIGN_DOWN(sp, 8);

    return sp;
}

static void setup_sigcontext(struct target_sigcontext *sc, CPUHexagonState *env)
{
    __put_user(env->gpr[HEX_REG_R00], &sc->r0);
    __put_user(env->gpr[HEX_REG_R01], &sc->r1);
    __put_user(env->gpr[HEX_REG_R02], &sc->r2);
    __put_user(env->gpr[HEX_REG_R03], &sc->r3);
    __put_user(env->gpr[HEX_REG_R04], &sc->r4);
    __put_user(env->gpr[HEX_REG_R05], &sc->r5);
    __put_user(env->gpr[HEX_REG_R06], &sc->r6);
    __put_user(env->gpr[HEX_REG_R07], &sc->r7);
    __put_user(env->gpr[HEX_REG_R08], &sc->r8);
    __put_user(env->gpr[HEX_REG_R09], &sc->r9);
    __put_user(env->gpr[HEX_REG_R10], &sc->r10);
    __put_user(env->gpr[HEX_REG_R11], &sc->r11);
    __put_user(env->gpr[HEX_REG_R12], &sc->r12);
    __put_user(env->gpr[HEX_REG_R13], &sc->r13);
    __put_user(env->gpr[HEX_REG_R14], &sc->r14);
    __put_user(env->gpr[HEX_REG_R15], &sc->r15);
    __put_user(env->gpr[HEX_REG_R16], &sc->r16);
    __put_user(env->gpr[HEX_REG_R17], &sc->r17);
    __put_user(env->gpr[HEX_REG_R18], &sc->r18);
    __put_user(env->gpr[HEX_REG_R19], &sc->r19);
    __put_user(env->gpr[HEX_REG_R20], &sc->r20);
    __put_user(env->gpr[HEX_REG_R21], &sc->r21);
    __put_user(env->gpr[HEX_REG_R22], &sc->r22);
    __put_user(env->gpr[HEX_REG_R23], &sc->r23);
    __put_user(env->gpr[HEX_REG_R24], &sc->r24);
    __put_user(env->gpr[HEX_REG_R25], &sc->r25);
    __put_user(env->gpr[HEX_REG_R26], &sc->r26);
    __put_user(env->gpr[HEX_REG_R27], &sc->r27);
    __put_user(env->gpr[HEX_REG_R28], &sc->r28);
    __put_user(env->gpr[HEX_REG_R29], &sc->r29);
    __put_user(env->gpr[HEX_REG_R30], &sc->r30);
    __put_user(env->gpr[HEX_REG_R31], &sc->r31);
    __put_user(env->gpr[HEX_REG_SA0], &sc->sa0);
    __put_user(env->gpr[HEX_REG_LC0], &sc->lc0);
    __put_user(env->gpr[HEX_REG_SA1], &sc->sa1);
    __put_user(env->gpr[HEX_REG_LC1], &sc->lc1);
    __put_user(env->gpr[HEX_REG_M0], &sc->m0);
    __put_user(env->gpr[HEX_REG_M1], &sc->m1);
    __put_user(env->gpr[HEX_REG_USR], &sc->usr);
    __put_user(env->gpr[HEX_REG_P3_0], &sc->p3_0);
    __put_user(env->gpr[HEX_REG_GP], &sc->gp);
    __put_user(env->gpr[HEX_REG_UGP], &sc->ugp);
    __put_user(env->gpr[HEX_REG_PC], &sc->pc);
}

static void setup_ucontext(struct target_ucontext *uc,
                           CPUHexagonState *env, target_sigset_t *set)
{
    __put_user(0,    &(uc->uc_flags));
    __put_user(0,    &(uc->uc_link));

    target_save_altstack(&uc->uc_stack, env);

    int i;
    for (i = 0; i < TARGET_NSIG_WORDS; i++) {
        __put_user(set->sig[i], &(uc->uc_sigmask.sig[i]));
    }

    setup_sigcontext(&uc->uc_mcontext, env);
}

static inline void install_sigtramp(uint32_t *tramp)
{
    __put_user(0x7800d166, tramp + 0); /*  { r6=#__NR_rt_sigreturn } */
    __put_user(0x5400c004, tramp + 1); /*  { trap0(#1) } */
}

void setup_rt_frame(int sig, struct target_sigaction *ka,
                    target_siginfo_t *info,
                    target_sigset_t *set, CPUHexagonState *env)
{
    abi_ulong frame_addr;
    struct target_rt_sigframe *frame;

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

    if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
        goto badframe;
    }

    setup_ucontext(&frame->uc, env, set);
    tswap_siginfo(&frame->info, info);
    install_sigtramp(frame->tramp);

    env->gpr[HEX_REG_PC] = ka->_sa_handler;
    env->gpr[HEX_REG_SP] = frame_addr;
    env->gpr[HEX_REG_R00] = sig;
    env->gpr[HEX_REG_R01] =
        frame_addr + offsetof(struct target_rt_sigframe, info);
    env->gpr[HEX_REG_R02] =
        frame_addr + offsetof(struct target_rt_sigframe, uc);
    env->gpr[HEX_REG_LR] =
        frame_addr + offsetof(struct target_rt_sigframe, tramp);

    return;

badframe:
    unlock_user_struct(frame, frame_addr, 1);
    if (sig == TARGET_SIGSEGV) {
        ka->_sa_handler = TARGET_SIG_DFL;
    }
    force_sig(TARGET_SIGSEGV);
}

static void restore_sigcontext(CPUHexagonState *env,
                               struct target_sigcontext *sc)
{
    __get_user(env->gpr[HEX_REG_R00], &sc->r0);
    __get_user(env->gpr[HEX_REG_R01], &sc->r1);
    __get_user(env->gpr[HEX_REG_R02], &sc->r2);
    __get_user(env->gpr[HEX_REG_R03], &sc->r3);
    __get_user(env->gpr[HEX_REG_R04], &sc->r4);
    __get_user(env->gpr[HEX_REG_R05], &sc->r5);
    __get_user(env->gpr[HEX_REG_R06], &sc->r6);
    __get_user(env->gpr[HEX_REG_R07], &sc->r7);
    __get_user(env->gpr[HEX_REG_R08], &sc->r8);
    __get_user(env->gpr[HEX_REG_R09], &sc->r9);
    __get_user(env->gpr[HEX_REG_R10], &sc->r10);
    __get_user(env->gpr[HEX_REG_R11], &sc->r11);
    __get_user(env->gpr[HEX_REG_R12], &sc->r12);
    __get_user(env->gpr[HEX_REG_R13], &sc->r13);
    __get_user(env->gpr[HEX_REG_R14], &sc->r14);
    __get_user(env->gpr[HEX_REG_R15], &sc->r15);
    __get_user(env->gpr[HEX_REG_R16], &sc->r16);
    __get_user(env->gpr[HEX_REG_R17], &sc->r17);
    __get_user(env->gpr[HEX_REG_R18], &sc->r18);
    __get_user(env->gpr[HEX_REG_R19], &sc->r19);
    __get_user(env->gpr[HEX_REG_R20], &sc->r20);
    __get_user(env->gpr[HEX_REG_R21], &sc->r21);
    __get_user(env->gpr[HEX_REG_R22], &sc->r22);
    __get_user(env->gpr[HEX_REG_R23], &sc->r23);
    __get_user(env->gpr[HEX_REG_R24], &sc->r24);
    __get_user(env->gpr[HEX_REG_R25], &sc->r25);
    __get_user(env->gpr[HEX_REG_R26], &sc->r26);
    __get_user(env->gpr[HEX_REG_R27], &sc->r27);
    __get_user(env->gpr[HEX_REG_R28], &sc->r28);
    __get_user(env->gpr[HEX_REG_R29], &sc->r29);
    __get_user(env->gpr[HEX_REG_R30], &sc->r30);
    __get_user(env->gpr[HEX_REG_R31], &sc->r31);
    __get_user(env->gpr[HEX_REG_SA0], &sc->sa0);
    __get_user(env->gpr[HEX_REG_LC0], &sc->lc0);
    __get_user(env->gpr[HEX_REG_SA1], &sc->sa1);
    __get_user(env->gpr[HEX_REG_LC1], &sc->lc1);
    __get_user(env->gpr[HEX_REG_M0], &sc->m0);
    __get_user(env->gpr[HEX_REG_M1], &sc->m1);
    __get_user(env->gpr[HEX_REG_USR], &sc->usr);
    __get_user(env->gpr[HEX_REG_P3_0], &sc->p3_0);
    __get_user(env->gpr[HEX_REG_GP], &sc->gp);
    __get_user(env->gpr[HEX_REG_UGP], &sc->ugp);
    __get_user(env->gpr[HEX_REG_PC], &sc->pc);
}

static void restore_ucontext(CPUHexagonState *env, struct target_ucontext *uc)
{
    sigset_t blocked;
    target_sigset_t target_set;
    int i;

    target_sigemptyset(&target_set);
    for (i = 0; i < TARGET_NSIG_WORDS; i++) {
        __get_user(target_set.sig[i], &(uc->uc_sigmask.sig[i]));
    }

    target_to_host_sigset_internal(&blocked, &target_set);
    set_sigmask(&blocked);

    restore_sigcontext(env, &uc->uc_mcontext);
}

long do_rt_sigreturn(CPUHexagonState *env)
{
    struct target_rt_sigframe *frame;
    abi_ulong frame_addr;

    frame_addr = env->gpr[HEX_REG_SP];
    trace_user_do_sigreturn(env, frame_addr);
    if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
        goto badframe;
    }

    restore_ucontext(env, &frame->uc);
    target_restore_altstack(&frame->uc.uc_stack, env);

    unlock_user_struct(frame, frame_addr, 0);
    return -TARGET_QEMU_ESIGRETURN;

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