2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2001 - 2007 Tensilica Inc.
8 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
9 * Chris Zankel <chris@zankel.net>
10 * Scott Foehner<sfoehner@yahoo.com>,
12 * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
15 #include <linux/errno.h>
16 #include <linux/hw_breakpoint.h>
17 #include <linux/kernel.h>
19 #include <linux/perf_event.h>
20 #include <linux/ptrace.h>
21 #include <linux/sched.h>
22 #include <linux/sched/task_stack.h>
23 #include <linux/security.h>
24 #include <linux/signal.h>
25 #include <linux/smp.h>
26 #include <linux/tracehook.h>
27 #include <linux/uaccess.h>
29 #include <asm/coprocessor.h>
32 #include <asm/pgtable.h>
33 #include <asm/ptrace.h>
36 void user_enable_single_step(struct task_struct
*child
)
38 child
->ptrace
|= PT_SINGLESTEP
;
41 void user_disable_single_step(struct task_struct
*child
)
43 child
->ptrace
&= ~PT_SINGLESTEP
;
47 * Called by kernel/ptrace.c when detaching to disable single stepping.
50 void ptrace_disable(struct task_struct
*child
)
55 static int ptrace_getregs(struct task_struct
*child
, void __user
*uregs
)
57 struct pt_regs
*regs
= task_pt_regs(child
);
58 xtensa_gregset_t __user
*gregset
= uregs
;
59 unsigned long wb
= regs
->windowbase
;
62 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(xtensa_gregset_t
)))
65 __put_user(regs
->pc
, &gregset
->pc
);
66 __put_user(regs
->ps
& ~(1 << PS_EXCM_BIT
), &gregset
->ps
);
67 __put_user(regs
->lbeg
, &gregset
->lbeg
);
68 __put_user(regs
->lend
, &gregset
->lend
);
69 __put_user(regs
->lcount
, &gregset
->lcount
);
70 __put_user(regs
->windowstart
, &gregset
->windowstart
);
71 __put_user(regs
->windowbase
, &gregset
->windowbase
);
72 __put_user(regs
->threadptr
, &gregset
->threadptr
);
74 for (i
= 0; i
< XCHAL_NUM_AREGS
; i
++)
75 __put_user(regs
->areg
[i
],
76 gregset
->a
+ ((wb
* 4 + i
) % XCHAL_NUM_AREGS
));
81 static int ptrace_setregs(struct task_struct
*child
, void __user
*uregs
)
83 struct pt_regs
*regs
= task_pt_regs(child
);
84 xtensa_gregset_t
*gregset
= uregs
;
85 const unsigned long ps_mask
= PS_CALLINC_MASK
| PS_OWB_MASK
;
89 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(xtensa_gregset_t
)))
92 __get_user(regs
->pc
, &gregset
->pc
);
93 __get_user(ps
, &gregset
->ps
);
94 __get_user(regs
->lbeg
, &gregset
->lbeg
);
95 __get_user(regs
->lend
, &gregset
->lend
);
96 __get_user(regs
->lcount
, &gregset
->lcount
);
97 __get_user(ws
, &gregset
->windowstart
);
98 __get_user(wb
, &gregset
->windowbase
);
99 __get_user(regs
->threadptr
, &gregset
->threadptr
);
101 regs
->ps
= (regs
->ps
& ~ps_mask
) | (ps
& ps_mask
) | (1 << PS_EXCM_BIT
);
103 if (wb
>= XCHAL_NUM_AREGS
/ 4)
106 if (wb
!= regs
->windowbase
|| ws
!= regs
->windowstart
) {
107 unsigned long rotws
, wmask
;
109 rotws
= (((ws
| (ws
<< WSBITS
)) >> wb
) &
110 ((1 << WSBITS
) - 1)) & ~1;
111 wmask
= ((rotws
? WSBITS
+ 1 - ffs(rotws
) : 0) << 4) |
113 regs
->windowbase
= wb
;
114 regs
->windowstart
= ws
;
118 if (wb
!= 0 && __copy_from_user(regs
->areg
+ XCHAL_NUM_AREGS
- wb
* 4,
119 gregset
->a
, wb
* 16))
122 if (__copy_from_user(regs
->areg
, gregset
->a
+ wb
* 4,
130 #if XTENSA_HAVE_COPROCESSORS
131 #define CP_OFFSETS(cp) \
133 .elf_xtregs_offset = offsetof(elf_xtregs_t, cp), \
134 .ti_offset = offsetof(struct thread_info, xtregs_cp.cp), \
135 .sz = sizeof(xtregs_ ## cp ## _t), \
138 static const struct {
139 size_t elf_xtregs_offset
;
154 static int ptrace_getxregs(struct task_struct
*child
, void __user
*uregs
)
156 struct pt_regs
*regs
= task_pt_regs(child
);
157 struct thread_info
*ti
= task_thread_info(child
);
158 elf_xtregs_t __user
*xtregs
= uregs
;
160 int i __maybe_unused
;
162 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(elf_xtregs_t
)))
165 #if XTENSA_HAVE_COPROCESSORS
166 /* Flush all coprocessor registers to memory. */
167 coprocessor_flush_all(ti
);
169 for (i
= 0; i
< ARRAY_SIZE(cp_offsets
); ++i
)
170 ret
|= __copy_to_user((char __user
*)xtregs
+
171 cp_offsets
[i
].elf_xtregs_offset
,
173 cp_offsets
[i
].ti_offset
,
176 ret
|= __copy_to_user(&xtregs
->opt
, ®s
->xtregs_opt
,
177 sizeof(xtregs
->opt
));
178 ret
|= __copy_to_user(&xtregs
->user
,&ti
->xtregs_user
,
179 sizeof(xtregs
->user
));
181 return ret
? -EFAULT
: 0;
184 static int ptrace_setxregs(struct task_struct
*child
, void __user
*uregs
)
186 struct thread_info
*ti
= task_thread_info(child
);
187 struct pt_regs
*regs
= task_pt_regs(child
);
188 elf_xtregs_t
*xtregs
= uregs
;
190 int i __maybe_unused
;
192 if (!access_ok(VERIFY_READ
, uregs
, sizeof(elf_xtregs_t
)))
195 #if XTENSA_HAVE_COPROCESSORS
196 /* Flush all coprocessors before we overwrite them. */
197 coprocessor_flush_all(ti
);
198 coprocessor_release_all(ti
);
200 for (i
= 0; i
< ARRAY_SIZE(cp_offsets
); ++i
)
201 ret
|= __copy_from_user((char *)ti
+ cp_offsets
[i
].ti_offset
,
202 (const char __user
*)xtregs
+
203 cp_offsets
[i
].elf_xtregs_offset
,
206 ret
|= __copy_from_user(®s
->xtregs_opt
, &xtregs
->opt
,
207 sizeof(xtregs
->opt
));
208 ret
|= __copy_from_user(&ti
->xtregs_user
, &xtregs
->user
,
209 sizeof(xtregs
->user
));
211 return ret
? -EFAULT
: 0;
214 static int ptrace_peekusr(struct task_struct
*child
, long regno
,
217 struct pt_regs
*regs
;
220 regs
= task_pt_regs(child
);
221 tmp
= 0; /* Default return value. */
224 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
225 tmp
= regs
->areg
[regno
- REG_AR_BASE
];
228 case REG_A_BASE
... REG_A_BASE
+ 15:
229 tmp
= regs
->areg
[regno
- REG_A_BASE
];
237 /* Note: PS.EXCM is not set while user task is running;
238 * its being set in regs is for exception handling
241 tmp
= (regs
->ps
& ~(1 << PS_EXCM_BIT
));
249 unsigned long wb
= regs
->windowbase
;
250 unsigned long ws
= regs
->windowstart
;
251 tmp
= ((ws
>> wb
) | (ws
<< (WSBITS
- wb
))) &
278 return put_user(tmp
, ret
);
281 static int ptrace_pokeusr(struct task_struct
*child
, long regno
, long val
)
283 struct pt_regs
*regs
;
284 regs
= task_pt_regs(child
);
287 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
288 regs
->areg
[regno
- REG_AR_BASE
] = val
;
291 case REG_A_BASE
... REG_A_BASE
+ 15:
292 regs
->areg
[regno
- REG_A_BASE
] = val
;
309 #ifdef CONFIG_HAVE_HW_BREAKPOINT
310 static void ptrace_hbptriggered(struct perf_event
*bp
,
311 struct perf_sample_data
*data
,
312 struct pt_regs
*regs
)
316 struct arch_hw_breakpoint
*bkpt
= counter_arch_bp(bp
);
318 if (bp
->attr
.bp_type
& HW_BREAKPOINT_X
) {
319 for (i
= 0; i
< XCHAL_NUM_IBREAK
; ++i
)
320 if (current
->thread
.ptrace_bp
[i
] == bp
)
324 for (i
= 0; i
< XCHAL_NUM_DBREAK
; ++i
)
325 if (current
->thread
.ptrace_wp
[i
] == bp
)
330 info
.si_signo
= SIGTRAP
;
332 info
.si_code
= TRAP_HWBKPT
;
333 info
.si_addr
= (void __user
*)bkpt
->address
;
335 force_sig_info(SIGTRAP
, &info
, current
);
338 static struct perf_event
*ptrace_hbp_create(struct task_struct
*tsk
, int type
)
340 struct perf_event_attr attr
;
342 ptrace_breakpoint_init(&attr
);
344 /* Initialise fields to sane defaults. */
350 return register_user_hw_breakpoint(&attr
, ptrace_hbptriggered
, NULL
,
355 * Address bit 0 choose instruction (0) or data (1) break register, bits
356 * 31..1 are the register number.
357 * Both PTRACE_GETHBPREGS and PTRACE_SETHBPREGS transfer two 32-bit words:
358 * address (0) and control (1).
359 * Instruction breakpoint contorl word is 0 to clear breakpoint, 1 to set.
360 * Data breakpoint control word bit 31 is 'trigger on store', bit 30 is
361 * 'trigger on load, bits 29..0 are length. Length 0 is used to clear a
362 * breakpoint. To set a breakpoint length must be a power of 2 in the range
363 * 1..64 and the address must be length-aligned.
366 static long ptrace_gethbpregs(struct task_struct
*child
, long addr
,
369 struct perf_event
*bp
;
370 u32 user_data
[2] = {0};
371 bool dbreak
= addr
& 1;
372 unsigned idx
= addr
>> 1;
374 if ((!dbreak
&& idx
>= XCHAL_NUM_IBREAK
) ||
375 (dbreak
&& idx
>= XCHAL_NUM_DBREAK
))
379 bp
= child
->thread
.ptrace_wp
[idx
];
381 bp
= child
->thread
.ptrace_bp
[idx
];
384 user_data
[0] = bp
->attr
.bp_addr
;
385 user_data
[1] = bp
->attr
.disabled
? 0 : bp
->attr
.bp_len
;
387 if (bp
->attr
.bp_type
& HW_BREAKPOINT_R
)
388 user_data
[1] |= DBREAKC_LOAD_MASK
;
389 if (bp
->attr
.bp_type
& HW_BREAKPOINT_W
)
390 user_data
[1] |= DBREAKC_STOR_MASK
;
394 if (copy_to_user(datap
, user_data
, sizeof(user_data
)))
400 static long ptrace_sethbpregs(struct task_struct
*child
, long addr
,
403 struct perf_event
*bp
;
404 struct perf_event_attr attr
;
406 bool dbreak
= addr
& 1;
407 unsigned idx
= addr
>> 1;
410 if ((!dbreak
&& idx
>= XCHAL_NUM_IBREAK
) ||
411 (dbreak
&& idx
>= XCHAL_NUM_DBREAK
))
414 if (copy_from_user(user_data
, datap
, sizeof(user_data
)))
418 bp
= child
->thread
.ptrace_wp
[idx
];
419 if (user_data
[1] & DBREAKC_LOAD_MASK
)
420 bp_type
|= HW_BREAKPOINT_R
;
421 if (user_data
[1] & DBREAKC_STOR_MASK
)
422 bp_type
|= HW_BREAKPOINT_W
;
424 bp
= child
->thread
.ptrace_bp
[idx
];
425 bp_type
= HW_BREAKPOINT_X
;
429 bp
= ptrace_hbp_create(child
,
430 bp_type
? bp_type
: HW_BREAKPOINT_RW
);
434 child
->thread
.ptrace_wp
[idx
] = bp
;
436 child
->thread
.ptrace_bp
[idx
] = bp
;
440 attr
.bp_addr
= user_data
[0];
441 attr
.bp_len
= user_data
[1] & ~(DBREAKC_LOAD_MASK
| DBREAKC_STOR_MASK
);
442 attr
.bp_type
= bp_type
;
443 attr
.disabled
= !attr
.bp_len
;
445 return modify_user_hw_breakpoint(bp
, &attr
);
449 long arch_ptrace(struct task_struct
*child
, long request
,
450 unsigned long addr
, unsigned long data
)
453 void __user
*datap
= (void __user
*) data
;
456 case PTRACE_PEEKTEXT
: /* read word at location addr. */
457 case PTRACE_PEEKDATA
:
458 ret
= generic_ptrace_peekdata(child
, addr
, data
);
461 case PTRACE_PEEKUSR
: /* read register specified by addr. */
462 ret
= ptrace_peekusr(child
, addr
, datap
);
465 case PTRACE_POKETEXT
: /* write the word at location addr. */
466 case PTRACE_POKEDATA
:
467 ret
= generic_ptrace_pokedata(child
, addr
, data
);
470 case PTRACE_POKEUSR
: /* write register specified by addr. */
471 ret
= ptrace_pokeusr(child
, addr
, data
);
475 ret
= ptrace_getregs(child
, datap
);
479 ret
= ptrace_setregs(child
, datap
);
482 case PTRACE_GETXTREGS
:
483 ret
= ptrace_getxregs(child
, datap
);
486 case PTRACE_SETXTREGS
:
487 ret
= ptrace_setxregs(child
, datap
);
489 #ifdef CONFIG_HAVE_HW_BREAKPOINT
490 case PTRACE_GETHBPREGS
:
491 ret
= ptrace_gethbpregs(child
, addr
, datap
);
494 case PTRACE_SETHBPREGS
:
495 ret
= ptrace_sethbpregs(child
, addr
, datap
);
499 ret
= ptrace_request(child
, request
, addr
, data
);
506 unsigned long do_syscall_trace_enter(struct pt_regs
*regs
)
508 if (test_thread_flag(TIF_SYSCALL_TRACE
) &&
509 tracehook_report_syscall_entry(regs
))
512 return regs
->areg
[2];
515 void do_syscall_trace_leave(struct pt_regs
*regs
)
519 step
= test_thread_flag(TIF_SINGLESTEP
);
521 if (step
|| test_thread_flag(TIF_SYSCALL_TRACE
))
522 tracehook_report_syscall_exit(regs
, step
);