]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/sys_process.c
MFC revs 203696, 203708, 203783 and 203788:
[FreeBSD/stable/8.git] / sys / kern / sys_process.c
1 /*-
2  * Copyright (c) 1994, Sean Eric Fagan
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  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Sean Eric Fagan.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_compat.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysent.h>
43 #include <sys/sysproto.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/ptrace.h>
47 #include <sys/sx.h>
48 #include <sys/malloc.h>
49 #include <sys/signalvar.h>
50
51 #include <machine/reg.h>
52
53 #include <security/audit/audit.h>
54
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_param.h>
63
64 #ifdef COMPAT_IA32
65 #include <sys/procfs.h>
66 #include <machine/fpu.h>
67 #include <compat/ia32/ia32_reg.h>
68
69 struct ptrace_io_desc32 {
70         int             piod_op;
71         u_int32_t       piod_offs;
72         u_int32_t       piod_addr;
73         u_int32_t       piod_len;
74 };
75
76 struct ptrace_vm_entry32 {
77         int             pve_entry;
78         int             pve_timestamp;
79         uint32_t        pve_start;
80         uint32_t        pve_end;
81         uint32_t        pve_offset;
82         u_int           pve_prot;
83         u_int           pve_pathlen;
84         int32_t         pve_fileid;
85         u_int           pve_fsid;
86         uint32_t        pve_path;
87 };
88
89 #endif
90
91 /*
92  * Functions implemented using PROC_ACTION():
93  *
94  * proc_read_regs(proc, regs)
95  *      Get the current user-visible register set from the process
96  *      and copy it into the regs structure (<machine/reg.h>).
97  *      The process is stopped at the time read_regs is called.
98  *
99  * proc_write_regs(proc, regs)
100  *      Update the current register set from the passed in regs
101  *      structure.  Take care to avoid clobbering special CPU
102  *      registers or privileged bits in the PSL.
103  *      Depending on the architecture this may have fix-up work to do,
104  *      especially if the IAR or PCW are modified.
105  *      The process is stopped at the time write_regs is called.
106  *
107  * proc_read_fpregs, proc_write_fpregs
108  *      deal with the floating point register set, otherwise as above.
109  *
110  * proc_read_dbregs, proc_write_dbregs
111  *      deal with the processor debug register set, otherwise as above.
112  *
113  * proc_sstep(proc)
114  *      Arrange for the process to trap after executing a single instruction.
115  */
116
117 #define PROC_ACTION(action) do {                                        \
118         int error;                                                      \
119                                                                         \
120         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);                        \
121         if ((td->td_proc->p_flag & P_INMEM) == 0)                       \
122                 error = EIO;                                            \
123         else                                                            \
124                 error = (action);                                       \
125         return (error);                                                 \
126 } while(0)
127
128 int
129 proc_read_regs(struct thread *td, struct reg *regs)
130 {
131
132         PROC_ACTION(fill_regs(td, regs));
133 }
134
135 int
136 proc_write_regs(struct thread *td, struct reg *regs)
137 {
138
139         PROC_ACTION(set_regs(td, regs));
140 }
141
142 int
143 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
144 {
145
146         PROC_ACTION(fill_dbregs(td, dbregs));
147 }
148
149 int
150 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
151 {
152
153         PROC_ACTION(set_dbregs(td, dbregs));
154 }
155
156 /*
157  * Ptrace doesn't support fpregs at all, and there are no security holes
158  * or translations for fpregs, so we can just copy them.
159  */
160 int
161 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
162 {
163
164         PROC_ACTION(fill_fpregs(td, fpregs));
165 }
166
167 int
168 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
169 {
170
171         PROC_ACTION(set_fpregs(td, fpregs));
172 }
173
174 #ifdef COMPAT_IA32
175 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
176 int
177 proc_read_regs32(struct thread *td, struct reg32 *regs32)
178 {
179
180         PROC_ACTION(fill_regs32(td, regs32));
181 }
182
183 int
184 proc_write_regs32(struct thread *td, struct reg32 *regs32)
185 {
186
187         PROC_ACTION(set_regs32(td, regs32));
188 }
189
190 int
191 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
192 {
193
194         PROC_ACTION(fill_dbregs32(td, dbregs32));
195 }
196
197 int
198 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
199 {
200
201         PROC_ACTION(set_dbregs32(td, dbregs32));
202 }
203
204 int
205 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
206 {
207
208         PROC_ACTION(fill_fpregs32(td, fpregs32));
209 }
210
211 int
212 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
213 {
214
215         PROC_ACTION(set_fpregs32(td, fpregs32));
216 }
217 #endif
218
219 int
220 proc_sstep(struct thread *td)
221 {
222
223         PROC_ACTION(ptrace_single_step(td));
224 }
225
226 int
227 proc_rwmem(struct proc *p, struct uio *uio)
228 {
229         vm_map_t map;
230         vm_object_t backing_object, object = NULL;
231         vm_offset_t pageno = 0;         /* page number */
232         vm_prot_t reqprot;
233         int error, fault_flags, writing;
234
235         /*
236          * Assert that someone has locked this vmspace.  (Should be
237          * curthread but we can't assert that.)  This keeps the process
238          * from exiting out from under us until this operation completes.
239          */
240         KASSERT(p->p_lock >= 1, ("%s: process %p (pid %d) not held", __func__,
241             p, p->p_pid));
242
243         /*
244          * The map we want...
245          */
246         map = &p->p_vmspace->vm_map;
247
248         writing = uio->uio_rw == UIO_WRITE;
249         reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) :
250             VM_PROT_READ;
251         fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL; 
252
253         /*
254          * Only map in one page at a time.  We don't have to, but it
255          * makes things easier.  This way is trivial - right?
256          */
257         do {
258                 vm_map_t tmap;
259                 vm_offset_t uva;
260                 int page_offset;                /* offset into page */
261                 vm_map_entry_t out_entry;
262                 vm_prot_t out_prot;
263                 boolean_t wired;
264                 vm_pindex_t pindex;
265                 u_int len;
266                 vm_page_t m;
267
268                 object = NULL;
269
270                 uva = (vm_offset_t)uio->uio_offset;
271
272                 /*
273                  * Get the page number of this segment.
274                  */
275                 pageno = trunc_page(uva);
276                 page_offset = uva - pageno;
277
278                 /*
279                  * How many bytes to copy
280                  */
281                 len = min(PAGE_SIZE - page_offset, uio->uio_resid);
282
283                 /*
284                  * Fault the page on behalf of the process
285                  */
286                 error = vm_fault(map, pageno, reqprot, fault_flags);
287                 if (error) {
288                         if (error == KERN_RESOURCE_SHORTAGE)
289                                 error = ENOMEM;
290                         else
291                                 error = EFAULT;
292                         break;
293                 }
294
295                 /*
296                  * Now we need to get the page.  out_entry, out_prot, wired,
297                  * and single_use aren't used.  One would think the vm code
298                  * would be a *bit* nicer...  We use tmap because
299                  * vm_map_lookup() can change the map argument.
300                  */
301                 tmap = map;
302                 error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry,
303                     &object, &pindex, &out_prot, &wired);
304                 if (error) {
305                         error = EFAULT;
306                         break;
307                 }
308                 VM_OBJECT_LOCK(object);
309                 while ((m = vm_page_lookup(object, pindex)) == NULL &&
310                     !writing &&
311                     (backing_object = object->backing_object) != NULL) {
312                         /*
313                          * Allow fallback to backing objects if we are reading.
314                          */
315                         VM_OBJECT_LOCK(backing_object);
316                         pindex += OFF_TO_IDX(object->backing_object_offset);
317                         VM_OBJECT_UNLOCK(object);
318                         object = backing_object;
319                 }
320                 VM_OBJECT_UNLOCK(object);
321                 if (m == NULL) {
322                         vm_map_lookup_done(tmap, out_entry);
323                         error = EFAULT;
324                         break;
325                 }
326
327                 /*
328                  * Hold the page in memory.
329                  */
330                 vm_page_lock_queues();
331                 vm_page_hold(m);
332                 vm_page_unlock_queues();
333
334                 /*
335                  * We're done with tmap now.
336                  */
337                 vm_map_lookup_done(tmap, out_entry);
338
339                 /*
340                  * Now do the i/o move.
341                  */
342                 error = uiomove_fromphys(&m, page_offset, len, uio);
343
344                 /*
345                  * Release the page.
346                  */
347                 vm_page_lock_queues();
348                 vm_page_unhold(m);
349                 vm_page_unlock_queues();
350
351         } while (error == 0 && uio->uio_resid > 0);
352
353         return (error);
354 }
355
356 static int
357 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
358 {
359         struct vattr vattr;
360         vm_map_t map;
361         vm_map_entry_t entry;
362         vm_object_t obj, tobj, lobj;
363         struct vmspace *vm;
364         struct vnode *vp;
365         char *freepath, *fullpath;
366         u_int pathlen;
367         int error, index, vfslocked;
368
369         error = 0;
370         obj = NULL;
371
372         vm = vmspace_acquire_ref(p);
373         map = &vm->vm_map;
374         vm_map_lock_read(map);
375
376         do {
377                 entry = map->header.next;
378                 index = 0;
379                 while (index < pve->pve_entry && entry != &map->header) {
380                         entry = entry->next;
381                         index++;
382                 }
383                 if (index != pve->pve_entry) {
384                         error = EINVAL;
385                         break;
386                 }
387                 while (entry != &map->header &&
388                     (entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
389                         entry = entry->next;
390                         index++;
391                 }
392                 if (entry == &map->header) {
393                         error = ENOENT;
394                         break;
395                 }
396
397                 /* We got an entry. */
398                 pve->pve_entry = index + 1;
399                 pve->pve_timestamp = map->timestamp;
400                 pve->pve_start = entry->start;
401                 pve->pve_end = entry->end - 1;
402                 pve->pve_offset = entry->offset;
403                 pve->pve_prot = entry->protection;
404
405                 /* Backing object's path needed? */
406                 if (pve->pve_pathlen == 0)
407                         break;
408
409                 pathlen = pve->pve_pathlen;
410                 pve->pve_pathlen = 0;
411
412                 obj = entry->object.vm_object;
413                 if (obj != NULL)
414                         VM_OBJECT_LOCK(obj);
415         } while (0);
416
417         vm_map_unlock_read(map);
418         vmspace_free(vm);
419
420         pve->pve_fsid = VNOVAL;
421         pve->pve_fileid = VNOVAL;
422
423         if (error == 0 && obj != NULL) {
424                 lobj = obj;
425                 for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
426                         if (tobj != obj)
427                                 VM_OBJECT_LOCK(tobj);
428                         if (lobj != obj)
429                                 VM_OBJECT_UNLOCK(lobj);
430                         lobj = tobj;
431                         pve->pve_offset += tobj->backing_object_offset;
432                 }
433                 vp = (lobj->type == OBJT_VNODE) ? lobj->handle : NULL;
434                 if (vp != NULL)
435                         vref(vp);
436                 if (lobj != obj)
437                         VM_OBJECT_UNLOCK(lobj);
438                 VM_OBJECT_UNLOCK(obj);
439
440                 if (vp != NULL) {
441                         freepath = NULL;
442                         fullpath = NULL;
443                         vn_fullpath(td, vp, &fullpath, &freepath);
444                         vfslocked = VFS_LOCK_GIANT(vp->v_mount);
445                         vn_lock(vp, LK_SHARED | LK_RETRY);
446                         if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
447                                 pve->pve_fileid = vattr.va_fileid;
448                                 pve->pve_fsid = vattr.va_fsid;
449                         }
450                         vput(vp);
451                         VFS_UNLOCK_GIANT(vfslocked);
452
453                         if (fullpath != NULL) {
454                                 pve->pve_pathlen = strlen(fullpath) + 1;
455                                 if (pve->pve_pathlen <= pathlen) {
456                                         error = copyout(fullpath, pve->pve_path,
457                                             pve->pve_pathlen);
458                                 } else
459                                         error = ENAMETOOLONG;
460                         }
461                         if (freepath != NULL)
462                                 free(freepath, M_TEMP);
463                 }
464         }
465
466         return (error);
467 }
468
469 #ifdef COMPAT_IA32
470 static int      
471 ptrace_vm_entry32(struct thread *td, struct proc *p,
472     struct ptrace_vm_entry32 *pve32)
473 {
474         struct ptrace_vm_entry pve;
475         int error;
476
477         pve.pve_entry = pve32->pve_entry;
478         pve.pve_pathlen = pve32->pve_pathlen;
479         pve.pve_path = (void *)(uintptr_t)pve32->pve_path;
480
481         error = ptrace_vm_entry(td, p, &pve);
482         if (error == 0) {
483                 pve32->pve_entry = pve.pve_entry;
484                 pve32->pve_timestamp = pve.pve_timestamp;
485                 pve32->pve_start = pve.pve_start;
486                 pve32->pve_end = pve.pve_end;
487                 pve32->pve_offset = pve.pve_offset;
488                 pve32->pve_prot = pve.pve_prot;
489                 pve32->pve_fileid = pve.pve_fileid;
490                 pve32->pve_fsid = pve.pve_fsid;
491         }
492
493         pve32->pve_pathlen = pve.pve_pathlen;
494         return (error);
495 }
496 #endif /* COMPAT_IA32 */
497
498 /*
499  * Process debugging system call.
500  */
501 #ifndef _SYS_SYSPROTO_H_
502 struct ptrace_args {
503         int     req;
504         pid_t   pid;
505         caddr_t addr;
506         int     data;
507 };
508 #endif
509
510 #ifdef COMPAT_IA32
511 /*
512  * This CPP subterfuge is to try and reduce the number of ifdefs in
513  * the body of the code.
514  *   COPYIN(uap->addr, &r.reg, sizeof r.reg);
515  * becomes either:
516  *   copyin(uap->addr, &r.reg, sizeof r.reg);
517  * or
518  *   copyin(uap->addr, &r.reg32, sizeof r.reg32);
519  * .. except this is done at runtime.
520  */
521 #define COPYIN(u, k, s)         wrap32 ? \
522         copyin(u, k ## 32, s ## 32) : \
523         copyin(u, k, s)
524 #define COPYOUT(k, u, s)        wrap32 ? \
525         copyout(k ## 32, u, s ## 32) : \
526         copyout(k, u, s)
527 #else
528 #define COPYIN(u, k, s)         copyin(u, k, s)
529 #define COPYOUT(k, u, s)        copyout(k, u, s)
530 #endif
531 int
532 ptrace(struct thread *td, struct ptrace_args *uap)
533 {
534         /*
535          * XXX this obfuscation is to reduce stack usage, but the register
536          * structs may be too large to put on the stack anyway.
537          */
538         union {
539                 struct ptrace_io_desc piod;
540                 struct ptrace_lwpinfo pl;
541                 struct ptrace_vm_entry pve;
542                 struct dbreg dbreg;
543                 struct fpreg fpreg;
544                 struct reg reg;
545 #ifdef COMPAT_IA32
546                 struct dbreg32 dbreg32;
547                 struct fpreg32 fpreg32;
548                 struct reg32 reg32;
549                 struct ptrace_io_desc32 piod32;
550                 struct ptrace_vm_entry32 pve32;
551 #endif
552         } r;
553         void *addr;
554         int error = 0;
555 #ifdef COMPAT_IA32
556         int wrap32 = 0;
557
558         if (SV_CURPROC_FLAG(SV_ILP32))
559                 wrap32 = 1;
560 #endif
561         AUDIT_ARG_PID(uap->pid);
562         AUDIT_ARG_CMD(uap->req);
563         AUDIT_ARG_VALUE(uap->data);
564         addr = &r;
565         switch (uap->req) {
566         case PT_GETREGS:
567         case PT_GETFPREGS:
568         case PT_GETDBREGS:
569         case PT_LWPINFO:
570                 break;
571         case PT_SETREGS:
572                 error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
573                 break;
574         case PT_SETFPREGS:
575                 error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
576                 break;
577         case PT_SETDBREGS:
578                 error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
579                 break;
580         case PT_IO:
581                 error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
582                 break;
583         case PT_VM_ENTRY:
584                 error = COPYIN(uap->addr, &r.pve, sizeof r.pve);
585                 break;
586         default:
587                 addr = uap->addr;
588                 break;
589         }
590         if (error)
591                 return (error);
592
593         error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
594         if (error)
595                 return (error);
596
597         switch (uap->req) {
598         case PT_VM_ENTRY:
599                 error = COPYOUT(&r.pve, uap->addr, sizeof r.pve);
600                 break;
601         case PT_IO:
602                 error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
603                 break;
604         case PT_GETREGS:
605                 error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
606                 break;
607         case PT_GETFPREGS:
608                 error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
609                 break;
610         case PT_GETDBREGS:
611                 error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
612                 break;
613         case PT_LWPINFO:
614                 error = copyout(&r.pl, uap->addr, uap->data);
615                 break;
616         }
617
618         return (error);
619 }
620 #undef COPYIN
621 #undef COPYOUT
622
623 #ifdef COMPAT_IA32
624 /*
625  *   PROC_READ(regs, td2, addr);
626  * becomes either:
627  *   proc_read_regs(td2, addr);
628  * or
629  *   proc_read_regs32(td2, addr);
630  * .. except this is done at runtime.  There is an additional
631  * complication in that PROC_WRITE disallows 32 bit consumers
632  * from writing to 64 bit address space targets.
633  */
634 #define PROC_READ(w, t, a)      wrap32 ? \
635         proc_read_ ## w ## 32(t, a) : \
636         proc_read_ ## w (t, a)
637 #define PROC_WRITE(w, t, a)     wrap32 ? \
638         (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
639         proc_write_ ## w (t, a)
640 #else
641 #define PROC_READ(w, t, a)      proc_read_ ## w (t, a)
642 #define PROC_WRITE(w, t, a)     proc_write_ ## w (t, a)
643 #endif
644
645 int
646 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
647 {
648         struct iovec iov;
649         struct uio uio;
650         struct proc *curp, *p, *pp;
651         struct thread *td2 = NULL;
652         struct ptrace_io_desc *piod = NULL;
653         struct ptrace_lwpinfo *pl;
654         int error, write, tmp, num;
655         int proctree_locked = 0;
656         lwpid_t tid = 0, *buf;
657 #ifdef COMPAT_IA32
658         int wrap32 = 0, safe = 0;
659         struct ptrace_io_desc32 *piod32 = NULL;
660 #endif
661
662         curp = td->td_proc;
663
664         /* Lock proctree before locking the process. */
665         switch (req) {
666         case PT_TRACE_ME:
667         case PT_ATTACH:
668         case PT_STEP:
669         case PT_CONTINUE:
670         case PT_TO_SCE:
671         case PT_TO_SCX:
672         case PT_SYSCALL:
673         case PT_DETACH:
674                 sx_xlock(&proctree_lock);
675                 proctree_locked = 1;
676                 break;
677         default:
678                 break;
679         }
680
681         write = 0;
682         if (req == PT_TRACE_ME) {
683                 p = td->td_proc;
684                 PROC_LOCK(p);
685         } else {
686                 if (pid <= PID_MAX) {
687                         if ((p = pfind(pid)) == NULL) {
688                                 if (proctree_locked)
689                                         sx_xunlock(&proctree_lock);
690                                 return (ESRCH);
691                         }
692                 } else {
693                         /* this is slow, should be optimized */
694                         sx_slock(&allproc_lock);
695                         FOREACH_PROC_IN_SYSTEM(p) {
696                                 PROC_LOCK(p);
697                                 FOREACH_THREAD_IN_PROC(p, td2) {
698                                         if (td2->td_tid == pid)
699                                                 break;
700                                 }
701                                 if (td2 != NULL)
702                                         break; /* proc lock held */
703                                 PROC_UNLOCK(p);
704                         }
705                         sx_sunlock(&allproc_lock);
706                         if (p == NULL) {
707                                 if (proctree_locked)
708                                         sx_xunlock(&proctree_lock);
709                                 return (ESRCH);
710                         }
711                         tid = pid;
712                         pid = p->p_pid;
713                 }
714         }
715         AUDIT_ARG_PROCESS(p);
716
717         if ((p->p_flag & P_WEXIT) != 0) {
718                 error = ESRCH;
719                 goto fail;
720         }
721         if ((error = p_cansee(td, p)) != 0)
722                 goto fail;
723
724         if ((error = p_candebug(td, p)) != 0)
725                 goto fail;
726
727         /*
728          * System processes can't be debugged.
729          */
730         if ((p->p_flag & P_SYSTEM) != 0) {
731                 error = EINVAL;
732                 goto fail;
733         }
734
735         if (tid == 0) {
736                 if ((p->p_flag & P_STOPPED_TRACE) != 0) {
737                         KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
738                         td2 = p->p_xthread;
739                 } else {
740                         td2 = FIRST_THREAD_IN_PROC(p);
741                 }
742                 tid = td2->td_tid;
743         }
744
745 #ifdef COMPAT_IA32
746         /*
747          * Test if we're a 32 bit client and what the target is.
748          * Set the wrap controls accordingly.
749          */
750         if (SV_CURPROC_FLAG(SV_ILP32)) {
751                 if (td2->td_proc->p_sysent->sv_flags & SV_ILP32)
752                         safe = 1;
753                 wrap32 = 1;
754         }
755 #endif
756         /*
757          * Permissions check
758          */
759         switch (req) {
760         case PT_TRACE_ME:
761                 /* Always legal. */
762                 break;
763
764         case PT_ATTACH:
765                 /* Self */
766                 if (p->p_pid == td->td_proc->p_pid) {
767                         error = EINVAL;
768                         goto fail;
769                 }
770
771                 /* Already traced */
772                 if (p->p_flag & P_TRACED) {
773                         error = EBUSY;
774                         goto fail;
775                 }
776
777                 /* Can't trace an ancestor if you're being traced. */
778                 if (curp->p_flag & P_TRACED) {
779                         for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
780                                 if (pp == p) {
781                                         error = EINVAL;
782                                         goto fail;
783                                 }
784                         }
785                 }
786
787
788                 /* OK */
789                 break;
790
791         case PT_CLEARSTEP:
792                 /* Allow thread to clear single step for itself */
793                 if (td->td_tid == tid)
794                         break;
795
796                 /* FALLTHROUGH */
797         default:
798                 /* not being traced... */
799                 if ((p->p_flag & P_TRACED) == 0) {
800                         error = EPERM;
801                         goto fail;
802                 }
803
804                 /* not being traced by YOU */
805                 if (p->p_pptr != td->td_proc) {
806                         error = EBUSY;
807                         goto fail;
808                 }
809
810                 /* not currently stopped */
811                 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 ||
812                     p->p_suspcount != p->p_numthreads  ||
813                     (p->p_flag & P_WAITED) == 0) {
814                         error = EBUSY;
815                         goto fail;
816                 }
817
818                 if ((p->p_flag & P_STOPPED_TRACE) == 0) {
819                         static int count = 0;
820                         if (count++ == 0)
821                                 printf("P_STOPPED_TRACE not set.\n");
822                 }
823
824                 /* OK */
825                 break;
826         }
827
828         /* Keep this process around until we finish this request. */
829         _PHOLD(p);
830
831 #ifdef FIX_SSTEP
832         /*
833          * Single step fixup ala procfs
834          */
835         FIX_SSTEP(td2);
836 #endif
837
838         /*
839          * Actually do the requests
840          */
841
842         td->td_retval[0] = 0;
843
844         switch (req) {
845         case PT_TRACE_ME:
846                 /* set my trace flag and "owner" so it can read/write me */
847                 p->p_flag |= P_TRACED;
848                 p->p_oppid = p->p_pptr->p_pid;
849                 break;
850
851         case PT_ATTACH:
852                 /* security check done above */
853                 p->p_flag |= P_TRACED;
854                 p->p_oppid = p->p_pptr->p_pid;
855                 if (p->p_pptr != td->td_proc)
856                         proc_reparent(p, td->td_proc);
857                 data = SIGSTOP;
858                 goto sendsig;   /* in PT_CONTINUE below */
859
860         case PT_CLEARSTEP:
861                 error = ptrace_clear_single_step(td2);
862                 break;
863
864         case PT_SETSTEP:
865                 error = ptrace_single_step(td2);
866                 break;
867
868         case PT_SUSPEND:
869                 td2->td_dbgflags |= TDB_SUSPEND;
870                 thread_lock(td2);
871                 td2->td_flags |= TDF_NEEDSUSPCHK;
872                 thread_unlock(td2);
873                 break;
874
875         case PT_RESUME:
876                 td2->td_dbgflags &= ~TDB_SUSPEND;
877                 break;
878
879         case PT_STEP:
880         case PT_CONTINUE:
881         case PT_TO_SCE:
882         case PT_TO_SCX:
883         case PT_SYSCALL:
884         case PT_DETACH:
885                 /* Zero means do not send any signal */
886                 if (data < 0 || data > _SIG_MAXSIG) {
887                         error = EINVAL;
888                         break;
889                 }
890
891                 switch (req) {
892                 case PT_STEP:
893                         error = ptrace_single_step(td2);
894                         if (error)
895                                 goto out;
896                         break;
897                 case PT_TO_SCE:
898                         p->p_stops |= S_PT_SCE;
899                         break;
900                 case PT_TO_SCX:
901                         p->p_stops |= S_PT_SCX;
902                         break;
903                 case PT_SYSCALL:
904                         p->p_stops |= S_PT_SCE | S_PT_SCX;
905                         break;
906                 }
907
908                 if (addr != (void *)1) {
909                         error = ptrace_set_pc(td2, (u_long)(uintfptr_t)addr);
910                         if (error)
911                                 break;
912                 }
913
914                 if (req == PT_DETACH) {
915                         /* reset process parent */
916                         if (p->p_oppid != p->p_pptr->p_pid) {
917                                 struct proc *pp;
918
919                                 PROC_LOCK(p->p_pptr);
920                                 sigqueue_take(p->p_ksi);
921                                 PROC_UNLOCK(p->p_pptr);
922
923                                 PROC_UNLOCK(p);
924                                 pp = pfind(p->p_oppid);
925                                 if (pp == NULL)
926                                         pp = initproc;
927                                 else
928                                         PROC_UNLOCK(pp);
929                                 PROC_LOCK(p);
930                                 proc_reparent(p, pp);
931                                 if (pp == initproc)
932                                         p->p_sigparent = SIGCHLD;
933                         }
934                         p->p_flag &= ~(P_TRACED | P_WAITED);
935                         p->p_oppid = 0;
936
937                         /* should we send SIGCHLD? */
938                         /* childproc_continued(p); */
939                 }
940
941         sendsig:
942                 if (proctree_locked) {
943                         sx_xunlock(&proctree_lock);
944                         proctree_locked = 0;
945                 }
946                 p->p_xstat = data;
947                 p->p_xthread = NULL;
948                 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) != 0) {
949                         /* deliver or queue signal */
950                         td2->td_dbgflags &= ~TDB_XSIG;
951                         td2->td_xsig = data;
952
953                         if (req == PT_DETACH) {
954                                 struct thread *td3;
955                                 FOREACH_THREAD_IN_PROC(p, td3) {
956                                         td3->td_dbgflags &= ~TDB_SUSPEND; 
957                                 }
958                         }
959                         /*
960                          * unsuspend all threads, to not let a thread run,
961                          * you should use PT_SUSPEND to suspend it before
962                          * continuing process.
963                          */
964                         PROC_SLOCK(p);
965                         p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED);
966                         thread_unsuspend(p);
967                         PROC_SUNLOCK(p);
968                 } else {
969                         if (data)
970                                 psignal(p, data);
971                 }
972                 break;
973
974         case PT_WRITE_I:
975         case PT_WRITE_D:
976                 td2->td_dbgflags |= TDB_USERWR;
977                 write = 1;
978                 /* FALLTHROUGH */
979         case PT_READ_I:
980         case PT_READ_D:
981                 PROC_UNLOCK(p);
982                 tmp = 0;
983                 /* write = 0 set above */
984                 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
985                 iov.iov_len = sizeof(int);
986                 uio.uio_iov = &iov;
987                 uio.uio_iovcnt = 1;
988                 uio.uio_offset = (off_t)(uintptr_t)addr;
989                 uio.uio_resid = sizeof(int);
990                 uio.uio_segflg = UIO_SYSSPACE;  /* i.e.: the uap */
991                 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
992                 uio.uio_td = td;
993                 error = proc_rwmem(p, &uio);
994                 if (uio.uio_resid != 0) {
995                         /*
996                          * XXX proc_rwmem() doesn't currently return ENOSPC,
997                          * so I think write() can bogusly return 0.
998                          * XXX what happens for short writes?  We don't want
999                          * to write partial data.
1000                          * XXX proc_rwmem() returns EPERM for other invalid
1001                          * addresses.  Convert this to EINVAL.  Does this
1002                          * clobber returns of EPERM for other reasons?
1003                          */
1004                         if (error == 0 || error == ENOSPC || error == EPERM)
1005                                 error = EINVAL; /* EOF */
1006                 }
1007                 if (!write)
1008                         td->td_retval[0] = tmp;
1009                 PROC_LOCK(p);
1010                 break;
1011
1012         case PT_IO:
1013 #ifdef COMPAT_IA32
1014                 if (wrap32) {
1015                         piod32 = addr;
1016                         iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
1017                         iov.iov_len = piod32->piod_len;
1018                         uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
1019                         uio.uio_resid = piod32->piod_len;
1020                 } else
1021 #endif
1022                 {
1023                         piod = addr;
1024                         iov.iov_base = piod->piod_addr;
1025                         iov.iov_len = piod->piod_len;
1026                         uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1027                         uio.uio_resid = piod->piod_len;
1028                 }
1029                 uio.uio_iov = &iov;
1030                 uio.uio_iovcnt = 1;
1031                 uio.uio_segflg = UIO_USERSPACE;
1032                 uio.uio_td = td;
1033 #ifdef COMPAT_IA32
1034                 tmp = wrap32 ? piod32->piod_op : piod->piod_op;
1035 #else
1036                 tmp = piod->piod_op;
1037 #endif
1038                 switch (tmp) {
1039                 case PIOD_READ_D:
1040                 case PIOD_READ_I:
1041                         uio.uio_rw = UIO_READ;
1042                         break;
1043                 case PIOD_WRITE_D:
1044                 case PIOD_WRITE_I:
1045                         td2->td_dbgflags |= TDB_USERWR;
1046                         uio.uio_rw = UIO_WRITE;
1047                         break;
1048                 default:
1049                         error = EINVAL;
1050                         goto out;
1051                 }
1052                 PROC_UNLOCK(p);
1053                 error = proc_rwmem(p, &uio);
1054 #ifdef COMPAT_IA32
1055                 if (wrap32)
1056                         piod32->piod_len -= uio.uio_resid;
1057                 else
1058 #endif
1059                         piod->piod_len -= uio.uio_resid;
1060                 PROC_LOCK(p);
1061                 break;
1062
1063         case PT_KILL:
1064                 data = SIGKILL;
1065                 goto sendsig;   /* in PT_CONTINUE above */
1066
1067         case PT_SETREGS:
1068                 td2->td_dbgflags |= TDB_USERWR;
1069                 error = PROC_WRITE(regs, td2, addr);
1070                 break;
1071
1072         case PT_GETREGS:
1073                 error = PROC_READ(regs, td2, addr);
1074                 break;
1075
1076         case PT_SETFPREGS:
1077                 td2->td_dbgflags |= TDB_USERWR;
1078                 error = PROC_WRITE(fpregs, td2, addr);
1079                 break;
1080
1081         case PT_GETFPREGS:
1082                 error = PROC_READ(fpregs, td2, addr);
1083                 break;
1084
1085         case PT_SETDBREGS:
1086                 td2->td_dbgflags |= TDB_USERWR;
1087                 error = PROC_WRITE(dbregs, td2, addr);
1088                 break;
1089
1090         case PT_GETDBREGS:
1091                 error = PROC_READ(dbregs, td2, addr);
1092                 break;
1093
1094         case PT_LWPINFO:
1095                 if (data <= 0 || data > sizeof(*pl)) {
1096                         error = EINVAL;
1097                         break;
1098                 }
1099                 pl = addr;
1100                 pl->pl_lwpid = td2->td_tid;
1101                 if (td2->td_dbgflags & TDB_XSIG)
1102                         pl->pl_event = PL_EVENT_SIGNAL;
1103                 else
1104                         pl->pl_event = 0;
1105                 pl->pl_flags = 0;
1106                 pl->pl_sigmask = td2->td_sigmask;
1107                 pl->pl_siglist = td2->td_siglist;
1108                 break;
1109
1110         case PT_GETNUMLWPS:
1111                 td->td_retval[0] = p->p_numthreads;
1112                 break;
1113
1114         case PT_GETLWPLIST:
1115                 if (data <= 0) {
1116                         error = EINVAL;
1117                         break;
1118                 }
1119                 num = imin(p->p_numthreads, data);
1120                 PROC_UNLOCK(p);
1121                 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1122                 tmp = 0;
1123                 PROC_LOCK(p);
1124                 FOREACH_THREAD_IN_PROC(p, td2) {
1125                         if (tmp >= num)
1126                                 break;
1127                         buf[tmp++] = td2->td_tid;
1128                 }
1129                 PROC_UNLOCK(p);
1130                 error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1131                 free(buf, M_TEMP);
1132                 if (!error)
1133                         td->td_retval[0] = tmp;
1134                 PROC_LOCK(p);
1135                 break;
1136
1137         case PT_VM_TIMESTAMP:
1138                 td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1139                 break;
1140
1141         case PT_VM_ENTRY:
1142                 PROC_UNLOCK(p);
1143 #ifdef COMPAT_IA32
1144                 if (wrap32)
1145                         error = ptrace_vm_entry32(td, p, addr);
1146                 else
1147 #endif
1148                 error = ptrace_vm_entry(td, p, addr);
1149                 PROC_LOCK(p);
1150                 break;
1151
1152         default:
1153 #ifdef __HAVE_PTRACE_MACHDEP
1154                 if (req >= PT_FIRSTMACH) {
1155                         PROC_UNLOCK(p);
1156                         error = cpu_ptrace(td2, req, addr, data);
1157                         PROC_LOCK(p);
1158                 } else
1159 #endif
1160                         /* Unknown request. */
1161                         error = EINVAL;
1162                 break;
1163         }
1164
1165 out:
1166         /* Drop our hold on this process now that the request has completed. */
1167         _PRELE(p);
1168 fail:
1169         PROC_UNLOCK(p);
1170         if (proctree_locked)
1171                 sx_xunlock(&proctree_lock);
1172         return (error);
1173 }
1174 #undef PROC_READ
1175 #undef PROC_WRITE
1176
1177 /*
1178  * Stop a process because of a debugging event;
1179  * stay stopped until p->p_step is cleared
1180  * (cleared by PIOCCONT in procfs).
1181  */
1182 void
1183 stopevent(struct proc *p, unsigned int event, unsigned int val)
1184 {
1185
1186         PROC_LOCK_ASSERT(p, MA_OWNED);
1187         p->p_step = 1;
1188         do {
1189                 p->p_xstat = val;
1190                 p->p_xthread = NULL;
1191                 p->p_stype = event;     /* Which event caused the stop? */
1192                 wakeup(&p->p_stype);    /* Wake up any PIOCWAIT'ing procs */
1193                 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
1194         } while (p->p_step);
1195 }