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/kernel.h>
17 #include <linux/sched.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/smp.h>
22 #include <linux/security.h>
23 #include <linux/signal.h>
25 #include <asm/pgtable.h>
27 #include <asm/uaccess.h>
28 #include <asm/ptrace.h>
30 #include <asm/coprocessor.h>
33 void user_enable_single_step(struct task_struct
*child
)
35 child
->ptrace
|= PT_SINGLESTEP
;
38 void user_disable_single_step(struct task_struct
*child
)
40 child
->ptrace
&= ~PT_SINGLESTEP
;
44 * Called by kernel/ptrace.c when detaching to disable single stepping.
47 void ptrace_disable(struct task_struct
*child
)
52 int ptrace_getregs(struct task_struct
*child
, void __user
*uregs
)
54 struct pt_regs
*regs
= task_pt_regs(child
);
55 xtensa_gregset_t __user
*gregset
= uregs
;
56 unsigned long wb
= regs
->windowbase
;
59 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(xtensa_gregset_t
)))
62 __put_user(regs
->pc
, &gregset
->pc
);
63 __put_user(regs
->ps
& ~(1 << PS_EXCM_BIT
), &gregset
->ps
);
64 __put_user(regs
->lbeg
, &gregset
->lbeg
);
65 __put_user(regs
->lend
, &gregset
->lend
);
66 __put_user(regs
->lcount
, &gregset
->lcount
);
67 __put_user(regs
->windowstart
, &gregset
->windowstart
);
68 __put_user(regs
->windowbase
, &gregset
->windowbase
);
69 __put_user(regs
->threadptr
, &gregset
->threadptr
);
71 for (i
= 0; i
< XCHAL_NUM_AREGS
; i
++)
72 __put_user(regs
->areg
[i
],
73 gregset
->a
+ ((wb
* 4 + i
) % XCHAL_NUM_AREGS
));
78 int ptrace_setregs(struct task_struct
*child
, void __user
*uregs
)
80 struct pt_regs
*regs
= task_pt_regs(child
);
81 xtensa_gregset_t
*gregset
= uregs
;
82 const unsigned long ps_mask
= PS_CALLINC_MASK
| PS_OWB_MASK
;
86 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(xtensa_gregset_t
)))
89 __get_user(regs
->pc
, &gregset
->pc
);
90 __get_user(ps
, &gregset
->ps
);
91 __get_user(regs
->lbeg
, &gregset
->lbeg
);
92 __get_user(regs
->lend
, &gregset
->lend
);
93 __get_user(regs
->lcount
, &gregset
->lcount
);
94 __get_user(ws
, &gregset
->windowstart
);
95 __get_user(wb
, &gregset
->windowbase
);
96 __get_user(regs
->threadptr
, &gregset
->threadptr
);
98 regs
->ps
= (regs
->ps
& ~ps_mask
) | (ps
& ps_mask
) | (1 << PS_EXCM_BIT
);
100 if (wb
>= XCHAL_NUM_AREGS
/ 4)
103 if (wb
!= regs
->windowbase
|| ws
!= regs
->windowstart
) {
104 unsigned long rotws
, wmask
;
106 rotws
= (((ws
| (ws
<< WSBITS
)) >> wb
) &
107 ((1 << WSBITS
) - 1)) & ~1;
108 wmask
= ((rotws
? WSBITS
+ 1 - ffs(rotws
) : 0) << 4) |
110 regs
->windowbase
= wb
;
111 regs
->windowstart
= ws
;
115 if (wb
!= 0 && __copy_from_user(regs
->areg
+ XCHAL_NUM_AREGS
- wb
* 4,
116 gregset
->a
, wb
* 16))
119 if (__copy_from_user(regs
->areg
, gregset
->a
+ wb
* 4,
127 int ptrace_getxregs(struct task_struct
*child
, void __user
*uregs
)
129 struct pt_regs
*regs
= task_pt_regs(child
);
130 struct thread_info
*ti
= task_thread_info(child
);
131 elf_xtregs_t __user
*xtregs
= uregs
;
134 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(elf_xtregs_t
)))
137 #if XTENSA_HAVE_COPROCESSORS
138 /* Flush all coprocessor registers to memory. */
139 coprocessor_flush_all(ti
);
140 ret
|= __copy_to_user(&xtregs
->cp0
, &ti
->xtregs_cp
,
141 sizeof(xtregs_coprocessor_t
));
143 ret
|= __copy_to_user(&xtregs
->opt
, ®s
->xtregs_opt
,
144 sizeof(xtregs
->opt
));
145 ret
|= __copy_to_user(&xtregs
->user
,&ti
->xtregs_user
,
146 sizeof(xtregs
->user
));
148 return ret
? -EFAULT
: 0;
151 int ptrace_setxregs(struct task_struct
*child
, void __user
*uregs
)
153 struct thread_info
*ti
= task_thread_info(child
);
154 struct pt_regs
*regs
= task_pt_regs(child
);
155 elf_xtregs_t
*xtregs
= uregs
;
158 if (!access_ok(VERIFY_READ
, uregs
, sizeof(elf_xtregs_t
)))
161 #if XTENSA_HAVE_COPROCESSORS
162 /* Flush all coprocessors before we overwrite them. */
163 coprocessor_flush_all(ti
);
164 coprocessor_release_all(ti
);
166 ret
|= __copy_from_user(&ti
->xtregs_cp
, &xtregs
->cp0
,
167 sizeof(xtregs_coprocessor_t
));
169 ret
|= __copy_from_user(®s
->xtregs_opt
, &xtregs
->opt
,
170 sizeof(xtregs
->opt
));
171 ret
|= __copy_from_user(&ti
->xtregs_user
, &xtregs
->user
,
172 sizeof(xtregs
->user
));
174 return ret
? -EFAULT
: 0;
177 int ptrace_peekusr(struct task_struct
*child
, long regno
, long __user
*ret
)
179 struct pt_regs
*regs
;
182 regs
= task_pt_regs(child
);
183 tmp
= 0; /* Default return value. */
187 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
188 tmp
= regs
->areg
[regno
- REG_AR_BASE
];
191 case REG_A_BASE
... REG_A_BASE
+ 15:
192 tmp
= regs
->areg
[regno
- REG_A_BASE
];
200 /* Note: PS.EXCM is not set while user task is running;
201 * its being set in regs is for exception handling
203 tmp
= (regs
->ps
& ~(1 << PS_EXCM_BIT
));
211 unsigned long wb
= regs
->windowbase
;
212 unsigned long ws
= regs
->windowstart
;
213 tmp
= ((ws
>>wb
) | (ws
<<(WSBITS
-wb
))) & ((1<<WSBITS
)-1);
239 return put_user(tmp
, ret
);
242 int ptrace_pokeusr(struct task_struct
*child
, long regno
, long val
)
244 struct pt_regs
*regs
;
245 regs
= task_pt_regs(child
);
248 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
249 regs
->areg
[regno
- REG_AR_BASE
] = val
;
252 case REG_A_BASE
... REG_A_BASE
+ 15:
253 regs
->areg
[regno
- REG_A_BASE
] = val
;
270 long arch_ptrace(struct task_struct
*child
, long request
,
271 unsigned long addr
, unsigned long data
)
274 void __user
*datap
= (void __user
*) data
;
277 case PTRACE_PEEKTEXT
: /* read word at location addr. */
278 case PTRACE_PEEKDATA
:
279 ret
= generic_ptrace_peekdata(child
, addr
, data
);
282 case PTRACE_PEEKUSR
: /* read register specified by addr. */
283 ret
= ptrace_peekusr(child
, addr
, datap
);
286 case PTRACE_POKETEXT
: /* write the word at location addr. */
287 case PTRACE_POKEDATA
:
288 ret
= generic_ptrace_pokedata(child
, addr
, data
);
291 case PTRACE_POKEUSR
: /* write register specified by addr. */
292 ret
= ptrace_pokeusr(child
, addr
, data
);
296 ret
= ptrace_getregs(child
, datap
);
300 ret
= ptrace_setregs(child
, datap
);
303 case PTRACE_GETXTREGS
:
304 ret
= ptrace_getxregs(child
, datap
);
307 case PTRACE_SETXTREGS
:
308 ret
= ptrace_setxregs(child
, datap
);
312 ret
= ptrace_request(child
, request
, addr
, data
);
319 void do_syscall_trace(void)
322 * The 0x80 provides a way for the tracing parent to distinguish
323 * between a syscall stop and SIGTRAP delivery
325 ptrace_notify(SIGTRAP
|((current
->ptrace
& PT_TRACESYSGOOD
) ? 0x80 : 0));
328 * this isn't the same as continuing with a signal, but it will do
329 * for normal use. strace only continues with a signal if the
330 * stopping signal is not SIGTRAP. -brl
332 if (current
->exit_code
) {
333 send_sig(current
->exit_code
, current
, 1);
334 current
->exit_code
= 0;
338 void do_syscall_trace_enter(struct pt_regs
*regs
)
340 if (test_thread_flag(TIF_SYSCALL_TRACE
)
341 && (current
->ptrace
& PT_PTRACED
))
345 audit_syscall_entry(...);
349 void do_syscall_trace_leave(struct pt_regs
*regs
)
351 if ((test_thread_flag(TIF_SYSCALL_TRACE
))
352 && (current
->ptrace
& PT_PTRACED
))