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, &wired);
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, IDX_TO_OFF(pindex),
92 VM_PROT_ALL, VM_PROT_ALL,
96 vm_object_reference (object);
98 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
101 bcopy ((caddr_t)kva + page_offset,
102 retval, sizeof *retval);
104 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
111 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
116 int page_offset; /* offset into page */
117 vm_offset_t pageno; /* page number */
118 vm_map_entry_t out_entry;
122 boolean_t fix_prot = 0;
124 /* Map page into kernel space */
126 map = &procp->p_vmspace->vm_map;
128 page_offset = addr - trunc_page(addr);
129 pageno = trunc_page(addr);
132 * Check the permissions for the area we're interested in.
135 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
136 VM_PROT_WRITE, FALSE) == FALSE) {
138 * If the page was not writable, we make it so.
139 * XXX It is possible a page may *not* be read/executable,
140 * if a process changes that!
143 /* The page isn't writable, so let's try making it so... */
144 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
145 VM_PROT_ALL, 0)) != KERN_SUCCESS)
146 return EFAULT; /* I guess... */
150 * Now we need to get the page. out_entry, out_prot, wired, and
151 * single_use aren't used. One would think the vm code would be
152 * a *bit* nicer... We use tmap because vm_map_lookup() can
153 * change the map argument.
157 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
158 &object, &pindex, &out_prot, &wired);
159 if (rv != KERN_SUCCESS) {
164 * Okay, we've got the page. Let's release tmap.
167 vm_map_lookup_done (tmap, out_entry, 0);
170 * Fault the page in...
173 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
174 if (rv != KERN_SUCCESS)
177 /* Find space in kernel_map for the page we're interested in */
178 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
180 PAGE_SIZE, PAGE_SIZE,
181 0, VM_MAPTYPE_NORMAL,
182 VM_PROT_ALL, VM_PROT_ALL,
185 vm_object_reference (object);
187 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
189 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
191 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
195 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
196 VM_PROT_READ|VM_PROT_EXECUTE, 0);
202 * Process debugging system call.
207 sys_ptrace(struct ptrace_args *uap)
209 struct proc *p = curproc;
212 * XXX this obfuscation is to reduce stack usage, but the register
213 * structs may be too large to put on the stack anyway.
216 struct ptrace_io_desc piod;
233 error = copyin(uap->addr, &r.reg, sizeof r.reg);
236 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
240 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
244 error = copyin(uap->addr, &r.piod, sizeof r.piod);
252 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
253 &uap->sysmsg_result);
259 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
262 error = copyout(&r.reg, uap->addr, sizeof r.reg);
265 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
269 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
278 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data, int *res)
284 struct ptrace_io_desc *piod;
289 lwkt_gettoken(&proc_token);
292 if (req == PT_TRACE_ME) {
295 if ((p = pfind(pid)) == NULL) {
296 lwkt_reltoken(&proc_token);
300 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
301 lwkt_reltoken(&proc_token);
305 /* Can't trace a process that's currently exec'ing. */
306 if ((p->p_flag & P_INEXEC) != 0) {
307 lwkt_reltoken(&proc_token);
321 if (p->p_pid == curp->p_pid) {
322 lwkt_reltoken(&proc_token);
327 if (p->p_flag & P_TRACED) {
328 lwkt_reltoken(&proc_token);
332 if (curp->p_flag & P_TRACED)
333 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
335 lwkt_reltoken(&proc_token);
339 /* not owned by you, has done setuid (unless you're root) */
340 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
341 (p->p_flag & P_SUGID)) {
342 if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0) {
343 lwkt_reltoken(&proc_token);
348 /* can't trace init when securelevel > 0 */
349 if (securelevel > 0 && p->p_pid == 1) {
350 lwkt_reltoken(&proc_token);
384 /* not being traced... */
385 if ((p->p_flag & P_TRACED) == 0) {
386 lwkt_reltoken(&proc_token);
390 /* not being traced by YOU */
391 if (p->p_pptr != curp) {
392 lwkt_reltoken(&proc_token);
396 /* not currently stopped */
397 if (p->p_stat != SSTOP ||
398 (p->p_flag & P_WAITED) == 0) {
399 lwkt_reltoken(&proc_token);
407 lwkt_reltoken(&proc_token);
412 lp = FIRST_LWP_IN_PROC(p);
415 * Single step fixup ala procfs
421 * Actually do the requests
428 /* set my trace flag and "owner" so it can read/write me */
429 p->p_flag |= P_TRACED;
430 p->p_oppid = p->p_pptr->p_pid;
431 lwkt_reltoken(&proc_token);
435 /* security check done above */
436 p->p_flag |= P_TRACED;
437 p->p_oppid = p->p_pptr->p_pid;
438 if (p->p_pptr != curp)
439 proc_reparent(p, curp);
441 goto sendsig; /* in PT_CONTINUE below */
446 /* Zero means do not send any signal */
447 if (data < 0 || data > _SIG_MAXSIG) {
448 lwkt_reltoken(&proc_token);
454 if (req == PT_STEP) {
455 if ((error = ptrace_single_step (lp))) {
457 lwkt_reltoken(&proc_token);
462 if (addr != (void *)1) {
463 if ((error = ptrace_set_pc (lp,
464 (u_long)(uintfptr_t)addr))) {
466 lwkt_reltoken(&proc_token);
472 if (req == PT_DETACH) {
473 /* reset process parent */
474 if (p->p_oppid != p->p_pptr->p_pid) {
477 pp = pfind(p->p_oppid);
478 proc_reparent(p, pp ? pp : initproc);
481 p->p_flag &= ~(P_TRACED | P_WAITED);
484 /* should we send SIGCHLD? */
489 * Deliver or queue signal. If the process is stopped
490 * force it to be SACTIVE again.
493 if (p->p_stat == SSTOP) {
495 lp->lwp_flag |= LWP_BREAKTSLEEP;
501 lwkt_reltoken(&proc_token);
511 * NOTE! uio_offset represents the offset in the target
512 * process. The iov is in the current process (the guy
513 * making the ptrace call) so uio_td must be the current
514 * process (though for a SYSSPACE transfer it doesn't
518 /* write = 0 set above */
519 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
520 iov.iov_len = sizeof(int);
523 uio.uio_offset = (off_t)(uintptr_t)addr;
524 uio.uio_resid = sizeof(int);
525 uio.uio_segflg = UIO_SYSSPACE;
526 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
527 uio.uio_td = curthread;
528 error = procfs_domem(curp, lp, NULL, &uio);
529 if (uio.uio_resid != 0) {
531 * XXX procfs_domem() doesn't currently return ENOSPC,
532 * so I think write() can bogusly return 0.
533 * XXX what happens for short writes? We don't want
534 * to write partial data.
535 * XXX procfs_domem() returns EPERM for other invalid
536 * addresses. Convert this to EINVAL. Does this
537 * clobber returns of EPERM for other reasons?
539 if (error == 0 || error == ENOSPC || error == EPERM)
540 error = EINVAL; /* EOF */
544 lwkt_reltoken(&proc_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
555 iov.iov_base = piod->piod_addr;
556 iov.iov_len = piod->piod_len;
559 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
560 uio.uio_resid = piod->piod_len;
561 uio.uio_segflg = UIO_USERSPACE;
562 uio.uio_td = curthread;
563 switch (piod->piod_op) {
566 uio.uio_rw = UIO_READ;
570 uio.uio_rw = UIO_WRITE;
573 lwkt_reltoken(&proc_token);
576 error = procfs_domem(curp, lp, NULL, &uio);
577 piod->piod_len -= uio.uio_resid;
578 lwkt_reltoken(&proc_token);
583 goto sendsig; /* in PT_CONTINUE above */
589 #endif /* PT_SETREGS */
592 /* write = 0 above */
593 #endif /* PT_SETREGS */
594 #if defined(PT_SETREGS) || defined(PT_GETREGS)
595 if (!procfs_validregs(lp)) { /* no P_SYSTEM procs please */
596 lwkt_reltoken(&proc_token);
600 iov.iov_len = sizeof(struct reg);
604 uio.uio_resid = sizeof(struct reg);
605 uio.uio_segflg = UIO_SYSSPACE;
606 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
607 uio.uio_td = curthread;
608 t = procfs_doregs(curp, lp, NULL, &uio);
609 lwkt_reltoken(&proc_token);
612 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
618 #endif /* PT_SETFPREGS */
621 /* write = 0 above */
622 #endif /* PT_SETFPREGS */
623 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
624 if (!procfs_validfpregs(lp)) { /* no P_SYSTEM procs please */
625 lwkt_reltoken(&proc_token);
629 iov.iov_len = sizeof(struct fpreg);
633 uio.uio_resid = sizeof(struct fpreg);
634 uio.uio_segflg = UIO_SYSSPACE;
635 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
636 uio.uio_td = curthread;
637 t = procfs_dofpregs(curp, lp, NULL, &uio);
638 lwkt_reltoken(&proc_token);
641 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
647 #endif /* PT_SETDBREGS */
650 /* write = 0 above */
651 #endif /* PT_SETDBREGS */
652 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
653 if (!procfs_validdbregs(lp)) { /* no P_SYSTEM procs please */
654 lwkt_reltoken(&proc_token);
658 iov.iov_len = sizeof(struct dbreg);
662 uio.uio_resid = sizeof(struct dbreg);
663 uio.uio_segflg = UIO_SYSSPACE;
664 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
665 uio.uio_td = curthread;
666 t = procfs_dodbregs(curp, lp, NULL, &uio);
667 lwkt_reltoken(&proc_token);
670 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
676 lwkt_reltoken(&proc_token);
681 trace_req(struct proc *p)
689 * Stop a process because of a procfs event. Stay stopped until p->p_step
690 * is cleared (cleared by PIOCCONT in procfs).
695 stopevent(struct proc *p, unsigned int event, unsigned int val)
698 * Set event info. Recheck p_stops in case we are
699 * racing a close() on procfs.
701 spin_lock_wr(&p->p_spin);
702 if ((p->p_stops & event) == 0) {
703 spin_unlock_wr(&p->p_spin);
709 tsleep_interlock(&p->p_step, 0);
710 spin_unlock_wr(&p->p_spin);
713 * Wakeup any PIOCWAITing procs and wait for p_step to
718 tsleep(&p->p_step, PINTERLOCKED, "stopevent", 0);
719 spin_lock_wr(&p->p_spin);
720 if (p->p_step == 0) {
721 spin_unlock_wr(&p->p_spin);
724 tsleep_interlock(&p->p_step, 0);
725 spin_unlock_wr(&p->p_spin);