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 *)user_regs(task->thread_info))[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 *)user_regs(task->thread_info))[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 ((unsigned long *)user_regs(task->thread_info))[PT_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);
103 sys_ptrace(long request, long pid, long addr, long data)
105 struct task_struct *child;
107 unsigned long __user *datap = (unsigned long __user *)data;
112 if (request == PTRACE_TRACEME) {
113 /* are we already being traced? */
114 if (current->ptrace & PT_PTRACED)
116 ret = security_ptrace(current->parent, current);
119 /* set the ptrace bit in the process flags. */
120 current->ptrace |= PT_PTRACED;
126 read_lock(&tasklist_lock);
127 child = find_task_by_pid(pid);
130 get_task_struct(child);
132 read_unlock(&tasklist_lock);
139 if (pid == 1) /* Leave the init process alone! */
142 if (request == PTRACE_ATTACH) {
143 ret = ptrace_attach(child);
147 ret = ptrace_check_attach(child, request == PTRACE_KILL);
152 /* Read word at location address. */
153 case PTRACE_PEEKTEXT:
154 case PTRACE_PEEKDATA: {
160 /* The signal trampoline page is outside the normal user-addressable
161 * space but still accessible. This is hack to make it possible to
162 * access the signal handler code in GDB.
164 if ((addr & PAGE_MASK) == cris_signal_return_page) {
165 /* The trampoline page is globally mapped, no page table to traverse.*/
166 tmp = *(unsigned long*)addr;
168 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
170 if (copied != sizeof(tmp))
174 ret = put_user(tmp,datap);
178 /* Read the word at location address in the USER area. */
179 case PTRACE_PEEKUSR: {
183 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
186 tmp = get_reg(child, addr >> 2);
187 ret = put_user(tmp, datap);
191 /* Write the word at location address. */
192 case PTRACE_POKETEXT:
193 case PTRACE_POKEDATA:
196 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
202 /* Write the word at location address in the USER area. */
205 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
210 if (addr == PT_CCS) {
211 /* don't allow the tracing process to change stuff like
212 * interrupt enable, kernel/user bit, dma enables etc.
215 data |= get_reg(child, PT_CCS) & ~CCS_MASK;
217 if (put_reg(child, addr, data))
226 if (!valid_signal(data))
229 /* Continue means no single-step. */
230 put_reg(child, PT_SPC, 0);
232 if (!get_debugreg(child->pid, PT_BP_CTRL)) {
234 /* If no h/w bp configured, disable S bit. */
235 tmp = get_reg(child, PT_CCS) & ~SBIT_USER;
236 put_reg(child, PT_CCS, tmp);
239 if (request == PTRACE_SYSCALL) {
240 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
243 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
246 child->exit_code = data;
248 /* TODO: make sure any pending breakpoint is killed */
249 wake_up_process(child);
254 /* Make the child exit by sending it a sigkill. */
258 if (child->exit_state == EXIT_ZOMBIE)
261 child->exit_code = SIGKILL;
263 /* Deconfigure single-step and h/w bp. */
264 ptrace_disable(child);
266 /* TODO: make sure any pending breakpoint is killed */
267 wake_up_process(child);
270 /* Set the trap flag. */
271 case PTRACE_SINGLESTEP: {
275 /* Set up SPC if not set already (in which case we have
276 no other choice but to trust it). */
277 if (!get_reg(child, PT_SPC)) {
278 /* In case we're stopped in a delay slot. */
279 tmp = get_reg(child, PT_ERP) & ~1;
280 put_reg(child, PT_SPC, tmp);
282 tmp = get_reg(child, PT_CCS) | SBIT_USER;
283 put_reg(child, PT_CCS, tmp);
285 if (!valid_signal(data))
288 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
290 /* TODO: set some clever breakpoint mechanism... */
292 child->exit_code = data;
293 wake_up_process(child);
299 ret = ptrace_detach(child, data);
302 /* Get all GP registers from the child. */
303 case PTRACE_GETREGS: {
307 for (i = 0; i <= PT_MAX; i++) {
308 tmp = get_reg(child, i);
310 if (put_user(tmp, datap)) {
322 /* Set all GP registers in the child. */
323 case PTRACE_SETREGS: {
327 for (i = 0; i <= PT_MAX; i++) {
328 if (get_user(tmp, datap)) {
335 tmp |= get_reg(child, PT_CCS) & ~CCS_MASK;
338 put_reg(child, i, tmp);
347 ret = ptrace_request(child, request, addr, data);
351 put_task_struct(child);
357 void do_syscall_trace(void)
359 if (!test_thread_flag(TIF_SYSCALL_TRACE))
362 if (!(current->ptrace & PT_PTRACED))
365 /* the 0x80 provides a way for the tracing parent to distinguish
366 between a syscall stop and SIGTRAP delivery */
367 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
371 * This isn't the same as continuing with a signal, but it will do for
374 if (current->exit_code) {
375 send_sig(current->exit_code, current, 1);
376 current->exit_code = 0;
380 /* Returns the size of an instruction that has a delay slot. */
382 static int insn_size(struct task_struct *child, unsigned long pc)
384 unsigned long opcode;
388 /* Read the opcode at pc (do what PTRACE_PEEKTEXT would do). */
389 copied = access_process_vm(child, pc, &opcode, sizeof(opcode), 0);
390 if (copied != sizeof(opcode))
393 switch ((opcode & 0x0f00) >> 8) {
404 /* Could be 4 or 6; check more bits. */
405 if ((opcode & 0xff) == 0xff)
411 panic("ERROR: Couldn't find size of opcode 0x%lx at 0x%lx\n",
418 static unsigned long get_pseudo_pc(struct task_struct *child)
420 /* Default value for PC is ERP. */
421 unsigned long pc = get_reg(child, PT_ERP);
424 unsigned long spc = get_reg(child, PT_SPC);
425 /* Delay slot bit set. Report as stopped on proper
428 /* Rely on SPC if set. FIXME: We might want to check
429 that EXS indicates we stopped due to a single-step
433 /* Calculate the PC from the size of the instruction
434 that the delay slot we're in belongs to. */
435 pc += insn_size(child, pc & ~1) - 1;
441 static long bp_owner = 0;
443 /* Reachable from exit_thread in signal.c, so not static. */
444 void deconfigure_bp(long pid)
448 /* Only deconfigure if the pid is the owner. */
452 for (bp = 0; bp < 6; bp++) {
454 /* Deconfigure start and end address (also gets rid of ownership). */
455 put_debugreg(pid, PT_BP + 3 + (bp * 2), 0);
456 put_debugreg(pid, PT_BP + 4 + (bp * 2), 0);
458 /* Deconfigure relevant bits in control register. */
459 tmp = get_debugreg(pid, PT_BP_CTRL) & ~(3 << (2 + (bp * 4)));
460 put_debugreg(pid, PT_BP_CTRL, tmp);
466 static int put_debugreg(long pid, unsigned int regno, long data)
469 register int old_srs;
471 #ifdef CONFIG_ETRAX_KGDB
472 /* Ignore write, but pretend it was ok if value is 0
473 (we don't want POKEUSR/SETREGS failing unnessecarily). */
474 return (data == 0) ? ret : -1;
477 /* Simple owner management. */
480 else if (bp_owner != pid) {
481 /* Ignore write, but pretend it was ok if value is 0
482 (we don't want POKEUSR/SETREGS failing unnessecarily). */
483 return (data == 0) ? ret : -1;
486 /* Remember old SRS. */
487 SPEC_REG_RD(SPEC_REG_SRS, old_srs);
488 /* Switch to BP bank. */
489 SUPP_BANK_SEL(BANK_BP);
491 switch (regno - PT_BP) {
493 SUPP_REG_WR(0, data); break;
500 SUPP_REG_WR(3, data); break;
502 SUPP_REG_WR(4, data); break;
504 SUPP_REG_WR(5, data); break;
506 SUPP_REG_WR(6, data); break;
508 SUPP_REG_WR(7, data); break;
510 SUPP_REG_WR(8, data); break;
512 SUPP_REG_WR(9, data); break;
514 SUPP_REG_WR(10, data); break;
516 SUPP_REG_WR(11, data); break;
518 SUPP_REG_WR(12, data); break;
520 SUPP_REG_WR(13, data); break;
522 SUPP_REG_WR(14, data); break;
529 SPEC_REG_WR(SPEC_REG_SRS, old_srs);
538 static long get_debugreg(long pid, unsigned int regno)
540 register int old_srs;
543 if (pid != bp_owner) {
547 /* Remember old SRS. */
548 SPEC_REG_RD(SPEC_REG_SRS, old_srs);
549 /* Switch to BP bank. */
550 SUPP_BANK_SEL(BANK_BP);
552 switch (regno - PT_BP) {
554 SUPP_REG_RD(0, data); break;
557 /* error return value? */
561 SUPP_REG_RD(3, data); break;
563 SUPP_REG_RD(4, data); break;
565 SUPP_REG_RD(5, data); break;
567 SUPP_REG_RD(6, data); break;
569 SUPP_REG_RD(7, data); break;
571 SUPP_REG_RD(8, data); break;
573 SUPP_REG_RD(9, data); break;
575 SUPP_REG_RD(10, data); break;
577 SUPP_REG_RD(11, data); break;
579 SUPP_REG_RD(12, data); break;
581 SUPP_REG_RD(13, data); break;
583 SUPP_REG_RD(14, data); break;
585 /* error return value? */
590 SPEC_REG_WR(SPEC_REG_SRS, old_srs);