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 * This routine will get a word off of the process kernel stack.
39 static inline int get_stack_long(struct task_struct *task, int offset)
43 stack = (unsigned char *)task_pt_regs(task);
45 return (*((int *)stack));
49 * This routine will put a word on the process kernel stack.
51 static inline int put_stack_long(struct task_struct *task, int offset,
56 stack = (unsigned char *)task_pt_regs(task);
58 *(unsigned long *) stack = data;
62 void user_enable_single_step(struct task_struct *child)
64 /* Next scheduling will set up UBC */
65 if (child->thread.ubc_pc == 0)
68 child->thread.ubc_pc = get_stack_long(child,
69 offsetof(struct pt_regs, pc));
71 set_tsk_thread_flag(child, TIF_SINGLESTEP);
74 void user_disable_single_step(struct task_struct *child)
76 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
79 * Ensure the UBC is not programmed at the next context switch.
81 * Normally this is not needed but there are sequences such as
82 * singlestep, signal delivery, and continue that leave the
83 * ubc_pc non-zero leading to spurious SIGTRAPs.
85 if (child->thread.ubc_pc != 0) {
87 child->thread.ubc_pc = 0;
92 * Called by kernel/ptrace.c when detaching..
94 * Make sure single step bits etc are not set.
96 void ptrace_disable(struct task_struct *child)
98 user_disable_single_step(child);
101 static int genregs_get(struct task_struct *target,
102 const struct user_regset *regset,
103 unsigned int pos, unsigned int count,
104 void *kbuf, void __user *ubuf)
106 const struct pt_regs *regs = task_pt_regs(target);
109 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
111 0, 16 * sizeof(unsigned long));
113 /* PC, PR, SR, GBR, MACH, MACL, TRA */
114 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
116 offsetof(struct pt_regs, pc),
117 sizeof(struct pt_regs));
119 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
120 sizeof(struct pt_regs), -1);
125 static int genregs_set(struct task_struct *target,
126 const struct user_regset *regset,
127 unsigned int pos, unsigned int count,
128 const void *kbuf, const void __user *ubuf)
130 struct pt_regs *regs = task_pt_regs(target);
133 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
135 0, 16 * sizeof(unsigned long));
136 if (!ret && count > 0)
137 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
139 offsetof(struct pt_regs, pc),
140 sizeof(struct pt_regs));
142 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
143 sizeof(struct pt_regs), -1);
149 static int dspregs_get(struct task_struct *target,
150 const struct user_regset *regset,
151 unsigned int pos, unsigned int count,
152 void *kbuf, void __user *ubuf)
154 const struct pt_dspregs *regs = task_pt_dspregs(target);
157 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
158 0, sizeof(struct pt_dspregs));
160 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
161 sizeof(struct pt_dspregs), -1);
166 static int dspregs_set(struct task_struct *target,
167 const struct user_regset *regset,
168 unsigned int pos, unsigned int count,
169 const void *kbuf, const void __user *ubuf)
171 struct pt_dspregs *regs = task_pt_dspregs(target);
174 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
175 0, sizeof(struct pt_dspregs));
177 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
178 sizeof(struct pt_dspregs), -1);
185 * These are our native regset flavours.
194 static const struct user_regset sh_regsets[] = {
198 * PC, PR, SR, GBR, MACH, MACL, TRA
201 .core_note_type = NT_PRSTATUS,
203 .size = sizeof(long),
204 .align = sizeof(long),
211 .n = sizeof(struct pt_dspregs) / sizeof(long),
212 .size = sizeof(long),
213 .align = sizeof(long),
220 static const struct user_regset_view user_sh_native_view = {
223 .regsets = sh_regsets,
224 .n = ARRAY_SIZE(sh_regsets),
227 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
229 return &user_sh_native_view;
232 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
234 struct user * dummy = NULL;
235 unsigned long __user *datap = (unsigned long __user *)data;
239 /* read the word at location addr in the USER area. */
240 case PTRACE_PEEKUSR: {
244 if ((addr & 3) || addr < 0 ||
245 addr > sizeof(struct user) - 3)
248 if (addr < sizeof(struct pt_regs))
249 tmp = get_stack_long(child, addr);
250 else if (addr >= (long) &dummy->fpu &&
251 addr < (long) &dummy->u_fpvalid) {
252 if (!tsk_used_math(child)) {
253 if (addr == (long)&dummy->fpu.fpscr)
258 tmp = ((long *)&child->thread.fpu)
259 [(addr - (long)&dummy->fpu) >> 2];
260 } else if (addr == (long) &dummy->u_fpvalid)
261 tmp = !!tsk_used_math(child);
264 ret = put_user(tmp, datap);
268 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
270 if ((addr & 3) || addr < 0 ||
271 addr > sizeof(struct user) - 3)
274 if (addr < sizeof(struct pt_regs))
275 ret = put_stack_long(child, addr, data);
276 else if (addr >= (long) &dummy->fpu &&
277 addr < (long) &dummy->u_fpvalid) {
278 set_stopped_child_used_math(child);
279 ((long *)&child->thread.fpu)
280 [(addr - (long)&dummy->fpu) >> 2] = data;
282 } else if (addr == (long) &dummy->u_fpvalid) {
283 conditional_stopped_child_used_math(data, child);
289 return copy_regset_to_user(child, &user_sh_native_view,
291 0, sizeof(struct pt_regs),
292 (void __user *)data);
294 return copy_regset_from_user(child, &user_sh_native_view,
296 0, sizeof(struct pt_regs),
297 (const void __user *)data);
299 case PTRACE_GETDSPREGS:
300 return copy_regset_to_user(child, &user_sh_native_view,
302 0, sizeof(struct pt_dspregs),
303 (void __user *)data);
304 case PTRACE_SETDSPREGS:
305 return copy_regset_from_user(child, &user_sh_native_view,
307 0, sizeof(struct pt_dspregs),
308 (const void __user *)data);
310 #ifdef CONFIG_BINFMT_ELF_FDPIC
311 case PTRACE_GETFDPIC: {
312 unsigned long tmp = 0;
315 case PTRACE_GETFDPIC_EXEC:
316 tmp = child->mm->context.exec_fdpic_loadmap;
318 case PTRACE_GETFDPIC_INTERP:
319 tmp = child->mm->context.interp_fdpic_loadmap;
326 if (put_user(tmp, datap)) {
334 ret = ptrace_request(child, request, addr, data);
341 static inline int audit_arch(void)
345 #ifdef CONFIG_CPU_LITTLE_ENDIAN
346 arch |= __AUDIT_ARCH_LE;
352 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
356 secure_computing(regs->regs[0]);
358 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
359 tracehook_report_syscall_entry(regs))
361 * Tracing decided this syscall should not happen.
362 * We'll return a bogus call number to get an ENOSYS
363 * error, but leave the original number in regs->regs[0].
367 if (unlikely(current->audit_context))
368 audit_syscall_entry(audit_arch(), regs->regs[3],
369 regs->regs[4], regs->regs[5],
370 regs->regs[6], regs->regs[7]);
372 return ret ?: regs->regs[0];
375 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
379 if (unlikely(current->audit_context))
380 audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
383 step = test_thread_flag(TIF_SINGLESTEP);
384 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
385 tracehook_report_syscall_exit(regs, step);