score: add address space annotations
[linux-2.6.git] / arch / score / kernel / ptrace.c
1 /*
2  * arch/score/kernel/ptrace.c
3  *
4  * Score Processor version.
5  *
6  * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
7  *  Chen Liqin <liqin.chen@sunplusct.com>
8  *  Lennox Wu <lennox.wu@sunplusct.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see the file COPYING, or write
22  * to the Free Software Foundation, Inc.,
23  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/ptrace.h>
28
29 #include <asm/uaccess.h>
30
31 static int is_16bitinsn(unsigned long insn)
32 {
33         if ((insn & INSN32_MASK) == INSN32_MASK)
34                 return 0;
35         else
36                 return 1;
37 }
38
39 int
40 read_tsk_long(struct task_struct *child,
41                 unsigned long addr, unsigned long *res)
42 {
43         int copied;
44
45         copied = access_process_vm(child, addr, res, sizeof(*res), 0);
46
47         return copied != sizeof(*res) ? -EIO : 0;
48 }
49
50 int
51 read_tsk_short(struct task_struct *child,
52                 unsigned long addr, unsigned short *res)
53 {
54         int copied;
55
56         copied = access_process_vm(child, addr, res, sizeof(*res), 0);
57
58         return copied != sizeof(*res) ? -EIO : 0;
59 }
60
61 static int
62 write_tsk_short(struct task_struct *child,
63                 unsigned long addr, unsigned short val)
64 {
65         int copied;
66
67         copied = access_process_vm(child, addr, &val, sizeof(val), 1);
68
69         return copied != sizeof(val) ? -EIO : 0;
70 }
71
72 static int
73 write_tsk_long(struct task_struct *child,
74                 unsigned long addr, unsigned long val)
75 {
76         int copied;
77
78         copied = access_process_vm(child, addr, &val, sizeof(val), 1);
79
80         return copied != sizeof(val) ? -EIO : 0;
81 }
82
83 /*
84  * Get all user integer registers.
85  */
86 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
87 {
88         struct pt_regs *regs = task_pt_regs(tsk);
89
90         return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
91 }
92
93 /*
94  * Set all user integer registers.
95  */
96 static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
97 {
98         struct pt_regs newregs;
99         int ret;
100
101         ret = -EFAULT;
102         if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
103                 struct pt_regs *regs = task_pt_regs(tsk);
104                 *regs = newregs;
105                 ret = 0;
106         }
107
108         return ret;
109 }
110
111 void user_enable_single_step(struct task_struct *child)
112 {
113         /* far_epc is the target of branch */
114         unsigned int epc, far_epc = 0;
115         unsigned long epc_insn, far_epc_insn;
116         int ninsn_type;                 /* next insn type 0=16b, 1=32b */
117         unsigned int tmp, tmp2;
118         struct pt_regs *regs = task_pt_regs(child);
119         child->thread.single_step = 1;
120         child->thread.ss_nextcnt = 1;
121         epc = regs->cp0_epc;
122
123         read_tsk_long(child, epc, &epc_insn);
124
125         if (is_16bitinsn(epc_insn)) {
126                 if ((epc_insn & J16M) == J16) {
127                         tmp = epc_insn & 0xFFE;
128                         epc = (epc & 0xFFFFF000) | tmp;
129                 } else if ((epc_insn & B16M) == B16) {
130                         child->thread.ss_nextcnt = 2;
131                         tmp = (epc_insn & 0xFF) << 1;
132                         tmp = tmp << 23;
133                         tmp = (unsigned int)((int) tmp >> 23);
134                         far_epc = epc + tmp;
135                         epc += 2;
136                 } else if ((epc_insn & BR16M) == BR16) {
137                         child->thread.ss_nextcnt = 2;
138                         tmp = (epc_insn >> 4) & 0xF;
139                         far_epc = regs->regs[tmp];
140                         epc += 2;
141                 } else
142                         epc += 2;
143         } else {
144                 if ((epc_insn & J32M) == J32) {
145                         tmp = epc_insn & 0x03FFFFFE;
146                         tmp2 = tmp & 0x7FFF;
147                         tmp = (((tmp >> 16) & 0x3FF) << 15) | tmp2;
148                         epc = (epc & 0xFFC00000) | tmp;
149                 } else if ((epc_insn & B32M) == B32) {
150                         child->thread.ss_nextcnt = 2;
151                         tmp = epc_insn & 0x03FFFFFE;    /* discard LK bit */
152                         tmp2 = tmp & 0x3FF;
153                         tmp = (((tmp >> 16) & 0x3FF) << 10) | tmp2; /* 20bit */
154                         tmp = tmp << 12;
155                         tmp = (unsigned int)((int) tmp >> 12);
156                         far_epc = epc + tmp;
157                         epc += 4;
158                 } else if ((epc_insn & BR32M) == BR32) {
159                         child->thread.ss_nextcnt = 2;
160                         tmp = (epc_insn >> 16) & 0x1F;
161                         far_epc = regs->regs[tmp];
162                         epc += 4;
163                 } else
164                         epc += 4;
165         }
166
167         if (child->thread.ss_nextcnt == 1) {
168                 read_tsk_long(child, epc, &epc_insn);
169
170                 if (is_16bitinsn(epc_insn)) {
171                         write_tsk_short(child, epc, SINGLESTEP16_INSN);
172                         ninsn_type = 0;
173                 } else {
174                         write_tsk_long(child, epc, SINGLESTEP32_INSN);
175                         ninsn_type = 1;
176                 }
177
178                 if (ninsn_type == 0) {  /* 16bits */
179                         child->thread.insn1_type = 0;
180                         child->thread.addr1 = epc;
181                          /* the insn may have 32bit data */
182                         child->thread.insn1 = (short)epc_insn;
183                 } else {
184                         child->thread.insn1_type = 1;
185                         child->thread.addr1 = epc;
186                         child->thread.insn1 = epc_insn;
187                 }
188         } else {
189                 /* branch! have two target child->thread.ss_nextcnt=2 */
190                 read_tsk_long(child, epc, &epc_insn);
191                 read_tsk_long(child, far_epc, &far_epc_insn);
192                 if (is_16bitinsn(epc_insn)) {
193                         write_tsk_short(child, epc, SINGLESTEP16_INSN);
194                         ninsn_type = 0;
195                 } else {
196                         write_tsk_long(child, epc, SINGLESTEP32_INSN);
197                         ninsn_type = 1;
198                 }
199
200                 if (ninsn_type == 0) {  /* 16bits */
201                         child->thread.insn1_type = 0;
202                         child->thread.addr1 = epc;
203                          /* the insn may have 32bit data */
204                         child->thread.insn1 = (short)epc_insn;
205                 } else {
206                         child->thread.insn1_type = 1;
207                         child->thread.addr1 = epc;
208                         child->thread.insn1 = epc_insn;
209                 }
210
211                 if (is_16bitinsn(far_epc_insn)) {
212                         write_tsk_short(child, far_epc, SINGLESTEP16_INSN);
213                         ninsn_type = 0;
214                 } else {
215                         write_tsk_long(child, far_epc, SINGLESTEP32_INSN);
216                         ninsn_type = 1;
217                 }
218
219                 if (ninsn_type == 0) {  /* 16bits */
220                         child->thread.insn2_type = 0;
221                         child->thread.addr2 = far_epc;
222                          /* the insn may have 32bit data */
223                         child->thread.insn2 = (short)far_epc_insn;
224                 } else {
225                         child->thread.insn2_type = 1;
226                         child->thread.addr2 = far_epc;
227                         child->thread.insn2 = far_epc_insn;
228                 }
229         }
230 }
231
232 void user_disable_single_step(struct task_struct *child)
233 {
234         if (child->thread.insn1_type == 0)
235                 write_tsk_short(child, child->thread.addr1,
236                                 child->thread.insn1);
237
238         if (child->thread.insn1_type == 1)
239                 write_tsk_long(child, child->thread.addr1,
240                                 child->thread.insn1);
241
242         if (child->thread.ss_nextcnt == 2) {    /* branch */
243                 if (child->thread.insn1_type == 0)
244                         write_tsk_short(child, child->thread.addr1,
245                                         child->thread.insn1);
246                 if (child->thread.insn1_type == 1)
247                         write_tsk_long(child, child->thread.addr1,
248                                         child->thread.insn1);
249                 if (child->thread.insn2_type == 0)
250                         write_tsk_short(child, child->thread.addr2,
251                                         child->thread.insn2);
252                 if (child->thread.insn2_type == 1)
253                         write_tsk_long(child, child->thread.addr2,
254                                         child->thread.insn2);
255         }
256
257         child->thread.single_step = 0;
258         child->thread.ss_nextcnt = 0;
259 }
260
261 void ptrace_disable(struct task_struct *child)
262 {
263         user_disable_single_step(child);
264 }
265
266 long
267 arch_ptrace(struct task_struct *child, long request, long addr, long data)
268 {
269         int ret;
270         unsigned long __user *datap = (void __user *)data;
271
272         switch (request) {
273         /* Read the word at location addr in the USER area.  */
274         case PTRACE_PEEKUSR: {
275                 struct pt_regs *regs;
276                 unsigned long tmp;
277
278                 regs = task_pt_regs(child);
279
280                 tmp = 0;  /* Default return value. */
281                 switch (addr) {
282                 case 0 ... 31:
283                         tmp = regs->regs[addr];
284                         break;
285                 case PC:
286                         tmp = regs->cp0_epc;
287                         break;
288                 case ECR:
289                         tmp = regs->cp0_ecr;
290                         break;
291                 case EMA:
292                         tmp = regs->cp0_ema;
293                         break;
294                 case CEH:
295                         tmp = regs->ceh;
296                         break;
297                 case CEL:
298                         tmp = regs->cel;
299                         break;
300                 case CONDITION:
301                         tmp = regs->cp0_condition;
302                         break;
303                 case PSR:
304                         tmp = regs->cp0_psr;
305                         break;
306                 case COUNTER:
307                         tmp = regs->sr0;
308                         break;
309                 case LDCR:
310                         tmp = regs->sr1;
311                         break;
312                 case STCR:
313                         tmp = regs->sr2;
314                         break;
315                 default:
316                         tmp = 0;
317                         return -EIO;
318                 }
319
320                 ret = put_user(tmp, (unsigned int __user *) datap);
321                 return ret;
322         }
323
324         case PTRACE_POKEUSR: {
325                 struct pt_regs *regs;
326                 ret = 0;
327                 regs = task_pt_regs(child);
328
329                 switch (addr) {
330                 case 0 ... 31:
331                         regs->regs[addr] = data;
332                         break;
333                 case PC:
334                         regs->cp0_epc = data;
335                         break;
336                 case CEH:
337                         regs->ceh = data;
338                         break;
339                 case CEL:
340                         regs->cel = data;
341                         break;
342                 case CONDITION:
343                         regs->cp0_condition = data;
344                         break;
345                 case PSR:
346                 case COUNTER:
347                 case STCR:
348                 case LDCR:
349                         break; /* user can't write the reg */
350                 default:
351                         /* The rest are not allowed. */
352                         ret = -EIO;
353                         break;
354                 }
355                 break;
356         }
357
358         case PTRACE_GETREGS:
359                 ret = ptrace_getregs(child, (void __user *)datap);
360                 break;
361
362         case PTRACE_SETREGS:
363                 ret = ptrace_setregs(child, (void __user *)datap);
364                 break;
365
366         default:
367                 ret = ptrace_request(child, request, addr, data);
368                 break;
369         }
370
371         return ret;
372 }
373
374 /*
375  * Notification of system call entry/exit
376  * - triggered by current->work.syscall_trace
377  */
378 asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
379 {
380         if (!(current->ptrace & PT_PTRACED))
381                 return;
382
383         if (!test_thread_flag(TIF_SYSCALL_TRACE))
384                 return;
385
386         /* The 0x80 provides a way for the tracing parent to distinguish
387            between a syscall stop and SIGTRAP delivery. */
388         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
389                         0x80 : 0));
390
391         /*
392          * this isn't the same as continuing with a signal, but it will do
393          * for normal use.  strace only continues with a signal if the
394          * stopping signal is not SIGTRAP.  -brl
395          */
396         if (current->exit_code) {
397                 send_sig(current->exit_code, current, 1);
398                 current->exit_code = 0;
399         }
400 }