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

Cache object: 90ca8df6bd999c0ecf9de62a8d26c91b


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