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 __SIGNAL_FRAMESIZE 160 /* FIXME: 31-bit mode -> 96 */
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)
38 abi_ulong gprs
[__NUM_GPRS
];
39 abi_uint acrs
[__NUM_ACRS
];
40 } target_s390_regs_common
;
45 uint64_t fprs
[__NUM_FPRS
];
46 } target_s390_fp_regs
;
49 target_s390_regs_common regs
;
50 target_s390_fp_regs fpregs
;
54 abi_ulong oldmask
[_SIGCONTEXT_NSIG_WORDS
];
59 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
66 struct target_ucontext
{
69 target_stack_t tuc_stack
;
70 target_sigregs tuc_mcontext
;
71 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
75 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
77 struct target_siginfo info
;
78 struct target_ucontext uc
;
81 static inline abi_ulong
82 get_sigframe(struct target_sigaction
*ka
, CPUS390XState
*env
, size_t frame_size
)
86 /* Default to using normal stack */
87 sp
= get_sp_from_cpustate(env
);
89 /* This is the X/Open sanctioned signal stack switching. */
90 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
91 sp
= target_sigsp(sp
, ka
);
94 /* This is the legacy signal stack switching. */
95 else if (/* FIXME !user_mode(regs) */ 0 &&
96 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
98 sp
= (abi_ulong
) ka
->sa_restorer
;
101 return (sp
- frame_size
) & -8ul;
104 static void save_sigregs(CPUS390XState
*env
, target_sigregs
*sregs
)
109 * Copy a 'clean' PSW mask to the user to avoid leaking
110 * 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
);
115 for (i
= 0; i
< 16; i
++) {
116 __put_user(env
->regs
[i
], &sregs
->regs
.gprs
[i
]);
118 for (i
= 0; i
< 16; i
++) {
119 __put_user(env
->aregs
[i
], &sregs
->regs
.acrs
[i
]);
123 * We have to store the fp registers to current->thread.fp_regs
124 * to merge them with the emulated registers.
126 for (i
= 0; i
< 16; i
++) {
127 __put_user(*get_freg(env
, i
), &sregs
->fpregs
.fprs
[i
]);
131 void setup_frame(int sig
, struct target_sigaction
*ka
,
132 target_sigset_t
*set
, CPUS390XState
*env
)
135 abi_ulong frame_addr
;
137 frame_addr
= get_sigframe(ka
, env
, sizeof(*frame
));
138 trace_user_setup_frame(env
, frame_addr
);
139 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
144 __put_user(set
->sig
[0], &frame
->sc
.oldmask
[0]);
146 save_sigregs(env
, &frame
->sregs
);
148 __put_user(frame_addr
+ offsetof(sigframe
, sregs
), &frame
->sc
.sregs
);
150 /* Set up to return from userspace. If provided, use a stub
151 already in userspace. */
152 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
153 env
->regs
[14] = ka
->sa_restorer
;
155 env
->regs
[14] = frame_addr
+ offsetof(sigframe
, retcode
);
156 __put_user(S390_SYSCALL_OPCODE
| TARGET_NR_sigreturn
,
160 /* Set up backchain. */
161 __put_user(env
->regs
[15], (abi_ulong
*) frame
);
163 /* Set up registers for signal handler */
164 env
->regs
[15] = frame_addr
;
165 env
->psw
.addr
= ka
->_sa_handler
;
167 env
->regs
[2] = sig
; //map_signal(sig);
168 env
->regs
[3] = frame_addr
+= offsetof(typeof(*frame
), sc
);
170 /* We forgot to include these in the sigcontext.
171 To avoid breaking binary compatibility, they are passed as args. */
172 env
->regs
[4] = 0; // FIXME: no clue... current->thread.trap_no;
173 env
->regs
[5] = 0; // FIXME: no clue... current->thread.prot_addr;
175 /* Place signal number on stack to allow backtrace from handler. */
176 __put_user(env
->regs
[2], &frame
->signo
);
177 unlock_user_struct(frame
, frame_addr
, 1);
180 void setup_rt_frame(int sig
, struct target_sigaction
*ka
,
181 target_siginfo_t
*info
,
182 target_sigset_t
*set
, CPUS390XState
*env
)
185 abi_ulong frame_addr
;
187 frame_addr
= get_sigframe(ka
, env
, sizeof *frame
);
188 trace_user_setup_rt_frame(env
, frame_addr
);
189 if (!lock_user_struct(VERIFY_WRITE
, frame
, frame_addr
, 0)) {
194 tswap_siginfo(&frame
->info
, info
);
196 /* Create the ucontext. */
197 __put_user(0, &frame
->uc
.tuc_flags
);
198 __put_user((abi_ulong
)0, (abi_ulong
*)&frame
->uc
.tuc_link
);
199 target_save_altstack(&frame
->uc
.tuc_stack
, env
);
200 save_sigregs(env
, &frame
->uc
.tuc_mcontext
);
201 tswap_sigset(&frame
->uc
.tuc_sigmask
, set
);
203 /* Set up to return from userspace. If provided, use a stub
204 already in userspace. */
205 if (ka
->sa_flags
& TARGET_SA_RESTORER
) {
206 env
->regs
[14] = ka
->sa_restorer
;
208 env
->regs
[14] = frame_addr
+ offsetof(typeof(*frame
), retcode
);
209 __put_user(S390_SYSCALL_OPCODE
| TARGET_NR_rt_sigreturn
,
213 /* Set up backchain. */
214 __put_user(env
->regs
[15], (abi_ulong
*) frame
);
216 /* Set up registers for signal handler */
217 env
->regs
[15] = frame_addr
;
218 env
->psw
.addr
= ka
->_sa_handler
;
220 env
->regs
[2] = sig
; //map_signal(sig);
221 env
->regs
[3] = frame_addr
+ offsetof(typeof(*frame
), info
);
222 env
->regs
[4] = frame_addr
+ offsetof(typeof(*frame
), uc
);
225 static void restore_sigregs(CPUS390XState
*env
, target_sigregs
*sc
)
227 target_ulong prev_addr
;
230 for (i
= 0; i
< 16; i
++) {
231 __get_user(env
->regs
[i
], &sc
->regs
.gprs
[i
]);
234 prev_addr
= env
->psw
.addr
;
235 __get_user(env
->psw
.mask
, &sc
->regs
.psw
.mask
);
236 __get_user(env
->psw
.addr
, &sc
->regs
.psw
.addr
);
237 trace_user_s390x_restore_sigregs(env
, env
->psw
.addr
, prev_addr
);
239 for (i
= 0; i
< 16; i
++) {
240 __get_user(env
->aregs
[i
], &sc
->regs
.acrs
[i
]);
242 for (i
= 0; i
< 16; i
++) {
243 __get_user(*get_freg(env
, i
), &sc
->fpregs
.fprs
[i
]);
247 long do_sigreturn(CPUS390XState
*env
)
250 abi_ulong frame_addr
= env
->regs
[15];
251 target_sigset_t target_set
;
254 trace_user_do_sigreturn(env
, frame_addr
);
255 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
256 force_sig(TARGET_SIGSEGV
);
257 return -TARGET_QEMU_ESIGRETURN
;
259 __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
[0]);
261 target_to_host_sigset_internal(&set
, &target_set
);
262 set_sigmask(&set
); /* ~_BLOCKABLE? */
264 restore_sigregs(env
, &frame
->sregs
);
266 unlock_user_struct(frame
, frame_addr
, 0);
267 return -TARGET_QEMU_ESIGRETURN
;
270 long do_rt_sigreturn(CPUS390XState
*env
)
273 abi_ulong frame_addr
= env
->regs
[15];
276 trace_user_do_rt_sigreturn(env
, frame_addr
);
277 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
278 force_sig(TARGET_SIGSEGV
);
279 return -TARGET_QEMU_ESIGRETURN
;
281 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
283 set_sigmask(&set
); /* ~_BLOCKABLE? */
285 restore_sigregs(env
, &frame
->uc
.tuc_mcontext
);
287 target_restore_altstack(&frame
->uc
.tuc_stack
, env
);
289 unlock_user_struct(frame
, frame_addr
, 0);
290 return -TARGET_QEMU_ESIGRETURN
;