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