1 /**	$MirOS: src/sys/compat/linux/linux_ptrace.c,v 1.2 2008/09/06 22:21:00 tg Exp $ */
2 /*	$NetBSD: linux_ptrace.c,v 1.13 2005/12/11 12:20:14 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Matthias Scheler.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.13 2005/12/11 12:20:14 christos Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/proc.h>
47 #include <sys/ptrace.h>
48 #include <sys/systm.h>
49 #include <sys/syscallargs.h>
50 #include <uvm/uvm_extern.h>
51 
52 #include <machine/reg.h>
53 
54 #include <compat/linux/linux_types.h>
55 #include <compat/linux/linux_ptrace.h>
56 #include <compat/linux/linux_signal.h>
57 
58 #include <compat/linux/linux_util.h>
59 #include <compat/linux/linux_emuldata.h>
60 
61 #include <compat/linux/linux_syscallargs.h>
62 
63 #include <sys/slibkern.h>	/* for offsetof() */
64 
65 extern struct emul emul_linux_aout, emul_linux_elf;
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
linux_sys_ptrace_arch(l,v,retval)128 linux_sys_ptrace_arch(l, v, retval)
129 	struct proc *l;
130 	void *v;
131 	register_t *retval;
132 {
133 	struct linux_sys_ptrace_args /* {
134 		syscallarg(int) request;
135 		syscallarg(int) pid;
136 		syscallarg(int) addr;
137 		syscallarg(int) data;
138 	} */ *uap = v;
139 	struct proc *p = l;
140 	int request, error;
141 	struct proc *t;				/* target process */
142 	struct reg *regs = NULL;
143 	struct fpreg *fpregs = NULL;
144 	struct linux_reg *linux_regs = NULL;
145 	struct linux_fpctx *linux_fpregs = NULL;
146 	int addr;
147 
148 	request = SCARG(uap, request);
149 
150 	if ((request != LINUX_PTRACE_PEEKUSR) &&
151 	    (request != LINUX_PTRACE_POKEUSR) &&
152 	    (request != LINUX_PTRACE_GETREGS) &&
153 	    (request != LINUX_PTRACE_SETREGS) &&
154 	    (request != LINUX_PTRACE_GETFPREGS) &&
155 	    (request != LINUX_PTRACE_SETFPREGS))
156 		return EIO;
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_flag, P_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_flag, P_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 || !ISSET(t->p_flag, P_WAITED))
186 		return EBUSY;
187 
188 	*retval = 0;
189 
190 	switch (request) {
191 	case  LINUX_PTRACE_GETREGS:
192 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
193 		MALLOC(linux_regs, struct linux_reg*, sizeof(struct linux_reg),
194 			M_TEMP, M_WAITOK);
195 
196 		error = process_read_regs(t, regs);
197 		if (error != 0)
198 			goto out;
199 
200 		linux_regs->ebx = regs->r_ebx;
201 		linux_regs->ecx = regs->r_ecx;
202 		linux_regs->edx = regs->r_edx;
203 		linux_regs->esi = regs->r_esi;
204 		linux_regs->edi = regs->r_edi;
205 		linux_regs->ebp = regs->r_ebp;
206 		linux_regs->eax = regs->r_eax;
207 		linux_regs->xds = regs->r_ds;
208 		linux_regs->xes = regs->r_es;
209 		linux_regs->orig_eax = regs->r_eax; /* XXX is this correct? */
210 		linux_regs->eip = regs->r_cs + regs->r_eip;
211 		linux_regs->xcs = regs->r_cs;
212 		linux_regs->eflags = regs->r_eflags;
213 		linux_regs->esp = regs->r_esp;
214 		linux_regs->xss = regs->r_ss;
215 
216 		error = copyout(linux_regs, (caddr_t)SCARG(uap, data),
217 		    sizeof(struct linux_reg));
218 		goto out;
219 
220 	case  LINUX_PTRACE_SETREGS:
221 		MALLOC(regs, struct reg*, sizeof(struct reg), M_TEMP, M_WAITOK);
222 		MALLOC(linux_regs, struct linux_reg *, sizeof(struct linux_reg),
223 			M_TEMP, M_WAITOK);
224 
225 		error = copyin((caddr_t)SCARG(uap, data), linux_regs,
226 		    sizeof(struct linux_reg));
227 		if (error != 0)
228 			goto out;
229 
230 		regs->r_ebx = linux_regs->ebx;
231 		regs->r_ecx = linux_regs->ecx;
232 		regs->r_edx = linux_regs->edx;
233 		regs->r_esi = linux_regs->esi;
234 		regs->r_edi = linux_regs->edi;
235 		regs->r_ebp = linux_regs->ebp;
236 		regs->r_eax = linux_regs->eax;
237 		regs->r_ds = linux_regs->xds;
238 		regs->r_es = linux_regs->xes;
239 		regs->r_eip = linux_regs->eip - linux_regs->xcs;
240 		regs->r_cs = linux_regs->xcs;
241 		regs->r_eflags = linux_regs->eflags;
242 		regs->r_esp = linux_regs->esp;
243 		regs->r_ss = linux_regs->xss;
244 
245 		error = process_write_regs(t, regs);
246 		goto out;
247 
248 	case  LINUX_PTRACE_GETFPREGS:
249 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
250 			M_TEMP, M_WAITOK);
251 		MALLOC(linux_fpregs, struct linux_fpctx *,
252 			sizeof(struct linux_fpctx), M_TEMP, M_WAITOK);
253 
254 		error = process_read_fpregs(t, fpregs);
255 		if (error != 0)
256 			goto out;
257 
258 		/* zero the contents if NetBSD fpreg structure is smaller */
259 		if (sizeof(struct fpreg) < sizeof(struct linux_fpctx))
260 			memset(linux_fpregs, '\0', sizeof(struct linux_fpctx));
261 
262 		memcpy(linux_fpregs, fpregs,
263 			min(sizeof(struct linux_fpctx), sizeof(struct fpreg)));
264 		error = copyout(linux_fpregs, (caddr_t)SCARG(uap, data),
265 		    sizeof(struct linux_fpctx));
266 		goto out;
267 
268 	case  LINUX_PTRACE_SETFPREGS:
269 		MALLOC(fpregs, struct fpreg *, sizeof(struct fpreg),
270 			M_TEMP, M_WAITOK);
271 		MALLOC(linux_fpregs, struct linux_fpctx *,
272 			sizeof(struct linux_fpctx), M_TEMP, M_WAITOK);
273 		error = copyin((caddr_t)SCARG(uap, data), linux_fpregs,
274 		    sizeof(struct linux_fpctx));
275 		if (error != 0)
276 			goto out;
277 
278 		memset(fpregs, '\0', sizeof(struct fpreg));
279 		memcpy(fpregs, linux_fpregs,
280 			min(sizeof(struct linux_fpctx), sizeof(struct fpreg)));
281 
282 		error = process_write_regs(t, regs);
283 		goto out;
284 
285 	case  LINUX_PTRACE_PEEKUSR:
286 		addr = SCARG(uap, addr);
287 
288 		PHOLD(t);	/* need full process info */
289 		error = 0;
290 		if (addr < LUSR_OFF(lusr_startgdb)) {
291 			/* XXX should provide appropriate register */
292 			error = 1;
293 		} else if (addr == LUSR_OFF(u_tsize))
294 			*retval = p->p_vmspace->vm_tsize;
295 		else if (addr == LUSR_OFF(u_dsize))
296 			*retval = p->p_vmspace->vm_dsize;
297 		else if (addr == LUSR_OFF(u_ssize))
298 			*retval = p->p_vmspace->vm_ssize;
299 		else if (addr == LUSR_OFF(start_code))
300 			*retval = (register_t) p->p_vmspace->vm_taddr;
301 		else if (addr == LUSR_OFF(start_stack))
302 			*retval = (register_t) p->p_vmspace->vm_minsaddr;
303 		else if (addr == LUSR_OFF(u_ar0))
304 			*retval = LUSR_OFF(regs);
305 		else if (addr >= LUSR_OFF(u_debugreg)
306 			   && addr <= LUSR_OFF(u_debugreg_end)) {
307 			int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
308 
309 			/* only do this for Linux processes */
310 			if ((t->p_emul != &emul_linux_elf) &&
311 			    (t->p_emul != &emul_linux_aout))
312 				error = EINVAL;
313 			else {
314 				*retval = ((struct linux_emuldata *)
315 						t->p_emuldata)->debugreg[off];
316 			}
317 		} else if (addr == LUSR_OFF(__signal)) {
318 			error = 1;
319 		} else if (addr == LUSR_OFF(__signal)) {
320 			error = 1;
321 		} else if (addr == LUSR_OFF(u_fpstate)) {
322 			error = 1;
323 		} else if (addr == LUSR_OFF(__magic)) {
324 			error = 1;
325 		} else if (addr == LUSR_OFF(u_comm)) {
326 			error = 1;
327 		} else {
328 #ifdef DEBUG_LINUX
329 			printf("linux_ptrace: unsupported address: %d\n", addr);
330 #endif
331 			error = 1;
332 		}
333 
334 		PRELE(t);
335 
336 		if (!error)
337 			return 0;
338 
339 	case  LINUX_PTRACE_POKEUSR:
340 		/* we only support setting debugregs for now */
341 		addr = SCARG(uap, addr);
342 		if (addr >= LUSR_OFF(u_debugreg)
343 			   && addr <= LUSR_OFF(u_debugreg_end)) {
344 			int off = (addr - LUSR_OFF(u_debugreg)) / sizeof(int);
345 			int data = SCARG(uap, data);
346 
347 			/* only do this for Linux processes */
348 			if ((t->p_emul != &emul_linux_elf) &&
349 			    (t->p_emul != &emul_linux_aout))
350 				return EINVAL;
351 
352 			PHOLD(t);
353 			((struct linux_emuldata *)t->p_emuldata)->debugreg[off] = data;
354 			PRELE(t);
355 			return (0);
356 		}
357 
358 		break;
359 	default:
360 		/* never reached */
361 		break;
362 	}
363 
364 	return EIO;
365 
366     out:
367 	if (regs)
368 		FREE(regs, M_TEMP);
369 	if (fpregs)
370 		FREE(fpregs, M_TEMP);
371 	if (linux_regs)
372 		FREE(linux_regs, M_TEMP);
373 	if (linux_fpregs)
374 		FREE(linux_fpregs, M_TEMP);
375 	return (error);
376 }
377