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