xref: /qemu/linux-user/s390x/signal.c (revision 4d6d8a05)
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 "qemu/osdep.h"
20 #include "qemu.h"
21 #include "user-internals.h"
22 #include "signal-common.h"
23 #include "linux-user/trace.h"
24 #include "vdso-asmoffset.h"
25 
26 #define __NUM_GPRS 16
27 #define __NUM_FPRS 16
28 #define __NUM_ACRS 16
29 
30 #define _SIGCONTEXT_NSIG        64
31 #define _SIGCONTEXT_NSIG_BPW    64 /* FIXME: 31-bit mode -> 32 */
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 
36 typedef struct {
37     target_psw_t psw;
38     abi_ulong gprs[__NUM_GPRS];
39     abi_uint acrs[__NUM_ACRS];
40 } target_s390_regs_common;
41 
42 typedef struct {
43     uint32_t fpc;
44     uint32_t pad;
45     uint64_t fprs[__NUM_FPRS];
46 } target_s390_fp_regs;
47 
48 typedef struct {
49     target_s390_regs_common regs;
50     target_s390_fp_regs     fpregs;
51 } target_sigregs;
52 
53 typedef struct {
54     uint64_t vxrs_low[16];
55     uint64_t vxrs_high[16][2];
56     uint8_t reserved[128];
57 } target_sigregs_ext;
58 
59 typedef struct {
60     abi_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
61     abi_ulong sregs;
62 } target_sigcontext;
63 
64 typedef struct {
65     uint8_t callee_used_stack[STACK_FRAME_OVERHEAD];
66     target_sigcontext sc;
67     target_sigregs sregs;
68     int signo;
69     target_sigregs_ext sregs_ext;
70 } sigframe;
71 
72 #define TARGET_UC_VXRS 2
73 
74 struct target_ucontext {
75     abi_ulong tuc_flags;
76     abi_ulong tuc_link;
77     target_stack_t tuc_stack;
78     target_sigregs tuc_mcontext;
79     target_sigset_t tuc_sigmask;
80     uint8_t reserved[128 - sizeof(target_sigset_t)];
81     target_sigregs_ext tuc_mcontext_ext;
82 };
83 
84 typedef struct {
85     uint8_t callee_used_stack[STACK_FRAME_OVERHEAD];
86     /*
87      * This field is no longer initialized by the kernel, but it's still a part
88      * of the ABI.
89      */
90     uint16_t svc_insn;
91     struct target_siginfo info;
92     struct target_ucontext uc;
93 } rt_sigframe;
94 
95 static inline abi_ulong
get_sigframe(struct target_sigaction * ka,CPUS390XState * env,size_t frame_size)96 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
97 {
98     abi_ulong sp;
99 
100     /* Default to using normal stack */
101     sp = get_sp_from_cpustate(env);
102 
103     /* This is the X/Open sanctioned signal stack switching.  */
104     if (ka->sa_flags & TARGET_SA_ONSTACK) {
105         sp = target_sigsp(sp, ka);
106     }
107 
108     /* This is the legacy signal stack switching. */
109     else if (/* FIXME !user_mode(regs) */ 0 &&
110              !(ka->sa_flags & TARGET_SA_RESTORER) &&
111              ka->sa_restorer) {
112         sp = (abi_ulong) ka->sa_restorer;
113     }
114 
115     return (sp - frame_size) & -8ul;
116 }
117 
118 #define PSW_USER_BITS   (PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \
119                          PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_ASC_PRIMARY)
120 #define PSW_MASK_USER   (PSW_MASK_ASC | PSW_MASK_CC | PSW_MASK_PM | \
121                          PSW_MASK_64 | PSW_MASK_32)
122 
save_sigregs(CPUS390XState * env,target_sigregs * sregs)123 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
124 {
125     uint64_t psw_mask = s390_cpu_get_psw_mask(env);
126     int i;
127 
128     /*
129      * Copy a 'clean' PSW mask to the user to avoid leaking
130      * information about whether PER is currently on.
131      * TODO: qemu does not support PSW_MASK_RI; it will never be set.
132      */
133     psw_mask = PSW_USER_BITS | (psw_mask & PSW_MASK_USER);
134     __put_user(psw_mask, &sregs->regs.psw.mask);
135     __put_user(env->psw.addr, &sregs->regs.psw.addr);
136 
137     for (i = 0; i < 16; i++) {
138         __put_user(env->regs[i], &sregs->regs.gprs[i]);
139     }
140     for (i = 0; i < 16; i++) {
141         __put_user(env->aregs[i], &sregs->regs.acrs[i]);
142     }
143 
144     /*
145      * We have to store the fp registers to current->thread.fp_regs
146      * to merge them with the emulated registers.
147      */
148     __put_user(env->fpc, &sregs->fpregs.fpc);
149     for (i = 0; i < 16; i++) {
150         __put_user(*get_freg(env, i), &sregs->fpregs.fprs[i]);
151     }
152 }
153 
save_sigregs_ext(CPUS390XState * env,target_sigregs_ext * ext)154 static void save_sigregs_ext(CPUS390XState *env, target_sigregs_ext *ext)
155 {
156     int i;
157 
158     /*
159      * if (MACHINE_HAS_VX) ...
160      * That said, we always allocate the stack storage and the
161      * space is always available in env.
162      */
163     for (i = 0; i < 16; ++i) {
164         __put_user(env->vregs[i][1], &ext->vxrs_low[i]);
165     }
166     for (i = 0; i < 16; ++i) {
167         __put_user(env->vregs[i + 16][0], &ext->vxrs_high[i][0]);
168         __put_user(env->vregs[i + 16][1], &ext->vxrs_high[i][1]);
169     }
170 }
171 
setup_frame(int sig,struct target_sigaction * ka,target_sigset_t * set,CPUS390XState * env)172 void setup_frame(int sig, struct target_sigaction *ka,
173                  target_sigset_t *set, CPUS390XState *env)
174 {
175     sigframe *frame;
176     abi_ulong frame_addr;
177     abi_ulong restorer;
178 
179     frame_addr = get_sigframe(ka, env, sizeof(*frame));
180     trace_user_setup_frame(env, frame_addr);
181     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
182         force_sigsegv(sig);
183         return;
184     }
185 
186     /* Set up backchain. */
187     __put_user(env->regs[15], (abi_ulong *) frame);
188 
189     /* Create struct sigcontext on the signal stack. */
190     /* Make sure that we're initializing all of oldmask. */
191     QEMU_BUILD_BUG_ON(ARRAY_SIZE(frame->sc.oldmask) != 1);
192     __put_user(set->sig[0], &frame->sc.oldmask[0]);
193     __put_user(frame_addr + offsetof(sigframe, sregs), &frame->sc.sregs);
194 
195     /* Create _sigregs on the signal stack */
196     save_sigregs(env, &frame->sregs);
197 
198     /*
199      * ??? The kernel uses regs->gprs[2] here, which is not yet the signo.
200      * Moreover the comment talks about allowing backtrace, which is really
201      * done by the r15 copy above.
202      */
203     __put_user(sig, &frame->signo);
204 
205     /* Create sigregs_ext on the signal stack. */
206     save_sigregs_ext(env, &frame->sregs_ext);
207 
208     /*
209      * Set up to return from userspace.
210      * If provided, use a stub already in userspace.
211      */
212     if (ka->sa_flags & TARGET_SA_RESTORER) {
213         restorer = ka->sa_restorer;
214     } else {
215         restorer = default_sigreturn;
216     }
217 
218     /* Set up registers for signal handler */
219     env->regs[14] = restorer;
220     env->regs[15] = frame_addr;
221     /* Force default amode and default user address space control. */
222     env->psw.mask = PSW_MASK_64 | PSW_MASK_32 | PSW_ASC_PRIMARY
223                   | (env->psw.mask & ~PSW_MASK_ASC);
224     env->psw.addr = ka->_sa_handler;
225 
226     env->regs[2] = sig;
227     env->regs[3] = frame_addr + offsetof(typeof(*frame), sc);
228 
229     /*
230      * We forgot to include these in the sigcontext.
231      * To avoid breaking binary compatibility, they are passed as args.
232      */
233     env->regs[4] = 0; /* FIXME: regs->int_code & 127 */
234     env->regs[5] = 0; /* FIXME: regs->int_parm_long */
235     env->regs[6] = 0; /* FIXME: current->thread.last_break */
236 
237     unlock_user_struct(frame, frame_addr, 1);
238 }
239 
setup_rt_frame(int sig,struct target_sigaction * ka,target_siginfo_t * info,target_sigset_t * set,CPUS390XState * env)240 void setup_rt_frame(int sig, struct target_sigaction *ka,
241                     target_siginfo_t *info,
242                     target_sigset_t *set, CPUS390XState *env)
243 {
244     rt_sigframe *frame;
245     abi_ulong frame_addr;
246     abi_ulong restorer;
247     abi_ulong uc_flags;
248 
249     frame_addr = get_sigframe(ka, env, sizeof *frame);
250     trace_user_setup_rt_frame(env, frame_addr);
251     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
252         force_sigsegv(sig);
253         return;
254     }
255 
256     /* Set up backchain. */
257     __put_user(env->regs[15], (abi_ulong *) frame);
258 
259     /*
260      * Set up to return from userspace.
261      * If provided, use a stub already in userspace.
262      */
263     if (ka->sa_flags & TARGET_SA_RESTORER) {
264         restorer = ka->sa_restorer;
265     } else {
266         restorer = default_rt_sigreturn;
267     }
268 
269     /* Create siginfo on the signal stack. */
270     frame->info = *info;
271 
272     /* Create ucontext on the signal stack. */
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     /* Set up registers for signal handler */
285     env->regs[14] = restorer;
286     env->regs[15] = frame_addr;
287     /* Force default amode and default user address space control. */
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; /* FIXME: current->thread.last_break */
296 }
297 
restore_sigregs(CPUS390XState * env,target_sigregs * sc)298 static 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      * Use current psw.mask to preserve PER bit.
314      * TODO:
315      *  if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW_MASK_RI))
316      *          return -EINVAL;
317      * Simply do not allow it to be set in mask.
318      */
319     prev_mask = s390_cpu_get_psw_mask(env);
320     mask = (prev_mask & ~PSW_MASK_USER) | (mask & PSW_MASK_USER);
321     /* Check for invalid user address space control. */
322     if ((mask & PSW_MASK_ASC) == PSW_ASC_HOME) {
323         mask = (mask & ~PSW_MASK_ASC) | PSW_ASC_PRIMARY;
324     }
325     /* Check for invalid amode. */
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     __get_user(env->fpc, &sc->fpregs.fpc);
335     for (i = 0; i < 16; i++) {
336         __get_user(*get_freg(env, i), &sc->fpregs.fprs[i]);
337     }
338 }
339 
restore_sigregs_ext(CPUS390XState * env,target_sigregs_ext * ext)340 static void restore_sigregs_ext(CPUS390XState *env, target_sigregs_ext *ext)
341 {
342     int i;
343 
344     /*
345      * if (MACHINE_HAS_VX) ...
346      * That said, we always allocate the stack storage and the
347      * space is always available in env.
348      */
349     for (i = 0; i < 16; ++i) {
350         __get_user(env->vregs[i][1], &ext->vxrs_low[i]);
351     }
352     for (i = 0; i < 16; ++i) {
353         __get_user(env->vregs[i + 16][0], &ext->vxrs_high[i][0]);
354         __get_user(env->vregs[i + 16][1], &ext->vxrs_high[i][1]);
355     }
356 }
357 
do_sigreturn(CPUS390XState * env)358 long do_sigreturn(CPUS390XState *env)
359 {
360     sigframe *frame;
361     abi_ulong frame_addr = env->regs[15];
362     target_sigset_t target_set;
363     sigset_t set;
364 
365     trace_user_do_sigreturn(env, frame_addr);
366     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
367         force_sig(TARGET_SIGSEGV);
368         return -QEMU_ESIGRETURN;
369     }
370 
371     /* Make sure that we're initializing all of target_set. */
372     QEMU_BUILD_BUG_ON(ARRAY_SIZE(target_set.sig) != 1);
373     __get_user(target_set.sig[0], &frame->sc.oldmask[0]);
374 
375     target_to_host_sigset_internal(&set, &target_set);
376     set_sigmask(&set); /* ~_BLOCKABLE? */
377 
378     restore_sigregs(env, &frame->sregs);
379     restore_sigregs_ext(env, &frame->sregs_ext);
380 
381     unlock_user_struct(frame, frame_addr, 0);
382     return -QEMU_ESIGRETURN;
383 }
384 
do_rt_sigreturn(CPUS390XState * env)385 long do_rt_sigreturn(CPUS390XState *env)
386 {
387     rt_sigframe *frame;
388     abi_ulong frame_addr = env->regs[15];
389     sigset_t set;
390 
391     trace_user_do_rt_sigreturn(env, frame_addr);
392     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
393         force_sig(TARGET_SIGSEGV);
394         return -QEMU_ESIGRETURN;
395     }
396     target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
397 
398     set_sigmask(&set); /* ~_BLOCKABLE? */
399 
400     restore_sigregs(env, &frame->uc.tuc_mcontext);
401     restore_sigregs_ext(env, &frame->uc.tuc_mcontext_ext);
402 
403     target_restore_altstack(&frame->uc.tuc_stack, env);
404 
405     unlock_user_struct(frame, frame_addr, 0);
406     return -QEMU_ESIGRETURN;
407 }
408 
setup_sigtramp(abi_ulong sigtramp_page)409 void setup_sigtramp(abi_ulong sigtramp_page)
410 {
411     uint16_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 2 + 2, 0);
412     assert(tramp != NULL);
413 
414     default_sigreturn = sigtramp_page;
415     __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn, &tramp[0]);
416 
417     default_rt_sigreturn = sigtramp_page + 2;
418     __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn, &tramp[1]);
419 
420     unlock_user(tramp, sigtramp_page, 2 + 2);
421 }
422