| 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 |  * arch/sh64/kernel/ptrace.c | 
 | 7 |  * | 
 | 8 |  * Copyright (C) 2000, 2001  Paolo Alberelli | 
 | 9 |  * Copyright (C) 2003  Paul Mundt | 
 | 10 |  * | 
 | 11 |  * Started from SH3/4 version: | 
 | 12 |  *   SuperH version:   Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka | 
 | 13 |  * | 
 | 14 |  *   Original x86 implementation: | 
 | 15 |  *	By Ross Biro 1/23/92 | 
 | 16 |  *	edited by Linus Torvalds | 
 | 17 |  * | 
 | 18 |  */ | 
 | 19 |  | 
 | 20 | #include <linux/config.h> | 
 | 21 | #include <linux/kernel.h> | 
 | 22 | #include <linux/rwsem.h> | 
 | 23 | #include <linux/sched.h> | 
 | 24 | #include <linux/mm.h> | 
 | 25 | #include <linux/smp.h> | 
 | 26 | #include <linux/smp_lock.h> | 
 | 27 | #include <linux/errno.h> | 
 | 28 | #include <linux/ptrace.h> | 
 | 29 | #include <linux/user.h> | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 30 | #include <linux/signal.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 |  | 
 | 32 | #include <asm/io.h> | 
 | 33 | #include <asm/uaccess.h> | 
 | 34 | #include <asm/pgtable.h> | 
 | 35 | #include <asm/system.h> | 
 | 36 | #include <asm/processor.h> | 
 | 37 | #include <asm/mmu_context.h> | 
 | 38 |  | 
 | 39 | /* This mask defines the bits of the SR which the user is not allowed to | 
 | 40 |    change, which are everything except S, Q, M, PR, SZ, FR. */ | 
 | 41 | #define SR_MASK      (0xffff8cfd) | 
 | 42 |  | 
 | 43 | /* | 
 | 44 |  * does not yet catch signals sent when the child dies. | 
 | 45 |  * in exit.c or in signal.c. | 
 | 46 |  */ | 
 | 47 |  | 
 | 48 | /* | 
 | 49 |  * This routine will get a word from the user area in the process kernel stack. | 
 | 50 |  */ | 
 | 51 | static inline int get_stack_long(struct task_struct *task, int offset) | 
 | 52 | { | 
 | 53 | 	unsigned char *stack; | 
 | 54 |  | 
 | 55 | 	stack = (unsigned char *)(task->thread.uregs); | 
 | 56 | 	stack += offset; | 
 | 57 | 	return (*((int *)stack)); | 
 | 58 | } | 
 | 59 |  | 
 | 60 | static inline unsigned long | 
 | 61 | get_fpu_long(struct task_struct *task, unsigned long addr) | 
 | 62 | { | 
 | 63 | 	unsigned long tmp; | 
 | 64 | 	struct pt_regs *regs; | 
 | 65 | 	regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1; | 
 | 66 |  | 
 | 67 | 	if (!tsk_used_math(task)) { | 
 | 68 | 		if (addr == offsetof(struct user_fpu_struct, fpscr)) { | 
 | 69 | 			tmp = FPSCR_INIT; | 
 | 70 | 		} else { | 
 | 71 | 			tmp = 0xffffffffUL; /* matches initial value in fpu.c */ | 
 | 72 | 		} | 
 | 73 | 		return tmp; | 
 | 74 | 	} | 
 | 75 |  | 
 | 76 | 	if (last_task_used_math == task) { | 
 | 77 | 		grab_fpu(); | 
 | 78 | 		fpsave(&task->thread.fpu.hard); | 
 | 79 | 		release_fpu(); | 
 | 80 | 		last_task_used_math = 0; | 
 | 81 | 		regs->sr |= SR_FD; | 
 | 82 | 	} | 
 | 83 |  | 
 | 84 | 	tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)]; | 
 | 85 | 	return tmp; | 
 | 86 | } | 
 | 87 |  | 
 | 88 | /* | 
 | 89 |  * This routine will put a word into the user area in the process kernel stack. | 
 | 90 |  */ | 
 | 91 | static inline int put_stack_long(struct task_struct *task, int offset, | 
 | 92 | 				 unsigned long data) | 
 | 93 | { | 
 | 94 | 	unsigned char *stack; | 
 | 95 |  | 
 | 96 | 	stack = (unsigned char *)(task->thread.uregs); | 
 | 97 | 	stack += offset; | 
 | 98 | 	*(unsigned long *) stack = data; | 
 | 99 | 	return 0; | 
 | 100 | } | 
 | 101 |  | 
 | 102 | static inline int | 
 | 103 | put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data) | 
 | 104 | { | 
 | 105 | 	struct pt_regs *regs; | 
 | 106 |  | 
 | 107 | 	regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1; | 
 | 108 |  | 
 | 109 | 	if (!tsk_used_math(task)) { | 
 | 110 | 		fpinit(&task->thread.fpu.hard); | 
 | 111 | 		set_stopped_child_used_math(task); | 
 | 112 | 	} else if (last_task_used_math == task) { | 
 | 113 | 		grab_fpu(); | 
 | 114 | 		fpsave(&task->thread.fpu.hard); | 
 | 115 | 		release_fpu(); | 
 | 116 | 		last_task_used_math = 0; | 
 | 117 | 		regs->sr |= SR_FD; | 
 | 118 | 	} | 
 | 119 |  | 
 | 120 | 	((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data; | 
 | 121 | 	return 0; | 
 | 122 | } | 
 | 123 |  | 
 | 124 | asmlinkage int sys_ptrace(long request, long pid, long addr, long data) | 
 | 125 | { | 
 | 126 | 	struct task_struct *child; | 
 | 127 | 	extern void poke_real_address_q(unsigned long long addr, unsigned long long data); | 
 | 128 | #define WPC_DBRMODE 0x0d104008 | 
 | 129 | 	static int first_call = 1; | 
 | 130 | 	int ret; | 
 | 131 |  | 
 | 132 | 	lock_kernel(); | 
 | 133 |  | 
 | 134 | 	if (first_call) { | 
 | 135 | 		/* Set WPC.DBRMODE to 0.  This makes all debug events get | 
 | 136 | 		 * delivered through RESVEC, i.e. into the handlers in entry.S. | 
 | 137 | 		 * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE | 
 | 138 | 		 * would normally be left set to 1, which makes debug events get | 
 | 139 | 		 * delivered through DBRVEC, i.e. into the remote gdb's | 
 | 140 | 		 * handlers.  This prevents ptrace getting them, and confuses | 
 | 141 | 		 * the remote gdb.) */ | 
 | 142 | 		printk("DBRMODE set to 0 to permit native debugging\n"); | 
 | 143 | 		poke_real_address_q(WPC_DBRMODE, 0); | 
 | 144 | 		first_call = 0; | 
 | 145 | 	} | 
 | 146 |  | 
 | 147 | 	ret = -EPERM; | 
 | 148 | 	if (request == PTRACE_TRACEME) { | 
 | 149 | 		/* are we already being traced? */ | 
 | 150 | 		if (current->ptrace & PT_PTRACED) | 
 | 151 | 			goto out; | 
 | 152 | 		/* set the ptrace bit in the process flags. */ | 
 | 153 | 		current->ptrace |= PT_PTRACED; | 
 | 154 | 		ret = 0; | 
 | 155 | 		goto out; | 
 | 156 | 	} | 
 | 157 | 	ret = -ESRCH; | 
 | 158 | 	read_lock(&tasklist_lock); | 
 | 159 | 	child = find_task_by_pid(pid); | 
 | 160 | 	if (child) | 
 | 161 | 		get_task_struct(child); | 
 | 162 | 	read_unlock(&tasklist_lock); | 
 | 163 | 	if (!child) | 
 | 164 | 		goto out; | 
 | 165 |  | 
 | 166 | 	ret = -EPERM; | 
 | 167 | 	if (pid == 1)		/* you may not mess with init */ | 
 | 168 | 		goto out_tsk; | 
 | 169 |  | 
 | 170 | 	if (request == PTRACE_ATTACH) { | 
 | 171 | 		ret = ptrace_attach(child); | 
 | 172 | 			goto out_tsk; | 
 | 173 | 		} | 
 | 174 |  | 
 | 175 | 	ret = ptrace_check_attach(child, request == PTRACE_KILL); | 
 | 176 | 	if (ret < 0) | 
 | 177 | 		goto out_tsk; | 
 | 178 |  | 
 | 179 | 	switch (request) { | 
 | 180 | 	/* when I and D space are separate, these will need to be fixed. */ | 
 | 181 | 	case PTRACE_PEEKTEXT: /* read word at location addr. */ | 
 | 182 | 	case PTRACE_PEEKDATA: { | 
 | 183 | 		unsigned long tmp; | 
 | 184 | 		int copied; | 
 | 185 |  | 
 | 186 | 		copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); | 
 | 187 | 		ret = -EIO; | 
 | 188 | 		if (copied != sizeof(tmp)) | 
 | 189 | 			break; | 
 | 190 | 		ret = put_user(tmp,(unsigned long *) data); | 
 | 191 | 		break; | 
 | 192 | 	} | 
 | 193 |  | 
 | 194 | 	/* read the word at location addr in the USER area. */ | 
 | 195 | 	case PTRACE_PEEKUSR: { | 
 | 196 | 		unsigned long tmp; | 
 | 197 |  | 
 | 198 | 		ret = -EIO; | 
 | 199 | 		if ((addr & 3) || addr < 0) | 
 | 200 | 			break; | 
 | 201 |  | 
 | 202 | 		if (addr < sizeof(struct pt_regs)) | 
 | 203 | 			tmp = get_stack_long(child, addr); | 
 | 204 | 		else if ((addr >= offsetof(struct user, fpu)) && | 
 | 205 | 			 (addr <  offsetof(struct user, u_fpvalid))) { | 
 | 206 | 			tmp = get_fpu_long(child, addr - offsetof(struct user, fpu)); | 
 | 207 | 		} else if (addr == offsetof(struct user, u_fpvalid)) { | 
 | 208 | 			tmp = !!tsk_used_math(child); | 
 | 209 | 		} else { | 
 | 210 | 			break; | 
 | 211 | 		} | 
 | 212 | 		ret = put_user(tmp, (unsigned long *)data); | 
 | 213 | 		break; | 
 | 214 | 	} | 
 | 215 |  | 
 | 216 | 	/* when I and D space are separate, this will have to be fixed. */ | 
 | 217 | 	case PTRACE_POKETEXT: /* write the word at location addr. */ | 
 | 218 | 	case PTRACE_POKEDATA: | 
 | 219 | 		ret = 0; | 
 | 220 | 		if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data)) | 
 | 221 | 			break; | 
 | 222 | 		ret = -EIO; | 
 | 223 | 		break; | 
 | 224 |  | 
 | 225 | 	case PTRACE_POKEUSR: | 
 | 226 |                 /* write the word at location addr in the USER area. We must | 
 | 227 |                    disallow any changes to certain SR bits or u_fpvalid, since | 
 | 228 |                    this could crash the kernel or result in a security | 
 | 229 |                    loophole. */ | 
 | 230 | 		ret = -EIO; | 
 | 231 | 		if ((addr & 3) || addr < 0) | 
 | 232 | 			break; | 
 | 233 |  | 
 | 234 | 		if (addr < sizeof(struct pt_regs)) { | 
 | 235 | 			/* Ignore change of top 32 bits of SR */ | 
 | 236 | 			if (addr == offsetof (struct pt_regs, sr)+4) | 
 | 237 | 			{ | 
 | 238 | 				ret = 0; | 
 | 239 | 				break; | 
 | 240 | 			} | 
 | 241 | 			/* If lower 32 bits of SR, ignore non-user bits */ | 
 | 242 | 			if (addr == offsetof (struct pt_regs, sr)) | 
 | 243 | 			{ | 
 | 244 | 				long cursr = get_stack_long(child, addr); | 
 | 245 | 				data &= ~(SR_MASK); | 
 | 246 | 				data |= (cursr & SR_MASK); | 
 | 247 | 			} | 
 | 248 | 			ret = put_stack_long(child, addr, data); | 
 | 249 | 		} | 
 | 250 | 		else if ((addr >= offsetof(struct user, fpu)) && | 
 | 251 | 			 (addr <  offsetof(struct user, u_fpvalid))) { | 
 | 252 | 			ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data); | 
 | 253 | 		} | 
 | 254 | 		break; | 
 | 255 |  | 
 | 256 | 	case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ | 
 | 257 | 	case PTRACE_CONT: { /* restart after signal. */ | 
 | 258 | 		ret = -EIO; | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 259 | 		if (!valid_signal(data)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | 			break; | 
 | 261 | 		if (request == PTRACE_SYSCALL) | 
 | 262 | 			set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
 | 263 | 		else | 
 | 264 | 			clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
 | 265 | 		child->exit_code = data; | 
 | 266 | 		wake_up_process(child); | 
 | 267 | 		ret = 0; | 
 | 268 | 		break; | 
 | 269 | 	} | 
 | 270 |  | 
 | 271 | /* | 
 | 272 |  * make the child exit.  Best I can do is send it a sigkill. | 
 | 273 |  * perhaps it should be put in the status that it wants to | 
 | 274 |  * exit. | 
 | 275 |  */ | 
 | 276 | 	case PTRACE_KILL: { | 
 | 277 | 		ret = 0; | 
 | 278 | 		if (child->exit_state == EXIT_ZOMBIE)	/* already dead */ | 
 | 279 | 			break; | 
 | 280 | 		child->exit_code = SIGKILL; | 
 | 281 | 		wake_up_process(child); | 
 | 282 | 		break; | 
 | 283 | 	} | 
 | 284 |  | 
 | 285 | 	case PTRACE_SINGLESTEP: {  /* set the trap flag. */ | 
 | 286 | 		struct pt_regs *regs; | 
 | 287 |  | 
 | 288 | 		ret = -EIO; | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 289 | 		if (!valid_signal(data)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 			break; | 
 | 291 | 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
 | 292 | 		if ((child->ptrace & PT_DTRACE) == 0) { | 
 | 293 | 			/* Spurious delayed TF traps may occur */ | 
 | 294 | 			child->ptrace |= PT_DTRACE; | 
 | 295 | 		} | 
 | 296 |  | 
 | 297 | 		regs = child->thread.uregs; | 
 | 298 |  | 
 | 299 | 		regs->sr |= SR_SSTEP;	/* auto-resetting upon exception */ | 
 | 300 |  | 
 | 301 | 		child->exit_code = data; | 
 | 302 | 		/* give it a chance to run. */ | 
 | 303 | 		wake_up_process(child); | 
 | 304 | 		ret = 0; | 
 | 305 | 		break; | 
 | 306 | 	} | 
 | 307 |  | 
 | 308 | 	case PTRACE_DETACH: /* detach a process that was attached. */ | 
 | 309 | 		ret = ptrace_detach(child, data); | 
 | 310 | 		break; | 
 | 311 |  | 
 | 312 | 	default: | 
 | 313 | 		ret = ptrace_request(child, request, addr, data); | 
 | 314 | 		break; | 
 | 315 | 	} | 
 | 316 | out_tsk: | 
 | 317 | 	put_task_struct(child); | 
 | 318 | out: | 
 | 319 | 	unlock_kernel(); | 
 | 320 | 	return ret; | 
 | 321 | } | 
 | 322 |  | 
 | 323 | asmlinkage void syscall_trace(void) | 
 | 324 | { | 
 | 325 | 	struct task_struct *tsk = current; | 
 | 326 |  | 
 | 327 | 	if (!test_thread_flag(TIF_SYSCALL_TRACE)) | 
 | 328 | 		return; | 
 | 329 | 	if (!(tsk->ptrace & PT_PTRACED)) | 
 | 330 | 		return; | 
 | 331 |  | 
 | 332 | 	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) | 
 | 333 | 				 ? 0x80 : 0)); | 
 | 334 | 	/* | 
 | 335 | 	 * this isn't the same as continuing with a signal, but it will do | 
 | 336 | 	 * for normal use.  strace only continues with a signal if the | 
 | 337 | 	 * stopping signal is not SIGTRAP.  -brl | 
 | 338 | 	 */ | 
 | 339 | 	if (tsk->exit_code) { | 
 | 340 | 		send_sig(tsk->exit_code, tsk, 1); | 
 | 341 | 		tsk->exit_code = 0; | 
 | 342 | 	} | 
 | 343 | } | 
 | 344 |  | 
 | 345 | /* Called with interrupts disabled */ | 
 | 346 | asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs) | 
 | 347 | { | 
 | 348 | 	/* This is called after a single step exception (DEBUGSS). | 
 | 349 | 	   There is no need to change the PC, as it is a post-execution | 
 | 350 | 	   exception, as entry.S does not do anything to the PC for DEBUGSS. | 
 | 351 | 	   We need to clear the Single Step setting in SR to avoid | 
 | 352 | 	   continually stepping. */ | 
 | 353 | 	local_irq_enable(); | 
 | 354 | 	regs->sr &= ~SR_SSTEP; | 
 | 355 | 	force_sig(SIGTRAP, current); | 
 | 356 | } | 
 | 357 |  | 
 | 358 | /* Called with interrupts disabled */ | 
 | 359 | asmlinkage void do_software_break_point(unsigned long long vec, | 
 | 360 | 					struct pt_regs *regs) | 
 | 361 | { | 
 | 362 | 	/* We need to forward step the PC, to counteract the backstep done | 
 | 363 | 	   in signal.c. */ | 
 | 364 | 	local_irq_enable(); | 
 | 365 | 	force_sig(SIGTRAP, current); | 
 | 366 | 	regs->pc += 4; | 
 | 367 | } | 
 | 368 |  | 
 | 369 | /* | 
 | 370 |  * Called by kernel/ptrace.c when detaching.. | 
 | 371 |  * | 
 | 372 |  * Make sure single step bits etc are not set. | 
 | 373 |  */ | 
 | 374 | void ptrace_disable(struct task_struct *child) | 
 | 375 | { | 
 | 376 |         /* nothing to do.. */ | 
 | 377 | } |