GNU Linux-libre 4.9.337-gnu1
[releases.git] / arch / arm / kernel / signal.c
1 /*
2  *  linux/arch/arm/kernel/signal.c
3  *
4  *  Copyright (C) 1995-2009 Russell King
5  *
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>
17
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>
24
25 extern const unsigned long sigreturn_codes[7];
26
27 static unsigned long signal_return_offset;
28
29 #ifdef CONFIG_CRUNCH
30 static int preserve_crunch_context(struct crunch_sigframe __user *frame)
31 {
32         char kbuf[sizeof(*frame) + 8];
33         struct crunch_sigframe *kframe;
34
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));
41 }
42
43 static int restore_crunch_context(struct crunch_sigframe __user *frame)
44 {
45         char kbuf[sizeof(*frame) + 8];
46         struct crunch_sigframe *kframe;
47
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;
57 }
58 #endif
59
60 #ifdef CONFIG_IWMMXT
61
62 static int preserve_iwmmxt_context(struct iwmmxt_sigframe *frame)
63 {
64         char kbuf[sizeof(*frame) + 8];
65         struct iwmmxt_sigframe *kframe;
66
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));
73 }
74
75 static int restore_iwmmxt_context(struct iwmmxt_sigframe *frame)
76 {
77         char kbuf[sizeof(*frame) + 8];
78         struct iwmmxt_sigframe *kframe;
79
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;
89 }
90
91 #endif
92
93 #ifdef CONFIG_VFP
94
95 static int preserve_vfp_context(struct vfp_sigframe __user *frame)
96 {
97         struct vfp_sigframe kframe;
98         int err = 0;
99
100         memset(&kframe, 0, sizeof(kframe));
101         kframe.magic = VFP_MAGIC;
102         kframe.size = VFP_STORAGE_SIZE;
103
104         err = vfp_preserve_user_clear_hwstate(&kframe.ufp, &kframe.ufp_exc);
105         if (err)
106                 return err;
107
108         return __copy_to_user(frame, &kframe, sizeof(kframe));
109 }
110
111 static int restore_vfp_context(struct vfp_sigframe __user *auxp)
112 {
113         struct vfp_sigframe frame;
114         int err;
115
116         err = __copy_from_user(&frame, (char __user *) auxp, sizeof(frame));
117
118         if (err)
119                 return err;
120
121         if (frame.magic != VFP_MAGIC || frame.size != VFP_STORAGE_SIZE)
122                 return -EINVAL;
123
124         return vfp_restore_user_hwstate(&frame.ufp, &frame.ufp_exc);
125 }
126
127 #endif
128
129 /*
130  * Do a signal return; undo the signal stack.  These are aligned to 64-bit.
131  */
132 struct sigframe {
133         struct ucontext uc;
134         unsigned long retcode[2];
135 };
136
137 struct rt_sigframe {
138         struct siginfo info;
139         struct sigframe sig;
140 };
141
142 static int restore_sigframe(struct pt_regs *regs, struct sigframe __user *sf)
143 {
144         struct sigcontext context;
145         struct aux_sigframe __user *aux;
146         sigset_t set;
147         int err;
148
149         err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
150         if (err == 0)
151                 set_current_blocked(&set);
152
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;
172         }
173
174         err |= !valid_user_regs(regs);
175
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
189
190         return err;
191 }
192
193 asmlinkage int sys_sigreturn(struct pt_regs *regs)
194 {
195         struct sigframe __user *frame;
196
197         /* Always make any pending restarted system calls return -EINTR */
198         current->restart_block.fn = do_no_restart_syscall;
199
200         /*
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.
204          */
205         if (regs->ARM_sp & 7)
206                 goto badframe;
207
208         frame = (struct sigframe __user *)regs->ARM_sp;
209
210         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
211                 goto badframe;
212
213         if (restore_sigframe(regs, frame))
214                 goto badframe;
215
216         return regs->ARM_r0;
217
218 badframe:
219         force_sig(SIGSEGV, current);
220         return 0;
221 }
222
223 asmlinkage int sys_rt_sigreturn(struct pt_regs *regs)
224 {
225         struct rt_sigframe __user *frame;
226
227         /* Always make any pending restarted system calls return -EINTR */
228         current->restart_block.fn = do_no_restart_syscall;
229
230         /*
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.
234          */
235         if (regs->ARM_sp & 7)
236                 goto badframe;
237
238         frame = (struct rt_sigframe __user *)regs->ARM_sp;
239
240         if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
241                 goto badframe;
242
243         if (restore_sigframe(regs, &frame->sig))
244                 goto badframe;
245
246         if (restore_altstack(&frame->sig.uc.uc_stack))
247                 goto badframe;
248
249         return regs->ARM_r0;
250
251 badframe:
252         force_sig(SIGSEGV, current);
253         return 0;
254 }
255
256 static int
257 setup_sigframe(struct sigframe __user *sf, struct pt_regs *regs, sigset_t *set)
258 {
259         struct aux_sigframe __user *aux;
260         struct sigcontext context;
261         int err = 0;
262
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,
281
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],
286         };
287
288         err |= __copy_to_user(&sf->uc.uc_mcontext, &context, sizeof(context));
289
290         err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
291
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);
306
307         return err;
308 }
309
310 static inline void __user *
311 get_sigframe(struct ksignal *ksig, struct pt_regs *regs, int framesize)
312 {
313         unsigned long sp = sigsp(regs->ARM_sp, ksig);
314         void __user *frame;
315
316         /*
317          * ATPCS B01 mandates 8-byte alignment
318          */
319         frame = (void __user *)((sp - framesize) & ~7);
320
321         /*
322          * Check that we can actually write to the signal frame.
323          */
324         if (!access_ok(VERIFY_WRITE, frame, framesize))
325                 frame = NULL;
326
327         return frame;
328 }
329
330 static int
331 setup_return(struct pt_regs *regs, struct ksignal *ksig,
332              unsigned long __user *rc, void __user *frame)
333 {
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);
338
339         cpsr |= PSR_ENDSTATE;
340
341         /*
342          * Maybe we need to deliver a 32-bit signal to a 26-bit task.
343          */
344         if (ksig->ka.sa.sa_flags & SA_THIRTYTWO)
345                 cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
346
347 #ifdef CONFIG_ARM_THUMB
348         if (elf_hwcap & HWCAP_THUMB) {
349                 /*
350                  * The LSB of the handler determines if we're going to
351                  * be using THUMB or ARM mode for this signal handler.
352                  */
353                 thumb = handler & 1;
354
355                 /*
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.
360                  *
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.
365                  */
366                 cpsr &= ~PSR_IT_MASK;
367
368                 if (thumb) {
369                         cpsr |= PSR_T_BIT;
370                 } else
371                         cpsr &= ~PSR_T_BIT;
372         }
373 #endif
374
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;
379
380                 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
381                         idx += 3;
382
383                 /*
384                  * Put the sigreturn code on the stack no matter which return
385                  * mechanism we use in order to remain ABI compliant
386                  */
387                 if (__put_user(sigreturn_codes[idx],   rc) ||
388                     __put_user(sigreturn_codes[idx+1], rc+1))
389                         return 1;
390
391 #ifdef CONFIG_MMU
392                 if (cpsr & MODE32_BIT) {
393                         struct mm_struct *mm = current->mm;
394
395                         /*
396                          * 32-bit code can use the signal return page
397                          * except when the MPU has protected the vectors
398                          * page from PL0
399                          */
400                         retcode = mm->context.sigpage + signal_return_offset +
401                                   (idx << 2) + thumb;
402                 } else
403 #endif
404                 {
405                         /*
406                          * Ensure that the instruction cache sees
407                          * the return code written onto the stack.
408                          */
409                         flush_icache_range((unsigned long)rc,
410                                            (unsigned long)(rc + 2));
411
412                         retcode = ((unsigned long)rc) + thumb;
413                 }
414         }
415
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;
421
422         return 0;
423 }
424
425 static int
426 setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
427 {
428         struct sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
429         int err = 0;
430
431         if (!frame)
432                 return 1;
433
434         /*
435          * Set uc.uc_flags to a value which sc.trap_no would never have.
436          */
437         err = __put_user(0x5ac3c35a, &frame->uc.uc_flags);
438
439         err |= setup_sigframe(frame, regs, set);
440         if (err == 0)
441                 err = setup_return(regs, ksig, frame->retcode, frame);
442
443         return err;
444 }
445
446 static int
447 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
448 {
449         struct rt_sigframe __user *frame = get_sigframe(ksig, regs, sizeof(*frame));
450         int err = 0;
451
452         if (!frame)
453                 return 1;
454
455         err |= copy_siginfo_to_user(&frame->info, &ksig->info);
456
457         err |= __put_user(0, &frame->sig.uc.uc_flags);
458         err |= __put_user(NULL, &frame->sig.uc.uc_link);
459
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);
464
465         if (err == 0) {
466                 /*
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
470                  */
471                 regs->ARM_r1 = (unsigned long)&frame->info;
472                 regs->ARM_r2 = (unsigned long)&frame->sig.uc;
473         }
474
475         return err;
476 }
477
478 /*
479  * OK, we're invoking a handler
480  */     
481 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
482 {
483         sigset_t *oldset = sigmask_to_save();
484         int ret;
485
486         /*
487          * Set up the stack frame
488          */
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);
493
494         /*
495          * Check that the resulting registers are actually sane.
496          */
497         ret |= !valid_user_regs(regs);
498
499         signal_setup_done(ret, ksig, 0);
500 }
501
502 /*
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.
506  *
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.
510  */
511 static int do_signal(struct pt_regs *regs, int syscall)
512 {
513         unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
514         struct ksignal ksig;
515         int restart = 0;
516
517         /*
518          * If we were from a system call, check for system call restarting...
519          */
520         if (syscall) {
521                 continue_addr = regs->ARM_pc;
522                 restart_addr = continue_addr - (thumb_mode(regs) ? 2 : 4);
523                 retval = regs->ARM_r0;
524
525                 /*
526                  * Prepare for system call restart.  We do this here so that a
527                  * debugger will see the already changed PSW.
528                  */
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;
539                 }
540         }
541
542         /*
543          * Get the signal to deliver.  When running under ptrace, at this
544          * point the debugger may change all our registers ...
545          */
546         /*
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.
550          */
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;
560                         }
561                 }
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;
569                 }
570         }
571         return 0;
572 }
573
574 asmlinkage int
575 do_work_pending(struct pt_regs *regs, unsigned int thread_flags, int syscall)
576 {
577         /*
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.
581          */
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)) {
593                                         /*
594                                          * Restart without handlers.
595                                          * Deal with it without leaving
596                                          * the kernel space.
597                                          */
598                                         return restart;
599                                 }
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);
606                         }
607                 }
608                 local_irq_disable();
609                 thread_flags = current_thread_info()->flags;
610         } while (thread_flags & _TIF_WORK_MASK);
611         return 0;
612 }
613
614 struct page *get_signal_page(void)
615 {
616         unsigned long ptr;
617         unsigned offset;
618         struct page *page;
619         void *addr;
620
621         page = alloc_pages(GFP_KERNEL, 0);
622
623         if (!page)
624                 return NULL;
625
626         addr = page_address(page);
627
628         /* Poison the entire page */
629         memset32(addr, __opcode_to_mem_arm(0xe7fddef1),
630                  PAGE_SIZE / sizeof(u32));
631
632         /* Give the signal return code some randomness */
633         offset = 0x200 + (get_random_int() & 0x7fc);
634         signal_return_offset = offset;
635
636         /* Copy signal return handlers into the page */
637         memcpy(addr + offset, sigreturn_codes, sizeof(sigreturn_codes));
638
639         /* Flush out all instructions in this page */
640         ptr = (unsigned long)addr;
641         flush_icache_range(ptr, ptr + PAGE_SIZE);
642
643         return page;
644 }