2 * linux/arch/arm/kernel/signal.c
4 * Copyright (C) 1995-2009 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/errno.h>
11 #include <linux/random.h>
12 #include <linux/signal.h>
13 #include <linux/personality.h>
14 #include <linux/uaccess.h>
15 #include <linux/tracehook.h>
16 #include <linux/uprobes.h>
19 #include <asm/cacheflush.h>
20 #include <asm/traps.h>
21 #include <asm/ucontext.h>
22 #include <asm/unistd.h>
25 extern const unsigned long sigreturn_codes
[7];
27 static unsigned long signal_return_offset
;
30 static int preserve_crunch_context(struct crunch_sigframe __user
*frame
)
32 char kbuf
[sizeof(*frame
) + 8];
33 struct crunch_sigframe
*kframe
;
35 /* the crunch context must be 64 bit aligned */
36 kframe
= (struct crunch_sigframe
*)((unsigned long)(kbuf
+ 8) & ~7);
37 kframe
->magic
= CRUNCH_MAGIC
;
38 kframe
->size
= CRUNCH_STORAGE_SIZE
;
39 crunch_task_copy(current_thread_info(), &kframe
->storage
);
40 return __copy_to_user(frame
, kframe
, sizeof(*frame
));
43 static int restore_crunch_context(struct crunch_sigframe __user
*frame
)
45 char kbuf
[sizeof(*frame
) + 8];
46 struct crunch_sigframe
*kframe
;
48 /* the crunch context must be 64 bit aligned */
49 kframe
= (struct crunch_sigframe
*)((unsigned long)(kbuf
+ 8) & ~7);
50 if (__copy_from_user(kframe
, frame
, sizeof(*frame
)))
52 if (kframe
->magic
!= CRUNCH_MAGIC
||
53 kframe
->size
!= CRUNCH_STORAGE_SIZE
)
55 crunch_task_restore(current_thread_info(), &kframe
->storage
);
62 static int preserve_iwmmxt_context(struct iwmmxt_sigframe
*frame
)
64 char kbuf
[sizeof(*frame
) + 8];
65 struct iwmmxt_sigframe
*kframe
;
67 /* the iWMMXt context must be 64 bit aligned */
68 kframe
= (struct iwmmxt_sigframe
*)((unsigned long)(kbuf
+ 8) & ~7);
69 kframe
->magic
= IWMMXT_MAGIC
;
70 kframe
->size
= IWMMXT_STORAGE_SIZE
;
71 iwmmxt_task_copy(current_thread_info(), &kframe
->storage
);
72 return __copy_to_user(frame
, kframe
, sizeof(*frame
));
75 static int restore_iwmmxt_context(struct iwmmxt_sigframe
*frame
)
77 char kbuf
[sizeof(*frame
) + 8];
78 struct iwmmxt_sigframe
*kframe
;
80 /* the iWMMXt context must be 64 bit aligned */
81 kframe
= (struct iwmmxt_sigframe
*)((unsigned long)(kbuf
+ 8) & ~7);
82 if (__copy_from_user(kframe
, frame
, sizeof(*frame
)))
84 if (kframe
->magic
!= IWMMXT_MAGIC
||
85 kframe
->size
!= IWMMXT_STORAGE_SIZE
)
87 iwmmxt_task_restore(current_thread_info(), &kframe
->storage
);
95 static int preserve_vfp_context(struct vfp_sigframe __user
*frame
)
97 struct vfp_sigframe kframe
;
100 memset(&kframe
, 0, sizeof(kframe
));
101 kframe
.magic
= VFP_MAGIC
;
102 kframe
.size
= VFP_STORAGE_SIZE
;
104 err
= vfp_preserve_user_clear_hwstate(&kframe
.ufp
, &kframe
.ufp_exc
);
108 return __copy_to_user(frame
, &kframe
, sizeof(kframe
));
111 static int restore_vfp_context(struct vfp_sigframe __user
*auxp
)
113 struct vfp_sigframe frame
;
116 err
= __copy_from_user(&frame
, (char __user
*) auxp
, sizeof(frame
));
121 if (frame
.magic
!= VFP_MAGIC
|| frame
.size
!= VFP_STORAGE_SIZE
)
124 return vfp_restore_user_hwstate(&frame
.ufp
, &frame
.ufp_exc
);
130 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
134 unsigned long retcode
[2];
142 static int restore_sigframe(struct pt_regs
*regs
, struct sigframe __user
*sf
)
144 struct sigcontext context
;
145 struct aux_sigframe __user
*aux
;
149 err
= __copy_from_user(&set
, &sf
->uc
.uc_sigmask
, sizeof(set
));
151 set_current_blocked(&set
);
153 err
|= __copy_from_user(&context
, &sf
->uc
.uc_mcontext
, sizeof(context
));
155 regs
->ARM_r0
= context
.arm_r0
;
156 regs
->ARM_r1
= context
.arm_r1
;
157 regs
->ARM_r2
= context
.arm_r2
;
158 regs
->ARM_r3
= context
.arm_r3
;
159 regs
->ARM_r4
= context
.arm_r4
;
160 regs
->ARM_r5
= context
.arm_r5
;
161 regs
->ARM_r6
= context
.arm_r6
;
162 regs
->ARM_r7
= context
.arm_r7
;
163 regs
->ARM_r8
= context
.arm_r8
;
164 regs
->ARM_r9
= context
.arm_r9
;
165 regs
->ARM_r10
= context
.arm_r10
;
166 regs
->ARM_fp
= context
.arm_fp
;
167 regs
->ARM_ip
= context
.arm_ip
;
168 regs
->ARM_sp
= context
.arm_sp
;
169 regs
->ARM_lr
= context
.arm_lr
;
170 regs
->ARM_pc
= context
.arm_pc
;
171 regs
->ARM_cpsr
= context
.arm_cpsr
;
174 err
|= !valid_user_regs(regs
);
176 aux
= (struct aux_sigframe __user
*) sf
->uc
.uc_regspace
;
179 err
|= restore_crunch_context(&aux
->crunch
);
182 if (err
== 0 && test_thread_flag(TIF_USING_IWMMXT
))
183 err
|= restore_iwmmxt_context(&aux
->iwmmxt
);
187 err
|= restore_vfp_context(&aux
->vfp
);
193 asmlinkage
int sys_sigreturn(struct pt_regs
*regs
)
195 struct sigframe __user
*frame
;
197 /* Always make any pending restarted system calls return -EINTR */
198 current
->restart_block
.fn
= do_no_restart_syscall
;
201 * Since we stacked the signal on a 64-bit boundary,
202 * then 'sp' should be word aligned here. If it's
203 * not, then the user is trying to mess with us.
205 if (regs
->ARM_sp
& 7)
208 frame
= (struct sigframe __user
*)regs
->ARM_sp
;
210 if (!access_ok(VERIFY_READ
, frame
, sizeof (*frame
)))
213 if (restore_sigframe(regs
, frame
))
219 force_sig(SIGSEGV
, current
);
223 asmlinkage
int sys_rt_sigreturn(struct pt_regs
*regs
)
225 struct rt_sigframe __user
*frame
;
227 /* Always make any pending restarted system calls return -EINTR */
228 current
->restart_block
.fn
= do_no_restart_syscall
;
231 * Since we stacked the signal on a 64-bit boundary,
232 * then 'sp' should be word aligned here. If it's
233 * not, then the user is trying to mess with us.
235 if (regs
->ARM_sp
& 7)
238 frame
= (struct rt_sigframe __user
*)regs
->ARM_sp
;
240 if (!access_ok(VERIFY_READ
, frame
, sizeof (*frame
)))
243 if (restore_sigframe(regs
, &frame
->sig
))
246 if (restore_altstack(&frame
->sig
.uc
.uc_stack
))
252 force_sig(SIGSEGV
, current
);
257 setup_sigframe(struct sigframe __user
*sf
, struct pt_regs
*regs
, sigset_t
*set
)
259 struct aux_sigframe __user
*aux
;
260 struct sigcontext context
;
263 context
= (struct sigcontext
) {
264 .arm_r0
= regs
->ARM_r0
,
265 .arm_r1
= regs
->ARM_r1
,
266 .arm_r2
= regs
->ARM_r2
,
267 .arm_r3
= regs
->ARM_r3
,
268 .arm_r4
= regs
->ARM_r4
,
269 .arm_r5
= regs
->ARM_r5
,
270 .arm_r6
= regs
->ARM_r6
,
271 .arm_r7
= regs
->ARM_r7
,
272 .arm_r8
= regs
->ARM_r8
,
273 .arm_r9
= regs
->ARM_r9
,
274 .arm_r10
= regs
->ARM_r10
,
275 .arm_fp
= regs
->ARM_fp
,
276 .arm_ip
= regs
->ARM_ip
,
277 .arm_sp
= regs
->ARM_sp
,
278 .arm_lr
= regs
->ARM_lr
,
279 .arm_pc
= regs
->ARM_pc
,
280 .arm_cpsr
= regs
->ARM_cpsr
,
282 .trap_no
= current
->thread
.trap_no
,
283 .error_code
= current
->thread
.error_code
,
284 .fault_address
= current
->thread
.address
,
285 .oldmask
= set
->sig
[0],
288 err
|= __copy_to_user(&sf
->uc
.uc_mcontext
, &context
, sizeof(context
));
290 err
|= __copy_to_user(&sf
->uc
.uc_sigmask
, set
, sizeof(*set
));
292 aux
= (struct aux_sigframe __user
*) sf
->uc
.uc_regspace
;
295 err
|= preserve_crunch_context(&aux
->crunch
);
298 if (err
== 0 && test_thread_flag(TIF_USING_IWMMXT
))
299 err
|= preserve_iwmmxt_context(&aux
->iwmmxt
);
303 err
|= preserve_vfp_context(&aux
->vfp
);
305 err
|= __put_user(0, &aux
->end_magic
);
310 static inline void __user
*
311 get_sigframe(struct ksignal
*ksig
, struct pt_regs
*regs
, int framesize
)
313 unsigned long sp
= sigsp(regs
->ARM_sp
, ksig
);
317 * ATPCS B01 mandates 8-byte alignment
319 frame
= (void __user
*)((sp
- framesize
) & ~7);
322 * Check that we can actually write to the signal frame.
324 if (!access_ok(VERIFY_WRITE
, frame
, framesize
))
331 setup_return(struct pt_regs
*regs
, struct ksignal
*ksig
,
332 unsigned long __user
*rc
, void __user
*frame
)
334 unsigned long handler
= (unsigned long)ksig
->ka
.sa
.sa_handler
;
335 unsigned long retcode
;
337 unsigned long cpsr
= regs
->ARM_cpsr
& ~(PSR_f
| PSR_E_BIT
);
339 cpsr
|= PSR_ENDSTATE
;
342 * Maybe we need to deliver a 32-bit signal to a 26-bit task.
344 if (ksig
->ka
.sa
.sa_flags
& SA_THIRTYTWO
)
345 cpsr
= (cpsr
& ~MODE_MASK
) | USR_MODE
;
347 #ifdef CONFIG_ARM_THUMB
348 if (elf_hwcap
& HWCAP_THUMB
) {
350 * The LSB of the handler determines if we're going to
351 * be using THUMB or ARM mode for this signal handler.
356 * Clear the If-Then Thumb-2 execution state. ARM spec
357 * requires this to be all 000s in ARM mode. Snapdragon
358 * S4/Krait misbehaves on a Thumb=>ARM signal transition
361 * We must do this whenever we are running on a Thumb-2
362 * capable CPU, which includes ARMv6T2. However, we elect
363 * to always do this to simplify the code; this field is
364 * marked UNK/SBZP for older architectures.
366 cpsr
&= ~PSR_IT_MASK
;
375 if (ksig
->ka
.sa
.sa_flags
& SA_RESTORER
) {
376 retcode
= (unsigned long)ksig
->ka
.sa
.sa_restorer
;
378 unsigned int idx
= thumb
<< 1;
380 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
384 * Put the sigreturn code on the stack no matter which return
385 * mechanism we use in order to remain ABI compliant
387 if (__put_user(sigreturn_codes
[idx
], rc
) ||
388 __put_user(sigreturn_codes
[idx
+1], rc
+1))
392 if (cpsr
& MODE32_BIT
) {
393 struct mm_struct
*mm
= current
->mm
;
396 * 32-bit code can use the signal return page
397 * except when the MPU has protected the vectors
400 retcode
= mm
->context
.sigpage
+ signal_return_offset
+
406 * Ensure that the instruction cache sees
407 * the return code written onto the stack.
409 flush_icache_range((unsigned long)rc
,
410 (unsigned long)(rc
+ 2));
412 retcode
= ((unsigned long)rc
) + thumb
;
416 regs
->ARM_r0
= ksig
->sig
;
417 regs
->ARM_sp
= (unsigned long)frame
;
418 regs
->ARM_lr
= retcode
;
419 regs
->ARM_pc
= handler
;
420 regs
->ARM_cpsr
= cpsr
;
426 setup_frame(struct ksignal
*ksig
, sigset_t
*set
, struct pt_regs
*regs
)
428 struct sigframe __user
*frame
= get_sigframe(ksig
, regs
, sizeof(*frame
));
435 * Set uc.uc_flags to a value which sc.trap_no would never have.
437 err
= __put_user(0x5ac3c35a, &frame
->uc
.uc_flags
);
439 err
|= setup_sigframe(frame
, regs
, set
);
441 err
= setup_return(regs
, ksig
, frame
->retcode
, frame
);
447 setup_rt_frame(struct ksignal
*ksig
, sigset_t
*set
, struct pt_regs
*regs
)
449 struct rt_sigframe __user
*frame
= get_sigframe(ksig
, regs
, sizeof(*frame
));
455 err
|= copy_siginfo_to_user(&frame
->info
, &ksig
->info
);
457 err
|= __put_user(0, &frame
->sig
.uc
.uc_flags
);
458 err
|= __put_user(NULL
, &frame
->sig
.uc
.uc_link
);
460 err
|= __save_altstack(&frame
->sig
.uc
.uc_stack
, regs
->ARM_sp
);
461 err
|= setup_sigframe(&frame
->sig
, regs
, set
);
463 err
= setup_return(regs
, ksig
, frame
->sig
.retcode
, frame
);
467 * For realtime signals we must also set the second and third
468 * arguments for the signal handler.
469 * -- Peter Maydell <pmaydell@chiark.greenend.org.uk> 2000-12-06
471 regs
->ARM_r1
= (unsigned long)&frame
->info
;
472 regs
->ARM_r2
= (unsigned long)&frame
->sig
.uc
;
479 * OK, we're invoking a handler
481 static void handle_signal(struct ksignal
*ksig
, struct pt_regs
*regs
)
483 sigset_t
*oldset
= sigmask_to_save();
487 * Set up the stack frame
489 if (ksig
->ka
.sa
.sa_flags
& SA_SIGINFO
)
490 ret
= setup_rt_frame(ksig
, oldset
, regs
);
492 ret
= setup_frame(ksig
, oldset
, regs
);
495 * Check that the resulting registers are actually sane.
497 ret
|= !valid_user_regs(regs
);
499 signal_setup_done(ret
, ksig
, 0);
503 * Note that 'init' is a special process: it doesn't get signals it doesn't
504 * want to handle. Thus you cannot kill init even with a SIGKILL even by
507 * Note that we go through the signals twice: once to check the signals that
508 * the kernel can handle, and then we build all the user-level signal handling
509 * stack-frames in one go after that.
511 static int do_signal(struct pt_regs
*regs
, int syscall
)
513 unsigned int retval
= 0, continue_addr
= 0, restart_addr
= 0;
518 * If we were from a system call, check for system call restarting...
521 continue_addr
= regs
->ARM_pc
;
522 restart_addr
= continue_addr
- (thumb_mode(regs
) ? 2 : 4);
523 retval
= regs
->ARM_r0
;
526 * Prepare for system call restart. We do this here so that a
527 * debugger will see the already changed PSW.
530 case -ERESTART_RESTARTBLOCK
:
532 case -ERESTARTNOHAND
:
534 case -ERESTARTNOINTR
:
536 regs
->ARM_r0
= regs
->ARM_ORIG_r0
;
537 regs
->ARM_pc
= restart_addr
;
543 * Get the signal to deliver. When running under ptrace, at this
544 * point the debugger may change all our registers ...
547 * Depending on the signal settings we may need to revert the
548 * decision to restart the system call. But skip this if a
549 * debugger has chosen to restart at a different PC.
551 if (get_signal(&ksig
)) {
553 if (unlikely(restart
) && regs
->ARM_pc
== restart_addr
) {
554 if (retval
== -ERESTARTNOHAND
||
555 retval
== -ERESTART_RESTARTBLOCK
556 || (retval
== -ERESTARTSYS
557 && !(ksig
.ka
.sa
.sa_flags
& SA_RESTART
))) {
558 regs
->ARM_r0
= -EINTR
;
559 regs
->ARM_pc
= continue_addr
;
562 handle_signal(&ksig
, regs
);
565 restore_saved_sigmask();
566 if (unlikely(restart
) && regs
->ARM_pc
== restart_addr
) {
567 regs
->ARM_pc
= continue_addr
;
575 do_work_pending(struct pt_regs
*regs
, unsigned int thread_flags
, int syscall
)
578 * The assembly code enters us with IRQs off, but it hasn't
579 * informed the tracing code of that for efficiency reasons.
580 * Update the trace code with the current status.
582 trace_hardirqs_off();
584 if (likely(thread_flags
& _TIF_NEED_RESCHED
)) {
587 if (unlikely(!user_mode(regs
)))
590 if (thread_flags
& _TIF_SIGPENDING
) {
591 int restart
= do_signal(regs
, syscall
);
592 if (unlikely(restart
)) {
594 * Restart without handlers.
595 * Deal with it without leaving
601 } else if (thread_flags
& _TIF_UPROBE
) {
602 uprobe_notify_resume(regs
);
604 clear_thread_flag(TIF_NOTIFY_RESUME
);
605 tracehook_notify_resume(regs
);
609 thread_flags
= current_thread_info()->flags
;
610 } while (thread_flags
& _TIF_WORK_MASK
);
614 struct page
*get_signal_page(void)
621 page
= alloc_pages(GFP_KERNEL
, 0);
626 addr
= page_address(page
);
628 /* Give the signal return code some randomness */
629 offset
= 0x200 + (get_random_int() & 0x7fc);
630 signal_return_offset
= offset
;
633 * Copy signal return handlers into the vector page, and
634 * set sigreturn to be a pointer to these.
636 memcpy(addr
+ offset
, sigreturn_codes
, sizeof(sigreturn_codes
));
638 ptr
= (unsigned long)addr
+ offset
;
639 flush_icache_range(ptr
, ptr
+ sizeof(sigreturn_codes
));