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