Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/sh/kernel/ptrace.c |
| 3 | * |
| 4 | * Original x86 implementation: |
| 5 | * By Ross Biro 1/23/92 |
| 6 | * edited by Linus Torvalds |
| 7 | * |
| 8 | * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 9 | * Audit support: Yuichi Nakamura <ynakam@hitachisoft.jp> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/errno.h> |
| 16 | #include <linux/ptrace.h> |
| 17 | #include <linux/user.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/security.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 20 | #include <linux/signal.h> |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 21 | #include <linux/io.h> |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 22 | #include <linux/audit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/uaccess.h> |
| 24 | #include <asm/pgtable.h> |
| 25 | #include <asm/system.h> |
| 26 | #include <asm/processor.h> |
| 27 | #include <asm/mmu_context.h> |
| 28 | |
| 29 | /* |
| 30 | * does not yet catch signals sent when the child dies. |
| 31 | * in exit.c or in signal.c. |
| 32 | */ |
| 33 | |
| 34 | /* |
| 35 | * This routine will get a word off of the process kernel stack. |
| 36 | */ |
| 37 | static inline int get_stack_long(struct task_struct *task, int offset) |
| 38 | { |
| 39 | unsigned char *stack; |
| 40 | |
Al Viro | 3cf0f4e | 2006-01-12 01:05:44 -0800 | [diff] [blame] | 41 | stack = (unsigned char *)task_pt_regs(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | stack += offset; |
| 43 | return (*((int *)stack)); |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * This routine will put a word on the process kernel stack. |
| 48 | */ |
| 49 | static inline int put_stack_long(struct task_struct *task, int offset, |
| 50 | unsigned long data) |
| 51 | { |
| 52 | unsigned char *stack; |
| 53 | |
Al Viro | 3cf0f4e | 2006-01-12 01:05:44 -0800 | [diff] [blame] | 54 | stack = (unsigned char *)task_pt_regs(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | stack += offset; |
| 56 | *(unsigned long *) stack = data; |
| 57 | return 0; |
| 58 | } |
| 59 | |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 60 | static void ptrace_disable_singlestep(struct task_struct *child) |
| 61 | { |
| 62 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 63 | |
| 64 | /* |
| 65 | * Ensure the UBC is not programmed at the next context switch. |
| 66 | * |
| 67 | * Normally this is not needed but there are sequences such as |
| 68 | * singlestep, signal delivery, and continue that leave the |
| 69 | * ubc_pc non-zero leading to spurious SIGTRAPs. |
| 70 | */ |
| 71 | if (child->thread.ubc_pc != 0) { |
| 72 | ubc_usercnt -= 1; |
| 73 | child->thread.ubc_pc = 0; |
| 74 | } |
| 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* |
| 78 | * Called by kernel/ptrace.c when detaching.. |
| 79 | * |
| 80 | * Make sure single step bits etc are not set. |
| 81 | */ |
| 82 | void ptrace_disable(struct task_struct *child) |
| 83 | { |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 84 | ptrace_disable_singlestep(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 87 | 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] | 88 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | struct user * dummy = NULL; |
| 90 | int ret; |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | switch (request) { |
| 93 | /* when I and D space are separate, these will need to be fixed. */ |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 94 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
Alexey Dobriyan | 7664732 | 2007-07-17 04:03:43 -0700 | [diff] [blame] | 95 | case PTRACE_PEEKDATA: |
| 96 | ret = generic_ptrace_peekdata(child, addr, data); |
Paul Mundt | 662ae21 | 2007-08-04 13:39:21 +0900 | [diff] [blame] | 97 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | /* read the word at location addr in the USER area. */ |
| 100 | case PTRACE_PEEKUSR: { |
| 101 | unsigned long tmp; |
| 102 | |
| 103 | ret = -EIO; |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 104 | if ((addr & 3) || addr < 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | addr > sizeof(struct user) - 3) |
| 106 | break; |
| 107 | |
| 108 | if (addr < sizeof(struct pt_regs)) |
| 109 | tmp = get_stack_long(child, addr); |
| 110 | else if (addr >= (long) &dummy->fpu && |
| 111 | addr < (long) &dummy->u_fpvalid) { |
| 112 | if (!tsk_used_math(child)) { |
| 113 | if (addr == (long)&dummy->fpu.fpscr) |
| 114 | tmp = FPSCR_INIT; |
| 115 | else |
| 116 | tmp = 0; |
| 117 | } else |
| 118 | tmp = ((long *)&child->thread.fpu) |
| 119 | [(addr - (long)&dummy->fpu) >> 2]; |
| 120 | } else if (addr == (long) &dummy->u_fpvalid) |
| 121 | tmp = !!tsk_used_math(child); |
| 122 | else |
| 123 | tmp = 0; |
Paul Mundt | e08f457 | 2007-05-14 12:52:56 +0900 | [diff] [blame] | 124 | ret = put_user(tmp, (unsigned long __user *)data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | |
| 128 | /* when I and D space are separate, this will have to be fixed. */ |
| 129 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 130 | case PTRACE_POKEDATA: |
Alexey Dobriyan | f284ce7 | 2007-07-17 04:03:44 -0700 | [diff] [blame] | 131 | ret = generic_ptrace_pokedata(child, addr, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | break; |
| 133 | |
| 134 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
| 135 | ret = -EIO; |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 136 | if ((addr & 3) || addr < 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | addr > sizeof(struct user) - 3) |
| 138 | break; |
| 139 | |
| 140 | if (addr < sizeof(struct pt_regs)) |
| 141 | ret = put_stack_long(child, addr, data); |
| 142 | else if (addr >= (long) &dummy->fpu && |
| 143 | addr < (long) &dummy->u_fpvalid) { |
| 144 | set_stopped_child_used_math(child); |
| 145 | ((long *)&child->thread.fpu) |
| 146 | [(addr - (long)&dummy->fpu) >> 2] = data; |
| 147 | ret = 0; |
| 148 | } else if (addr == (long) &dummy->u_fpvalid) { |
| 149 | conditional_stopped_child_used_math(data, child); |
| 150 | ret = 0; |
| 151 | } |
| 152 | break; |
| 153 | |
| 154 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
| 155 | case PTRACE_CONT: { /* restart after signal. */ |
| 156 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 157 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | break; |
| 159 | if (request == PTRACE_SYSCALL) |
| 160 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 161 | else |
| 162 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 163 | |
| 164 | ptrace_disable_singlestep(child); |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | child->exit_code = data; |
| 167 | wake_up_process(child); |
| 168 | ret = 0; |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | /* |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 173 | * make the child exit. Best I can do is send it a sigkill. |
| 174 | * perhaps it should be put in the status that it wants to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | * exit. |
| 176 | */ |
| 177 | case PTRACE_KILL: { |
| 178 | ret = 0; |
| 179 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 180 | break; |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 181 | ptrace_disable_singlestep(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | child->exit_code = SIGKILL; |
| 183 | wake_up_process(child); |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | case PTRACE_SINGLESTEP: { /* set the trap flag. */ |
| 188 | long pc; |
Paul Mundt | e08f457 | 2007-05-14 12:52:56 +0900 | [diff] [blame] | 189 | struct pt_regs *regs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
| 191 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 192 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | break; |
| 194 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 195 | if ((child->ptrace & PT_DTRACE) == 0) { |
| 196 | /* Spurious delayed TF traps may occur */ |
| 197 | child->ptrace |= PT_DTRACE; |
| 198 | } |
| 199 | |
Paul Mundt | e08f457 | 2007-05-14 12:52:56 +0900 | [diff] [blame] | 200 | pc = get_stack_long(child, (long)®s->pc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | /* Next scheduling will set up UBC */ |
| 203 | if (child->thread.ubc_pc == 0) |
| 204 | ubc_usercnt += 1; |
| 205 | child->thread.ubc_pc = pc; |
| 206 | |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 207 | set_tsk_thread_flag(child, TIF_SINGLESTEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | child->exit_code = data; |
| 209 | /* give it a chance to run. */ |
| 210 | wake_up_process(child); |
| 211 | ret = 0; |
| 212 | break; |
| 213 | } |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | #ifdef CONFIG_SH_DSP |
| 216 | case PTRACE_GETDSPREGS: { |
| 217 | unsigned long dp; |
| 218 | |
| 219 | ret = -EIO; |
| 220 | dp = ((unsigned long) child) + THREAD_SIZE - |
| 221 | sizeof(struct pt_dspregs); |
| 222 | if (*((int *) (dp - 4)) == SR_FD) { |
Magnus Damm | 0906185 | 2008-02-08 17:26:54 +0900 | [diff] [blame] | 223 | copy_to_user((void *)addr, (void *) dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | sizeof(struct pt_dspregs)); |
| 225 | ret = 0; |
| 226 | } |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | case PTRACE_SETDSPREGS: { |
| 231 | unsigned long dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | ret = -EIO; |
| 234 | dp = ((unsigned long) child) + THREAD_SIZE - |
| 235 | sizeof(struct pt_dspregs); |
| 236 | if (*((int *) (dp - 4)) == SR_FD) { |
Magnus Damm | 0906185 | 2008-02-08 17:26:54 +0900 | [diff] [blame] | 237 | copy_from_user((void *) dp, (void *)addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | sizeof(struct pt_dspregs)); |
| 239 | ret = 0; |
| 240 | } |
| 241 | break; |
| 242 | } |
| 243 | #endif |
Paul Mundt | 3bc24a1 | 2008-05-19 13:40:12 +0900 | [diff] [blame] | 244 | #ifdef CONFIG_BINFMT_ELF_FDPIC |
| 245 | case PTRACE_GETFDPIC: { |
| 246 | unsigned long tmp = 0; |
| 247 | |
| 248 | switch (addr) { |
| 249 | case PTRACE_GETFDPIC_EXEC: |
| 250 | tmp = child->mm->context.exec_fdpic_loadmap; |
| 251 | break; |
| 252 | case PTRACE_GETFDPIC_INTERP: |
| 253 | tmp = child->mm->context.interp_fdpic_loadmap; |
| 254 | break; |
| 255 | default: |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | ret = 0; |
| 260 | if (put_user(tmp, (unsigned long *) data)) { |
| 261 | ret = -EFAULT; |
| 262 | break; |
| 263 | } |
| 264 | break; |
| 265 | } |
| 266 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | default: |
| 268 | ret = ptrace_request(child, request, addr, data); |
| 269 | break; |
| 270 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | return ret; |
| 273 | } |
| 274 | |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 275 | asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | struct task_struct *tsk = current; |
| 278 | |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 279 | if (unlikely(current->audit_context) && entryexit) |
| 280 | audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]), |
| 281 | regs->regs[0]); |
| 282 | |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 283 | if (!test_thread_flag(TIF_SYSCALL_TRACE) && |
| 284 | !test_thread_flag(TIF_SINGLESTEP)) |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 285 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (!(tsk->ptrace & PT_PTRACED)) |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 287 | goto out; |
| 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 290 | between a syscall stop and SIGTRAP delivery */ |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 291 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) && |
| 292 | !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | /* |
| 295 | * this isn't the same as continuing with a signal, but it will do |
| 296 | * for normal use. strace only continues with a signal if the |
| 297 | * stopping signal is not SIGTRAP. -brl |
| 298 | */ |
| 299 | if (tsk->exit_code) { |
| 300 | send_sig(tsk->exit_code, tsk, 1); |
| 301 | tsk->exit_code = 0; |
| 302 | } |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 303 | |
| 304 | out: |
| 305 | if (unlikely(current->audit_context) && !entryexit) |
| 306 | audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[3], |
| 307 | regs->regs[4], regs->regs[5], |
| 308 | regs->regs[6], regs->regs[7]); |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |