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

Cache object: a0e09f1bc285342b973a8390b7b25573


[ 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.