xref: /netbsd/sys/compat/linux/arch/i386/linux_ptrace.c (revision bf9ec67e)
1 /*	$NetBSD: linux_ptrace.c,v 1.8 2001/11/15 09:48:00 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matthias Scheler.
9  *
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.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.8 2001/11/15 09:48:00 lukem Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/malloc.h>
44 #include <sys/mount.h>
45 #include <sys/proc.h>
46 #include <sys/ptrace.h>
47 #include <sys/systm.h>
48 #include <sys/syscallargs.h>
49 #include <uvm/uvm_extern.h>
50 
51 #include <machine/reg.h>
52 
53 #include <compat/linux/common/linux_types.h>
54 #include <compat/linux/common/linux_ptrace.h>
55 #include <compat/linux/common/linux_signal.h>
56 
57 #include <compat/linux/common/linux_util.h>
58 #include <compat/linux/common/linux_machdep.h>
59 #include <compat/linux/common/linux_emuldata.h>
60 #include <compat/linux/common/linux_exec.h>	/* for emul_linux */
61 
62 #include <compat/linux/linux_syscallargs.h>
63 
64 #include <lib/libkern/libkern.h>	/* for offsetof() */
65 
66 struct linux_reg {
67 	long ebx;
68 	long ecx;
69 	long edx;
70 	long esi;
71 	long edi;
72 	long ebp;
73 	long eax;
74 	long xds, xes;		/* unsigned short ds, __ds, es, __es; */
75 	long __fs, __gs;	/* unsigned short fs, __fs, gs, __gs; */
76 	long orig_eax;
77 	long eip;
78 	long xcs;		/* unsigned short cs, __cs; */
79 	long eflags;
80 	long esp;
81 	long xss;		/* unsigned short ss, __ss; */
82 };
83 
84 /* structure used for storing floating point context */
85 struct linux_fpreg {
86   long cwd;
87   long swd;
88   long twd;
89   long fip;
90   long fcs;
91   long foo;
92   long fos;
93   long st_space [20];
94 };
95 
96 /* user struct for linux process - this is used for Linux ptrace emulation */
97 /* most of it is junk only used by gdb */
98 struct linux_user {
99   struct linux_reg regs;	/* registers */
100   int u_fpvalid;		/* true if math co-processor being used. */
101   struct linux_fpreg i387;	/* Math Co-processor registers. */
102 /* The rest of this junk is to help gdb figure out what goes where */
103 #define lusr_startgdb	u_tsize
104   unsigned long int u_tsize;	/* Text segment size (pages). */
105   unsigned long int u_dsize;	/* Data segment size (pages). */
106   unsigned long int u_ssize;	/* Stack segment size (pages). */
107   unsigned long start_code;     /* Starting virtual address of text. */
108   unsigned long start_stack;	/* Starting virtual address of stack area.
109 				   This is actually the bottom of the stack,
110 				   the top of the stack is always found in the
111 				   esp register.  */
112   long int __signal;  		/* Signal that caused the core dump. */
113   int __reserved;		/* unused */
114   void * u_ar0;			/* Used by gdb to help find the values for */
115 				/* the registers. */
116   struct linux_fpreg* u_fpstate;/* Math Co-processor pointer. */
117   unsigned long __magic;	/* To uniquely identify a core file */
118   char u_comm[32];		/* User command that was responsible */
119   int u_debugreg[8];
120 #define u_debugreg_end	u_debugreg[7]
121 };
122 
123 #define LUSR_OFF(member)	offsetof(struct linux_user, member)
124 #define ISSET(t, f)		((t) & (f))
125 
126 int
127 linux_sys_ptrace_arch(p, v, retval)
128 	struct proc *p;
129 	void *v;
130 	register_t *retval;
131 {
132 	struct linux_sys_ptrace_args /* {
133 		syscallarg(int) request;
134 		syscallarg(int) pid;
135 		syscallarg(int) addr;
136 		syscallarg(int) data;
137 	} */ *uap = v;
138 	int request, error;
139 	struct proc *t;				/* target process */
140 	struct reg *regs = NULL;
141 	struct fpreg *fpregs = NULL;
142 	struct linux_reg *linux_regs = NULL;
143 	struct linux_fpreg *linux_fpregs = NULL;
144 	int addr;
145 
146 	request = SCARG(uap, request);
147 
148 	if ((request != LINUX_PTRACE_PEEKUSR) &&
149 	    (request != LINUX_PTRACE_POKEUSR) &&
150 	    (request != LINUX_PTRACE_GETREGS) &&
151 	    (request != LINUX_PTRACE_SETREGS) &&
152 	    (request != LINUX_PTRACE_GETFPREGS) &&
153 	    (request != LINUX_PTRACE_SETFPREGS))
154 		return EIO;
155 
156 	/* Find the process we're supposed to be operating on. */
157 	if ((t = pfind(SCARG(uap, pid))) == NULL)
158 		return ESRCH;
159 
160 	/*
161 	 * You can't do what you want to the process if:
162 	 *	(1) It's not being traced at all,
163 	 */
164 	if (!ISSET(t->p_flag, P_TRACED))
165 		return EPERM;
166 
167 	/*
168 	 *	(2) it's being traced by procfs (which has
169 	 *	    different signal delivery semantics),
170 	 */
171 	if (ISSET(t->p_flag, P_FSTRACE))
172 		return EBUSY;
173 
174 	/*
175 	 *	(3) it's not being traced by _you_, or
176 	 */
177 	if (t->p_pptr != p)
178 		return EBUSY;
179 
180 	/*
181 	 *	(4) it's not currently stopped.
182 	 */
183 	if (t->p_stat != SSTOP || !ISSET(t->p_flag, P_WAITED))
184 		return EBUSY;
185 
186 	*retval = 0;
187 
188 	switch (request) {
189 	case  LINUX_PTRACE_GETREGS:
190 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
191 		MALLOC(linux_regs, struct linux_reg*, sizeof(struct linux_reg),
192 			M_TEMP, M_WAITOK);
193 
194 		error = process_read_regs(t, regs);
195 		if (error != 0)
196 			goto out;
197 
198 		linux_regs->ebx = regs->r_ebx;
199 		linux_regs->ecx = regs->r_ecx;
200 		linux_regs->edx = regs->r_edx;
201 		linux_regs->esi = regs->r_esi;
202 		linux_regs->edi = regs->r_edi;
203 		linux_regs->ebp = regs->r_ebp;
204 		linux_regs->eax = regs->r_eax;
205 		linux_regs->xds = regs->r_ds;
206 		linux_regs->xes = regs->r_es;
207 		linux_regs->orig_eax = regs->r_eax; /* XXX is this correct? */
208 		linux_regs->eip = regs->r_cs + regs->r_eip;
209 		linux_regs->xcs = regs->r_cs;
210 		linux_regs->eflags = regs->r_eflags;
211 		linux_regs->esp = regs->r_esp;
212 		linux_regs->xss = regs->r_ss;
213 
214 		error = copyout(linux_regs, (caddr_t)SCARG(uap, data),
215 		    sizeof(struct linux_reg));
216 		goto out;
217 
218 	case  LINUX_PTRACE_SETREGS:
219 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
220 		MALLOC(linux_regs, struct linux_reg *, sizeof(struct linux_reg),
221 			M_TEMP, M_WAITOK);
222 
223 		error = copyin((caddr_t)SCARG(uap, data), linux_regs,
224 		    sizeof(struct linux_reg));
225 		if (error != 0)
226 			goto out;
227 
228 		regs->r_ebx = linux_regs->ebx;
229 		regs->r_ecx = linux_regs->ecx;
230 		regs->r_edx = linux_regs->edx;
231 		regs->r_esi = linux_regs->esi;
232 		regs->r_edi = linux_regs->edi;
233 		regs->r_ebp = linux_regs->ebp;
234 		regs->r_eax = linux_regs->eax;
235 		regs->r_ds = linux_regs->xds;
236 		regs->r_es = linux_regs->xes;
237 		regs->r_eip = linux_regs->eip - linux_regs->xcs;
238 		regs->r_cs = linux_regs->xcs;
239 		regs->r_eflags = linux_regs->eflags;
240 		regs->r_esp = linux_regs->esp;
241 		regs->r_ss = linux_regs->xss;
242 
243 		error = process_write_regs(t, regs);
244 		goto out;
245 
246 	case  LINUX_PTRACE_GETFPREGS:
247 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
248 			M_TEMP, M_WAITOK);
249 		MALLOC(linux_fpregs, struct linux_fpreg *,
250 			sizeof(struct linux_fpreg), M_TEMP, M_WAITOK);
251 
252 		error = process_read_fpregs(t, fpregs);
253 		if (error != 0)
254 			goto out;
255 
256 		/* zero the contents if NetBSD fpreg structure is smaller */
257 		if (sizeof(struct fpreg) < sizeof(struct linux_fpreg))
258 			memset(linux_fpregs, '\0', sizeof(struct linux_fpreg));
259 
260 		memcpy(linux_fpregs, fpregs,
261 			min(sizeof(struct linux_fpreg), sizeof(struct fpreg)));
262 		error = copyout(linux_fpregs, (caddr_t)SCARG(uap, data),
263 		    sizeof(struct linux_fpreg));
264 		goto out;
265 
266 	case  LINUX_PTRACE_SETFPREGS:
267 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
268 			M_TEMP, M_WAITOK);
269 		MALLOC(linux_fpregs, struct linux_fpreg *,
270 			sizeof(struct linux_fpreg), M_TEMP, M_WAITOK);
271 		error = copyin((caddr_t)SCARG(uap, data), linux_fpregs,
272 		    sizeof(struct linux_fpreg));
273 		if (error != 0)
274 			goto out;
275 
276 		memset(fpregs, '\0', sizeof(struct fpreg));
277 		memcpy(fpregs, linux_fpregs,
278 			min(sizeof(struct linux_fpreg), sizeof(struct fpreg)));
279 
280 		error = process_write_regs(t, regs);
281 		goto out;
282 
283 	case  LINUX_PTRACE_PEEKUSR:
284 		addr = SCARG(uap, addr);
285 
286 		PHOLD(t);	/* need full process info */
287 		error = 0;
288 		if (addr < LUSR_OFF(lusr_startgdb)) {
289 			/* XXX should provide appropriate register */
290 			error = 1;
291 		} else if (addr == LUSR_OFF(u_tsize))
292 			*retval = p->p_vmspace->vm_tsize;
293 		else if (addr == LUSR_OFF(u_dsize))
294 			*retval = p->p_vmspace->vm_dsize;
295 		else if (addr == LUSR_OFF(u_ssize))
296 			*retval = p->p_vmspace->vm_ssize;
297 		else if (addr == LUSR_OFF(start_code))
298 			*retval = (register_t) p->p_vmspace->vm_taddr;
299 		else if (addr == LUSR_OFF(start_stack))
300 			*retval = (register_t) p->p_vmspace->vm_minsaddr;
301 		else if (addr == LUSR_OFF(u_ar0))
302 			*retval = LUSR_OFF(regs);
303 		else if (addr >= LUSR_OFF(u_debugreg)
304 			   && addr <= LUSR_OFF(u_debugreg_end)) {
305 			int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
306 
307 			/* only do this for Linux processes */
308 			if (t->p_emul != &emul_linux)
309 				error = EINVAL;
310 			else {
311 				*retval = ((struct linux_emuldata *)
312 						t->p_emuldata)->debugreg[off];
313 			}
314 		} else if (addr == LUSR_OFF(__signal)) {
315 			error = 1;
316 		} else if (addr == LUSR_OFF(__signal)) {
317 			error = 1;
318 		} else if (addr == LUSR_OFF(u_fpstate)) {
319 			error = 1;
320 		} else if (addr == LUSR_OFF(__magic)) {
321 			error = 1;
322 		} else if (addr == LUSR_OFF(u_comm)) {
323 			error = 1;
324 		} else {
325 #ifdef DEBUG_LINUX
326 			printf("linux_ptrace: unsupported address: %d\n", addr);
327 #endif
328 			error = 1;
329 		}
330 
331 		PRELE(t);
332 
333 		if (!error)
334 			return 0;
335 
336 	case  LINUX_PTRACE_POKEUSR:
337 		/* we only support setting debugregs for now */
338 		addr = SCARG(uap, addr);
339 		if (addr >= LUSR_OFF(u_debugreg)
340 			   && addr <= LUSR_OFF(u_debugreg_end)) {
341 			int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
342 			int data = SCARG(uap, data);
343 
344 			/* only do this for Linux processes */
345 			if (t->p_emul != &emul_linux)
346 				return EINVAL;
347 
348 			PHOLD(t);
349 			((struct linux_emuldata *)t->p_emuldata)->debugreg[off] = data;
350 			PRELE(t);
351 			return (0);
352 		}
353 
354 		break;
355 	default:
356 		/* never reached */
357 		break;
358 	}
359 
360 	return EIO;
361 
362     out:
363 	if (regs)
364 		FREE(regs, M_TEMP);
365 	if (fpregs)
366 		FREE(fpregs, M_TEMP);
367 	if (linux_regs)
368 		FREE(linux_regs, M_TEMP);
369 	if (linux_fpregs)
370 		FREE(linux_fpregs, M_TEMP);
371 	return (error);
372 }
373