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