1 // SPDX-License-Identifier: GPL-2.0
3 * SuperH process tracing
5 * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
6 * Copyright (C) 2002 - 2009 Paul Mundt
8 * Audit support by Yuichi Nakamura <ynakam@hitachisoft.jp>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/sched/task_stack.h>
14 #include <linux/smp.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/security.h>
19 #include <linux/signal.h>
21 #include <linux/audit.h>
22 #include <linux/seccomp.h>
23 #include <linux/tracehook.h>
24 #include <linux/elf.h>
25 #include <linux/regset.h>
26 #include <linux/hw_breakpoint.h>
27 #include <linux/uaccess.h>
28 #include <asm/processor.h>
29 #include <asm/mmu_context.h>
30 #include <asm/syscalls.h>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/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 ptrace_triggered(struct perf_event *bp,
63 struct perf_sample_data *data, struct pt_regs *regs)
65 struct perf_event_attr attr;
68 * Disable the breakpoint request here since ptrace has defined a
69 * one-shot behaviour for breakpoint exceptions.
73 modify_user_hw_breakpoint(bp, &attr);
76 static int set_single_step(struct task_struct *tsk, unsigned long addr)
78 struct thread_struct *thread = &tsk->thread;
79 struct perf_event *bp;
80 struct perf_event_attr attr;
82 bp = thread->ptrace_bps[0];
84 ptrace_breakpoint_init(&attr);
87 attr.bp_len = HW_BREAKPOINT_LEN_2;
88 attr.bp_type = HW_BREAKPOINT_R;
90 bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
95 thread->ptrace_bps[0] = bp;
101 /* reenable breakpoint */
102 attr.disabled = false;
103 err = modify_user_hw_breakpoint(bp, &attr);
111 void user_enable_single_step(struct task_struct *child)
113 unsigned long pc = get_stack_long(child, offsetof(struct pt_regs, pc));
115 set_tsk_thread_flag(child, TIF_SINGLESTEP);
117 set_single_step(child, pc);
120 void user_disable_single_step(struct task_struct *child)
122 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
126 * Called by kernel/ptrace.c when detaching..
128 * Make sure single step bits etc are not set.
130 void ptrace_disable(struct task_struct *child)
132 user_disable_single_step(child);
135 static int genregs_get(struct task_struct *target,
136 const struct user_regset *regset,
137 unsigned int pos, unsigned int count,
138 void *kbuf, void __user *ubuf)
140 const struct pt_regs *regs = task_pt_regs(target);
143 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
145 0, 16 * sizeof(unsigned long));
147 /* PC, PR, SR, GBR, MACH, MACL, TRA */
148 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
150 offsetof(struct pt_regs, pc),
151 sizeof(struct pt_regs));
153 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
154 sizeof(struct pt_regs), -1);
159 static int genregs_set(struct task_struct *target,
160 const struct user_regset *regset,
161 unsigned int pos, unsigned int count,
162 const void *kbuf, const void __user *ubuf)
164 struct pt_regs *regs = task_pt_regs(target);
167 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
169 0, 16 * sizeof(unsigned long));
170 if (!ret && count > 0)
171 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
173 offsetof(struct pt_regs, pc),
174 sizeof(struct pt_regs));
176 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
177 sizeof(struct pt_regs), -1);
183 int fpregs_get(struct task_struct *target,
184 const struct user_regset *regset,
185 unsigned int pos, unsigned int count,
186 void *kbuf, void __user *ubuf)
190 ret = init_fpu(target);
194 if ((boot_cpu_data.flags & CPU_HAS_FPU))
195 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
196 &target->thread.xstate->hardfpu, 0, -1);
198 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
199 &target->thread.xstate->softfpu, 0, -1);
202 static int fpregs_set(struct task_struct *target,
203 const struct user_regset *regset,
204 unsigned int pos, unsigned int count,
205 const void *kbuf, const void __user *ubuf)
209 ret = init_fpu(target);
213 set_stopped_child_used_math(target);
215 if ((boot_cpu_data.flags & CPU_HAS_FPU))
216 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
217 &target->thread.xstate->hardfpu, 0, -1);
219 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
220 &target->thread.xstate->softfpu, 0, -1);
223 static int fpregs_active(struct task_struct *target,
224 const struct user_regset *regset)
226 return tsk_used_math(target) ? regset->n : 0;
231 static int dspregs_get(struct task_struct *target,
232 const struct user_regset *regset,
233 unsigned int pos, unsigned int count,
234 void *kbuf, void __user *ubuf)
236 const struct pt_dspregs *regs =
237 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
240 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
241 0, sizeof(struct pt_dspregs));
243 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
244 sizeof(struct pt_dspregs), -1);
249 static int dspregs_set(struct task_struct *target,
250 const struct user_regset *regset,
251 unsigned int pos, unsigned int count,
252 const void *kbuf, const void __user *ubuf)
254 struct pt_dspregs *regs =
255 (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
258 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
259 0, sizeof(struct pt_dspregs));
261 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
262 sizeof(struct pt_dspregs), -1);
267 static int dspregs_active(struct task_struct *target,
268 const struct user_regset *regset)
270 struct pt_regs *regs = task_pt_regs(target);
272 return regs->sr & SR_DSP ? regset->n : 0;
276 const struct pt_regs_offset regoffset_table[] = {
287 REGS_OFFSET_NAME(10),
288 REGS_OFFSET_NAME(11),
289 REGS_OFFSET_NAME(12),
290 REGS_OFFSET_NAME(13),
291 REGS_OFFSET_NAME(14),
292 REGS_OFFSET_NAME(15),
296 REG_OFFSET_NAME(gbr),
297 REG_OFFSET_NAME(mach),
298 REG_OFFSET_NAME(macl),
299 REG_OFFSET_NAME(tra),
304 * These are our native regset flavours.
316 static const struct user_regset sh_regsets[] = {
320 * PC, PR, SR, GBR, MACH, MACL, TRA
323 .core_note_type = NT_PRSTATUS,
325 .size = sizeof(long),
326 .align = sizeof(long),
333 .core_note_type = NT_PRFPREG,
334 .n = sizeof(struct user_fpu_struct) / sizeof(long),
335 .size = sizeof(long),
336 .align = sizeof(long),
339 .active = fpregs_active,
345 .n = sizeof(struct pt_dspregs) / sizeof(long),
346 .size = sizeof(long),
347 .align = sizeof(long),
350 .active = dspregs_active,
355 static const struct user_regset_view user_sh_native_view = {
358 .regsets = sh_regsets,
359 .n = ARRAY_SIZE(sh_regsets),
362 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
364 return &user_sh_native_view;
367 long arch_ptrace(struct task_struct *child, long request,
368 unsigned long addr, unsigned long data)
370 unsigned long __user *datap = (unsigned long __user *)data;
374 /* read the word at location addr in the USER area. */
375 case PTRACE_PEEKUSR: {
379 if ((addr & 3) || addr < 0 ||
380 addr > sizeof(struct user) - 3)
383 if (addr < sizeof(struct pt_regs))
384 tmp = get_stack_long(child, addr);
385 else if (addr >= offsetof(struct user, fpu) &&
386 addr < offsetof(struct user, u_fpvalid)) {
387 if (!tsk_used_math(child)) {
388 if (addr == offsetof(struct user, fpu.fpscr))
394 ret = init_fpu(child);
397 index = addr - offsetof(struct user, fpu);
398 tmp = ((unsigned long *)child->thread.xstate)
401 } else if (addr == offsetof(struct user, u_fpvalid))
402 tmp = !!tsk_used_math(child);
403 else if (addr == PT_TEXT_ADDR)
404 tmp = child->mm->start_code;
405 else if (addr == PT_DATA_ADDR)
406 tmp = child->mm->start_data;
407 else if (addr == PT_TEXT_END_ADDR)
408 tmp = child->mm->end_code;
409 else if (addr == PT_TEXT_LEN)
410 tmp = child->mm->end_code - child->mm->start_code;
413 ret = put_user(tmp, datap);
417 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
419 if ((addr & 3) || addr < 0 ||
420 addr > sizeof(struct user) - 3)
423 if (addr < sizeof(struct pt_regs))
424 ret = put_stack_long(child, addr, data);
425 else if (addr >= offsetof(struct user, fpu) &&
426 addr < offsetof(struct user, u_fpvalid)) {
428 ret = init_fpu(child);
431 index = addr - offsetof(struct user, fpu);
432 set_stopped_child_used_math(child);
433 ((unsigned long *)child->thread.xstate)
436 } else if (addr == offsetof(struct user, u_fpvalid)) {
437 conditional_stopped_child_used_math(data, child);
443 return copy_regset_to_user(child, &user_sh_native_view,
445 0, sizeof(struct pt_regs),
448 return copy_regset_from_user(child, &user_sh_native_view,
450 0, sizeof(struct pt_regs),
453 case PTRACE_GETFPREGS:
454 return copy_regset_to_user(child, &user_sh_native_view,
456 0, sizeof(struct user_fpu_struct),
458 case PTRACE_SETFPREGS:
459 return copy_regset_from_user(child, &user_sh_native_view,
461 0, sizeof(struct user_fpu_struct),
465 case PTRACE_GETDSPREGS:
466 return copy_regset_to_user(child, &user_sh_native_view,
468 0, sizeof(struct pt_dspregs),
470 case PTRACE_SETDSPREGS:
471 return copy_regset_from_user(child, &user_sh_native_view,
473 0, sizeof(struct pt_dspregs),
477 ret = ptrace_request(child, request, addr, data);
484 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
488 secure_computing_strict(regs->regs[0]);
490 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
491 tracehook_report_syscall_entry(regs))
493 * Tracing decided this syscall should not happen.
494 * We'll return a bogus call number to get an ENOSYS
495 * error, but leave the original number in regs->regs[0].
499 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
500 trace_sys_enter(regs, regs->regs[0]);
502 audit_syscall_entry(regs->regs[3], 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 audit_syscall_exit(regs);
514 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
515 trace_sys_exit(regs, regs->regs[0]);
517 step = test_thread_flag(TIF_SINGLESTEP);
518 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
519 tracehook_report_syscall_exit(regs, step);