| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  linux/arch/m68k/kernel/ptrace.c | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 1994 by Hamish Macdonald | 
|  | 5 | *  Taken from linux/kernel/ptrace.c and modified for M680x0. | 
|  | 6 | *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds | 
|  | 7 | * | 
|  | 8 | * This file is subject to the terms and conditions of the GNU General | 
|  | 9 | * Public License.  See the file COPYING in the main directory of | 
|  | 10 | * this archive for more details. | 
|  | 11 | */ | 
|  | 12 |  | 
|  | 13 | #include <linux/kernel.h> | 
|  | 14 | #include <linux/sched.h> | 
|  | 15 | #include <linux/mm.h> | 
|  | 16 | #include <linux/smp.h> | 
|  | 17 | #include <linux/smp_lock.h> | 
|  | 18 | #include <linux/errno.h> | 
|  | 19 | #include <linux/ptrace.h> | 
|  | 20 | #include <linux/user.h> | 
|  | 21 | #include <linux/config.h> | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 22 | #include <linux/signal.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 |  | 
|  | 24 | #include <asm/uaccess.h> | 
|  | 25 | #include <asm/page.h> | 
|  | 26 | #include <asm/pgtable.h> | 
|  | 27 | #include <asm/system.h> | 
|  | 28 | #include <asm/processor.h> | 
|  | 29 |  | 
|  | 30 | /* | 
|  | 31 | * does not yet catch signals sent when the child dies. | 
|  | 32 | * in exit.c or in signal.c. | 
|  | 33 | */ | 
|  | 34 |  | 
|  | 35 | /* determines which bits in the SR the user has access to. */ | 
|  | 36 | /* 1 = access 0 = no access */ | 
|  | 37 | #define SR_MASK 0x001f | 
|  | 38 |  | 
|  | 39 | /* sets the trace bits. */ | 
|  | 40 | #define TRACE_BITS 0x8000 | 
|  | 41 |  | 
|  | 42 | /* Find the stack offset for a register, relative to thread.esp0. */ | 
|  | 43 | #define PT_REG(reg)	((long)&((struct pt_regs *)0)->reg) | 
|  | 44 | #define SW_REG(reg)	((long)&((struct switch_stack *)0)->reg \ | 
|  | 45 | - sizeof(struct switch_stack)) | 
|  | 46 | /* Mapping from PT_xxx to the stack offset at which the register is | 
|  | 47 | saved.  Notice that usp has no stack-slot and needs to be treated | 
|  | 48 | specially (see get_reg/put_reg below). */ | 
|  | 49 | static int regoff[] = { | 
|  | 50 | [0]	= PT_REG(d1), | 
|  | 51 | [1]	= PT_REG(d2), | 
|  | 52 | [2]	= PT_REG(d3), | 
|  | 53 | [3]	= PT_REG(d4), | 
|  | 54 | [4]	= PT_REG(d5), | 
|  | 55 | [5]	= SW_REG(d6), | 
|  | 56 | [6]	= SW_REG(d7), | 
|  | 57 | [7]	= PT_REG(a0), | 
|  | 58 | [8]	= PT_REG(a1), | 
|  | 59 | [9]	= PT_REG(a2), | 
|  | 60 | [10]	= SW_REG(a3), | 
|  | 61 | [11]	= SW_REG(a4), | 
|  | 62 | [12]	= SW_REG(a5), | 
|  | 63 | [13]	= SW_REG(a6), | 
|  | 64 | [14]	= PT_REG(d0), | 
|  | 65 | [15]	= -1, | 
|  | 66 | [16]	= PT_REG(orig_d0), | 
|  | 67 | [17]	= PT_REG(sr), | 
|  | 68 | [18]	= PT_REG(pc), | 
|  | 69 | }; | 
|  | 70 |  | 
|  | 71 | /* | 
|  | 72 | * Get contents of register REGNO in task TASK. | 
|  | 73 | */ | 
|  | 74 | static inline long get_reg(struct task_struct *task, int regno) | 
|  | 75 | { | 
|  | 76 | unsigned long *addr; | 
|  | 77 |  | 
|  | 78 | if (regno == PT_USP) | 
|  | 79 | addr = &task->thread.usp; | 
|  | 80 | else if (regno < sizeof(regoff)/sizeof(regoff[0])) | 
|  | 81 | addr = (unsigned long *)(task->thread.esp0 + regoff[regno]); | 
|  | 82 | else | 
|  | 83 | return 0; | 
|  | 84 | return *addr; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | /* | 
|  | 88 | * Write contents of register REGNO in task TASK. | 
|  | 89 | */ | 
|  | 90 | static inline int put_reg(struct task_struct *task, int regno, | 
|  | 91 | unsigned long data) | 
|  | 92 | { | 
|  | 93 | unsigned long *addr; | 
|  | 94 |  | 
|  | 95 | if (regno == PT_USP) | 
|  | 96 | addr = &task->thread.usp; | 
|  | 97 | else if (regno < sizeof(regoff)/sizeof(regoff[0])) | 
|  | 98 | addr = (unsigned long *) (task->thread.esp0 + regoff[regno]); | 
|  | 99 | else | 
|  | 100 | return -1; | 
|  | 101 | *addr = data; | 
|  | 102 | return 0; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | /* | 
|  | 106 | * Called by kernel/ptrace.c when detaching.. | 
|  | 107 | * | 
|  | 108 | * Make sure the single step bit is not set. | 
|  | 109 | */ | 
|  | 110 | void ptrace_disable(struct task_struct *child) | 
|  | 111 | { | 
|  | 112 | unsigned long tmp; | 
|  | 113 | /* make sure the single step bit is not set. */ | 
|  | 114 | tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16); | 
|  | 115 | put_reg(child, PT_SR, tmp); | 
|  | 116 | child->thread.work.delayed_trace = 0; | 
|  | 117 | child->thread.work.syscall_trace = 0; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | asmlinkage int sys_ptrace(long request, long pid, long addr, long data) | 
|  | 121 | { | 
|  | 122 | struct task_struct *child; | 
|  | 123 | int ret; | 
|  | 124 |  | 
|  | 125 | lock_kernel(); | 
|  | 126 | ret = -EPERM; | 
|  | 127 | if (request == PTRACE_TRACEME) { | 
|  | 128 | /* are we already being traced? */ | 
|  | 129 | if (current->ptrace & PT_PTRACED) | 
|  | 130 | goto out; | 
|  | 131 | /* set the ptrace bit in the process flags. */ | 
|  | 132 | current->ptrace |= PT_PTRACED; | 
|  | 133 | ret = 0; | 
|  | 134 | goto out; | 
|  | 135 | } | 
|  | 136 | ret = -ESRCH; | 
|  | 137 | read_lock(&tasklist_lock); | 
|  | 138 | child = find_task_by_pid(pid); | 
|  | 139 | if (child) | 
|  | 140 | get_task_struct(child); | 
|  | 141 | read_unlock(&tasklist_lock); | 
|  | 142 | if (!child) | 
|  | 143 | goto out; | 
|  | 144 |  | 
|  | 145 | ret = -EPERM; | 
|  | 146 | if (pid == 1)		/* you may not mess with init */ | 
|  | 147 | goto out_tsk; | 
|  | 148 |  | 
|  | 149 | if (request == PTRACE_ATTACH) { | 
|  | 150 | ret = ptrace_attach(child); | 
|  | 151 | goto out_tsk; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | ret = ptrace_check_attach(child, request == PTRACE_KILL); | 
|  | 155 | if (ret < 0) | 
|  | 156 | goto out_tsk; | 
|  | 157 |  | 
|  | 158 | switch (request) { | 
|  | 159 | /* when I and D space are separate, these will need to be fixed. */ | 
|  | 160 | case PTRACE_PEEKTEXT: /* read word at location addr. */ | 
|  | 161 | case PTRACE_PEEKDATA: { | 
|  | 162 | unsigned long tmp; | 
|  | 163 | int copied; | 
|  | 164 |  | 
|  | 165 | copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); | 
|  | 166 | ret = -EIO; | 
|  | 167 | if (copied != sizeof(tmp)) | 
|  | 168 | break; | 
|  | 169 | ret = put_user(tmp,(unsigned long *) data); | 
|  | 170 | break; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | /* read the word at location addr in the USER area. */ | 
|  | 174 | case PTRACE_PEEKUSR: { | 
|  | 175 | unsigned long tmp; | 
|  | 176 |  | 
|  | 177 | ret = -EIO; | 
|  | 178 | if ((addr & 3) || addr < 0 || | 
|  | 179 | addr > sizeof(struct user) - 3) | 
|  | 180 | break; | 
|  | 181 |  | 
|  | 182 | tmp = 0;  /* Default return condition */ | 
|  | 183 | addr = addr >> 2; /* temporary hack. */ | 
|  | 184 | ret = -EIO; | 
|  | 185 | if (addr < 19) { | 
|  | 186 | tmp = get_reg(child, addr); | 
|  | 187 | if (addr == PT_SR) | 
|  | 188 | tmp >>= 16; | 
|  | 189 | } else if (addr >= 21 && addr < 49) { | 
|  | 190 | tmp = child->thread.fp[addr - 21]; | 
|  | 191 | #ifdef CONFIG_M68KFPU_EMU | 
|  | 192 | /* Convert internal fpu reg representation | 
|  | 193 | * into long double format | 
|  | 194 | */ | 
|  | 195 | if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) | 
|  | 196 | tmp = ((tmp & 0xffff0000) << 15) | | 
|  | 197 | ((tmp & 0x0000ffff) << 16); | 
|  | 198 | #endif | 
|  | 199 | } else | 
|  | 200 | break; | 
|  | 201 | ret = put_user(tmp,(unsigned long *) data); | 
|  | 202 | break; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | /* when I and D space are separate, this will have to be fixed. */ | 
|  | 206 | case PTRACE_POKETEXT: /* write the word at location addr. */ | 
|  | 207 | case PTRACE_POKEDATA: | 
|  | 208 | ret = 0; | 
|  | 209 | if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data)) | 
|  | 210 | break; | 
|  | 211 | ret = -EIO; | 
|  | 212 | break; | 
|  | 213 |  | 
|  | 214 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ | 
|  | 215 | ret = -EIO; | 
|  | 216 | if ((addr & 3) || addr < 0 || | 
|  | 217 | addr > sizeof(struct user) - 3) | 
|  | 218 | break; | 
|  | 219 |  | 
|  | 220 | addr = addr >> 2; /* temporary hack. */ | 
|  | 221 |  | 
|  | 222 | if (addr == PT_SR) { | 
|  | 223 | data &= SR_MASK; | 
|  | 224 | data <<= 16; | 
|  | 225 | data |= get_reg(child, PT_SR) & ~(SR_MASK << 16); | 
|  | 226 | } | 
|  | 227 | if (addr < 19) { | 
|  | 228 | if (put_reg(child, addr, data)) | 
|  | 229 | break; | 
|  | 230 | ret = 0; | 
|  | 231 | break; | 
|  | 232 | } | 
|  | 233 | if (addr >= 21 && addr < 48) | 
|  | 234 | { | 
|  | 235 | #ifdef CONFIG_M68KFPU_EMU | 
|  | 236 | /* Convert long double format | 
|  | 237 | * into internal fpu reg representation | 
|  | 238 | */ | 
|  | 239 | if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) { | 
|  | 240 | data = (unsigned long)data << 15; | 
|  | 241 | data = (data & 0xffff0000) | | 
|  | 242 | ((data & 0x0000ffff) >> 1); | 
|  | 243 | } | 
|  | 244 | #endif | 
|  | 245 | child->thread.fp[addr - 21] = data; | 
|  | 246 | ret = 0; | 
|  | 247 | } | 
|  | 248 | break; | 
|  | 249 |  | 
|  | 250 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ | 
|  | 251 | case PTRACE_CONT: { /* restart after signal. */ | 
|  | 252 | long tmp; | 
|  | 253 |  | 
|  | 254 | ret = -EIO; | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 255 | if (!valid_signal(data)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | break; | 
|  | 257 | if (request == PTRACE_SYSCALL) { | 
|  | 258 | child->thread.work.syscall_trace = ~0; | 
|  | 259 | } else { | 
|  | 260 | child->thread.work.syscall_trace = 0; | 
|  | 261 | } | 
|  | 262 | child->exit_code = data; | 
|  | 263 | /* make sure the single step bit is not set. */ | 
|  | 264 | tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16); | 
|  | 265 | put_reg(child, PT_SR, tmp); | 
|  | 266 | child->thread.work.delayed_trace = 0; | 
|  | 267 | wake_up_process(child); | 
|  | 268 | ret = 0; | 
|  | 269 | break; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | /* | 
|  | 273 | * make the child exit.  Best I can do is send it a sigkill. | 
|  | 274 | * perhaps it should be put in the status that it wants to | 
|  | 275 | * exit. | 
|  | 276 | */ | 
|  | 277 | case PTRACE_KILL: { | 
|  | 278 | long tmp; | 
|  | 279 |  | 
|  | 280 | ret = 0; | 
|  | 281 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ | 
|  | 282 | break; | 
|  | 283 | child->exit_code = SIGKILL; | 
|  | 284 | /* make sure the single step bit is not set. */ | 
|  | 285 | tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16); | 
|  | 286 | put_reg(child, PT_SR, tmp); | 
|  | 287 | child->thread.work.delayed_trace = 0; | 
|  | 288 | wake_up_process(child); | 
|  | 289 | break; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | case PTRACE_SINGLESTEP: {  /* set the trap flag. */ | 
|  | 293 | long tmp; | 
|  | 294 |  | 
|  | 295 | ret = -EIO; | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 296 | if (!valid_signal(data)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | break; | 
|  | 298 | child->thread.work.syscall_trace = 0; | 
|  | 299 | tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16); | 
|  | 300 | put_reg(child, PT_SR, tmp); | 
|  | 301 | child->thread.work.delayed_trace = 1; | 
|  | 302 |  | 
|  | 303 | child->exit_code = data; | 
|  | 304 | /* give it a chance to run. */ | 
|  | 305 | wake_up_process(child); | 
|  | 306 | ret = 0; | 
|  | 307 | break; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | case PTRACE_DETACH:	/* detach a process that was attached. */ | 
|  | 311 | ret = ptrace_detach(child, data); | 
|  | 312 | break; | 
|  | 313 |  | 
|  | 314 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ | 
|  | 315 | int i; | 
|  | 316 | unsigned long tmp; | 
|  | 317 | for (i = 0; i < 19; i++) { | 
|  | 318 | tmp = get_reg(child, i); | 
|  | 319 | if (i == PT_SR) | 
|  | 320 | tmp >>= 16; | 
|  | 321 | if (put_user(tmp, (unsigned long *) data)) { | 
|  | 322 | ret = -EFAULT; | 
|  | 323 | break; | 
|  | 324 | } | 
|  | 325 | data += sizeof(long); | 
|  | 326 | } | 
|  | 327 | ret = 0; | 
|  | 328 | break; | 
|  | 329 | } | 
|  | 330 |  | 
|  | 331 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ | 
|  | 332 | int i; | 
|  | 333 | unsigned long tmp; | 
|  | 334 | for (i = 0; i < 19; i++) { | 
|  | 335 | if (get_user(tmp, (unsigned long *) data)) { | 
|  | 336 | ret = -EFAULT; | 
|  | 337 | break; | 
|  | 338 | } | 
|  | 339 | if (i == PT_SR) { | 
|  | 340 | tmp &= SR_MASK; | 
|  | 341 | tmp <<= 16; | 
|  | 342 | tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16); | 
|  | 343 | } | 
|  | 344 | put_reg(child, i, tmp); | 
|  | 345 | data += sizeof(long); | 
|  | 346 | } | 
|  | 347 | ret = 0; | 
|  | 348 | break; | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | case PTRACE_GETFPREGS: { /* Get the child FPU state. */ | 
|  | 352 | ret = 0; | 
|  | 353 | if (copy_to_user((void *)data, &child->thread.fp, | 
|  | 354 | sizeof(struct user_m68kfp_struct))) | 
|  | 355 | ret = -EFAULT; | 
|  | 356 | break; | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | case PTRACE_SETFPREGS: { /* Set the child FPU state. */ | 
|  | 360 | ret = 0; | 
|  | 361 | if (copy_from_user(&child->thread.fp, (void *)data, | 
|  | 362 | sizeof(struct user_m68kfp_struct))) | 
|  | 363 | ret = -EFAULT; | 
|  | 364 | break; | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | default: | 
|  | 368 | ret = ptrace_request(child, request, addr, data); | 
|  | 369 | break; | 
|  | 370 | } | 
|  | 371 | out_tsk: | 
|  | 372 | put_task_struct(child); | 
|  | 373 | out: | 
|  | 374 | unlock_kernel(); | 
|  | 375 | return ret; | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | asmlinkage void syscall_trace(void) | 
|  | 379 | { | 
|  | 380 | if (!current->thread.work.delayed_trace && | 
|  | 381 | !current->thread.work.syscall_trace) | 
|  | 382 | return; | 
|  | 383 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) | 
|  | 384 | ? 0x80 : 0)); | 
|  | 385 | /* | 
|  | 386 | * this isn't the same as continuing with a signal, but it will do | 
|  | 387 | * for normal use.  strace only continues with a signal if the | 
|  | 388 | * stopping signal is not SIGTRAP.  -brl | 
|  | 389 | */ | 
|  | 390 | if (current->exit_code) { | 
|  | 391 | send_sig(current->exit_code, current, 1); | 
|  | 392 | current->exit_code = 0; | 
|  | 393 | } | 
|  | 394 | } |