1 /*-
2 * Copyright (c) 2001 Alexander Kabaev
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_cpu.h"
33
34 #include <sys/param.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <sys/ptrace.h>
39 #include <sys/syscallsubr.h>
40 #include <sys/systm.h>
41
42 #include <machine/md_var.h>
43 #include <machine/pcb.h>
44 #include <machine/reg.h>
45
46 #include <i386/linux/linux.h>
47 #include <i386/linux/linux_proto.h>
48
49 #if !defined(CPU_ENABLE_SSE) && defined(I686_CPU)
50 #define CPU_ENABLE_SSE
51 #endif
52 #if defined(CPU_DISABLE_SSE)
53 #undef CPU_ENABLE_SSE
54 #endif
55
56 /*
57 * Linux ptrace requests numbers. Mostly identical to FreeBSD,
58 * except for MD ones and PT_ATTACH/PT_DETACH.
59 */
60 #define PTRACE_TRACEME 0
61 #define PTRACE_PEEKTEXT 1
62 #define PTRACE_PEEKDATA 2
63 #define PTRACE_PEEKUSR 3
64 #define PTRACE_POKETEXT 4
65 #define PTRACE_POKEDATA 5
66 #define PTRACE_POKEUSR 6
67 #define PTRACE_CONT 7
68 #define PTRACE_KILL 8
69 #define PTRACE_SINGLESTEP 9
70
71 #define PTRACE_ATTACH 16
72 #define PTRACE_DETACH 17
73
74 #define PTRACE_SYSCALL 24
75
76 #define PTRACE_GETREGS 12
77 #define PTRACE_SETREGS 13
78 #define PTRACE_GETFPREGS 14
79 #define PTRACE_SETFPREGS 15
80 #define PTRACE_GETFPXREGS 18
81 #define PTRACE_SETFPXREGS 19
82
83 #define PTRACE_SETOPTIONS 21
84
85 /*
86 * Linux keeps debug registers at the following
87 * offset in the user struct
88 */
89 #define LINUX_DBREG_OFFSET 252
90 #define LINUX_DBREG_SIZE (8*sizeof(l_int))
91
92 static __inline int
93 map_signum(int signum)
94 {
95
96 if (signum > 0 && signum <= LINUX_SIGTBLSZ)
97 signum = linux_to_bsd_signal[_SIG_IDX(signum)];
98 return ((signum == SIGSTOP)? 0 : signum);
99 }
100
101 struct linux_pt_reg {
102 l_long ebx;
103 l_long ecx;
104 l_long edx;
105 l_long esi;
106 l_long edi;
107 l_long ebp;
108 l_long eax;
109 l_int xds;
110 l_int xes;
111 l_int xfs;
112 l_int xgs;
113 l_long orig_eax;
114 l_long eip;
115 l_int xcs;
116 l_long eflags;
117 l_long esp;
118 l_int xss;
119 };
120
121 /*
122 * Translate i386 ptrace registers between Linux and FreeBSD formats.
123 * The translation is pretty straighforward, for all registers, but
124 * orig_eax on Linux side and r_trapno and r_err in FreeBSD
125 */
126 static void
127 map_regs_to_linux(struct reg *bsd_r, struct linux_pt_reg *linux_r)
128 {
129 linux_r->ebx = bsd_r->r_ebx;
130 linux_r->ecx = bsd_r->r_ecx;
131 linux_r->edx = bsd_r->r_edx;
132 linux_r->esi = bsd_r->r_esi;
133 linux_r->edi = bsd_r->r_edi;
134 linux_r->ebp = bsd_r->r_ebp;
135 linux_r->eax = bsd_r->r_eax;
136 linux_r->xds = bsd_r->r_ds;
137 linux_r->xes = bsd_r->r_es;
138 linux_r->xfs = bsd_r->r_fs;
139 linux_r->xgs = bsd_r->r_gs;
140 linux_r->orig_eax = bsd_r->r_eax;
141 linux_r->eip = bsd_r->r_eip;
142 linux_r->xcs = bsd_r->r_cs;
143 linux_r->eflags = bsd_r->r_eflags;
144 linux_r->esp = bsd_r->r_esp;
145 linux_r->xss = bsd_r->r_ss;
146 }
147
148 static void
149 map_regs_from_linux(struct reg *bsd_r, struct linux_pt_reg *linux_r)
150 {
151 bsd_r->r_ebx = linux_r->ebx;
152 bsd_r->r_ecx = linux_r->ecx;
153 bsd_r->r_edx = linux_r->edx;
154 bsd_r->r_esi = linux_r->esi;
155 bsd_r->r_edi = linux_r->edi;
156 bsd_r->r_ebp = linux_r->ebp;
157 bsd_r->r_eax = linux_r->eax;
158 bsd_r->r_ds = linux_r->xds;
159 bsd_r->r_es = linux_r->xes;
160 bsd_r->r_fs = linux_r->xfs;
161 bsd_r->r_gs = linux_r->xgs;
162 bsd_r->r_eip = linux_r->eip;
163 bsd_r->r_cs = linux_r->xcs;
164 bsd_r->r_eflags = linux_r->eflags;
165 bsd_r->r_esp = linux_r->esp;
166 bsd_r->r_ss = linux_r->xss;
167 }
168
169 struct linux_pt_fpreg {
170 l_long cwd;
171 l_long swd;
172 l_long twd;
173 l_long fip;
174 l_long fcs;
175 l_long foo;
176 l_long fos;
177 l_long st_space[2*10];
178 };
179
180 static void
181 map_fpregs_to_linux(struct fpreg *bsd_r, struct linux_pt_fpreg *linux_r)
182 {
183 linux_r->cwd = bsd_r->fpr_env[0];
184 linux_r->swd = bsd_r->fpr_env[1];
185 linux_r->twd = bsd_r->fpr_env[2];
186 linux_r->fip = bsd_r->fpr_env[3];
187 linux_r->fcs = bsd_r->fpr_env[4];
188 linux_r->foo = bsd_r->fpr_env[5];
189 linux_r->fos = bsd_r->fpr_env[6];
190 bcopy(bsd_r->fpr_acc, linux_r->st_space, sizeof(linux_r->st_space));
191 }
192
193 static void
194 map_fpregs_from_linux(struct fpreg *bsd_r, struct linux_pt_fpreg *linux_r)
195 {
196 bsd_r->fpr_env[0] = linux_r->cwd;
197 bsd_r->fpr_env[1] = linux_r->swd;
198 bsd_r->fpr_env[2] = linux_r->twd;
199 bsd_r->fpr_env[3] = linux_r->fip;
200 bsd_r->fpr_env[4] = linux_r->fcs;
201 bsd_r->fpr_env[5] = linux_r->foo;
202 bsd_r->fpr_env[6] = linux_r->fos;
203 bcopy(bsd_r->fpr_acc, linux_r->st_space, sizeof(bsd_r->fpr_acc));
204 }
205
206 struct linux_pt_fpxreg {
207 l_ushort cwd;
208 l_ushort swd;
209 l_ushort twd;
210 l_ushort fop;
211 l_long fip;
212 l_long fcs;
213 l_long foo;
214 l_long fos;
215 l_long mxcsr;
216 l_long reserved;
217 l_long st_space[32];
218 l_long xmm_space[32];
219 l_long padding[56];
220 };
221
222 #ifdef CPU_ENABLE_SSE
223 static int
224 linux_proc_read_fpxregs(struct thread *td, struct linux_pt_fpxreg *fpxregs)
225 {
226
227 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
228 if (cpu_fxsr == 0 || (td->td_proc->p_sflag & PS_INMEM) == 0)
229 return (EIO);
230 bcopy(&td->td_pcb->pcb_save.sv_xmm, fpxregs, sizeof(*fpxregs));
231 return (0);
232 }
233
234 static int
235 linux_proc_write_fpxregs(struct thread *td, struct linux_pt_fpxreg *fpxregs)
236 {
237
238 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
239 if (cpu_fxsr == 0 || (td->td_proc->p_sflag & PS_INMEM) == 0)
240 return (EIO);
241 bcopy(fpxregs, &td->td_pcb->pcb_save.sv_xmm, sizeof(*fpxregs));
242 return (0);
243 }
244 #endif
245
246 int
247 linux_ptrace(struct thread *td, struct linux_ptrace_args *uap)
248 {
249 union {
250 struct linux_pt_reg reg;
251 struct linux_pt_fpreg fpreg;
252 struct linux_pt_fpxreg fpxreg;
253 } r;
254 union {
255 struct reg bsd_reg;
256 struct fpreg bsd_fpreg;
257 struct dbreg bsd_dbreg;
258 } u;
259 void *addr;
260 pid_t pid;
261 int error, req;
262
263 error = 0;
264
265 /* by default, just copy data intact */
266 req = uap->req;
267 pid = (pid_t)uap->pid;
268 addr = (void *)uap->addr;
269
270 switch (req) {
271 case PTRACE_TRACEME:
272 case PTRACE_POKETEXT:
273 case PTRACE_POKEDATA:
274 case PTRACE_KILL:
275 error = kern_ptrace(td, req, pid, addr, uap->data);
276 break;
277 case PTRACE_PEEKTEXT:
278 case PTRACE_PEEKDATA: {
279 /* need to preserve return value */
280 int rval = td->td_retval[0];
281 error = kern_ptrace(td, req, pid, addr, 0);
282 if (error == 0)
283 error = copyout(td->td_retval, (void *)uap->data,
284 sizeof(l_int));
285 td->td_retval[0] = rval;
286 break;
287 }
288 case PTRACE_DETACH:
289 error = kern_ptrace(td, PT_DETACH, pid, (void *)1,
290 map_signum(uap->data));
291 break;
292 case PTRACE_SINGLESTEP:
293 case PTRACE_CONT:
294 error = kern_ptrace(td, req, pid, (void *)1,
295 map_signum(uap->data));
296 break;
297 case PTRACE_ATTACH:
298 error = kern_ptrace(td, PT_ATTACH, pid, addr, uap->data);
299 break;
300 case PTRACE_GETREGS:
301 /* Linux is using data where FreeBSD is using addr */
302 error = kern_ptrace(td, PT_GETREGS, pid, &u.bsd_reg, 0);
303 if (error == 0) {
304 map_regs_to_linux(&u.bsd_reg, &r.reg);
305 error = copyout(&r.reg, (void *)uap->data,
306 sizeof(r.reg));
307 }
308 break;
309 case PTRACE_SETREGS:
310 /* Linux is using data where FreeBSD is using addr */
311 error = copyin((void *)uap->data, &r.reg, sizeof(r.reg));
312 if (error == 0) {
313 map_regs_from_linux(&u.bsd_reg, &r.reg);
314 error = kern_ptrace(td, PT_SETREGS, pid, &u.bsd_reg, 0);
315 }
316 break;
317 case PTRACE_GETFPREGS:
318 /* Linux is using data where FreeBSD is using addr */
319 error = kern_ptrace(td, PT_GETFPREGS, pid, &u.bsd_fpreg, 0);
320 if (error == 0) {
321 map_fpregs_to_linux(&u.bsd_fpreg, &r.fpreg);
322 error = copyout(&r.fpreg, (void *)uap->data,
323 sizeof(r.fpreg));
324 }
325 break;
326 case PTRACE_SETFPREGS:
327 /* Linux is using data where FreeBSD is using addr */
328 error = copyin((void *)uap->data, &r.fpreg, sizeof(r.fpreg));
329 if (error == 0) {
330 map_fpregs_from_linux(&u.bsd_fpreg, &r.fpreg);
331 error = kern_ptrace(td, PT_SETFPREGS, pid,
332 &u.bsd_fpreg, 0);
333 }
334 break;
335 case PTRACE_SETFPXREGS:
336 #ifdef CPU_ENABLE_SSE
337 error = copyin((void *)uap->data, &r.fpxreg, sizeof(r.fpxreg));
338 if (error)
339 break;
340 #endif
341 /* FALL THROUGH */
342 case PTRACE_GETFPXREGS: {
343 #ifdef CPU_ENABLE_SSE
344 struct proc *p;
345 struct thread *td2;
346
347 if (sizeof(struct linux_pt_fpxreg) != sizeof(struct savexmm)) {
348 static int once = 0;
349 if (!once) {
350 printf("linux: savexmm != linux_pt_fpxreg\n");
351 once = 1;
352 }
353 error = EIO;
354 break;
355 }
356
357 if ((p = pfind(uap->pid)) == NULL) {
358 error = ESRCH;
359 break;
360 }
361
362 if ((error = p_candebug(td, p)) != 0)
363 goto fail;
364
365 /* System processes can't be debugged. */
366 if ((p->p_flag & P_SYSTEM) != 0) {
367 error = EINVAL;
368 goto fail;
369 }
370
371 /* not being traced... */
372 if ((p->p_flag & P_TRACED) == 0) {
373 error = EPERM;
374 goto fail;
375 }
376
377 /* not being traced by YOU */
378 if (p->p_pptr != td->td_proc) {
379 error = EBUSY;
380 goto fail;
381 }
382
383 /* not currently stopped */
384 if (!P_SHOULDSTOP(p) || (p->p_flag & P_WAITED) == 0) {
385 error = EBUSY;
386 goto fail;
387 }
388
389 if (req == PTRACE_GETFPXREGS) {
390 _PHOLD(p); /* may block */
391 td2 = FIRST_THREAD_IN_PROC(p);
392 error = linux_proc_read_fpxregs(td2, &r.fpxreg);
393 _PRELE(p);
394 PROC_UNLOCK(p);
395 if (error == 0)
396 error = copyout(&r.fpxreg, (void *)uap->data,
397 sizeof(r.fpxreg));
398 } else {
399 /* clear dangerous bits exactly as Linux does*/
400 r.fpxreg.mxcsr &= 0xffbf;
401 _PHOLD(p); /* may block */
402 td2 = FIRST_THREAD_IN_PROC(p);
403 error = linux_proc_write_fpxregs(td2, &r.fpxreg);
404 _PRELE(p);
405 PROC_UNLOCK(p);
406 }
407 break;
408
409 fail:
410 PROC_UNLOCK(p);
411 #else
412 error = EIO;
413 #endif
414 break;
415 }
416 case PTRACE_PEEKUSR:
417 case PTRACE_POKEUSR: {
418 error = EIO;
419
420 /* check addr for alignment */
421 if (uap->addr < 0 || uap->addr & (sizeof(l_int) - 1))
422 break;
423 /*
424 * Allow linux programs to access register values in
425 * user struct. We simulate this through PT_GET/SETREGS
426 * as necessary.
427 */
428 if (uap->addr < sizeof(struct linux_pt_reg)) {
429 error = kern_ptrace(td, PT_GETREGS, pid, &u.bsd_reg, 0);
430 if (error != 0)
431 break;
432
433 map_regs_to_linux(&u.bsd_reg, &r.reg);
434 if (req == PTRACE_PEEKUSR) {
435 error = copyout((char *)&r.reg + uap->addr,
436 (void *)uap->data, sizeof(l_int));
437 break;
438 }
439
440 *(l_int *)((char *)&r.reg + uap->addr) =
441 (l_int)uap->data;
442
443 map_regs_from_linux(&u.bsd_reg, &r.reg);
444 error = kern_ptrace(td, PT_SETREGS, pid, &u.bsd_reg, 0);
445 }
446
447 /*
448 * Simulate debug registers access
449 */
450 if (uap->addr >= LINUX_DBREG_OFFSET &&
451 uap->addr <= LINUX_DBREG_OFFSET + LINUX_DBREG_SIZE) {
452 error = kern_ptrace(td, PT_GETDBREGS, pid, &u.bsd_dbreg,
453 0);
454 if (error != 0)
455 break;
456
457 uap->addr -= LINUX_DBREG_OFFSET;
458 if (req == PTRACE_PEEKUSR) {
459 error = copyout((char *)&u.bsd_dbreg +
460 uap->addr, (void *)uap->data,
461 sizeof(l_int));
462 break;
463 }
464
465 *(l_int *)((char *)&u.bsd_dbreg + uap->addr) =
466 uap->data;
467 error = kern_ptrace(td, PT_SETDBREGS, pid,
468 &u.bsd_dbreg, 0);
469 }
470
471 break;
472 }
473 case PTRACE_SYSCALL:
474 /* fall through */
475 default:
476 printf("linux: ptrace(%u, ...) not implemented\n",
477 (unsigned int)uap->req);
478 error = EINVAL;
479 break;
480 }
481
482 return (error);
483 }
Cache object: b3fd62ef29f737cb7ff821e3e448d770
|