vserver 1.9.5.x5
[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 #include "skas_ptrace.h"
20
21 /*
22  * Called by kernel/ptrace.c when detaching..
23  */
24 void ptrace_disable(struct task_struct *child)
25
26         child->ptrace &= ~PT_DTRACE;
27         child->thread.singlestep_syscall = 0;
28 }
29
30 long sys_ptrace(long request, long pid, long addr, long data)
31 {
32         struct task_struct *child;
33         int i, ret;
34
35         lock_kernel();
36         ret = -EPERM;
37         if (request == PTRACE_TRACEME) {
38                 /* are we already being traced? */
39                 if (current->ptrace & PT_PTRACED)
40                         goto out;
41
42                 ret = security_ptrace(current->parent, current);
43                 if (ret)
44                         goto out;
45
46                 /* set the ptrace bit in the process flags. */
47                 current->ptrace |= PT_PTRACED;
48                 ret = 0;
49                 goto out;
50         }
51         ret = -ESRCH;
52         read_lock(&tasklist_lock);
53         child = find_task_by_pid(pid);
54         if (child)
55                 get_task_struct(child);
56         read_unlock(&tasklist_lock);
57         if (!child)
58                 goto out;
59         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
60                 goto out_tsk;
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 __user *) 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 < MAX_REG_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 __user *) 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 < MAX_REG_OFFSET) {
128                         ret = putreg(child, addr, data);
129                         break;
130                 }
131 #if 0 /* XXX x86_64 */
132                 else if((addr >= offsetof(struct user, u_debugreg[0])) &&
133                         (addr <= offsetof(struct user, u_debugreg[7]))){
134                           addr -= offsetof(struct user, u_debugreg[0]);
135                           addr = addr >> 2;
136                           if((addr == 4) || (addr == 5)) break;
137                           child->thread.arch.debugregs[addr] = data;
138                           ret = 0;
139                 }
140 #endif
141
142                 break;
143
144         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
145         case PTRACE_CONT: { /* restart after signal. */
146                 ret = -EIO;
147                 if ((unsigned long) data > _NSIG)
148                         break;
149
150                 child->ptrace &= ~PT_DTRACE;
151                 child->thread.singlestep_syscall = 0;
152                 if (request == PTRACE_SYSCALL) {
153                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
154                 }
155                 else {
156                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
157                 }
158                 child->exit_code = data;
159                 wake_up_process(child);
160                 ret = 0;
161                 break;
162         }
163
164 /*
165  * make the child exit.  Best I can do is send it a sigkill. 
166  * perhaps it should be put in the status that it wants to 
167  * exit.
168  */
169         case PTRACE_KILL: {
170                 ret = 0;
171                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
172                         break;
173
174                 child->ptrace &= ~PT_DTRACE;
175                 child->thread.singlestep_syscall = 0;
176                 child->exit_code = SIGKILL;
177                 wake_up_process(child);
178                 break;
179         }
180
181         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
182                 ret = -EIO;
183                 if ((unsigned long) data > _NSIG)
184                         break;
185                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
186                 child->ptrace |= PT_DTRACE;
187                 child->thread.singlestep_syscall = 0;
188                 child->exit_code = data;
189                 /* give it a chance to run. */
190                 wake_up_process(child);
191                 ret = 0;
192                 break;
193         }
194
195         case PTRACE_DETACH:
196                 /* detach a process that was attached. */
197                 ret = ptrace_detach(child, data);
198                 break;
199
200 #ifdef PTRACE_GETREGS
201         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
202                 if (!access_ok(VERIFY_WRITE, (unsigned long *)data, 
203                                MAX_REG_OFFSET)) {
204                         ret = -EIO;
205                         break;
206                 }
207                 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
208                         __put_user(getreg(child, i),
209                                    (unsigned long __user *) data);
210                         data += sizeof(long);
211                 }
212                 ret = 0;
213                 break;
214         }
215 #endif
216 #ifdef PTRACE_SETREGS
217         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
218                 unsigned long tmp = 0;
219                 if (!access_ok(VERIFY_READ, (unsigned *)data, 
220                                MAX_REG_OFFSET)) {
221                         ret = -EIO;
222                         break;
223                 }
224                 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
225                         __get_user(tmp, (unsigned long __user *) data);
226                         putreg(child, i, tmp);
227                         data += sizeof(long);
228                 }
229                 ret = 0;
230                 break;
231         }
232 #endif
233 #ifdef PTRACE_GETFPREGS
234         case PTRACE_GETFPREGS: /* Get the child FPU state. */
235                 ret = get_fpregs(data, child);
236                 break;
237 #endif
238 #ifdef PTRACE_SETFPREGS
239         case PTRACE_SETFPREGS: /* Set the child FPU state. */
240                 ret = set_fpregs(data, child);
241                 break;
242 #endif
243 #ifdef PTRACE_GETFPXREGS
244         case PTRACE_GETFPXREGS: /* Get the child FPU state. */
245                 ret = get_fpxregs(data, child);
246                 break;
247 #endif
248 #ifdef PTRACE_SETFPXREGS
249         case PTRACE_SETFPXREGS: /* Set the child FPU state. */
250                 ret = set_fpxregs(data, child);
251                 break;
252 #endif
253         case PTRACE_FAULTINFO: {
254                 struct ptrace_faultinfo fault;
255
256                 fault = ((struct ptrace_faultinfo) 
257                         { .is_write     = child->thread.err,
258                           .addr         = child->thread.cr2 });
259                 ret = copy_to_user((unsigned long __user *) data, &fault,
260                                    sizeof(fault));
261                 if(ret)
262                         break;
263                 break;
264         }
265         case PTRACE_SIGPENDING:
266                 ret = copy_to_user((unsigned long __user *) data,
267                                    &child->pending.signal,
268                                    sizeof(child->pending.signal));
269                 break;
270
271         case PTRACE_LDT: {
272                 struct ptrace_ldt ldt;
273
274                 if(copy_from_user(&ldt, (unsigned long __user *) data,
275                                   sizeof(ldt))){
276                         ret = -EIO;
277                         break;
278                 }
279
280                 /* This one is confusing, so just punt and return -EIO for 
281                  * now
282                  */
283                 ret = -EIO;
284                 break;
285         }
286 #ifdef CONFIG_PROC_MM
287         case PTRACE_SWITCH_MM: {
288                 struct mm_struct *old = child->mm;
289                 struct mm_struct *new = proc_mm_get_mm(data);
290
291                 if(IS_ERR(new)){
292                         ret = PTR_ERR(new);
293                         break;
294                 }
295
296                 atomic_inc(&new->mm_users);
297                 child->mm = new;
298                 child->active_mm = new;
299                 mmput(old);
300                 ret = 0;
301                 break;
302         }
303 #endif
304         default:
305                 ret = ptrace_request(child, request, addr, data);
306                 break;
307         }
308  out_tsk:
309         put_task_struct(child);
310  out:
311         unlock_kernel();
312         return ret;
313 }
314
315 void send_sigtrap(struct task_struct *tsk, union uml_pt_regs *regs,
316                   int error_code)
317 {
318         struct siginfo info;
319
320         memset(&info, 0, sizeof(info));
321         info.si_signo = SIGTRAP;
322         info.si_code = TRAP_BRKPT;
323
324         /* User-mode eip? */
325         info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
326
327         /* Send us the fakey SIGTRAP */
328         force_sig_info(SIGTRAP, &info, tsk);
329 }
330
331 /* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
332  * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
333  */
334 void syscall_trace(union uml_pt_regs *regs, int entryexit)
335 {
336         int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
337         int tracesysgood;
338
339         if (unlikely(current->audit_context)) {
340                 if (!entryexit)
341                         audit_syscall_entry(current, regs->orig_eax,
342                                             regs->ebx, regs->ecx,
343                                             regs->edx, regs->esi);
344                 else
345                         audit_syscall_exit(current, regs->eax);
346         }
347
348         /* Fake a debug trap */
349         if (is_singlestep)
350                 send_sigtrap(current, regs, 0);
351
352         if (!test_thread_flag(TIF_SYSCALL_TRACE))
353                 return;
354
355         if (!(current->ptrace & PT_PTRACED))
356                 return;
357
358         /* the 0x80 provides a way for the tracing parent to distinguish
359            between a syscall stop and SIGTRAP delivery */
360         tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
361         ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
362
363         if (entryexit) /* force do_signal() --> is_syscall() */
364                 set_thread_flag(TIF_SIGPENDING);
365
366         /* this isn't the same as continuing with a signal, but it will do
367          * for normal use.  strace only continues with a signal if the
368          * stopping signal is not SIGTRAP.  -brl
369          */
370         if (current->exit_code) {
371                 send_sig(current->exit_code, current, 1);
372                 current->exit_code = 0;
373         }
374 }
375
376 /*
377  * Overrides for Emacs so that we follow Linus's tabbing style.
378  * Emacs will notice this stuff at the end of the file and automatically
379  * adjust the settings for this buffer only.  This must remain at the end
380  * of the file.
381  * ---------------------------------------------------------------------------
382  * Local variables:
383  * c-file-style: "linux"
384  * End:
385  */