2 * Copyright (c) 1994, Sean Eric Fagan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 * $FreeBSD: src/sys/kern/sys_process.c,v 1.51.2.6 2003/01/08 03:06:45 kan Exp $
32 * $DragonFly: src/sys/kern/sys_process.c,v 1.30 2007/02/19 01:14:23 corecode Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysproto.h>
40 #include <sys/vnode.h>
41 #include <sys/ptrace.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_page.h>
51 #include <vfs/procfs/procfs.h>
53 #include <sys/thread2.h>
54 #include <sys/spinlock2.h>
56 /* use the equivalent procfs code */
59 pread (struct proc
*procp
, unsigned int addr
, unsigned int *retval
) {
64 int page_offset
; /* offset into page */
65 vm_offset_t pageno
; /* page number */
66 vm_map_entry_t out_entry
;
71 /* Map page into kernel space */
73 map
= &procp
->p_vmspace
->vm_map
;
75 page_offset
= addr
- trunc_page(addr
);
76 pageno
= trunc_page(addr
);
79 rv
= vm_map_lookup(&tmap
, pageno
, VM_PROT_READ
, &out_entry
,
80 &object
, &pindex
, &out_prot
, &wflags
);
82 if (rv
!= KERN_SUCCESS
)
85 vm_map_lookup_done (tmap
, out_entry
, 0);
87 /* Find space in kernel_map for the page we're interested in */
88 rv
= vm_map_find (&kernel_map
, object
, NULL
,
89 IDX_TO_OFF(pindex
), &kva
, PAGE_SIZE
,
91 VM_MAPTYPE_NORMAL
, VM_SUBSYS_PROC
,
92 VM_PROT_ALL
, VM_PROT_ALL
, 0);
95 vm_object_reference
XXX (object
);
97 rv
= vm_map_wire (&kernel_map
, kva
, kva
+ PAGE_SIZE
, 0);
100 bcopy ((caddr_t
)kva
+ page_offset
,
101 retval
, sizeof *retval
);
103 vm_map_remove (&kernel_map
, kva
, kva
+ PAGE_SIZE
);
110 pwrite (struct proc
*procp
, unsigned int addr
, unsigned int datum
) {
115 int page_offset
; /* offset into page */
116 vm_offset_t pageno
; /* page number */
117 vm_map_entry_t out_entry
;
121 boolean_t fix_prot
= 0;
123 /* Map page into kernel space */
125 map
= &procp
->p_vmspace
->vm_map
;
127 page_offset
= addr
- trunc_page(addr
);
128 pageno
= trunc_page(addr
);
131 * Check the permissions for the area we're interested in.
134 if (vm_map_check_protection (map
, pageno
, pageno
+ PAGE_SIZE
,
135 VM_PROT_WRITE
, FALSE
) == FALSE
) {
137 * If the page was not writable, we make it so.
138 * XXX It is possible a page may *not* be read/executable,
139 * if a process changes that!
142 /* The page isn't writable, so let's try making it so... */
143 if ((rv
= vm_map_protect (map
, pageno
, pageno
+ PAGE_SIZE
,
144 VM_PROT_ALL
, 0)) != KERN_SUCCESS
)
145 return EFAULT
; /* I guess... */
149 * Now we need to get the page. out_entry, out_prot, wflags, and
150 * single_use aren't used. One would think the vm code would be
151 * a *bit* nicer... We use tmap because vm_map_lookup() can
152 * change the map argument.
156 rv
= vm_map_lookup(&tmap
, pageno
, VM_PROT_WRITE
, &out_entry
,
157 &object
, &pindex
, &out_prot
, &wflags
);
158 if (rv
!= KERN_SUCCESS
)
162 * Okay, we've got the page. Let's release tmap.
164 vm_map_lookup_done (tmap
, out_entry
, 0);
167 * Fault the page in...
169 rv
= vm_fault(map
, pageno
, VM_PROT_WRITE
|VM_PROT_READ
, FALSE
);
170 if (rv
!= KERN_SUCCESS
)
173 /* Find space in kernel_map for the page we're interested in */
174 rv
= vm_map_find (&kernel_map
, object
, NULL
,
175 IDX_TO_OFF(pindex
), &kva
, PAGE_SIZE
,
177 VM_MAPTYPE_NORMAL
, VM_SUBSYS_PROC
,
178 VM_PROT_ALL
, VM_PROT_ALL
, 0);
180 vm_object_reference
XXX (object
);
182 rv
= vm_map_wire (&kernel_map
, kva
, kva
+ PAGE_SIZE
, 0);
184 bcopy (&datum
, (caddr_t
)kva
+ page_offset
, sizeof datum
);
186 vm_map_remove (&kernel_map
, kva
, kva
+ PAGE_SIZE
);
190 vm_map_protect (map
, pageno
, pageno
+ PAGE_SIZE
,
191 VM_PROT_READ
|VM_PROT_EXECUTE
, 0);
197 * Process debugging system call.
202 sys_ptrace(struct ptrace_args
*uap
)
204 struct proc
*p
= curproc
;
207 * XXX this obfuscation is to reduce stack usage, but the register
208 * structs may be too large to put on the stack anyway.
211 struct ptrace_io_desc piod
;
228 error
= copyin(uap
->addr
, &r
.reg
, sizeof r
.reg
);
231 error
= copyin(uap
->addr
, &r
.fpreg
, sizeof r
.fpreg
);
235 error
= copyin(uap
->addr
, &r
.dbreg
, sizeof r
.dbreg
);
239 error
= copyin(uap
->addr
, &r
.piod
, sizeof r
.piod
);
247 error
= kern_ptrace(p
, uap
->req
, uap
->pid
, addr
, uap
->data
,
248 &uap
->sysmsg_result
);
254 (void)copyout(&r
.piod
, uap
->addr
, sizeof r
.piod
);
257 error
= copyout(&r
.reg
, uap
->addr
, sizeof r
.reg
);
260 error
= copyout(&r
.fpreg
, uap
->addr
, sizeof r
.fpreg
);
264 error
= copyout(&r
.dbreg
, uap
->addr
, sizeof r
.dbreg
);
273 kern_ptrace(struct proc
*curp
, int req
, pid_t pid
, void *addr
,
280 struct ptrace_io_desc
*piod
;
286 if (req
== PT_TRACE_ME
) {
290 if ((p
= pfind(pid
)) == NULL
)
293 if (!PRISON_CHECK(curp
->p_ucred
, p
->p_ucred
)) {
297 if (p
->p_flags
& P_SYSTEM
) {
302 lwkt_gettoken(&p
->p_token
);
303 /* Can't trace a process that's currently exec'ing. */
304 if ((p
->p_flags
& P_INEXEC
) != 0) {
305 lwkt_reltoken(&p
->p_token
);
320 if (p
->p_pid
== curp
->p_pid
) {
321 lwkt_reltoken(&p
->p_token
);
327 if (p
->p_flags
& P_TRACED
) {
328 lwkt_reltoken(&p
->p_token
);
333 if (curp
->p_flags
& P_TRACED
)
334 for (pp
= curp
->p_pptr
; pp
!= NULL
; pp
= pp
->p_pptr
)
336 lwkt_reltoken(&p
->p_token
);
341 /* not owned by you, has done setuid (unless you're root) */
342 if ((p
->p_ucred
->cr_ruid
!= curp
->p_ucred
->cr_ruid
) ||
343 (p
->p_flags
& P_SUGID
)) {
344 if ((error
= priv_check_cred(curp
->p_ucred
, PRIV_ROOT
, 0)) != 0) {
345 lwkt_reltoken(&p
->p_token
);
351 /* can't trace init when securelevel > 0 */
352 if (securelevel
> 0 && p
->p_pid
== 1) {
353 lwkt_reltoken(&p
->p_token
);
388 /* not being traced... */
389 if ((p
->p_flags
& P_TRACED
) == 0) {
390 lwkt_reltoken(&p
->p_token
);
395 /* not being traced by YOU */
396 if (p
->p_pptr
!= curp
) {
397 lwkt_reltoken(&p
->p_token
);
402 /* not currently stopped */
403 if (p
->p_stat
!= SSTOP
||
404 (p
->p_flags
& P_WAITED
) == 0) {
405 lwkt_reltoken(&p
->p_token
);
414 lwkt_reltoken(&p
->p_token
);
420 lp
= FIRST_LWP_IN_PROC(p
);
423 * Single step fixup ala procfs
429 * Actually do the requests
436 /* set my trace flag and "owner" so it can read/write me */
437 p
->p_flags
|= P_TRACED
;
438 p
->p_oppid
= p
->p_pptr
->p_pid
;
439 lwkt_reltoken(&p
->p_token
);
444 /* security check done above */
445 p
->p_flags
|= P_TRACED
;
446 p
->p_oppid
= p
->p_pptr
->p_pid
;
447 proc_reparent(p
, curp
);
449 goto sendsig
; /* in PT_CONTINUE below */
454 /* Zero means do not send any signal */
455 if (data
< 0 || data
> _SIG_MAXSIG
) {
456 lwkt_reltoken(&p
->p_token
);
463 if (req
== PT_STEP
) {
464 if ((error
= ptrace_single_step (lp
))) {
466 lwkt_reltoken(&p
->p_token
);
472 if (addr
!= (void *)1) {
473 if ((error
= ptrace_set_pc (lp
,
474 (u_long
)(uintfptr_t
)addr
))) {
476 lwkt_reltoken(&p
->p_token
);
483 if (req
== PT_DETACH
) {
484 /* reset process parent */
485 if (p
->p_oppid
!= p
->p_pptr
->p_pid
) {
488 pp
= pfind(p
->p_oppid
);
490 proc_reparent(p
, pp
);
495 p
->p_flags
&= ~(P_TRACED
| P_WAITED
);
498 /* should we send SIGCHLD? */
503 * Deliver or queue signal. If the process is stopped
504 * force it to be SACTIVE again.
507 if (p
->p_stat
== SSTOP
) {
509 proc_unstop(p
, SSTOP
);
514 lwkt_reltoken(&p
->p_token
);
525 * NOTE! uio_offset represents the offset in the target
526 * process. The iov is in the current process (the guy
527 * making the ptrace call) so uio_td must be the current
528 * process (though for a SYSSPACE transfer it doesn't
532 /* write = 0 set above */
533 iov
.iov_base
= write
? (caddr_t
)&data
: (caddr_t
)&tmp
;
534 iov
.iov_len
= sizeof(int);
537 uio
.uio_offset
= (off_t
)(uintptr_t)addr
;
538 uio
.uio_resid
= sizeof(int);
539 uio
.uio_segflg
= UIO_SYSSPACE
;
540 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
541 uio
.uio_td
= curthread
;
542 error
= procfs_domem(curp
, lp
, NULL
, &uio
);
543 if (uio
.uio_resid
!= 0) {
545 * XXX procfs_domem() doesn't currently return ENOSPC,
546 * so I think write() can bogusly return 0.
547 * XXX what happens for short writes? We don't want
548 * to write partial data.
549 * XXX procfs_domem() returns EPERM for other invalid
550 * addresses. Convert this to EINVAL. Does this
551 * clobber returns of EPERM for other reasons?
553 if (error
== 0 || error
== ENOSPC
|| error
== EPERM
)
554 error
= EINVAL
; /* EOF */
558 lwkt_reltoken(&p
->p_token
);
564 * NOTE! uio_offset represents the offset in the target
565 * process. The iov is in the current process (the guy
566 * making the ptrace call) so uio_td must be the current
570 iov
.iov_base
= piod
->piod_addr
;
571 iov
.iov_len
= piod
->piod_len
;
574 uio
.uio_offset
= (off_t
)(uintptr_t)piod
->piod_offs
;
575 uio
.uio_resid
= piod
->piod_len
;
576 uio
.uio_segflg
= UIO_USERSPACE
;
577 uio
.uio_td
= curthread
;
578 switch (piod
->piod_op
) {
581 uio
.uio_rw
= UIO_READ
;
585 uio
.uio_rw
= UIO_WRITE
;
588 lwkt_reltoken(&p
->p_token
);
592 error
= procfs_domem(curp
, lp
, NULL
, &uio
);
593 piod
->piod_len
-= uio
.uio_resid
;
594 lwkt_reltoken(&p
->p_token
);
600 goto sendsig
; /* in PT_CONTINUE above */
606 #endif /* PT_SETREGS */
609 /* write = 0 above */
610 #endif /* PT_SETREGS */
611 #if defined(PT_SETREGS) || defined(PT_GETREGS)
612 if (!procfs_validregs(lp
)) {
613 lwkt_reltoken(&p
->p_token
);
618 iov
.iov_len
= sizeof(struct reg
);
622 uio
.uio_resid
= sizeof(struct reg
);
623 uio
.uio_segflg
= UIO_SYSSPACE
;
624 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
625 uio
.uio_td
= curthread
;
626 t
= procfs_doregs(curp
, lp
, NULL
, &uio
);
627 lwkt_reltoken(&p
->p_token
);
631 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
637 #endif /* PT_SETFPREGS */
640 /* write = 0 above */
641 #endif /* PT_SETFPREGS */
642 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
643 if (!procfs_validfpregs(lp
)) {
644 lwkt_reltoken(&p
->p_token
);
649 iov
.iov_len
= sizeof(struct fpreg
);
653 uio
.uio_resid
= sizeof(struct fpreg
);
654 uio
.uio_segflg
= UIO_SYSSPACE
;
655 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
656 uio
.uio_td
= curthread
;
657 t
= procfs_dofpregs(curp
, lp
, NULL
, &uio
);
658 lwkt_reltoken(&p
->p_token
);
662 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
668 #endif /* PT_SETDBREGS */
671 /* write = 0 above */
672 #endif /* PT_SETDBREGS */
673 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
674 if (!procfs_validdbregs(lp
)) {
675 lwkt_reltoken(&p
->p_token
);
680 iov
.iov_len
= sizeof(struct dbreg
);
684 uio
.uio_resid
= sizeof(struct dbreg
);
685 uio
.uio_segflg
= UIO_SYSSPACE
;
686 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
687 uio
.uio_td
= curthread
;
688 t
= procfs_dodbregs(curp
, lp
, NULL
, &uio
);
689 lwkt_reltoken(&p
->p_token
);
693 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
699 lwkt_reltoken(&p
->p_token
);
706 trace_req(struct proc
*p
)
714 * Stop a process because of a procfs event. Stay stopped until p->p_step
715 * is cleared (cleared by PIOCCONT in procfs).
720 stopevent(struct proc
*p
, unsigned int event
, unsigned int val
)
723 * Set event info. Recheck p_stops in case we are
724 * racing a close() on procfs.
726 spin_lock(&p
->p_spin
);
727 if ((p
->p_stops
& event
) == 0) {
728 spin_unlock(&p
->p_spin
);
734 tsleep_interlock(&p
->p_step
, 0);
735 spin_unlock(&p
->p_spin
);
738 * Wakeup any PIOCWAITing procs and wait for p_step to
743 tsleep(&p
->p_step
, PINTERLOCKED
, "stopevent", 0);
744 spin_lock(&p
->p_spin
);
745 if (p
->p_step
== 0) {
746 spin_unlock(&p
->p_spin
);
749 tsleep_interlock(&p
->p_step
, 0);
750 spin_unlock(&p
->p_spin
);