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