c68c937f6812b14ce99104e106ded7480c253e3c
[linux-2.6.git] / arch / um / kernel / ptrace.c
1 /* 
2  * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/sched.h"
7 #include "linux/mm.h"
8 #include "linux/errno.h"
9 #include "linux/smp_lock.h"
10 #include "linux/security.h"
11 #include "linux/ptrace.h"
12 #ifdef CONFIG_PROC_MM
13 #include "linux/proc_mm.h"
14 #endif
15 #include "asm/ptrace.h"
16 #include "asm/uaccess.h"
17 #include "kern_util.h"
18 #include "ptrace_user.h"
19
20 /*
21  * Called by kernel/ptrace.c when detaching..
22  */
23 void ptrace_disable(struct task_struct *child)
24
25 }
26
27 extern long do_mmap2(struct task_struct *task, unsigned long addr, 
28                      unsigned long len, unsigned long prot, 
29                      unsigned long flags, unsigned long fd,
30                      unsigned long pgoff);
31
32 int sys_ptrace(long request, long pid, long addr, long data)
33 {
34         struct task_struct *child;
35         int i, ret;
36
37         lock_kernel();
38         ret = -EPERM;
39         if (request == PTRACE_TRACEME) {
40                 /* are we already being traced? */
41                 if (current->ptrace & PT_PTRACED)
42                         goto out;
43
44                 ret = security_ptrace(current->parent, current);
45                 if (ret)
46                         goto out;
47
48                 /* set the ptrace bit in the process flags. */
49                 current->ptrace |= PT_PTRACED;
50                 ret = 0;
51                 goto out;
52         }
53         ret = -ESRCH;
54         read_lock(&tasklist_lock);
55         child = find_task_by_pid(pid);
56         if (child)
57                 get_task_struct(child);
58         read_unlock(&tasklist_lock);
59         if (!child)
60                 goto out;
61
62         ret = -EPERM;
63         if (pid == 1)           /* you may not mess with init */
64                 goto out_tsk;
65
66         if (request == PTRACE_ATTACH) {
67                 ret = ptrace_attach(child);
68                 goto out_tsk;
69         }
70
71         ret = ptrace_check_attach(child, request == PTRACE_KILL);
72         if (ret < 0)
73                 goto out_tsk;
74
75         switch (request) {
76                 /* when I and D space are separate, these will need to be fixed. */
77         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
78         case PTRACE_PEEKDATA: {
79                 unsigned long tmp;
80                 int copied;
81
82                 ret = -EIO;
83                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
84                 if (copied != sizeof(tmp))
85                         break;
86                 ret = put_user(tmp,(unsigned long *) data);
87                 break;
88         }
89
90         /* read the word at location addr in the USER area. */
91         case PTRACE_PEEKUSR: {
92                 unsigned long tmp;
93
94                 ret = -EIO;
95                 if ((addr & 3) || addr < 0) 
96                         break;
97
98                 tmp = 0;  /* Default return condition */
99                 if(addr < FRAME_SIZE_OFFSET){
100                         tmp = getreg(child, addr);
101                 }
102                 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
103                         (addr <= offsetof(struct user, u_debugreg[7]))){
104                         addr -= offsetof(struct user, u_debugreg[0]);
105                         addr = addr >> 2;
106                         tmp = child->thread.arch.debugregs[addr];
107                 }
108                 ret = put_user(tmp, (unsigned long *) data);
109                 break;
110         }
111
112         /* when I and D space are separate, this will have to be fixed. */
113         case PTRACE_POKETEXT: /* write the word at location addr. */
114         case PTRACE_POKEDATA:
115                 ret = -EIO;
116                 if (access_process_vm(child, addr, &data, sizeof(data), 
117                                       1) != sizeof(data))
118                         break;
119                 ret = 0;
120                 break;
121
122         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
123                 ret = -EIO;
124                 if ((addr & 3) || addr < 0)
125                         break;
126
127                 if (addr < FRAME_SIZE_OFFSET) {
128                         ret = putreg(child, addr, data);
129                         break;
130                 }
131                 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
132                         (addr <= offsetof(struct user, u_debugreg[7]))){
133                           addr -= offsetof(struct user, u_debugreg[0]);
134                           addr = addr >> 2;
135                           if((addr == 4) || (addr == 5)) break;
136                           child->thread.arch.debugregs[addr] = data;
137                           ret = 0;
138                 }
139
140                 break;
141
142         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
143         case PTRACE_CONT: { /* restart after signal. */
144                 ret = -EIO;
145                 if ((unsigned long) data > _NSIG)
146                         break;
147                 if (request == PTRACE_SYSCALL) {
148                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
149                 }
150                 else {
151                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
152                 }
153                 child->exit_code = data;
154                 wake_up_process(child);
155                 ret = 0;
156                 break;
157         }
158
159 /*
160  * make the child exit.  Best I can do is send it a sigkill. 
161  * perhaps it should be put in the status that it wants to 
162  * exit.
163  */
164         case PTRACE_KILL: {
165                 ret = 0;
166                 if (child->state == TASK_ZOMBIE)        /* already dead */
167                         break;
168                 child->exit_code = SIGKILL;
169                 wake_up_process(child);
170                 break;
171         }
172
173         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
174                 ret = -EIO;
175                 if ((unsigned long) data > _NSIG)
176                         break;
177                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
178                 child->ptrace |= PT_DTRACE;
179                 child->exit_code = data;
180                 /* give it a chance to run. */
181                 wake_up_process(child);
182                 ret = 0;
183                 break;
184         }
185
186         case PTRACE_DETACH:
187                 /* detach a process that was attached. */
188                 ret = ptrace_detach(child, data);
189                 break;
190
191 #ifdef PTRACE_GETREGS
192         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
193                 if (!access_ok(VERIFY_WRITE, (unsigned long *)data, 
194                                FRAME_SIZE_OFFSET)) {
195                         ret = -EIO;
196                         break;
197                 }
198                 for ( i = 0; i < FRAME_SIZE_OFFSET; i += sizeof(long) ) {
199                         __put_user(getreg(child, i), (unsigned long *) data);
200                         data += sizeof(long);
201                 }
202                 ret = 0;
203                 break;
204         }
205 #endif
206 #ifdef PTRACE_SETREGS
207         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
208                 unsigned long tmp = 0;
209                 if (!access_ok(VERIFY_READ, (unsigned *)data, 
210                                FRAME_SIZE_OFFSET)) {
211                         ret = -EIO;
212                         break;
213                 }
214                 for ( i = 0; i < FRAME_SIZE_OFFSET; i += sizeof(long) ) {
215                         __get_user(tmp, (unsigned long *) data);
216                         putreg(child, i, tmp);
217                         data += sizeof(long);
218                 }
219                 ret = 0;
220                 break;
221         }
222 #endif
223 #ifdef PTRACE_GETFPREGS
224         case PTRACE_GETFPREGS: /* Get the child FPU state. */
225                 ret = get_fpregs(data, child);
226                 break;
227 #endif
228 #ifdef PTRACE_SETFPREGS
229         case PTRACE_SETFPREGS: /* Set the child FPU state. */
230                 ret = set_fpregs(data, child);
231                 break;
232 #endif
233 #ifdef PTRACE_GETFPXREGS
234         case PTRACE_GETFPXREGS: /* Get the child FPU state. */
235                 ret = get_fpxregs(data, child);
236                 break;
237 #endif
238 #ifdef PTRACE_SETFPXREGS
239         case PTRACE_SETFPXREGS: /* Set the child FPU state. */
240                 ret = set_fpxregs(data, child);
241                 break;
242 #endif
243         case PTRACE_FAULTINFO: {
244                 struct ptrace_faultinfo fault;
245
246                 fault = ((struct ptrace_faultinfo) 
247                         { .is_write     = child->thread.err,
248                           .addr         = child->thread.cr2 });
249                 ret = copy_to_user((unsigned long *) data, &fault, 
250                                    sizeof(fault));
251                 if(ret)
252                         break;
253                 break;
254         }
255         case PTRACE_SIGPENDING:
256                 ret = copy_to_user((unsigned long *) data, 
257                                    &child->pending.signal,
258                                    sizeof(child->pending.signal));
259                 break;
260
261         case PTRACE_LDT: {
262                 struct ptrace_ldt ldt;
263
264                 if(copy_from_user(&ldt, (unsigned long *) data, 
265                                   sizeof(ldt))){
266                         ret = -EIO;
267                         break;
268                 }
269
270                 /* This one is confusing, so just punt and return -EIO for 
271                  * now
272                  */
273                 ret = -EIO;
274                 break;
275         }
276 #ifdef CONFIG_PROC_MM
277         case PTRACE_SWITCH_MM: {
278                 struct mm_struct *old = child->mm;
279                 struct mm_struct *new = proc_mm_get_mm(data);
280
281                 if(IS_ERR(new)){
282                         ret = PTR_ERR(new);
283                         break;
284                 }
285
286                 atomic_inc(&new->mm_users);
287                 child->mm = new;
288                 child->active_mm = new;
289                 mmput(old);
290                 ret = 0;
291                 break;
292         }
293 #endif
294         default:
295                 ret = -EIO;
296                 break;
297         }
298  out_tsk:
299         put_task_struct(child);
300  out:
301         unlock_kernel();
302         return ret;
303 }
304
305 void syscall_trace(void)
306 {
307         if (!test_thread_flag(TIF_SYSCALL_TRACE))
308                 return;
309         if (!(current->ptrace & PT_PTRACED))
310                 return;
311
312         /* the 0x80 provides a way for the tracing parent to distinguish
313            between a syscall stop and SIGTRAP delivery */
314         current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
315                                         ? 0x80 : 0);
316         current->state = TASK_STOPPED;
317         notify_parent(current, SIGCHLD);
318         schedule();
319
320         /*
321          * this isn't the same as continuing with a signal, but it will do
322          * for normal use.  strace only continues with a signal if the
323          * stopping signal is not SIGTRAP.  -brl
324          */
325         if (current->exit_code) {
326                 send_sig(current->exit_code, current, 1);
327                 current->exit_code = 0;
328         }
329 }
330
331 /*
332  * Overrides for Emacs so that we follow Linus's tabbing style.
333  * Emacs will notice this stuff at the end of the file and automatically
334  * adjust the settings for this buffer only.  This must remain at the end
335  * of the file.
336  * ---------------------------------------------------------------------------
337  * Local variables:
338  * c-file-style: "linux"
339  * End:
340  */