linux_ptrace.c revision 1.4 1 1.4 manu /* $NetBSD: linux_ptrace.c,v 1.4 2001/05/22 21:09: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 /*
40 1.1 manu * Function linux_sys_ptrace_arch() is taken from NetBSD/i386
41 1.1 manu * with some stuff deleted to get it compiling. It is not expected to
42 1.1 manu * work, and it should be checked with care or re-writen.
43 1.1 manu */
44 1.1 manu
45 1.1 manu #include <sys/types.h>
46 1.1 manu #include <sys/param.h>
47 1.1 manu #include <sys/malloc.h>
48 1.1 manu #include <sys/mount.h>
49 1.1 manu #include <sys/proc.h>
50 1.1 manu #include <sys/ptrace.h>
51 1.1 manu #include <sys/systm.h>
52 1.1 manu #include <sys/syscallargs.h>
53 1.1 manu #include <uvm/uvm_extern.h>
54 1.1 manu
55 1.1 manu #include <machine/reg.h>
56 1.1 manu
57 1.1 manu #include <compat/linux/common/linux_types.h>
58 1.1 manu #include <compat/linux/common/linux_ptrace.h>
59 1.1 manu #include <compat/linux/common/linux_signal.h>
60 1.1 manu
61 1.1 manu #include <compat/linux/common/linux_util.h>
62 1.1 manu #include <compat/linux/common/linux_machdep.h>
63 1.1 manu #include <compat/linux/common/linux_emuldata.h>
64 1.1 manu #include <compat/linux/common/linux_exec.h> /* for emul_linux */
65 1.1 manu
66 1.1 manu #include <compat/linux/linux_syscallargs.h>
67 1.1 manu
68 1.1 manu #include <lib/libkern/libkern.h> /* for offsetof() */
69 1.1 manu
70 1.1 manu /*
71 1.1 manu * From Linux's include/asm-ppc/ptrace.h.
72 1.1 manu * structure used for storing reg context: defined in linux_machdep.h
73 1.1 manu * structure used for storing floating point context:
74 1.1 manu * Linux just uses a double fpr[32], no struct
75 1.1 manu */
76 1.1 manu
77 1.1 manu /*
78 1.1 manu * user struct for linux process - this is used for Linux ptrace emulation
79 1.1 manu * most of it is junk only used by gdb
80 1.1 manu *
81 1.1 manu * From Linux's include/asm-ppc/user.h
82 1.1 manu *
83 1.1 manu * XXX u_ar0 was a struct reg in Linux/powerpc
84 1.1 manu * Can't find out what a struct regs is for Linux/powerpc,
85 1.1 manu * so we use a struct pt_regs instead. don't know if this is right.
86 1.1 manu */
87 1.1 manu
88 1.1 manu struct linux_user {
89 1.1 manu struct linux_pt_regs regs;
90 1.4 manu #define lusr_startgdb regs
91 1.1 manu size_t u_tsize;
92 1.1 manu size_t u_dsize;
93 1.1 manu size_t u_ssize;
94 1.1 manu unsigned long start_code;
95 1.1 manu unsigned long start_data;
96 1.1 manu unsigned long start_stack;
97 1.1 manu long int signal;
98 1.3 christos struct linux_pt_regs *u_ar0; /* help gdb find registers */
99 1.1 manu unsigned long magic;
100 1.1 manu char u_comm[32];
101 1.4 manu #define lu_comm_end u_comm[31]
102 1.1 manu };
103 1.1 manu
104 1.1 manu #define LUSR_OFF(member) offsetof(struct linux_user, member)
105 1.1 manu #define ISSET(t, f) ((t) & (f))
106 1.1 manu
107 1.1 manu int
108 1.1 manu linux_sys_ptrace_arch(p, v, retval) /* XXX Check me! (From NetBSD/i386) */
109 1.1 manu struct proc *p;
110 1.1 manu void *v;
111 1.1 manu register_t *retval;
112 1.1 manu {
113 1.1 manu struct linux_sys_ptrace_args /* {
114 1.1 manu syscallarg(int) request;
115 1.1 manu syscallarg(int) pid;
116 1.1 manu syscallarg(int) addr;
117 1.1 manu syscallarg(int) data;
118 1.1 manu } */ *uap = v;
119 1.1 manu int request, error;
120 1.1 manu struct proc *t; /* target process */
121 1.1 manu struct reg *regs = NULL;
122 1.1 manu struct fpreg *fpregs = NULL;
123 1.1 manu struct linux_pt_regs *linux_regs = NULL;
124 1.3 christos double *linux_fpreg = NULL; /* it's an array, not a struct */
125 1.1 manu int addr;
126 1.1 manu int i;
127 1.1 manu
128 1.3 christos switch (request = SCARG(uap, request)) {
129 1.3 christos case LINUX_PTRACE_PEEKUSR:
130 1.3 christos case LINUX_PTRACE_POKEUSR:
131 1.3 christos case LINUX_PTRACE_GETREGS:
132 1.3 christos case LINUX_PTRACE_SETREGS:
133 1.3 christos case LINUX_PTRACE_GETFPREGS:
134 1.3 christos case LINUX_PTRACE_SETFPREGS:
135 1.3 christos break;
136 1.3 christos default:
137 1.1 manu return EIO;
138 1.3 christos }
139 1.1 manu
140 1.1 manu /* Find the process we're supposed to be operating on. */
141 1.1 manu if ((t = pfind(SCARG(uap, pid))) == NULL)
142 1.1 manu return ESRCH;
143 1.1 manu
144 1.1 manu /*
145 1.1 manu * You can't do what you want to the process if:
146 1.1 manu * (1) It's not being traced at all,
147 1.1 manu */
148 1.1 manu if (!ISSET(t->p_flag, P_TRACED))
149 1.1 manu return EPERM;
150 1.1 manu
151 1.1 manu /*
152 1.1 manu * (2) it's being traced by procfs (which has
153 1.1 manu * different signal delivery semantics),
154 1.1 manu */
155 1.1 manu if (ISSET(t->p_flag, P_FSTRACE))
156 1.1 manu return EBUSY;
157 1.1 manu
158 1.1 manu /*
159 1.1 manu * (3) it's not being traced by _you_, or
160 1.1 manu */
161 1.1 manu if (t->p_pptr != p)
162 1.1 manu return EBUSY;
163 1.1 manu
164 1.1 manu /*
165 1.1 manu * (4) it's not currently stopped.
166 1.1 manu */
167 1.1 manu if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
168 1.1 manu return EBUSY;
169 1.1 manu
170 1.1 manu *retval = 0;
171 1.1 manu
172 1.1 manu switch (request) {
173 1.1 manu case LINUX_PTRACE_GETREGS:
174 1.1 manu MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
175 1.3 christos MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
176 1.1 manu M_TEMP, M_WAITOK);
177 1.1 manu
178 1.1 manu error = process_read_regs(t, regs);
179 1.1 manu if (error != 0)
180 1.1 manu goto out;
181 1.1 manu
182 1.1 manu for (i=0; i<=31; i++)
183 1.1 manu linux_regs->lgpr[i] = regs->fixreg[i];
184 1.1 manu linux_regs->lnip = regs->pc;
185 1.1 manu linux_regs->lmsr = 0;
186 1.3 christos /* XXX Is that right? */
187 1.3 christos linux_regs->lorig_gpr3 = regs->fixreg[3];
188 1.1 manu linux_regs->lctr = regs->ctr;
189 1.1 manu linux_regs->llink = regs->lr;
190 1.1 manu linux_regs->lxer = regs->xer;
191 1.1 manu linux_regs->lccr = regs->cr;
192 1.1 manu linux_regs->lmq = 0;
193 1.1 manu linux_regs->ltrap = 0;
194 1.1 manu linux_regs->ldar = 0;
195 1.1 manu linux_regs->ldsisr = 0;
196 1.1 manu linux_regs->lresult = 0;
197 1.1 manu
198 1.1 manu error = copyout(linux_regs, (caddr_t)SCARG(uap, data),
199 1.1 manu sizeof(struct linux_pt_regs));
200 1.1 manu goto out;
201 1.1 manu
202 1.1 manu case LINUX_PTRACE_SETREGS:
203 1.1 manu MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
204 1.3 christos MALLOC(linux_regs, struct linux_pt_regs*, sizeof(*linux_regs),
205 1.1 manu M_TEMP, M_WAITOK);
206 1.1 manu
207 1.1 manu error = copyin((caddr_t)SCARG(uap, data), linux_regs,
208 1.1 manu sizeof(struct linux_pt_regs));
209 1.1 manu if (error != 0)
210 1.1 manu goto out;
211 1.1 manu
212 1.1 manu for (i=0; i<=31; i++)
213 1.1 manu regs->fixreg[i] = linux_regs->lgpr[i];
214 1.1 manu regs->lr = linux_regs->llink;
215 1.1 manu regs->cr = linux_regs->lccr;
216 1.1 manu regs->xer = linux_regs->lxer;
217 1.1 manu regs->ctr = linux_regs->lctr;
218 1.2 manu regs->pc = linux_regs->lnip; /* XXX */
219 1.1 manu
220 1.1 manu error = process_write_regs(t, regs);
221 1.1 manu goto out;
222 1.1 manu
223 1.1 manu case LINUX_PTRACE_GETFPREGS:
224 1.1 manu MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
225 1.1 manu M_TEMP, M_WAITOK);
226 1.1 manu MALLOC(linux_fpreg, double *,
227 1.1 manu 32*sizeof(double), M_TEMP, M_WAITOK);
228 1.1 manu
229 1.1 manu error = process_read_fpregs(t, fpregs);
230 1.1 manu if (error != 0)
231 1.1 manu goto out;
232 1.1 manu
233 1.1 manu /* zero the contents if NetBSD fpreg structure is smaller */
234 1.1 manu if (sizeof(struct fpreg) < (32*sizeof(double)))
235 1.1 manu memset(linux_fpreg, '\0', (32*sizeof(double)));
236 1.1 manu
237 1.1 manu memcpy(linux_fpreg, fpregs,
238 1.1 manu min(32*sizeof(double), sizeof(struct fpreg)));
239 1.1 manu error = copyout(linux_fpreg, (caddr_t)SCARG(uap, data),
240 1.1 manu 32*sizeof(double));
241 1.1 manu goto out;
242 1.1 manu
243 1.1 manu case LINUX_PTRACE_SETFPREGS:
244 1.1 manu MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
245 1.1 manu M_TEMP, M_WAITOK);
246 1.1 manu MALLOC(linux_fpreg, double *,
247 1.1 manu 32*sizeof(double), M_TEMP, M_WAITOK);
248 1.1 manu error = copyin((caddr_t)SCARG(uap, data), linux_fpreg,
249 1.1 manu 32*sizeof(double));
250 1.1 manu if (error != 0)
251 1.1 manu goto out;
252 1.1 manu
253 1.1 manu memset(fpregs, '\0', sizeof(struct fpreg));
254 1.1 manu memcpy(fpregs, linux_fpreg,
255 1.1 manu min(32*sizeof(double), sizeof(struct fpreg)));
256 1.1 manu
257 1.1 manu error = process_write_regs(t, regs);
258 1.1 manu goto out;
259 1.1 manu
260 1.1 manu case LINUX_PTRACE_PEEKUSR:
261 1.1 manu addr = SCARG(uap, addr);
262 1.1 manu
263 1.1 manu PHOLD(t); /* need full process info */
264 1.1 manu error = 0;
265 1.4 manu if ((addr < LUSR_OFF(lusr_startgdb)) ||
266 1.4 manu (addr > LUSR_OFF(lu_comm_end))) {
267 1.1 manu error = 1;
268 1.1 manu } else if (addr == LUSR_OFF(u_tsize))
269 1.1 manu *retval = p->p_vmspace->vm_tsize;
270 1.1 manu else if (addr == LUSR_OFF(u_dsize))
271 1.1 manu *retval = p->p_vmspace->vm_dsize;
272 1.1 manu else if (addr == LUSR_OFF(u_ssize))
273 1.1 manu *retval = p->p_vmspace->vm_ssize;
274 1.1 manu else if (addr == LUSR_OFF(start_code))
275 1.1 manu *retval = (register_t) p->p_vmspace->vm_taddr;
276 1.1 manu else if (addr == LUSR_OFF(start_stack))
277 1.1 manu *retval = (register_t) p->p_vmspace->vm_minsaddr;
278 1.4 manu else if ((addr >=0 ) && (addr < LUSR_OFF(u_tsize)))
279 1.1 manu *retval = LUSR_OFF(regs);
280 1.1 manu else if (addr == LUSR_OFF(signal)) {
281 1.1 manu error = 1;
282 1.1 manu } else if (addr == LUSR_OFF(signal)) {
283 1.1 manu error = 1;
284 1.1 manu } else if (addr == LUSR_OFF(magic)) {
285 1.1 manu error = 1;
286 1.1 manu } else if (addr == LUSR_OFF(u_comm)) {
287 1.1 manu error = 1;
288 1.1 manu } else {
289 1.1 manu #ifdef DEBUG_LINUX
290 1.1 manu printf("linux_ptrace: unsupported address: %d\n", addr);
291 1.1 manu #endif
292 1.1 manu error = 1;
293 1.1 manu }
294 1.1 manu
295 1.1 manu PRELE(t);
296 1.1 manu
297 1.1 manu if (!error)
298 1.1 manu return 0;
299 1.1 manu
300 1.1 manu case LINUX_PTRACE_POKEUSR:
301 1.2 manu /*
302 1.2 manu * XXX NetBSD/i386 only handle debugregs here. It seems these
303 1.2 manu * debugregs are not availlable on powerpc. Hence we do nothing.
304 1.2 manu */
305 1.1 manu break;
306 1.1 manu default:
307 1.1 manu /* never reached */
308 1.1 manu break;
309 1.1 manu }
310 1.1 manu return EIO;
311 1.1 manu
312 1.1 manu out:
313 1.1 manu if (regs)
314 1.1 manu FREE(regs, M_TEMP);
315 1.1 manu if (fpregs)
316 1.1 manu FREE(fpregs, M_TEMP);
317 1.1 manu if (linux_regs)
318 1.1 manu FREE(linux_regs, M_TEMP);
319 1.1 manu if (linux_fpreg)
320 1.1 manu FREE(linux_fpreg, M_TEMP);
321 1.1 manu return (error);
322 1.1 manu }
323