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