2 * Emulation of Linux signals
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
19 #include "qemu/osdep.h"
21 #include "signal-common.h"
22 #include "linux-user/trace.h"
28 #define S390_SYSCALL_SIZE 2
29 #define __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
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 PSW_ADDR_AMODE 0x0000000000000000UL /* 0x80000000UL for 31-bit */
36 #define S390_SYSCALL_OPCODE ((uint16_t)0x0a00)
40 target_ulong gprs
[__NUM_GPRS
];
41 unsigned int acrs
[__NUM_ACRS
];
42 } target_s390_regs_common
;
46 double fprs
[__NUM_FPRS
];
47 } target_s390_fp_regs
;
50 target_s390_regs_common regs
;
51 target_s390_fp_regs fpregs
;
54 struct target_sigcontext
{
55 target_ulong oldmask
[_SIGCONTEXT_NSIG_WORDS
];
56 target_sigregs
*sregs
;
60 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
61 struct target_sigcontext sc
;
64 uint8_t retcode
[S390_SYSCALL_SIZE
];
67 struct target_ucontext
{
68 target_ulong tuc_flags
;
69 struct target_ucontext
*tuc_link
;
70 target_stack_t tuc_stack
;
71 target_sigregs tuc_mcontext
;
72 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
76 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
77 uint8_t retcode
[S390_SYSCALL_SIZE
];
78 struct target_siginfo info
;
79 struct target_ucontext uc
;
82 static inline abi_ulong
83 get_sigframe(struct target_sigaction
*ka
, CPUS390XState
*env
, size_t frame_size
)
87 /* Default to using normal stack */
88 sp
= get_sp_from_cpustate(env
);
90 /* This is the X/Open sanctioned signal stack switching. */
91 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
92 sp
= target_sigsp(sp
, ka
);
95 /* This is the legacy signal stack switching. */
96 else if (/* FIXME !user_mode(regs) */ 0 &&
97 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
99 sp
= (abi_ulong
) ka
->sa_restorer
;
102 return (sp
- frame_size
) & -8ul;
105 static void save_sigregs(CPUS390XState
*env
, target_sigregs
*sregs
)
108 //save_access_regs(current->thread.acrs); FIXME
110 /* Copy a 'clean' PSW mask to the user to avoid leaking
111 information about whether PER is currently on. */
112 __put_user(env
->psw
.mask
, &sregs
->regs
.psw
.mask
);
113 __put_user(env
->psw
.addr
, &sregs
->regs
.psw
.addr
);
114 for (i
= 0; i
< 16; i
++) {
115 __put_user(env
->regs
[i
], &sregs
->regs
.gprs
[i
]);
117 for (i
= 0; i
< 16; i
++) {
118 __put_user(env
->aregs
[i
], &sregs
->regs
.acrs
[i
]);
121 * We have to store the fp registers to current->thread.fp_regs
122 * to merge them with the emulated registers.
124 //save_fp_regs(¤t->thread.fp_regs); FIXME
125 for (i
= 0; i
< 16; i
++) {
126 __put_user(get_freg(env
, i
)->ll
, &sregs
->fpregs
.fprs
[i
]);
130 void setup_frame(int sig
, struct target_sigaction
*ka
,
131 target_sigset_t
*set
, CPUS390XState
*env
)
134 abi_ulong frame_addr
;
136 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
137 trace_user_setup_frame(env
, frame_addr
);
138 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
142 __put_user(set
->sig
[0], &frame
->sc
.oldmask
[0]);
144 save_sigregs(env
, &frame
->sregs
);
146 __put_user((abi_ulong
)(unsigned long)&frame
->sregs
,
147 (abi_ulong
*)&frame
->sc
.sregs
);
149 /* Set up to return from userspace. If provided, use a stub
150 already in userspace. */
151 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
152 env
->regs
[14] = (unsigned long)
153 ka
->sa_restorer
| PSW_ADDR_AMODE
;
155 env
->regs
[14] = (frame_addr
+ offsetof(sigframe
, retcode
))
157 __put_user(S390_SYSCALL_OPCODE
| TARGET_NR_sigreturn
,
158 (uint16_t *)(frame
->retcode
));
161 /* Set up backchain. */
162 __put_user(env
->regs
[15], (abi_ulong
*) frame
);
164 /* Set up registers for signal handler */
165 env
->regs
[15] = frame_addr
;
166 env
->psw
.addr
= (target_ulong
) ka
->_sa_handler
| PSW_ADDR_AMODE
;
168 env
->regs
[2] = sig
; //map_signal(sig);
169 env
->regs
[3] = frame_addr
+= offsetof(typeof(*frame
), sc
);
171 /* We forgot to include these in the sigcontext.
172 To avoid breaking binary compatibility, they are passed as args. */
173 env
->regs
[4] = 0; // FIXME: no clue... current->thread.trap_no;
174 env
->regs
[5] = 0; // FIXME: no clue... current->thread.prot_addr;
176 /* Place signal number on stack to allow backtrace from handler. */
177 __put_user(env
->regs
[2], &frame
->signo
);
178 unlock_user_struct(frame
, frame_addr
, 1);
185 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
186 target_siginfo_t
*info
,
187 target_sigset_t
*set
, CPUS390XState
*env
)
191 abi_ulong frame_addr
;
193 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
194 trace_user_setup_rt_frame(env
, frame_addr
);
195 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
199 tswap_siginfo(&frame
->info
, info
);
201 /* Create the ucontext. */
202 __put_user(0, &frame
->uc
.tuc_flags
);
203 __put_user((abi_ulong
)0, (abi_ulong
*)&frame
->uc
.tuc_link
);
204 target_save_altstack(&frame
->uc
.tuc_stack
, env
);
205 save_sigregs(env
, &frame
->uc
.tuc_mcontext
);
206 for (i
= 0; i
< TARGET_NSIG_WORDS
; i
++) {
207 __put_user((abi_ulong
)set
->sig
[i
],
208 (abi_ulong
*)&frame
->uc
.tuc_sigmask
.sig
[i
]);
211 /* Set up to return from userspace. If provided, use a stub
212 already in userspace. */
213 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
214 env
->regs
[14] = (unsigned long) ka
->sa_restorer
| PSW_ADDR_AMODE
;
216 env
->regs
[14] = (unsigned long) frame
->retcode
| PSW_ADDR_AMODE
;
217 __put_user(S390_SYSCALL_OPCODE
| TARGET_NR_rt_sigreturn
,
218 (uint16_t *)(frame
->retcode
));
221 /* Set up backchain. */
222 __put_user(env
->regs
[15], (abi_ulong
*) frame
);
224 /* Set up registers for signal handler */
225 env
->regs
[15] = frame_addr
;
226 env
->psw
.addr
= (target_ulong
) ka
->_sa_handler
| PSW_ADDR_AMODE
;
228 env
->regs
[2] = sig
; //map_signal(sig);
229 env
->regs
[3] = frame_addr
+ offsetof(typeof(*frame
), info
);
230 env
->regs
[4] = frame_addr
+ offsetof(typeof(*frame
), uc
);
238 restore_sigregs(CPUS390XState
*env
, target_sigregs
*sc
)
243 for (i
= 0; i
< 16; i
++) {
244 __get_user(env
->regs
[i
], &sc
->regs
.gprs
[i
]);
247 __get_user(env
->psw
.mask
, &sc
->regs
.psw
.mask
);
248 trace_user_s390x_restore_sigregs(env
, (unsigned long long)sc
->regs
.psw
.addr
,
249 (unsigned long long)env
->psw
.addr
);
250 __get_user(env
->psw
.addr
, &sc
->regs
.psw
.addr
);
251 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
253 for (i
= 0; i
< 16; i
++) {
254 __get_user(env
->aregs
[i
], &sc
->regs
.acrs
[i
]);
256 for (i
= 0; i
< 16; i
++) {
257 __get_user(get_freg(env
, i
)->ll
, &sc
->fpregs
.fprs
[i
]);
263 long do_sigreturn(CPUS390XState
*env
)
266 abi_ulong frame_addr
= env
->regs
[15];
267 target_sigset_t target_set
;
270 trace_user_do_sigreturn(env
, frame_addr
);
271 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
274 __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
[0]);
276 target_to_host_sigset_internal(&set
, &target_set
);
277 set_sigmask(&set
); /* ~_BLOCKABLE? */
279 if (restore_sigregs(env
, &frame
->sregs
)) {
283 unlock_user_struct(frame
, frame_addr
, 0);
284 return -TARGET_QEMU_ESIGRETURN
;
287 force_sig(TARGET_SIGSEGV
);
288 return -TARGET_QEMU_ESIGRETURN
;
291 long do_rt_sigreturn(CPUS390XState
*env
)
294 abi_ulong frame_addr
= env
->regs
[15];
297 trace_user_do_rt_sigreturn(env
, frame_addr
);
298 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
301 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
303 set_sigmask(&set
); /* ~_BLOCKABLE? */
305 if (restore_sigregs(env
, &frame
->uc
.tuc_mcontext
)) {
309 if (do_sigaltstack(frame_addr
+ offsetof(rt_sigframe
, uc
.tuc_stack
), 0,
310 get_sp_from_cpustate(env
)) == -EFAULT
) {
313 unlock_user_struct(frame
, frame_addr
, 0);
314 return -TARGET_QEMU_ESIGRETURN
;
317 unlock_user_struct(frame
, frame_addr
, 0);
318 force_sig(TARGET_SIGSEGV
);
319 return -TARGET_QEMU_ESIGRETURN
;