linux_ptrace.c revision 1.6 1 /* $NetBSD: linux_ptrace.c,v 1.6 2001/05/15 15:20:29 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Scheler.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/proc.h>
44 #include <sys/ptrace.h>
45 #include <sys/systm.h>
46 #include <sys/syscallargs.h>
47 #include <uvm/uvm_extern.h>
48
49 #include <machine/reg.h>
50
51 #include <compat/linux/common/linux_types.h>
52 #include <compat/linux/common/linux_ptrace.h>
53 #include <compat/linux/common/linux_signal.h>
54
55 #include <compat/linux/common/linux_util.h>
56 #include <compat/linux/common/linux_machdep.h>
57 #include <compat/linux/common/linux_emuldata.h>
58 #include <compat/linux/common/linux_exec.h> /* for emul_linux */
59
60 #include <compat/linux/linux_syscallargs.h>
61
62 #include <lib/libkern/libkern.h> /* for offsetof() */
63
64 struct linux_reg {
65 long ebx;
66 long ecx;
67 long edx;
68 long esi;
69 long edi;
70 long ebp;
71 long eax;
72 long xds, xes; /* unsigned short ds, __ds, es, __es; */
73 long __fs, __gs; /* unsigned short fs, __fs, gs, __gs; */
74 long orig_eax;
75 long eip;
76 long xcs; /* unsigned short cs, __cs; */
77 long eflags;
78 long esp;
79 long xss; /* unsigned short ss, __ss; */
80 };
81
82 /* structure used for storing floating point context */
83 struct linux_fpreg {
84 long cwd;
85 long swd;
86 long twd;
87 long fip;
88 long fcs;
89 long foo;
90 long fos;
91 long st_space [20];
92 };
93
94 /* user struct for linux process - this is used for Linux ptrace emulation */
95 /* most of it is junk only used by gdb */
96 struct linux_user {
97 struct linux_reg regs; /* registers */
98 int u_fpvalid; /* true if math co-processor being used. */
99 struct linux_fpreg i387; /* Math Co-processor registers. */
100 /* The rest of this junk is to help gdb figure out what goes where */
101 #define lusr_startgdb u_tsize
102 unsigned long int u_tsize; /* Text segment size (pages). */
103 unsigned long int u_dsize; /* Data segment size (pages). */
104 unsigned long int u_ssize; /* Stack segment size (pages). */
105 unsigned long start_code; /* Starting virtual address of text. */
106 unsigned long start_stack; /* Starting virtual address of stack area.
107 This is actually the bottom of the stack,
108 the top of the stack is always found in the
109 esp register. */
110 long int __signal; /* Signal that caused the core dump. */
111 int __reserved; /* unused */
112 void * u_ar0; /* Used by gdb to help find the values for */
113 /* the registers. */
114 struct linux_fpreg* u_fpstate;/* Math Co-processor pointer. */
115 unsigned long __magic; /* To uniquely identify a core file */
116 char u_comm[32]; /* User command that was responsible */
117 int u_debugreg[8];
118 #define u_debugreg_end u_debugreg[7]
119 };
120
121 #define LUSR_OFF(member) offsetof(struct linux_user, member)
122 #define ISSET(t, f) ((t) & (f))
123
124 int
125 linux_sys_ptrace_arch(p, v, retval)
126 struct proc *p;
127 void *v;
128 register_t *retval;
129 {
130 struct linux_sys_ptrace_args /* {
131 syscallarg(int) request;
132 syscallarg(int) pid;
133 syscallarg(int) addr;
134 syscallarg(int) data;
135 } */ *uap = v;
136 int request, error;
137 struct proc *t; /* target process */
138 struct reg *regs = NULL;
139 struct fpreg *fpregs = NULL;
140 struct linux_reg *linux_regs = NULL;
141 struct linux_fpreg *linux_fpregs = NULL;
142 int addr;
143
144 request = SCARG(uap, request);
145
146 if ((request != LINUX_PTRACE_PEEKUSR) &&
147 (request != LINUX_PTRACE_POKEUSR) &&
148 (request != LINUX_PTRACE_GETREGS) &&
149 (request != LINUX_PTRACE_SETREGS) &&
150 (request != LINUX_PTRACE_GETFPREGS) &&
151 (request != LINUX_PTRACE_SETFPREGS))
152 return EIO;
153
154 /* Find the process we're supposed to be operating on. */
155 if ((t = pfind(SCARG(uap, pid))) == NULL)
156 return ESRCH;
157
158 /*
159 * You can't do what you want to the process if:
160 * (1) It's not being traced at all,
161 */
162 if (!ISSET(t->p_flag, P_TRACED))
163 return EPERM;
164
165 /*
166 * (2) it's being traced by procfs (which has
167 * different signal delivery semantics),
168 */
169 if (ISSET(t->p_flag, P_FSTRACE))
170 return EBUSY;
171
172 /*
173 * (3) it's not being traced by _you_, or
174 */
175 if (t->p_pptr != p)
176 return EBUSY;
177
178 /*
179 * (4) it's not currently stopped.
180 */
181 if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
182 return EBUSY;
183
184 *retval = 0;
185
186 switch (request) {
187 case LINUX_PTRACE_GETREGS:
188 MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
189 MALLOC(linux_regs, struct linux_reg*, sizeof(struct linux_reg),
190 M_TEMP, M_WAITOK);
191
192 error = process_read_regs(t, regs);
193 if (error != 0)
194 goto out;
195
196 linux_regs->ebx = regs->r_ebx;
197 linux_regs->ecx = regs->r_ecx;
198 linux_regs->edx = regs->r_edx;
199 linux_regs->esi = regs->r_esi;
200 linux_regs->edi = regs->r_edi;
201 linux_regs->ebp = regs->r_ebp;
202 linux_regs->eax = regs->r_eax;
203 linux_regs->xds = regs->r_ds;
204 linux_regs->xes = regs->r_es;
205 linux_regs->orig_eax = regs->r_eax; /* XXX is this correct? */
206 linux_regs->eip = regs->r_cs + regs->r_eip;
207 linux_regs->xcs = regs->r_cs;
208 linux_regs->eflags = regs->r_eflags;
209 linux_regs->esp = regs->r_esp;
210 linux_regs->xss = regs->r_ss;
211
212 error = copyout(linux_regs, (caddr_t)SCARG(uap, data),
213 sizeof(struct linux_reg));
214 goto out;
215
216 case LINUX_PTRACE_SETREGS:
217 MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
218 MALLOC(linux_regs, struct linux_reg *, sizeof(struct linux_reg),
219 M_TEMP, M_WAITOK);
220
221 error = copyin((caddr_t)SCARG(uap, data), linux_regs,
222 sizeof(struct linux_reg));
223 if (error != 0)
224 goto out;
225
226 regs->r_ebx = linux_regs->ebx;
227 regs->r_ecx = linux_regs->ecx;
228 regs->r_edx = linux_regs->edx;
229 regs->r_esi = linux_regs->esi;
230 regs->r_edi = linux_regs->edi;
231 regs->r_ebp = linux_regs->ebp;
232 regs->r_eax = linux_regs->eax;
233 regs->r_ds = linux_regs->xds;
234 regs->r_es = linux_regs->xes;
235 regs->r_eip = linux_regs->eip - linux_regs->xcs;
236 regs->r_cs = linux_regs->xcs;
237 regs->r_eflags = linux_regs->eflags;
238 regs->r_esp = linux_regs->esp;
239 regs->r_ss = linux_regs->xss;
240
241 error = process_write_regs(t, regs);
242 goto out;
243
244 case LINUX_PTRACE_GETFPREGS:
245 MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
246 M_TEMP, M_WAITOK);
247 MALLOC(linux_fpregs, struct linux_fpreg *,
248 sizeof(struct linux_fpreg), M_TEMP, M_WAITOK);
249
250 error = process_read_fpregs(t, fpregs);
251 if (error != 0)
252 goto out;
253
254 /* zero the contents if NetBSD fpreg structure is smaller */
255 if (sizeof(struct fpreg) < sizeof(struct linux_fpreg))
256 memset(linux_fpregs, '\0', sizeof(struct linux_fpreg));
257
258 memcpy(linux_fpregs, fpregs,
259 min(sizeof(struct linux_fpreg), sizeof(struct fpreg)));
260 error = copyout(linux_fpregs, (caddr_t)SCARG(uap, data),
261 sizeof(struct linux_fpreg));
262 goto out;
263
264 case LINUX_PTRACE_SETFPREGS:
265 MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
266 M_TEMP, M_WAITOK);
267 MALLOC(linux_fpregs, struct linux_fpreg *,
268 sizeof(struct linux_fpreg), M_TEMP, M_WAITOK);
269 error = copyin((caddr_t)SCARG(uap, data), linux_fpregs,
270 sizeof(struct linux_fpreg));
271 if (error != 0)
272 goto out;
273
274 memset(fpregs, '\0', sizeof(struct fpreg));
275 memcpy(fpregs, linux_fpregs,
276 min(sizeof(struct linux_fpreg), sizeof(struct fpreg)));
277
278 error = process_write_regs(t, regs);
279 goto out;
280
281 case LINUX_PTRACE_PEEKUSR:
282 addr = SCARG(uap, addr);
283
284 PHOLD(t); /* need full process info */
285 error = 0;
286 if (addr < LUSR_OFF(lusr_startgdb)) {
287 /* XXX should provide appropriate register */
288 error = 1;
289 } else if (addr == LUSR_OFF(u_tsize))
290 *retval = p->p_vmspace->vm_tsize;
291 else if (addr == LUSR_OFF(u_dsize))
292 *retval = p->p_vmspace->vm_dsize;
293 else if (addr == LUSR_OFF(u_ssize))
294 *retval = p->p_vmspace->vm_ssize;
295 else if (addr == LUSR_OFF(start_code))
296 *retval = (register_t) p->p_vmspace->vm_taddr;
297 else if (addr == LUSR_OFF(start_stack))
298 *retval = (register_t) p->p_vmspace->vm_minsaddr;
299 else if (addr == LUSR_OFF(u_ar0))
300 *retval = LUSR_OFF(regs);
301 else if (addr >= LUSR_OFF(u_debugreg)
302 && addr <= LUSR_OFF(u_debugreg_end)) {
303 int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
304
305 /* only do this for Linux processes */
306 if (t->p_emul != &emul_linux)
307 error = EINVAL;
308 else {
309 *retval = ((struct linux_emuldata *)
310 t->p_emuldata)->debugreg[off];
311 }
312 } else if (addr == LUSR_OFF(__signal)) {
313 error = 1;
314 } else if (addr == LUSR_OFF(__signal)) {
315 error = 1;
316 } else if (addr == LUSR_OFF(u_fpstate)) {
317 error = 1;
318 } else if (addr == LUSR_OFF(__magic)) {
319 error = 1;
320 } else if (addr == LUSR_OFF(u_comm)) {
321 error = 1;
322 } else {
323 #ifdef DEBUG_LINUX
324 printf("linux_ptrace: unsupported address: %d\n", addr);
325 #endif
326 error = 1;
327 }
328
329 PRELE(t);
330
331 if (!error)
332 return 0;
333
334 case LINUX_PTRACE_POKEUSR:
335 /* we only support setting debugregs for now */
336 addr = SCARG(uap, addr);
337 if (addr >= LUSR_OFF(u_debugreg)
338 && addr <= LUSR_OFF(u_debugreg_end)) {
339 int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
340 int data = SCARG(uap, data);
341
342 /* only do this for Linux processes */
343 if (t->p_emul != &emul_linux)
344 return EINVAL;
345
346 PHOLD(t);
347 ((struct linux_emuldata *)t->p_emuldata)->debugreg[off] = data;
348 PRELE(t);
349 return (0);
350 }
351
352 break;
353 default:
354 /* never reached */
355 break;
356 }
357
358 return EIO;
359
360 out:
361 if (regs)
362 FREE(regs, M_TEMP);
363 if (fpregs)
364 FREE(fpregs, M_TEMP);
365 if (linux_regs)
366 FREE(linux_regs, M_TEMP);
367 if (linux_fpregs)
368 FREE(linux_fpregs, M_TEMP);
369 return (error);
370 }
371