| Michal Simek | 2b43845 | 2009-03-27 14:25:27 +0100 | [diff] [blame] | 1 | /* | 
 | 2 |  * `ptrace' system call | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu> | 
 | 5 |  * Copyright (C) 2007-2009 PetaLogix | 
 | 6 |  * Copyright (C) 2004-2007 John Williams <john.williams@petalogix.com> | 
 | 7 |  * | 
 | 8 |  * derived from arch/v850/kernel/ptrace.c | 
 | 9 |  * | 
 | 10 |  * Copyright (C) 2002,03 NEC Electronics Corporation | 
 | 11 |  * Copyright (C) 2002,03 Miles Bader <miles@gnu.org> | 
 | 12 |  * | 
 | 13 |  * Derived from arch/mips/kernel/ptrace.c: | 
 | 14 |  * | 
 | 15 |  * Copyright (C) 1992 Ross Biro | 
 | 16 |  * Copyright (C) Linus Torvalds | 
 | 17 |  * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle | 
 | 18 |  * Copyright (C) 1996 David S. Miller | 
 | 19 |  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com | 
 | 20 |  * Copyright (C) 1999 MIPS Technologies, Inc. | 
 | 21 |  * | 
 | 22 |  * This file is subject to the terms and conditions of the GNU General | 
 | 23 |  * Public License. See the file COPYING in the main directory of this | 
 | 24 |  * archive for more details. | 
 | 25 |  */ | 
 | 26 |  | 
 | 27 | #include <linux/kernel.h> | 
 | 28 | #include <linux/mm.h> | 
 | 29 | #include <linux/sched.h> | 
| Michal Simek | 2b43845 | 2009-03-27 14:25:27 +0100 | [diff] [blame] | 30 | #include <linux/ptrace.h> | 
 | 31 | #include <linux/signal.h> | 
| Michal Simek | 2357548 | 2009-08-24 13:26:04 +0200 | [diff] [blame] | 32 | #include <linux/elf.h> | 
 | 33 | #include <linux/audit.h> | 
 | 34 | #include <linux/seccomp.h> | 
 | 35 | #include <linux/tracehook.h> | 
| Michal Simek | 2b43845 | 2009-03-27 14:25:27 +0100 | [diff] [blame] | 36 |  | 
 | 37 | #include <linux/errno.h> | 
| Michal Simek | 2b43845 | 2009-03-27 14:25:27 +0100 | [diff] [blame] | 38 | #include <asm/processor.h> | 
 | 39 | #include <linux/uaccess.h> | 
 | 40 | #include <asm/asm-offsets.h> | 
 | 41 |  | 
 | 42 | /* Returns the address where the register at REG_OFFS in P is stashed away. */ | 
 | 43 | static microblaze_reg_t *reg_save_addr(unsigned reg_offs, | 
 | 44 | 					struct task_struct *t) | 
 | 45 | { | 
 | 46 | 	struct pt_regs *regs; | 
 | 47 |  | 
 | 48 | 	/* | 
 | 49 | 	 * Three basic cases: | 
 | 50 | 	 * | 
 | 51 | 	 * (1)	A register normally saved before calling the scheduler, is | 
 | 52 | 	 *	available in the kernel entry pt_regs structure at the top | 
 | 53 | 	 *	of the kernel stack. The kernel trap/irq exit path takes | 
 | 54 | 	 *	care to save/restore almost all registers for ptrace'd | 
 | 55 | 	 *	processes. | 
 | 56 | 	 * | 
 | 57 | 	 * (2)	A call-clobbered register, where the process P entered the | 
 | 58 | 	 *	kernel via [syscall] trap, is not stored anywhere; that's | 
 | 59 | 	 *	OK, because such registers are not expected to be preserved | 
 | 60 | 	 *	when the trap returns anyway (so we don't actually bother to | 
 | 61 | 	 *	test for this case). | 
 | 62 | 	 * | 
 | 63 | 	 * (3)	A few registers not used at all by the kernel, and so | 
 | 64 | 	 *	normally never saved except by context-switches, are in the | 
 | 65 | 	 *	context switch state. | 
 | 66 | 	 */ | 
 | 67 |  | 
 | 68 | 	/* Register saved during kernel entry (or not available). */ | 
 | 69 | 	regs = task_pt_regs(t); | 
 | 70 |  | 
 | 71 | 	return (microblaze_reg_t *)((char *)regs + reg_offs); | 
 | 72 | } | 
 | 73 |  | 
 | 74 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) | 
 | 75 | { | 
 | 76 | 	int rval; | 
 | 77 | 	unsigned long val = 0; | 
 | 78 | 	unsigned long copied; | 
 | 79 |  | 
 | 80 | 	switch (request) { | 
 | 81 | 	case PTRACE_PEEKTEXT: /* read word at location addr. */ | 
 | 82 | 	case PTRACE_PEEKDATA: | 
 | 83 | 		pr_debug("PEEKTEXT/PEEKDATA at %08lX\n", addr); | 
 | 84 | 		copied = access_process_vm(child, addr, &val, sizeof(val), 0); | 
 | 85 | 		rval = -EIO; | 
 | 86 | 		if (copied != sizeof(val)) | 
 | 87 | 			break; | 
 | 88 | 		rval = put_user(val, (unsigned long *)data); | 
 | 89 | 		break; | 
 | 90 |  | 
 | 91 | 	case PTRACE_POKETEXT: /* write the word at location addr. */ | 
 | 92 | 	case PTRACE_POKEDATA: | 
 | 93 | 		pr_debug("POKETEXT/POKEDATA to %08lX\n", addr); | 
 | 94 | 		rval = 0; | 
 | 95 | 		if (access_process_vm(child, addr, &data, sizeof(data), 1) | 
 | 96 | 		    == sizeof(data)) | 
 | 97 | 			break; | 
 | 98 | 		rval = -EIO; | 
 | 99 | 		break; | 
 | 100 |  | 
 | 101 | 	/* Read/write the word at location ADDR in the registers. */ | 
 | 102 | 	case PTRACE_PEEKUSR: | 
 | 103 | 	case PTRACE_POKEUSR: | 
 | 104 | 		pr_debug("PEEKUSR/POKEUSR : 0x%08lx\n", addr); | 
 | 105 | 		rval = 0; | 
 | 106 | 		if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) { | 
 | 107 | 			/* | 
 | 108 | 			 * Special requests that don't actually correspond | 
 | 109 | 			 * to offsets in struct pt_regs. | 
 | 110 | 			 */ | 
 | 111 | 			if (addr == PT_TEXT_ADDR) { | 
 | 112 | 				val = child->mm->start_code; | 
 | 113 | 			} else if (addr == PT_DATA_ADDR) { | 
 | 114 | 				val = child->mm->start_data; | 
 | 115 | 			} else if (addr == PT_TEXT_LEN) { | 
 | 116 | 				val = child->mm->end_code | 
 | 117 | 					- child->mm->start_code; | 
 | 118 | 			} else { | 
 | 119 | 				rval = -EIO; | 
 | 120 | 			} | 
 | 121 | 		} else if (addr >= 0 && addr < PT_SIZE && (addr & 0x3) == 0) { | 
 | 122 | 			microblaze_reg_t *reg_addr = reg_save_addr(addr, child); | 
 | 123 | 			if (request == PTRACE_PEEKUSR) | 
 | 124 | 				val = *reg_addr; | 
 | 125 | 			else | 
 | 126 | 				*reg_addr = data; | 
 | 127 | 		} else | 
 | 128 | 			rval = -EIO; | 
 | 129 |  | 
 | 130 | 		if (rval == 0 && request == PTRACE_PEEKUSR) | 
 | 131 | 			rval = put_user(val, (unsigned long *)data); | 
 | 132 | 		break; | 
 | 133 | 	/* Continue and stop at next (return from) syscall */ | 
 | 134 | 	case PTRACE_SYSCALL: | 
 | 135 | 		pr_debug("PTRACE_SYSCALL\n"); | 
 | 136 | 	case PTRACE_SINGLESTEP: | 
 | 137 | 		pr_debug("PTRACE_SINGLESTEP\n"); | 
 | 138 | 	/* Restart after a signal.  */ | 
 | 139 | 	case PTRACE_CONT: | 
 | 140 | 		pr_debug("PTRACE_CONT\n"); | 
 | 141 | 		rval = -EIO; | 
 | 142 | 		if (!valid_signal(data)) | 
 | 143 | 			break; | 
 | 144 |  | 
 | 145 | 		if (request == PTRACE_SYSCALL) | 
 | 146 | 			set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
 | 147 | 		else | 
 | 148 | 			clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
 | 149 |  | 
 | 150 | 		child->exit_code = data; | 
 | 151 | 		pr_debug("wakeup_process\n"); | 
 | 152 | 		wake_up_process(child); | 
 | 153 | 		rval = 0; | 
 | 154 | 		break; | 
 | 155 |  | 
 | 156 | 	/* | 
 | 157 | 	 * make the child exit.  Best I can do is send it a sigkill. | 
 | 158 | 	 * perhaps it should be put in the status that it wants to | 
 | 159 | 	 * exit. | 
 | 160 | 	 */ | 
 | 161 | 	case PTRACE_KILL: | 
 | 162 | 		pr_debug("PTRACE_KILL\n"); | 
 | 163 | 		rval = 0; | 
 | 164 | 		if (child->exit_state == EXIT_ZOMBIE)	/* already dead */ | 
 | 165 | 			break; | 
 | 166 | 		child->exit_code = SIGKILL; | 
 | 167 | 		wake_up_process(child); | 
 | 168 | 		break; | 
 | 169 |  | 
 | 170 | 	case PTRACE_DETACH: /* detach a process that was attached. */ | 
 | 171 | 		pr_debug("PTRACE_DETACH\n"); | 
 | 172 | 		rval = ptrace_detach(child, data); | 
 | 173 | 		break; | 
 | 174 | 	default: | 
 | 175 | 		/* rval = ptrace_request(child, request, addr, data); noMMU */ | 
 | 176 | 		rval = -EIO; | 
 | 177 | 	} | 
 | 178 | 	return rval; | 
 | 179 | } | 
 | 180 |  | 
| Michal Simek | 2357548 | 2009-08-24 13:26:04 +0200 | [diff] [blame] | 181 | asmlinkage long do_syscall_trace_enter(struct pt_regs *regs) | 
 | 182 | { | 
 | 183 | 	long ret = 0; | 
 | 184 |  | 
 | 185 | 	secure_computing(regs->r12); | 
 | 186 |  | 
 | 187 | 	if (test_thread_flag(TIF_SYSCALL_TRACE) && | 
 | 188 | 	    tracehook_report_syscall_entry(regs)) | 
 | 189 | 		/* | 
 | 190 | 		 * Tracing decided this syscall should not happen. | 
 | 191 | 		 * We'll return a bogus call number to get an ENOSYS | 
 | 192 | 		 * error, but leave the original number in regs->regs[0]. | 
 | 193 | 		 */ | 
 | 194 | 		ret = -1L; | 
 | 195 |  | 
 | 196 | 	if (unlikely(current->audit_context)) | 
 | 197 | 		audit_syscall_entry(EM_XILINX_MICROBLAZE, regs->r12, | 
 | 198 | 				    regs->r5, regs->r6, | 
 | 199 | 				    regs->r7, regs->r8); | 
 | 200 |  | 
 | 201 | 	return ret ?: regs->r12; | 
 | 202 | } | 
 | 203 |  | 
 | 204 | asmlinkage void do_syscall_trace_leave(struct pt_regs *regs) | 
 | 205 | { | 
 | 206 | 	int step; | 
 | 207 |  | 
 | 208 | 	if (unlikely(current->audit_context)) | 
 | 209 | 		audit_syscall_exit(AUDITSC_RESULT(regs->r3), regs->r3); | 
 | 210 |  | 
 | 211 | 	step = test_thread_flag(TIF_SINGLESTEP); | 
 | 212 | 	if (step || test_thread_flag(TIF_SYSCALL_TRACE)) | 
 | 213 | 		tracehook_report_syscall_exit(regs, step); | 
 | 214 | } | 
 | 215 |  | 
 | 216 | #if 0 | 
 | 217 | static asmlinkage void syscall_trace(void) | 
 | 218 | { | 
 | 219 | 	if (!test_thread_flag(TIF_SYSCALL_TRACE)) | 
 | 220 | 		return; | 
 | 221 | 	if (!(current->ptrace & PT_PTRACED)) | 
 | 222 | 		return; | 
 | 223 | 	/* The 0x80 provides a way for the tracing parent to distinguish | 
 | 224 | 	 between a syscall stop and SIGTRAP delivery */ | 
 | 225 | 	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) | 
 | 226 | 				? 0x80 : 0)); | 
 | 227 | 	/* | 
 | 228 | 	 * this isn't the same as continuing with a signal, but it will do | 
 | 229 | 	 * for normal use. strace only continues with a signal if the | 
 | 230 | 	 * stopping signal is not SIGTRAP. -brl | 
 | 231 | 	 */ | 
 | 232 | 	if (current->exit_code) { | 
 | 233 | 		send_sig(current->exit_code, current, 1); | 
 | 234 | 		current->exit_code = 0; | 
 | 235 | 	} | 
 | 236 | } | 
 | 237 | #endif | 
 | 238 |  | 
| Michal Simek | 2b43845 | 2009-03-27 14:25:27 +0100 | [diff] [blame] | 239 | void ptrace_disable(struct task_struct *child) | 
 | 240 | { | 
 | 241 | 	/* nothing to do */ | 
 | 242 | } |