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/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/ptrace.h>
31 #include <asm/coprocessor.h>
34 * Called by kernel/ptrace.c when detaching to disable single stepping.
37 void ptrace_disable(struct task_struct
*child
)
42 int ptrace_getregs(struct task_struct
*child
, void __user
*uregs
)
44 struct pt_regs
*regs
= task_pt_regs(child
);
45 xtensa_gregset_t __user
*gregset
= uregs
;
46 unsigned long wm
= regs
->wmask
;
47 unsigned long wb
= regs
->windowbase
;
50 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(xtensa_gregset_t
)))
53 __put_user(regs
->pc
, &gregset
->pc
);
54 __put_user(regs
->ps
& ~(1 << PS_EXCM_BIT
), &gregset
->ps
);
55 __put_user(regs
->lbeg
, &gregset
->lbeg
);
56 __put_user(regs
->lend
, &gregset
->lend
);
57 __put_user(regs
->lcount
, &gregset
->lcount
);
58 __put_user(regs
->windowstart
, &gregset
->windowstart
);
59 __put_user(regs
->windowbase
, &gregset
->windowbase
);
61 live
= (wm
& 2) ? 4 : (wm
& 4) ? 8 : (wm
& 8) ? 12 : 16;
63 for (i
= 0; i
< live
; i
++)
64 __put_user(regs
->areg
[i
],gregset
->a
+((wb
*4+i
)%XCHAL_NUM_AREGS
));
65 for (i
= XCHAL_NUM_AREGS
- (wm
>> 4) * 4; i
< XCHAL_NUM_AREGS
; i
++)
66 __put_user(regs
->areg
[i
],gregset
->a
+((wb
*4+i
)%XCHAL_NUM_AREGS
));
71 int ptrace_setregs(struct task_struct
*child
, void __user
*uregs
)
73 struct pt_regs
*regs
= task_pt_regs(child
);
74 xtensa_gregset_t
*gregset
= uregs
;
75 const unsigned long ps_mask
= PS_CALLINC_MASK
| PS_OWB_MASK
;
79 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(xtensa_gregset_t
)))
82 __get_user(regs
->pc
, &gregset
->pc
);
83 __get_user(ps
, &gregset
->ps
);
84 __get_user(regs
->lbeg
, &gregset
->lbeg
);
85 __get_user(regs
->lend
, &gregset
->lend
);
86 __get_user(regs
->lcount
, &gregset
->lcount
);
87 __get_user(regs
->windowstart
, &gregset
->windowstart
);
88 __get_user(wb
, &gregset
->windowbase
);
90 regs
->ps
= (regs
->ps
& ~ps_mask
) | (ps
& ps_mask
) | (1 << PS_EXCM_BIT
);
92 if (wb
>= XCHAL_NUM_AREGS
/ 4)
95 regs
->windowbase
= wb
;
97 if (wb
!= 0 && __copy_from_user(regs
->areg
+ XCHAL_NUM_AREGS
- wb
* 4,
101 if (__copy_from_user(regs
->areg
, gregset
->a
+ wb
*4, (WSBITS
-wb
) * 16))
108 int ptrace_getxregs(struct task_struct
*child
, void __user
*uregs
)
110 struct pt_regs
*regs
= task_pt_regs(child
);
111 struct thread_info
*ti
= task_thread_info(child
);
112 elf_xtregs_t __user
*xtregs
= uregs
;
115 if (!access_ok(VERIFY_WRITE
, uregs
, sizeof(elf_xtregs_t
)))
118 #if XTENSA_HAVE_COPROCESSORS
119 /* Flush all coprocessor registers to memory. */
120 coprocessor_flush_all(ti
);
121 ret
|= __copy_to_user(&xtregs
->cp0
, &ti
->xtregs_cp
,
122 sizeof(xtregs_coprocessor_t
));
124 ret
|= __copy_to_user(&xtregs
->opt
, ®s
->xtregs_opt
,
125 sizeof(xtregs
->opt
));
126 ret
|= __copy_to_user(&xtregs
->user
,&ti
->xtregs_user
,
127 sizeof(xtregs
->user
));
129 return ret
? -EFAULT
: 0;
132 int ptrace_setxregs(struct task_struct
*child
, void __user
*uregs
)
134 struct thread_info
*ti
= task_thread_info(child
);
135 struct pt_regs
*regs
= task_pt_regs(child
);
136 elf_xtregs_t
*xtregs
= uregs
;
139 if (!access_ok(VERIFY_READ
, uregs
, sizeof(elf_xtregs_t
)))
142 #if XTENSA_HAVE_COPROCESSORS
143 /* Flush all coprocessors before we overwrite them. */
144 coprocessor_flush_all(ti
);
145 coprocessor_release_all(ti
);
147 ret
|= __copy_from_user(&ti
->xtregs_cp
, &xtregs
->cp0
,
148 sizeof(xtregs_coprocessor_t
));
150 ret
|= __copy_from_user(®s
->xtregs_opt
, &xtregs
->opt
,
151 sizeof(xtregs
->opt
));
152 ret
|= __copy_from_user(&ti
->xtregs_user
, &xtregs
->user
,
153 sizeof(xtregs
->user
));
155 return ret
? -EFAULT
: 0;
158 int ptrace_peekusr(struct task_struct
*child
, long regno
, long __user
*ret
)
160 struct pt_regs
*regs
;
163 regs
= task_pt_regs(child
);
164 tmp
= 0; /* Default return value. */
168 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
169 tmp
= regs
->areg
[regno
- REG_AR_BASE
];
172 case REG_A_BASE
... REG_A_BASE
+ 15:
173 tmp
= regs
->areg
[regno
- REG_A_BASE
];
181 /* Note: PS.EXCM is not set while user task is running;
182 * its being set in regs is for exception handling
184 tmp
= (regs
->ps
& ~(1 << PS_EXCM_BIT
));
192 unsigned long wb
= regs
->windowbase
;
193 unsigned long ws
= regs
->windowstart
;
194 tmp
= ((ws
>>wb
) | (ws
<<(WSBITS
-wb
))) & ((1<<WSBITS
)-1);
220 return put_user(tmp
, ret
);
223 int ptrace_pokeusr(struct task_struct
*child
, long regno
, long val
)
225 struct pt_regs
*regs
;
226 regs
= task_pt_regs(child
);
229 case REG_AR_BASE
... REG_AR_BASE
+ XCHAL_NUM_AREGS
- 1:
230 regs
->areg
[regno
- REG_AR_BASE
] = val
;
233 case REG_A_BASE
... REG_A_BASE
+ 15:
234 regs
->areg
[regno
- REG_A_BASE
] = val
;
251 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
256 case PTRACE_PEEKTEXT
: /* read word at location addr. */
257 case PTRACE_PEEKDATA
:
258 ret
= generic_ptrace_peekdata(child
, addr
, data
);
261 case PTRACE_PEEKUSR
: /* read register specified by addr. */
262 ret
= ptrace_peekusr(child
, addr
, (void __user
*) data
);
265 case PTRACE_POKETEXT
: /* write the word at location addr. */
266 case PTRACE_POKEDATA
:
267 ret
= generic_ptrace_pokedata(child
, addr
, data
);
270 case PTRACE_POKEUSR
: /* write register specified by addr. */
271 ret
= ptrace_pokeusr(child
, addr
, data
);
274 /* continue and stop at next (return from) syscall */
277 case PTRACE_CONT
: /* restart after signal. */
280 if (!valid_signal(data
))
282 if (request
== PTRACE_SYSCALL
)
283 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
285 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
286 child
->exit_code
= data
;
287 /* Make sure the single step bit is not set. */
288 child
->ptrace
&= ~PT_SINGLESTEP
;
289 wake_up_process(child
);
295 * make the child exit. Best I can do is send it a sigkill.
296 * perhaps it should be put in the status that it wants to
301 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
303 child
->exit_code
= SIGKILL
;
304 child
->ptrace
&= ~PT_SINGLESTEP
;
305 wake_up_process(child
);
308 case PTRACE_SINGLESTEP
:
310 if (!valid_signal(data
))
312 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
313 child
->ptrace
|= PT_SINGLESTEP
;
314 child
->exit_code
= data
;
315 wake_up_process(child
);
320 ret
= ptrace_getregs(child
, (void __user
*) data
);
324 ret
= ptrace_setregs(child
, (void __user
*) data
);
327 case PTRACE_GETXTREGS
:
328 ret
= ptrace_getxregs(child
, (void __user
*) data
);
331 case PTRACE_SETXTREGS
:
332 ret
= ptrace_setxregs(child
, (void __user
*) data
);
336 ret
= ptrace_request(child
, request
, addr
, data
);
343 void do_syscall_trace(void)
346 * The 0x80 provides a way for the tracing parent to distinguish
347 * between a syscall stop and SIGTRAP delivery
349 ptrace_notify(SIGTRAP
|((current
->ptrace
& PT_TRACESYSGOOD
) ? 0x80 : 0));
352 * this isn't the same as continuing with a signal, but it will do
353 * for normal use. strace only continues with a signal if the
354 * stopping signal is not SIGTRAP. -brl
356 if (current
->exit_code
) {
357 send_sig(current
->exit_code
, current
, 1);
358 current
->exit_code
= 0;
362 void do_syscall_trace_enter(struct pt_regs
*regs
)
364 if (test_thread_flag(TIF_SYSCALL_TRACE
)
365 && (current
->ptrace
& PT_PTRACED
))
369 if (unlikely(current
->audit_context
))
370 audit_syscall_entry(current
, AUDIT_ARCH_XTENSA
..);
374 void do_syscall_trace_leave(struct pt_regs
*regs
)
376 if ((test_thread_flag(TIF_SYSCALL_TRACE
))
377 && (current
->ptrace
& PT_PTRACED
))