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 error = caps_priv_check(curp->p_ucred,
362 SYSCAP_RESTRICTEDROOT);
364 lwkt_reltoken(&p->p_token);
370 /* can't trace init when securelevel > 0 */
371 if (securelevel > 0 && p->p_pid == 1) {
372 lwkt_reltoken(&p->p_token);
407 /* not being traced... */
408 if ((p->p_flags & P_TRACED) == 0) {
409 lwkt_reltoken(&p->p_token);
414 /* not being traced by YOU */
415 if (p->p_pptr != curp) {
416 lwkt_reltoken(&p->p_token);
421 /* not currently stopped */
422 if (p->p_stat != SSTOP ||
423 (p->p_flags & P_WAITED) == 0) {
424 lwkt_reltoken(&p->p_token);
433 lwkt_reltoken(&p->p_token);
439 lp = FIRST_LWP_IN_PROC(p);
441 lwkt_reltoken(&p->p_token);
448 * Single step fixup ala procfs
454 * Actually do the requests
461 /* set my trace flag and "owner" so it can read/write me */
462 p->p_flags |= P_TRACED;
463 p->p_oppid = p->p_pptr->p_pid;
464 lwkt_reltoken(&p->p_token);
469 /* security check done above */
470 p->p_flags |= P_TRACED;
471 p->p_oppid = p->p_pptr->p_pid;
472 proc_reparent(p, curp);
474 goto sendsig; /* in PT_CONTINUE below */
479 /* Zero means do not send any signal */
480 if (data < 0 || data >= _SIG_MAXSIG) {
481 lwkt_reltoken(&p->p_token);
488 if (req == PT_STEP) {
489 if ((error = ptrace_single_step (lp))) {
491 lwkt_reltoken(&p->p_token);
497 if (addr != (void *)1) {
498 if ((error = ptrace_set_pc (lp, (u_long)addr))) {
500 lwkt_reltoken(&p->p_token);
507 if (req == PT_DETACH) {
508 /* reset process parent */
509 if (p->p_oppid != p->p_pptr->p_pid) {
512 pp = pfind(p->p_oppid);
514 proc_reparent(p, pp);
519 p->p_flags &= ~(P_TRACED | P_WAITED);
522 /* should we send SIGCHLD? */
527 * Deliver or queue signal. If the process is stopped
528 * force it to be SACTIVE again.
531 if (p->p_stat == SSTOP) {
533 proc_unstop(p, SSTOP);
538 lwkt_reltoken(&p->p_token);
549 * NOTE! uio_offset represents the offset in the target
550 * process. The iov is in the current process (the guy
551 * making the ptrace call) so uio_td must be the current
552 * process (though for a SYSSPACE transfer it doesn't
556 /* write = 0 set above */
557 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
558 iov.iov_len = sizeof(int);
561 uio.uio_offset = (off_t)(uintptr_t)addr;
562 uio.uio_resid = sizeof(int);
563 uio.uio_segflg = UIO_SYSSPACE;
564 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
565 uio.uio_td = curthread;
566 error = procfs_domem(curp, lp, NULL, &uio);
567 if (uio.uio_resid != 0) {
569 * XXX procfs_domem() doesn't currently return ENOSPC,
570 * so I think write() can bogusly return 0.
571 * XXX what happens for short writes? We don't want
572 * to write partial data.
573 * XXX procfs_domem() returns EPERM for other invalid
574 * addresses. Convert this to EINVAL. Does this
575 * clobber returns of EPERM for other reasons?
577 if (error == 0 || error == ENOSPC || error == EPERM)
578 error = EINVAL; /* EOF */
582 lwkt_reltoken(&p->p_token);
588 * NOTE! uio_offset represents the offset in the target
589 * process. The iov is in the current process (the guy
590 * making the ptrace call) so uio_td must be the current
594 iov.iov_base = piod->piod_addr;
595 iov.iov_len = piod->piod_len;
598 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
599 uio.uio_resid = piod->piod_len;
600 uio.uio_segflg = UIO_USERSPACE;
601 uio.uio_td = curthread;
602 switch (piod->piod_op) {
605 uio.uio_rw = UIO_READ;
609 uio.uio_rw = UIO_WRITE;
612 lwkt_reltoken(&p->p_token);
616 error = procfs_domem(curp, lp, NULL, &uio);
617 piod->piod_len -= uio.uio_resid;
618 lwkt_reltoken(&p->p_token);
624 goto sendsig; /* in PT_CONTINUE above */
630 #endif /* PT_SETREGS */
633 /* write = 0 above */
634 #endif /* PT_SETREGS */
635 #if defined(PT_SETREGS) || defined(PT_GETREGS)
636 if (!procfs_validregs(lp)) {
637 lwkt_reltoken(&p->p_token);
642 iov.iov_len = sizeof(struct reg);
646 uio.uio_resid = sizeof(struct reg);
647 uio.uio_segflg = UIO_SYSSPACE;
648 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
649 uio.uio_td = curthread;
650 t = procfs_doregs(curp, lp, NULL, &uio);
651 lwkt_reltoken(&p->p_token);
655 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
661 #endif /* PT_SETFPREGS */
664 /* write = 0 above */
665 #endif /* PT_SETFPREGS */
666 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
667 if (!procfs_validfpregs(lp)) {
668 lwkt_reltoken(&p->p_token);
673 iov.iov_len = sizeof(struct fpreg);
677 uio.uio_resid = sizeof(struct fpreg);
678 uio.uio_segflg = UIO_SYSSPACE;
679 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
680 uio.uio_td = curthread;
681 t = procfs_dofpregs(curp, lp, NULL, &uio);
682 lwkt_reltoken(&p->p_token);
686 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
692 #endif /* PT_SETDBREGS */
695 /* write = 0 above */
696 #endif /* PT_SETDBREGS */
697 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
698 if (!procfs_validdbregs(lp)) {
699 lwkt_reltoken(&p->p_token);
704 iov.iov_len = sizeof(struct dbreg);
708 uio.uio_resid = sizeof(struct dbreg);
709 uio.uio_segflg = UIO_SYSSPACE;
710 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
711 uio.uio_td = curthread;
712 t = procfs_dodbregs(curp, lp, NULL, &uio);
713 lwkt_reltoken(&p->p_token);
717 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
723 lwkt_reltoken(&p->p_token);
730 trace_req(struct proc *p)
738 * Stop a process because of a procfs event. Stay stopped until p->p_step
739 * is cleared (cleared by PIOCCONT in procfs).
744 stopevent(struct proc *p, unsigned int event, unsigned int val)
747 * Set event info. Recheck p_stops in case we are
748 * racing a close() on procfs.
750 spin_lock(&p->p_spin);
751 if ((p->p_stops & event) == 0) {
752 spin_unlock(&p->p_spin);
758 tsleep_interlock(&p->p_step, 0);
759 spin_unlock(&p->p_spin);
762 * Wakeup any PIOCWAITing procs and wait for p_step to
767 tsleep(&p->p_step, PINTERLOCKED, "stopevent", 0);
768 spin_lock(&p->p_spin);
769 if (p->p_step == 0) {
770 spin_unlock(&p->p_spin);
773 tsleep_interlock(&p->p_step, 0);
774 spin_unlock(&p->p_spin);