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 $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sysmsg.h>
40 #include <sys/vnode.h>
41 #include <sys/ptrace.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_page.h>
50 #include <vfs/procfs/procfs.h>
52 #include <sys/thread2.h>
53 #include <sys/spinlock2.h>
55 /* use the equivalent procfs code */
58 pread (struct proc
*procp
, unsigned int addr
, unsigned int *retval
)
65 int page_offset
; /* offset into page */
66 vm_offset_t pageno
; /* page number */
67 vm_map_entry_t out_entry
;
73 /* Map page into kernel space */
75 map
= &procp
->p_vmspace
->vm_map
;
77 page_offset
= addr
- trunc_page(addr
);
78 pageno
= trunc_page(addr
);
81 rv
= vm_map_lookup(&tmap
, pageno
, VM_PROT_READ
, &out_entry
,
82 &ba
, &pindex
, &pcount
, &out_prot
, &wflags
);
89 if (rv
!= KERN_SUCCESS
)
92 vm_map_lookup_done (tmap
, out_entry
, 0);
94 /* Find space in kernel_map for the page we're interested in */
95 rv
= vm_map_find (kernel_map
, object
, NULL
,
96 IDX_TO_OFF(pindex
), &kva
, PAGE_SIZE
,
98 VM_MAPTYPE_NORMAL
, VM_SUBSYS_PROC
,
99 VM_PROT_ALL
, VM_PROT_ALL
, 0);
102 vm_object_reference
XXX (object
);
105 rv
= vm_map_kernel_wiring(kernel_map
, kva
, kva
+ PAGE_SIZE
, 0);
108 bcopy ((caddr_t
)kva
+ page_offset
,
109 retval
, sizeof *retval
);
111 vm_map_remove (kernel_map
, kva
, kva
+ PAGE_SIZE
);
118 pwrite (struct proc
*procp
, unsigned int addr
, unsigned int datum
)
125 int page_offset
; /* offset into page */
126 vm_offset_t pageno
; /* page number */
127 vm_map_entry_t out_entry
;
132 boolean_t fix_prot
= 0;
134 /* Map page into kernel space */
136 map
= &procp
->p_vmspace
->vm_map
;
138 page_offset
= addr
- trunc_page(addr
);
139 pageno
= trunc_page(addr
);
142 * Check the permissions for the area we're interested in.
145 if (vm_map_check_protection (map
, pageno
, pageno
+ PAGE_SIZE
,
146 VM_PROT_WRITE
, FALSE
) == FALSE
) {
148 * If the page was not writable, we make it so.
149 * XXX It is possible a page may *not* be read/executable,
150 * if a process changes that!
153 /* The page isn't writable, so let's try making it so... */
154 if ((rv
= vm_map_protect (map
, pageno
, pageno
+ PAGE_SIZE
,
155 VM_PROT_ALL
, 0)) != KERN_SUCCESS
)
156 return EFAULT
; /* I guess... */
160 * Now we need to get the page. out_entry, out_prot, wflags, and
161 * single_use aren't used. One would think the vm code would be
162 * a *bit* nicer... We use tmap because vm_map_lookup() can
163 * change the map argument.
167 rv
= vm_map_lookup(&tmap
, pageno
, VM_PROT_WRITE
, &out_entry
,
168 &ba
, &pindex
, &pcount
, &out_prot
, &wflags
);
174 if (rv
!= KERN_SUCCESS
)
178 * Okay, we've got the page. Let's release tmap.
180 vm_map_lookup_done (tmap
, out_entry
, 0);
183 * Fault the page in...
185 rv
= vm_fault(map
, pageno
, VM_PROT_WRITE
|VM_PROT_READ
, FALSE
);
186 if (rv
!= KERN_SUCCESS
)
189 /* Find space in kernel_map for the page we're interested in */
190 rv
= vm_map_find (kernel_map
, object
, NULL
,
191 IDX_TO_OFF(pindex
), &kva
, PAGE_SIZE
,
193 VM_MAPTYPE_NORMAL
, VM_SUBSYS_PROC
,
194 VM_PROT_ALL
, VM_PROT_ALL
, 0);
196 vm_object_reference
XXX (object
);
199 rv
= vm_map_kernel_wiring(kernel_map
, kva
, kva
+ PAGE_SIZE
, 0);
201 bcopy (&datum
, (caddr_t
)kva
+ page_offset
, sizeof datum
);
203 vm_map_remove (kernel_map
, kva
, kva
+ PAGE_SIZE
);
207 vm_map_protect (map
, pageno
, pageno
+ PAGE_SIZE
,
208 VM_PROT_READ
|VM_PROT_EXECUTE
, 0);
214 * Process debugging system call.
219 sys_ptrace(struct sysmsg
*sysmsg
, const struct ptrace_args
*uap
)
221 struct proc
*p
= curproc
;
224 * XXX this obfuscation is to reduce stack usage, but the register
225 * structs may be too large to put on the stack anyway.
228 struct ptrace_io_desc piod
;
245 error
= copyin(uap
->addr
, &r
.reg
, sizeof r
.reg
);
248 error
= copyin(uap
->addr
, &r
.fpreg
, sizeof r
.fpreg
);
252 error
= copyin(uap
->addr
, &r
.dbreg
, sizeof r
.dbreg
);
256 error
= copyin(uap
->addr
, &r
.piod
, sizeof r
.piod
);
264 error
= kern_ptrace(p
, uap
->req
, uap
->pid
, addr
, uap
->data
,
265 &sysmsg
->sysmsg_result
);
271 (void)copyout(&r
.piod
, uap
->addr
, sizeof r
.piod
);
274 error
= copyout(&r
.reg
, uap
->addr
, sizeof r
.reg
);
277 error
= copyout(&r
.fpreg
, uap
->addr
, sizeof r
.fpreg
);
281 error
= copyout(&r
.dbreg
, uap
->addr
, sizeof r
.dbreg
);
290 kern_ptrace(struct proc
*curp
, int req
, pid_t pid
, void *addr
,
297 struct ptrace_io_desc
*piod
;
303 if (req
== PT_TRACE_ME
) {
307 if ((p
= pfind(pid
)) == NULL
)
310 if (!PRISON_CHECK(curp
->p_ucred
, p
->p_ucred
)) {
314 if (p
->p_flags
& P_SYSTEM
) {
319 lwkt_gettoken(&p
->p_token
);
320 /* Can't trace a process that's currently exec'ing. */
321 if ((p
->p_flags
& P_INEXEC
) != 0) {
322 lwkt_reltoken(&p
->p_token
);
337 if (p
->p_pid
== curp
->p_pid
) {
338 lwkt_reltoken(&p
->p_token
);
344 if (p
->p_flags
& P_TRACED
) {
345 lwkt_reltoken(&p
->p_token
);
350 if (curp
->p_flags
& P_TRACED
)
351 for (pp
= curp
->p_pptr
; pp
!= NULL
; pp
= pp
->p_pptr
)
353 lwkt_reltoken(&p
->p_token
);
358 /* not owned by you, has done setuid (unless you're root) */
359 if ((p
->p_ucred
->cr_ruid
!= curp
->p_ucred
->cr_ruid
) ||
360 (p
->p_flags
& P_SUGID
)) {
361 if ((error
= priv_check_cred(curp
->p_ucred
, PRIV_ROOT
, 0)) != 0) {
362 lwkt_reltoken(&p
->p_token
);
368 /* can't trace init when securelevel > 0 */
369 if (securelevel
> 0 && p
->p_pid
== 1) {
370 lwkt_reltoken(&p
->p_token
);
405 /* not being traced... */
406 if ((p
->p_flags
& P_TRACED
) == 0) {
407 lwkt_reltoken(&p
->p_token
);
412 /* not being traced by YOU */
413 if (p
->p_pptr
!= curp
) {
414 lwkt_reltoken(&p
->p_token
);
419 /* not currently stopped */
420 if (p
->p_stat
!= SSTOP
||
421 (p
->p_flags
& P_WAITED
) == 0) {
422 lwkt_reltoken(&p
->p_token
);
431 lwkt_reltoken(&p
->p_token
);
437 lp
= FIRST_LWP_IN_PROC(p
);
439 lwkt_reltoken(&p
->p_token
);
446 * Single step fixup ala procfs
452 * Actually do the requests
459 /* set my trace flag and "owner" so it can read/write me */
460 p
->p_flags
|= P_TRACED
;
461 p
->p_oppid
= p
->p_pptr
->p_pid
;
462 lwkt_reltoken(&p
->p_token
);
467 /* security check done above */
468 p
->p_flags
|= P_TRACED
;
469 p
->p_oppid
= p
->p_pptr
->p_pid
;
470 proc_reparent(p
, curp
);
472 goto sendsig
; /* in PT_CONTINUE below */
477 /* Zero means do not send any signal */
478 if (data
< 0 || data
>= _SIG_MAXSIG
) {
479 lwkt_reltoken(&p
->p_token
);
486 if (req
== PT_STEP
) {
487 if ((error
= ptrace_single_step (lp
))) {
489 lwkt_reltoken(&p
->p_token
);
495 if (addr
!= (void *)1) {
496 if ((error
= ptrace_set_pc (lp
, (u_long
)addr
))) {
498 lwkt_reltoken(&p
->p_token
);
505 if (req
== PT_DETACH
) {
506 /* reset process parent */
507 if (p
->p_oppid
!= p
->p_pptr
->p_pid
) {
510 pp
= pfind(p
->p_oppid
);
512 proc_reparent(p
, pp
);
517 p
->p_flags
&= ~(P_TRACED
| P_WAITED
);
520 /* should we send SIGCHLD? */
525 * Deliver or queue signal. If the process is stopped
526 * force it to be SACTIVE again.
529 if (p
->p_stat
== SSTOP
) {
531 proc_unstop(p
, SSTOP
);
536 lwkt_reltoken(&p
->p_token
);
547 * NOTE! uio_offset represents the offset in the target
548 * process. The iov is in the current process (the guy
549 * making the ptrace call) so uio_td must be the current
550 * process (though for a SYSSPACE transfer it doesn't
554 /* write = 0 set above */
555 iov
.iov_base
= write
? (caddr_t
)&data
: (caddr_t
)&tmp
;
556 iov
.iov_len
= sizeof(int);
559 uio
.uio_offset
= (off_t
)(uintptr_t)addr
;
560 uio
.uio_resid
= sizeof(int);
561 uio
.uio_segflg
= UIO_SYSSPACE
;
562 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
563 uio
.uio_td
= curthread
;
564 error
= procfs_domem(curp
, lp
, NULL
, &uio
);
565 if (uio
.uio_resid
!= 0) {
567 * XXX procfs_domem() doesn't currently return ENOSPC,
568 * so I think write() can bogusly return 0.
569 * XXX what happens for short writes? We don't want
570 * to write partial data.
571 * XXX procfs_domem() returns EPERM for other invalid
572 * addresses. Convert this to EINVAL. Does this
573 * clobber returns of EPERM for other reasons?
575 if (error
== 0 || error
== ENOSPC
|| error
== EPERM
)
576 error
= EINVAL
; /* EOF */
580 lwkt_reltoken(&p
->p_token
);
586 * NOTE! uio_offset represents the offset in the target
587 * process. The iov is in the current process (the guy
588 * making the ptrace call) so uio_td must be the current
592 iov
.iov_base
= piod
->piod_addr
;
593 iov
.iov_len
= piod
->piod_len
;
596 uio
.uio_offset
= (off_t
)(uintptr_t)piod
->piod_offs
;
597 uio
.uio_resid
= piod
->piod_len
;
598 uio
.uio_segflg
= UIO_USERSPACE
;
599 uio
.uio_td
= curthread
;
600 switch (piod
->piod_op
) {
603 uio
.uio_rw
= UIO_READ
;
607 uio
.uio_rw
= UIO_WRITE
;
610 lwkt_reltoken(&p
->p_token
);
614 error
= procfs_domem(curp
, lp
, NULL
, &uio
);
615 piod
->piod_len
-= uio
.uio_resid
;
616 lwkt_reltoken(&p
->p_token
);
622 goto sendsig
; /* in PT_CONTINUE above */
628 #endif /* PT_SETREGS */
631 /* write = 0 above */
632 #endif /* PT_SETREGS */
633 #if defined(PT_SETREGS) || defined(PT_GETREGS)
634 if (!procfs_validregs(lp
)) {
635 lwkt_reltoken(&p
->p_token
);
640 iov
.iov_len
= sizeof(struct reg
);
644 uio
.uio_resid
= sizeof(struct reg
);
645 uio
.uio_segflg
= UIO_SYSSPACE
;
646 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
647 uio
.uio_td
= curthread
;
648 t
= procfs_doregs(curp
, lp
, NULL
, &uio
);
649 lwkt_reltoken(&p
->p_token
);
653 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
659 #endif /* PT_SETFPREGS */
662 /* write = 0 above */
663 #endif /* PT_SETFPREGS */
664 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
665 if (!procfs_validfpregs(lp
)) {
666 lwkt_reltoken(&p
->p_token
);
671 iov
.iov_len
= sizeof(struct fpreg
);
675 uio
.uio_resid
= sizeof(struct fpreg
);
676 uio
.uio_segflg
= UIO_SYSSPACE
;
677 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
678 uio
.uio_td
= curthread
;
679 t
= procfs_dofpregs(curp
, lp
, NULL
, &uio
);
680 lwkt_reltoken(&p
->p_token
);
684 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
690 #endif /* PT_SETDBREGS */
693 /* write = 0 above */
694 #endif /* PT_SETDBREGS */
695 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
696 if (!procfs_validdbregs(lp
)) {
697 lwkt_reltoken(&p
->p_token
);
702 iov
.iov_len
= sizeof(struct dbreg
);
706 uio
.uio_resid
= sizeof(struct dbreg
);
707 uio
.uio_segflg
= UIO_SYSSPACE
;
708 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
709 uio
.uio_td
= curthread
;
710 t
= procfs_dodbregs(curp
, lp
, NULL
, &uio
);
711 lwkt_reltoken(&p
->p_token
);
715 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
721 lwkt_reltoken(&p
->p_token
);
728 trace_req(struct proc
*p
)
736 * Stop a process because of a procfs event. Stay stopped until p->p_step
737 * is cleared (cleared by PIOCCONT in procfs).
742 stopevent(struct proc
*p
, unsigned int event
, unsigned int val
)
745 * Set event info. Recheck p_stops in case we are
746 * racing a close() on procfs.
748 spin_lock(&p
->p_spin
);
749 if ((p
->p_stops
& event
) == 0) {
750 spin_unlock(&p
->p_spin
);
756 tsleep_interlock(&p
->p_step
, 0);
757 spin_unlock(&p
->p_spin
);
760 * Wakeup any PIOCWAITing procs and wait for p_step to
765 tsleep(&p
->p_step
, PINTERLOCKED
, "stopevent", 0);
766 spin_lock(&p
->p_spin
);
767 if (p
->p_step
== 0) {
768 spin_unlock(&p
->p_spin
);
771 tsleep_interlock(&p
->p_step
, 0);
772 spin_unlock(&p
->p_spin
);