Merge branches 'sh/dwarf-unwinder', 'sh/g3-prep' and 'sh/stable-updates'
[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
137 void user_disable_single_step(struct task_struct *child)
138 {
139         struct pt_regs *regs = child->thread.uregs;
140
141         regs->sr &= ~SR_SSTEP;
142 }
143
144 static int genregs_get(struct task_struct *target,
145                        const struct user_regset *regset,
146                        unsigned int pos, unsigned int count,
147                        void *kbuf, void __user *ubuf)
148 {
149         const struct pt_regs *regs = task_pt_regs(target);
150         int ret;
151
152         /* PC, SR, SYSCALL */
153         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
154                                   &regs->pc,
155                                   0, 3 * sizeof(unsigned long long));
156
157         /* R1 -> R63 */
158         if (!ret)
159                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
160                                           regs->regs,
161                                           offsetof(struct pt_regs, regs[0]),
162                                           63 * sizeof(unsigned long long));
163         /* TR0 -> TR7 */
164         if (!ret)
165                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
166                                           regs->tregs,
167                                           offsetof(struct pt_regs, tregs[0]),
168                                           8 * sizeof(unsigned long long));
169
170         if (!ret)
171                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
172                                                sizeof(struct pt_regs), -1);
173
174         return ret;
175 }
176
177 static int genregs_set(struct task_struct *target,
178                        const struct user_regset *regset,
179                        unsigned int pos, unsigned int count,
180                        const void *kbuf, const void __user *ubuf)
181 {
182         struct pt_regs *regs = task_pt_regs(target);
183         int ret;
184
185         /* PC, SR, SYSCALL */
186         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
187                                  &regs->pc,
188                                  0, 3 * sizeof(unsigned long long));
189
190         /* R1 -> R63 */
191         if (!ret && count > 0)
192                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
193                                          regs->regs,
194                                          offsetof(struct pt_regs, regs[0]),
195                                          63 * sizeof(unsigned long long));
196
197         /* TR0 -> TR7 */
198         if (!ret && count > 0)
199                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
200                                          regs->tregs,
201                                          offsetof(struct pt_regs, tregs[0]),
202                                          8 * sizeof(unsigned long long));
203
204         if (!ret)
205                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
206                                                 sizeof(struct pt_regs), -1);
207
208         return ret;
209 }
210
211 #ifdef CONFIG_SH_FPU
212 int fpregs_get(struct task_struct *target,
213                const struct user_regset *regset,
214                unsigned int pos, unsigned int count,
215                void *kbuf, void __user *ubuf)
216 {
217         int ret;
218
219         ret = init_fpu(target);
220         if (ret)
221                 return ret;
222
223         return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
224                                    &target->thread.xstate->hardfpu, 0, -1);
225 }
226
227 static int fpregs_set(struct task_struct *target,
228                        const struct user_regset *regset,
229                        unsigned int pos, unsigned int count,
230                        const void *kbuf, const void __user *ubuf)
231 {
232         int ret;
233
234         ret = init_fpu(target);
235         if (ret)
236                 return ret;
237
238         set_stopped_child_used_math(target);
239
240         return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
241                                   &target->thread.xstate->hardfpu, 0, -1);
242 }
243
244 static int fpregs_active(struct task_struct *target,
245                          const struct user_regset *regset)
246 {
247         return tsk_used_math(target) ? regset->n : 0;
248 }
249 #endif
250
251 /*
252  * These are our native regset flavours.
253  */
254 enum sh_regset {
255         REGSET_GENERAL,
256 #ifdef CONFIG_SH_FPU
257         REGSET_FPU,
258 #endif
259 };
260
261 static const struct user_regset sh_regsets[] = {
262         /*
263          * Format is:
264          *      PC, SR, SYSCALL,
265          *      R1 --> R63,
266          *      TR0 --> TR7,
267          */
268         [REGSET_GENERAL] = {
269                 .core_note_type = NT_PRSTATUS,
270                 .n              = ELF_NGREG,
271                 .size           = sizeof(long long),
272                 .align          = sizeof(long long),
273                 .get            = genregs_get,
274                 .set            = genregs_set,
275         },
276
277 #ifdef CONFIG_SH_FPU
278         [REGSET_FPU] = {
279                 .core_note_type = NT_PRFPREG,
280                 .n              = sizeof(struct user_fpu_struct) /
281                                   sizeof(long long),
282                 .size           = sizeof(long long),
283                 .align          = sizeof(long long),
284                 .get            = fpregs_get,
285                 .set            = fpregs_set,
286                 .active         = fpregs_active,
287         },
288 #endif
289 };
290
291 static const struct user_regset_view user_sh64_native_view = {
292         .name           = "sh64",
293         .e_machine      = EM_SH,
294         .regsets        = sh_regsets,
295         .n              = ARRAY_SIZE(sh_regsets),
296 };
297
298 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
299 {
300         return &user_sh64_native_view;
301 }
302
303 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
304 {
305         int ret;
306
307         switch (request) {
308         /* read the word at location addr in the USER area. */
309         case PTRACE_PEEKUSR: {
310                 unsigned long tmp;
311
312                 ret = -EIO;
313                 if ((addr & 3) || addr < 0)
314                         break;
315
316                 if (addr < sizeof(struct pt_regs))
317                         tmp = get_stack_long(child, addr);
318                 else if ((addr >= offsetof(struct user, fpu)) &&
319                          (addr <  offsetof(struct user, u_fpvalid))) {
320                         tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
321                 } else if (addr == offsetof(struct user, u_fpvalid)) {
322                         tmp = !!tsk_used_math(child);
323                 } else {
324                         break;
325                 }
326                 ret = put_user(tmp, (unsigned long *)data);
327                 break;
328         }
329
330         case PTRACE_POKEUSR:
331                 /* write the word at location addr in the USER area. We must
332                    disallow any changes to certain SR bits or u_fpvalid, since
333                    this could crash the kernel or result in a security
334                    loophole. */
335                 ret = -EIO;
336                 if ((addr & 3) || addr < 0)
337                         break;
338
339                 if (addr < sizeof(struct pt_regs)) {
340                         /* Ignore change of top 32 bits of SR */
341                         if (addr == offsetof (struct pt_regs, sr)+4)
342                         {
343                                 ret = 0;
344                                 break;
345                         }
346                         /* If lower 32 bits of SR, ignore non-user bits */
347                         if (addr == offsetof (struct pt_regs, sr))
348                         {
349                                 long cursr = get_stack_long(child, addr);
350                                 data &= ~(SR_MASK);
351                                 data |= (cursr & SR_MASK);
352                         }
353                         ret = put_stack_long(child, addr, data);
354                 }
355                 else if ((addr >= offsetof(struct user, fpu)) &&
356                          (addr <  offsetof(struct user, u_fpvalid))) {
357                         ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
358                 }
359                 break;
360
361         case PTRACE_GETREGS:
362                 return copy_regset_to_user(child, &user_sh64_native_view,
363                                            REGSET_GENERAL,
364                                            0, sizeof(struct pt_regs),
365                                            (void __user *)data);
366         case PTRACE_SETREGS:
367                 return copy_regset_from_user(child, &user_sh64_native_view,
368                                              REGSET_GENERAL,
369                                              0, sizeof(struct pt_regs),
370                                              (const void __user *)data);
371 #ifdef CONFIG_SH_FPU
372         case PTRACE_GETFPREGS:
373                 return copy_regset_to_user(child, &user_sh64_native_view,
374                                            REGSET_FPU,
375                                            0, sizeof(struct user_fpu_struct),
376                                            (void __user *)data);
377         case PTRACE_SETFPREGS:
378                 return copy_regset_from_user(child, &user_sh64_native_view,
379                                              REGSET_FPU,
380                                              0, sizeof(struct user_fpu_struct),
381                                              (const void __user *)data);
382 #endif
383         default:
384                 ret = ptrace_request(child, request, addr, data);
385                 break;
386         }
387
388         return ret;
389 }
390
391 asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
392 {
393 #define WPC_DBRMODE 0x0d104008
394         static int first_call = 1;
395
396         lock_kernel();
397         if (first_call) {
398                 /* Set WPC.DBRMODE to 0.  This makes all debug events get
399                  * delivered through RESVEC, i.e. into the handlers in entry.S.
400                  * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
401                  * would normally be left set to 1, which makes debug events get
402                  * delivered through DBRVEC, i.e. into the remote gdb's
403                  * handlers.  This prevents ptrace getting them, and confuses
404                  * the remote gdb.) */
405                 printk("DBRMODE set to 0 to permit native debugging\n");
406                 poke_real_address_q(WPC_DBRMODE, 0);
407                 first_call = 0;
408         }
409         unlock_kernel();
410
411         return sys_ptrace(request, pid, addr, data);
412 }
413
414 static inline int audit_arch(void)
415 {
416         int arch = EM_SH;
417
418 #ifdef CONFIG_64BIT
419         arch |= __AUDIT_ARCH_64BIT;
420 #endif
421 #ifdef CONFIG_CPU_LITTLE_ENDIAN
422         arch |= __AUDIT_ARCH_LE;
423 #endif
424
425         return arch;
426 }
427
428 asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
429 {
430         long long ret = 0;
431
432         secure_computing(regs->regs[9]);
433
434         if (test_thread_flag(TIF_SYSCALL_TRACE) &&
435             tracehook_report_syscall_entry(regs))
436                 /*
437                  * Tracing decided this syscall should not happen.
438                  * We'll return a bogus call number to get an ENOSYS
439                  * error, but leave the original number in regs->regs[0].
440                  */
441                 ret = -1LL;
442
443         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
444                 trace_sys_enter(regs, regs->regs[9]);
445
446         if (unlikely(current->audit_context))
447                 audit_syscall_entry(audit_arch(), regs->regs[1],
448                                     regs->regs[2], regs->regs[3],
449                                     regs->regs[4], regs->regs[5]);
450
451         return ret ?: regs->regs[9];
452 }
453
454 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
455 {
456         if (unlikely(current->audit_context))
457                 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
458                                    regs->regs[9]);
459
460         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
461                 trace_sys_exit(regs, regs->regs[9]);
462
463         if (test_thread_flag(TIF_SYSCALL_TRACE))
464                 tracehook_report_syscall_exit(regs, 0);
465 }
466
467 /* Called with interrupts disabled */
468 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
469 {
470         /* This is called after a single step exception (DEBUGSS).
471            There is no need to change the PC, as it is a post-execution
472            exception, as entry.S does not do anything to the PC for DEBUGSS.
473            We need to clear the Single Step setting in SR to avoid
474            continually stepping. */
475         local_irq_enable();
476         regs->sr &= ~SR_SSTEP;
477         force_sig(SIGTRAP, current);
478 }
479
480 /* Called with interrupts disabled */
481 BUILD_TRAP_HANDLER(breakpoint)
482 {
483         TRAP_HANDLER_DECL;
484
485         /* We need to forward step the PC, to counteract the backstep done
486            in signal.c. */
487         local_irq_enable();
488         force_sig(SIGTRAP, current);
489         regs->pc += 4;
490 }
491
492 /*
493  * Called by kernel/ptrace.c when detaching..
494  *
495  * Make sure single step bits etc are not set.
496  */
497 void ptrace_disable(struct task_struct *child)
498 {
499         user_disable_single_step(child);
500 }