xref: /openbmc/qemu/linux-user/s390x/signal.c (revision 744c72a8)
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 "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 /* FIXME: 31-bit mode -> 96 */
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[__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 
75 struct 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 
85 typedef 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 
92 static inline abi_ulong
93 get_sigframe(struct target_sigaction *ka, CPUS390XState *env, size_t frame_size)
94 {
95     abi_ulong sp;
96 
97     /* Default to using normal stack */
98     sp = get_sp_from_cpustate(env);
99 
100     /* This is the X/Open sanctioned signal stack switching.  */
101     if (ka->sa_flags & TARGET_SA_ONSTACK) {
102         sp = target_sigsp(sp, ka);
103     }
104 
105     /* This is the legacy signal stack switching. */
106     else if (/* FIXME !user_mode(regs) */ 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 static void save_sigregs(CPUS390XState *env, target_sigregs *sregs)
116 {
117     int i;
118 
119     /*
120      * Copy a 'clean' PSW mask to the user to avoid leaking
121      * information about whether PER is currently on.
122      */
123     __put_user(env->psw.mask, &sregs->regs.psw.mask);
124     __put_user(env->psw.addr, &sregs->regs.psw.addr);
125 
126     for (i = 0; i < 16; i++) {
127         __put_user(env->regs[i], &sregs->regs.gprs[i]);
128     }
129     for (i = 0; i < 16; i++) {
130         __put_user(env->aregs[i], &sregs->regs.acrs[i]);
131     }
132 
133     /*
134      * We have to store the fp registers to current->thread.fp_regs
135      * to merge them with the emulated registers.
136      */
137     for (i = 0; i < 16; i++) {
138         __put_user(*get_freg(env, i), &sregs->fpregs.fprs[i]);
139     }
140 }
141 
142 static void save_sigregs_ext(CPUS390XState *env, target_sigregs_ext *ext)
143 {
144     int i;
145 
146     /*
147      * if (MACHINE_HAS_VX) ...
148      * That said, we always allocate the stack storage and the
149      * space is always available in env.
150      */
151     for (i = 0; i < 16; ++i) {
152         __put_user(env->vregs[i][1], &ext->vxrs_low[i]);
153     }
154     for (i = 0; i < 16; ++i) {
155         __put_user(env->vregs[i + 16][0], &ext->vxrs_high[i][0]);
156         __put_user(env->vregs[i + 16][1], &ext->vxrs_high[i][1]);
157     }
158 }
159 
160 void setup_frame(int sig, struct target_sigaction *ka,
161                  target_sigset_t *set, CPUS390XState *env)
162 {
163     sigframe *frame;
164     abi_ulong frame_addr;
165     abi_ulong restorer;
166 
167     frame_addr = get_sigframe(ka, env, sizeof(*frame));
168     trace_user_setup_frame(env, frame_addr);
169     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
170         force_sigsegv(sig);
171         return;
172     }
173 
174     /* Set up backchain. */
175     __put_user(env->regs[15], (abi_ulong *) frame);
176 
177     /* Create struct sigcontext on the signal stack. */
178     /* Make sure that we're initializing all of oldmask. */
179     QEMU_BUILD_BUG_ON(ARRAY_SIZE(frame->sc.oldmask) != 1);
180     __put_user(set->sig[0], &frame->sc.oldmask[0]);
181     __put_user(frame_addr + offsetof(sigframe, sregs), &frame->sc.sregs);
182 
183     /* Create _sigregs on the signal stack */
184     save_sigregs(env, &frame->sregs);
185 
186     /*
187      * ??? The kernel uses regs->gprs[2] here, which is not yet the signo.
188      * Moreover the comment talks about allowing backtrace, which is really
189      * done by the r15 copy above.
190      */
191     __put_user(sig, &frame->signo);
192 
193     /* Create sigregs_ext on the signal stack. */
194     save_sigregs_ext(env, &frame->sregs_ext);
195 
196     /*
197      * Set up to return from userspace.
198      * If provided, use a stub already in userspace.
199      */
200     if (ka->sa_flags & TARGET_SA_RESTORER) {
201         restorer = ka->sa_restorer;
202     } else {
203         restorer = frame_addr + offsetof(sigframe, retcode);
204         __put_user(S390_SYSCALL_OPCODE | TARGET_NR_sigreturn,
205                    &frame->retcode);
206     }
207 
208     /* Set up registers for signal handler */
209     env->regs[14] = restorer;
210     env->regs[15] = frame_addr;
211     /* Force default amode and default user address space control. */
212     env->psw.mask = PSW_MASK_64 | PSW_MASK_32 | PSW_ASC_PRIMARY
213                   | (env->psw.mask & ~PSW_MASK_ASC);
214     env->psw.addr = ka->_sa_handler;
215 
216     env->regs[2] = sig;
217     env->regs[3] = frame_addr + offsetof(typeof(*frame), sc);
218 
219     /*
220      * We forgot to include these in the sigcontext.
221      * To avoid breaking binary compatibility, they are passed as args.
222      */
223     env->regs[4] = 0; /* FIXME: regs->int_code & 127 */
224     env->regs[5] = 0; /* FIXME: regs->int_parm_long */
225     env->regs[6] = 0; /* FIXME: current->thread.last_break */
226 
227     unlock_user_struct(frame, frame_addr, 1);
228 }
229 
230 void setup_rt_frame(int sig, struct target_sigaction *ka,
231                     target_siginfo_t *info,
232                     target_sigset_t *set, CPUS390XState *env)
233 {
234     rt_sigframe *frame;
235     abi_ulong frame_addr;
236     abi_ulong restorer;
237     abi_ulong uc_flags;
238 
239     frame_addr = get_sigframe(ka, env, sizeof *frame);
240     trace_user_setup_rt_frame(env, frame_addr);
241     if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
242         force_sigsegv(sig);
243         return;
244     }
245 
246     /* Set up backchain. */
247     __put_user(env->regs[15], (abi_ulong *) frame);
248 
249     /*
250      * Set up to return from userspace.
251      * If provided, use a stub already in userspace.
252      */
253     if (ka->sa_flags & TARGET_SA_RESTORER) {
254         restorer = ka->sa_restorer;
255     } else {
256         restorer = frame_addr + offsetof(typeof(*frame), retcode);
257         __put_user(S390_SYSCALL_OPCODE | TARGET_NR_rt_sigreturn,
258                    &frame->retcode);
259     }
260 
261     /* Create siginfo on the signal stack. */
262     tswap_siginfo(&frame->info, info);
263 
264     /* Create ucontext on the signal stack. */
265     uc_flags = 0;
266     if (s390_has_feat(S390_FEAT_VECTOR)) {
267         uc_flags |= TARGET_UC_VXRS;
268     }
269     __put_user(uc_flags, &frame->uc.tuc_flags);
270     __put_user(0, &frame->uc.tuc_link);
271     target_save_altstack(&frame->uc.tuc_stack, env);
272     save_sigregs(env, &frame->uc.tuc_mcontext);
273     save_sigregs_ext(env, &frame->uc.tuc_mcontext_ext);
274     tswap_sigset(&frame->uc.tuc_sigmask, set);
275 
276     /* Set up registers for signal handler */
277     env->regs[14] = restorer;
278     env->regs[15] = frame_addr;
279     /* Force default amode and default user address space control. */
280     env->psw.mask = PSW_MASK_64 | PSW_MASK_32 | PSW_ASC_PRIMARY
281                   | (env->psw.mask & ~PSW_MASK_ASC);
282     env->psw.addr = ka->_sa_handler;
283 
284     env->regs[2] = sig;
285     env->regs[3] = frame_addr + offsetof(typeof(*frame), info);
286     env->regs[4] = frame_addr + offsetof(typeof(*frame), uc);
287     env->regs[5] = 0; /* FIXME: current->thread.last_break */
288 }
289 
290 static void restore_sigregs(CPUS390XState *env, target_sigregs *sc)
291 {
292     target_ulong prev_addr;
293     int i;
294 
295     for (i = 0; i < 16; i++) {
296         __get_user(env->regs[i], &sc->regs.gprs[i]);
297     }
298 
299     prev_addr = env->psw.addr;
300     __get_user(env->psw.mask, &sc->regs.psw.mask);
301     __get_user(env->psw.addr, &sc->regs.psw.addr);
302     trace_user_s390x_restore_sigregs(env, env->psw.addr, prev_addr);
303 
304     for (i = 0; i < 16; i++) {
305         __get_user(env->aregs[i], &sc->regs.acrs[i]);
306     }
307     for (i = 0; i < 16; i++) {
308         __get_user(*get_freg(env, i), &sc->fpregs.fprs[i]);
309     }
310 }
311 
312 static void restore_sigregs_ext(CPUS390XState *env, target_sigregs_ext *ext)
313 {
314     int i;
315 
316     /*
317      * if (MACHINE_HAS_VX) ...
318      * That said, we always allocate the stack storage and the
319      * space is always available in env.
320      */
321     for (i = 0; i < 16; ++i) {
322         __get_user(env->vregs[i][1], &ext->vxrs_low[i]);
323     }
324     for (i = 0; i < 16; ++i) {
325         __get_user(env->vregs[i + 16][0], &ext->vxrs_high[i][0]);
326         __get_user(env->vregs[i + 16][1], &ext->vxrs_high[i][1]);
327     }
328 }
329 
330 long do_sigreturn(CPUS390XState *env)
331 {
332     sigframe *frame;
333     abi_ulong frame_addr = env->regs[15];
334     target_sigset_t target_set;
335     sigset_t set;
336 
337     trace_user_do_sigreturn(env, frame_addr);
338     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
339         force_sig(TARGET_SIGSEGV);
340         return -TARGET_QEMU_ESIGRETURN;
341     }
342 
343     /* Make sure that we're initializing all of target_set. */
344     QEMU_BUILD_BUG_ON(ARRAY_SIZE(target_set.sig) != 1);
345     __get_user(target_set.sig[0], &frame->sc.oldmask[0]);
346 
347     target_to_host_sigset_internal(&set, &target_set);
348     set_sigmask(&set); /* ~_BLOCKABLE? */
349 
350     restore_sigregs(env, &frame->sregs);
351     restore_sigregs_ext(env, &frame->sregs_ext);
352 
353     unlock_user_struct(frame, frame_addr, 0);
354     return -TARGET_QEMU_ESIGRETURN;
355 }
356 
357 long do_rt_sigreturn(CPUS390XState *env)
358 {
359     rt_sigframe *frame;
360     abi_ulong frame_addr = env->regs[15];
361     sigset_t set;
362 
363     trace_user_do_rt_sigreturn(env, frame_addr);
364     if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
365         force_sig(TARGET_SIGSEGV);
366         return -TARGET_QEMU_ESIGRETURN;
367     }
368     target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
369 
370     set_sigmask(&set); /* ~_BLOCKABLE? */
371 
372     restore_sigregs(env, &frame->uc.tuc_mcontext);
373     restore_sigregs_ext(env, &frame->uc.tuc_mcontext_ext);
374 
375     target_restore_altstack(&frame->uc.tuc_stack, env);
376 
377     unlock_user_struct(frame, frame_addr, 0);
378     return -TARGET_QEMU_ESIGRETURN;
379 }
380