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