2 * Kernel support for the ptrace() and syscall tracing interfaces.
4 * Copyright (C) 1999-2005 Hewlett-Packard Co
5 * David Mosberger-Tang <davidm@hpl.hp.com>
7 * Derived from the x86 and Alpha versions.
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/ptrace.h>
16 #include <linux/smp_lock.h>
17 #include <linux/user.h>
18 #include <linux/security.h>
19 #include <linux/audit.h>
20 #include <linux/signal.h>
22 #include <asm/pgtable.h>
23 #include <asm/processor.h>
24 #include <asm/ptrace_offsets.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/unwind.h>
30 #include <asm/perfmon.h>
36 * Bits in the PSR that we allow ptrace() to change:
37 * be, up, ac, mfl, mfh (the user mask; five bits total)
38 * db (debug breakpoint fault; one bit)
39 * id (instruction debug fault disable; one bit)
40 * dd (data debug fault disable; one bit)
41 * ri (restart instruction; two bits)
42 * is (instruction set; one bit)
44 #define IPSR_MASK (IA64_PSR_UM | IA64_PSR_DB | IA64_PSR_IS \
45 | IA64_PSR_ID | IA64_PSR_DD | IA64_PSR_RI)
47 #define MASK(nbits) ((1UL << (nbits)) - 1) /* mask with NBITS bits set */
48 #define PFM_MASK MASK(38)
50 #define PTRACE_DEBUG 0
53 # define dprintk(format...) printk(format)
56 # define dprintk(format...)
59 /* Return TRUE if PT was created due to kernel-entry via a system-call. */
62 in_syscall (struct pt_regs *pt)
64 return (long) pt->cr_ifs >= 0;
68 * Collect the NaT bits for r1-r31 from scratch_unat and return a NaT
69 * bitset where bit i is set iff the NaT bit of register i is set.
72 ia64_get_scratch_nat_bits (struct pt_regs *pt, unsigned long scratch_unat)
74 # define GET_BITS(first, last, unat) \
76 unsigned long bit = ia64_unat_pos(&pt->r##first); \
77 unsigned long nbits = (last - first + 1); \
78 unsigned long mask = MASK(nbits) << first; \
81 dist = 64 + bit - first; \
84 ia64_rotr(unat, dist) & mask; \
89 * Registers that are stored consecutively in struct pt_regs
90 * can be handled in parallel. If the register order in
91 * struct_pt_regs changes, this code MUST be updated.
93 val = GET_BITS( 1, 1, scratch_unat);
94 val |= GET_BITS( 2, 3, scratch_unat);
95 val |= GET_BITS(12, 13, scratch_unat);
96 val |= GET_BITS(14, 14, scratch_unat);
97 val |= GET_BITS(15, 15, scratch_unat);
98 val |= GET_BITS( 8, 11, scratch_unat);
99 val |= GET_BITS(16, 31, scratch_unat);
106 * Set the NaT bits for the scratch registers according to NAT and
107 * return the resulting unat (assuming the scratch registers are
111 ia64_put_scratch_nat_bits (struct pt_regs *pt, unsigned long nat)
113 # define PUT_BITS(first, last, nat) \
115 unsigned long bit = ia64_unat_pos(&pt->r##first); \
116 unsigned long nbits = (last - first + 1); \
117 unsigned long mask = MASK(nbits) << first; \
120 dist = 64 + bit - first; \
122 dist = bit - first; \
123 ia64_rotl(nat & mask, dist); \
125 unsigned long scratch_unat;
128 * Registers that are stored consecutively in struct pt_regs
129 * can be handled in parallel. If the register order in
130 * struct_pt_regs changes, this code MUST be updated.
132 scratch_unat = PUT_BITS( 1, 1, nat);
133 scratch_unat |= PUT_BITS( 2, 3, nat);
134 scratch_unat |= PUT_BITS(12, 13, nat);
135 scratch_unat |= PUT_BITS(14, 14, nat);
136 scratch_unat |= PUT_BITS(15, 15, nat);
137 scratch_unat |= PUT_BITS( 8, 11, nat);
138 scratch_unat |= PUT_BITS(16, 31, nat);
145 #define IA64_MLX_TEMPLATE 0x2
146 #define IA64_MOVL_OPCODE 6
149 ia64_increment_ip (struct pt_regs *regs)
151 unsigned long w0, ri = ia64_psr(regs)->ri + 1;
156 } else if (ri == 2) {
157 get_user(w0, (char __user *) regs->cr_iip + 0);
158 if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
160 * rfi'ing to slot 2 of an MLX bundle causes
161 * an illegal operation fault. We don't want
168 ia64_psr(regs)->ri = ri;
172 ia64_decrement_ip (struct pt_regs *regs)
174 unsigned long w0, ri = ia64_psr(regs)->ri - 1;
176 if (ia64_psr(regs)->ri == 0) {
179 get_user(w0, (char __user *) regs->cr_iip + 0);
180 if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
182 * rfi'ing to slot 2 of an MLX bundle causes
183 * an illegal operation fault. We don't want
189 ia64_psr(regs)->ri = ri;
193 * This routine is used to read an rnat bits that are stored on the
194 * kernel backing store. Since, in general, the alignment of the user
195 * and kernel are different, this is not completely trivial. In
196 * essence, we need to construct the user RNAT based on up to two
197 * kernel RNAT values and/or the RNAT value saved in the child's
202 * +--------+ <-- lowest address
209 * | slot01 | > child_regs->ar_rnat
211 * | slot02 | / kernel rbs
212 * +--------+ +--------+
213 * <- child_regs->ar_bspstore | slot61 | <-- krbs
214 * +- - - - + +--------+
216 * +- - - - + +--------+
218 * +- - - - + +--------+
220 * +- - - - + +--------+
225 * | slot01 | > child_stack->ar_rnat
229 * <--- child_stack->ar_bspstore
231 * The way to think of this code is as follows: bit 0 in the user rnat
232 * corresponds to some bit N (0 <= N <= 62) in one of the kernel rnat
233 * value. The kernel rnat value holding this bit is stored in
234 * variable rnat0. rnat1 is loaded with the kernel rnat value that
235 * form the upper bits of the user rnat value.
239 * o when reading the rnat "below" the first rnat slot on the kernel
240 * backing store, rnat0/rnat1 are set to 0 and the low order bits are
241 * merged in from pt->ar_rnat.
243 * o when reading the rnat "above" the last rnat slot on the kernel
244 * backing store, rnat0/rnat1 gets its value from sw->ar_rnat.
247 get_rnat (struct task_struct *task, struct switch_stack *sw,
248 unsigned long *krbs, unsigned long *urnat_addr,
249 unsigned long *urbs_end)
251 unsigned long rnat0 = 0, rnat1 = 0, urnat = 0, *slot0_kaddr;
252 unsigned long umask = 0, mask, m;
253 unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
254 long num_regs, nbits;
257 pt = ia64_task_regs(task);
258 kbsp = (unsigned long *) sw->ar_bspstore;
259 ubspstore = (unsigned long *) pt->ar_bspstore;
261 if (urbs_end < urnat_addr)
262 nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_end);
267 * First, figure out which bit number slot 0 in user-land maps
268 * to in the kernel rnat. Do this by figuring out how many
269 * register slots we're beyond the user's backingstore and
270 * then computing the equivalent address in kernel space.
272 num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
273 slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
274 shift = ia64_rse_slot_num(slot0_kaddr);
275 rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
276 rnat0_kaddr = rnat1_kaddr - 64;
278 if (ubspstore + 63 > urnat_addr) {
279 /* some bits need to be merged in from pt->ar_rnat */
280 umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;
281 urnat = (pt->ar_rnat & umask);
288 if (rnat0_kaddr >= kbsp)
290 else if (rnat0_kaddr > krbs)
291 rnat0 = *rnat0_kaddr;
292 urnat |= (rnat0 & m) >> shift;
294 m = mask >> (63 - shift);
295 if (rnat1_kaddr >= kbsp)
297 else if (rnat1_kaddr > krbs)
298 rnat1 = *rnat1_kaddr;
299 urnat |= (rnat1 & m) << (63 - shift);
304 * The reverse of get_rnat.
307 put_rnat (struct task_struct *task, struct switch_stack *sw,
308 unsigned long *krbs, unsigned long *urnat_addr, unsigned long urnat,
309 unsigned long *urbs_end)
311 unsigned long rnat0 = 0, rnat1 = 0, *slot0_kaddr, umask = 0, mask, m;
312 unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
313 long num_regs, nbits;
315 unsigned long cfm, *urbs_kargs;
317 pt = ia64_task_regs(task);
318 kbsp = (unsigned long *) sw->ar_bspstore;
319 ubspstore = (unsigned long *) pt->ar_bspstore;
321 urbs_kargs = urbs_end;
322 if (in_syscall(pt)) {
324 * If entered via syscall, don't allow user to set rnat bits
328 urbs_kargs = ia64_rse_skip_regs(urbs_end, -(cfm & 0x7f));
331 if (urbs_kargs >= urnat_addr)
334 if ((urnat_addr - 63) >= urbs_kargs)
336 nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_kargs);
341 * First, figure out which bit number slot 0 in user-land maps
342 * to in the kernel rnat. Do this by figuring out how many
343 * register slots we're beyond the user's backingstore and
344 * then computing the equivalent address in kernel space.
346 num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
347 slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
348 shift = ia64_rse_slot_num(slot0_kaddr);
349 rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
350 rnat0_kaddr = rnat1_kaddr - 64;
352 if (ubspstore + 63 > urnat_addr) {
353 /* some bits need to be place in pt->ar_rnat: */
354 umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;
355 pt->ar_rnat = (pt->ar_rnat & ~umask) | (urnat & umask);
361 * Note: Section 11.1 of the EAS guarantees that bit 63 of an
362 * rnat slot is ignored. so we don't have to clear it here.
364 rnat0 = (urnat << shift);
366 if (rnat0_kaddr >= kbsp)
367 sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat0 & m);
368 else if (rnat0_kaddr > krbs)
369 *rnat0_kaddr = ((*rnat0_kaddr & ~m) | (rnat0 & m));
371 rnat1 = (urnat >> (63 - shift));
372 m = mask >> (63 - shift);
373 if (rnat1_kaddr >= kbsp)
374 sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat1 & m);
375 else if (rnat1_kaddr > krbs)
376 *rnat1_kaddr = ((*rnat1_kaddr & ~m) | (rnat1 & m));
380 on_kernel_rbs (unsigned long addr, unsigned long bspstore,
381 unsigned long urbs_end)
383 unsigned long *rnat_addr = ia64_rse_rnat_addr((unsigned long *)
385 return (addr >= bspstore && addr <= (unsigned long) rnat_addr);
389 * Read a word from the user-level backing store of task CHILD. ADDR
390 * is the user-level address to read the word from, VAL a pointer to
391 * the return value, and USER_BSP gives the end of the user-level
392 * backing store (i.e., it's the address that would be in ar.bsp after
393 * the user executed a "cover" instruction).
395 * This routine takes care of accessing the kernel register backing
396 * store for those registers that got spilled there. It also takes
397 * care of calculating the appropriate RNaT collection words.
400 ia64_peek (struct task_struct *child, struct switch_stack *child_stack,
401 unsigned long user_rbs_end, unsigned long addr, long *val)
403 unsigned long *bspstore, *krbs, regnum, *laddr, *urbs_end, *rnat_addr;
404 struct pt_regs *child_regs;
408 urbs_end = (long *) user_rbs_end;
409 laddr = (unsigned long *) addr;
410 child_regs = ia64_task_regs(child);
411 bspstore = (unsigned long *) child_regs->ar_bspstore;
412 krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
413 if (on_kernel_rbs(addr, (unsigned long) bspstore,
414 (unsigned long) urbs_end))
417 * Attempt to read the RBS in an area that's actually
418 * on the kernel RBS => read the corresponding bits in
421 rnat_addr = ia64_rse_rnat_addr(laddr);
422 ret = get_rnat(child, child_stack, krbs, rnat_addr, urbs_end);
424 if (laddr == rnat_addr) {
425 /* return NaT collection word itself */
430 if (((1UL << ia64_rse_slot_num(laddr)) & ret) != 0) {
432 * It is implementation dependent whether the
433 * data portion of a NaT value gets saved on a
434 * st8.spill or RSE spill (e.g., see EAS 2.6,
435 * 4.4.4.6 Register Spill and Fill). To get
436 * consistent behavior across all possible
437 * IA-64 implementations, we return zero in
444 if (laddr < urbs_end) {
446 * The desired word is on the kernel RBS and
449 regnum = ia64_rse_num_regs(bspstore, laddr);
450 *val = *ia64_rse_skip_regs(krbs, regnum);
454 copied = access_process_vm(child, addr, &ret, sizeof(ret), 0);
455 if (copied != sizeof(ret))
462 ia64_poke (struct task_struct *child, struct switch_stack *child_stack,
463 unsigned long user_rbs_end, unsigned long addr, long val)
465 unsigned long *bspstore, *krbs, regnum, *laddr;
466 unsigned long *urbs_end = (long *) user_rbs_end;
467 struct pt_regs *child_regs;
469 laddr = (unsigned long *) addr;
470 child_regs = ia64_task_regs(child);
471 bspstore = (unsigned long *) child_regs->ar_bspstore;
472 krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
473 if (on_kernel_rbs(addr, (unsigned long) bspstore,
474 (unsigned long) urbs_end))
477 * Attempt to write the RBS in an area that's actually
478 * on the kernel RBS => write the corresponding bits
481 if (ia64_rse_is_rnat_slot(laddr))
482 put_rnat(child, child_stack, krbs, laddr, val,
485 if (laddr < urbs_end) {
486 regnum = ia64_rse_num_regs(bspstore, laddr);
487 *ia64_rse_skip_regs(krbs, regnum) = val;
490 } else if (access_process_vm(child, addr, &val, sizeof(val), 1)
497 * Calculate the address of the end of the user-level register backing
498 * store. This is the address that would have been stored in ar.bsp
499 * if the user had executed a "cover" instruction right before
500 * entering the kernel. If CFMP is not NULL, it is used to return the
501 * "current frame mask" that was active at the time the kernel was
505 ia64_get_user_rbs_end (struct task_struct *child, struct pt_regs *pt,
508 unsigned long *krbs, *bspstore, cfm = pt->cr_ifs;
511 krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
512 bspstore = (unsigned long *) pt->ar_bspstore;
513 ndirty = ia64_rse_num_regs(krbs, krbs + (pt->loadrs >> 19));
516 ndirty += (cfm & 0x7f);
518 cfm &= ~(1UL << 63); /* clear valid bit */
522 return (unsigned long) ia64_rse_skip_regs(bspstore, ndirty);
526 * Synchronize (i.e, write) the RSE backing store living in kernel
527 * space to the VM of the CHILD task. SW and PT are the pointers to
528 * the switch_stack and pt_regs structures, respectively.
529 * USER_RBS_END is the user-level address at which the backing store
533 ia64_sync_user_rbs (struct task_struct *child, struct switch_stack *sw,
534 unsigned long user_rbs_start, unsigned long user_rbs_end)
536 unsigned long addr, val;
539 /* now copy word for word from kernel rbs to user rbs: */
540 for (addr = user_rbs_start; addr < user_rbs_end; addr += 8) {
541 ret = ia64_peek(child, sw, user_rbs_end, addr, &val);
544 if (access_process_vm(child, addr, &val, sizeof(val), 1)
552 thread_matches (struct task_struct *thread, unsigned long addr)
554 unsigned long thread_rbs_end;
555 struct pt_regs *thread_regs;
557 if (ptrace_check_attach(thread, 0) < 0)
559 * If the thread is not in an attachable state, we'll
560 * ignore it. The net effect is that if ADDR happens
561 * to overlap with the portion of the thread's
562 * register backing store that is currently residing
563 * on the thread's kernel stack, then ptrace() may end
564 * up accessing a stale value. But if the thread
565 * isn't stopped, that's a problem anyhow, so we're
566 * doing as well as we can...
570 thread_regs = ia64_task_regs(thread);
571 thread_rbs_end = ia64_get_user_rbs_end(thread, thread_regs, NULL);
572 if (!on_kernel_rbs(addr, thread_regs->ar_bspstore, thread_rbs_end))
575 return 1; /* looks like we've got a winner */
579 * GDB apparently wants to be able to read the register-backing store
580 * of any thread when attached to a given process. If we are peeking
581 * or poking an address that happens to reside in the kernel-backing
582 * store of another thread, we need to attach to that thread, because
583 * otherwise we end up accessing stale data.
585 * task_list_lock must be read-locked before calling this routine!
587 static struct task_struct *
588 find_thread_for_addr (struct task_struct *child, unsigned long addr)
590 struct task_struct *g, *p;
591 struct mm_struct *mm;
594 if (!(mm = get_task_mm(child)))
597 /* -1 because of our get_task_mm(): */
598 mm_users = atomic_read(&mm->mm_users) - 1;
600 goto out; /* not multi-threaded */
603 * First, traverse the child's thread-list. Good for scalability with
608 if (thread_matches(p, addr)) {
614 } while ((p = next_thread(p)) != child);
616 do_each_thread(g, p) {
620 if (thread_matches(p, addr)) {
624 } while_each_thread(g, p);
631 * Write f32-f127 back to task->thread.fph if it has been modified.
634 ia64_flush_fph (struct task_struct *task)
636 struct ia64_psr *psr = ia64_psr(ia64_task_regs(task));
639 * Prevent migrating this task while
640 * we're fiddling with the FPU state
643 if (ia64_is_local_fpu_owner(task) && psr->mfh) {
645 task->thread.flags |= IA64_THREAD_FPH_VALID;
646 ia64_save_fpu(&task->thread.fph[0]);
652 * Sync the fph state of the task so that it can be manipulated
653 * through thread.fph. If necessary, f32-f127 are written back to
654 * thread.fph or, if the fph state hasn't been used before, thread.fph
655 * is cleared to zeroes. Also, access to f32-f127 is disabled to
656 * ensure that the task picks up the state from thread.fph when it
660 ia64_sync_fph (struct task_struct *task)
662 struct ia64_psr *psr = ia64_psr(ia64_task_regs(task));
664 ia64_flush_fph(task);
665 if (!(task->thread.flags & IA64_THREAD_FPH_VALID)) {
666 task->thread.flags |= IA64_THREAD_FPH_VALID;
667 memset(&task->thread.fph, 0, sizeof(task->thread.fph));
674 access_fr (struct unw_frame_info *info, int regnum, int hi,
675 unsigned long *data, int write_access)
677 struct ia64_fpreg fpval;
680 ret = unw_get_fr(info, regnum, &fpval);
685 fpval.u.bits[hi] = *data;
686 ret = unw_set_fr(info, regnum, fpval);
688 *data = fpval.u.bits[hi];
693 * Change the machine-state of CHILD such that it will return via the normal
694 * kernel exit-path, rather than the syscall-exit path.
697 convert_to_non_syscall (struct task_struct *child, struct pt_regs *pt,
700 struct unw_frame_info info, prev_info;
701 unsigned long ip, sp, pr;
703 unw_init_from_blocked_task(&info, child);
706 if (unw_unwind(&info) < 0)
709 unw_get_sp(&info, &sp);
710 if ((long)((unsigned long)child + IA64_STK_OFFSET - sp)
711 < IA64_PT_REGS_SIZE) {
712 dprintk("ptrace.%s: ran off the top of the kernel "
713 "stack\n", __FUNCTION__);
716 if (unw_get_pr (&prev_info, &pr) < 0) {
717 unw_get_rp(&prev_info, &ip);
718 dprintk("ptrace.%s: failed to read "
719 "predicate register (ip=0x%lx)\n",
723 if (unw_is_intr_frame(&info)
724 && (pr & (1UL << PRED_USER_STACK)))
728 unw_get_pr(&prev_info, &pr);
729 pr &= ~(1UL << PRED_SYSCALL);
730 pr |= (1UL << PRED_NON_SYSCALL);
731 unw_set_pr(&prev_info, pr);
733 pt->cr_ifs = (1UL << 63) | cfm;
737 access_nat_bits (struct task_struct *child, struct pt_regs *pt,
738 struct unw_frame_info *info,
739 unsigned long *data, int write_access)
741 unsigned long regnum, nat_bits, scratch_unat, dummy = 0;
746 scratch_unat = ia64_put_scratch_nat_bits(pt, nat_bits);
747 if (unw_set_ar(info, UNW_AR_UNAT, scratch_unat) < 0) {
748 dprintk("ptrace: failed to set ar.unat\n");
751 for (regnum = 4; regnum <= 7; ++regnum) {
752 unw_get_gr(info, regnum, &dummy, &nat);
753 unw_set_gr(info, regnum, dummy,
754 (nat_bits >> regnum) & 1);
757 if (unw_get_ar(info, UNW_AR_UNAT, &scratch_unat) < 0) {
758 dprintk("ptrace: failed to read ar.unat\n");
761 nat_bits = ia64_get_scratch_nat_bits(pt, scratch_unat);
762 for (regnum = 4; regnum <= 7; ++regnum) {
763 unw_get_gr(info, regnum, &dummy, &nat);
764 nat_bits |= (nat != 0) << regnum;
772 access_uarea (struct task_struct *child, unsigned long addr,
773 unsigned long *data, int write_access)
775 unsigned long *ptr, regnum, urbs_end, rnat_addr, cfm;
776 struct switch_stack *sw;
778 # define pt_reg_addr(pt, reg) ((void *) \
779 ((unsigned long) (pt) \
780 + offsetof(struct pt_regs, reg)))
783 pt = ia64_task_regs(child);
784 sw = (struct switch_stack *) (child->thread.ksp + 16);
786 if ((addr & 0x7) != 0) {
787 dprintk("ptrace: unaligned register address 0x%lx\n", addr);
791 if (addr < PT_F127 + 16) {
794 ia64_sync_fph(child);
796 ia64_flush_fph(child);
797 ptr = (unsigned long *)
798 ((unsigned long) &child->thread.fph + addr);
799 } else if ((addr >= PT_F10) && (addr < PT_F11 + 16)) {
800 /* scratch registers untouched by kernel (saved in pt_regs) */
801 ptr = pt_reg_addr(pt, f10) + (addr - PT_F10);
802 } else if (addr >= PT_F12 && addr < PT_F15 + 16) {
804 * Scratch registers untouched by kernel (saved in
807 ptr = (unsigned long *) ((long) sw
808 + (addr - PT_NAT_BITS - 32));
809 } else if (addr < PT_AR_LC + 8) {
810 /* preserved state: */
811 struct unw_frame_info info;
815 unw_init_from_blocked_task(&info, child);
816 if (unw_unwind_to_user(&info) < 0)
821 return access_nat_bits(child, pt, &info,
824 case PT_R4: case PT_R5: case PT_R6: case PT_R7:
826 /* read NaT bit first: */
829 ret = unw_get_gr(&info, (addr - PT_R4)/8 + 4,
834 return unw_access_gr(&info, (addr - PT_R4)/8 + 4, data,
837 case PT_B1: case PT_B2: case PT_B3:
838 case PT_B4: case PT_B5:
839 return unw_access_br(&info, (addr - PT_B1)/8 + 1, data,
843 return unw_access_ar(&info, UNW_AR_EC, data,
847 return unw_access_ar(&info, UNW_AR_LC, data,
851 if (addr >= PT_F2 && addr < PT_F5 + 16)
852 return access_fr(&info, (addr - PT_F2)/16 + 2,
853 (addr & 8) != 0, data,
855 else if (addr >= PT_F16 && addr < PT_F31 + 16)
856 return access_fr(&info,
857 (addr - PT_F16)/16 + 16,
861 dprintk("ptrace: rejecting access to register "
862 "address 0x%lx\n", addr);
866 } else if (addr < PT_F9+16) {
871 * By convention, we use PT_AR_BSP to refer to
872 * the end of the user-level backing store.
873 * Use ia64_rse_skip_regs(PT_AR_BSP, -CFM.sof)
874 * to get the real value of ar.bsp at the time
875 * the kernel was entered.
877 * Furthermore, when changing the contents of
878 * PT_AR_BSP (or PT_CFM) we MUST copy any
879 * users-level stacked registers that are
880 * stored on the kernel stack back to
881 * user-space because otherwise, we might end
882 * up clobbering kernel stacked registers.
883 * Also, if this happens while the task is
884 * blocked in a system call, which convert the
885 * state such that the non-system-call exit
886 * path is used. This ensures that the proper
887 * state will be picked up when resuming
888 * execution. However, it *also* means that
889 * once we write PT_AR_BSP/PT_CFM, it won't be
890 * possible to modify the syscall arguments of
891 * the pending system call any longer. This
892 * shouldn't be an issue because modifying
893 * PT_AR_BSP/PT_CFM generally implies that
894 * we're either abandoning the pending system
895 * call or that we defer it's re-execution
896 * (e.g., due to GDB doing an inferior
899 urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
901 if (*data != urbs_end) {
902 if (ia64_sync_user_rbs(child, sw,
907 convert_to_non_syscall(child,
911 * Simulate user-level write
915 pt->ar_bspstore = *data;
922 urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
924 if (((cfm ^ *data) & PFM_MASK) != 0) {
925 if (ia64_sync_user_rbs(child, sw,
930 convert_to_non_syscall(child,
933 pt->cr_ifs = ((pt->cr_ifs & ~PFM_MASK)
934 | (*data & PFM_MASK));
942 pt->cr_ipsr = ((*data & IPSR_MASK)
943 | (pt->cr_ipsr & ~IPSR_MASK));
945 *data = (pt->cr_ipsr & IPSR_MASK);
950 pt->ar_rsc = *data | (3 << 2); /* force PL3 */
956 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
957 rnat_addr = (long) ia64_rse_rnat_addr((long *)
960 return ia64_poke(child, sw, urbs_end,
963 return ia64_peek(child, sw, urbs_end,
967 ptr = pt_reg_addr(pt, r1);
969 case PT_R2: case PT_R3:
970 ptr = pt_reg_addr(pt, r2) + (addr - PT_R2);
972 case PT_R8: case PT_R9: case PT_R10: case PT_R11:
973 ptr = pt_reg_addr(pt, r8) + (addr - PT_R8);
975 case PT_R12: case PT_R13:
976 ptr = pt_reg_addr(pt, r12) + (addr - PT_R12);
979 ptr = pt_reg_addr(pt, r14);
982 ptr = pt_reg_addr(pt, r15);
984 case PT_R16: case PT_R17: case PT_R18: case PT_R19:
985 case PT_R20: case PT_R21: case PT_R22: case PT_R23:
986 case PT_R24: case PT_R25: case PT_R26: case PT_R27:
987 case PT_R28: case PT_R29: case PT_R30: case PT_R31:
988 ptr = pt_reg_addr(pt, r16) + (addr - PT_R16);
991 ptr = pt_reg_addr(pt, b0);
994 ptr = pt_reg_addr(pt, b6);
997 ptr = pt_reg_addr(pt, b7);
999 case PT_F6: case PT_F6+8: case PT_F7: case PT_F7+8:
1000 case PT_F8: case PT_F8+8: case PT_F9: case PT_F9+8:
1001 ptr = pt_reg_addr(pt, f6) + (addr - PT_F6);
1003 case PT_AR_BSPSTORE:
1004 ptr = pt_reg_addr(pt, ar_bspstore);
1007 ptr = pt_reg_addr(pt, ar_unat);
1010 ptr = pt_reg_addr(pt, ar_pfs);
1013 ptr = pt_reg_addr(pt, ar_ccv);
1016 ptr = pt_reg_addr(pt, ar_fpsr);
1019 ptr = pt_reg_addr(pt, cr_iip);
1022 ptr = pt_reg_addr(pt, pr);
1024 /* scratch register */
1027 /* disallow accessing anything else... */
1028 dprintk("ptrace: rejecting access to register "
1029 "address 0x%lx\n", addr);
1032 } else if (addr <= PT_AR_SSD) {
1033 ptr = pt_reg_addr(pt, ar_csd) + (addr - PT_AR_CSD);
1035 /* access debug registers */
1037 if (addr >= PT_IBR) {
1038 regnum = (addr - PT_IBR) >> 3;
1039 ptr = &child->thread.ibr[0];
1041 regnum = (addr - PT_DBR) >> 3;
1042 ptr = &child->thread.dbr[0];
1046 dprintk("ptrace: rejecting access to register "
1047 "address 0x%lx\n", addr);
1050 #ifdef CONFIG_PERFMON
1052 * Check if debug registers are used by perfmon. This
1053 * test must be done once we know that we can do the
1054 * operation, i.e. the arguments are all valid, but
1055 * before we start modifying the state.
1057 * Perfmon needs to keep a count of how many processes
1058 * are trying to modify the debug registers for system
1059 * wide monitoring sessions.
1061 * We also include read access here, because they may
1062 * cause the PMU-installed debug register state
1063 * (dbr[], ibr[]) to be reset. The two arrays are also
1064 * used by perfmon, but we do not use
1065 * IA64_THREAD_DBG_VALID. The registers are restored
1066 * by the PMU context switch code.
1068 if (pfm_use_debug_registers(child)) return -1;
1071 if (!(child->thread.flags & IA64_THREAD_DBG_VALID)) {
1072 child->thread.flags |= IA64_THREAD_DBG_VALID;
1073 memset(child->thread.dbr, 0,
1074 sizeof(child->thread.dbr));
1075 memset(child->thread.ibr, 0,
1076 sizeof(child->thread.ibr));
1081 if ((regnum & 1) && write_access) {
1082 /* don't let the user set kernel-level breakpoints: */
1083 *ptr = *data & ~(7UL << 56);
1095 ptrace_getregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
1097 unsigned long psr, ec, lc, rnat, bsp, cfm, nat_bits, val;
1098 struct unw_frame_info info;
1099 struct ia64_fpreg fpval;
1100 struct switch_stack *sw;
1102 long ret, retval = 0;
1106 if (!access_ok(VERIFY_WRITE, ppr, sizeof(struct pt_all_user_regs)))
1109 pt = ia64_task_regs(child);
1110 sw = (struct switch_stack *) (child->thread.ksp + 16);
1111 unw_init_from_blocked_task(&info, child);
1112 if (unw_unwind_to_user(&info) < 0) {
1116 if (((unsigned long) ppr & 0x7) != 0) {
1117 dprintk("ptrace:unaligned register address %p\n", ppr);
1121 if (access_uarea(child, PT_CR_IPSR, &psr, 0) < 0
1122 || access_uarea(child, PT_AR_EC, &ec, 0) < 0
1123 || access_uarea(child, PT_AR_LC, &lc, 0) < 0
1124 || access_uarea(child, PT_AR_RNAT, &rnat, 0) < 0
1125 || access_uarea(child, PT_AR_BSP, &bsp, 0) < 0
1126 || access_uarea(child, PT_CFM, &cfm, 0)
1127 || access_uarea(child, PT_NAT_BITS, &nat_bits, 0))
1132 retval |= __put_user(pt->cr_iip, &ppr->cr_iip);
1133 retval |= __put_user(psr, &ppr->cr_ipsr);
1137 retval |= __put_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
1138 retval |= __put_user(pt->ar_rsc, &ppr->ar[PT_AUR_RSC]);
1139 retval |= __put_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
1140 retval |= __put_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
1141 retval |= __put_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
1142 retval |= __put_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
1144 retval |= __put_user(ec, &ppr->ar[PT_AUR_EC]);
1145 retval |= __put_user(lc, &ppr->ar[PT_AUR_LC]);
1146 retval |= __put_user(rnat, &ppr->ar[PT_AUR_RNAT]);
1147 retval |= __put_user(bsp, &ppr->ar[PT_AUR_BSP]);
1148 retval |= __put_user(cfm, &ppr->cfm);
1152 retval |= __copy_to_user(&ppr->gr[1], &pt->r1, sizeof(long));
1153 retval |= __copy_to_user(&ppr->gr[2], &pt->r2, sizeof(long) *2);
1157 for (i = 4; i < 8; i++) {
1158 if (unw_access_gr(&info, i, &val, &nat, 0) < 0)
1160 retval |= __put_user(val, &ppr->gr[i]);
1165 retval |= __copy_to_user(&ppr->gr[8], &pt->r8, sizeof(long) * 4);
1169 retval |= __copy_to_user(&ppr->gr[12], &pt->r12, sizeof(long) * 2);
1170 retval |= __copy_to_user(&ppr->gr[14], &pt->r14, sizeof(long));
1171 retval |= __copy_to_user(&ppr->gr[15], &pt->r15, sizeof(long));
1175 retval |= __copy_to_user(&ppr->gr[16], &pt->r16, sizeof(long) * 16);
1179 retval |= __put_user(pt->b0, &ppr->br[0]);
1183 for (i = 1; i < 6; i++) {
1184 if (unw_access_br(&info, i, &val, 0) < 0)
1186 __put_user(val, &ppr->br[i]);
1191 retval |= __put_user(pt->b6, &ppr->br[6]);
1192 retval |= __put_user(pt->b7, &ppr->br[7]);
1196 for (i = 2; i < 6; i++) {
1197 if (unw_get_fr(&info, i, &fpval) < 0)
1199 retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
1204 retval |= __copy_to_user(&ppr->fr[6], &pt->f6,
1205 sizeof(struct ia64_fpreg) * 6);
1207 /* fp scratch regs(12-15) */
1209 retval |= __copy_to_user(&ppr->fr[12], &sw->f12,
1210 sizeof(struct ia64_fpreg) * 4);
1214 for (i = 16; i < 32; i++) {
1215 if (unw_get_fr(&info, i, &fpval) < 0)
1217 retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
1222 ia64_flush_fph(child);
1223 retval |= __copy_to_user(&ppr->fr[32], &child->thread.fph,
1224 sizeof(ppr->fr[32]) * 96);
1228 retval |= __put_user(pt->pr, &ppr->pr);
1232 retval |= __put_user(nat_bits, &ppr->nat);
1234 ret = retval ? -EIO : 0;
1239 ptrace_setregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
1241 unsigned long psr, rsc, ec, lc, rnat, bsp, cfm, nat_bits, val = 0;
1242 struct unw_frame_info info;
1243 struct switch_stack *sw;
1244 struct ia64_fpreg fpval;
1246 long ret, retval = 0;
1249 memset(&fpval, 0, sizeof(fpval));
1251 if (!access_ok(VERIFY_READ, ppr, sizeof(struct pt_all_user_regs)))
1254 pt = ia64_task_regs(child);
1255 sw = (struct switch_stack *) (child->thread.ksp + 16);
1256 unw_init_from_blocked_task(&info, child);
1257 if (unw_unwind_to_user(&info) < 0) {
1261 if (((unsigned long) ppr & 0x7) != 0) {
1262 dprintk("ptrace:unaligned register address %p\n", ppr);
1268 retval |= __get_user(pt->cr_iip, &ppr->cr_iip);
1269 retval |= __get_user(psr, &ppr->cr_ipsr);
1273 retval |= __get_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
1274 retval |= __get_user(rsc, &ppr->ar[PT_AUR_RSC]);
1275 retval |= __get_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
1276 retval |= __get_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
1277 retval |= __get_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
1278 retval |= __get_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
1280 retval |= __get_user(ec, &ppr->ar[PT_AUR_EC]);
1281 retval |= __get_user(lc, &ppr->ar[PT_AUR_LC]);
1282 retval |= __get_user(rnat, &ppr->ar[PT_AUR_RNAT]);
1283 retval |= __get_user(bsp, &ppr->ar[PT_AUR_BSP]);
1284 retval |= __get_user(cfm, &ppr->cfm);
1288 retval |= __copy_from_user(&pt->r1, &ppr->gr[1], sizeof(long));
1289 retval |= __copy_from_user(&pt->r2, &ppr->gr[2], sizeof(long) * 2);
1293 for (i = 4; i < 8; i++) {
1294 retval |= __get_user(val, &ppr->gr[i]);
1295 /* NaT bit will be set via PT_NAT_BITS: */
1296 if (unw_set_gr(&info, i, val, 0) < 0)
1302 retval |= __copy_from_user(&pt->r8, &ppr->gr[8], sizeof(long) * 4);
1306 retval |= __copy_from_user(&pt->r12, &ppr->gr[12], sizeof(long) * 2);
1307 retval |= __copy_from_user(&pt->r14, &ppr->gr[14], sizeof(long));
1308 retval |= __copy_from_user(&pt->r15, &ppr->gr[15], sizeof(long));
1312 retval |= __copy_from_user(&pt->r16, &ppr->gr[16], sizeof(long) * 16);
1316 retval |= __get_user(pt->b0, &ppr->br[0]);
1320 for (i = 1; i < 6; i++) {
1321 retval |= __get_user(val, &ppr->br[i]);
1322 unw_set_br(&info, i, val);
1327 retval |= __get_user(pt->b6, &ppr->br[6]);
1328 retval |= __get_user(pt->b7, &ppr->br[7]);
1332 for (i = 2; i < 6; i++) {
1333 retval |= __copy_from_user(&fpval, &ppr->fr[i], sizeof(fpval));
1334 if (unw_set_fr(&info, i, fpval) < 0)
1340 retval |= __copy_from_user(&pt->f6, &ppr->fr[6],
1341 sizeof(ppr->fr[6]) * 6);
1343 /* fp scratch regs(12-15) */
1345 retval |= __copy_from_user(&sw->f12, &ppr->fr[12],
1346 sizeof(ppr->fr[12]) * 4);
1350 for (i = 16; i < 32; i++) {
1351 retval |= __copy_from_user(&fpval, &ppr->fr[i],
1353 if (unw_set_fr(&info, i, fpval) < 0)
1359 ia64_sync_fph(child);
1360 retval |= __copy_from_user(&child->thread.fph, &ppr->fr[32],
1361 sizeof(ppr->fr[32]) * 96);
1365 retval |= __get_user(pt->pr, &ppr->pr);
1369 retval |= __get_user(nat_bits, &ppr->nat);
1371 retval |= access_uarea(child, PT_CR_IPSR, &psr, 1);
1372 retval |= access_uarea(child, PT_AR_RSC, &rsc, 1);
1373 retval |= access_uarea(child, PT_AR_EC, &ec, 1);
1374 retval |= access_uarea(child, PT_AR_LC, &lc, 1);
1375 retval |= access_uarea(child, PT_AR_RNAT, &rnat, 1);
1376 retval |= access_uarea(child, PT_AR_BSP, &bsp, 1);
1377 retval |= access_uarea(child, PT_CFM, &cfm, 1);
1378 retval |= access_uarea(child, PT_NAT_BITS, &nat_bits, 1);
1380 ret = retval ? -EIO : 0;
1385 * Called by kernel/ptrace.c when detaching..
1387 * Make sure the single step bit is not set.
1390 ptrace_disable (struct task_struct *child)
1392 struct ia64_psr *child_psr = ia64_psr(ia64_task_regs(child));
1394 /* make sure the single step/taken-branch trap bits are not set: */
1400 sys_ptrace (long request, pid_t pid, unsigned long addr, unsigned long data)
1403 unsigned long urbs_end, peek_or_poke;
1404 struct task_struct *child;
1405 struct switch_stack *sw;
1410 if (request == PTRACE_TRACEME) {
1411 /* are we already being traced? */
1412 if (current->ptrace & PT_PTRACED)
1414 ret = security_ptrace(current->parent, current);
1417 current->ptrace |= PT_PTRACED;
1422 peek_or_poke = (request == PTRACE_PEEKTEXT
1423 || request == PTRACE_PEEKDATA
1424 || request == PTRACE_POKETEXT
1425 || request == PTRACE_POKEDATA);
1427 read_lock(&tasklist_lock);
1429 child = find_task_by_pid(pid);
1432 child = find_thread_for_addr(child, addr);
1433 get_task_struct(child);
1436 read_unlock(&tasklist_lock);
1440 if (pid == 1) /* no messing around with init! */
1443 if (request == PTRACE_ATTACH) {
1444 ret = ptrace_attach(child);
1448 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1452 pt = ia64_task_regs(child);
1453 sw = (struct switch_stack *) (child->thread.ksp + 16);
1456 case PTRACE_PEEKTEXT:
1457 case PTRACE_PEEKDATA:
1458 /* read word at location addr */
1459 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
1460 ret = ia64_peek(child, sw, urbs_end, addr, &data);
1463 /* ensure "ret" is not mistaken as an error code: */
1464 force_successful_syscall_return();
1468 case PTRACE_POKETEXT:
1469 case PTRACE_POKEDATA:
1470 /* write the word at location addr */
1471 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
1472 ret = ia64_poke(child, sw, urbs_end, addr, data);
1475 case PTRACE_PEEKUSR:
1476 /* read the word at addr in the USER area */
1477 if (access_uarea(child, addr, &data, 0) < 0) {
1482 /* ensure "ret" is not mistaken as an error code */
1483 force_successful_syscall_return();
1486 case PTRACE_POKEUSR:
1487 /* write the word at addr in the USER area */
1488 if (access_uarea(child, addr, &data, 1) < 0) {
1495 case PTRACE_OLD_GETSIGINFO:
1496 /* for backwards-compatibility */
1497 ret = ptrace_request(child, PTRACE_GETSIGINFO, addr, data);
1500 case PTRACE_OLD_SETSIGINFO:
1501 /* for backwards-compatibility */
1502 ret = ptrace_request(child, PTRACE_SETSIGINFO, addr, data);
1505 case PTRACE_SYSCALL:
1506 /* continue and stop at next (return from) syscall */
1508 /* restart after signal. */
1510 if (!valid_signal(data))
1512 if (request == PTRACE_SYSCALL)
1513 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1515 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1516 child->exit_code = data;
1519 * Make sure the single step/taken-branch trap bits
1522 ia64_psr(pt)->ss = 0;
1523 ia64_psr(pt)->tb = 0;
1525 wake_up_process(child);
1531 * Make the child exit. Best I can do is send it a
1532 * sigkill. Perhaps it should be put in the status
1533 * that it wants to exit.
1535 if (child->exit_state == EXIT_ZOMBIE)
1538 child->exit_code = SIGKILL;
1540 ptrace_disable(child);
1541 wake_up_process(child);
1545 case PTRACE_SINGLESTEP:
1546 /* let child execute for one instruction */
1547 case PTRACE_SINGLEBLOCK:
1549 if (!valid_signal(data))
1552 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1553 if (request == PTRACE_SINGLESTEP) {
1554 ia64_psr(pt)->ss = 1;
1556 ia64_psr(pt)->tb = 1;
1558 child->exit_code = data;
1560 /* give it a chance to run. */
1561 wake_up_process(child);
1566 /* detach a process that was attached. */
1567 ret = ptrace_detach(child, data);
1570 case PTRACE_GETREGS:
1571 ret = ptrace_getregs(child,
1572 (struct pt_all_user_regs __user *) data);
1575 case PTRACE_SETREGS:
1576 ret = ptrace_setregs(child,
1577 (struct pt_all_user_regs __user *) data);
1581 ret = ptrace_request(child, request, addr, data);
1585 put_task_struct(child);
1593 syscall_trace (void)
1595 if (!test_thread_flag(TIF_SYSCALL_TRACE))
1597 if (!(current->ptrace & PT_PTRACED))
1600 * The 0x80 provides a way for the tracing parent to
1601 * distinguish between a syscall stop and SIGTRAP delivery.
1603 ptrace_notify(SIGTRAP
1604 | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
1607 * This isn't the same as continuing with a signal, but it
1608 * will do for normal use. strace only continues with a
1609 * signal if the stopping signal is not SIGTRAP. -brl
1611 if (current->exit_code) {
1612 send_sig(current->exit_code, current, 1);
1613 current->exit_code = 0;
1617 /* "asmlinkage" so the input arguments are preserved... */
1620 syscall_trace_enter (long arg0, long arg1, long arg2, long arg3,
1621 long arg4, long arg5, long arg6, long arg7,
1622 struct pt_regs regs)
1624 if (test_thread_flag(TIF_SYSCALL_TRACE)
1625 && (current->ptrace & PT_PTRACED))
1628 if (unlikely(current->audit_context)) {
1632 if (IS_IA32_PROCESS(®s)) {
1634 arch = AUDIT_ARCH_I386;
1637 arch = AUDIT_ARCH_IA64;
1640 audit_syscall_entry(current, arch, syscall, arg0, arg1, arg2, arg3);
1645 /* "asmlinkage" so the input arguments are preserved... */
1648 syscall_trace_leave (long arg0, long arg1, long arg2, long arg3,
1649 long arg4, long arg5, long arg6, long arg7,
1650 struct pt_regs regs)
1652 if (unlikely(current->audit_context))
1653 audit_syscall_exit(current, AUDITSC_RESULT(regs.r10), regs.r8);
1655 if (test_thread_flag(TIF_SYSCALL_TRACE)
1656 && (current->ptrace & PT_PTRACED))