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