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