Merge branch 'vendor/FILE'
[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.15 2007/02/19 01:14:23 corecode 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 #include <sys/reg.h>
45
46 #include <sys/mplock2.h>
47
48 #include <machine/md_var.h>
49 #include <machine/pcb.h>
50
51 #include "linux.h"
52 #include "linux_proto.h"
53
54 /*
55  *   Linux ptrace requests numbers. Mostly identical to FreeBSD,
56  *   except for MD ones and PT_ATTACH/PT_DETACH.
57  */
58 #define PTRACE_TRACEME          0
59 #define PTRACE_PEEKTEXT         1
60 #define PTRACE_PEEKDATA         2
61 #define PTRACE_PEEKUSR          3
62 #define PTRACE_POKETEXT         4
63 #define PTRACE_POKEDATA         5
64 #define PTRACE_POKEUSR          6
65 #define PTRACE_CONT             7
66 #define PTRACE_KILL             8
67 #define PTRACE_SINGLESTEP       9
68
69 #define PTRACE_ATTACH           16
70 #define PTRACE_DETACH           17
71
72 #define PTRACE_SYSCALL          24
73
74 #define PTRACE_GETREGS          12
75 #define PTRACE_SETREGS          13
76 #define PTRACE_GETFPREGS        14
77 #define PTRACE_SETFPREGS        15
78 #define PTRACE_GETFPXREGS       18
79 #define PTRACE_SETFPXREGS       19
80
81 #define PTRACE_SETOPTIONS       21
82
83 /*
84  * Linux keeps debug registers at the following
85  * offset in the user struct
86  */
87 #define LINUX_DBREG_OFFSET      252
88 #define LINUX_DBREG_SIZE        (8*sizeof(l_int))
89
90 static __inline__ int
91 map_signum(int signum)
92 {
93
94         if (signum > 0 && signum <= LINUX_SIGTBLSZ)
95                 signum = linux_to_bsd_signal[_SIG_IDX(signum)];
96         return ((signum == SIGSTOP)? 0 : signum);
97 }
98
99 struct linux_pt_reg {
100         l_long  ebx;
101         l_long  ecx;
102         l_long  edx;
103         l_long  esi;
104         l_long  edi;
105         l_long  ebp;
106         l_long  eax;
107         l_int   xds;
108         l_int   xes;
109         l_int   xfs;
110         l_int   xgs;
111         l_long  orig_eax;
112         l_long  eip;
113         l_int   xcs;
114         l_long  eflags;
115         l_long  esp;
116         l_int   xss;
117 };
118
119 /*
120  *   Translate i386 ptrace registers between Linux and FreeBSD formats.
121  *   The translation is pretty straighforward, for all registers, but
122  *   orig_eax on Linux side and r_trapno and r_err in FreeBSD
123  */
124 static void
125 map_regs_to_linux(struct reg *bsd_r, struct linux_pt_reg *linux_r)
126 {
127         linux_r->ebx = bsd_r->r_ebx;
128         linux_r->ecx = bsd_r->r_ecx;
129         linux_r->edx = bsd_r->r_edx;
130         linux_r->esi = bsd_r->r_esi;
131         linux_r->edi = bsd_r->r_edi;
132         linux_r->ebp = bsd_r->r_ebp;
133         linux_r->eax = bsd_r->r_eax;
134         linux_r->xds = bsd_r->r_ds;
135         linux_r->xes = bsd_r->r_es;
136         linux_r->xfs = bsd_r->r_fs;
137         linux_r->xgs = bsd_r->r_gs;
138         linux_r->orig_eax = bsd_r->r_eax;
139         linux_r->eip = bsd_r->r_eip;
140         linux_r->xcs = bsd_r->r_cs;
141         linux_r->eflags = bsd_r->r_eflags;
142         linux_r->esp = bsd_r->r_esp;
143         linux_r->xss = bsd_r->r_ss;
144 }
145
146 static void
147 map_regs_from_linux(struct reg *bsd_r, struct linux_pt_reg *linux_r)
148 {
149         bsd_r->r_ebx = linux_r->ebx;
150         bsd_r->r_ecx = linux_r->ecx;
151         bsd_r->r_edx = linux_r->edx;
152         bsd_r->r_esi = linux_r->esi;
153         bsd_r->r_edi = linux_r->edi;
154         bsd_r->r_ebp = linux_r->ebp;
155         bsd_r->r_eax = linux_r->eax;
156         bsd_r->r_ds  = linux_r->xds;
157         bsd_r->r_es  = linux_r->xes;
158         bsd_r->r_fs  = linux_r->xfs;
159         bsd_r->r_gs  = linux_r->xgs;
160         bsd_r->r_eip = linux_r->eip;
161         bsd_r->r_cs  = linux_r->xcs;
162         bsd_r->r_eflags = linux_r->eflags;
163         bsd_r->r_esp = linux_r->esp;
164         bsd_r->r_ss = linux_r->xss;
165 }
166
167 struct linux_pt_fpreg {
168         l_long cwd;
169         l_long swd;
170         l_long twd;
171         l_long fip;
172         l_long fcs;
173         l_long foo;
174         l_long fos;
175         l_long st_space[2*10];
176 };
177
178 static void
179 map_fpregs_to_linux(struct fpreg *bsd_r, struct linux_pt_fpreg *linux_r)
180 {
181         linux_r->cwd = bsd_r->fpr_env[0];
182         linux_r->swd = bsd_r->fpr_env[1];
183         linux_r->twd = bsd_r->fpr_env[2];
184         linux_r->fip = bsd_r->fpr_env[3];
185         linux_r->fcs = bsd_r->fpr_env[4];
186         linux_r->foo = bsd_r->fpr_env[5];
187         linux_r->fos = bsd_r->fpr_env[6];
188         bcopy(bsd_r->fpr_acc, linux_r->st_space, sizeof(linux_r->st_space));
189 }
190
191 static void
192 map_fpregs_from_linux(struct fpreg *bsd_r, struct linux_pt_fpreg *linux_r)
193 {
194         bsd_r->fpr_env[0] = linux_r->cwd;
195         bsd_r->fpr_env[1] = linux_r->swd;
196         bsd_r->fpr_env[2] = linux_r->twd;
197         bsd_r->fpr_env[3] = linux_r->fip;
198         bsd_r->fpr_env[4] = linux_r->fcs;
199         bsd_r->fpr_env[5] = linux_r->foo;
200         bsd_r->fpr_env[6] = linux_r->fos;
201         bcopy(bsd_r->fpr_acc, linux_r->st_space, sizeof(bsd_r->fpr_acc));
202 }
203
204 struct linux_pt_fpxreg {
205         l_ushort        cwd;
206         l_ushort        swd;
207         l_ushort        twd;
208         l_ushort        fop;
209         l_long          fip;
210         l_long          fcs;
211         l_long          foo;
212         l_long          fos;
213         l_long          mxcsr;
214         l_long          reserved;
215         l_long          st_space[32];
216         l_long          xmm_space[32];
217         l_long          padding[56];
218 };
219
220 #ifndef CPU_DISABLE_SSE
221 static int
222 linux_proc_read_fpxregs(struct lwp *lp, struct linux_pt_fpxreg *fpxregs)
223 {
224         int error;
225
226         error = 0;
227         if (cpu_fxsr == 0)
228                 error = EIO;
229         else
230                 bcopy(&lp->lwp_thread->td_pcb->pcb_save.sv_xmm,
231                     fpxregs, sizeof(*fpxregs));
232         return (error);
233 }
234
235 static int
236 linux_proc_write_fpxregs(struct lwp *lp, struct linux_pt_fpxreg *fpxregs)
237 {
238         int error;
239
240         error = 0;
241         if (cpu_fxsr == 0)
242                 error = EIO;
243         else
244                 bcopy(fpxregs, &lp->lwp_thread->td_pcb->pcb_save.sv_xmm,
245                     sizeof(*fpxregs));
246         return (error);
247 }
248 #endif
249
250 /*
251  * MPALMOSTSAFE
252  */
253 int
254 sys_linux_ptrace(struct linux_ptrace_args *uap)
255 {
256         struct thread *td = curthread;
257         struct proc *curp = td->td_proc;
258         union {
259                 struct linux_pt_reg     reg;
260                 struct linux_pt_fpreg   fpreg;
261                 struct linux_pt_fpxreg  fpxreg;
262         } r;
263         union {
264                 struct reg              bsd_reg;
265                 struct fpreg            bsd_fpreg;
266                 struct dbreg            bsd_dbreg;
267         } u;
268         void *addr;
269         pid_t pid;
270         int error, req;
271
272         error = 0;
273
274         /* by default, just copy data intact */
275         req  = uap->req;
276         pid  = (pid_t)uap->pid;
277         addr = (void *)uap->addr;
278
279         get_mplock();
280
281         switch (req) {
282         case PTRACE_TRACEME:
283         case PTRACE_POKETEXT:
284         case PTRACE_POKEDATA:
285         case PTRACE_KILL:
286                 error = kern_ptrace(curp, req, pid, addr, uap->data,
287                                     &uap->sysmsg_iresult);
288                 break;
289         case PTRACE_PEEKTEXT:
290         case PTRACE_PEEKDATA: {
291                 /* need to preserve return value, use dummy */
292                 l_int rval = 0;
293                 error = kern_ptrace(curp, req, pid, addr, 0, &rval);
294                 if (error == 0) {
295                         error = copyout(&rval, (caddr_t)uap->data, sizeof(l_int));
296                 }
297                 break;
298         }
299         case PTRACE_DETACH:
300                 error = kern_ptrace(curp, PT_DETACH, pid, (void *)1,
301                                     map_signum(uap->data),
302                                     &uap->sysmsg_iresult);
303                 break;
304         case PTRACE_SINGLESTEP:
305         case PTRACE_CONT:
306                 error = kern_ptrace(curp, req, pid, (void *)1,
307                                     map_signum(uap->data),
308                                     &uap->sysmsg_iresult);
309                 break;
310         case PTRACE_ATTACH:
311                 error = kern_ptrace(curp, PT_ATTACH, pid, addr, uap->data,
312                                     &uap->sysmsg_iresult);
313                 break;
314         case PTRACE_GETREGS:
315                 /* Linux is using data where FreeBSD is using addr */
316                 error = kern_ptrace(curp, PT_GETREGS, pid, &u.bsd_reg, 0,
317                                     &uap->sysmsg_iresult);
318                 if (error == 0) {
319                         map_regs_to_linux(&u.bsd_reg, &r.reg);
320                         error = copyout(&r.reg, (void *)uap->data,
321                                         sizeof(r.reg));
322                 }
323                 break;
324         case PTRACE_SETREGS:
325                 /* Linux is using data where FreeBSD is using addr */
326                 error = copyin((caddr_t)uap->data, &r.reg, sizeof(r.reg));
327                 if (error == 0) {
328                         map_regs_from_linux(&u.bsd_reg, &r.reg);
329                         error = kern_ptrace(curp, PT_SETREGS, pid, &u.bsd_reg,
330                                             0, &uap->sysmsg_iresult);
331                 }
332                 break;
333         case PTRACE_GETFPREGS:
334                 /* Linux is using data where FreeBSD is using addr */
335                 error = kern_ptrace(curp, PT_GETFPREGS, pid, &u.bsd_fpreg,
336                                     0, &uap->sysmsg_iresult);
337                 if (error == 0) {
338                         map_fpregs_to_linux(&u.bsd_fpreg, &r.fpreg);
339                         error = copyout(&r.fpreg, (caddr_t)uap->data,
340                             sizeof(r.fpreg));
341                 }
342                 break;
343         case PTRACE_SETFPREGS:
344                 /* Linux is using data where FreeBSD is using addr */
345                 error = copyin((caddr_t)uap->data, &r.fpreg, sizeof(r.fpreg));
346                 if (error == 0) {
347                         map_fpregs_from_linux(&u.bsd_fpreg, &r.fpreg);
348                         error = kern_ptrace(curp, PT_SETFPREGS, pid,
349                                             &u.bsd_fpreg,
350                                             0, &uap->sysmsg_iresult);
351                 }
352                 break;
353         case PTRACE_SETFPXREGS:
354 #ifndef CPU_DISABLE_SSE
355                 error = copyin((caddr_t)uap->data, &r.fpxreg,
356                     sizeof(r.fpxreg));
357                 if (error)
358                         break;
359 #endif
360                 /* FALL THROUGH */
361         case PTRACE_GETFPXREGS: {
362 #ifndef CPU_DISABLE_SSE
363                 struct proc *p;
364                 struct lwp *lp;
365
366                 if (sizeof(struct linux_pt_fpxreg) != sizeof(struct savexmm)) {
367                         static int once = 0;
368                         if (!once) {
369                                 kprintf("linux: savexmm != linux_pt_fpxreg\n");
370                                 once = 1;
371                         }
372                         error = EIO;
373                         break;
374                 }
375
376                 if ((p = pfind(uap->pid)) == NULL) {
377                         error = ESRCH;
378                         break;
379                 }
380
381                 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
382                         error = ESRCH;
383                         goto fail;
384                 }
385
386                 /* System processes can't be debugged. */
387                 if ((p->p_flag & P_SYSTEM) != 0) {
388                         error = EINVAL;
389                         goto fail;
390                 }
391
392                 /* not being traced... */
393                 if ((p->p_flag & P_TRACED) == 0) {
394                         error = EPERM;
395                         goto fail;
396                 }
397
398                 /* not being traced by YOU */
399                 if (p->p_pptr != curp) {
400                         error = EBUSY;
401                         goto fail;
402                 }
403
404                 /* not currently stopped */
405                 if ((p->p_flag & (P_TRACED|P_WAITED)) == 0) {
406                         error = EBUSY;
407                         goto fail;
408                 }
409
410                 /* XXX lwp */
411                 lp = FIRST_LWP_IN_PROC(p);
412
413                 if (req == PTRACE_GETFPXREGS) {
414                         LWPHOLD(lp);
415                         error = linux_proc_read_fpxregs(lp, &r.fpxreg);
416                         LWPRELE(lp);
417                         if (error == 0)
418                                 error = copyout(&r.fpxreg, (caddr_t)uap->data,
419                                     sizeof(r.fpxreg));
420                 } else {
421                         /* clear dangerous bits exactly as Linux does*/
422                         r.fpxreg.mxcsr &= 0xffbf;
423                         LWPHOLD(lp);
424                         error = linux_proc_write_fpxregs(lp, &r.fpxreg);
425                         LWPRELE(lp);
426                 }
427                 break;
428
429         fail:
430 #else
431                 error = EIO;
432 #endif
433                 break;
434         }
435         case PTRACE_PEEKUSR:
436         case PTRACE_POKEUSR: {
437                 error = EIO;
438
439                 /* check addr for alignment */
440                 if (uap->addr < 0 || uap->addr & (sizeof(l_int) - 1))
441                         break;
442                 /*
443                  * Allow linux programs to access register values in
444                  * user struct. We simulate this through PT_GET/SETREGS
445                  * as necessary.
446                  */
447                 if (uap->addr < sizeof(struct linux_pt_reg)) {
448                         error = kern_ptrace(curp, PT_GETREGS, pid, &u.bsd_reg,
449                                             0, &uap->sysmsg_iresult);
450                         if (error != 0)
451                                 break;
452
453                         map_regs_to_linux(&u.bsd_reg, &r.reg);
454                         if (req == PTRACE_PEEKUSR) {
455                                 error = copyout((char *)&r.reg + uap->addr,
456                                     (caddr_t)uap->data, sizeof(l_int));
457                                 break;
458                         }
459
460                         *(l_int *)((char *)&r.reg + uap->addr) =
461                             (l_int)uap->data;
462
463                         map_regs_from_linux(&u.bsd_reg, &r.reg);
464                         error = kern_ptrace(curp, PT_SETREGS, pid, &u.bsd_reg,
465                                             0, &uap->sysmsg_iresult);
466                 }
467
468                 /*
469                  * Simulate debug registers access
470                  */
471                 if (uap->addr >= LINUX_DBREG_OFFSET &&
472                     uap->addr <= LINUX_DBREG_OFFSET + LINUX_DBREG_SIZE) {
473                         error = kern_ptrace(curp, PT_GETDBREGS, pid, 
474                                             &u.bsd_dbreg,
475                                             0, &uap->sysmsg_iresult);
476                         if (error != 0)
477                                 break;
478
479                         uap->addr -= LINUX_DBREG_OFFSET;
480                         if (req == PTRACE_PEEKUSR) {
481                                 error = copyout((char *)&u.bsd_dbreg +
482                                     uap->addr, (caddr_t)uap->data,
483                                     sizeof(l_int));
484                                 break;
485                         }
486
487                         *(l_int *)((char *)&u.bsd_dbreg + uap->addr) =
488                              uap->data;
489                         error = kern_ptrace(curp, PT_SETDBREGS, pid,
490                                             &u.bsd_dbreg,
491                                             0, &uap->sysmsg_iresult);
492                 }
493
494                 break;
495         }
496         case PTRACE_SYSCALL:
497                 /* fall through */
498         default:
499                 kprintf("linux: ptrace(%u, ...) not implemented\n",
500                     (unsigned int)uap->req);
501                 error = EINVAL;
502                 break;
503         }
504
505         rel_mplock();
506         return (error);
507 }