0432abe563c3642842385a1eb50c3004fc09f71e
[linux-2.6.git] / arch / ia64 / kernel / ptrace.c
1 /*
2  * Kernel support for the ptrace() and syscall tracing interfaces.
3  *
4  * Copyright (C) 1999-2003 Hewlett-Packard Co
5  *      David Mosberger-Tang <davidm@hpl.hp.com>
6  *
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.
9  */
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/mm.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>
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 #define p4      (1UL << 4)      /* for pSys (see entry.h) */
35 #define p5      (1UL << 5)      /* for pNonSys (see entry.h) */
36
37 /*
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)
45  */
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
49
50 #define PTRACE_DEBUG    0
51
52 #if PTRACE_DEBUG
53 # define dprintk(format...)     printk(format)
54 # define inline
55 #else
56 # define dprintk(format...)
57 #endif
58
59 /* Return TRUE if PT was created due to kernel-entry via a system-call.  */
60
61 static inline int
62 in_syscall (struct pt_regs *pt)
63 {
64         return (long) pt->cr_ifs >= 0;
65 }
66
67 /*
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.
70  */
71 unsigned long
72 ia64_get_scratch_nat_bits (struct pt_regs *pt, unsigned long scratch_unat)
73 {
74 #       define GET_BITS(first, last, unat)                                              \
75         ({                                                                              \
76                 unsigned long bit = ia64_unat_pos(&pt->r##first);                       \
77                 unsigned long mask = ((1UL << (last - first + 1)) - 1) << 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 can be handled in
89          * parallel.  If the register order in struct_pt_regs changes, this code MUST be
90          * 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 mask = ((1UL << (last - first + 1)) - 1) << first;        \
116                 long dist;                                                              \
117                 if (bit < first)                                                        \
118                         dist = 64 + bit - first;                                        \
119                 else                                                                    \
120                         dist = bit - first;                                             \
121                 ia64_rotl(nat & mask, dist);                                            \
122         })
123         unsigned long scratch_unat;
124
125         /*
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
128          * updated.
129          */
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);
137
138         return scratch_unat;
139
140 #       undef PUT_BITS
141 }
142
143 #define IA64_MLX_TEMPLATE       0x2
144 #define IA64_MOVL_OPCODE        6
145
146 void
147 ia64_increment_ip (struct pt_regs *regs)
148 {
149         unsigned long w0, ri = ia64_psr(regs)->ri + 1;
150
151         if (ri > 2) {
152                 ri = 0;
153                 regs->cr_iip += 16;
154         } else if (ri == 2) {
155                 get_user(w0, (char *) regs->cr_iip + 0);
156                 if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
157                         /*
158                          * rfi'ing to slot 2 of an MLX bundle causes
159                          * an illegal operation fault.  We don't want
160                          * that to happen...
161                          */
162                         ri = 0;
163                         regs->cr_iip += 16;
164                 }
165         }
166         ia64_psr(regs)->ri = ri;
167 }
168
169 void
170 ia64_decrement_ip (struct pt_regs *regs)
171 {
172         unsigned long w0, ri = ia64_psr(regs)->ri - 1;
173
174         if (ia64_psr(regs)->ri == 0) {
175                 regs->cr_iip -= 16;
176                 ri = 2;
177                 get_user(w0, (char *) regs->cr_iip + 0);
178                 if (((w0 >> 1) & 0xf) == IA64_MLX_TEMPLATE) {
179                         /*
180                          * rfi'ing to slot 2 of an MLX bundle causes
181                          * an illegal operation fault.  We don't want
182                          * that to happen...
183                          */
184                         ri = 1;
185                 }
186         }
187         ia64_psr(regs)->ri = ri;
188 }
189
190 /*
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.
195  *
196  * user rbs
197  *
198  * +--------+ <-- lowest address
199  * | slot62 |
200  * +--------+
201  * |  rnat  | 0x....1f8
202  * +--------+
203  * | slot00 | \
204  * +--------+ |
205  * | slot01 | > child_regs->ar_rnat
206  * +--------+ |
207  * | slot02 | /                         kernel rbs
208  * +--------+                           +--------+
209  *          <- child_regs->ar_bspstore  | slot61 | <-- krbs
210  * +- - - - +                           +--------+
211  *                                      | slot62 |
212  * +- - - - +                           +--------+
213  *                                      |  rnat  |
214  * +- - - - +                           +--------+
215  *   vrnat                              | slot00 |
216  * +- - - - +                           +--------+
217  *                                      =        =
218  *                                      +--------+
219  *                                      | slot00 | \
220  *                                      +--------+ |
221  *                                      | slot01 | > child_stack->ar_rnat
222  *                                      +--------+ |
223  *                                      | slot02 | /
224  *                                      +--------+
225  *                                                <--- child_stack->ar_bspstore
226  *
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.
231  *
232  * Boundary cases:
233  *
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.
236  *
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.
239  */
240 static unsigned long
241 get_rnat (struct task_struct *task, struct switch_stack *sw,
242           unsigned long *krbs, unsigned long *urnat_addr, unsigned long *urbs_end)
243 {
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;
247         struct pt_regs *pt;
248
249         pt = ia64_task_regs(task);
250         kbsp = (unsigned long *) sw->ar_bspstore;
251         ubspstore = (unsigned long *) pt->ar_bspstore;
252
253         if (urbs_end < urnat_addr)
254                 nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_end);
255         else
256                 nbits = 63;
257         mask = (1UL << nbits) - 1;
258         /*
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.
262          */
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;
268
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);
273                 mask &= ~umask;
274                 if (!mask)
275                         return urnat;
276         }
277
278         m = mask << shift;
279         if (rnat0_kaddr >= kbsp)
280                 rnat0 = sw->ar_rnat;
281         else if (rnat0_kaddr > krbs)
282                 rnat0 = *rnat0_kaddr;
283         urnat |= (rnat0 & m) >> shift;
284
285         m = mask >> (63 - shift);
286         if (rnat1_kaddr >= kbsp)
287                 rnat1 = sw->ar_rnat;
288         else if (rnat1_kaddr > krbs)
289                 rnat1 = *rnat1_kaddr;
290         urnat |= (rnat1 & m) << (63 - shift);
291         return urnat;
292 }
293
294 /*
295  * The reverse of get_rnat.
296  */
297 static void
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)
301 {
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;
305         struct pt_regs *pt;
306         unsigned long cfm, *urbs_kargs;
307         struct unw_frame_info info;
308
309         pt = ia64_task_regs(task);
310         kbsp = (unsigned long *) sw->ar_bspstore;
311         ubspstore = (unsigned long *) pt->ar_bspstore;
312
313         urbs_kargs = urbs_end;
314         if (in_syscall(pt)) {
315                 /*
316                  * If entered via syscall, don't allow user to set rnat bits
317                  * for syscall args.
318                  */
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));
323                 }
324         }
325
326         if (urbs_kargs >= urnat_addr)
327                 nbits = 63;
328         else {
329                 if ((urnat_addr - 63) >= urbs_kargs)
330                         return;
331                 nbits = ia64_rse_num_regs(urnat_addr - 63, urbs_kargs);
332         }
333         mask = (1UL << nbits) - 1;
334
335         /*
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.
339          */
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;
345
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);
350                 mask &= ~umask;
351                 if (!mask)
352                         return;
353         }
354         /*
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.
357          */
358         rnat0 = (urnat << shift);
359         m = mask << 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));
364
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));
371 }
372
373 static inline int
374 on_kernel_rbs (unsigned long addr, unsigned long bspstore, unsigned long urbs_end)
375 {
376         return (addr >= bspstore
377                 && addr <= (unsigned long) ia64_rse_rnat_addr((unsigned long *) urbs_end));
378 }
379
380 /*
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).
385  *
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.
389  */
390 long
391 ia64_peek (struct task_struct *child, struct switch_stack *child_stack, unsigned long user_rbs_end,
392            unsigned long addr, long *val)
393 {
394         unsigned long *bspstore, *krbs, regnum, *laddr, *urbs_end, *rnat_addr;
395         struct pt_regs *child_regs;
396         size_t copied;
397         long ret;
398
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)) {
405                 /*
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.
408                  */
409                 rnat_addr = ia64_rse_rnat_addr(laddr);
410                 ret = get_rnat(child, child_stack, krbs, rnat_addr, urbs_end);
411
412                 if (laddr == rnat_addr) {
413                         /* return NaT collection word itself */
414                         *val = ret;
415                         return 0;
416                 }
417
418                 if (((1UL << ia64_rse_slot_num(laddr)) & ret) != 0) {
419                         /*
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
424                          * zero in this case.
425                          */
426                         *val = 0;
427                         return 0;
428                 }
429
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);
434                         return 0;
435                 }
436         }
437         copied = access_process_vm(child, addr, &ret, sizeof(ret), 0);
438         if (copied != sizeof(ret))
439                 return -EIO;
440         *val = ret;
441         return 0;
442 }
443
444 long
445 ia64_poke (struct task_struct *child, struct switch_stack *child_stack, unsigned long user_rbs_end,
446            unsigned long addr, long val)
447 {
448         unsigned long *bspstore, *krbs, regnum, *laddr, *urbs_end = (long *) user_rbs_end;
449         struct pt_regs *child_regs;
450
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)) {
456                 /*
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.
459                  */
460                 if (ia64_rse_is_rnat_slot(laddr))
461                         put_rnat(child, child_stack, krbs, laddr, val, urbs_end);
462                 else {
463                         if (laddr < urbs_end) {
464                                 regnum = ia64_rse_num_regs(bspstore, laddr);
465                                 *ia64_rse_skip_regs(krbs, regnum) = val;
466                         }
467                 }
468         } else if (access_process_vm(child, addr, &val, sizeof(val), 1) != sizeof(val)) {
469                 return -EIO;
470         }
471         return 0;
472 }
473
474 /*
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.
479  */
480 unsigned long
481 ia64_get_user_rbs_end (struct task_struct *child, struct pt_regs *pt, unsigned long *cfmp)
482 {
483         unsigned long *krbs, *bspstore, cfm;
484         struct unw_frame_info info;
485         long ndirty;
486
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);
491
492         if (in_syscall(pt)) {
493                 /*
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.
497                  */
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);
502                 }
503         }
504         if (cfmp)
505                 *cfmp = cfm;
506         return (unsigned long) ia64_rse_skip_regs(bspstore, ndirty);
507 }
508
509 /*
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.
513  */
514 long
515 ia64_sync_user_rbs (struct task_struct *child, struct switch_stack *sw,
516                     unsigned long user_rbs_start, unsigned long user_rbs_end)
517 {
518         unsigned long addr, val;
519         long ret;
520
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);
524                 if (ret < 0)
525                         return ret;
526                 if (access_process_vm(child, addr, &val, sizeof(val), 1) != sizeof(val))
527                         return -EIO;
528         }
529         return 0;
530 }
531
532 static inline int
533 thread_matches (struct task_struct *thread, unsigned long addr)
534 {
535         unsigned long thread_rbs_end;
536         struct pt_regs *thread_regs;
537
538         if (ptrace_check_attach(thread, 0) < 0)
539                 /*
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
546                  * can...
547                  */
548                 return 0;
549
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))
553                 return 0;
554
555         return 1;       /* looks like we've got a winner */
556 }
557
558 /*
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.
563  *
564  * task_list_lock must be read-locked before calling this routine!
565  */
566 static struct task_struct *
567 find_thread_for_addr (struct task_struct *child, unsigned long addr)
568 {
569         struct task_struct *g, *p;
570         struct mm_struct *mm;
571         int mm_users;
572
573         if (!(mm = get_task_mm(child)))
574                 return child;
575
576         mm_users = atomic_read(&mm->mm_users) - 1;      /* -1 because of our get_task_mm()... */
577         if (mm_users <= 1)
578                 goto out;               /* not multi-threaded */
579
580         /*
581          * First, traverse the child's thread-list.  Good for scalability with
582          * NPTL-threads.
583          */
584         p = child;
585         do {
586                 if (thread_matches(p, addr)) {
587                         child = p;
588                         goto out;
589                 }
590                 if (mm_users-- <= 1)
591                         goto out;
592         } while ((p = next_thread(p)) != child);
593
594         do_each_thread(g, p) {
595                 if (child->mm != mm)
596                         continue;
597
598                 if (thread_matches(p, addr)) {
599                         child = p;
600                         goto out;
601                 }
602         } while_each_thread(g, p);
603   out:
604         mmput(mm);
605         return child;
606 }
607
608 /*
609  * Write f32-f127 back to task->thread.fph if it has been modified.
610  */
611 inline void
612 ia64_flush_fph (struct task_struct *task)
613 {
614         struct ia64_psr *psr = ia64_psr(ia64_task_regs(task));
615
616         if (ia64_is_local_fpu_owner(task) && psr->mfh) {
617                 psr->mfh = 0;
618                 task->thread.flags |= IA64_THREAD_FPH_VALID;
619                 ia64_save_fpu(&task->thread.fph[0]);
620         }
621 }
622
623 /*
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
629  * executes again.
630  */
631 void
632 ia64_sync_fph (struct task_struct *task)
633 {
634         struct ia64_psr *psr = ia64_psr(ia64_task_regs(task));
635
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));
640         }
641         ia64_drop_fpu(task);
642         psr->dfh = 1;
643 }
644
645 static int
646 access_fr (struct unw_frame_info *info, int regnum, int hi, unsigned long *data, int write_access)
647 {
648         struct ia64_fpreg fpval;
649         int ret;
650
651         ret = unw_get_fr(info, regnum, &fpval);
652         if (ret < 0)
653                 return ret;
654
655         if (write_access) {
656                 fpval.u.bits[hi] = *data;
657                 ret = unw_set_fr(info, regnum, fpval);
658         } else
659                 *data = fpval.u.bits[hi];
660         return ret;
661 }
662
663 /*
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.
666  */
667 static void
668 convert_to_non_syscall (struct task_struct *child, struct pt_regs  *pt, unsigned long cfm)
669 {
670         struct unw_frame_info info, prev_info;
671         unsigned long ip, pr;
672
673         unw_init_from_blocked_task(&info, child);
674         while (1) {
675                 prev_info = info;
676                 if (unw_unwind(&info) < 0)
677                         return;
678                 if (unw_get_rp(&info, &ip) < 0)
679                         return;
680                 if (ip < FIXADDR_USER_END)
681                         break;
682         }
683
684         unw_get_pr(&prev_info, &pr);
685         pr &= ~pSys;
686         pr |= pNonSys;
687         unw_set_pr(&prev_info, pr);
688
689         pt->cr_ifs = (1UL << 63) | cfm;
690 }
691
692 static int
693 access_uarea (struct task_struct *child, unsigned long addr, unsigned long *data, int write_access)
694 {
695         unsigned long *ptr, regnum, urbs_end, rnat_addr, cfm;
696         struct switch_stack *sw;
697         struct pt_regs *pt;
698
699         pt = ia64_task_regs(child);
700         sw = (struct switch_stack *) (child->thread.ksp + 16);
701
702         if ((addr & 0x7) != 0) {
703                 dprintk("ptrace: unaligned register address 0x%lx\n", addr);
704                 return -1;
705         }
706
707         if (addr < PT_F127 + 16) {
708                 /* accessing fph */
709                 if (write_access)
710                         ia64_sync_fph(child);
711                 else
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;
725                 char nat = 0;
726                 int ret;
727
728                 unw_init_from_blocked_task(&info, child);
729                 if (unw_unwind_to_user(&info) < 0)
730                         return -1;
731
732                 switch (addr) {
733                       case PT_NAT_BITS:
734                         if (write_access) {
735                                 nat_bits = *data;
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");
739                                         return -1;
740                                 }
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);
744                                 }
745                         } else {
746                                 if (unw_get_ar(&info, UNW_AR_UNAT, &scratch_unat) < 0) {
747                                         dprintk("ptrace: failed to read ar.unat\n");
748                                         return -1;
749                                 }
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;
754                                 }
755                                 *data = nat_bits;
756                         }
757                         return 0;
758
759                       case PT_R4: case PT_R5: case PT_R6: case PT_R7:
760                         if (write_access) {
761                                 /* read NaT bit first: */
762                                 unsigned long dummy;
763
764                                 ret = unw_get_gr(&info, (addr - PT_R4)/8 + 4, &dummy, &nat);
765                                 if (ret < 0)
766                                         return ret;
767                         }
768                         return unw_access_gr(&info, (addr - PT_R4)/8 + 4, data, &nat,
769                                              write_access);
770
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);
773
774                       case PT_AR_EC:
775                         return unw_access_ar(&info, UNW_AR_EC, data, write_access);
776
777                       case PT_AR_LC:
778                         return unw_access_ar(&info, UNW_AR_LC, data, write_access);
779
780                       default:
781                         if (addr >= PT_F2 && addr < PT_F5 + 16)
782                                 return access_fr(&info, (addr - PT_F2)/16 + 2, (addr & 8) != 0,
783                                                  data, write_access);
784                         else if (addr >= PT_F16 && addr < PT_F31 + 16)
785                                 return access_fr(&info, (addr - PT_F16)/16 + 16, (addr & 8) != 0,
786                                                  data, write_access);
787                         else {
788                                 dprintk("ptrace: rejecting access to register address 0x%lx\n",
789                                         addr);
790                                 return -1;
791                         }
792                 }
793         } else if (addr < PT_F9+16) {
794                 /* scratch state */
795                 switch (addr) {
796                       case PT_AR_BSP:
797                         /*
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.
801                          *
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).
816                          */
817                         urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
818                         if (write_access) {
819                                 if (*data != urbs_end) {
820                                         if (ia64_sync_user_rbs(child, sw,
821                                                                pt->ar_bspstore, urbs_end) < 0)
822                                                 return -1;
823                                         if (in_syscall(pt))
824                                                 convert_to_non_syscall(child, pt, cfm);
825                                         /* simulate user-level write of ar.bsp: */
826                                         pt->loadrs = 0;
827                                         pt->ar_bspstore = *data;
828                                 }
829                         } else
830                                 *data = urbs_end;
831                         return 0;
832
833                       case PT_CFM:
834                         urbs_end = ia64_get_user_rbs_end(child, pt, &cfm);
835                         if (write_access) {
836                                 if (((cfm ^ *data) & 0x3fffffffffU) != 0) {
837                                         if (ia64_sync_user_rbs(child, sw,
838                                                                pt->ar_bspstore, urbs_end) < 0)
839                                                 return -1;
840                                         if (in_syscall(pt))
841                                                 convert_to_non_syscall(child, pt, cfm);
842                                         pt->cr_ifs = ((pt->cr_ifs & ~0x3fffffffffUL)
843                                                       | (*data & 0x3fffffffffUL));
844                                 }
845                         } else
846                                 *data = cfm;
847                         return 0;
848
849                       case PT_CR_IPSR:
850                         if (write_access)
851                                 pt->cr_ipsr = ((*data & IPSR_WRITE_MASK)
852                                                | (pt->cr_ipsr & ~IPSR_WRITE_MASK));
853                         else
854                                 *data = (pt->cr_ipsr & IPSR_READ_MASK);
855                         return 0;
856
857                       case PT_AR_RNAT:
858                         urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
859                         rnat_addr = (long) ia64_rse_rnat_addr((long *) urbs_end);
860                         if (write_access)
861                                 return ia64_poke(child, sw, urbs_end, rnat_addr, *data);
862                         else
863                                 return ia64_peek(child, sw, urbs_end, rnat_addr, data);
864
865                       case PT_R1:
866                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, r1));
867                         break;
868
869                       case PT_R2:  case PT_R3:
870                         ptr = (unsigned long *)
871                                 ((long) pt + offsetof(struct pt_regs, r2) + addr - PT_R2);
872                         break;
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);
876                         break;
877                       case PT_R12: case PT_R13:
878                         ptr = (unsigned long *)
879                                 ((long) pt + offsetof(struct pt_regs, r12)+  addr - PT_R12);
880                         break;
881                       case PT_R14:
882                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, r14));
883                         break;
884                       case PT_R15:
885                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, r15));
886                         break;
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);
893                         break;
894                       case PT_B0:
895                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, b0));
896                         break;
897                       case PT_B6:
898                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, b6));
899                         break;
900                       case PT_B7:
901                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, b7));
902                         break;
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);
907                         break;
908                       case PT_AR_BSPSTORE:
909                         ptr = (unsigned long *)
910                                 ((long) pt + offsetof(struct pt_regs, ar_bspstore));
911                         break;
912                       case PT_AR_RSC:
913                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_rsc));
914                         break;
915                       case PT_AR_UNAT:
916                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_unat));
917                         break;
918                       case PT_AR_PFS:
919                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_pfs));
920                         break;
921                       case PT_AR_CCV:
922                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_ccv));
923                         break;
924                       case PT_AR_FPSR:
925                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, ar_fpsr));
926                         break;
927                       case PT_CR_IIP:
928                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, cr_iip));
929                         break;
930                       case PT_PR:
931                         ptr = (unsigned long *) ((long) pt + offsetof(struct pt_regs, pr));
932                         break;
933                         /* scratch register */
934
935                       default:
936                         /* disallow accessing anything else... */
937                         dprintk("ptrace: rejecting access to register address 0x%lx\n",
938                                 addr);
939                         return -1;
940                 }
941         } else if (addr <= PT_AR_SSD) {
942                 ptr = (unsigned long *)
943                         ((long) pt + offsetof(struct pt_regs, ar_csd) + addr - PT_AR_CSD);
944         } else {
945                 /* access debug registers */
946
947                 if (addr >= PT_IBR) {
948                         regnum = (addr - PT_IBR) >> 3;
949                         ptr = &child->thread.ibr[0];
950                 } else {
951                         regnum = (addr - PT_DBR) >> 3;
952                         ptr = &child->thread.dbr[0];
953                 }
954
955                 if (regnum >= 8) {
956                         dprintk("ptrace: rejecting access to register address 0x%lx\n", addr);
957                         return -1;
958                 }
959 #ifdef CONFIG_PERFMON
960                 /*
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.
964                  *
965                  * Perfmon needs to keep a count of how many processes are trying to
966                  * modify the debug registers for system wide monitoring sessions.
967                  *
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
972                  * switch code.
973                  */
974                 if (pfm_use_debug_registers(child)) return -1;
975 #endif
976
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));
981                 }
982
983                 ptr += regnum;
984
985                 if (write_access)
986                         /* don't let the user set kernel-level breakpoints... */
987                         *ptr = *data & ~(7UL << 56);
988                 else
989                         *data = *ptr;
990                 return 0;
991         }
992         if (write_access)
993                 *ptr = *data;
994         else
995                 *data = *ptr;
996         return 0;
997 }
998
999 static long
1000 ptrace_getregs (struct task_struct *child, struct pt_all_user_regs *ppr)
1001 {
1002         struct switch_stack *sw;
1003         struct pt_regs *pt;
1004         long ret, retval;
1005         struct unw_frame_info info;
1006         char nat = 0;
1007         int i;
1008
1009         retval = verify_area(VERIFY_WRITE, ppr, sizeof(struct pt_all_user_regs));
1010         if (retval != 0) {
1011                 return -EIO;
1012         }
1013
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) {
1018                 return -EIO;
1019         }
1020
1021         if (((unsigned long) ppr & 0x7) != 0) {
1022                 dprintk("ptrace:unaligned register address %p\n", ppr);
1023                 return -EIO;
1024         }
1025
1026         retval = 0;
1027
1028         /* control regs */
1029
1030         retval |= __put_user(pt->cr_iip, &ppr->cr_iip);
1031         retval |= access_uarea(child, PT_CR_IPSR, &ppr->cr_ipsr, 0);
1032
1033         /* app regs */
1034
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]);
1041
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);
1047
1048         /* gr1-gr3 */
1049
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);
1052
1053         /* gr4-gr7 */
1054
1055         for (i = 4; i < 8; i++) {
1056                 retval |= unw_access_gr(&info, i, &ppr->gr[i], &nat, 0);
1057         }
1058
1059         /* gr8-gr11 */
1060
1061         retval |= __copy_to_user(&ppr->gr[8], &pt->r8, sizeof(long) * 4);
1062
1063         /* gr12-gr15 */
1064
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));
1068
1069         /* gr16-gr31 */
1070
1071         retval |= __copy_to_user(&ppr->gr[16], &pt->r16, sizeof(long) * 16);
1072
1073         /* b0 */
1074
1075         retval |= __put_user(pt->b0, &ppr->br[0]);
1076
1077         /* b1-b5 */
1078
1079         for (i = 1; i < 6; i++) {
1080                 retval |= unw_access_br(&info, i, &ppr->br[i], 0);
1081         }
1082
1083         /* b6-b7 */
1084
1085         retval |= __put_user(pt->b6, &ppr->br[6]);
1086         retval |= __put_user(pt->b7, &ppr->br[7]);
1087
1088         /* fr2-fr5 */
1089
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);
1093         }
1094
1095         /* fr6-fr11 */
1096
1097         retval |= __copy_to_user(&ppr->fr[6], &pt->f6, sizeof(struct ia64_fpreg) * 6);
1098
1099         /* fp scratch regs(12-15) */
1100
1101         retval |= __copy_to_user(&ppr->fr[12], &sw->f12, sizeof(struct ia64_fpreg) * 4);
1102
1103         /* fr16-fr31 */
1104
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);
1108         }
1109
1110         /* fph */
1111
1112         ia64_flush_fph(child);
1113         retval |= __copy_to_user(&ppr->fr[32], &child->thread.fph, sizeof(ppr->fr[32]) * 96);
1114
1115         /*  preds */
1116
1117         retval |= __put_user(pt->pr, &ppr->pr);
1118
1119         /* nat bits */
1120
1121         retval |= access_uarea(child, PT_NAT_BITS, &ppr->nat, 0);
1122
1123         ret = retval ? -EIO : 0;
1124         return ret;
1125 }
1126
1127 static long
1128 ptrace_setregs (struct task_struct *child, struct pt_all_user_regs *ppr)
1129 {
1130         struct switch_stack *sw;
1131         struct pt_regs *pt;
1132         long ret, retval;
1133         struct unw_frame_info info;
1134         char nat = 0;
1135         int i;
1136
1137         retval = verify_area(VERIFY_READ, ppr, sizeof(struct pt_all_user_regs));
1138         if (retval != 0) {
1139                 return -EIO;
1140         }
1141
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) {
1146                 return -EIO;
1147         }
1148
1149         if (((unsigned long) ppr & 0x7) != 0) {
1150                 dprintk("ptrace:unaligned register address %p\n", ppr);
1151                 return -EIO;
1152         }
1153
1154         retval = 0;
1155
1156         /* control regs */
1157
1158         retval |= __get_user(pt->cr_iip, &ppr->cr_iip);
1159         retval |= access_uarea(child, PT_CR_IPSR, &ppr->cr_ipsr, 1);
1160
1161         /* app regs */
1162
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]);
1169
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);
1175
1176         /* gr1-gr3 */
1177
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);
1180
1181         /* gr4-gr7 */
1182
1183         for (i = 4; i < 8; i++) {
1184                 long ret = unw_get_gr(&info, i, &ppr->gr[i], &nat);
1185                 if (ret < 0) {
1186                         return ret;
1187                 }
1188                 retval |= unw_access_gr(&info, i, &ppr->gr[i], &nat, 1);
1189         }
1190
1191         /* gr8-gr11 */
1192
1193         retval |= __copy_from_user(&pt->r8, &ppr->gr[8], sizeof(long) * 4);
1194
1195         /* gr12-gr15 */
1196
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));
1200
1201         /* gr16-gr31 */
1202
1203         retval |= __copy_from_user(&pt->r16, &ppr->gr[16], sizeof(long) * 16);
1204
1205         /* b0 */
1206
1207         retval |= __get_user(pt->b0, &ppr->br[0]);
1208
1209         /* b1-b5 */
1210
1211         for (i = 1; i < 6; i++) {
1212                 retval |= unw_access_br(&info, i, &ppr->br[i], 1);
1213         }
1214
1215         /* b6-b7 */
1216
1217         retval |= __get_user(pt->b6, &ppr->br[6]);
1218         retval |= __get_user(pt->b7, &ppr->br[7]);
1219
1220         /* fr2-fr5 */
1221
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);
1225         }
1226
1227         /* fr6-fr11 */
1228
1229         retval |= __copy_from_user(&pt->f6, &ppr->fr[6], sizeof(ppr->fr[6]) * 6);
1230
1231         /* fp scratch regs(12-15) */
1232
1233         retval |= __copy_from_user(&sw->f12, &ppr->fr[12], sizeof(ppr->fr[12]) * 4);
1234
1235         /* fr16-fr31 */
1236
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);
1240         }
1241
1242         /* fph */
1243
1244         ia64_sync_fph(child);
1245         retval |= __copy_from_user(&child->thread.fph, &ppr->fr[32], sizeof(ppr->fr[32]) * 96);
1246
1247         /* preds */
1248
1249         retval |= __get_user(pt->pr, &ppr->pr);
1250
1251         /* nat bits */
1252
1253         retval |= access_uarea(child, PT_NAT_BITS, &ppr->nat, 1);
1254
1255         ret = retval ? -EIO : 0;
1256         return ret;
1257 }
1258
1259 /*
1260  * Called by kernel/ptrace.c when detaching..
1261  *
1262  * Make sure the single step bit is not set.
1263  */
1264 void
1265 ptrace_disable (struct task_struct *child)
1266 {
1267         struct ia64_psr *child_psr = ia64_psr(ia64_task_regs(child));
1268
1269         /* make sure the single step/take-branch tra bits are not set: */
1270         child_psr->ss = 0;
1271         child_psr->tb = 0;
1272 }
1273
1274 asmlinkage long
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)
1277 {
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;
1282         long ret;
1283
1284         lock_kernel();
1285         ret = -EPERM;
1286         if (request == PTRACE_TRACEME) {
1287                 /* are we already being traced? */
1288                 if (current->ptrace & PT_PTRACED)
1289                         goto out;
1290                 ret = security_ptrace(current->parent, current);
1291                 if (ret)
1292                         goto out;
1293                 current->ptrace |= PT_PTRACED;
1294                 ret = 0;
1295                 goto out;
1296         }
1297
1298         peek_or_poke = (request == PTRACE_PEEKTEXT || request == PTRACE_PEEKDATA
1299                         || request == PTRACE_POKETEXT || request == PTRACE_POKEDATA);
1300         ret = -ESRCH;
1301         read_lock(&tasklist_lock);
1302         {
1303                 child = find_task_by_pid(pid);
1304                 if (child) {
1305                         if (peek_or_poke)
1306                                 child = find_thread_for_addr(child, addr);
1307                         get_task_struct(child);
1308                 }
1309         }
1310         read_unlock(&tasklist_lock);
1311         if (!child)
1312                 goto out;
1313         ret = -EPERM;
1314         if (pid == 1)           /* no messing around with init! */
1315                 goto out_tsk;
1316
1317         if (request == PTRACE_ATTACH) {
1318                 ret = ptrace_attach(child);
1319                 goto out_tsk;
1320         }
1321
1322         ret = ptrace_check_attach(child, request == PTRACE_KILL);
1323         if (ret < 0)
1324                 goto out_tsk;
1325
1326         pt = ia64_task_regs(child);
1327         sw = (struct switch_stack *) (child->thread.ksp + 16);
1328
1329         switch (request) {
1330               case PTRACE_PEEKTEXT:
1331               case PTRACE_PEEKDATA:             /* read word at location addr */
1332                 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
1333                 ret = ia64_peek(child, sw, urbs_end, addr, &data);
1334                 if (ret == 0) {
1335                         ret = data;
1336                         regs->r8 = 0;   /* ensure "ret" is not mistaken as an error code */
1337                 }
1338                 goto out_tsk;
1339
1340               case PTRACE_POKETEXT:
1341               case PTRACE_POKEDATA:             /* write the word at location addr */
1342                 urbs_end = ia64_get_user_rbs_end(child, pt, NULL);
1343                 ret = ia64_poke(child, sw, urbs_end, addr, data);
1344                 goto out_tsk;
1345
1346               case PTRACE_PEEKUSR:              /* read the word at addr in the USER area */
1347                 if (access_uarea(child, addr, &data, 0) < 0) {
1348                         ret = -EIO;
1349                         goto out_tsk;
1350                 }
1351                 ret = data;
1352                 regs->r8 = 0;   /* ensure "ret" is not mistaken as an error code */
1353                 goto out_tsk;
1354
1355               case PTRACE_POKEUSR:            /* write the word at addr in the USER area */
1356                 if (access_uarea(child, addr, &data, 1) < 0) {
1357                         ret = -EIO;
1358                         goto out_tsk;
1359                 }
1360                 ret = 0;
1361                 goto out_tsk;
1362
1363               case PTRACE_OLD_GETSIGINFO:               /* for backwards-compatibility */
1364                 ret = ptrace_request(child, PTRACE_GETSIGINFO, addr, data);
1365                 goto out_tsk;
1366
1367               case PTRACE_OLD_SETSIGINFO:               /* for backwards-compatibility */
1368                 ret = ptrace_request(child, PTRACE_SETSIGINFO, addr, data);
1369                 goto out_tsk;
1370
1371               case PTRACE_SYSCALL:      /* continue and stop at next (return from) syscall */
1372               case PTRACE_CONT:         /* restart after signal. */
1373                 ret = -EIO;
1374                 if (data > _NSIG)
1375                         goto out_tsk;
1376                 if (request == PTRACE_SYSCALL)
1377                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1378                 else
1379                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1380                 child->exit_code = data;
1381
1382                 /* make sure the single step/taken-branch trap bits are not set: */
1383                 ia64_psr(pt)->ss = 0;
1384                 ia64_psr(pt)->tb = 0;
1385
1386                 wake_up_process(child);
1387                 ret = 0;
1388                 goto out_tsk;
1389
1390               case PTRACE_KILL:
1391                 /*
1392                  * Make the child exit.  Best I can do is send it a
1393                  * sigkill.  Perhaps it should be put in the status
1394                  * that it wants to exit.
1395                  */
1396                 if (child->state == TASK_ZOMBIE)                /* already dead */
1397                         goto out_tsk;
1398                 child->exit_code = SIGKILL;
1399
1400                 /* make sure the single step/take-branch tra bits are not set: */
1401                 ia64_psr(pt)->ss = 0;
1402                 ia64_psr(pt)->tb = 0;
1403
1404                 wake_up_process(child);
1405                 ret = 0;
1406                 goto out_tsk;
1407
1408               case PTRACE_SINGLESTEP:           /* let child execute for one instruction */
1409               case PTRACE_SINGLEBLOCK:
1410                 ret = -EIO;
1411                 if (data > _NSIG)
1412                         goto out_tsk;
1413
1414                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
1415                 if (request == PTRACE_SINGLESTEP) {
1416                         ia64_psr(pt)->ss = 1;
1417                 } else {
1418                         ia64_psr(pt)->tb = 1;
1419                 }
1420                 child->exit_code = data;
1421
1422                 /* give it a chance to run. */
1423                 wake_up_process(child);
1424                 ret = 0;
1425                 goto out_tsk;
1426
1427               case PTRACE_DETACH:               /* detach a process that was attached. */
1428                 ret = ptrace_detach(child, data);
1429                 goto out_tsk;
1430
1431               case PTRACE_GETREGS:
1432                 ret = ptrace_getregs(child, (struct pt_all_user_regs*) data);
1433                 goto out_tsk;
1434
1435               case PTRACE_SETREGS:
1436                 ret = ptrace_setregs(child, (struct pt_all_user_regs*) data);
1437                 goto out_tsk;
1438
1439               default:
1440                 ret = ptrace_request(child, request, addr, data);
1441                 goto out_tsk;
1442         }
1443   out_tsk:
1444         put_task_struct(child);
1445   out:
1446         unlock_kernel();
1447         return ret;
1448 }
1449
1450 /* "asmlinkage" so the input arguments are preserved... */
1451
1452 asmlinkage void
1453 syscall_trace (void)
1454 {
1455         if (!test_thread_flag(TIF_SYSCALL_TRACE))
1456                 return;
1457         if (!(current->ptrace & PT_PTRACED))
1458                 return;
1459         /*
1460          * The 0x80 provides a way for the tracing parent to distinguish between a syscall
1461          * stop and SIGTRAP delivery.
1462          */
1463         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
1464
1465         /*
1466          * This isn't the same as continuing with a signal, but it will do for normal use.
1467          * strace only continues with a signal if the stopping signal is not SIGTRAP.
1468          * -brl
1469          */
1470         if (current->exit_code) {
1471                 send_sig(current->exit_code, current, 1);
1472                 current->exit_code = 0;
1473         }
1474 }