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