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