2 * Ptrace user space interface.
4 * Copyright IBM Corp. 1999, 2010
5 * Author(s): Denis Joseph Barrow
6 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
12 #include <linux/smp.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/user.h>
16 #include <linux/security.h>
17 #include <linux/audit.h>
18 #include <linux/signal.h>
19 #include <linux/elf.h>
20 #include <linux/regset.h>
21 #include <linux/tracehook.h>
22 #include <linux/seccomp.h>
23 #include <linux/compat.h>
24 #include <trace/syscall.h>
25 #include <asm/segment.h>
27 #include <asm/pgtable.h>
28 #include <asm/pgalloc.h>
29 #include <asm/uaccess.h>
30 #include <asm/unistd.h>
31 #include <asm/switch_to.h>
35 #include "compat_ptrace.h"
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/syscalls.h>
47 REGSET_GENERAL_EXTENDED,
50 void update_cr_regs(struct task_struct *task)
52 struct pt_regs *regs = task_pt_regs(task);
53 struct thread_struct *thread = &task->thread;
54 struct per_regs old, new;
57 /* Take care of the enable/disable of transactional execution. */
59 unsigned long cr, cr_new;
61 __ctl_store(cr, 0, 0);
62 /* Set or clear transaction execution TXC bit 8. */
63 cr_new = cr | (1UL << 55);
64 if (task->thread.per_flags & PER_FLAG_NO_TE)
65 cr_new &= ~(1UL << 55);
67 __ctl_load(cr_new, 0, 0);
68 /* Set or clear transaction execution TDC bits 62 and 63. */
69 __ctl_store(cr, 2, 2);
71 if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND) {
72 if (task->thread.per_flags & PER_FLAG_TE_ABORT_RAND_TEND)
78 __ctl_load(cr_new, 2, 2);
81 /* Copy user specified PER registers */
82 new.control = thread->per_user.control;
83 new.start = thread->per_user.start;
84 new.end = thread->per_user.end;
86 /* merge TIF_SINGLE_STEP into user specified PER registers. */
87 if (test_tsk_thread_flag(task, TIF_SINGLE_STEP)) {
88 if (test_tsk_thread_flag(task, TIF_BLOCK_STEP))
89 new.control |= PER_EVENT_BRANCH;
91 new.control |= PER_EVENT_IFETCH;
93 new.control |= PER_CONTROL_SUSPENSION;
94 new.control |= PER_EVENT_TRANSACTION_END;
97 new.end = PSW_ADDR_INSN;
100 /* Take care of the PER enablement bit in the PSW. */
101 if (!(new.control & PER_EVENT_MASK)) {
102 regs->psw.mask &= ~PSW_MASK_PER;
105 regs->psw.mask |= PSW_MASK_PER;
106 __ctl_store(old, 9, 11);
107 if (memcmp(&new, &old, sizeof(struct per_regs)) != 0)
108 __ctl_load(new, 9, 11);
111 void user_enable_single_step(struct task_struct *task)
113 clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
114 set_tsk_thread_flag(task, TIF_SINGLE_STEP);
117 void user_disable_single_step(struct task_struct *task)
119 clear_tsk_thread_flag(task, TIF_BLOCK_STEP);
120 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
123 void user_enable_block_step(struct task_struct *task)
125 set_tsk_thread_flag(task, TIF_SINGLE_STEP);
126 set_tsk_thread_flag(task, TIF_BLOCK_STEP);
130 * Called by kernel/ptrace.c when detaching..
132 * Clear all debugging related fields.
134 void ptrace_disable(struct task_struct *task)
136 memset(&task->thread.per_user, 0, sizeof(task->thread.per_user));
137 memset(&task->thread.per_event, 0, sizeof(task->thread.per_event));
138 clear_tsk_thread_flag(task, TIF_SINGLE_STEP);
139 clear_pt_regs_flag(task_pt_regs(task), PIF_PER_TRAP);
140 task->thread.per_flags = 0;
144 # define __ADDR_MASK 3
146 # define __ADDR_MASK 7
149 static inline unsigned long __peek_user_per(struct task_struct *child,
152 struct per_struct_kernel *dummy = NULL;
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 PSW_ADDR_INSN : 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);
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.
199 static unsigned long __peek_user(struct task_struct *child, addr_t addr)
201 struct user *dummy = NULL;
204 if (addr < (addr_t) &dummy->regs.acrs) {
206 * psw and gprs are stored on the stack
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;
215 } else if (addr < (addr_t) &dummy->regs.orig_gpr2) {
217 * access registers are stored in the thread structure
219 offset = addr - (addr_t) &dummy->regs.acrs;
222 * Very special case: old & broken 64 bit gdb reading
223 * from acrs[15]. Result is a 64 bit value. Read the
224 * 32 bit acrs[15] value and shift it by 32. Sick...
226 if (addr == (addr_t) &dummy->regs.acrs[15])
227 tmp = ((unsigned long) child->thread.acrs[15]) << 32;
230 tmp = *(addr_t *)((addr_t) &child->thread.acrs + offset);
232 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
234 * orig_gpr2 is stored on the kernel stack
236 tmp = (addr_t) task_pt_regs(child)->orig_gpr2;
238 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
240 * prevent reads of padding hole between
241 * orig_gpr2 and fp_regs on s390.
245 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
247 * floating point regs. are stored in the thread structure
249 offset = addr - (addr_t) &dummy->regs.fp_regs;
250 tmp = *(addr_t *)((addr_t) &child->thread.fp_regs + offset);
251 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
252 tmp <<= BITS_PER_LONG - 32;
254 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
256 * Handle access to the per_info structure.
258 addr -= (addr_t) &dummy->regs.per_info;
259 tmp = __peek_user_per(child, addr);
268 peek_user(struct task_struct *child, addr_t addr, addr_t data)
273 * Stupid gdb peeks/pokes the access registers in 64 bit with
274 * an alignment of 4. Programmers from hell...
278 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
279 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
282 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
285 tmp = __peek_user(child, addr);
286 return put_user(tmp, (addr_t __user *) data);
289 static inline void __poke_user_per(struct task_struct *child,
290 addr_t addr, addr_t data)
292 struct per_struct_kernel *dummy = NULL;
295 * There are only three fields in the per_info struct that the
296 * debugger user can write to.
297 * 1) cr9: the debugger wants to set a new PER event mask
298 * 2) starting_addr: the debugger wants to set a new starting
299 * address to use with the PER event mask.
300 * 3) ending_addr: the debugger wants to set a new ending
301 * address to use with the PER event mask.
302 * The user specified PER event mask and the start and end
303 * addresses are used only if single stepping is not in effect.
304 * Writes to any other field in per_info are ignored.
306 if (addr == (addr_t) &dummy->cr9)
307 /* PER event mask of the user specified per set. */
308 child->thread.per_user.control =
309 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
310 else if (addr == (addr_t) &dummy->starting_addr)
311 /* Starting address of the user specified per set. */
312 child->thread.per_user.start = data;
313 else if (addr == (addr_t) &dummy->ending_addr)
314 /* Ending address of the user specified per set. */
315 child->thread.per_user.end = data;
319 * Write a word to the user area of a process at location addr. This
320 * operation does have an additional problem compared to peek_user.
321 * Stores to the program status word and on the floating point
322 * control register needs to get checked for validity.
324 static int __poke_user(struct task_struct *child, addr_t addr, addr_t data)
326 struct user *dummy = NULL;
329 if (addr < (addr_t) &dummy->regs.acrs) {
331 * psw and gprs are stored on the stack
333 if (addr == (addr_t) &dummy->regs.psw.mask) {
334 unsigned long mask = PSW_MASK_USER;
336 mask |= is_ri_task(child) ? PSW_MASK_RI : 0;
337 if ((data ^ PSW_USER_BITS) & ~mask)
338 /* Invalid psw mask. */
340 if ((data & PSW_MASK_ASC) == PSW_ASC_HOME)
341 /* Invalid address-space-control bits */
343 if ((data & PSW_MASK_EA) && !(data & PSW_MASK_BA))
344 /* Invalid addressing mode bits */
347 *(addr_t *)((addr_t) &task_pt_regs(child)->psw + addr) = data;
349 } else if (addr < (addr_t) (&dummy->regs.orig_gpr2)) {
351 * access registers are stored in the thread structure
353 offset = addr - (addr_t) &dummy->regs.acrs;
356 * Very special case: old & broken 64 bit gdb writing
357 * to acrs[15] with a 64 bit value. Ignore the lower
358 * half of the value and write the upper 32 bit to
361 if (addr == (addr_t) &dummy->regs.acrs[15])
362 child->thread.acrs[15] = (unsigned int) (data >> 32);
365 *(addr_t *)((addr_t) &child->thread.acrs + offset) = data;
367 } else if (addr == (addr_t) &dummy->regs.orig_gpr2) {
369 * orig_gpr2 is stored on the kernel stack
371 task_pt_regs(child)->orig_gpr2 = data;
373 } else if (addr < (addr_t) &dummy->regs.fp_regs) {
375 * prevent writes of padding hole between
376 * orig_gpr2 and fp_regs on s390.
380 } else if (addr < (addr_t) (&dummy->regs.fp_regs + 1)) {
382 * floating point regs. are stored in the thread structure
384 if (addr == (addr_t) &dummy->regs.fp_regs.fpc)
385 if ((unsigned int) data != 0 ||
386 test_fp_ctl(data >> (BITS_PER_LONG - 32)))
388 offset = addr - (addr_t) &dummy->regs.fp_regs;
389 *(addr_t *)((addr_t) &child->thread.fp_regs + offset) = data;
391 } else if (addr < (addr_t) (&dummy->regs.per_info + 1)) {
393 * Handle access to the per_info structure.
395 addr -= (addr_t) &dummy->regs.per_info;
396 __poke_user_per(child, addr, data);
403 static int poke_user(struct task_struct *child, addr_t addr, addr_t data)
408 * Stupid gdb peeks/pokes the access registers in 64 bit with
409 * an alignment of 4. Programmers from hell indeed...
413 if (addr >= (addr_t) &((struct user *) NULL)->regs.acrs &&
414 addr < (addr_t) &((struct user *) NULL)->regs.orig_gpr2)
417 if ((addr & mask) || addr > sizeof(struct user) - __ADDR_MASK)
420 return __poke_user(child, addr, data);
423 long arch_ptrace(struct task_struct *child, long request,
424 unsigned long addr, unsigned long data)
431 /* read the word at location addr in the USER area. */
432 return peek_user(child, addr, data);
435 /* write the word at location addr in the USER area */
436 return poke_user(child, addr, data);
438 case PTRACE_PEEKUSR_AREA:
439 case PTRACE_POKEUSR_AREA:
440 if (copy_from_user(&parea, (void __force __user *) addr,
443 addr = parea.kernel_addr;
444 data = parea.process_addr;
446 while (copied < parea.len) {
447 if (request == PTRACE_PEEKUSR_AREA)
448 ret = peek_user(child, addr, data);
452 (addr_t __force __user *) data))
454 ret = poke_user(child, addr, utmp);
458 addr += sizeof(unsigned long);
459 data += sizeof(unsigned long);
460 copied += sizeof(unsigned long);
463 case PTRACE_GET_LAST_BREAK:
464 put_user(task_thread_info(child)->last_break,
465 (unsigned long __user *) data);
467 case PTRACE_ENABLE_TE:
470 child->thread.per_flags &= ~PER_FLAG_NO_TE;
472 case PTRACE_DISABLE_TE:
475 child->thread.per_flags |= PER_FLAG_NO_TE;
476 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
478 case PTRACE_TE_ABORT_RAND:
479 if (!MACHINE_HAS_TE || (child->thread.per_flags & PER_FLAG_NO_TE))
483 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND;
486 child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
487 child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND_TEND;
490 child->thread.per_flags |= PER_FLAG_TE_ABORT_RAND;
491 child->thread.per_flags &= ~PER_FLAG_TE_ABORT_RAND_TEND;
498 /* Removing high order bit from addr (only for 31 bit). */
499 addr &= PSW_ADDR_INSN;
500 return ptrace_request(child, request, addr, data);
506 * Now the fun part starts... a 31 bit program running in the
507 * 31 bit emulation tracing another program. PTRACE_PEEKTEXT,
508 * PTRACE_PEEKDATA, PTRACE_POKETEXT and PTRACE_POKEDATA are easy
509 * to handle, the difference to the 64 bit versions of the requests
510 * is that the access is done in multiples of 4 byte instead of
511 * 8 bytes (sizeof(unsigned long) on 31/64 bit).
512 * The ugly part are PTRACE_PEEKUSR, PTRACE_PEEKUSR_AREA,
513 * PTRACE_POKEUSR and PTRACE_POKEUSR_AREA. If the traced program
514 * is a 31 bit program too, the content of struct user can be
515 * emulated. A 31 bit program peeking into the struct user of
516 * a 64 bit program is a no-no.
520 * Same as peek_user_per but for a 31 bit program.
522 static inline __u32 __peek_user_per_compat(struct task_struct *child,
525 struct compat_per_struct_kernel *dummy32 = NULL;
527 if (addr == (addr_t) &dummy32->cr9)
528 /* Control bits of the active per set. */
529 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
530 PER_EVENT_IFETCH : child->thread.per_user.control;
531 else if (addr == (addr_t) &dummy32->cr10)
532 /* Start address of the active per set. */
533 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
534 0 : child->thread.per_user.start;
535 else if (addr == (addr_t) &dummy32->cr11)
536 /* End address of the active per set. */
537 return test_thread_flag(TIF_SINGLE_STEP) ?
538 PSW32_ADDR_INSN : child->thread.per_user.end;
539 else if (addr == (addr_t) &dummy32->bits)
540 /* Single-step bit. */
541 return (__u32) test_thread_flag(TIF_SINGLE_STEP) ?
543 else if (addr == (addr_t) &dummy32->starting_addr)
544 /* Start address of the user specified per set. */
545 return (__u32) child->thread.per_user.start;
546 else if (addr == (addr_t) &dummy32->ending_addr)
547 /* End address of the user specified per set. */
548 return (__u32) child->thread.per_user.end;
549 else if (addr == (addr_t) &dummy32->perc_atmid)
550 /* PER code, ATMID and AI of the last PER trap */
551 return (__u32) child->thread.per_event.cause << 16;
552 else if (addr == (addr_t) &dummy32->address)
553 /* Address of the last PER trap */
554 return (__u32) child->thread.per_event.address;
555 else if (addr == (addr_t) &dummy32->access_id)
556 /* Access id of the last PER trap */
557 return (__u32) child->thread.per_event.paid << 24;
562 * Same as peek_user but for a 31 bit program.
564 static u32 __peek_user_compat(struct task_struct *child, addr_t addr)
566 struct compat_user *dummy32 = NULL;
570 if (addr < (addr_t) &dummy32->regs.acrs) {
571 struct pt_regs *regs = task_pt_regs(child);
573 * psw and gprs are stored on the stack
575 if (addr == (addr_t) &dummy32->regs.psw.mask) {
576 /* Fake a 31 bit psw mask. */
577 tmp = (__u32)(regs->psw.mask >> 32);
578 tmp &= PSW32_MASK_USER | PSW32_MASK_RI;
579 tmp |= PSW32_USER_BITS;
580 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
581 /* Fake a 31 bit psw address. */
582 tmp = (__u32) regs->psw.addr |
583 (__u32)(regs->psw.mask & PSW_MASK_BA);
586 tmp = *(__u32 *)((addr_t) ®s->psw + addr*2 + 4);
588 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
590 * access registers are stored in the thread structure
592 offset = addr - (addr_t) &dummy32->regs.acrs;
593 tmp = *(__u32*)((addr_t) &child->thread.acrs + offset);
595 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
597 * orig_gpr2 is stored on the kernel stack
599 tmp = *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4);
601 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
603 * prevent reads of padding hole between
604 * orig_gpr2 and fp_regs on s390.
608 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
610 * floating point regs. are stored in the thread structure
612 offset = addr - (addr_t) &dummy32->regs.fp_regs;
613 tmp = *(__u32 *)((addr_t) &child->thread.fp_regs + offset);
615 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
617 * Handle access to the per_info structure.
619 addr -= (addr_t) &dummy32->regs.per_info;
620 tmp = __peek_user_per_compat(child, addr);
628 static int peek_user_compat(struct task_struct *child,
629 addr_t addr, addr_t data)
633 if (!is_compat_task() || (addr & 3) || addr > sizeof(struct user) - 3)
636 tmp = __peek_user_compat(child, addr);
637 return put_user(tmp, (__u32 __user *) data);
641 * Same as poke_user_per but for a 31 bit program.
643 static inline void __poke_user_per_compat(struct task_struct *child,
644 addr_t addr, __u32 data)
646 struct compat_per_struct_kernel *dummy32 = NULL;
648 if (addr == (addr_t) &dummy32->cr9)
649 /* PER event mask of the user specified per set. */
650 child->thread.per_user.control =
651 data & (PER_EVENT_MASK | PER_CONTROL_MASK);
652 else if (addr == (addr_t) &dummy32->starting_addr)
653 /* Starting address of the user specified per set. */
654 child->thread.per_user.start = data;
655 else if (addr == (addr_t) &dummy32->ending_addr)
656 /* Ending address of the user specified per set. */
657 child->thread.per_user.end = data;
661 * Same as poke_user but for a 31 bit program.
663 static int __poke_user_compat(struct task_struct *child,
664 addr_t addr, addr_t data)
666 struct compat_user *dummy32 = NULL;
667 __u32 tmp = (__u32) data;
670 if (addr < (addr_t) &dummy32->regs.acrs) {
671 struct pt_regs *regs = task_pt_regs(child);
673 * psw, gprs, acrs and orig_gpr2 are stored on the stack
675 if (addr == (addr_t) &dummy32->regs.psw.mask) {
676 __u32 mask = PSW32_MASK_USER;
678 mask |= is_ri_task(child) ? PSW32_MASK_RI : 0;
679 /* Build a 64 bit psw mask from 31 bit mask. */
680 if ((tmp ^ PSW32_USER_BITS) & ~mask)
681 /* Invalid psw mask. */
683 if ((data & PSW32_MASK_ASC) == PSW32_ASC_HOME)
684 /* Invalid address-space-control bits */
686 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
687 (regs->psw.mask & PSW_MASK_BA) |
688 (__u64)(tmp & mask) << 32;
689 } else if (addr == (addr_t) &dummy32->regs.psw.addr) {
690 /* Build a 64 bit psw address from 31 bit address. */
691 regs->psw.addr = (__u64) tmp & PSW32_ADDR_INSN;
692 /* Transfer 31 bit amode bit to psw mask. */
693 regs->psw.mask = (regs->psw.mask & ~PSW_MASK_BA) |
694 (__u64)(tmp & PSW32_ADDR_AMODE);
697 *(__u32*)((addr_t) ®s->psw + addr*2 + 4) = tmp;
699 } else if (addr < (addr_t) (&dummy32->regs.orig_gpr2)) {
701 * access registers are stored in the thread structure
703 offset = addr - (addr_t) &dummy32->regs.acrs;
704 *(__u32*)((addr_t) &child->thread.acrs + offset) = tmp;
706 } else if (addr == (addr_t) (&dummy32->regs.orig_gpr2)) {
708 * orig_gpr2 is stored on the kernel stack
710 *(__u32*)((addr_t) &task_pt_regs(child)->orig_gpr2 + 4) = tmp;
712 } else if (addr < (addr_t) &dummy32->regs.fp_regs) {
714 * prevent writess of padding hole between
715 * orig_gpr2 and fp_regs on s390.
719 } else if (addr < (addr_t) (&dummy32->regs.fp_regs + 1)) {
721 * floating point regs. are stored in the thread structure
723 if (addr == (addr_t) &dummy32->regs.fp_regs.fpc &&
726 offset = addr - (addr_t) &dummy32->regs.fp_regs;
727 *(__u32 *)((addr_t) &child->thread.fp_regs + offset) = tmp;
729 } else if (addr < (addr_t) (&dummy32->regs.per_info + 1)) {
731 * Handle access to the per_info structure.
733 addr -= (addr_t) &dummy32->regs.per_info;
734 __poke_user_per_compat(child, addr, data);
740 static int poke_user_compat(struct task_struct *child,
741 addr_t addr, addr_t data)
743 if (!is_compat_task() || (addr & 3) ||
744 addr > sizeof(struct compat_user) - 3)
747 return __poke_user_compat(child, addr, data);
750 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
751 compat_ulong_t caddr, compat_ulong_t cdata)
753 unsigned long addr = caddr;
754 unsigned long data = cdata;
755 compat_ptrace_area parea;
760 /* read the word at location addr in the USER area. */
761 return peek_user_compat(child, addr, data);
764 /* write the word at location addr in the USER area */
765 return poke_user_compat(child, addr, data);
767 case PTRACE_PEEKUSR_AREA:
768 case PTRACE_POKEUSR_AREA:
769 if (copy_from_user(&parea, (void __force __user *) addr,
772 addr = parea.kernel_addr;
773 data = parea.process_addr;
775 while (copied < parea.len) {
776 if (request == PTRACE_PEEKUSR_AREA)
777 ret = peek_user_compat(child, addr, data);
781 (__u32 __force __user *) data))
783 ret = poke_user_compat(child, addr, utmp);
787 addr += sizeof(unsigned int);
788 data += sizeof(unsigned int);
789 copied += sizeof(unsigned int);
792 case PTRACE_GET_LAST_BREAK:
793 put_user(task_thread_info(child)->last_break,
794 (unsigned int __user *) data);
797 return compat_ptrace_request(child, request, addr, data);
801 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
805 /* Do the secure computing check first. */
806 if (secure_computing(regs->gprs[2])) {
807 /* seccomp failures shouldn't expose any additional code. */
813 * The sysc_tracesys code in entry.S stored the system
814 * call number to gprs[2].
816 if (test_thread_flag(TIF_SYSCALL_TRACE) &&
817 (tracehook_report_syscall_entry(regs) ||
818 regs->gprs[2] >= NR_syscalls)) {
820 * Tracing decided this syscall should not happen or the
821 * debugger stored an invalid system call number. Skip
822 * the system call and the system call restart handling.
824 clear_pt_regs_flag(regs, PIF_SYSCALL);
828 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
829 trace_sys_enter(regs, regs->gprs[2]);
831 audit_syscall_entry(is_compat_task() ?
832 AUDIT_ARCH_S390 : AUDIT_ARCH_S390X,
833 regs->gprs[2], regs->orig_gpr2,
834 regs->gprs[3], regs->gprs[4],
837 return ret ?: regs->gprs[2];
840 asmlinkage void do_syscall_trace_exit(struct pt_regs *regs)
842 audit_syscall_exit(regs);
844 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
845 trace_sys_exit(regs, regs->gprs[2]);
847 if (test_thread_flag(TIF_SYSCALL_TRACE))
848 tracehook_report_syscall_exit(regs, 0);
852 * user_regset definitions.
855 static int s390_regs_get(struct task_struct *target,
856 const struct user_regset *regset,
857 unsigned int pos, unsigned int count,
858 void *kbuf, void __user *ubuf)
860 if (target == current)
861 save_access_regs(target->thread.acrs);
864 unsigned long *k = kbuf;
866 *k++ = __peek_user(target, pos);
871 unsigned long __user *u = ubuf;
873 if (__put_user(__peek_user(target, pos), u++))
882 static int s390_regs_set(struct task_struct *target,
883 const struct user_regset *regset,
884 unsigned int pos, unsigned int count,
885 const void *kbuf, const void __user *ubuf)
889 if (target == current)
890 save_access_regs(target->thread.acrs);
893 const unsigned long *k = kbuf;
894 while (count > 0 && !rc) {
895 rc = __poke_user(target, pos, *k++);
900 const unsigned long __user *u = ubuf;
901 while (count > 0 && !rc) {
903 rc = __get_user(word, u++);
906 rc = __poke_user(target, pos, word);
912 if (rc == 0 && target == current)
913 restore_access_regs(target->thread.acrs);
918 static int s390_fpregs_get(struct task_struct *target,
919 const struct user_regset *regset, unsigned int pos,
920 unsigned int count, void *kbuf, void __user *ubuf)
922 if (target == current) {
923 save_fp_ctl(&target->thread.fp_regs.fpc);
924 save_fp_regs(target->thread.fp_regs.fprs);
927 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
928 &target->thread.fp_regs, 0, -1);
931 static int s390_fpregs_set(struct task_struct *target,
932 const struct user_regset *regset, unsigned int pos,
933 unsigned int count, const void *kbuf,
934 const void __user *ubuf)
938 if (target == current) {
939 save_fp_ctl(&target->thread.fp_regs.fpc);
940 save_fp_regs(target->thread.fp_regs.fprs);
943 /* If setting FPC, must validate it first. */
944 if (count > 0 && pos < offsetof(s390_fp_regs, fprs)) {
945 u32 ufpc[2] = { target->thread.fp_regs.fpc, 0 };
946 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ufpc,
947 0, offsetof(s390_fp_regs, fprs));
950 if (ufpc[1] != 0 || test_fp_ctl(ufpc[0]))
952 target->thread.fp_regs.fpc = ufpc[0];
955 if (rc == 0 && count > 0)
956 rc = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
957 target->thread.fp_regs.fprs,
958 offsetof(s390_fp_regs, fprs), -1);
960 if (rc == 0 && target == current) {
961 restore_fp_ctl(&target->thread.fp_regs.fpc);
962 restore_fp_regs(target->thread.fp_regs.fprs);
970 static int s390_last_break_get(struct task_struct *target,
971 const struct user_regset *regset,
972 unsigned int pos, unsigned int count,
973 void *kbuf, void __user *ubuf)
977 unsigned long *k = kbuf;
978 *k = task_thread_info(target)->last_break;
980 unsigned long __user *u = ubuf;
981 if (__put_user(task_thread_info(target)->last_break, u))
988 static int s390_last_break_set(struct task_struct *target,
989 const struct user_regset *regset,
990 unsigned int pos, unsigned int count,
991 const void *kbuf, const void __user *ubuf)
996 static int s390_tdb_get(struct task_struct *target,
997 const struct user_regset *regset,
998 unsigned int pos, unsigned int count,
999 void *kbuf, void __user *ubuf)
1001 struct pt_regs *regs = task_pt_regs(target);
1002 unsigned char *data;
1004 if (!(regs->int_code & 0x200))
1006 data = target->thread.trap_tdb;
1007 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, data, 0, 256);
1010 static int s390_tdb_set(struct task_struct *target,
1011 const struct user_regset *regset,
1012 unsigned int pos, unsigned int count,
1013 const void *kbuf, const void __user *ubuf)
1020 static int s390_system_call_get(struct task_struct *target,
1021 const struct user_regset *regset,
1022 unsigned int pos, unsigned int count,
1023 void *kbuf, void __user *ubuf)
1025 unsigned int *data = &task_thread_info(target)->system_call;
1026 return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
1027 data, 0, sizeof(unsigned int));
1030 static int s390_system_call_set(struct task_struct *target,
1031 const struct user_regset *regset,
1032 unsigned int pos, unsigned int count,
1033 const void *kbuf, const void __user *ubuf)
1035 unsigned int *data = &task_thread_info(target)->system_call;
1036 return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
1037 data, 0, sizeof(unsigned int));
1040 static const struct user_regset s390_regsets[] = {
1041 [REGSET_GENERAL] = {
1042 .core_note_type = NT_PRSTATUS,
1043 .n = sizeof(s390_regs) / sizeof(long),
1044 .size = sizeof(long),
1045 .align = sizeof(long),
1046 .get = s390_regs_get,
1047 .set = s390_regs_set,
1050 .core_note_type = NT_PRFPREG,
1051 .n = sizeof(s390_fp_regs) / sizeof(long),
1052 .size = sizeof(long),
1053 .align = sizeof(long),
1054 .get = s390_fpregs_get,
1055 .set = s390_fpregs_set,
1058 [REGSET_LAST_BREAK] = {
1059 .core_note_type = NT_S390_LAST_BREAK,
1061 .size = sizeof(long),
1062 .align = sizeof(long),
1063 .get = s390_last_break_get,
1064 .set = s390_last_break_set,
1067 .core_note_type = NT_S390_TDB,
1071 .get = s390_tdb_get,
1072 .set = s390_tdb_set,
1075 [REGSET_SYSTEM_CALL] = {
1076 .core_note_type = NT_S390_SYSTEM_CALL,
1078 .size = sizeof(unsigned int),
1079 .align = sizeof(unsigned int),
1080 .get = s390_system_call_get,
1081 .set = s390_system_call_set,
1085 static const struct user_regset_view user_s390_view = {
1086 .name = UTS_MACHINE,
1087 .e_machine = EM_S390,
1088 .regsets = s390_regsets,
1089 .n = ARRAY_SIZE(s390_regsets)
1092 #ifdef CONFIG_COMPAT
1093 static int s390_compat_regs_get(struct task_struct *target,
1094 const struct user_regset *regset,
1095 unsigned int pos, unsigned int count,
1096 void *kbuf, void __user *ubuf)
1098 if (target == current)
1099 save_access_regs(target->thread.acrs);
1102 compat_ulong_t *k = kbuf;
1104 *k++ = __peek_user_compat(target, pos);
1105 count -= sizeof(*k);
1109 compat_ulong_t __user *u = ubuf;
1111 if (__put_user(__peek_user_compat(target, pos), u++))
1113 count -= sizeof(*u);
1120 static int s390_compat_regs_set(struct task_struct *target,
1121 const struct user_regset *regset,
1122 unsigned int pos, unsigned int count,
1123 const void *kbuf, const void __user *ubuf)
1127 if (target == current)
1128 save_access_regs(target->thread.acrs);
1131 const compat_ulong_t *k = kbuf;
1132 while (count > 0 && !rc) {
1133 rc = __poke_user_compat(target, pos, *k++);
1134 count -= sizeof(*k);
1138 const compat_ulong_t __user *u = ubuf;
1139 while (count > 0 && !rc) {
1140 compat_ulong_t word;
1141 rc = __get_user(word, u++);
1144 rc = __poke_user_compat(target, pos, word);
1145 count -= sizeof(*u);
1150 if (rc == 0 && target == current)
1151 restore_access_regs(target->thread.acrs);
1156 static int s390_compat_regs_high_get(struct task_struct *target,
1157 const struct user_regset *regset,
1158 unsigned int pos, unsigned int count,
1159 void *kbuf, void __user *ubuf)
1161 compat_ulong_t *gprs_high;
1163 gprs_high = (compat_ulong_t *)
1164 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1166 compat_ulong_t *k = kbuf;
1170 count -= sizeof(*k);
1173 compat_ulong_t __user *u = ubuf;
1175 if (__put_user(*gprs_high, u++))
1178 count -= sizeof(*u);
1184 static int s390_compat_regs_high_set(struct task_struct *target,
1185 const struct user_regset *regset,
1186 unsigned int pos, unsigned int count,
1187 const void *kbuf, const void __user *ubuf)
1189 compat_ulong_t *gprs_high;
1192 gprs_high = (compat_ulong_t *)
1193 &task_pt_regs(target)->gprs[pos / sizeof(compat_ulong_t)];
1195 const compat_ulong_t *k = kbuf;
1199 count -= sizeof(*k);
1202 const compat_ulong_t __user *u = ubuf;
1203 while (count > 0 && !rc) {
1205 rc = __get_user(word, u++);
1210 count -= sizeof(*u);
1217 static int s390_compat_last_break_get(struct task_struct *target,
1218 const struct user_regset *regset,
1219 unsigned int pos, unsigned int count,
1220 void *kbuf, void __user *ubuf)
1222 compat_ulong_t last_break;
1225 last_break = task_thread_info(target)->last_break;
1227 unsigned long *k = kbuf;
1230 unsigned long __user *u = ubuf;
1231 if (__put_user(last_break, u))
1238 static int s390_compat_last_break_set(struct task_struct *target,
1239 const struct user_regset *regset,
1240 unsigned int pos, unsigned int count,
1241 const void *kbuf, const void __user *ubuf)
1246 static const struct user_regset s390_compat_regsets[] = {
1247 [REGSET_GENERAL] = {
1248 .core_note_type = NT_PRSTATUS,
1249 .n = sizeof(s390_compat_regs) / sizeof(compat_long_t),
1250 .size = sizeof(compat_long_t),
1251 .align = sizeof(compat_long_t),
1252 .get = s390_compat_regs_get,
1253 .set = s390_compat_regs_set,
1256 .core_note_type = NT_PRFPREG,
1257 .n = sizeof(s390_fp_regs) / sizeof(compat_long_t),
1258 .size = sizeof(compat_long_t),
1259 .align = sizeof(compat_long_t),
1260 .get = s390_fpregs_get,
1261 .set = s390_fpregs_set,
1263 [REGSET_LAST_BREAK] = {
1264 .core_note_type = NT_S390_LAST_BREAK,
1266 .size = sizeof(long),
1267 .align = sizeof(long),
1268 .get = s390_compat_last_break_get,
1269 .set = s390_compat_last_break_set,
1272 .core_note_type = NT_S390_TDB,
1276 .get = s390_tdb_get,
1277 .set = s390_tdb_set,
1279 [REGSET_SYSTEM_CALL] = {
1280 .core_note_type = NT_S390_SYSTEM_CALL,
1282 .size = sizeof(compat_uint_t),
1283 .align = sizeof(compat_uint_t),
1284 .get = s390_system_call_get,
1285 .set = s390_system_call_set,
1287 [REGSET_GENERAL_EXTENDED] = {
1288 .core_note_type = NT_S390_HIGH_GPRS,
1289 .n = sizeof(s390_compat_regs_high) / sizeof(compat_long_t),
1290 .size = sizeof(compat_long_t),
1291 .align = sizeof(compat_long_t),
1292 .get = s390_compat_regs_high_get,
1293 .set = s390_compat_regs_high_set,
1297 static const struct user_regset_view user_s390_compat_view = {
1299 .e_machine = EM_S390,
1300 .regsets = s390_compat_regsets,
1301 .n = ARRAY_SIZE(s390_compat_regsets)
1305 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
1307 #ifdef CONFIG_COMPAT
1308 if (test_tsk_thread_flag(task, TIF_31BIT))
1309 return &user_s390_compat_view;
1311 return &user_s390_view;
1314 static const char *gpr_names[NUM_GPRS] = {
1315 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1316 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
1319 unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset)
1321 if (offset >= NUM_GPRS)
1323 return regs->gprs[offset];
1326 int regs_query_register_offset(const char *name)
1328 unsigned long offset;
1330 if (!name || *name != 'r')
1332 if (kstrtoul(name + 1, 10, &offset))
1334 if (offset >= NUM_GPRS)
1339 const char *regs_query_register_name(unsigned int offset)
1341 if (offset >= NUM_GPRS)
1343 return gpr_names[offset];
1346 static int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
1348 unsigned long ksp = kernel_stack_pointer(regs);
1350 return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1));
1354 * regs_get_kernel_stack_nth() - get Nth entry of the stack
1355 * @regs:pt_regs which contains kernel stack pointer.
1356 * @n:stack entry number.
1358 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
1359 * is specifined by @regs. If the @n th entry is NOT in the kernel stack,
1362 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
1366 addr = kernel_stack_pointer(regs) + n * sizeof(long);
1367 if (!regs_within_kernel_stack(regs, addr))
1369 return *(unsigned long *)addr;