kernel - Fix proc_reparent() race/assertion panic
[dragonfly.git] / sys / kern / sys_process.c
CommitLineData
984263bc
MD
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 $
c7e98b2f 32 * $DragonFly: src/sys/kern/sys_process.c,v 1.30 2007/02/19 01:14:23 corecode Exp $
984263bc
MD
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/sysproto.h>
38#include <sys/proc.h>
895c1f85 39#include <sys/priv.h>
984263bc
MD
40#include <sys/vnode.h>
41#include <sys/ptrace.h>
527fddf7
MD
42#include <sys/reg.h>
43#include <sys/lock.h>
984263bc 44
984263bc 45#include <vm/vm.h>
984263bc
MD
46#include <vm/pmap.h>
47#include <vm/vm_map.h>
48#include <vm/vm_page.h>
49
50#include <sys/user.h>
1f2de5d4 51#include <vfs/procfs/procfs.h>
684a93c4 52
a2a5ad0d 53#include <sys/thread2.h>
e193508c 54#include <sys/spinlock2.h>
984263bc
MD
55
56/* use the equivalent procfs code */
57#if 0
58static int
59pread (struct proc *procp, unsigned int addr, unsigned int *retval) {
60 int rv;
61 vm_map_t map, tmap;
62 vm_object_t object;
63 vm_offset_t kva = 0;
64 int page_offset; /* offset into page */
65 vm_offset_t pageno; /* page number */
66 vm_map_entry_t out_entry;
67 vm_prot_t out_prot;
68 boolean_t wired;
69 vm_pindex_t pindex;
70
71 /* Map page into kernel space */
72
73 map = &procp->p_vmspace->vm_map;
74
75 page_offset = addr - trunc_page(addr);
76 pageno = trunc_page(addr);
77
78 tmap = map;
dda969a8
MD
79 rv = vm_map_lookup(&tmap, pageno, VM_PROT_READ, &out_entry,
80 &object, &pindex, &out_prot, &wired);
984263bc
MD
81
82 if (rv != KERN_SUCCESS)
83 return EINVAL;
84
a108bf71 85 vm_map_lookup_done (tmap, out_entry, 0);
984263bc
MD
86
87 /* Find space in kernel_map for the page we're interested in */
e4846942 88 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
9388fcaa
MD
89 &kva,
90 PAGE_SIZE, PAGE_SIZE,
91 0, VM_MAPTYPE_NORMAL,
1b874851
MD
92 VM_PROT_ALL, VM_PROT_ALL,
93 0);
984263bc
MD
94
95 if (!rv) {
b12defdc 96 vm_object_reference XXX (object);
984263bc 97
e4846942 98 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
984263bc
MD
99 if (!rv) {
100 *retval = 0;
101 bcopy ((caddr_t)kva + page_offset,
102 retval, sizeof *retval);
103 }
e4846942 104 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
984263bc
MD
105 }
106
107 return rv;
108}
109
110static int
111pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
112 int rv;
113 vm_map_t map, tmap;
114 vm_object_t object;
115 vm_offset_t kva = 0;
116 int page_offset; /* offset into page */
117 vm_offset_t pageno; /* page number */
118 vm_map_entry_t out_entry;
119 vm_prot_t out_prot;
120 boolean_t wired;
121 vm_pindex_t pindex;
122 boolean_t fix_prot = 0;
123
124 /* Map page into kernel space */
125
126 map = &procp->p_vmspace->vm_map;
127
128 page_offset = addr - trunc_page(addr);
129 pageno = trunc_page(addr);
130
131 /*
132 * Check the permissions for the area we're interested in.
133 */
134
135 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
46754a20 136 VM_PROT_WRITE, FALSE) == FALSE) {
984263bc
MD
137 /*
138 * If the page was not writable, we make it so.
139 * XXX It is possible a page may *not* be read/executable,
140 * if a process changes that!
141 */
142 fix_prot = 1;
143 /* The page isn't writable, so let's try making it so... */
144 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
145 VM_PROT_ALL, 0)) != KERN_SUCCESS)
146 return EFAULT; /* I guess... */
147 }
148
149 /*
150 * Now we need to get the page. out_entry, out_prot, wired, and
151 * single_use aren't used. One would think the vm code would be
152 * a *bit* nicer... We use tmap because vm_map_lookup() can
153 * change the map argument.
154 */
155
156 tmap = map;
dda969a8
MD
157 rv = vm_map_lookup(&tmap, pageno, VM_PROT_WRITE, &out_entry,
158 &object, &pindex, &out_prot, &wired);
b12defdc 159 if (rv != KERN_SUCCESS)
984263bc 160 return EINVAL;
984263bc
MD
161
162 /*
163 * Okay, we've got the page. Let's release tmap.
164 */
a108bf71 165 vm_map_lookup_done (tmap, out_entry, 0);
984263bc
MD
166
167 /*
168 * Fault the page in...
169 */
984263bc
MD
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 */
e4846942 175 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
9388fcaa
MD
176 &kva,
177 PAGE_SIZE, PAGE_SIZE,
178 0, VM_MAPTYPE_NORMAL,
1b874851
MD
179 VM_PROT_ALL, VM_PROT_ALL,
180 0);
984263bc 181 if (!rv) {
b12defdc 182 vm_object_reference XXX (object);
984263bc 183
e4846942 184 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
984263bc
MD
185 if (!rv) {
186 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
187 }
e4846942 188 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
984263bc
MD
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.
3919ced0
MD
200 *
201 * MPALMOSTSAFE
984263bc 202 */
984263bc 203int
753fd850 204sys_ptrace(struct ptrace_args *uap)
984263bc 205{
41c20dac
MD
206 struct proc *p = curproc;
207
984263bc
MD
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
90b9818c 249 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
c7114eea 250 &uap->sysmsg_result);
984263bc
MD
251 if (error)
252 return (error);
253
254 switch (uap->req) {
255 case PT_IO:
256 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
257 break;
258 case PT_GETREGS:
259 error = copyout(&r.reg, uap->addr, sizeof r.reg);
260 break;
261 case PT_GETFPREGS:
262 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
263 break;
264#ifdef PT_GETDBREGS
265 case PT_GETDBREGS:
266 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
267 break;
268#endif
269 }
270
271 return (error);
272}
273
274int
5686ec5a
MD
275kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr,
276 int data, int *res)
984263bc
MD
277{
278 struct proc *p, *pp;
08f2f1bb 279 struct lwp *lp;
984263bc
MD
280 struct iovec iov;
281 struct uio uio;
282 struct ptrace_io_desc *piod;
283 int error = 0;
d767fe64 284 int write, tmp;
24c69223
VS
285 int t;
286
287 lwkt_gettoken(&proc_token);
984263bc
MD
288
289 write = 0;
1301b4d8 290 if (req == PT_TRACE_ME) {
984263bc 291 p = curp;
58c2553a 292 PHOLD(p);
1301b4d8 293 } else {
24c69223
VS
294 if ((p = pfind(pid)) == NULL) {
295 lwkt_reltoken(&proc_token);
984263bc 296 return ESRCH;
24c69223 297 }
984263bc 298 }
24c69223 299 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
58c2553a 300 PRELE(p);
24c69223 301 lwkt_reltoken(&proc_token);
984263bc 302 return (ESRCH);
24c69223 303 }
984263bc 304
5686ec5a 305 lwkt_gettoken(&p->p_token);
984263bc 306 /* Can't trace a process that's currently exec'ing. */
4643740a 307 if ((p->p_flags & P_INEXEC) != 0) {
5686ec5a 308 lwkt_reltoken(&p->p_token);
58c2553a 309 PRELE(p);
24c69223 310 lwkt_reltoken(&proc_token);
984263bc 311 return EAGAIN;
24c69223 312 }
984263bc
MD
313
314 /*
315 * Permissions check
316 */
317 switch (req) {
318 case PT_TRACE_ME:
319 /* Always legal. */
320 break;
321
322 case PT_ATTACH:
323 /* Self */
24c69223 324 if (p->p_pid == curp->p_pid) {
5686ec5a 325 lwkt_reltoken(&p->p_token);
58c2553a 326 PRELE(p);
24c69223 327 lwkt_reltoken(&proc_token);
984263bc 328 return EINVAL;
24c69223 329 }
984263bc
MD
330
331 /* Already traced */
4643740a 332 if (p->p_flags & P_TRACED) {
5686ec5a 333 lwkt_reltoken(&p->p_token);
58c2553a 334 PRELE(p);
24c69223 335 lwkt_reltoken(&proc_token);
984263bc 336 return EBUSY;
24c69223 337 }
984263bc 338
4643740a 339 if (curp->p_flags & P_TRACED)
984263bc 340 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
24c69223 341 if (pp == p) {
5686ec5a 342 lwkt_reltoken(&p->p_token);
58c2553a 343 PRELE(p);
24c69223 344 lwkt_reltoken(&proc_token);
984263bc 345 return (EINVAL);
24c69223 346 }
984263bc
MD
347
348 /* not owned by you, has done setuid (unless you're root) */
41c20dac 349 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
4643740a 350 (p->p_flags & P_SUGID)) {
24c69223 351 if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0) {
5686ec5a 352 lwkt_reltoken(&p->p_token);
58c2553a 353 PRELE(p);
24c69223 354 lwkt_reltoken(&proc_token);
984263bc 355 return error;
24c69223 356 }
984263bc
MD
357 }
358
359 /* can't trace init when securelevel > 0 */
24c69223 360 if (securelevel > 0 && p->p_pid == 1) {
5686ec5a 361 lwkt_reltoken(&p->p_token);
58c2553a 362 PRELE(p);
24c69223 363 lwkt_reltoken(&proc_token);
984263bc 364 return EPERM;
24c69223 365 }
984263bc
MD
366
367 /* OK */
368 break;
369
370 case PT_READ_I:
371 case PT_READ_D:
372 case PT_WRITE_I:
373 case PT_WRITE_D:
374 case PT_IO:
375 case PT_CONTINUE:
376 case PT_KILL:
377 case PT_STEP:
378 case PT_DETACH:
379#ifdef PT_GETREGS
380 case PT_GETREGS:
381#endif
382#ifdef PT_SETREGS
383 case PT_SETREGS:
384#endif
385#ifdef PT_GETFPREGS
386 case PT_GETFPREGS:
387#endif
388#ifdef PT_SETFPREGS
389 case PT_SETFPREGS:
390#endif
391#ifdef PT_GETDBREGS
392 case PT_GETDBREGS:
393#endif
394#ifdef PT_SETDBREGS
395 case PT_SETDBREGS:
396#endif
397 /* not being traced... */
4643740a 398 if ((p->p_flags & P_TRACED) == 0) {
5686ec5a 399 lwkt_reltoken(&p->p_token);
58c2553a 400 PRELE(p);
24c69223 401 lwkt_reltoken(&proc_token);
984263bc 402 return EPERM;
24c69223 403 }
984263bc
MD
404
405 /* not being traced by YOU */
24c69223 406 if (p->p_pptr != curp) {
5686ec5a 407 lwkt_reltoken(&p->p_token);
58c2553a 408 PRELE(p);
24c69223 409 lwkt_reltoken(&proc_token);
984263bc 410 return EBUSY;
24c69223 411 }
984263bc
MD
412
413 /* not currently stopped */
164b8401 414 if (p->p_stat != SSTOP ||
4643740a 415 (p->p_flags & P_WAITED) == 0) {
5686ec5a 416 lwkt_reltoken(&p->p_token);
58c2553a 417 PRELE(p);
24c69223 418 lwkt_reltoken(&proc_token);
984263bc 419 return EBUSY;
344ad853 420 }
984263bc
MD
421
422 /* OK */
423 break;
424
425 default:
5686ec5a 426 lwkt_reltoken(&p->p_token);
58c2553a 427 PRELE(p);
24c69223 428 lwkt_reltoken(&proc_token);
984263bc
MD
429 return EINVAL;
430 }
431
08f2f1bb
SS
432 /* XXX lwp */
433 lp = FIRST_LWP_IN_PROC(p);
984263bc
MD
434#ifdef FIX_SSTEP
435 /*
436 * Single step fixup ala procfs
437 */
c7e98b2f 438 FIX_SSTEP(lp);
984263bc
MD
439#endif
440
441 /*
442 * Actually do the requests
443 */
444
90b9818c 445 *res = 0;
984263bc
MD
446
447 switch (req) {
448 case PT_TRACE_ME:
449 /* set my trace flag and "owner" so it can read/write me */
4643740a 450 p->p_flags |= P_TRACED;
984263bc 451 p->p_oppid = p->p_pptr->p_pid;
5686ec5a 452 lwkt_reltoken(&p->p_token);
58c2553a 453 PRELE(p);
24c69223 454 lwkt_reltoken(&proc_token);
984263bc
MD
455 return 0;
456
457 case PT_ATTACH:
458 /* security check done above */
4643740a 459 p->p_flags |= P_TRACED;
984263bc 460 p->p_oppid = p->p_pptr->p_pid;
55c81f71 461 proc_reparent(p, curp);
984263bc
MD
462 data = SIGSTOP;
463 goto sendsig; /* in PT_CONTINUE below */
464
465 case PT_STEP:
466 case PT_CONTINUE:
467 case PT_DETACH:
43300f51 468 /* Zero means do not send any signal */
24c69223 469 if (data < 0 || data > _SIG_MAXSIG) {
5686ec5a 470 lwkt_reltoken(&p->p_token);
58c2553a 471 PRELE(p);
24c69223 472 lwkt_reltoken(&proc_token);
984263bc 473 return EINVAL;
24c69223 474 }
984263bc 475
c7e98b2f 476 LWPHOLD(lp);
984263bc
MD
477
478 if (req == PT_STEP) {
08f2f1bb 479 if ((error = ptrace_single_step (lp))) {
c7e98b2f 480 LWPRELE(lp);
5686ec5a 481 lwkt_reltoken(&p->p_token);
58c2553a 482 PRELE(p);
24c69223 483 lwkt_reltoken(&proc_token);
984263bc
MD
484 return error;
485 }
486 }
487
488 if (addr != (void *)1) {
08f2f1bb 489 if ((error = ptrace_set_pc (lp,
984263bc 490 (u_long)(uintfptr_t)addr))) {
c7e98b2f 491 LWPRELE(lp);
5686ec5a 492 lwkt_reltoken(&p->p_token);
58c2553a 493 PRELE(p);
24c69223 494 lwkt_reltoken(&proc_token);
984263bc
MD
495 return error;
496 }
497 }
c7e98b2f 498 LWPRELE(lp);
984263bc
MD
499
500 if (req == PT_DETACH) {
501 /* reset process parent */
502 if (p->p_oppid != p->p_pptr->p_pid) {
503 struct proc *pp;
504
505 pp = pfind(p->p_oppid);
55c81f71
MD
506 proc_reparent(p, pp);
507 if (pp)
4c141516 508 PRELE(pp);
984263bc
MD
509 }
510
4643740a 511 p->p_flags &= ~(P_TRACED | P_WAITED);
984263bc
MD
512 p->p_oppid = 0;
513
514 /* should we send SIGCHLD? */
515 }
516
517 sendsig:
344ad853
MD
518 /*
519 * Deliver or queue signal. If the process is stopped
9a379a4a 520 * force it to be SACTIVE again.
344ad853 521 */
e43a034f 522 crit_enter();
164b8401 523 if (p->p_stat == SSTOP) {
984263bc 524 p->p_xstat = data;
9a379a4a 525 proc_unstop(p);
984263bc 526 } else if (data) {
84204577 527 ksignal(p, data);
984263bc 528 }
e43a034f 529 crit_exit();
5686ec5a 530 lwkt_reltoken(&p->p_token);
58c2553a 531 PRELE(p);
24c69223 532 lwkt_reltoken(&proc_token);
984263bc
MD
533 return 0;
534
535 case PT_WRITE_I:
536 case PT_WRITE_D:
537 write = 1;
538 /* fallthrough */
539 case PT_READ_I:
540 case PT_READ_D:
1301b4d8
MD
541 /*
542 * NOTE! uio_offset represents the offset in the target
543 * process. The iov is in the current process (the guy
544 * making the ptrace call) so uio_td must be the current
545 * process (though for a SYSSPACE transfer it doesn't
546 * really matter).
547 */
984263bc
MD
548 tmp = 0;
549 /* write = 0 set above */
550 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
551 iov.iov_len = sizeof(int);
552 uio.uio_iov = &iov;
553 uio.uio_iovcnt = 1;
554 uio.uio_offset = (off_t)(uintptr_t)addr;
555 uio.uio_resid = sizeof(int);
556 uio.uio_segflg = UIO_SYSSPACE;
557 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 558 uio.uio_td = curthread;
c7e98b2f 559 error = procfs_domem(curp, lp, NULL, &uio);
984263bc
MD
560 if (uio.uio_resid != 0) {
561 /*
562 * XXX procfs_domem() doesn't currently return ENOSPC,
563 * so I think write() can bogusly return 0.
564 * XXX what happens for short writes? We don't want
565 * to write partial data.
566 * XXX procfs_domem() returns EPERM for other invalid
567 * addresses. Convert this to EINVAL. Does this
568 * clobber returns of EPERM for other reasons?
569 */
570 if (error == 0 || error == ENOSPC || error == EPERM)
571 error = EINVAL; /* EOF */
572 }
573 if (!write)
90b9818c 574 *res = tmp;
5686ec5a 575 lwkt_reltoken(&p->p_token);
58c2553a 576 PRELE(p);
24c69223 577 lwkt_reltoken(&proc_token);
984263bc
MD
578 return (error);
579
580 case PT_IO:
1301b4d8
MD
581 /*
582 * NOTE! uio_offset represents the offset in the target
583 * process. The iov is in the current process (the guy
584 * making the ptrace call) so uio_td must be the current
585 * process.
586 */
984263bc
MD
587 piod = addr;
588 iov.iov_base = piod->piod_addr;
589 iov.iov_len = piod->piod_len;
590 uio.uio_iov = &iov;
591 uio.uio_iovcnt = 1;
592 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
593 uio.uio_resid = piod->piod_len;
594 uio.uio_segflg = UIO_USERSPACE;
08f2f1bb 595 uio.uio_td = curthread;
984263bc
MD
596 switch (piod->piod_op) {
597 case PIOD_READ_D:
598 case PIOD_READ_I:
599 uio.uio_rw = UIO_READ;
600 break;
601 case PIOD_WRITE_D:
602 case PIOD_WRITE_I:
603 uio.uio_rw = UIO_WRITE;
604 break;
605 default:
5686ec5a 606 lwkt_reltoken(&p->p_token);
58c2553a 607 PRELE(p);
24c69223 608 lwkt_reltoken(&proc_token);
984263bc
MD
609 return (EINVAL);
610 }
c7e98b2f 611 error = procfs_domem(curp, lp, NULL, &uio);
984263bc 612 piod->piod_len -= uio.uio_resid;
5686ec5a 613 lwkt_reltoken(&p->p_token);
58c2553a 614 PRELE(p);
24c69223 615 lwkt_reltoken(&proc_token);
984263bc
MD
616 return (error);
617
618 case PT_KILL:
619 data = SIGKILL;
620 goto sendsig; /* in PT_CONTINUE above */
621
622#ifdef PT_SETREGS
623 case PT_SETREGS:
624 write = 1;
625 /* fallthrough */
626#endif /* PT_SETREGS */
627#ifdef PT_GETREGS
628 case PT_GETREGS:
629 /* write = 0 above */
630#endif /* PT_SETREGS */
631#if defined(PT_SETREGS) || defined(PT_GETREGS)
24c69223 632 if (!procfs_validregs(lp)) { /* no P_SYSTEM procs please */
5686ec5a 633 lwkt_reltoken(&p->p_token);
58c2553a 634 PRELE(p);
24c69223 635 lwkt_reltoken(&proc_token);
984263bc 636 return EINVAL;
24c69223 637 } else {
984263bc
MD
638 iov.iov_base = addr;
639 iov.iov_len = sizeof(struct reg);
640 uio.uio_iov = &iov;
641 uio.uio_iovcnt = 1;
642 uio.uio_offset = 0;
643 uio.uio_resid = sizeof(struct reg);
644 uio.uio_segflg = UIO_SYSSPACE;
645 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 646 uio.uio_td = curthread;
24c69223 647 t = procfs_doregs(curp, lp, NULL, &uio);
5686ec5a 648 lwkt_reltoken(&p->p_token);
58c2553a 649 PRELE(p);
24c69223
VS
650 lwkt_reltoken(&proc_token);
651 return t;
984263bc
MD
652 }
653#endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
654
655#ifdef PT_SETFPREGS
656 case PT_SETFPREGS:
657 write = 1;
658 /* fallthrough */
659#endif /* PT_SETFPREGS */
660#ifdef PT_GETFPREGS
661 case PT_GETFPREGS:
662 /* write = 0 above */
663#endif /* PT_SETFPREGS */
664#if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
24c69223 665 if (!procfs_validfpregs(lp)) { /* no P_SYSTEM procs please */
5686ec5a 666 lwkt_reltoken(&p->p_token);
58c2553a 667 PRELE(p);
24c69223 668 lwkt_reltoken(&proc_token);
984263bc 669 return EINVAL;
24c69223 670 } else {
984263bc
MD
671 iov.iov_base = addr;
672 iov.iov_len = sizeof(struct fpreg);
673 uio.uio_iov = &iov;
674 uio.uio_iovcnt = 1;
675 uio.uio_offset = 0;
676 uio.uio_resid = sizeof(struct fpreg);
677 uio.uio_segflg = UIO_SYSSPACE;
678 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 679 uio.uio_td = curthread;
24c69223 680 t = procfs_dofpregs(curp, lp, NULL, &uio);
5686ec5a 681 lwkt_reltoken(&p->p_token);
58c2553a 682 PRELE(p);
24c69223
VS
683 lwkt_reltoken(&proc_token);
684 return t;
984263bc
MD
685 }
686#endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
687
688#ifdef PT_SETDBREGS
689 case PT_SETDBREGS:
690 write = 1;
691 /* fallthrough */
692#endif /* PT_SETDBREGS */
693#ifdef PT_GETDBREGS
694 case PT_GETDBREGS:
695 /* write = 0 above */
696#endif /* PT_SETDBREGS */
697#if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
24c69223 698 if (!procfs_validdbregs(lp)) { /* no P_SYSTEM procs please */
5686ec5a 699 lwkt_reltoken(&p->p_token);
58c2553a 700 PRELE(p);
24c69223 701 lwkt_reltoken(&proc_token);
984263bc 702 return EINVAL;
24c69223 703 } else {
984263bc
MD
704 iov.iov_base = addr;
705 iov.iov_len = sizeof(struct dbreg);
706 uio.uio_iov = &iov;
707 uio.uio_iovcnt = 1;
708 uio.uio_offset = 0;
709 uio.uio_resid = sizeof(struct dbreg);
710 uio.uio_segflg = UIO_SYSSPACE;
711 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 712 uio.uio_td = curthread;
24c69223 713 t = procfs_dodbregs(curp, lp, NULL, &uio);
5686ec5a 714 lwkt_reltoken(&p->p_token);
58c2553a 715 PRELE(p);
24c69223
VS
716 lwkt_reltoken(&proc_token);
717 return t;
984263bc
MD
718 }
719#endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
720
721 default:
722 break;
723 }
724
5686ec5a 725 lwkt_reltoken(&p->p_token);
58c2553a 726 PRELE(p);
24c69223 727 lwkt_reltoken(&proc_token);
58c2553a 728
984263bc
MD
729 return 0;
730}
731
732int
c972a82f 733trace_req(struct proc *p)
984263bc
MD
734{
735 return 1;
736}
737
738/*
739 * stopevent()
270ac911
MD
740 *
741 * Stop a process because of a procfs event. Stay stopped until p->p_step
742 * is cleared (cleared by PIOCCONT in procfs).
743 *
744 * MPSAFE
984263bc 745 */
984263bc 746void
a2a5ad0d
MD
747stopevent(struct proc *p, unsigned int event, unsigned int val)
748{
e193508c
MD
749 /*
750 * Set event info. Recheck p_stops in case we are
751 * racing a close() on procfs.
752 */
287a8577 753 spin_lock(&p->p_spin);
e193508c 754 if ((p->p_stops & event) == 0) {
287a8577 755 spin_unlock(&p->p_spin);
e193508c
MD
756 return;
757 }
758 p->p_xstat = val;
759 p->p_stype = event;
984263bc 760 p->p_step = 1;
e193508c 761 tsleep_interlock(&p->p_step, 0);
287a8577 762 spin_unlock(&p->p_spin);
984263bc 763
e193508c
MD
764 /*
765 * Wakeup any PIOCWAITing procs and wait for p_step to
766 * be cleared.
767 */
768 for (;;) {
769 wakeup(&p->p_stype);
770 tsleep(&p->p_step, PINTERLOCKED, "stopevent", 0);
287a8577 771 spin_lock(&p->p_spin);
e193508c 772 if (p->p_step == 0) {
287a8577 773 spin_unlock(&p->p_spin);
e193508c
MD
774 break;
775 }
776 tsleep_interlock(&p->p_step, 0);
287a8577 777 spin_unlock(&p->p_spin);
e193508c 778 }
984263bc 779}
a2a5ad0d 780