vserver 2.0 rc7
[linux-2.6.git] / arch / mips / kernel / ptrace32.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1992 Ross Biro
7  * Copyright (C) Linus Torvalds
8  * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
9  * Copyright (C) 1996 David S. Miller
10  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
11  * Copyright (C) 1999 MIPS Technologies, Inc.
12  * Copyright (C) 2000 Ulf Carlsson
13  *
14  * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
15  * binaries.
16  */
17 #include <linux/compiler.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/mm.h>
21 #include <linux/errno.h>
22 #include <linux/ptrace.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/user.h>
26 #include <linux/security.h>
27 #include <linux/signal.h>
28
29 #include <asm/cpu.h>
30 #include <asm/fpu.h>
31 #include <asm/mipsregs.h>
32 #include <asm/pgtable.h>
33 #include <asm/page.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <asm/bootinfo.h>
37
38 /*
39  * Tracing a 32-bit process with a 64-bit strace and vice versa will not
40  * work.  I don't know how to fix this.
41  */
42 asmlinkage int sys32_ptrace(int request, int pid, int addr, int data)
43 {
44         struct task_struct *child;
45         int ret;
46
47 #if 0
48         printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
49                (int) request, (int) pid, (unsigned long) addr,
50                (unsigned long) data);
51 #endif
52         lock_kernel();
53         ret = -EPERM;
54         if (request == PTRACE_TRACEME) {
55                 /* are we already being traced? */
56                 if (current->ptrace & PT_PTRACED)
57                         goto out;
58                 if ((ret = security_ptrace(current->parent, current)))
59                         goto out;
60                 /* set the ptrace bit in the process flags. */
61                 current->ptrace |= PT_PTRACED;
62                 ret = 0;
63                 goto out;
64         }
65         ret = -ESRCH;
66         read_lock(&tasklist_lock);
67         child = find_task_by_pid(pid);
68         if (child)
69                 get_task_struct(child);
70         read_unlock(&tasklist_lock);
71         if (!child)
72                 goto out;
73
74         ret = -EPERM;
75         if (pid == 1)           /* you may not mess with init */
76                 goto out_tsk;
77
78         if (request == PTRACE_ATTACH) {
79                 ret = ptrace_attach(child);
80                 goto out_tsk;
81         }
82
83         ret = ptrace_check_attach(child, request == PTRACE_KILL);
84         if (ret < 0)
85                 goto out_tsk;
86
87         switch (request) {
88         /* when I and D space are separate, these will need to be fixed. */
89         case PTRACE_PEEKTEXT: /* read word at location addr. */
90         case PTRACE_PEEKDATA: {
91                 unsigned int tmp;
92                 int copied;
93
94                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
95                 ret = -EIO;
96                 if (copied != sizeof(tmp))
97                         break;
98                 ret = put_user(tmp, (unsigned int *) (unsigned long) data);
99                 break;
100         }
101
102         /* Read the word at location addr in the USER area. */
103         case PTRACE_PEEKUSR: {
104                 struct pt_regs *regs;
105                 unsigned int tmp;
106
107                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
108                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
109                 ret = 0;  /* Default return value. */
110
111                 switch (addr) {
112                 case 0 ... 31:
113                         tmp = regs->regs[addr];
114                         break;
115                 case FPR_BASE ... FPR_BASE + 31:
116                         if (tsk_used_math(child)) {
117                                 fpureg_t *fregs = get_fpu_regs(child);
118
119                                 /*
120                                  * The odd registers are actually the high
121                                  * order bits of the values stored in the even
122                                  * registers - unless we're using r2k_switch.S.
123                                  */
124                                 if (addr & 1)
125                                         tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
126                                 else
127                                         tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
128                         } else {
129                                 tmp = -1;       /* FP not yet used  */
130                         }
131                         break;
132                 case PC:
133                         tmp = regs->cp0_epc;
134                         break;
135                 case CAUSE:
136                         tmp = regs->cp0_cause;
137                         break;
138                 case BADVADDR:
139                         tmp = regs->cp0_badvaddr;
140                         break;
141                 case MMHI:
142                         tmp = regs->hi;
143                         break;
144                 case MMLO:
145                         tmp = regs->lo;
146                         break;
147                 case FPC_CSR:
148                         if (cpu_has_fpu)
149                                 tmp = child->thread.fpu.hard.fcr31;
150                         else
151                                 tmp = child->thread.fpu.soft.fcr31;
152                         break;
153                 case FPC_EIR: { /* implementation / version register */
154                         unsigned int flags;
155
156                         if (!cpu_has_fpu)
157                                 break;
158
159                         flags = read_c0_status();
160                         __enable_fpu();
161                         __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
162                         write_c0_status(flags);
163                         break;
164                 }
165                 default:
166                         tmp = 0;
167                         ret = -EIO;
168                         goto out_tsk;
169                 }
170                 ret = put_user(tmp, (unsigned *) (unsigned long) data);
171                 break;
172         }
173
174         /* when I and D space are separate, this will have to be fixed. */
175         case PTRACE_POKETEXT: /* write the word at location addr. */
176         case PTRACE_POKEDATA:
177                 ret = 0;
178                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
179                     == sizeof(data))
180                         break;
181                 ret = -EIO;
182                 break;
183
184         case PTRACE_POKEUSR: {
185                 struct pt_regs *regs;
186                 ret = 0;
187                 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
188                        THREAD_SIZE - 32 - sizeof(struct pt_regs));
189
190                 switch (addr) {
191                 case 0 ... 31:
192                         regs->regs[addr] = data;
193                         break;
194                 case FPR_BASE ... FPR_BASE + 31: {
195                         fpureg_t *fregs = get_fpu_regs(child);
196
197                         if (!tsk_used_math(child)) {
198                                 /* FP not yet used  */
199                                 memset(&child->thread.fpu.hard, ~0,
200                                        sizeof(child->thread.fpu.hard));
201                                 child->thread.fpu.hard.fcr31 = 0;
202                         }
203                         /*
204                          * The odd registers are actually the high order bits
205                          * of the values stored in the even registers - unless
206                          * we're using r2k_switch.S.
207                          */
208                         if (addr & 1) {
209                                 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
210                                 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
211                         } else {
212                                 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
213                                 /* Must cast, lest sign extension fill upper
214                                    bits!  */
215                                 fregs[addr - FPR_BASE] |= (unsigned int)data;
216                         }
217                         break;
218                 }
219                 case PC:
220                         regs->cp0_epc = data;
221                         break;
222                 case MMHI:
223                         regs->hi = data;
224                         break;
225                 case MMLO:
226                         regs->lo = data;
227                         break;
228                 case FPC_CSR:
229                         if (cpu_has_fpu)
230                                 child->thread.fpu.hard.fcr31 = data;
231                         else
232                                 child->thread.fpu.soft.fcr31 = data;
233                         break;
234                 default:
235                         /* The rest are not allowed. */
236                         ret = -EIO;
237                         break;
238                 }
239                 break;
240                 }
241
242         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
243         case PTRACE_CONT: { /* restart after signal. */
244                 ret = -EIO;
245                 if (!valid_signal(data))
246                         break;
247                 if (request == PTRACE_SYSCALL) {
248                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
249                 }
250                 else {
251                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
252                 }
253                 child->exit_code = data;
254                 wake_up_process(child);
255                 ret = 0;
256                 break;
257         }
258
259         /*
260          * make the child exit.  Best I can do is send it a sigkill.
261          * perhaps it should be put in the status that it wants to
262          * exit.
263          */
264         case PTRACE_KILL:
265                 ret = 0;
266                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
267                         break;
268                 child->exit_code = SIGKILL;
269                 wake_up_process(child);
270                 break;
271
272         case PTRACE_DETACH: /* detach a process that was attached. */
273                 ret = ptrace_detach(child, data);
274                 break;
275
276         default:
277                 ret = ptrace_request(child, request, addr, data);
278                 break;
279         }
280
281 out_tsk:
282         put_task_struct(child);
283 out:
284         unlock_kernel();
285         return ret;
286 }