The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/linux/linux_ptrace.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: releng/10.2/sys/i386/linux/linux_ptrace.c 276084 2014-12-22 21:32:39Z jhb $");
   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 #include <compat/linux/linux_signal.h>
   49 
   50 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
   51 #define CPU_ENABLE_SSE
   52 #endif
   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 #ifdef CPU_ENABLE_SSE
  221 static int
  222 linux_proc_read_fpxregs(struct thread *td, struct linux_pt_fpxreg *fpxregs)
  223 {
  224 
  225         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
  226         if (cpu_fxsr == 0 || (td->td_proc->p_flag & P_INMEM) == 0)
  227                 return (EIO);
  228         bcopy(&get_pcb_user_save_td(td)->sv_xmm, fpxregs, sizeof(*fpxregs));
  229         return (0);
  230 }
  231 
  232 static int
  233 linux_proc_write_fpxregs(struct thread *td, struct linux_pt_fpxreg *fpxregs)
  234 {
  235 
  236         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
  237         if (cpu_fxsr == 0 || (td->td_proc->p_flag & P_INMEM) == 0)
  238                 return (EIO);
  239         bcopy(fpxregs, &get_pcb_user_save_td(td)->sv_xmm, sizeof(*fpxregs));
  240         return (0);
  241 }
  242 #endif
  243 
  244 int
  245 linux_ptrace(struct thread *td, struct linux_ptrace_args *uap)
  246 {
  247         union {
  248                 struct linux_pt_reg     reg;
  249                 struct linux_pt_fpreg   fpreg;
  250                 struct linux_pt_fpxreg  fpxreg;
  251         } r;
  252         union {
  253                 struct reg              bsd_reg;
  254                 struct fpreg            bsd_fpreg;
  255                 struct dbreg            bsd_dbreg;
  256         } u;
  257         void *addr;
  258         pid_t pid;
  259         int error, req;
  260 
  261         error = 0;
  262 
  263         /* by default, just copy data intact */
  264         req  = uap->req;
  265         pid  = (pid_t)uap->pid;
  266         addr = (void *)uap->addr;
  267 
  268         switch (req) {
  269         case PTRACE_TRACEME:
  270         case PTRACE_POKETEXT:
  271         case PTRACE_POKEDATA:
  272         case PTRACE_KILL:
  273                 error = kern_ptrace(td, req, pid, addr, uap->data);
  274                 break;
  275         case PTRACE_PEEKTEXT:
  276         case PTRACE_PEEKDATA: {
  277                 /* need to preserve return value */
  278                 int rval = td->td_retval[0];
  279                 error = kern_ptrace(td, req, pid, addr, 0);
  280                 if (error == 0)
  281                         error = copyout(td->td_retval, (void *)uap->data,
  282                             sizeof(l_int));
  283                 td->td_retval[0] = rval;
  284                 break;
  285         }
  286         case PTRACE_DETACH:
  287                 error = kern_ptrace(td, PT_DETACH, pid, (void *)1,
  288                      map_signum(uap->data));
  289                 break;
  290         case PTRACE_SINGLESTEP:
  291         case PTRACE_CONT:
  292                 error = kern_ptrace(td, req, pid, (void *)1,
  293                      map_signum(uap->data));
  294                 break;
  295         case PTRACE_ATTACH:
  296                 error = kern_ptrace(td, PT_ATTACH, pid, addr, uap->data);
  297                 break;
  298         case PTRACE_GETREGS:
  299                 /* Linux is using data where FreeBSD is using addr */
  300                 error = kern_ptrace(td, PT_GETREGS, pid, &u.bsd_reg, 0);
  301                 if (error == 0) {
  302                         map_regs_to_linux(&u.bsd_reg, &r.reg);
  303                         error = copyout(&r.reg, (void *)uap->data,
  304                             sizeof(r.reg));
  305                 }
  306                 break;
  307         case PTRACE_SETREGS:
  308                 /* Linux is using data where FreeBSD is using addr */
  309                 error = copyin((void *)uap->data, &r.reg, sizeof(r.reg));
  310                 if (error == 0) {
  311                         map_regs_from_linux(&u.bsd_reg, &r.reg);
  312                         error = kern_ptrace(td, PT_SETREGS, pid, &u.bsd_reg, 0);
  313                 }
  314                 break;
  315         case PTRACE_GETFPREGS:
  316                 /* Linux is using data where FreeBSD is using addr */
  317                 error = kern_ptrace(td, PT_GETFPREGS, pid, &u.bsd_fpreg, 0);
  318                 if (error == 0) {
  319                         map_fpregs_to_linux(&u.bsd_fpreg, &r.fpreg);
  320                         error = copyout(&r.fpreg, (void *)uap->data,
  321                             sizeof(r.fpreg));
  322                 }
  323                 break;
  324         case PTRACE_SETFPREGS:
  325                 /* Linux is using data where FreeBSD is using addr */
  326                 error = copyin((void *)uap->data, &r.fpreg, sizeof(r.fpreg));
  327                 if (error == 0) {
  328                         map_fpregs_from_linux(&u.bsd_fpreg, &r.fpreg);
  329                         error = kern_ptrace(td, PT_SETFPREGS, pid,
  330                             &u.bsd_fpreg, 0);
  331                 }
  332                 break;
  333         case PTRACE_SETFPXREGS:
  334 #ifdef CPU_ENABLE_SSE
  335                 error = copyin((void *)uap->data, &r.fpxreg, sizeof(r.fpxreg));
  336                 if (error)
  337                         break;
  338 #endif
  339                 /* FALL THROUGH */
  340         case PTRACE_GETFPXREGS: {
  341 #ifdef CPU_ENABLE_SSE
  342                 struct proc *p;
  343                 struct thread *td2;
  344 
  345                 if (sizeof(struct linux_pt_fpxreg) != sizeof(struct savexmm)) {
  346                         static int once = 0;
  347                         if (!once) {
  348                                 printf("linux: savexmm != linux_pt_fpxreg\n");
  349                                 once = 1;
  350                         }
  351                         error = EIO;
  352                         break;
  353                 }
  354 
  355                 if ((p = pfind(uap->pid)) == NULL) {
  356                         error = ESRCH;
  357                         break;
  358                 }
  359 
  360                 /* Exiting processes can't be debugged. */
  361                 if ((p->p_flag & P_WEXIT) != 0) {
  362                         error = ESRCH;
  363                         goto fail;
  364                 }
  365 
  366                 if ((error = p_candebug(td, p)) != 0)
  367                         goto fail;
  368 
  369                 /* System processes can't be debugged. */
  370                 if ((p->p_flag & P_SYSTEM) != 0) {
  371                         error = EINVAL;
  372                         goto fail;
  373                 }
  374 
  375                 /* not being traced... */
  376                 if ((p->p_flag & P_TRACED) == 0) {
  377                         error = EPERM;
  378                         goto fail;
  379                 }
  380 
  381                 /* not being traced by YOU */
  382                 if (p->p_pptr != td->td_proc) {
  383                         error = EBUSY;
  384                         goto fail;
  385                 }
  386 
  387                 /* not currently stopped */
  388                 if (!P_SHOULDSTOP(p) || (p->p_flag & P_WAITED) == 0) {
  389                         error = EBUSY;
  390                         goto fail;
  391                 }
  392 
  393                 if (req == PTRACE_GETFPXREGS) {
  394                         _PHOLD(p);      /* may block */
  395                         td2 = FIRST_THREAD_IN_PROC(p);
  396                         error = linux_proc_read_fpxregs(td2, &r.fpxreg);
  397                         _PRELE(p);
  398                         PROC_UNLOCK(p);
  399                         if (error == 0)
  400                                 error = copyout(&r.fpxreg, (void *)uap->data,
  401                                     sizeof(r.fpxreg));
  402                 } else {
  403                         /* clear dangerous bits exactly as Linux does*/
  404                         r.fpxreg.mxcsr &= 0xffbf;
  405                         _PHOLD(p);      /* may block */
  406                         td2 = FIRST_THREAD_IN_PROC(p);
  407                         error = linux_proc_write_fpxregs(td2, &r.fpxreg);
  408                         _PRELE(p);
  409                         PROC_UNLOCK(p);
  410                 }
  411                 break;
  412 
  413         fail:
  414                 PROC_UNLOCK(p);
  415 #else
  416                 error = EIO;
  417 #endif
  418                 break;
  419         }
  420         case PTRACE_PEEKUSR:
  421         case PTRACE_POKEUSR: {
  422                 error = EIO;
  423 
  424                 /* check addr for alignment */
  425                 if (uap->addr < 0 || uap->addr & (sizeof(l_int) - 1))
  426                         break;
  427                 /*
  428                  * Allow linux programs to access register values in
  429                  * user struct. We simulate this through PT_GET/SETREGS
  430                  * as necessary.
  431                  */
  432                 if (uap->addr < sizeof(struct linux_pt_reg)) {
  433                         error = kern_ptrace(td, PT_GETREGS, pid, &u.bsd_reg, 0);
  434                         if (error != 0)
  435                                 break;
  436 
  437                         map_regs_to_linux(&u.bsd_reg, &r.reg);
  438                         if (req == PTRACE_PEEKUSR) {
  439                                 error = copyout((char *)&r.reg + uap->addr,
  440                                     (void *)uap->data, sizeof(l_int));
  441                                 break;
  442                         }
  443 
  444                         *(l_int *)((char *)&r.reg + uap->addr) =
  445                             (l_int)uap->data;
  446 
  447                         map_regs_from_linux(&u.bsd_reg, &r.reg);
  448                         error = kern_ptrace(td, PT_SETREGS, pid, &u.bsd_reg, 0);
  449                 }
  450 
  451                 /*
  452                  * Simulate debug registers access
  453                  */
  454                 if (uap->addr >= LINUX_DBREG_OFFSET &&
  455                     uap->addr <= LINUX_DBREG_OFFSET + LINUX_DBREG_SIZE) {
  456                         error = kern_ptrace(td, PT_GETDBREGS, pid, &u.bsd_dbreg,
  457                             0);
  458                         if (error != 0)
  459                                 break;
  460 
  461                         uap->addr -= LINUX_DBREG_OFFSET;
  462                         if (req == PTRACE_PEEKUSR) {
  463                                 error = copyout((char *)&u.bsd_dbreg +
  464                                     uap->addr, (void *)uap->data,
  465                                     sizeof(l_int));
  466                                 break;
  467                         }
  468 
  469                         *(l_int *)((char *)&u.bsd_dbreg + uap->addr) =
  470                              uap->data;
  471                         error = kern_ptrace(td, PT_SETDBREGS, pid,
  472                             &u.bsd_dbreg, 0);
  473                 }
  474 
  475                 break;
  476         }
  477         case PTRACE_SYSCALL:
  478                 /* fall through */
  479         default:
  480                 printf("linux: ptrace(%u, ...) not implemented\n",
  481                     (unsigned int)uap->req);
  482                 error = EINVAL;
  483                 break;
  484         }
  485 
  486         return (error);
  487 }

Cache object: d1e89bf113b454c128918a30343d42b2


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.