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 abi_ulong gprs
[__NUM_GPRS
];
41 abi_uint acrs
[__NUM_ACRS
];
42 } target_s390_regs_common
;
47 uint64_t fprs
[__NUM_FPRS
];
48 } target_s390_fp_regs
;
51 target_s390_regs_common regs
;
52 target_s390_fp_regs fpregs
;
56 abi_ulong oldmask
[_SIGCONTEXT_NSIG_WORDS
];
61 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
65 uint8_t retcode
[S390_SYSCALL_SIZE
];
68 struct target_ucontext
{
71 target_stack_t tuc_stack
;
72 target_sigregs tuc_mcontext
;
73 target_sigset_t tuc_sigmask
; /* mask last for extensibility */
77 uint8_t callee_used_stack
[__SIGNAL_FRAMESIZE
];
78 uint8_t retcode
[S390_SYSCALL_SIZE
];
79 struct target_siginfo info
;
80 struct target_ucontext uc
;
83 static inline abi_ulong
84 get_sigframe(struct target_sigaction
*ka
, CPUS390XState
*env
, size_t frame_size
)
88 /* Default to using normal stack */
89 sp
= get_sp_from_cpustate(env
);
91 /* This is the X/Open sanctioned signal stack switching. */
92 if (ka
->sa_flags
& TARGET_SA_ONSTACK
) {
93 sp
= target_sigsp(sp
, ka
);
96 /* This is the legacy signal stack switching. */
97 else if (/* FIXME !user_mode(regs) */ 0 &&
98 !(ka
->sa_flags
& TARGET_SA_RESTORER
) &&
100 sp
= (abi_ulong
) ka
->sa_restorer
;
103 return (sp
- frame_size
) & -8ul;
106 static void save_sigregs(CPUS390XState
*env
, target_sigregs
*sregs
)
109 //save_access_regs(current->thread.acrs); FIXME
111 /* Copy a 'clean' PSW mask to the user to avoid leaking
112 information about whether PER is currently on. */
113 __put_user(env
->psw
.mask
, &sregs
->regs
.psw
.mask
);
114 __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
]);
122 * We have to store the fp registers to current->thread.fp_regs
123 * to merge them with the emulated registers.
125 //save_fp_regs(¤t->thread.fp_regs); FIXME
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)) {
143 __put_user(set
->sig
[0], &frame
->sc
.oldmask
[0]);
145 save_sigregs(env
, &frame
->sregs
);
147 __put_user((abi_ulong
)(unsigned long)&frame
->sregs
, &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] = ka
->sa_restorer
| PSW_ADDR_AMODE
;
216 env
->regs
[14] = (frame_addr
+ offsetof(typeof(*frame
), retcode
))
218 __put_user(S390_SYSCALL_OPCODE
| TARGET_NR_rt_sigreturn
,
219 (uint16_t *)(frame
->retcode
));
222 /* Set up backchain. */
223 __put_user(env
->regs
[15], (abi_ulong
*) frame
);
225 /* Set up registers for signal handler */
226 env
->regs
[15] = frame_addr
;
227 env
->psw
.addr
= (target_ulong
) ka
->_sa_handler
| PSW_ADDR_AMODE
;
229 env
->regs
[2] = sig
; //map_signal(sig);
230 env
->regs
[3] = frame_addr
+ offsetof(typeof(*frame
), info
);
231 env
->regs
[4] = frame_addr
+ offsetof(typeof(*frame
), uc
);
239 restore_sigregs(CPUS390XState
*env
, target_sigregs
*sc
)
244 for (i
= 0; i
< 16; i
++) {
245 __get_user(env
->regs
[i
], &sc
->regs
.gprs
[i
]);
248 __get_user(env
->psw
.mask
, &sc
->regs
.psw
.mask
);
249 trace_user_s390x_restore_sigregs(env
, (unsigned long long)sc
->regs
.psw
.addr
,
250 (unsigned long long)env
->psw
.addr
);
251 __get_user(env
->psw
.addr
, &sc
->regs
.psw
.addr
);
252 /* FIXME: 31-bit -> | PSW_ADDR_AMODE */
254 for (i
= 0; i
< 16; i
++) {
255 __get_user(env
->aregs
[i
], &sc
->regs
.acrs
[i
]);
257 for (i
= 0; i
< 16; i
++) {
258 __get_user(*get_freg(env
, i
), &sc
->fpregs
.fprs
[i
]);
264 long do_sigreturn(CPUS390XState
*env
)
267 abi_ulong frame_addr
= env
->regs
[15];
268 target_sigset_t target_set
;
271 trace_user_do_sigreturn(env
, frame_addr
);
272 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
275 __get_user(target_set
.sig
[0], &frame
->sc
.oldmask
[0]);
277 target_to_host_sigset_internal(&set
, &target_set
);
278 set_sigmask(&set
); /* ~_BLOCKABLE? */
280 if (restore_sigregs(env
, &frame
->sregs
)) {
284 unlock_user_struct(frame
, frame_addr
, 0);
285 return -TARGET_QEMU_ESIGRETURN
;
288 force_sig(TARGET_SIGSEGV
);
289 return -TARGET_QEMU_ESIGRETURN
;
292 long do_rt_sigreturn(CPUS390XState
*env
)
295 abi_ulong frame_addr
= env
->regs
[15];
298 trace_user_do_rt_sigreturn(env
, frame_addr
);
299 if (!lock_user_struct(VERIFY_READ
, frame
, frame_addr
, 1)) {
302 target_to_host_sigset(&set
, &frame
->uc
.tuc_sigmask
);
304 set_sigmask(&set
); /* ~_BLOCKABLE? */
306 if (restore_sigregs(env
, &frame
->uc
.tuc_mcontext
)) {
310 target_restore_altstack(&frame
->uc
.tuc_stack
, env
);
312 unlock_user_struct(frame
, frame_addr
, 0);
313 return -TARGET_QEMU_ESIGRETURN
;
316 unlock_user_struct(frame
, frame_addr
, 0);
317 force_sig(TARGET_SIGSEGV
);
318 return -TARGET_QEMU_ESIGRETURN
;