2 * SuperH process tracing
4 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
5 * Copyright (C) 2002 - 2008 Paul Mundt
7 * Audit support by Yuichi Nakamura <ynakam@hitachisoft.jp>
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
13 #include <linux/kernel.h>
14 #include <linux/sched.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>
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 <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/system.h>
32 #include <asm/processor.h>
33 #include <asm/mmu_context.h>
34 #include <asm/syscalls.h>
37 #include <trace/syscall.h>
40 * This routine will get a word off of the process kernel stack.
42 static inline int get_stack_long(struct task_struct *task, int offset)
46 stack = (unsigned char *)task_pt_regs(task);
48 return (*((int *)stack));
52 * This routine will put a word on the process kernel stack.
54 static inline int put_stack_long(struct task_struct *task, int offset,
59 stack = (unsigned char *)task_pt_regs(task);
61 *(unsigned long *) stack = data;
65 void user_enable_single_step(struct task_struct *child)
67 /* Next scheduling will set up UBC */
68 if (child->thread.ubc_pc == 0)
71 child->thread.ubc_pc = get_stack_long(child,
72 offsetof(struct pt_regs, pc));
74 set_tsk_thread_flag(child, TIF_SINGLESTEP);
77 void user_disable_single_step(struct task_struct *child)
79 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
82 * Ensure the UBC is not programmed at the next context switch.
84 * Normally this is not needed but there are sequences such as
85 * singlestep, signal delivery, and continue that leave the
86 * ubc_pc non-zero leading to spurious SIGTRAPs.
88 if (child->thread.ubc_pc != 0) {
90 child->thread.ubc_pc = 0;
95 * Called by kernel/ptrace.c when detaching..
97 * Make sure single step bits etc are not set.
99 void ptrace_disable(struct task_struct *child)
101 user_disable_single_step(child);
104 static int genregs_get(struct task_struct *target,
105 const struct user_regset *regset,
106 unsigned int pos, unsigned int count,
107 void *kbuf, void __user *ubuf)
109 const struct pt_regs *regs = task_pt_regs(target);
112 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
114 0, 16 * sizeof(unsigned long));
116 /* PC, PR, SR, GBR, MACH, MACL, TRA */
117 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
119 offsetof(struct pt_regs, pc),
120 sizeof(struct pt_regs));
122 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
123 sizeof(struct pt_regs), -1);
128 static int genregs_set(struct task_struct *target,
129 const struct user_regset *regset,
130 unsigned int pos, unsigned int count,
131 const void *kbuf, const void __user *ubuf)
133 struct pt_regs *regs = task_pt_regs(target);
136 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
138 0, 16 * sizeof(unsigned long));
139 if (!ret && count > 0)
140 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
142 offsetof(struct pt_regs, pc),
143 sizeof(struct pt_regs));
145 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
146 sizeof(struct pt_regs), -1);
152 int fpregs_get(struct task_struct *target,
153 const struct user_regset *regset,
154 unsigned int pos, unsigned int count,
155 void *kbuf, void __user *ubuf)
159 ret = init_fpu(target);
163 if ((boot_cpu_data.flags & CPU_HAS_FPU))
164 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
165 &target->thread.fpu.hard, 0, -1);
167 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
168 &target->thread.fpu.soft, 0, -1);
171 static int fpregs_set(struct task_struct *target,
172 const struct user_regset *regset,
173 unsigned int pos, unsigned int count,
174 const void *kbuf, const void __user *ubuf)
178 ret = init_fpu(target);
182 set_stopped_child_used_math(target);
184 if ((boot_cpu_data.flags & CPU_HAS_FPU))
185 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
186 &target->thread.fpu.hard, 0, -1);
188 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
189 &target->thread.fpu.soft, 0, -1);
192 static int fpregs_active(struct task_struct *target,
193 const struct user_regset *regset)
195 return tsk_used_math(target) ? regset->n : 0;
200 static int dspregs_get(struct task_struct *target,
201 const struct user_regset *regset,
202 unsigned int pos, unsigned int count,
203 void *kbuf, void __user *ubuf)
205 const struct pt_dspregs *regs =
206 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
209 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
210 0, sizeof(struct pt_dspregs));
212 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
213 sizeof(struct pt_dspregs), -1);
218 static int dspregs_set(struct task_struct *target,
219 const struct user_regset *regset,
220 unsigned int pos, unsigned int count,
221 const void *kbuf, const void __user *ubuf)
223 struct pt_dspregs *regs =
224 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
227 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
228 0, sizeof(struct pt_dspregs));
230 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
231 sizeof(struct pt_dspregs), -1);
236 static int dspregs_active(struct task_struct *target,
237 const struct user_regset *regset)
239 struct pt_regs *regs = task_pt_regs(target);
241 return regs->sr & SR_DSP ? regset->n : 0;
246 * These are our native regset flavours.
258 static const struct user_regset sh_regsets[] = {
262 * PC, PR, SR, GBR, MACH, MACL, TRA
265 .core_note_type = NT_PRSTATUS,
267 .size = sizeof(long),
268 .align = sizeof(long),
275 .core_note_type = NT_PRFPREG,
276 .n = sizeof(struct user_fpu_struct) / sizeof(long),
277 .size = sizeof(long),
278 .align = sizeof(long),
281 .active = fpregs_active,
287 .n = sizeof(struct pt_dspregs) / sizeof(long),
288 .size = sizeof(long),
289 .align = sizeof(long),
292 .active = dspregs_active,
297 static const struct user_regset_view user_sh_native_view = {
300 .regsets = sh_regsets,
301 .n = ARRAY_SIZE(sh_regsets),
304 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
306 return &user_sh_native_view;
309 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
311 struct user * dummy = NULL;
312 unsigned long __user *datap = (unsigned long __user *)data;
316 /* read the word at location addr in the USER area. */
317 case PTRACE_PEEKUSR: {
321 if ((addr & 3) || addr < 0 ||
322 addr > sizeof(struct user) - 3)
325 if (addr < sizeof(struct pt_regs))
326 tmp = get_stack_long(child, addr);
327 else if (addr >= (long) &dummy->fpu &&
328 addr < (long) &dummy->u_fpvalid) {
329 if (!tsk_used_math(child)) {
330 if (addr == (long)&dummy->fpu.fpscr)
335 tmp = ((long *)&child->thread.fpu)
336 [(addr - (long)&dummy->fpu) >> 2];
337 } else if (addr == (long) &dummy->u_fpvalid)
338 tmp = !!tsk_used_math(child);
339 else if (addr == PT_TEXT_ADDR)
340 tmp = child->mm->start_code;
341 else if (addr == PT_DATA_ADDR)
342 tmp = child->mm->start_data;
343 else if (addr == PT_TEXT_END_ADDR)
344 tmp = child->mm->end_code;
345 else if (addr == PT_TEXT_LEN)
346 tmp = child->mm->end_code - child->mm->start_code;
349 ret = put_user(tmp, datap);
353 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
355 if ((addr & 3) || addr < 0 ||
356 addr > sizeof(struct user) - 3)
359 if (addr < sizeof(struct pt_regs))
360 ret = put_stack_long(child, addr, data);
361 else if (addr >= (long) &dummy->fpu &&
362 addr < (long) &dummy->u_fpvalid) {
363 set_stopped_child_used_math(child);
364 ((long *)&child->thread.fpu)
365 [(addr - (long)&dummy->fpu) >> 2] = data;
367 } else if (addr == (long) &dummy->u_fpvalid) {
368 conditional_stopped_child_used_math(data, child);
374 return copy_regset_to_user(child, &user_sh_native_view,
376 0, sizeof(struct pt_regs),
377 (void __user *)data);
379 return copy_regset_from_user(child, &user_sh_native_view,
381 0, sizeof(struct pt_regs),
382 (const void __user *)data);
384 case PTRACE_GETFPREGS:
385 return copy_regset_to_user(child, &user_sh_native_view,
387 0, sizeof(struct user_fpu_struct),
388 (void __user *)data);
389 case PTRACE_SETFPREGS:
390 return copy_regset_from_user(child, &user_sh_native_view,
392 0, sizeof(struct user_fpu_struct),
393 (const void __user *)data);
396 case PTRACE_GETDSPREGS:
397 return copy_regset_to_user(child, &user_sh_native_view,
399 0, sizeof(struct pt_dspregs),
400 (void __user *)data);
401 case PTRACE_SETDSPREGS:
402 return copy_regset_from_user(child, &user_sh_native_view,
404 0, sizeof(struct pt_dspregs),
405 (const void __user *)data);
407 #ifdef CONFIG_BINFMT_ELF_FDPIC
408 case PTRACE_GETFDPIC: {
409 unsigned long tmp = 0;
412 case PTRACE_GETFDPIC_EXEC:
413 tmp = child->mm->context.exec_fdpic_loadmap;
415 case PTRACE_GETFDPIC_INTERP:
416 tmp = child->mm->context.interp_fdpic_loadmap;
423 if (put_user(tmp, datap)) {
431 ret = ptrace_request(child, request, addr, data);
438 static inline int audit_arch(void)
442 #ifdef CONFIG_CPU_LITTLE_ENDIAN
443 arch |= __AUDIT_ARCH_LE;
449 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
453 secure_computing(regs->regs[0]);
455 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
456 tracehook_report_syscall_entry(regs))
458 * Tracing decided this syscall should not happen.
459 * We'll return a bogus call number to get an ENOSYS
460 * error, but leave the original number in regs->regs[0].
464 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
465 ftrace_syscall_enter(regs);
467 if (unlikely(current->audit_context))
468 audit_syscall_entry(audit_arch(), regs->regs[3],
469 regs->regs[4], regs->regs[5],
470 regs->regs[6], regs->regs[7]);
472 return ret ?: regs->regs[0];
475 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
479 if (unlikely(current->audit_context))
480 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
483 if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
484 ftrace_syscall_exit(regs);
486 step = test_thread_flag(TIF_SINGLESTEP);
487 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
488 tracehook_report_syscall_exit(regs, step);