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

Cache object: c056b57de3d75387c6e0df0f9e817f74


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