2 * SuperH process tracing
4 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
5 * Copyright (C) 2002 - 2009 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 <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>
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/syscalls.h>
42 * This routine will get a word off of the process kernel stack.
44 static inline int get_stack_long(struct task_struct *task, int offset)
48 stack = (unsigned char *)task_pt_regs(task);
50 return (*((int *)stack));
54 * This routine will put a word on the process kernel stack.
56 static inline int put_stack_long(struct task_struct *task, int offset,
61 stack = (unsigned char *)task_pt_regs(task);
63 *(unsigned long *) stack = data;
67 void ptrace_triggered(struct perf_event *bp, int nmi,
68 struct perf_sample_data *data, struct pt_regs *regs)
70 struct perf_event_attr attr;
73 * Disable the breakpoint request here since ptrace has defined a
74 * one-shot behaviour for breakpoint exceptions.
78 modify_user_hw_breakpoint(bp, &attr);
81 static int set_single_step(struct task_struct *tsk, unsigned long addr)
83 struct thread_struct *thread = &tsk->thread;
84 struct perf_event *bp;
85 struct perf_event_attr attr;
87 bp = thread->ptrace_bps[0];
89 hw_breakpoint_init(&attr);
92 attr.bp_len = HW_BREAKPOINT_LEN_2;
93 attr.bp_type = HW_BREAKPOINT_R;
95 bp = register_user_hw_breakpoint(&attr, ptrace_triggered, tsk);
99 thread->ptrace_bps[0] = bp;
105 err = modify_user_hw_breakpoint(bp, &attr);
113 void user_enable_single_step(struct task_struct *child)
115 unsigned long pc = get_stack_long(child, offsetof(struct pt_regs, pc));
117 set_tsk_thread_flag(child, TIF_SINGLESTEP);
119 set_single_step(child, pc);
122 void user_disable_single_step(struct task_struct *child)
124 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
128 * Called by kernel/ptrace.c when detaching..
130 * Make sure single step bits etc are not set.
132 void ptrace_disable(struct task_struct *child)
134 user_disable_single_step(child);
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)
142 const struct pt_regs *regs = task_pt_regs(target);
145 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
147 0, 16 * sizeof(unsigned long));
149 /* PC, PR, SR, GBR, MACH, MACL, TRA */
150 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
152 offsetof(struct pt_regs, pc),
153 sizeof(struct pt_regs));
155 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
156 sizeof(struct pt_regs), -1);
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)
166 struct pt_regs *regs = task_pt_regs(target);
169 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
171 0, 16 * sizeof(unsigned long));
172 if (!ret && count > 0)
173 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
175 offsetof(struct pt_regs, pc),
176 sizeof(struct pt_regs));
178 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
179 sizeof(struct pt_regs), -1);
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)
192 ret = init_fpu(target);
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);
200 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
201 &target->thread.xstate->softfpu, 0, -1);
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)
211 ret = init_fpu(target);
215 set_stopped_child_used_math(target);
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);
221 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
222 &target->thread.xstate->softfpu, 0, -1);
225 static int fpregs_active(struct task_struct *target,
226 const struct user_regset *regset)
228 return tsk_used_math(target) ? regset->n : 0;
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)
238 const struct pt_dspregs *regs =
239 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
242 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
243 0, sizeof(struct pt_dspregs));
245 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
246 sizeof(struct pt_dspregs), -1);
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)
256 struct pt_dspregs *regs =
257 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
260 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
261 0, sizeof(struct pt_dspregs));
263 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
264 sizeof(struct pt_dspregs), -1);
269 static int dspregs_active(struct task_struct *target,
270 const struct user_regset *regset)
272 struct pt_regs *regs = task_pt_regs(target);
274 return regs->sr & SR_DSP ? regset->n : 0;
279 * These are our native regset flavours.
291 static const struct user_regset sh_regsets[] = {
295 * PC, PR, SR, GBR, MACH, MACL, TRA
298 .core_note_type = NT_PRSTATUS,
300 .size = sizeof(long),
301 .align = sizeof(long),
308 .core_note_type = NT_PRFPREG,
309 .n = sizeof(struct user_fpu_struct) / sizeof(long),
310 .size = sizeof(long),
311 .align = sizeof(long),
314 .active = fpregs_active,
320 .n = sizeof(struct pt_dspregs) / sizeof(long),
321 .size = sizeof(long),
322 .align = sizeof(long),
325 .active = dspregs_active,
330 static const struct user_regset_view user_sh_native_view = {
333 .regsets = sh_regsets,
334 .n = ARRAY_SIZE(sh_regsets),
337 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
339 return &user_sh_native_view;
342 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
344 struct user * dummy = NULL;
345 unsigned long __user *datap = (unsigned long __user *)data;
349 /* read the word at location addr in the USER area. */
350 case PTRACE_PEEKUSR: {
354 if ((addr & 3) || addr < 0 ||
355 addr > sizeof(struct user) - 3)
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)
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;
382 ret = put_user(tmp, datap);
386 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
388 if ((addr & 3) || addr < 0 ||
389 addr > sizeof(struct user) - 3)
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;
400 } else if (addr == (long) &dummy->u_fpvalid) {
401 conditional_stopped_child_used_math(data, child);
407 return copy_regset_to_user(child, &user_sh_native_view,
409 0, sizeof(struct pt_regs),
410 (void __user *)data);
412 return copy_regset_from_user(child, &user_sh_native_view,
414 0, sizeof(struct pt_regs),
415 (const void __user *)data);
417 case PTRACE_GETFPREGS:
418 return copy_regset_to_user(child, &user_sh_native_view,
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,
425 0, sizeof(struct user_fpu_struct),
426 (const void __user *)data);
429 case PTRACE_GETDSPREGS:
430 return copy_regset_to_user(child, &user_sh_native_view,
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,
437 0, sizeof(struct pt_dspregs),
438 (const void __user *)data);
440 #ifdef CONFIG_BINFMT_ELF_FDPIC
441 case PTRACE_GETFDPIC: {
442 unsigned long tmp = 0;
445 case PTRACE_GETFDPIC_EXEC:
446 tmp = child->mm->context.exec_fdpic_loadmap;
448 case PTRACE_GETFDPIC_INTERP:
449 tmp = child->mm->context.interp_fdpic_loadmap;
456 if (put_user(tmp, datap)) {
464 ret = ptrace_request(child, request, addr, data);
471 static inline int audit_arch(void)
475 #ifdef CONFIG_CPU_LITTLE_ENDIAN
476 arch |= __AUDIT_ARCH_LE;
482 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
486 secure_computing(regs->regs[0]);
488 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
489 tracehook_report_syscall_entry(regs))
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].
497 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
498 trace_sys_enter(regs, regs->regs[0]);
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]);
505 return ret ?: regs->regs[0];
508 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
512 if (unlikely(current->audit_context))
513 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
516 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
517 trace_sys_exit(regs, regs->regs[0]);
519 step = test_thread_flag(TIF_SINGLESTEP);
520 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
521 tracehook_report_syscall_exit(regs, step);