xref: /netbsd-src/sys/compat/linux/arch/arm/linux_ptrace.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: linux_ptrace.c,v 1.14 2008/12/17 20:51:33 cegger 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 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.14 2008/12/17 20:51:33 cegger Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/malloc.h>
38 #include <sys/mount.h>
39 #include <sys/proc.h>
40 #include <sys/ptrace.h>
41 #include <sys/systm.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 /*
61  * On ARMv2, uregs contains R0--R15, orig_R0.
62  * On ARMv3 and later, it's R0--R15, CPSR, orig_R0.
63  * As far as I can see, Linux doesn't initialise orig_R0 on ARMv2, so we
64  * just produce the ARMv3 version.
65  */
66 
67 struct linux_reg {
68 	long uregs[18];
69 };
70 
71 #define LINUX_REG_R0	0
72 #define LINUX_REG_R1	1
73 #define LINUX_REG_R2	2
74 #define LINUX_REG_R3	3
75 #define LINUX_REG_R4	4
76 #define LINUX_REG_R5	5
77 #define LINUX_REG_R6	6
78 #define LINUX_REG_R7	7
79 #define LINUX_REG_R8	8
80 #define LINUX_REG_R9	9
81 #define LINUX_REG_R10	10
82 #define LINUX_REG_FP	11
83 #define LINUX_REG_IP	12
84 #define LINUX_REG_SP	13
85 #define LINUX_REG_LR	14
86 #define LINUX_REG_PC	15
87 #define LINUX_REG_CPSR	16
88 #define LINUX_REG_ORIG_R0 17
89 
90 int linux_ptrace_disabled = 1;	/* bitrotted */
91 
92 int
93 linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap, register_t *retval)
94 {
95 	/* {
96 		syscallarg(int) request;
97 		syscallarg(int) pid;
98 		syscallarg(int) addr;
99 		syscallarg(int) data;
100 	} */
101 	struct proc *p = l->l_proc;
102 	int request, error;
103 	struct proc *t;				/* target process */
104 	struct lwp *lt;
105 	struct reg *regs = NULL;
106 	struct fpreg *fpregs = NULL;
107 	struct linux_reg *linux_regs = NULL;
108 	struct linux_fpreg *linux_fpregs = NULL;
109 
110 	if (linux_ptrace_disabled)
111 		return ENOSYS;
112 
113 	request = SCARG(uap, request);
114 
115 	if ((request != LINUX_PTRACE_GETREGS) &&
116 	    (request != LINUX_PTRACE_SETREGS))
117 		return EIO;
118 
119 	/* Find the process we're supposed to be operating on. */
120 	if ((t = pfind(SCARG(uap, pid))) == NULL)
121 		return ESRCH;
122 
123 	/*
124 	 * You can't do what you want to the process if:
125 	 *	(1) It's not being traced at all,
126 	 */
127 	if (!ISSET(t->p_slflag, PSL_TRACED))
128 		return EPERM;
129 
130 	/*
131 	 *	(2) it's being traced by procfs (which has
132 	 *	    different signal delivery semantics),
133 	 */
134 	if (ISSET(t->p_slflag, PSL_FSTRACE))
135 		return EBUSY;
136 
137 	/*
138 	 *	(3) it's not being traced by _you_, or
139 	 */
140 	if (t->p_pptr != p)
141 		return EBUSY;
142 
143 	/*
144 	 *	(4) it's not currently stopped.
145 	 */
146 	if (t->p_stat != SSTOP || !t->p_waited)
147 		return EBUSY;
148 
149 	/* XXX NJWLWP
150 	 * The entire ptrace interface needs work to be useful to
151 	 * a process with multiple LWPs. For the moment, we'll
152 	 * just kluge this and fail on others.
153 	 */
154 
155 	if (p->p_nlwps > 1)
156 		return (ENOSYS);
157 
158 	lt = LIST_FIRST(&t->p_lwps);
159 
160 	*retval = 0;
161 
162 	switch (request) {
163 	case  LINUX_PTRACE_GETREGS:
164 		regs = malloc(sizeof(struct reg), M_TEMP, M_WAITOK);
165 		linux_regs = malloc(sizeof(struct linux_reg),
166 			M_TEMP, M_WAITOK);
167 
168 		error = process_read_regs(lt, regs);
169 		if (error != 0)
170 			goto out;
171 
172 		memcpy(linux_regs->uregs, regs->r, 13 * sizeof(register_t));
173 		linux_regs->uregs[LINUX_REG_SP] = regs->r_sp;
174 		linux_regs->uregs[LINUX_REG_LR] = regs->r_lr;
175 		linux_regs->uregs[LINUX_REG_PC] = regs->r_pc;
176 		linux_regs->uregs[LINUX_REG_CPSR] = regs->r_cpsr;
177 		linux_regs->uregs[LINUX_REG_ORIG_R0] = regs->r[0];
178 
179 		error = copyout(linux_regs, (void *)SCARG(uap, data),
180 		    sizeof(struct linux_reg));
181 		goto out;
182 
183 	case  LINUX_PTRACE_SETREGS:
184 		regs = malloc(sizeof(struct reg), M_TEMP, M_WAITOK);
185 		linux_regs = malloc(sizeof(struct linux_reg),
186 			M_TEMP, M_WAITOK);
187 
188 		error = copyin((void *)SCARG(uap, data), linux_regs,
189 		    sizeof(struct linux_reg));
190 		if (error != 0)
191 			goto out;
192 
193 		memcpy(regs->r, linux_regs->uregs, 13 * sizeof(register_t));
194 		regs->r_sp = linux_regs->uregs[LINUX_REG_SP];
195 		regs->r_lr = linux_regs->uregs[LINUX_REG_LR];
196 		regs->r_pc = linux_regs->uregs[LINUX_REG_PC];
197 		regs->r_cpsr = linux_regs->uregs[LINUX_REG_CPSR];
198 
199 		error = process_write_regs(lt, regs);
200 		goto out;
201 
202 	default:
203 		/* never reached */
204 		break;
205 	}
206 
207 	return EIO;
208 
209     out:
210 	if (regs)
211 		free(regs, M_TEMP);
212 	if (fpregs)
213 		free(fpregs, M_TEMP);
214 	if (linux_regs)
215 		free(linux_regs, M_TEMP);
216 	if (linux_fpregs)
217 		free(linux_fpregs, M_TEMP);
218 	return (error);
219 
220 }
221