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) ®s->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 if ((unsigned int) data != 0 ||
415 test_fp_ctl(data >> (BITS_PER_LONG - 32)))
416 return -EINVAL;
417 child->thread.fpu.fpc = data >> (BITS_PER_LONG - 32);
418
419 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
420 /*
421 * floating point regs. are either in child->thread.fpu
422 * or the child->thread.fpu.vxrs array
423 */
424 offset = addr - (addr_t) &dummy->regs.fp_regs.fprs;
425 if (MACHINE_HAS_VX)
426 *(addr_t *)((addr_t)
427 child->thread.fpu.vxrs + 2*offset) = data;
428 else
429 *(addr_t *)((addr_t)
430 child->thread.fpu.fprs + offset) = data;
431
432 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
433 /*
434 * Handle access to the per_info structure.
435 */
436 addr -= (addr_t) &dummy->regs.per_info;
437 __poke_user_per(child, addr, data);
438
439 }
440
441 return 0;
442 }
443
poke_user(struct task_struct * child,addr_t addr,addr_t data)444 static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
445 {
446 addr_t mask;
447
448 /*
449 * Stupid gdb peeks/pokes the access registers in 64 bit with
450 * an alignment of 4. Programmers from hell indeed...
451 */
452 mask = __ADDR_MASK;
453 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
454 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
455 mask = 3;
456 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
457 return -EIO;
458
459 return __poke_user(child, addr, data);
460 }
461
arch_ptrace(struct task_struct * child,long request,unsigned long addr,unsigned long data)462 long arch_ptrace(struct task_struct *child, long request,
463 unsigned long addr, unsigned long data)
464 {
465 ptrace_area parea;
466 int copied, ret;
467
468 switch (request) {
469 case PTRACE_PEEKUSR:
470 /* read the word at location addr in the USER area. */
471 return peek_user(child, addr, data);
472
473 case PTRACE_POKEUSR:
474 /* write the word at location addr in the USER area */
475 return poke_user(child, addr, data);
476
477 case PTRACE_PEEKUSR_AREA:
478 case PTRACE_POKEUSR_AREA:
479 if (copy_from_user(&parea, (void __force __user *) addr,
480 sizeof(parea)))
481 return -EFAULT;
482 addr = parea.kernel_addr;
483 data = parea.process_addr;
484 copied = 0;
485 while (copied < parea.len) {
486 if (request == PTRACE_PEEKUSR_AREA)
487 ret = peek_user(child, addr, data);
488 else {
489 addr_t utmp;
490 if (get_user(utmp,
491 (addr_t __force __user *) data))
492 return -EFAULT;
493 ret = poke_user(child, addr, utmp);
494 }
495 if (ret)
496 return ret;
497 addr += sizeof(unsigned long);
498 data += sizeof(unsigned long);
499 copied += sizeof(unsigned long);
500 }
501 return 0;
502 case PTRACE_GET_LAST_BREAK:
503 return put_user(child->thread.last_break, (unsigned long __user *)data);
504 case PTRACE_ENABLE_TE:
505 if (!MACHINE_HAS_TE)
506 return -EIO;
507 child->thread.per_flags &= ~PER_FLAG_NO_TE;
508 return 0;
509 case PTRACE_DISABLE_TE:
510 if (!MACHINE_HAS_TE)
511 return -EIO;
512 child->thread.per_flags |= PER_FLAG_NO_TE;
513 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
514 return 0;
515 case PTRACE_TE_ABORT_RAND:
516 if (!MACHINE_HAS_TE || (child->thread.per_flags & PER_FLAG_NO_TE))
517 return -EIO;
518 switch (data) {
519 case 0UL:
520 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
521 break;
522 case 1UL:
523 child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
524 child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND_TEND;
525 break;
526 case 2UL:
527 child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
528 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND_TEND;
529 break;
530 default:
531 return -EINVAL;
532 }
533 return 0;
534 default:
535 return ptrace_request(child, request, addr, data);
536 }
537 }
538
539 #ifdef CONFIG_COMPAT
540 /*
541 * Now the fun part starts... a 31 bit program running in the
542 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
543 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
544 * to handle, the difference to the 64 bit versions of the requests
545 * is that the access is done in multiples of 4 byte instead of
546 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
547 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
548 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
549 * is a 31 bit program too, the content of struct user can be
550 * emulated. A 31 bit program peeking into the struct user of
551 * a 64 bit program is a no-no.
552 */
553
554 /*
555 * Same as peek_user_per but for a 31 bit program.
556 */
__peek_user_per_compat(struct task_struct * child,addr_t addr)557 static inline __u32 __peek_user_per_compat(struct task_struct *child,
558 addr_t addr)
559 {
560 struct compat_per_struct_kernel *dummy32 = NULL;
561
562 if (addr == (addr_t) &dummy32->cr9)
563 /* Control bits of the active per set. */
564 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
565 PER_EVENT_IFETCH : child->thread.per_user.control;
566 else if (addr == (addr_t) &dummy32->cr10)
567 /* Start address of the active per set. */
568 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
569 0 : child->thread.per_user.start;
570 else if (addr == (addr_t) &dummy32->cr11)
571 /* End address of the active per set. */
572 return test_thread_flag(TIF_SINGLE_STEP) ?
573 PSW32_ADDR_INSN : child->thread.per_user.end;
574 else if (addr == (addr_t) &dummy32->bits)
575 /* Single-step bit. */
576 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
577 0x80000000 : 0;
578 else if (addr == (addr_t) &dummy32->starting_addr)
579 /* Start address of the user specified per set. */
580 return (__u32) child->thread.per_user.start;
581 else if (addr == (addr_t) &dummy32->ending_addr)
582 /* End address of the user specified per set. */
583 return (__u32) child->thread.per_user.end;
584 else if (addr == (addr_t) &dummy32->perc_atmid)
585 /* PER code, ATMID and AI of the last PER trap */
586 return (__u32) child->thread.per_event.cause << 16;
587 else if (addr == (addr_t) &dummy32->address)
588 /* Address of the last PER trap */
589 return (__u32) child->thread.per_event.address;
590 else if (addr == (addr_t) &dummy32->access_id)
591 /* Access id of the last PER trap */
592 return (__u32) child->thread.per_event.paid << 24;
593 return 0;
594 }
595
596 /*
597 * Same as peek_user but for a 31 bit program.
598 */
__peek_user_compat(struct task_struct * child,addr_t addr)599 static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
600 {
601 struct compat_user *dummy32 = NULL;
602 addr_t offset;
603 __u32 tmp;
604
605 if (addr < (addr_t) &dummy32->regs.acrs) {
606 struct pt_regs *regs = task_pt_regs(child);
607 /*
608 * psw and gprs are stored on the stack
609 */
610 if (addr == (addr_t) &dummy32->regs.psw.mask) {
611 /* Fake a 31 bit psw mask. */
612 tmp = (__u32)(regs->psw.mask >> 32);
613 tmp &= PSW32_MASK_USER | PSW32_MASK_RI;
614 tmp |= PSW32_USER_BITS;
615 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
616 /* Fake a 31 bit psw address. */
617 tmp = (__u32) regs->psw.addr |
618 (__u32)(regs->psw.mask & PSW_MASK_BA);
619 } else {
620 /* gpr 0-15 */
621 tmp = *(__u32 *)((addr_t) ®s->psw + addr*2 + 4);
622 }
623 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
624 /*
625 * access registers are stored in the thread structure
626 */
627 offset = addr - (addr_t) &dummy32->regs.acrs;
628 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
629
630 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
631 /*
632 * orig_gpr2 is stored on the kernel stack
633 */
634 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
635
636 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
637 /*
638 * prevent reads of padding hole between
639 * orig_gpr2 and fp_regs on s390.
640 */
641 tmp = 0;
642
643 } else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
644 /*
645 * floating point control reg. is in the thread structure
646 */
647 tmp = child->thread.fpu.fpc;
648
649 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
650 /*
651 * floating point regs. are either in child->thread.fpu
652 * or the child->thread.fpu.vxrs array
653 */
654 offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
655 if (MACHINE_HAS_VX)
656 tmp = *(__u32 *)
657 ((addr_t) child->thread.fpu.vxrs + 2*offset);
658 else
659 tmp = *(__u32 *)
660 ((addr_t) child->thread.fpu.fprs + offset);
661
662 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
663 /*
664 * Handle access to the per_info structure.
665 */
666 addr -= (addr_t) &dummy32->regs.per_info;
667 tmp = __peek_user_per_compat(child, addr);
668
669 } else
670 tmp = 0;
671
672 return tmp;
673 }
674
peek_user_compat(struct task_struct * child,addr_t addr,addr_t data)675 static int peek_user_compat(struct task_struct *child,
676 addr_t addr, addr_t data)
677 {
678 __u32 tmp;
679
680 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
681 return -EIO;
682
683 tmp = __peek_user_compat(child, addr);
684 return put_user(tmp, (__u32 __user *) data);
685 }
686
687 /*
688 * Same as poke_user_per but for a 31 bit program.
689 */
__poke_user_per_compat(struct task_struct * child,addr_t addr,__u32 data)690 static inline void __poke_user_per_compat(struct task_struct *child,
691 addr_t addr, __u32 data)
692 {
693 struct compat_per_struct_kernel *dummy32 = NULL;
694
695 if (addr == (addr_t) &dummy32->cr9)
696 /* PER event mask of the user specified per set. */
697 child->thread.per_user.control =
698 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
699 else if (addr == (addr_t) &dummy32->starting_addr)
700 /* Starting address of the user specified per set. */
701 child->thread.per_user.start = data;
702 else if (addr == (addr_t) &dummy32->ending_addr)
703 /* Ending address of the user specified per set. */
704 child->thread.per_user.end = data;
705 }
706
707 /*
708 * Same as poke_user but for a 31 bit program.
709 */
__poke_user_compat(struct task_struct * child,addr_t addr,addr_t data)710 static int __poke_user_compat(struct task_struct *child,
711 addr_t addr, addr_t data)
712 {
713 struct compat_user *dummy32 = NULL;
714 __u32 tmp = (__u32) data;
715 addr_t offset;
716
717 if (addr < (addr_t) &dummy32->regs.acrs) {
718 struct pt_regs *regs = task_pt_regs(child);
719 /*
720 * psw, gprs, acrs and orig_gpr2 are stored on the stack
721 */
722 if (addr == (addr_t) &dummy32->regs.psw.mask) {
723 __u32 mask = PSW32_MASK_USER;
724
725 mask |= is_ri_task(child) ? PSW32_MASK_RI : 0;
726 /* Build a 64 bit psw mask from 31 bit mask. */
727 if ((tmp ^ PSW32_USER_BITS) & ~mask)
728 /* Invalid psw mask. */
729 return -EINVAL;
730 if ((data & PSW32_MASK_ASC) == PSW32_ASC_HOME)
731 /* Invalid address-space-control bits */
732 return -EINVAL;
733 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
734 (regs->psw.mask & PSW_MASK_BA) |
735 (__u64)(tmp & mask) << 32;
736 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
737 /* Build a 64 bit psw address from 31 bit address. */
738 regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
739 /* Transfer 31 bit amode bit to psw mask. */
740 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
741 (__u64)(tmp & PSW32_ADDR_AMODE);
742 } else {
743
744 if (test_pt_regs_flag(regs, PIF_SYSCALL) &&
745 addr == offsetof(struct compat_user, regs.gprs[2]))
746 fixup_int_code(child, data);
747 /* gpr 0-15 */
748 *(__u32*)((addr_t) ®s->psw + addr*2 + 4) = tmp;
749 }
750 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
751 /*
752 * access registers are stored in the thread structure
753 */
754 offset = addr - (addr_t) &dummy32->regs.acrs;
755 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
756
757 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
758 /*
759 * orig_gpr2 is stored on the kernel stack
760 */
761 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
762
763 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
764 /*
765 * prevent writess of padding hole between
766 * orig_gpr2 and fp_regs on s390.
767 */
768 return 0;
769
770 } else if (addr == (addr_t) &dummy32->regs.fp_regs.fpc) {
771 /*
772 * floating point control reg. is in the thread structure
773 */
774 if (test_fp_ctl(tmp))
775 return -EINVAL;
776 child->thread.fpu.fpc = data;
777
778 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
779 /*
780 * floating point regs. are either in child->thread.fpu
781 * or the child->thread.fpu.vxrs array
782 */
783 offset = addr - (addr_t) &dummy32->regs.fp_regs.fprs;
784 if (MACHINE_HAS_VX)
785 *(__u32 *)((addr_t)
786 child->thread.fpu.vxrs + 2*offset) = tmp;
787 else
788 *(__u32 *)((addr_t)
789 child->thread.fpu.fprs + offset) = tmp;
790
791 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
792 /*
793 * Handle access to the per_info structure.
794 */
795 addr -= (addr_t) &dummy32->regs.per_info;
796 __poke_user_per_compat(child, addr, data);
797 }
798
799 return 0;
800 }
801
poke_user_compat(struct task_struct * child,addr_t addr,addr_t data)802 static int poke_user_compat(struct task_struct *child,
803 addr_t addr, addr_t data)
804 {
805 if (!is_compat_task() || (addr & 3) ||
806 addr > sizeof(struct compat_user) - 3)
807 return -EIO;
808
809 return __poke_user_compat(child, addr, data);
810 }
811
compat_arch_ptrace(struct task_struct * child,compat_long_t request,compat_ulong_t caddr,compat_ulong_t cdata)812 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
813 compat_ulong_t caddr, compat_ulong_t cdata)
814 {
815 unsigned long addr = caddr;
816 unsigned long data = cdata;
817 compat_ptrace_area parea;
818 int copied, ret;
819
820 switch (request) {
821 case PTRACE_PEEKUSR:
822 /* read the word at location addr in the USER area. */
823 return peek_user_compat(child, addr, data);
824
825 case PTRACE_POKEUSR:
826 /* write the word at location addr in the USER area */
827 return poke_user_compat(child, addr, data);
828
829 case PTRACE_PEEKUSR_AREA:
830 case PTRACE_POKEUSR_AREA:
831 if (copy_from_user(&parea, (void __force __user *) addr,
832 sizeof(parea)))
833 return -EFAULT;
834 addr = parea.kernel_addr;
835 data = parea.process_addr;
836 copied = 0;
837 while (copied < parea.len) {
838 if (request == PTRACE_PEEKUSR_AREA)
839 ret = peek_user_compat(child, addr, data);
840 else {
841 __u32 utmp;
842 if (get_user(utmp,
843 (__u32 __force __user *) data))
844 return -EFAULT;
845 ret = poke_user_compat(child, addr, utmp);
846 }
847 if (ret)
848 return ret;
849 addr += sizeof(unsigned int);
850 data += sizeof(unsigned int);
851 copied += sizeof(unsigned int);
852 }
853 return 0;
854 case PTRACE_GET_LAST_BREAK:
855 return put_user(child->thread.last_break, (unsigned int __user *)data);
856 }
857 return compat_ptrace_request(child, request, addr, data);
858 }
859 #endif
860
do_syscall_trace_enter(struct pt_regs * regs)861 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
862 {
863 unsigned long mask = -1UL;
864 long ret = -1;
865
866 if (is_compat_task())
867 mask = 0xffffffff;
868
869 /*
870 * The sysc_tracesys code in entry.S stored the system
871 * call number to gprs[2].
872 */
873 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
874 tracehook_report_syscall_entry(regs)) {
875 /*
876 * Tracing decided this syscall should not happen. Skip
877 * the system call and the system call restart handling.
878 */
879 goto skip;
880 }
881
882 #ifdef CONFIG_SECCOMP
883 /* Do the secure computing check after ptrace. */
884 if (unlikely(test_thread_flag(TIF_SECCOMP))) {
885 struct seccomp_data sd;
886
887 if (is_compat_task()) {
888 sd.instruction_pointer = regs->psw.addr & 0x7fffffff;
889 sd.arch = AUDIT_ARCH_S390;
890 } else {
891 sd.instruction_pointer = regs->psw.addr;
892 sd.arch = AUDIT_ARCH_S390X;
893 }
894
895 sd.nr = regs->int_code & 0xffff;
896 sd.args[0] = regs->orig_gpr2 & mask;
897 sd.args[1] = regs->gprs[3] & mask;
898 sd.args[2] = regs->gprs[4] & mask;
899 sd.args[3] = regs->gprs[5] & mask;
900 sd.args[4] = regs->gprs[6] & mask;
901 sd.args[5] = regs->gprs[7] & mask;
902
903 if (__secure_computing(&sd) == -1)
904 goto skip;
905 }
906 #endif /* CONFIG_SECCOMP */
907
908 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
909 trace_sys_enter(regs, regs->int_code & 0xffff);
910
911
912 audit_syscall_entry(regs->int_code & 0xffff, regs->orig_gpr2 & mask,
913 regs->gprs[3] &mask, regs->gprs[4] &mask,
914 regs->gprs[5] &mask);
915
916 if ((signed long)regs->gprs[2] >= NR_syscalls) {
917 regs->gprs[2] = -ENOSYS;
918 ret = -ENOSYS;
919 }
920 return regs->gprs[2];
921 skip:
922 clear_pt_regs_flag(regs, PIF_SYSCALL);
923 return ret;
924 }
925
do_syscall_trace_exit(struct pt_regs * regs)926 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
927 {
928 audit_syscall_exit(regs);
929
930 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
931 trace_sys_exit(regs, regs->gprs[2]);
932
933 if (test_thread_flag(TIF_SYSCALL_TRACE))
934 tracehook_report_syscall_exit(regs, 0);
935 }
936
937 /*
938 * user_regset definitions.
939 */
940
s390_regs_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)941 static int s390_regs_get(struct task_struct *target,
942 const struct user_regset *regset,
943 struct membuf to)
944 {
945 unsigned pos;
946 if (target == current)
947 save_access_regs(target->thread.acrs);
948
949 for (pos = 0; pos < sizeof(s390_regs); pos += sizeof(long))
950 membuf_store(&to, __peek_user(target, pos));
951 return 0;
952 }
953
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)954 static int s390_regs_set(struct task_struct *target,
955 const struct user_regset *regset,
956 unsigned int pos, unsigned int count,
957 const void *kbuf, const void __user *ubuf)
958 {
959 int rc = 0;
960
961 if (target == current)
962 save_access_regs(target->thread.acrs);
963
964 if (kbuf) {
965 const unsigned long *k = kbuf;
966 while (count > 0 && !rc) {
967 rc = __poke_user(target, pos, *k++);
968 count -= sizeof(*k);
969 pos += sizeof(*k);
970 }
971 } else {
972 const unsigned long __user *u = ubuf;
973 while (count > 0 && !rc) {
974 unsigned long word;
975 rc = __get_user(word, u++);
976 if (rc)
977 break;
978 rc = __poke_user(target, pos, word);
979 count -= sizeof(*u);
980 pos += sizeof(*u);
981 }
982 }
983
984 if (rc == 0 && target == current)
985 restore_access_regs(target->thread.acrs);
986
987 return rc;
988 }
989
s390_fpregs_get(struct task_struct * target,const struct user_regset * regset,struct membuf to)990 static int s390_fpregs_get(struct task_struct *target,
991 const struct user_regset *regset,
992 struct membuf to)
993 {
994 _s390_fp_regs fp_regs;
995
996 if (target == current)
997 save_fpu_regs();
998
999 fp_regs.fpc = target->thread.fpu.fpc;
1000 fpregs_store(&fp_regs, &target->thread.fpu);
1001
1002 return membuf_write(&to, &fp_regs, sizeof(fp_regs));
1003 }
1004
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)1005 static int s390_fpregs_set(struct task_struct *target,
1006 const struct user_regset *regset, unsigned int pos,
1007 unsigned int count, const void *kbuf,
1008 const void __user *ubuf)
1009 {
1010 int rc = 0;
1011 freg_t fprs[__NUM_FPRS];
1012
1013 if (target == current)
1014 save_fpu_regs();
1015
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