2 * Copyright (C) 2000-2003, Axis Communications AB.
5 #include <linux/kernel.h>
6 #include <linux/sched.h>
9 #include <linux/smp_lock.h>
10 #include <linux/errno.h>
11 #include <linux/ptrace.h>
12 #include <linux/user.h>
13 #include <linux/signal.h>
14 #include <linux/security.h>
16 #include <asm/uaccess.h>
18 #include <asm/pgtable.h>
19 #include <asm/system.h>
20 #include <asm/processor.h>
21 #include <asm/arch/hwregs/supp_reg.h>
24 * Determines which bits in CCS the user has access to.
25 * 1 = access, 0 = no access.
27 #define CCS_MASK 0x00087c00 /* SXNZVC */
29 #define SBIT_USER (1 << (S_CCS_BITNR + CCS_SHIFT))
31 static int put_debugreg(long pid
, unsigned int regno
, long data
);
32 static long get_debugreg(long pid
, unsigned int regno
);
33 static unsigned long get_pseudo_pc(struct task_struct
*child
);
34 void deconfigure_bp(long pid
);
36 extern unsigned long cris_signal_return_page
;
39 * Get contents of register REGNO in task TASK.
41 long get_reg(struct task_struct
*task
, unsigned int regno
)
43 /* USP is a special case, it's not in the pt_regs struct but
44 * in the tasks thread struct
49 ret
= ((unsigned long *)task_pt_regs(task
))[regno
];
50 else if (regno
== PT_USP
)
51 ret
= task
->thread
.usp
;
52 else if (regno
== PT_PPC
)
53 ret
= get_pseudo_pc(task
);
54 else if (regno
<= PT_MAX
)
55 ret
= get_debugreg(task
->pid
, regno
);
63 * Write contents of register REGNO in task TASK.
65 int put_reg(struct task_struct
*task
, unsigned int regno
, unsigned long data
)
68 ((unsigned long *)task_pt_regs(task
))[regno
] = data
;
69 else if (regno
== PT_USP
)
70 task
->thread
.usp
= data
;
71 else if (regno
== PT_PPC
) {
72 /* Write pseudo-PC to ERP only if changed. */
73 if (data
!= get_pseudo_pc(task
))
74 task_pt_regs(task
)->erp
= data
;
75 } else if (regno
<= PT_MAX
)
76 return put_debugreg(task
->pid
, regno
, data
);
83 * Called by kernel/ptrace.c when detaching.
85 * Make sure the single step bit is not set.
88 ptrace_disable(struct task_struct
*child
)
92 /* Deconfigure SPC and S-bit. */
93 tmp
= get_reg(child
, PT_CCS
) & ~SBIT_USER
;
94 put_reg(child
, PT_CCS
, tmp
);
95 put_reg(child
, PT_SPC
, 0);
97 /* Deconfigure any watchpoints associated with the child. */
98 deconfigure_bp(child
->pid
);
102 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
105 unsigned long __user
*datap
= (unsigned long __user
*)data
;
108 /* Read word at location address. */
109 case PTRACE_PEEKTEXT
:
110 case PTRACE_PEEKDATA
: {
116 /* The signal trampoline page is outside the normal user-addressable
117 * space but still accessible. This is hack to make it possible to
118 * access the signal handler code in GDB.
120 if ((addr
& PAGE_MASK
) == cris_signal_return_page
) {
121 /* The trampoline page is globally mapped, no page table to traverse.*/
122 tmp
= *(unsigned long*)addr
;
124 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
126 if (copied
!= sizeof(tmp
))
130 ret
= put_user(tmp
,datap
);
134 /* Read the word at location address in the USER area. */
135 case PTRACE_PEEKUSR
: {
139 if ((addr
& 3) || addr
< 0 || addr
> PT_MAX
<< 2)
142 tmp
= get_reg(child
, addr
>> 2);
143 ret
= put_user(tmp
, datap
);
147 /* Write the word at location address. */
148 case PTRACE_POKETEXT
:
149 case PTRACE_POKEDATA
:
152 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1) == sizeof(data
))
158 /* Write the word at location address in the USER area. */
161 if ((addr
& 3) || addr
< 0 || addr
> PT_MAX
<< 2)
166 if (addr
== PT_CCS
) {
167 /* don't allow the tracing process to change stuff like
168 * interrupt enable, kernel/user bit, dma enables etc.
171 data
|= get_reg(child
, PT_CCS
) & ~CCS_MASK
;
173 if (put_reg(child
, addr
, data
))
182 if (!valid_signal(data
))
185 /* Continue means no single-step. */
186 put_reg(child
, PT_SPC
, 0);
188 if (!get_debugreg(child
->pid
, PT_BP_CTRL
)) {
190 /* If no h/w bp configured, disable S bit. */
191 tmp
= get_reg(child
, PT_CCS
) & ~SBIT_USER
;
192 put_reg(child
, PT_CCS
, tmp
);
195 if (request
== PTRACE_SYSCALL
) {
196 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
199 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
202 child
->exit_code
= data
;
204 /* TODO: make sure any pending breakpoint is killed */
205 wake_up_process(child
);
210 /* Make the child exit by sending it a sigkill. */
214 if (child
->exit_state
== EXIT_ZOMBIE
)
217 child
->exit_code
= SIGKILL
;
219 /* Deconfigure single-step and h/w bp. */
220 ptrace_disable(child
);
222 /* TODO: make sure any pending breakpoint is killed */
223 wake_up_process(child
);
226 /* Set the trap flag. */
227 case PTRACE_SINGLESTEP
: {
231 /* Set up SPC if not set already (in which case we have
232 no other choice but to trust it). */
233 if (!get_reg(child
, PT_SPC
)) {
234 /* In case we're stopped in a delay slot. */
235 tmp
= get_reg(child
, PT_ERP
) & ~1;
236 put_reg(child
, PT_SPC
, tmp
);
238 tmp
= get_reg(child
, PT_CCS
) | SBIT_USER
;
239 put_reg(child
, PT_CCS
, tmp
);
241 if (!valid_signal(data
))
244 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
246 /* TODO: set some clever breakpoint mechanism... */
248 child
->exit_code
= data
;
249 wake_up_process(child
);
255 ret
= ptrace_detach(child
, data
);
258 /* Get all GP registers from the child. */
259 case PTRACE_GETREGS
: {
263 for (i
= 0; i
<= PT_MAX
; i
++) {
264 tmp
= get_reg(child
, i
);
266 if (put_user(tmp
, datap
)) {
278 /* Set all GP registers in the child. */
279 case PTRACE_SETREGS
: {
283 for (i
= 0; i
<= PT_MAX
; i
++) {
284 if (get_user(tmp
, datap
)) {
291 tmp
|= get_reg(child
, PT_CCS
) & ~CCS_MASK
;
294 put_reg(child
, i
, tmp
);
303 ret
= ptrace_request(child
, request
, addr
, data
);
310 void do_syscall_trace(void)
312 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
315 if (!(current
->ptrace
& PT_PTRACED
))
318 /* the 0x80 provides a way for the tracing parent to distinguish
319 between a syscall stop and SIGTRAP delivery */
320 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
324 * This isn't the same as continuing with a signal, but it will do for
327 if (current
->exit_code
) {
328 send_sig(current
->exit_code
, current
, 1);
329 current
->exit_code
= 0;
333 /* Returns the size of an instruction that has a delay slot. */
335 static int insn_size(struct task_struct
*child
, unsigned long pc
)
337 unsigned long opcode
;
341 /* Read the opcode at pc (do what PTRACE_PEEKTEXT would do). */
342 copied
= access_process_vm(child
, pc
, &opcode
, sizeof(opcode
), 0);
343 if (copied
!= sizeof(opcode
))
346 switch ((opcode
& 0x0f00) >> 8) {
357 /* Could be 4 or 6; check more bits. */
358 if ((opcode
& 0xff) == 0xff)
364 panic("ERROR: Couldn't find size of opcode 0x%lx at 0x%lx\n",
371 static unsigned long get_pseudo_pc(struct task_struct
*child
)
373 /* Default value for PC is ERP. */
374 unsigned long pc
= get_reg(child
, PT_ERP
);
377 unsigned long spc
= get_reg(child
, PT_SPC
);
378 /* Delay slot bit set. Report as stopped on proper
381 /* Rely on SPC if set. FIXME: We might want to check
382 that EXS indicates we stopped due to a single-step
386 /* Calculate the PC from the size of the instruction
387 that the delay slot we're in belongs to. */
388 pc
+= insn_size(child
, pc
& ~1) - 1;
394 static long bp_owner
= 0;
396 /* Reachable from exit_thread in signal.c, so not static. */
397 void deconfigure_bp(long pid
)
401 /* Only deconfigure if the pid is the owner. */
405 for (bp
= 0; bp
< 6; bp
++) {
407 /* Deconfigure start and end address (also gets rid of ownership). */
408 put_debugreg(pid
, PT_BP
+ 3 + (bp
* 2), 0);
409 put_debugreg(pid
, PT_BP
+ 4 + (bp
* 2), 0);
411 /* Deconfigure relevant bits in control register. */
412 tmp
= get_debugreg(pid
, PT_BP_CTRL
) & ~(3 << (2 + (bp
* 4)));
413 put_debugreg(pid
, PT_BP_CTRL
, tmp
);
419 static int put_debugreg(long pid
, unsigned int regno
, long data
)
422 register int old_srs
;
424 #ifdef CONFIG_ETRAX_KGDB
425 /* Ignore write, but pretend it was ok if value is 0
426 (we don't want POKEUSR/SETREGS failing unnessecarily). */
427 return (data
== 0) ? ret
: -1;
430 /* Simple owner management. */
433 else if (bp_owner
!= pid
) {
434 /* Ignore write, but pretend it was ok if value is 0
435 (we don't want POKEUSR/SETREGS failing unnessecarily). */
436 return (data
== 0) ? ret
: -1;
439 /* Remember old SRS. */
440 SPEC_REG_RD(SPEC_REG_SRS
, old_srs
);
441 /* Switch to BP bank. */
442 SUPP_BANK_SEL(BANK_BP
);
444 switch (regno
- PT_BP
) {
446 SUPP_REG_WR(0, data
); break;
453 SUPP_REG_WR(3, data
); break;
455 SUPP_REG_WR(4, data
); break;
457 SUPP_REG_WR(5, data
); break;
459 SUPP_REG_WR(6, data
); break;
461 SUPP_REG_WR(7, data
); break;
463 SUPP_REG_WR(8, data
); break;
465 SUPP_REG_WR(9, data
); break;
467 SUPP_REG_WR(10, data
); break;
469 SUPP_REG_WR(11, data
); break;
471 SUPP_REG_WR(12, data
); break;
473 SUPP_REG_WR(13, data
); break;
475 SUPP_REG_WR(14, data
); break;
482 SPEC_REG_WR(SPEC_REG_SRS
, old_srs
);
491 static long get_debugreg(long pid
, unsigned int regno
)
493 register int old_srs
;
496 if (pid
!= bp_owner
) {
500 /* Remember old SRS. */
501 SPEC_REG_RD(SPEC_REG_SRS
, old_srs
);
502 /* Switch to BP bank. */
503 SUPP_BANK_SEL(BANK_BP
);
505 switch (regno
- PT_BP
) {
507 SUPP_REG_RD(0, data
); break;
510 /* error return value? */
514 SUPP_REG_RD(3, data
); break;
516 SUPP_REG_RD(4, data
); break;
518 SUPP_REG_RD(5, data
); break;
520 SUPP_REG_RD(6, data
); break;
522 SUPP_REG_RD(7, data
); break;
524 SUPP_REG_RD(8, data
); break;
526 SUPP_REG_RD(9, data
); break;
528 SUPP_REG_RD(10, data
); break;
530 SUPP_REG_RD(11, data
); break;
532 SUPP_REG_RD(12, data
); break;
534 SUPP_REG_RD(13, data
); break;
536 SUPP_REG_RD(14, data
); break;
538 /* error return value? */
543 SPEC_REG_WR(SPEC_REG_SRS
, old_srs
);