libstand.3: add HAMMER and MS-DOS filesystems and improve mark-up
[dragonfly.git] / sys / kern / sys_process.c
blob4c0d8257c0e505ae548ea5e3279a8e4dcfdb46a5
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/priv.h>
40 #include <sys/vnode.h>
41 #include <sys/ptrace.h>
42 #include <sys/reg.h>
43 #include <sys/lock.h>
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_page.h>
50 #include <sys/user.h>
51 #include <vfs/procfs/procfs.h>
52 #include <sys/thread2.h>
54 /* use the equivalent procfs code */
55 #if 0
56 static int
57 pread (struct proc *procp, unsigned int addr, unsigned int *retval) {
58 int rv;
59 vm_map_t map, tmap;
60 vm_object_t object;
61 vm_offset_t kva = 0;
62 int page_offset; /* offset into page */
63 vm_offset_t pageno; /* page number */
64 vm_map_entry_t out_entry;
65 vm_prot_t out_prot;
66 boolean_t wired;
67 vm_pindex_t pindex;
69 /* Map page into kernel space */
71 map = &procp->p_vmspace->vm_map;
73 page_offset = addr - trunc_page(addr);
74 pageno = trunc_page(addr);
76 tmap = map;
77 rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
78 &object, &pindex, &out_prot, &wired);
80 if (rv != KERN_SUCCESS)
81 return EINVAL;
83 vm_map_lookup_done (tmap, out_entry, 0);
85 /* Find space in kernel_map for the page we're interested in */
86 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
87 &kva, PAGE_SIZE,
88 0,
89 VM_MAPTYPE_NORMAL,
90 VM_PROT_ALL, VM_PROT_ALL,
91 0);
93 if (!rv) {
94 vm_object_reference (object);
96 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
97 if (!rv) {
98 *retval = 0;
99 bcopy ((caddr_t)kva + page_offset,
100 retval, sizeof *retval);
102 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
105 return rv;
108 static int
109 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
110 int rv;
111 vm_map_t map, tmap;
112 vm_object_t object;
113 vm_offset_t kva = 0;
114 int page_offset; /* offset into page */
115 vm_offset_t pageno; /* page number */
116 vm_map_entry_t out_entry;
117 vm_prot_t out_prot;
118 boolean_t wired;
119 vm_pindex_t pindex;
120 boolean_t fix_prot = 0;
122 /* Map page into kernel space */
124 map = &procp->p_vmspace->vm_map;
126 page_offset = addr - trunc_page(addr);
127 pageno = trunc_page(addr);
130 * Check the permissions for the area we're interested in.
133 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
134 VM_PROT_WRITE) == FALSE) {
136 * If the page was not writable, we make it so.
137 * XXX It is possible a page may *not* be read/executable,
138 * if a process changes that!
140 fix_prot = 1;
141 /* The page isn't writable, so let's try making it so... */
142 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
143 VM_PROT_ALL, 0)) != KERN_SUCCESS)
144 return EFAULT; /* I guess... */
148 * Now we need to get the page. out_entry, out_prot, wired, and
149 * single_use aren't used. One would think the vm code would be
150 * a *bit* nicer... We use tmap because vm_map_lookup() can
151 * change the map argument.
154 tmap = map;
155 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
156 &object, &pindex, &out_prot, &wired);
157 if (rv != KERN_SUCCESS) {
158 return EINVAL;
162 * Okay, we've got the page. Let's release tmap.
165 vm_map_lookup_done (tmap, out_entry, 0);
168 * Fault the page in...
171 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
172 if (rv != KERN_SUCCESS)
173 return EFAULT;
175 /* Find space in kernel_map for the page we're interested in */
176 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
177 &kva, PAGE_SIZE,
179 VM_MAPTYPE_NORMAL,
180 VM_PROT_ALL, VM_PROT_ALL,
182 if (!rv) {
183 vm_object_reference (object);
185 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
186 if (!rv) {
187 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
189 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
192 if (fix_prot)
193 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
194 VM_PROT_READ|VM_PROT_EXECUTE, 0);
195 return rv;
197 #endif
200 * Process debugging system call.
203 sys_ptrace(struct ptrace_args *uap)
205 struct proc *p = curproc;
208 * XXX this obfuscation is to reduce stack usage, but the register
209 * structs may be too large to put on the stack anyway.
211 union {
212 struct ptrace_io_desc piod;
213 struct dbreg dbreg;
214 struct fpreg fpreg;
215 struct reg reg;
216 } r;
217 void *addr;
218 int error = 0;
220 addr = &r;
221 switch (uap->req) {
222 case PT_GETREGS:
223 case PT_GETFPREGS:
224 #ifdef PT_GETDBREGS
225 case PT_GETDBREGS:
226 #endif
227 break;
228 case PT_SETREGS:
229 error = copyin(uap->addr, &r.reg, sizeof r.reg);
230 break;
231 case PT_SETFPREGS:
232 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
233 break;
234 #ifdef PT_SETDBREGS
235 case PT_SETDBREGS:
236 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
237 break;
238 #endif
239 case PT_IO:
240 error = copyin(uap->addr, &r.piod, sizeof r.piod);
241 break;
242 default:
243 addr = uap->addr;
245 if (error)
246 return (error);
248 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
249 &uap->sysmsg_result);
250 if (error)
251 return (error);
253 switch (uap->req) {
254 case PT_IO:
255 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
256 break;
257 case PT_GETREGS:
258 error = copyout(&r.reg, uap->addr, sizeof r.reg);
259 break;
260 case PT_GETFPREGS:
261 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
262 break;
263 #ifdef PT_GETDBREGS
264 case PT_GETDBREGS:
265 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
266 break;
267 #endif
270 return (error);
274 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data, int *res)
276 struct proc *p, *pp;
277 struct lwp *lp;
278 struct iovec iov;
279 struct uio uio;
280 struct ptrace_io_desc *piod;
281 int error = 0;
282 int write, tmp;
284 write = 0;
285 if (req == PT_TRACE_ME) {
286 p = curp;
287 } else {
288 if ((p = pfind(pid)) == NULL)
289 return ESRCH;
291 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
292 return (ESRCH);
294 /* Can't trace a process that's currently exec'ing. */
295 if ((p->p_flag & P_INEXEC) != 0)
296 return EAGAIN;
299 * Permissions check
301 switch (req) {
302 case PT_TRACE_ME:
303 /* Always legal. */
304 break;
306 case PT_ATTACH:
307 /* Self */
308 if (p->p_pid == curp->p_pid)
309 return EINVAL;
311 /* Already traced */
312 if (p->p_flag & P_TRACED)
313 return EBUSY;
315 if (curp->p_flag & P_TRACED)
316 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
317 if (pp == p)
318 return (EINVAL);
320 /* not owned by you, has done setuid (unless you're root) */
321 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
322 (p->p_flag & P_SUGID)) {
323 if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0)
324 return error;
327 /* can't trace init when securelevel > 0 */
328 if (securelevel > 0 && p->p_pid == 1)
329 return EPERM;
331 /* OK */
332 break;
334 case PT_READ_I:
335 case PT_READ_D:
336 case PT_WRITE_I:
337 case PT_WRITE_D:
338 case PT_IO:
339 case PT_CONTINUE:
340 case PT_KILL:
341 case PT_STEP:
342 case PT_DETACH:
343 #ifdef PT_GETREGS
344 case PT_GETREGS:
345 #endif
346 #ifdef PT_SETREGS
347 case PT_SETREGS:
348 #endif
349 #ifdef PT_GETFPREGS
350 case PT_GETFPREGS:
351 #endif
352 #ifdef PT_SETFPREGS
353 case PT_SETFPREGS:
354 #endif
355 #ifdef PT_GETDBREGS
356 case PT_GETDBREGS:
357 #endif
358 #ifdef PT_SETDBREGS
359 case PT_SETDBREGS:
360 #endif
361 /* not being traced... */
362 if ((p->p_flag & P_TRACED) == 0)
363 return EPERM;
365 /* not being traced by YOU */
366 if (p->p_pptr != curp)
367 return EBUSY;
369 /* not currently stopped */
370 if (p->p_stat != SSTOP ||
371 (p->p_flag & P_WAITED) == 0) {
372 return EBUSY;
375 /* OK */
376 break;
378 default:
379 return EINVAL;
382 /* XXX lwp */
383 lp = FIRST_LWP_IN_PROC(p);
384 #ifdef FIX_SSTEP
386 * Single step fixup ala procfs
388 FIX_SSTEP(lp);
389 #endif
392 * Actually do the requests
395 *res = 0;
397 switch (req) {
398 case PT_TRACE_ME:
399 /* set my trace flag and "owner" so it can read/write me */
400 p->p_flag |= P_TRACED;
401 p->p_oppid = p->p_pptr->p_pid;
402 return 0;
404 case PT_ATTACH:
405 /* security check done above */
406 p->p_flag |= P_TRACED;
407 p->p_oppid = p->p_pptr->p_pid;
408 if (p->p_pptr != curp)
409 proc_reparent(p, curp);
410 data = SIGSTOP;
411 goto sendsig; /* in PT_CONTINUE below */
413 case PT_STEP:
414 case PT_CONTINUE:
415 case PT_DETACH:
416 /* Zero means do not send any signal */
417 if (data < 0 || data > _SIG_MAXSIG)
418 return EINVAL;
420 LWPHOLD(lp);
422 if (req == PT_STEP) {
423 if ((error = ptrace_single_step (lp))) {
424 LWPRELE(lp);
425 return error;
429 if (addr != (void *)1) {
430 if ((error = ptrace_set_pc (lp,
431 (u_long)(uintfptr_t)addr))) {
432 LWPRELE(lp);
433 return error;
436 LWPRELE(lp);
438 if (req == PT_DETACH) {
439 /* reset process parent */
440 if (p->p_oppid != p->p_pptr->p_pid) {
441 struct proc *pp;
443 pp = pfind(p->p_oppid);
444 proc_reparent(p, pp ? pp : initproc);
447 p->p_flag &= ~(P_TRACED | P_WAITED);
448 p->p_oppid = 0;
450 /* should we send SIGCHLD? */
453 sendsig:
455 * Deliver or queue signal. If the process is stopped
456 * force it to be SACTIVE again.
458 crit_enter();
459 if (p->p_stat == SSTOP) {
460 p->p_xstat = data;
461 lp->lwp_flag |= LWP_BREAKTSLEEP;
462 proc_unstop(p);
463 } else if (data) {
464 ksignal(p, data);
466 crit_exit();
467 return 0;
469 case PT_WRITE_I:
470 case PT_WRITE_D:
471 write = 1;
472 /* fallthrough */
473 case PT_READ_I:
474 case PT_READ_D:
476 * NOTE! uio_offset represents the offset in the target
477 * process. The iov is in the current process (the guy
478 * making the ptrace call) so uio_td must be the current
479 * process (though for a SYSSPACE transfer it doesn't
480 * really matter).
482 tmp = 0;
483 /* write = 0 set above */
484 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
485 iov.iov_len = sizeof(int);
486 uio.uio_iov = &iov;
487 uio.uio_iovcnt = 1;
488 uio.uio_offset = (off_t)(uintptr_t)addr;
489 uio.uio_resid = sizeof(int);
490 uio.uio_segflg = UIO_SYSSPACE;
491 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
492 uio.uio_td = curthread;
493 error = procfs_domem(curp, lp, NULL, &uio);
494 if (uio.uio_resid != 0) {
496 * XXX procfs_domem() doesn't currently return ENOSPC,
497 * so I think write() can bogusly return 0.
498 * XXX what happens for short writes? We don't want
499 * to write partial data.
500 * XXX procfs_domem() returns EPERM for other invalid
501 * addresses. Convert this to EINVAL. Does this
502 * clobber returns of EPERM for other reasons?
504 if (error == 0 || error == ENOSPC || error == EPERM)
505 error = EINVAL; /* EOF */
507 if (!write)
508 *res = tmp;
509 return (error);
511 case PT_IO:
513 * NOTE! uio_offset represents the offset in the target
514 * process. The iov is in the current process (the guy
515 * making the ptrace call) so uio_td must be the current
516 * process.
518 piod = addr;
519 iov.iov_base = piod->piod_addr;
520 iov.iov_len = piod->piod_len;
521 uio.uio_iov = &iov;
522 uio.uio_iovcnt = 1;
523 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
524 uio.uio_resid = piod->piod_len;
525 uio.uio_segflg = UIO_USERSPACE;
526 uio.uio_td = curthread;
527 switch (piod->piod_op) {
528 case PIOD_READ_D:
529 case PIOD_READ_I:
530 uio.uio_rw = UIO_READ;
531 break;
532 case PIOD_WRITE_D:
533 case PIOD_WRITE_I:
534 uio.uio_rw = UIO_WRITE;
535 break;
536 default:
537 return (EINVAL);
539 error = procfs_domem(curp, lp, NULL, &uio);
540 piod->piod_len -= uio.uio_resid;
541 return (error);
543 case PT_KILL:
544 data = SIGKILL;
545 goto sendsig; /* in PT_CONTINUE above */
547 #ifdef PT_SETREGS
548 case PT_SETREGS:
549 write = 1;
550 /* fallthrough */
551 #endif /* PT_SETREGS */
552 #ifdef PT_GETREGS
553 case PT_GETREGS:
554 /* write = 0 above */
555 #endif /* PT_SETREGS */
556 #if defined(PT_SETREGS) || defined(PT_GETREGS)
557 if (!procfs_validregs(lp)) /* no P_SYSTEM procs please */
558 return EINVAL;
559 else {
560 iov.iov_base = addr;
561 iov.iov_len = sizeof(struct reg);
562 uio.uio_iov = &iov;
563 uio.uio_iovcnt = 1;
564 uio.uio_offset = 0;
565 uio.uio_resid = sizeof(struct reg);
566 uio.uio_segflg = UIO_SYSSPACE;
567 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
568 uio.uio_td = curthread;
569 return (procfs_doregs(curp, lp, NULL, &uio));
571 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
573 #ifdef PT_SETFPREGS
574 case PT_SETFPREGS:
575 write = 1;
576 /* fallthrough */
577 #endif /* PT_SETFPREGS */
578 #ifdef PT_GETFPREGS
579 case PT_GETFPREGS:
580 /* write = 0 above */
581 #endif /* PT_SETFPREGS */
582 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
583 if (!procfs_validfpregs(lp)) /* no P_SYSTEM procs please */
584 return EINVAL;
585 else {
586 iov.iov_base = addr;
587 iov.iov_len = sizeof(struct fpreg);
588 uio.uio_iov = &iov;
589 uio.uio_iovcnt = 1;
590 uio.uio_offset = 0;
591 uio.uio_resid = sizeof(struct fpreg);
592 uio.uio_segflg = UIO_SYSSPACE;
593 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
594 uio.uio_td = curthread;
595 return (procfs_dofpregs(curp, lp, NULL, &uio));
597 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
599 #ifdef PT_SETDBREGS
600 case PT_SETDBREGS:
601 write = 1;
602 /* fallthrough */
603 #endif /* PT_SETDBREGS */
604 #ifdef PT_GETDBREGS
605 case PT_GETDBREGS:
606 /* write = 0 above */
607 #endif /* PT_SETDBREGS */
608 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
609 if (!procfs_validdbregs(lp)) /* no P_SYSTEM procs please */
610 return EINVAL;
611 else {
612 iov.iov_base = addr;
613 iov.iov_len = sizeof(struct dbreg);
614 uio.uio_iov = &iov;
615 uio.uio_iovcnt = 1;
616 uio.uio_offset = 0;
617 uio.uio_resid = sizeof(struct dbreg);
618 uio.uio_segflg = UIO_SYSSPACE;
619 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
620 uio.uio_td = curthread;
621 return (procfs_dodbregs(curp, lp, NULL, &uio));
623 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
625 default:
626 break;
629 return 0;
633 trace_req(struct proc *p)
635 return 1;
639 * stopevent()
641 * Stop a process because of a procfs event. Stay stopped until p->p_step
642 * is cleared (cleared by PIOCCONT in procfs).
644 * MPSAFE
646 void
647 stopevent(struct proc *p, unsigned int event, unsigned int val)
649 p->p_step = 1;
651 do {
652 crit_enter();
653 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
654 p->p_xstat = val;
655 p->p_stype = event; /* Which event caused the stop? */
656 tsleep(&p->p_step, 0, "stopevent", 0);
657 crit_exit();
658 } while (p->p_step);