xref: /linux/arch/s390/kernel/ptrace.c (revision 93d90ad708b8da6efc0e487b66111aa9db7f70c7)
1 /*
2  *  Ptrace user space interface.
3  *
4  *    Copyright IBM Corp. 1999, 2010
5  *    Author(s): Denis Joseph Barrow
6  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/security.h>
17 #include <linux/audit.h>
18 #include <linux/signal.h>
19 #include <linux/elf.h>
20 #include <linux/regset.h>
21 #include <linux/tracehook.h>
22 #include <linux/seccomp.h>
23 #include <linux/compat.h>
24 #include <trace/syscall.h>
25 #include <asm/segment.h>
26 #include <asm/page.h>
27 #include <asm/pgtable.h>
28 #include <asm/pgalloc.h>
29 #include <asm/uaccess.h>
30 #include <asm/unistd.h>
31 #include <asm/switch_to.h>
32 #include "entry.h"
33 
34 #ifdef CONFIG_COMPAT
35 #include "compat_ptrace.h"
36 #endif
37 
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/syscalls.h>
40 
41 void update_cr_regs(struct task_struct *task)
42 {
43 	struct pt_regs *regs = task_pt_regs(task);
44 	struct thread_struct *thread = &task->thread;
45 	struct per_regs old, new;
46 
47 #ifdef CONFIG_64BIT
48 	/* Take care of the enable/disable of transactional execution. */
49 	if (MACHINE_HAS_TE || MACHINE_HAS_VX) {
50 		unsigned long cr, cr_new;
51 
52 		__ctl_store(cr, 0, 0);
53 		cr_new = cr;
54 		if (MACHINE_HAS_TE) {
55 			/* Set or clear transaction execution TXC bit 8. */
56 			cr_new |= (1UL << 55);
57 			if (task->thread.per_flags & PER_FLAG_NO_TE)
58 				cr_new &= ~(1UL << 55);
59 		}
60 		if (MACHINE_HAS_VX) {
61 			/* Enable/disable of vector extension */
62 			cr_new &= ~(1UL << 17);
63 			if (task->thread.vxrs)
64 				cr_new |= (1UL << 17);
65 		}
66 		if (cr_new != cr)
67 			__ctl_load(cr_new, 0, 0);
68 		if (MACHINE_HAS_TE) {
69 			/* Set/clear transaction execution TDC bits 62/63. */
70 			__ctl_store(cr, 2, 2);
71 			cr_new = cr & ~3UL;
72 			if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND) {
73 				if (task->thread.per_flags &
74 				    PER_FLAG_TE_ABORT_RAND_TEND)
75 					cr_new |= 1UL;
76 				else
77 					cr_new |= 2UL;
78 			}
79 			if (cr_new != cr)
80 				__ctl_load(cr_new, 2, 2);
81 		}
82 	}
83 #endif
84 	/* Copy user specified PER registers */
85 	new.control = thread->per_user.control;
86 	new.start = thread->per_user.start;
87 	new.end = thread->per_user.end;
88 
89 	/* merge TIF_SINGLE_STEP into user specified PER registers. */
90 	if (test_tsk_thread_flag(task, TIF_SINGLE_STEP) ||
91 	    test_tsk_thread_flag(task, TIF_UPROBE_SINGLESTEP)) {
92 		if (test_tsk_thread_flag(task, TIF_BLOCK_STEP))
93 			new.control |= PER_EVENT_BRANCH;
94 		else
95 			new.control |= PER_EVENT_IFETCH;
96 #ifdef CONFIG_64BIT
97 		new.control |= PER_CONTROL_SUSPENSION;
98 		new.control |= PER_EVENT_TRANSACTION_END;
99 #endif
100 		if (test_tsk_thread_flag(task, TIF_UPROBE_SINGLESTEP))
101 			new.control |= PER_EVENT_IFETCH;
102 		new.start = 0;
103 		new.end = PSW_ADDR_INSN;
104 	}
105 
106 	/* Take care of the PER enablement bit in the PSW. */
107 	if (!(new.control & PER_EVENT_MASK)) {
108 		regs->psw.mask &= ~PSW_MASK_PER;
109 		return;
110 	}
111 	regs->psw.mask |= PSW_MASK_PER;
112 	__ctl_store(old, 9, 11);
113 	if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
114 		__ctl_load(new, 9, 11);
115 }
116 
117 void user_enable_single_step(struct task_struct *task)
118 {
119 	clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
120 	set_tsk_thread_flag(task, TIF_SINGLE_STEP);
121 }
122 
123 void user_disable_single_step(struct task_struct *task)
124 {
125 	clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
126 	clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
127 }
128 
129 void user_enable_block_step(struct task_struct *task)
130 {
131 	set_tsk_thread_flag(task, TIF_SINGLE_STEP);
132 	set_tsk_thread_flag(task, TIF_BLOCK_STEP);
133 }
134 
135 /*
136  * Called by kernel/ptrace.c when detaching..
137  *
138  * Clear all debugging related fields.
139  */
140 void ptrace_disable(struct task_struct *task)
141 {
142 	memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
143 	memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
144 	clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
145 	clear_pt_regs_flag(task_pt_regs(task), PIF_PER_TRAP);
146 	task->thread.per_flags = 0;
147 }
148 
149 #ifndef CONFIG_64BIT
150 # define __ADDR_MASK 3
151 #else
152 # define __ADDR_MASK 7
153 #endif
154 
155 static inline unsigned long __peek_user_per(struct task_struct *child,
156 					    addr_t addr)
157 {
158 	struct per_struct_kernel *dummy = NULL;
159 
160 	if (addr == (addr_t) &dummy->cr9)
161 		/* Control bits of the active per set. */
162 		return test_thread_flag(TIF_SINGLE_STEP) ?
163 			PER_EVENT_IFETCH : child->thread.per_user.control;
164 	else if (addr == (addr_t) &dummy->cr10)
165 		/* Start address of the active per set. */
166 		return test_thread_flag(TIF_SINGLE_STEP) ?
167 			0 : child->thread.per_user.start;
168 	else if (addr == (addr_t) &dummy->cr11)
169 		/* End address of the active per set. */
170 		return test_thread_flag(TIF_SINGLE_STEP) ?
171 			PSW_ADDR_INSN : child->thread.per_user.end;
172 	else if (addr == (addr_t) &dummy->bits)
173 		/* Single-step bit. */
174 		return test_thread_flag(TIF_SINGLE_STEP) ?
175 			(1UL << (BITS_PER_LONG - 1)) : 0;
176 	else if (addr == (addr_t) &dummy->starting_addr)
177 		/* Start address of the user specified per set. */
178 		return child->thread.per_user.start;
179 	else if (addr == (addr_t) &dummy->ending_addr)
180 		/* End address of the user specified per set. */
181 		return child->thread.per_user.end;
182 	else if (addr == (addr_t) &dummy->perc_atmid)
183 		/* PER code, ATMID and AI of the last PER trap */
184 		return (unsigned long)
185 			child->thread.per_event.cause << (BITS_PER_LONG - 16);
186 	else if (addr == (addr_t) &dummy->address)
187 		/* Address of the last PER trap */
188 		return child->thread.per_event.address;
189 	else if (addr == (addr_t) &dummy->access_id)
190 		/* Access id of the last PER trap */
191 		return (unsigned long)
192 			child->thread.per_event.paid << (BITS_PER_LONG - 8);
193 	return 0;
194 }
195 
196 /*
197  * Read the word at offset addr from the user area of a process. The
198  * trouble here is that the information is littered over different
199  * locations. The process registers are found on the kernel stack,
200  * the floating point stuff and the trace settings are stored in
201  * the task structure. In addition the different structures in
202  * struct user contain pad bytes that should be read as zeroes.
203  * Lovely...
204  */
205 static unsigned long __peek_user(struct task_struct *child, addr_t addr)
206 {
207 	struct user *dummy = NULL;
208 	addr_t offset, tmp;
209 
210 	if (addr < (addr_t) &dummy->regs.acrs) {
211 		/*
212 		 * psw and gprs are stored on the stack
213 		 */
214 		tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
215 		if (addr == (addr_t) &dummy->regs.psw.mask) {
216 			/* Return a clean psw mask. */
217 			tmp &= PSW_MASK_USER | PSW_MASK_RI;
218 			tmp |= PSW_USER_BITS;
219 		}
220 
221 	} else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
222 		/*
223 		 * access registers are stored in the thread structure
224 		 */
225 		offset = addr - (addr_t) &dummy->regs.acrs;
226 #ifdef CONFIG_64BIT
227 		/*
228 		 * Very special case: old & broken 64 bit gdb reading
229 		 * from acrs[15]. Result is a 64 bit value. Read the
230 		 * 32 bit acrs[15] value and shift it by 32. Sick...
231 		 */
232 		if (addr == (addr_t) &dummy->regs.acrs[15])
233 			tmp = ((unsigned long) child->thread.acrs[15]) << 32;
234 		else
235 #endif
236 		tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
237 
238 	} else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
239 		/*
240 		 * orig_gpr2 is stored on the kernel stack
241 		 */
242 		tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
243 
244 	} else if (addr < (addr_t) &dummy->regs.fp_regs) {
245 		/*
246 		 * prevent reads of padding hole between
247 		 * orig_gpr2 and fp_regs on s390.
248 		 */
249 		tmp = 0;
250 
251 	} else if (addr == (addr_t) &dummy->regs.fp_regs.fpc) {
252 		/*
253 		 * floating point control reg. is in the thread structure
254 		 */
255 		tmp = child->thread.fp_regs.fpc;
256 		tmp <<= BITS_PER_LONG - 32;
257 
258 	} else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
259 		/*
260 		 * floating point regs. are either in child->thread.fp_regs
261 		 * or the child->thread.vxrs array
262 		 */
263 		offset = addr - (addr_t) &dummy->regs.fp_regs.fprs;
264 #ifdef CONFIG_64BIT
265 		if (child->thread.vxrs)
266 			tmp = *(addr_t *)
267 			       ((addr_t) child->thread.vxrs + 2*offset);
268 		else
269 #endif
270 			tmp = *(addr_t *)
271 			       ((addr_t) &child->thread.fp_regs.fprs + offset);
272 
273 	} else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
274 		/*
275 		 * Handle access to the per_info structure.
276 		 */
277 		addr -= (addr_t) &dummy->regs.per_info;
278 		tmp = __peek_user_per(child, addr);
279 
280 	} else
281 		tmp = 0;
282 
283 	return tmp;
284 }
285 
286 static int
287 peek_user(struct task_struct *child, addr_t addr, addr_t data)
288 {
289 	addr_t tmp, mask;
290 
291 	/*
292 	 * Stupid gdb peeks/pokes the access registers in 64 bit with
293 	 * an alignment of 4. Programmers from hell...
294 	 */
295 	mask = __ADDR_MASK;
296 #ifdef CONFIG_64BIT
297 	if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
298 	    addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
299 		mask = 3;
300 #endif
301 	if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
302 		return -EIO;
303 
304 	tmp = __peek_user(child, addr);
305 	return put_user(tmp, (addr_t __user *) data);
306 }
307 
308 static inline void __poke_user_per(struct task_struct *child,
309 				   addr_t addr, addr_t data)
310 {
311 	struct per_struct_kernel *dummy = NULL;
312 
313 	/*
314 	 * There are only three fields in the per_info struct that the
315 	 * debugger user can write to.
316 	 * 1) cr9: the debugger wants to set a new PER event mask
317 	 * 2) starting_addr: the debugger wants to set a new starting
318 	 *    address to use with the PER event mask.
319 	 * 3) ending_addr: the debugger wants to set a new ending
320 	 *    address to use with the PER event mask.
321 	 * The user specified PER event mask and the start and end
322 	 * addresses are used only if single stepping is not in effect.
323 	 * Writes to any other field in per_info are ignored.
324 	 */
325 	if (addr == (addr_t) &dummy->cr9)
326 		/* PER event mask of the user specified per set. */
327 		child->thread.per_user.control =
328 			data & (PER_EVENT_MASK | PER_CONTROL_MASK);
329 	else if (addr == (addr_t) &dummy->starting_addr)
330 		/* Starting address of the user specified per set. */
331 		child->thread.per_user.start = data;
332 	else if (addr == (addr_t) &dummy->ending_addr)
333 		/* Ending address of the user specified per set. */
334 		child->thread.per_user.end = data;
335 }
336 
337 /*
338  * Write a word to the user area of a process at location addr. This
339  * operation does have an additional problem compared to peek_user.
340  * Stores to the program status word and on the floating point
341  * control register needs to get checked for validity.
342  */
343 static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
344 {
345 	struct user *dummy = NULL;
346 	addr_t offset;
347 
348 	if (addr < (addr_t) &dummy->regs.acrs) {
349 		/*
350 		 * psw and gprs are stored on the stack
351 		 */
352 		if (addr == (addr_t) &dummy->regs.psw.mask) {
353 			unsigned long mask = PSW_MASK_USER;
354 
355 			mask |= is_ri_task(child) ? PSW_MASK_RI : 0;
356 			if ((data ^ PSW_USER_BITS) & ~mask)
357 				/* Invalid psw mask. */
358 				return -EINVAL;
359 			if ((data & PSW_MASK_ASC) == PSW_ASC_HOME)
360 				/* Invalid address-space-control bits */
361 				return -EINVAL;
362 			if ((data & PSW_MASK_EA) && !(data & PSW_MASK_BA))
363 				/* Invalid addressing mode bits */
364 				return -EINVAL;
365 		}
366 		*(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
367 
368 	} else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
369 		/*
370 		 * access registers are stored in the thread structure
371 		 */
372 		offset = addr - (addr_t) &dummy->regs.acrs;
373 #ifdef CONFIG_64BIT
374 		/*
375 		 * Very special case: old & broken 64 bit gdb writing
376 		 * to acrs[15] with a 64 bit value. Ignore the lower
377 		 * half of the value and write the upper 32 bit to
378 		 * acrs[15]. Sick...
379 		 */
380 		if (addr == (addr_t) &dummy->regs.acrs[15])
381 			child->thread.acrs[15] = (unsigned int) (data >> 32);
382 		else
383 #endif
384 		*(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
385 
386 	} else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
387 		/*
388 		 * orig_gpr2 is stored on the kernel stack
389 		 */
390 		task_pt_regs(child)->orig_gpr2 = data;
391 
392 	} else if (addr < (addr_t) &dummy->regs.fp_regs) {
393 		/*
394 		 * prevent writes of padding hole between
395 		 * orig_gpr2 and fp_regs on s390.
396 		 */
397 		return 0;
398 
399 	} else if (addr == (addr_t) &dummy->regs.fp_regs.fpc) {
400 		/*
401 		 * floating point control reg. is in the thread structure
402 		 */
403 		if ((unsigned int) data != 0 ||
404 		    test_fp_ctl(data >> (BITS_PER_LONG - 32)))
405 			return -EINVAL;
406 		child->thread.fp_regs.fpc = data >> (BITS_PER_LONG - 32);
407 
408 	} else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
409 		/*
410 		 * floating point regs. are either in child->thread.fp_regs
411 		 * or the child->thread.vxrs array
412 		 */
413 		offset = addr - (addr_t) &dummy->regs.fp_regs.fprs;
414 #ifdef CONFIG_64BIT
415 		if (child->thread.vxrs)
416 			*(addr_t *)((addr_t)
417 				child->thread.vxrs + 2*offset) = data;
418 		else
419 #endif
420 			*(addr_t *)((addr_t)
421 				&child->thread.fp_regs.fprs + offset) = data;
422 
423 	} else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
424 		/*
425 		 * Handle access to the per_info structure.
426 		 */
427 		addr -= (addr_t) &dummy->regs.per_info;
428 		__poke_user_per(child, addr, data);
429 
430 	}
431 
432 	return 0;
433 }
434 
435 static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
436 {
437 	addr_t mask;
438 
439 	/*
440 	 * Stupid gdb peeks/pokes the access registers in 64 bit with
441 	 * an alignment of 4. Programmers from hell indeed...
442 	 */
443 	mask = __ADDR_MASK;
444 #ifdef CONFIG_64BIT
445 	if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
446 	    addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
447 		mask = 3;
448 #endif
449 	if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
450 		return -EIO;
451 
452 	return __poke_user(child, addr, data);
453 }
454 
455 long arch_ptrace(struct task_struct *child, long request,
456 		 unsigned long addr, unsigned long data)
457 {
458 	ptrace_area parea;
459 	int copied, ret;
460 
461 	switch (request) {
462 	case PTRACE_PEEKUSR:
463 		/* read the word at location addr in the USER area. */
464 		return peek_user(child, addr, data);
465 
466 	case PTRACE_POKEUSR:
467 		/* write the word at location addr in the USER area */
468 		return poke_user(child, addr, data);
469 
470 	case PTRACE_PEEKUSR_AREA:
471 	case PTRACE_POKEUSR_AREA:
472 		if (copy_from_user(&parea, (void __force __user *) addr,
473 							sizeof(parea)))
474 			return -EFAULT;
475 		addr = parea.kernel_addr;
476 		data = parea.process_addr;
477 		copied = 0;
478 		while (copied < parea.len) {
479 			if (request == PTRACE_PEEKUSR_AREA)
480 				ret = peek_user(child, addr, data);
481 			else {
482 				addr_t utmp;
483 				if (get_user(utmp,
484 					     (addr_t __force __user *) data))
485 					return -EFAULT;
486 				ret = poke_user(child, addr, utmp);
487 			}
488 			if (ret)
489 				return ret;
490 			addr += sizeof(unsigned long);
491 			data += sizeof(unsigned long);
492 			copied += sizeof(unsigned long);
493 		}
494 		return 0;
495 	case PTRACE_GET_LAST_BREAK:
496 		put_user(task_thread_info(child)->last_break,
497 			 (unsigned long __user *) data);
498 		return 0;
499 	case PTRACE_ENABLE_TE:
500 		if (!MACHINE_HAS_TE)
501 			return -EIO;
502 		child->thread.per_flags &= ~PER_FLAG_NO_TE;
503 		return 0;
504 	case PTRACE_DISABLE_TE:
505 		if (!MACHINE_HAS_TE)
506 			return -EIO;
507 		child->thread.per_flags |= PER_FLAG_NO_TE;
508 		child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
509 		return 0;
510 	case PTRACE_TE_ABORT_RAND:
511 		if (!MACHINE_HAS_TE || (child->thread.per_flags & PER_FLAG_NO_TE))
512 			return -EIO;
513 		switch (data) {
514 		case 0UL:
515 			child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
516 			break;
517 		case 1UL:
518 			child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
519 			child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND_TEND;
520 			break;
521 		case 2UL:
522 			child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
523 			child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND_TEND;
524 			break;
525 		default:
526 			return -EINVAL;
527 		}
528 		return 0;
529 	default:
530 		/* Removing high order bit from addr (only for 31 bit). */
531 		addr &= PSW_ADDR_INSN;
532 		return ptrace_request(child, request, addr, data);
533 	}
534 }
535 
536 #ifdef CONFIG_COMPAT
537 /*
538  * Now the fun part starts... a 31 bit program running in the
539  * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
540  * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
541  * to handle, the difference to the 64 bit versions of the requests
542  * is that the access is done in multiples of 4 byte instead of
543  * 8 bytes (sizeof(unsigned long) on 31/64 bit).
544  * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
545  * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
546  * is a 31 bit program too, the content of struct user can be
547  * emulated. A 31 bit program peeking into the struct user of
548  * a 64 bit program is a no-no.
549  */
550 
551 /*
552  * Same as peek_user_per but for a 31 bit program.
553  */
554 static inline __u32 __peek_user_per_compat(struct task_struct *child,
555 					   addr_t addr)
556 {
557 	struct compat_per_struct_kernel *dummy32 = NULL;
558 
559 	if (addr == (addr_t) &dummy32->cr9)
560 		/* Control bits of the active per set. */
561 		return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
562 			PER_EVENT_IFETCH : child->thread.per_user.control;
563 	else if (addr == (addr_t) &dummy32->cr10)
564 		/* Start address of the active per set. */
565 		return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
566 			0 : child->thread.per_user.start;
567 	else if (addr == (addr_t) &dummy32->cr11)
568 		/* End address of the active per set. */
569 		return test_thread_flag(TIF_SINGLE_STEP) ?
570 			PSW32_ADDR_INSN : child->thread.per_user.end;
571 	else if (addr == (addr_t) &dummy32->bits)
572 		/* Single-step bit. */
573 		return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
574 			0x80000000 : 0;
575 	else if (addr == (addr_t) &dummy32->starting_addr)
576 		/* Start address of the user specified per set. */
577 		return (__u32) child->thread.per_user.start;
578 	else if (addr == (addr_t) &dummy32->ending_addr)
579 		/* End address of the user specified per set. */
580 		return (__u32) child->thread.per_user.end;
581 	else if (addr == (addr_t) &dummy32->perc_atmid)
582 		/* PER code, ATMID and AI of the last PER trap */
583 		return (__u32) child->thread.per_event.cause << 16;
584 	else if (addr == (addr_t) &dummy32->address)
585 		/* Address of the last PER trap */
586 		return (__u32) child->thread.per_event.address;
587 	else if (addr == (addr_t) &dummy32->access_id)
588 		/* Access id of the last PER trap */
589 		return (__u32) child->thread.per_event.paid << 24;
590 	return 0;
591 }
592 
593 /*
594  * Same as peek_user but for a 31 bit program.
595  */
596 static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
597 {
598 	struct compat_user *dummy32 = NULL;
599 	addr_t offset;
600 	__u32 tmp;
601 
602 	if (addr < (addr_t) &dummy32->regs.acrs) {
603 		struct pt_regs *regs = task_pt_regs(child);
604 		/*
605 		 * psw and gprs are stored on the stack
606 		 */
607 		if (addr == (addr_t) &dummy32->regs.psw.mask) {
608 			/* Fake a 31 bit psw mask. */
609 			tmp = (__u32)(regs->psw.mask >> 32);
610 			tmp &= PSW32_MASK_USER | PSW32_MASK_RI;
611 			tmp |= PSW32_USER_BITS;
612 		} else if (addr == (addr_t) &dummy32->regs.psw.addr) {
613 			/* Fake a 31 bit psw address. */
614 			tmp = (__u32) regs->psw.addr |
615 				(__u32)(regs->psw.mask & PSW_MASK_BA);
616 		} else {
617 			/* gpr 0-15 */
618 			tmp = *(__u32 *)((addr_t) &regs->psw + addr*2 + 4);
619 		}
620 	} else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
621 		/*
622 		 * access registers are stored in the thread structure
623 		 */
624 		offset = addr - (addr_t) &dummy32->regs.acrs;
625 		tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
626 
627 	} else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
628 		/*
629 		 * orig_gpr2 is stored on the kernel stack
630 		 */
631 		tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
632 
633 	} else if (addr < (addr_t) &dummy32->regs.fp_regs) {
634 		/*
635 		 * prevent reads of padding hole between
636 		 * orig_gpr2 and fp_regs on s390.
637 		 */
638 		tmp = 0;
639 
640 	} else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
641 		/*
642 		 * floating point control reg. is in the thread structure
643 		 */
644 		tmp = child->thread.fp_regs.fpc;
645 
646 	} else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
647 		/*
648 		 * floating point regs. are either in child->thread.fp_regs
649 		 * or the child->thread.vxrs array
650 		 */
651 		offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
652 #ifdef CONFIG_64BIT
653 		if (child->thread.vxrs)
654 			tmp = *(__u32 *)
655 			       ((addr_t) child->thread.vxrs + 2*offset);
656 		else
657 #endif
658 			tmp = *(__u32 *)
659 			       ((addr_t) &child->thread.fp_regs.fprs + offset);
660 
661 	} else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
662 		/*
663 		 * Handle access to the per_info structure.
664 		 */
665 		addr -= (addr_t) &dummy32->regs.per_info;
666 		tmp = __peek_user_per_compat(child, addr);
667 
668 	} else
669 		tmp = 0;
670 
671 	return tmp;
672 }
673 
674 static int peek_user_compat(struct task_struct *child,
675 			    addr_t addr, addr_t data)
676 {
677 	__u32 tmp;
678 
679 	if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
680 		return -EIO;
681 
682 	tmp = __peek_user_compat(child, addr);
683 	return put_user(tmp, (__u32 __user *) data);
684 }
685 
686 /*
687  * Same as poke_user_per but for a 31 bit program.
688  */
689 static inline void __poke_user_per_compat(struct task_struct *child,
690 					  addr_t addr, __u32 data)
691 {
692 	struct compat_per_struct_kernel *dummy32 = NULL;
693 
694 	if (addr == (addr_t) &dummy32->cr9)
695 		/* PER event mask of the user specified per set. */
696 		child->thread.per_user.control =
697 			data & (PER_EVENT_MASK | PER_CONTROL_MASK);
698 	else if (addr == (addr_t) &dummy32->starting_addr)
699 		/* Starting address of the user specified per set. */
700 		child->thread.per_user.start = data;
701 	else if (addr == (addr_t) &dummy32->ending_addr)
702 		/* Ending address of the user specified per set. */
703 		child->thread.per_user.end = data;
704 }
705 
706 /*
707  * Same as poke_user but for a 31 bit program.
708  */
709 static int __poke_user_compat(struct task_struct *child,
710 			      addr_t addr, addr_t data)
711 {
712 	struct compat_user *dummy32 = NULL;
713 	__u32 tmp = (__u32) data;
714 	addr_t offset;
715 
716 	if (addr < (addr_t) &dummy32->regs.acrs) {
717 		struct pt_regs *regs = task_pt_regs(child);
718 		/*
719 		 * psw, gprs, acrs and orig_gpr2 are stored on the stack
720 		 */
721 		if (addr == (addr_t) &dummy32->regs.psw.mask) {
722 			__u32 mask = PSW32_MASK_USER;
723 
724 			mask |= is_ri_task(child) ? PSW32_MASK_RI : 0;
725 			/* Build a 64 bit psw mask from 31 bit mask. */
726 			if ((tmp ^ PSW32_USER_BITS) & ~mask)
727 				/* Invalid psw mask. */
728 				return -EINVAL;
729 			if ((data & PSW32_MASK_ASC) == PSW32_ASC_HOME)
730 				/* Invalid address-space-control bits */
731 				return -EINVAL;
732 			regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
733 				(regs->psw.mask & PSW_MASK_BA) |
734 				(__u64)(tmp & mask) << 32;
735 		} else if (addr == (addr_t) &dummy32->regs.psw.addr) {
736 			/* Build a 64 bit psw address from 31 bit address. */
737 			regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
738 			/* Transfer 31 bit amode bit to psw mask. */
739 			regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
740 				(__u64)(tmp & PSW32_ADDR_AMODE);
741 		} else {
742 			/* gpr 0-15 */
743 			*(__u32*)((addr_t) &regs->psw + addr*2 + 4) = tmp;
744 		}
745 	} else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
746 		/*
747 		 * access registers are stored in the thread structure
748 		 */
749 		offset = addr - (addr_t) &dummy32->regs.acrs;
750 		*(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
751 
752 	} else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
753 		/*
754 		 * orig_gpr2 is stored on the kernel stack
755 		 */
756 		*(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
757 
758 	} else if (addr < (addr_t) &dummy32->regs.fp_regs) {
759 		/*
760 		 * prevent writess of padding hole between
761 		 * orig_gpr2 and fp_regs on s390.
762 		 */
763 		return 0;
764 
765 	} else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
766 		/*
767 		 * floating point control reg. is in the thread structure
768 		 */
769 		if (test_fp_ctl(tmp))
770 			return -EINVAL;
771 		child->thread.fp_regs.fpc = data;
772 
773 	} else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
774 		/*
775 		 * floating point regs. are either in child->thread.fp_regs
776 		 * or the child->thread.vxrs array
777 		 */
778 		offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
779 #ifdef CONFIG_64BIT
780 		if (child->thread.vxrs)
781 			*(__u32 *)((addr_t)
782 				child->thread.vxrs + 2*offset) = tmp;
783 		else
784 #endif
785 			*(__u32 *)((addr_t)
786 				&child->thread.fp_regs.fprs + offset) = tmp;
787 
788 	} else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
789 		/*
790 		 * Handle access to the per_info structure.
791 		 */
792 		addr -= (addr_t) &dummy32->regs.per_info;
793 		__poke_user_per_compat(child, addr, data);
794 	}
795 
796 	return 0;
797 }
798 
799 static int poke_user_compat(struct task_struct *child,
800 			    addr_t addr, addr_t data)
801 {
802 	if (!is_compat_task() || (addr & 3) ||
803 	    addr > sizeof(struct compat_user) - 3)
804 		return -EIO;
805 
806 	return __poke_user_compat(child, addr, data);
807 }
808 
809 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
810 			compat_ulong_t caddr, compat_ulong_t cdata)
811 {
812 	unsigned long addr = caddr;
813 	unsigned long data = cdata;
814 	compat_ptrace_area parea;
815 	int copied, ret;
816 
817 	switch (request) {
818 	case PTRACE_PEEKUSR:
819 		/* read the word at location addr in the USER area. */
820 		return peek_user_compat(child, addr, data);
821 
822 	case PTRACE_POKEUSR:
823 		/* write the word at location addr in the USER area */
824 		return poke_user_compat(child, addr, data);
825 
826 	case PTRACE_PEEKUSR_AREA:
827 	case PTRACE_POKEUSR_AREA:
828 		if (copy_from_user(&parea, (void __force __user *) addr,
829 							sizeof(parea)))
830 			return -EFAULT;
831 		addr = parea.kernel_addr;
832 		data = parea.process_addr;
833 		copied = 0;
834 		while (copied < parea.len) {
835 			if (request == PTRACE_PEEKUSR_AREA)
836 				ret = peek_user_compat(child, addr, data);
837 			else {
838 				__u32 utmp;
839 				if (get_user(utmp,
840 					     (__u32 __force __user *) data))
841 					return -EFAULT;
842 				ret = poke_user_compat(child, addr, utmp);
843 			}
844 			if (ret)
845 				return ret;
846 			addr += sizeof(unsigned int);
847 			data += sizeof(unsigned int);
848 			copied += sizeof(unsigned int);
849 		}
850 		return 0;
851 	case PTRACE_GET_LAST_BREAK:
852 		put_user(task_thread_info(child)->last_break,
853 			 (unsigned int __user *) data);
854 		return 0;
855 	}
856 	return compat_ptrace_request(child, request, addr, data);
857 }
858 #endif
859 
860 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
861 {
862 	long ret = 0;
863 
864 	/* Do the secure computing check first. */
865 	if (secure_computing()) {
866 		/* seccomp failures shouldn't expose any additional code. */
867 		ret = -1;
868 		goto out;
869 	}
870 
871 	/*
872 	 * The sysc_tracesys code in entry.S stored the system
873 	 * call number to gprs[2].
874 	 */
875 	if (test_thread_flag(TIF_SYSCALL_TRACE) &&
876 	    (tracehook_report_syscall_entry(regs) ||
877 	     regs->gprs[2] >= NR_syscalls)) {
878 		/*
879 		 * Tracing decided this syscall should not happen or the
880 		 * debugger stored an invalid system call number. Skip
881 		 * the system call and the system call restart handling.
882 		 */
883 		clear_pt_regs_flag(regs, PIF_SYSCALL);
884 		ret = -1;
885 	}
886 
887 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
888 		trace_sys_enter(regs, regs->gprs[2]);
889 
890 	audit_syscall_entry(regs->gprs[2], regs->orig_gpr2,
891 			    regs->gprs[3], regs->gprs[4],
892 			    regs->gprs[5]);
893 out:
894 	return ret ?: regs->gprs[2];
895 }
896 
897 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
898 {
899 	audit_syscall_exit(regs);
900 
901 	if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
902 		trace_sys_exit(regs, regs->gprs[2]);
903 
904 	if (test_thread_flag(TIF_SYSCALL_TRACE))
905 		tracehook_report_syscall_exit(regs, 0);
906 }
907 
908 /*
909  * user_regset definitions.
910  */
911 
912 static int s390_regs_get(struct task_struct *target,
913 			 const struct user_regset *regset,
914 			 unsigned int pos, unsigned int count,
915 			 void *kbuf, void __user *ubuf)
916 {
917 	if (target == current)
918 		save_access_regs(target->thread.acrs);
919 
920 	if (kbuf) {
921 		unsigned long *k = kbuf;
922 		while (count > 0) {
923 			*k++ = __peek_user(target, pos);
924 			count -= sizeof(*k);
925 			pos += sizeof(*k);
926 		}
927 	} else {
928 		unsigned long __user *u = ubuf;
929 		while (count > 0) {
930 			if (__put_user(__peek_user(target, pos), u++))
931 				return -EFAULT;
932 			count -= sizeof(*u);
933 			pos += sizeof(*u);
934 		}
935 	}
936 	return 0;
937 }
938 
939 static int s390_regs_set(struct task_struct *target,
940 			 const struct user_regset *regset,
941 			 unsigned int pos, unsigned int count,
942 			 const void *kbuf, const void __user *ubuf)
943 {
944 	int rc = 0;
945 
946 	if (target == current)
947 		save_access_regs(target->thread.acrs);
948 
949 	if (kbuf) {
950 		const unsigned long *k = kbuf;
951 		while (count > 0 && !rc) {
952 			rc = __poke_user(target, pos, *k++);
953 			count -= sizeof(*k);
954 			pos += sizeof(*k);
955 		}
956 	} else {
957 		const unsigned long  __user *u = ubuf;
958 		while (count > 0 && !rc) {
959 			unsigned long word;
960 			rc = __get_user(word, u++);
961 			if (rc)
962 				break;
963 			rc = __poke_user(target, pos, word);
964 			count -= sizeof(*u);
965 			pos += sizeof(*u);
966 		}
967 	}
968 
969 	if (rc == 0 && target == current)
970 		restore_access_regs(target->thread.acrs);
971 
972 	return rc;
973 }
974 
975 static int s390_fpregs_get(struct task_struct *target,
976 			   const struct user_regset *regset, unsigned int pos,
977 			   unsigned int count, void *kbuf, void __user *ubuf)
978 {
979 	if (target == current) {
980 		save_fp_ctl(&target->thread.fp_regs.fpc);
981 		save_fp_regs(target->thread.fp_regs.fprs);
982 	}
983 #ifdef CONFIG_64BIT
984 	else if (target->thread.vxrs) {
985 		int i;
986 
987 		for (i = 0; i < __NUM_VXRS_LOW; i++)
988 			target->thread.fp_regs.fprs[i] =
989 				*(freg_t *)(target->thread.vxrs + i);
990 	}
991 #endif
992 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
993 				   &target->thread.fp_regs, 0, -1);
994 }
995 
996 static int s390_fpregs_set(struct task_struct *target,
997 			   const struct user_regset *regset, unsigned int pos,
998 			   unsigned int count, const void *kbuf,
999 			   const void __user *ubuf)
1000 {
1001 	int rc = 0;
1002 
1003 	if (target == current) {
1004 		save_fp_ctl(&target->thread.fp_regs.fpc);
1005 		save_fp_regs(target->thread.fp_regs.fprs);
1006 	}
1007 
1008 	/* If setting FPC, must validate it first. */
1009 	if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
1010 		u32 ufpc[2] = { target->thread.fp_regs.fpc, 0 };
1011 		rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ufpc,
1012 					0, offsetof(s390_fp_regs, fprs));
1013 		if (rc)
1014 			return rc;
1015 		if (ufpc[1] != 0 || test_fp_ctl(ufpc[0]))
1016 			return -EINVAL;
1017 		target->thread.fp_regs.fpc = ufpc[0];
1018 	}
1019 
1020 	if (rc == 0 && count > 0)
1021 		rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1022 					target->thread.fp_regs.fprs,
1023 					offsetof(s390_fp_regs, fprs), -1);
1024 
1025 	if (rc == 0) {
1026 		if (target == current) {
1027 			restore_fp_ctl(&target->thread.fp_regs.fpc);
1028 			restore_fp_regs(target->thread.fp_regs.fprs);
1029 		}
1030 #ifdef CONFIG_64BIT
1031 		else if (target->thread.vxrs) {
1032 			int i;
1033 
1034 			for (i = 0; i < __NUM_VXRS_LOW; i++)
1035 				*(freg_t *)(target->thread.vxrs + i) =
1036 					target->thread.fp_regs.fprs[i];
1037 		}
1038 #endif
1039 	}
1040 
1041 	return rc;
1042 }
1043 
1044 #ifdef CONFIG_64BIT
1045 
1046 static int s390_last_break_get(struct task_struct *target,
1047 			       const struct user_regset *regset,
1048 			       unsigned int pos, unsigned int count,
1049 			       void *kbuf, void __user *ubuf)
1050 {
1051 	if (count > 0) {
1052 		if (kbuf) {
1053 			unsigned long *k = kbuf;
1054 			*k = task_thread_info(target)->last_break;
1055 		} else {
1056 			unsigned long  __user *u = ubuf;
1057 			if (__put_user(task_thread_info(target)->last_break, u))
1058 				return -EFAULT;
1059 		}
1060 	}
1061 	return 0;
1062 }
1063 
1064 static int s390_last_break_set(struct task_struct *target,
1065 			       const struct user_regset *regset,
1066 			       unsigned int pos, unsigned int count,
1067 			       const void *kbuf, const void __user *ubuf)
1068 {
1069 	return 0;
1070 }
1071 
1072 static int s390_tdb_get(struct task_struct *target,
1073 			const struct user_regset *regset,
1074 			unsigned int pos, unsigned int count,
1075 			void *kbuf, void __user *ubuf)
1076 {
1077 	struct pt_regs *regs = task_pt_regs(target);
1078 	unsigned char *data;
1079 
1080 	if (!(regs->int_code & 0x200))
1081 		return -ENODATA;
1082 	data = target->thread.trap_tdb;
1083 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, data, 0, 256);
1084 }
1085 
1086 static int s390_tdb_set(struct task_struct *target,
1087 			const struct user_regset *regset,
1088 			unsigned int pos, unsigned int count,
1089 			const void *kbuf, const void __user *ubuf)
1090 {
1091 	return 0;
1092 }
1093 
1094 static int s390_vxrs_low_get(struct task_struct *target,
1095 			     const struct user_regset *regset,
1096 			     unsigned int pos, unsigned int count,
1097 			     void *kbuf, void __user *ubuf)
1098 {
1099 	__u64 vxrs[__NUM_VXRS_LOW];
1100 	int i;
1101 
1102 	if (!MACHINE_HAS_VX)
1103 		return -ENODEV;
1104 	if (target->thread.vxrs) {
1105 		if (target == current)
1106 			save_vx_regs(target->thread.vxrs);
1107 		for (i = 0; i < __NUM_VXRS_LOW; i++)
1108 			vxrs[i] = *((__u64 *)(target->thread.vxrs + i) + 1);
1109 	} else
1110 		memset(vxrs, 0, sizeof(vxrs));
1111 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
1112 }
1113 
1114 static int s390_vxrs_low_set(struct task_struct *target,
1115 			     const struct user_regset *regset,
1116 			     unsigned int pos, unsigned int count,
1117 			     const void *kbuf, const void __user *ubuf)
1118 {
1119 	__u64 vxrs[__NUM_VXRS_LOW];
1120 	int i, rc;
1121 
1122 	if (!MACHINE_HAS_VX)
1123 		return -ENODEV;
1124 	if (!target->thread.vxrs) {
1125 		rc = alloc_vector_registers(target);
1126 		if (rc)
1127 			return rc;
1128 	} else if (target == current)
1129 		save_vx_regs(target->thread.vxrs);
1130 
1131 	rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
1132 	if (rc == 0) {
1133 		for (i = 0; i < __NUM_VXRS_LOW; i++)
1134 			*((__u64 *)(target->thread.vxrs + i) + 1) = vxrs[i];
1135 		if (target == current)
1136 			restore_vx_regs(target->thread.vxrs);
1137 	}
1138 
1139 	return rc;
1140 }
1141 
1142 static int s390_vxrs_high_get(struct task_struct *target,
1143 			      const struct user_regset *regset,
1144 			      unsigned int pos, unsigned int count,
1145 			      void *kbuf, void __user *ubuf)
1146 {
1147 	__vector128 vxrs[__NUM_VXRS_HIGH];
1148 
1149 	if (!MACHINE_HAS_VX)
1150 		return -ENODEV;
1151 	if (target->thread.vxrs) {
1152 		if (target == current)
1153 			save_vx_regs(target->thread.vxrs);
1154 		memcpy(vxrs, target->thread.vxrs + __NUM_VXRS_LOW,
1155 		       sizeof(vxrs));
1156 	} else
1157 		memset(vxrs, 0, sizeof(vxrs));
1158 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
1159 }
1160 
1161 static int s390_vxrs_high_set(struct task_struct *target,
1162 			      const struct user_regset *regset,
1163 			      unsigned int pos, unsigned int count,
1164 			      const void *kbuf, const void __user *ubuf)
1165 {
1166 	int rc;
1167 
1168 	if (!MACHINE_HAS_VX)
1169 		return -ENODEV;
1170 	if (!target->thread.vxrs) {
1171 		rc = alloc_vector_registers(target);
1172 		if (rc)
1173 			return rc;
1174 	} else if (target == current)
1175 		save_vx_regs(target->thread.vxrs);
1176 
1177 	rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1178 				target->thread.vxrs + __NUM_VXRS_LOW, 0, -1);
1179 	if (rc == 0 && target == current)
1180 		restore_vx_regs(target->thread.vxrs);
1181 
1182 	return rc;
1183 }
1184 
1185 #endif
1186 
1187 static int s390_system_call_get(struct task_struct *target,
1188 				const struct user_regset *regset,
1189 				unsigned int pos, unsigned int count,
1190 				void *kbuf, void __user *ubuf)
1191 {
1192 	unsigned int *data = &task_thread_info(target)->system_call;
1193 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
1194 				   data, 0, sizeof(unsigned int));
1195 }
1196 
1197 static int s390_system_call_set(struct task_struct *target,
1198 				const struct user_regset *regset,
1199 				unsigned int pos, unsigned int count,
1200 				const void *kbuf, const void __user *ubuf)
1201 {
1202 	unsigned int *data = &task_thread_info(target)->system_call;
1203 	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1204 				  data, 0, sizeof(unsigned int));
1205 }
1206 
1207 static const struct user_regset s390_regsets[] = {
1208 	{
1209 		.core_note_type = NT_PRSTATUS,
1210 		.n = sizeof(s390_regs) / sizeof(long),
1211 		.size = sizeof(long),
1212 		.align = sizeof(long),
1213 		.get = s390_regs_get,
1214 		.set = s390_regs_set,
1215 	},
1216 	{
1217 		.core_note_type = NT_PRFPREG,
1218 		.n = sizeof(s390_fp_regs) / sizeof(long),
1219 		.size = sizeof(long),
1220 		.align = sizeof(long),
1221 		.get = s390_fpregs_get,
1222 		.set = s390_fpregs_set,
1223 	},
1224 	{
1225 		.core_note_type = NT_S390_SYSTEM_CALL,
1226 		.n = 1,
1227 		.size = sizeof(unsigned int),
1228 		.align = sizeof(unsigned int),
1229 		.get = s390_system_call_get,
1230 		.set = s390_system_call_set,
1231 	},
1232 #ifdef CONFIG_64BIT
1233 	{
1234 		.core_note_type = NT_S390_LAST_BREAK,
1235 		.n = 1,
1236 		.size = sizeof(long),
1237 		.align = sizeof(long),
1238 		.get = s390_last_break_get,
1239 		.set = s390_last_break_set,
1240 	},
1241 	{
1242 		.core_note_type = NT_S390_TDB,
1243 		.n = 1,
1244 		.size = 256,
1245 		.align = 1,
1246 		.get = s390_tdb_get,
1247 		.set = s390_tdb_set,
1248 	},
1249 	{
1250 		.core_note_type = NT_S390_VXRS_LOW,
1251 		.n = __NUM_VXRS_LOW,
1252 		.size = sizeof(__u64),
1253 		.align = sizeof(__u64),
1254 		.get = s390_vxrs_low_get,
1255 		.set = s390_vxrs_low_set,
1256 	},
1257 	{
1258 		.core_note_type = NT_S390_VXRS_HIGH,
1259 		.n = __NUM_VXRS_HIGH,
1260 		.size = sizeof(__vector128),
1261 		.align = sizeof(__vector128),
1262 		.get = s390_vxrs_high_get,
1263 		.set = s390_vxrs_high_set,
1264 	},
1265 #endif
1266 };
1267 
1268 static const struct user_regset_view user_s390_view = {
1269 	.name = UTS_MACHINE,
1270 	.e_machine = EM_S390,
1271 	.regsets = s390_regsets,
1272 	.n = ARRAY_SIZE(s390_regsets)
1273 };
1274 
1275 #ifdef CONFIG_COMPAT
1276 static int s390_compat_regs_get(struct task_struct *target,
1277 				const struct user_regset *regset,
1278 				unsigned int pos, unsigned int count,
1279 				void *kbuf, void __user *ubuf)
1280 {
1281 	if (target == current)
1282 		save_access_regs(target->thread.acrs);
1283 
1284 	if (kbuf) {
1285 		compat_ulong_t *k = kbuf;
1286 		while (count > 0) {
1287 			*k++ = __peek_user_compat(target, pos);
1288 			count -= sizeof(*k);
1289 			pos += sizeof(*k);
1290 		}
1291 	} else {
1292 		compat_ulong_t __user *u = ubuf;
1293 		while (count > 0) {
1294 			if (__put_user(__peek_user_compat(target, pos), u++))
1295 				return -EFAULT;
1296 			count -= sizeof(*u);
1297 			pos += sizeof(*u);
1298 		}
1299 	}
1300 	return 0;
1301 }
1302 
1303 static int s390_compat_regs_set(struct task_struct *target,
1304 				const struct user_regset *regset,
1305 				unsigned int pos, unsigned int count,
1306 				const void *kbuf, const void __user *ubuf)
1307 {
1308 	int rc = 0;
1309 
1310 	if (target == current)
1311 		save_access_regs(target->thread.acrs);
1312 
1313 	if (kbuf) {
1314 		const compat_ulong_t *k = kbuf;
1315 		while (count > 0 && !rc) {
1316 			rc = __poke_user_compat(target, pos, *k++);
1317 			count -= sizeof(*k);
1318 			pos += sizeof(*k);
1319 		}
1320 	} else {
1321 		const compat_ulong_t  __user *u = ubuf;
1322 		while (count > 0 && !rc) {
1323 			compat_ulong_t word;
1324 			rc = __get_user(word, u++);
1325 			if (rc)
1326 				break;
1327 			rc = __poke_user_compat(target, pos, word);
1328 			count -= sizeof(*u);
1329 			pos += sizeof(*u);
1330 		}
1331 	}
1332 
1333 	if (rc == 0 && target == current)
1334 		restore_access_regs(target->thread.acrs);
1335 
1336 	return rc;
1337 }
1338 
1339 static int s390_compat_regs_high_get(struct task_struct *target,
1340 				     const struct user_regset *regset,
1341 				     unsigned int pos, unsigned int count,
1342 				     void *kbuf, void __user *ubuf)
1343 {
1344 	compat_ulong_t *gprs_high;
1345 
1346 	gprs_high = (compat_ulong_t *)
1347 		&task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1348 	if (kbuf) {
1349 		compat_ulong_t *k = kbuf;
1350 		while (count > 0) {
1351 			*k++ = *gprs_high;
1352 			gprs_high += 2;
1353 			count -= sizeof(*k);
1354 		}
1355 	} else {
1356 		compat_ulong_t __user *u = ubuf;
1357 		while (count > 0) {
1358 			if (__put_user(*gprs_high, u++))
1359 				return -EFAULT;
1360 			gprs_high += 2;
1361 			count -= sizeof(*u);
1362 		}
1363 	}
1364 	return 0;
1365 }
1366 
1367 static int s390_compat_regs_high_set(struct task_struct *target,
1368 				     const struct user_regset *regset,
1369 				     unsigned int pos, unsigned int count,
1370 				     const void *kbuf, const void __user *ubuf)
1371 {
1372 	compat_ulong_t *gprs_high;
1373 	int rc = 0;
1374 
1375 	gprs_high = (compat_ulong_t *)
1376 		&task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1377 	if (kbuf) {
1378 		const compat_ulong_t *k = kbuf;
1379 		while (count > 0) {
1380 			*gprs_high = *k++;
1381 			*gprs_high += 2;
1382 			count -= sizeof(*k);
1383 		}
1384 	} else {
1385 		const compat_ulong_t  __user *u = ubuf;
1386 		while (count > 0 && !rc) {
1387 			unsigned long word;
1388 			rc = __get_user(word, u++);
1389 			if (rc)
1390 				break;
1391 			*gprs_high = word;
1392 			*gprs_high += 2;
1393 			count -= sizeof(*u);
1394 		}
1395 	}
1396 
1397 	return rc;
1398 }
1399 
1400 static int s390_compat_last_break_get(struct task_struct *target,
1401 				      const struct user_regset *regset,
1402 				      unsigned int pos, unsigned int count,
1403 				      void *kbuf, void __user *ubuf)
1404 {
1405 	compat_ulong_t last_break;
1406 
1407 	if (count > 0) {
1408 		last_break = task_thread_info(target)->last_break;
1409 		if (kbuf) {
1410 			unsigned long *k = kbuf;
1411 			*k = last_break;
1412 		} else {
1413 			unsigned long  __user *u = ubuf;
1414 			if (__put_user(last_break, u))
1415 				return -EFAULT;
1416 		}
1417 	}
1418 	return 0;
1419 }
1420 
1421 static int s390_compat_last_break_set(struct task_struct *target,
1422 				      const struct user_regset *regset,
1423 				      unsigned int pos, unsigned int count,
1424 				      const void *kbuf, const void __user *ubuf)
1425 {
1426 	return 0;
1427 }
1428 
1429 static const struct user_regset s390_compat_regsets[] = {
1430 	{
1431 		.core_note_type = NT_PRSTATUS,
1432 		.n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
1433 		.size = sizeof(compat_long_t),
1434 		.align = sizeof(compat_long_t),
1435 		.get = s390_compat_regs_get,
1436 		.set = s390_compat_regs_set,
1437 	},
1438 	{
1439 		.core_note_type = NT_PRFPREG,
1440 		.n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
1441 		.size = sizeof(compat_long_t),
1442 		.align = sizeof(compat_long_t),
1443 		.get = s390_fpregs_get,
1444 		.set = s390_fpregs_set,
1445 	},
1446 	{
1447 		.core_note_type = NT_S390_SYSTEM_CALL,
1448 		.n = 1,
1449 		.size = sizeof(compat_uint_t),
1450 		.align = sizeof(compat_uint_t),
1451 		.get = s390_system_call_get,
1452 		.set = s390_system_call_set,
1453 	},
1454 	{
1455 		.core_note_type = NT_S390_LAST_BREAK,
1456 		.n = 1,
1457 		.size = sizeof(long),
1458 		.align = sizeof(long),
1459 		.get = s390_compat_last_break_get,
1460 		.set = s390_compat_last_break_set,
1461 	},
1462 	{
1463 		.core_note_type = NT_S390_TDB,
1464 		.n = 1,
1465 		.size = 256,
1466 		.align = 1,
1467 		.get = s390_tdb_get,
1468 		.set = s390_tdb_set,
1469 	},
1470 	{
1471 		.core_note_type = NT_S390_VXRS_LOW,
1472 		.n = __NUM_VXRS_LOW,
1473 		.size = sizeof(__u64),
1474 		.align = sizeof(__u64),
1475 		.get = s390_vxrs_low_get,
1476 		.set = s390_vxrs_low_set,
1477 	},
1478 	{
1479 		.core_note_type = NT_S390_VXRS_HIGH,
1480 		.n = __NUM_VXRS_HIGH,
1481 		.size = sizeof(__vector128),
1482 		.align = sizeof(__vector128),
1483 		.get = s390_vxrs_high_get,
1484 		.set = s390_vxrs_high_set,
1485 	},
1486 	{
1487 		.core_note_type = NT_S390_HIGH_GPRS,
1488 		.n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
1489 		.size = sizeof(compat_long_t),
1490 		.align = sizeof(compat_long_t),
1491 		.get = s390_compat_regs_high_get,
1492 		.set = s390_compat_regs_high_set,
1493 	},
1494 };
1495 
1496 static const struct user_regset_view user_s390_compat_view = {
1497 	.name = "s390",
1498 	.e_machine = EM_S390,
1499 	.regsets = s390_compat_regsets,
1500 	.n = ARRAY_SIZE(s390_compat_regsets)
1501 };
1502 #endif
1503 
1504 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1505 {
1506 #ifdef CONFIG_COMPAT
1507 	if (test_tsk_thread_flag(task, TIF_31BIT))
1508 		return &user_s390_compat_view;
1509 #endif
1510 	return &user_s390_view;
1511 }
1512 
1513 static const char *gpr_names[NUM_GPRS] = {
1514 	"r0", "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
1515 	"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1516 };
1517 
1518 unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
1519 {
1520 	if (offset >= NUM_GPRS)
1521 		return 0;
1522 	return regs->gprs[offset];
1523 }
1524 
1525 int regs_query_register_offset(const char *name)
1526 {
1527 	unsigned long offset;
1528 
1529 	if (!name || *name != 'r')
1530 		return -EINVAL;
1531 	if (kstrtoul(name + 1, 10, &offset))
1532 		return -EINVAL;
1533 	if (offset >= NUM_GPRS)
1534 		return -EINVAL;
1535 	return offset;
1536 }
1537 
1538 const char *regs_query_register_name(unsigned int offset)
1539 {
1540 	if (offset >= NUM_GPRS)
1541 		return NULL;
1542 	return gpr_names[offset];
1543 }
1544 
1545 static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
1546 {
1547 	unsigned long ksp = kernel_stack_pointer(regs);
1548 
1549 	return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
1550 }
1551 
1552 /**
1553  * regs_get_kernel_stack_nth() - get Nth entry of the stack
1554  * @regs:pt_regs which contains kernel stack pointer.
1555  * @n:stack entry number.
1556  *
1557  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
1558  * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
1559  * this returns 0.
1560  */
1561 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
1562 {
1563 	unsigned long addr;
1564 
1565 	addr = kernel_stack_pointer(regs) + n * sizeof(long);
1566 	if (!regs_within_kernel_stack(regs, addr))
1567 		return 0;
1568 	return *(unsigned long *)addr;
1569 }
1570