xref: /netbsd-src/sys/compat/linux/arch/arm/linux_ptrace.c (revision 54c71dee8ce8ff710b7e2b5a511b77d6cae19a0e)
1 /*	$NetBSD: linux_ptrace.c,v 1.15 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 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.15 2010/07/01 02:38:28 rmind 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,
94     register_t *retval)
95 {
96 	/* {
97 		syscallarg(int) request;
98 		syscallarg(int) pid;
99 		syscallarg(int) addr;
100 		syscallarg(int) data;
101 	} */
102 	struct proc *p = l->l_proc, *t;
103 	struct lwp *lt;
104 	struct reg *regs = NULL;
105 	struct linux_reg *linux_regs = NULL;
106 	int request, error;
107 
108 	if (linux_ptrace_disabled)
109 		return ENOSYS;
110 
111 	error = 0;
112 	request = SCARG(uap, request);
113 	regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
114 	linux_regs = kmem_alloc(sizeof(struct linux_reg), KM_SLEEP);
115 
116 	switch (request) {
117 	case LINUX_PTRACE_GETREGS:
118 		break;
119 	case LINUX_PTRACE_SETREGS:
120 		error = copyin((void *)SCARG(uap, data), linux_regs,
121 		    sizeof(struct linux_reg));
122 		if (error) {
123 			goto out;
124 		}
125 		break;
126 	default:
127 		error = EIO;
128 		goto out;
129 	}
130 
131 	/* Find the process we are supposed to be operating on. */
132 	mutex_enter(proc_lock);
133 	if ((t = proc_find(SCARG(uap, pid))) == NULL) {
134 		mutex_exit(proc_lock);
135 		error = ESRCH;
136 		goto out;
137 	}
138 	mutex_enter(t->p_lock);
139 	mutex_exit(proc_lock);
140 
141 	/*
142 	 * You cannot do what you want to the process if:
143 	 * 1. It is not being traced at all,
144 	 */
145 	if (!ISSET(t->p_slflag, PSL_TRACED)) {
146 		mutex_exit(t->p_lock);
147 		error = EPERM;
148 		goto out;
149 	}
150 	/*
151 	 * 2. It is being traced by procfs (which has different signal
152 	 *    delivery semantics),
153 	 * 3. It is not being traced by _you_, or
154 	 * 4. It is not currently stopped.
155 	 */
156 	if (ISSET(t->p_slflag, PSL_FSTRACE) || t->p_pptr != p ||
157 	    t->p_stat != SSTOP || !t->p_waited) {
158 		mutex_exit(t->p_lock);
159 		error = EBUSY;
160 		goto out;
161 	}
162 	/* XXX: ptrace needs revamp for multi-threading support. */
163 	if (t->p_nlwps > 1) {
164 		mutex_exit(t->p_lock);
165 		error = ENOSYS;
166 		goto out;
167 	}
168 	lt = LIST_FIRST(&t->p_lwps);
169 	*retval = 0;
170 
171 	switch (request) {
172 	case LINUX_PTRACE_GETREGS:
173 		error = process_read_regs(lt, regs);
174 		mutex_exit(t->p_lock);
175 		if (error) {
176 			break;
177 		}
178 		memcpy(linux_regs->uregs, regs->r, 13 * sizeof(register_t));
179 		linux_regs->uregs[LINUX_REG_SP] = regs->r_sp;
180 		linux_regs->uregs[LINUX_REG_LR] = regs->r_lr;
181 		linux_regs->uregs[LINUX_REG_PC] = regs->r_pc;
182 		linux_regs->uregs[LINUX_REG_CPSR] = regs->r_cpsr;
183 		linux_regs->uregs[LINUX_REG_ORIG_R0] = regs->r[0];
184 
185 		error = copyout(linux_regs, (void *)SCARG(uap, data),
186 		    sizeof(struct linux_reg));
187 		break;
188 
189 	case LINUX_PTRACE_SETREGS:
190 		memcpy(regs->r, linux_regs->uregs, 13 * sizeof(register_t));
191 		regs->r_sp = linux_regs->uregs[LINUX_REG_SP];
192 		regs->r_lr = linux_regs->uregs[LINUX_REG_LR];
193 		regs->r_pc = linux_regs->uregs[LINUX_REG_PC];
194 		regs->r_cpsr = linux_regs->uregs[LINUX_REG_CPSR];
195 		error = process_write_regs(lt, regs);
196 		/* FALLTHROUGH */
197 	default:
198 		mutex_exit(t->p_lock);
199 	}
200 out:
201 	if (regs)
202 		kmem_free(regs, sizeof(*regs));
203 	if (linux_regs)
204 		kmem_free(linux_regs, sizeof(*linux_regs));
205 	return error;
206 
207 }
208