Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / compat / linux / arch / powerpc / linux_ptrace.c
bloba2d09ec0bc11ed8c2adeeb57381adf707a905b8e
1 /* $NetBSD: linux_ptrace.c,v 1.21 2008/12/17 20:51:33 cegger Exp $ */
3 /*-
4 * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Scheler and Emmanuel Dreyfus.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.21 2008/12/17 20:51:33 cegger Exp $");
35 #include <sys/param.h>
36 #include <sys/malloc.h>
37 #include <sys/mount.h>
38 #include <sys/proc.h>
39 #include <sys/ptrace.h>
40 #include <sys/systm.h>
41 #include <sys/syscallargs.h>
42 #include <uvm/uvm_extern.h>
44 #include <machine/reg.h>
46 #include <compat/linux/common/linux_types.h>
47 #include <compat/linux/common/linux_ptrace.h>
48 #include <compat/linux/common/linux_signal.h>
50 #include <compat/linux/common/linux_util.h>
51 #include <compat/linux/common/linux_machdep.h>
52 #include <compat/linux/common/linux_emuldata.h>
53 #include <compat/linux/common/linux_exec.h> /* for emul_linux */
55 #include <compat/linux/linux_syscallargs.h>
57 #include <lib/libkern/libkern.h> /* for offsetof() */
60 * From Linux's include/asm-ppc/ptrace.h.
61 * structure used for storing reg context: defined in linux_machdep.h
62 * structure used for storing floating point context:
63 * Linux just uses a double fpr[32], no struct
67 * user struct for linux process - this is used for Linux ptrace emulation
68 * most of it is junk only used by gdb
70 * From Linux's include/asm-ppc/user.h
72 * XXX u_ar0 was a struct reg in Linux/powerpc
73 * Can't find out what a struct regs is for Linux/powerpc,
74 * so we use a struct pt_regs instead. don't know if this is right.
77 struct linux_user {
78 struct linux_pt_regs regs;
79 #define lusr_startgdb regs
80 size_t u_tsize;
81 size_t u_dsize;
82 size_t u_ssize;
83 unsigned long start_code;
84 unsigned long start_data;
85 unsigned long start_stack;
86 long int signal;
87 struct linux_pt_regs *u_ar0; /* help gdb find registers */
88 unsigned long magic;
89 char u_comm[32];
90 #define lu_comm_end u_comm[31]
93 #define LUSR_OFF(member) offsetof(struct linux_user, member)
94 #define LUSR_REG_OFF(member) offsetof(struct linux_pt_regs, member)
95 #define ISSET(t, f) ((t) & (f))
97 int linux_ptrace_disabled = 1; /* bitrotted */
99 /* XXX Check me! (From NetBSD/i386) */
101 linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
103 /* {
104 syscallarg(int) request;
105 syscallarg(int) pid;
106 syscallarg(int) addr;
107 syscallarg(int) data;
108 } */
109 int request, error;
110 struct proc *p = l->l_proc;
111 struct proc *t; /* target process */
112 struct lwp *lt;
113 struct reg *regs = NULL;
114 struct fpreg *fpregs = NULL;
115 struct linux_pt_regs *linux_regs = NULL;
116 double *linux_fpreg = NULL; /* it's an array, not a struct */
117 int addr;
118 int i;
120 if (linux_ptrace_disabled)
121 return ENOSYS;
123 switch (request = SCARG(uap, request)) {
124 case LINUX_PTRACE_PEEKUSR:
125 case LINUX_PTRACE_POKEUSR:
126 case LINUX_PTRACE_GETREGS:
127 case LINUX_PTRACE_SETREGS:
128 case LINUX_PTRACE_GETFPREGS:
129 case LINUX_PTRACE_SETFPREGS:
130 break;
131 default:
132 return EIO;
135 /* Find the process we're supposed to be operating on. */
136 if ((t = pfind(SCARG(uap, pid))) == NULL)
137 return ESRCH;
140 * You can't do what you want to the process if:
141 * (1) It's not being traced at all,
143 if (!ISSET(t->p_slflag, PSL_TRACED)) /* XXXSMP */
144 return EPERM;
147 * (2) it's being traced by procfs (which has
148 * different signal delivery semantics),
150 if (ISSET(t->p_slflag, PSL_FSTRACE))
151 return EBUSY;
154 * (3) it's not being traced by _you_, or
156 if (t->p_pptr != p)
157 return EBUSY;
160 * (4) it's not currently stopped.
162 if (t->p_stat != SSTOP || !t->p_waited)
163 return EBUSY;
165 lt = LIST_FIRST(&t->p_lwps);
166 *retval = 0;
168 switch (request) {
169 case LINUX_PTRACE_GETREGS:
170 regs = malloc(sizeof(struct reg), M_TEMP, M_WAITOK);
171 linux_regs = malloc(sizeof(*linux_regs), M_TEMP, M_WAITOK);
173 error = process_read_regs(lt, regs);
174 if (error != 0)
175 goto out;
177 for (i=0; i<=31; i++)
178 linux_regs->lgpr[i] = regs->fixreg[i];
179 linux_regs->lnip = regs->pc;
180 linux_regs->lmsr = 0;
181 /* XXX Is that right? */
182 linux_regs->lorig_gpr3 = regs->fixreg[3];
183 linux_regs->lctr = regs->ctr;
184 linux_regs->llink = regs->lr;
185 linux_regs->lxer = regs->xer;
186 linux_regs->lccr = regs->cr;
187 linux_regs->lmq = 0;
188 linux_regs->ltrap = 0;
189 linux_regs->ldar = 0;
190 linux_regs->ldsisr = 0;
191 linux_regs->lresult = 0;
193 error = copyout(linux_regs, (void *)SCARG(uap, data),
194 sizeof(struct linux_pt_regs));
195 goto out;
197 case LINUX_PTRACE_SETREGS:
198 regs = malloc(sizeof(struct reg), M_TEMP, M_WAITOK);
199 linux_regs = malloc(sizeof(*linux_regs), M_TEMP, M_WAITOK);
201 error = copyin((void *)SCARG(uap, data), linux_regs,
202 sizeof(struct linux_pt_regs));
203 if (error != 0)
204 goto out;
206 for (i=0; i<=31; i++)
207 regs->fixreg[i] = linux_regs->lgpr[i];
208 regs->lr = linux_regs->llink;
209 regs->cr = linux_regs->lccr;
210 regs->xer = linux_regs->lxer;
211 regs->ctr = linux_regs->lctr;
212 regs->pc = linux_regs->lnip; /* XXX */
214 error = process_write_regs(lt, regs);
215 goto out;
217 case LINUX_PTRACE_GETFPREGS:
218 fpregs = malloc(sizeof(struct fpreg), M_TEMP, M_WAITOK);
219 linux_fpreg = malloc(32*sizeof(double), M_TEMP, M_WAITOK);
221 error = process_read_fpregs(lt, fpregs);
222 if (error != 0)
223 goto out;
225 /* zero the contents if NetBSD fpreg structure is smaller */
226 if (sizeof(struct fpreg) < (32*sizeof(double)))
227 memset(linux_fpreg, '\0', (32*sizeof(double)));
229 memcpy(linux_fpreg, fpregs,
230 min(32*sizeof(double), sizeof(struct fpreg)));
231 error = copyout(linux_fpreg, (void *)SCARG(uap, data),
232 32*sizeof(double));
233 goto out;
235 case LINUX_PTRACE_SETFPREGS:
236 fpregs = malloc(sizeof(struct fpreg), M_TEMP, M_WAITOK);
237 linux_fpreg = malloc(32*sizeof(double), M_TEMP, M_WAITOK);
238 error = copyin((void *)SCARG(uap, data), linux_fpreg,
239 32*sizeof(double));
240 if (error != 0)
241 goto out;
243 memset(fpregs, '\0', sizeof(struct fpreg));
244 memcpy(fpregs, linux_fpreg,
245 min(32*sizeof(double), sizeof(struct fpreg)));
247 error = process_write_fpregs(lt, fpregs);
248 goto out;
250 case LINUX_PTRACE_PEEKUSR:
251 addr = SCARG(uap, addr);
252 regs = malloc(sizeof(struct reg), M_TEMP, M_WAITOK);
253 error = process_read_regs(lt, regs);
254 if (error)
255 goto out;
257 error = 0;
258 if ((addr < LUSR_OFF(lusr_startgdb)) ||
259 (addr > LUSR_OFF(lu_comm_end)))
260 error = 1;
261 else if (addr == LUSR_OFF(u_tsize))
262 *retval = p->p_vmspace->vm_tsize;
263 else if (addr == LUSR_OFF(u_dsize))
264 *retval = p->p_vmspace->vm_dsize;
265 else if (addr == LUSR_OFF(u_ssize))
266 *retval = p->p_vmspace->vm_ssize;
267 else if (addr == LUSR_OFF(start_code))
268 *retval = (register_t) p->p_vmspace->vm_taddr;
269 else if (addr == LUSR_OFF(start_stack))
270 *retval = (register_t) p->p_vmspace->vm_minsaddr;
271 else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
272 (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
273 *retval = regs->fixreg[addr / sizeof (register_t)];
274 else if (addr == LUSR_REG_OFF(lnip))
275 *retval = regs->pc;
276 else if (addr == LUSR_REG_OFF(lctr))
277 *retval = regs->ctr;
278 else if (addr == LUSR_REG_OFF(llink))
279 *retval = regs->lr;
280 else if (addr == LUSR_REG_OFF(lxer))
281 *retval = regs->xer;
282 else if (addr == LUSR_REG_OFF(lccr))
283 *retval = regs->cr;
284 else if (addr == LUSR_OFF(signal))
285 error = 1;
286 else if (addr == LUSR_OFF(signal))
287 error = 1;
288 else if (addr == LUSR_OFF(magic))
289 error = 1;
290 else if (addr == LUSR_OFF(u_comm))
291 error = 1;
292 else {
293 #ifdef DEBUG_LINUX
294 printf("linux_ptrace: unsupported address: %d\n", addr);
295 #endif
296 error = 1;
299 if (error)
300 goto out;
302 error = copyout (retval,
303 (void *)SCARG(uap, data), sizeof retval);
304 *retval = SCARG(uap, data);
306 goto out;
308 break;
310 case LINUX_PTRACE_POKEUSR: /* XXX Not tested */
311 addr = SCARG(uap, addr);
312 regs = malloc(sizeof(struct reg), M_TEMP, M_WAITOK);
313 error = process_read_regs(lt, regs);
314 if (error)
315 goto out;
317 error = 0;
318 if ((addr < LUSR_OFF(lusr_startgdb)) ||
319 (addr > LUSR_OFF(lu_comm_end)))
320 error = 1;
321 else if (addr == LUSR_OFF(u_tsize))
322 error = 1;
323 else if (addr == LUSR_OFF(u_dsize))
324 error = 1;
325 else if (addr == LUSR_OFF(u_ssize))
326 error = 1;
327 else if (addr == LUSR_OFF(start_code))
328 error = 1;
329 else if (addr == LUSR_OFF(start_stack))
330 error = 1;
331 else if ((addr >= LUSR_REG_OFF(lpt_regs_fixreg_begin)) &&
332 (addr <= LUSR_REG_OFF(lpt_regs_fixreg_end)))
333 regs->fixreg[addr / sizeof (register_t)] =
334 (register_t)SCARG(uap, data);
335 else if (addr == LUSR_REG_OFF(lnip))
336 regs->pc = (register_t)SCARG(uap, data);
337 else if (addr == LUSR_REG_OFF(lctr))
338 regs->ctr = (register_t)SCARG(uap, data);
339 else if (addr == LUSR_REG_OFF(llink))
340 regs->lr = (register_t)SCARG(uap, data);
341 else if (addr == LUSR_REG_OFF(lxer))
342 regs->xer = (register_t)SCARG(uap, data);
343 else if (addr == LUSR_REG_OFF(lccr))
344 regs->cr = (register_t)SCARG(uap, data);
345 else if (addr == LUSR_OFF(signal))
346 error = 1;
347 else if (addr == LUSR_OFF(magic))
348 error = 1;
349 else if (addr == LUSR_OFF(u_comm))
350 error = 1;
351 else {
352 #ifdef DEBUG_LINUX
353 printf("linux_ptrace: unsupported address: %d\n", addr);
354 #endif
355 error = 1;
358 error = process_write_regs(lt,regs);
359 if (error)
360 goto out;
362 break;
363 default:
364 /* never reached */
365 break;
367 return EIO;
369 out:
370 if (regs)
371 free(regs, M_TEMP);
372 if (fpregs)
373 free(fpregs, M_TEMP);
374 if (linux_regs)
375 free(linux_regs, M_TEMP);
376 if (linux_fpreg)
377 free(linux_fpreg, M_TEMP);
378 return (error);