1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "qemu/osdep.h"
20#include "qemu.h"
21#include "signal-common.h"
22#include "linux-user/trace.h"
23
24#define __NUM_GPRS 16
25#define __NUM_FPRS 16
26#define __NUM_ACRS 16
27
28#define __SIGNAL_FRAMESIZE 160
29
30#define _SIGCONTEXT_NSIG 64
31#define _SIGCONTEXT_NSIG_BPW 64
32#define _SIGCONTEXT_NSIG_WORDS (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
33#define _SIGMASK_COPY_SIZE (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
34#define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
35
36typedef struct {
37 target_psw_t psw;
38 abi_ulong gprs[__NUM_GPRS];
39 abi_uint acrs[__NUM_ACRS];
40} target_s390_regs_common;
41
42typedef struct {
43 uint32_t fpc;
44 uint32_t pad;
45 uint64_t fprs[__NUM_FPRS];
46} target_s390_fp_regs;
47
48typedef struct {
49 target_s390_regs_common regs;
50 target_s390_fp_regs fpregs;
51} target_sigregs;
52
53typedef struct {
54 uint64_t vxrs_low[16];
55 uint64_t vxrs_high[16][2];
56 uint8_t reserved[128];
57} target_sigregs_ext;
58
59typedef struct {
60 abi_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
61 abi_ulong sregs;
62} target_sigcontext;
63
64typedef struct {
65 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
66 target_sigcontext sc;
67 target_sigregs sregs;
68 int signo;
69 target_sigregs_ext sregs_ext;
70 uint16_t retcode;
71} sigframe;
72
73#define TARGET_UC_VXRS 2
74
75struct target_ucontext {
76 abi_ulong tuc_flags;
77 abi_ulong tuc_link;
78 target_stack_t tuc_stack;
79 target_sigregs tuc_mcontext;
80 target_sigset_t tuc_sigmask;
81 uint8_t reserved[128 - sizeof(target_sigset_t)];
82 target_sigregs_ext tuc_mcontext_ext;
83};
84
85typedef struct {
86 uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
87 uint16_t retcode;
88 struct target_siginfo info;
89 struct target_ucontext uc;
90} rt_sigframe;
91
92static inline abi_ulong
93get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
94{
95 abi_ulong sp;
96
97
98 sp = get_sp_from_cpustate(env);
99
100
101 if (ka->sa_flags & TARGET_SA_ONSTACK) {
102 sp = target_sigsp(sp, ka);
103 }
104
105
106 else if ( 0 &&
107 !(ka->sa_flags & TARGET_SA_RESTORER) &&
108 ka->sa_restorer) {
109 sp = (abi_ulong) ka->sa_restorer;
110 }
111
112 return (sp - frame_size) & -8ul;
113}
114
115#define PSW_USER_BITS (PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \
116 PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_ASC_PRIMARY)
117#define PSW_MASK_USER (PSW_MASK_ASC | PSW_MASK_CC | PSW_MASK_PM | \
118 PSW_MASK_64 | PSW_MASK_32)
119
120static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
121{
122 uint64_t psw_mask = s390_cpu_get_psw_mask(env);
123 int i;
124
125
126
127
128
129
130 psw_mask = PSW_USER_BITS | (psw_mask & PSW_MASK_USER);
131 __put_user(psw_mask, &sregs->regs.psw.mask);
132 __put_user(env->psw.addr, &sregs->regs.psw.addr);
133
134 for (i = 0; i < 16; i++) {
135 __put_user(env->regs[i], &sregs->regs.gprs[i]);
136 }
137 for (i = 0; i < 16; i++) {
138 __put_user(env->aregs[i], &sregs->regs.acrs[i]);
139 }
140
141
142
143
144
145 for (i = 0; i < 16; i++) {
146 __put_user(*get_freg(env, i), &sregs->fpregs.fprs[i]);
147 }
148}
149
150static void save_sigregs_ext(CPUS390XState *env, target_sigregs_ext *ext)
151{
152 int i;
153
154
155
156
157
158
159 for (i = 0; i < 16; ++i) {
160 __put_user(env->vregs[i][1], &ext->vxrs_low[i]);
161 }
162 for (i = 0; i < 16; ++i) {
163 __put_user(env->vregs[i + 16][0], &ext->vxrs_high[i][0]);
164 __put_user(env->vregs[i + 16][1], &ext->vxrs_high[i][1]);
165 }
166}
167
168void setup_frame(int sig, struct target_sigaction *ka,
169 target_sigset_t *set, CPUS390XState *env)
170{
171 sigframe *frame;
172 abi_ulong frame_addr;
173 abi_ulong restorer;
174
175 frame_addr = get_sigframe(ka, env, sizeof(*frame));
176 trace_user_setup_frame(env, frame_addr);
177 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
178 force_sigsegv(sig);
179 return;
180 }
181
182
183 __put_user(env->regs[15], (abi_ulong *) frame);
184
185
186
187 QEMU_BUILD_BUG_ON(ARRAY_SIZE(frame->sc.oldmask) != 1);
188 __put_user(set->sig[0], &frame->sc.oldmask[0]);
189 __put_user(frame_addr + offsetof(sigframe, sregs), &frame->sc.sregs);
190
191
192 save_sigregs(env, &frame->sregs);
193
194
195
196
197
198
199 __put_user(sig, &frame->signo);
200
201
202 save_sigregs_ext(env, &frame->sregs_ext);
203
204
205
206
207
208 if (ka->sa_flags & TARGET_SA_RESTORER) {
209 restorer = ka->sa_restorer;
210 } else {
211 restorer = frame_addr + offsetof(sigframe, retcode);
212 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
213 &frame->retcode);
214 }
215
216
217 env->regs[14] = restorer;
218 env->regs[15] = frame_addr;
219
220 env->psw.mask = PSW_MASK_64 | PSW_MASK_32 | PSW_ASC_PRIMARY
221 | (env->psw.mask & ~PSW_MASK_ASC);
222 env->psw.addr = ka->_sa_handler;
223
224 env->regs[2] = sig;
225 env->regs[3] = frame_addr + offsetof(typeof(*frame), sc);
226
227
228
229
230
231 env->regs[4] = 0;
232 env->regs[5] = 0;
233 env->regs[6] = 0;
234
235 unlock_user_struct(frame, frame_addr, 1);
236}
237
238void setup_rt_frame(int sig, struct target_sigaction *ka,
239 target_siginfo_t *info,
240 target_sigset_t *set, CPUS390XState *env)
241{
242 rt_sigframe *frame;
243 abi_ulong frame_addr;
244 abi_ulong restorer;
245 abi_ulong uc_flags;
246
247 frame_addr = get_sigframe(ka, env, sizeof *frame);
248 trace_user_setup_rt_frame(env, frame_addr);
249 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
250 force_sigsegv(sig);
251 return;
252 }
253
254
255 __put_user(env->regs[15], (abi_ulong *) frame);
256
257
258
259
260
261 if (ka->sa_flags & TARGET_SA_RESTORER) {
262 restorer = ka->sa_restorer;
263 } else {
264 restorer = frame_addr + offsetof(typeof(*frame), retcode);
265 __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
266 &frame->retcode);
267 }
268
269
270 tswap_siginfo(&frame->info, info);
271
272
273 uc_flags = 0;
274 if (s390_has_feat(S390_FEAT_VECTOR)) {
275 uc_flags |= TARGET_UC_VXRS;
276 }
277 __put_user(uc_flags, &frame->uc.tuc_flags);
278 __put_user(0, &frame->uc.tuc_link);
279 target_save_altstack(&frame->uc.tuc_stack, env);
280 save_sigregs(env, &frame->uc.tuc_mcontext);
281 save_sigregs_ext(env, &frame->uc.tuc_mcontext_ext);
282 tswap_sigset(&frame->uc.tuc_sigmask, set);
283
284
285 env->regs[14] = restorer;
286 env->regs[15] = frame_addr;
287
288 env->psw.mask = PSW_MASK_64 | PSW_MASK_32 | PSW_ASC_PRIMARY
289 | (env->psw.mask & ~PSW_MASK_ASC);
290 env->psw.addr = ka->_sa_handler;
291
292 env->regs[2] = sig;
293 env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
294 env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
295 env->regs[5] = 0;
296}
297
298static void restore_sigregs(CPUS390XState *env, target_sigregs *sc)
299{
300 uint64_t prev_addr, prev_mask, mask, addr;
301 int i;
302
303 for (i = 0; i < 16; i++) {
304 __get_user(env->regs[i], &sc->regs.gprs[i]);
305 }
306
307 prev_addr = env->psw.addr;
308 __get_user(mask, &sc->regs.psw.mask);
309 __get_user(addr, &sc->regs.psw.addr);
310 trace_user_s390x_restore_sigregs(env, addr, prev_addr);
311
312
313
314
315
316
317
318
319 prev_mask = s390_cpu_get_psw_mask(env);
320 mask = (prev_mask & ~PSW_MASK_USER) | (mask & PSW_MASK_USER);
321
322 if ((mask & PSW_MASK_ASC) == PSW_ASC_HOME) {
323 mask = (mask & ~PSW_MASK_ASC) | PSW_ASC_PRIMARY;
324 }
325
326 if (mask & PSW_MASK_64) {
327 mask |= PSW_MASK_32;
328 }
329 s390_cpu_set_psw(env, mask, addr);
330
331 for (i = 0; i < 16; i++) {
332 __get_user(env->aregs[i], &sc->regs.acrs[i]);
333 }
334 for (i = 0; i < 16; i++) {
335 __get_user(*get_freg(env, i), &sc->fpregs.fprs[i]);
336 }
337}
338
339static void restore_sigregs_ext(CPUS390XState *env, target_sigregs_ext *ext)
340{
341 int i;
342
343
344
345
346
347
348 for (i = 0; i < 16; ++i) {
349 __get_user(env->vregs[i][1], &ext->vxrs_low[i]);
350 }
351 for (i = 0; i < 16; ++i) {
352 __get_user(env->vregs[i + 16][0], &ext->vxrs_high[i][0]);
353 __get_user(env->vregs[i + 16][1], &ext->vxrs_high[i][1]);
354 }
355}
356
357long do_sigreturn(CPUS390XState *env)
358{
359 sigframe *frame;
360 abi_ulong frame_addr = env->regs[15];
361 target_sigset_t target_set;
362 sigset_t set;
363
364 trace_user_do_sigreturn(env, frame_addr);
365 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
366 force_sig(TARGET_SIGSEGV);
367 return -TARGET_QEMU_ESIGRETURN;
368 }
369
370
371 QEMU_BUILD_BUG_ON(ARRAY_SIZE(target_set.sig) != 1);
372 __get_user(target_set.sig[0], &frame->sc.oldmask[0]);
373
374 target_to_host_sigset_internal(&set, &target_set);
375 set_sigmask(&set);
376
377 restore_sigregs(env, &frame->sregs);
378 restore_sigregs_ext(env, &frame->sregs_ext);
379
380 unlock_user_struct(frame, frame_addr, 0);
381 return -TARGET_QEMU_ESIGRETURN;
382}
383
384long do_rt_sigreturn(CPUS390XState *env)
385{
386 rt_sigframe *frame;
387 abi_ulong frame_addr = env->regs[15];
388 sigset_t set;
389
390 trace_user_do_rt_sigreturn(env, frame_addr);
391 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
392 force_sig(TARGET_SIGSEGV);
393 return -TARGET_QEMU_ESIGRETURN;
394 }
395 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
396
397 set_sigmask(&set);
398
399 restore_sigregs(env, &frame->uc.tuc_mcontext);
400 restore_sigregs_ext(env, &frame->uc.tuc_mcontext_ext);
401
402 target_restore_altstack(&frame->uc.tuc_stack, env);
403
404 unlock_user_struct(frame, frame_addr, 0);
405 return -TARGET_QEMU_ESIGRETURN;
406}
407