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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/ptrace.h> |
| 19 | #include <linux/user.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 20 | #include <linux/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/uaccess.h> |
| 23 | #include <asm/page.h> |
| 24 | #include <asm/pgtable.h> |
| 25 | #include <asm/system.h> |
| 26 | #include <asm/processor.h> |
| 27 | |
| 28 | /* |
| 29 | * does not yet catch signals sent when the child dies. |
| 30 | * in exit.c or in signal.c. |
| 31 | */ |
| 32 | |
| 33 | /* determines which bits in the SR the user has access to. */ |
| 34 | /* 1 = access 0 = no access */ |
| 35 | #define SR_MASK 0x001f |
| 36 | |
| 37 | /* sets the trace bits. */ |
| 38 | #define TRACE_BITS 0x8000 |
| 39 | |
| 40 | /* Find the stack offset for a register, relative to thread.esp0. */ |
| 41 | #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg) |
| 42 | #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \ |
| 43 | - sizeof(struct switch_stack)) |
| 44 | /* Mapping from PT_xxx to the stack offset at which the register is |
| 45 | saved. Notice that usp has no stack-slot and needs to be treated |
| 46 | specially (see get_reg/put_reg below). */ |
| 47 | static int regoff[] = { |
| 48 | [0] = PT_REG(d1), |
| 49 | [1] = PT_REG(d2), |
| 50 | [2] = PT_REG(d3), |
| 51 | [3] = PT_REG(d4), |
| 52 | [4] = PT_REG(d5), |
| 53 | [5] = SW_REG(d6), |
| 54 | [6] = SW_REG(d7), |
| 55 | [7] = PT_REG(a0), |
| 56 | [8] = PT_REG(a1), |
| 57 | [9] = PT_REG(a2), |
| 58 | [10] = SW_REG(a3), |
| 59 | [11] = SW_REG(a4), |
| 60 | [12] = SW_REG(a5), |
| 61 | [13] = SW_REG(a6), |
| 62 | [14] = PT_REG(d0), |
| 63 | [15] = -1, |
| 64 | [16] = PT_REG(orig_d0), |
| 65 | [17] = PT_REG(sr), |
| 66 | [18] = PT_REG(pc), |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | * Get contents of register REGNO in task TASK. |
| 71 | */ |
| 72 | static inline long get_reg(struct task_struct *task, int regno) |
| 73 | { |
| 74 | unsigned long *addr; |
| 75 | |
| 76 | if (regno == PT_USP) |
| 77 | addr = &task->thread.usp; |
Ahmed S. Darwish | ea5e1a8 | 2007-02-10 01:43:47 -0800 | [diff] [blame] | 78 | else if (regno < ARRAY_SIZE(regoff)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | addr = (unsigned long *)(task->thread.esp0 + regoff[regno]); |
| 80 | else |
| 81 | return 0; |
| 82 | return *addr; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Write contents of register REGNO in task TASK. |
| 87 | */ |
| 88 | static inline int put_reg(struct task_struct *task, int regno, |
| 89 | unsigned long data) |
| 90 | { |
| 91 | unsigned long *addr; |
| 92 | |
| 93 | if (regno == PT_USP) |
| 94 | addr = &task->thread.usp; |
Ahmed S. Darwish | ea5e1a8 | 2007-02-10 01:43:47 -0800 | [diff] [blame] | 95 | else if (regno < ARRAY_SIZE(regoff)) |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 96 | addr = (unsigned long *)(task->thread.esp0 + regoff[regno]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | else |
| 98 | return -1; |
| 99 | *addr = data; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * Make sure the single step bit is not set. |
| 105 | */ |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 106 | static inline void singlestep_disable(struct task_struct *child) |
| 107 | { |
| 108 | unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16); |
| 109 | put_reg(child, PT_SR, tmp); |
Roman Zippel | 3b66a1e | 2005-11-13 16:06:59 -0800 | [diff] [blame] | 110 | clear_tsk_thread_flag(child, TIF_DELAYED_TRACE); |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Called by kernel/ptrace.c when detaching.. |
| 115 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | void ptrace_disable(struct task_struct *child) |
| 117 | { |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 118 | singlestep_disable(child); |
Roman Zippel | 3b66a1e | 2005-11-13 16:06:59 -0800 | [diff] [blame] | 119 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 122 | 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] | 123 | { |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 124 | unsigned long tmp; |
| 125 | int i, ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | switch (request) { |
| 128 | /* when I and D space are separate, these will need to be fixed. */ |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 129 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 130 | case PTRACE_PEEKDATA: |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame^] | 131 | ret = generic_ptrace_peekdata(child, addr, data); |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 132 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | /* read the word at location addr in the USER area. */ |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 135 | case PTRACE_PEEKUSR: |
| 136 | if (addr & 3) |
| 137 | goto out_eio; |
| 138 | addr >>= 2; /* temporary hack. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 140 | if (addr >= 0 && addr < 19) { |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 141 | tmp = get_reg(child, addr); |
| 142 | if (addr == PT_SR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | tmp >>= 16; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 144 | } else if (addr >= 21 && addr < 49) { |
| 145 | tmp = child->thread.fp[addr - 21]; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 146 | /* Convert internal fpu reg representation |
| 147 | * into long double format |
| 148 | */ |
| 149 | if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) |
| 150 | tmp = ((tmp & 0xffff0000) << 15) | |
| 151 | ((tmp & 0x0000ffff) << 16); |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 152 | } else |
| 153 | break; |
| 154 | ret = put_user(tmp, (unsigned long *)data); |
| 155 | break; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 156 | |
| 157 | /* when I and D space are separate, this will have to be fixed. */ |
| 158 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 159 | case PTRACE_POKEDATA: |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 160 | if (access_process_vm(child, addr, &data, sizeof(data), 1) != sizeof(data)) |
| 161 | goto out_eio; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 162 | break; |
| 163 | |
| 164 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 165 | if (addr & 3) |
| 166 | goto out_eio; |
| 167 | addr >>= 2; /* temporary hack. */ |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 168 | |
| 169 | if (addr == PT_SR) { |
| 170 | data &= SR_MASK; |
| 171 | data <<= 16; |
| 172 | data |= get_reg(child, PT_SR) & ~(SR_MASK << 16); |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 173 | } else if (addr >= 0 && addr < 19) { |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 174 | if (put_reg(child, addr, data)) |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 175 | goto out_eio; |
| 176 | } else if (addr >= 21 && addr < 48) { |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 177 | /* Convert long double format |
| 178 | * into internal fpu reg representation |
| 179 | */ |
| 180 | if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) { |
| 181 | data = (unsigned long)data << 15; |
| 182 | data = (data & 0xffff0000) | |
| 183 | ((data & 0x0000ffff) >> 1); |
| 184 | } |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 185 | child->thread.fp[addr - 21] = data; |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 186 | } else |
| 187 | goto out_eio; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 188 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 190 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 191 | case PTRACE_CONT: /* restart after signal. */ |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 192 | if (!valid_signal(data)) |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 193 | goto out_eio; |
| 194 | |
| 195 | if (request == PTRACE_SYSCALL) |
Roman Zippel | 3b66a1e | 2005-11-13 16:06:59 -0800 | [diff] [blame] | 196 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 197 | else |
Roman Zippel | 3b66a1e | 2005-11-13 16:06:59 -0800 | [diff] [blame] | 198 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 199 | child->exit_code = data; |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 200 | singlestep_disable(child); |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 201 | wake_up_process(child); |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 202 | break; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * make the child exit. Best I can do is send it a sigkill. |
| 206 | * perhaps it should be put in the status that it wants to |
| 207 | * exit. |
| 208 | */ |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 209 | case PTRACE_KILL: |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 210 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 211 | break; |
| 212 | child->exit_code = SIGKILL; |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 213 | singlestep_disable(child); |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 214 | wake_up_process(child); |
| 215 | break; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 216 | |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 217 | case PTRACE_SINGLESTEP: /* set the trap flag. */ |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 218 | if (!valid_signal(data)) |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 219 | goto out_eio; |
| 220 | |
Roman Zippel | 3b66a1e | 2005-11-13 16:06:59 -0800 | [diff] [blame] | 221 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 222 | tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16); |
| 223 | put_reg(child, PT_SR, tmp); |
Roman Zippel | 3b66a1e | 2005-11-13 16:06:59 -0800 | [diff] [blame] | 224 | set_tsk_thread_flag(child, TIF_DELAYED_TRACE); |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 225 | |
| 226 | child->exit_code = data; |
| 227 | /* give it a chance to run. */ |
| 228 | wake_up_process(child); |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 229 | break; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 230 | |
| 231 | case PTRACE_DETACH: /* detach a process that was attached. */ |
| 232 | ret = ptrace_detach(child, data); |
| 233 | break; |
| 234 | |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 235 | case PTRACE_GETREGS: /* Get all gp regs from the child. */ |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 236 | for (i = 0; i < 19; i++) { |
| 237 | tmp = get_reg(child, i); |
| 238 | if (i == PT_SR) |
| 239 | tmp >>= 16; |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 240 | ret = put_user(tmp, (unsigned long *)data); |
| 241 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | break; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 243 | data += sizeof(long); |
| 244 | } |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 245 | break; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 246 | |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 247 | case PTRACE_SETREGS: /* Set all gp regs in the child. */ |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 248 | for (i = 0; i < 19; i++) { |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 249 | ret = get_user(tmp, (unsigned long *)data); |
| 250 | if (ret) |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 251 | break; |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 252 | if (i == PT_SR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | tmp &= SR_MASK; |
| 254 | tmp <<= 16; |
| 255 | tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 257 | put_reg(child, i, tmp); |
| 258 | data += sizeof(long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 260 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 262 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 263 | if (copy_to_user((void *)data, &child->thread.fp, |
| 264 | sizeof(struct user_m68kfp_struct))) |
| 265 | ret = -EFAULT; |
| 266 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 268 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 269 | if (copy_from_user(&child->thread.fp, (void *)data, |
| 270 | sizeof(struct user_m68kfp_struct))) |
| 271 | ret = -EFAULT; |
| 272 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Roman Zippel | b3319f5 | 2005-09-03 15:57:07 -0700 | [diff] [blame] | 274 | default: |
| 275 | ret = ptrace_request(child, request, addr, data); |
| 276 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return ret; |
Roman Zippel | 69f447c | 2005-09-03 15:57:08 -0700 | [diff] [blame] | 280 | out_eio: |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 281 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | asmlinkage void syscall_trace(void) |
| 285 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
| 287 | ? 0x80 : 0)); |
| 288 | /* |
| 289 | * this isn't the same as continuing with a signal, but it will do |
| 290 | * for normal use. strace only continues with a signal if the |
| 291 | * stopping signal is not SIGTRAP. -brl |
| 292 | */ |
| 293 | if (current->exit_code) { |
| 294 | send_sig(current->exit_code, current, 1); |
| 295 | current->exit_code = 0; |
| 296 | } |
| 297 | } |