]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/ia64/kernel/ptrace.c
2de5a524a0ee463037d165d3a8df295f47029ed6
[linux-2.6-omap-h63xx.git] / arch / ia64 / kernel / ptrace.c
1 /*
2  * Kernel support for the ptrace() and syscall tracing interfaces.
3  *
4  * Copyright (C) 1999-2005 Hewlett-Packard Co
5  *      David Mosberger-Tang <davidm@hpl.hp.com>
6  *
7  * Derived from the x86 and Alpha versions.
8  */
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/mm.h>
13 #include <linux/errno.h>
14 #include <linux/ptrace.h>
15 #include <linux/smp_lock.h>
16 #include <linux/user.h>
17 #include <linux/security.h>
18 #include <linux/audit.h>
19 #include <linux/signal.h>
20
21 #include <asm/pgtable.h>
22 #include <asm/processor.h>
23 #include <asm/ptrace_offsets.h>
24 #include <asm/rse.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
27 #include <asm/unwind.h>
28 #ifdef CONFIG_PERFMON
29 #include <asm/perfmon.h>
30 #endif
31
32 #include "entry.h"
33
34 /*
35  * Bits in the PSR that we allow ptrace() to change:
36  *      be, up, ac, mfl, mfh (the user mask; five bits total)
37  *      db (debug breakpoint fault; one bit)
38  *      id (instruction debug fault disable; one bit)
39  *      dd (data debug fault disable; one bit)
40  *      ri (restart instruction; two bits)
41  *      is (instruction set; one bit)
42  */
43 #define IPSR_MASK (IA64_PSR_UM | IA64_PSR_DB | IA64_PSR_IS      \
44                    | IA64_PSR_ID | IA64_PSR_DD | IA64_PSR_RI)
45
46 #define MASK(nbits)     ((1UL << (nbits)) - 1)  /* mask with NBITS bits set */
47 #define PFM_MASK        MASK(38)
48
49 #define PTRACE_DEBUG    0
50
51 #if PTRACE_DEBUG
52 # define dprintk(format...)     printk(format)
53 # define inline
54 #else
55 # define dprintk(format...)
56 #endif
57
58 /* Return TRUE if PT was created due to kernel-entry via a system-call.  */
59
60 static inline int
61 in_syscall (struct pt_regs *pt)
62 {
63         return (long) pt->cr_ifs >= 0;
64 }
65
66 /*
67  * Collect the NaT bits for r1-r31 from scratch_unat and return a NaT
68  * bitset where bit i is set iff the NaT bit of register i is set.
69  */
70 unsigned long
71 ia64_get_scratch_nat_bits (struct pt_regs *pt, unsigned long scratch_unat)
72 {
73 #       define GET_BITS(first, last, unat)                              \
74         ({                                                              \
75                 unsigned long bit = ia64_unat_pos(&pt->r##first);       \
76                 unsigned long nbits = (last - first + 1);               \
77                 unsigned long mask = MASK(nbits) << first;              \
78                 unsigned long dist;                                     \
79                 if (bit < first)                                        \
80                         dist = 64 + bit - first;                        \
81                 else                                                    \
82                         dist = bit - first;                             \
83                 ia64_rotr(unat, dist) & mask;                           \
84         })
85         unsigned long val;
86
87         /*
88          * Registers that are stored consecutively in struct pt_regs
89          * can be handled in parallel.  If the register order in
90          * struct_pt_regs changes, this code MUST be updated.
91          */
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);
99         return val;
100
101 #       undef GET_BITS
102 }
103
104 /*
105  * Set the NaT bits for the scratch registers according to NAT and
106  * return the resulting unat (assuming the scratch registers are
107  * stored in PT).
108  */
109 unsigned long
110 ia64_put_scratch_nat_bits (struct pt_regs *pt, unsigned long nat)
111 {
112 #       define PUT_BITS(first, last, nat)                               \
113         ({                                                              \
114                 unsigned long bit = ia64_unat_pos(&pt->r##first);       \
115                 unsigned long nbits = (last - first + 1);               \
116                 unsigned long mask = MASK(nbits) << first;              \
117                 long dist;                                              \
118                 if (bit < first)                                        \
119                         dist = 64 + bit - first;                        \
120                 else                                                    \
121                         dist = bit - first;                             \
122                 ia64_rotl(nat & mask, dist);                            \
123         })
124         unsigned long scratch_unat;
125
126         /*
127          * Registers that are stored consecutively in struct pt_regs
128          * can be handled in parallel.  If the register order in
129          * struct_pt_regs changes, this code MUST be updated.
130          */
131         scratch_unat  = PUT_BITS( 1,  1, nat);
132         scratch_unat |= PUT_BITS( 2,  3, nat);
133         scratch_unat |= PUT_BITS(12, 13, nat);
134         scratch_unat |= PUT_BITS(14, 14, nat);
135         scratch_unat |= PUT_BITS(15, 15, nat);
136         scratch_unat |= PUT_BITS( 8, 11, nat);
137         scratch_unat |= PUT_BITS(16, 31, nat);
138
139         return scratch_unat;
140
141 #       undef PUT_BITS
142 }
143
144 #define IA64_MLX_TEMPLATE       0x2
145 #define IA64_MOVL_OPCODE        6
146
147 void
148 ia64_increment_ip (struct pt_regs *regs)
149 {
150         unsigned long w0, ri = ia64_psr(regs)->ri + 1;
151
152         if (ri > 2) {
153                 ri = 0;
154                 regs->cr_iip += 16;
155         } else if (ri == 2) {
156                 get_user(w0, (char __user *) regs->cr_iip + 0);
157                 if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
158                         /*
159                          * rfi'ing to slot 2 of an MLX bundle causes
160                          * an illegal operation fault.  We don't want
161                          * that to happen...
162                          */
163                         ri = 0;
164                         regs->cr_iip += 16;
165                 }
166         }
167         ia64_psr(regs)->ri = ri;
168 }
169
170 void
171 ia64_decrement_ip (struct pt_regs *regs)
172 {
173         unsigned long w0, ri = ia64_psr(regs)->ri - 1;
174
175         if (ia64_psr(regs)->ri == 0) {
176                 regs->cr_iip -= 16;
177                 ri = 2;
178                 get_user(w0, (char __user *) regs->cr_iip + 0);
179                 if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
180                         /*
181                          * rfi'ing to slot 2 of an MLX bundle causes
182                          * an illegal operation fault.  We don't want
183                          * that to happen...
184                          */
185                         ri = 1;
186                 }
187         }
188         ia64_psr(regs)->ri = ri;
189 }
190
191 /*
192  * This routine is used to read an rnat bits that are stored on the
193  * kernel backing store.  Since, in general, the alignment of the user
194  * and kernel are different, this is not completely trivial.  In
195  * essence, we need to construct the user RNAT based on up to two
196  * kernel RNAT values and/or the RNAT value saved in the child's
197  * pt_regs.
198  *
199  * user rbs
200  *
201  * +--------+ <-- lowest address
202  * | slot62 |
203  * +--------+
204  * |  rnat  | 0x....1f8
205  * +--------+
206  * | slot00 | \
207  * +--------+ |
208  * | slot01 | > child_regs->ar_rnat
209  * +--------+ |
210  * | slot02 | /                         kernel rbs
211  * +--------+                           +--------+
212  *          <- child_regs->ar_bspstore  | slot61 | <-- krbs
213  * +- - - - +                           +--------+
214  *                                      | slot62 |
215  * +- - - - +                           +--------+
216  *                                      |  rnat  |
217  * +- - - - +                           +--------+
218  *   vrnat                              | slot00 |
219  * +- - - - +                           +--------+
220  *                                      =        =
221  *                                      +--------+
222  *                                      | slot00 | \
223  *                                      +--------+ |
224  *                                      | slot01 | > child_stack->ar_rnat
225  *                                      +--------+ |
226  *                                      | slot02 | /
227  *                                      +--------+
228  *                                                <--- child_stack->ar_bspstore
229  *
230  * The way to think of this code is as follows: bit 0 in the user rnat
231  * corresponds to some bit N (0 <= N <= 62) in one of the kernel rnat
232  * value.  The kernel rnat value holding this bit is stored in
233  * variable rnat0.  rnat1 is loaded with the kernel rnat value that
234  * form the upper bits of the user rnat value.
235  *
236  * Boundary cases:
237  *
238  * o when reading the rnat "below" the first rnat slot on the kernel
239  *   backing store, rnat0/rnat1 are set to 0 and the low order bits are
240  *   merged in from pt->ar_rnat.
241  *
242  * o when reading the rnat "above" the last rnat slot on the kernel
243  *   backing store, rnat0/rnat1 gets its value from sw->ar_rnat.
244  */
245 static unsigned long
246 get_rnat (struct task_struct *task, struct switch_stack *sw,
247           unsigned long *krbs, unsigned long *urnat_addr,
248           unsigned long *urbs_end)
249 {
250         unsigned long rnat0 = 0, rnat1 = 0, urnat = 0, *slot0_kaddr;
251         unsigned long umask = 0, mask, m;
252         unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
253         long num_regs, nbits;
254         struct pt_regs *pt;
255
256         pt = task_pt_regs(task);
257         kbsp = (unsigned long *) sw->ar_bspstore;
258         ubspstore = (unsigned long *) pt->ar_bspstore;
259
260         if (urbs_end < urnat_addr)
261                 nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_end);
262         else
263                 nbits = 63;
264         mask = MASK(nbits);
265         /*
266          * First, figure out which bit number slot 0 in user-land maps
267          * to in the kernel rnat.  Do this by figuring out how many
268          * register slots we're beyond the user's backingstore and
269          * then computing the equivalent address in kernel space.
270          */
271         num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
272         slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
273         shift = ia64_rse_slot_num(slot0_kaddr);
274         rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
275         rnat0_kaddr = rnat1_kaddr - 64;
276
277         if (ubspstore + 63 > urnat_addr) {
278                 /* some bits need to be merged in from pt->ar_rnat */
279                 umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;
280                 urnat = (pt->ar_rnat & umask);
281                 mask &= ~umask;
282                 if (!mask)
283                         return urnat;
284         }
285
286         m = mask << shift;
287         if (rnat0_kaddr >= kbsp)
288                 rnat0 = sw->ar_rnat;
289         else if (rnat0_kaddr > krbs)
290                 rnat0 = *rnat0_kaddr;
291         urnat |= (rnat0 & m) >> shift;
292
293         m = mask >> (63 - shift);
294         if (rnat1_kaddr >= kbsp)
295                 rnat1 = sw->ar_rnat;
296         else if (rnat1_kaddr > krbs)
297                 rnat1 = *rnat1_kaddr;
298         urnat |= (rnat1 & m) << (63 - shift);
299         return urnat;
300 }
301
302 /*
303  * The reverse of get_rnat.
304  */
305 static void
306 put_rnat (struct task_struct *task, struct switch_stack *sw,
307           unsigned long *krbs, unsigned long *urnat_addr, unsigned long urnat,
308           unsigned long *urbs_end)
309 {
310         unsigned long rnat0 = 0, rnat1 = 0, *slot0_kaddr, umask = 0, mask, m;
311         unsigned long *kbsp, *ubspstore, *rnat0_kaddr, *rnat1_kaddr, shift;
312         long num_regs, nbits;
313         struct pt_regs *pt;
314         unsigned long cfm, *urbs_kargs;
315
316         pt = task_pt_regs(task);
317         kbsp = (unsigned long *) sw->ar_bspstore;
318         ubspstore = (unsigned long *) pt->ar_bspstore;
319
320         urbs_kargs = urbs_end;
321         if (in_syscall(pt)) {
322                 /*
323                  * If entered via syscall, don't allow user to set rnat bits
324                  * for syscall args.
325                  */
326                 cfm = pt->cr_ifs;
327                 urbs_kargs = ia64_rse_skip_regs(urbs_end, -(cfm & 0x7f));
328         }
329
330         if (urbs_kargs >= urnat_addr)
331                 nbits = 63;
332         else {
333                 if ((urnat_addr - 63) >= urbs_kargs)
334                         return;
335                 nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_kargs);
336         }
337         mask = MASK(nbits);
338
339         /*
340          * First, figure out which bit number slot 0 in user-land maps
341          * to in the kernel rnat.  Do this by figuring out how many
342          * register slots we're beyond the user's backingstore and
343          * then computing the equivalent address in kernel space.
344          */
345         num_regs = ia64_rse_num_regs(ubspstore, urnat_addr + 1);
346         slot0_kaddr = ia64_rse_skip_regs(krbs, num_regs);
347         shift = ia64_rse_slot_num(slot0_kaddr);
348         rnat1_kaddr = ia64_rse_rnat_addr(slot0_kaddr);
349         rnat0_kaddr = rnat1_kaddr - 64;
350
351         if (ubspstore + 63 > urnat_addr) {
352                 /* some bits need to be place in pt->ar_rnat: */
353                 umask = MASK(ia64_rse_slot_num(ubspstore)) & mask;
354                 pt->ar_rnat = (pt->ar_rnat & ~umask) | (urnat & umask);
355                 mask &= ~umask;
356                 if (!mask)
357                         return;
358         }
359         /*
360          * Note: Section 11.1 of the EAS guarantees that bit 63 of an
361          * rnat slot is ignored. so we don't have to clear it here.
362          */
363         rnat0 = (urnat << shift);
364         m = mask << shift;
365         if (rnat0_kaddr >= kbsp)
366                 sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat0 & m);
367         else if (rnat0_kaddr > krbs)
368                 *rnat0_kaddr = ((*rnat0_kaddr & ~m) | (rnat0 & m));
369
370         rnat1 = (urnat >> (63 - shift));
371         m = mask >> (63 - shift);
372         if (rnat1_kaddr >= kbsp)
373                 sw->ar_rnat = (sw->ar_rnat & ~m) | (rnat1 & m);
374         else if (rnat1_kaddr > krbs)
375                 *rnat1_kaddr = ((*rnat1_kaddr & ~m) | (rnat1 & m));
376 }
377
378 static inline int
379 on_kernel_rbs (unsigned long addr, unsigned long bspstore,
380                unsigned long urbs_end)
381 {
382         unsigned long *rnat_addr = ia64_rse_rnat_addr((unsigned long *)
383                                                       urbs_end);
384         return (addr >= bspstore && addr <= (unsigned long) rnat_addr);
385 }
386
387 /*
388  * Read a word from the user-level backing store of task CHILD.  ADDR
389  * is the user-level address to read the word from, VAL a pointer to
390  * the return value, and USER_BSP gives the end of the user-level
391  * backing store (i.e., it's the address that would be in ar.bsp after
392  * the user executed a "cover" instruction).
393  *
394  * This routine takes care of accessing the kernel register backing
395  * store for those registers that got spilled there.  It also takes
396  * care of calculating the appropriate RNaT collection words.
397  */
398 long
399 ia64_peek (struct task_struct *child, struct switch_stack *child_stack,
400            unsigned long user_rbs_end, unsigned long addr, long *val)
401 {
402         unsigned long *bspstore, *krbs, regnum, *laddr, *urbs_end, *rnat_addr;
403         struct pt_regs *child_regs;
404         size_t copied;
405         long ret;
406
407         urbs_end = (long *) user_rbs_end;
408         laddr = (unsigned long *) addr;
409         child_regs = task_pt_regs(child);
410         bspstore = (unsigned long *) child_regs->ar_bspstore;
411         krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
412         if (on_kernel_rbs(addr, (unsigned long) bspstore,
413                           (unsigned long) urbs_end))
414         {
415                 /*
416                  * Attempt to read the RBS in an area that's actually
417                  * on the kernel RBS => read the corresponding bits in
418                  * the kernel RBS.
419                  */
420                 rnat_addr = ia64_rse_rnat_addr(laddr);
421                 ret = get_rnat(child, child_stack, krbs, rnat_addr, urbs_end);
422
423                 if (laddr == rnat_addr) {
424                         /* return NaT collection word itself */
425                         *val = ret;
426                         return 0;
427                 }
428
429                 if (((1UL << ia64_rse_slot_num(laddr)) & ret) != 0) {
430                         /*
431                          * It is implementation dependent whether the
432                          * data portion of a NaT value gets saved on a
433                          * st8.spill or RSE spill (e.g., see EAS 2.6,
434                          * 4.4.4.6 Register Spill and Fill).  To get
435                          * consistent behavior across all possible
436                          * IA-64 implementations, we return zero in
437                          * this case.
438                          */
439                         *val = 0;
440                         return 0;
441                 }
442
443                 if (laddr < urbs_end) {
444                         /*
445                          * The desired word is on the kernel RBS and
446                          * is not a NaT.
447                          */
448                         regnum = ia64_rse_num_regs(bspstore, laddr);
449                         *val = *ia64_rse_skip_regs(krbs, regnum);
450                         return 0;
451                 }
452         }
453         copied = access_process_vm(child, addr, &ret, sizeof(ret), 0);
454         if (copied != sizeof(ret))
455                 return -EIO;
456         *val = ret;
457         return 0;
458 }
459
460 long
461 ia64_poke (struct task_struct *child, struct switch_stack *child_stack,
462            unsigned long user_rbs_end, unsigned long addr, long val)
463 {
464         unsigned long *bspstore, *krbs, regnum, *laddr;
465         unsigned long *urbs_end = (long *) user_rbs_end;
466         struct pt_regs *child_regs;
467
468         laddr = (unsigned long *) addr;
469         child_regs = task_pt_regs(child);
470         bspstore = (unsigned long *) child_regs->ar_bspstore;
471         krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
472         if (on_kernel_rbs(addr, (unsigned long) bspstore,
473                           (unsigned long) urbs_end))
474         {
475                 /*
476                  * Attempt to write the RBS in an area that's actually
477                  * on the kernel RBS => write the corresponding bits
478                  * in the kernel RBS.
479                  */
480                 if (ia64_rse_is_rnat_slot(laddr))
481                         put_rnat(child, child_stack, krbs, laddr, val,
482                                  urbs_end);
483                 else {
484                         if (laddr < urbs_end) {
485                                 regnum = ia64_rse_num_regs(bspstore, laddr);
486                                 *ia64_rse_skip_regs(krbs, regnum) = val;
487                         }
488                 }
489         } else if (access_process_vm(child, addr, &val, sizeof(val), 1)
490                    != sizeof(val))
491                 return -EIO;
492         return 0;
493 }
494
495 /*
496  * Calculate the address of the end of the user-level register backing
497  * store.  This is the address that would have been stored in ar.bsp
498  * if the user had executed a "cover" instruction right before
499  * entering the kernel.  If CFMP is not NULL, it is used to return the
500  * "current frame mask" that was active at the time the kernel was
501  * entered.
502  */
503 unsigned long
504 ia64_get_user_rbs_end (struct task_struct *child, struct pt_regs *pt,
505                        unsigned long *cfmp)
506 {
507         unsigned long *krbs, *bspstore, cfm = pt->cr_ifs;
508         long ndirty;
509
510         krbs = (unsigned long *) child + IA64_RBS_OFFSET/8;
511         bspstore = (unsigned long *) pt->ar_bspstore;
512         ndirty = ia64_rse_num_regs(krbs, krbs + (pt->loadrs >> 19));
513
514         if (in_syscall(pt))
515                 ndirty += (cfm & 0x7f);
516         else
517                 cfm &= ~(1UL << 63);    /* clear valid bit */
518
519         if (cfmp)
520                 *cfmp = cfm;
521         return (unsigned long) ia64_rse_skip_regs(bspstore, ndirty);
522 }
523
524 /*
525  * Synchronize (i.e, write) the RSE backing store living in kernel
526  * space to the VM of the CHILD task.  SW and PT are the pointers to
527  * the switch_stack and pt_regs structures, respectively.
528  * USER_RBS_END is the user-level address at which the backing store
529  * ends.
530  */
531 long
532 ia64_sync_user_rbs (struct task_struct *child, struct switch_stack *sw,
533                     unsigned long user_rbs_start, unsigned long user_rbs_end)
534 {
535         unsigned long addr, val;
536         long ret;
537
538         /* now copy word for word from kernel rbs to user rbs: */
539         for (addr = user_rbs_start; addr < user_rbs_end; addr += 8) {
540                 ret = ia64_peek(child, sw, user_rbs_end, addr, &val);
541                 if (ret < 0)
542                         return ret;
543                 if (access_process_vm(child, addr, &val, sizeof(val), 1)
544                     != sizeof(val))
545                         return -EIO;
546         }
547         return 0;
548 }
549
550 static long
551 ia64_sync_kernel_rbs (struct task_struct *child, struct switch_stack *sw,
552                 unsigned long user_rbs_start, unsigned long user_rbs_end)
553 {
554         unsigned long addr, val;
555         long ret;
556
557         /* now copy word for word from user rbs to kernel rbs: */
558         for (addr = user_rbs_start; addr < user_rbs_end; addr += 8) {
559                 if (access_process_vm(child, addr, &val, sizeof(val), 0)
560                                 != sizeof(val))
561                         return -EIO;
562
563                 ret = ia64_poke(child, sw, user_rbs_end, addr, val);
564                 if (ret < 0)
565                         return ret;
566         }
567         return 0;
568 }
569
570 typedef long (*syncfunc_t)(struct task_struct *, struct switch_stack *,
571                             unsigned long, unsigned long);
572
573 static void do_sync_rbs(struct unw_frame_info *info, void *arg)
574 {
575         struct pt_regs *pt;
576         unsigned long urbs_end;
577         syncfunc_t fn = arg;
578
579         if (unw_unwind_to_user(info) < 0)
580                 return;
581         pt = task_pt_regs(info->task);
582         urbs_end = ia64_get_user_rbs_end(info->task, pt, NULL);
583
584         fn(info->task, info->sw, pt->ar_bspstore, urbs_end);
585 }
586
587 /*
588  * when a thread is stopped (ptraced), debugger might change thread's user
589  * stack (change memory directly), and we must avoid the RSE stored in kernel
590  * to override user stack (user space's RSE is newer than kernel's in the
591  * case). To workaround the issue, we copy kernel RSE to user RSE before the
592  * task is stopped, so user RSE has updated data.  we then copy user RSE to
593  * kernel after the task is resummed from traced stop and kernel will use the
594  * newer RSE to return to user. TIF_RESTORE_RSE is the flag to indicate we need
595  * synchronize user RSE to kernel.
596  */
597 void ia64_ptrace_stop(void)
598 {
599         if (test_and_set_tsk_thread_flag(current, TIF_RESTORE_RSE))
600                 return;
601         tsk_set_notify_resume(current);
602         unw_init_running(do_sync_rbs, ia64_sync_user_rbs);
603 }
604
605 /*
606  * This is called to read back the register backing store.
607  */
608 void ia64_sync_krbs(void)
609 {
610         clear_tsk_thread_flag(current, TIF_RESTORE_RSE);
611         tsk_clear_notify_resume(current);
612
613         unw_init_running(do_sync_rbs, ia64_sync_kernel_rbs);
614 }
615
616 static inline int
617 thread_matches (struct task_struct *thread, unsigned long addr)
618 {
619         unsigned long thread_rbs_end;
620         struct pt_regs *thread_regs;
621
622         if (ptrace_check_attach(thread, 0) < 0)
623                 /*
624                  * If the thread is not in an attachable state, we'll
625                  * ignore it.  The net effect is that if ADDR happens
626                  * to overlap with the portion of the thread's
627                  * register backing store that is currently residing
628                  * on the thread's kernel stack, then ptrace() may end
629                  * up accessing a stale value.  But if the thread
630                  * isn't stopped, that's a problem anyhow, so we're
631                  * doing as well as we can...
632                  */
633                 return 0;
634
635         thread_regs = task_pt_regs(thread);
636         thread_rbs_end = ia64_get_user_rbs_end(thread, thread_regs, NULL);
637         if (!on_kernel_rbs(addr, thread_regs->ar_bspstore, thread_rbs_end))
638                 return 0;
639
640         return 1;       /* looks like we've got a winner */
641 }
642
643 /*
644  * GDB apparently wants to be able to read the register-backing store
645  * of any thread when attached to a given process.  If we are peeking
646  * or poking an address that happens to reside in the kernel-backing
647  * store of another thread, we need to attach to that thread, because
648  * otherwise we end up accessing stale data.
649  *
650  * task_list_lock must be read-locked before calling this routine!
651  */
652 static struct task_struct *
653 find_thread_for_addr (struct task_struct *child, unsigned long addr)
654 {
655         struct task_struct *p;
656         struct mm_struct *mm;
657         struct list_head *this, *next;
658         int mm_users;
659
660         if (!(mm = get_task_mm(child)))
661                 return child;
662
663         /* -1 because of our get_task_mm(): */
664         mm_users = atomic_read(&mm->mm_users) - 1;
665         if (mm_users <= 1)
666                 goto out;               /* not multi-threaded */
667
668         /*
669          * Traverse the current process' children list.  Every task that
670          * one attaches to becomes a child.  And it is only attached children
671          * of the debugger that are of interest (ptrace_check_attach checks
672          * for this).
673          */
674         list_for_each_safe(this, next, &current->children) {
675                 p = list_entry(this, struct task_struct, sibling);
676                 if (p->tgid != child->tgid)
677                         continue;
678                 if (thread_matches(p, addr)) {
679                         child = p;
680                         goto out;
681                 }
682         }
683
684   out:
685         mmput(mm);
686         return child;
687 }
688
689 /*
690  * Write f32-f127 back to task->thread.fph if it has been modified.
691  */
692 inline void
693 ia64_flush_fph (struct task_struct *task)
694 {
695         struct ia64_psr *psr = ia64_psr(task_pt_regs(task));
696
697         /*
698          * Prevent migrating this task while
699          * we're fiddling with the FPU state
700          */
701         preempt_disable();
702         if (ia64_is_local_fpu_owner(task) && psr->mfh) {
703                 psr->mfh = 0;
704                 task->thread.flags |= IA64_THREAD_FPH_VALID;
705                 ia64_save_fpu(&task->thread.fph[0]);
706         }
707         preempt_enable();
708 }
709
710 /*
711  * Sync the fph state of the task so that it can be manipulated
712  * through thread.fph.  If necessary, f32-f127 are written back to
713  * thread.fph or, if the fph state hasn't been used before, thread.fph
714  * is cleared to zeroes.  Also, access to f32-f127 is disabled to
715  * ensure that the task picks up the state from thread.fph when it
716  * executes again.
717  */
718 void
719 ia64_sync_fph (struct task_struct *task)
720 {
721         struct ia64_psr *psr = ia64_psr(task_pt_regs(task));
722
723         ia64_flush_fph(task);
724         if (!(task->thread.flags & IA64_THREAD_FPH_VALID)) {
725                 task->thread.flags |= IA64_THREAD_FPH_VALID;
726                 memset(&task->thread.fph, 0, sizeof(task->thread.fph));
727         }
728         ia64_drop_fpu(task);
729         psr->dfh = 1;
730 }
731
732 static int
733 access_fr (struct unw_frame_info *info, int regnum, int hi,
734            unsigned long *data, int write_access)
735 {
736         struct ia64_fpreg fpval;
737         int ret;
738
739         ret = unw_get_fr(info, regnum, &fpval);
740         if (ret < 0)
741                 return ret;
742
743         if (write_access) {
744                 fpval.u.bits[hi] = *data;
745                 ret = unw_set_fr(info, regnum, fpval);
746         } else
747                 *data = fpval.u.bits[hi];
748         return ret;
749 }
750
751 /*
752  * Change the machine-state of CHILD such that it will return via the normal
753  * kernel exit-path, rather than the syscall-exit path.
754  */
755 static void
756 convert_to_non_syscall (struct task_struct *child, struct pt_regs  *pt,
757                         unsigned long cfm)
758 {
759         struct unw_frame_info info, prev_info;
760         unsigned long ip, sp, pr;
761
762         unw_init_from_blocked_task(&info, child);
763         while (1) {
764                 prev_info = info;
765                 if (unw_unwind(&info) < 0)
766                         return;
767
768                 unw_get_sp(&info, &sp);
769                 if ((long)((unsigned long)child + IA64_STK_OFFSET - sp)
770                     < IA64_PT_REGS_SIZE) {
771                         dprintk("ptrace.%s: ran off the top of the kernel "
772                                 "stack\n", __FUNCTION__);
773                         return;
774                 }
775                 if (unw_get_pr (&prev_info, &pr) < 0) {
776                         unw_get_rp(&prev_info, &ip);
777                         dprintk("ptrace.%s: failed to read "
778                                 "predicate register (ip=0x%lx)\n",
779                                 __FUNCTION__, ip);
780                         return;
781                 }
782                 if (unw_is_intr_frame(&info)
783                     && (pr & (1UL << PRED_USER_STACK)))
784                         break;
785         }
786
787         /*
788          * Note: at the time of this call, the target task is blocked
789          * in notify_resume_user() and by clearling PRED_LEAVE_SYSCALL
790          * (aka, "pLvSys") we redirect execution from
791          * .work_pending_syscall_end to .work_processed_kernel.
792          */
793         unw_get_pr(&prev_info, &pr);
794         pr &= ~((1UL << PRED_SYSCALL) | (1UL << PRED_LEAVE_SYSCALL));
795         pr |=  (1UL << PRED_NON_SYSCALL);
796         unw_set_pr(&prev_info, pr);
797
798         pt->cr_ifs = (1UL << 63) | cfm;
799         /*
800          * Clear the memory that is NOT written on syscall-entry to
801          * ensure we do not leak kernel-state to user when execution
802          * resumes.
803          */
804         pt->r2 = 0;
805         pt->r3 = 0;
806         pt->r14 = 0;
807         memset(&pt->r16, 0, 16*8);      /* clear r16-r31 */
808         memset(&pt->f6, 0, 6*16);       /* clear f6-f11 */
809         pt->b7 = 0;
810         pt->ar_ccv = 0;
811         pt->ar_csd = 0;
812         pt->ar_ssd = 0;
813 }
814
815 static int
816 access_nat_bits (struct task_struct *child, struct pt_regs *pt,
817                  struct unw_frame_info *info,
818                  unsigned long *data, int write_access)
819 {
820         unsigned long regnum, nat_bits, scratch_unat, dummy = 0;
821         char nat = 0;
822
823         if (write_access) {
824                 nat_bits = *data;
825                 scratch_unat = ia64_put_scratch_nat_bits(pt, nat_bits);
826                 if (unw_set_ar(info, UNW_AR_UNAT, scratch_unat) < 0) {
827                         dprintk("ptrace: failed to set ar.unat\n");
828                         return -1;
829                 }
830                 for (regnum = 4; regnum <= 7; ++regnum) {
831                         unw_get_gr(info, regnum, &dummy, &nat);
832                         unw_set_gr(info, regnum, dummy,
833                                    (nat_bits >> regnum) & 1);
834                 }
835         } else {
836                 if (unw_get_ar(info, UNW_AR_UNAT, &scratch_unat) < 0) {
837                         dprintk("ptrace: failed to read ar.unat\n");
838                         return -1;
839                 }
840                 nat_bits = ia64_get_scratch_nat_bits(pt, scratch_unat);
841                 for (regnum = 4; regnum <= 7; ++regnum) {
842                         unw_get_gr(info, regnum, &dummy, &nat);
843                         nat_bits |= (nat != 0) << regnum;
844                 }
845                 *data = nat_bits;
846         }
847         return 0;
848 }
849
850 static int
851 access_uarea (struct task_struct *child, unsigned long addr,
852               unsigned long *data, int write_access)
853 {
854         unsigned long *ptr, regnum, urbs_end, rnat_addr, cfm;
855         struct switch_stack *sw;
856         struct pt_regs *pt;
857 #       define pt_reg_addr(pt, reg)     ((void *)                           \
858                                          ((unsigned long) (pt)              \
859                                           + offsetof(struct pt_regs, reg)))
860
861
862         pt = task_pt_regs(child);
863         sw = (struct switch_stack *) (child->thread.ksp + 16);
864
865         if ((addr & 0x7) != 0) {
866                 dprintk("ptrace: unaligned register address 0x%lx\n", addr);
867                 return -1;
868         }
869
870         if (addr < PT_F127 + 16) {
871                 /* accessing fph */
872                 if (write_access)
873                         ia64_sync_fph(child);
874                 else
875                         ia64_flush_fph(child);
876                 ptr = (unsigned long *)
877                         ((unsigned long) &child->thread.fph + addr);
878         } else if ((addr >= PT_F10) && (addr < PT_F11 + 16)) {
879                 /* scratch registers untouched by kernel (saved in pt_regs) */
880                 ptr = pt_reg_addr(pt, f10) + (addr - PT_F10);
881         } else if (addr >= PT_F12 && addr < PT_F15 + 16) {
882                 /*
883                  * Scratch registers untouched by kernel (saved in
884                  * switch_stack).
885                  */
886                 ptr = (unsigned long *) ((long) sw
887                                          + (addr - PT_NAT_BITS - 32));
888         } else if (addr < PT_AR_LC + 8) {
889                 /* preserved state: */
890                 struct unw_frame_info info;
891                 char nat = 0;
892                 int ret;
893
894                 unw_init_from_blocked_task(&info, child);
895                 if (unw_unwind_to_user(&info) < 0)
896                         return -1;
897
898                 switch (addr) {
899                       case PT_NAT_BITS:
900                         return access_nat_bits(child, pt, &info,
901                                                data, write_access);
902
903                       case PT_R4: case PT_R5: case PT_R6: case PT_R7:
904                         if (write_access) {
905                                 /* read NaT bit first: */
906                                 unsigned long dummy;
907
908                                 ret = unw_get_gr(&info, (addr - PT_R4)/8 + 4,
909                                                  &dummy, &nat);
910                                 if (ret < 0)
911                                         return ret;
912                         }
913                         return unw_access_gr(&info, (addr - PT_R4)/8 + 4, data,
914                                              &nat, write_access);
915
916                       case PT_B1: case PT_B2: case PT_B3:
917                       case PT_B4: case PT_B5:
918                         return unw_access_br(&info, (addr - PT_B1)/8 + 1, data,
919                                              write_access);
920
921                       case PT_AR_EC:
922                         return unw_access_ar(&info, UNW_AR_EC, data,
923                                              write_access);
924
925                       case PT_AR_LC:
926                         return unw_access_ar(&info, UNW_AR_LC, data,
927                                              write_access);
928
929                       default:
930                         if (addr >= PT_F2 && addr < PT_F5 + 16)
931                                 return access_fr(&info, (addr - PT_F2)/16 + 2,
932                                                  (addr & 8) != 0, data,
933                                                  write_access);
934                         else if (addr >= PT_F16 && addr < PT_F31 + 16)
935                                 return access_fr(&info,
936                                                  (addr - PT_F16)/16 + 16,
937                                                  (addr & 8) != 0,
938                                                  data, write_access);
939                         else {
940                                 dprintk("ptrace: rejecting access to register "
941                                         "address 0x%lx\n", addr);
942                                 return -1;
943                         }
944                 }
945         } else if (addr < PT_F9+16) {
946                 /* scratch state */
947                 switch (addr) {
948                       case PT_AR_BSP:
949                         /*
950                          * By convention, we use PT_AR_BSP to refer to
951                          * the end of the user-level backing store.
952                          * Use ia64_rse_skip_regs(PT_AR_BSP, -CFM.sof)
953                          * to get the real value of ar.bsp at the time
954                          * the kernel was entered.
955                          *
956                          * Furthermore, when changing the contents of
957                          * PT_AR_BSP (or PT_CFM) we MUST copy any
958                          * users-level stacked registers that are
959                          * stored on the kernel stack back to
960                          * user-space because otherwise, we might end
961                          * up clobbering kernel stacked registers.
962                          * Also, if this happens while the task is
963                          * blocked in a system call, which convert the
964                          * state such that the non-system-call exit
965                          * path is used.  This ensures that the proper
966                          * state will be picked up when resuming
967                          * execution.  However, it *also* means that
968                          * once we write PT_AR_BSP/PT_CFM, it won't be
969                          * possible to modify the syscall arguments of
970                          * the pending system call any longer.  This
971                          * shouldn't be an issue because modifying
972                          * PT_AR_BSP/PT_CFM generally implies that
973                          * we're either abandoning the pending system
974                          * call or that we defer it's re-execution
975                          * (e.g., due to GDB doing an inferior
976                          * function call).
977                          */
978                         urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
979                         if (write_access) {
980                                 if (*data != urbs_end) {
981                                         if (ia64_sync_user_rbs(child, sw,
982                                                                pt->ar_bspstore,
983                                                                urbs_end) < 0)
984                                                 return -1;
985                                         if (in_syscall(pt))
986                                                 convert_to_non_syscall(child,
987                                                                        pt,
988                                                                        cfm);
989                                         /*
990                                          * Simulate user-level write
991                                          * of ar.bsp:
992                                          */
993                                         pt->loadrs = 0;
994                                         pt->ar_bspstore = *data;
995                                 }
996                         } else
997                                 *data = urbs_end;
998                         return 0;
999
1000                       case PT_CFM:
1001                         urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
1002                         if (write_access) {
1003                                 if (((cfm ^ *data) & PFM_MASK) != 0) {
1004                                         if (ia64_sync_user_rbs(child, sw,
1005                                                                pt->ar_bspstore,
1006                                                                urbs_end) < 0)
1007                                                 return -1;
1008                                         if (in_syscall(pt))
1009                                                 convert_to_non_syscall(child,
1010                                                                        pt,
1011                                                                        cfm);
1012                                         pt->cr_ifs = ((pt->cr_ifs & ~PFM_MASK)
1013                                                       | (*data & PFM_MASK));
1014                                 }
1015                         } else
1016                                 *data = cfm;
1017                         return 0;
1018
1019                       case PT_CR_IPSR:
1020                         if (write_access) {
1021                                 unsigned long tmp = *data;
1022                                 /* psr.ri==3 is a reserved value: SDM 2:25 */
1023                                 if ((tmp & IA64_PSR_RI) == IA64_PSR_RI)
1024                                         tmp &= ~IA64_PSR_RI;
1025                                 pt->cr_ipsr = ((tmp & IPSR_MASK)
1026                                                | (pt->cr_ipsr & ~IPSR_MASK));
1027                         } else
1028                                 *data = (pt->cr_ipsr & IPSR_MASK);
1029                         return 0;
1030
1031                       case PT_AR_RSC:
1032                         if (write_access)
1033                                 pt->ar_rsc = *data | (3 << 2); /* force PL3 */
1034                         else
1035                                 *data = pt->ar_rsc;
1036                         return 0;
1037
1038                       case PT_AR_RNAT:
1039                         urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
1040                         rnat_addr = (long) ia64_rse_rnat_addr((long *)
1041                                                               urbs_end);
1042                         if (write_access)
1043                                 return ia64_poke(child, sw, urbs_end,
1044                                                  rnat_addr, *data);
1045                         else
1046                                 return ia64_peek(child, sw, urbs_end,
1047                                                  rnat_addr, data);
1048
1049                       case PT_R1:
1050                         ptr = pt_reg_addr(pt, r1);
1051                         break;
1052                       case PT_R2:  case PT_R3:
1053                         ptr = pt_reg_addr(pt, r2) + (addr - PT_R2);
1054                         break;
1055                       case PT_R8:  case PT_R9:  case PT_R10: case PT_R11:
1056                         ptr = pt_reg_addr(pt, r8) + (addr - PT_R8);
1057                         break;
1058                       case PT_R12: case PT_R13:
1059                         ptr = pt_reg_addr(pt, r12) + (addr - PT_R12);
1060                         break;
1061                       case PT_R14:
1062                         ptr = pt_reg_addr(pt, r14);
1063                         break;
1064                       case PT_R15:
1065                         ptr = pt_reg_addr(pt, r15);
1066                         break;
1067                       case PT_R16: case PT_R17: case PT_R18: case PT_R19:
1068                       case PT_R20: case PT_R21: case PT_R22: case PT_R23:
1069                       case PT_R24: case PT_R25: case PT_R26: case PT_R27:
1070                       case PT_R28: case PT_R29: case PT_R30: case PT_R31:
1071                         ptr = pt_reg_addr(pt, r16) + (addr - PT_R16);
1072                         break;
1073                       case PT_B0:
1074                         ptr = pt_reg_addr(pt, b0);
1075                         break;
1076                       case PT_B6:
1077                         ptr = pt_reg_addr(pt, b6);
1078                         break;
1079                       case PT_B7:
1080                         ptr = pt_reg_addr(pt, b7);
1081                         break;
1082                       case PT_F6:  case PT_F6+8: case PT_F7: case PT_F7+8:
1083                       case PT_F8:  case PT_F8+8: case PT_F9: case PT_F9+8:
1084                         ptr = pt_reg_addr(pt, f6) + (addr - PT_F6);
1085                         break;
1086                       case PT_AR_BSPSTORE:
1087                         ptr = pt_reg_addr(pt, ar_bspstore);
1088                         break;
1089                       case PT_AR_UNAT:
1090                         ptr = pt_reg_addr(pt, ar_unat);
1091                         break;
1092                       case PT_AR_PFS:
1093                         ptr = pt_reg_addr(pt, ar_pfs);
1094                         break;
1095                       case PT_AR_CCV:
1096                         ptr = pt_reg_addr(pt, ar_ccv);
1097                         break;
1098                       case PT_AR_FPSR:
1099                         ptr = pt_reg_addr(pt, ar_fpsr);
1100                         break;
1101                       case PT_CR_IIP:
1102                         ptr = pt_reg_addr(pt, cr_iip);
1103                         break;
1104                       case PT_PR:
1105                         ptr = pt_reg_addr(pt, pr);
1106                         break;
1107                         /* scratch register */
1108
1109                       default:
1110                         /* disallow accessing anything else... */
1111                         dprintk("ptrace: rejecting access to register "
1112                                 "address 0x%lx\n", addr);
1113                         return -1;
1114                 }
1115         } else if (addr <= PT_AR_SSD) {
1116                 ptr = pt_reg_addr(pt, ar_csd) + (addr - PT_AR_CSD);
1117         } else {
1118                 /* access debug registers */
1119
1120                 if (addr >= PT_IBR) {
1121                         regnum = (addr - PT_IBR) >> 3;
1122                         ptr = &child->thread.ibr[0];
1123                 } else {
1124                         regnum = (addr - PT_DBR) >> 3;
1125                         ptr = &child->thread.dbr[0];
1126                 }
1127
1128                 if (regnum >= 8) {
1129                         dprintk("ptrace: rejecting access to register "
1130                                 "address 0x%lx\n", addr);
1131                         return -1;
1132                 }
1133 #ifdef CONFIG_PERFMON
1134                 /*
1135                  * Check if debug registers are used by perfmon. This
1136                  * test must be done once we know that we can do the
1137                  * operation, i.e. the arguments are all valid, but
1138                  * before we start modifying the state.
1139                  *
1140                  * Perfmon needs to keep a count of how many processes
1141                  * are trying to modify the debug registers for system
1142                  * wide monitoring sessions.
1143                  *
1144                  * We also include read access here, because they may
1145                  * cause the PMU-installed debug register state
1146                  * (dbr[], ibr[]) to be reset. The two arrays are also
1147                  * used by perfmon, but we do not use
1148                  * IA64_THREAD_DBG_VALID. The registers are restored
1149                  * by the PMU context switch code.
1150                  */
1151                 if (pfm_use_debug_registers(child)) return -1;
1152 #endif
1153
1154                 if (!(child->thread.flags & IA64_THREAD_DBG_VALID)) {
1155                         child->thread.flags |= IA64_THREAD_DBG_VALID;
1156                         memset(child->thread.dbr, 0,
1157                                sizeof(child->thread.dbr));
1158                         memset(child->thread.ibr, 0,
1159                                sizeof(child->thread.ibr));
1160                 }
1161
1162                 ptr += regnum;
1163
1164                 if ((regnum & 1) && write_access) {
1165                         /* don't let the user set kernel-level breakpoints: */
1166                         *ptr = *data & ~(7UL << 56);
1167                         return 0;
1168                 }
1169         }
1170         if (write_access)
1171                 *ptr = *data;
1172         else
1173                 *data = *ptr;
1174         return 0;
1175 }
1176
1177 static long
1178 ptrace_getregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
1179 {
1180         unsigned long psr, ec, lc, rnat, bsp, cfm, nat_bits, val;
1181         struct unw_frame_info info;
1182         struct ia64_fpreg fpval;
1183         struct switch_stack *sw;
1184         struct pt_regs *pt;
1185         long ret, retval = 0;
1186         char nat = 0;
1187         int i;
1188
1189         if (!access_ok(VERIFY_WRITE, ppr, sizeof(struct pt_all_user_regs)))
1190                 return -EIO;
1191
1192         pt = task_pt_regs(child);
1193         sw = (struct switch_stack *) (child->thread.ksp + 16);
1194         unw_init_from_blocked_task(&info, child);
1195         if (unw_unwind_to_user(&info) < 0) {
1196                 return -EIO;
1197         }
1198
1199         if (((unsigned long) ppr & 0x7) != 0) {
1200                 dprintk("ptrace:unaligned register address %p\n", ppr);
1201                 return -EIO;
1202         }
1203
1204         if (access_uarea(child, PT_CR_IPSR, &psr, 0) < 0
1205             || access_uarea(child, PT_AR_EC, &ec, 0) < 0
1206             || access_uarea(child, PT_AR_LC, &lc, 0) < 0
1207             || access_uarea(child, PT_AR_RNAT, &rnat, 0) < 0
1208             || access_uarea(child, PT_AR_BSP, &bsp, 0) < 0
1209             || access_uarea(child, PT_CFM, &cfm, 0)
1210             || access_uarea(child, PT_NAT_BITS, &nat_bits, 0))
1211                 return -EIO;
1212
1213         /* control regs */
1214
1215         retval |= __put_user(pt->cr_iip, &ppr->cr_iip);
1216         retval |= __put_user(psr, &ppr->cr_ipsr);
1217
1218         /* app regs */
1219
1220         retval |= __put_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
1221         retval |= __put_user(pt->ar_rsc, &ppr->ar[PT_AUR_RSC]);
1222         retval |= __put_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
1223         retval |= __put_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
1224         retval |= __put_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
1225         retval |= __put_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
1226
1227         retval |= __put_user(ec, &ppr->ar[PT_AUR_EC]);
1228         retval |= __put_user(lc, &ppr->ar[PT_AUR_LC]);
1229         retval |= __put_user(rnat, &ppr->ar[PT_AUR_RNAT]);
1230         retval |= __put_user(bsp, &ppr->ar[PT_AUR_BSP]);
1231         retval |= __put_user(cfm, &ppr->cfm);
1232
1233         /* gr1-gr3 */
1234
1235         retval |= __copy_to_user(&ppr->gr[1], &pt->r1, sizeof(long));
1236         retval |= __copy_to_user(&ppr->gr[2], &pt->r2, sizeof(long) *2);
1237
1238         /* gr4-gr7 */
1239
1240         for (i = 4; i < 8; i++) {
1241                 if (unw_access_gr(&info, i, &val, &nat, 0) < 0)
1242                         return -EIO;
1243                 retval |= __put_user(val, &ppr->gr[i]);
1244         }
1245
1246         /* gr8-gr11 */
1247
1248         retval |= __copy_to_user(&ppr->gr[8], &pt->r8, sizeof(long) * 4);
1249
1250         /* gr12-gr15 */
1251
1252         retval |= __copy_to_user(&ppr->gr[12], &pt->r12, sizeof(long) * 2);
1253         retval |= __copy_to_user(&ppr->gr[14], &pt->r14, sizeof(long));
1254         retval |= __copy_to_user(&ppr->gr[15], &pt->r15, sizeof(long));
1255
1256         /* gr16-gr31 */
1257
1258         retval |= __copy_to_user(&ppr->gr[16], &pt->r16, sizeof(long) * 16);
1259
1260         /* b0 */
1261
1262         retval |= __put_user(pt->b0, &ppr->br[0]);
1263
1264         /* b1-b5 */
1265
1266         for (i = 1; i < 6; i++) {
1267                 if (unw_access_br(&info, i, &val, 0) < 0)
1268                         return -EIO;
1269                 __put_user(val, &ppr->br[i]);
1270         }
1271
1272         /* b6-b7 */
1273
1274         retval |= __put_user(pt->b6, &ppr->br[6]);
1275         retval |= __put_user(pt->b7, &ppr->br[7]);
1276
1277         /* fr2-fr5 */
1278
1279         for (i = 2; i < 6; i++) {
1280                 if (unw_get_fr(&info, i, &fpval) < 0)
1281                         return -EIO;
1282                 retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
1283         }
1284
1285         /* fr6-fr11 */
1286
1287         retval |= __copy_to_user(&ppr->fr[6], &pt->f6,
1288                                  sizeof(struct ia64_fpreg) * 6);
1289
1290         /* fp scratch regs(12-15) */
1291
1292         retval |= __copy_to_user(&ppr->fr[12], &sw->f12,
1293                                  sizeof(struct ia64_fpreg) * 4);
1294
1295         /* fr16-fr31 */
1296
1297         for (i = 16; i < 32; i++) {
1298                 if (unw_get_fr(&info, i, &fpval) < 0)
1299                         return -EIO;
1300                 retval |= __copy_to_user(&ppr->fr[i], &fpval, sizeof (fpval));
1301         }
1302
1303         /* fph */
1304
1305         ia64_flush_fph(child);
1306         retval |= __copy_to_user(&ppr->fr[32], &child->thread.fph,
1307                                  sizeof(ppr->fr[32]) * 96);
1308
1309         /*  preds */
1310
1311         retval |= __put_user(pt->pr, &ppr->pr);
1312
1313         /* nat bits */
1314
1315         retval |= __put_user(nat_bits, &ppr->nat);
1316
1317         ret = retval ? -EIO : 0;
1318         return ret;
1319 }
1320
1321 static long
1322 ptrace_setregs (struct task_struct *child, struct pt_all_user_regs __user *ppr)
1323 {
1324         unsigned long psr, rsc, ec, lc, rnat, bsp, cfm, nat_bits, val = 0;
1325         struct unw_frame_info info;
1326         struct switch_stack *sw;
1327         struct ia64_fpreg fpval;
1328         struct pt_regs *pt;
1329         long ret, retval = 0;
1330         int i;
1331
1332         memset(&fpval, 0, sizeof(fpval));
1333
1334         if (!access_ok(VERIFY_READ, ppr, sizeof(struct pt_all_user_regs)))
1335                 return -EIO;
1336
1337         pt = task_pt_regs(child);
1338         sw = (struct switch_stack *) (child->thread.ksp + 16);
1339         unw_init_from_blocked_task(&info, child);
1340         if (unw_unwind_to_user(&info) < 0) {
1341                 return -EIO;
1342         }
1343
1344         if (((unsigned long) ppr & 0x7) != 0) {
1345                 dprintk("ptrace:unaligned register address %p\n", ppr);
1346                 return -EIO;
1347         }
1348
1349         /* control regs */
1350
1351         retval |= __get_user(pt->cr_iip, &ppr->cr_iip);
1352         retval |= __get_user(psr, &ppr->cr_ipsr);
1353
1354         /* app regs */
1355
1356         retval |= __get_user(pt->ar_pfs, &ppr->ar[PT_AUR_PFS]);
1357         retval |= __get_user(rsc, &ppr->ar[PT_AUR_RSC]);
1358         retval |= __get_user(pt->ar_bspstore, &ppr->ar[PT_AUR_BSPSTORE]);
1359         retval |= __get_user(pt->ar_unat, &ppr->ar[PT_AUR_UNAT]);
1360         retval |= __get_user(pt->ar_ccv, &ppr->ar[PT_AUR_CCV]);
1361         retval |= __get_user(pt->ar_fpsr, &ppr->ar[PT_AUR_FPSR]);
1362
1363         retval |= __get_user(ec, &ppr->ar[PT_AUR_EC]);
1364         retval |= __get_user(lc, &ppr->ar[PT_AUR_LC]);
1365         retval |= __get_user(rnat, &ppr->ar[PT_AUR_RNAT]);
1366         retval |= __get_user(bsp, &ppr->ar[PT_AUR_BSP]);
1367         retval |= __get_user(cfm, &ppr->cfm);
1368
1369         /* gr1-gr3 */
1370
1371         retval |= __copy_from_user(&pt->r1, &ppr->gr[1], sizeof(long));
1372         retval |= __copy_from_user(&pt->r2, &ppr->gr[2], sizeof(long) * 2);
1373
1374         /* gr4-gr7 */
1375
1376         for (i = 4; i < 8; i++) {
1377                 retval |= __get_user(val, &ppr->gr[i]);
1378                 /* NaT bit will be set via PT_NAT_BITS: */
1379                 if (unw_set_gr(&info, i, val, 0) < 0)
1380                         return -EIO;
1381         }
1382
1383         /* gr8-gr11 */
1384
1385         retval |= __copy_from_user(&pt->r8, &ppr->gr[8], sizeof(long) * 4);
1386
1387         /* gr12-gr15 */
1388
1389         retval |= __copy_from_user(&pt->r12, &ppr->gr[12], sizeof(long) * 2);
1390         retval |= __copy_from_user(&pt->r14, &ppr->gr[14], sizeof(long));
1391         retval |= __copy_from_user(&pt->r15, &ppr->gr[15], sizeof(long));
1392
1393         /* gr16-gr31 */
1394
1395         retval |= __copy_from_user(&pt->r16, &ppr->gr[16], sizeof(long) * 16);
1396
1397         /* b0 */
1398
1399         retval |= __get_user(pt->b0, &ppr->br[0]);
1400
1401         /* b1-b5 */
1402
1403         for (i = 1; i < 6; i++) {
1404                 retval |= __get_user(val, &ppr->br[i]);
1405                 unw_set_br(&info, i, val);
1406         }
1407
1408         /* b6-b7 */
1409
1410         retval |= __get_user(pt->b6, &ppr->br[6]);
1411         retval |= __get_user(pt->b7, &ppr->br[7]);
1412
1413         /* fr2-fr5 */
1414
1415         for (i = 2; i < 6; i++) {
1416                 retval |= __copy_from_user(&fpval, &ppr->fr[i], sizeof(fpval));
1417                 if (unw_set_fr(&info, i, fpval) < 0)
1418                         return -EIO;
1419         }
1420
1421         /* fr6-fr11 */
1422
1423         retval |= __copy_from_user(&pt->f6, &ppr->fr[6],
1424                                    sizeof(ppr->fr[6]) * 6);
1425
1426         /* fp scratch regs(12-15) */
1427
1428         retval |= __copy_from_user(&sw->f12, &ppr->fr[12],
1429                                    sizeof(ppr->fr[12]) * 4);
1430
1431         /* fr16-fr31 */
1432
1433         for (i = 16; i < 32; i++) {
1434                 retval |= __copy_from_user(&fpval, &ppr->fr[i],
1435                                            sizeof(fpval));
1436                 if (unw_set_fr(&info, i, fpval) < 0)
1437                         return -EIO;
1438         }
1439
1440         /* fph */
1441
1442         ia64_sync_fph(child);
1443         retval |= __copy_from_user(&child->thread.fph, &ppr->fr[32],
1444                                    sizeof(ppr->fr[32]) * 96);
1445
1446         /* preds */
1447
1448         retval |= __get_user(pt->pr, &ppr->pr);
1449
1450         /* nat bits */
1451
1452         retval |= __get_user(nat_bits, &ppr->nat);
1453
1454         retval |= access_uarea(child, PT_CR_IPSR, &psr, 1);
1455         retval |= access_uarea(child, PT_AR_RSC, &rsc, 1);
1456         retval |= access_uarea(child, PT_AR_EC, &ec, 1);
1457         retval |= access_uarea(child, PT_AR_LC, &lc, 1);
1458         retval |= access_uarea(child, PT_AR_RNAT, &rnat, 1);
1459         retval |= access_uarea(child, PT_AR_BSP, &bsp, 1);
1460         retval |= access_uarea(child, PT_CFM, &cfm, 1);
1461         retval |= access_uarea(child, PT_NAT_BITS, &nat_bits, 1);
1462
1463         ret = retval ? -EIO : 0;
1464         return ret;
1465 }
1466
1467 /*
1468  * Called by kernel/ptrace.c when detaching..
1469  *
1470  * Make sure the single step bit is not set.
1471  */
1472 void
1473 ptrace_disable (struct task_struct *child)
1474 {
1475         struct ia64_psr *child_psr = ia64_psr(task_pt_regs(child));
1476
1477         /* make sure the single step/taken-branch trap bits are not set: */
1478         clear_tsk_thread_flag(child, TIF_SINGLESTEP);
1479         child_psr->ss = 0;
1480         child_psr->tb = 0;
1481 }
1482
1483 asmlinkage long
1484 sys_ptrace (long request, pid_t pid, unsigned long addr, unsigned long data)
1485 {
1486         struct pt_regs *pt;
1487         unsigned long urbs_end, peek_or_poke;
1488         struct task_struct *child;
1489         struct switch_stack *sw;
1490         long ret;
1491         struct unw_frame_info info;
1492
1493         lock_kernel();
1494         ret = -EPERM;
1495         if (request == PTRACE_TRACEME) {
1496                 ret = ptrace_traceme();
1497                 goto out;
1498         }
1499
1500         peek_or_poke = (request == PTRACE_PEEKTEXT
1501                         || request == PTRACE_PEEKDATA
1502                         || request == PTRACE_POKETEXT
1503                         || request == PTRACE_POKEDATA);
1504         ret = -ESRCH;
1505         read_lock(&tasklist_lock);
1506         {
1507                 child = find_task_by_pid(pid);
1508                 if (child) {
1509                         if (peek_or_poke)
1510                                 child = find_thread_for_addr(child, addr);
1511                         get_task_struct(child);
1512                 }
1513         }
1514         read_unlock(&tasklist_lock);
1515         if (!child)
1516                 goto out;
1517         ret = -EPERM;
1518         if (pid == 1)           /* no messing around with init! */
1519                 goto out_tsk;
1520
1521         if (request == PTRACE_ATTACH) {
1522                 ret = ptrace_attach(child);
1523                 if (!ret)
1524                         arch_ptrace_attach(child);
1525                 goto out_tsk;
1526         }
1527
1528         ret = ptrace_check_attach(child, request == PTRACE_KILL);
1529         if (ret < 0)
1530                 goto out_tsk;
1531
1532         pt = task_pt_regs(child);
1533         sw = (struct switch_stack *) (child->thread.ksp + 16);
1534
1535         switch (request) {
1536               case PTRACE_PEEKTEXT:
1537               case PTRACE_PEEKDATA:
1538                 /* read word at location addr */
1539                 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
1540                 ret = ia64_peek(child, sw, urbs_end, addr, &data);
1541                 if (ret == 0) {
1542                         ret = data;
1543                         /* ensure "ret" is not mistaken as an error code: */
1544                         force_successful_syscall_return();
1545                 }
1546                 goto out_tsk;
1547
1548               case PTRACE_POKETEXT:
1549               case PTRACE_POKEDATA:
1550                 /* write the word at location addr */
1551                 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
1552                 ret = ia64_poke(child, sw, urbs_end, addr, data);
1553
1554                 /* Make sure user RBS has the latest data */
1555                 unw_init_from_blocked_task(&info, child);
1556                 do_sync_rbs(&info, ia64_sync_user_rbs);
1557
1558                 goto out_tsk;
1559
1560               case PTRACE_PEEKUSR:
1561                 /* read the word at addr in the USER area */
1562                 if (access_uarea(child, addr, &data, 0) < 0) {
1563                         ret = -EIO;
1564                         goto out_tsk;
1565                 }
1566                 ret = data;
1567                 /* ensure "ret" is not mistaken as an error code */
1568                 force_successful_syscall_return();
1569                 goto out_tsk;
1570
1571               case PTRACE_POKEUSR:
1572                 /* write the word at addr in the USER area */
1573                 if (access_uarea(child, addr, &data, 1) < 0) {
1574                         ret = -EIO;
1575                         goto out_tsk;
1576                 }
1577                 ret = 0;
1578                 goto out_tsk;
1579
1580               case PTRACE_OLD_GETSIGINFO:
1581                 /* for backwards-compatibility */
1582                 ret = ptrace_request(child, PTRACE_GETSIGINFO, addr, data);
1583                 goto out_tsk;
1584
1585               case PTRACE_OLD_SETSIGINFO:
1586                 /* for backwards-compatibility */
1587                 ret = ptrace_request(child, PTRACE_SETSIGINFO, addr, data);
1588                 goto out_tsk;
1589
1590               case PTRACE_SYSCALL:
1591                 /* continue and stop at next (return from) syscall */
1592               case PTRACE_CONT:
1593                 /* restart after signal. */
1594                 ret = -EIO;
1595                 if (!valid_signal(data))
1596                         goto out_tsk;
1597                 if (request == PTRACE_SYSCALL)
1598                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1599                 else
1600                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1601                 child->exit_code = data;
1602
1603                 /*
1604                  * Make sure the single step/taken-branch trap bits
1605                  * are not set:
1606                  */
1607                 clear_tsk_thread_flag(child, TIF_SINGLESTEP);
1608                 ia64_psr(pt)->ss = 0;
1609                 ia64_psr(pt)->tb = 0;
1610
1611                 wake_up_process(child);
1612                 ret = 0;
1613                 goto out_tsk;
1614
1615               case PTRACE_KILL:
1616                 /*
1617                  * Make the child exit.  Best I can do is send it a
1618                  * sigkill.  Perhaps it should be put in the status
1619                  * that it wants to exit.
1620                  */
1621                 if (child->exit_state == EXIT_ZOMBIE)
1622                         /* already dead */
1623                         goto out_tsk;
1624                 child->exit_code = SIGKILL;
1625
1626                 ptrace_disable(child);
1627                 wake_up_process(child);
1628                 ret = 0;
1629                 goto out_tsk;
1630
1631               case PTRACE_SINGLESTEP:
1632                 /* let child execute for one instruction */
1633               case PTRACE_SINGLEBLOCK:
1634                 ret = -EIO;
1635                 if (!valid_signal(data))
1636                         goto out_tsk;
1637
1638                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1639                 set_tsk_thread_flag(child, TIF_SINGLESTEP);
1640                 if (request == PTRACE_SINGLESTEP) {
1641                         ia64_psr(pt)->ss = 1;
1642                 } else {
1643                         ia64_psr(pt)->tb = 1;
1644                 }
1645                 child->exit_code = data;
1646
1647                 /* give it a chance to run. */
1648                 wake_up_process(child);
1649                 ret = 0;
1650                 goto out_tsk;
1651
1652               case PTRACE_DETACH:
1653                 /* detach a process that was attached. */
1654                 ret = ptrace_detach(child, data);
1655                 goto out_tsk;
1656
1657               case PTRACE_GETREGS:
1658                 ret = ptrace_getregs(child,
1659                                      (struct pt_all_user_regs __user *) data);
1660                 goto out_tsk;
1661
1662               case PTRACE_SETREGS:
1663                 ret = ptrace_setregs(child,
1664                                      (struct pt_all_user_regs __user *) data);
1665                 goto out_tsk;
1666
1667               default:
1668                 ret = ptrace_request(child, request, addr, data);
1669                 goto out_tsk;
1670         }
1671   out_tsk:
1672         put_task_struct(child);
1673   out:
1674         unlock_kernel();
1675         return ret;
1676 }
1677
1678
1679 static void
1680 syscall_trace (void)
1681 {
1682         /*
1683          * The 0x80 provides a way for the tracing parent to
1684          * distinguish between a syscall stop and SIGTRAP delivery.
1685          */
1686         ptrace_notify(SIGTRAP
1687                       | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
1688
1689         /*
1690          * This isn't the same as continuing with a signal, but it
1691          * will do for normal use.  strace only continues with a
1692          * signal if the stopping signal is not SIGTRAP.  -brl
1693          */
1694         if (current->exit_code) {
1695                 send_sig(current->exit_code, current, 1);
1696                 current->exit_code = 0;
1697         }
1698 }
1699
1700 /* "asmlinkage" so the input arguments are preserved... */
1701
1702 asmlinkage void
1703 syscall_trace_enter (long arg0, long arg1, long arg2, long arg3,
1704                      long arg4, long arg5, long arg6, long arg7,
1705                      struct pt_regs regs)
1706 {
1707         if (test_thread_flag(TIF_SYSCALL_TRACE) 
1708             && (current->ptrace & PT_PTRACED))
1709                 syscall_trace();
1710
1711         /* copy user rbs to kernel rbs */
1712         if (test_thread_flag(TIF_RESTORE_RSE))
1713                 ia64_sync_krbs();
1714
1715         if (unlikely(current->audit_context)) {
1716                 long syscall;
1717                 int arch;
1718
1719                 if (IS_IA32_PROCESS(&regs)) {
1720                         syscall = regs.r1;
1721                         arch = AUDIT_ARCH_I386;
1722                 } else {
1723                         syscall = regs.r15;
1724                         arch = AUDIT_ARCH_IA64;
1725                 }
1726
1727                 audit_syscall_entry(arch, syscall, arg0, arg1, arg2, arg3);
1728         }
1729
1730 }
1731
1732 /* "asmlinkage" so the input arguments are preserved... */
1733
1734 asmlinkage void
1735 syscall_trace_leave (long arg0, long arg1, long arg2, long arg3,
1736                      long arg4, long arg5, long arg6, long arg7,
1737                      struct pt_regs regs)
1738 {
1739         if (unlikely(current->audit_context)) {
1740                 int success = AUDITSC_RESULT(regs.r10);
1741                 long result = regs.r8;
1742
1743                 if (success != AUDITSC_SUCCESS)
1744                         result = -result;
1745                 audit_syscall_exit(success, result);
1746         }
1747
1748         if ((test_thread_flag(TIF_SYSCALL_TRACE)
1749             || test_thread_flag(TIF_SINGLESTEP))
1750             && (current->ptrace & PT_PTRACED))
1751                 syscall_trace();
1752
1753         /* copy user rbs to kernel rbs */
1754         if (test_thread_flag(TIF_RESTORE_RSE))
1755                 ia64_sync_krbs();
1756 }