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