xref: /linux/arch/s390/kernel/ptrace.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
1 /*
2  *  arch/s390/kernel/ptrace.c
3  *
4  *  S390 version
5  *    Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
7  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
8  *
9  *  Based on PowerPC version
10  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
11  *
12  *  Derived from "arch/m68k/kernel/ptrace.c"
13  *  Copyright (C) 1994 by Hamish Macdonald
14  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
15  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
16  *
17  * Modified by Cort Dougan (cort@cs.nmt.edu)
18  *
19  *
20  * This file is subject to the terms and conditions of the GNU General
21  * Public License.  See the file README.legal in the main directory of
22  * this archive for more details.
23  */
24 
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/mm.h>
28 #include <linux/smp.h>
29 #include <linux/smp_lock.h>
30 #include <linux/errno.h>
31 #include <linux/ptrace.h>
32 #include <linux/user.h>
33 #include <linux/security.h>
34 #include <linux/audit.h>
35 #include <linux/signal.h>
36 
37 #include <asm/segment.h>
38 #include <asm/page.h>
39 #include <asm/pgtable.h>
40 #include <asm/pgalloc.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43 #include <asm/unistd.h>
44 
45 #ifdef CONFIG_COMPAT
46 #include "compat_ptrace.h"
47 #endif
48 
49 static void
50 FixPerRegisters(struct task_struct *task)
51 {
52 	struct pt_regs *regs;
53 	per_struct *per_info;
54 
55 	regs = task_pt_regs(task);
56 	per_info = (per_struct *) &task->thread.per_info;
57 	per_info->control_regs.bits.em_instruction_fetch =
58 		per_info->single_step | per_info->instruction_fetch;
59 
60 	if (per_info->single_step) {
61 		per_info->control_regs.bits.starting_addr = 0;
62 #ifdef CONFIG_COMPAT
63 		if (test_thread_flag(TIF_31BIT))
64 			per_info->control_regs.bits.ending_addr = 0x7fffffffUL;
65 		else
66 #endif
67 			per_info->control_regs.bits.ending_addr = PSW_ADDR_INSN;
68 	} else {
69 		per_info->control_regs.bits.starting_addr =
70 			per_info->starting_addr;
71 		per_info->control_regs.bits.ending_addr =
72 			per_info->ending_addr;
73 	}
74 	/*
75 	 * if any of the control reg tracing bits are on
76 	 * we switch on per in the psw
77 	 */
78 	if (per_info->control_regs.words.cr[0] & PER_EM_MASK)
79 		regs->psw.mask |= PSW_MASK_PER;
80 	else
81 		regs->psw.mask &= ~PSW_MASK_PER;
82 
83 	if (per_info->control_regs.bits.em_storage_alteration)
84 		per_info->control_regs.bits.storage_alt_space_ctl = 1;
85 	else
86 		per_info->control_regs.bits.storage_alt_space_ctl = 0;
87 }
88 
89 static void set_single_step(struct task_struct *task)
90 {
91 	task->thread.per_info.single_step = 1;
92 	FixPerRegisters(task);
93 }
94 
95 static void clear_single_step(struct task_struct *task)
96 {
97 	task->thread.per_info.single_step = 0;
98 	FixPerRegisters(task);
99 }
100 
101 /*
102  * Called by kernel/ptrace.c when detaching..
103  *
104  * Make sure single step bits etc are not set.
105  */
106 void
107 ptrace_disable(struct task_struct *child)
108 {
109 	/* make sure the single step bit is not set. */
110 	clear_single_step(child);
111 }
112 
113 #ifndef CONFIG_64BIT
114 # define __ADDR_MASK 3
115 #else
116 # define __ADDR_MASK 7
117 #endif
118 
119 /*
120  * Read the word at offset addr from the user area of a process. The
121  * trouble here is that the information is littered over different
122  * locations. The process registers are found on the kernel stack,
123  * the floating point stuff and the trace settings are stored in
124  * the task structure. In addition the different structures in
125  * struct user contain pad bytes that should be read as zeroes.
126  * Lovely...
127  */
128 static int
129 peek_user(struct task_struct *child, addr_t addr, addr_t data)
130 {
131 	struct user *dummy = NULL;
132 	addr_t offset, tmp, mask;
133 
134 	/*
135 	 * Stupid gdb peeks/pokes the access registers in 64 bit with
136 	 * an alignment of 4. Programmers from hell...
137 	 */
138 	mask = __ADDR_MASK;
139 #ifdef CONFIG_64BIT
140 	if (addr >= (addr_t) &dummy->regs.acrs &&
141 	    addr < (addr_t) &dummy->regs.orig_gpr2)
142 		mask = 3;
143 #endif
144 	if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
145 		return -EIO;
146 
147 	if (addr < (addr_t) &dummy->regs.acrs) {
148 		/*
149 		 * psw and gprs are stored on the stack
150 		 */
151 		tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
152 		if (addr == (addr_t) &dummy->regs.psw.mask)
153 			/* Remove per bit from user psw. */
154 			tmp &= ~PSW_MASK_PER;
155 
156 	} else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
157 		/*
158 		 * access registers are stored in the thread structure
159 		 */
160 		offset = addr - (addr_t) &dummy->regs.acrs;
161 #ifdef CONFIG_64BIT
162 		/*
163 		 * Very special case: old & broken 64 bit gdb reading
164 		 * from acrs[15]. Result is a 64 bit value. Read the
165 		 * 32 bit acrs[15] value and shift it by 32. Sick...
166 		 */
167 		if (addr == (addr_t) &dummy->regs.acrs[15])
168 			tmp = ((unsigned long) child->thread.acrs[15]) << 32;
169 		else
170 #endif
171 		tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
172 
173 	} else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
174 		/*
175 		 * orig_gpr2 is stored on the kernel stack
176 		 */
177 		tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
178 
179 	} else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
180 		/*
181 		 * floating point regs. are stored in the thread structure
182 		 */
183 		offset = addr - (addr_t) &dummy->regs.fp_regs;
184 		tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
185 		if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
186 			tmp &= (unsigned long) FPC_VALID_MASK
187 				<< (BITS_PER_LONG - 32);
188 
189 	} else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
190 		/*
191 		 * per_info is found in the thread structure
192 		 */
193 		offset = addr - (addr_t) &dummy->regs.per_info;
194 		tmp = *(addr_t *)((addr_t) &child->thread.per_info + offset);
195 
196 	} else
197 		tmp = 0;
198 
199 	return put_user(tmp, (addr_t __user *) data);
200 }
201 
202 /*
203  * Write a word to the user area of a process at location addr. This
204  * operation does have an additional problem compared to peek_user.
205  * Stores to the program status word and on the floating point
206  * control register needs to get checked for validity.
207  */
208 static int
209 poke_user(struct task_struct *child, addr_t addr, addr_t data)
210 {
211 	struct user *dummy = NULL;
212 	addr_t offset, mask;
213 
214 	/*
215 	 * Stupid gdb peeks/pokes the access registers in 64 bit with
216 	 * an alignment of 4. Programmers from hell indeed...
217 	 */
218 	mask = __ADDR_MASK;
219 #ifdef CONFIG_64BIT
220 	if (addr >= (addr_t) &dummy->regs.acrs &&
221 	    addr < (addr_t) &dummy->regs.orig_gpr2)
222 		mask = 3;
223 #endif
224 	if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
225 		return -EIO;
226 
227 	if (addr < (addr_t) &dummy->regs.acrs) {
228 		/*
229 		 * psw and gprs are stored on the stack
230 		 */
231 		if (addr == (addr_t) &dummy->regs.psw.mask &&
232 #ifdef CONFIG_COMPAT
233 		    data != PSW_MASK_MERGE(psw_user32_bits, data) &&
234 #endif
235 		    data != PSW_MASK_MERGE(psw_user_bits, data))
236 			/* Invalid psw mask. */
237 			return -EINVAL;
238 #ifndef CONFIG_64BIT
239 		if (addr == (addr_t) &dummy->regs.psw.addr)
240 			/* I'd like to reject addresses without the
241 			   high order bit but older gdb's rely on it */
242 			data |= PSW_ADDR_AMODE;
243 #endif
244 		*(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
245 
246 	} else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
247 		/*
248 		 * access registers are stored in the thread structure
249 		 */
250 		offset = addr - (addr_t) &dummy->regs.acrs;
251 #ifdef CONFIG_64BIT
252 		/*
253 		 * Very special case: old & broken 64 bit gdb writing
254 		 * to acrs[15] with a 64 bit value. Ignore the lower
255 		 * half of the value and write the upper 32 bit to
256 		 * acrs[15]. Sick...
257 		 */
258 		if (addr == (addr_t) &dummy->regs.acrs[15])
259 			child->thread.acrs[15] = (unsigned int) (data >> 32);
260 		else
261 #endif
262 		*(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
263 
264 	} else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
265 		/*
266 		 * orig_gpr2 is stored on the kernel stack
267 		 */
268 		task_pt_regs(child)->orig_gpr2 = data;
269 
270 	} else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
271 		/*
272 		 * floating point regs. are stored in the thread structure
273 		 */
274 		if (addr == (addr_t) &dummy->regs.fp_regs.fpc &&
275 		    (data & ~((unsigned long) FPC_VALID_MASK
276 			      << (BITS_PER_LONG - 32))) != 0)
277 			return -EINVAL;
278 		offset = addr - (addr_t) &dummy->regs.fp_regs;
279 		*(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
280 
281 	} else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
282 		/*
283 		 * per_info is found in the thread structure
284 		 */
285 		offset = addr - (addr_t) &dummy->regs.per_info;
286 		*(addr_t *)((addr_t) &child->thread.per_info + offset) = data;
287 
288 	}
289 
290 	FixPerRegisters(child);
291 	return 0;
292 }
293 
294 static int
295 do_ptrace_normal(struct task_struct *child, long request, long addr, long data)
296 {
297 	unsigned long tmp;
298 	ptrace_area parea;
299 	int copied, ret;
300 
301 	switch (request) {
302 	case PTRACE_PEEKTEXT:
303 	case PTRACE_PEEKDATA:
304 		/* Remove high order bit from address (only for 31 bit). */
305 		addr &= PSW_ADDR_INSN;
306 		/* read word at location addr. */
307 		copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
308 		if (copied != sizeof(tmp))
309 			return -EIO;
310 		return put_user(tmp, (unsigned long __force __user *) data);
311 
312 	case PTRACE_PEEKUSR:
313 		/* read the word at location addr in the USER area. */
314 		return peek_user(child, addr, data);
315 
316 	case PTRACE_POKETEXT:
317 	case PTRACE_POKEDATA:
318 		/* Remove high order bit from address (only for 31 bit). */
319 		addr &= PSW_ADDR_INSN;
320 		/* write the word at location addr. */
321 		copied = access_process_vm(child, addr, &data, sizeof(data),1);
322 		if (copied != sizeof(data))
323 			return -EIO;
324 		return 0;
325 
326 	case PTRACE_POKEUSR:
327 		/* write the word at location addr in the USER area */
328 		return poke_user(child, addr, data);
329 
330 	case PTRACE_PEEKUSR_AREA:
331 	case PTRACE_POKEUSR_AREA:
332 		if (copy_from_user(&parea, (void __force __user *) addr,
333 							sizeof(parea)))
334 			return -EFAULT;
335 		addr = parea.kernel_addr;
336 		data = parea.process_addr;
337 		copied = 0;
338 		while (copied < parea.len) {
339 			if (request == PTRACE_PEEKUSR_AREA)
340 				ret = peek_user(child, addr, data);
341 			else {
342 				addr_t utmp;
343 				if (get_user(utmp,
344 					     (addr_t __force __user *) data))
345 					return -EFAULT;
346 				ret = poke_user(child, addr, utmp);
347 			}
348 			if (ret)
349 				return ret;
350 			addr += sizeof(unsigned long);
351 			data += sizeof(unsigned long);
352 			copied += sizeof(unsigned long);
353 		}
354 		return 0;
355 	}
356 	return ptrace_request(child, request, addr, data);
357 }
358 
359 #ifdef CONFIG_COMPAT
360 /*
361  * Now the fun part starts... a 31 bit program running in the
362  * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
363  * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
364  * to handle, the difference to the 64 bit versions of the requests
365  * is that the access is done in multiples of 4 byte instead of
366  * 8 bytes (sizeof(unsigned long) on 31/64 bit).
367  * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
368  * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
369  * is a 31 bit program too, the content of struct user can be
370  * emulated. A 31 bit program peeking into the struct user of
371  * a 64 bit program is a no-no.
372  */
373 
374 /*
375  * Same as peek_user but for a 31 bit program.
376  */
377 static int
378 peek_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
379 {
380 	struct user32 *dummy32 = NULL;
381 	per_struct32 *dummy_per32 = NULL;
382 	addr_t offset;
383 	__u32 tmp;
384 
385 	if (!test_thread_flag(TIF_31BIT) ||
386 	    (addr & 3) || addr > sizeof(struct user) - 3)
387 		return -EIO;
388 
389 	if (addr < (addr_t) &dummy32->regs.acrs) {
390 		/*
391 		 * psw and gprs are stored on the stack
392 		 */
393 		if (addr == (addr_t) &dummy32->regs.psw.mask) {
394 			/* Fake a 31 bit psw mask. */
395 			tmp = (__u32)(task_pt_regs(child)->psw.mask >> 32);
396 			tmp = PSW32_MASK_MERGE(psw32_user_bits, tmp);
397 		} else if (addr == (addr_t) &dummy32->regs.psw.addr) {
398 			/* Fake a 31 bit psw address. */
399 			tmp = (__u32) task_pt_regs(child)->psw.addr |
400 				PSW32_ADDR_AMODE31;
401 		} else {
402 			/* gpr 0-15 */
403 			tmp = *(__u32 *)((addr_t) &task_pt_regs(child)->psw +
404 					 addr*2 + 4);
405 		}
406 	} else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
407 		/*
408 		 * access registers are stored in the thread structure
409 		 */
410 		offset = addr - (addr_t) &dummy32->regs.acrs;
411 		tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
412 
413 	} else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
414 		/*
415 		 * orig_gpr2 is stored on the kernel stack
416 		 */
417 		tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
418 
419 	} else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
420 		/*
421 		 * floating point regs. are stored in the thread structure
422 		 */
423 	        offset = addr - (addr_t) &dummy32->regs.fp_regs;
424 		tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
425 
426 	} else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
427 		/*
428 		 * per_info is found in the thread structure
429 		 */
430 		offset = addr - (addr_t) &dummy32->regs.per_info;
431 		/* This is magic. See per_struct and per_struct32. */
432 		if ((offset >= (addr_t) &dummy_per32->control_regs &&
433 		     offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
434 		    (offset >= (addr_t) &dummy_per32->starting_addr &&
435 		     offset <= (addr_t) &dummy_per32->ending_addr) ||
436 		    offset == (addr_t) &dummy_per32->lowcore.words.address)
437 			offset = offset*2 + 4;
438 		else
439 			offset = offset*2;
440 		tmp = *(__u32 *)((addr_t) &child->thread.per_info + offset);
441 
442 	} else
443 		tmp = 0;
444 
445 	return put_user(tmp, (__u32 __user *) data);
446 }
447 
448 /*
449  * Same as poke_user but for a 31 bit program.
450  */
451 static int
452 poke_user_emu31(struct task_struct *child, addr_t addr, addr_t data)
453 {
454 	struct user32 *dummy32 = NULL;
455 	per_struct32 *dummy_per32 = NULL;
456 	addr_t offset;
457 	__u32 tmp;
458 
459 	if (!test_thread_flag(TIF_31BIT) ||
460 	    (addr & 3) || addr > sizeof(struct user32) - 3)
461 		return -EIO;
462 
463 	tmp = (__u32) data;
464 
465 	if (addr < (addr_t) &dummy32->regs.acrs) {
466 		/*
467 		 * psw, gprs, acrs and orig_gpr2 are stored on the stack
468 		 */
469 		if (addr == (addr_t) &dummy32->regs.psw.mask) {
470 			/* Build a 64 bit psw mask from 31 bit mask. */
471 			if (tmp != PSW32_MASK_MERGE(psw32_user_bits, tmp))
472 				/* Invalid psw mask. */
473 				return -EINVAL;
474 			task_pt_regs(child)->psw.mask =
475 				PSW_MASK_MERGE(psw_user32_bits, (__u64) tmp << 32);
476 		} else if (addr == (addr_t) &dummy32->regs.psw.addr) {
477 			/* Build a 64 bit psw address from 31 bit address. */
478 			task_pt_regs(child)->psw.addr =
479 				(__u64) tmp & PSW32_ADDR_INSN;
480 		} else {
481 			/* gpr 0-15 */
482 			*(__u32*)((addr_t) &task_pt_regs(child)->psw
483 				  + addr*2 + 4) = tmp;
484 		}
485 	} else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
486 		/*
487 		 * access registers are stored in the thread structure
488 		 */
489 		offset = addr - (addr_t) &dummy32->regs.acrs;
490 		*(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
491 
492 	} else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
493 		/*
494 		 * orig_gpr2 is stored on the kernel stack
495 		 */
496 		*(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
497 
498 	} else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
499 		/*
500 		 * floating point regs. are stored in the thread structure
501 		 */
502 		if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
503 		    (tmp & ~FPC_VALID_MASK) != 0)
504 			/* Invalid floating point control. */
505 			return -EINVAL;
506 	        offset = addr - (addr_t) &dummy32->regs.fp_regs;
507 		*(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
508 
509 	} else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
510 		/*
511 		 * per_info is found in the thread structure.
512 		 */
513 		offset = addr - (addr_t) &dummy32->regs.per_info;
514 		/*
515 		 * This is magic. See per_struct and per_struct32.
516 		 * By incident the offsets in per_struct are exactly
517 		 * twice the offsets in per_struct32 for all fields.
518 		 * The 8 byte fields need special handling though,
519 		 * because the second half (bytes 4-7) is needed and
520 		 * not the first half.
521 		 */
522 		if ((offset >= (addr_t) &dummy_per32->control_regs &&
523 		     offset < (addr_t) (&dummy_per32->control_regs + 1)) ||
524 		    (offset >= (addr_t) &dummy_per32->starting_addr &&
525 		     offset <= (addr_t) &dummy_per32->ending_addr) ||
526 		    offset == (addr_t) &dummy_per32->lowcore.words.address)
527 			offset = offset*2 + 4;
528 		else
529 			offset = offset*2;
530 		*(__u32 *)((addr_t) &child->thread.per_info + offset) = tmp;
531 
532 	}
533 
534 	FixPerRegisters(child);
535 	return 0;
536 }
537 
538 static int
539 do_ptrace_emu31(struct task_struct *child, long request, long addr, long data)
540 {
541 	unsigned int tmp;  /* 4 bytes !! */
542 	ptrace_area_emu31 parea;
543 	int copied, ret;
544 
545 	switch (request) {
546 	case PTRACE_PEEKTEXT:
547 	case PTRACE_PEEKDATA:
548 		/* read word at location addr. */
549 		copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
550 		if (copied != sizeof(tmp))
551 			return -EIO;
552 		return put_user(tmp, (unsigned int __force __user *) data);
553 
554 	case PTRACE_PEEKUSR:
555 		/* read the word at location addr in the USER area. */
556 		return peek_user_emu31(child, addr, data);
557 
558 	case PTRACE_POKETEXT:
559 	case PTRACE_POKEDATA:
560 		/* write the word at location addr. */
561 		tmp = data;
562 		copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 1);
563 		if (copied != sizeof(tmp))
564 			return -EIO;
565 		return 0;
566 
567 	case PTRACE_POKEUSR:
568 		/* write the word at location addr in the USER area */
569 		return poke_user_emu31(child, addr, data);
570 
571 	case PTRACE_PEEKUSR_AREA:
572 	case PTRACE_POKEUSR_AREA:
573 		if (copy_from_user(&parea, (void __force __user *) addr,
574 							sizeof(parea)))
575 			return -EFAULT;
576 		addr = parea.kernel_addr;
577 		data = parea.process_addr;
578 		copied = 0;
579 		while (copied < parea.len) {
580 			if (request == PTRACE_PEEKUSR_AREA)
581 				ret = peek_user_emu31(child, addr, data);
582 			else {
583 				__u32 utmp;
584 				if (get_user(utmp,
585 					     (__u32 __force __user *) data))
586 					return -EFAULT;
587 				ret = poke_user_emu31(child, addr, utmp);
588 			}
589 			if (ret)
590 				return ret;
591 			addr += sizeof(unsigned int);
592 			data += sizeof(unsigned int);
593 			copied += sizeof(unsigned int);
594 		}
595 		return 0;
596 	case PTRACE_GETEVENTMSG:
597 		return put_user((__u32) child->ptrace_message,
598 				(unsigned int __force __user *) data);
599 	case PTRACE_GETSIGINFO:
600 		if (child->last_siginfo == NULL)
601 			return -EINVAL;
602 		return copy_siginfo_to_user32((compat_siginfo_t
603 					       __force __user *) data,
604 					      child->last_siginfo);
605 	case PTRACE_SETSIGINFO:
606 		if (child->last_siginfo == NULL)
607 			return -EINVAL;
608 		return copy_siginfo_from_user32(child->last_siginfo,
609 						(compat_siginfo_t
610 						 __force __user *) data);
611 	}
612 	return ptrace_request(child, request, addr, data);
613 }
614 #endif
615 
616 #define PT32_IEEE_IP 0x13c
617 
618 static int
619 do_ptrace(struct task_struct *child, long request, long addr, long data)
620 {
621 	int ret;
622 
623 	if (request == PTRACE_ATTACH)
624 		return ptrace_attach(child);
625 
626 	/*
627 	 * Special cases to get/store the ieee instructions pointer.
628 	 */
629 	if (child == current) {
630 		if (request == PTRACE_PEEKUSR && addr == PT_IEEE_IP)
631 			return peek_user(child, addr, data);
632 		if (request == PTRACE_POKEUSR && addr == PT_IEEE_IP)
633 			return poke_user(child, addr, data);
634 #ifdef CONFIG_COMPAT
635 		if (request == PTRACE_PEEKUSR &&
636 		    addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
637 			return peek_user_emu31(child, addr, data);
638 		if (request == PTRACE_POKEUSR &&
639 		    addr == PT32_IEEE_IP && test_thread_flag(TIF_31BIT))
640 			return poke_user_emu31(child, addr, data);
641 #endif
642 	}
643 
644 	ret = ptrace_check_attach(child, request == PTRACE_KILL);
645 	if (ret < 0)
646 		return ret;
647 
648 	switch (request) {
649 	case PTRACE_SYSCALL:
650 		/* continue and stop at next (return from) syscall */
651 	case PTRACE_CONT:
652 		/* restart after signal. */
653 		if (!valid_signal(data))
654 			return -EIO;
655 		if (request == PTRACE_SYSCALL)
656 			set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
657 		else
658 			clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
659 		child->exit_code = data;
660 		/* make sure the single step bit is not set. */
661 		clear_single_step(child);
662 		wake_up_process(child);
663 		return 0;
664 
665 	case PTRACE_KILL:
666 		/*
667 		 * make the child exit.  Best I can do is send it a sigkill.
668 		 * perhaps it should be put in the status that it wants to
669 		 * exit.
670 		 */
671 		if (child->exit_state == EXIT_ZOMBIE) /* already dead */
672 			return 0;
673 		child->exit_code = SIGKILL;
674 		/* make sure the single step bit is not set. */
675 		clear_single_step(child);
676 		wake_up_process(child);
677 		return 0;
678 
679 	case PTRACE_SINGLESTEP:
680 		/* set the trap flag. */
681 		if (!valid_signal(data))
682 			return -EIO;
683 		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
684 		child->exit_code = data;
685 		if (data)
686 			set_tsk_thread_flag(child, TIF_SINGLE_STEP);
687 		else
688 			set_single_step(child);
689 		/* give it a chance to run. */
690 		wake_up_process(child);
691 		return 0;
692 
693 	case PTRACE_DETACH:
694 		/* detach a process that was attached. */
695 		return ptrace_detach(child, data);
696 
697 
698 	/* Do requests that differ for 31/64 bit */
699 	default:
700 #ifdef CONFIG_COMPAT
701 		if (test_thread_flag(TIF_31BIT))
702 			return do_ptrace_emu31(child, request, addr, data);
703 #endif
704 		return do_ptrace_normal(child, request, addr, data);
705 	}
706 	/* Not reached.  */
707 	return -EIO;
708 }
709 
710 asmlinkage long
711 sys_ptrace(long request, long pid, long addr, long data)
712 {
713 	struct task_struct *child;
714 	int ret;
715 
716 	lock_kernel();
717 	if (request == PTRACE_TRACEME) {
718 		 ret = ptrace_traceme();
719 		 goto out;
720 	}
721 
722 	child = ptrace_get_task_struct(pid);
723 	if (IS_ERR(child)) {
724 		ret = PTR_ERR(child);
725 		goto out;
726 	}
727 
728 	ret = do_ptrace(child, request, addr, data);
729 	put_task_struct(child);
730 out:
731 	unlock_kernel();
732 	return ret;
733 }
734 
735 asmlinkage void
736 syscall_trace(struct pt_regs *regs, int entryexit)
737 {
738 	if (unlikely(current->audit_context) && entryexit)
739 		audit_syscall_exit(AUDITSC_RESULT(regs->gprs[2]), regs->gprs[2]);
740 
741 	if (!test_thread_flag(TIF_SYSCALL_TRACE))
742 		goto out;
743 	if (!(current->ptrace & PT_PTRACED))
744 		goto out;
745 	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
746 				 ? 0x80 : 0));
747 
748 	/*
749 	 * If the debuffer has set an invalid system call number,
750 	 * we prepare to skip the system call restart handling.
751 	 */
752 	if (!entryexit && regs->gprs[2] >= NR_syscalls)
753 		regs->trap = -1;
754 
755 	/*
756 	 * this isn't the same as continuing with a signal, but it will do
757 	 * for normal use.  strace only continues with a signal if the
758 	 * stopping signal is not SIGTRAP.  -brl
759 	 */
760 	if (current->exit_code) {
761 		send_sig(current->exit_code, current, 1);
762 		current->exit_code = 0;
763 	}
764  out:
765 	if (unlikely(current->audit_context) && !entryexit)
766 		audit_syscall_entry(test_thread_flag(TIF_31BIT)?AUDIT_ARCH_S390:AUDIT_ARCH_S390X,
767 				    regs->gprs[2], regs->orig_gpr2, regs->gprs[3],
768 				    regs->gprs[4], regs->gprs[5]);
769 }
770