linux_ptrace.c revision 1.2.4.4 1 1.2.4.4 nathanw /* $NetBSD: linux_ptrace.c,v 1.2.4.4 2002/05/29 21:32:30 nathanw Exp $ */
2 1.2.4.2 nathanw
3 1.2.4.2 nathanw /*-
4 1.2.4.2 nathanw * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.2.4.2 nathanw * All rights reserved.
6 1.2.4.2 nathanw *
7 1.2.4.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.2.4.2 nathanw * by Matthias Scheler.
9 1.2.4.2 nathanw *
10 1.2.4.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.2.4.2 nathanw * modification, are permitted provided that the following conditions
12 1.2.4.2 nathanw * are met:
13 1.2.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.2.4.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.2.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.2.4.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.2.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.2.4.2 nathanw * must display the following acknowledgement:
20 1.2.4.2 nathanw * This product includes software developed by the NetBSD
21 1.2.4.2 nathanw * Foundation, Inc. and its contributors.
22 1.2.4.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2.4.2 nathanw * contributors may be used to endorse or promote products derived
24 1.2.4.2 nathanw * from this software without specific prior written permission.
25 1.2.4.2 nathanw *
26 1.2.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2.4.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2.4.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2.4.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2.4.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2.4.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2.4.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2.4.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2.4.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2.4.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2.4.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.2.4.2 nathanw */
38 1.2.4.2 nathanw
39 1.2.4.2 nathanw
40 1.2.4.2 nathanw #include <sys/cdefs.h>
41 1.2.4.4 nathanw __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.2.4.4 2002/05/29 21:32:30 nathanw Exp $");
42 1.2.4.2 nathanw
43 1.2.4.2 nathanw #include <sys/param.h>
44 1.2.4.2 nathanw #include <sys/malloc.h>
45 1.2.4.2 nathanw #include <sys/mount.h>
46 1.2.4.2 nathanw #include <sys/proc.h>
47 1.2.4.2 nathanw #include <sys/ptrace.h>
48 1.2.4.2 nathanw #include <sys/systm.h>
49 1.2.4.4 nathanw #include <sys/sa.h>
50 1.2.4.2 nathanw #include <sys/syscallargs.h>
51 1.2.4.2 nathanw #include <uvm/uvm_extern.h>
52 1.2.4.2 nathanw
53 1.2.4.2 nathanw #include <machine/reg.h>
54 1.2.4.2 nathanw
55 1.2.4.2 nathanw #include <compat/linux/common/linux_types.h>
56 1.2.4.2 nathanw #include <compat/linux/common/linux_ptrace.h>
57 1.2.4.2 nathanw #include <compat/linux/common/linux_signal.h>
58 1.2.4.2 nathanw
59 1.2.4.2 nathanw #include <compat/linux/common/linux_util.h>
60 1.2.4.2 nathanw #include <compat/linux/common/linux_machdep.h>
61 1.2.4.2 nathanw #include <compat/linux/common/linux_emuldata.h>
62 1.2.4.2 nathanw #include <compat/linux/common/linux_exec.h> /* for emul_linux */
63 1.2.4.2 nathanw
64 1.2.4.2 nathanw #include <compat/linux/linux_syscallargs.h>
65 1.2.4.2 nathanw
66 1.2.4.2 nathanw #include <lib/libkern/libkern.h> /* for offsetof() */
67 1.2.4.2 nathanw
68 1.2.4.2 nathanw /*
69 1.2.4.2 nathanw * On ARMv2, uregs contains R0--R15, orig_R0.
70 1.2.4.2 nathanw * On ARMv3 and later, it's R0--R15, CPSR, orig_R0.
71 1.2.4.2 nathanw * As far as I can see, Linux doesn't initialise orig_R0 on ARMv2, so we
72 1.2.4.2 nathanw * just produce the ARMv3 version.
73 1.2.4.2 nathanw */
74 1.2.4.2 nathanw
75 1.2.4.2 nathanw struct linux_reg {
76 1.2.4.2 nathanw long uregs[18];
77 1.2.4.2 nathanw };
78 1.2.4.2 nathanw
79 1.2.4.2 nathanw #define LINUX_REG_R0 0
80 1.2.4.2 nathanw #define LINUX_REG_R1 1
81 1.2.4.2 nathanw #define LINUX_REG_R2 2
82 1.2.4.2 nathanw #define LINUX_REG_R3 3
83 1.2.4.2 nathanw #define LINUX_REG_R4 4
84 1.2.4.2 nathanw #define LINUX_REG_R5 5
85 1.2.4.2 nathanw #define LINUX_REG_R6 6
86 1.2.4.2 nathanw #define LINUX_REG_R7 7
87 1.2.4.2 nathanw #define LINUX_REG_R8 8
88 1.2.4.2 nathanw #define LINUX_REG_R9 9
89 1.2.4.2 nathanw #define LINUX_REG_R10 10
90 1.2.4.2 nathanw #define LINUX_REG_FP 11
91 1.2.4.2 nathanw #define LINUX_REG_IP 12
92 1.2.4.2 nathanw #define LINUX_REG_SP 13
93 1.2.4.2 nathanw #define LINUX_REG_LR 14
94 1.2.4.2 nathanw #define LINUX_REG_PC 15
95 1.2.4.2 nathanw #define LINUX_REG_CPSR 16
96 1.2.4.2 nathanw #define LINUX_REG_ORIG_R0 17
97 1.2.4.2 nathanw
98 1.2.4.2 nathanw /* Macros to clear/set/test flags. */
99 1.2.4.2 nathanw #define SET(t, f) (t) |= (f)
100 1.2.4.2 nathanw #define CLR(t, f) (t) &= ~(f)
101 1.2.4.2 nathanw #define ISSET(t, f) ((t) & (f))
102 1.2.4.2 nathanw
103 1.2.4.2 nathanw int
104 1.2.4.3 thorpej linux_sys_ptrace_arch(l, v, retval)
105 1.2.4.3 thorpej struct lwp *l;
106 1.2.4.2 nathanw void *v;
107 1.2.4.2 nathanw register_t *retval;
108 1.2.4.2 nathanw {
109 1.2.4.2 nathanw struct linux_sys_ptrace_args /* {
110 1.2.4.2 nathanw syscallarg(int) request;
111 1.2.4.2 nathanw syscallarg(int) pid;
112 1.2.4.2 nathanw syscallarg(int) addr;
113 1.2.4.2 nathanw syscallarg(int) data;
114 1.2.4.2 nathanw } */ *uap = v;
115 1.2.4.3 thorpej struct proc *p = l->l_proc;
116 1.2.4.2 nathanw int request, error;
117 1.2.4.2 nathanw struct proc *t; /* target process */
118 1.2.4.3 thorpej struct lwp *lt;
119 1.2.4.2 nathanw struct reg *regs = NULL;
120 1.2.4.2 nathanw struct fpreg *fpregs = NULL;
121 1.2.4.2 nathanw struct linux_reg *linux_regs = NULL;
122 1.2.4.2 nathanw struct linux_fpreg *linux_fpregs = NULL;
123 1.2.4.2 nathanw
124 1.2.4.2 nathanw request = SCARG(uap, request);
125 1.2.4.2 nathanw
126 1.2.4.2 nathanw if ((request != LINUX_PTRACE_GETREGS) &&
127 1.2.4.2 nathanw (request != LINUX_PTRACE_SETREGS))
128 1.2.4.2 nathanw return EIO;
129 1.2.4.2 nathanw
130 1.2.4.2 nathanw /* Find the process we're supposed to be operating on. */
131 1.2.4.2 nathanw if ((t = pfind(SCARG(uap, pid))) == NULL)
132 1.2.4.2 nathanw return ESRCH;
133 1.2.4.2 nathanw
134 1.2.4.2 nathanw /*
135 1.2.4.2 nathanw * You can't do what you want to the process if:
136 1.2.4.2 nathanw * (1) It's not being traced at all,
137 1.2.4.2 nathanw */
138 1.2.4.2 nathanw if (!ISSET(t->p_flag, P_TRACED))
139 1.2.4.2 nathanw return EPERM;
140 1.2.4.2 nathanw
141 1.2.4.2 nathanw /*
142 1.2.4.2 nathanw * (2) it's being traced by procfs (which has
143 1.2.4.2 nathanw * different signal delivery semantics),
144 1.2.4.2 nathanw */
145 1.2.4.2 nathanw if (ISSET(t->p_flag, P_FSTRACE))
146 1.2.4.2 nathanw return EBUSY;
147 1.2.4.2 nathanw
148 1.2.4.2 nathanw /*
149 1.2.4.2 nathanw * (3) it's not being traced by _you_, or
150 1.2.4.2 nathanw */
151 1.2.4.2 nathanw if (t->p_pptr != p)
152 1.2.4.2 nathanw return EBUSY;
153 1.2.4.2 nathanw
154 1.2.4.2 nathanw /*
155 1.2.4.2 nathanw * (4) it's not currently stopped.
156 1.2.4.2 nathanw */
157 1.2.4.2 nathanw if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
158 1.2.4.2 nathanw return EBUSY;
159 1.2.4.2 nathanw
160 1.2.4.3 thorpej /* XXX NJWLWP
161 1.2.4.3 thorpej * The entire ptrace interface needs work to be useful to
162 1.2.4.3 thorpej * a process with multiple LWPs. For the moment, we'll
163 1.2.4.3 thorpej * just kluge this and fail on others.
164 1.2.4.3 thorpej */
165 1.2.4.3 thorpej
166 1.2.4.3 thorpej if (p->p_nlwps > 1)
167 1.2.4.3 thorpej return (ENOSYS);
168 1.2.4.3 thorpej
169 1.2.4.3 thorpej lt = LIST_FIRST(&t->p_lwps);
170 1.2.4.3 thorpej
171 1.2.4.2 nathanw *retval = 0;
172 1.2.4.2 nathanw
173 1.2.4.2 nathanw switch (request) {
174 1.2.4.2 nathanw case LINUX_PTRACE_GETREGS:
175 1.2.4.2 nathanw MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
176 1.2.4.2 nathanw MALLOC(linux_regs, struct linux_reg*, sizeof(struct linux_reg),
177 1.2.4.2 nathanw M_TEMP, M_WAITOK);
178 1.2.4.2 nathanw
179 1.2.4.3 thorpej error = process_read_regs(lt, regs);
180 1.2.4.2 nathanw if (error != 0)
181 1.2.4.2 nathanw goto out;
182 1.2.4.2 nathanw
183 1.2.4.2 nathanw memcpy(linux_regs->uregs, regs->r, 13 * sizeof(register_t));
184 1.2.4.2 nathanw linux_regs->uregs[LINUX_REG_SP] = regs->r_sp;
185 1.2.4.2 nathanw linux_regs->uregs[LINUX_REG_LR] = regs->r_lr;
186 1.2.4.2 nathanw linux_regs->uregs[LINUX_REG_PC] = regs->r_pc;
187 1.2.4.2 nathanw linux_regs->uregs[LINUX_REG_CPSR] = regs->r_cpsr;
188 1.2.4.2 nathanw linux_regs->uregs[LINUX_REG_ORIG_R0] = regs->r[0];
189 1.2.4.2 nathanw
190 1.2.4.2 nathanw error = copyout(linux_regs, (caddr_t)SCARG(uap, data),
191 1.2.4.2 nathanw sizeof(struct linux_reg));
192 1.2.4.2 nathanw goto out;
193 1.2.4.2 nathanw
194 1.2.4.2 nathanw case LINUX_PTRACE_SETREGS:
195 1.2.4.2 nathanw MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
196 1.2.4.2 nathanw MALLOC(linux_regs, struct linux_reg *, sizeof(struct linux_reg),
197 1.2.4.2 nathanw M_TEMP, M_WAITOK);
198 1.2.4.2 nathanw
199 1.2.4.2 nathanw error = copyin((caddr_t)SCARG(uap, data), linux_regs,
200 1.2.4.2 nathanw sizeof(struct linux_reg));
201 1.2.4.2 nathanw if (error != 0)
202 1.2.4.2 nathanw goto out;
203 1.2.4.2 nathanw
204 1.2.4.2 nathanw memcpy(regs->r, linux_regs->uregs, 13 * sizeof(register_t));
205 1.2.4.2 nathanw regs->r_sp = linux_regs->uregs[LINUX_REG_SP];
206 1.2.4.2 nathanw regs->r_lr = linux_regs->uregs[LINUX_REG_LR];
207 1.2.4.2 nathanw regs->r_pc = linux_regs->uregs[LINUX_REG_PC];
208 1.2.4.2 nathanw regs->r_cpsr = linux_regs->uregs[LINUX_REG_CPSR];
209 1.2.4.2 nathanw
210 1.2.4.3 thorpej error = process_write_regs(lt, regs);
211 1.2.4.2 nathanw goto out;
212 1.2.4.2 nathanw
213 1.2.4.2 nathanw default:
214 1.2.4.2 nathanw /* never reached */
215 1.2.4.2 nathanw break;
216 1.2.4.2 nathanw }
217 1.2.4.2 nathanw
218 1.2.4.2 nathanw return EIO;
219 1.2.4.2 nathanw
220 1.2.4.2 nathanw out:
221 1.2.4.2 nathanw if (regs)
222 1.2.4.2 nathanw FREE(regs, M_TEMP);
223 1.2.4.2 nathanw if (fpregs)
224 1.2.4.2 nathanw FREE(fpregs, M_TEMP);
225 1.2.4.2 nathanw if (linux_regs)
226 1.2.4.2 nathanw FREE(linux_regs, M_TEMP);
227 1.2.4.2 nathanw if (linux_fpregs)
228 1.2.4.2 nathanw FREE(linux_fpregs, M_TEMP);
229 1.2.4.2 nathanw return (error);
230 1.2.4.2 nathanw
231 1.2.4.2 nathanw }
232