linux_ptrace.c revision 1.6 1 1.6 manu /* $NetBSD: linux_ptrace.c,v 1.6 2001/05/28 10:51:20 manu Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
5 1.1 manu * All rights reserved.
6 1.1 manu *
7 1.1 manu * This code is derived from software contributed to The NetBSD Foundation
8 1.1 manu * by Matthias Scheler and Emmanuel Dreyfus.
9 1.1 manu *
10 1.1 manu * Redistribution and use in source and binary forms, with or without
11 1.1 manu * modification, are permitted provided that the following conditions
12 1.1 manu * are met:
13 1.1 manu * 1. Redistributions of source code must retain the above copyright
14 1.1 manu * notice, this list of conditions and the following disclaimer.
15 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 manu * notice, this list of conditions and the following disclaimer in the
17 1.1 manu * documentation and/or other materials provided with the distribution.
18 1.1 manu * 3. All advertising materials mentioning features or use of this software
19 1.1 manu * must display the following acknowledgement:
20 1.1 manu * This product includes software developed by the NetBSD
21 1.1 manu * Foundation, Inc. and its contributors.
22 1.1 manu * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 manu * contributors may be used to endorse or promote products derived
24 1.1 manu * from this software without specific prior written permission.
25 1.1 manu *
26 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 manu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 manu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
37 1.1 manu */
38 1.1 manu
39 1.1 manu #include <sys/types.h>
40 1.1 manu #include <sys/param.h>
41 1.1 manu #include <sys/malloc.h>
42 1.1 manu #include <sys/mount.h>
43 1.1 manu #include <sys/proc.h>
44 1.1 manu #include <sys/ptrace.h>
45 1.1 manu #include <sys/systm.h>
46 1.1 manu #include <sys/syscallargs.h>
47 1.1 manu #include <uvm/uvm_extern.h>
48 1.1 manu
49 1.1 manu #include <machine/reg.h>
50 1.1 manu
51 1.1 manu #include <compat/linux/common/linux_types.h>
52 1.1 manu #include <compat/linux/common/linux_ptrace.h>
53 1.1 manu #include <compat/linux/common/linux_signal.h>
54 1.1 manu
55 1.1 manu #include <compat/linux/common/linux_util.h>
56 1.1 manu #include <compat/linux/common/linux_machdep.h>
57 1.1 manu #include <compat/linux/common/linux_emuldata.h>
58 1.1 manu #include <compat/linux/common/linux_exec.h> /* for emul_linux */
59 1.1 manu
60 1.1 manu #include <compat/linux/linux_syscallargs.h>
61 1.1 manu
62 1.1 manu #include <lib/libkern/libkern.h> /* for offsetof() */
63 1.1 manu
64 1.1 manu /*
65 1.1 manu * From Linux's include/asm-ppc/ptrace.h.
66 1.1 manu * structure used for storing reg context: defined in linux_machdep.h
67 1.1 manu * structure used for storing floating point context:
68 1.1 manu * Linux just uses a double fpr[32], no struct
69 1.1 manu */
70 1.1 manu
71 1.1 manu /*
72 1.1 manu * user struct for linux process - this is used for Linux ptrace emulation
73 1.1 manu * most of it is junk only used by gdb
74 1.1 manu *
75 1.1 manu * From Linux's include/asm-ppc/user.h
76 1.1 manu *
77 1.1 manu * XXX u_ar0 was a struct reg in Linux/powerpc
78 1.1 manu * Can't find out what a struct regs is for Linux/powerpc,
79 1.1 manu * so we use a struct pt_regs instead. don't know if this is right.
80 1.1 manu */
81 1.1 manu
82 1.1 manu struct linux_user {
83 1.1 manu struct linux_pt_regs regs;
84 1.4 manu #define lusr_startgdb regs
85 1.1 manu size_t u_tsize;
86 1.1 manu size_t u_dsize;
87 1.1 manu size_t u_ssize;
88 1.1 manu unsigned long start_code;
89 1.1 manu unsigned long start_data;
90 1.1 manu unsigned long start_stack;
91 1.1 manu long int signal;
92 1.3 christos struct linux_pt_regs *u_ar0; /* help gdb find registers */
93 1.1 manu unsigned long magic;
94 1.1 manu char u_comm[32];
95 1.4 manu #define lu_comm_end u_comm[31]
96 1.1 manu };
97 1.1 manu
98 1.1 manu #define LUSR_OFF(member) offsetof(struct linux_user, member)
99 1.5 manu #define LUSR_REG_OFF(member) offsetof(struct linux_pt_regs, member)
100 1.1 manu #define ISSET(t, f) ((t) & (f))
101 1.1 manu
102 1.1 manu int
103 1.1 manu linux_sys_ptrace_arch(p, v, retval) /* XXX Check me! (From NetBSD/i386) */
104 1.1 manu struct proc *p;
105 1.1 manu void *v;
106 1.1 manu register_t *retval;
107 1.1 manu {
108 1.1 manu struct linux_sys_ptrace_args /* {
109 1.1 manu syscallarg(int) request;
110 1.1 manu syscallarg(int) pid;
111 1.1 manu syscallarg(int) addr;
112 1.1 manu syscallarg(int) data;
113 1.1 manu } */ *uap = v;
114 1.1 manu int request, error;
115 1.1 manu struct proc *t; /* target process */
116 1.1 manu struct reg *regs = NULL;
117 1.1 manu struct fpreg *fpregs = NULL;
118 1.1 manu struct linux_pt_regs *linux_regs = NULL;
119 1.3 christos double *linux_fpreg = NULL; /* it's an array, not a struct */
120 1.1 manu int addr;
121 1.1 manu int i;
122 1.1 manu
123 1.3 christos switch (request = SCARG(uap, request)) {
124 1.5 manu case LINUX_PTRACE_PEEKUSR:
125 1.5 manu case LINUX_PTRACE_POKEUSR:
126 1.5 manu case LINUX_PTRACE_GETREGS:
127 1.5 manu case LINUX_PTRACE_SETREGS:
128 1.5 manu case LINUX_PTRACE_GETFPREGS:
129 1.5 manu case LINUX_PTRACE_SETFPREGS:
130 1.5 manu break;
131 1.5 manu default:
132 1.5 manu return EIO;
133 1.3 christos }
134 1.1 manu
135 1.1 manu /* Find the process we're supposed to be operating on. */
136 1.1 manu if ((t = pfind(SCARG(uap, pid))) == NULL)
137 1.1 manu return ESRCH;
138 1.1 manu
139 1.1 manu /*
140 1.1 manu * You can't do what you want to the process if:
141 1.1 manu * (1) It's not being traced at all,
142 1.1 manu */
143 1.1 manu if (!ISSET(t->p_flag, P_TRACED))
144 1.1 manu return EPERM;
145 1.1 manu
146 1.1 manu /*
147 1.1 manu * (2) it's being traced by procfs (which has
148 1.1 manu * different signal delivery semantics),
149 1.1 manu */
150 1.1 manu if (ISSET(t->p_flag, P_FSTRACE))
151 1.1 manu return EBUSY;
152 1.1 manu
153 1.1 manu /*
154 1.1 manu * (3) it's not being traced by _you_, or
155 1.1 manu */
156 1.1 manu if (t->p_pptr != p)
157 1.1 manu return EBUSY;
158 1.1 manu
159 1.1 manu /*
160 1.1 manu * (4) it's not currently stopped.
161 1.1 manu */
162 1.1 manu if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
163 1.1 manu return EBUSY;
164 1.1 manu
165 1.1 manu *retval = 0;
166 1.1 manu
167 1.1 manu switch (request) {
168 1.1 manu case LINUX_PTRACE_GETREGS:
169 1.1 manu MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
170 1.3 christos MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
171 1.1 manu M_TEMP, M_WAITOK);
172 1.1 manu
173 1.1 manu error = process_read_regs(t, regs);
174 1.1 manu if (error != 0)
175 1.1 manu goto out;
176 1.1 manu
177 1.1 manu for (i=0; i<=31; i++)
178 1.1 manu linux_regs->lgpr[i] = regs->fixreg[i];
179 1.1 manu linux_regs->lnip = regs->pc;
180 1.1 manu linux_regs->lmsr = 0;
181 1.3 christos /* XXX Is that right? */
182 1.3 christos linux_regs->lorig_gpr3 = regs->fixreg[3];
183 1.1 manu linux_regs->lctr = regs->ctr;
184 1.1 manu linux_regs->llink = regs->lr;
185 1.1 manu linux_regs->lxer = regs->xer;
186 1.1 manu linux_regs->lccr = regs->cr;
187 1.1 manu linux_regs->lmq = 0;
188 1.1 manu linux_regs->ltrap = 0;
189 1.1 manu linux_regs->ldar = 0;
190 1.1 manu linux_regs->ldsisr = 0;
191 1.1 manu linux_regs->lresult = 0;
192 1.1 manu
193 1.1 manu error = copyout(linux_regs, (caddr_t)SCARG(uap, data),
194 1.1 manu sizeof(struct linux_pt_regs));
195 1.1 manu goto out;
196 1.1 manu
197 1.1 manu case LINUX_PTRACE_SETREGS:
198 1.1 manu MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
199 1.3 christos MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
200 1.1 manu M_TEMP, M_WAITOK);
201 1.1 manu
202 1.1 manu error = copyin((caddr_t)SCARG(uap, data), linux_regs,
203 1.1 manu sizeof(struct linux_pt_regs));
204 1.1 manu if (error != 0)
205 1.1 manu goto out;
206 1.1 manu
207 1.1 manu for (i=0; i<=31; i++)
208 1.1 manu regs->fixreg[i] = linux_regs->lgpr[i];
209 1.1 manu regs->lr = linux_regs->llink;
210 1.1 manu regs->cr = linux_regs->lccr;
211 1.1 manu regs->xer = linux_regs->lxer;
212 1.1 manu regs->ctr = linux_regs->lctr;
213 1.2 manu regs->pc = linux_regs->lnip; /* XXX */
214 1.1 manu
215 1.1 manu error = process_write_regs(t, regs);
216 1.1 manu goto out;
217 1.1 manu
218 1.1 manu case LINUX_PTRACE_GETFPREGS:
219 1.1 manu MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
220 1.1 manu M_TEMP, M_WAITOK);
221 1.1 manu MALLOC(linux_fpreg, double *,
222 1.1 manu 32*sizeof(double), M_TEMP, M_WAITOK);
223 1.1 manu
224 1.1 manu error = process_read_fpregs(t, fpregs);
225 1.1 manu if (error != 0)
226 1.1 manu goto out;
227 1.1 manu
228 1.1 manu /* zero the contents if NetBSD fpreg structure is smaller */
229 1.1 manu if (sizeof(struct fpreg) < (32*sizeof(double)))
230 1.1 manu memset(linux_fpreg, '\0', (32*sizeof(double)));
231 1.1 manu
232 1.1 manu memcpy(linux_fpreg, fpregs,
233 1.1 manu min(32*sizeof(double), sizeof(struct fpreg)));
234 1.1 manu error = copyout(linux_fpreg, (caddr_t)SCARG(uap, data),
235 1.1 manu 32*sizeof(double));
236 1.1 manu goto out;
237 1.1 manu
238 1.1 manu case LINUX_PTRACE_SETFPREGS:
239 1.1 manu MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
240 1.1 manu M_TEMP, M_WAITOK);
241 1.1 manu MALLOC(linux_fpreg, double *,
242 1.1 manu 32*sizeof(double), M_TEMP, M_WAITOK);
243 1.1 manu error = copyin((caddr_t)SCARG(uap, data), linux_fpreg,
244 1.1 manu 32*sizeof(double));
245 1.1 manu if (error != 0)
246 1.1 manu goto out;
247 1.1 manu
248 1.1 manu memset(fpregs, '\0', sizeof(struct fpreg));
249 1.1 manu memcpy(fpregs, linux_fpreg,
250 1.1 manu min(32*sizeof(double), sizeof(struct fpreg)));
251 1.1 manu
252 1.1 manu error = process_write_regs(t, regs);
253 1.1 manu goto out;
254 1.1 manu
255 1.1 manu case LINUX_PTRACE_PEEKUSR:
256 1.1 manu addr = SCARG(uap, addr);
257 1.5 manu MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
258 1.5 manu error = process_read_regs(t, regs);
259 1.5 manu if (error)
260 1.5 manu goto out;
261 1.1 manu
262 1.1 manu PHOLD(t); /* need full process info */
263 1.1 manu error = 0;
264 1.4 manu if ((addr < LUSR_OFF(lusr_startgdb)) ||
265 1.5 manu (addr > LUSR_OFF(lu_comm_end)))
266 1.1 manu error = 1;
267 1.5 manu else if (addr == LUSR_OFF(u_tsize))
268 1.1 manu *retval = p->p_vmspace->vm_tsize;
269 1.1 manu else if (addr == LUSR_OFF(u_dsize))
270 1.1 manu *retval = p->p_vmspace->vm_dsize;
271 1.1 manu else if (addr == LUSR_OFF(u_ssize))
272 1.1 manu *retval = p->p_vmspace->vm_ssize;
273 1.1 manu else if (addr == LUSR_OFF(start_code))
274 1.1 manu *retval = (register_t) p->p_vmspace->vm_taddr;
275 1.1 manu else if (addr == LUSR_OFF(start_stack))
276 1.1 manu *retval = (register_t) p->p_vmspace->vm_minsaddr;
277 1.5 manu else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
278 1.5 manu (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
279 1.5 manu *retval = regs->fixreg[addr / sizeof (register_t)];
280 1.5 manu else if (addr == LUSR_REG_OFF(lnip))
281 1.5 manu *retval = regs->pc;
282 1.5 manu else if (addr == LUSR_REG_OFF(lctr))
283 1.5 manu *retval = regs->ctr;
284 1.5 manu else if (addr == LUSR_REG_OFF(llink))
285 1.5 manu *retval = regs->lr;
286 1.5 manu else if (addr == LUSR_REG_OFF(lxer))
287 1.5 manu *retval = regs->xer;
288 1.5 manu else if (addr == LUSR_REG_OFF(lccr))
289 1.5 manu *retval = regs->cr;
290 1.5 manu else if (addr == LUSR_OFF(signal))
291 1.5 manu error = 1;
292 1.5 manu else if (addr == LUSR_OFF(signal))
293 1.5 manu error = 1;
294 1.5 manu else if (addr == LUSR_OFF(magic))
295 1.5 manu error = 1;
296 1.5 manu else if (addr == LUSR_OFF(u_comm))
297 1.5 manu error = 1;
298 1.5 manu else {
299 1.5 manu #ifdef DEBUG_LINUX
300 1.5 manu printf("linux_ptrace: unsupported address: %d\n", addr);
301 1.5 manu #endif
302 1.5 manu error = 1;
303 1.5 manu }
304 1.5 manu
305 1.5 manu PRELE(t);
306 1.5 manu
307 1.6 manu if (error)
308 1.6 manu goto out;
309 1.6 manu
310 1.5 manu error = copyout (retval,
311 1.5 manu (caddr_t)SCARG(uap, data), sizeof retval);
312 1.5 manu *retval = SCARG(uap, data);
313 1.6 manu
314 1.5 manu goto out;
315 1.5 manu
316 1.5 manu break;
317 1.5 manu
318 1.5 manu case LINUX_PTRACE_POKEUSR: /* XXX Not tested */
319 1.5 manu addr = SCARG(uap, addr);
320 1.5 manu MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
321 1.5 manu error = process_read_regs(t, regs);
322 1.5 manu if (error)
323 1.5 manu goto out;
324 1.5 manu
325 1.5 manu PHOLD(t); /* need full process info */
326 1.5 manu error = 0;
327 1.5 manu if ((addr < LUSR_OFF(lusr_startgdb)) ||
328 1.5 manu (addr > LUSR_OFF(lu_comm_end)))
329 1.5 manu error = 1;
330 1.5 manu else if (addr == LUSR_OFF(u_tsize))
331 1.5 manu error = 1;
332 1.5 manu else if (addr == LUSR_OFF(u_dsize))
333 1.5 manu error = 1;
334 1.5 manu else if (addr == LUSR_OFF(u_ssize))
335 1.1 manu error = 1;
336 1.5 manu else if (addr == LUSR_OFF(start_code))
337 1.5 manu error = 1;
338 1.5 manu else if (addr == LUSR_OFF(start_stack))
339 1.1 manu error = 1;
340 1.5 manu else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
341 1.5 manu (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
342 1.5 manu regs->fixreg[addr / sizeof (register_t)] =
343 1.5 manu (register_t)SCARG(uap, data);
344 1.5 manu else if (addr == LUSR_REG_OFF(lnip))
345 1.5 manu regs->pc = (register_t)SCARG(uap, data);
346 1.5 manu else if (addr == LUSR_REG_OFF(lctr))
347 1.5 manu regs->ctr = (register_t)SCARG(uap, data);
348 1.5 manu else if (addr == LUSR_REG_OFF(llink))
349 1.5 manu regs->lr = (register_t)SCARG(uap, data);
350 1.5 manu else if (addr == LUSR_REG_OFF(lxer))
351 1.5 manu regs->xer = (register_t)SCARG(uap, data);
352 1.5 manu else if (addr == LUSR_REG_OFF(lccr))
353 1.5 manu regs->cr = (register_t)SCARG(uap, data);
354 1.5 manu else if (addr == LUSR_OFF(signal))
355 1.1 manu error = 1;
356 1.5 manu else if (addr == LUSR_OFF(magic))
357 1.1 manu error = 1;
358 1.5 manu else if (addr == LUSR_OFF(u_comm))
359 1.5 manu error = 1;
360 1.5 manu else {
361 1.1 manu #ifdef DEBUG_LINUX
362 1.1 manu printf("linux_ptrace: unsupported address: %d\n", addr);
363 1.1 manu #endif
364 1.1 manu error = 1;
365 1.1 manu }
366 1.1 manu
367 1.1 manu PRELE(t);
368 1.1 manu
369 1.5 manu error = process_write_regs(t,regs);
370 1.5 manu if (error)
371 1.5 manu goto out;
372 1.1 manu
373 1.1 manu break;
374 1.1 manu default:
375 1.1 manu /* never reached */
376 1.1 manu break;
377 1.1 manu }
378 1.1 manu return EIO;
379 1.1 manu
380 1.1 manu out:
381 1.1 manu if (regs)
382 1.1 manu FREE(regs, M_TEMP);
383 1.1 manu if (fpregs)
384 1.1 manu FREE(fpregs, M_TEMP);
385 1.1 manu if (linux_regs)
386 1.1 manu FREE(linux_regs, M_TEMP);
387 1.1 manu if (linux_fpreg)
388 1.1 manu FREE(linux_fpreg, M_TEMP);
389 1.1 manu return (error);
390 1.1 manu }
391