2 * arch/sh/kernel/ptrace_64.c
4 * Copyright (C) 2000, 2001 Paolo Alberelli
5 * Copyright (C) 2003 - 2008 Paul Mundt
7 * Started from SH3/4 version:
8 * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
10 * Original x86 implementation:
11 * By Ross Biro 1/23/92
12 * edited by Linus Torvalds
14 * This file is subject to the terms and conditions of the GNU General Public
15 * License. See the file "COPYING" in the main directory of this archive
18 #include <linux/kernel.h>
19 #include <linux/rwsem.h>
20 #include <linux/sched.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/errno.h>
25 #include <linux/ptrace.h>
26 #include <linux/user.h>
27 #include <linux/signal.h>
28 #include <linux/syscalls.h>
29 #include <linux/audit.h>
30 #include <linux/seccomp.h>
31 #include <linux/tracehook.h>
32 #include <linux/elf.h>
33 #include <linux/regset.h>
35 #include <asm/uaccess.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
38 #include <asm/processor.h>
39 #include <asm/mmu_context.h>
40 #include <asm/syscalls.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/syscalls.h>
46 /* This mask defines the bits of the SR which the user is not allowed to
47 change, which are everything except S, Q, M, PR, SZ, FR. */
48 #define SR_MASK (0xffff8cfd)
51 * does not yet catch signals sent when the child dies.
52 * in exit.c or in signal.c.
56 * This routine will get a word from the user area in the process kernel stack.
58 static inline int get_stack_long(struct task_struct *task, int offset)
62 stack = (unsigned char *)(task->thread.uregs);
64 return (*((int *)stack));
67 static inline unsigned long
68 get_fpu_long(struct task_struct *task, unsigned long addr)
72 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
74 if (!tsk_used_math(task)) {
75 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
78 tmp = 0xffffffffUL; /* matches initial value in fpu.c */
83 if (last_task_used_math == task) {
87 last_task_used_math = 0;
91 tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
96 * This routine will put a word into the user area in the process kernel stack.
98 static inline int put_stack_long(struct task_struct *task, int offset,
101 unsigned char *stack;
103 stack = (unsigned char *)(task->thread.uregs);
105 *(unsigned long *) stack = data;
110 put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
112 struct pt_regs *regs;
114 regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
116 if (!tsk_used_math(task)) {
117 fpinit(&task->thread.fpu.hard);
118 set_stopped_child_used_math(task);
119 } else if (last_task_used_math == task) {
123 last_task_used_math = 0;
127 ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
131 void user_enable_single_step(struct task_struct *child)
133 struct pt_regs *regs = child->thread.uregs;
135 regs->sr |= SR_SSTEP; /* auto-resetting upon exception */
137 set_tsk_thread_flag(child, TIF_SINGLESTEP);
140 void user_disable_single_step(struct task_struct *child)
142 struct pt_regs *regs = child->thread.uregs;
144 regs->sr &= ~SR_SSTEP;
146 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
149 static int genregs_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_regs *regs = task_pt_regs(target);
157 /* PC, SR, SYSCALL */
158 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
160 0, 3 * sizeof(unsigned long long));
164 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
166 offsetof(struct pt_regs, regs[0]),
167 63 * sizeof(unsigned long long));
170 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
172 offsetof(struct pt_regs, tregs[0]),
173 8 * sizeof(unsigned long long));
176 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
177 sizeof(struct pt_regs), -1);
182 static int genregs_set(struct task_struct *target,
183 const struct user_regset *regset,
184 unsigned int pos, unsigned int count,
185 const void *kbuf, const void __user *ubuf)
187 struct pt_regs *regs = task_pt_regs(target);
190 /* PC, SR, SYSCALL */
191 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
193 0, 3 * sizeof(unsigned long long));
196 if (!ret && count > 0)
197 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
199 offsetof(struct pt_regs, regs[0]),
200 63 * sizeof(unsigned long long));
203 if (!ret && count > 0)
204 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
206 offsetof(struct pt_regs, tregs[0]),
207 8 * sizeof(unsigned long long));
210 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
211 sizeof(struct pt_regs), -1);
217 int fpregs_get(struct task_struct *target,
218 const struct user_regset *regset,
219 unsigned int pos, unsigned int count,
220 void *kbuf, void __user *ubuf)
224 ret = init_fpu(target);
228 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
229 &target->thread.fpu.hard, 0, -1);
232 static int fpregs_set(struct task_struct *target,
233 const struct user_regset *regset,
234 unsigned int pos, unsigned int count,
235 const void *kbuf, const void __user *ubuf)
239 ret = init_fpu(target);
243 set_stopped_child_used_math(target);
245 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
246 &target->thread.fpu.hard, 0, -1);
249 static int fpregs_active(struct task_struct *target,
250 const struct user_regset *regset)
252 return tsk_used_math(target) ? regset->n : 0;
257 * These are our native regset flavours.
266 static const struct user_regset sh_regsets[] = {
274 .core_note_type = NT_PRSTATUS,
276 .size = sizeof(long long),
277 .align = sizeof(long long),
284 .core_note_type = NT_PRFPREG,
285 .n = sizeof(struct user_fpu_struct) /
287 .size = sizeof(long long),
288 .align = sizeof(long long),
291 .active = fpregs_active,
296 static const struct user_regset_view user_sh64_native_view = {
299 .regsets = sh_regsets,
300 .n = ARRAY_SIZE(sh_regsets),
303 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
305 return &user_sh64_native_view;
308 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
313 /* read the word at location addr in the USER area. */
314 case PTRACE_PEEKUSR: {
318 if ((addr & 3) || addr < 0)
321 if (addr < sizeof(struct pt_regs))
322 tmp = get_stack_long(child, addr);
323 else if ((addr >= offsetof(struct user, fpu)) &&
324 (addr < offsetof(struct user, u_fpvalid))) {
325 tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
326 } else if (addr == offsetof(struct user, u_fpvalid)) {
327 tmp = !!tsk_used_math(child);
331 ret = put_user(tmp, (unsigned long *)data);
336 /* write the word at location addr in the USER area. We must
337 disallow any changes to certain SR bits or u_fpvalid, since
338 this could crash the kernel or result in a security
341 if ((addr & 3) || addr < 0)
344 if (addr < sizeof(struct pt_regs)) {
345 /* Ignore change of top 32 bits of SR */
346 if (addr == offsetof (struct pt_regs, sr)+4)
351 /* If lower 32 bits of SR, ignore non-user bits */
352 if (addr == offsetof (struct pt_regs, sr))
354 long cursr = get_stack_long(child, addr);
356 data |= (cursr & SR_MASK);
358 ret = put_stack_long(child, addr, data);
360 else if ((addr >= offsetof(struct user, fpu)) &&
361 (addr < offsetof(struct user, u_fpvalid))) {
362 ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
367 return copy_regset_to_user(child, &user_sh64_native_view,
369 0, sizeof(struct pt_regs),
370 (void __user *)data);
372 return copy_regset_from_user(child, &user_sh64_native_view,
374 0, sizeof(struct pt_regs),
375 (const void __user *)data);
377 case PTRACE_GETFPREGS:
378 return copy_regset_to_user(child, &user_sh64_native_view,
380 0, sizeof(struct user_fpu_struct),
381 (void __user *)data);
382 case PTRACE_SETFPREGS:
383 return copy_regset_from_user(child, &user_sh64_native_view,
385 0, sizeof(struct user_fpu_struct),
386 (const void __user *)data);
389 ret = ptrace_request(child, request, addr, data);
396 asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
398 #define WPC_DBRMODE 0x0d104008
399 static int first_call = 1;
403 /* Set WPC.DBRMODE to 0. This makes all debug events get
404 * delivered through RESVEC, i.e. into the handlers in entry.S.
405 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
406 * would normally be left set to 1, which makes debug events get
407 * delivered through DBRVEC, i.e. into the remote gdb's
408 * handlers. This prevents ptrace getting them, and confuses
409 * the remote gdb.) */
410 printk("DBRMODE set to 0 to permit native debugging\n");
411 poke_real_address_q(WPC_DBRMODE, 0);
416 return sys_ptrace(request, pid, addr, data);
419 static inline int audit_arch(void)
424 arch |= __AUDIT_ARCH_64BIT;
426 #ifdef CONFIG_CPU_LITTLE_ENDIAN
427 arch |= __AUDIT_ARCH_LE;
433 asmlinkage long long do_syscall_trace_enter(struct pt_regs *regs)
437 secure_computing(regs->regs[9]);
439 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
440 tracehook_report_syscall_entry(regs))
442 * Tracing decided this syscall should not happen.
443 * We'll return a bogus call number to get an ENOSYS
444 * error, but leave the original number in regs->regs[0].
448 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
449 trace_sys_enter(regs, regs->regs[9]);
451 if (unlikely(current->audit_context))
452 audit_syscall_entry(audit_arch(), regs->regs[1],
453 regs->regs[2], regs->regs[3],
454 regs->regs[4], regs->regs[5]);
456 return ret ?: regs->regs[9];
459 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
463 if (unlikely(current->audit_context))
464 audit_syscall_exit(AUDITSC_RESULT(regs->regs[9]),
467 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
468 trace_sys_exit(regs, regs->regs[9]);
470 step = test_thread_flag(TIF_SINGLESTEP);
471 if (step || test_thread_flag(TIF_SYSCALL_TRACE))
472 tracehook_report_syscall_exit(regs, step);
475 /* Called with interrupts disabled */
476 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
478 /* This is called after a single step exception (DEBUGSS).
479 There is no need to change the PC, as it is a post-execution
480 exception, as entry.S does not do anything to the PC for DEBUGSS.
481 We need to clear the Single Step setting in SR to avoid
482 continually stepping. */
484 regs->sr &= ~SR_SSTEP;
485 force_sig(SIGTRAP, current);
488 /* Called with interrupts disabled */
489 asmlinkage void do_software_break_point(unsigned long long vec,
490 struct pt_regs *regs)
492 /* We need to forward step the PC, to counteract the backstep done
495 force_sig(SIGTRAP, current);
500 * Called by kernel/ptrace.c when detaching..
502 * Make sure single step bits etc are not set.
504 void ptrace_disable(struct task_struct *child)
506 user_disable_single_step(child);