partially fix pctcpu and userland rescheduling. We really have to distribute
[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.5 2003/07/10 04:47:54 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 #ifndef _SYS_SYSPROTO_H_
195 struct ptrace_args {
196         int     req;
197         pid_t   pid;
198         caddr_t addr;
199         int     data;
200 };
201 #endif
202
203 int
204 ptrace(struct ptrace_args *uap)
205 {
206         struct proc *p = curproc;
207
208         /*
209          * XXX this obfuscation is to reduce stack usage, but the register
210          * structs may be too large to put on the stack anyway.
211          */
212         union {
213                 struct ptrace_io_desc piod;
214                 struct dbreg dbreg;
215                 struct fpreg fpreg;
216                 struct reg reg;
217         } r;
218         void *addr;
219         int error = 0;
220
221         addr = &r;
222         switch (uap->req) {
223         case PT_GETREGS:
224         case PT_GETFPREGS:
225 #ifdef PT_GETDBREGS
226         case PT_GETDBREGS:
227 #endif
228                 break;
229         case PT_SETREGS:
230                 error = copyin(uap->addr, &r.reg, sizeof r.reg);
231                 break;
232         case PT_SETFPREGS:
233                 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
234                 break;
235 #ifdef PT_SETDBREGS
236         case PT_SETDBREGS:
237                 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
238                 break;
239 #endif
240         case PT_IO:
241                 error = copyin(uap->addr, &r.piod, sizeof r.piod);
242                 break;
243         default:
244                 addr = uap->addr;
245         }
246         if (error)
247                 return (error);
248
249         error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data);
250         if (error)
251                 return (error);
252
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
268         }
269
270         return (error);
271 }
272
273 int
274 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data)
275 {
276         struct proc *p, *pp;
277         struct iovec iov;
278         struct uio uio;
279         struct ptrace_io_desc *piod;
280         int error = 0;
281         int write, tmp, s;
282
283         write = 0;
284         if (req == PT_TRACE_ME)
285                 p = curp;
286         else {
287                 if ((p = pfind(pid)) == NULL)
288                         return ESRCH;
289         }
290         if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
291                 return (ESRCH);
292
293         /* Can't trace a process that's currently exec'ing. */
294         if ((p->p_flag & P_INEXEC) != 0)
295                 return EAGAIN;
296
297         /*
298          * Permissions check
299          */
300         switch (req) {
301         case PT_TRACE_ME:
302                 /* Always legal. */
303                 break;
304
305         case PT_ATTACH:
306                 /* Self */
307                 if (p->p_pid == curp->p_pid)
308                         return EINVAL;
309
310                 /* Already traced */
311                 if (p->p_flag & P_TRACED)
312                         return EBUSY;
313
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);
318
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(curp->p_thread)) != 0)
323                                 return error;
324                 }
325
326                 /* can't trace init when securelevel > 0 */
327                 if (securelevel > 0 && p->p_pid == 1)
328                         return EPERM;
329
330                 /* OK */
331                 break;
332
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;
363
364                 /* not being traced by YOU */
365                 if (p->p_pptr != curp)
366                         return EBUSY;
367
368                 /* not currently stopped */
369                 if (p->p_stat != SSTOP || (p->p_flag & P_WAITED) == 0)
370                         return EBUSY;
371
372                 /* OK */
373                 break;
374
375         default:
376                 return EINVAL;
377         }
378
379 #ifdef FIX_SSTEP
380         /*
381          * Single step fixup ala procfs
382          */
383         FIX_SSTEP(p);
384 #endif
385
386         /*
387          * Actually do the requests
388          */
389
390         curp->p_retval[0] = 0;
391
392         switch (req) {
393         case PT_TRACE_ME:
394                 /* set my trace flag and "owner" so it can read/write me */
395                 p->p_flag |= P_TRACED;
396                 p->p_oppid = p->p_pptr->p_pid;
397                 return 0;
398
399         case PT_ATTACH:
400                 /* security check done above */
401                 p->p_flag |= P_TRACED;
402                 p->p_oppid = p->p_pptr->p_pid;
403                 if (p->p_pptr != curp)
404                         proc_reparent(p, curp);
405                 data = SIGSTOP;
406                 goto sendsig;   /* in PT_CONTINUE below */
407
408         case PT_STEP:
409         case PT_CONTINUE:
410         case PT_DETACH:
411                 if ((req != PT_STEP) && ((unsigned)data > _SIG_MAXSIG))
412                         return EINVAL;
413
414                 PHOLD(p);
415
416                 if (req == PT_STEP) {
417                         if ((error = ptrace_single_step (p))) {
418                                 PRELE(p);
419                                 return error;
420                         }
421                 }
422
423                 if (addr != (void *)1) {
424                         if ((error = ptrace_set_pc (p,
425                             (u_long)(uintfptr_t)addr))) {
426                                 PRELE(p);
427                                 return error;
428                         }
429                 }
430                 PRELE(p);
431
432                 if (req == PT_DETACH) {
433                         /* reset process parent */
434                         if (p->p_oppid != p->p_pptr->p_pid) {
435                                 struct proc *pp;
436
437                                 pp = pfind(p->p_oppid);
438                                 proc_reparent(p, pp ? pp : initproc);
439                         }
440
441                         p->p_flag &= ~(P_TRACED | P_WAITED);
442                         p->p_oppid = 0;
443
444                         /* should we send SIGCHLD? */
445                 }
446
447         sendsig:
448                 /* deliver or queue signal */
449                 s = splhigh();
450                 if (p->p_stat == SSTOP) {
451                         p->p_xstat = data;
452                         setrunnable(p);
453                 } else if (data) {
454                         psignal(p, data);
455                 }
456                 splx(s);
457                 return 0;
458
459         case PT_WRITE_I:
460         case PT_WRITE_D:
461                 write = 1;
462                 /* fallthrough */
463         case PT_READ_I:
464         case PT_READ_D:
465                 tmp = 0;
466                 /* write = 0 set above */
467                 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
468                 iov.iov_len = sizeof(int);
469                 uio.uio_iov = &iov;
470                 uio.uio_iovcnt = 1;
471                 uio.uio_offset = (off_t)(uintptr_t)addr;
472                 uio.uio_resid = sizeof(int);
473                 uio.uio_segflg = UIO_SYSSPACE;
474                 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
475                 uio.uio_td = p->p_thread;
476                 error = procfs_domem(curp, p, NULL, &uio);
477                 if (uio.uio_resid != 0) {
478                         /*
479                          * XXX procfs_domem() doesn't currently return ENOSPC,
480                          * so I think write() can bogusly return 0.
481                          * XXX what happens for short writes?  We don't want
482                          * to write partial data.
483                          * XXX procfs_domem() returns EPERM for other invalid
484                          * addresses.  Convert this to EINVAL.  Does this
485                          * clobber returns of EPERM for other reasons?
486                          */
487                         if (error == 0 || error == ENOSPC || error == EPERM)
488                                 error = EINVAL; /* EOF */
489                 }
490                 if (!write)
491                         curp->p_retval[0] = tmp;
492                 return (error);
493
494         case PT_IO:
495                 piod = addr;
496                 iov.iov_base = piod->piod_addr;
497                 iov.iov_len = piod->piod_len;
498                 uio.uio_iov = &iov;
499                 uio.uio_iovcnt = 1;
500                 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
501                 uio.uio_resid = piod->piod_len;
502                 uio.uio_segflg = UIO_USERSPACE;
503                 uio.uio_td = p->p_thread;
504                 switch (piod->piod_op) {
505                 case PIOD_READ_D:
506                 case PIOD_READ_I:
507                         uio.uio_rw = UIO_READ;
508                         break;
509                 case PIOD_WRITE_D:
510                 case PIOD_WRITE_I:
511                         uio.uio_rw = UIO_WRITE;
512                         break;
513                 default:
514                         return (EINVAL);
515                 }
516                 error = procfs_domem(curp, p, NULL, &uio);
517                 piod->piod_len -= uio.uio_resid;
518                 return (error);
519
520         case PT_KILL:
521                 data = SIGKILL;
522                 goto sendsig;   /* in PT_CONTINUE above */
523
524 #ifdef PT_SETREGS
525         case PT_SETREGS:
526                 write = 1;
527                 /* fallthrough */
528 #endif /* PT_SETREGS */
529 #ifdef PT_GETREGS
530         case PT_GETREGS:
531                 /* write = 0 above */
532 #endif /* PT_SETREGS */
533 #if defined(PT_SETREGS) || defined(PT_GETREGS)
534                 if (!procfs_validregs(p))       /* no P_SYSTEM procs please */
535                         return EINVAL;
536                 else {
537                         iov.iov_base = addr;
538                         iov.iov_len = sizeof(struct reg);
539                         uio.uio_iov = &iov;
540                         uio.uio_iovcnt = 1;
541                         uio.uio_offset = 0;
542                         uio.uio_resid = sizeof(struct reg);
543                         uio.uio_segflg = UIO_SYSSPACE;
544                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
545                         uio.uio_td = curp->p_thread;
546                         return (procfs_doregs(curp, p, NULL, &uio));
547                 }
548 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
549
550 #ifdef PT_SETFPREGS
551         case PT_SETFPREGS:
552                 write = 1;
553                 /* fallthrough */
554 #endif /* PT_SETFPREGS */
555 #ifdef PT_GETFPREGS
556         case PT_GETFPREGS:
557                 /* write = 0 above */
558 #endif /* PT_SETFPREGS */
559 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
560                 if (!procfs_validfpregs(p))     /* no P_SYSTEM procs please */
561                         return EINVAL;
562                 else {
563                         iov.iov_base = addr;
564                         iov.iov_len = sizeof(struct fpreg);
565                         uio.uio_iov = &iov;
566                         uio.uio_iovcnt = 1;
567                         uio.uio_offset = 0;
568                         uio.uio_resid = sizeof(struct fpreg);
569                         uio.uio_segflg = UIO_SYSSPACE;
570                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
571                         uio.uio_td = curp->p_thread;
572                         return (procfs_dofpregs(curp, p, NULL, &uio));
573                 }
574 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
575
576 #ifdef PT_SETDBREGS
577         case PT_SETDBREGS:
578                 write = 1;
579                 /* fallthrough */
580 #endif /* PT_SETDBREGS */
581 #ifdef PT_GETDBREGS
582         case PT_GETDBREGS:
583                 /* write = 0 above */
584 #endif /* PT_SETDBREGS */
585 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
586                 if (!procfs_validdbregs(p))     /* no P_SYSTEM procs please */
587                         return EINVAL;
588                 else {
589                         iov.iov_base = addr;
590                         iov.iov_len = sizeof(struct dbreg);
591                         uio.uio_iov = &iov;
592                         uio.uio_iovcnt = 1;
593                         uio.uio_offset = 0;
594                         uio.uio_resid = sizeof(struct dbreg);
595                         uio.uio_segflg = UIO_SYSSPACE;
596                         uio.uio_rw = write ? UIO_WRITE : UIO_READ;
597                         uio.uio_td = curp->p_thread;
598                         return (procfs_dodbregs(curp, p, NULL, &uio));
599                 }
600 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
601
602         default:
603                 break;
604         }
605
606         return 0;
607 }
608
609 int
610 trace_req(p)
611         struct proc *p;
612 {
613         return 1;
614 }
615
616 /*
617  * stopevent()
618  * Stop a process because of a procfs event;
619  * stay stopped until p->p_step is cleared
620  * (cleared by PIOCCONT in procfs).
621  */
622
623 void
624 stopevent(struct proc *p, unsigned int event, unsigned int val) 
625 {
626         p->p_step = 1;
627
628         do {
629                 crit_enter();
630                 wakeup(&p->p_stype);    /* Wake up any PIOCWAIT'ing procs */
631                 p->p_xstat = val;
632                 p->p_stype = event;     /* Which event caused the stop? */
633                 tsleep(&p->p_step, PWAIT, "stopevent", 0);
634                 crit_exit();
635         } while (p->p_step);
636 }
637