Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/v850/kernel/ptrace.c -- `ptrace' system call |
| 3 | * |
| 4 | * Copyright (C) 2002,03,04 NEC Electronics Corporation |
| 5 | * Copyright (C) 2002,03,04 Miles Bader <miles@gnu.org> |
| 6 | * |
| 7 | * Derived from arch/mips/kernel/ptrace.c: |
| 8 | * |
| 9 | * Copyright (C) 1992 Ross Biro |
| 10 | * Copyright (C) Linus Torvalds |
| 11 | * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle |
| 12 | * Copyright (C) 1996 David S. Miller |
| 13 | * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com |
| 14 | * Copyright (C) 1999 MIPS Technologies, Inc. |
| 15 | * |
| 16 | * This file is subject to the terms and conditions of the GNU General |
| 17 | * Public License. See the file COPYING in the main directory of this |
| 18 | * archive for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/smp_lock.h> |
| 25 | #include <linux/ptrace.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 26 | #include <linux/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #include <asm/errno.h> |
| 29 | #include <asm/ptrace.h> |
| 30 | #include <asm/processor.h> |
| 31 | #include <asm/uaccess.h> |
| 32 | |
| 33 | /* Returns the address where the register at REG_OFFS in P is stashed away. */ |
| 34 | static v850_reg_t *reg_save_addr (unsigned reg_offs, struct task_struct *t) |
| 35 | { |
| 36 | struct pt_regs *regs; |
| 37 | |
| 38 | /* Three basic cases: |
| 39 | |
| 40 | (1) A register normally saved before calling the scheduler, is |
| 41 | available in the kernel entry pt_regs structure at the top |
| 42 | of the kernel stack. The kernel trap/irq exit path takes |
| 43 | care to save/restore almost all registers for ptrace'd |
| 44 | processes. |
| 45 | |
| 46 | (2) A call-clobbered register, where the process P entered the |
| 47 | kernel via [syscall] trap, is not stored anywhere; that's |
| 48 | OK, because such registers are not expected to be preserved |
| 49 | when the trap returns anyway (so we don't actually bother to |
| 50 | test for this case). |
| 51 | |
| 52 | (3) A few registers not used at all by the kernel, and so |
| 53 | normally never saved except by context-switches, are in the |
| 54 | context switch state. */ |
| 55 | |
| 56 | if (reg_offs == PT_CTPC || reg_offs == PT_CTPSW || reg_offs == PT_CTBP) |
| 57 | /* Register saved during context switch. */ |
| 58 | regs = thread_saved_regs (t); |
| 59 | else |
| 60 | /* Register saved during kernel entry (or not available). */ |
Al Viro | b7f6961 | 2006-01-12 01:05:51 -0800 | [diff] [blame] | 61 | regs = task_pt_regs (t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | return (v850_reg_t *)((char *)regs + reg_offs); |
| 64 | } |
| 65 | |
| 66 | /* Set the bits SET and clear the bits CLEAR in the v850e DIR |
| 67 | (`debug information register'). Returns the new value of DIR. */ |
| 68 | static inline v850_reg_t set_dir (v850_reg_t set, v850_reg_t clear) |
| 69 | { |
| 70 | register v850_reg_t rval asm ("r10"); |
| 71 | register v850_reg_t arg0 asm ("r6") = set; |
| 72 | register v850_reg_t arg1 asm ("r7") = clear; |
| 73 | |
| 74 | /* The dbtrap handler has exactly this functionality when called |
| 75 | from kernel mode. 0xf840 is a `dbtrap' insn. */ |
| 76 | asm (".short 0xf840" : "=r" (rval) : "r" (arg0), "r" (arg1)); |
| 77 | |
| 78 | return rval; |
| 79 | } |
| 80 | |
| 81 | /* Makes sure hardware single-stepping is (globally) enabled. |
| 82 | Returns true if successful. */ |
| 83 | static inline int enable_single_stepping (void) |
| 84 | { |
| 85 | static int enabled = 0; /* Remember whether we already did it. */ |
| 86 | if (! enabled) { |
| 87 | /* Turn on the SE (`single-step enable') bit, 0x100, in the |
| 88 | DIR (`debug information register'). This may fail if a |
| 89 | processor doesn't support it or something. We also try |
| 90 | to clear bit 0x40 (`INI'), which is necessary to use the |
| 91 | debug stuff on the v850e2; on the v850e, clearing 0x40 |
| 92 | shouldn't cause any problem. */ |
| 93 | v850_reg_t dir = set_dir (0x100, 0x40); |
| 94 | /* Make sure it really got set. */ |
| 95 | if (dir & 0x100) |
| 96 | enabled = 1; |
| 97 | } |
| 98 | return enabled; |
| 99 | } |
| 100 | |
| 101 | /* Try to set CHILD's single-step flag to VAL. Returns true if successful. */ |
| 102 | static int set_single_step (struct task_struct *t, int val) |
| 103 | { |
| 104 | v850_reg_t *psw_addr = reg_save_addr(PT_PSW, t); |
| 105 | if (val) { |
| 106 | /* Make sure single-stepping is enabled. */ |
| 107 | if (! enable_single_stepping ()) |
| 108 | return 0; |
| 109 | /* Set T's single-step flag. */ |
| 110 | *psw_addr |= 0x800; |
| 111 | } else |
| 112 | *psw_addr &= ~0x800; |
| 113 | return 1; |
| 114 | } |
| 115 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 116 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | int rval; |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | switch (request) { |
| 121 | unsigned long val, copied; |
| 122 | |
| 123 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
| 124 | case PTRACE_PEEKDATA: |
| 125 | copied = access_process_vm(child, addr, &val, sizeof(val), 0); |
| 126 | rval = -EIO; |
| 127 | if (copied != sizeof(val)) |
| 128 | break; |
| 129 | rval = put_user(val, (unsigned long *)data); |
| 130 | goto out; |
| 131 | |
| 132 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 133 | case PTRACE_POKEDATA: |
| 134 | rval = 0; |
| 135 | if (access_process_vm(child, addr, &data, sizeof(data), 1) |
| 136 | == sizeof(data)) |
| 137 | break; |
| 138 | rval = -EIO; |
| 139 | goto out; |
| 140 | |
| 141 | /* Read/write the word at location ADDR in the registers. */ |
| 142 | case PTRACE_PEEKUSR: |
| 143 | case PTRACE_POKEUSR: |
| 144 | rval = 0; |
| 145 | if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) { |
| 146 | /* Special requests that don't actually correspond |
| 147 | to offsets in struct pt_regs. */ |
| 148 | if (addr == PT_TEXT_ADDR) |
| 149 | val = child->mm->start_code; |
| 150 | else if (addr == PT_DATA_ADDR) |
| 151 | val = child->mm->start_data; |
| 152 | else if (addr == PT_TEXT_LEN) |
| 153 | val = child->mm->end_code |
| 154 | - child->mm->start_code; |
| 155 | else |
| 156 | rval = -EIO; |
| 157 | } else if (addr >= 0 && addr < PT_SIZE && (addr & 0x3) == 0) { |
| 158 | v850_reg_t *reg_addr = reg_save_addr(addr, child); |
| 159 | if (request == PTRACE_PEEKUSR) |
| 160 | val = *reg_addr; |
| 161 | else |
| 162 | *reg_addr = data; |
| 163 | } else |
| 164 | rval = -EIO; |
| 165 | |
| 166 | if (rval == 0 && request == PTRACE_PEEKUSR) |
| 167 | rval = put_user (val, (unsigned long *)data); |
| 168 | goto out; |
| 169 | |
| 170 | /* Continue and stop at next (return from) syscall */ |
| 171 | case PTRACE_SYSCALL: |
| 172 | /* Restart after a signal. */ |
| 173 | case PTRACE_CONT: |
| 174 | /* Execute a single instruction. */ |
| 175 | case PTRACE_SINGLESTEP: |
| 176 | rval = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 177 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | break; |
| 179 | |
| 180 | /* Turn CHILD's single-step flag on or off. */ |
| 181 | if (! set_single_step (child, request == PTRACE_SINGLESTEP)) |
| 182 | break; |
| 183 | |
| 184 | if (request == PTRACE_SYSCALL) |
| 185 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 186 | else |
| 187 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 188 | |
| 189 | child->exit_code = data; |
| 190 | wake_up_process(child); |
| 191 | rval = 0; |
| 192 | break; |
| 193 | |
| 194 | /* |
| 195 | * make the child exit. Best I can do is send it a sigkill. |
| 196 | * perhaps it should be put in the status that it wants to |
| 197 | * exit. |
| 198 | */ |
| 199 | case PTRACE_KILL: |
| 200 | rval = 0; |
| 201 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 202 | break; |
| 203 | child->exit_code = SIGKILL; |
| 204 | wake_up_process(child); |
| 205 | break; |
| 206 | |
| 207 | case PTRACE_DETACH: /* detach a process that was attached. */ |
| 208 | set_single_step (child, 0); /* Clear single-step flag */ |
| 209 | rval = ptrace_detach(child, data); |
| 210 | break; |
| 211 | |
| 212 | default: |
| 213 | rval = -EIO; |
| 214 | goto out; |
| 215 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 216 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return rval; |
| 218 | } |
| 219 | |
| 220 | asmlinkage void syscall_trace(void) |
| 221 | { |
| 222 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
| 223 | return; |
| 224 | if (!(current->ptrace & PT_PTRACED)) |
| 225 | return; |
| 226 | /* The 0x80 provides a way for the tracing parent to distinguish |
| 227 | between a syscall stop and SIGTRAP delivery */ |
| 228 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
| 229 | ? 0x80 : 0)); |
| 230 | /* |
| 231 | * this isn't the same as continuing with a signal, but it will do |
| 232 | * for normal use. strace only continues with a signal if the |
| 233 | * stopping signal is not SIGTRAP. -brl |
| 234 | */ |
| 235 | if (current->exit_code) { |
| 236 | send_sig(current->exit_code, current, 1); |
| 237 | current->exit_code = 0; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void ptrace_disable (struct task_struct *child) |
| 242 | { |
| 243 | /* nothing to do */ |
| 244 | } |