eb4d56205d6951bf308280290541b99d5946980d
[linux-flexiantxendom0-3.2.10.git] / arch / h8300 / kernel / ptrace.c
1 /*
2  *  linux/arch/h8300/kernel/ptrace.c
3  *
4  *  Yoshinori Sato <qzb04471@nifty.ne.jp>
5  *
6  *  Based on:
7  *  linux/arch/m68k/kernel/ptrace.c
8  *
9  *  Copyright (C) 1994 by Hamish Macdonald
10  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
11  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
12  *
13  * This file is subject to the terms and conditions of the GNU General
14  * Public License.  See the file COPYING in the main directory of
15  * this archive for more details.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/mm.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/errno.h>
24 #include <linux/ptrace.h>
25 #include <linux/user.h>
26 #include <linux/config.h>
27
28 #include <asm/uaccess.h>
29 #include <asm/page.h>
30 #include <asm/pgtable.h>
31 #include <asm/system.h>
32 #include <asm/processor.h>
33 #include <asm/signal.h>
34
35 /*
36  * does not yet catch signals sent when the child dies.
37  * in exit.c or in signal.c.
38  */
39
40 /* determines which bits in the SR the user has access to. */
41 /* 1 = access 0 = no access */
42 #define SR_MASK 0x001f
43
44 /* sets the trace bits. */
45 #define TRACE_BITS 0x8000
46
47 /* Find the stack offset for a register, relative to thread.esp0. */
48 #define PT_REG(reg)     ((long)&((struct pt_regs *)0)->reg)
49 #define SW_REG(reg)     ((long)&((struct switch_stack *)0)->reg \
50                          - sizeof(struct switch_stack))
51 /* Mapping from PT_xxx to the stack offset at which the register is
52    saved.  Notice that usp has no stack-slot and needs to be treated
53    specially (see get_reg/put_reg below). */
54 static const int regoff[] = {
55         PT_REG(er1), PT_REG(er2), PT_REG(er3), SW_REG(er4),
56         SW_REG(er5), SW_REG(er6), PT_REG(er0), PT_REG(orig_er0),
57         PT_REG(ccr), PT_REG(pc)
58 };
59
60 /*
61  * Get contents of register REGNO in task TASK.
62  */
63 static inline long get_reg(struct task_struct *task, int regno)
64 {
65         unsigned long *addr;
66
67         if (regno == PT_USP)
68                 addr = &task->thread.usp;
69         else if (regno < sizeof(regoff)/sizeof(regoff[0]))
70                 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
71         else
72                 return 0;
73         return *addr;
74 }
75
76 /*
77  * Write contents of register REGNO in task TASK.
78  */
79 static inline int put_reg(struct task_struct *task, int regno,
80                           unsigned long data)
81 {
82         unsigned long *addr;
83
84         if (regno == PT_USP)
85                 addr = &task->thread.usp;
86         else if (regno < sizeof(regoff)/sizeof(regoff[0]))
87                 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
88         else
89                 return -1;
90         *addr = data;
91         return 0;
92 }
93
94 /*
95  * Called by kernel/ptrace.c when detaching..
96  *
97  * Make sure the single step bit is not set.
98  */
99 int ptrace_cancel_bpt(struct task_struct *child)
100 {
101         int i,r=0;
102
103         for(i=0; i<4; i++) {
104                 if (child->thread.debugreg[i]) {
105                         if (child->thread.debugreg[i] != ~0)
106                                 put_user(child->thread.debugreg[i+4],
107                                          (unsigned short *)child->thread.debugreg[i]);
108                         r = 1;
109                         child->thread.debugreg[i] = 0;
110                 }
111         }
112         return r;
113 }
114
115 const static unsigned char opcode0[]={
116   0x04,0x02,0x04,0x02,0x04,0x02,0x04,0x02,  /* 0x58 */
117   0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,  /* 0x60 */
118   0x02,0x02,0x11,0x11,0x02,0x02,0x04,0x04,  /* 0x68 */
119   0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,  /* 0x70 */
120   0x08,0x04,0x06,0x04,0x04,0x04,0x04,0x04}; /* 0x78 */
121
122 const static int table_parser01(unsigned char *pc);
123 const static int table_parser02(unsigned char *pc);
124 const static int table_parser100(unsigned char *pc);
125 const static int table_parser101(unsigned char *pc);
126
127 const static int (*parsers[])(unsigned char *pc)={table_parser01,table_parser02};
128
129 static int insn_length(unsigned char *pc)
130 {
131   if (*pc == 0x01)
132     return table_parser01(pc+1);
133   if (*pc < 0x58 || *pc>=0x80) 
134     return 2;
135   else
136     if (opcode0[*pc-0x58]<0x10)
137       return opcode0[*pc-0x58];
138     else
139       return (*parsers[opcode0[*pc-0x58]-0x10])(pc+1);
140 }
141
142 const static int table_parser01(unsigned char *pc)
143 {
144   const unsigned char codelen[]={0x10,0x00,0x00,0x00,0x11,0x00,0x00,0x00,
145                                  0x02,0x00,0x00,0x00,0x04,0x04,0x00,0x04};
146   const static int (*parsers[])(unsigned char *)={table_parser100,table_parser101};
147   unsigned char second_index;
148   second_index = (*pc) >> 4;
149   if (codelen[second_index]<0x10)
150     return codelen[second_index];
151   else
152     return parsers[codelen[second_index]-0x10](pc);
153 }
154
155 const static int table_parser02(unsigned char *pc)
156 {
157   return (*pc & 0x20)?0x06:0x04;
158 }
159
160 const static int table_parser100(unsigned char *pc)
161 {
162   return (*(pc+2) & 0x02)?0x08:0x06;
163 }
164
165 const static int table_parser101(unsigned char *pc)
166 {
167   return (*(pc+2) & 0x02)?0x08:0x06;
168 }
169
170 #define BREAK_INST 0x5730 /* TRAPA #3 */
171
172 int ptrace_set_bpt(struct task_struct *child)
173 {
174         unsigned long pc,next;
175         unsigned short insn;
176         pc = get_reg(child,PT_PC);
177         next = insn_length((unsigned char *)pc) + pc;
178         get_user(insn,(unsigned short *)pc);
179         if (insn == 0x5470) {
180                 /* rts */ 
181                 unsigned long sp;
182                 sp = get_reg(child,PT_USP);
183                 get_user(next,(unsigned long *)sp);
184         } else if ((insn & 0xfb00) != 0x5800) {
185                 /* jmp / jsr */
186                 int regs;
187                 const short reg_tbl[]={PT_ER0,PT_ER1,PT_ER2,PT_ER3,
188                                        PT_ER4,PT_ER5,PT_ER6,PT_USP};
189                 switch(insn & 0xfb00) {
190                         case 0x5900:
191                                regs = (insn & 0x0070) >> 8;
192                                next = get_reg(child,reg_tbl[regs]);
193                                break;
194                         case 0x5a00:
195                                get_user(next,(unsigned long *)(pc+2));
196                                next &= 0x00ffffff;
197                                break;
198                         case 0x5b00:
199                                /* unneccessary? */
200                                next = *(unsigned long *)(insn & 0xff);
201                                break;
202                 }
203         } else if (((insn & 0xf000) == 0x4000) || ((insn &0xff00) == 0x5500)) { 
204                 /* b**:8 */
205                 unsigned long dsp;
206                 dsp = (long)(insn && 0xff)+pc+2;
207                 child->thread.debugreg[1] = dsp;
208                 get_user(child->thread.debugreg[5],(unsigned short *)dsp);
209                 put_user(BREAK_INST,(unsigned short *)dsp);
210         } else if (((insn & 0xff00) == 0x5800) || ((insn &0xff00) == 0x5c00)) { 
211                 /* b**:16 */
212                 unsigned long dsp;
213                 get_user(dsp,(unsigned short *)(pc+2));
214                 dsp = (long)dsp+pc+4;
215                 child->thread.debugreg[1] = dsp;
216                 get_user(child->thread.debugreg[5],(unsigned short *)dsp);
217                 put_user(BREAK_INST,(unsigned short *)dsp);
218         }
219         child->thread.debugreg[0] = next;
220         get_user(child->thread.debugreg[4],(unsigned short *)next);
221         put_user(BREAK_INST,(unsigned short *)next);
222         return 0;
223 }
224
225 inline
226 static int read_long(struct task_struct * tsk, unsigned long addr,
227         unsigned long * result)
228 {
229         *result = *(unsigned long *)addr;
230         return 0;
231 }
232
233 void ptrace_disable(struct task_struct *child)
234 {
235         ptrace_cancel_bpt(child);
236 }
237
238 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
239 {
240         struct task_struct *child;
241         int ret;
242
243         lock_kernel();
244         ret = -EPERM;
245         if (request == PTRACE_TRACEME) {
246                 /* are we already being traced? */
247                 if (current->ptrace & PT_PTRACED)
248                         goto out;
249                 /* set the ptrace bit in the process flags. */
250                 current->ptrace |= PT_PTRACED;
251                 ret = 0;
252                 goto out;
253         }
254         ret = -ESRCH;
255         read_lock(&tasklist_lock);
256         child = find_task_by_pid(pid);
257         if (child)
258                 get_task_struct(child);
259         read_unlock(&tasklist_lock);
260         if (!child)
261                 goto out;
262
263         ret = -EPERM;
264         if (pid == 1)           /* you may not mess with init */
265                 goto out_tsk;
266
267         if (request == PTRACE_ATTACH) {
268                 ret = ptrace_attach(child);
269                 goto out_tsk;
270         }
271         ret = -ESRCH;
272         if (!(child->ptrace & PT_PTRACED))
273                 goto out_tsk;
274         if (child->state != TASK_STOPPED) {
275                 if (request != PTRACE_KILL)
276                         goto out_tsk;
277         }
278         ret = ptrace_check_attach(child, request == PTRACE_KILL);
279         if (ret < 0)
280                 goto out_tsk;
281
282         switch (request) {
283                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
284                 case PTRACE_PEEKDATA: {
285                         unsigned long tmp;
286
287                         ret = read_long(child, addr, &tmp);
288                         if (ret < 0)
289                                 break ;
290                         ret = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
291                         if (!ret)
292                                 put_user(tmp, (unsigned long *) data);
293                         break ;
294                 }
295
296         /* read the word at location addr in the USER area. */
297                 case PTRACE_PEEKUSR: {
298                         unsigned long tmp;
299                         
300                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
301                                 ret = -EIO;
302                         
303                         ret = verify_area(VERIFY_WRITE, (void *) data,
304                                           sizeof(long));
305                         if (ret)
306                                 break ;
307                         tmp = 0;  /* Default return condition */
308                         addr = addr >> 2; /* temporary hack. */
309                         if (addr < 10)
310                                 tmp = get_reg(child, addr);
311                         else {
312                                 ret = -EIO;
313                                 break ;
314                         }
315                         put_user(tmp,(unsigned long *) data);
316                         ret = 0;
317                         break ;
318                 }
319
320       /* when I and D space are separate, this will have to be fixed. */
321                 case PTRACE_POKETEXT: /* write the word at location addr. */
322                 case PTRACE_POKEDATA:
323                         ret = 0;
324                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
325                                 break;
326                         ret = -EIO;
327                         break;
328
329                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
330                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
331                                 ret = -EIO;
332                                 break ;
333                         }
334                         addr = addr >> 2; /* temporary hack. */
335                             
336                         if (addr == PT_ORIG_ER0) {
337                                 ret = -EIO;
338                                 break ;
339                         }
340                         if (addr == PT_CCR) {
341                                 data &= SR_MASK;
342                         }
343                         if (addr < 10) {
344                                 if (put_reg(child, addr, data))
345                                         ret = -EIO;
346                                 else
347                                         ret = 0;
348                                 break ;
349                         }
350                         ret = -EIO;
351                         break ;
352                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
353                 case PTRACE_CONT: { /* restart after signal. */
354                         ret = -EIO;
355                         if ((unsigned long) data >= _NSIG)
356                                 break ;
357                         if (request == PTRACE_SYSCALL)
358                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
359                         else
360                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
361                         child->exit_code = data;
362                         wake_up_process(child);
363                         /* make sure the single step bit is not set. */
364                         ptrace_cancel_bpt(child);
365                         ret = 0;
366                 }
367
368 /*
369  * make the child exit.  Best I can do is send it a sigkill. 
370  * perhaps it should be put in the status that it wants to 
371  * exit.
372  */
373                 case PTRACE_KILL: {
374
375                         ret = 0;
376                         if (child->state == TASK_ZOMBIE) /* already dead */
377                                 break;
378                         child->exit_code = SIGKILL;
379                         ptrace_cancel_bpt(child);
380                         wake_up_process(child);
381                         break;
382                 }
383
384                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
385                         ret = -EIO;
386                         if ((unsigned long) data > _NSIG)
387                                 break;
388                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
389                         child->thread.debugreg[0]=-1;
390                         child->exit_code = data;
391                         wake_up_process(child);
392                         ret = 0;
393                         break;
394                 }
395
396                 case PTRACE_DETACH:     /* detach a process that was attached. */
397                         ret = ptrace_detach(child, data);
398                         break;
399
400                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
401                         int i;
402                         unsigned long tmp;
403                         for (i = 0; i < 19; i++) {
404                             tmp = get_reg(child, i);
405                             if (put_user(tmp, (unsigned long *) data)) {
406                                 ret = -EFAULT;
407                                 break;
408                             }
409                             data += sizeof(long);
410                         }
411                         ret = 0;
412                         break;
413                 }
414
415                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
416                         int i;
417                         unsigned long tmp;
418                         for (i = 0; i < 10; i++) {
419                             if (get_user(tmp, (unsigned long *) data)) {
420                                 ret = -EFAULT;
421                                 break;
422                             }
423                             put_reg(child, i, tmp);
424                             data += sizeof(long);
425                         }
426                         ret = 0;
427                         break;
428                 }
429
430                 default:
431                         ret = -EIO;
432                         break;
433         }
434 out_tsk:
435         put_task_struct(child);
436 out:
437         unlock_kernel();
438         return ret;
439 }
440
441 asmlinkage void syscall_trace(void)
442 {
443         if (!test_thread_flag(TIF_SYSCALL_TRACE))
444                 return;
445         if (!(current->ptrace & PT_PTRACED))
446                 return;
447         current->exit_code = SIGTRAP;
448         current->state = TASK_STOPPED;
449         notify_parent(current, SIGCHLD);
450         schedule();
451         /*
452          * this isn't the same as continuing with a signal, but it will do
453          * for normal use.  strace only continues with a signal if the
454          * stopping signal is not SIGTRAP.  -brl
455          */
456         if (current->exit_code) {
457                 send_sig(current->exit_code, current, 1);
458                 current->exit_code = 0;
459         }
460 }
461
462 asmlinkage void trace_trap(unsigned long bp)
463 {
464         if (current->thread.debugreg[0] == bp ||
465             current->thread.debugreg[1] == bp) {
466                 ptrace_cancel_bpt(current);
467                 force_sig(SIGTRAP,current);
468         } else
469                 force_sig(SIGILL,current);
470 }