2 * Copyright IBM Corp. 2000, 2006
3 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
4 * Gerhard Tonn (ton@de.ibm.com)
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
11 #include <linux/compat.h>
12 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/errno.h>
18 #include <linux/wait.h>
19 #include <linux/ptrace.h>
20 #include <linux/unistd.h>
21 #include <linux/stddef.h>
22 #include <linux/tty.h>
23 #include <linux/personality.h>
24 #include <linux/binfmts.h>
25 #include <asm/ucontext.h>
26 #include <asm/uaccess.h>
27 #include <asm/lowcore.h>
28 #include <asm/switch_to.h>
29 #include "compat_linux.h"
30 #include "compat_ptrace.h"
35 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE32
];
36 struct sigcontext32 sc
;
39 __u32 gprs_high
[NUM_GPRS
];
40 __u8 retcode
[S390_SYSCALL_SIZE
];
45 __u8 callee_used_stack
[__SIGNAL_FRAMESIZE32
];
46 __u8 retcode
[S390_SYSCALL_SIZE
];
47 compat_siginfo_t info
;
49 __u32 gprs_high
[NUM_GPRS
];
52 int copy_siginfo_to_user32(compat_siginfo_t __user
*to
, siginfo_t
*from
)
56 /* If you change siginfo_t structure, please be sure
57 this code is fixed accordingly.
58 It should never copy any pad contained in the structure
59 to avoid security leaks, but must copy the generic
60 3 ints plus the relevant union member.
61 This routine must convert siginfo from 64bit to 32bit as well
63 err
= __put_user(from
->si_signo
, &to
->si_signo
);
64 err
|= __put_user(from
->si_errno
, &to
->si_errno
);
65 err
|= __put_user((short)from
->si_code
, &to
->si_code
);
66 if (from
->si_code
< 0)
67 err
|= __copy_to_user(&to
->_sifields
._pad
, &from
->_sifields
._pad
, SI_PAD_SIZE
);
69 switch (from
->si_code
>> 16) {
70 case __SI_RT
>> 16: /* This is not generated by the kernel as of now. */
71 case __SI_MESGQ
>> 16:
72 err
|= __put_user(from
->si_int
, &to
->si_int
);
75 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
76 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
79 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
80 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
81 err
|= __put_user(from
->si_utime
, &to
->si_utime
);
82 err
|= __put_user(from
->si_stime
, &to
->si_stime
);
83 err
|= __put_user(from
->si_status
, &to
->si_status
);
85 case __SI_FAULT
>> 16:
86 err
|= __put_user((unsigned long) from
->si_addr
,
90 err
|= __put_user(from
->si_band
, &to
->si_band
);
91 err
|= __put_user(from
->si_fd
, &to
->si_fd
);
93 case __SI_TIMER
>> 16:
94 err
|= __put_user(from
->si_tid
, &to
->si_tid
);
95 err
|= __put_user(from
->si_overrun
, &to
->si_overrun
);
96 err
|= __put_user(from
->si_int
, &to
->si_int
);
105 int copy_siginfo_from_user32(siginfo_t
*to
, compat_siginfo_t __user
*from
)
110 err
= __get_user(to
->si_signo
, &from
->si_signo
);
111 err
|= __get_user(to
->si_errno
, &from
->si_errno
);
112 err
|= __get_user(to
->si_code
, &from
->si_code
);
115 err
|= __copy_from_user(&to
->_sifields
._pad
, &from
->_sifields
._pad
, SI_PAD_SIZE
);
117 switch (to
->si_code
>> 16) {
118 case __SI_RT
>> 16: /* This is not generated by the kernel as of now. */
119 case __SI_MESGQ
>> 16:
120 err
|= __get_user(to
->si_int
, &from
->si_int
);
122 case __SI_KILL
>> 16:
123 err
|= __get_user(to
->si_pid
, &from
->si_pid
);
124 err
|= __get_user(to
->si_uid
, &from
->si_uid
);
126 case __SI_CHLD
>> 16:
127 err
|= __get_user(to
->si_pid
, &from
->si_pid
);
128 err
|= __get_user(to
->si_uid
, &from
->si_uid
);
129 err
|= __get_user(to
->si_utime
, &from
->si_utime
);
130 err
|= __get_user(to
->si_stime
, &from
->si_stime
);
131 err
|= __get_user(to
->si_status
, &from
->si_status
);
133 case __SI_FAULT
>> 16:
134 err
|= __get_user(tmp
, &from
->si_addr
);
135 to
->si_addr
= (void __force __user
*)
136 (u64
) (tmp
& PSW32_ADDR_INSN
);
138 case __SI_POLL
>> 16:
139 err
|= __get_user(to
->si_band
, &from
->si_band
);
140 err
|= __get_user(to
->si_fd
, &from
->si_fd
);
142 case __SI_TIMER
>> 16:
143 err
|= __get_user(to
->si_tid
, &from
->si_tid
);
144 err
|= __get_user(to
->si_overrun
, &from
->si_overrun
);
145 err
|= __get_user(to
->si_int
, &from
->si_int
);
154 static int save_sigregs32(struct pt_regs
*regs
, _sigregs32 __user
*sregs
)
156 _s390_regs_common32 regs32
;
159 regs32
.psw
.mask
= psw32_user_bits
|
160 ((__u32
)(regs
->psw
.mask
>> 32) & PSW32_MASK_USER
);
161 regs32
.psw
.addr
= (__u32
) regs
->psw
.addr
|
162 (__u32
)(regs
->psw
.mask
& PSW_MASK_BA
);
163 for (i
= 0; i
< NUM_GPRS
; i
++)
164 regs32
.gprs
[i
] = (__u32
) regs
->gprs
[i
];
165 save_access_regs(current
->thread
.acrs
);
166 memcpy(regs32
.acrs
, current
->thread
.acrs
, sizeof(regs32
.acrs
));
167 err
= __copy_to_user(&sregs
->regs
, ®s32
, sizeof(regs32
));
170 save_fp_regs(¤t
->thread
.fp_regs
);
171 /* s390_fp_regs and _s390_fp_regs32 are the same ! */
172 return __copy_to_user(&sregs
->fpregs
, ¤t
->thread
.fp_regs
,
173 sizeof(_s390_fp_regs32
));
176 static int restore_sigregs32(struct pt_regs
*regs
,_sigregs32 __user
*sregs
)
178 _s390_regs_common32 regs32
;
181 /* Alwys make any pending restarted system call return -EINTR */
182 current_thread_info()->restart_block
.fn
= do_no_restart_syscall
;
184 err
= __copy_from_user(®s32
, &sregs
->regs
, sizeof(regs32
));
187 regs
->psw
.mask
= (regs
->psw
.mask
& ~PSW_MASK_USER
) |
188 (__u64
)(regs32
.psw
.mask
& PSW32_MASK_USER
) << 32 |
189 (__u64
)(regs32
.psw
.addr
& PSW32_ADDR_AMODE
);
190 /* Check for invalid user address space control. */
191 if ((regs
->psw
.mask
& PSW_MASK_ASC
) >= (psw_kernel_bits
& PSW_MASK_ASC
))
192 regs
->psw
.mask
= (psw_user_bits
& PSW_MASK_ASC
) |
193 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
194 regs
->psw
.addr
= (__u64
)(regs32
.psw
.addr
& PSW32_ADDR_INSN
);
195 for (i
= 0; i
< NUM_GPRS
; i
++)
196 regs
->gprs
[i
] = (__u64
) regs32
.gprs
[i
];
197 memcpy(current
->thread
.acrs
, regs32
.acrs
, sizeof(current
->thread
.acrs
));
198 restore_access_regs(current
->thread
.acrs
);
200 err
= __copy_from_user(¤t
->thread
.fp_regs
, &sregs
->fpregs
,
201 sizeof(_s390_fp_regs32
));
202 current
->thread
.fp_regs
.fpc
&= FPC_VALID_MASK
;
206 restore_fp_regs(¤t
->thread
.fp_regs
);
207 clear_thread_flag(TIF_SYSCALL
); /* No longer in a system call */
211 static int save_sigregs_gprs_high(struct pt_regs
*regs
, __u32 __user
*uregs
)
213 __u32 gprs_high
[NUM_GPRS
];
216 for (i
= 0; i
< NUM_GPRS
; i
++)
217 gprs_high
[i
] = regs
->gprs
[i
] >> 32;
219 return __copy_to_user(uregs
, &gprs_high
, sizeof(gprs_high
));
222 static int restore_sigregs_gprs_high(struct pt_regs
*regs
, __u32 __user
*uregs
)
224 __u32 gprs_high
[NUM_GPRS
];
227 err
= __copy_from_user(&gprs_high
, uregs
, sizeof(gprs_high
));
230 for (i
= 0; i
< NUM_GPRS
; i
++)
231 *(__u32
*)®s
->gprs
[i
] = gprs_high
[i
];
235 asmlinkage
long sys32_sigreturn(void)
237 struct pt_regs
*regs
= task_pt_regs(current
);
238 sigframe32 __user
*frame
= (sigframe32 __user
*)regs
->gprs
[15];
241 if (__copy_from_user(&set
.sig
, &frame
->sc
.oldmask
, _SIGMASK_COPY_SIZE32
))
243 set_current_blocked(&set
);
244 if (restore_sigregs32(regs
, &frame
->sregs
))
246 if (restore_sigregs_gprs_high(regs
, frame
->gprs_high
))
248 return regs
->gprs
[2];
250 force_sig(SIGSEGV
, current
);
254 asmlinkage
long sys32_rt_sigreturn(void)
256 struct pt_regs
*regs
= task_pt_regs(current
);
257 rt_sigframe32 __user
*frame
= (rt_sigframe32 __user
*)regs
->gprs
[15];
260 if (__copy_from_user(&set
, &frame
->uc
.uc_sigmask
, sizeof(set
)))
262 set_current_blocked(&set
);
263 if (restore_sigregs32(regs
, &frame
->uc
.uc_mcontext
))
265 if (restore_sigregs_gprs_high(regs
, frame
->gprs_high
))
267 if (compat_restore_altstack(&frame
->uc
.uc_stack
))
269 return regs
->gprs
[2];
271 force_sig(SIGSEGV
, current
);
276 * Set up a signal frame.
281 * Determine which stack to use..
283 static inline void __user
*
284 get_sigframe(struct k_sigaction
*ka
, struct pt_regs
* regs
, size_t frame_size
)
288 /* Default to using normal stack */
289 sp
= (unsigned long) A(regs
->gprs
[15]);
291 /* Overflow on alternate signal stack gives SIGSEGV. */
292 if (on_sig_stack(sp
) && !on_sig_stack((sp
- frame_size
) & -8UL))
293 return (void __user
*) -1UL;
295 /* This is the X/Open sanctioned signal stack switching. */
296 if (ka
->sa
.sa_flags
& SA_ONSTACK
) {
297 if (! sas_ss_flags(sp
))
298 sp
= current
->sas_ss_sp
+ current
->sas_ss_size
;
301 return (void __user
*)((sp
- frame_size
) & -8ul);
304 static inline int map_signal(int sig
)
306 if (current_thread_info()->exec_domain
307 && current_thread_info()->exec_domain
->signal_invmap
309 return current_thread_info()->exec_domain
->signal_invmap
[sig
];
314 static int setup_frame32(int sig
, struct k_sigaction
*ka
,
315 sigset_t
*set
, struct pt_regs
* regs
)
317 sigframe32 __user
*frame
= get_sigframe(ka
, regs
, sizeof(sigframe32
));
319 if (frame
== (void __user
*) -1UL)
322 if (__copy_to_user(&frame
->sc
.oldmask
, &set
->sig
, _SIGMASK_COPY_SIZE32
))
325 if (save_sigregs32(regs
, &frame
->sregs
))
327 if (save_sigregs_gprs_high(regs
, frame
->gprs_high
))
329 if (__put_user((unsigned long) &frame
->sregs
, &frame
->sc
.sregs
))
332 /* Set up to return from userspace. If provided, use a stub
333 already in userspace. */
334 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
335 regs
->gprs
[14] = (__u64
) ka
->sa
.sa_restorer
| PSW32_ADDR_AMODE
;
337 regs
->gprs
[14] = (__u64
) frame
->retcode
| PSW32_ADDR_AMODE
;
338 if (__put_user(S390_SYSCALL_OPCODE
| __NR_sigreturn
,
339 (u16 __force __user
*)(frame
->retcode
)))
343 /* Set up backchain. */
344 if (__put_user(regs
->gprs
[15], (unsigned int __user
*) frame
))
347 /* Set up registers for signal handler */
348 regs
->gprs
[15] = (__force __u64
) frame
;
349 /* Force 31 bit amode and default user address space control. */
350 regs
->psw
.mask
= PSW_MASK_BA
|
351 (psw_user_bits
& PSW_MASK_ASC
) |
352 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
353 regs
->psw
.addr
= (__force __u64
) ka
->sa
.sa_handler
;
355 regs
->gprs
[2] = map_signal(sig
);
356 regs
->gprs
[3] = (__force __u64
) &frame
->sc
;
358 /* We forgot to include these in the sigcontext.
359 To avoid breaking binary compatibility, they are passed as args. */
360 if (sig
== SIGSEGV
|| sig
== SIGBUS
|| sig
== SIGILL
||
361 sig
== SIGTRAP
|| sig
== SIGFPE
) {
362 /* set extra registers only for synchronous signals */
363 regs
->gprs
[4] = regs
->int_code
& 127;
364 regs
->gprs
[5] = regs
->int_parm_long
;
365 regs
->gprs
[6] = task_thread_info(current
)->last_break
;
368 /* Place signal number on stack to allow backtrace from handler. */
369 if (__put_user(regs
->gprs
[2], (int __force __user
*) &frame
->signo
))
374 force_sigsegv(sig
, current
);
378 static int setup_rt_frame32(int sig
, struct k_sigaction
*ka
, siginfo_t
*info
,
379 sigset_t
*set
, struct pt_regs
* regs
)
382 rt_sigframe32 __user
*frame
= get_sigframe(ka
, regs
, sizeof(rt_sigframe32
));
384 if (frame
== (void __user
*) -1UL)
387 if (copy_siginfo_to_user32(&frame
->info
, info
))
390 /* Create the ucontext. */
391 err
|= __put_user(UC_EXTENDED
, &frame
->uc
.uc_flags
);
392 err
|= __put_user(0, &frame
->uc
.uc_link
);
393 err
|= __compat_save_altstack(&frame
->uc
.uc_stack
, regs
->gprs
[15]);
394 err
|= save_sigregs32(regs
, &frame
->uc
.uc_mcontext
);
395 err
|= save_sigregs_gprs_high(regs
, frame
->gprs_high
);
396 err
|= __copy_to_user(&frame
->uc
.uc_sigmask
, set
, sizeof(*set
));
400 /* Set up to return from userspace. If provided, use a stub
401 already in userspace. */
402 if (ka
->sa
.sa_flags
& SA_RESTORER
) {
403 regs
->gprs
[14] = (__u64
) ka
->sa
.sa_restorer
| PSW32_ADDR_AMODE
;
405 regs
->gprs
[14] = (__u64
) frame
->retcode
| PSW32_ADDR_AMODE
;
406 err
|= __put_user(S390_SYSCALL_OPCODE
| __NR_rt_sigreturn
,
407 (u16 __force __user
*)(frame
->retcode
));
410 /* Set up backchain. */
411 if (__put_user(regs
->gprs
[15], (unsigned int __force __user
*) frame
))
414 /* Set up registers for signal handler */
415 regs
->gprs
[15] = (__force __u64
) frame
;
416 /* Force 31 bit amode and default user address space control. */
417 regs
->psw
.mask
= PSW_MASK_BA
|
418 (psw_user_bits
& PSW_MASK_ASC
) |
419 (regs
->psw
.mask
& ~PSW_MASK_ASC
);
420 regs
->psw
.addr
= (__u64
) ka
->sa
.sa_handler
;
422 regs
->gprs
[2] = map_signal(sig
);
423 regs
->gprs
[3] = (__force __u64
) &frame
->info
;
424 regs
->gprs
[4] = (__force __u64
) &frame
->uc
;
425 regs
->gprs
[5] = task_thread_info(current
)->last_break
;
429 force_sigsegv(sig
, current
);
434 * OK, we're invoking a handler
437 void handle_signal32(unsigned long sig
, struct k_sigaction
*ka
,
438 siginfo_t
*info
, sigset_t
*oldset
, struct pt_regs
*regs
)
442 /* Set up the stack frame */
443 if (ka
->sa
.sa_flags
& SA_SIGINFO
)
444 ret
= setup_rt_frame32(sig
, ka
, info
, oldset
, regs
);
446 ret
= setup_frame32(sig
, ka
, oldset
, regs
);
449 signal_delivered(sig
, info
, ka
, regs
,
450 test_thread_flag(TIF_SINGLE_STEP
));