xref: /linux/arch/s390/kernel/ptrace.c (revision bb29b957eaaa15be33243550d9ee576c2d601669)
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/cpufeature.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/errno.h>
17 #include <linux/ptrace.h>
18 #include <linux/user.h>
19 #include <linux/security.h>
20 #include <linux/audit.h>
21 #include <linux/signal.h>
22 #include <linux/elf.h>
23 #include <linux/regset.h>
24 #include <linux/seccomp.h>
25 #include <trace/syscall.h>
26 #include <asm/guarded_storage.h>
27 #include <asm/access-regs.h>
28 #include <asm/page.h>
29 #include <linux/uaccess.h>
30 #include <asm/unistd.h>
31 #include <asm/runtime_instr.h>
32 #include <asm/facility.h>
33 #include <asm/machine.h>
34 #include <asm/ptrace.h>
35 #include <asm/rwonce.h>
36 #include <asm/fpu.h>
37 #include <asm/syscall.h>
38 
39 #include "entry.h"
40 
41 void update_cr_regs(struct task_struct *task)
42 {
43 	struct pt_regs *regs = task_pt_regs(task);
44 	struct thread_struct *thread = &task->thread;
45 	union ctlreg0 cr0_old, cr0_new;
46 	union ctlreg2 cr2_old, cr2_new;
47 	int cr0_changed, cr2_changed;
48 	union {
49 		struct ctlreg regs[3];
50 		struct {
51 			struct ctlreg control;
52 			struct ctlreg start;
53 			struct ctlreg end;
54 		};
55 	} old, new;
56 
57 	local_ctl_store(0, &cr0_old.reg);
58 	local_ctl_store(2, &cr2_old.reg);
59 	cr0_new = cr0_old;
60 	cr2_new = cr2_old;
61 	/* Take care of the enable/disable of transactional execution. */
62 	if (machine_has_tx()) {
63 		/* Set or clear transaction execution TXC bit 8. */
64 		cr0_new.tcx = 1;
65 		if (task->thread.per_flags & PER_FLAG_NO_TE)
66 			cr0_new.tcx = 0;
67 		/* Set or clear transaction execution TDC bits 62 and 63. */
68 		cr2_new.tdc = 0;
69 		if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND) {
70 			if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND_TEND)
71 				cr2_new.tdc = 1;
72 			else
73 				cr2_new.tdc = 2;
74 		}
75 	}
76 	/* Take care of enable/disable of guarded storage. */
77 	if (cpu_has_gs()) {
78 		cr2_new.gse = 0;
79 		if (task->thread.gs_cb)
80 			cr2_new.gse = 1;
81 	}
82 	/* Load control register 0/2 iff changed */
83 	cr0_changed = cr0_new.val != cr0_old.val;
84 	cr2_changed = cr2_new.val != cr2_old.val;
85 	if (cr0_changed)
86 		local_ctl_load(0, &cr0_new.reg);
87 	if (cr2_changed)
88 		local_ctl_load(2, &cr2_new.reg);
89 	/* Copy user specified PER registers */
90 	new.control.val = thread->per_user.control;
91 	new.start.val = thread->per_user.start;
92 	new.end.val = thread->per_user.end;
93 
94 	/* merge TIF_SINGLE_STEP into user specified PER registers. */
95 	if (test_tsk_thread_flag(task, TIF_SINGLE_STEP) ||
96 	    test_tsk_thread_flag(task, TIF_UPROBE_SINGLESTEP)) {
97 		if (test_tsk_thread_flag(task, TIF_BLOCK_STEP))
98 			new.control.val |= PER_EVENT_BRANCH;
99 		else
100 			new.control.val |= PER_EVENT_IFETCH;
101 		new.control.val |= PER_CONTROL_SUSPENSION;
102 		new.control.val |= PER_EVENT_TRANSACTION_END;
103 		if (test_tsk_thread_flag(task, TIF_UPROBE_SINGLESTEP))
104 			new.control.val |= PER_EVENT_IFETCH;
105 		new.start.val = 0;
106 		new.end.val = -1UL;
107 	}
108 
109 	/* Take care of the PER enablement bit in the PSW. */
110 	if (!(new.control.val & PER_EVENT_MASK)) {
111 		regs->psw.mask &= ~PSW_MASK_PER;
112 		return;
113 	}
114 	regs->psw.mask |= PSW_MASK_PER;
115 	__local_ctl_store(9, 11, old.regs);
116 	if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
117 		__local_ctl_load(9, 11, new.regs);
118 }
119 
120 void user_enable_single_step(struct task_struct *task)
121 {
122 	clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
123 	set_tsk_thread_flag(task, TIF_SINGLE_STEP);
124 }
125 
126 void user_disable_single_step(struct task_struct *task)
127 {
128 	clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
129 	clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
130 }
131 
132 void user_enable_block_step(struct task_struct *task)
133 {
134 	set_tsk_thread_flag(task, TIF_SINGLE_STEP);
135 	set_tsk_thread_flag(task, TIF_BLOCK_STEP);
136 }
137 
138 /*
139  * Called by kernel/ptrace.c when detaching..
140  *
141  * Clear all debugging related fields.
142  */
143 void ptrace_disable(struct task_struct *task)
144 {
145 	memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
146 	memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
147 	clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
148 	clear_tsk_thread_flag(task, TIF_PER_TRAP);
149 	task->thread.per_flags = 0;
150 }
151 
152 #define __ADDR_MASK 7
153 
154 static inline unsigned long __peek_user_per(struct task_struct *child,
155 					    addr_t addr)
156 {
157 	if (addr == offsetof(struct per_struct_kernel, cr9))
158 		/* Control bits of the active per set. */
159 		return test_thread_flag(TIF_SINGLE_STEP) ?
160 			PER_EVENT_IFETCH : child->thread.per_user.control;
161 	else if (addr == offsetof(struct per_struct_kernel, cr10))
162 		/* Start address of the active per set. */
163 		return test_thread_flag(TIF_SINGLE_STEP) ?
164 			0 : child->thread.per_user.start;
165 	else if (addr == offsetof(struct per_struct_kernel, cr11))
166 		/* End address of the active per set. */
167 		return test_thread_flag(TIF_SINGLE_STEP) ?
168 			-1UL : child->thread.per_user.end;
169 	else if (addr == offsetof(struct per_struct_kernel, bits))
170 		/* Single-step bit. */
171 		return test_thread_flag(TIF_SINGLE_STEP) ?
172 			(1UL << (BITS_PER_LONG - 1)) : 0;
173 	else if (addr == offsetof(struct per_struct_kernel, starting_addr))
174 		/* Start address of the user specified per set. */
175 		return child->thread.per_user.start;
176 	else if (addr == offsetof(struct per_struct_kernel, ending_addr))
177 		/* End address of the user specified per set. */
178 		return child->thread.per_user.end;
179 	else if (addr == offsetof(struct per_struct_kernel, perc_atmid))
180 		/* PER code, ATMID and AI of the last PER trap */
181 		return (unsigned long)
182 			child->thread.per_event.cause << (BITS_PER_LONG - 16);
183 	else if (addr == offsetof(struct per_struct_kernel, address))
184 		/* Address of the last PER trap */
185 		return child->thread.per_event.address;
186 	else if (addr == offsetof(struct per_struct_kernel, access_id))
187 		/* Access id of the last PER trap */
188 		return (unsigned long)
189 			child->thread.per_event.paid << (BITS_PER_LONG - 8);
190 	return 0;
191 }
192 
193 /*
194  * Read the word at offset addr from the user area of a process. The
195  * trouble here is that the information is littered over different
196  * locations. The process registers are found on the kernel stack,
197  * the floating point stuff and the trace settings are stored in
198  * the task structure. In addition the different structures in
199  * struct user contain pad bytes that should be read as zeroes.
200  * Lovely...
201  */
202 static unsigned long __peek_user(struct task_struct *child, addr_t addr)
203 {
204 	addr_t offset, tmp;
205 
206 	if (addr < offsetof(struct user, regs.acrs)) {
207 		/*
208 		 * psw and gprs are stored on the stack
209 		 */
210 		tmp = *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr);
211 		if (addr == offsetof(struct user, regs.psw.mask)) {
212 			/* Return a clean psw mask. */
213 			tmp &= PSW_MASK_USER | PSW_MASK_RI;
214 			tmp |= PSW_USER_BITS;
215 		}
216 
217 	} else if (addr < offsetof(struct user, regs.orig_gpr2)) {
218 		/*
219 		 * access registers are stored in the thread structure
220 		 */
221 		offset = addr - offsetof(struct user, regs.acrs);
222 		/*
223 		 * Very special case: old & broken 64 bit gdb reading
224 		 * from acrs[15]. Result is a 64 bit value. Read the
225 		 * 32 bit acrs[15] value and shift it by 32. Sick...
226 		 */
227 		if (addr == offsetof(struct user, regs.acrs[15]))
228 			tmp = ((unsigned long) child->thread.acrs[15]) << 32;
229 		else
230 			tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
231 
232 	} else if (addr == offsetof(struct user, regs.orig_gpr2)) {
233 		/*
234 		 * orig_gpr2 is stored on the kernel stack
235 		 */
236 		tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
237 
238 	} else if (addr < offsetof(struct user, regs.fp_regs)) {
239 		/*
240 		 * prevent reads of padding hole between
241 		 * orig_gpr2 and fp_regs on s390.
242 		 */
243 		tmp = 0;
244 
245 	} else if (addr == offsetof(struct user, regs.fp_regs.fpc)) {
246 		/*
247 		 * floating point control reg. is in the thread structure
248 		 */
249 		tmp = child->thread.ufpu.fpc;
250 		tmp <<= BITS_PER_LONG - 32;
251 
252 	} else if (addr < offsetof(struct user, regs.fp_regs) + sizeof(s390_fp_regs)) {
253 		/*
254 		 * floating point regs. are in the child->thread.ufpu.vxrs array
255 		 */
256 		offset = addr - offsetof(struct user, regs.fp_regs.fprs);
257 		tmp = *(addr_t *)((addr_t)child->thread.ufpu.vxrs + 2 * offset);
258 	} else if (addr < offsetof(struct user, regs.per_info) + sizeof(per_struct)) {
259 		/*
260 		 * Handle access to the per_info structure.
261 		 */
262 		addr -= offsetof(struct user, regs.per_info);
263 		tmp = __peek_user_per(child, addr);
264 
265 	} else
266 		tmp = 0;
267 
268 	return tmp;
269 }
270 
271 static int
272 peek_user(struct task_struct *child, addr_t addr, addr_t data)
273 {
274 	addr_t tmp, mask;
275 
276 	/*
277 	 * Stupid gdb peeks/pokes the access registers in 64 bit with
278 	 * an alignment of 4. Programmers from hell...
279 	 */
280 	mask = __ADDR_MASK;
281 	if (addr >= offsetof(struct user, regs.acrs) &&
282 	    addr < offsetof(struct user, regs.orig_gpr2))
283 		mask = 3;
284 	if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
285 		return -EIO;
286 
287 	tmp = __peek_user(child, addr);
288 	return put_user(tmp, (addr_t __user *) data);
289 }
290 
291 static inline void __poke_user_per(struct task_struct *child,
292 				   addr_t addr, addr_t data)
293 {
294 	/*
295 	 * There are only three fields in the per_info struct that the
296 	 * debugger user can write to.
297 	 * 1) cr9: the debugger wants to set a new PER event mask
298 	 * 2) starting_addr: the debugger wants to set a new starting
299 	 *    address to use with the PER event mask.
300 	 * 3) ending_addr: the debugger wants to set a new ending
301 	 *    address to use with the PER event mask.
302 	 * The user specified PER event mask and the start and end
303 	 * addresses are used only if single stepping is not in effect.
304 	 * Writes to any other field in per_info are ignored.
305 	 */
306 	if (addr == offsetof(struct per_struct_kernel, cr9))
307 		/* PER event mask of the user specified per set. */
308 		child->thread.per_user.control =
309 			data & (PER_EVENT_MASK | PER_CONTROL_MASK);
310 	else if (addr == offsetof(struct per_struct_kernel, starting_addr))
311 		/* Starting address of the user specified per set. */
312 		child->thread.per_user.start = data;
313 	else if (addr == offsetof(struct per_struct_kernel, ending_addr))
314 		/* Ending address of the user specified per set. */
315 		child->thread.per_user.end = data;
316 }
317 
318 /*
319  * Write a word to the user area of a process at location addr. This
320  * operation does have an additional problem compared to peek_user.
321  * Stores to the program status word and on the floating point
322  * control register needs to get checked for validity.
323  */
324 static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
325 {
326 	addr_t offset;
327 
328 
329 	if (addr < offsetof(struct user, regs.acrs)) {
330 		struct pt_regs *regs = task_pt_regs(child);
331 		/*
332 		 * psw and gprs are stored on the stack
333 		 */
334 		if (addr == offsetof(struct user, regs.psw.mask)) {
335 			unsigned long mask = PSW_MASK_USER;
336 
337 			mask |= is_ri_task(child) ? PSW_MASK_RI : 0;
338 			if ((data ^ PSW_USER_BITS) & ~mask)
339 				/* Invalid psw mask. */
340 				return -EINVAL;
341 			if ((data & PSW_MASK_ASC) == PSW_ASC_HOME)
342 				/* Invalid address-space-control bits */
343 				return -EINVAL;
344 			if ((data & PSW_MASK_EA) && !(data & PSW_MASK_BA))
345 				/* Invalid addressing mode bits */
346 				return -EINVAL;
347 		}
348 
349 		if (addr == offsetof(struct user, regs.gprs[2])) {
350 			if (test_pt_regs_flag(regs, PIF_SYSCALL))
351 				syscall_set_nr(child, regs, data);
352 		}
353 		*(addr_t *)((addr_t) &regs->psw + addr) = data;
354 	} else if (addr < offsetof(struct user, regs.orig_gpr2)) {
355 		/*
356 		 * access registers are stored in the thread structure
357 		 */
358 		offset = addr - offsetof(struct user, regs.acrs);
359 		/*
360 		 * Very special case: old & broken 64 bit gdb writing
361 		 * to acrs[15] with a 64 bit value. Ignore the lower
362 		 * half of the value and write the upper 32 bit to
363 		 * acrs[15]. Sick...
364 		 */
365 		if (addr == offsetof(struct user, regs.acrs[15]))
366 			child->thread.acrs[15] = (unsigned int) (data >> 32);
367 		else
368 			*(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
369 
370 	} else if (addr == offsetof(struct user, regs.orig_gpr2)) {
371 		/*
372 		 * orig_gpr2 is stored on the kernel stack
373 		 */
374 		task_pt_regs(child)->orig_gpr2 = data;
375 
376 	} else if (addr < offsetof(struct user, regs.fp_regs)) {
377 		/*
378 		 * prevent writes of padding hole between
379 		 * orig_gpr2 and fp_regs on s390.
380 		 */
381 		return 0;
382 
383 	} else if (addr == offsetof(struct user, regs.fp_regs.fpc)) {
384 		/*
385 		 * floating point control reg. is in the thread structure
386 		 */
387 		if ((unsigned int)data != 0)
388 			return -EINVAL;
389 		child->thread.ufpu.fpc = data >> (BITS_PER_LONG - 32);
390 
391 	} else if (addr < offsetof(struct user, regs.fp_regs) + sizeof(s390_fp_regs)) {
392 		/*
393 		 * floating point regs. are in the child->thread.ufpu.vxrs array
394 		 */
395 		offset = addr - offsetof(struct user, regs.fp_regs.fprs);
396 		*(addr_t *)((addr_t)child->thread.ufpu.vxrs + 2 * offset) = data;
397 	} else if (addr < offsetof(struct user, regs.per_info) + sizeof(per_struct)) {
398 		/*
399 		 * Handle access to the per_info structure.
400 		 */
401 		addr -= offsetof(struct user, regs.per_info);
402 		__poke_user_per(child, addr, data);
403 
404 	}
405 
406 	return 0;
407 }
408 
409 static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
410 {
411 	addr_t mask;
412 
413 	/*
414 	 * Stupid gdb peeks/pokes the access registers in 64 bit with
415 	 * an alignment of 4. Programmers from hell indeed...
416 	 */
417 	mask = __ADDR_MASK;
418 	if (addr >= offsetof(struct user, regs.acrs) &&
419 	    addr < offsetof(struct user, regs.orig_gpr2))
420 		mask = 3;
421 	if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
422 		return -EIO;
423 
424 	return __poke_user(child, addr, data);
425 }
426 
427 long arch_ptrace(struct task_struct *child, long request,
428 		 unsigned long addr, unsigned long data)
429 {
430 	ptrace_area parea;
431 	int copied, ret;
432 
433 	switch (request) {
434 	case PTRACE_PEEKUSR:
435 		/* read the word at location addr in the USER area. */
436 		return peek_user(child, addr, data);
437 
438 	case PTRACE_POKEUSR:
439 		/* write the word at location addr in the USER area */
440 		return poke_user(child, addr, data);
441 
442 	case PTRACE_PEEKUSR_AREA:
443 	case PTRACE_POKEUSR_AREA:
444 		if (copy_from_user(&parea, (void __force __user *) addr,
445 							sizeof(parea)))
446 			return -EFAULT;
447 		addr = parea.kernel_addr;
448 		data = parea.process_addr;
449 		copied = 0;
450 		while (copied < parea.len) {
451 			if (request == PTRACE_PEEKUSR_AREA)
452 				ret = peek_user(child, addr, data);
453 			else {
454 				addr_t utmp;
455 				if (get_user(utmp,
456 					     (addr_t __force __user *) data))
457 					return -EFAULT;
458 				ret = poke_user(child, addr, utmp);
459 			}
460 			if (ret)
461 				return ret;
462 			addr += sizeof(unsigned long);
463 			data += sizeof(unsigned long);
464 			copied += sizeof(unsigned long);
465 		}
466 		return 0;
467 	case PTRACE_GET_LAST_BREAK:
468 		return put_user(child->thread.last_break, (unsigned long __user *)data);
469 	case PTRACE_ENABLE_TE:
470 		if (!machine_has_tx())
471 			return -EIO;
472 		child->thread.per_flags &= ~PER_FLAG_NO_TE;
473 		return 0;
474 	case PTRACE_DISABLE_TE:
475 		if (!machine_has_tx())
476 			return -EIO;
477 		child->thread.per_flags |= PER_FLAG_NO_TE;
478 		child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
479 		return 0;
480 	case PTRACE_TE_ABORT_RAND:
481 		if (!machine_has_tx() || (child->thread.per_flags & PER_FLAG_NO_TE))
482 			return -EIO;
483 		switch (data) {
484 		case 0UL:
485 			child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
486 			break;
487 		case 1UL:
488 			child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
489 			child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND_TEND;
490 			break;
491 		case 2UL:
492 			child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
493 			child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND_TEND;
494 			break;
495 		default:
496 			return -EINVAL;
497 		}
498 		return 0;
499 	default:
500 		return ptrace_request(child, request, addr, data);
501 	}
502 }
503 
504 /*
505  * user_regset definitions.
506  */
507 
508 static int s390_regs_get(struct task_struct *target,
509 			 const struct user_regset *regset,
510 			 struct membuf to)
511 {
512 	unsigned pos;
513 	if (target == current)
514 		save_access_regs(target->thread.acrs);
515 
516 	for (pos = 0; pos < sizeof(s390_regs); pos += sizeof(long))
517 		membuf_store(&to, __peek_user(target, pos));
518 	return 0;
519 }
520 
521 static int s390_regs_set(struct task_struct *target,
522 			 const struct user_regset *regset,
523 			 unsigned int pos, unsigned int count,
524 			 const void *kbuf, const void __user *ubuf)
525 {
526 	int rc = 0;
527 
528 	if (target == current)
529 		save_access_regs(target->thread.acrs);
530 
531 	if (kbuf) {
532 		const unsigned long *k = kbuf;
533 		while (count > 0 && !rc) {
534 			rc = __poke_user(target, pos, *k++);
535 			count -= sizeof(*k);
536 			pos += sizeof(*k);
537 		}
538 	} else {
539 		const unsigned long  __user *u = ubuf;
540 		while (count > 0 && !rc) {
541 			unsigned long word;
542 			rc = get_user(word, u++);
543 			if (rc)
544 				break;
545 			rc = __poke_user(target, pos, word);
546 			count -= sizeof(*u);
547 			pos += sizeof(*u);
548 		}
549 	}
550 
551 	if (rc == 0 && target == current)
552 		restore_access_regs(target->thread.acrs);
553 
554 	return rc;
555 }
556 
557 static int s390_fpregs_get(struct task_struct *target,
558 			   const struct user_regset *regset,
559 			   struct membuf to)
560 {
561 	_s390_fp_regs fp_regs;
562 
563 	if (target == current)
564 		save_user_fpu_regs();
565 
566 	fp_regs.fpc = target->thread.ufpu.fpc;
567 	fpregs_store(&fp_regs, &target->thread.ufpu);
568 
569 	return membuf_write(&to, &fp_regs, sizeof(fp_regs));
570 }
571 
572 static int s390_fpregs_set(struct task_struct *target,
573 			   const struct user_regset *regset, unsigned int pos,
574 			   unsigned int count, const void *kbuf,
575 			   const void __user *ubuf)
576 {
577 	int rc = 0;
578 	freg_t fprs[__NUM_FPRS];
579 
580 	if (target == current)
581 		save_user_fpu_regs();
582 	convert_vx_to_fp(fprs, target->thread.ufpu.vxrs);
583 	if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
584 		u32 ufpc[2] = { target->thread.ufpu.fpc, 0 };
585 		rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ufpc,
586 					0, offsetof(s390_fp_regs, fprs));
587 		if (rc)
588 			return rc;
589 		if (ufpc[1] != 0)
590 			return -EINVAL;
591 		target->thread.ufpu.fpc = ufpc[0];
592 	}
593 
594 	if (rc == 0 && count > 0)
595 		rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
596 					fprs, offsetof(s390_fp_regs, fprs), -1);
597 	if (rc)
598 		return rc;
599 	convert_fp_to_vx(target->thread.ufpu.vxrs, fprs);
600 	return rc;
601 }
602 
603 static int s390_last_break_get(struct task_struct *target,
604 			       const struct user_regset *regset,
605 			       struct membuf to)
606 {
607 	return membuf_store(&to, target->thread.last_break);
608 }
609 
610 static int s390_last_break_set(struct task_struct *target,
611 			       const struct user_regset *regset,
612 			       unsigned int pos, unsigned int count,
613 			       const void *kbuf, const void __user *ubuf)
614 {
615 	return 0;
616 }
617 
618 static int s390_tdb_get(struct task_struct *target,
619 			const struct user_regset *regset,
620 			struct membuf to)
621 {
622 	struct pt_regs *regs = task_pt_regs(target);
623 	size_t size;
624 
625 	if (!(regs->int_code & 0x200))
626 		return -ENODATA;
627 	size = sizeof(target->thread.trap_tdb.data);
628 	return membuf_write(&to, target->thread.trap_tdb.data, size);
629 }
630 
631 static int s390_tdb_set(struct task_struct *target,
632 			const struct user_regset *regset,
633 			unsigned int pos, unsigned int count,
634 			const void *kbuf, const void __user *ubuf)
635 {
636 	return 0;
637 }
638 
639 static int s390_vxrs_low_get(struct task_struct *target,
640 			     const struct user_regset *regset,
641 			     struct membuf to)
642 {
643 	__u64 vxrs[__NUM_VXRS_LOW];
644 	int i;
645 
646 	if (!cpu_has_vx())
647 		return -ENODEV;
648 	if (target == current)
649 		save_user_fpu_regs();
650 	for (i = 0; i < __NUM_VXRS_LOW; i++)
651 		vxrs[i] = target->thread.ufpu.vxrs[i].low;
652 	return membuf_write(&to, vxrs, sizeof(vxrs));
653 }
654 
655 static int s390_vxrs_low_set(struct task_struct *target,
656 			     const struct user_regset *regset,
657 			     unsigned int pos, unsigned int count,
658 			     const void *kbuf, const void __user *ubuf)
659 {
660 	__u64 vxrs[__NUM_VXRS_LOW];
661 	int i, rc;
662 
663 	if (!cpu_has_vx())
664 		return -ENODEV;
665 	if (target == current)
666 		save_user_fpu_regs();
667 
668 	for (i = 0; i < __NUM_VXRS_LOW; i++)
669 		vxrs[i] = target->thread.ufpu.vxrs[i].low;
670 
671 	rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vxrs, 0, -1);
672 	if (rc == 0)
673 		for (i = 0; i < __NUM_VXRS_LOW; i++)
674 			target->thread.ufpu.vxrs[i].low = vxrs[i];
675 
676 	return rc;
677 }
678 
679 static int s390_vxrs_high_get(struct task_struct *target,
680 			      const struct user_regset *regset,
681 			      struct membuf to)
682 {
683 	if (!cpu_has_vx())
684 		return -ENODEV;
685 	if (target == current)
686 		save_user_fpu_regs();
687 	return membuf_write(&to, target->thread.ufpu.vxrs + __NUM_VXRS_LOW,
688 			    __NUM_VXRS_HIGH * sizeof(__vector128));
689 }
690 
691 static int s390_vxrs_high_set(struct task_struct *target,
692 			      const struct user_regset *regset,
693 			      unsigned int pos, unsigned int count,
694 			      const void *kbuf, const void __user *ubuf)
695 {
696 	int rc;
697 
698 	if (!cpu_has_vx())
699 		return -ENODEV;
700 	if (target == current)
701 		save_user_fpu_regs();
702 
703 	rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
704 				target->thread.ufpu.vxrs + __NUM_VXRS_LOW, 0, -1);
705 	return rc;
706 }
707 
708 static int s390_system_call_get(struct task_struct *target,
709 				const struct user_regset *regset,
710 				struct membuf to)
711 {
712 	return membuf_store(&to, target->thread.system_call);
713 }
714 
715 static int s390_system_call_set(struct task_struct *target,
716 				const struct user_regset *regset,
717 				unsigned int pos, unsigned int count,
718 				const void *kbuf, const void __user *ubuf)
719 {
720 	unsigned int *data = &target->thread.system_call;
721 	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
722 				  data, 0, sizeof(unsigned int));
723 }
724 
725 static int s390_gs_cb_get(struct task_struct *target,
726 			  const struct user_regset *regset,
727 			  struct membuf to)
728 {
729 	struct gs_cb *data = target->thread.gs_cb;
730 
731 	if (!cpu_has_gs())
732 		return -ENODEV;
733 	if (!data)
734 		return -ENODATA;
735 	if (target == current)
736 		save_gs_cb(data);
737 	return membuf_write(&to, data, sizeof(struct gs_cb));
738 }
739 
740 static int s390_gs_cb_set(struct task_struct *target,
741 			  const struct user_regset *regset,
742 			  unsigned int pos, unsigned int count,
743 			  const void *kbuf, const void __user *ubuf)
744 {
745 	struct gs_cb gs_cb = { }, *data = NULL;
746 	int rc;
747 
748 	if (!cpu_has_gs())
749 		return -ENODEV;
750 	if (!target->thread.gs_cb) {
751 		data = kzalloc_obj(*data);
752 		if (!data)
753 			return -ENOMEM;
754 	}
755 	if (!target->thread.gs_cb)
756 		gs_cb.gsd = 25;
757 	else if (target == current)
758 		save_gs_cb(&gs_cb);
759 	else
760 		gs_cb = *target->thread.gs_cb;
761 	rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
762 				&gs_cb, 0, sizeof(gs_cb));
763 	if (rc) {
764 		kfree(data);
765 		return -EFAULT;
766 	}
767 	preempt_disable();
768 	if (!target->thread.gs_cb)
769 		target->thread.gs_cb = data;
770 	*target->thread.gs_cb = gs_cb;
771 	if (target == current) {
772 		local_ctl_set_bit(2, CR2_GUARDED_STORAGE_BIT);
773 		restore_gs_cb(target->thread.gs_cb);
774 	}
775 	preempt_enable();
776 	return rc;
777 }
778 
779 static int s390_gs_bc_get(struct task_struct *target,
780 			  const struct user_regset *regset,
781 			  struct membuf to)
782 {
783 	struct gs_cb *data = target->thread.gs_bc_cb;
784 
785 	if (!cpu_has_gs())
786 		return -ENODEV;
787 	if (!data)
788 		return -ENODATA;
789 	return membuf_write(&to, data, sizeof(struct gs_cb));
790 }
791 
792 static int s390_gs_bc_set(struct task_struct *target,
793 			  const struct user_regset *regset,
794 			  unsigned int pos, unsigned int count,
795 			  const void *kbuf, const void __user *ubuf)
796 {
797 	struct gs_cb *data = target->thread.gs_bc_cb;
798 
799 	if (!cpu_has_gs())
800 		return -ENODEV;
801 	if (!data) {
802 		data = kzalloc_obj(*data);
803 		if (!data)
804 			return -ENOMEM;
805 		target->thread.gs_bc_cb = data;
806 	}
807 	return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
808 				  data, 0, sizeof(struct gs_cb));
809 }
810 
811 static bool is_ri_cb_valid(struct runtime_instr_cb *cb)
812 {
813 	return (cb->rca & 0x1f) == 0 &&
814 		(cb->roa & 0xfff) == 0 &&
815 		(cb->rla & 0xfff) == 0xfff &&
816 		cb->s == 1 &&
817 		cb->k == 1 &&
818 		cb->h == 0 &&
819 		cb->reserved1 == 0 &&
820 		cb->ps == 1 &&
821 		cb->qs == 0 &&
822 		cb->pc == 1 &&
823 		cb->qc == 0 &&
824 		cb->reserved2 == 0 &&
825 		cb->reserved3 == 0 &&
826 		cb->reserved4 == 0 &&
827 		cb->reserved5 == 0 &&
828 		cb->reserved6 == 0 &&
829 		cb->reserved7 == 0 &&
830 		cb->reserved8 == 0 &&
831 		cb->rla >= cb->roa &&
832 		cb->rca >= cb->roa &&
833 		cb->rca <= cb->rla+1 &&
834 		cb->m < 3;
835 }
836 
837 static int s390_runtime_instr_get(struct task_struct *target,
838 				const struct user_regset *regset,
839 				struct membuf to)
840 {
841 	struct runtime_instr_cb *data = target->thread.ri_cb;
842 
843 	if (!test_facility(64))
844 		return -ENODEV;
845 	if (!data)
846 		return -ENODATA;
847 
848 	return membuf_write(&to, data, sizeof(struct runtime_instr_cb));
849 }
850 
851 static int s390_runtime_instr_set(struct task_struct *target,
852 				  const struct user_regset *regset,
853 				  unsigned int pos, unsigned int count,
854 				  const void *kbuf, const void __user *ubuf)
855 {
856 	struct runtime_instr_cb ri_cb = { }, *data = NULL;
857 	int rc;
858 
859 	if (!test_facility(64))
860 		return -ENODEV;
861 
862 	if (!target->thread.ri_cb) {
863 		data = kzalloc_obj(*data);
864 		if (!data)
865 			return -ENOMEM;
866 	}
867 
868 	if (target->thread.ri_cb) {
869 		if (target == current)
870 			store_runtime_instr_cb(&ri_cb);
871 		else
872 			ri_cb = *target->thread.ri_cb;
873 	}
874 
875 	rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
876 				&ri_cb, 0, sizeof(struct runtime_instr_cb));
877 	if (rc) {
878 		kfree(data);
879 		return -EFAULT;
880 	}
881 
882 	if (!is_ri_cb_valid(&ri_cb)) {
883 		kfree(data);
884 		return -EINVAL;
885 	}
886 	/*
887 	 * Override access key in any case, since user space should
888 	 * not be able to set it, nor should it care about it.
889 	 */
890 	ri_cb.key = PAGE_DEFAULT_KEY >> 4;
891 	preempt_disable();
892 	if (!target->thread.ri_cb)
893 		target->thread.ri_cb = data;
894 	*target->thread.ri_cb = ri_cb;
895 	if (target == current)
896 		load_runtime_instr_cb(target->thread.ri_cb);
897 	preempt_enable();
898 
899 	return 0;
900 }
901 
902 static const struct user_regset s390_regsets[] = {
903 	{
904 		USER_REGSET_NOTE_TYPE(PRSTATUS),
905 		.n = sizeof(s390_regs) / sizeof(long),
906 		.size = sizeof(long),
907 		.align = sizeof(long),
908 		.regset_get = s390_regs_get,
909 		.set = s390_regs_set,
910 	},
911 	{
912 		USER_REGSET_NOTE_TYPE(PRFPREG),
913 		.n = sizeof(s390_fp_regs) / sizeof(long),
914 		.size = sizeof(long),
915 		.align = sizeof(long),
916 		.regset_get = s390_fpregs_get,
917 		.set = s390_fpregs_set,
918 	},
919 	{
920 		USER_REGSET_NOTE_TYPE(S390_SYSTEM_CALL),
921 		.n = 1,
922 		.size = sizeof(unsigned int),
923 		.align = sizeof(unsigned int),
924 		.regset_get = s390_system_call_get,
925 		.set = s390_system_call_set,
926 	},
927 	{
928 		USER_REGSET_NOTE_TYPE(S390_LAST_BREAK),
929 		.n = 1,
930 		.size = sizeof(long),
931 		.align = sizeof(long),
932 		.regset_get = s390_last_break_get,
933 		.set = s390_last_break_set,
934 	},
935 	{
936 		USER_REGSET_NOTE_TYPE(S390_TDB),
937 		.n = 1,
938 		.size = 256,
939 		.align = 1,
940 		.regset_get = s390_tdb_get,
941 		.set = s390_tdb_set,
942 	},
943 	{
944 		USER_REGSET_NOTE_TYPE(S390_VXRS_LOW),
945 		.n = __NUM_VXRS_LOW,
946 		.size = sizeof(__u64),
947 		.align = sizeof(__u64),
948 		.regset_get = s390_vxrs_low_get,
949 		.set = s390_vxrs_low_set,
950 	},
951 	{
952 		USER_REGSET_NOTE_TYPE(S390_VXRS_HIGH),
953 		.n = __NUM_VXRS_HIGH,
954 		.size = sizeof(__vector128),
955 		.align = sizeof(__vector128),
956 		.regset_get = s390_vxrs_high_get,
957 		.set = s390_vxrs_high_set,
958 	},
959 	{
960 		USER_REGSET_NOTE_TYPE(S390_GS_CB),
961 		.n = sizeof(struct gs_cb) / sizeof(__u64),
962 		.size = sizeof(__u64),
963 		.align = sizeof(__u64),
964 		.regset_get = s390_gs_cb_get,
965 		.set = s390_gs_cb_set,
966 	},
967 	{
968 		USER_REGSET_NOTE_TYPE(S390_GS_BC),
969 		.n = sizeof(struct gs_cb) / sizeof(__u64),
970 		.size = sizeof(__u64),
971 		.align = sizeof(__u64),
972 		.regset_get = s390_gs_bc_get,
973 		.set = s390_gs_bc_set,
974 	},
975 	{
976 		USER_REGSET_NOTE_TYPE(S390_RI_CB),
977 		.n = sizeof(struct runtime_instr_cb) / sizeof(__u64),
978 		.size = sizeof(__u64),
979 		.align = sizeof(__u64),
980 		.regset_get = s390_runtime_instr_get,
981 		.set = s390_runtime_instr_set,
982 	},
983 };
984 
985 static const struct user_regset_view user_s390_view = {
986 	.name = "s390x",
987 	.e_machine = EM_S390,
988 	.regsets = s390_regsets,
989 	.n = ARRAY_SIZE(s390_regsets)
990 };
991 
992 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
993 {
994 	return &user_s390_view;
995 }
996 
997 static const char *gpr_names[NUM_GPRS] = {
998 	"r0", "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
999 	"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1000 };
1001 
1002 int regs_query_register_offset(const char *name)
1003 {
1004 	unsigned long offset;
1005 
1006 	if (!name || *name != 'r')
1007 		return -EINVAL;
1008 	if (kstrtoul(name + 1, 10, &offset))
1009 		return -EINVAL;
1010 	if (offset >= NUM_GPRS)
1011 		return -EINVAL;
1012 	return offset;
1013 }
1014 
1015 const char *regs_query_register_name(unsigned int offset)
1016 {
1017 	if (offset >= NUM_GPRS)
1018 		return NULL;
1019 	return gpr_names[offset];
1020 }
1021