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