2 * Copyright (C) 2000-2007, Axis Communications AB.
5 #include <linux/kernel.h>
6 #include <linux/sched.h>
9 #include <linux/errno.h>
10 #include <linux/ptrace.h>
11 #include <linux/user.h>
12 #include <linux/signal.h>
13 #include <linux/security.h>
15 #include <asm/uaccess.h>
17 #include <asm/pgtable.h>
18 #include <asm/system.h>
19 #include <asm/processor.h>
20 #include <arch/hwregs/supp_reg.h>
23 * Determines which bits in CCS the user has access to.
24 * 1 = access, 0 = no access.
26 #define CCS_MASK 0x00087c00 /* SXNZVC */
28 #define SBIT_USER (1 << (S_CCS_BITNR + CCS_SHIFT))
30 static int put_debugreg(long pid
, unsigned int regno
, long data
);
31 static long get_debugreg(long pid
, unsigned int regno
);
32 static unsigned long get_pseudo_pc(struct task_struct
*child
);
33 void deconfigure_bp(long pid
);
35 extern unsigned long cris_signal_return_page
;
38 * Get contents of register REGNO in task TASK.
40 long get_reg(struct task_struct
*task
, unsigned int regno
)
42 /* USP is a special case, it's not in the pt_regs struct but
43 * in the tasks thread struct
48 ret
= ((unsigned long *)task_pt_regs(task
))[regno
];
49 else if (regno
== PT_USP
)
50 ret
= task
->thread
.usp
;
51 else if (regno
== PT_PPC
)
52 ret
= get_pseudo_pc(task
);
53 else if (regno
<= PT_MAX
)
54 ret
= get_debugreg(task
->pid
, regno
);
62 * Write contents of register REGNO in task TASK.
64 int put_reg(struct task_struct
*task
, unsigned int regno
, unsigned long data
)
67 ((unsigned long *)task_pt_regs(task
))[regno
] = data
;
68 else if (regno
== PT_USP
)
69 task
->thread
.usp
= data
;
70 else if (regno
== PT_PPC
) {
71 /* Write pseudo-PC to ERP only if changed. */
72 if (data
!= get_pseudo_pc(task
))
73 task_pt_regs(task
)->erp
= data
;
74 } else if (regno
<= PT_MAX
)
75 return put_debugreg(task
->pid
, regno
, data
);
81 void user_enable_single_step(struct task_struct
*child
)
86 * Set up SPC if not set already (in which case we have no other
87 * choice but to trust it).
89 if (!get_reg(child
, PT_SPC
)) {
90 /* In case we're stopped in a delay slot. */
91 tmp
= get_reg(child
, PT_ERP
) & ~1;
92 put_reg(child
, PT_SPC
, tmp
);
94 tmp
= get_reg(child
, PT_CCS
) | SBIT_USER
;
95 put_reg(child
, PT_CCS
, tmp
);
98 void user_disable_single_step(struct task_struct
*child
)
100 put_reg(child
, PT_SPC
, 0);
102 if (!get_debugreg(child
->pid
, PT_BP_CTRL
)) {
104 /* If no h/w bp configured, disable S bit. */
105 tmp
= get_reg(child
, PT_CCS
) & ~SBIT_USER
;
106 put_reg(child
, PT_CCS
, tmp
);
111 * Called by kernel/ptrace.c when detaching.
113 * Make sure the single step bit is not set.
116 ptrace_disable(struct task_struct
*child
)
120 /* Deconfigure SPC and S-bit. */
121 user_disable_single_step(child
);
122 put_reg(child
, PT_SPC
, 0);
124 /* Deconfigure any watchpoints associated with the child. */
125 deconfigure_bp(child
->pid
);
129 long arch_ptrace(struct task_struct
*child
, long request
,
130 unsigned long addr
, unsigned long data
)
133 unsigned int regno
= addr
>> 2;
134 unsigned long __user
*datap
= (unsigned long __user
*)data
;
137 /* Read word at location address. */
138 case PTRACE_PEEKTEXT
:
139 case PTRACE_PEEKDATA
: {
145 /* The signal trampoline page is outside the normal user-addressable
146 * space but still accessible. This is hack to make it possible to
147 * access the signal handler code in GDB.
149 if ((addr
& PAGE_MASK
) == cris_signal_return_page
) {
150 /* The trampoline page is globally mapped, no page table to traverse.*/
151 tmp
= *(unsigned long*)addr
;
153 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
155 if (copied
!= sizeof(tmp
))
159 ret
= put_user(tmp
,datap
);
163 /* Read the word at location address in the USER area. */
164 case PTRACE_PEEKUSR
: {
168 if ((addr
& 3) || regno
> PT_MAX
)
171 tmp
= get_reg(child
, regno
);
172 ret
= put_user(tmp
, datap
);
176 /* Write the word at location address. */
177 case PTRACE_POKETEXT
:
178 case PTRACE_POKEDATA
:
179 ret
= generic_ptrace_pokedata(child
, addr
, data
);
182 /* Write the word at location address in the USER area. */
185 if ((addr
& 3) || regno
> PT_MAX
)
188 if (regno
== PT_CCS
) {
189 /* don't allow the tracing process to change stuff like
190 * interrupt enable, kernel/user bit, dma enables etc.
193 data
|= get_reg(child
, PT_CCS
) & ~CCS_MASK
;
195 if (put_reg(child
, regno
, data
))
200 /* Get all GP registers from the child. */
201 case PTRACE_GETREGS
: {
205 for (i
= 0; i
<= PT_MAX
; i
++) {
206 tmp
= get_reg(child
, i
);
208 if (put_user(tmp
, datap
)) {
220 /* Set all GP registers in the child. */
221 case PTRACE_SETREGS
: {
225 for (i
= 0; i
<= PT_MAX
; i
++) {
226 if (get_user(tmp
, datap
)) {
233 tmp
|= get_reg(child
, PT_CCS
) & ~CCS_MASK
;
236 put_reg(child
, i
, tmp
);
245 ret
= ptrace_request(child
, request
, addr
, data
);
253 void do_syscall_trace(void)
255 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
258 if (!(current
->ptrace
& PT_PTRACED
))
261 /* the 0x80 provides a way for the tracing parent to distinguish
262 between a syscall stop and SIGTRAP delivery */
263 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
267 * This isn't the same as continuing with a signal, but it will do for
270 if (current
->exit_code
) {
271 send_sig(current
->exit_code
, current
, 1);
272 current
->exit_code
= 0;
276 /* Returns the size of an instruction that has a delay slot. */
278 static int insn_size(struct task_struct
*child
, unsigned long pc
)
280 unsigned long opcode
;
284 /* Read the opcode at pc (do what PTRACE_PEEKTEXT would do). */
285 copied
= access_process_vm(child
, pc
, &opcode
, sizeof(opcode
), 0);
286 if (copied
!= sizeof(opcode
))
289 switch ((opcode
& 0x0f00) >> 8) {
300 /* Could be 4 or 6; check more bits. */
301 if ((opcode
& 0xff) == 0xff)
307 panic("ERROR: Couldn't find size of opcode 0x%lx at 0x%lx\n",
314 static unsigned long get_pseudo_pc(struct task_struct
*child
)
316 /* Default value for PC is ERP. */
317 unsigned long pc
= get_reg(child
, PT_ERP
);
320 unsigned long spc
= get_reg(child
, PT_SPC
);
321 /* Delay slot bit set. Report as stopped on proper
324 /* Rely on SPC if set. FIXME: We might want to check
325 that EXS indicates we stopped due to a single-step
329 /* Calculate the PC from the size of the instruction
330 that the delay slot we're in belongs to. */
331 pc
+= insn_size(child
, pc
& ~1) - 1;
337 static long bp_owner
= 0;
339 /* Reachable from exit_thread in signal.c, so not static. */
340 void deconfigure_bp(long pid
)
344 /* Only deconfigure if the pid is the owner. */
348 for (bp
= 0; bp
< 6; bp
++) {
350 /* Deconfigure start and end address (also gets rid of ownership). */
351 put_debugreg(pid
, PT_BP
+ 3 + (bp
* 2), 0);
352 put_debugreg(pid
, PT_BP
+ 4 + (bp
* 2), 0);
354 /* Deconfigure relevant bits in control register. */
355 tmp
= get_debugreg(pid
, PT_BP_CTRL
) & ~(3 << (2 + (bp
* 4)));
356 put_debugreg(pid
, PT_BP_CTRL
, tmp
);
362 static int put_debugreg(long pid
, unsigned int regno
, long data
)
365 register int old_srs
;
367 #ifdef CONFIG_ETRAX_KGDB
368 /* Ignore write, but pretend it was ok if value is 0
369 (we don't want POKEUSR/SETREGS failing unnessecarily). */
370 return (data
== 0) ? ret
: -1;
373 /* Simple owner management. */
376 else if (bp_owner
!= pid
) {
377 /* Ignore write, but pretend it was ok if value is 0
378 (we don't want POKEUSR/SETREGS failing unnessecarily). */
379 return (data
== 0) ? ret
: -1;
382 /* Remember old SRS. */
383 SPEC_REG_RD(SPEC_REG_SRS
, old_srs
);
384 /* Switch to BP bank. */
385 SUPP_BANK_SEL(BANK_BP
);
387 switch (regno
- PT_BP
) {
389 SUPP_REG_WR(0, data
); break;
396 SUPP_REG_WR(3, data
); break;
398 SUPP_REG_WR(4, data
); break;
400 SUPP_REG_WR(5, data
); break;
402 SUPP_REG_WR(6, data
); break;
404 SUPP_REG_WR(7, data
); break;
406 SUPP_REG_WR(8, data
); break;
408 SUPP_REG_WR(9, data
); break;
410 SUPP_REG_WR(10, data
); break;
412 SUPP_REG_WR(11, data
); break;
414 SUPP_REG_WR(12, data
); break;
416 SUPP_REG_WR(13, data
); break;
418 SUPP_REG_WR(14, data
); break;
425 SPEC_REG_WR(SPEC_REG_SRS
, old_srs
);
434 static long get_debugreg(long pid
, unsigned int regno
)
436 register int old_srs
;
439 if (pid
!= bp_owner
) {
443 /* Remember old SRS. */
444 SPEC_REG_RD(SPEC_REG_SRS
, old_srs
);
445 /* Switch to BP bank. */
446 SUPP_BANK_SEL(BANK_BP
);
448 switch (regno
- PT_BP
) {
450 SUPP_REG_RD(0, data
); break;
453 /* error return value? */
457 SUPP_REG_RD(3, data
); break;
459 SUPP_REG_RD(4, data
); break;
461 SUPP_REG_RD(5, data
); break;
463 SUPP_REG_RD(6, data
); break;
465 SUPP_REG_RD(7, data
); break;
467 SUPP_REG_RD(8, data
); break;
469 SUPP_REG_RD(9, data
); break;
471 SUPP_REG_RD(10, data
); break;
473 SUPP_REG_RD(11, data
); break;
475 SUPP_REG_RD(12, data
); break;
477 SUPP_REG_RD(13, data
); break;
479 SUPP_REG_RD(14, data
); break;
481 /* error return value? */
486 SPEC_REG_WR(SPEC_REG_SRS
, old_srs
);