ptrace: change signature of arch_ptrace()
[pandora-kernel.git] / arch / cris / arch-v10 / kernel / ptrace.c
1 /*
2  * Copyright (C) 2000-2003, Axis Communications AB.
3  */
4
5 #include <linux/kernel.h>
6 #include <linux/sched.h>
7 #include <linux/mm.h>
8 #include <linux/smp.h>
9 #include <linux/errno.h>
10 #include <linux/ptrace.h>
11 #include <linux/user.h>
12 #include <linux/signal.h>
13 #include <linux/security.h>
14
15 #include <asm/uaccess.h>
16 #include <asm/page.h>
17 #include <asm/pgtable.h>
18 #include <asm/system.h>
19 #include <asm/processor.h>
20
21 /* 
22  * Determines which bits in DCCR the user has access to.
23  * 1 = access, 0 = no access.
24  */
25 #define DCCR_MASK 0x0000001f     /* XNZVC */
26
27 /*
28  * Get contents of register REGNO in task TASK.
29  */
30 inline long get_reg(struct task_struct *task, unsigned int regno)
31 {
32         /* USP is a special case, it's not in the pt_regs struct but
33          * in the tasks thread struct
34          */
35
36         if (regno == PT_USP)
37                 return task->thread.usp;
38         else if (regno < PT_MAX)
39                 return ((unsigned long *)task_pt_regs(task))[regno];
40         else
41                 return 0;
42 }
43
44 /*
45  * Write contents of register REGNO in task TASK.
46  */
47 inline int put_reg(struct task_struct *task, unsigned int regno,
48                           unsigned long data)
49 {
50         if (regno == PT_USP)
51                 task->thread.usp = data;
52         else if (regno < PT_MAX)
53                 ((unsigned long *)task_pt_regs(task))[regno] = data;
54         else
55                 return -1;
56         return 0;
57 }
58
59 /*
60  * Called by kernel/ptrace.c when detaching.
61  *
62  * Make sure the single step bit is not set.
63  */
64 void 
65 ptrace_disable(struct task_struct *child)
66 {
67        /* Todo - pending singlesteps? */
68        clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
69 }
70
71 /* 
72  * Note that this implementation of ptrace behaves differently from vanilla
73  * ptrace.  Contrary to what the man page says, in the PTRACE_PEEKTEXT,
74  * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
75  * ignored.  Instead, the data variable is expected to point at a location
76  * (in user space) where the result of the ptrace call is written (instead of
77  * being returned).
78  */
79 long arch_ptrace(struct task_struct *child, long request,
80                  unsigned long addr, unsigned long data)
81 {
82         int ret;
83         unsigned long __user *datap = (unsigned long __user *)data;
84
85         switch (request) {
86                 /* Read word at location address. */ 
87                 case PTRACE_PEEKTEXT:
88                 case PTRACE_PEEKDATA:
89                         ret = generic_ptrace_peekdata(child, addr, data);
90                         break;
91
92                 /* Read the word at location address in the USER area. */
93                 case PTRACE_PEEKUSR: {
94                         unsigned long tmp;
95
96                         ret = -EIO;
97                         if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
98                                 break;
99
100                         tmp = get_reg(child, addr >> 2);
101                         ret = put_user(tmp, datap);
102                         break;
103                 }
104                 
105                 /* Write the word at location address. */
106                 case PTRACE_POKETEXT:
107                 case PTRACE_POKEDATA:
108                         ret = generic_ptrace_pokedata(child, addr, data);
109                         break;
110  
111                 /* Write the word at location address in the USER area. */
112                 case PTRACE_POKEUSR:
113                         ret = -EIO;
114                         if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
115                                 break;
116
117                         addr >>= 2;
118
119                         if (addr == PT_DCCR) {
120                                 /* don't allow the tracing process to change stuff like
121                                  * interrupt enable, kernel/user bit, dma enables etc.
122                                  */
123                                 data &= DCCR_MASK;
124                                 data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
125                         }
126                         if (put_reg(child, addr, data))
127                                 break;
128                         ret = 0;
129                         break;
130
131                 /* Get all GP registers from the child. */
132                 case PTRACE_GETREGS: {
133                         int i;
134                         unsigned long tmp;
135                         
136                         ret = 0;
137                         for (i = 0; i <= PT_MAX; i++) {
138                                 tmp = get_reg(child, i);
139                                 
140                                 if (put_user(tmp, datap)) {
141                                         ret = -EFAULT;
142                                         break;
143                                 }
144                                 
145                                 data += sizeof(unsigned long);
146                         }
147
148                         break;
149                 }
150
151                 /* Set all GP registers in the child. */
152                 case PTRACE_SETREGS: {
153                         int i;
154                         unsigned long tmp;
155                         
156                         ret = 0;
157                         for (i = 0; i <= PT_MAX; i++) {
158                                 if (get_user(tmp, datap)) {
159                                         ret = -EFAULT;
160                                         break;
161                                 }
162                                 
163                                 if (i == PT_DCCR) {
164                                         tmp &= DCCR_MASK;
165                                         tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
166                                 }
167                                 
168                                 put_reg(child, i, tmp);
169                                 data += sizeof(unsigned long);
170                         }
171                         
172                         break;
173                 }
174
175                 default:
176                         ret = ptrace_request(child, request, addr, data);
177                         break;
178         }
179
180         return ret;
181 }
182
183 void do_syscall_trace(void)
184 {
185         if (!test_thread_flag(TIF_SYSCALL_TRACE))
186                 return;
187         
188         if (!(current->ptrace & PT_PTRACED))
189                 return;
190         
191         /* the 0x80 provides a way for the tracing parent to distinguish
192            between a syscall stop and SIGTRAP delivery */
193         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
194                                  ? 0x80 : 0));
195         
196         /*
197          * This isn't the same as continuing with a signal, but it will do for
198          * normal use.
199          */
200         if (current->exit_code) {
201                 send_sig(current->exit_code, current, 1);
202                 current->exit_code = 0;
203         }
204 }