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/sysproto.h>
38 #include <sys/vnode.h>
39 #include <sys/ptrace.h>
41 #include <machine/reg.h>
45 #include <vm/vm_map.h>
46 #include <vm/vm_page.h>
49 #include <miscfs/procfs/procfs.h>
51 /* use the equivalent procfs code */
54 pread (struct proc *procp, unsigned int addr, unsigned int *retval) {
59 int page_offset; /* offset into page */
60 vm_offset_t pageno; /* page number */
61 vm_map_entry_t out_entry;
66 /* Map page into kernel space */
68 map = &procp->p_vmspace->vm_map;
70 page_offset = addr - trunc_page(addr);
71 pageno = trunc_page(addr);
74 rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
75 &object, &pindex, &out_prot, &wired);
77 if (rv != KERN_SUCCESS)
80 vm_map_lookup_done (tmap, out_entry);
82 /* Find space in kernel_map for the page we're interested in */
83 rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex),
84 &kva, PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0);
87 vm_object_reference (object);
89 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0);
92 bcopy ((caddr_t)kva + page_offset,
93 retval, sizeof *retval);
95 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE);
102 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
107 int page_offset; /* offset into page */
108 vm_offset_t pageno; /* page number */
109 vm_map_entry_t out_entry;
113 boolean_t fix_prot = 0;
115 /* Map page into kernel space */
117 map = &procp->p_vmspace->vm_map;
119 page_offset = addr - trunc_page(addr);
120 pageno = trunc_page(addr);
123 * Check the permissions for the area we're interested in.
126 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
127 VM_PROT_WRITE) == FALSE) {
129 * If the page was not writable, we make it so.
130 * XXX It is possible a page may *not* be read/executable,
131 * if a process changes that!
134 /* The page isn't writable, so let's try making it so... */
135 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
136 VM_PROT_ALL, 0)) != KERN_SUCCESS)
137 return EFAULT; /* I guess... */
141 * Now we need to get the page. out_entry, out_prot, wired, and
142 * single_use aren't used. One would think the vm code would be
143 * a *bit* nicer... We use tmap because vm_map_lookup() can
144 * change the map argument.
148 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
149 &object, &pindex, &out_prot, &wired);
150 if (rv != KERN_SUCCESS) {
155 * Okay, we've got the page. Let's release tmap.
158 vm_map_lookup_done (tmap, out_entry);
161 * Fault the page in...
164 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
165 if (rv != KERN_SUCCESS)
168 /* Find space in kernel_map for the page we're interested in */
169 rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex),
171 VM_PROT_ALL, VM_PROT_ALL, 0);
173 vm_object_reference (object);
175 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0);
177 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
179 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE);
183 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
184 VM_PROT_READ|VM_PROT_EXECUTE, 0);
190 * Process debugging system call.
192 #ifndef _SYS_SYSPROTO_H_
202 ptrace(struct proc *p, struct ptrace_args *uap)
205 * XXX this obfuscation is to reduce stack usage, but the register
206 * structs may be too large to put on the stack anyway.
209 struct ptrace_io_desc piod;
226 error = copyin(uap->addr, &r.reg, sizeof r.reg);
229 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
233 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
237 error = copyin(uap->addr, &r.piod, sizeof r.piod);
245 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data);
251 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
254 error = copyout(&r.reg, uap->addr, sizeof r.reg);
257 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
261 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
270 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data)
275 struct ptrace_io_desc *piod;
280 if (req == PT_TRACE_ME)
283 if ((p = pfind(pid)) == NULL)
286 if (!PRISON_CHECK(curp, p))
289 /* Can't trace a process that's currently exec'ing. */
290 if ((p->p_flag & P_INEXEC) != 0)
303 if (p->p_pid == curp->p_pid)
307 if (p->p_flag & P_TRACED)
310 if (curp->p_flag & P_TRACED)
311 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
315 /* not owned by you, has done setuid (unless you're root) */
316 if ((p->p_cred->p_ruid != curp->p_cred->p_ruid) ||
317 (p->p_flag & P_SUGID)) {
318 if ((error = suser(curp)) != 0)
322 /* can't trace init when securelevel > 0 */
323 if (securelevel > 0 && p->p_pid == 1)
356 /* not being traced... */
357 if ((p->p_flag & P_TRACED) == 0)
360 /* not being traced by YOU */
361 if (p->p_pptr != curp)
364 /* not currently stopped */
365 if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0)
377 * Single step fixup ala procfs
383 * Actually do the requests
386 curp->p_retval[0] = 0;
390 /* set my trace flag and "owner" so it can read/write me */
391 p->p_flag |= P_TRACED;
392 p->p_oppid = p->p_pptr->p_pid;
396 /* security check done above */
397 p->p_flag |= P_TRACED;
398 p->p_oppid = p->p_pptr->p_pid;
399 if (p->p_pptr != curp)
400 proc_reparent(p, curp);
402 goto sendsig; /* in PT_CONTINUE below */
407 if ((req != PT_STEP) && ((unsigned)data > _SIG_MAXSIG))
412 if (req == PT_STEP) {
413 if ((error = ptrace_single_step (p))) {
419 if (addr != (void *)1) {
420 if ((error = ptrace_set_pc (p,
421 (u_long)(uintfptr_t)addr))) {
428 if (req == PT_DETACH) {
429 /* reset process parent */
430 if (p->p_oppid != p->p_pptr->p_pid) {
433 pp = pfind(p->p_oppid);
434 proc_reparent(p, pp ? pp : initproc);
437 p->p_flag &= ~(P_TRACED | P_WAITED);
440 /* should we send SIGCHLD? */
444 /* deliver or queue signal */
446 if (p->p_stat == SSTOP) {
462 /* write = 0 set above */
463 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
464 iov.iov_len = sizeof(int);
467 uio.uio_offset = (off_t)(uintptr_t)addr;
468 uio.uio_resid = sizeof(int);
469 uio.uio_segflg = UIO_SYSSPACE;
470 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
472 error = procfs_domem(curp, p, NULL, &uio);
473 if (uio.uio_resid != 0) {
475 * XXX procfs_domem() doesn't currently return ENOSPC,
476 * so I think write() can bogusly return 0.
477 * XXX what happens for short writes? We don't want
478 * to write partial data.
479 * XXX procfs_domem() returns EPERM for other invalid
480 * addresses. Convert this to EINVAL. Does this
481 * clobber returns of EPERM for other reasons?
483 if (error == 0 || error == ENOSPC || error == EPERM)
484 error = EINVAL; /* EOF */
487 curp->p_retval[0] = tmp;
492 iov.iov_base = piod->piod_addr;
493 iov.iov_len = piod->piod_len;
496 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
497 uio.uio_resid = piod->piod_len;
498 uio.uio_segflg = UIO_USERSPACE;
500 switch (piod->piod_op) {
503 uio.uio_rw = UIO_READ;
507 uio.uio_rw = UIO_WRITE;
512 error = procfs_domem(curp, p, NULL, &uio);
513 piod->piod_len -= uio.uio_resid;
518 goto sendsig; /* in PT_CONTINUE above */
524 #endif /* PT_SETREGS */
527 /* write = 0 above */
528 #endif /* PT_SETREGS */
529 #if defined(PT_SETREGS) || defined(PT_GETREGS)
530 if (!procfs_validregs(p)) /* no P_SYSTEM procs please */
534 iov.iov_len = sizeof(struct reg);
538 uio.uio_resid = sizeof(struct reg);
539 uio.uio_segflg = UIO_SYSSPACE;
540 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
541 uio.uio_procp = curp;
542 return (procfs_doregs(curp, p, NULL, &uio));
544 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
550 #endif /* PT_SETFPREGS */
553 /* write = 0 above */
554 #endif /* PT_SETFPREGS */
555 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
556 if (!procfs_validfpregs(p)) /* no P_SYSTEM procs please */
560 iov.iov_len = sizeof(struct fpreg);
564 uio.uio_resid = sizeof(struct fpreg);
565 uio.uio_segflg = UIO_SYSSPACE;
566 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
567 uio.uio_procp = curp;
568 return (procfs_dofpregs(curp, p, NULL, &uio));
570 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
576 #endif /* PT_SETDBREGS */
579 /* write = 0 above */
580 #endif /* PT_SETDBREGS */
581 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
582 if (!procfs_validdbregs(p)) /* no P_SYSTEM procs please */
586 iov.iov_len = sizeof(struct dbreg);
590 uio.uio_resid = sizeof(struct dbreg);
591 uio.uio_segflg = UIO_SYSSPACE;
592 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
593 uio.uio_procp = curp;
594 return (procfs_dodbregs(curp, p, NULL, &uio));
596 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
614 * Stop a process because of a procfs event;
615 * stay stopped until p->p_step is cleared
616 * (cleared by PIOCCONT in procfs).
620 stopevent(struct proc *p, unsigned int event, unsigned int val) {
625 p->p_stype = event; /* Which event caused the stop? */
626 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
627 tsleep(&p->p_step, PWAIT, "stopevent", 0);