MFC r1.27:
[dragonfly.git] / sys / kern / sys_process.c
blob4427ac4c19ac2501522e4f383c02af913f747619
1 /*
2 * Copyright (c) 1994, Sean Eric Fagan
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
29 * SUCH DAMAGE.
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>
38 #include <sys/proc.h>
39 #include <sys/vnode.h>
40 #include <sys/ptrace.h>
41 #include <sys/reg.h>
42 #include <sys/lock.h>
44 #include <vm/vm.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_page.h>
49 #include <sys/user.h>
50 #include <vfs/procfs/procfs.h>
51 #include <sys/thread2.h>
53 /* use the equivalent procfs code */
54 #if 0
55 static int
56 pread (struct proc *procp, unsigned int addr, unsigned int *retval) {
57 int rv;
58 vm_map_t map, tmap;
59 vm_object_t object;
60 vm_offset_t kva = 0;
61 int page_offset; /* offset into page */
62 vm_offset_t pageno; /* page number */
63 vm_map_entry_t out_entry;
64 vm_prot_t out_prot;
65 boolean_t wired;
66 vm_pindex_t pindex;
68 /* Map page into kernel space */
70 map = &procp->p_vmspace->vm_map;
72 page_offset = addr - trunc_page(addr);
73 pageno = trunc_page(addr);
75 tmap = map;
76 rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
77 &object, &pindex, &out_prot, &wired);
79 if (rv != KERN_SUCCESS)
80 return EINVAL;
82 vm_map_lookup_done (tmap, out_entry, 0);
84 /* Find space in kernel_map for the page we're interested in */
85 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
86 &kva, PAGE_SIZE,
87 0,
88 VM_MAPTYPE_NORMAL,
89 VM_PROT_ALL, VM_PROT_ALL,
90 0);
92 if (!rv) {
93 vm_object_reference (object);
95 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
96 if (!rv) {
97 *retval = 0;
98 bcopy ((caddr_t)kva + page_offset,
99 retval, sizeof *retval);
101 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
104 return rv;
107 static int
108 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
109 int rv;
110 vm_map_t map, tmap;
111 vm_object_t object;
112 vm_offset_t kva = 0;
113 int page_offset; /* offset into page */
114 vm_offset_t pageno; /* page number */
115 vm_map_entry_t out_entry;
116 vm_prot_t out_prot;
117 boolean_t wired;
118 vm_pindex_t pindex;
119 boolean_t fix_prot = 0;
121 /* Map page into kernel space */
123 map = &procp->p_vmspace->vm_map;
125 page_offset = addr - trunc_page(addr);
126 pageno = trunc_page(addr);
129 * Check the permissions for the area we're interested in.
132 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
133 VM_PROT_WRITE) == FALSE) {
135 * If the page was not writable, we make it so.
136 * XXX It is possible a page may *not* be read/executable,
137 * if a process changes that!
139 fix_prot = 1;
140 /* The page isn't writable, so let's try making it so... */
141 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
142 VM_PROT_ALL, 0)) != KERN_SUCCESS)
143 return EFAULT; /* I guess... */
147 * Now we need to get the page. out_entry, out_prot, wired, and
148 * single_use aren't used. One would think the vm code would be
149 * a *bit* nicer... We use tmap because vm_map_lookup() can
150 * change the map argument.
153 tmap = map;
154 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
155 &object, &pindex, &out_prot, &wired);
156 if (rv != KERN_SUCCESS) {
157 return EINVAL;
161 * Okay, we've got the page. Let's release tmap.
164 vm_map_lookup_done (tmap, out_entry, 0);
167 * Fault the page in...
170 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
171 if (rv != KERN_SUCCESS)
172 return EFAULT;
174 /* Find space in kernel_map for the page we're interested in */
175 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
176 &kva, PAGE_SIZE,
178 VM_MAPTYPE_NORMAL,
179 VM_PROT_ALL, VM_PROT_ALL,
181 if (!rv) {
182 vm_object_reference (object);
184 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
185 if (!rv) {
186 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
188 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
191 if (fix_prot)
192 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
193 VM_PROT_READ|VM_PROT_EXECUTE, 0);
194 return rv;
196 #endif
199 * 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.
210 union {
211 struct ptrace_io_desc piod;
212 struct dbreg dbreg;
213 struct fpreg fpreg;
214 struct reg reg;
215 } r;
216 void *addr;
217 int error = 0;
219 addr = &r;
220 switch (uap->req) {
221 case PT_GETREGS:
222 case PT_GETFPREGS:
223 #ifdef PT_GETDBREGS
224 case PT_GETDBREGS:
225 #endif
226 break;
227 case PT_SETREGS:
228 error = copyin(uap->addr, &r.reg, sizeof r.reg);
229 break;
230 case PT_SETFPREGS:
231 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
232 break;
233 #ifdef PT_SETDBREGS
234 case PT_SETDBREGS:
235 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
236 break;
237 #endif
238 case PT_IO:
239 error = copyin(uap->addr, &r.piod, sizeof r.piod);
240 break;
241 default:
242 addr = uap->addr;
244 if (error)
245 return (error);
247 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
248 &uap->sysmsg_result);
249 if (error)
250 return (error);
252 switch (uap->req) {
253 case PT_IO:
254 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
255 break;
256 case PT_GETREGS:
257 error = copyout(&r.reg, uap->addr, sizeof r.reg);
258 break;
259 case PT_GETFPREGS:
260 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
261 break;
262 #ifdef PT_GETDBREGS
263 case PT_GETDBREGS:
264 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
265 break;
266 #endif
269 return (error);
273 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data, int *res)
275 struct proc *p, *pp;
276 struct lwp *lp;
277 struct iovec iov;
278 struct uio uio;
279 struct ptrace_io_desc *piod;
280 int error = 0;
281 int write, tmp;
283 write = 0;
284 if (req == PT_TRACE_ME) {
285 p = curp;
286 } else {
287 if ((p = pfind(pid)) == NULL)
288 return ESRCH;
290 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
291 return (ESRCH);
293 /* Can't trace a process that's currently exec'ing. */
294 if ((p->p_flag & P_INEXEC) != 0)
295 return EAGAIN;
298 * Permissions check
300 switch (req) {
301 case PT_TRACE_ME:
302 /* Always legal. */
303 break;
305 case PT_ATTACH:
306 /* Self */
307 if (p->p_pid == curp->p_pid)
308 return EINVAL;
310 /* Already traced */
311 if (p->p_flag & P_TRACED)
312 return EBUSY;
314 if (curp->p_flag & P_TRACED)
315 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
316 if (pp == p)
317 return (EINVAL);
319 /* not owned by you, has done setuid (unless you're root) */
320 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
321 (p->p_flag & P_SUGID)) {
322 if ((error = suser_cred(curp->p_ucred, 0)) != 0)
323 return error;
326 /* can't trace init when securelevel > 0 */
327 if (securelevel > 0 && p->p_pid == 1)
328 return EPERM;
330 /* OK */
331 break;
333 case PT_READ_I:
334 case PT_READ_D:
335 case PT_WRITE_I:
336 case PT_WRITE_D:
337 case PT_IO:
338 case PT_CONTINUE:
339 case PT_KILL:
340 case PT_STEP:
341 case PT_DETACH:
342 #ifdef PT_GETREGS
343 case PT_GETREGS:
344 #endif
345 #ifdef PT_SETREGS
346 case PT_SETREGS:
347 #endif
348 #ifdef PT_GETFPREGS
349 case PT_GETFPREGS:
350 #endif
351 #ifdef PT_SETFPREGS
352 case PT_SETFPREGS:
353 #endif
354 #ifdef PT_GETDBREGS
355 case PT_GETDBREGS:
356 #endif
357 #ifdef PT_SETDBREGS
358 case PT_SETDBREGS:
359 #endif
360 /* not being traced... */
361 if ((p->p_flag & P_TRACED) == 0)
362 return EPERM;
364 /* not being traced by YOU */
365 if (p->p_pptr != curp)
366 return EBUSY;
368 /* not currently stopped */
369 if (p->p_stat != SSTOP ||
370 (p->p_flag & P_WAITED) == 0) {
371 return EBUSY;
374 /* OK */
375 break;
377 default:
378 return EINVAL;
381 /* XXX lwp */
382 lp = FIRST_LWP_IN_PROC(p);
383 #ifdef FIX_SSTEP
385 * Single step fixup ala procfs
387 FIX_SSTEP(lp);
388 #endif
391 * Actually do the requests
394 *res = 0;
396 switch (req) {
397 case PT_TRACE_ME:
398 /* set my trace flag and "owner" so it can read/write me */
399 p->p_flag |= P_TRACED;
400 p->p_oppid = p->p_pptr->p_pid;
401 return 0;
403 case PT_ATTACH:
404 /* security check done above */
405 p->p_flag |= P_TRACED;
406 p->p_oppid = p->p_pptr->p_pid;
407 if (p->p_pptr != curp)
408 proc_reparent(p, curp);
409 data = SIGSTOP;
410 goto sendsig; /* in PT_CONTINUE below */
412 case PT_STEP:
413 case PT_CONTINUE:
414 case PT_DETACH:
415 /* Zero means do not send any signal */
416 if (data < 0 || data > _SIG_MAXSIG)
417 return EINVAL;
419 LWPHOLD(lp);
421 if (req == PT_STEP) {
422 if ((error = ptrace_single_step (lp))) {
423 LWPRELE(lp);
424 return error;
428 if (addr != (void *)1) {
429 if ((error = ptrace_set_pc (lp,
430 (u_long)(uintfptr_t)addr))) {
431 LWPRELE(lp);
432 return error;
435 LWPRELE(lp);
437 if (req == PT_DETACH) {
438 /* reset process parent */
439 if (p->p_oppid != p->p_pptr->p_pid) {
440 struct proc *pp;
442 pp = pfind(p->p_oppid);
443 proc_reparent(p, pp ? pp : initproc);
446 p->p_flag &= ~(P_TRACED | P_WAITED);
447 p->p_oppid = 0;
449 /* should we send SIGCHLD? */
452 sendsig:
454 * Deliver or queue signal. If the process is stopped
455 * force it to be SACTIVE again.
457 crit_enter();
458 if (p->p_stat == SSTOP) {
459 p->p_xstat = data;
460 lp->lwp_flag |= LWP_BREAKTSLEEP;
461 proc_unstop(p);
462 } else if (data) {
463 ksignal(p, data);
465 crit_exit();
466 return 0;
468 case PT_WRITE_I:
469 case PT_WRITE_D:
470 write = 1;
471 /* fallthrough */
472 case PT_READ_I:
473 case PT_READ_D:
475 * NOTE! uio_offset represents the offset in the target
476 * process. The iov is in the current process (the guy
477 * making the ptrace call) so uio_td must be the current
478 * process (though for a SYSSPACE transfer it doesn't
479 * really matter).
481 tmp = 0;
482 /* write = 0 set above */
483 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
484 iov.iov_len = sizeof(int);
485 uio.uio_iov = &iov;
486 uio.uio_iovcnt = 1;
487 uio.uio_offset = (off_t)(uintptr_t)addr;
488 uio.uio_resid = sizeof(int);
489 uio.uio_segflg = UIO_SYSSPACE;
490 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
491 uio.uio_td = curthread;
492 error = procfs_domem(curp, lp, NULL, &uio);
493 if (uio.uio_resid != 0) {
495 * XXX procfs_domem() doesn't currently return ENOSPC,
496 * so I think write() can bogusly return 0.
497 * XXX what happens for short writes? We don't want
498 * to write partial data.
499 * XXX procfs_domem() returns EPERM for other invalid
500 * addresses. Convert this to EINVAL. Does this
501 * clobber returns of EPERM for other reasons?
503 if (error == 0 || error == ENOSPC || error == EPERM)
504 error = EINVAL; /* EOF */
506 if (!write)
507 *res = tmp;
508 return (error);
510 case PT_IO:
512 * NOTE! uio_offset represents the offset in the target
513 * process. The iov is in the current process (the guy
514 * making the ptrace call) so uio_td must be the current
515 * process.
517 piod = addr;
518 iov.iov_base = piod->piod_addr;
519 iov.iov_len = piod->piod_len;
520 uio.uio_iov = &iov;
521 uio.uio_iovcnt = 1;
522 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
523 uio.uio_resid = piod->piod_len;
524 uio.uio_segflg = UIO_USERSPACE;
525 uio.uio_td = curthread;
526 switch (piod->piod_op) {
527 case PIOD_READ_D:
528 case PIOD_READ_I:
529 uio.uio_rw = UIO_READ;
530 break;
531 case PIOD_WRITE_D:
532 case PIOD_WRITE_I:
533 uio.uio_rw = UIO_WRITE;
534 break;
535 default:
536 return (EINVAL);
538 error = procfs_domem(curp, lp, NULL, &uio);
539 piod->piod_len -= uio.uio_resid;
540 return (error);
542 case PT_KILL:
543 data = SIGKILL;
544 goto sendsig; /* in PT_CONTINUE above */
546 #ifdef PT_SETREGS
547 case PT_SETREGS:
548 write = 1;
549 /* fallthrough */
550 #endif /* PT_SETREGS */
551 #ifdef PT_GETREGS
552 case PT_GETREGS:
553 /* write = 0 above */
554 #endif /* PT_SETREGS */
555 #if defined(PT_SETREGS) || defined(PT_GETREGS)
556 if (!procfs_validregs(lp)) /* no P_SYSTEM procs please */
557 return EINVAL;
558 else {
559 iov.iov_base = addr;
560 iov.iov_len = sizeof(struct reg);
561 uio.uio_iov = &iov;
562 uio.uio_iovcnt = 1;
563 uio.uio_offset = 0;
564 uio.uio_resid = sizeof(struct reg);
565 uio.uio_segflg = UIO_SYSSPACE;
566 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
567 uio.uio_td = curthread;
568 return (procfs_doregs(curp, lp, NULL, &uio));
570 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
572 #ifdef PT_SETFPREGS
573 case PT_SETFPREGS:
574 write = 1;
575 /* fallthrough */
576 #endif /* PT_SETFPREGS */
577 #ifdef PT_GETFPREGS
578 case PT_GETFPREGS:
579 /* write = 0 above */
580 #endif /* PT_SETFPREGS */
581 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
582 if (!procfs_validfpregs(lp)) /* no P_SYSTEM procs please */
583 return EINVAL;
584 else {
585 iov.iov_base = addr;
586 iov.iov_len = sizeof(struct fpreg);
587 uio.uio_iov = &iov;
588 uio.uio_iovcnt = 1;
589 uio.uio_offset = 0;
590 uio.uio_resid = sizeof(struct fpreg);
591 uio.uio_segflg = UIO_SYSSPACE;
592 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
593 uio.uio_td = curthread;
594 return (procfs_dofpregs(curp, lp, NULL, &uio));
596 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
598 #ifdef PT_SETDBREGS
599 case PT_SETDBREGS:
600 write = 1;
601 /* fallthrough */
602 #endif /* PT_SETDBREGS */
603 #ifdef PT_GETDBREGS
604 case PT_GETDBREGS:
605 /* write = 0 above */
606 #endif /* PT_SETDBREGS */
607 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
608 if (!procfs_validdbregs(lp)) /* no P_SYSTEM procs please */
609 return EINVAL;
610 else {
611 iov.iov_base = addr;
612 iov.iov_len = sizeof(struct dbreg);
613 uio.uio_iov = &iov;
614 uio.uio_iovcnt = 1;
615 uio.uio_offset = 0;
616 uio.uio_resid = sizeof(struct dbreg);
617 uio.uio_segflg = UIO_SYSSPACE;
618 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
619 uio.uio_td = curthread;
620 return (procfs_dodbregs(curp, lp, NULL, &uio));
622 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
624 default:
625 break;
628 return 0;
632 trace_req(struct proc *p)
634 return 1;
638 * stopevent()
640 * Stop a process because of a procfs event. Stay stopped until p->p_step
641 * is cleared (cleared by PIOCCONT in procfs).
643 * MPSAFE
645 void
646 stopevent(struct proc *p, unsigned int event, unsigned int val)
648 p->p_step = 1;
650 do {
651 crit_enter();
652 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
653 p->p_xstat = val;
654 p->p_stype = event; /* Which event caused the stop? */
655 tsleep(&p->p_step, 0, "stopevent", 0);
656 crit_exit();
657 } while (p->p_step);