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