1 // SPDX-License-Identifier: GPL-2.0
3 * arch/sh/kernel/ptrace_64.c
5 * Copyright (C) 2000, 2001 Paolo Alberelli
6 * Copyright (C) 2003 - 2008 Paul Mundt
8 * Started from SH3/4 version:
9 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
11 * Original x86 implementation:
12 * By Ross Biro 1/23/92
13 * edited by Linus Torvalds
15 #include <linux/kernel.h>
16 #include <linux/rwsem.h>
17 #include <linux/sched.h>
18 #include <linux/sched/task_stack.h>
20 #include <linux/smp.h>
21 #include <linux/bitops.h>
22 #include <linux/errno.h>
23 #include <linux/ptrace.h>
24 #include <linux/user.h>
25 #include <linux/signal.h>
26 #include <linux/syscalls.h>
27 #include <linux/audit.h>
28 #include <linux/seccomp.h>
29 #include <linux/tracehook.h>
30 #include <linux/elf.h>
31 #include <linux/regset.h>
33 #include <linux/uaccess.h>
34 #include <asm/pgtable.h>
35 #include <asm/processor.h>
36 #include <asm/mmu_context.h>
37 #include <asm/syscalls.h>
39 #include <asm/traps.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/syscalls.h>
44 /* This mask defines the bits of the SR which the user is not allowed to
45 change, which are everything except S, Q, M, PR, SZ, FR. */
46 #define SR_MASK (0xffff8cfd)
49 * does not yet catch signals sent when the child dies.
50 * in exit.c or in signal.c.
54 * This routine will get a word from the user area in the process kernel stack.
56 static inline int get_stack_long(struct task_struct *task, int offset)
60 stack = (unsigned char *)(task->thread.uregs);
62 return (*((int *)stack));
65 static inline unsigned long
66 get_fpu_long(struct task_struct *task, unsigned long addr)
70 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
72 if (!tsk_used_math(task)) {
73 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
76 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
81 if (last_task_used_math == task) {
85 last_task_used_math = 0;
89 tmp = ((long *)task->thread.xstate)[addr / sizeof(unsigned long)];
94 * This routine will put a word into the user area in the process kernel stack.
96 static inline int put_stack_long(struct task_struct *task, int offset,
101 stack = (unsigned char *)(task->thread.uregs);
103 *(unsigned long *) stack = data;
108 put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
110 struct pt_regs *regs;
112 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
114 if (!tsk_used_math(task)) {
116 } else if (last_task_used_math == task) {
120 last_task_used_math = 0;
124 ((long *)task->thread.xstate)[addr / sizeof(unsigned long)] = data;
128 void user_enable_single_step(struct task_struct *child)
130 struct pt_regs *regs = child->thread.uregs;
132 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
134 set_tsk_thread_flag(child, TIF_SINGLESTEP);
137 void user_disable_single_step(struct task_struct *child)
139 struct pt_regs *regs = child->thread.uregs;
141 regs->sr &= ~SR_SSTEP;
143 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
146 static int genregs_get(struct task_struct *target,
147 const struct user_regset *regset,
148 unsigned int pos, unsigned int count,
149 void *kbuf, void __user *ubuf)
151 const struct pt_regs *regs = task_pt_regs(target);
154 /* PC, SR, SYSCALL */
155 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
157 0, 3 * sizeof(unsigned long long));
161 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
163 offsetof(struct pt_regs, regs[0]),
164 63 * sizeof(unsigned long long));
167 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
169 offsetof(struct pt_regs, tregs[0]),
170 8 * sizeof(unsigned long long));
173 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
174 sizeof(struct pt_regs), -1);
179 static int genregs_set(struct task_struct *target,
180 const struct user_regset *regset,
181 unsigned int pos, unsigned int count,
182 const void *kbuf, const void __user *ubuf)
184 struct pt_regs *regs = task_pt_regs(target);
187 /* PC, SR, SYSCALL */
188 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
190 0, 3 * sizeof(unsigned long long));
193 if (!ret && count > 0)
194 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
196 offsetof(struct pt_regs, regs[0]),
197 63 * sizeof(unsigned long long));
200 if (!ret && count > 0)
201 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
203 offsetof(struct pt_regs, tregs[0]),
204 8 * sizeof(unsigned long long));
207 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
208 sizeof(struct pt_regs), -1);
214 int fpregs_get(struct task_struct *target,
215 const struct user_regset *regset,
216 unsigned int pos, unsigned int count,
217 void *kbuf, void __user *ubuf)
221 ret = init_fpu(target);
225 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
226 &target->thread.xstate->hardfpu, 0, -1);
229 static int fpregs_set(struct task_struct *target,
230 const struct user_regset *regset,
231 unsigned int pos, unsigned int count,
232 const void *kbuf, const void __user *ubuf)
236 ret = init_fpu(target);
240 set_stopped_child_used_math(target);
242 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
243 &target->thread.xstate->hardfpu, 0, -1);
246 static int fpregs_active(struct task_struct *target,
247 const struct user_regset *regset)
249 return tsk_used_math(target) ? regset->n : 0;
253 const struct pt_regs_offset regoffset_table[] = {
256 REG_OFFSET_NAME(syscall_nr),
267 REGS_OFFSET_NAME(10),
268 REGS_OFFSET_NAME(11),
269 REGS_OFFSET_NAME(12),
270 REGS_OFFSET_NAME(13),
271 REGS_OFFSET_NAME(14),
272 REGS_OFFSET_NAME(15),
273 REGS_OFFSET_NAME(16),
274 REGS_OFFSET_NAME(17),
275 REGS_OFFSET_NAME(18),
276 REGS_OFFSET_NAME(19),
277 REGS_OFFSET_NAME(20),
278 REGS_OFFSET_NAME(21),
279 REGS_OFFSET_NAME(22),
280 REGS_OFFSET_NAME(23),
281 REGS_OFFSET_NAME(24),
282 REGS_OFFSET_NAME(25),
283 REGS_OFFSET_NAME(26),
284 REGS_OFFSET_NAME(27),
285 REGS_OFFSET_NAME(28),
286 REGS_OFFSET_NAME(29),
287 REGS_OFFSET_NAME(30),
288 REGS_OFFSET_NAME(31),
289 REGS_OFFSET_NAME(32),
290 REGS_OFFSET_NAME(33),
291 REGS_OFFSET_NAME(34),
292 REGS_OFFSET_NAME(35),
293 REGS_OFFSET_NAME(36),
294 REGS_OFFSET_NAME(37),
295 REGS_OFFSET_NAME(38),
296 REGS_OFFSET_NAME(39),
297 REGS_OFFSET_NAME(40),
298 REGS_OFFSET_NAME(41),
299 REGS_OFFSET_NAME(42),
300 REGS_OFFSET_NAME(43),
301 REGS_OFFSET_NAME(44),
302 REGS_OFFSET_NAME(45),
303 REGS_OFFSET_NAME(46),
304 REGS_OFFSET_NAME(47),
305 REGS_OFFSET_NAME(48),
306 REGS_OFFSET_NAME(49),
307 REGS_OFFSET_NAME(50),
308 REGS_OFFSET_NAME(51),
309 REGS_OFFSET_NAME(52),
310 REGS_OFFSET_NAME(53),
311 REGS_OFFSET_NAME(54),
312 REGS_OFFSET_NAME(55),
313 REGS_OFFSET_NAME(56),
314 REGS_OFFSET_NAME(57),
315 REGS_OFFSET_NAME(58),
316 REGS_OFFSET_NAME(59),
317 REGS_OFFSET_NAME(60),
318 REGS_OFFSET_NAME(61),
319 REGS_OFFSET_NAME(62),
320 REGS_OFFSET_NAME(63),
321 TREGS_OFFSET_NAME(0),
322 TREGS_OFFSET_NAME(1),
323 TREGS_OFFSET_NAME(2),
324 TREGS_OFFSET_NAME(3),
325 TREGS_OFFSET_NAME(4),
326 TREGS_OFFSET_NAME(5),
327 TREGS_OFFSET_NAME(6),
328 TREGS_OFFSET_NAME(7),
333 * These are our native regset flavours.
342 static const struct user_regset sh_regsets[] = {
350 .core_note_type = NT_PRSTATUS,
352 .size = sizeof(long long),
353 .align = sizeof(long long),
360 .core_note_type = NT_PRFPREG,
361 .n = sizeof(struct user_fpu_struct) /
363 .size = sizeof(long long),
364 .align = sizeof(long long),
367 .active = fpregs_active,
372 static const struct user_regset_view user_sh64_native_view = {
375 .regsets = sh_regsets,
376 .n = ARRAY_SIZE(sh_regsets),
379 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
381 return &user_sh64_native_view;
384 long arch_ptrace(struct task_struct *child, long request,
385 unsigned long addr, unsigned long data)
388 unsigned long __user *datap = (unsigned long __user *) data;
391 /* read the word at location addr in the USER area. */
392 case PTRACE_PEEKUSR: {
396 if ((addr & 3) || addr < 0)
399 if (addr < sizeof(struct pt_regs))
400 tmp = get_stack_long(child, addr);
401 else if ((addr >= offsetof(struct user, fpu)) &&
402 (addr < offsetof(struct user, u_fpvalid))) {
404 ret = init_fpu(child);
407 index = addr - offsetof(struct user, fpu);
408 tmp = get_fpu_long(child, index);
409 } else if (addr == offsetof(struct user, u_fpvalid)) {
410 tmp = !!tsk_used_math(child);
414 ret = put_user(tmp, datap);
419 /* write the word at location addr in the USER area. We must
420 disallow any changes to certain SR bits or u_fpvalid, since
421 this could crash the kernel or result in a security
424 if ((addr & 3) || addr < 0)
427 if (addr < sizeof(struct pt_regs)) {
428 /* Ignore change of top 32 bits of SR */
429 if (addr == offsetof (struct pt_regs, sr)+4)
434 /* If lower 32 bits of SR, ignore non-user bits */
435 if (addr == offsetof (struct pt_regs, sr))
437 long cursr = get_stack_long(child, addr);
439 data |= (cursr & SR_MASK);
441 ret = put_stack_long(child, addr, data);
443 else if ((addr >= offsetof(struct user, fpu)) &&
444 (addr < offsetof(struct user, u_fpvalid))) {
446 ret = init_fpu(child);
449 index = addr - offsetof(struct user, fpu);
450 ret = put_fpu_long(child, index, data);
455 return copy_regset_to_user(child, &user_sh64_native_view,
457 0, sizeof(struct pt_regs),
460 return copy_regset_from_user(child, &user_sh64_native_view,
462 0, sizeof(struct pt_regs),
465 case PTRACE_GETFPREGS:
466 return copy_regset_to_user(child, &user_sh64_native_view,
468 0, sizeof(struct user_fpu_struct),
470 case PTRACE_SETFPREGS:
471 return copy_regset_from_user(child, &user_sh64_native_view,
473 0, sizeof(struct user_fpu_struct),
477 ret = ptrace_request(child, request, addr, data);
484 asmlinkage int sh64_ptrace(long request, long pid,
485 unsigned long addr, unsigned long data)
487 #define WPC_DBRMODE 0x0d104008
488 static unsigned long first_call;
490 if (!test_and_set_bit(0, &first_call)) {
491 /* Set WPC.DBRMODE to 0. This makes all debug events get
492 * delivered through RESVEC, i.e. into the handlers in entry.S.
493 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
494 * would normally be left set to 1, which makes debug events get
495 * delivered through DBRVEC, i.e. into the remote gdb's
496 * handlers. This prevents ptrace getting them, and confuses
497 * the remote gdb.) */
498 printk("DBRMODE set to 0 to permit native debugging\n");
499 poke_real_address_q(WPC_DBRMODE, 0);
502 return sys_ptrace(request, pid, addr, data);
505 asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
509 secure_computing_strict(regs->regs[9]);
511 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
512 tracehook_report_syscall_entry(regs))
514 * Tracing decided this syscall should not happen.
515 * We'll return a bogus call number to get an ENOSYS
516 * error, but leave the original number in regs->regs[0].
520 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
521 trace_sys_enter(regs, regs->regs[9]);
523 audit_syscall_entry(regs->regs[1], regs->regs[2], regs->regs[3],
524 regs->regs[4], regs->regs[5]);
526 return ret ?: regs->regs[9];
529 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
533 audit_syscall_exit(regs);
535 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
536 trace_sys_exit(regs, regs->regs[9]);
538 step = test_thread_flag(TIF_SINGLESTEP);
539 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
540 tracehook_report_syscall_exit(regs, step);
543 /* Called with interrupts disabled */
544 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
546 /* This is called after a single step exception (DEBUGSS).
547 There is no need to change the PC, as it is a post-execution
548 exception, as entry.S does not do anything to the PC for DEBUGSS.
549 We need to clear the Single Step setting in SR to avoid
550 continually stepping. */
552 regs->sr &= ~SR_SSTEP;
556 /* Called with interrupts disabled */
557 BUILD_TRAP_HANDLER(breakpoint)
561 /* We need to forward step the PC, to counteract the backstep done
569 * Called by kernel/ptrace.c when detaching..
571 * Make sure single step bits etc are not set.
573 void ptrace_disable(struct task_struct *child)
575 user_disable_single_step(child);