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