2 * Kernel support for the ptrace() and syscall tracing interfaces.
4 * Copyright (C) 1999-2003 Hewlett-Packard Co
5 * David Mosberger-Tang <davidm@hpl.hp.com>
7 * Derived from the x86 and Alpha versions. Most of the code in here
8 * could actually be factored into a common set of routines.
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/smp_lock.h>
18 #include <linux/user.h>
19 #include <linux/security.h>
21 #include <asm/pgtable.h>
22 #include <asm/processor.h>
23 #include <asm/ptrace_offsets.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
27 #include <asm/unwind.h>
29 #include <asm/perfmon.h>
34 #define p4 (1UL << 4) /* for pSys (see entry.h) */
35 #define p5 (1UL << 5) /* for pNonSys (see entry.h) */
38 * Bits in the PSR that we allow ptrace() to change:
39 * be, up, ac, mfl, mfh (the user mask; five bits total)
40 * db (debug breakpoint fault; one bit)
41 * id (instruction debug fault disable; one bit)
42 * dd (data debug fault disable; one bit)
43 * ri (restart instruction; two bits)
44 * is (instruction set; one bit)
46 #define IPSR_WRITE_MASK \
47 (IA64_PSR_UM | IA64_PSR_DB | IA64_PSR_IS | IA64_PSR_ID | IA64_PSR_DD | IA64_PSR_RI)
48 #define IPSR_READ_MASK IPSR_WRITE_MASK
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 mask = ((1UL << (last - first + 1)) - 1) << first; \
80 dist = 64 + bit - first; \
83 ia64_rotr(unat, dist) & mask; \
88 * Registers that are stored consecutively in struct pt_regs can be handled in
89 * parallel. If the register order in struct_pt_regs changes, this code MUST be
92 val = GET_BITS( 1, 1, scratch_unat);
93 val |= GET_BITS( 2, 3, scratch_unat);
94 val |= GET_BITS(12, 13, scratch_unat);
95 val |= GET_BITS(14, 14, scratch_unat);
96 val |= GET_BITS(15, 15, scratch_unat);
97 val |= GET_BITS( 8, 11, scratch_unat);
98 val |= GET_BITS(16, 31, scratch_unat);
105 * Set the NaT bits for the scratch registers according to NAT and
106 * return the resulting unat (assuming the scratch registers are
110 ia64_put_scratch_nat_bits (struct pt_regs *pt, unsigned long nat)
112 # define PUT_BITS(first, last, nat) \
114 unsigned long bit = ia64_unat_pos(&pt->r##first); \
115 unsigned long mask = ((1UL << (last - first + 1)) - 1) << first; \
118 dist = 64 + bit - first; \
120 dist = bit - first; \
121 ia64_rotl(nat & mask, dist); \
123 unsigned long scratch_unat;
126 * Registers that are stored consecutively in struct pt_regs can be handled in
127 * parallel. If the register order in struct_pt_regs changes, this code MUST be
130 scratch_unat = PUT_BITS( 1, 1, nat);
131 scratch_unat |= PUT_BITS( 2, 3, nat);
132 scratch_unat |= PUT_BITS(12, 13, nat);
133 scratch_unat |= PUT_BITS(14, 14, nat);
134 scratch_unat |= PUT_BITS(15, 15, nat);
135 scratch_unat |= PUT_BITS( 8, 11, nat);
136 scratch_unat |= PUT_BITS(16, 31, nat);
143 #define IA64_MLX_TEMPLATE 0x2
144 #define IA64_MOVL_OPCODE 6
147 ia64_increment_ip (struct pt_regs *regs)
149 unsigned long w0, ri = ia64_psr(regs)->ri + 1;
154 } else if (ri == 2) {
155 get_user(w0, (char *) regs->cr_iip + 0);
156 if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
158 * rfi'ing to slot 2 of an MLX bundle causes
159 * an illegal operation fault. We don't want
166 ia64_psr(regs)->ri = ri;
170 ia64_decrement_ip (struct pt_regs *regs)
172 unsigned long w0, ri = ia64_psr(regs)->ri - 1;
174 if (ia64_psr(regs)->ri == 0) {
177 get_user(w0, (char *) regs->cr_iip + 0);
178 if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
180 * rfi'ing to slot 2 of an MLX bundle causes
181 * an illegal operation fault. We don't want
187 ia64_psr(regs)->ri = ri;
191 * This routine is used to read an rnat bits that are stored on the kernel backing store.
192 * Since, in general, the alignment of the user and kernel are different, this is not
193 * completely trivial. In essence, we need to construct the user RNAT based on up to two
194 * kernel RNAT values and/or the RNAT value saved in the child's pt_regs.
198 * +--------+ <-- lowest address
205 * | slot01 | > child_regs->ar_rnat
207 * | slot02 | / kernel rbs
208 * +--------+ +--------+
209 * <- child_regs->ar_bspstore | slot61 | <-- krbs
210 * +- - - - + +--------+
212 * +- - - - + +--------+
214 * +- - - - + +--------+
216 * +- - - - + +--------+
221 * | slot01 | > child_stack->ar_rnat
225 * <--- child_stack->ar_bspstore
227 * The way to think of this code is as follows: bit 0 in the user rnat corresponds to some
228 * bit N (0 <= N <= 62) in one of the kernel rnat value. The kernel rnat value holding
229 * this bit is stored in variable rnat0. rnat1 is loaded with the kernel rnat value that
230 * form the upper bits of the user rnat value.
234 * o when reading the rnat "below" the first rnat slot on the kernel backing store,
235 * rnat0/rnat1 are set to 0 and the low order bits are merged in from pt->ar_rnat.
237 * o when reading the rnat "above" the last rnat slot on the kernel backing store,
238 * rnat0/rnat1 gets its value from sw->ar_rnat.
241 get_rnat (struct task_struct *task, struct switch_stack *sw,
242 unsigned long *krbs, unsigned long *urnat_addr, unsigned long *urbs_end)
244 unsigned long rnat0 = 0, rnat1 = 0, urnat = 0, *slot0_kaddr, umask = 0, mask, m;
245 unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
246 long num_regs, nbits;
249 pt = ia64_task_regs(task);
250 kbsp = (unsigned long *) sw->ar_bspstore;
251 ubspstore = (unsigned long *) pt->ar_bspstore;
253 if (urbs_end < urnat_addr)
254 nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_end);
257 mask = (1UL << nbits) - 1;
259 * First, figure out which bit number slot 0 in user-land maps to in the kernel
260 * rnat. Do this by figuring out how many register slots we're beyond the user's
261 * backingstore and then computing the equivalent address in kernel space.
263 num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
264 slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
265 shift = ia64_rse_slot_num(slot0_kaddr);
266 rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
267 rnat0_kaddr = rnat1_kaddr - 64;
269 if (ubspstore + 63 > urnat_addr) {
270 /* some bits need to be merged in from pt->ar_rnat */
271 umask = ((1UL << ia64_rse_slot_num(ubspstore)) - 1) & mask;
272 urnat = (pt->ar_rnat & umask);
279 if (rnat0_kaddr >= kbsp)
281 else if (rnat0_kaddr > krbs)
282 rnat0 = *rnat0_kaddr;
283 urnat |= (rnat0 & m) >> shift;
285 m = mask >> (63 - shift);
286 if (rnat1_kaddr >= kbsp)
288 else if (rnat1_kaddr > krbs)
289 rnat1 = *rnat1_kaddr;
290 urnat |= (rnat1 & m) << (63 - shift);
295 * The reverse of get_rnat.
298 put_rnat (struct task_struct *task, struct switch_stack *sw,
299 unsigned long *krbs, unsigned long *urnat_addr, unsigned long urnat,
300 unsigned long *urbs_end)
302 unsigned long rnat0 = 0, rnat1 = 0, *slot0_kaddr, umask = 0, mask, m;
303 unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
304 long num_regs, nbits;
306 unsigned long cfm, *urbs_kargs;
307 struct unw_frame_info info;
309 pt = ia64_task_regs(task);
310 kbsp = (unsigned long *) sw->ar_bspstore;
311 ubspstore = (unsigned long *) pt->ar_bspstore;
313 urbs_kargs = urbs_end;
314 if (in_syscall(pt)) {
316 * If entered via syscall, don't allow user to set rnat bits
319 unw_init_from_blocked_task(&info,task);
320 if (unw_unwind_to_user(&info) == 0) {
321 unw_get_cfm(&info,&cfm);
322 urbs_kargs = ia64_rse_skip_regs(urbs_end,-(cfm & 0x7f));
326 if (urbs_kargs >= urnat_addr)
329 if ((urnat_addr - 63) >= urbs_kargs)
331 nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_kargs);
333 mask = (1UL << nbits) - 1;
336 * First, figure out which bit number slot 0 in user-land maps to in the kernel
337 * rnat. Do this by figuring out how many register slots we're beyond the user's
338 * backingstore and then computing the equivalent address in kernel space.
340 num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
341 slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
342 shift = ia64_rse_slot_num(slot0_kaddr);
343 rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
344 rnat0_kaddr = rnat1_kaddr - 64;
346 if (ubspstore + 63 > urnat_addr) {
347 /* some bits need to be place in pt->ar_rnat: */
348 umask = ((1UL << ia64_rse_slot_num(ubspstore)) - 1) & mask;
349 pt->ar_rnat = (pt->ar_rnat & ~umask) | (urnat & umask);
355 * Note: Section 11.1 of the EAS guarantees that bit 63 of an
356 * rnat slot is ignored. so we don't have to clear it here.
358 rnat0 = (urnat << shift);
360 if (rnat0_kaddr >= kbsp)
361 sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat0 & m);
362 else if (rnat0_kaddr > krbs)
363 *rnat0_kaddr = ((*rnat0_kaddr & ~m) | (rnat0 & m));
365 rnat1 = (urnat >> (63 - shift));
366 m = mask >> (63 - shift);
367 if (rnat1_kaddr >= kbsp)
368 sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat1 & m);
369 else if (rnat1_kaddr > krbs)
370 *rnat1_kaddr = ((*rnat1_kaddr & ~m) | (rnat1 & m));
374 on_kernel_rbs (unsigned long addr, unsigned long bspstore, unsigned long urbs_end)
376 return (addr >= bspstore
377 && addr <= (unsigned long) ia64_rse_rnat_addr((unsigned long *) urbs_end));
381 * Read a word from the user-level backing store of task CHILD. ADDR is the user-level
382 * address to read the word from, VAL a pointer to the return value, and USER_BSP gives
383 * the end of the user-level backing store (i.e., it's the address that would be in ar.bsp
384 * after the user executed a "cover" instruction).
386 * This routine takes care of accessing the kernel register backing store for those
387 * registers that got spilled there. It also takes care of calculating the appropriate
388 * RNaT collection words.
391 ia64_peek (struct task_struct *child, struct switch_stack *child_stack, unsigned long user_rbs_end,
392 unsigned long addr, long *val)
394 unsigned long *bspstore, *krbs, regnum, *laddr, *urbs_end, *rnat_addr;
395 struct pt_regs *child_regs;
399 urbs_end = (long *) user_rbs_end;
400 laddr = (unsigned long *) addr;
401 child_regs = ia64_task_regs(child);
402 bspstore = (unsigned long *) child_regs->ar_bspstore;
403 krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
404 if (on_kernel_rbs(addr, (unsigned long) bspstore, (unsigned long) urbs_end)) {
406 * Attempt to read the RBS in an area that's actually on the kernel RBS =>
407 * read the corresponding bits in the kernel RBS.
409 rnat_addr = ia64_rse_rnat_addr(laddr);
410 ret = get_rnat(child, child_stack, krbs, rnat_addr, urbs_end);
412 if (laddr == rnat_addr) {
413 /* return NaT collection word itself */
418 if (((1UL << ia64_rse_slot_num(laddr)) & ret) != 0) {
420 * It is implementation dependent whether the data portion of a
421 * NaT value gets saved on a st8.spill or RSE spill (e.g., see
422 * EAS 2.6, 4.4.4.6 Register Spill and Fill). To get consistent
423 * behavior across all possible IA-64 implementations, we return
430 if (laddr < urbs_end) {
431 /* the desired word is on the kernel RBS and is not a NaT */
432 regnum = ia64_rse_num_regs(bspstore, laddr);
433 *val = *ia64_rse_skip_regs(krbs, regnum);
437 copied = access_process_vm(child, addr, &ret, sizeof(ret), 0);
438 if (copied != sizeof(ret))
445 ia64_poke (struct task_struct *child, struct switch_stack *child_stack, unsigned long user_rbs_end,
446 unsigned long addr, long val)
448 unsigned long *bspstore, *krbs, regnum, *laddr, *urbs_end = (long *) user_rbs_end;
449 struct pt_regs *child_regs;
451 laddr = (unsigned long *) addr;
452 child_regs = ia64_task_regs(child);
453 bspstore = (unsigned long *) child_regs->ar_bspstore;
454 krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
455 if (on_kernel_rbs(addr, (unsigned long) bspstore, (unsigned long) urbs_end)) {
457 * Attempt to write the RBS in an area that's actually on the kernel RBS
458 * => write the corresponding bits in the kernel RBS.
460 if (ia64_rse_is_rnat_slot(laddr))
461 put_rnat(child, child_stack, krbs, laddr, val, urbs_end);
463 if (laddr < urbs_end) {
464 regnum = ia64_rse_num_regs(bspstore, laddr);
465 *ia64_rse_skip_regs(krbs, regnum) = val;
468 } else if (access_process_vm(child, addr, &val, sizeof(val), 1) != sizeof(val)) {
475 * Calculate the address of the end of the user-level register backing store. This is the
476 * address that would have been stored in ar.bsp if the user had executed a "cover"
477 * instruction right before entering the kernel. If CFMP is not NULL, it is used to
478 * return the "current frame mask" that was active at the time the kernel was entered.
481 ia64_get_user_rbs_end (struct task_struct *child, struct pt_regs *pt, unsigned long *cfmp)
483 unsigned long *krbs, *bspstore, cfm;
484 struct unw_frame_info info;
487 krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
488 bspstore = (unsigned long *) pt->ar_bspstore;
489 ndirty = ia64_rse_num_regs(krbs, krbs + (pt->loadrs >> 19));
490 cfm = pt->cr_ifs & ~(1UL << 63);
492 if (in_syscall(pt)) {
494 * If bit 63 of cr.ifs is cleared, the kernel was entered via a system
495 * call and we need to recover the CFM that existed on entry to the
496 * kernel by unwinding the kernel stack.
498 unw_init_from_blocked_task(&info, child);
499 if (unw_unwind_to_user(&info) == 0) {
500 unw_get_cfm(&info, &cfm);
501 ndirty += (cfm & 0x7f);
506 return (unsigned long) ia64_rse_skip_regs(bspstore, ndirty);
510 * Synchronize (i.e, write) the RSE backing store living in kernel space to the VM of the
511 * CHILD task. SW and PT are the pointers to the switch_stack and pt_regs structures,
512 * respectively. USER_RBS_END is the user-level address at which the backing store ends.
515 ia64_sync_user_rbs (struct task_struct *child, struct switch_stack *sw,
516 unsigned long user_rbs_start, unsigned long user_rbs_end)
518 unsigned long addr, val;
521 /* now copy word for word from kernel rbs to user rbs: */
522 for (addr = user_rbs_start; addr < user_rbs_end; addr += 8) {
523 ret = ia64_peek(child, sw, user_rbs_end, addr, &val);
526 if (access_process_vm(child, addr, &val, sizeof(val), 1) != sizeof(val))
533 thread_matches (struct task_struct *thread, unsigned long addr)
535 unsigned long thread_rbs_end;
536 struct pt_regs *thread_regs;
538 if (ptrace_check_attach(thread, 0) < 0)
540 * If the thread is not in an attachable state, we'll ignore it.
541 * The net effect is that if ADDR happens to overlap with the
542 * portion of the thread's register backing store that is
543 * currently residing on the thread's kernel stack, then ptrace()
544 * may end up accessing a stale value. But if the thread isn't
545 * stopped, that's a problem anyhow, so we're doing as well as we
550 thread_regs = ia64_task_regs(thread);
551 thread_rbs_end = ia64_get_user_rbs_end(thread, thread_regs, NULL);
552 if (!on_kernel_rbs(addr, thread_regs->ar_bspstore, thread_rbs_end))
555 return 1; /* looks like we've got a winner */
559 * GDB apparently wants to be able to read the register-backing store of any thread when
560 * attached to a given process. If we are peeking or poking an address that happens to
561 * reside in the kernel-backing store of another thread, we need to attach to that thread,
562 * because otherwise we end up accessing stale data.
564 * task_list_lock must be read-locked before calling this routine!
566 static struct task_struct *
567 find_thread_for_addr (struct task_struct *child, unsigned long addr)
569 struct task_struct *g, *p;
570 struct mm_struct *mm;
573 if (!(mm = get_task_mm(child)))
576 mm_users = atomic_read(&mm->mm_users) - 1; /* -1 because of our get_task_mm()... */
578 goto out; /* not multi-threaded */
581 * First, traverse the child's thread-list. Good for scalability with
586 if (thread_matches(p, addr)) {
592 } while ((p = next_thread(p)) != child);
594 do_each_thread(g, p) {
598 if (thread_matches(p, addr)) {
602 } while_each_thread(g, p);
609 * Write f32-f127 back to task->thread.fph if it has been modified.
612 ia64_flush_fph (struct task_struct *task)
614 struct ia64_psr *psr = ia64_psr(ia64_task_regs(task));
616 if (ia64_is_local_fpu_owner(task) && psr->mfh) {
618 task->thread.flags |= IA64_THREAD_FPH_VALID;
619 ia64_save_fpu(&task->thread.fph[0]);
624 * Sync the fph state of the task so that it can be manipulated
625 * through thread.fph. If necessary, f32-f127 are written back to
626 * thread.fph or, if the fph state hasn't been used before, thread.fph
627 * is cleared to zeroes. Also, access to f32-f127 is disabled to
628 * ensure that the task picks up the state from thread.fph when it
632 ia64_sync_fph (struct task_struct *task)
634 struct ia64_psr *psr = ia64_psr(ia64_task_regs(task));
636 ia64_flush_fph(task);
637 if (!(task->thread.flags & IA64_THREAD_FPH_VALID)) {
638 task->thread.flags |= IA64_THREAD_FPH_VALID;
639 memset(&task->thread.fph, 0, sizeof(task->thread.fph));
646 access_fr (struct unw_frame_info *info, int regnum, int hi, unsigned long *data, int write_access)
648 struct ia64_fpreg fpval;
651 ret = unw_get_fr(info, regnum, &fpval);
656 fpval.u.bits[hi] = *data;
657 ret = unw_set_fr(info, regnum, fpval);
659 *data = fpval.u.bits[hi];
664 * Change the machine-state of CHILD such that it will return via the normal
665 * kernel exit-path, rather than the syscall-exit path.
668 convert_to_non_syscall (struct task_struct *child, struct pt_regs *pt, unsigned long cfm)
670 struct unw_frame_info info, prev_info;
671 unsigned long ip, pr;
673 unw_init_from_blocked_task(&info, child);
676 if (unw_unwind(&info) < 0)
678 if (unw_get_rp(&info, &ip) < 0)
680 if (ip < FIXADDR_USER_END)
684 unw_get_pr(&prev_info, &pr);
687 unw_set_pr(&prev_info, pr);
689 pt->cr_ifs = (1UL << 63) | cfm;
693 access_uarea (struct task_struct *child, unsigned long addr, unsigned long *data, int write_access)
695 unsigned long *ptr, regnum, urbs_end, rnat_addr, cfm;
696 struct switch_stack *sw;
699 pt = ia64_task_regs(child);
700 sw = (struct switch_stack *) (child->thread.ksp + 16);
702 if ((addr & 0x7) != 0) {
703 dprintk("ptrace: unaligned register address 0x%lx\n", addr);
707 if (addr < PT_F127 + 16) {
710 ia64_sync_fph(child);
712 ia64_flush_fph(child);
713 ptr = (unsigned long *) ((unsigned long) &child->thread.fph + addr);
714 } else if ((addr >= PT_F10) && (addr < PT_F11 + 16)) {
715 /* scratch registers untouched by kernel (saved in pt_regs) */
716 ptr = (unsigned long *)
717 ((long) pt + offsetof(struct pt_regs, f10) + addr - PT_F10);
718 } else if (addr >= PT_F12 && addr < PT_F15 + 16) {
719 /* scratch registers untouched by kernel (saved in switch_stack) */
720 ptr = (unsigned long *) ((long) sw + (addr - PT_NAT_BITS - 32));
721 } else if (addr < PT_AR_LC + 8) {
722 /* preserved state: */
723 unsigned long nat_bits, scratch_unat, dummy = 0;
724 struct unw_frame_info info;
728 unw_init_from_blocked_task(&info, child);
729 if (unw_unwind_to_user(&info) < 0)
736 scratch_unat = ia64_put_scratch_nat_bits(pt, nat_bits);
737 if (unw_set_ar(&info, UNW_AR_UNAT, scratch_unat) < 0) {
738 dprintk("ptrace: failed to set ar.unat\n");
741 for (regnum = 4; regnum <= 7; ++regnum) {
742 unw_get_gr(&info, regnum, &dummy, &nat);
743 unw_set_gr(&info, regnum, dummy, (nat_bits >> regnum) & 1);
746 if (unw_get_ar(&info, UNW_AR_UNAT, &scratch_unat) < 0) {
747 dprintk("ptrace: failed to read ar.unat\n");
750 nat_bits = ia64_get_scratch_nat_bits(pt, scratch_unat);
751 for (regnum = 4; regnum <= 7; ++regnum) {
752 unw_get_gr(&info, regnum, &dummy, &nat);
753 nat_bits |= (nat != 0) << regnum;
759 case PT_R4: case PT_R5: case PT_R6: case PT_R7:
761 /* read NaT bit first: */
764 ret = unw_get_gr(&info, (addr - PT_R4)/8 + 4, &dummy, &nat);
768 return unw_access_gr(&info, (addr - PT_R4)/8 + 4, data, &nat,
771 case PT_B1: case PT_B2: case PT_B3: case PT_B4: case PT_B5:
772 return unw_access_br(&info, (addr - PT_B1)/8 + 1, data, write_access);
775 return unw_access_ar(&info, UNW_AR_EC, data, write_access);
778 return unw_access_ar(&info, UNW_AR_LC, data, write_access);
781 if (addr >= PT_F2 && addr < PT_F5 + 16)
782 return access_fr(&info, (addr - PT_F2)/16 + 2, (addr & 8) != 0,
784 else if (addr >= PT_F16 && addr < PT_F31 + 16)
785 return access_fr(&info, (addr - PT_F16)/16 + 16, (addr & 8) != 0,
788 dprintk("ptrace: rejecting access to register address 0x%lx\n",
793 } else if (addr < PT_F9+16) {
798 * By convention, we use PT_AR_BSP to refer to the end of the user-level
799 * backing store. Use ia64_rse_skip_regs(PT_AR_BSP, -CFM.sof) to get
800 * the real value of ar.bsp at the time the kernel was entered.
802 * Furthermore, when changing the contents of PT_AR_BSP (or
803 * PT_CFM) we MUST copy any users-level stacked registers that are
804 * stored on the kernel stack back to user-space because
805 * otherwise, we might end up clobbering kernel stacked registers.
806 * Also, if this happens while the task is blocked in a system
807 * call, which convert the state such that the non-system-call
808 * exit path is used. This ensures that the proper state will be
809 * picked up when resuming execution. However, it *also* means
810 * that once we write PT_AR_BSP/PT_CFM, it won't be possible to
811 * modify the syscall arguments of the pending system call any
812 * longer. This shouldn't be an issue because modifying
813 * PT_AR_BSP/PT_CFM generally implies that we're either abandoning
814 * the pending system call or that we defer it's re-execution
815 * (e.g., due to GDB doing an inferior function call).
817 urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
819 if (*data != urbs_end) {
820 if (ia64_sync_user_rbs(child, sw,
821 pt->ar_bspstore, urbs_end) < 0)
824 convert_to_non_syscall(child, pt, cfm);
825 /* simulate user-level write of ar.bsp: */
827 pt->ar_bspstore = *data;
834 urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
836 if (((cfm ^ *data) & 0x3fffffffffU) != 0) {
837 if (ia64_sync_user_rbs(child, sw,
838 pt->ar_bspstore, urbs_end) < 0)
841 convert_to_non_syscall(child, pt, cfm);
842 pt->cr_ifs = ((pt->cr_ifs & ~0x3fffffffffUL)
843 | (*data & 0x3fffffffffUL));
851 pt->cr_ipsr = ((*data & IPSR_WRITE_MASK)
852 | (pt->cr_ipsr & ~IPSR_WRITE_MASK));
854 *data = (pt->cr_ipsr & IPSR_READ_MASK);
858 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
859 rnat_addr = (long) ia64_rse_rnat_addr((long *) urbs_end);
861 return ia64_poke(child, sw, urbs_end, rnat_addr, *data);
863 return ia64_peek(child, sw, urbs_end, rnat_addr, data);
866 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, r1));
869 case PT_R2: case PT_R3:
870 ptr = (unsigned long *)
871 ((long) pt + offsetof(struct pt_regs, r2) + addr - PT_R2);
873 case PT_R8: case PT_R9: case PT_R10: case PT_R11:
874 ptr = (unsigned long *)
875 ((long) pt + offsetof(struct pt_regs, r8)+ addr - PT_R8);
877 case PT_R12: case PT_R13:
878 ptr = (unsigned long *)
879 ((long) pt + offsetof(struct pt_regs, r12)+ addr - PT_R12);
882 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, r14));
885 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, r15));
887 case PT_R16: case PT_R17: case PT_R18: case PT_R19:
888 case PT_R20: case PT_R21: case PT_R22: case PT_R23:
889 case PT_R24: case PT_R25: case PT_R26: case PT_R27:
890 case PT_R28: case PT_R29: case PT_R30: case PT_R31:
891 ptr = (unsigned long *)
892 ((long) pt + offsetof(struct pt_regs, r16) + addr - PT_R16);
895 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, b0));
898 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, b6));
901 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, b7));
903 case PT_F6: case PT_F6+8: case PT_F7: case PT_F7+8:
904 case PT_F8: case PT_F8+8: case PT_F9: case PT_F9+8:
905 ptr = (unsigned long *)
906 ((long) pt + offsetof(struct pt_regs, f6) + addr - PT_F6);
909 ptr = (unsigned long *)
910 ((long) pt + offsetof(struct pt_regs, ar_bspstore));
913 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_rsc));
916 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_unat));
919 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_pfs));
922 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_ccv));
925 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_fpsr));
928 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, cr_iip));
931 ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, pr));
933 /* scratch register */
936 /* disallow accessing anything else... */
937 dprintk("ptrace: rejecting access to register address 0x%lx\n",
941 } else if (addr <= PT_AR_SSD) {
942 ptr = (unsigned long *)
943 ((long) pt + offsetof(struct pt_regs, ar_csd) + addr - PT_AR_CSD);
945 /* access debug registers */
947 if (addr >= PT_IBR) {
948 regnum = (addr - PT_IBR) >> 3;
949 ptr = &child->thread.ibr[0];
951 regnum = (addr - PT_DBR) >> 3;
952 ptr = &child->thread.dbr[0];
956 dprintk("ptrace: rejecting access to register address 0x%lx\n", addr);
959 #ifdef CONFIG_PERFMON
961 * Check if debug registers are used by perfmon. This test must be done
962 * once we know that we can do the operation, i.e. the arguments are all
963 * valid, but before we start modifying the state.
965 * Perfmon needs to keep a count of how many processes are trying to
966 * modify the debug registers for system wide monitoring sessions.
968 * We also include read access here, because they may cause the
969 * PMU-installed debug register state (dbr[], ibr[]) to be reset. The two
970 * arrays are also used by perfmon, but we do not use
971 * IA64_THREAD_DBG_VALID. The registers are restored by the PMU context
974 if (pfm_use_debug_registers(child)) return -1;
977 if (!(child->thread.flags & IA64_THREAD_DBG_VALID)) {
978 child->thread.flags |= IA64_THREAD_DBG_VALID;
979 memset(child->thread.dbr, 0, sizeof(child->thread.dbr));
980 memset(child->thread.ibr, 0, sizeof(child->thread.ibr));
986 /* don't let the user set kernel-level breakpoints... */
987 *ptr = *data & ~(7UL << 56);
1000 ptrace_getregs (struct task_struct *child, struct pt_all_user_regs *ppr)
1002 struct switch_stack *sw;
1005 struct unw_frame_info info;
1009 retval = verify_area(VERIFY_WRITE, ppr, sizeof(struct pt_all_user_regs));
1014 pt = ia64_task_regs(child);
1015 sw = (struct switch_stack *) (child->thread.ksp + 16);
1016 unw_init_from_blocked_task(&info, child);
1017 if (unw_unwind_to_user(&info) < 0) {
1021 if (((unsigned long) ppr & 0x7) != 0) {
1022 dprintk("ptrace:unaligned register address %p\n", ppr);
1030 retval |= __put_user(pt->cr_iip, &ppr->cr_iip);
1031 retval |= access_uarea(child, PT_CR_IPSR, &ppr->cr_ipsr, 0);
1035 retval |= __put_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
1036 retval |= __put_user(pt->ar_rsc, &ppr->ar[PT_AUR_RSC]);
1037 retval |= __put_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
1038 retval |= __put_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
1039 retval |= __put_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
1040 retval |= __put_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
1042 retval |= access_uarea(child, PT_AR_EC, &ppr->ar[PT_AUR_EC], 0);
1043 retval |= access_uarea(child, PT_AR_LC, &ppr->ar[PT_AUR_LC], 0);
1044 retval |= access_uarea(child, PT_AR_RNAT, &ppr->ar[PT_AUR_RNAT], 0);
1045 retval |= access_uarea(child, PT_AR_BSP, &ppr->ar[PT_AUR_BSP], 0);
1046 retval |= access_uarea(child, PT_CFM, &ppr->cfm, 0);
1050 retval |= __copy_to_user(&ppr->gr[1], &pt->r1, sizeof(long));
1051 retval |= __copy_to_user(&ppr->gr[2], &pt->r2, sizeof(long) *2);
1055 for (i = 4; i < 8; i++) {
1056 retval |= unw_access_gr(&info, i, &ppr->gr[i], &nat, 0);
1061 retval |= __copy_to_user(&ppr->gr[8], &pt->r8, sizeof(long) * 4);
1065 retval |= __copy_to_user(&ppr->gr[12], &pt->r12, sizeof(long) * 2);
1066 retval |= __copy_to_user(&ppr->gr[14], &pt->r14, sizeof(long));
1067 retval |= __copy_to_user(&ppr->gr[15], &pt->r15, sizeof(long));
1071 retval |= __copy_to_user(&ppr->gr[16], &pt->r16, sizeof(long) * 16);
1075 retval |= __put_user(pt->b0, &ppr->br[0]);
1079 for (i = 1; i < 6; i++) {
1080 retval |= unw_access_br(&info, i, &ppr->br[i], 0);
1085 retval |= __put_user(pt->b6, &ppr->br[6]);
1086 retval |= __put_user(pt->b7, &ppr->br[7]);
1090 for (i = 2; i < 6; i++) {
1091 retval |= access_fr(&info, i, 0, (unsigned long *) &ppr->fr[i], 0);
1092 retval |= access_fr(&info, i, 1, (unsigned long *) &ppr->fr[i] + 1, 0);
1097 retval |= __copy_to_user(&ppr->fr[6], &pt->f6, sizeof(struct ia64_fpreg) * 6);
1099 /* fp scratch regs(12-15) */
1101 retval |= __copy_to_user(&ppr->fr[12], &sw->f12, sizeof(struct ia64_fpreg) * 4);
1105 for (i = 16; i < 32; i++) {
1106 retval |= access_fr(&info, i, 0, (unsigned long *) &ppr->fr[i], 0);
1107 retval |= access_fr(&info, i, 1, (unsigned long *) &ppr->fr[i] + 1, 0);
1112 ia64_flush_fph(child);
1113 retval |= __copy_to_user(&ppr->fr[32], &child->thread.fph, sizeof(ppr->fr[32]) * 96);
1117 retval |= __put_user(pt->pr, &ppr->pr);
1121 retval |= access_uarea(child, PT_NAT_BITS, &ppr->nat, 0);
1123 ret = retval ? -EIO : 0;
1128 ptrace_setregs (struct task_struct *child, struct pt_all_user_regs *ppr)
1130 struct switch_stack *sw;
1133 struct unw_frame_info info;
1137 retval = verify_area(VERIFY_READ, ppr, sizeof(struct pt_all_user_regs));
1142 pt = ia64_task_regs(child);
1143 sw = (struct switch_stack *) (child->thread.ksp + 16);
1144 unw_init_from_blocked_task(&info, child);
1145 if (unw_unwind_to_user(&info) < 0) {
1149 if (((unsigned long) ppr & 0x7) != 0) {
1150 dprintk("ptrace:unaligned register address %p\n", ppr);
1158 retval |= __get_user(pt->cr_iip, &ppr->cr_iip);
1159 retval |= access_uarea(child, PT_CR_IPSR, &ppr->cr_ipsr, 1);
1163 retval |= __get_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
1164 retval |= __get_user(pt->ar_rsc, &ppr->ar[PT_AUR_RSC]);
1165 retval |= __get_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
1166 retval |= __get_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
1167 retval |= __get_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
1168 retval |= __get_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
1170 retval |= access_uarea(child, PT_AR_EC, &ppr->ar[PT_AUR_EC], 1);
1171 retval |= access_uarea(child, PT_AR_LC, &ppr->ar[PT_AUR_LC], 1);
1172 retval |= access_uarea(child, PT_AR_RNAT, &ppr->ar[PT_AUR_RNAT], 1);
1173 retval |= access_uarea(child, PT_AR_BSP, &ppr->ar[PT_AUR_BSP], 1);
1174 retval |= access_uarea(child, PT_CFM, &ppr->cfm, 1);
1178 retval |= __copy_from_user(&pt->r1, &ppr->gr[1], sizeof(long));
1179 retval |= __copy_from_user(&pt->r2, &ppr->gr[2], sizeof(long) * 2);
1183 for (i = 4; i < 8; i++) {
1184 long ret = unw_get_gr(&info, i, &ppr->gr[i], &nat);
1188 retval |= unw_access_gr(&info, i, &ppr->gr[i], &nat, 1);
1193 retval |= __copy_from_user(&pt->r8, &ppr->gr[8], sizeof(long) * 4);
1197 retval |= __copy_from_user(&pt->r12, &ppr->gr[12], sizeof(long) * 2);
1198 retval |= __copy_from_user(&pt->r14, &ppr->gr[14], sizeof(long));
1199 retval |= __copy_from_user(&pt->r15, &ppr->gr[15], sizeof(long));
1203 retval |= __copy_from_user(&pt->r16, &ppr->gr[16], sizeof(long) * 16);
1207 retval |= __get_user(pt->b0, &ppr->br[0]);
1211 for (i = 1; i < 6; i++) {
1212 retval |= unw_access_br(&info, i, &ppr->br[i], 1);
1217 retval |= __get_user(pt->b6, &ppr->br[6]);
1218 retval |= __get_user(pt->b7, &ppr->br[7]);
1222 for (i = 2; i < 6; i++) {
1223 retval |= access_fr(&info, i, 0, (unsigned long *) &ppr->fr[i], 1);
1224 retval |= access_fr(&info, i, 1, (unsigned long *) &ppr->fr[i] + 1, 1);
1229 retval |= __copy_from_user(&pt->f6, &ppr->fr[6], sizeof(ppr->fr[6]) * 6);
1231 /* fp scratch regs(12-15) */
1233 retval |= __copy_from_user(&sw->f12, &ppr->fr[12], sizeof(ppr->fr[12]) * 4);
1237 for (i = 16; i < 32; i++) {
1238 retval |= access_fr(&info, i, 0, (unsigned long *) &ppr->fr[i], 1);
1239 retval |= access_fr(&info, i, 1, (unsigned long *) &ppr->fr[i] + 1, 1);
1244 ia64_sync_fph(child);
1245 retval |= __copy_from_user(&child->thread.fph, &ppr->fr[32], sizeof(ppr->fr[32]) * 96);
1249 retval |= __get_user(pt->pr, &ppr->pr);
1253 retval |= access_uarea(child, PT_NAT_BITS, &ppr->nat, 1);
1255 ret = retval ? -EIO : 0;
1260 * Called by kernel/ptrace.c when detaching..
1262 * Make sure the single step bit is not set.
1265 ptrace_disable (struct task_struct *child)
1267 struct ia64_psr *child_psr = ia64_psr(ia64_task_regs(child));
1269 /* make sure the single step/take-branch tra bits are not set: */
1275 sys_ptrace (long request, pid_t pid, unsigned long addr, unsigned long data,
1276 long arg4, long arg5, long arg6, long arg7, long stack)
1278 struct pt_regs *pt, *regs = (struct pt_regs *) &stack;
1279 unsigned long urbs_end, peek_or_poke;
1280 struct task_struct *child;
1281 struct switch_stack *sw;
1286 if (request == PTRACE_TRACEME) {
1287 /* are we already being traced? */
1288 if (current->ptrace & PT_PTRACED)
1290 ret = security_ptrace(current->parent, current);
1293 current->ptrace |= PT_PTRACED;
1298 peek_or_poke = (request == PTRACE_PEEKTEXT || request == PTRACE_PEEKDATA
1299 || request == PTRACE_POKETEXT || request == PTRACE_POKEDATA);
1301 read_lock(&tasklist_lock);
1303 child = find_task_by_pid(pid);
1306 child = find_thread_for_addr(child, addr);
1307 get_task_struct(child);
1310 read_unlock(&tasklist_lock);
1313 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
1317 if (pid == 1) /* no messing around with init! */
1320 if (request == PTRACE_ATTACH) {
1321 ret = ptrace_attach(child);
1325 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1329 pt = ia64_task_regs(child);
1330 sw = (struct switch_stack *) (child->thread.ksp + 16);
1333 case PTRACE_PEEKTEXT:
1334 case PTRACE_PEEKDATA: /* read word at location addr */
1335 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
1336 ret = ia64_peek(child, sw, urbs_end, addr, &data);
1339 regs->r8 = 0; /* ensure "ret" is not mistaken as an error code */
1343 case PTRACE_POKETEXT:
1344 case PTRACE_POKEDATA: /* write the word at location addr */
1345 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
1346 ret = ia64_poke(child, sw, urbs_end, addr, data);
1349 case PTRACE_PEEKUSR: /* read the word at addr in the USER area */
1350 if (access_uarea(child, addr, &data, 0) < 0) {
1355 regs->r8 = 0; /* ensure "ret" is not mistaken as an error code */
1358 case PTRACE_POKEUSR: /* write the word at addr in the USER area */
1359 if (access_uarea(child, addr, &data, 1) < 0) {
1366 case PTRACE_OLD_GETSIGINFO: /* for backwards-compatibility */
1367 ret = ptrace_request(child, PTRACE_GETSIGINFO, addr, data);
1370 case PTRACE_OLD_SETSIGINFO: /* for backwards-compatibility */
1371 ret = ptrace_request(child, PTRACE_SETSIGINFO, addr, data);
1374 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
1375 case PTRACE_CONT: /* restart after signal. */
1379 if (request == PTRACE_SYSCALL)
1380 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1382 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1383 child->exit_code = data;
1385 /* make sure the single step/taken-branch trap bits are not set: */
1386 ia64_psr(pt)->ss = 0;
1387 ia64_psr(pt)->tb = 0;
1389 wake_up_process(child);
1395 * Make the child exit. Best I can do is send it a
1396 * sigkill. Perhaps it should be put in the status
1397 * that it wants to exit.
1399 if (child->state == TASK_ZOMBIE) /* already dead */
1401 child->exit_code = SIGKILL;
1403 /* make sure the single step/take-branch tra bits are not set: */
1404 ia64_psr(pt)->ss = 0;
1405 ia64_psr(pt)->tb = 0;
1407 wake_up_process(child);
1411 case PTRACE_SINGLESTEP: /* let child execute for one instruction */
1412 case PTRACE_SINGLEBLOCK:
1417 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1418 if (request == PTRACE_SINGLESTEP) {
1419 ia64_psr(pt)->ss = 1;
1421 ia64_psr(pt)->tb = 1;
1423 child->exit_code = data;
1425 /* give it a chance to run. */
1426 wake_up_process(child);
1430 case PTRACE_DETACH: /* detach a process that was attached. */
1431 ret = ptrace_detach(child, data);
1434 case PTRACE_GETREGS:
1435 ret = ptrace_getregs(child, (struct pt_all_user_regs*) data);
1438 case PTRACE_SETREGS:
1439 ret = ptrace_setregs(child, (struct pt_all_user_regs*) data);
1443 ret = ptrace_request(child, request, addr, data);
1447 put_task_struct(child);
1455 syscall_trace (void)
1457 if (!test_thread_flag(TIF_SYSCALL_TRACE))
1459 if (!(current->ptrace & PT_PTRACED))
1462 * The 0x80 provides a way for the tracing parent to distinguish between a syscall
1463 * stop and SIGTRAP delivery.
1465 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
1468 * This isn't the same as continuing with a signal, but it will do for normal use.
1469 * strace only continues with a signal if the stopping signal is not SIGTRAP.
1472 if (current->exit_code) {
1473 send_sig(current->exit_code, current, 1);
1474 current->exit_code = 0;
1478 /* "asmlinkage" so the input arguments are preserved... */
1481 syscall_trace_enter (long arg0, long arg1, long arg2, long arg3,
1482 long arg4, long arg5, long arg6, long arg7, long stack)
1484 struct pt_regs *regs = (struct pt_regs *) &stack;
1487 if (unlikely(current->audit_context)) {
1488 if (IS_IA32_PROCESS(regs))
1491 syscall = regs->r15;
1493 audit_syscall_entry(current, syscall, arg0, arg1, arg2, arg3);
1496 if (test_thread_flag(TIF_SYSCALL_TRACE) && (current->ptrace & PT_PTRACED))
1500 /* "asmlinkage" so the input arguments are preserved... */
1503 syscall_trace_leave (long arg0, long arg1, long arg2, long arg3,
1504 long arg4, long arg5, long arg6, long arg7, long stack)
1506 if (unlikely(current->audit_context))
1507 audit_syscall_exit(current, ((struct pt_regs *) &stack)->r8);
1509 if (test_thread_flag(TIF_SYSCALL_TRACE) && (current->ptrace & PT_PTRACED))