Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / kern / sys_process.c
1 /*
2  * Copyright (c) 1994, Sean Eric Fagan
3  * All rights reserved.
4  *
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.
18  *
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.
30  *
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.2 2003/06/17 04:28:41 dillon Exp $
33  */
34
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
42 #include <machine/reg.h>
43 #include <vm/vm.h>
44 #include <sys/lock.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_page.h>
48
49 #include <sys/user.h>
50 #include <miscfs/procfs/procfs.h>
51
52 /* use the equivalent procfs code */
53 #if 0
54 static int
55 pread (struct proc *procp, unsigned int addr, unsigned int *retval) {
56         int             rv;
57         vm_map_t        map, tmap;
58         vm_object_t     object;
59         vm_offset_t     kva = 0;
60         int             page_offset;    /* offset into page */
61         vm_offset_t     pageno;         /* page number */
62         vm_map_entry_t  out_entry;
63         vm_prot_t       out_prot;
64         boolean_t       wired;
65         vm_pindex_t     pindex;
66
67         /* Map page into kernel space */
68
69         map = &procp->p_vmspace->vm_map;
70
71         page_offset = addr - trunc_page(addr);
72         pageno = trunc_page(addr);
73
74         tmap = map;
75         rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
76                 &object, &pindex, &out_prot, &wired);
77
78         if (rv != KERN_SUCCESS)
79                 return EINVAL;
80
81         vm_map_lookup_done (tmap, out_entry);
82
83         /* Find space in kernel_map for the page we're interested in */
84         rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex),
85                 &kva, PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0);
86
87         if (!rv) {
88                 vm_object_reference (object);
89
90                 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0);
91                 if (!rv) {
92                         *retval = 0;
93                         bcopy ((caddr_t)kva + page_offset,
94                                retval, sizeof *retval);
95                 }
96                 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE);
97         }
98
99         return rv;
100 }
101
102 static int
103 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
104         int             rv;
105         vm_map_t        map, tmap;
106         vm_object_t     object;
107         vm_offset_t     kva = 0;
108         int             page_offset;    /* offset into page */
109         vm_offset_t     pageno;         /* page number */
110         vm_map_entry_t  out_entry;
111         vm_prot_t       out_prot;
112         boolean_t       wired;
113         vm_pindex_t     pindex;
114         boolean_t       fix_prot = 0;
115
116         /* Map page into kernel space */
117
118         map = &procp->p_vmspace->vm_map;
119
120         page_offset = addr - trunc_page(addr);
121         pageno = trunc_page(addr);
122
123         /*
124          * Check the permissions for the area we're interested in.
125          */
126
127         if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
128                 VM_PROT_WRITE) == FALSE) {
129                 /*
130                  * If the page was not writable, we make it so.
131                  * XXX It is possible a page may *not* be read/executable,
132                  * if a process changes that!
133                  */
134                 fix_prot = 1;
135                 /* The page isn't writable, so let's try making it so... */
136                 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
137                         VM_PROT_ALL, 0)) != KERN_SUCCESS)
138                   return EFAULT;        /* I guess... */
139         }
140
141         /*
142          * Now we need to get the page.  out_entry, out_prot, wired, and
143          * single_use aren't used.  One would think the vm code would be
144          * a *bit* nicer...  We use tmap because vm_map_lookup() can
145          * change the map argument.
146          */
147
148         tmap = map;
149         rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
150                 &object, &pindex, &out_prot, &wired);
151         if (rv != KERN_SUCCESS) {
152                 return EINVAL;
153         }
154
155         /*
156          * Okay, we've got the page.  Let's release tmap.
157          */
158
159         vm_map_lookup_done (tmap, out_entry);
160
161         /*
162          * Fault the page in...
163          */
164
165         rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
166         if (rv != KERN_SUCCESS)
167                 return EFAULT;
168
169         /* Find space in kernel_map for the page we're interested in */
170         rv = vm_map_find (kernel_map, object, IDX_TO_OFF(pindex),
171                 &kva, PAGE_SIZE, 0,
172                 VM_PROT_ALL, VM_PROT_ALL, 0);
173         if (!rv) {
174                 vm_object_reference (object);
175
176                 rv = vm_map_pageable (kernel_map, kva, kva + PAGE_SIZE, 0);
177                 if (!rv) {
178                   bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
179                 }
180                 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE);
181         }
182
183         if (fix_prot)
184                 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
185                         VM_PROT_READ|VM_PROT_EXECUTE, 0);
186         return rv;
187 }
188 #endif
189
190 /*
191  * Process debugging system call.
192  */
193 #ifndef _SYS_SYSPROTO_H_
194 struct ptrace_args {
195         int     req;
196         pid_t   pid;
197         caddr_t addr;
198         int     data;
199 };
200 #endif
201
202 int
203 ptrace(struct proc *p, struct ptrace_args *uap)
204 {
205         /*
206          * XXX this obfuscation is to reduce stack usage, but the register
207          * structs may be too large to put on the stack anyway.
208          */
209         union {
210                 struct ptrace_io_desc piod;
211                 struct dbreg dbreg;
212                 struct fpreg fpreg;
213                 struct reg reg;
214         } r;
215         void *addr;
216         int error = 0;
217
218         addr = &r;
219         switch (uap->req) {
220         case PT_GETREGS:
221         case PT_GETFPREGS:
222 #ifdef PT_GETDBREGS
223         case PT_GETDBREGS:
224 #endif
225                 break;
226         case PT_SETREGS:
227                 error = copyin(uap->addr, &r.reg, sizeof r.reg);
228                 break;
229         case PT_SETFPREGS:
230                 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
231                 break;
232 #ifdef PT_SETDBREGS
233         case PT_SETDBREGS:
234                 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
235                 break;
236 #endif
237         case PT_IO:
238                 error = copyin(uap->addr, &r.piod, sizeof r.piod);
239                 break;
240         default:
241                 addr = uap->addr;
242         }
243         if (error)
244                 return (error);
245
246         error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data);
247         if (error)
248                 return (error);
249
250         switch (uap->req) {
251         case PT_IO:
252                 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
253                 break;
254         case PT_GETREGS:
255                 error = copyout(&r.reg, uap->addr, sizeof r.reg);
256                 break;
257         case PT_GETFPREGS:
258                 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
259                 break;
260 #ifdef PT_GETDBREGS
261         case PT_GETDBREGS:
262                 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
263                 break;
264 #endif
265         }
266
267         return (error);
268 }
269
270 int
271 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data)
272 {
273         struct proc *p, *pp;
274         struct iovec iov;
275         struct uio uio;
276         struct ptrace_io_desc *piod;
277         int error = 0;
278         int write, tmp, s;
279
280         write = 0;
281         if (req == PT_TRACE_ME)
282                 p = curp;
283         else {
284                 if ((p = pfind(pid)) == NULL)
285                         return ESRCH;
286         }
287         if (!PRISON_CHECK(curp, p))
288                 return (ESRCH);
289
290         /* Can't trace a process that's currently exec'ing. */
291         if ((p->p_flag & P_INEXEC) != 0)
292                 return EAGAIN;
293
294         /*
295          * Permissions check
296          */
297         switch (req) {
298         case PT_TRACE_ME:
299                 /* Always legal. */
300                 break;
301
302         case PT_ATTACH:
303                 /* Self */
304                 if (p->p_pid == curp->p_pid)
305                         return EINVAL;
306
307                 /* Already traced */
308                 if (p->p_flag & P_TRACED)
309                         return EBUSY;
310
311                 if (curp->p_flag & P_TRACED)
312                         for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
313                                 if (pp == p)
314                                         return (EINVAL);
315
316                 /* not owned by you, has done setuid (unless you're root) */
317                 if ((p->p_cred->p_ruid != curp->p_cred->p_ruid) ||
318                      (p->p_flag & P_SUGID)) {
319                         if ((error = suser(curp)) != 0)
320                                 return error;
321                 }
322
323                 /* can't trace init when securelevel > 0 */
324                 if (securelevel > 0 && p->p_pid == 1)
325                         return EPERM;
326
327                 /* OK */
328                 break;
329
330         case PT_READ_I:
331         case PT_READ_D:
332         case PT_WRITE_I:
333         case PT_WRITE_D:
334         case PT_IO:
335         case PT_CONTINUE:
336         case PT_KILL:
337         case PT_STEP:
338         case PT_DETACH:
339 #ifdef PT_GETREGS
340         case PT_GETREGS:
341 #endif
342 #ifdef PT_SETREGS
343         case PT_SETREGS:
344 #endif
345 #ifdef PT_GETFPREGS
346         case PT_GETFPREGS:
347 #endif
348 #ifdef PT_SETFPREGS
349         case PT_SETFPREGS:
350 #endif
351 #ifdef PT_GETDBREGS
352         case PT_GETDBREGS:
353 #endif
354 #ifdef PT_SETDBREGS
355         case PT_SETDBREGS:
356 #endif
357                 /* not being traced... */
358                 if ((p->p_flag & P_TRACED) == 0)
359                         return EPERM;
360
361                 /* not being traced by YOU */
362                 if (p->p_pptr != curp)
363                         return EBUSY;
364
365                 /* not currently stopped */
366                 if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0)
367                         return EBUSY;
368
369                 /* OK */
370                 break;
371
372         default:
373                 return EINVAL;
374         }
375
376 #ifdef FIX_SSTEP
377         /*
378          * Single step fixup ala procfs
379          */
380         FIX_SSTEP(p);
381 #endif
382
383         /*
384          * Actually do the requests
385          */
386
387         curp->p_retval[0] = 0;
388
389         switch (req) {
390         case PT_TRACE_ME:
391                 /* set my trace flag and "owner" so it can read/write me */
392                 p->p_flag |= P_TRACED;
393                 p->p_oppid = p->p_pptr->p_pid;
394                 return 0;
395
396         case PT_ATTACH:
397                 /* security check done above */
398                 p->p_flag |= P_TRACED;
399                 p->p_oppid = p->p_pptr->p_pid;
400                 if (p->p_pptr != curp)
401                         proc_reparent(p, curp);
402                 data = SIGSTOP;
403                 goto sendsig;   /* in PT_CONTINUE below */
404
405         case PT_STEP:
406         case PT_CONTINUE:
407         case PT_DETACH:
408                 if ((req != PT_STEP) && ((unsigned)data > _SIG_MAXSIG))
409                         return EINVAL;
410
411                 PHOLD(p);
412
413                 if (req == PT_STEP) {
414                         if ((error = ptrace_single_step (p))) {
415                                 PRELE(p);
416                                 return error;
417                         }
418                 }
419
420                 if (addr != (void *)1) {
421                         if ((error = ptrace_set_pc (p,
422                             (u_long)(uintfptr_t)addr))) {
423                                 PRELE(p);
424                                 return error;
425                         }
426                 }
427                 PRELE(p);
428
429                 if (req == PT_DETACH) {
430                         /* reset process parent */
431                         if (p->p_oppid != p->p_pptr->p_pid) {
432                                 struct proc *pp;
433
434                                 pp = pfind(p->p_oppid);
435                                 proc_reparent(p, pp ? pp : initproc);
436                         }
437
438                         p->p_flag &= ~(P_TRACED | P_WAITED);
439                         p->p_oppid = 0;
440
441                         /* should we send SIGCHLD? */
442                 }
443
444         sendsig:
445                 /* deliver or queue signal */
446                 s = splhigh();
447                 if (p->p_stat == SSTOP) {
448                         p->p_xstat = data;
449                         setrunnable(p);
450                 } else if (data) {
451                         psignal(p, data);
452                 }
453                 splx(s);
454                 return 0;
455
456         case PT_WRITE_I:
457         case PT_WRITE_D:
458                 write = 1;
459                 /* fallthrough */
460         case PT_READ_I:
461         case PT_READ_D:
462                 tmp = 0;
463                 /* write = 0 set above */
464                 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
465                 iov.iov_len = sizeof(int);
466                 uio.uio_iov = &iov;
467                 uio.uio_iovcnt = 1;
468                 uio.uio_offset = (off_t)(uintptr_t)addr;
469                 uio.uio_resid = sizeof(int);
470                 uio.uio_segflg = UIO_SYSSPACE;
471                 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
472                 uio.uio_procp = p;
473                 error = procfs_domem(curp, p, NULL, &uio);
474                 if (uio.uio_resid != 0) {
475                         /*
476                          * XXX procfs_domem() doesn't currently return ENOSPC,
477                          * so I think write() can bogusly return 0.
478                          * XXX what happens for short writes?  We don't want
479                          * to write partial data.
480                          * XXX procfs_domem() returns EPERM for other invalid
481                          * addresses.  Convert this to EINVAL.  Does this
482                          * clobber returns of EPERM for other reasons?
483                          */
484                         if (error == 0 || error == ENOSPC || error == EPERM)
485                                 error = EINVAL; /* EOF */
486                 }
487                 if (!write)
488                         curp->p_retval[0] = tmp;
489                 return (error);
490
491         case PT_IO:
492                 piod = addr;
493                 iov.iov_base = piod->piod_addr;
494                 iov.iov_len = piod->piod_len;
495                 uio.uio_iov = &iov;
496                 uio.uio_iovcnt = 1;
497                 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
498                 uio.uio_resid = piod->piod_len;
499                 uio.uio_segflg = UIO_USERSPACE;
500                 uio.uio_procp = p;
501                 switch (piod->piod_op) {
502                 case PIOD_READ_D:
503                 case PIOD_READ_I:
504                         uio.uio_rw = UIO_READ;
505                         break;
506                 case PIOD_WRITE_D:
507                 case PIOD_WRITE_I:
508                         uio.uio_rw = UIO_WRITE;
509                         break;
510                 default:
511                         return (EINVAL);
512                 }
513                 error = procfs_domem(curp, p, NULL, &uio);
514                 piod->piod_len -= uio.uio_resid;
515                 return (error);
516
517         case PT_KILL:
518                 data = SIGKILL;
519                 goto sendsig;   /* in PT_CONTINUE above */
520
521 #ifdef PT_SETREGS
522         case PT_SETREGS:
523                 write = 1;
524                 /* fallthrough */
525 #endif /* PT_SETREGS */
526 #ifdef PT_GETREGS
527         case PT_GETREGS:
528                 /* write = 0 above */
529 #endif /* PT_SETREGS */
530 #if defined(PT_SETREGS) || defined(PT_GETREGS)
531                 if (!procfs_validregs(p))       /* no P_SYSTEM procs please */
532                         return EINVAL;
533                 else {
534                         iov.iov_base = addr;
535                         iov.iov_len = sizeof(struct reg);
536                         uio.uio_iov = &iov;
537                         uio.uio_iovcnt = 1;
538                         uio.uio_offset = 0;
539                         uio.uio_resid = sizeof(struct reg);
540                         uio.uio_segflg = UIO_SYSSPACE;
541                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
542                         uio.uio_procp = curp;
543                         return (procfs_doregs(curp, p, NULL, &uio));
544                 }
545 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
546
547 #ifdef PT_SETFPREGS
548         case PT_SETFPREGS:
549                 write = 1;
550                 /* fallthrough */
551 #endif /* PT_SETFPREGS */
552 #ifdef PT_GETFPREGS
553         case PT_GETFPREGS:
554                 /* write = 0 above */
555 #endif /* PT_SETFPREGS */
556 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
557                 if (!procfs_validfpregs(p))     /* no P_SYSTEM procs please */
558                         return EINVAL;
559                 else {
560                         iov.iov_base = addr;
561                         iov.iov_len = sizeof(struct fpreg);
562                         uio.uio_iov = &iov;
563                         uio.uio_iovcnt = 1;
564                         uio.uio_offset = 0;
565                         uio.uio_resid = sizeof(struct fpreg);
566                         uio.uio_segflg = UIO_SYSSPACE;
567                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
568                         uio.uio_procp = curp;
569                         return (procfs_dofpregs(curp, p, NULL, &uio));
570                 }
571 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
572
573 #ifdef PT_SETDBREGS
574         case PT_SETDBREGS:
575                 write = 1;
576                 /* fallthrough */
577 #endif /* PT_SETDBREGS */
578 #ifdef PT_GETDBREGS
579         case PT_GETDBREGS:
580                 /* write = 0 above */
581 #endif /* PT_SETDBREGS */
582 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
583                 if (!procfs_validdbregs(p))     /* no P_SYSTEM procs please */
584                         return EINVAL;
585                 else {
586                         iov.iov_base = addr;
587                         iov.iov_len = sizeof(struct dbreg);
588                         uio.uio_iov = &iov;
589                         uio.uio_iovcnt = 1;
590                         uio.uio_offset = 0;
591                         uio.uio_resid = sizeof(struct dbreg);
592                         uio.uio_segflg = UIO_SYSSPACE;
593                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
594                         uio.uio_procp = curp;
595                         return (procfs_dodbregs(curp, p, NULL, &uio));
596                 }
597 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
598
599         default:
600                 break;
601         }
602
603         return 0;
604 }
605
606 int
607 trace_req(p)
608         struct proc *p;
609 {
610         return 1;
611 }
612
613 /*
614  * stopevent()
615  * Stop a process because of a procfs event;
616  * stay stopped until p->p_step is cleared
617  * (cleared by PIOCCONT in procfs).
618  */
619
620 void
621 stopevent(struct proc *p, unsigned int event, unsigned int val) {
622         p->p_step = 1;
623
624         do {
625                 p->p_xstat = val;
626                 p->p_stype = event;     /* Which event caused the stop? */
627                 wakeup(&p->p_stype);    /* Wake up any PIOCWAIT'ing procs */
628                 tsleep(&p->p_step, PWAIT, "stopevent", 0);
629         } while (p->p_step);
630 }