bd0842059d115c86c01bae9981d2642fe6d0d6d7
[safe/jmp/linux-2.6] / arch / m68k / kernel / ptrace.c
1 /*
2  *  linux/arch/m68k/kernel/ptrace.c
3  *
4  *  Copyright (C) 1994 by Hamish Macdonald
5  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
6  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
7  *
8  * This file is subject to the terms and conditions of the GNU General
9  * Public License.  See the file COPYING in the main directory of
10  * this archive for more details.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/errno.h>
18 #include <linux/ptrace.h>
19 #include <linux/user.h>
20 #include <linux/signal.h>
21
22 #include <asm/uaccess.h>
23 #include <asm/page.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/processor.h>
27
28 /*
29  * does not yet catch signals sent when the child dies.
30  * in exit.c or in signal.c.
31  */
32
33 /* determines which bits in the SR the user has access to. */
34 /* 1 = access 0 = no access */
35 #define SR_MASK 0x001f
36
37 /* sets the trace bits. */
38 #define TRACE_BITS 0xC000
39 #define T1_BIT 0x8000
40 #define T0_BIT 0x4000
41
42 /* Find the stack offset for a register, relative to thread.esp0. */
43 #define PT_REG(reg)     ((long)&((struct pt_regs *)0)->reg)
44 #define SW_REG(reg)     ((long)&((struct switch_stack *)0)->reg \
45                          - sizeof(struct switch_stack))
46 /* Mapping from PT_xxx to the stack offset at which the register is
47    saved.  Notice that usp has no stack-slot and needs to be treated
48    specially (see get_reg/put_reg below). */
49 static int regoff[] = {
50         [0]     = PT_REG(d1),
51         [1]     = PT_REG(d2),
52         [2]     = PT_REG(d3),
53         [3]     = PT_REG(d4),
54         [4]     = PT_REG(d5),
55         [5]     = SW_REG(d6),
56         [6]     = SW_REG(d7),
57         [7]     = PT_REG(a0),
58         [8]     = PT_REG(a1),
59         [9]     = PT_REG(a2),
60         [10]    = SW_REG(a3),
61         [11]    = SW_REG(a4),
62         [12]    = SW_REG(a5),
63         [13]    = SW_REG(a6),
64         [14]    = PT_REG(d0),
65         [15]    = -1,
66         [16]    = PT_REG(orig_d0),
67         [17]    = PT_REG(sr),
68         [18]    = PT_REG(pc),
69 };
70
71 /*
72  * Get contents of register REGNO in task TASK.
73  */
74 static inline long get_reg(struct task_struct *task, int regno)
75 {
76         unsigned long *addr;
77
78         if (regno == PT_USP)
79                 addr = &task->thread.usp;
80         else if (regno < ARRAY_SIZE(regoff))
81                 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
82         else
83                 return 0;
84         return *addr;
85 }
86
87 /*
88  * Write contents of register REGNO in task TASK.
89  */
90 static inline int put_reg(struct task_struct *task, int regno,
91                           unsigned long data)
92 {
93         unsigned long *addr;
94
95         if (regno == PT_USP)
96                 addr = &task->thread.usp;
97         else if (regno < ARRAY_SIZE(regoff))
98                 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
99         else
100                 return -1;
101         *addr = data;
102         return 0;
103 }
104
105 /*
106  * Make sure the single step bit is not set.
107  */
108 static inline void singlestep_disable(struct task_struct *child)
109 {
110         unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
111         put_reg(child, PT_SR, tmp);
112         clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
113 }
114
115 /*
116  * Called by kernel/ptrace.c when detaching..
117  */
118 void ptrace_disable(struct task_struct *child)
119 {
120         singlestep_disable(child);
121 }
122
123 void user_enable_single_step(struct task_struct *child)
124 {
125         unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
126         put_reg(child, PT_SR, tmp | (T1_BIT << 16));
127         set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
128 }
129
130 void user_enable_block_step(struct task_struct *child)
131 {
132         unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
133         put_reg(child, PT_SR, tmp | (T0_BIT << 16));
134 }
135
136 void user_disable_single_step(struct task_struct *child)
137 {
138         singlestep_disable(child);
139 }
140
141 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
142 {
143         unsigned long tmp;
144         int i, ret = 0;
145
146         switch (request) {
147         /* read the word at location addr in the USER area. */
148         case PTRACE_PEEKUSR:
149                 if (addr & 3)
150                         goto out_eio;
151                 addr >>= 2;     /* temporary hack. */
152
153                 if (addr >= 0 && addr < 19) {
154                         tmp = get_reg(child, addr);
155                         if (addr == PT_SR)
156                                 tmp >>= 16;
157                 } else if (addr >= 21 && addr < 49) {
158                         tmp = child->thread.fp[addr - 21];
159                         /* Convert internal fpu reg representation
160                          * into long double format
161                          */
162                         if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
163                                 tmp = ((tmp & 0xffff0000) << 15) |
164                                       ((tmp & 0x0000ffff) << 16);
165                 } else
166                         break;
167                 ret = put_user(tmp, (unsigned long *)data);
168                 break;
169
170         case PTRACE_POKEUSR:    /* write the word at location addr in the USER area */
171                 if (addr & 3)
172                         goto out_eio;
173                 addr >>= 2;     /* temporary hack. */
174
175                 if (addr == PT_SR) {
176                         data &= SR_MASK;
177                         data <<= 16;
178                         data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
179                 } else if (addr >= 0 && addr < 19) {
180                         if (put_reg(child, addr, data))
181                                 goto out_eio;
182                 } else if (addr >= 21 && addr < 48) {
183                         /* Convert long double format
184                          * into internal fpu reg representation
185                          */
186                         if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
187                                 data = (unsigned long)data << 15;
188                                 data = (data & 0xffff0000) |
189                                        ((data & 0x0000ffff) >> 1);
190                         }
191                         child->thread.fp[addr - 21] = data;
192                 } else
193                         goto out_eio;
194                 break;
195
196         case PTRACE_GETREGS:    /* Get all gp regs from the child. */
197                 for (i = 0; i < 19; i++) {
198                         tmp = get_reg(child, i);
199                         if (i == PT_SR)
200                                 tmp >>= 16;
201                         ret = put_user(tmp, (unsigned long *)data);
202                         if (ret)
203                                 break;
204                         data += sizeof(long);
205                 }
206                 break;
207
208         case PTRACE_SETREGS:    /* Set all gp regs in the child. */
209                 for (i = 0; i < 19; i++) {
210                         ret = get_user(tmp, (unsigned long *)data);
211                         if (ret)
212                                 break;
213                         if (i == PT_SR) {
214                                 tmp &= SR_MASK;
215                                 tmp <<= 16;
216                                 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
217                         }
218                         put_reg(child, i, tmp);
219                         data += sizeof(long);
220                 }
221                 break;
222
223         case PTRACE_GETFPREGS:  /* Get the child FPU state. */
224                 if (copy_to_user((void *)data, &child->thread.fp,
225                                  sizeof(struct user_m68kfp_struct)))
226                         ret = -EFAULT;
227                 break;
228
229         case PTRACE_SETFPREGS:  /* Set the child FPU state. */
230                 if (copy_from_user(&child->thread.fp, (void *)data,
231                                    sizeof(struct user_m68kfp_struct)))
232                         ret = -EFAULT;
233                 break;
234
235         default:
236                 ret = ptrace_request(child, request, addr, data);
237                 break;
238         }
239
240         return ret;
241 out_eio:
242         return -EIO;
243 }
244
245 asmlinkage void syscall_trace(void)
246 {
247         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
248                                  ? 0x80 : 0));
249         /*
250          * this isn't the same as continuing with a signal, but it will do
251          * for normal use.  strace only continues with a signal if the
252          * stopping signal is not SIGTRAP.  -brl
253          */
254         if (current->exit_code) {
255                 send_sig(current->exit_code, current, 1);
256                 current->exit_code = 0;
257         }
258 }