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>
21 #include <linux/sched/task_stack.h>
23 #include <linux/smp.h>
24 #include <linux/bitops.h>
25 #include <linux/errno.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
28 #include <linux/signal.h>
29 #include <linux/syscalls.h>
30 #include <linux/audit.h>
31 #include <linux/seccomp.h>
32 #include <linux/tracehook.h>
33 #include <linux/elf.h>
34 #include <linux/regset.h>
36 #include <linux/uaccess.h>
37 #include <asm/pgtable.h>
38 #include <asm/processor.h>
39 #include <asm/mmu_context.h>
40 #include <asm/syscalls.h>
42 #include <asm/traps.h>
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/syscalls.h>
47 /* This mask defines the bits of the SR which the user is not allowed to
48 change, which are everything except S, Q, M, PR, SZ, FR. */
49 #define SR_MASK (0xffff8cfd)
52 * does not yet catch signals sent when the child dies.
53 * in exit.c or in signal.c.
57 * This routine will get a word from the user area in the process kernel stack.
59 static inline int get_stack_long(struct task_struct
*task
, int offset
)
63 stack
= (unsigned char *)(task
->thread
.uregs
);
65 return (*((int *)stack
));
68 static inline unsigned long
69 get_fpu_long(struct task_struct
*task
, unsigned long addr
)
73 regs
= (struct pt_regs
*)((unsigned char *)task
+ THREAD_SIZE
) - 1;
75 if (!tsk_used_math(task
)) {
76 if (addr
== offsetof(struct user_fpu_struct
, fpscr
)) {
79 tmp
= 0xffffffffUL
; /* matches initial value in fpu.c */
84 if (last_task_used_math
== task
) {
88 last_task_used_math
= 0;
92 tmp
= ((long *)task
->thread
.xstate
)[addr
/ sizeof(unsigned long)];
97 * This routine will put a word into the user area in the process kernel stack.
99 static inline int put_stack_long(struct task_struct
*task
, int offset
,
102 unsigned char *stack
;
104 stack
= (unsigned char *)(task
->thread
.uregs
);
106 *(unsigned long *) stack
= data
;
111 put_fpu_long(struct task_struct
*task
, unsigned long addr
, unsigned long data
)
113 struct pt_regs
*regs
;
115 regs
= (struct pt_regs
*)((unsigned char *)task
+ THREAD_SIZE
) - 1;
117 if (!tsk_used_math(task
)) {
119 } else if (last_task_used_math
== task
) {
123 last_task_used_math
= 0;
127 ((long *)task
->thread
.xstate
)[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
.xstate
->hardfpu
, 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
.xstate
->hardfpu
, 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;
256 const struct pt_regs_offset regoffset_table
[] = {
259 REG_OFFSET_NAME(syscall_nr
),
270 REGS_OFFSET_NAME(10),
271 REGS_OFFSET_NAME(11),
272 REGS_OFFSET_NAME(12),
273 REGS_OFFSET_NAME(13),
274 REGS_OFFSET_NAME(14),
275 REGS_OFFSET_NAME(15),
276 REGS_OFFSET_NAME(16),
277 REGS_OFFSET_NAME(17),
278 REGS_OFFSET_NAME(18),
279 REGS_OFFSET_NAME(19),
280 REGS_OFFSET_NAME(20),
281 REGS_OFFSET_NAME(21),
282 REGS_OFFSET_NAME(22),
283 REGS_OFFSET_NAME(23),
284 REGS_OFFSET_NAME(24),
285 REGS_OFFSET_NAME(25),
286 REGS_OFFSET_NAME(26),
287 REGS_OFFSET_NAME(27),
288 REGS_OFFSET_NAME(28),
289 REGS_OFFSET_NAME(29),
290 REGS_OFFSET_NAME(30),
291 REGS_OFFSET_NAME(31),
292 REGS_OFFSET_NAME(32),
293 REGS_OFFSET_NAME(33),
294 REGS_OFFSET_NAME(34),
295 REGS_OFFSET_NAME(35),
296 REGS_OFFSET_NAME(36),
297 REGS_OFFSET_NAME(37),
298 REGS_OFFSET_NAME(38),
299 REGS_OFFSET_NAME(39),
300 REGS_OFFSET_NAME(40),
301 REGS_OFFSET_NAME(41),
302 REGS_OFFSET_NAME(42),
303 REGS_OFFSET_NAME(43),
304 REGS_OFFSET_NAME(44),
305 REGS_OFFSET_NAME(45),
306 REGS_OFFSET_NAME(46),
307 REGS_OFFSET_NAME(47),
308 REGS_OFFSET_NAME(48),
309 REGS_OFFSET_NAME(49),
310 REGS_OFFSET_NAME(50),
311 REGS_OFFSET_NAME(51),
312 REGS_OFFSET_NAME(52),
313 REGS_OFFSET_NAME(53),
314 REGS_OFFSET_NAME(54),
315 REGS_OFFSET_NAME(55),
316 REGS_OFFSET_NAME(56),
317 REGS_OFFSET_NAME(57),
318 REGS_OFFSET_NAME(58),
319 REGS_OFFSET_NAME(59),
320 REGS_OFFSET_NAME(60),
321 REGS_OFFSET_NAME(61),
322 REGS_OFFSET_NAME(62),
323 REGS_OFFSET_NAME(63),
324 TREGS_OFFSET_NAME(0),
325 TREGS_OFFSET_NAME(1),
326 TREGS_OFFSET_NAME(2),
327 TREGS_OFFSET_NAME(3),
328 TREGS_OFFSET_NAME(4),
329 TREGS_OFFSET_NAME(5),
330 TREGS_OFFSET_NAME(6),
331 TREGS_OFFSET_NAME(7),
336 * These are our native regset flavours.
345 static const struct user_regset sh_regsets
[] = {
353 .core_note_type
= NT_PRSTATUS
,
355 .size
= sizeof(long long),
356 .align
= sizeof(long long),
363 .core_note_type
= NT_PRFPREG
,
364 .n
= sizeof(struct user_fpu_struct
) /
366 .size
= sizeof(long long),
367 .align
= sizeof(long long),
370 .active
= fpregs_active
,
375 static const struct user_regset_view user_sh64_native_view
= {
378 .regsets
= sh_regsets
,
379 .n
= ARRAY_SIZE(sh_regsets
),
382 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
384 return &user_sh64_native_view
;
387 long arch_ptrace(struct task_struct
*child
, long request
,
388 unsigned long addr
, unsigned long data
)
391 unsigned long __user
*datap
= (unsigned long __user
*) data
;
394 /* read the word at location addr in the USER area. */
395 case PTRACE_PEEKUSR
: {
399 if ((addr
& 3) || addr
< 0)
402 if (addr
< sizeof(struct pt_regs
))
403 tmp
= get_stack_long(child
, addr
);
404 else if ((addr
>= offsetof(struct user
, fpu
)) &&
405 (addr
< offsetof(struct user
, u_fpvalid
))) {
407 ret
= init_fpu(child
);
410 index
= addr
- offsetof(struct user
, fpu
);
411 tmp
= get_fpu_long(child
, index
);
412 } else if (addr
== offsetof(struct user
, u_fpvalid
)) {
413 tmp
= !!tsk_used_math(child
);
417 ret
= put_user(tmp
, datap
);
422 /* write the word at location addr in the USER area. We must
423 disallow any changes to certain SR bits or u_fpvalid, since
424 this could crash the kernel or result in a security
427 if ((addr
& 3) || addr
< 0)
430 if (addr
< sizeof(struct pt_regs
)) {
431 /* Ignore change of top 32 bits of SR */
432 if (addr
== offsetof (struct pt_regs
, sr
)+4)
437 /* If lower 32 bits of SR, ignore non-user bits */
438 if (addr
== offsetof (struct pt_regs
, sr
))
440 long cursr
= get_stack_long(child
, addr
);
442 data
|= (cursr
& SR_MASK
);
444 ret
= put_stack_long(child
, addr
, data
);
446 else if ((addr
>= offsetof(struct user
, fpu
)) &&
447 (addr
< offsetof(struct user
, u_fpvalid
))) {
449 ret
= init_fpu(child
);
452 index
= addr
- offsetof(struct user
, fpu
);
453 ret
= put_fpu_long(child
, index
, data
);
458 return copy_regset_to_user(child
, &user_sh64_native_view
,
460 0, sizeof(struct pt_regs
),
463 return copy_regset_from_user(child
, &user_sh64_native_view
,
465 0, sizeof(struct pt_regs
),
468 case PTRACE_GETFPREGS
:
469 return copy_regset_to_user(child
, &user_sh64_native_view
,
471 0, sizeof(struct user_fpu_struct
),
473 case PTRACE_SETFPREGS
:
474 return copy_regset_from_user(child
, &user_sh64_native_view
,
476 0, sizeof(struct user_fpu_struct
),
480 ret
= ptrace_request(child
, request
, addr
, data
);
487 asmlinkage
int sh64_ptrace(long request
, long pid
,
488 unsigned long addr
, unsigned long data
)
490 #define WPC_DBRMODE 0x0d104008
491 static unsigned long first_call
;
493 if (!test_and_set_bit(0, &first_call
)) {
494 /* Set WPC.DBRMODE to 0. This makes all debug events get
495 * delivered through RESVEC, i.e. into the handlers in entry.S.
496 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
497 * would normally be left set to 1, which makes debug events get
498 * delivered through DBRVEC, i.e. into the remote gdb's
499 * handlers. This prevents ptrace getting them, and confuses
500 * the remote gdb.) */
501 printk("DBRMODE set to 0 to permit native debugging\n");
502 poke_real_address_q(WPC_DBRMODE
, 0);
505 return sys_ptrace(request
, pid
, addr
, data
);
508 asmlinkage
long long do_syscall_trace_enter(struct pt_regs
*regs
)
512 secure_computing_strict(regs
->regs
[9]);
514 if (test_thread_flag(TIF_SYSCALL_TRACE
) &&
515 tracehook_report_syscall_entry(regs
))
517 * Tracing decided this syscall should not happen.
518 * We'll return a bogus call number to get an ENOSYS
519 * error, but leave the original number in regs->regs[0].
523 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
524 trace_sys_enter(regs
, regs
->regs
[9]);
526 audit_syscall_entry(regs
->regs
[1], regs
->regs
[2], regs
->regs
[3],
527 regs
->regs
[4], regs
->regs
[5]);
529 return ret
?: regs
->regs
[9];
532 asmlinkage
void do_syscall_trace_leave(struct pt_regs
*regs
)
536 audit_syscall_exit(regs
);
538 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT
)))
539 trace_sys_exit(regs
, regs
->regs
[9]);
541 step
= test_thread_flag(TIF_SINGLESTEP
);
542 if (step
|| test_thread_flag(TIF_SYSCALL_TRACE
))
543 tracehook_report_syscall_exit(regs
, step
);
546 /* Called with interrupts disabled */
547 asmlinkage
void do_single_step(unsigned long long vec
, struct pt_regs
*regs
)
549 /* This is called after a single step exception (DEBUGSS).
550 There is no need to change the PC, as it is a post-execution
551 exception, as entry.S does not do anything to the PC for DEBUGSS.
552 We need to clear the Single Step setting in SR to avoid
553 continually stepping. */
555 regs
->sr
&= ~SR_SSTEP
;
556 force_sig(SIGTRAP
, current
);
559 /* Called with interrupts disabled */
560 BUILD_TRAP_HANDLER(breakpoint
)
564 /* We need to forward step the PC, to counteract the backstep done
567 force_sig(SIGTRAP
, current
);
572 * Called by kernel/ptrace.c when detaching..
574 * Make sure single step bits etc are not set.
576 void ptrace_disable(struct task_struct
*child
)
578 user_disable_single_step(child
);