xref: /openbmc/qemu/linux-user/s390x/signal.c (revision d43f1670)
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 
25 #define __NUM_GPRS 16
26 #define __NUM_FPRS 16
27 #define __NUM_ACRS 16
28 
29 #define __SIGNAL_FRAMESIZE      160 /* FIXME: 31-bit mode -> 96 */
30 
31 #define _SIGCONTEXT_NSIG        64
32 #define _SIGCONTEXT_NSIG_BPW    64 /* FIXME: 31-bit mode -> 32 */
33 #define _SIGCONTEXT_NSIG_WORDS  (_SIGCONTEXT_NSIG / _SIGCONTEXT_NSIG_BPW)
34 #define _SIGMASK_COPY_SIZE    (sizeof(unsigned long)*_SIGCONTEXT_NSIG_WORDS)
35 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
36 
37 typedef struct {
38     target_psw_t psw;
39     abi_ulong gprs[__NUM_GPRS];
40     abi_uint acrs[__NUM_ACRS];
41 } target_s390_regs_common;
42 
43 typedef struct {
44     uint32_t fpc;
45     uint32_t pad;
46     uint64_t fprs[__NUM_FPRS];
47 } target_s390_fp_regs;
48 
49 typedef struct {
50     target_s390_regs_common regs;
51     target_s390_fp_regs     fpregs;
52 } target_sigregs;
53 
54 typedef struct {
55     uint64_t vxrs_low[16];
56     uint64_t vxrs_high[16][2];
57     uint8_t reserved[128];
58 } target_sigregs_ext;
59 
60 typedef struct {
61     abi_ulong oldmask[_SIGCONTEXT_NSIG_WORDS];
62     abi_ulong sregs;
63 } target_sigcontext;
64 
65 typedef struct {
66     uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
67     target_sigcontext sc;
68     target_sigregs sregs;
69     int signo;
70     target_sigregs_ext sregs_ext;
71     uint16_t retcode;
72 } sigframe;
73 
74 #define TARGET_UC_VXRS 2
75 
76 struct target_ucontext {
77     abi_ulong tuc_flags;
78     abi_ulong tuc_link;
79     target_stack_t tuc_stack;
80     target_sigregs tuc_mcontext;
81     target_sigset_t tuc_sigmask;
82     uint8_t reserved[128 - sizeof(target_sigset_t)];
83     target_sigregs_ext tuc_mcontext_ext;
84 };
85 
86 typedef struct {
87     uint8_t callee_used_stack[__SIGNAL_FRAMESIZE];
88     uint16_t retcode;
89     struct target_siginfo info;
90     struct target_ucontext uc;
91 } rt_sigframe;
92 
93 static inline abi_ulong
94 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
95 {
96     abi_ulong sp;
97 
98     /* Default to using normal stack */
99     sp = get_sp_from_cpustate(env);
100 
101     /* This is the X/Open sanctioned signal stack switching.  */
102     if (ka->sa_flags & TARGET_SA_ONSTACK) {
103         sp = target_sigsp(sp, ka);
104     }
105 
106     /* This is the legacy signal stack switching. */
107     else if (/* FIXME !user_mode(regs) */ 0 &&
108              !(ka->sa_flags & TARGET_SA_RESTORER) &&
109              ka->sa_restorer) {
110         sp = (abi_ulong) ka->sa_restorer;
111     }
112 
113     return (sp - frame_size) & -8ul;
114 }
115 
116 #define PSW_USER_BITS   (PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \
117                          PSW_MASK_MCHECK | PSW_MASK_PSTATE | PSW_ASC_PRIMARY)
118 #define PSW_MASK_USER   (PSW_MASK_ASC | PSW_MASK_CC | PSW_MASK_PM | \
119                          PSW_MASK_64 | PSW_MASK_32)
120 
121 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
122 {
123     uint64_t psw_mask = s390_cpu_get_psw_mask(env);
124     int i;
125 
126     /*
127      * Copy a 'clean' PSW mask to the user to avoid leaking
128      * information about whether PER is currently on.
129      * TODO: qemu does not support PSW_MASK_RI; it will never be set.
130      */
131     psw_mask = PSW_USER_BITS | (psw_mask & PSW_MASK_USER);
132     __put_user(psw_mask, &sregs->regs.psw.mask);
133     __put_user(env->psw.addr, &sregs->regs.psw.addr);
134 
135     for (i = 0; i < 16; i++) {
136         __put_user(env->regs[i], &sregs->regs.gprs[i]);
137     }
138     for (i = 0; i < 16; i++) {
139         __put_user(env->aregs[i], &sregs->regs.acrs[i]);
140     }
141 
142     /*
143      * We have to store the fp registers to current->thread.fp_regs
144      * to merge them with the emulated registers.
145      */
146     for (i = 0; i < 16; i++) {
147         __put_user(*get_freg(env, i), &sregs->fpregs.fprs[i]);
148     }
149 }
150 
151 static void save_sigregs_ext(CPUS390XState *env, target_sigregs_ext *ext)
152 {
153     int i;
154 
155     /*
156      * if (MACHINE_HAS_VX) ...
157      * That said, we always allocate the stack storage and the
158      * space is always available in env.
159      */
160     for (i = 0; i < 16; ++i) {
161         __put_user(env->vregs[i][1], &ext->vxrs_low[i]);
162     }
163     for (i = 0; i < 16; ++i) {
164         __put_user(env->vregs[i + 16][0], &ext->vxrs_high[i][0]);
165         __put_user(env->vregs[i + 16][1], &ext->vxrs_high[i][1]);
166     }
167 }
168 
169 void setup_frame(int sig, struct target_sigaction *ka,
170                  target_sigset_t *set, CPUS390XState *env)
171 {
172     sigframe *frame;
173     abi_ulong frame_addr;
174     abi_ulong restorer;
175 
176     frame_addr = get_sigframe(ka, env, sizeof(*frame));
177     trace_user_setup_frame(env, frame_addr);
178     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
179         force_sigsegv(sig);
180         return;
181     }
182 
183     /* Set up backchain. */
184     __put_user(env->regs[15], (abi_ulong *) frame);
185 
186     /* Create struct sigcontext on the signal stack. */
187     /* Make sure that we're initializing all of oldmask. */
188     QEMU_BUILD_BUG_ON(ARRAY_SIZE(frame->sc.oldmask) != 1);
189     __put_user(set->sig[0], &frame->sc.oldmask[0]);
190     __put_user(frame_addr + offsetof(sigframe, sregs), &frame->sc.sregs);
191 
192     /* Create _sigregs on the signal stack */
193     save_sigregs(env, &frame->sregs);
194 
195     /*
196      * ??? The kernel uses regs->gprs[2] here, which is not yet the signo.
197      * Moreover the comment talks about allowing backtrace, which is really
198      * done by the r15 copy above.
199      */
200     __put_user(sig, &frame->signo);
201 
202     /* Create sigregs_ext on the signal stack. */
203     save_sigregs_ext(env, &frame->sregs_ext);
204 
205     /*
206      * Set up to return from userspace.
207      * If provided, use a stub already in userspace.
208      */
209     if (ka->sa_flags & TARGET_SA_RESTORER) {
210         restorer = ka->sa_restorer;
211     } else {
212         restorer = frame_addr + offsetof(sigframe, retcode);
213         __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
214                    &frame->retcode);
215     }
216 
217     /* Set up registers for signal handler */
218     env->regs[14] = restorer;
219     env->regs[15] = frame_addr;
220     /* Force default amode and default user address space control. */
221     env->psw.mask = PSW_MASK_64 | PSW_MASK_32 | PSW_ASC_PRIMARY
222                   | (env->psw.mask & ~PSW_MASK_ASC);
223     env->psw.addr = ka->_sa_handler;
224 
225     env->regs[2] = sig;
226     env->regs[3] = frame_addr + offsetof(typeof(*frame), sc);
227 
228     /*
229      * We forgot to include these in the sigcontext.
230      * To avoid breaking binary compatibility, they are passed as args.
231      */
232     env->regs[4] = 0; /* FIXME: regs->int_code & 127 */
233     env->regs[5] = 0; /* FIXME: regs->int_parm_long */
234     env->regs[6] = 0; /* FIXME: current->thread.last_break */
235 
236     unlock_user_struct(frame, frame_addr, 1);
237 }
238 
239 void setup_rt_frame(int sig, struct target_sigaction *ka,
240                     target_siginfo_t *info,
241                     target_sigset_t *set, CPUS390XState *env)
242 {
243     rt_sigframe *frame;
244     abi_ulong frame_addr;
245     abi_ulong restorer;
246     abi_ulong uc_flags;
247 
248     frame_addr = get_sigframe(ka, env, sizeof *frame);
249     trace_user_setup_rt_frame(env, frame_addr);
250     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
251         force_sigsegv(sig);
252         return;
253     }
254 
255     /* Set up backchain. */
256     __put_user(env->regs[15], (abi_ulong *) frame);
257 
258     /*
259      * Set up to return from userspace.
260      * If provided, use a stub already in userspace.
261      */
262     if (ka->sa_flags & TARGET_SA_RESTORER) {
263         restorer = ka->sa_restorer;
264     } else {
265         restorer = frame_addr + offsetof(typeof(*frame), retcode);
266         __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
267                    &frame->retcode);
268     }
269 
270     /* Create siginfo on the signal stack. */
271     tswap_siginfo(&frame->info, info);
272 
273     /* Create ucontext on the signal stack. */
274     uc_flags = 0;
275     if (s390_has_feat(S390_FEAT_VECTOR)) {
276         uc_flags |= TARGET_UC_VXRS;
277     }
278     __put_user(uc_flags, &frame->uc.tuc_flags);
279     __put_user(0, &frame->uc.tuc_link);
280     target_save_altstack(&frame->uc.tuc_stack, env);
281     save_sigregs(env, &frame->uc.tuc_mcontext);
282     save_sigregs_ext(env, &frame->uc.tuc_mcontext_ext);
283     tswap_sigset(&frame->uc.tuc_sigmask, set);
284 
285     /* Set up registers for signal handler */
286     env->regs[14] = restorer;
287     env->regs[15] = frame_addr;
288     /* Force default amode and default user address space control. */
289     env->psw.mask = PSW_MASK_64 | PSW_MASK_32 | PSW_ASC_PRIMARY
290                   | (env->psw.mask & ~PSW_MASK_ASC);
291     env->psw.addr = ka->_sa_handler;
292 
293     env->regs[2] = sig;
294     env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
295     env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
296     env->regs[5] = 0; /* FIXME: current->thread.last_break */
297 }
298 
299 static void restore_sigregs(CPUS390XState *env, target_sigregs *sc)
300 {
301     uint64_t prev_addr, prev_mask, mask, addr;
302     int i;
303 
304     for (i = 0; i < 16; i++) {
305         __get_user(env->regs[i], &sc->regs.gprs[i]);
306     }
307 
308     prev_addr = env->psw.addr;
309     __get_user(mask, &sc->regs.psw.mask);
310     __get_user(addr, &sc->regs.psw.addr);
311     trace_user_s390x_restore_sigregs(env, addr, prev_addr);
312 
313     /*
314      * Use current psw.mask to preserve PER bit.
315      * TODO:
316      *  if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW_MASK_RI))
317      *          return -EINVAL;
318      * Simply do not allow it to be set in mask.
319      */
320     prev_mask = s390_cpu_get_psw_mask(env);
321     mask = (prev_mask & ~PSW_MASK_USER) | (mask & PSW_MASK_USER);
322     /* Check for invalid user address space control. */
323     if ((mask & PSW_MASK_ASC) == PSW_ASC_HOME) {
324         mask = (mask & ~PSW_MASK_ASC) | PSW_ASC_PRIMARY;
325     }
326     /* Check for invalid amode. */
327     if (mask & PSW_MASK_64) {
328         mask |= PSW_MASK_32;
329     }
330     s390_cpu_set_psw(env, mask, addr);
331 
332     for (i = 0; i < 16; i++) {
333         __get_user(env->aregs[i], &sc->regs.acrs[i]);
334     }
335     for (i = 0; i < 16; i++) {
336         __get_user(*get_freg(env, i), &sc->fpregs.fprs[i]);
337     }
338 }
339 
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 
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 -TARGET_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 -TARGET_QEMU_ESIGRETURN;
383 }
384 
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 -TARGET_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 -TARGET_QEMU_ESIGRETURN;
407 }
408