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