Linux 2.6.13-rc4
[linux-2.6/next.git] / arch / cris / arch-v32 / kernel / ptrace.c
blob208489da2a87fe0ee7ad1843ae69b28439539d4c
1 /*
2 * Copyright (C) 2000-2003, Axis Communications AB.
3 */
5 #include <linux/kernel.h>
6 #include <linux/sched.h>
7 #include <linux/mm.h>
8 #include <linux/smp.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>
17 #include <asm/page.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
46 unsigned long ret;
48 if (regno <= PT_EDA)
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);
56 else
57 ret = 0;
59 return ret;
63 * Write contents of register REGNO in task TASK.
65 int put_reg(struct task_struct *task, unsigned int regno, unsigned long data)
67 if (regno <= PT_EDA)
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);
77 else
78 return -1;
79 return 0;
83 * Called by kernel/ptrace.c when detaching.
85 * Make sure the single step bit is not set.
87 void
88 ptrace_disable(struct task_struct *child)
90 unsigned long tmp;
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 asmlinkage int
103 sys_ptrace(long request, long pid, long addr, long data)
105 struct task_struct *child;
106 int ret;
107 unsigned long __user *datap = (unsigned long __user *)data;
109 lock_kernel();
110 ret = -EPERM;
112 if (request == PTRACE_TRACEME) {
113 /* are we already being traced? */
114 if (current->ptrace & PT_PTRACED)
115 goto out;
116 ret = security_ptrace(current->parent, current);
117 if (ret)
118 goto out;
119 /* set the ptrace bit in the process flags. */
120 current->ptrace |= PT_PTRACED;
121 ret = 0;
122 goto out;
125 ret = -ESRCH;
126 read_lock(&tasklist_lock);
127 child = find_task_by_pid(pid);
129 if (child)
130 get_task_struct(child);
132 read_unlock(&tasklist_lock);
134 if (!child)
135 goto out;
137 ret = -EPERM;
139 if (pid == 1) /* Leave the init process alone! */
140 goto out_tsk;
142 if (request == PTRACE_ATTACH) {
143 ret = ptrace_attach(child);
144 goto out_tsk;
147 ret = ptrace_check_attach(child, request == PTRACE_KILL);
148 if (ret < 0)
149 goto out_tsk;
151 switch (request) {
152 /* Read word at location address. */
153 case PTRACE_PEEKTEXT:
154 case PTRACE_PEEKDATA: {
155 unsigned long tmp;
156 int copied;
158 ret = -EIO;
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;
167 } else {
168 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
170 if (copied != sizeof(tmp))
171 break;
174 ret = put_user(tmp,datap);
175 break;
178 /* Read the word at location address in the USER area. */
179 case PTRACE_PEEKUSR: {
180 unsigned long tmp;
182 ret = -EIO;
183 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
184 break;
186 tmp = get_reg(child, addr >> 2);
187 ret = put_user(tmp, datap);
188 break;
191 /* Write the word at location address. */
192 case PTRACE_POKETEXT:
193 case PTRACE_POKEDATA:
194 ret = 0;
196 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
197 break;
199 ret = -EIO;
200 break;
202 /* Write the word at location address in the USER area. */
203 case PTRACE_POKEUSR:
204 ret = -EIO;
205 if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
206 break;
208 addr >>= 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.
214 data &= CCS_MASK;
215 data |= get_reg(child, PT_CCS) & ~CCS_MASK;
217 if (put_reg(child, addr, data))
218 break;
219 ret = 0;
220 break;
222 case PTRACE_SYSCALL:
223 case PTRACE_CONT:
224 ret = -EIO;
226 if (!valid_signal(data))
227 break;
229 /* Continue means no single-step. */
230 put_reg(child, PT_SPC, 0);
232 if (!get_debugreg(child->pid, PT_BP_CTRL)) {
233 unsigned long tmp;
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);
242 else {
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);
250 ret = 0;
252 break;
254 /* Make the child exit by sending it a sigkill. */
255 case PTRACE_KILL:
256 ret = 0;
258 if (child->exit_state == EXIT_ZOMBIE)
259 break;
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);
268 break;
270 /* Set the trap flag. */
271 case PTRACE_SINGLESTEP: {
272 unsigned long tmp;
273 ret = -EIO;
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))
286 break;
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);
294 ret = 0;
295 break;
298 case PTRACE_DETACH:
299 ret = ptrace_detach(child, data);
300 break;
302 /* Get all GP registers from the child. */
303 case PTRACE_GETREGS: {
304 int i;
305 unsigned long tmp;
307 for (i = 0; i <= PT_MAX; i++) {
308 tmp = get_reg(child, i);
310 if (put_user(tmp, datap)) {
311 ret = -EFAULT;
312 goto out_tsk;
315 datap++;
318 ret = 0;
319 break;
322 /* Set all GP registers in the child. */
323 case PTRACE_SETREGS: {
324 int i;
325 unsigned long tmp;
327 for (i = 0; i <= PT_MAX; i++) {
328 if (get_user(tmp, datap)) {
329 ret = -EFAULT;
330 goto out_tsk;
333 if (i == PT_CCS) {
334 tmp &= CCS_MASK;
335 tmp |= get_reg(child, PT_CCS) & ~CCS_MASK;
338 put_reg(child, i, tmp);
339 datap++;
342 ret = 0;
343 break;
346 default:
347 ret = ptrace_request(child, request, addr, data);
348 break;
350 out_tsk:
351 put_task_struct(child);
352 out:
353 unlock_kernel();
354 return ret;
357 void do_syscall_trace(void)
359 if (!test_thread_flag(TIF_SYSCALL_TRACE))
360 return;
362 if (!(current->ptrace & PT_PTRACED))
363 return;
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)
368 ? 0x80 : 0));
371 * This isn't the same as continuing with a signal, but it will do for
372 * normal use.
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;
385 int copied;
386 int opsize = 0;
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))
391 return 0;
393 switch ((opcode & 0x0f00) >> 8) {
394 case 0x0:
395 case 0x9:
396 case 0xb:
397 opsize = 2;
398 break;
399 case 0xe:
400 case 0xf:
401 opsize = 6;
402 break;
403 case 0xd:
404 /* Could be 4 or 6; check more bits. */
405 if ((opcode & 0xff) == 0xff)
406 opsize = 4;
407 else
408 opsize = 6;
409 break;
410 default:
411 panic("ERROR: Couldn't find size of opcode 0x%lx at 0x%lx\n",
412 opcode, pc);
415 return opsize;
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);
423 if (pc & 0x1) {
424 unsigned long spc = get_reg(child, PT_SPC);
425 /* Delay slot bit set. Report as stopped on proper
426 instruction. */
427 if (spc) {
428 /* Rely on SPC if set. FIXME: We might want to check
429 that EXS indicates we stopped due to a single-step
430 exception. */
431 pc = spc;
432 } else {
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;
438 return pc;
441 static long bp_owner = 0;
443 /* Reachable from exit_thread in signal.c, so not static. */
444 void deconfigure_bp(long pid)
446 int bp;
448 /* Only deconfigure if the pid is the owner. */
449 if (bp_owner != pid)
450 return;
452 for (bp = 0; bp < 6; bp++) {
453 unsigned long tmp;
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);
462 /* No owner now. */
463 bp_owner = 0;
466 static int put_debugreg(long pid, unsigned int regno, long data)
468 int ret = 0;
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;
475 #endif
477 /* Simple owner management. */
478 if (!bp_owner)
479 bp_owner = pid;
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) {
492 case 0:
493 SUPP_REG_WR(0, data); break;
494 case 1:
495 case 2:
496 if (data)
497 ret = -1;
498 break;
499 case 3:
500 SUPP_REG_WR(3, data); break;
501 case 4:
502 SUPP_REG_WR(4, data); break;
503 case 5:
504 SUPP_REG_WR(5, data); break;
505 case 6:
506 SUPP_REG_WR(6, data); break;
507 case 7:
508 SUPP_REG_WR(7, data); break;
509 case 8:
510 SUPP_REG_WR(8, data); break;
511 case 9:
512 SUPP_REG_WR(9, data); break;
513 case 10:
514 SUPP_REG_WR(10, data); break;
515 case 11:
516 SUPP_REG_WR(11, data); break;
517 case 12:
518 SUPP_REG_WR(12, data); break;
519 case 13:
520 SUPP_REG_WR(13, data); break;
521 case 14:
522 SUPP_REG_WR(14, data); break;
523 default:
524 ret = -1;
525 break;
528 /* Restore SRS. */
529 SPEC_REG_WR(SPEC_REG_SRS, old_srs);
530 /* Just for show. */
531 NOP();
532 NOP();
533 NOP();
535 return ret;
538 static long get_debugreg(long pid, unsigned int regno)
540 register int old_srs;
541 register long data;
543 if (pid != bp_owner) {
544 return 0;
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) {
553 case 0:
554 SUPP_REG_RD(0, data); break;
555 case 1:
556 case 2:
557 /* error return value? */
558 data = 0;
559 break;
560 case 3:
561 SUPP_REG_RD(3, data); break;
562 case 4:
563 SUPP_REG_RD(4, data); break;
564 case 5:
565 SUPP_REG_RD(5, data); break;
566 case 6:
567 SUPP_REG_RD(6, data); break;
568 case 7:
569 SUPP_REG_RD(7, data); break;
570 case 8:
571 SUPP_REG_RD(8, data); break;
572 case 9:
573 SUPP_REG_RD(9, data); break;
574 case 10:
575 SUPP_REG_RD(10, data); break;
576 case 11:
577 SUPP_REG_RD(11, data); break;
578 case 12:
579 SUPP_REG_RD(12, data); break;
580 case 13:
581 SUPP_REG_RD(13, data); break;
582 case 14:
583 SUPP_REG_RD(14, data); break;
584 default:
585 /* error return value? */
586 data = 0;
589 /* Restore SRS. */
590 SPEC_REG_WR(SPEC_REG_SRS, old_srs);
591 /* Just for show. */
592 NOP();
593 NOP();
594 NOP();
596 return data;