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> |
Paul Mundt | c4637d4 | 2008-07-30 15:30:52 +0900 | [diff] [blame] | 23 | #include <linux/seccomp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/uaccess.h> |
| 25 | #include <asm/pgtable.h> |
| 26 | #include <asm/system.h> |
| 27 | #include <asm/processor.h> |
| 28 | #include <asm/mmu_context.h> |
| 29 | |
| 30 | /* |
| 31 | * does not yet catch signals sent when the child dies. |
| 32 | * in exit.c or in signal.c. |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | * This routine will get a word off of the process kernel stack. |
| 37 | */ |
| 38 | static inline int get_stack_long(struct task_struct *task, int offset) |
| 39 | { |
| 40 | unsigned char *stack; |
| 41 | |
Al Viro | 3cf0f4e | 2006-01-12 01:05:44 -0800 | [diff] [blame] | 42 | stack = (unsigned char *)task_pt_regs(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | stack += offset; |
| 44 | return (*((int *)stack)); |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * This routine will put a word on the process kernel stack. |
| 49 | */ |
| 50 | static inline int put_stack_long(struct task_struct *task, int offset, |
| 51 | unsigned long data) |
| 52 | { |
| 53 | unsigned char *stack; |
| 54 | |
Al Viro | 3cf0f4e | 2006-01-12 01:05:44 -0800 | [diff] [blame] | 55 | stack = (unsigned char *)task_pt_regs(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | stack += offset; |
| 57 | *(unsigned long *) stack = data; |
| 58 | return 0; |
| 59 | } |
| 60 | |
Paul Mundt | c459dbf | 2008-07-30 19:09:31 +0900 | [diff] [blame^] | 61 | void user_enable_single_step(struct task_struct *child) |
| 62 | { |
| 63 | struct pt_regs *regs = task_pt_regs(child); |
| 64 | long pc; |
| 65 | |
| 66 | pc = get_stack_long(child, (long)®s->pc); |
| 67 | |
| 68 | /* Next scheduling will set up UBC */ |
| 69 | if (child->thread.ubc_pc == 0) |
| 70 | ubc_usercnt += 1; |
| 71 | |
| 72 | child->thread.ubc_pc = pc; |
| 73 | |
| 74 | set_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 75 | } |
| 76 | |
| 77 | void user_disable_single_step(struct task_struct *child) |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 78 | { |
| 79 | clear_tsk_thread_flag(child, TIF_SINGLESTEP); |
| 80 | |
| 81 | /* |
| 82 | * Ensure the UBC is not programmed at the next context switch. |
| 83 | * |
| 84 | * Normally this is not needed but there are sequences such as |
| 85 | * singlestep, signal delivery, and continue that leave the |
| 86 | * ubc_pc non-zero leading to spurious SIGTRAPs. |
| 87 | */ |
| 88 | if (child->thread.ubc_pc != 0) { |
| 89 | ubc_usercnt -= 1; |
| 90 | child->thread.ubc_pc = 0; |
| 91 | } |
| 92 | } |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* |
| 95 | * Called by kernel/ptrace.c when detaching.. |
| 96 | * |
| 97 | * Make sure single step bits etc are not set. |
| 98 | */ |
| 99 | void ptrace_disable(struct task_struct *child) |
| 100 | { |
Paul Mundt | c459dbf | 2008-07-30 19:09:31 +0900 | [diff] [blame^] | 101 | user_disable_single_step(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 104 | 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] | 105 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | struct user * dummy = NULL; |
| 107 | int ret; |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | switch (request) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* read the word at location addr in the USER area. */ |
| 111 | case PTRACE_PEEKUSR: { |
| 112 | unsigned long tmp; |
| 113 | |
| 114 | ret = -EIO; |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 115 | if ((addr & 3) || addr < 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | addr > sizeof(struct user) - 3) |
| 117 | break; |
| 118 | |
| 119 | if (addr < sizeof(struct pt_regs)) |
| 120 | tmp = get_stack_long(child, addr); |
| 121 | else if (addr >= (long) &dummy->fpu && |
| 122 | addr < (long) &dummy->u_fpvalid) { |
| 123 | if (!tsk_used_math(child)) { |
| 124 | if (addr == (long)&dummy->fpu.fpscr) |
| 125 | tmp = FPSCR_INIT; |
| 126 | else |
| 127 | tmp = 0; |
| 128 | } else |
| 129 | tmp = ((long *)&child->thread.fpu) |
| 130 | [(addr - (long)&dummy->fpu) >> 2]; |
| 131 | } else if (addr == (long) &dummy->u_fpvalid) |
| 132 | tmp = !!tsk_used_math(child); |
| 133 | else |
| 134 | tmp = 0; |
Paul Mundt | e08f457 | 2007-05-14 12:52:56 +0900 | [diff] [blame] | 135 | ret = put_user(tmp, (unsigned long __user *)data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ |
| 140 | ret = -EIO; |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 141 | if ((addr & 3) || addr < 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | addr > sizeof(struct user) - 3) |
| 143 | break; |
| 144 | |
| 145 | if (addr < sizeof(struct pt_regs)) |
| 146 | ret = put_stack_long(child, addr, data); |
| 147 | else if (addr >= (long) &dummy->fpu && |
| 148 | addr < (long) &dummy->u_fpvalid) { |
| 149 | set_stopped_child_used_math(child); |
| 150 | ((long *)&child->thread.fpu) |
| 151 | [(addr - (long)&dummy->fpu) >> 2] = data; |
| 152 | ret = 0; |
| 153 | } else if (addr == (long) &dummy->u_fpvalid) { |
| 154 | conditional_stopped_child_used_math(data, child); |
| 155 | ret = 0; |
| 156 | } |
| 157 | break; |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | #ifdef CONFIG_SH_DSP |
| 160 | case PTRACE_GETDSPREGS: { |
| 161 | unsigned long dp; |
| 162 | |
| 163 | ret = -EIO; |
| 164 | dp = ((unsigned long) child) + THREAD_SIZE - |
| 165 | sizeof(struct pt_dspregs); |
| 166 | if (*((int *) (dp - 4)) == SR_FD) { |
Magnus Damm | 0906185 | 2008-02-08 17:26:54 +0900 | [diff] [blame] | 167 | copy_to_user((void *)addr, (void *) dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | sizeof(struct pt_dspregs)); |
| 169 | ret = 0; |
| 170 | } |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | case PTRACE_SETDSPREGS: { |
| 175 | unsigned long dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | ret = -EIO; |
| 178 | dp = ((unsigned long) child) + THREAD_SIZE - |
| 179 | sizeof(struct pt_dspregs); |
| 180 | if (*((int *) (dp - 4)) == SR_FD) { |
Magnus Damm | 0906185 | 2008-02-08 17:26:54 +0900 | [diff] [blame] | 181 | copy_from_user((void *) dp, (void *)addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | sizeof(struct pt_dspregs)); |
| 183 | ret = 0; |
| 184 | } |
| 185 | break; |
| 186 | } |
| 187 | #endif |
Paul Mundt | 3bc24a1 | 2008-05-19 13:40:12 +0900 | [diff] [blame] | 188 | #ifdef CONFIG_BINFMT_ELF_FDPIC |
| 189 | case PTRACE_GETFDPIC: { |
| 190 | unsigned long tmp = 0; |
| 191 | |
| 192 | switch (addr) { |
| 193 | case PTRACE_GETFDPIC_EXEC: |
| 194 | tmp = child->mm->context.exec_fdpic_loadmap; |
| 195 | break; |
| 196 | case PTRACE_GETFDPIC_INTERP: |
| 197 | tmp = child->mm->context.interp_fdpic_loadmap; |
| 198 | break; |
| 199 | default: |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | ret = 0; |
| 204 | if (put_user(tmp, (unsigned long *) data)) { |
| 205 | ret = -EFAULT; |
| 206 | break; |
| 207 | } |
| 208 | break; |
| 209 | } |
| 210 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | default: |
| 212 | ret = ptrace_request(child, request, addr, data); |
| 213 | break; |
| 214 | } |
Christoph Hellwig | 481bed4 | 2005-11-07 00:59:47 -0800 | [diff] [blame] | 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return ret; |
| 217 | } |
| 218 | |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 219 | asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | struct task_struct *tsk = current; |
| 222 | |
Paul Mundt | c4637d4 | 2008-07-30 15:30:52 +0900 | [diff] [blame] | 223 | secure_computing(regs->regs[0]); |
| 224 | |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 225 | if (unlikely(current->audit_context) && entryexit) |
| 226 | audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]), |
| 227 | regs->regs[0]); |
| 228 | |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 229 | if (!test_thread_flag(TIF_SYSCALL_TRACE) && |
| 230 | !test_thread_flag(TIF_SINGLESTEP)) |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 231 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | if (!(tsk->ptrace & PT_PTRACED)) |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 233 | goto out; |
| 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 236 | between a syscall stop and SIGTRAP delivery */ |
Stuart Menefy | 9432f96 | 2007-02-23 13:22:17 +0900 | [diff] [blame] | 237 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) && |
| 238 | !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * this isn't the same as continuing with a signal, but it will do |
| 242 | * for normal use. strace only continues with a signal if the |
| 243 | * stopping signal is not SIGTRAP. -brl |
| 244 | */ |
| 245 | if (tsk->exit_code) { |
| 246 | send_sig(tsk->exit_code, tsk, 1); |
| 247 | tsk->exit_code = 0; |
| 248 | } |
Yuichi Nakamura | 1322b9d | 2007-11-10 19:21:34 +0900 | [diff] [blame] | 249 | |
| 250 | out: |
| 251 | if (unlikely(current->audit_context) && !entryexit) |
| 252 | audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[3], |
| 253 | regs->regs[4], regs->regs[5], |
| 254 | regs->regs[6], regs->regs[7]); |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |