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