kernel - Major signal path adjustments to fix races, tsleep race fixes, +more
[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;
79 rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
80 &object, &pindex, &out_prot, &wired);
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;
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
MD
460 p->p_oppid = p->p_pptr->p_pid;
461 if (p->p_pptr != curp)
462 proc_reparent(p, curp);
463 data = SIGSTOP;
464 goto sendsig; /* in PT_CONTINUE below */
465
466 case PT_STEP:
467 case PT_CONTINUE:
468 case PT_DETACH:
43300f51 469 /* Zero means do not send any signal */
24c69223 470 if (data < 0 || data > _SIG_MAXSIG) {
5686ec5a 471 lwkt_reltoken(&p->p_token);
58c2553a 472 PRELE(p);
24c69223 473 lwkt_reltoken(&proc_token);
984263bc 474 return EINVAL;
24c69223 475 }
984263bc 476
c7e98b2f 477 LWPHOLD(lp);
984263bc
MD
478
479 if (req == PT_STEP) {
08f2f1bb 480 if ((error = ptrace_single_step (lp))) {
c7e98b2f 481 LWPRELE(lp);
5686ec5a 482 lwkt_reltoken(&p->p_token);
58c2553a 483 PRELE(p);
24c69223 484 lwkt_reltoken(&proc_token);
984263bc
MD
485 return error;
486 }
487 }
488
489 if (addr != (void *)1) {
08f2f1bb 490 if ((error = ptrace_set_pc (lp,
984263bc 491 (u_long)(uintfptr_t)addr))) {
c7e98b2f 492 LWPRELE(lp);
5686ec5a 493 lwkt_reltoken(&p->p_token);
58c2553a 494 PRELE(p);
24c69223 495 lwkt_reltoken(&proc_token);
984263bc
MD
496 return error;
497 }
498 }
c7e98b2f 499 LWPRELE(lp);
984263bc
MD
500
501 if (req == PT_DETACH) {
502 /* reset process parent */
503 if (p->p_oppid != p->p_pptr->p_pid) {
504 struct proc *pp;
505
506 pp = pfind(p->p_oppid);
507 proc_reparent(p, pp ? pp : initproc);
4c141516
YT
508 if (pp != NULL)
509 PRELE(pp);
984263bc
MD
510 }
511
4643740a 512 p->p_flags &= ~(P_TRACED | P_WAITED);
984263bc
MD
513 p->p_oppid = 0;
514
515 /* should we send SIGCHLD? */
516 }
517
518 sendsig:
344ad853
MD
519 /*
520 * Deliver or queue signal. If the process is stopped
9a379a4a 521 * force it to be SACTIVE again.
344ad853 522 */
e43a034f 523 crit_enter();
164b8401 524 if (p->p_stat == SSTOP) {
984263bc 525 p->p_xstat = data;
9a379a4a 526 proc_unstop(p);
984263bc 527 } else if (data) {
84204577 528 ksignal(p, data);
984263bc 529 }
e43a034f 530 crit_exit();
5686ec5a 531 lwkt_reltoken(&p->p_token);
58c2553a 532 PRELE(p);
24c69223 533 lwkt_reltoken(&proc_token);
984263bc
MD
534 return 0;
535
536 case PT_WRITE_I:
537 case PT_WRITE_D:
538 write = 1;
539 /* fallthrough */
540 case PT_READ_I:
541 case PT_READ_D:
1301b4d8
MD
542 /*
543 * NOTE! uio_offset represents the offset in the target
544 * process. The iov is in the current process (the guy
545 * making the ptrace call) so uio_td must be the current
546 * process (though for a SYSSPACE transfer it doesn't
547 * really matter).
548 */
984263bc
MD
549 tmp = 0;
550 /* write = 0 set above */
551 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
552 iov.iov_len = sizeof(int);
553 uio.uio_iov = &iov;
554 uio.uio_iovcnt = 1;
555 uio.uio_offset = (off_t)(uintptr_t)addr;
556 uio.uio_resid = sizeof(int);
557 uio.uio_segflg = UIO_SYSSPACE;
558 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 559 uio.uio_td = curthread;
c7e98b2f 560 error = procfs_domem(curp, lp, NULL, &uio);
984263bc
MD
561 if (uio.uio_resid != 0) {
562 /*
563 * XXX procfs_domem() doesn't currently return ENOSPC,
564 * so I think write() can bogusly return 0.
565 * XXX what happens for short writes? We don't want
566 * to write partial data.
567 * XXX procfs_domem() returns EPERM for other invalid
568 * addresses. Convert this to EINVAL. Does this
569 * clobber returns of EPERM for other reasons?
570 */
571 if (error == 0 || error == ENOSPC || error == EPERM)
572 error = EINVAL; /* EOF */
573 }
574 if (!write)
90b9818c 575 *res = tmp;
5686ec5a 576 lwkt_reltoken(&p->p_token);
58c2553a 577 PRELE(p);
24c69223 578 lwkt_reltoken(&proc_token);
984263bc
MD
579 return (error);
580
581 case PT_IO:
1301b4d8
MD
582 /*
583 * NOTE! uio_offset represents the offset in the target
584 * process. The iov is in the current process (the guy
585 * making the ptrace call) so uio_td must be the current
586 * process.
587 */
984263bc
MD
588 piod = addr;
589 iov.iov_base = piod->piod_addr;
590 iov.iov_len = piod->piod_len;
591 uio.uio_iov = &iov;
592 uio.uio_iovcnt = 1;
593 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
594 uio.uio_resid = piod->piod_len;
595 uio.uio_segflg = UIO_USERSPACE;
08f2f1bb 596 uio.uio_td = curthread;
984263bc
MD
597 switch (piod->piod_op) {
598 case PIOD_READ_D:
599 case PIOD_READ_I:
600 uio.uio_rw = UIO_READ;
601 break;
602 case PIOD_WRITE_D:
603 case PIOD_WRITE_I:
604 uio.uio_rw = UIO_WRITE;
605 break;
606 default:
5686ec5a 607 lwkt_reltoken(&p->p_token);
58c2553a 608 PRELE(p);
24c69223 609 lwkt_reltoken(&proc_token);
984263bc
MD
610 return (EINVAL);
611 }
c7e98b2f 612 error = procfs_domem(curp, lp, NULL, &uio);
984263bc 613 piod->piod_len -= uio.uio_resid;
5686ec5a 614 lwkt_reltoken(&p->p_token);
58c2553a 615 PRELE(p);
24c69223 616 lwkt_reltoken(&proc_token);
984263bc
MD
617 return (error);
618
619 case PT_KILL:
620 data = SIGKILL;
621 goto sendsig; /* in PT_CONTINUE above */
622
623#ifdef PT_SETREGS
624 case PT_SETREGS:
625 write = 1;
626 /* fallthrough */
627#endif /* PT_SETREGS */
628#ifdef PT_GETREGS
629 case PT_GETREGS:
630 /* write = 0 above */
631#endif /* PT_SETREGS */
632#if defined(PT_SETREGS) || defined(PT_GETREGS)
24c69223 633 if (!procfs_validregs(lp)) { /* no P_SYSTEM procs please */
5686ec5a 634 lwkt_reltoken(&p->p_token);
58c2553a 635 PRELE(p);
24c69223 636 lwkt_reltoken(&proc_token);
984263bc 637 return EINVAL;
24c69223 638 } else {
984263bc
MD
639 iov.iov_base = addr;
640 iov.iov_len = sizeof(struct reg);
641 uio.uio_iov = &iov;
642 uio.uio_iovcnt = 1;
643 uio.uio_offset = 0;
644 uio.uio_resid = sizeof(struct reg);
645 uio.uio_segflg = UIO_SYSSPACE;
646 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 647 uio.uio_td = curthread;
24c69223 648 t = procfs_doregs(curp, lp, NULL, &uio);
5686ec5a 649 lwkt_reltoken(&p->p_token);
58c2553a 650 PRELE(p);
24c69223
VS
651 lwkt_reltoken(&proc_token);
652 return t;
984263bc
MD
653 }
654#endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
655
656#ifdef PT_SETFPREGS
657 case PT_SETFPREGS:
658 write = 1;
659 /* fallthrough */
660#endif /* PT_SETFPREGS */
661#ifdef PT_GETFPREGS
662 case PT_GETFPREGS:
663 /* write = 0 above */
664#endif /* PT_SETFPREGS */
665#if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
24c69223 666 if (!procfs_validfpregs(lp)) { /* no P_SYSTEM procs please */
5686ec5a 667 lwkt_reltoken(&p->p_token);
58c2553a 668 PRELE(p);
24c69223 669 lwkt_reltoken(&proc_token);
984263bc 670 return EINVAL;
24c69223 671 } else {
984263bc
MD
672 iov.iov_base = addr;
673 iov.iov_len = sizeof(struct fpreg);
674 uio.uio_iov = &iov;
675 uio.uio_iovcnt = 1;
676 uio.uio_offset = 0;
677 uio.uio_resid = sizeof(struct fpreg);
678 uio.uio_segflg = UIO_SYSSPACE;
679 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 680 uio.uio_td = curthread;
24c69223 681 t = procfs_dofpregs(curp, lp, NULL, &uio);
5686ec5a 682 lwkt_reltoken(&p->p_token);
58c2553a 683 PRELE(p);
24c69223
VS
684 lwkt_reltoken(&proc_token);
685 return t;
984263bc
MD
686 }
687#endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
688
689#ifdef PT_SETDBREGS
690 case PT_SETDBREGS:
691 write = 1;
692 /* fallthrough */
693#endif /* PT_SETDBREGS */
694#ifdef PT_GETDBREGS
695 case PT_GETDBREGS:
696 /* write = 0 above */
697#endif /* PT_SETDBREGS */
698#if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
24c69223 699 if (!procfs_validdbregs(lp)) { /* no P_SYSTEM procs please */
5686ec5a 700 lwkt_reltoken(&p->p_token);
58c2553a 701 PRELE(p);
24c69223 702 lwkt_reltoken(&proc_token);
984263bc 703 return EINVAL;
24c69223 704 } else {
984263bc
MD
705 iov.iov_base = addr;
706 iov.iov_len = sizeof(struct dbreg);
707 uio.uio_iov = &iov;
708 uio.uio_iovcnt = 1;
709 uio.uio_offset = 0;
710 uio.uio_resid = sizeof(struct dbreg);
711 uio.uio_segflg = UIO_SYSSPACE;
712 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 713 uio.uio_td = curthread;
24c69223 714 t = procfs_dodbregs(curp, lp, NULL, &uio);
5686ec5a 715 lwkt_reltoken(&p->p_token);
58c2553a 716 PRELE(p);
24c69223
VS
717 lwkt_reltoken(&proc_token);
718 return t;
984263bc
MD
719 }
720#endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
721
722 default:
723 break;
724 }
725
5686ec5a 726 lwkt_reltoken(&p->p_token);
58c2553a 727 PRELE(p);
24c69223 728 lwkt_reltoken(&proc_token);
58c2553a 729
984263bc
MD
730 return 0;
731}
732
733int
c972a82f 734trace_req(struct proc *p)
984263bc
MD
735{
736 return 1;
737}
738
739/*
740 * stopevent()
270ac911
MD
741 *
742 * Stop a process because of a procfs event. Stay stopped until p->p_step
743 * is cleared (cleared by PIOCCONT in procfs).
744 *
745 * MPSAFE
984263bc 746 */
984263bc 747void
a2a5ad0d
MD
748stopevent(struct proc *p, unsigned int event, unsigned int val)
749{
e193508c
MD
750 /*
751 * Set event info. Recheck p_stops in case we are
752 * racing a close() on procfs.
753 */
287a8577 754 spin_lock(&p->p_spin);
e193508c 755 if ((p->p_stops & event) == 0) {
287a8577 756 spin_unlock(&p->p_spin);
e193508c
MD
757 return;
758 }
759 p->p_xstat = val;
760 p->p_stype = event;
984263bc 761 p->p_step = 1;
e193508c 762 tsleep_interlock(&p->p_step, 0);
287a8577 763 spin_unlock(&p->p_spin);
984263bc 764
e193508c
MD
765 /*
766 * Wakeup any PIOCWAITing procs and wait for p_step to
767 * be cleared.
768 */
769 for (;;) {
770 wakeup(&p->p_stype);
771 tsleep(&p->p_step, PINTERLOCKED, "stopevent", 0);
287a8577 772 spin_lock(&p->p_spin);
e193508c 773 if (p->p_step == 0) {
287a8577 774 spin_unlock(&p->p_spin);
e193508c
MD
775 break;
776 }
777 tsleep_interlock(&p->p_step, 0);
287a8577 778 spin_unlock(&p->p_spin);
e193508c 779 }
984263bc 780}
a2a5ad0d 781