643e80ccdc192baf99c377ae47402453124bc58a
[dragonfly.git] / sys / emulation / linux / i386 / linux_ptrace.c
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  * $FreeBSD: src/sys/i386/linux/linux_ptrace.c,v 1.7.4.3 2003/01/03 17:13:23 kan Exp $
29  * $DragonFly: src/sys/emulation/linux/i386/linux_ptrace.c,v 1.4 2003/06/23 17:55:39 dillon Exp $
30  */
31
32 #include "opt_cpu.h"
33
34 #include <sys/param.h>
35 #include <sys/lock.h>
36 #include <sys/proc.h>
37 #include <sys/ptrace.h>
38 #include <sys/systm.h>
39
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 #include <vm/vm_map.h>
43 #include <sys/user.h>
44
45 #include <machine/md_var.h>
46 #include <machine/pcb.h>
47 #include <machine/reg.h>
48
49 #include <i386/linux/linux.h>
50 #include <i386/linux/linux_proto.h>
51
52 /*
53  *   Linux ptrace requests numbers. Mostly identical to FreeBSD,
54  *   except for MD ones and PT_ATTACH/PT_DETACH.
55  */
56 #define PTRACE_TRACEME          0
57 #define PTRACE_PEEKTEXT         1
58 #define PTRACE_PEEKDATA         2
59 #define PTRACE_PEEKUSR          3
60 #define PTRACE_POKETEXT         4
61 #define PTRACE_POKEDATA         5
62 #define PTRACE_POKEUSR          6
63 #define PTRACE_CONT             7
64 #define PTRACE_KILL             8
65 #define PTRACE_SINGLESTEP       9
66
67 #define PTRACE_ATTACH           16
68 #define PTRACE_DETACH           17
69
70 #define PTRACE_SYSCALL          24
71
72 #define PTRACE_GETREGS          12
73 #define PTRACE_SETREGS          13
74 #define PTRACE_GETFPREGS        14
75 #define PTRACE_SETFPREGS        15
76 #define PTRACE_GETFPXREGS       18
77 #define PTRACE_SETFPXREGS       19
78
79 #define PTRACE_SETOPTIONS       21
80
81 /*
82  * Linux keeps debug registers at the following
83  * offset in the user struct
84  */
85 #define LINUX_DBREG_OFFSET      252
86 #define LINUX_DBREG_SIZE        (8*sizeof(l_int))
87
88 static __inline__ int
89 map_signum(int signum)
90 {
91
92         if (signum > 0 && signum <= LINUX_SIGTBLSZ)
93                 signum = linux_to_bsd_signal[_SIG_IDX(signum)];
94         return ((signum == SIGSTOP)? 0 : signum);
95 }
96
97 struct linux_pt_reg {
98         l_long  ebx;
99         l_long  ecx;
100         l_long  edx;
101         l_long  esi;
102         l_long  edi;
103         l_long  ebp;
104         l_long  eax;
105         l_int   xds;
106         l_int   xes;
107         l_int   xfs;
108         l_int   xgs;
109         l_long  orig_eax;
110         l_long  eip;
111         l_int   xcs;
112         l_long  eflags;
113         l_long  esp;
114         l_int   xss;
115 };
116
117 /*
118  *   Translate i386 ptrace registers between Linux and FreeBSD formats.
119  *   The translation is pretty straighforward, for all registers, but
120  *   orig_eax on Linux side and r_trapno and r_err in FreeBSD
121  */
122 static void
123 map_regs_to_linux(struct reg *bsd_r, struct linux_pt_reg *linux_r)
124 {
125         linux_r->ebx = bsd_r->r_ebx;
126         linux_r->ecx = bsd_r->r_ecx;
127         linux_r->edx = bsd_r->r_edx;
128         linux_r->esi = bsd_r->r_esi;
129         linux_r->edi = bsd_r->r_edi;
130         linux_r->ebp = bsd_r->r_ebp;
131         linux_r->eax = bsd_r->r_eax;
132         linux_r->xds = bsd_r->r_ds;
133         linux_r->xes = bsd_r->r_es;
134         linux_r->xfs = bsd_r->r_fs;
135         linux_r->xgs = bsd_r->r_gs;
136         linux_r->orig_eax = bsd_r->r_eax;
137         linux_r->eip = bsd_r->r_eip;
138         linux_r->xcs = bsd_r->r_cs;
139         linux_r->eflags = bsd_r->r_eflags;
140         linux_r->esp = bsd_r->r_esp;
141         linux_r->xss = bsd_r->r_ss;
142 }
143
144 static void
145 map_regs_from_linux(struct reg *bsd_r, struct linux_pt_reg *linux_r)
146 {
147         bsd_r->r_ebx = linux_r->ebx;
148         bsd_r->r_ecx = linux_r->ecx;
149         bsd_r->r_edx = linux_r->edx;
150         bsd_r->r_esi = linux_r->esi;
151         bsd_r->r_edi = linux_r->edi;
152         bsd_r->r_ebp = linux_r->ebp;
153         bsd_r->r_eax = linux_r->eax;
154         bsd_r->r_ds  = linux_r->xds;
155         bsd_r->r_es  = linux_r->xes;
156         bsd_r->r_fs  = linux_r->xfs;
157         bsd_r->r_gs  = linux_r->xgs;
158         bsd_r->r_eip = linux_r->eip;
159         bsd_r->r_cs  = linux_r->xcs;
160         bsd_r->r_eflags = linux_r->eflags;
161         bsd_r->r_esp = linux_r->esp;
162         bsd_r->r_ss = linux_r->xss;
163 }
164
165 struct linux_pt_fpreg {
166         l_long cwd;
167         l_long swd;
168         l_long twd;
169         l_long fip;
170         l_long fcs;
171         l_long foo;
172         l_long fos;
173         l_long st_space[2*10];
174 };
175
176 static void
177 map_fpregs_to_linux(struct fpreg *bsd_r, struct linux_pt_fpreg *linux_r)
178 {
179         linux_r->cwd = bsd_r->fpr_env[0];
180         linux_r->swd = bsd_r->fpr_env[1];
181         linux_r->twd = bsd_r->fpr_env[2];
182         linux_r->fip = bsd_r->fpr_env[3];
183         linux_r->fcs = bsd_r->fpr_env[4];
184         linux_r->foo = bsd_r->fpr_env[5];
185         linux_r->fos = bsd_r->fpr_env[6];
186         bcopy(bsd_r->fpr_acc, linux_r->st_space, sizeof(linux_r->st_space));
187 }
188
189 static void
190 map_fpregs_from_linux(struct fpreg *bsd_r, struct linux_pt_fpreg *linux_r)
191 {
192         bsd_r->fpr_env[0] = linux_r->cwd;
193         bsd_r->fpr_env[1] = linux_r->swd;
194         bsd_r->fpr_env[2] = linux_r->twd;
195         bsd_r->fpr_env[3] = linux_r->fip;
196         bsd_r->fpr_env[4] = linux_r->fcs;
197         bsd_r->fpr_env[5] = linux_r->foo;
198         bsd_r->fpr_env[6] = linux_r->fos;
199         bcopy(bsd_r->fpr_acc, linux_r->st_space, sizeof(bsd_r->fpr_acc));
200 }
201
202 struct linux_pt_fpxreg {
203         l_ushort        cwd;
204         l_ushort        swd;
205         l_ushort        twd;
206         l_ushort        fop;
207         l_long          fip;
208         l_long          fcs;
209         l_long          foo;
210         l_long          fos;
211         l_long          mxcsr;
212         l_long          reserved;
213         l_long          st_space[32];
214         l_long          xmm_space[32];
215         l_long          padding[56];
216 };
217
218 #ifdef CPU_ENABLE_SSE
219 static int
220 linux_proc_read_fpxregs(struct proc *p, struct linux_pt_fpxreg *fpxregs)
221 {
222         int error;
223
224         error = 0;
225         if (cpu_fxsr == 0 || (p->p_flag & P_INMEM) == 0)
226                 error = EIO;
227         else
228                 bcopy(&p->p_thread->td_pcb->pcb_save.sv_xmm,
229                     fpxregs, sizeof(*fpxregs));
230         return (error);
231 }
232
233 static int
234 linux_proc_write_fpxregs(struct proc *p, struct linux_pt_fpxreg *fpxregs)
235 {
236         int error;
237
238         error = 0;
239         if (cpu_fxsr == 0 || (p->p_flag & P_INMEM) == 0)
240                 error = EIO;
241         else
242                 bcopy(fpxregs, &p->p_thread->td_pcb->pcb_save.sv_xmm,
243                     sizeof(*fpxregs));
244         return (error);
245 }
246 #endif
247
248 int
249 linux_ptrace(struct linux_ptrace_args *uap)
250 {
251         struct proc *curp = curproc;
252         union {
253                 struct linux_pt_reg     reg;
254                 struct linux_pt_fpreg   fpreg;
255                 struct linux_pt_fpxreg  fpxreg;
256         } r;
257         union {
258                 struct reg              bsd_reg;
259                 struct fpreg            bsd_fpreg;
260                 struct dbreg            bsd_dbreg;
261         } u;
262         void *addr;
263         pid_t pid;
264         int error, req;
265
266         error = 0;
267
268         /* by default, just copy data intact */
269         req  = uap->req;
270         pid  = (pid_t)uap->pid;
271         addr = (void *)uap->addr;
272
273         switch (req) {
274         case PTRACE_TRACEME:
275         case PTRACE_POKETEXT:
276         case PTRACE_POKEDATA:
277         case PTRACE_KILL:
278                 error = kern_ptrace(curp, req, pid, addr, uap->data);
279                 break;
280         case PTRACE_PEEKTEXT:
281         case PTRACE_PEEKDATA: {
282                 /* need to preserve return value */
283                 int rval = curp->p_retval[0];
284                 error = kern_ptrace(curp, req, pid, addr, 0);
285                 if (error == 0)
286                         error = copyout(curp->p_retval, (caddr_t)uap->data,
287                             sizeof(l_int));
288                 curp->p_retval[0] = rval;
289                 break;
290         }
291         case PTRACE_DETACH:
292                 error = kern_ptrace(curp, PT_DETACH, pid, (void *)1,
293                      map_signum(uap->data));
294                 break;
295         case PTRACE_SINGLESTEP:
296         case PTRACE_CONT:
297                 error = kern_ptrace(curp, req, pid, (void *)1,
298                      map_signum(uap->data));
299                 break;
300         case PTRACE_ATTACH:
301                 error = kern_ptrace(curp, PT_ATTACH, pid, addr, uap->data);
302                 break;
303         case PTRACE_GETREGS:
304                 /* Linux is using data where FreeBSD is using addr */
305                 error = kern_ptrace(curp, PT_GETREGS, pid, &u.bsd_reg, 0);
306                 if (error == 0) {
307                         map_regs_to_linux(&u.bsd_reg, &r.reg);
308                         error = copyout(&r.reg, (caddr_t)uap->data,
309                             sizeof(r.reg));
310                 }
311                 break;
312         case PTRACE_SETREGS:
313                 /* Linux is using data where FreeBSD is using addr */
314                 error = copyin((caddr_t)uap->data, &r.reg, sizeof(r.reg));
315                 if (error == 0) {
316                         map_regs_from_linux(&u.bsd_reg, &r.reg);
317                         error = kern_ptrace(curp, PT_SETREGS, pid, &u.bsd_reg, 0);
318                 }
319                 break;
320         case PTRACE_GETFPREGS:
321                 /* Linux is using data where FreeBSD is using addr */
322                 error = kern_ptrace(curp, PT_GETFPREGS, pid, &u.bsd_fpreg, 0);
323                 if (error == 0) {
324                         map_fpregs_to_linux(&u.bsd_fpreg, &r.fpreg);
325                         error = copyout(&r.fpreg, (caddr_t)uap->data,
326                             sizeof(r.fpreg));
327                 }
328                 break;
329         case PTRACE_SETFPREGS:
330                 /* Linux is using data where FreeBSD is using addr */
331                 error = copyin((caddr_t)uap->data, &r.fpreg, sizeof(r.fpreg));
332                 if (error == 0) {
333                         map_fpregs_from_linux(&u.bsd_fpreg, &r.fpreg);
334                         error = kern_ptrace(curp, PT_SETFPREGS, pid,
335                             &u.bsd_fpreg, 0);
336                 }
337                 break;
338         case PTRACE_SETFPXREGS:
339 #ifdef CPU_ENABLE_SSE
340                 error = copyin((caddr_t)uap->data, &r.fpxreg,
341                     sizeof(r.fpxreg));
342                 if (error)
343                         break;
344 #endif
345                 /* FALL THROUGH */
346         case PTRACE_GETFPXREGS: {
347 #ifdef CPU_ENABLE_SSE
348                 struct proc *p;
349
350                 if (sizeof(struct linux_pt_fpxreg) != sizeof(struct savexmm)) {
351                         static int once = 0;
352                         if (!once) {
353                                 printf("linux: savexmm != linux_pt_fpxreg\n");
354                                 once = 1;
355                         }
356                         error = EIO;
357                         break;
358                 }
359
360                 if ((p = pfind(uap->pid)) == NULL) {
361                         error = ESRCH;
362                         break;
363                 }
364
365                 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
366                         error = ESRCH;
367                         goto fail;
368                 }
369
370                 /* System processes can't be debugged. */
371                 if ((p->p_flag & P_SYSTEM) != 0) {
372                         error = EINVAL;
373                         goto fail;
374                 }
375
376                 /* not being traced... */
377                 if ((p->p_flag & P_TRACED) == 0) {
378                         error = EPERM;
379                         goto fail;
380                 }
381
382                 /* not being traced by YOU */
383                 if (p->p_pptr != curp) {
384                         error = EBUSY;
385                         goto fail;
386                 }
387
388                 /* not currently stopped */
389                 if ((p->p_flag & (P_TRACED|P_WAITED)) == 0) {
390                         error = EBUSY;
391                         goto fail;
392                 }
393
394                 if (req == PTRACE_GETFPXREGS) {
395                         PHOLD(p);
396                         error = linux_proc_read_fpxregs(p, &r.fpxreg);
397                         PRELE(p);
398                         if (error == 0)
399                                 error = copyout(&r.fpxreg, (caddr_t)uap->data,
400                                     sizeof(r.fpxreg));
401                 } else {
402                         /* clear dangerous bits exactly as Linux does*/
403                         r.fpxreg.mxcsr &= 0xffbf;
404                         PHOLD(p);
405                         error = linux_proc_write_fpxregs(p, &r.fpxreg);
406                         PRELE(p);
407                 }
408                 break;
409
410         fail:
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(curp, 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                                     (caddr_t)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(curp, 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(curp, 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, (caddr_t)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(curp, 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 }