Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 1992 Ross Biro |
| 7 | * Copyright (C) Linus Torvalds |
| 8 | * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle |
| 9 | * Copyright (C) 1996 David S. Miller |
| 10 | * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com |
| 11 | * Copyright (C) 1999 MIPS Technologies, Inc. |
| 12 | * Copyright (C) 2000 Ulf Carlsson |
| 13 | * |
| 14 | * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit |
| 15 | * binaries. |
| 16 | */ |
| 17 | #include <linux/config.h> |
| 18 | #include <linux/compiler.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/ptrace.h> |
| 24 | #include <linux/audit.h> |
| 25 | #include <linux/smp.h> |
| 26 | #include <linux/smp_lock.h> |
| 27 | #include <linux/user.h> |
| 28 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <asm/cpu.h> |
| 31 | #include <asm/fpu.h> |
| 32 | #include <asm/mipsregs.h> |
| 33 | #include <asm/pgtable.h> |
| 34 | #include <asm/page.h> |
| 35 | #include <asm/system.h> |
| 36 | #include <asm/uaccess.h> |
| 37 | #include <asm/bootinfo.h> |
| 38 | |
| 39 | /* |
| 40 | * Called by kernel/ptrace.c when detaching.. |
| 41 | * |
| 42 | * Make sure single step bits etc are not set. |
| 43 | */ |
| 44 | void ptrace_disable(struct task_struct *child) |
| 45 | { |
| 46 | /* Nothing to do.. */ |
| 47 | } |
| 48 | |
| 49 | asmlinkage int sys_ptrace(long request, long pid, long addr, long data) |
| 50 | { |
| 51 | struct task_struct *child; |
| 52 | int ret; |
| 53 | |
| 54 | #if 0 |
| 55 | printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n", |
| 56 | (int) request, (int) pid, (unsigned long) addr, |
| 57 | (unsigned long) data); |
| 58 | #endif |
| 59 | lock_kernel(); |
| 60 | ret = -EPERM; |
| 61 | if (request == PTRACE_TRACEME) { |
| 62 | /* are we already being traced? */ |
| 63 | if (current->ptrace & PT_PTRACED) |
| 64 | goto out; |
| 65 | if ((ret = security_ptrace(current->parent, current))) |
| 66 | goto out; |
| 67 | /* set the ptrace bit in the process flags. */ |
| 68 | current->ptrace |= PT_PTRACED; |
| 69 | ret = 0; |
| 70 | goto out; |
| 71 | } |
| 72 | ret = -ESRCH; |
| 73 | read_lock(&tasklist_lock); |
| 74 | child = find_task_by_pid(pid); |
| 75 | if (child) |
| 76 | get_task_struct(child); |
| 77 | read_unlock(&tasklist_lock); |
| 78 | if (!child) |
| 79 | goto out; |
| 80 | |
| 81 | ret = -EPERM; |
| 82 | if (pid == 1) /* you may not mess with init */ |
| 83 | goto out_tsk; |
| 84 | |
| 85 | if (request == PTRACE_ATTACH) { |
| 86 | ret = ptrace_attach(child); |
| 87 | goto out_tsk; |
| 88 | } |
| 89 | |
| 90 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 91 | if (ret < 0) |
| 92 | goto out_tsk; |
| 93 | |
| 94 | switch (request) { |
| 95 | /* when I and D space are separate, these will need to be fixed. */ |
| 96 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
| 97 | case PTRACE_PEEKDATA: { |
| 98 | unsigned long tmp; |
| 99 | int copied; |
| 100 | |
| 101 | copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); |
| 102 | ret = -EIO; |
| 103 | if (copied != sizeof(tmp)) |
| 104 | break; |
| 105 | ret = put_user(tmp,(unsigned long *) data); |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | /* Read the word at location addr in the USER area. */ |
| 110 | case PTRACE_PEEKUSR: { |
| 111 | struct pt_regs *regs; |
| 112 | unsigned long tmp = 0; |
| 113 | |
| 114 | regs = (struct pt_regs *) ((unsigned long) child->thread_info + |
| 115 | THREAD_SIZE - 32 - sizeof(struct pt_regs)); |
| 116 | ret = 0; /* Default return value. */ |
| 117 | |
| 118 | switch (addr) { |
| 119 | case 0 ... 31: |
| 120 | tmp = regs->regs[addr]; |
| 121 | break; |
| 122 | case FPR_BASE ... FPR_BASE + 31: |
| 123 | if (tsk_used_math(child)) { |
| 124 | fpureg_t *fregs = get_fpu_regs(child); |
| 125 | |
| 126 | #ifdef CONFIG_MIPS32 |
| 127 | /* |
| 128 | * The odd registers are actually the high |
| 129 | * order bits of the values stored in the even |
| 130 | * registers - unless we're using r2k_switch.S. |
| 131 | */ |
| 132 | if (addr & 1) |
| 133 | tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32); |
| 134 | else |
| 135 | tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff); |
| 136 | #endif |
| 137 | #ifdef CONFIG_MIPS64 |
| 138 | tmp = fregs[addr - FPR_BASE]; |
| 139 | #endif |
| 140 | } else { |
| 141 | tmp = -1; /* FP not yet used */ |
| 142 | } |
| 143 | break; |
| 144 | case PC: |
| 145 | tmp = regs->cp0_epc; |
| 146 | break; |
| 147 | case CAUSE: |
| 148 | tmp = regs->cp0_cause; |
| 149 | break; |
| 150 | case BADVADDR: |
| 151 | tmp = regs->cp0_badvaddr; |
| 152 | break; |
| 153 | case MMHI: |
| 154 | tmp = regs->hi; |
| 155 | break; |
| 156 | case MMLO: |
| 157 | tmp = regs->lo; |
| 158 | break; |
| 159 | case FPC_CSR: |
| 160 | if (cpu_has_fpu) |
| 161 | tmp = child->thread.fpu.hard.fcr31; |
| 162 | else |
| 163 | tmp = child->thread.fpu.soft.fcr31; |
| 164 | break; |
| 165 | case FPC_EIR: { /* implementation / version register */ |
| 166 | unsigned int flags; |
| 167 | |
| 168 | if (!cpu_has_fpu) |
| 169 | break; |
| 170 | |
| 171 | flags = read_c0_status(); |
| 172 | __enable_fpu(); |
| 173 | __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp)); |
| 174 | write_c0_status(flags); |
| 175 | break; |
| 176 | } |
| 177 | default: |
| 178 | tmp = 0; |
| 179 | ret = -EIO; |
| 180 | goto out_tsk; |
| 181 | } |
| 182 | ret = put_user(tmp, (unsigned long *) data); |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | /* when I and D space are separate, this will have to be fixed. */ |
| 187 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 188 | case PTRACE_POKEDATA: |
| 189 | ret = 0; |
| 190 | if (access_process_vm(child, addr, &data, sizeof(data), 1) |
| 191 | == sizeof(data)) |
| 192 | break; |
| 193 | ret = -EIO; |
| 194 | break; |
| 195 | |
| 196 | case PTRACE_POKEUSR: { |
| 197 | struct pt_regs *regs; |
| 198 | ret = 0; |
| 199 | regs = (struct pt_regs *) ((unsigned long) child->thread_info + |
| 200 | THREAD_SIZE - 32 - sizeof(struct pt_regs)); |
| 201 | |
| 202 | switch (addr) { |
| 203 | case 0 ... 31: |
| 204 | regs->regs[addr] = data; |
| 205 | break; |
| 206 | case FPR_BASE ... FPR_BASE + 31: { |
| 207 | fpureg_t *fregs = get_fpu_regs(child); |
| 208 | |
| 209 | if (!tsk_used_math(child)) { |
| 210 | /* FP not yet used */ |
| 211 | memset(&child->thread.fpu.hard, ~0, |
| 212 | sizeof(child->thread.fpu.hard)); |
| 213 | child->thread.fpu.hard.fcr31 = 0; |
| 214 | } |
| 215 | #ifdef CONFIG_MIPS32 |
| 216 | /* |
| 217 | * The odd registers are actually the high order bits |
| 218 | * of the values stored in the even registers - unless |
| 219 | * we're using r2k_switch.S. |
| 220 | */ |
| 221 | if (addr & 1) { |
| 222 | fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff; |
| 223 | fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32; |
| 224 | } else { |
| 225 | fregs[addr - FPR_BASE] &= ~0xffffffffLL; |
| 226 | fregs[addr - FPR_BASE] |= data; |
| 227 | } |
| 228 | #endif |
| 229 | #ifdef CONFIG_MIPS64 |
| 230 | fregs[addr - FPR_BASE] = data; |
| 231 | #endif |
| 232 | break; |
| 233 | } |
| 234 | case PC: |
| 235 | regs->cp0_epc = data; |
| 236 | break; |
| 237 | case MMHI: |
| 238 | regs->hi = data; |
| 239 | break; |
| 240 | case MMLO: |
| 241 | regs->lo = data; |
| 242 | break; |
| 243 | case FPC_CSR: |
| 244 | if (cpu_has_fpu) |
| 245 | child->thread.fpu.hard.fcr31 = data; |
| 246 | else |
| 247 | child->thread.fpu.soft.fcr31 = data; |
| 248 | break; |
| 249 | default: |
| 250 | /* The rest are not allowed. */ |
| 251 | ret = -EIO; |
| 252 | break; |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
| 258 | case PTRACE_CONT: { /* restart after signal. */ |
| 259 | ret = -EIO; |
| 260 | if ((unsigned long) data > _NSIG) |
| 261 | break; |
| 262 | if (request == PTRACE_SYSCALL) { |
| 263 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 264 | } |
| 265 | else { |
| 266 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 267 | } |
| 268 | child->exit_code = data; |
| 269 | wake_up_process(child); |
| 270 | ret = 0; |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * make the child exit. Best I can do is send it a sigkill. |
| 276 | * perhaps it should be put in the status that it wants to |
| 277 | * exit. |
| 278 | */ |
| 279 | case PTRACE_KILL: |
| 280 | ret = 0; |
| 281 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 282 | break; |
| 283 | child->exit_code = SIGKILL; |
| 284 | wake_up_process(child); |
| 285 | break; |
| 286 | |
| 287 | case PTRACE_DETACH: /* detach a process that was attached. */ |
| 288 | ret = ptrace_detach(child, data); |
| 289 | break; |
| 290 | |
| 291 | default: |
| 292 | ret = ptrace_request(child, request, addr, data); |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | out_tsk: |
| 297 | put_task_struct(child); |
| 298 | out: |
| 299 | unlock_kernel(); |
| 300 | return ret; |
| 301 | } |
| 302 | |
Yoichi Yuasa | 67eb81e | 2005-04-29 16:13:35 +0100 | [diff] [blame^] | 303 | static inline int audit_arch(void) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 304 | { |
| 305 | #ifdef CONFIG_CPU_LITTLE_ENDIAN |
| 306 | #ifdef CONFIG_MIPS64 |
| 307 | if (!(current->thread.mflags & MF_32BIT_REGS)) |
| 308 | return AUDIT_ARCH_MIPSEL64; |
| 309 | #endif /* MIPS64 */ |
| 310 | return AUDIT_ARCH_MIPSEL; |
| 311 | |
| 312 | #else /* big endian... */ |
| 313 | #ifdef CONFIG_MIPS64 |
| 314 | if (!(current->thread.mflags & MF_32BIT_REGS)) |
| 315 | return AUDIT_ARCH_MIPS64; |
| 316 | #endif /* MIPS64 */ |
| 317 | return AUDIT_ARCH_MIPS; |
| 318 | |
| 319 | #endif /* endian */ |
| 320 | } |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | /* |
| 323 | * Notification of system call entry/exit |
| 324 | * - triggered by current->work.syscall_trace |
| 325 | */ |
| 326 | asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit) |
| 327 | { |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 328 | if (unlikely(current->audit_context) && entryexit) |
| 329 | audit_syscall_exit(current, AUDITSC_RESULT(regs->regs[2]), regs->regs[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
| 331 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 332 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | if (!(current->ptrace & PT_PTRACED)) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 334 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | /* The 0x80 provides a way for the tracing parent to distinguish |
| 337 | between a syscall stop and SIGTRAP delivery */ |
| 338 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? |
| 339 | 0x80 : 0)); |
| 340 | |
| 341 | /* |
| 342 | * this isn't the same as continuing with a signal, but it will do |
| 343 | * for normal use. strace only continues with a signal if the |
| 344 | * stopping signal is not SIGTRAP. -brl |
| 345 | */ |
| 346 | if (current->exit_code) { |
| 347 | send_sig(current->exit_code, current, 1); |
| 348 | current->exit_code = 0; |
| 349 | } |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 350 | out: |
| 351 | if (unlikely(current->audit_context) && !entryexit) |
| 352 | audit_syscall_entry(current, audit_arch(), regs->regs[2], |
| 353 | regs->regs[4], regs->regs[5], |
| 354 | regs->regs[6], regs->regs[7]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |