1 // TODO some minor issues
3 * This file is subject to the terms and conditions of the GNU General Public
4 * License. See the file "COPYING" in the main directory of this archive
7 * Copyright (C) 2001 - 2007 Tensilica Inc.
9 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
10 * Chris Zankel <chris@zankel.net>
11 * Scott Foehner<sfoehner@yahoo.com>,
13 * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
16 #include <linux/errno.h>
17 #include <linux/hw_breakpoint.h>
18 #include <linux/kernel.h>
20 #include <linux/perf_event.h>
21 #include <linux/ptrace.h>
22 #include <linux/sched.h>
23 #include <linux/security.h>
24 #include <linux/signal.h>
25 #include <linux/smp.h>
27 #include <asm/coprocessor.h>
30 #include <asm/pgtable.h>
31 #include <asm/ptrace.h>
32 #include <asm/uaccess.h>
35 void user_enable_single_step(struct task_struct
*child
)
37 child
->ptrace
|= PT_SINGLESTEP
;
40 void user_disable_single_step(struct task_struct
*child
)
42 child
->ptrace
&= ~PT_SINGLESTEP
;
46 * Called by kernel/ptrace.c when detaching to disable single stepping.
49 void ptrace_disable(struct task_struct
*child
)
54 int ptrace_getregs(struct task_struct
*child
, void __user
*uregs
)
56 struct pt_regs
*regs
= task_pt_regs(child
);
57 xtensa_gregset_t __user
*gregset
= uregs
;
58 unsigned long wb
= regs
->windowbase
;
61 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(xtensa_gregset_t
)))
64 __put_user(regs
->pc
, &gregset
->pc
);
65 __put_user(regs
->ps
& ~(1 << PS_EXCM_BIT
), &gregset
->ps
);
66 __put_user(regs
->lbeg
, &gregset
->lbeg
);
67 __put_user(regs
->lend
, &gregset
->lend
);
68 __put_user(regs
->lcount
, &gregset
->lcount
);
69 __put_user(regs
->windowstart
, &gregset
->windowstart
);
70 __put_user(regs
->windowbase
, &gregset
->windowbase
);
71 __put_user(regs
->threadptr
, &gregset
->threadptr
);
73 for (i
= 0; i
< XCHAL_NUM_AREGS
; i
++)
74 __put_user(regs
->areg
[i
],
75 gregset
->a
+ ((wb
* 4 + i
) % XCHAL_NUM_AREGS
));
80 int ptrace_setregs(struct task_struct
*child
, void __user
*uregs
)
82 struct pt_regs
*regs
= task_pt_regs(child
);
83 xtensa_gregset_t
*gregset
= uregs
;
84 const unsigned long ps_mask
= PS_CALLINC_MASK
| PS_OWB_MASK
;
88 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(xtensa_gregset_t
)))
91 __get_user(regs
->pc
, &gregset
->pc
);
92 __get_user(ps
, &gregset
->ps
);
93 __get_user(regs
->lbeg
, &gregset
->lbeg
);
94 __get_user(regs
->lend
, &gregset
->lend
);
95 __get_user(regs
->lcount
, &gregset
->lcount
);
96 __get_user(ws
, &gregset
->windowstart
);
97 __get_user(wb
, &gregset
->windowbase
);
98 __get_user(regs
->threadptr
, &gregset
->threadptr
);
100 regs
->ps
= (regs
->ps
& ~ps_mask
) | (ps
& ps_mask
) | (1 << PS_EXCM_BIT
);
102 if (wb
>= XCHAL_NUM_AREGS
/ 4)
105 if (wb
!= regs
->windowbase
|| ws
!= regs
->windowstart
) {
106 unsigned long rotws
, wmask
;
108 rotws
= (((ws
| (ws
<< WSBITS
)) >> wb
) &
109 ((1 << WSBITS
) - 1)) & ~1;
110 wmask
= ((rotws
? WSBITS
+ 1 - ffs(rotws
) : 0) << 4) |
112 regs
->windowbase
= wb
;
113 regs
->windowstart
= ws
;
117 if (wb
!= 0 && __copy_from_user(regs
->areg
+ XCHAL_NUM_AREGS
- wb
* 4,
118 gregset
->a
, wb
* 16))
121 if (__copy_from_user(regs
->areg
, gregset
->a
+ wb
* 4,
129 int ptrace_getxregs(struct task_struct
*child
, void __user
*uregs
)
131 struct pt_regs
*regs
= task_pt_regs(child
);
132 struct thread_info
*ti
= task_thread_info(child
);
133 elf_xtregs_t __user
*xtregs
= uregs
;
136 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(elf_xtregs_t
)))
139 #if XTENSA_HAVE_COPROCESSORS
140 /* Flush all coprocessor registers to memory. */
141 coprocessor_flush_all(ti
);
142 ret
|= __copy_to_user(&xtregs
->cp0
, &ti
->xtregs_cp
,
143 sizeof(xtregs_coprocessor_t
));
145 ret
|= __copy_to_user(&xtregs
->opt
, ®s
->xtregs_opt
,
146 sizeof(xtregs
->opt
));
147 ret
|= __copy_to_user(&xtregs
->user
,&ti
->xtregs_user
,
148 sizeof(xtregs
->user
));
150 return ret
? -EFAULT
: 0;
153 int ptrace_setxregs(struct task_struct
*child
, void __user
*uregs
)
155 struct thread_info
*ti
= task_thread_info(child
);
156 struct pt_regs
*regs
= task_pt_regs(child
);
157 elf_xtregs_t
*xtregs
= uregs
;
160 if (!access_ok(VERIFY_READ
, uregs
, sizeof(elf_xtregs_t
)))
163 #if XTENSA_HAVE_COPROCESSORS
164 /* Flush all coprocessors before we overwrite them. */
165 coprocessor_flush_all(ti
);
166 coprocessor_release_all(ti
);
168 ret
|= __copy_from_user(&ti
->xtregs_cp
, &xtregs
->cp0
,
169 sizeof(xtregs_coprocessor_t
));
171 ret
|= __copy_from_user(®s
->xtregs_opt
, &xtregs
->opt
,
172 sizeof(xtregs
->opt
));
173 ret
|= __copy_from_user(&ti
->xtregs_user
, &xtregs
->user
,
174 sizeof(xtregs
->user
));
176 return ret
? -EFAULT
: 0;
179 int ptrace_peekusr(struct task_struct
*child
, long regno
, long __user
*ret
)
181 struct pt_regs
*regs
;
184 regs
= task_pt_regs(child
);
185 tmp
= 0; /* Default return value. */
189 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
190 tmp
= regs
->areg
[regno
- REG_AR_BASE
];
193 case REG_A_BASE
... REG_A_BASE
+ 15:
194 tmp
= regs
->areg
[regno
- REG_A_BASE
];
202 /* Note: PS.EXCM is not set while user task is running;
203 * its being set in regs is for exception handling
205 tmp
= (regs
->ps
& ~(1 << PS_EXCM_BIT
));
213 unsigned long wb
= regs
->windowbase
;
214 unsigned long ws
= regs
->windowstart
;
215 tmp
= ((ws
>>wb
) | (ws
<<(WSBITS
-wb
))) & ((1<<WSBITS
)-1);
241 return put_user(tmp
, ret
);
244 int ptrace_pokeusr(struct task_struct
*child
, long regno
, long val
)
246 struct pt_regs
*regs
;
247 regs
= task_pt_regs(child
);
250 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
251 regs
->areg
[regno
- REG_AR_BASE
] = val
;
254 case REG_A_BASE
... REG_A_BASE
+ 15:
255 regs
->areg
[regno
- REG_A_BASE
] = val
;
272 #ifdef CONFIG_HAVE_HW_BREAKPOINT
273 static void ptrace_hbptriggered(struct perf_event
*bp
,
274 struct perf_sample_data
*data
,
275 struct pt_regs
*regs
)
279 struct arch_hw_breakpoint
*bkpt
= counter_arch_bp(bp
);
281 if (bp
->attr
.bp_type
& HW_BREAKPOINT_X
) {
282 for (i
= 0; i
< XCHAL_NUM_IBREAK
; ++i
)
283 if (current
->thread
.ptrace_bp
[i
] == bp
)
287 for (i
= 0; i
< XCHAL_NUM_DBREAK
; ++i
)
288 if (current
->thread
.ptrace_wp
[i
] == bp
)
293 info
.si_signo
= SIGTRAP
;
295 info
.si_code
= TRAP_HWBKPT
;
296 info
.si_addr
= (void __user
*)bkpt
->address
;
298 force_sig_info(SIGTRAP
, &info
, current
);
301 static struct perf_event
*ptrace_hbp_create(struct task_struct
*tsk
, int type
)
303 struct perf_event_attr attr
;
305 ptrace_breakpoint_init(&attr
);
307 /* Initialise fields to sane defaults. */
313 return register_user_hw_breakpoint(&attr
, ptrace_hbptriggered
, NULL
,
318 * Address bit 0 choose instruction (0) or data (1) break register, bits
319 * 31..1 are the register number.
320 * Both PTRACE_GETHBPREGS and PTRACE_SETHBPREGS transfer two 32-bit words:
321 * address (0) and control (1).
322 * Instruction breakpoint contorl word is 0 to clear breakpoint, 1 to set.
323 * Data breakpoint control word bit 31 is 'trigger on store', bit 30 is
324 * 'trigger on load, bits 29..0 are length. Length 0 is used to clear a
325 * breakpoint. To set a breakpoint length must be a power of 2 in the range
326 * 1..64 and the address must be length-aligned.
329 static long ptrace_gethbpregs(struct task_struct
*child
, long addr
,
332 struct perf_event
*bp
;
333 u32 user_data
[2] = {0};
334 bool dbreak
= addr
& 1;
335 unsigned idx
= addr
>> 1;
337 if ((!dbreak
&& idx
>= XCHAL_NUM_IBREAK
) ||
338 (dbreak
&& idx
>= XCHAL_NUM_DBREAK
))
342 bp
= child
->thread
.ptrace_wp
[idx
];
344 bp
= child
->thread
.ptrace_bp
[idx
];
347 user_data
[0] = bp
->attr
.bp_addr
;
348 user_data
[1] = bp
->attr
.disabled
? 0 : bp
->attr
.bp_len
;
350 if (bp
->attr
.bp_type
& HW_BREAKPOINT_R
)
351 user_data
[1] |= DBREAKC_LOAD_MASK
;
352 if (bp
->attr
.bp_type
& HW_BREAKPOINT_W
)
353 user_data
[1] |= DBREAKC_STOR_MASK
;
357 if (copy_to_user(datap
, user_data
, sizeof(user_data
)))
363 static long ptrace_sethbpregs(struct task_struct
*child
, long addr
,
366 struct perf_event
*bp
;
367 struct perf_event_attr attr
;
369 bool dbreak
= addr
& 1;
370 unsigned idx
= addr
>> 1;
373 if ((!dbreak
&& idx
>= XCHAL_NUM_IBREAK
) ||
374 (dbreak
&& idx
>= XCHAL_NUM_DBREAK
))
377 if (copy_from_user(user_data
, datap
, sizeof(user_data
)))
381 bp
= child
->thread
.ptrace_wp
[idx
];
382 if (user_data
[1] & DBREAKC_LOAD_MASK
)
383 bp_type
|= HW_BREAKPOINT_R
;
384 if (user_data
[1] & DBREAKC_STOR_MASK
)
385 bp_type
|= HW_BREAKPOINT_W
;
387 bp
= child
->thread
.ptrace_bp
[idx
];
388 bp_type
= HW_BREAKPOINT_X
;
392 bp
= ptrace_hbp_create(child
,
393 bp_type
? bp_type
: HW_BREAKPOINT_RW
);
397 child
->thread
.ptrace_wp
[idx
] = bp
;
399 child
->thread
.ptrace_bp
[idx
] = bp
;
403 attr
.bp_addr
= user_data
[0];
404 attr
.bp_len
= user_data
[1] & ~(DBREAKC_LOAD_MASK
| DBREAKC_STOR_MASK
);
405 attr
.bp_type
= bp_type
;
406 attr
.disabled
= !attr
.bp_len
;
408 return modify_user_hw_breakpoint(bp
, &attr
);
412 long arch_ptrace(struct task_struct
*child
, long request
,
413 unsigned long addr
, unsigned long data
)
416 void __user
*datap
= (void __user
*) data
;
419 case PTRACE_PEEKTEXT
: /* read word at location addr. */
420 case PTRACE_PEEKDATA
:
421 ret
= generic_ptrace_peekdata(child
, addr
, data
);
424 case PTRACE_PEEKUSR
: /* read register specified by addr. */
425 ret
= ptrace_peekusr(child
, addr
, datap
);
428 case PTRACE_POKETEXT
: /* write the word at location addr. */
429 case PTRACE_POKEDATA
:
430 ret
= generic_ptrace_pokedata(child
, addr
, data
);
433 case PTRACE_POKEUSR
: /* write register specified by addr. */
434 ret
= ptrace_pokeusr(child
, addr
, data
);
438 ret
= ptrace_getregs(child
, datap
);
442 ret
= ptrace_setregs(child
, datap
);
445 case PTRACE_GETXTREGS
:
446 ret
= ptrace_getxregs(child
, datap
);
449 case PTRACE_SETXTREGS
:
450 ret
= ptrace_setxregs(child
, datap
);
452 #ifdef CONFIG_HAVE_HW_BREAKPOINT
453 case PTRACE_GETHBPREGS
:
454 ret
= ptrace_gethbpregs(child
, addr
, datap
);
457 case PTRACE_SETHBPREGS
:
458 ret
= ptrace_sethbpregs(child
, addr
, datap
);
462 ret
= ptrace_request(child
, request
, addr
, data
);
469 void do_syscall_trace(void)
472 * The 0x80 provides a way for the tracing parent to distinguish
473 * between a syscall stop and SIGTRAP delivery
475 ptrace_notify(SIGTRAP
|((current
->ptrace
& PT_TRACESYSGOOD
) ? 0x80 : 0));
478 * this isn't the same as continuing with a signal, but it will do
479 * for normal use. strace only continues with a signal if the
480 * stopping signal is not SIGTRAP. -brl
482 if (current
->exit_code
) {
483 send_sig(current
->exit_code
, current
, 1);
484 current
->exit_code
= 0;
488 void do_syscall_trace_enter(struct pt_regs
*regs
)
490 if (test_thread_flag(TIF_SYSCALL_TRACE
)
491 && (current
->ptrace
& PT_PTRACED
))
495 audit_syscall_entry(...);
499 void do_syscall_trace_leave(struct pt_regs
*regs
)
501 if ((test_thread_flag(TIF_SYSCALL_TRACE
))
502 && (current
->ptrace
& PT_PTRACED
))