1 /* Signal support for 32-bit kernel builds
3 * Copyright (C) 2001 Matthew Wilcox <willy at parisc-linux.org>
4 * Copyright (C) 2006 Kyle McMartin <kyle at parisc-linux.org>
6 * Code was mostly borrowed from kernel/signal.c.
7 * See kernel/signal.c for additional Copyrights.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/compat.h>
26 #include <linux/module.h>
27 #include <linux/unistd.h>
28 #include <linux/init.h>
29 #include <linux/sched.h>
30 #include <linux/syscalls.h>
31 #include <linux/types.h>
32 #include <linux/errno.h>
34 #include <asm/uaccess.h>
39 #define DEBUG_COMPAT_SIG 0
40 #define DEBUG_COMPAT_SIG_LEVEL 2
43 #define DBG(LEVEL, ...) \
44 ((DEBUG_COMPAT_SIG_LEVEL >= LEVEL) \
45 ? printk(__VA_ARGS__) : (void) 0)
47 #define DBG(LEVEL, ...)
51 sigset_32to64(sigset_t
*s64
, compat_sigset_t
*s32
)
53 s64
->sig
[0] = s32
->sig
[0] | ((unsigned long)s32
->sig
[1] << 32);
57 sigset_64to32(compat_sigset_t
*s32
, sigset_t
*s64
)
59 s32
->sig
[0] = s64
->sig
[0] & 0xffffffffUL
;
60 s32
->sig
[1] = (s64
->sig
[0] >> 32) & 0xffffffffUL
;
64 put_sigset32(compat_sigset_t __user
*up
, sigset_t
*set
, size_t sz
)
68 if (sz
!= sizeof *set
) panic("put_sigset32()");
69 sigset_64to32(&s
, set
);
71 return copy_to_user(up
, &s
, sizeof s
);
75 get_sigset32(compat_sigset_t __user
*up
, sigset_t
*set
, size_t sz
)
80 if (sz
!= sizeof *set
) panic("put_sigset32()");
82 if ((r
= copy_from_user(&s
, up
, sz
)) == 0) {
83 sigset_32to64(set
, &s
);
89 int sys32_rt_sigprocmask(int how
, compat_sigset_t __user
*set
, compat_sigset_t __user
*oset
,
90 unsigned int sigsetsize
)
92 sigset_t old_set
, new_set
;
95 if (set
&& get_sigset32(set
, &new_set
, sigsetsize
))
98 KERNEL_SYSCALL(ret
, sys_rt_sigprocmask
, how
, set
? (sigset_t __user
*)&new_set
: NULL
,
99 oset
? (sigset_t __user
*)&old_set
: NULL
, sigsetsize
);
101 if (!ret
&& oset
&& put_sigset32(oset
, &old_set
, sigsetsize
))
108 int sys32_rt_sigpending(compat_sigset_t __user
*uset
, unsigned int sigsetsize
)
113 KERNEL_SYSCALL(ret
, sys_rt_sigpending
, (sigset_t __user
*)&set
, sigsetsize
);
115 if (!ret
&& put_sigset32(uset
, &set
, sigsetsize
))
122 sys32_rt_sigaction(int sig
, const struct sigaction32 __user
*act
, struct sigaction32 __user
*oact
,
125 struct k_sigaction32 new_sa32
, old_sa32
;
126 struct k_sigaction new_sa
, old_sa
;
130 if (copy_from_user(&new_sa32
.sa
, act
, sizeof new_sa32
.sa
))
132 new_sa
.sa
.sa_handler
= (__sighandler_t
)(unsigned long)new_sa32
.sa
.sa_handler
;
133 new_sa
.sa
.sa_flags
= new_sa32
.sa
.sa_flags
;
134 sigset_32to64(&new_sa
.sa
.sa_mask
, &new_sa32
.sa
.sa_mask
);
137 ret
= do_sigaction(sig
, act
? &new_sa
: NULL
, oact
? &old_sa
: NULL
);
140 sigset_64to32(&old_sa32
.sa
.sa_mask
, &old_sa
.sa
.sa_mask
);
141 old_sa32
.sa
.sa_flags
= old_sa
.sa
.sa_flags
;
142 old_sa32
.sa
.sa_handler
= (__sighandler_t32
)(unsigned long)old_sa
.sa
.sa_handler
;
143 if (copy_to_user(oact
, &old_sa32
.sa
, sizeof old_sa32
.sa
))
150 do_sigaltstack32 (const compat_stack_t __user
*uss32
, compat_stack_t __user
*uoss32
, unsigned long sp
)
152 compat_stack_t ss32
, oss32
;
154 stack_t
*ssp
= NULL
, *ossp
= NULL
;
158 if (copy_from_user(&ss32
, uss32
, sizeof ss32
))
161 ss
.ss_sp
= (void __user
*)(unsigned long)ss32
.ss_sp
;
162 ss
.ss_flags
= ss32
.ss_flags
;
163 ss
.ss_size
= ss32
.ss_size
;
171 KERNEL_SYSCALL(ret
, do_sigaltstack
, (const stack_t __user
*)ssp
, (stack_t __user
*)ossp
, sp
);
173 if (!ret
&& uoss32
) {
174 oss32
.ss_sp
= (unsigned int)(unsigned long)oss
.ss_sp
;
175 oss32
.ss_flags
= oss
.ss_flags
;
176 oss32
.ss_size
= oss
.ss_size
;
177 if (copy_to_user(uoss32
, &oss32
, sizeof *uoss32
))
185 restore_sigcontext32(struct compat_sigcontext __user
*sc
, struct compat_regfile __user
* rf
,
186 struct pt_regs
*regs
)
189 compat_uint_t compat_reg
;
190 compat_uint_t compat_regt
;
193 /* When loading 32-bit values into 64-bit registers make
194 sure to clear the upper 32-bits */
195 DBG(2,"restore_sigcontext32: PER_LINUX32 process\n");
196 DBG(2,"restore_sigcontext32: sc = 0x%p, rf = 0x%p, regs = 0x%p\n", sc
, rf
, regs
);
197 DBG(2,"restore_sigcontext32: compat_sigcontext is %#lx bytes\n", sizeof(*sc
));
198 for(regn
=0; regn
< 32; regn
++){
199 err
|= __get_user(compat_reg
,&sc
->sc_gr
[regn
]);
200 regs
->gr
[regn
] = compat_reg
;
201 /* Load upper half */
202 err
|= __get_user(compat_regt
,&rf
->rf_gr
[regn
]);
203 regs
->gr
[regn
] = ((u64
)compat_regt
<< 32) | (u64
)compat_reg
;
204 DBG(3,"restore_sigcontext32: gr%02d = %#lx (%#x / %#x)\n",
205 regn
, regs
->gr
[regn
], compat_regt
, compat_reg
);
207 DBG(2,"restore_sigcontext32: sc->sc_fr = 0x%p (%#lx)\n",sc
->sc_fr
, sizeof(sc
->sc_fr
));
208 /* XXX: BE WARNED FR's are 64-BIT! */
209 err
|= __copy_from_user(regs
->fr
, sc
->sc_fr
, sizeof(regs
->fr
));
211 /* Better safe than sorry, pass __get_user two things of
212 the same size and let gcc do the upward conversion to
214 err
|= __get_user(compat_reg
, &sc
->sc_iaoq
[0]);
215 /* Load upper half */
216 err
|= __get_user(compat_regt
, &rf
->rf_iaoq
[0]);
217 regs
->iaoq
[0] = ((u64
)compat_regt
<< 32) | (u64
)compat_reg
;
218 DBG(2,"restore_sigcontext32: upper half of iaoq[0] = %#lx\n", compat_regt
);
219 DBG(2,"restore_sigcontext32: sc->sc_iaoq[0] = %p => %#x\n",
220 &sc
->sc_iaoq
[0], compat_reg
);
222 err
|= __get_user(compat_reg
, &sc
->sc_iaoq
[1]);
223 /* Load upper half */
224 err
|= __get_user(compat_regt
, &rf
->rf_iaoq
[1]);
225 regs
->iaoq
[1] = ((u64
)compat_regt
<< 32) | (u64
)compat_reg
;
226 DBG(2,"restore_sigcontext32: upper half of iaoq[1] = %#lx\n", compat_regt
);
227 DBG(2,"restore_sigcontext32: sc->sc_iaoq[1] = %p => %#x\n",
228 &sc
->sc_iaoq
[1],compat_reg
);
229 DBG(2,"restore_sigcontext32: iaoq is %#lx / %#lx\n",
230 regs
->iaoq
[0],regs
->iaoq
[1]);
232 err
|= __get_user(compat_reg
, &sc
->sc_iasq
[0]);
233 /* Load the upper half for iasq */
234 err
|= __get_user(compat_regt
, &rf
->rf_iasq
[0]);
235 regs
->iasq
[0] = ((u64
)compat_regt
<< 32) | (u64
)compat_reg
;
236 DBG(2,"restore_sigcontext32: upper half of iasq[0] = %#lx\n", compat_regt
);
238 err
|= __get_user(compat_reg
, &sc
->sc_iasq
[1]);
239 /* Load the upper half for iasq */
240 err
|= __get_user(compat_regt
, &rf
->rf_iasq
[1]);
241 regs
->iasq
[1] = ((u64
)compat_regt
<< 32) | (u64
)compat_reg
;
242 DBG(2,"restore_sigcontext32: upper half of iasq[1] = %#lx\n", compat_regt
);
243 DBG(2,"restore_sigcontext32: iasq is %#lx / %#lx\n",
244 regs
->iasq
[0],regs
->iasq
[1]);
246 err
|= __get_user(compat_reg
, &sc
->sc_sar
);
247 /* Load the upper half for sar */
248 err
|= __get_user(compat_regt
, &rf
->rf_sar
);
249 regs
->sar
= ((u64
)compat_regt
<< 32) | (u64
)compat_reg
;
250 DBG(2,"restore_sigcontext32: upper_half & sar = %#lx\n", compat_regt
);
251 DBG(2,"restore_sigcontext32: sar is %#lx\n", regs
->sar
);
252 DBG(2,"restore_sigcontext32: r28 is %ld\n", regs
->gr
[28]);
258 * Set up the sigcontext structure for this process.
259 * This is not an easy task if the kernel is 64-bit, it will require
260 * that we examine the process personality to determine if we need to
261 * truncate for a 32-bit userspace.
264 setup_sigcontext32(struct compat_sigcontext __user
*sc
, struct compat_regfile __user
* rf
,
265 struct pt_regs
*regs
, int in_syscall
)
267 compat_int_t flags
= 0;
269 compat_uint_t compat_reg
;
270 compat_uint_t compat_regb
;
273 if (on_sig_stack((unsigned long) sc
))
274 flags
|= PARISC_SC_FLAG_ONSTACK
;
278 DBG(1,"setup_sigcontext32: in_syscall\n");
280 flags
|= PARISC_SC_FLAG_IN_SYSCALL
;
282 compat_reg
= (compat_uint_t
)(regs
->gr
[31]);
283 /* regs->iaoq is undefined in the syscall return path */
284 err
|= __put_user(compat_reg
, &sc
->sc_iaoq
[0]);
285 DBG(2,"setup_sigcontext32: sc->sc_iaoq[0] = %p <= %#x\n",
286 &sc
->sc_iaoq
[0], compat_reg
);
288 /* Store upper half */
289 compat_reg
= (compat_uint_t
)(regs
->gr
[31] >> 32);
290 err
|= __put_user(compat_reg
, &rf
->rf_iaoq
[0]);
291 DBG(2,"setup_sigcontext32: upper half iaoq[0] = %#x\n", compat_reg
);
294 compat_reg
= (compat_uint_t
)(regs
->gr
[31]+4);
295 err
|= __put_user(compat_reg
, &sc
->sc_iaoq
[1]);
296 DBG(2,"setup_sigcontext32: sc->sc_iaoq[1] = %p <= %#x\n",
297 &sc
->sc_iaoq
[1], compat_reg
);
298 /* Store upper half */
299 compat_reg
= (compat_uint_t
)((regs
->gr
[31]+4) >> 32);
300 err
|= __put_user(compat_reg
, &rf
->rf_iaoq
[1]);
301 DBG(2,"setup_sigcontext32: upper half iaoq[1] = %#x\n", compat_reg
);
304 compat_reg
= (compat_uint_t
)(regs
->sr
[3]);
305 err
|= __put_user(compat_reg
, &sc
->sc_iasq
[0]);
306 err
|= __put_user(compat_reg
, &sc
->sc_iasq
[1]);
308 /* Store upper half */
309 compat_reg
= (compat_uint_t
)(regs
->sr
[3] >> 32);
310 err
|= __put_user(compat_reg
, &rf
->rf_iasq
[0]);
311 err
|= __put_user(compat_reg
, &rf
->rf_iasq
[1]);
313 DBG(2,"setup_sigcontext32: upper half iasq[0] = %#x\n", compat_reg
);
314 DBG(2,"setup_sigcontext32: upper half iasq[1] = %#x\n", compat_reg
);
315 DBG(1,"setup_sigcontext32: iaoq %#lx / %#lx\n",
316 regs
->gr
[31], regs
->gr
[31]+4);
320 compat_reg
= (compat_uint_t
)(regs
->iaoq
[0]);
321 err
|= __put_user(compat_reg
, &sc
->sc_iaoq
[0]);
322 DBG(2,"setup_sigcontext32: sc->sc_iaoq[0] = %p <= %#x\n",
323 &sc
->sc_iaoq
[0], compat_reg
);
324 /* Store upper half */
325 compat_reg
= (compat_uint_t
)(regs
->iaoq
[0] >> 32);
326 err
|= __put_user(compat_reg
, &rf
->rf_iaoq
[0]);
327 DBG(2,"setup_sigcontext32: upper half iaoq[0] = %#x\n", compat_reg
);
329 compat_reg
= (compat_uint_t
)(regs
->iaoq
[1]);
330 err
|= __put_user(compat_reg
, &sc
->sc_iaoq
[1]);
331 DBG(2,"setup_sigcontext32: sc->sc_iaoq[1] = %p <= %#x\n",
332 &sc
->sc_iaoq
[1], compat_reg
);
333 /* Store upper half */
334 compat_reg
= (compat_uint_t
)(regs
->iaoq
[1] >> 32);
335 err
|= __put_user(compat_reg
, &rf
->rf_iaoq
[1]);
336 DBG(2,"setup_sigcontext32: upper half iaoq[1] = %#x\n", compat_reg
);
339 compat_reg
= (compat_uint_t
)(regs
->iasq
[0]);
340 err
|= __put_user(compat_reg
, &sc
->sc_iasq
[0]);
341 DBG(2,"setup_sigcontext32: sc->sc_iasq[0] = %p <= %#x\n",
342 &sc
->sc_iasq
[0], compat_reg
);
343 /* Store upper half */
344 compat_reg
= (compat_uint_t
)(regs
->iasq
[0] >> 32);
345 err
|= __put_user(compat_reg
, &rf
->rf_iasq
[0]);
346 DBG(2,"setup_sigcontext32: upper half iasq[0] = %#x\n", compat_reg
);
349 compat_reg
= (compat_uint_t
)(regs
->iasq
[1]);
350 err
|= __put_user(compat_reg
, &sc
->sc_iasq
[1]);
351 DBG(2,"setup_sigcontext32: sc->sc_iasq[1] = %p <= %#x\n",
352 &sc
->sc_iasq
[1], compat_reg
);
353 /* Store upper half */
354 compat_reg
= (compat_uint_t
)(regs
->iasq
[1] >> 32);
355 err
|= __put_user(compat_reg
, &rf
->rf_iasq
[1]);
356 DBG(2,"setup_sigcontext32: upper half iasq[1] = %#x\n", compat_reg
);
358 /* Print out the IAOQ for debugging */
359 DBG(1,"setup_sigcontext32: ia0q %#lx / %#lx\n",
360 regs
->iaoq
[0], regs
->iaoq
[1]);
363 err
|= __put_user(flags
, &sc
->sc_flags
);
365 DBG(1,"setup_sigcontext32: Truncating general registers.\n");
367 for(regn
=0; regn
< 32; regn
++){
368 /* Truncate a general register */
369 compat_reg
= (compat_uint_t
)(regs
->gr
[regn
]);
370 err
|= __put_user(compat_reg
, &sc
->sc_gr
[regn
]);
371 /* Store upper half */
372 compat_regb
= (compat_uint_t
)(regs
->gr
[regn
] >> 32);
373 err
|= __put_user(compat_regb
, &rf
->rf_gr
[regn
]);
375 /* DEBUG: Write out the "upper / lower" register data */
376 DBG(2,"setup_sigcontext32: gr%02d = %#x / %#x\n", regn
,
377 compat_regb
, compat_reg
);
380 /* Copy the floating point registers (same size)
381 XXX: BE WARNED FR's are 64-BIT! */
382 DBG(1,"setup_sigcontext32: Copying from regs to sc, "
383 "sc->sc_fr size = %#lx, regs->fr size = %#lx\n",
384 sizeof(regs
->fr
), sizeof(sc
->sc_fr
));
385 err
|= __copy_to_user(sc
->sc_fr
, regs
->fr
, sizeof(regs
->fr
));
387 compat_reg
= (compat_uint_t
)(regs
->sar
);
388 err
|= __put_user(compat_reg
, &sc
->sc_sar
);
389 DBG(2,"setup_sigcontext32: sar is %#x\n", compat_reg
);
390 /* Store upper half */
391 compat_reg
= (compat_uint_t
)(regs
->sar
>> 32);
392 err
|= __put_user(compat_reg
, &rf
->rf_sar
);
393 DBG(2,"setup_sigcontext32: upper half sar = %#x\n", compat_reg
);
394 DBG(1,"setup_sigcontext32: r28 is %ld\n", regs
->gr
[28]);
400 copy_siginfo_from_user32 (siginfo_t
*to
, compat_siginfo_t __user
*from
)
405 if (!access_ok(VERIFY_READ
, from
, sizeof(compat_siginfo_t
)))
408 err
= __get_user(to
->si_signo
, &from
->si_signo
);
409 err
|= __get_user(to
->si_errno
, &from
->si_errno
);
410 err
|= __get_user(to
->si_code
, &from
->si_code
);
413 err
|= __copy_from_user(&to
->_sifields
._pad
, &from
->_sifields
._pad
, SI_PAD_SIZE
);
415 switch (to
->si_code
>> 16) {
416 case __SI_CHLD
>> 16:
417 err
|= __get_user(to
->si_utime
, &from
->si_utime
);
418 err
|= __get_user(to
->si_stime
, &from
->si_stime
);
419 err
|= __get_user(to
->si_status
, &from
->si_status
);
421 err
|= __get_user(to
->si_pid
, &from
->si_pid
);
422 err
|= __get_user(to
->si_uid
, &from
->si_uid
);
424 case __SI_FAULT
>> 16:
425 err
|= __get_user(addr
, &from
->si_addr
);
426 to
->si_addr
= compat_ptr(addr
);
428 case __SI_POLL
>> 16:
429 err
|= __get_user(to
->si_band
, &from
->si_band
);
430 err
|= __get_user(to
->si_fd
, &from
->si_fd
);
432 case __SI_RT
>> 16: /* This is not generated by the kernel as of now. */
433 case __SI_MESGQ
>> 16:
434 err
|= __get_user(to
->si_pid
, &from
->si_pid
);
435 err
|= __get_user(to
->si_uid
, &from
->si_uid
);
436 err
|= __get_user(to
->si_int
, &from
->si_int
);
444 copy_siginfo_to_user32 (compat_siginfo_t __user
*to
, siginfo_t
*from
)
450 if (!access_ok(VERIFY_WRITE
, to
, sizeof(compat_siginfo_t
)))
453 /* If you change siginfo_t structure, please be sure
454 this code is fixed accordingly.
455 It should never copy any pad contained in the structure
456 to avoid security leaks, but must copy the generic
457 3 ints plus the relevant union member.
458 This routine must convert siginfo from 64bit to 32bit as well
460 err
= __put_user(from
->si_signo
, &to
->si_signo
);
461 err
|= __put_user(from
->si_errno
, &to
->si_errno
);
462 err
|= __put_user((short)from
->si_code
, &to
->si_code
);
463 if (from
->si_code
< 0)
464 err
|= __copy_to_user(&to
->_sifields
._pad
, &from
->_sifields
._pad
, SI_PAD_SIZE
);
466 switch (from
->si_code
>> 16) {
467 case __SI_CHLD
>> 16:
468 err
|= __put_user(from
->si_utime
, &to
->si_utime
);
469 err
|= __put_user(from
->si_stime
, &to
->si_stime
);
470 err
|= __put_user(from
->si_status
, &to
->si_status
);
472 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
473 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
475 case __SI_FAULT
>> 16:
476 addr
= ptr_to_compat(from
->si_addr
);
477 err
|= __put_user(addr
, &to
->si_addr
);
479 case __SI_POLL
>> 16:
480 err
|= __put_user(from
->si_band
, &to
->si_band
);
481 err
|= __put_user(from
->si_fd
, &to
->si_fd
);
483 case __SI_TIMER
>> 16:
484 err
|= __put_user(from
->si_tid
, &to
->si_tid
);
485 err
|= __put_user(from
->si_overrun
, &to
->si_overrun
);
486 val
= (compat_int_t
)from
->si_int
;
487 err
|= __put_user(val
, &to
->si_int
);
489 case __SI_RT
>> 16: /* Not generated by the kernel as of now. */
490 case __SI_MESGQ
>> 16:
491 err
|= __put_user(from
->si_uid
, &to
->si_uid
);
492 err
|= __put_user(from
->si_pid
, &to
->si_pid
);
493 val
= (compat_int_t
)from
->si_int
;
494 err
|= __put_user(val
, &to
->si_int
);
501 asmlinkage
long compat_sys_rt_sigqueueinfo(int pid
, int sig
,
502 struct compat_siginfo __user
*uinfo
)
506 if (copy_siginfo_from_user32(&info
, uinfo
))
509 /* Not even root can pretend to send signals from the kernel.
510 Nor can they impersonate a kill(), which adds source info. */
511 if (info
.si_code
>= 0)
515 /* POSIX.1b doesn't mention process groups. */
516 return kill_proc_info(sig
, &info
, pid
);