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 9 * 10 */ 11 12 #include <linux/config.h> 13 #include <linux/kernel.h> 14 #include <linux/sched.h> 15 #include <linux/mm.h> 16 #include <linux/smp.h> 17 #include <linux/smp_lock.h> 18 #include <linux/errno.h> 19 #include <linux/ptrace.h> 20 #include <linux/user.h> 21 #include <linux/slab.h> 22 #include <linux/security.h> 23 #include <linux/signal.h> 24 25 #include <asm/io.h> 26 #include <asm/uaccess.h> 27 #include <asm/pgtable.h> 28 #include <asm/system.h> 29 #include <asm/processor.h> 30 #include <asm/mmu_context.h> 31 32 /* 33 * does not yet catch signals sent when the child dies. 34 * in exit.c or in signal.c. 35 */ 36 37 /* 38 * This routine will get a word off of the process kernel stack. 39 */ 40 static inline int get_stack_long(struct task_struct *task, int offset) 41 { 42 unsigned char *stack; 43 44 stack = (unsigned char *) 45 task->thread_info + THREAD_SIZE - sizeof(struct pt_regs) 46 #ifdef CONFIG_SH_DSP 47 - sizeof(struct pt_dspregs) 48 #endif 49 - sizeof(unsigned long); 50 stack += offset; 51 return (*((int *)stack)); 52 } 53 54 /* 55 * This routine will put a word on the process kernel stack. 56 */ 57 static inline int put_stack_long(struct task_struct *task, int offset, 58 unsigned long data) 59 { 60 unsigned char *stack; 61 62 stack = (unsigned char *) 63 task->thread_info + THREAD_SIZE - sizeof(struct pt_regs) 64 #ifdef CONFIG_SH_DSP 65 - sizeof(struct pt_dspregs) 66 #endif 67 - sizeof(unsigned long); 68 stack += offset; 69 *(unsigned long *) stack = data; 70 return 0; 71 } 72 73 /* 74 * Called by kernel/ptrace.c when detaching.. 75 * 76 * Make sure single step bits etc are not set. 77 */ 78 void ptrace_disable(struct task_struct *child) 79 { 80 /* nothing to do.. */ 81 } 82 83 asmlinkage long sys_ptrace(long request, long pid, long addr, long data) 84 { 85 struct task_struct *child; 86 struct user * dummy = NULL; 87 int ret; 88 89 lock_kernel(); 90 ret = -EPERM; 91 if (request == PTRACE_TRACEME) { 92 /* are we already being traced? */ 93 if (current->ptrace & PT_PTRACED) 94 goto out; 95 ret = security_ptrace(current->parent, current); 96 if (ret) 97 goto out; 98 /* set the ptrace bit in the process flags. */ 99 current->ptrace |= PT_PTRACED; 100 ret = 0; 101 goto out; 102 } 103 ret = -ESRCH; 104 read_lock(&tasklist_lock); 105 child = find_task_by_pid(pid); 106 if (child) 107 get_task_struct(child); 108 read_unlock(&tasklist_lock); 109 if (!child) 110 goto out; 111 112 ret = -EPERM; 113 if (pid == 1) /* you may not mess with init */ 114 goto out_tsk; 115 116 if (request == PTRACE_ATTACH) { 117 ret = ptrace_attach(child); 118 goto out_tsk; 119 } 120 121 ret = ptrace_check_attach(child, request == PTRACE_KILL); 122 if (ret < 0) 123 goto out_tsk; 124 125 switch (request) { 126 /* when I and D space are separate, these will need to be fixed. */ 127 case PTRACE_PEEKTEXT: /* read word at location addr. */ 128 case PTRACE_PEEKDATA: { 129 unsigned long tmp; 130 int copied; 131 132 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); 133 ret = -EIO; 134 if (copied != sizeof(tmp)) 135 break; 136 ret = put_user(tmp,(unsigned long *) data); 137 break; 138 } 139 140 /* read the word at location addr in the USER area. */ 141 case PTRACE_PEEKUSR: { 142 unsigned long tmp; 143 144 ret = -EIO; 145 if ((addr & 3) || addr < 0 || 146 addr > sizeof(struct user) - 3) 147 break; 148 149 if (addr < sizeof(struct pt_regs)) 150 tmp = get_stack_long(child, addr); 151 else if (addr >= (long) &dummy->fpu && 152 addr < (long) &dummy->u_fpvalid) { 153 if (!tsk_used_math(child)) { 154 if (addr == (long)&dummy->fpu.fpscr) 155 tmp = FPSCR_INIT; 156 else 157 tmp = 0; 158 } else 159 tmp = ((long *)&child->thread.fpu) 160 [(addr - (long)&dummy->fpu) >> 2]; 161 } else if (addr == (long) &dummy->u_fpvalid) 162 tmp = !!tsk_used_math(child); 163 else 164 tmp = 0; 165 ret = put_user(tmp, (unsigned long *)data); 166 break; 167 } 168 169 /* when I and D space are separate, this will have to be fixed. */ 170 case PTRACE_POKETEXT: /* write the word at location addr. */ 171 case PTRACE_POKEDATA: 172 ret = 0; 173 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data)) 174 break; 175 ret = -EIO; 176 break; 177 178 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ 179 ret = -EIO; 180 if ((addr & 3) || addr < 0 || 181 addr > sizeof(struct user) - 3) 182 break; 183 184 if (addr < sizeof(struct pt_regs)) 185 ret = put_stack_long(child, addr, data); 186 else if (addr >= (long) &dummy->fpu && 187 addr < (long) &dummy->u_fpvalid) { 188 set_stopped_child_used_math(child); 189 ((long *)&child->thread.fpu) 190 [(addr - (long)&dummy->fpu) >> 2] = data; 191 ret = 0; 192 } else if (addr == (long) &dummy->u_fpvalid) { 193 conditional_stopped_child_used_math(data, child); 194 ret = 0; 195 } 196 break; 197 198 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ 199 case PTRACE_CONT: { /* restart after signal. */ 200 ret = -EIO; 201 if (!valid_signal(data)) 202 break; 203 if (request == PTRACE_SYSCALL) 204 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 205 else 206 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 207 child->exit_code = data; 208 wake_up_process(child); 209 ret = 0; 210 break; 211 } 212 213 /* 214 * make the child exit. Best I can do is send it a sigkill. 215 * perhaps it should be put in the status that it wants to 216 * exit. 217 */ 218 case PTRACE_KILL: { 219 ret = 0; 220 if (child->exit_state == EXIT_ZOMBIE) /* already dead */ 221 break; 222 child->exit_code = SIGKILL; 223 wake_up_process(child); 224 break; 225 } 226 227 case PTRACE_SINGLESTEP: { /* set the trap flag. */ 228 long pc; 229 struct pt_regs *dummy = NULL; 230 231 ret = -EIO; 232 if (!valid_signal(data)) 233 break; 234 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 235 if ((child->ptrace & PT_DTRACE) == 0) { 236 /* Spurious delayed TF traps may occur */ 237 child->ptrace |= PT_DTRACE; 238 } 239 240 pc = get_stack_long(child, (long)&dummy->pc); 241 242 /* Next scheduling will set up UBC */ 243 if (child->thread.ubc_pc == 0) 244 ubc_usercnt += 1; 245 child->thread.ubc_pc = pc; 246 247 child->exit_code = data; 248 /* give it a chance to run. */ 249 wake_up_process(child); 250 ret = 0; 251 break; 252 } 253 254 case PTRACE_DETACH: /* detach a process that was attached. */ 255 ret = ptrace_detach(child, data); 256 break; 257 258 #ifdef CONFIG_SH_DSP 259 case PTRACE_GETDSPREGS: { 260 unsigned long dp; 261 262 ret = -EIO; 263 dp = ((unsigned long) child) + THREAD_SIZE - 264 sizeof(struct pt_dspregs); 265 if (*((int *) (dp - 4)) == SR_FD) { 266 copy_to_user(addr, (void *) dp, 267 sizeof(struct pt_dspregs)); 268 ret = 0; 269 } 270 break; 271 } 272 273 case PTRACE_SETDSPREGS: { 274 unsigned long dp; 275 int i; 276 277 ret = -EIO; 278 dp = ((unsigned long) child) + THREAD_SIZE - 279 sizeof(struct pt_dspregs); 280 if (*((int *) (dp - 4)) == SR_FD) { 281 copy_from_user((void *) dp, addr, 282 sizeof(struct pt_dspregs)); 283 ret = 0; 284 } 285 break; 286 } 287 #endif 288 default: 289 ret = ptrace_request(child, request, addr, data); 290 break; 291 } 292 out_tsk: 293 put_task_struct(child); 294 out: 295 unlock_kernel(); 296 return ret; 297 } 298 299 asmlinkage void do_syscall_trace(void) 300 { 301 struct task_struct *tsk = current; 302 303 if (!test_thread_flag(TIF_SYSCALL_TRACE)) 304 return; 305 if (!(tsk->ptrace & PT_PTRACED)) 306 return; 307 /* the 0x80 provides a way for the tracing parent to distinguish 308 between a syscall stop and SIGTRAP delivery */ 309 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) 310 ? 0x80 : 0)); 311 312 /* 313 * this isn't the same as continuing with a signal, but it will do 314 * for normal use. strace only continues with a signal if the 315 * stopping signal is not SIGTRAP. -brl 316 */ 317 if (tsk->exit_code) { 318 send_sig(tsk->exit_code, tsk, 1); 319 tsk->exit_code = 0; 320 } 321 } 322