Revert "ALSA: hda: Flush interrupts on disabling"
[linux/fpc-iii.git] / arch / arm / kernel / signal.c
blob0a066f03b5ec9b2d0db2627c8813a833993623e3
1 /*
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.
9 */
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>
18 #include <asm/elf.h>
19 #include <asm/cacheflush.h>
20 #include <asm/traps.h>
21 #include <asm/ucontext.h>
22 #include <asm/unistd.h>
23 #include <asm/vfp.h>
25 extern const unsigned long sigreturn_codes[7];
27 static unsigned long signal_return_offset;
29 #ifdef CONFIG_CRUNCH
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)))
51 return -1;
52 if (kframe->magic != CRUNCH_MAGIC ||
53 kframe->size != CRUNCH_STORAGE_SIZE)
54 return -1;
55 crunch_task_restore(current_thread_info(), &kframe->storage);
56 return 0;
58 #endif
60 #ifdef CONFIG_IWMMXT
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)))
83 return -1;
84 if (kframe->magic != IWMMXT_MAGIC ||
85 kframe->size != IWMMXT_STORAGE_SIZE)
86 return -1;
87 iwmmxt_task_restore(current_thread_info(), &kframe->storage);
88 return 0;
91 #endif
93 #ifdef CONFIG_VFP
95 static int preserve_vfp_context(struct vfp_sigframe __user *frame)
97 struct vfp_sigframe kframe;
98 int err = 0;
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);
105 if (err)
106 return err;
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;
114 int err;
116 err = __copy_from_user(&frame, (char __user *) auxp, sizeof(frame));
118 if (err)
119 return err;
121 if (frame.magic != VFP_MAGIC || frame.size != VFP_STORAGE_SIZE)
122 return -EINVAL;
124 return vfp_restore_user_hwstate(&frame.ufp, &frame.ufp_exc);
127 #endif
130 * Do a signal return; undo the signal stack. These are aligned to 64-bit.
132 struct sigframe {
133 struct ucontext uc;
134 unsigned long retcode[2];
137 struct rt_sigframe {
138 struct siginfo info;
139 struct sigframe sig;
142 static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
144 struct sigcontext context;
145 struct aux_sigframe __user *aux;
146 sigset_t set;
147 int err;
149 err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
150 if (err == 0)
151 set_current_blocked(&set);
153 err |= __copy_from_user(&context, &sf->uc.uc_mcontext, sizeof(context));
154 if (err == 0) {
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;
177 #ifdef CONFIG_CRUNCH
178 if (err == 0)
179 err |= restore_crunch_context(&aux->crunch);
180 #endif
181 #ifdef CONFIG_IWMMXT
182 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
183 err |= restore_iwmmxt_context(&aux->iwmmxt);
184 #endif
185 #ifdef CONFIG_VFP
186 if (err == 0)
187 err |= restore_vfp_context(&aux->vfp);
188 #endif
190 return err;
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)
206 goto badframe;
208 frame = (struct sigframe __user *)regs->ARM_sp;
210 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
211 goto badframe;
213 if (restore_sigframe(regs, frame))
214 goto badframe;
216 return regs->ARM_r0;
218 badframe:
219 force_sig(SIGSEGV, current);
220 return 0;
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)
236 goto badframe;
238 frame = (struct rt_sigframe __user *)regs->ARM_sp;
240 if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
241 goto badframe;
243 if (restore_sigframe(regs, &frame->sig))
244 goto badframe;
246 if (restore_altstack(&frame->sig.uc.uc_stack))
247 goto badframe;
249 return regs->ARM_r0;
251 badframe:
252 force_sig(SIGSEGV, current);
253 return 0;
256 static int
257 setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
259 struct aux_sigframe __user *aux;
260 struct sigcontext context;
261 int err = 0;
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;
293 #ifdef CONFIG_CRUNCH
294 if (err == 0)
295 err |= preserve_crunch_context(&aux->crunch);
296 #endif
297 #ifdef CONFIG_IWMMXT
298 if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
299 err |= preserve_iwmmxt_context(&aux->iwmmxt);
300 #endif
301 #ifdef CONFIG_VFP
302 if (err == 0)
303 err |= preserve_vfp_context(&aux->vfp);
304 #endif
305 err |= __put_user(0, &aux->end_magic);
307 return err;
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);
314 void __user *frame;
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))
325 frame = NULL;
327 return frame;
330 static int
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;
336 int thumb = 0;
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.
353 thumb = handler & 1;
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
359 * without this.
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;
368 if (thumb) {
369 cpsr |= PSR_T_BIT;
370 } else
371 cpsr &= ~PSR_T_BIT;
373 #endif
375 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
376 retcode = (unsigned long)ksig->ka.sa.sa_restorer;
377 } else {
378 unsigned int idx = thumb << 1;
380 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
381 idx += 3;
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))
389 return 1;
391 #ifdef CONFIG_MMU
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
398 * page from PL0
400 retcode = mm->context.sigpage + signal_return_offset +
401 (idx << 2) + thumb;
402 } else
403 #endif
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;
422 return 0;
425 static int
426 setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
428 struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
429 int err = 0;
431 if (!frame)
432 return 1;
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);
440 if (err == 0)
441 err = setup_return(regs, ksig, frame->retcode, frame);
443 return err;
446 static int
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));
450 int err = 0;
452 if (!frame)
453 return 1;
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);
462 if (err == 0)
463 err = setup_return(regs, ksig, frame->sig.retcode, frame);
465 if (err == 0) {
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;
475 return err;
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();
484 int ret;
487 * Set up the stack frame
489 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
490 ret = setup_rt_frame(ksig, oldset, regs);
491 else
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
505 * mistake.
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;
514 struct ksignal ksig;
515 int restart = 0;
518 * If we were from a system call, check for system call restarting...
520 if (syscall) {
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.
529 switch (retval) {
530 case -ERESTART_RESTARTBLOCK:
531 restart -= 2;
532 case -ERESTARTNOHAND:
533 case -ERESTARTSYS:
534 case -ERESTARTNOINTR:
535 restart++;
536 regs->ARM_r0 = regs->ARM_ORIG_r0;
537 regs->ARM_pc = restart_addr;
538 break;
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)) {
552 /* handler */
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);
563 } else {
564 /* no handler */
565 restore_saved_sigmask();
566 if (unlikely(restart) && regs->ARM_pc == restart_addr) {
567 regs->ARM_pc = continue_addr;
568 return restart;
571 return 0;
574 asmlinkage int
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();
583 do {
584 if (likely(thread_flags & _TIF_NEED_RESCHED)) {
585 schedule();
586 } else {
587 if (unlikely(!user_mode(regs)))
588 return 0;
589 local_irq_enable();
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
596 * the kernel space.
598 return restart;
600 syscall = 0;
601 } else if (thread_flags & _TIF_UPROBE) {
602 uprobe_notify_resume(regs);
603 } else {
604 clear_thread_flag(TIF_NOTIFY_RESUME);
605 tracehook_notify_resume(regs);
608 local_irq_disable();
609 thread_flags = current_thread_info()->flags;
610 } while (thread_flags & _TIF_WORK_MASK);
611 return 0;
614 struct page *get_signal_page(void)
616 unsigned long ptr;
617 unsigned offset;
618 struct page *page;
619 void *addr;
621 page = alloc_pages(GFP_KERNEL, 0);
623 if (!page)
624 return NULL;
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));
641 return page;