| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* ptrace.c: Sparc process tracing support. | 
 | 2 |  * | 
 | 3 |  * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu) | 
 | 4 |  * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz) | 
 | 5 |  * | 
 | 6 |  * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson, | 
 | 7 |  * and David Mosberger. | 
 | 8 |  * | 
 | 9 |  * Added Linux support -miguel (weird, eh?, the original code was meant | 
 | 10 |  * to emulate SunOS). | 
 | 11 |  */ | 
 | 12 |  | 
 | 13 | #include <linux/kernel.h> | 
 | 14 | #include <linux/sched.h> | 
 | 15 | #include <linux/mm.h> | 
 | 16 | #include <linux/errno.h> | 
 | 17 | #include <linux/ptrace.h> | 
 | 18 | #include <linux/user.h> | 
 | 19 | #include <linux/smp.h> | 
 | 20 | #include <linux/smp_lock.h> | 
 | 21 | #include <linux/security.h> | 
| David S. Miller | f7ceba3 | 2005-07-10 19:29:45 -0700 | [diff] [blame] | 22 | #include <linux/seccomp.h> | 
 | 23 | #include <linux/audit.h> | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 24 | #include <linux/signal.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 |  | 
 | 26 | #include <asm/asi.h> | 
 | 27 | #include <asm/pgtable.h> | 
 | 28 | #include <asm/system.h> | 
 | 29 | #include <asm/uaccess.h> | 
 | 30 | #include <asm/psrcompat.h> | 
 | 31 | #include <asm/visasm.h> | 
 | 32 | #include <asm/spitfire.h> | 
| David S. Miller | 6a9b490 | 2005-09-19 20:11:57 -0700 | [diff] [blame] | 33 | #include <asm/page.h> | 
| David S. Miller | 717463d | 2005-09-29 18:50:34 -0700 | [diff] [blame] | 34 | #include <asm/cpudata.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
 | 36 | /* Returning from ptrace is a bit tricky because the syscall return | 
 | 37 |  * low level code assumes any value returned which is negative and | 
 | 38 |  * is a valid errno will mean setting the condition codes to indicate | 
 | 39 |  * an error return.  This doesn't work, so we have this hook. | 
 | 40 |  */ | 
 | 41 | static inline void pt_error_return(struct pt_regs *regs, unsigned long error) | 
 | 42 | { | 
 | 43 | 	regs->u_regs[UREG_I0] = error; | 
 | 44 | 	regs->tstate |= (TSTATE_ICARRY | TSTATE_XCARRY); | 
 | 45 | 	regs->tpc = regs->tnpc; | 
 | 46 | 	regs->tnpc += 4; | 
 | 47 | } | 
 | 48 |  | 
 | 49 | static inline void pt_succ_return(struct pt_regs *regs, unsigned long value) | 
 | 50 | { | 
 | 51 | 	regs->u_regs[UREG_I0] = value; | 
 | 52 | 	regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY); | 
 | 53 | 	regs->tpc = regs->tnpc; | 
 | 54 | 	regs->tnpc += 4; | 
 | 55 | } | 
 | 56 |  | 
 | 57 | static inline void | 
 | 58 | pt_succ_return_linux(struct pt_regs *regs, unsigned long value, void __user *addr) | 
 | 59 | { | 
 | 60 | 	if (test_thread_flag(TIF_32BIT)) { | 
 | 61 | 		if (put_user(value, (unsigned int __user *) addr)) { | 
 | 62 | 			pt_error_return(regs, EFAULT); | 
 | 63 | 			return; | 
 | 64 | 		} | 
 | 65 | 	} else { | 
 | 66 | 		if (put_user(value, (long __user *) addr)) { | 
 | 67 | 			pt_error_return(regs, EFAULT); | 
 | 68 | 			return; | 
 | 69 | 		} | 
 | 70 | 	} | 
 | 71 | 	regs->u_regs[UREG_I0] = 0; | 
 | 72 | 	regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY); | 
 | 73 | 	regs->tpc = regs->tnpc; | 
 | 74 | 	regs->tnpc += 4; | 
 | 75 | } | 
 | 76 |  | 
 | 77 | static void | 
 | 78 | pt_os_succ_return (struct pt_regs *regs, unsigned long val, void __user *addr) | 
 | 79 | { | 
 | 80 | 	if (current->personality == PER_SUNOS) | 
 | 81 | 		pt_succ_return (regs, val); | 
 | 82 | 	else | 
 | 83 | 		pt_succ_return_linux (regs, val, addr); | 
 | 84 | } | 
 | 85 |  | 
 | 86 | /* #define ALLOW_INIT_TRACING */ | 
 | 87 | /* #define DEBUG_PTRACE */ | 
 | 88 |  | 
 | 89 | #ifdef DEBUG_PTRACE | 
 | 90 | char *pt_rq [] = { | 
 | 91 | 	/* 0  */ "TRACEME", "PEEKTEXT", "PEEKDATA", "PEEKUSR", | 
 | 92 | 	/* 4  */ "POKETEXT", "POKEDATA", "POKEUSR", "CONT", | 
 | 93 | 	/* 8  */ "KILL", "SINGLESTEP", "SUNATTACH", "SUNDETACH", | 
 | 94 | 	/* 12 */ "GETREGS", "SETREGS", "GETFPREGS", "SETFPREGS", | 
 | 95 | 	/* 16 */ "READDATA", "WRITEDATA", "READTEXT", "WRITETEXT", | 
 | 96 | 	/* 20 */ "GETFPAREGS", "SETFPAREGS", "unknown", "unknown", | 
 | 97 | 	/* 24 */ "SYSCALL", "" | 
 | 98 | }; | 
 | 99 | #endif | 
 | 100 |  | 
 | 101 | /* | 
 | 102 |  * Called by kernel/ptrace.c when detaching.. | 
 | 103 |  * | 
 | 104 |  * Make sure single step bits etc are not set. | 
 | 105 |  */ | 
 | 106 | void ptrace_disable(struct task_struct *child) | 
 | 107 | { | 
 | 108 | 	/* nothing to do */ | 
 | 109 | } | 
 | 110 |  | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 111 | /* To get the necessary page struct, access_process_vm() first calls | 
 | 112 |  * get_user_pages().  This has done a flush_dcache_page() on the | 
 | 113 |  * accessed page.  Then our caller (copy_{to,from}_user_page()) did | 
 | 114 |  * to memcpy to read/write the data from that page. | 
 | 115 |  * | 
 | 116 |  * Now, the only thing we have to do is: | 
 | 117 |  * 1) flush the D-cache if it's possible than an illegal alias | 
 | 118 |  *    has been created | 
 | 119 |  * 2) flush the I-cache if this is pre-cheetah and we did a write | 
 | 120 |  */ | 
 | 121 | void flush_ptrace_access(struct vm_area_struct *vma, struct page *page, | 
 | 122 | 			 unsigned long uaddr, void *kaddr, | 
 | 123 | 			 unsigned long len, int write) | 
 | 124 | { | 
 | 125 | 	BUG_ON(len > PAGE_SIZE); | 
 | 126 |  | 
 | 127 | #ifdef DCACHE_ALIASING_POSSIBLE | 
 | 128 | 	/* If bit 13 of the kernel address we used to access the | 
 | 129 | 	 * user page is the same as the virtual address that page | 
 | 130 | 	 * is mapped to in the user's address space, we can skip the | 
 | 131 | 	 * D-cache flush. | 
 | 132 | 	 */ | 
| David S. Miller | 6a9b490 | 2005-09-19 20:11:57 -0700 | [diff] [blame] | 133 | 	if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) { | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 134 | 		unsigned long start = __pa(kaddr); | 
 | 135 | 		unsigned long end = start + len; | 
| David S. Miller | 717463d | 2005-09-29 18:50:34 -0700 | [diff] [blame] | 136 | 		unsigned long dcache_line_size; | 
 | 137 |  | 
 | 138 | 		dcache_line_size = local_cpu_data().dcache_line_size; | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 139 |  | 
 | 140 | 		if (tlb_type == spitfire) { | 
| David S. Miller | 717463d | 2005-09-29 18:50:34 -0700 | [diff] [blame] | 141 | 			for (; start < end; start += dcache_line_size) | 
| David S. Miller | 6a9b490 | 2005-09-19 20:11:57 -0700 | [diff] [blame] | 142 | 				spitfire_put_dcache_tag(start & 0x3fe0, 0x0); | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 143 | 		} else { | 
| David S. Miller | 717463d | 2005-09-29 18:50:34 -0700 | [diff] [blame] | 144 | 			start &= ~(dcache_line_size - 1); | 
 | 145 | 			for (; start < end; start += dcache_line_size) | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 146 | 				__asm__ __volatile__( | 
 | 147 | 					"stxa %%g0, [%0] %1\n\t" | 
 | 148 | 					"membar #Sync" | 
 | 149 | 					: /* no outputs */ | 
| David S. Miller | 6a9b490 | 2005-09-19 20:11:57 -0700 | [diff] [blame] | 150 | 					: "r" (start), | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 151 | 					"i" (ASI_DCACHE_INVALIDATE)); | 
 | 152 | 		} | 
 | 153 | 	} | 
 | 154 | #endif | 
 | 155 | 	if (write && tlb_type == spitfire) { | 
 | 156 | 		unsigned long start = (unsigned long) kaddr; | 
 | 157 | 		unsigned long end = start + len; | 
| David S. Miller | 717463d | 2005-09-29 18:50:34 -0700 | [diff] [blame] | 158 | 		unsigned long icache_line_size; | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 159 |  | 
| David S. Miller | 717463d | 2005-09-29 18:50:34 -0700 | [diff] [blame] | 160 | 		icache_line_size = local_cpu_data().icache_line_size; | 
 | 161 |  | 
 | 162 | 		for (; start < end; start += icache_line_size) | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 163 | 			flushi(start); | 
 | 164 | 	} | 
 | 165 | } | 
 | 166 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | asmlinkage void do_ptrace(struct pt_regs *regs) | 
 | 168 | { | 
 | 169 | 	int request = regs->u_regs[UREG_I0]; | 
 | 170 | 	pid_t pid = regs->u_regs[UREG_I1]; | 
 | 171 | 	unsigned long addr = regs->u_regs[UREG_I2]; | 
 | 172 | 	unsigned long data = regs->u_regs[UREG_I3]; | 
 | 173 | 	unsigned long addr2 = regs->u_regs[UREG_I4]; | 
 | 174 | 	struct task_struct *child; | 
 | 175 | 	int ret; | 
 | 176 |  | 
 | 177 | 	if (test_thread_flag(TIF_32BIT)) { | 
 | 178 | 		addr &= 0xffffffffUL; | 
 | 179 | 		data &= 0xffffffffUL; | 
 | 180 | 		addr2 &= 0xffffffffUL; | 
 | 181 | 	} | 
 | 182 | 	lock_kernel(); | 
 | 183 | #ifdef DEBUG_PTRACE | 
 | 184 | 	{ | 
 | 185 | 		char *s; | 
 | 186 |  | 
 | 187 | 		if ((request >= 0) && (request <= 24)) | 
 | 188 | 			s = pt_rq [request]; | 
 | 189 | 		else | 
 | 190 | 			s = "unknown"; | 
 | 191 |  | 
 | 192 | 		if (request == PTRACE_POKEDATA && data == 0x91d02001){ | 
 | 193 | 			printk ("do_ptrace: breakpoint pid=%d, addr=%016lx addr2=%016lx\n", | 
 | 194 | 				pid, addr, addr2); | 
 | 195 | 		} else  | 
 | 196 | 			printk("do_ptrace: rq=%s(%d) pid=%d addr=%016lx data=%016lx addr2=%016lx\n", | 
 | 197 | 			       s, request, pid, addr, data, addr2); | 
 | 198 | 	} | 
 | 199 | #endif | 
 | 200 | 	if (request == PTRACE_TRACEME) { | 
 | 201 | 		int ret; | 
 | 202 |  | 
 | 203 | 		/* are we already being traced? */ | 
 | 204 | 		if (current->ptrace & PT_PTRACED) { | 
 | 205 | 			pt_error_return(regs, EPERM); | 
 | 206 | 			goto out; | 
 | 207 | 		} | 
 | 208 | 		ret = security_ptrace(current->parent, current); | 
 | 209 | 		if (ret) { | 
 | 210 | 			pt_error_return(regs, -ret); | 
 | 211 | 			goto out; | 
 | 212 | 		} | 
 | 213 |  | 
 | 214 | 		/* set the ptrace bit in the process flags. */ | 
 | 215 | 		current->ptrace |= PT_PTRACED; | 
 | 216 | 		pt_succ_return(regs, 0); | 
 | 217 | 		goto out; | 
 | 218 | 	} | 
 | 219 | #ifndef ALLOW_INIT_TRACING | 
 | 220 | 	if (pid == 1) { | 
 | 221 | 		/* Can't dork with init. */ | 
 | 222 | 		pt_error_return(regs, EPERM); | 
 | 223 | 		goto out; | 
 | 224 | 	} | 
 | 225 | #endif | 
 | 226 | 	read_lock(&tasklist_lock); | 
 | 227 | 	child = find_task_by_pid(pid); | 
 | 228 | 	if (child) | 
 | 229 | 		get_task_struct(child); | 
 | 230 | 	read_unlock(&tasklist_lock); | 
 | 231 |  | 
 | 232 | 	if (!child) { | 
 | 233 | 		pt_error_return(regs, ESRCH); | 
 | 234 | 		goto out; | 
 | 235 | 	} | 
 | 236 |  | 
 | 237 | 	if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH) | 
 | 238 | 	    || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) { | 
 | 239 | 		if (ptrace_attach(child)) { | 
 | 240 | 			pt_error_return(regs, EPERM); | 
 | 241 | 			goto out_tsk; | 
 | 242 | 		} | 
 | 243 | 		pt_succ_return(regs, 0); | 
 | 244 | 		goto out_tsk; | 
 | 245 | 	} | 
 | 246 |  | 
 | 247 | 	ret = ptrace_check_attach(child, request == PTRACE_KILL); | 
 | 248 | 	if (ret < 0) { | 
 | 249 | 		pt_error_return(regs, -ret); | 
 | 250 | 		goto out_tsk; | 
 | 251 | 	} | 
 | 252 |  | 
 | 253 | 	if (!(test_thread_flag(TIF_32BIT))	&& | 
 | 254 | 	    ((request == PTRACE_READDATA64)		|| | 
 | 255 | 	     (request == PTRACE_WRITEDATA64)		|| | 
 | 256 | 	     (request == PTRACE_READTEXT64)		|| | 
 | 257 | 	     (request == PTRACE_WRITETEXT64)		|| | 
 | 258 | 	     (request == PTRACE_PEEKTEXT64)		|| | 
 | 259 | 	     (request == PTRACE_POKETEXT64)		|| | 
 | 260 | 	     (request == PTRACE_PEEKDATA64)		|| | 
 | 261 | 	     (request == PTRACE_POKEDATA64))) { | 
 | 262 | 		addr = regs->u_regs[UREG_G2]; | 
 | 263 | 		addr2 = regs->u_regs[UREG_G3]; | 
 | 264 | 		request -= 30; /* wheee... */ | 
 | 265 | 	} | 
 | 266 |  | 
 | 267 | 	switch(request) { | 
 | 268 | 	case PTRACE_PEEKTEXT: /* read word at location addr. */  | 
 | 269 | 	case PTRACE_PEEKDATA: { | 
 | 270 | 		unsigned long tmp64; | 
 | 271 | 		unsigned int tmp32; | 
 | 272 | 		int res, copied; | 
 | 273 |  | 
 | 274 | 		res = -EIO; | 
 | 275 | 		if (test_thread_flag(TIF_32BIT)) { | 
 | 276 | 			copied = access_process_vm(child, addr, | 
 | 277 | 						   &tmp32, sizeof(tmp32), 0); | 
 | 278 | 			tmp64 = (unsigned long) tmp32; | 
 | 279 | 			if (copied == sizeof(tmp32)) | 
 | 280 | 				res = 0; | 
 | 281 | 		} else { | 
 | 282 | 			copied = access_process_vm(child, addr, | 
 | 283 | 						   &tmp64, sizeof(tmp64), 0); | 
 | 284 | 			if (copied == sizeof(tmp64)) | 
 | 285 | 				res = 0; | 
 | 286 | 		} | 
 | 287 | 		if (res < 0) | 
 | 288 | 			pt_error_return(regs, -res); | 
 | 289 | 		else | 
 | 290 | 			pt_os_succ_return(regs, tmp64, (void __user *) data); | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 291 | 		goto out_tsk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | 	} | 
 | 293 |  | 
 | 294 | 	case PTRACE_POKETEXT: /* write the word at location addr. */ | 
 | 295 | 	case PTRACE_POKEDATA: { | 
 | 296 | 		unsigned long tmp64; | 
 | 297 | 		unsigned int tmp32; | 
 | 298 | 		int copied, res = -EIO; | 
 | 299 |  | 
 | 300 | 		if (test_thread_flag(TIF_32BIT)) { | 
 | 301 | 			tmp32 = data; | 
 | 302 | 			copied = access_process_vm(child, addr, | 
 | 303 | 						   &tmp32, sizeof(tmp32), 1); | 
 | 304 | 			if (copied == sizeof(tmp32)) | 
 | 305 | 				res = 0; | 
 | 306 | 		} else { | 
 | 307 | 			tmp64 = data; | 
 | 308 | 			copied = access_process_vm(child, addr, | 
 | 309 | 						   &tmp64, sizeof(tmp64), 1); | 
 | 310 | 			if (copied == sizeof(tmp64)) | 
 | 311 | 				res = 0; | 
 | 312 | 		} | 
 | 313 | 		if (res < 0) | 
 | 314 | 			pt_error_return(regs, -res); | 
 | 315 | 		else | 
 | 316 | 			pt_succ_return(regs, res); | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 317 | 		goto out_tsk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | 	} | 
 | 319 |  | 
 | 320 | 	case PTRACE_GETREGS: { | 
 | 321 | 		struct pt_regs32 __user *pregs = | 
 | 322 | 			(struct pt_regs32 __user *) addr; | 
 | 323 | 		struct pt_regs *cregs = child->thread_info->kregs; | 
 | 324 | 		int rval; | 
 | 325 |  | 
 | 326 | 		if (__put_user(tstate_to_psr(cregs->tstate), (&pregs->psr)) || | 
 | 327 | 		    __put_user(cregs->tpc, (&pregs->pc)) || | 
 | 328 | 		    __put_user(cregs->tnpc, (&pregs->npc)) || | 
 | 329 | 		    __put_user(cregs->y, (&pregs->y))) { | 
 | 330 | 			pt_error_return(regs, EFAULT); | 
 | 331 | 			goto out_tsk; | 
 | 332 | 		} | 
 | 333 | 		for (rval = 1; rval < 16; rval++) | 
 | 334 | 			if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) { | 
 | 335 | 				pt_error_return(regs, EFAULT); | 
 | 336 | 				goto out_tsk; | 
 | 337 | 			} | 
 | 338 | 		pt_succ_return(regs, 0); | 
 | 339 | #ifdef DEBUG_PTRACE | 
 | 340 | 		printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]); | 
 | 341 | #endif | 
 | 342 | 		goto out_tsk; | 
 | 343 | 	} | 
 | 344 |  | 
 | 345 | 	case PTRACE_GETREGS64: { | 
 | 346 | 		struct pt_regs __user *pregs = (struct pt_regs __user *) addr; | 
 | 347 | 		struct pt_regs *cregs = child->thread_info->kregs; | 
 | 348 | 		unsigned long tpc = cregs->tpc; | 
 | 349 | 		int rval; | 
 | 350 |  | 
 | 351 | 		if ((child->thread_info->flags & _TIF_32BIT) != 0) | 
 | 352 | 			tpc &= 0xffffffff; | 
 | 353 | 		if (__put_user(cregs->tstate, (&pregs->tstate)) || | 
 | 354 | 		    __put_user(tpc, (&pregs->tpc)) || | 
 | 355 | 		    __put_user(cregs->tnpc, (&pregs->tnpc)) || | 
 | 356 | 		    __put_user(cregs->y, (&pregs->y))) { | 
 | 357 | 			pt_error_return(regs, EFAULT); | 
 | 358 | 			goto out_tsk; | 
 | 359 | 		} | 
 | 360 | 		for (rval = 1; rval < 16; rval++) | 
 | 361 | 			if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) { | 
 | 362 | 				pt_error_return(regs, EFAULT); | 
 | 363 | 				goto out_tsk; | 
 | 364 | 			} | 
 | 365 | 		pt_succ_return(regs, 0); | 
 | 366 | #ifdef DEBUG_PTRACE | 
 | 367 | 		printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]); | 
 | 368 | #endif | 
 | 369 | 		goto out_tsk; | 
 | 370 | 	} | 
 | 371 |  | 
 | 372 | 	case PTRACE_SETREGS: { | 
 | 373 | 		struct pt_regs32 __user *pregs = | 
 | 374 | 			(struct pt_regs32 __user *) addr; | 
 | 375 | 		struct pt_regs *cregs = child->thread_info->kregs; | 
 | 376 | 		unsigned int psr, pc, npc, y; | 
 | 377 | 		int i; | 
 | 378 |  | 
 | 379 | 		/* Must be careful, tracing process can only set certain | 
 | 380 | 		 * bits in the psr. | 
 | 381 | 		 */ | 
 | 382 | 		if (__get_user(psr, (&pregs->psr)) || | 
 | 383 | 		    __get_user(pc, (&pregs->pc)) || | 
 | 384 | 		    __get_user(npc, (&pregs->npc)) || | 
 | 385 | 		    __get_user(y, (&pregs->y))) { | 
 | 386 | 			pt_error_return(regs, EFAULT); | 
 | 387 | 			goto out_tsk; | 
 | 388 | 		} | 
 | 389 | 		cregs->tstate &= ~(TSTATE_ICC); | 
 | 390 | 		cregs->tstate |= psr_to_tstate_icc(psr); | 
 | 391 |                	if (!((pc | npc) & 3)) { | 
 | 392 | 			cregs->tpc = pc; | 
 | 393 | 			cregs->tnpc = npc; | 
 | 394 | 		} | 
 | 395 | 		cregs->y = y; | 
 | 396 | 		for (i = 1; i < 16; i++) { | 
 | 397 | 			if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) { | 
 | 398 | 				pt_error_return(regs, EFAULT); | 
 | 399 | 				goto out_tsk; | 
 | 400 | 			} | 
 | 401 | 		} | 
 | 402 | 		pt_succ_return(regs, 0); | 
 | 403 | 		goto out_tsk; | 
 | 404 | 	} | 
 | 405 |  | 
 | 406 | 	case PTRACE_SETREGS64: { | 
 | 407 | 		struct pt_regs __user *pregs = (struct pt_regs __user *) addr; | 
 | 408 | 		struct pt_regs *cregs = child->thread_info->kregs; | 
 | 409 | 		unsigned long tstate, tpc, tnpc, y; | 
 | 410 | 		int i; | 
 | 411 |  | 
 | 412 | 		/* Must be careful, tracing process can only set certain | 
 | 413 | 		 * bits in the psr. | 
 | 414 | 		 */ | 
 | 415 | 		if (__get_user(tstate, (&pregs->tstate)) || | 
 | 416 | 		    __get_user(tpc, (&pregs->tpc)) || | 
 | 417 | 		    __get_user(tnpc, (&pregs->tnpc)) || | 
 | 418 | 		    __get_user(y, (&pregs->y))) { | 
 | 419 | 			pt_error_return(regs, EFAULT); | 
 | 420 | 			goto out_tsk; | 
 | 421 | 		} | 
 | 422 | 		if ((child->thread_info->flags & _TIF_32BIT) != 0) { | 
 | 423 | 			tpc &= 0xffffffff; | 
 | 424 | 			tnpc &= 0xffffffff; | 
 | 425 | 		} | 
 | 426 | 		tstate &= (TSTATE_ICC | TSTATE_XCC); | 
 | 427 | 		cregs->tstate &= ~(TSTATE_ICC | TSTATE_XCC); | 
 | 428 | 		cregs->tstate |= tstate; | 
 | 429 | 		if (!((tpc | tnpc) & 3)) { | 
 | 430 | 			cregs->tpc = tpc; | 
 | 431 | 			cregs->tnpc = tnpc; | 
 | 432 | 		} | 
 | 433 | 		cregs->y = y; | 
 | 434 | 		for (i = 1; i < 16; i++) { | 
 | 435 | 			if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) { | 
 | 436 | 				pt_error_return(regs, EFAULT); | 
 | 437 | 				goto out_tsk; | 
 | 438 | 			} | 
 | 439 | 		} | 
 | 440 | 		pt_succ_return(regs, 0); | 
 | 441 | 		goto out_tsk; | 
 | 442 | 	} | 
 | 443 |  | 
 | 444 | 	case PTRACE_GETFPREGS: { | 
 | 445 | 		struct fps { | 
 | 446 | 			unsigned int regs[32]; | 
 | 447 | 			unsigned int fsr; | 
 | 448 | 			unsigned int flags; | 
 | 449 | 			unsigned int extra; | 
 | 450 | 			unsigned int fpqd; | 
 | 451 | 			struct fq { | 
 | 452 | 				unsigned int insnaddr; | 
 | 453 | 				unsigned int insn; | 
 | 454 | 			} fpq[16]; | 
 | 455 | 		}; | 
 | 456 | 		struct fps __user *fps = (struct fps __user *) addr; | 
 | 457 | 		unsigned long *fpregs = child->thread_info->fpregs; | 
 | 458 |  | 
 | 459 | 		if (copy_to_user(&fps->regs[0], fpregs, | 
 | 460 | 				 (32 * sizeof(unsigned int))) || | 
 | 461 | 		    __put_user(child->thread_info->xfsr[0], (&fps->fsr)) || | 
 | 462 | 		    __put_user(0, (&fps->fpqd)) || | 
 | 463 | 		    __put_user(0, (&fps->flags)) || | 
 | 464 | 		    __put_user(0, (&fps->extra)) || | 
 | 465 | 		    clear_user(&fps->fpq[0], 32 * sizeof(unsigned int))) { | 
 | 466 | 			pt_error_return(regs, EFAULT); | 
 | 467 | 			goto out_tsk; | 
 | 468 | 		} | 
 | 469 | 		pt_succ_return(regs, 0); | 
 | 470 | 		goto out_tsk; | 
 | 471 | 	} | 
 | 472 |  | 
 | 473 | 	case PTRACE_GETFPREGS64: { | 
 | 474 | 		struct fps { | 
 | 475 | 			unsigned int regs[64]; | 
 | 476 | 			unsigned long fsr; | 
 | 477 | 		}; | 
 | 478 | 		struct fps __user *fps = (struct fps __user *) addr; | 
 | 479 | 		unsigned long *fpregs = child->thread_info->fpregs; | 
 | 480 |  | 
 | 481 | 		if (copy_to_user(&fps->regs[0], fpregs, | 
 | 482 | 				 (64 * sizeof(unsigned int))) || | 
 | 483 | 		    __put_user(child->thread_info->xfsr[0], (&fps->fsr))) { | 
 | 484 | 			pt_error_return(regs, EFAULT); | 
 | 485 | 			goto out_tsk; | 
 | 486 | 		} | 
 | 487 | 		pt_succ_return(regs, 0); | 
 | 488 | 		goto out_tsk; | 
 | 489 | 	} | 
 | 490 |  | 
 | 491 | 	case PTRACE_SETFPREGS: { | 
 | 492 | 		struct fps { | 
 | 493 | 			unsigned int regs[32]; | 
 | 494 | 			unsigned int fsr; | 
 | 495 | 			unsigned int flags; | 
 | 496 | 			unsigned int extra; | 
 | 497 | 			unsigned int fpqd; | 
 | 498 | 			struct fq { | 
 | 499 | 				unsigned int insnaddr; | 
 | 500 | 				unsigned int insn; | 
 | 501 | 			} fpq[16]; | 
 | 502 | 		}; | 
 | 503 | 		struct fps __user *fps = (struct fps __user *) addr; | 
 | 504 | 		unsigned long *fpregs = child->thread_info->fpregs; | 
 | 505 | 		unsigned fsr; | 
 | 506 |  | 
 | 507 | 		if (copy_from_user(fpregs, &fps->regs[0], | 
 | 508 | 				   (32 * sizeof(unsigned int))) || | 
 | 509 | 		    __get_user(fsr, (&fps->fsr))) { | 
 | 510 | 			pt_error_return(regs, EFAULT); | 
 | 511 | 			goto out_tsk; | 
 | 512 | 		} | 
 | 513 | 		child->thread_info->xfsr[0] &= 0xffffffff00000000UL; | 
 | 514 | 		child->thread_info->xfsr[0] |= fsr; | 
 | 515 | 		if (!(child->thread_info->fpsaved[0] & FPRS_FEF)) | 
 | 516 | 			child->thread_info->gsr[0] = 0; | 
 | 517 | 		child->thread_info->fpsaved[0] |= (FPRS_FEF | FPRS_DL); | 
 | 518 | 		pt_succ_return(regs, 0); | 
 | 519 | 		goto out_tsk; | 
 | 520 | 	} | 
 | 521 |  | 
 | 522 | 	case PTRACE_SETFPREGS64: { | 
 | 523 | 		struct fps { | 
 | 524 | 			unsigned int regs[64]; | 
 | 525 | 			unsigned long fsr; | 
 | 526 | 		}; | 
 | 527 | 		struct fps __user *fps = (struct fps __user *) addr; | 
 | 528 | 		unsigned long *fpregs = child->thread_info->fpregs; | 
 | 529 |  | 
 | 530 | 		if (copy_from_user(fpregs, &fps->regs[0], | 
 | 531 | 				   (64 * sizeof(unsigned int))) || | 
 | 532 | 		    __get_user(child->thread_info->xfsr[0], (&fps->fsr))) { | 
 | 533 | 			pt_error_return(regs, EFAULT); | 
 | 534 | 			goto out_tsk; | 
 | 535 | 		} | 
 | 536 | 		if (!(child->thread_info->fpsaved[0] & FPRS_FEF)) | 
 | 537 | 			child->thread_info->gsr[0] = 0; | 
 | 538 | 		child->thread_info->fpsaved[0] |= (FPRS_FEF | FPRS_DL | FPRS_DU); | 
 | 539 | 		pt_succ_return(regs, 0); | 
 | 540 | 		goto out_tsk; | 
 | 541 | 	} | 
 | 542 |  | 
 | 543 | 	case PTRACE_READTEXT: | 
 | 544 | 	case PTRACE_READDATA: { | 
 | 545 | 		int res = ptrace_readdata(child, addr, | 
 | 546 | 					  (char __user *)addr2, data); | 
 | 547 | 		if (res == data) { | 
 | 548 | 			pt_succ_return(regs, 0); | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 549 | 			goto out_tsk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | 		} | 
 | 551 | 		if (res >= 0) | 
 | 552 | 			res = -EIO; | 
 | 553 | 		pt_error_return(regs, -res); | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 554 | 		goto out_tsk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | 	} | 
 | 556 |  | 
 | 557 | 	case PTRACE_WRITETEXT: | 
 | 558 | 	case PTRACE_WRITEDATA: { | 
 | 559 | 		int res = ptrace_writedata(child, (char __user *) addr2, | 
 | 560 | 					   addr, data); | 
 | 561 | 		if (res == data) { | 
 | 562 | 			pt_succ_return(regs, 0); | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 563 | 			goto out_tsk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | 		} | 
 | 565 | 		if (res >= 0) | 
 | 566 | 			res = -EIO; | 
 | 567 | 		pt_error_return(regs, -res); | 
| David S. Miller | dadeafd | 2005-04-17 18:03:11 -0700 | [diff] [blame] | 568 | 		goto out_tsk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | 	} | 
 | 570 | 	case PTRACE_SYSCALL: /* continue and stop at (return from) syscall */ | 
 | 571 | 		addr = 1; | 
 | 572 |  | 
 | 573 | 	case PTRACE_CONT: { /* restart after signal. */ | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 574 | 		if (!valid_signal(data)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | 			pt_error_return(regs, EIO); | 
 | 576 | 			goto out_tsk; | 
 | 577 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 |  | 
 | 579 | 		if (request == PTRACE_SYSCALL) { | 
 | 580 | 			set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
 | 581 | 		} else { | 
 | 582 | 			clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | 
 | 583 | 		} | 
 | 584 |  | 
 | 585 | 		child->exit_code = data; | 
 | 586 | #ifdef DEBUG_PTRACE | 
 | 587 | 		printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n", child->comm, | 
 | 588 | 			child->pid, child->exit_code, | 
 | 589 | 			child->thread_info->kregs->tpc, | 
 | 590 | 			child->thread_info->kregs->tnpc); | 
 | 591 | 		        | 
 | 592 | #endif | 
 | 593 | 		wake_up_process(child); | 
 | 594 | 		pt_succ_return(regs, 0); | 
 | 595 | 		goto out_tsk; | 
 | 596 | 	} | 
 | 597 |  | 
 | 598 | /* | 
 | 599 |  * make the child exit.  Best I can do is send it a sigkill.  | 
 | 600 |  * perhaps it should be put in the status that it wants to  | 
 | 601 |  * exit. | 
 | 602 |  */ | 
 | 603 | 	case PTRACE_KILL: { | 
 | 604 | 		if (child->exit_state == EXIT_ZOMBIE) {	/* already dead */ | 
 | 605 | 			pt_succ_return(regs, 0); | 
 | 606 | 			goto out_tsk; | 
 | 607 | 		} | 
 | 608 | 		child->exit_code = SIGKILL; | 
 | 609 | 		wake_up_process(child); | 
 | 610 | 		pt_succ_return(regs, 0); | 
 | 611 | 		goto out_tsk; | 
 | 612 | 	} | 
 | 613 |  | 
 | 614 | 	case PTRACE_SUNDETACH: { /* detach a process that was attached. */ | 
 | 615 | 		int error = ptrace_detach(child, data); | 
 | 616 | 		if (error) { | 
 | 617 | 			pt_error_return(regs, EIO); | 
 | 618 | 			goto out_tsk; | 
 | 619 | 		} | 
 | 620 | 		pt_succ_return(regs, 0); | 
 | 621 | 		goto out_tsk; | 
 | 622 | 	} | 
 | 623 |  | 
 | 624 | 	/* PTRACE_DUMPCORE unsupported... */ | 
 | 625 |  | 
 | 626 | 	default: { | 
 | 627 | 		int err = ptrace_request(child, request, addr, data); | 
 | 628 | 		if (err) | 
 | 629 | 			pt_error_return(regs, -err); | 
 | 630 | 		else | 
 | 631 | 			pt_succ_return(regs, 0); | 
 | 632 | 		goto out_tsk; | 
 | 633 | 	} | 
 | 634 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | out_tsk: | 
 | 636 | 	if (child) | 
 | 637 | 		put_task_struct(child); | 
 | 638 | out: | 
 | 639 | 	unlock_kernel(); | 
 | 640 | } | 
 | 641 |  | 
| David S. Miller | 8d8a647 | 2005-07-10 16:55:48 -0700 | [diff] [blame] | 642 | asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { | 
| David S. Miller | bb49bcd | 2005-07-10 16:49:28 -0700 | [diff] [blame] | 644 | 	/* do the secure computing check first */ | 
| David S. Miller | 8d8a647 | 2005-07-10 16:55:48 -0700 | [diff] [blame] | 645 | 	secure_computing(regs->u_regs[UREG_G1]); | 
| David S. Miller | bb49bcd | 2005-07-10 16:49:28 -0700 | [diff] [blame] | 646 |  | 
| David S. Miller | f7ceba3 | 2005-07-10 19:29:45 -0700 | [diff] [blame] | 647 | 	if (unlikely(current->audit_context) && syscall_exit_p) { | 
 | 648 | 		unsigned long tstate = regs->tstate; | 
 | 649 | 		int result = AUDITSC_SUCCESS; | 
 | 650 |  | 
 | 651 | 		if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY))) | 
 | 652 | 			result = AUDITSC_FAILURE; | 
 | 653 |  | 
 | 654 | 		audit_syscall_exit(current, result, regs->u_regs[UREG_I0]); | 
 | 655 | 	} | 
 | 656 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | 	if (!(current->ptrace & PT_PTRACED)) | 
| David S. Miller | f7ceba3 | 2005-07-10 19:29:45 -0700 | [diff] [blame] | 658 | 		goto out; | 
 | 659 |  | 
 | 660 | 	if (!test_thread_flag(TIF_SYSCALL_TRACE)) | 
 | 661 | 		goto out; | 
 | 662 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | 	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) | 
 | 664 | 				 ? 0x80 : 0)); | 
 | 665 |  | 
 | 666 | 	/* | 
 | 667 | 	 * this isn't the same as continuing with a signal, but it will do | 
 | 668 | 	 * for normal use.  strace only continues with a signal if the | 
 | 669 | 	 * stopping signal is not SIGTRAP.  -brl | 
 | 670 | 	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | 	if (current->exit_code) { | 
| David S. Miller | bb49bcd | 2005-07-10 16:49:28 -0700 | [diff] [blame] | 672 | 		send_sig(current->exit_code, current, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | 		current->exit_code = 0; | 
 | 674 | 	} | 
| David S. Miller | f7ceba3 | 2005-07-10 19:29:45 -0700 | [diff] [blame] | 675 |  | 
 | 676 | out: | 
 | 677 | 	if (unlikely(current->audit_context) && !syscall_exit_p) | 
 | 678 | 		audit_syscall_entry(current, | 
 | 679 | 				    (test_thread_flag(TIF_32BIT) ? | 
 | 680 | 				     AUDIT_ARCH_SPARC : | 
 | 681 | 				     AUDIT_ARCH_SPARC64), | 
 | 682 | 				    regs->u_regs[UREG_G1], | 
 | 683 | 				    regs->u_regs[UREG_I0], | 
 | 684 | 				    regs->u_regs[UREG_I1], | 
 | 685 | 				    regs->u_regs[UREG_I2], | 
 | 686 | 				    regs->u_regs[UREG_I3]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } |