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 #define CREATE_TRACE_POINTS
38 #include <trace/events/syscalls.h>
41 * This routine will get a word off of the process kernel stack.
43 static inline int get_stack_long(struct task_struct
*task
, int offset
)
47 stack
= (unsigned char *)task_pt_regs(task
);
49 return (*((int *)stack
));
53 * This routine will put a word on the process kernel stack.
55 static inline int put_stack_long(struct task_struct
*task
, int offset
,
60 stack
= (unsigned char *)task_pt_regs(task
);
62 *(unsigned long *) stack
= data
;
66 void user_enable_single_step(struct task_struct
*child
)
68 /* Next scheduling will set up UBC */
69 if (child
->thread
.ubc_pc
== 0)
72 child
->thread
.ubc_pc
= get_stack_long(child
,
73 offsetof(struct pt_regs
, pc
));
75 set_tsk_thread_flag(child
, TIF_SINGLESTEP
);
78 void user_disable_single_step(struct task_struct
*child
)
80 clear_tsk_thread_flag(child
, TIF_SINGLESTEP
);
83 * Ensure the UBC is not programmed at the next context switch.
85 * Normally this is not needed but there are sequences such as
86 * singlestep, signal delivery, and continue that leave the
87 * ubc_pc non-zero leading to spurious SIGTRAPs.
89 if (child
->thread
.ubc_pc
!= 0) {
91 child
->thread
.ubc_pc
= 0;
96 * Called by kernel/ptrace.c when detaching..
98 * Make sure single step bits etc are not set.
100 void ptrace_disable(struct task_struct
*child
)
102 user_disable_single_step(child
);
105 static int genregs_get(struct task_struct
*target
,
106 const struct user_regset
*regset
,
107 unsigned int pos
, unsigned int count
,
108 void *kbuf
, void __user
*ubuf
)
110 const struct pt_regs
*regs
= task_pt_regs(target
);
113 ret
= user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
115 0, 16 * sizeof(unsigned long));
117 /* PC, PR, SR, GBR, MACH, MACL, TRA */
118 ret
= user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
120 offsetof(struct pt_regs
, pc
),
121 sizeof(struct pt_regs
));
123 ret
= user_regset_copyout_zero(&pos
, &count
, &kbuf
, &ubuf
,
124 sizeof(struct pt_regs
), -1);
129 static int genregs_set(struct task_struct
*target
,
130 const struct user_regset
*regset
,
131 unsigned int pos
, unsigned int count
,
132 const void *kbuf
, const void __user
*ubuf
)
134 struct pt_regs
*regs
= task_pt_regs(target
);
137 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
139 0, 16 * sizeof(unsigned long));
140 if (!ret
&& count
> 0)
141 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
143 offsetof(struct pt_regs
, pc
),
144 sizeof(struct pt_regs
));
146 ret
= user_regset_copyin_ignore(&pos
, &count
, &kbuf
, &ubuf
,
147 sizeof(struct pt_regs
), -1);
153 int fpregs_get(struct task_struct
*target
,
154 const struct user_regset
*regset
,
155 unsigned int pos
, unsigned int count
,
156 void *kbuf
, void __user
*ubuf
)
160 ret
= init_fpu(target
);
164 if ((boot_cpu_data
.flags
& CPU_HAS_FPU
))
165 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
166 &target
->thread
.fpu
.hard
, 0, -1);
168 return user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
,
169 &target
->thread
.fpu
.soft
, 0, -1);
172 static int fpregs_set(struct task_struct
*target
,
173 const struct user_regset
*regset
,
174 unsigned int pos
, unsigned int count
,
175 const void *kbuf
, const void __user
*ubuf
)
179 ret
= init_fpu(target
);
183 set_stopped_child_used_math(target
);
185 if ((boot_cpu_data
.flags
& CPU_HAS_FPU
))
186 return user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
187 &target
->thread
.fpu
.hard
, 0, -1);
189 return user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
,
190 &target
->thread
.fpu
.soft
, 0, -1);
193 static int fpregs_active(struct task_struct
*target
,
194 const struct user_regset
*regset
)
196 return tsk_used_math(target
) ? regset
->n
: 0;
201 static int dspregs_get(struct task_struct
*target
,
202 const struct user_regset
*regset
,
203 unsigned int pos
, unsigned int count
,
204 void *kbuf
, void __user
*ubuf
)
206 const struct pt_dspregs
*regs
=
207 (struct pt_dspregs
*)&target
->thread
.dsp_status
.dsp_regs
;
210 ret
= user_regset_copyout(&pos
, &count
, &kbuf
, &ubuf
, regs
,
211 0, sizeof(struct pt_dspregs
));
213 ret
= user_regset_copyout_zero(&pos
, &count
, &kbuf
, &ubuf
,
214 sizeof(struct pt_dspregs
), -1);
219 static int dspregs_set(struct task_struct
*target
,
220 const struct user_regset
*regset
,
221 unsigned int pos
, unsigned int count
,
222 const void *kbuf
, const void __user
*ubuf
)
224 struct pt_dspregs
*regs
=
225 (struct pt_dspregs
*)&target
->thread
.dsp_status
.dsp_regs
;
228 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, regs
,
229 0, sizeof(struct pt_dspregs
));
231 ret
= user_regset_copyin_ignore(&pos
, &count
, &kbuf
, &ubuf
,
232 sizeof(struct pt_dspregs
), -1);
237 static int dspregs_active(struct task_struct
*target
,
238 const struct user_regset
*regset
)
240 struct pt_regs
*regs
= task_pt_regs(target
);
242 return regs
->sr
& SR_DSP
? regset
->n
: 0;
247 * These are our native regset flavours.
259 static const struct user_regset sh_regsets
[] = {
263 * PC, PR, SR, GBR, MACH, MACL, TRA
266 .core_note_type
= NT_PRSTATUS
,
268 .size
= sizeof(long),
269 .align
= sizeof(long),
276 .core_note_type
= NT_PRFPREG
,
277 .n
= sizeof(struct user_fpu_struct
) / sizeof(long),
278 .size
= sizeof(long),
279 .align
= sizeof(long),
282 .active
= fpregs_active
,
288 .n
= sizeof(struct pt_dspregs
) / sizeof(long),
289 .size
= sizeof(long),
290 .align
= sizeof(long),
293 .active
= dspregs_active
,
298 static const struct user_regset_view user_sh_native_view
= {
301 .regsets
= sh_regsets
,
302 .n
= ARRAY_SIZE(sh_regsets
),
305 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
307 return &user_sh_native_view
;
310 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
312 struct user
* dummy
= NULL
;
313 unsigned long __user
*datap
= (unsigned long __user
*)data
;
317 /* read the word at location addr in the USER area. */
318 case PTRACE_PEEKUSR
: {
322 if ((addr
& 3) || addr
< 0 ||
323 addr
> sizeof(struct user
) - 3)
326 if (addr
< sizeof(struct pt_regs
))
327 tmp
= get_stack_long(child
, addr
);
328 else if (addr
>= (long) &dummy
->fpu
&&
329 addr
< (long) &dummy
->u_fpvalid
) {
330 if (!tsk_used_math(child
)) {
331 if (addr
== (long)&dummy
->fpu
.fpscr
)
336 tmp
= ((long *)&child
->thread
.fpu
)
337 [(addr
- (long)&dummy
->fpu
) >> 2];
338 } else if (addr
== (long) &dummy
->u_fpvalid
)
339 tmp
= !!tsk_used_math(child
);
340 else if (addr
== PT_TEXT_ADDR
)
341 tmp
= child
->mm
->start_code
;
342 else if (addr
== PT_DATA_ADDR
)
343 tmp
= child
->mm
->start_data
;
344 else if (addr
== PT_TEXT_END_ADDR
)
345 tmp
= child
->mm
->end_code
;
346 else if (addr
== PT_TEXT_LEN
)
347 tmp
= child
->mm
->end_code
- child
->mm
->start_code
;
350 ret
= put_user(tmp
, datap
);
354 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
356 if ((addr
& 3) || addr
< 0 ||
357 addr
> sizeof(struct user
) - 3)
360 if (addr
< sizeof(struct pt_regs
))
361 ret
= put_stack_long(child
, addr
, data
);
362 else if (addr
>= (long) &dummy
->fpu
&&
363 addr
< (long) &dummy
->u_fpvalid
) {
364 set_stopped_child_used_math(child
);
365 ((long *)&child
->thread
.fpu
)
366 [(addr
- (long)&dummy
->fpu
) >> 2] = data
;
368 } else if (addr
== (long) &dummy
->u_fpvalid
) {
369 conditional_stopped_child_used_math(data
, child
);
375 return copy_regset_to_user(child
, &user_sh_native_view
,
377 0, sizeof(struct pt_regs
),
378 (void __user
*)data
);
380 return copy_regset_from_user(child
, &user_sh_native_view
,
382 0, sizeof(struct pt_regs
),
383 (const void __user
*)data
);
385 case PTRACE_GETFPREGS
:
386 return copy_regset_to_user(child
, &user_sh_native_view
,
388 0, sizeof(struct user_fpu_struct
),
389 (void __user
*)data
);
390 case PTRACE_SETFPREGS
:
391 return copy_regset_from_user(child
, &user_sh_native_view
,
393 0, sizeof(struct user_fpu_struct
),
394 (const void __user
*)data
);
397 case PTRACE_GETDSPREGS
:
398 return copy_regset_to_user(child
, &user_sh_native_view
,
400 0, sizeof(struct pt_dspregs
),
401 (void __user
*)data
);
402 case PTRACE_SETDSPREGS
:
403 return copy_regset_from_user(child
, &user_sh_native_view
,
405 0, sizeof(struct pt_dspregs
),
406 (const void __user
*)data
);
408 #ifdef CONFIG_BINFMT_ELF_FDPIC
409 case PTRACE_GETFDPIC
: {
410 unsigned long tmp
= 0;
413 case PTRACE_GETFDPIC_EXEC
:
414 tmp
= child
->mm
->context
.exec_fdpic_loadmap
;
416 case PTRACE_GETFDPIC_INTERP
:
417 tmp
= child
->mm
->context
.interp_fdpic_loadmap
;
424 if (put_user(tmp
, datap
)) {
432 ret
= ptrace_request(child
, request
, addr
, data
);
439 static inline int audit_arch(void)
443 #ifdef CONFIG_CPU_LITTLE_ENDIAN
444 arch
|= __AUDIT_ARCH_LE
;
450 asmlinkage
long do_syscall_trace_enter(struct pt_regs
*regs
)
454 secure_computing(regs
->regs
[0]);
456 if (test_thread_flag(TIF_SYSCALL_TRACE
) &&
457 tracehook_report_syscall_entry(regs
))
459 * Tracing decided this syscall should not happen.
460 * We'll return a bogus call number to get an ENOSYS
461 * error, but leave the original number in regs->regs[0].
465 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
466 trace_sys_enter(regs
, regs
->regs
[0]);
468 if (unlikely(current
->audit_context
))
469 audit_syscall_entry(audit_arch(), regs
->regs
[3],
470 regs
->regs
[4], regs
->regs
[5],
471 regs
->regs
[6], regs
->regs
[7]);
473 return ret
?: regs
->regs
[0];
476 asmlinkage
void do_syscall_trace_leave(struct pt_regs
*regs
)
480 if (unlikely(current
->audit_context
))
481 audit_syscall_exit(AUDITSC_RESULT(regs
->regs
[0]),
484 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
485 trace_sys_exit(regs
, regs
->regs
[0]);
487 step
= test_thread_flag(TIF_SINGLESTEP
);
488 if (step
|| test_thread_flag(TIF_SYSCALL_TRACE
))
489 tracehook_report_syscall_exit(regs
, step
);