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