19911e3187be37453bceb66c8dc48d88f34d2e2f
[safe/jmp/linux-2.6] / 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
271         switch (request) {
272         /* Read the word at location addr in the USER area.  */
273         case PTRACE_PEEKUSR: {
274                 struct pt_regs *regs;
275                 unsigned long tmp;
276
277                 regs = task_pt_regs(child);
278
279                 tmp = 0;  /* Default return value. */
280                 switch (addr) {
281                 case 0 ... 31:
282                         tmp = regs->regs[addr];
283                         break;
284                 case PC:
285                         tmp = regs->cp0_epc;
286                         break;
287                 case ECR:
288                         tmp = regs->cp0_ecr;
289                         break;
290                 case EMA:
291                         tmp = regs->cp0_ema;
292                         break;
293                 case CEH:
294                         tmp = regs->ceh;
295                         break;
296                 case CEL:
297                         tmp = regs->cel;
298                         break;
299                 case CONDITION:
300                         tmp = regs->cp0_condition;
301                         break;
302                 case PSR:
303                         tmp = regs->cp0_psr;
304                         break;
305                 case COUNTER:
306                         tmp = regs->sr0;
307                         break;
308                 case LDCR:
309                         tmp = regs->sr1;
310                         break;
311                 case STCR:
312                         tmp = regs->sr2;
313                         break;
314                 default:
315                         tmp = 0;
316                         return -EIO;
317                 }
318
319                 ret = put_user(tmp, (unsigned long *) data);
320                 return ret;
321         }
322
323         case PTRACE_POKEUSR: {
324                 struct pt_regs *regs;
325                 ret = 0;
326                 regs = task_pt_regs(child);
327
328                 switch (addr) {
329                 case 0 ... 31:
330                         regs->regs[addr] = data;
331                         break;
332                 case PC:
333                         regs->cp0_epc = data;
334                         break;
335                 case CEH:
336                         regs->ceh = data;
337                         break;
338                 case CEL:
339                         regs->cel = data;
340                         break;
341                 case CONDITION:
342                         regs->cp0_condition = data;
343                         break;
344                 case PSR:
345                 case COUNTER:
346                 case STCR:
347                 case LDCR:
348                         break; /* user can't write the reg */
349                 default:
350                         /* The rest are not allowed. */
351                         ret = -EIO;
352                         break;
353                 }
354                 break;
355         }
356
357         case PTRACE_GETREGS:
358                 ret = ptrace_getregs(child, (void __user *)data);
359                 break;
360
361         case PTRACE_SETREGS:
362                 ret = ptrace_setregs(child, (void __user *)data);
363                 break;
364
365         default:
366                 ret = ptrace_request(child, request, addr, data);
367                 break;
368         }
369
370         return ret;
371 }
372
373 /*
374  * Notification of system call entry/exit
375  * - triggered by current->work.syscall_trace
376  */
377 asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
378 {
379         if (!(current->ptrace & PT_PTRACED))
380                 return;
381
382         if (!test_thread_flag(TIF_SYSCALL_TRACE))
383                 return;
384
385         /* The 0x80 provides a way for the tracing parent to distinguish
386            between a syscall stop and SIGTRAP delivery. */
387         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
388                         0x80 : 0));
389
390         /*
391          * this isn't the same as continuing with a signal, but it will do
392          * for normal use.  strace only continues with a signal if the
393          * stopping signal is not SIGTRAP.  -brl
394          */
395         if (current->exit_code) {
396                 send_sig(current->exit_code, current, 1);
397                 current->exit_code = 0;
398         }
399 }