Merge branch 'davinci-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / arch / sh / kernel / ptrace_64.c
1 /*
2  * arch/sh/kernel/ptrace_64.c
3  *
4  * Copyright (C) 2000, 2001  Paolo Alberelli
5  * Copyright (C) 2003 - 2008  Paul Mundt
6  *
7  * Started from SH3/4 version:
8  *   SuperH version:   Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
9  *
10  *   Original x86 implementation:
11  *      By Ross Biro 1/23/92
12  *      edited by Linus Torvalds
13  *
14  * This file is subject to the terms and conditions of the GNU General Public
15  * License.  See the file "COPYING" in the main directory of this archive
16  * for more details.
17  */
18 #include <linux/kernel.h>
19 #include <linux/rwsem.h>
20 #include <linux/sched.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/errno.h>
25 #include <linux/ptrace.h>
26 #include <linux/user.h>
27 #include <linux/signal.h>
28 #include <linux/syscalls.h>
29 #include <linux/audit.h>
30 #include <linux/seccomp.h>
31 #include <linux/tracehook.h>
32 #include <linux/elf.h>
33 #include <linux/regset.h>
34 #include <asm/io.h>
35 #include <asm/uaccess.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
38 #include <asm/processor.h>
39 #include <asm/mmu_context.h>
40 #include <asm/syscalls.h>
41 #include <asm/fpu.h>
42
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/syscalls.h>
45
46 /* This mask defines the bits of the SR which the user is not allowed to
47    change, which are everything except S, Q, M, PR, SZ, FR. */
48 #define SR_MASK      (0xffff8cfd)
49
50 /*
51  * does not yet catch signals sent when the child dies.
52  * in exit.c or in signal.c.
53  */
54
55 /*
56  * This routine will get a word from the user area in the process kernel stack.
57  */
58 static inline int get_stack_long(struct task_struct *task, int offset)
59 {
60         unsigned char *stack;
61
62         stack = (unsigned char *)(task->thread.uregs);
63         stack += offset;
64         return (*((int *)stack));
65 }
66
67 static inline unsigned long
68 get_fpu_long(struct task_struct *task, unsigned long addr)
69 {
70         unsigned long tmp;
71         struct pt_regs *regs;
72         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
73
74         if (!tsk_used_math(task)) {
75                 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
76                         tmp = FPSCR_INIT;
77                 } else {
78                         tmp = 0xffffffffUL; /* matches initial value in fpu.c */
79                 }
80                 return tmp;
81         }
82
83         if (last_task_used_math == task) {
84                 enable_fpu();
85                 save_fpu(task);
86                 disable_fpu();
87                 last_task_used_math = 0;
88                 regs->sr |= SR_FD;
89         }
90
91         tmp = ((long *)task->thread.xstate)[addr / sizeof(unsigned long)];
92         return tmp;
93 }
94
95 /*
96  * This routine will put a word into the user area in the process kernel stack.
97  */
98 static inline int put_stack_long(struct task_struct *task, int offset,
99                                  unsigned long data)
100 {
101         unsigned char *stack;
102
103         stack = (unsigned char *)(task->thread.uregs);
104         stack += offset;
105         *(unsigned long *) stack = data;
106         return 0;
107 }
108
109 static inline int
110 put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
111 {
112         struct pt_regs *regs;
113
114         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
115
116         if (!tsk_used_math(task)) {
117                 init_fpu(task);
118         } else if (last_task_used_math == task) {
119                 enable_fpu();
120                 save_fpu(task);
121                 disable_fpu();
122                 last_task_used_math = 0;
123                 regs->sr |= SR_FD;
124         }
125
126         ((long *)task->thread.xstate)[addr / sizeof(unsigned long)] = data;
127         return 0;
128 }
129
130 void user_enable_single_step(struct task_struct *child)
131 {
132         struct pt_regs *regs = child->thread.uregs;
133
134         regs->sr |= SR_SSTEP;   /* auto-resetting upon exception */
135
136         set_tsk_thread_flag(child, TIF_SINGLESTEP);
137 }
138
139 void user_disable_single_step(struct task_struct *child)
140 {
141         struct pt_regs *regs = child->thread.uregs;
142
143         regs->sr &= ~SR_SSTEP;
144
145         clear_tsk_thread_flag(child, TIF_SINGLESTEP);
146 }
147
148 static int genregs_get(struct task_struct *target,
149                        const struct user_regset *regset,
150                        unsigned int pos, unsigned int count,
151                        void *kbuf, void __user *ubuf)
152 {
153         const struct pt_regs *regs = task_pt_regs(target);
154         int ret;
155
156         /* PC, SR, SYSCALL */
157         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
158                                   &regs->pc,
159                                   0, 3 * sizeof(unsigned long long));
160
161         /* R1 -> R63 */
162         if (!ret)
163                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
164                                           regs->regs,
165                                           offsetof(struct pt_regs, regs[0]),
166                                           63 * sizeof(unsigned long long));
167         /* TR0 -> TR7 */
168         if (!ret)
169                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
170                                           regs->tregs,
171                                           offsetof(struct pt_regs, tregs[0]),
172                                           8 * sizeof(unsigned long long));
173
174         if (!ret)
175                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
176                                                sizeof(struct pt_regs), -1);
177
178         return ret;
179 }
180
181 static int genregs_set(struct task_struct *target,
182                        const struct user_regset *regset,
183                        unsigned int pos, unsigned int count,
184                        const void *kbuf, const void __user *ubuf)
185 {
186         struct pt_regs *regs = task_pt_regs(target);
187         int ret;
188
189         /* PC, SR, SYSCALL */
190         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
191                                  &regs->pc,
192                                  0, 3 * sizeof(unsigned long long));
193
194         /* R1 -> R63 */
195         if (!ret && count > 0)
196                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
197                                          regs->regs,
198                                          offsetof(struct pt_regs, regs[0]),
199                                          63 * sizeof(unsigned long long));
200
201         /* TR0 -> TR7 */
202         if (!ret && count > 0)
203                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
204                                          regs->tregs,
205                                          offsetof(struct pt_regs, tregs[0]),
206                                          8 * sizeof(unsigned long long));
207
208         if (!ret)
209                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
210                                                 sizeof(struct pt_regs), -1);
211
212         return ret;
213 }
214
215 #ifdef CONFIG_SH_FPU
216 int fpregs_get(struct task_struct *target,
217                const struct user_regset *regset,
218                unsigned int pos, unsigned int count,
219                void *kbuf, void __user *ubuf)
220 {
221         int ret;
222
223         ret = init_fpu(target);
224         if (ret)
225                 return ret;
226
227         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
228                                    &target->thread.xstate->hardfpu, 0, -1);
229 }
230
231 static int fpregs_set(struct task_struct *target,
232                        const struct user_regset *regset,
233                        unsigned int pos, unsigned int count,
234                        const void *kbuf, const void __user *ubuf)
235 {
236         int ret;
237
238         ret = init_fpu(target);
239         if (ret)
240                 return ret;
241
242         set_stopped_child_used_math(target);
243
244         return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
245                                   &target->thread.xstate->hardfpu, 0, -1);
246 }
247
248 static int fpregs_active(struct task_struct *target,
249                          const struct user_regset *regset)
250 {
251         return tsk_used_math(target) ? regset->n : 0;
252 }
253 #endif
254
255 /*
256  * These are our native regset flavours.
257  */
258 enum sh_regset {
259         REGSET_GENERAL,
260 #ifdef CONFIG_SH_FPU
261         REGSET_FPU,
262 #endif
263 };
264
265 static const struct user_regset sh_regsets[] = {
266         /*
267          * Format is:
268          *      PC, SR, SYSCALL,
269          *      R1 --> R63,
270          *      TR0 --> TR7,
271          */
272         [REGSET_GENERAL] = {
273                 .core_note_type = NT_PRSTATUS,
274                 .n              = ELF_NGREG,
275                 .size           = sizeof(long long),
276                 .align          = sizeof(long long),
277                 .get            = genregs_get,
278                 .set            = genregs_set,
279         },
280
281 #ifdef CONFIG_SH_FPU
282         [REGSET_FPU] = {
283                 .core_note_type = NT_PRFPREG,
284                 .n              = sizeof(struct user_fpu_struct) /
285                                   sizeof(long long),
286                 .size           = sizeof(long long),
287                 .align          = sizeof(long long),
288                 .get            = fpregs_get,
289                 .set            = fpregs_set,
290                 .active         = fpregs_active,
291         },
292 #endif
293 };
294
295 static const struct user_regset_view user_sh64_native_view = {
296         .name           = "sh64",
297         .e_machine      = EM_SH,
298         .regsets        = sh_regsets,
299         .n              = ARRAY_SIZE(sh_regsets),
300 };
301
302 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
303 {
304         return &user_sh64_native_view;
305 }
306
307 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
308 {
309         int ret;
310
311         switch (request) {
312         /* read the word at location addr in the USER area. */
313         case PTRACE_PEEKUSR: {
314                 unsigned long tmp;
315
316                 ret = -EIO;
317                 if ((addr & 3) || addr < 0)
318                         break;
319
320                 if (addr < sizeof(struct pt_regs))
321                         tmp = get_stack_long(child, addr);
322                 else if ((addr >= offsetof(struct user, fpu)) &&
323                          (addr <  offsetof(struct user, u_fpvalid))) {
324                         tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
325                 } else if (addr == offsetof(struct user, u_fpvalid)) {
326                         tmp = !!tsk_used_math(child);
327                 } else {
328                         break;
329                 }
330                 ret = put_user(tmp, (unsigned long *)data);
331                 break;
332         }
333
334         case PTRACE_POKEUSR:
335                 /* write the word at location addr in the USER area. We must
336                    disallow any changes to certain SR bits or u_fpvalid, since
337                    this could crash the kernel or result in a security
338                    loophole. */
339                 ret = -EIO;
340                 if ((addr & 3) || addr < 0)
341                         break;
342
343                 if (addr < sizeof(struct pt_regs)) {
344                         /* Ignore change of top 32 bits of SR */
345                         if (addr == offsetof (struct pt_regs, sr)+4)
346                         {
347                                 ret = 0;
348                                 break;
349                         }
350                         /* If lower 32 bits of SR, ignore non-user bits */
351                         if (addr == offsetof (struct pt_regs, sr))
352                         {
353                                 long cursr = get_stack_long(child, addr);
354                                 data &= ~(SR_MASK);
355                                 data |= (cursr & SR_MASK);
356                         }
357                         ret = put_stack_long(child, addr, data);
358                 }
359                 else if ((addr >= offsetof(struct user, fpu)) &&
360                          (addr <  offsetof(struct user, u_fpvalid))) {
361                         ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
362                 }
363                 break;
364
365         case PTRACE_GETREGS:
366                 return copy_regset_to_user(child, &user_sh64_native_view,
367                                            REGSET_GENERAL,
368                                            0, sizeof(struct pt_regs),
369                                            (void __user *)data);
370         case PTRACE_SETREGS:
371                 return copy_regset_from_user(child, &user_sh64_native_view,
372                                              REGSET_GENERAL,
373                                              0, sizeof(struct pt_regs),
374                                              (const void __user *)data);
375 #ifdef CONFIG_SH_FPU
376         case PTRACE_GETFPREGS:
377                 return copy_regset_to_user(child, &user_sh64_native_view,
378                                            REGSET_FPU,
379                                            0, sizeof(struct user_fpu_struct),
380                                            (void __user *)data);
381         case PTRACE_SETFPREGS:
382                 return copy_regset_from_user(child, &user_sh64_native_view,
383                                              REGSET_FPU,
384                                              0, sizeof(struct user_fpu_struct),
385                                              (const void __user *)data);
386 #endif
387         default:
388                 ret = ptrace_request(child, request, addr, data);
389                 break;
390         }
391
392         return ret;
393 }
394
395 asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
396 {
397 #define WPC_DBRMODE 0x0d104008
398         static int first_call = 1;
399
400         lock_kernel();
401         if (first_call) {
402                 /* Set WPC.DBRMODE to 0.  This makes all debug events get
403                  * delivered through RESVEC, i.e. into the handlers in entry.S.
404                  * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
405                  * would normally be left set to 1, which makes debug events get
406                  * delivered through DBRVEC, i.e. into the remote gdb's
407                  * handlers.  This prevents ptrace getting them, and confuses
408                  * the remote gdb.) */
409                 printk("DBRMODE set to 0 to permit native debugging\n");
410                 poke_real_address_q(WPC_DBRMODE, 0);
411                 first_call = 0;
412         }
413         unlock_kernel();
414
415         return sys_ptrace(request, pid, addr, data);
416 }
417
418 static inline int audit_arch(void)
419 {
420         int arch = EM_SH;
421
422 #ifdef CONFIG_64BIT
423         arch |= __AUDIT_ARCH_64BIT;
424 #endif
425 #ifdef CONFIG_CPU_LITTLE_ENDIAN
426         arch |= __AUDIT_ARCH_LE;
427 #endif
428
429         return arch;
430 }
431
432 asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
433 {
434         long long ret = 0;
435
436         secure_computing(regs->regs[9]);
437
438         if (test_thread_flag(TIF_SYSCALL_TRACE) &&
439             tracehook_report_syscall_entry(regs))
440                 /*
441                  * Tracing decided this syscall should not happen.
442                  * We'll return a bogus call number to get an ENOSYS
443                  * error, but leave the original number in regs->regs[0].
444                  */
445                 ret = -1LL;
446
447         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
448                 trace_sys_enter(regs, regs->regs[9]);
449
450         if (unlikely(current->audit_context))
451                 audit_syscall_entry(audit_arch(), regs->regs[1],
452                                     regs->regs[2], regs->regs[3],
453                                     regs->regs[4], regs->regs[5]);
454
455         return ret ?: regs->regs[9];
456 }
457
458 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
459 {
460         int step;
461
462         if (unlikely(current->audit_context))
463                 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
464                                    regs->regs[9]);
465
466         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
467                 trace_sys_exit(regs, regs->regs[9]);
468
469         step = test_thread_flag(TIF_SINGLESTEP);
470         if (step || test_thread_flag(TIF_SYSCALL_TRACE))
471                 tracehook_report_syscall_exit(regs, step);
472 }
473
474 /* Called with interrupts disabled */
475 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
476 {
477         /* This is called after a single step exception (DEBUGSS).
478            There is no need to change the PC, as it is a post-execution
479            exception, as entry.S does not do anything to the PC for DEBUGSS.
480            We need to clear the Single Step setting in SR to avoid
481            continually stepping. */
482         local_irq_enable();
483         regs->sr &= ~SR_SSTEP;
484         force_sig(SIGTRAP, current);
485 }
486
487 /* Called with interrupts disabled */
488 BUILD_TRAP_HANDLER(breakpoint)
489 {
490         TRAP_HANDLER_DECL;
491
492         /* We need to forward step the PC, to counteract the backstep done
493            in signal.c. */
494         local_irq_enable();
495         force_sig(SIGTRAP, current);
496         regs->pc += 4;
497 }
498
499 /*
500  * Called by kernel/ptrace.c when detaching..
501  *
502  * Make sure single step bits etc are not set.
503  */
504 void ptrace_disable(struct task_struct *child)
505 {
506         user_disable_single_step(child);
507 }