98e09395c093c1d75be57ab82ed3b46c084e533f
[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 #include "linux/audit.h"
13 #ifdef CONFIG_PROC_MM
14 #include "linux/proc_mm.h"
15 #endif
16 #include "asm/ptrace.h"
17 #include "asm/uaccess.h"
18 #include "kern_util.h"
19 #include "skas_ptrace.h"
20 #include "sysdep/ptrace.h"
21
22 static inline void set_singlestepping(struct task_struct *child, int on)
23 {
24         if (on)
25                 child->ptrace |= PT_DTRACE;
26         else
27                 child->ptrace &= ~PT_DTRACE;
28         child->thread.singlestep_syscall = 0;
29
30 #ifdef SUBARCH_SET_SINGLESTEPPING
31         SUBARCH_SET_SINGLESTEPPING(child, on);
32 #endif
33 }
34
35 /*
36  * Called by kernel/ptrace.c when detaching..
37  */
38 void ptrace_disable(struct task_struct *child)
39
40         set_singlestepping(child,0);
41 }
42
43 extern int peek_user(struct task_struct * child, long addr, long data);
44 extern int poke_user(struct task_struct * child, long addr, long data);
45
46 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
47 {
48         int i, ret;
49
50         switch (request) {
51                 /* when I and D space are separate, these will need to be fixed. */
52         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
53         case PTRACE_PEEKDATA: {
54                 unsigned long tmp;
55                 int copied;
56
57                 ret = -EIO;
58                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
59                 if (copied != sizeof(tmp))
60                         break;
61                 ret = put_user(tmp, (unsigned long __user *) data);
62                 break;
63         }
64
65         /* read the word at location addr in the USER area. */
66         case PTRACE_PEEKUSR:
67                 ret = peek_user(child, addr, data);
68                 break;
69
70         /* when I and D space are separate, this will have to be fixed. */
71         case PTRACE_POKETEXT: /* write the word at location addr. */
72         case PTRACE_POKEDATA:
73                 ret = -EIO;
74                 if (access_process_vm(child, addr, &data, sizeof(data), 
75                                       1) != sizeof(data))
76                         break;
77                 ret = 0;
78                 break;
79
80         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
81                 ret = poke_user(child, addr, data);
82                 break;
83
84         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
85         case PTRACE_CONT: { /* restart after signal. */
86                 ret = -EIO;
87                 if (!valid_signal(data))
88                         break;
89
90                 set_singlestepping(child, 0);
91                 if (request == PTRACE_SYSCALL) {
92                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
93                 }
94                 else {
95                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
96                 }
97                 child->exit_code = data;
98                 wake_up_process(child);
99                 ret = 0;
100                 break;
101         }
102
103 /*
104  * make the child exit.  Best I can do is send it a sigkill. 
105  * perhaps it should be put in the status that it wants to 
106  * exit.
107  */
108         case PTRACE_KILL: {
109                 ret = 0;
110                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
111                         break;
112
113                 set_singlestepping(child, 0);
114                 child->exit_code = SIGKILL;
115                 wake_up_process(child);
116                 break;
117         }
118
119         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
120                 ret = -EIO;
121                 if (!valid_signal(data))
122                         break;
123                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
124                 set_singlestepping(child, 1);
125                 child->exit_code = data;
126                 /* give it a chance to run. */
127                 wake_up_process(child);
128                 ret = 0;
129                 break;
130         }
131
132         case PTRACE_DETACH:
133                 /* detach a process that was attached. */
134                 ret = ptrace_detach(child, data);
135                 break;
136
137 #ifdef PTRACE_GETREGS
138         case PTRACE_GETREGS: { /* Get all gp regs from the child. */
139                 if (!access_ok(VERIFY_WRITE, (unsigned long *)data, 
140                                MAX_REG_OFFSET)) {
141                         ret = -EIO;
142                         break;
143                 }
144                 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
145                         __put_user(getreg(child, i),
146                                    (unsigned long __user *) data);
147                         data += sizeof(long);
148                 }
149                 ret = 0;
150                 break;
151         }
152 #endif
153 #ifdef PTRACE_SETREGS
154         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
155                 unsigned long tmp = 0;
156                 if (!access_ok(VERIFY_READ, (unsigned *)data, 
157                                MAX_REG_OFFSET)) {
158                         ret = -EIO;
159                         break;
160                 }
161                 for ( i = 0; i < MAX_REG_OFFSET; i += sizeof(long) ) {
162                         __get_user(tmp, (unsigned long __user *) data);
163                         putreg(child, i, tmp);
164                         data += sizeof(long);
165                 }
166                 ret = 0;
167                 break;
168         }
169 #endif
170 #ifdef PTRACE_GETFPREGS
171         case PTRACE_GETFPREGS: /* Get the child FPU state. */
172                 ret = get_fpregs(data, child);
173                 break;
174 #endif
175 #ifdef PTRACE_SETFPREGS
176         case PTRACE_SETFPREGS: /* Set the child FPU state. */
177                 ret = set_fpregs(data, child);
178                 break;
179 #endif
180 #ifdef PTRACE_GETFPXREGS
181         case PTRACE_GETFPXREGS: /* Get the child FPU state. */
182                 ret = get_fpxregs(data, child);
183                 break;
184 #endif
185 #ifdef PTRACE_SETFPXREGS
186         case PTRACE_SETFPXREGS: /* Set the child FPU state. */
187                 ret = set_fpxregs(data, child);
188                 break;
189 #endif
190         case PTRACE_FAULTINFO: {
191                 /* Take the info from thread->arch->faultinfo,
192                  * but transfer max. sizeof(struct ptrace_faultinfo).
193                  * On i386, ptrace_faultinfo is smaller!
194                  */
195                 ret = copy_to_user((unsigned long __user *) data,
196                                    &child->thread.arch.faultinfo,
197                                    sizeof(struct ptrace_faultinfo));
198                 if(ret)
199                         break;
200                 break;
201         }
202
203 #ifdef PTRACE_LDT
204         case PTRACE_LDT: {
205                 struct ptrace_ldt ldt;
206
207                 if(copy_from_user(&ldt, (unsigned long __user *) data,
208                                   sizeof(ldt))){
209                         ret = -EIO;
210                         break;
211                 }
212
213                 /* This one is confusing, so just punt and return -EIO for 
214                  * now
215                  */
216                 ret = -EIO;
217                 break;
218         }
219 #endif
220 #ifdef CONFIG_PROC_MM
221         case PTRACE_SWITCH_MM: {
222                 struct mm_struct *old = child->mm;
223                 struct mm_struct *new = proc_mm_get_mm(data);
224
225                 if(IS_ERR(new)){
226                         ret = PTR_ERR(new);
227                         break;
228                 }
229
230                 atomic_inc(&new->mm_users);
231                 child->mm = new;
232                 child->active_mm = new;
233                 mmput(old);
234                 ret = 0;
235                 break;
236         }
237 #endif
238         default:
239                 ret = ptrace_request(child, request, addr, data);
240                 break;
241         }
242
243         return ret;
244 }
245
246 void send_sigtrap(struct task_struct *tsk, union uml_pt_regs *regs,
247                   int error_code)
248 {
249         struct siginfo info;
250
251         memset(&info, 0, sizeof(info));
252         info.si_signo = SIGTRAP;
253         info.si_code = TRAP_BRKPT;
254
255         /* User-mode eip? */
256         info.si_addr = UPT_IS_USER(regs) ? (void __user *) UPT_IP(regs) : NULL;
257
258         /* Send us the fakey SIGTRAP */
259         force_sig_info(SIGTRAP, &info, tsk);
260 }
261
262 /* XXX Check PT_DTRACE vs TIF_SINGLESTEP for singlestepping check and
263  * PT_PTRACED vs TIF_SYSCALL_TRACE for syscall tracing check
264  */
265 void syscall_trace(union uml_pt_regs *regs, int entryexit)
266 {
267         int is_singlestep = (current->ptrace & PT_DTRACE) && entryexit;
268         int tracesysgood;
269
270         if (unlikely(current->audit_context)) {
271                 if (!entryexit)
272                         audit_syscall_entry(current,
273                                             HOST_AUDIT_ARCH,
274                                             UPT_SYSCALL_NR(regs),
275                                             UPT_SYSCALL_ARG1(regs),
276                                             UPT_SYSCALL_ARG2(regs),
277                                             UPT_SYSCALL_ARG3(regs),
278                                             UPT_SYSCALL_ARG4(regs));
279                 else audit_syscall_exit(current,
280                                         AUDITSC_RESULT(UPT_SYSCALL_RET(regs)),
281                                         UPT_SYSCALL_RET(regs));
282         }
283
284         /* Fake a debug trap */
285         if (is_singlestep)
286                 send_sigtrap(current, regs, 0);
287
288         if (!test_thread_flag(TIF_SYSCALL_TRACE))
289                 return;
290
291         if (!(current->ptrace & PT_PTRACED))
292                 return;
293
294         /* the 0x80 provides a way for the tracing parent to distinguish
295            between a syscall stop and SIGTRAP delivery */
296         tracesysgood = (current->ptrace & PT_TRACESYSGOOD);
297         ptrace_notify(SIGTRAP | (tracesysgood ? 0x80 : 0));
298
299         if (entryexit) /* force do_signal() --> is_syscall() */
300                 set_thread_flag(TIF_SIGPENDING);
301
302         /* this isn't the same as continuing with a signal, but it will do
303          * for normal use.  strace only continues with a signal if the
304          * stopping signal is not SIGTRAP.  -brl
305          */
306         if (current->exit_code) {
307                 send_sig(current->exit_code, current, 1);
308                 current->exit_code = 0;
309         }
310 }