xref: /netbsd-src/sys/compat/linux/arch/i386/linux_ptrace.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: linux_ptrace.c,v 1.19 2007/12/08 18:36:06 dsl 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.19 2007/12/08 18:36:06 dsl 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/syscall.h>
49 #include <sys/syscallargs.h>
50 #include <uvm/uvm_extern.h>
51 
52 #include <machine/reg.h>
53 
54 #include <compat/linux/common/linux_types.h>
55 #include <compat/linux/common/linux_ptrace.h>
56 #include <compat/linux/common/linux_signal.h>
57 
58 #include <compat/linux/common/linux_util.h>
59 #include <compat/linux/common/linux_machdep.h>
60 #include <compat/linux/common/linux_emuldata.h>
61 #include <compat/linux/common/linux_exec.h>	/* for emul_linux */
62 
63 #include <compat/linux/linux_syscallargs.h>
64 
65 #include <lib/libkern/libkern.h>	/* for offsetof() */
66 
67 struct linux_reg {
68 	long ebx;
69 	long ecx;
70 	long edx;
71 	long esi;
72 	long edi;
73 	long ebp;
74 	long eax;
75 	long xds, xes;		/* unsigned short ds, __ds, es, __es; */
76 	long __fs, __gs;	/* unsigned short fs, __fs, gs, __gs; */
77 	long orig_eax;
78 	long eip;
79 	long xcs;		/* unsigned short cs, __cs; */
80 	long eflags;
81 	long esp;
82 	long xss;		/* unsigned short ss, __ss; */
83 };
84 
85 /* structure used for storing floating point context */
86 struct linux_fpctx {
87 	long cwd;
88 	long swd;
89 	long twd;
90 	long fip;
91 	long fcs;
92 	long foo;
93 	long fos;
94 	long st_space[20];
95 };
96 
97 /* user struct for linux process - this is used for Linux ptrace emulation */
98 /* most of it is junk only used by gdb */
99 struct linux_user {
100 	struct linux_reg regs;		/* registers */
101 	int u_fpvalid;		/* true if math co-processor being used. */
102 	struct linux_fpctx i387;	/* Math Co-processor registers. */
103 /* The rest of this junk is to help gdb figure out what goes where */
104 #define lusr_startgdb	u_tsize
105 	unsigned long int u_tsize;	/* Text segment size (pages). */
106 	unsigned long int u_dsize;	/* Data segment size (pages). */
107 	unsigned long int u_ssize;	/* Stack segment size (pages). */
108 	unsigned long start_code;	/* Starting virtual address of text. */
109 	unsigned long start_stack;	/* Starting virtual address of stack
110 					   area. This is actually the bottom of
111 					   the stack, the top of the stack is
112 					   always found in the esp register. */
113 	long int __signal;  		/* Signal that caused the core dump. */
114 	int __reserved;			/* unused */
115 	void *u_ar0;			/* Used by gdb to help find the values
116 					   for the registers. */
117 	struct linux_fpctx *u_fpstate;	/* Math Co-processor pointer. */
118 	unsigned long __magic;		/* To uniquely identify a core file */
119 	char u_comm[32];		/* User command that was responsible */
120 	int u_debugreg[8];
121 #define u_debugreg_end	u_debugreg[7]
122 };
123 
124 #define LUSR_OFF(member)	offsetof(struct linux_user, member)
125 #define ISSET(t, f)		((t) & (f))
126 
127 int
128 linux_sys_ptrace_arch(struct lwp *l, void *v, register_t *retval)
129 {
130 	struct linux_sys_ptrace_args /* {
131 		syscallarg(int) request;
132 		syscallarg(int) pid;
133 		syscallarg(int) addr;
134 		syscallarg(int) data;
135 	} */ *uap = v;
136 	struct proc *p = l->l_proc;
137 	int request, error;
138 	struct proc *t;				/* target process */
139 	struct lwp *lt;
140 	struct reg *regs = NULL;
141 	struct fpreg *fpregs = NULL;
142 	struct linux_reg *linux_regs = NULL;
143 	struct linux_fpctx *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 	/* XXXAD locking */
157 
158 	/* Find the process we're supposed to be operating on. */
159 	if ((t = pfind(SCARG(uap, pid))) == NULL)
160 		return ESRCH;
161 
162 	/*
163 	 * You can't do what you want to the process if:
164 	 *	(1) It's not being traced at all,
165 	 */
166 	if (!ISSET(t->p_slflag, PSL_TRACED))
167 		return EPERM;
168 
169 	/*
170 	 *	(2) it's being traced by procfs (which has
171 	 *	    different signal delivery semantics),
172 	 */
173 	if (ISSET(t->p_slflag, PSL_FSTRACE))
174 		return EBUSY;
175 
176 	/*
177 	 *	(3) it's not being traced by _you_, or
178 	 */
179 	if (t->p_pptr != p)
180 		return EBUSY;
181 
182 	/*
183 	 *	(4) it's not currently stopped.
184 	 */
185 	if (t->p_stat != SSTOP || !t->p_waited /* XXXSMP */)
186 		return EBUSY;
187 
188 	/* XXX NJWLWP
189 	 * The entire ptrace interface needs work to be useful to
190 	 * a process with multiple LWPs. For the moment, we'll
191 	 * just kluge this and fail on others.
192 	 */
193 
194 	if (p->p_nlwps > 1)
195 		return (ENOSYS);
196 
197 	lt = LIST_FIRST(&t->p_lwps);
198 
199 	*retval = 0;
200 
201 	switch (request) {
202 	case  LINUX_PTRACE_GETREGS:
203 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
204 		MALLOC(linux_regs, struct linux_reg*, sizeof(struct linux_reg),
205 			M_TEMP, M_WAITOK);
206 
207 		error = process_read_regs(lt, regs);
208 		if (error != 0)
209 			goto out;
210 
211 		linux_regs->ebx = regs->r_ebx;
212 		linux_regs->ecx = regs->r_ecx;
213 		linux_regs->edx = regs->r_edx;
214 		linux_regs->esi = regs->r_esi;
215 		linux_regs->edi = regs->r_edi;
216 		linux_regs->ebp = regs->r_ebp;
217 		linux_regs->eax = regs->r_eax;
218 		linux_regs->xds = regs->r_ds;
219 		linux_regs->xes = regs->r_es;
220 		linux_regs->orig_eax = regs->r_eax; /* XXX is this correct? */
221 		linux_regs->eip = regs->r_cs + regs->r_eip;
222 		linux_regs->xcs = regs->r_cs;
223 		linux_regs->eflags = regs->r_eflags;
224 		linux_regs->esp = regs->r_esp;
225 		linux_regs->xss = regs->r_ss;
226 
227 		error = copyout(linux_regs, (void *)SCARG(uap, data),
228 		    sizeof(struct linux_reg));
229 		goto out;
230 
231 	case  LINUX_PTRACE_SETREGS:
232 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
233 		MALLOC(linux_regs, struct linux_reg *, sizeof(struct linux_reg),
234 			M_TEMP, M_WAITOK);
235 
236 		error = copyin((void *)SCARG(uap, data), linux_regs,
237 		    sizeof(struct linux_reg));
238 		if (error != 0)
239 			goto out;
240 
241 		regs->r_ebx = linux_regs->ebx;
242 		regs->r_ecx = linux_regs->ecx;
243 		regs->r_edx = linux_regs->edx;
244 		regs->r_esi = linux_regs->esi;
245 		regs->r_edi = linux_regs->edi;
246 		regs->r_ebp = linux_regs->ebp;
247 		regs->r_eax = linux_regs->eax;
248 		regs->r_ds = linux_regs->xds;
249 		regs->r_es = linux_regs->xes;
250 		regs->r_eip = linux_regs->eip - linux_regs->xcs;
251 		regs->r_cs = linux_regs->xcs;
252 		regs->r_eflags = linux_regs->eflags;
253 		regs->r_esp = linux_regs->esp;
254 		regs->r_ss = linux_regs->xss;
255 
256 		error = process_write_regs(lt, regs);
257 		goto out;
258 
259 	case  LINUX_PTRACE_GETFPREGS:
260 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
261 			M_TEMP, M_WAITOK);
262 		MALLOC(linux_fpregs, struct linux_fpctx *,
263 			sizeof(struct linux_fpctx), M_TEMP, M_WAITOK);
264 
265 		error = process_read_fpregs(lt, fpregs);
266 		if (error != 0)
267 			goto out;
268 
269 		/* zero the contents if NetBSD fpreg structure is smaller */
270 		if (sizeof(struct fpreg) < sizeof(struct linux_fpctx))
271 			memset(linux_fpregs, '\0', sizeof(struct linux_fpctx));
272 
273 		memcpy(linux_fpregs, fpregs,
274 			min(sizeof(struct linux_fpctx), sizeof(struct fpreg)));
275 		error = copyout(linux_fpregs, (void *)SCARG(uap, data),
276 		    sizeof(struct linux_fpctx));
277 		goto out;
278 
279 	case  LINUX_PTRACE_SETFPREGS:
280 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
281 			M_TEMP, M_WAITOK);
282 		MALLOC(linux_fpregs, struct linux_fpctx *,
283 			sizeof(struct linux_fpctx), M_TEMP, M_WAITOK);
284 		error = copyin((void *)SCARG(uap, data), linux_fpregs,
285 		    sizeof(struct linux_fpctx));
286 		if (error != 0)
287 			goto out;
288 
289 		memset(fpregs, '\0', sizeof(struct fpreg));
290 		memcpy(fpregs, linux_fpregs,
291 			min(sizeof(struct linux_fpctx), sizeof(struct fpreg)));
292 
293 		error = process_write_regs(lt, regs);
294 		goto out;
295 
296 	case  LINUX_PTRACE_PEEKUSR:
297 		addr = SCARG(uap, addr);
298 
299 		uvm_lwp_hold(lt);	/* need full process info */
300 		error = 0;
301 		if (addr < LUSR_OFF(lusr_startgdb)) {
302 			/* XXX should provide appropriate register */
303 			error = 1;
304 		} else if (addr == LUSR_OFF(u_tsize))
305 			*retval = p->p_vmspace->vm_tsize;
306 		else if (addr == LUSR_OFF(u_dsize))
307 			*retval = p->p_vmspace->vm_dsize;
308 		else if (addr == LUSR_OFF(u_ssize))
309 			*retval = p->p_vmspace->vm_ssize;
310 		else if (addr == LUSR_OFF(start_code))
311 			*retval = (register_t) p->p_vmspace->vm_taddr;
312 		else if (addr == LUSR_OFF(start_stack))
313 			*retval = (register_t) p->p_vmspace->vm_minsaddr;
314 		else if (addr == LUSR_OFF(u_ar0))
315 			*retval = LUSR_OFF(regs);
316 		else if (addr >= LUSR_OFF(u_debugreg)
317 			   && addr <= LUSR_OFF(u_debugreg_end)) {
318 			int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
319 
320 			/* only do this for Linux processes */
321 			if (t->p_emul != &emul_linux)
322 				error = EINVAL;
323 			else {
324 				*retval = ((struct linux_emuldata *)
325 						t->p_emuldata)->debugreg[off];
326 			}
327 		} else if (addr == LUSR_OFF(__signal)) {
328 			error = 1;
329 		} else if (addr == LUSR_OFF(__signal)) {
330 			error = 1;
331 		} else if (addr == LUSR_OFF(u_fpstate)) {
332 			error = 1;
333 		} else if (addr == LUSR_OFF(__magic)) {
334 			error = 1;
335 		} else if (addr == LUSR_OFF(u_comm)) {
336 			error = 1;
337 		} else {
338 #ifdef DEBUG_LINUX
339 			printf("linux_ptrace: unsupported address: %d\n", addr);
340 #endif
341 			error = 1;
342 		}
343 
344 		uvm_lwp_rele(lt);
345 
346 		if (!error)
347 			return 0;
348 
349 	case  LINUX_PTRACE_POKEUSR:
350 		/* we only support setting debugregs for now */
351 		addr = SCARG(uap, addr);
352 		if (addr >= LUSR_OFF(u_debugreg)
353 			   && addr <= LUSR_OFF(u_debugreg_end)) {
354 			int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
355 			int data = SCARG(uap, data);
356 
357 			/* only do this for Linux processes */
358 			if (t->p_emul != &emul_linux)
359 				return EINVAL;
360 
361 			uvm_lwp_hold(lt);
362 			((struct linux_emuldata *)t->p_emuldata)->debugreg[off] = data;
363 			uvm_lwp_rele(lt);
364 			return (0);
365 		}
366 
367 		break;
368 	default:
369 		/* never reached */
370 		break;
371 	}
372 
373 	return EIO;
374 
375     out:
376 	if (regs)
377 		FREE(regs, M_TEMP);
378 	if (fpregs)
379 		FREE(fpregs, M_TEMP);
380 	if (linux_regs)
381 		FREE(linux_regs, M_TEMP);
382 	if (linux_fpregs)
383 		FREE(linux_fpregs, M_TEMP);
384 	return (error);
385 }
386