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