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