Merge from vendor branch FILE:
[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.24 2006/11/07 20:48:14 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 #include <sys/reg.h>
42 #include <sys/lock.h>
43
44 #include <vm/vm.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 <vfs/procfs/procfs.h>
51 #include <sys/thread2.h>
52
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;
67
68         /* Map page into kernel space */
69
70         map = &procp->p_vmspace->vm_map;
71
72         page_offset = addr - trunc_page(addr);
73         pageno = trunc_page(addr);
74
75         tmap = map;
76         rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
77                 &object, &pindex, &out_prot, &wired);
78
79         if (rv != KERN_SUCCESS)
80                 return EINVAL;
81
82         vm_map_lookup_done (tmap, out_entry, 0);
83
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);
91
92         if (!rv) {
93                 vm_object_reference (object);
94
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);
100                 }
101                 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE);
102         }
103
104         return rv;
105 }
106
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;
120
121         /* Map page into kernel space */
122
123         map = &procp->p_vmspace->vm_map;
124
125         page_offset = addr - trunc_page(addr);
126         pageno = trunc_page(addr);
127
128         /*
129          * Check the permissions for the area we're interested in.
130          */
131
132         if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
133                 VM_PROT_WRITE) == FALSE) {
134                 /*
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!
138                  */
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... */
144         }
145
146         /*
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.
151          */
152
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;
158         }
159
160         /*
161          * Okay, we've got the page.  Let's release tmap.
162          */
163
164         vm_map_lookup_done (tmap, out_entry, 0);
165
166         /*
167          * Fault the page in...
168          */
169
170         rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
171         if (rv != KERN_SUCCESS)
172                 return EFAULT;
173
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,
177                           0,
178                           VM_MAPTYPE_NORMAL,
179                           VM_PROT_ALL, VM_PROT_ALL,
180                           0);
181         if (!rv) {
182                 vm_object_reference (object);
183
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);
187                 }
188                 vm_map_remove (kernel_map, kva, kva + PAGE_SIZE);
189         }
190
191         if (fix_prot)
192                 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
193                         VM_PROT_READ|VM_PROT_EXECUTE, 0);
194         return rv;
195 }
196 #endif
197
198 /*
199  * Process debugging system call.
200  */
201 int
202 sys_ptrace(struct ptrace_args *uap)
203 {
204         struct proc *p = curproc;
205
206         /*
207          * XXX this obfuscation is to reduce stack usage, but the register
208          * structs may be too large to put on the stack anyway.
209          */
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;
218
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;
243         }
244         if (error)
245                 return (error);
246
247         error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
248                         &uap->sysmsg_result);
249         if (error)
250                 return (error);
251
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
267         }
268
269         return (error);
270 }
271
272 int
273 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data, int *res)
274 {
275         struct proc *p, *pp;
276         struct iovec iov;
277         struct uio uio;
278         struct ptrace_io_desc *piod;
279         int error = 0;
280         int write, tmp;
281
282         write = 0;
283         if (req == PT_TRACE_ME) {
284                 p = curp;
285         } else {
286                 if ((p = pfind(pid)) == NULL)
287                         return ESRCH;
288         }
289         if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
290                 return (ESRCH);
291
292         /* Can't trace a process that's currently exec'ing. */
293         if ((p->p_flag & P_INEXEC) != 0)
294                 return EAGAIN;
295
296         /*
297          * Permissions check
298          */
299         switch (req) {
300         case PT_TRACE_ME:
301                 /* Always legal. */
302                 break;
303
304         case PT_ATTACH:
305                 /* Self */
306                 if (p->p_pid == curp->p_pid)
307                         return EINVAL;
308
309                 /* Already traced */
310                 if (p->p_flag & P_TRACED)
311                         return EBUSY;
312
313                 if (curp->p_flag & P_TRACED)
314                         for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
315                                 if (pp == p)
316                                         return (EINVAL);
317
318                 /* not owned by you, has done setuid (unless you're root) */
319                 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
320                      (p->p_flag & P_SUGID)) {
321                         if ((error = suser_cred(curp->p_ucred, 0)) != 0)
322                                 return error;
323                 }
324
325                 /* can't trace init when securelevel > 0 */
326                 if (securelevel > 0 && p->p_pid == 1)
327                         return EPERM;
328
329                 /* OK */
330                 break;
331
332         case PT_READ_I:
333         case PT_READ_D:
334         case PT_WRITE_I:
335         case PT_WRITE_D:
336         case PT_IO:
337         case PT_CONTINUE:
338         case PT_KILL:
339         case PT_STEP:
340         case PT_DETACH:
341 #ifdef PT_GETREGS
342         case PT_GETREGS:
343 #endif
344 #ifdef PT_SETREGS
345         case PT_SETREGS:
346 #endif
347 #ifdef PT_GETFPREGS
348         case PT_GETFPREGS:
349 #endif
350 #ifdef PT_SETFPREGS
351         case PT_SETFPREGS:
352 #endif
353 #ifdef PT_GETDBREGS
354         case PT_GETDBREGS:
355 #endif
356 #ifdef PT_SETDBREGS
357         case PT_SETDBREGS:
358 #endif
359                 /* not being traced... */
360                 if ((p->p_flag & P_TRACED) == 0)
361                         return EPERM;
362
363                 /* not being traced by YOU */
364                 if (p->p_pptr != curp)
365                         return EBUSY;
366
367                 /* not currently stopped */
368                 if ((p->p_flag & P_STOPPED) == 0 ||
369                     (p->p_flag & P_WAITED) == 0) {
370                         return EBUSY;
371                 }
372
373                 /* OK */
374                 break;
375
376         default:
377                 return EINVAL;
378         }
379
380 #ifdef FIX_SSTEP
381         /*
382          * Single step fixup ala procfs
383          */
384         FIX_SSTEP(p);
385 #endif
386
387         /*
388          * Actually do the requests
389          */
390
391         *res = 0;
392
393         switch (req) {
394         case PT_TRACE_ME:
395                 /* set my trace flag and "owner" so it can read/write me */
396                 p->p_flag |= P_TRACED;
397                 p->p_oppid = p->p_pptr->p_pid;
398                 return 0;
399
400         case PT_ATTACH:
401                 /* security check done above */
402                 p->p_flag |= P_TRACED;
403                 p->p_oppid = p->p_pptr->p_pid;
404                 if (p->p_pptr != curp)
405                         proc_reparent(p, curp);
406                 data = SIGSTOP;
407                 goto sendsig;   /* in PT_CONTINUE below */
408
409         case PT_STEP:
410         case PT_CONTINUE:
411         case PT_DETACH:
412                 /* Zero means do not send any signal */
413                 if (data < 0 || data > _SIG_MAXSIG)
414                         return EINVAL;
415
416                 PHOLD(p);
417
418                 if (req == PT_STEP) {
419                         if ((error = ptrace_single_step (&p->p_lwp))) {
420                                 PRELE(p);
421                                 return error;
422                         }
423                 }
424
425                 if (addr != (void *)1) {
426                         if ((error = ptrace_set_pc (p,
427                             (u_long)(uintfptr_t)addr))) {
428                                 PRELE(p);
429                                 return error;
430                         }
431                 }
432                 PRELE(p);
433
434                 if (req == PT_DETACH) {
435                         /* reset process parent */
436                         if (p->p_oppid != p->p_pptr->p_pid) {
437                                 struct proc *pp;
438
439                                 pp = pfind(p->p_oppid);
440                                 proc_reparent(p, pp ? pp : initproc);
441                         }
442
443                         p->p_flag &= ~(P_TRACED | P_WAITED);
444                         p->p_oppid = 0;
445
446                         /* should we send SIGCHLD? */
447                 }
448
449         sendsig:
450                 /*
451                  * Deliver or queue signal.  If the process is stopped
452                  * force it to SRUN again.
453                  */
454                 crit_enter();
455                 if (p->p_flag & P_STOPPED) {
456                         p->p_xstat = data;
457                         p->p_flag |= P_BREAKTSLEEP;
458                         setrunnable(p);
459                 } else if (data) {
460                         ksignal(p, data);
461                 }
462                 crit_exit();
463                 return 0;
464
465         case PT_WRITE_I:
466         case PT_WRITE_D:
467                 write = 1;
468                 /* fallthrough */
469         case PT_READ_I:
470         case PT_READ_D:
471                 /*
472                  * NOTE! uio_offset represents the offset in the target
473                  * process.  The iov is in the current process (the guy
474                  * making the ptrace call) so uio_td must be the current
475                  * process (though for a SYSSPACE transfer it doesn't
476                  * really matter).
477                  */
478                 tmp = 0;
479                 /* write = 0 set above */
480                 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
481                 iov.iov_len = sizeof(int);
482                 uio.uio_iov = &iov;
483                 uio.uio_iovcnt = 1;
484                 uio.uio_offset = (off_t)(uintptr_t)addr;
485                 uio.uio_resid = sizeof(int);
486                 uio.uio_segflg = UIO_SYSSPACE;
487                 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
488                 uio.uio_td = curp->p_thread;
489                 error = procfs_domem(curp, p, NULL, &uio);
490                 if (uio.uio_resid != 0) {
491                         /*
492                          * XXX procfs_domem() doesn't currently return ENOSPC,
493                          * so I think write() can bogusly return 0.
494                          * XXX what happens for short writes?  We don't want
495                          * to write partial data.
496                          * XXX procfs_domem() returns EPERM for other invalid
497                          * addresses.  Convert this to EINVAL.  Does this
498                          * clobber returns of EPERM for other reasons?
499                          */
500                         if (error == 0 || error == ENOSPC || error == EPERM)
501                                 error = EINVAL; /* EOF */
502                 }
503                 if (!write)
504                         *res = tmp;
505                 return (error);
506
507         case PT_IO:
508                 /*
509                  * NOTE! uio_offset represents the offset in the target
510                  * process.  The iov is in the current process (the guy
511                  * making the ptrace call) so uio_td must be the current
512                  * process.
513                  */
514                 piod = addr;
515                 iov.iov_base = piod->piod_addr;
516                 iov.iov_len = piod->piod_len;
517                 uio.uio_iov = &iov;
518                 uio.uio_iovcnt = 1;
519                 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
520                 uio.uio_resid = piod->piod_len;
521                 uio.uio_segflg = UIO_USERSPACE;
522                 uio.uio_td = curp->p_thread;
523                 switch (piod->piod_op) {
524                 case PIOD_READ_D:
525                 case PIOD_READ_I:
526                         uio.uio_rw = UIO_READ;
527                         break;
528                 case PIOD_WRITE_D:
529                 case PIOD_WRITE_I:
530                         uio.uio_rw = UIO_WRITE;
531                         break;
532                 default:
533                         return (EINVAL);
534                 }
535                 error = procfs_domem(curp, p, NULL, &uio);
536                 piod->piod_len -= uio.uio_resid;
537                 return (error);
538
539         case PT_KILL:
540                 data = SIGKILL;
541                 goto sendsig;   /* in PT_CONTINUE above */
542
543 #ifdef PT_SETREGS
544         case PT_SETREGS:
545                 write = 1;
546                 /* fallthrough */
547 #endif /* PT_SETREGS */
548 #ifdef PT_GETREGS
549         case PT_GETREGS:
550                 /* write = 0 above */
551 #endif /* PT_SETREGS */
552 #if defined(PT_SETREGS) || defined(PT_GETREGS)
553                 if (!procfs_validregs(p))       /* no P_SYSTEM procs please */
554                         return EINVAL;
555                 else {
556                         iov.iov_base = addr;
557                         iov.iov_len = sizeof(struct reg);
558                         uio.uio_iov = &iov;
559                         uio.uio_iovcnt = 1;
560                         uio.uio_offset = 0;
561                         uio.uio_resid = sizeof(struct reg);
562                         uio.uio_segflg = UIO_SYSSPACE;
563                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
564                         uio.uio_td = curp->p_thread;
565                         return (procfs_doregs(curp, p, NULL, &uio));
566                 }
567 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
568
569 #ifdef PT_SETFPREGS
570         case PT_SETFPREGS:
571                 write = 1;
572                 /* fallthrough */
573 #endif /* PT_SETFPREGS */
574 #ifdef PT_GETFPREGS
575         case PT_GETFPREGS:
576                 /* write = 0 above */
577 #endif /* PT_SETFPREGS */
578 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
579                 if (!procfs_validfpregs(p))     /* no P_SYSTEM procs please */
580                         return EINVAL;
581                 else {
582                         iov.iov_base = addr;
583                         iov.iov_len = sizeof(struct fpreg);
584                         uio.uio_iov = &iov;
585                         uio.uio_iovcnt = 1;
586                         uio.uio_offset = 0;
587                         uio.uio_resid = sizeof(struct fpreg);
588                         uio.uio_segflg = UIO_SYSSPACE;
589                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
590                         uio.uio_td = curp->p_thread;
591                         return (procfs_dofpregs(curp, p, NULL, &uio));
592                 }
593 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
594
595 #ifdef PT_SETDBREGS
596         case PT_SETDBREGS:
597                 write = 1;
598                 /* fallthrough */
599 #endif /* PT_SETDBREGS */
600 #ifdef PT_GETDBREGS
601         case PT_GETDBREGS:
602                 /* write = 0 above */
603 #endif /* PT_SETDBREGS */
604 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
605                 if (!procfs_validdbregs(p))     /* no P_SYSTEM procs please */
606                         return EINVAL;
607                 else {
608                         iov.iov_base = addr;
609                         iov.iov_len = sizeof(struct dbreg);
610                         uio.uio_iov = &iov;
611                         uio.uio_iovcnt = 1;
612                         uio.uio_offset = 0;
613                         uio.uio_resid = sizeof(struct dbreg);
614                         uio.uio_segflg = UIO_SYSSPACE;
615                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
616                         uio.uio_td = curp->p_thread;
617                         return (procfs_dodbregs(curp, p, NULL, &uio));
618                 }
619 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
620
621         default:
622                 break;
623         }
624
625         return 0;
626 }
627
628 int
629 trace_req(p)
630         struct proc *p;
631 {
632         return 1;
633 }
634
635 /*
636  * stopevent()
637  *
638  * Stop a process because of a procfs event.  Stay stopped until p->p_step
639  * is cleared (cleared by PIOCCONT in procfs).
640  *
641  * MPSAFE
642  */
643 void
644 stopevent(struct proc *p, unsigned int event, unsigned int val) 
645 {
646         p->p_step = 1;
647
648         do {
649                 crit_enter();
650                 wakeup(&p->p_stype);    /* Wake up any PIOCWAIT'ing procs */
651                 p->p_xstat = val;
652                 p->p_stype = event;     /* Which event caused the stop? */
653                 tsleep(&p->p_step, 0, "stopevent", 0);
654                 crit_exit();
655         } while (p->p_step);
656 }
657