linux_ptrace.c revision 1.16.8.1 1 1.16.8.1 yamt /* $NetBSD: linux_ptrace.c,v 1.16.8.1 2012/10/30 17:20:41 yamt Exp $ */
2 1.2 bjh21
3 1.2 bjh21 /*-
4 1.2 bjh21 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.2 bjh21 * All rights reserved.
6 1.2 bjh21 *
7 1.2 bjh21 * This code is derived from software contributed to The NetBSD Foundation
8 1.2 bjh21 * by Matthias Scheler.
9 1.2 bjh21 *
10 1.2 bjh21 * Redistribution and use in source and binary forms, with or without
11 1.2 bjh21 * modification, are permitted provided that the following conditions
12 1.2 bjh21 * are met:
13 1.2 bjh21 * 1. Redistributions of source code must retain the above copyright
14 1.2 bjh21 * notice, this list of conditions and the following disclaimer.
15 1.2 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 bjh21 * notice, this list of conditions and the following disclaimer in the
17 1.2 bjh21 * documentation and/or other materials provided with the distribution.
18 1.2 bjh21 *
19 1.2 bjh21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 bjh21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 bjh21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 bjh21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 bjh21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 bjh21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 bjh21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 bjh21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 bjh21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 bjh21 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 bjh21 * POSSIBILITY OF SUCH DAMAGE.
30 1.2 bjh21 */
31 1.2 bjh21
32 1.2 bjh21
33 1.2 bjh21 #include <sys/cdefs.h>
34 1.16.8.1 yamt __KERNEL_RCSID(0, "$NetBSD: linux_ptrace.c,v 1.16.8.1 2012/10/30 17:20:41 yamt Exp $");
35 1.1 bjh21
36 1.1 bjh21 #include <sys/param.h>
37 1.2 bjh21 #include <sys/malloc.h>
38 1.2 bjh21 #include <sys/mount.h>
39 1.2 bjh21 #include <sys/proc.h>
40 1.2 bjh21 #include <sys/ptrace.h>
41 1.2 bjh21 #include <sys/systm.h>
42 1.2 bjh21 #include <sys/syscallargs.h>
43 1.2 bjh21 #include <uvm/uvm_extern.h>
44 1.2 bjh21
45 1.2 bjh21 #include <machine/reg.h>
46 1.16.8.1 yamt #include <machine/pcb.h>
47 1.2 bjh21
48 1.2 bjh21 #include <compat/linux/common/linux_types.h>
49 1.2 bjh21 #include <compat/linux/common/linux_ptrace.h>
50 1.2 bjh21 #include <compat/linux/common/linux_signal.h>
51 1.2 bjh21
52 1.2 bjh21 #include <compat/linux/common/linux_util.h>
53 1.2 bjh21 #include <compat/linux/common/linux_machdep.h>
54 1.2 bjh21 #include <compat/linux/common/linux_emuldata.h>
55 1.2 bjh21 #include <compat/linux/common/linux_exec.h> /* for emul_linux */
56 1.2 bjh21
57 1.2 bjh21 #include <compat/linux/linux_syscallargs.h>
58 1.1 bjh21
59 1.2 bjh21 #include <lib/libkern/libkern.h> /* for offsetof() */
60 1.1 bjh21
61 1.2 bjh21 /*
62 1.2 bjh21 * On ARMv2, uregs contains R0--R15, orig_R0.
63 1.2 bjh21 * On ARMv3 and later, it's R0--R15, CPSR, orig_R0.
64 1.2 bjh21 * As far as I can see, Linux doesn't initialise orig_R0 on ARMv2, so we
65 1.2 bjh21 * just produce the ARMv3 version.
66 1.2 bjh21 */
67 1.2 bjh21
68 1.2 bjh21 struct linux_reg {
69 1.2 bjh21 long uregs[18];
70 1.2 bjh21 };
71 1.2 bjh21
72 1.2 bjh21 #define LINUX_REG_R0 0
73 1.2 bjh21 #define LINUX_REG_R1 1
74 1.2 bjh21 #define LINUX_REG_R2 2
75 1.2 bjh21 #define LINUX_REG_R3 3
76 1.2 bjh21 #define LINUX_REG_R4 4
77 1.2 bjh21 #define LINUX_REG_R5 5
78 1.2 bjh21 #define LINUX_REG_R6 6
79 1.2 bjh21 #define LINUX_REG_R7 7
80 1.2 bjh21 #define LINUX_REG_R8 8
81 1.2 bjh21 #define LINUX_REG_R9 9
82 1.2 bjh21 #define LINUX_REG_R10 10
83 1.2 bjh21 #define LINUX_REG_FP 11
84 1.2 bjh21 #define LINUX_REG_IP 12
85 1.2 bjh21 #define LINUX_REG_SP 13
86 1.2 bjh21 #define LINUX_REG_LR 14
87 1.2 bjh21 #define LINUX_REG_PC 15
88 1.2 bjh21 #define LINUX_REG_CPSR 16
89 1.2 bjh21 #define LINUX_REG_ORIG_R0 17
90 1.2 bjh21
91 1.12 ad int linux_ptrace_disabled = 1; /* bitrotted */
92 1.12 ad
93 1.1 bjh21 int
94 1.15 rmind linux_sys_ptrace_arch(struct lwp *l, const struct linux_sys_ptrace_args *uap,
95 1.15 rmind register_t *retval)
96 1.1 bjh21 {
97 1.11 dsl /* {
98 1.2 bjh21 syscallarg(int) request;
99 1.2 bjh21 syscallarg(int) pid;
100 1.2 bjh21 syscallarg(int) addr;
101 1.2 bjh21 syscallarg(int) data;
102 1.11 dsl } */
103 1.15 rmind struct proc *p = l->l_proc, *t;
104 1.3 thorpej struct lwp *lt;
105 1.16 chs #ifdef _ARM_ARCH_6
106 1.16 chs struct pcb *pcb;
107 1.16 chs void *val;
108 1.16 chs #endif
109 1.2 bjh21 struct reg *regs = NULL;
110 1.2 bjh21 struct linux_reg *linux_regs = NULL;
111 1.15 rmind int request, error;
112 1.2 bjh21
113 1.12 ad if (linux_ptrace_disabled)
114 1.12 ad return ENOSYS;
115 1.12 ad
116 1.15 rmind error = 0;
117 1.2 bjh21 request = SCARG(uap, request);
118 1.15 rmind regs = kmem_alloc(sizeof(struct reg), KM_SLEEP);
119 1.15 rmind linux_regs = kmem_alloc(sizeof(struct linux_reg), KM_SLEEP);
120 1.2 bjh21
121 1.15 rmind switch (request) {
122 1.15 rmind case LINUX_PTRACE_GETREGS:
123 1.15 rmind break;
124 1.15 rmind case LINUX_PTRACE_SETREGS:
125 1.15 rmind error = copyin((void *)SCARG(uap, data), linux_regs,
126 1.15 rmind sizeof(struct linux_reg));
127 1.15 rmind if (error) {
128 1.15 rmind goto out;
129 1.15 rmind }
130 1.15 rmind break;
131 1.15 rmind default:
132 1.15 rmind error = EIO;
133 1.15 rmind goto out;
134 1.15 rmind }
135 1.2 bjh21
136 1.15 rmind /* Find the process we are supposed to be operating on. */
137 1.15 rmind mutex_enter(proc_lock);
138 1.15 rmind if ((t = proc_find(SCARG(uap, pid))) == NULL) {
139 1.15 rmind mutex_exit(proc_lock);
140 1.15 rmind error = ESRCH;
141 1.15 rmind goto out;
142 1.15 rmind }
143 1.15 rmind mutex_enter(t->p_lock);
144 1.15 rmind mutex_exit(proc_lock);
145 1.2 bjh21
146 1.2 bjh21 /*
147 1.15 rmind * You cannot do what you want to the process if:
148 1.15 rmind * 1. It is not being traced at all,
149 1.2 bjh21 */
150 1.15 rmind if (!ISSET(t->p_slflag, PSL_TRACED)) {
151 1.15 rmind mutex_exit(t->p_lock);
152 1.15 rmind error = EPERM;
153 1.15 rmind goto out;
154 1.15 rmind }
155 1.2 bjh21 /*
156 1.15 rmind * 2. It is being traced by procfs (which has different signal
157 1.15 rmind * delivery semantics),
158 1.15 rmind * 3. It is not being traced by _you_, or
159 1.15 rmind * 4. It is not currently stopped.
160 1.2 bjh21 */
161 1.15 rmind if (ISSET(t->p_slflag, PSL_FSTRACE) || t->p_pptr != p ||
162 1.15 rmind t->p_stat != SSTOP || !t->p_waited) {
163 1.15 rmind mutex_exit(t->p_lock);
164 1.15 rmind error = EBUSY;
165 1.15 rmind goto out;
166 1.15 rmind }
167 1.15 rmind /* XXX: ptrace needs revamp for multi-threading support. */
168 1.15 rmind if (t->p_nlwps > 1) {
169 1.15 rmind mutex_exit(t->p_lock);
170 1.15 rmind error = ENOSYS;
171 1.15 rmind goto out;
172 1.15 rmind }
173 1.3 thorpej lt = LIST_FIRST(&t->p_lwps);
174 1.2 bjh21 *retval = 0;
175 1.2 bjh21
176 1.2 bjh21 switch (request) {
177 1.15 rmind case LINUX_PTRACE_GETREGS:
178 1.3 thorpej error = process_read_regs(lt, regs);
179 1.15 rmind mutex_exit(t->p_lock);
180 1.15 rmind if (error) {
181 1.15 rmind break;
182 1.15 rmind }
183 1.2 bjh21 memcpy(linux_regs->uregs, regs->r, 13 * sizeof(register_t));
184 1.2 bjh21 linux_regs->uregs[LINUX_REG_SP] = regs->r_sp;
185 1.2 bjh21 linux_regs->uregs[LINUX_REG_LR] = regs->r_lr;
186 1.2 bjh21 linux_regs->uregs[LINUX_REG_PC] = regs->r_pc;
187 1.2 bjh21 linux_regs->uregs[LINUX_REG_CPSR] = regs->r_cpsr;
188 1.2 bjh21 linux_regs->uregs[LINUX_REG_ORIG_R0] = regs->r[0];
189 1.2 bjh21
190 1.9 christos error = copyout(linux_regs, (void *)SCARG(uap, data),
191 1.2 bjh21 sizeof(struct linux_reg));
192 1.15 rmind break;
193 1.2 bjh21
194 1.15 rmind case LINUX_PTRACE_SETREGS:
195 1.2 bjh21 memcpy(regs->r, linux_regs->uregs, 13 * sizeof(register_t));
196 1.2 bjh21 regs->r_sp = linux_regs->uregs[LINUX_REG_SP];
197 1.2 bjh21 regs->r_lr = linux_regs->uregs[LINUX_REG_LR];
198 1.2 bjh21 regs->r_pc = linux_regs->uregs[LINUX_REG_PC];
199 1.2 bjh21 regs->r_cpsr = linux_regs->uregs[LINUX_REG_CPSR];
200 1.3 thorpej error = process_write_regs(lt, regs);
201 1.16 chs mutex_exit(t->p_lock);
202 1.16 chs break;
203 1.16 chs
204 1.16 chs #ifdef _ARM_ARCH_6
205 1.16 chs #define LINUX_PTRACE_GET_THREAD_AREA 22
206 1.16 chs case LINUX_PTRACE_GET_THREAD_AREA:
207 1.16 chs mutex_exit(t->p_lock);
208 1.16 chs pcb = lwp_getpcb(l);
209 1.16 chs val = (void *)pcb->pcb_un.un_32.pcb32_user_pid_ro;
210 1.16 chs error = copyout(&val, (void *)SCARG(uap, data), sizeof(val));
211 1.16 chs break;
212 1.16 chs #endif
213 1.16 chs
214 1.2 bjh21 default:
215 1.15 rmind mutex_exit(t->p_lock);
216 1.2 bjh21 }
217 1.15 rmind out:
218 1.2 bjh21 if (regs)
219 1.15 rmind kmem_free(regs, sizeof(*regs));
220 1.2 bjh21 if (linux_regs)
221 1.15 rmind kmem_free(linux_regs, sizeof(*linux_regs));
222 1.15 rmind return error;
223 1.1 bjh21
224 1.1 bjh21 }
225