Merge branch 'davinci-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / arch / sh / kernel / ptrace_32.c
1 /*
2  * SuperH process tracing
3  *
4  * Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
5  * Copyright (C) 2002 - 2009  Paul Mundt
6  *
7  * Audit support by Yuichi Nakamura <ynakam@hitachisoft.jp>
8  *
9  * This file is subject to the terms and conditions of the GNU General Public
10  * License.  See the file "COPYING" in the main directory of this archive
11  * for more details.
12  */
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/errno.h>
18 #include <linux/ptrace.h>
19 #include <linux/user.h>
20 #include <linux/slab.h>
21 #include <linux/security.h>
22 #include <linux/signal.h>
23 #include <linux/io.h>
24 #include <linux/audit.h>
25 #include <linux/seccomp.h>
26 #include <linux/tracehook.h>
27 #include <linux/elf.h>
28 #include <linux/regset.h>
29 #include <linux/hw_breakpoint.h>
30 #include <asm/uaccess.h>
31 #include <asm/pgtable.h>
32 #include <asm/system.h>
33 #include <asm/processor.h>
34 #include <asm/mmu_context.h>
35 #include <asm/syscalls.h>
36 #include <asm/fpu.h>
37
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/syscalls.h>
40
41 /*
42  * This routine will get a word off of the process kernel stack.
43  */
44 static inline int get_stack_long(struct task_struct *task, int offset)
45 {
46         unsigned char *stack;
47
48         stack = (unsigned char *)task_pt_regs(task);
49         stack += offset;
50         return (*((int *)stack));
51 }
52
53 /*
54  * This routine will put a word on the process kernel stack.
55  */
56 static inline int put_stack_long(struct task_struct *task, int offset,
57                                  unsigned long data)
58 {
59         unsigned char *stack;
60
61         stack = (unsigned char *)task_pt_regs(task);
62         stack += offset;
63         *(unsigned long *) stack = data;
64         return 0;
65 }
66
67 void ptrace_triggered(struct perf_event *bp, int nmi,
68                       struct perf_sample_data *data, struct pt_regs *regs)
69 {
70         struct perf_event_attr attr;
71
72         /*
73          * Disable the breakpoint request here since ptrace has defined a
74          * one-shot behaviour for breakpoint exceptions.
75          */
76         attr = bp->attr;
77         attr.disabled = true;
78         modify_user_hw_breakpoint(bp, &attr);
79 }
80
81 static int set_single_step(struct task_struct *tsk, unsigned long addr)
82 {
83         struct thread_struct *thread = &tsk->thread;
84         struct perf_event *bp;
85         struct perf_event_attr attr;
86
87         bp = thread->ptrace_bps[0];
88         if (!bp) {
89                 hw_breakpoint_init(&attr);
90
91                 attr.bp_addr = addr;
92                 attr.bp_len = HW_BREAKPOINT_LEN_2;
93                 attr.bp_type = HW_BREAKPOINT_R;
94
95                 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
96                 if (IS_ERR(bp))
97                         return PTR_ERR(bp);
98
99                 thread->ptrace_bps[0] = bp;
100         } else {
101                 int err;
102
103                 attr = bp->attr;
104                 attr.bp_addr = addr;
105                 err = modify_user_hw_breakpoint(bp, &attr);
106                 if (unlikely(err))
107                         return err;
108         }
109
110         return 0;
111 }
112
113 void user_enable_single_step(struct task_struct *child)
114 {
115         unsigned long pc = get_stack_long(child, offsetof(struct pt_regs, pc));
116
117         set_tsk_thread_flag(child, TIF_SINGLESTEP);
118
119         set_single_step(child, pc);
120 }
121
122 void user_disable_single_step(struct task_struct *child)
123 {
124         clear_tsk_thread_flag(child, TIF_SINGLESTEP);
125 }
126
127 /*
128  * Called by kernel/ptrace.c when detaching..
129  *
130  * Make sure single step bits etc are not set.
131  */
132 void ptrace_disable(struct task_struct *child)
133 {
134         user_disable_single_step(child);
135 }
136
137 static int genregs_get(struct task_struct *target,
138                        const struct user_regset *regset,
139                        unsigned int pos, unsigned int count,
140                        void *kbuf, void __user *ubuf)
141 {
142         const struct pt_regs *regs = task_pt_regs(target);
143         int ret;
144
145         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
146                                   regs->regs,
147                                   0, 16 * sizeof(unsigned long));
148         if (!ret)
149                 /* PC, PR, SR, GBR, MACH, MACL, TRA */
150                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
151                                           &regs->pc,
152                                           offsetof(struct pt_regs, pc),
153                                           sizeof(struct pt_regs));
154         if (!ret)
155                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
156                                                sizeof(struct pt_regs), -1);
157
158         return ret;
159 }
160
161 static int genregs_set(struct task_struct *target,
162                        const struct user_regset *regset,
163                        unsigned int pos, unsigned int count,
164                        const void *kbuf, const void __user *ubuf)
165 {
166         struct pt_regs *regs = task_pt_regs(target);
167         int ret;
168
169         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
170                                  regs->regs,
171                                  0, 16 * sizeof(unsigned long));
172         if (!ret && count > 0)
173                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
174                                          &regs->pc,
175                                          offsetof(struct pt_regs, pc),
176                                          sizeof(struct pt_regs));
177         if (!ret)
178                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
179                                                 sizeof(struct pt_regs), -1);
180
181         return ret;
182 }
183
184 #ifdef CONFIG_SH_FPU
185 int fpregs_get(struct task_struct *target,
186                const struct user_regset *regset,
187                unsigned int pos, unsigned int count,
188                void *kbuf, void __user *ubuf)
189 {
190         int ret;
191
192         ret = init_fpu(target);
193         if (ret)
194                 return ret;
195
196         if ((boot_cpu_data.flags & CPU_HAS_FPU))
197                 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
198                                            &target->thread.xstate->hardfpu, 0, -1);
199
200         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
201                                    &target->thread.xstate->softfpu, 0, -1);
202 }
203
204 static int fpregs_set(struct task_struct *target,
205                        const struct user_regset *regset,
206                        unsigned int pos, unsigned int count,
207                        const void *kbuf, const void __user *ubuf)
208 {
209         int ret;
210
211         ret = init_fpu(target);
212         if (ret)
213                 return ret;
214
215         set_stopped_child_used_math(target);
216
217         if ((boot_cpu_data.flags & CPU_HAS_FPU))
218                 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
219                                           &target->thread.xstate->hardfpu, 0, -1);
220
221         return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
222                                   &target->thread.xstate->softfpu, 0, -1);
223 }
224
225 static int fpregs_active(struct task_struct *target,
226                          const struct user_regset *regset)
227 {
228         return tsk_used_math(target) ? regset->n : 0;
229 }
230 #endif
231
232 #ifdef CONFIG_SH_DSP
233 static int dspregs_get(struct task_struct *target,
234                        const struct user_regset *regset,
235                        unsigned int pos, unsigned int count,
236                        void *kbuf, void __user *ubuf)
237 {
238         const struct pt_dspregs *regs =
239                 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
240         int ret;
241
242         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
243                                   0, sizeof(struct pt_dspregs));
244         if (!ret)
245                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
246                                                sizeof(struct pt_dspregs), -1);
247
248         return ret;
249 }
250
251 static int dspregs_set(struct task_struct *target,
252                        const struct user_regset *regset,
253                        unsigned int pos, unsigned int count,
254                        const void *kbuf, const void __user *ubuf)
255 {
256         struct pt_dspregs *regs =
257                 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
258         int ret;
259
260         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
261                                  0, sizeof(struct pt_dspregs));
262         if (!ret)
263                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
264                                                 sizeof(struct pt_dspregs), -1);
265
266         return ret;
267 }
268
269 static int dspregs_active(struct task_struct *target,
270                           const struct user_regset *regset)
271 {
272         struct pt_regs *regs = task_pt_regs(target);
273
274         return regs->sr & SR_DSP ? regset->n : 0;
275 }
276 #endif
277
278 /*
279  * These are our native regset flavours.
280  */
281 enum sh_regset {
282         REGSET_GENERAL,
283 #ifdef CONFIG_SH_FPU
284         REGSET_FPU,
285 #endif
286 #ifdef CONFIG_SH_DSP
287         REGSET_DSP,
288 #endif
289 };
290
291 static const struct user_regset sh_regsets[] = {
292         /*
293          * Format is:
294          *      R0 --> R15
295          *      PC, PR, SR, GBR, MACH, MACL, TRA
296          */
297         [REGSET_GENERAL] = {
298                 .core_note_type = NT_PRSTATUS,
299                 .n              = ELF_NGREG,
300                 .size           = sizeof(long),
301                 .align          = sizeof(long),
302                 .get            = genregs_get,
303                 .set            = genregs_set,
304         },
305
306 #ifdef CONFIG_SH_FPU
307         [REGSET_FPU] = {
308                 .core_note_type = NT_PRFPREG,
309                 .n              = sizeof(struct user_fpu_struct) / sizeof(long),
310                 .size           = sizeof(long),
311                 .align          = sizeof(long),
312                 .get            = fpregs_get,
313                 .set            = fpregs_set,
314                 .active         = fpregs_active,
315         },
316 #endif
317
318 #ifdef CONFIG_SH_DSP
319         [REGSET_DSP] = {
320                 .n              = sizeof(struct pt_dspregs) / sizeof(long),
321                 .size           = sizeof(long),
322                 .align          = sizeof(long),
323                 .get            = dspregs_get,
324                 .set            = dspregs_set,
325                 .active         = dspregs_active,
326         },
327 #endif
328 };
329
330 static const struct user_regset_view user_sh_native_view = {
331         .name           = "sh",
332         .e_machine      = EM_SH,
333         .regsets        = sh_regsets,
334         .n              = ARRAY_SIZE(sh_regsets),
335 };
336
337 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
338 {
339         return &user_sh_native_view;
340 }
341
342 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
343 {
344         struct user * dummy = NULL;
345         unsigned long __user *datap = (unsigned long __user *)data;
346         int ret;
347
348         switch (request) {
349         /* read the word at location addr in the USER area. */
350         case PTRACE_PEEKUSR: {
351                 unsigned long tmp;
352
353                 ret = -EIO;
354                 if ((addr & 3) || addr < 0 ||
355                     addr > sizeof(struct user) - 3)
356                         break;
357
358                 if (addr < sizeof(struct pt_regs))
359                         tmp = get_stack_long(child, addr);
360                 else if (addr >= (long) &dummy->fpu &&
361                          addr < (long) &dummy->u_fpvalid) {
362                         if (!tsk_used_math(child)) {
363                                 if (addr == (long)&dummy->fpu.fpscr)
364                                         tmp = FPSCR_INIT;
365                                 else
366                                         tmp = 0;
367                         } else
368                                 tmp = ((long *)child->thread.xstate)
369                                         [(addr - (long)&dummy->fpu) >> 2];
370                 } else if (addr == (long) &dummy->u_fpvalid)
371                         tmp = !!tsk_used_math(child);
372                 else if (addr == PT_TEXT_ADDR)
373                         tmp = child->mm->start_code;
374                 else if (addr == PT_DATA_ADDR)
375                         tmp = child->mm->start_data;
376                 else if (addr == PT_TEXT_END_ADDR)
377                         tmp = child->mm->end_code;
378                 else if (addr == PT_TEXT_LEN)
379                         tmp = child->mm->end_code - child->mm->start_code;
380                 else
381                         tmp = 0;
382                 ret = put_user(tmp, datap);
383                 break;
384         }
385
386         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
387                 ret = -EIO;
388                 if ((addr & 3) || addr < 0 ||
389                     addr > sizeof(struct user) - 3)
390                         break;
391
392                 if (addr < sizeof(struct pt_regs))
393                         ret = put_stack_long(child, addr, data);
394                 else if (addr >= (long) &dummy->fpu &&
395                          addr < (long) &dummy->u_fpvalid) {
396                         set_stopped_child_used_math(child);
397                         ((long *)child->thread.xstate)
398                                 [(addr - (long)&dummy->fpu) >> 2] = data;
399                         ret = 0;
400                 } else if (addr == (long) &dummy->u_fpvalid) {
401                         conditional_stopped_child_used_math(data, child);
402                         ret = 0;
403                 }
404                 break;
405
406         case PTRACE_GETREGS:
407                 return copy_regset_to_user(child, &user_sh_native_view,
408                                            REGSET_GENERAL,
409                                            0, sizeof(struct pt_regs),
410                                            (void __user *)data);
411         case PTRACE_SETREGS:
412                 return copy_regset_from_user(child, &user_sh_native_view,
413                                              REGSET_GENERAL,
414                                              0, sizeof(struct pt_regs),
415                                              (const void __user *)data);
416 #ifdef CONFIG_SH_FPU
417         case PTRACE_GETFPREGS:
418                 return copy_regset_to_user(child, &user_sh_native_view,
419                                            REGSET_FPU,
420                                            0, sizeof(struct user_fpu_struct),
421                                            (void __user *)data);
422         case PTRACE_SETFPREGS:
423                 return copy_regset_from_user(child, &user_sh_native_view,
424                                              REGSET_FPU,
425                                              0, sizeof(struct user_fpu_struct),
426                                              (const void __user *)data);
427 #endif
428 #ifdef CONFIG_SH_DSP
429         case PTRACE_GETDSPREGS:
430                 return copy_regset_to_user(child, &user_sh_native_view,
431                                            REGSET_DSP,
432                                            0, sizeof(struct pt_dspregs),
433                                            (void __user *)data);
434         case PTRACE_SETDSPREGS:
435                 return copy_regset_from_user(child, &user_sh_native_view,
436                                              REGSET_DSP,
437                                              0, sizeof(struct pt_dspregs),
438                                              (const void __user *)data);
439 #endif
440 #ifdef CONFIG_BINFMT_ELF_FDPIC
441         case PTRACE_GETFDPIC: {
442                 unsigned long tmp = 0;
443
444                 switch (addr) {
445                 case PTRACE_GETFDPIC_EXEC:
446                         tmp = child->mm->context.exec_fdpic_loadmap;
447                         break;
448                 case PTRACE_GETFDPIC_INTERP:
449                         tmp = child->mm->context.interp_fdpic_loadmap;
450                         break;
451                 default:
452                         break;
453                 }
454
455                 ret = 0;
456                 if (put_user(tmp, datap)) {
457                         ret = -EFAULT;
458                         break;
459                 }
460                 break;
461         }
462 #endif
463         default:
464                 ret = ptrace_request(child, request, addr, data);
465                 break;
466         }
467
468         return ret;
469 }
470
471 static inline int audit_arch(void)
472 {
473         int arch = EM_SH;
474
475 #ifdef CONFIG_CPU_LITTLE_ENDIAN
476         arch |= __AUDIT_ARCH_LE;
477 #endif
478
479         return arch;
480 }
481
482 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
483 {
484         long ret = 0;
485
486         secure_computing(regs->regs[0]);
487
488         if (test_thread_flag(TIF_SYSCALL_TRACE) &&
489             tracehook_report_syscall_entry(regs))
490                 /*
491                  * Tracing decided this syscall should not happen.
492                  * We'll return a bogus call number to get an ENOSYS
493                  * error, but leave the original number in regs->regs[0].
494                  */
495                 ret = -1L;
496
497         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
498                 trace_sys_enter(regs, regs->regs[0]);
499
500         if (unlikely(current->audit_context))
501                 audit_syscall_entry(audit_arch(), regs->regs[3],
502                                     regs->regs[4], regs->regs[5],
503                                     regs->regs[6], regs->regs[7]);
504
505         return ret ?: regs->regs[0];
506 }
507
508 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
509 {
510         int step;
511
512         if (unlikely(current->audit_context))
513                 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
514                                    regs->regs[0]);
515
516         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
517                 trace_sys_exit(regs, regs->regs[0]);
518
519         step = test_thread_flag(TIF_SINGLESTEP);
520         if (step || test_thread_flag(TIF_SYSCALL_TRACE))
521                 tracehook_report_syscall_exit(regs, step);
522 }