kernel - Add per-process token, adjust signal code to use it.
[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) {
96 vm_object_reference (object);
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);
159 if (rv != KERN_SUCCESS) {
160 return EINVAL;
161 }
162
163 /*
164 * Okay, we've got the page. Let's release tmap.
165 */
166
a108bf71 167 vm_map_lookup_done (tmap, out_entry, 0);
984263bc
MD
168
169 /*
170 * Fault the page in...
171 */
172
173 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
174 if (rv != KERN_SUCCESS)
175 return EFAULT;
176
177 /* Find space in kernel_map for the page we're interested in */
e4846942 178 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
9388fcaa
MD
179 &kva,
180 PAGE_SIZE, PAGE_SIZE,
181 0, VM_MAPTYPE_NORMAL,
1b874851
MD
182 VM_PROT_ALL, VM_PROT_ALL,
183 0);
984263bc
MD
184 if (!rv) {
185 vm_object_reference (object);
186
e4846942 187 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
984263bc
MD
188 if (!rv) {
189 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
190 }
e4846942 191 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
984263bc
MD
192 }
193
194 if (fix_prot)
195 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
196 VM_PROT_READ|VM_PROT_EXECUTE, 0);
197 return rv;
198}
199#endif
200
201/*
202 * Process debugging system call.
3919ced0
MD
203 *
204 * MPALMOSTSAFE
984263bc 205 */
984263bc 206int
753fd850 207sys_ptrace(struct ptrace_args *uap)
984263bc 208{
41c20dac
MD
209 struct proc *p = curproc;
210
984263bc
MD
211 /*
212 * XXX this obfuscation is to reduce stack usage, but the register
213 * structs may be too large to put on the stack anyway.
214 */
215 union {
216 struct ptrace_io_desc piod;
217 struct dbreg dbreg;
218 struct fpreg fpreg;
219 struct reg reg;
220 } r;
221 void *addr;
222 int error = 0;
223
224 addr = &r;
225 switch (uap->req) {
226 case PT_GETREGS:
227 case PT_GETFPREGS:
228#ifdef PT_GETDBREGS
229 case PT_GETDBREGS:
230#endif
231 break;
232 case PT_SETREGS:
233 error = copyin(uap->addr, &r.reg, sizeof r.reg);
234 break;
235 case PT_SETFPREGS:
236 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
237 break;
238#ifdef PT_SETDBREGS
239 case PT_SETDBREGS:
240 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
241 break;
242#endif
243 case PT_IO:
244 error = copyin(uap->addr, &r.piod, sizeof r.piod);
245 break;
246 default:
247 addr = uap->addr;
248 }
249 if (error)
250 return (error);
251
90b9818c 252 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
c7114eea 253 &uap->sysmsg_result);
984263bc
MD
254 if (error)
255 return (error);
256
257 switch (uap->req) {
258 case PT_IO:
259 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
260 break;
261 case PT_GETREGS:
262 error = copyout(&r.reg, uap->addr, sizeof r.reg);
263 break;
264 case PT_GETFPREGS:
265 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
266 break;
267#ifdef PT_GETDBREGS
268 case PT_GETDBREGS:
269 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
270 break;
271#endif
272 }
273
274 return (error);
275}
276
277int
5686ec5a
MD
278kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr,
279 int data, int *res)
984263bc
MD
280{
281 struct proc *p, *pp;
08f2f1bb 282 struct lwp *lp;
984263bc
MD
283 struct iovec iov;
284 struct uio uio;
285 struct ptrace_io_desc *piod;
286 int error = 0;
d767fe64 287 int write, tmp;
24c69223
VS
288 int t;
289
290 lwkt_gettoken(&proc_token);
984263bc
MD
291
292 write = 0;
1301b4d8 293 if (req == PT_TRACE_ME) {
984263bc 294 p = curp;
1301b4d8 295 } else {
24c69223
VS
296 if ((p = pfind(pid)) == NULL) {
297 lwkt_reltoken(&proc_token);
984263bc 298 return ESRCH;
24c69223 299 }
984263bc 300 }
24c69223
VS
301 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
302 lwkt_reltoken(&proc_token);
984263bc 303 return (ESRCH);
24c69223 304 }
984263bc 305
5686ec5a 306 lwkt_gettoken(&p->p_token);
984263bc 307 /* Can't trace a process that's currently exec'ing. */
24c69223 308 if ((p->p_flag & P_INEXEC) != 0) {
5686ec5a 309 lwkt_reltoken(&p->p_token);
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);
24c69223 326 lwkt_reltoken(&proc_token);
984263bc 327 return EINVAL;
24c69223 328 }
984263bc
MD
329
330 /* Already traced */
24c69223 331 if (p->p_flag & P_TRACED) {
5686ec5a 332 lwkt_reltoken(&p->p_token);
24c69223 333 lwkt_reltoken(&proc_token);
984263bc 334 return EBUSY;
24c69223 335 }
984263bc
MD
336
337 if (curp->p_flag & P_TRACED)
338 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
24c69223 339 if (pp == p) {
5686ec5a 340 lwkt_reltoken(&p->p_token);
24c69223 341 lwkt_reltoken(&proc_token);
984263bc 342 return (EINVAL);
24c69223 343 }
984263bc
MD
344
345 /* not owned by you, has done setuid (unless you're root) */
41c20dac 346 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
984263bc 347 (p->p_flag & P_SUGID)) {
24c69223 348 if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0) {
5686ec5a 349 lwkt_reltoken(&p->p_token);
24c69223 350 lwkt_reltoken(&proc_token);
984263bc 351 return error;
24c69223 352 }
984263bc
MD
353 }
354
355 /* can't trace init when securelevel > 0 */
24c69223 356 if (securelevel > 0 && p->p_pid == 1) {
5686ec5a 357 lwkt_reltoken(&p->p_token);
24c69223 358 lwkt_reltoken(&proc_token);
984263bc 359 return EPERM;
24c69223 360 }
984263bc
MD
361
362 /* OK */
363 break;
364
365 case PT_READ_I:
366 case PT_READ_D:
367 case PT_WRITE_I:
368 case PT_WRITE_D:
369 case PT_IO:
370 case PT_CONTINUE:
371 case PT_KILL:
372 case PT_STEP:
373 case PT_DETACH:
374#ifdef PT_GETREGS
375 case PT_GETREGS:
376#endif
377#ifdef PT_SETREGS
378 case PT_SETREGS:
379#endif
380#ifdef PT_GETFPREGS
381 case PT_GETFPREGS:
382#endif
383#ifdef PT_SETFPREGS
384 case PT_SETFPREGS:
385#endif
386#ifdef PT_GETDBREGS
387 case PT_GETDBREGS:
388#endif
389#ifdef PT_SETDBREGS
390 case PT_SETDBREGS:
391#endif
392 /* not being traced... */
24c69223 393 if ((p->p_flag & P_TRACED) == 0) {
5686ec5a 394 lwkt_reltoken(&p->p_token);
24c69223 395 lwkt_reltoken(&proc_token);
984263bc 396 return EPERM;
24c69223 397 }
984263bc
MD
398
399 /* not being traced by YOU */
24c69223 400 if (p->p_pptr != curp) {
5686ec5a 401 lwkt_reltoken(&p->p_token);
24c69223 402 lwkt_reltoken(&proc_token);
984263bc 403 return EBUSY;
24c69223 404 }
984263bc
MD
405
406 /* not currently stopped */
164b8401 407 if (p->p_stat != SSTOP ||
344ad853 408 (p->p_flag & P_WAITED) == 0) {
5686ec5a 409 lwkt_reltoken(&p->p_token);
24c69223 410 lwkt_reltoken(&proc_token);
984263bc 411 return EBUSY;
344ad853 412 }
984263bc
MD
413
414 /* OK */
415 break;
416
417 default:
5686ec5a 418 lwkt_reltoken(&p->p_token);
24c69223 419 lwkt_reltoken(&proc_token);
984263bc
MD
420 return EINVAL;
421 }
422
08f2f1bb
SS
423 /* XXX lwp */
424 lp = FIRST_LWP_IN_PROC(p);
984263bc
MD
425#ifdef FIX_SSTEP
426 /*
427 * Single step fixup ala procfs
428 */
c7e98b2f 429 FIX_SSTEP(lp);
984263bc
MD
430#endif
431
432 /*
433 * Actually do the requests
434 */
435
90b9818c 436 *res = 0;
984263bc
MD
437
438 switch (req) {
439 case PT_TRACE_ME:
440 /* set my trace flag and "owner" so it can read/write me */
441 p->p_flag |= P_TRACED;
442 p->p_oppid = p->p_pptr->p_pid;
5686ec5a 443 lwkt_reltoken(&p->p_token);
24c69223 444 lwkt_reltoken(&proc_token);
984263bc
MD
445 return 0;
446
447 case PT_ATTACH:
448 /* security check done above */
449 p->p_flag |= P_TRACED;
450 p->p_oppid = p->p_pptr->p_pid;
451 if (p->p_pptr != curp)
452 proc_reparent(p, curp);
453 data = SIGSTOP;
454 goto sendsig; /* in PT_CONTINUE below */
455
456 case PT_STEP:
457 case PT_CONTINUE:
458 case PT_DETACH:
43300f51 459 /* Zero means do not send any signal */
24c69223 460 if (data < 0 || data > _SIG_MAXSIG) {
5686ec5a 461 lwkt_reltoken(&p->p_token);
24c69223 462 lwkt_reltoken(&proc_token);
984263bc 463 return EINVAL;
24c69223 464 }
984263bc 465
c7e98b2f 466 LWPHOLD(lp);
984263bc
MD
467
468 if (req == PT_STEP) {
08f2f1bb 469 if ((error = ptrace_single_step (lp))) {
c7e98b2f 470 LWPRELE(lp);
5686ec5a 471 lwkt_reltoken(&p->p_token);
24c69223 472 lwkt_reltoken(&proc_token);
984263bc
MD
473 return error;
474 }
475 }
476
477 if (addr != (void *)1) {
08f2f1bb 478 if ((error = ptrace_set_pc (lp,
984263bc 479 (u_long)(uintfptr_t)addr))) {
c7e98b2f 480 LWPRELE(lp);
5686ec5a 481 lwkt_reltoken(&p->p_token);
24c69223 482 lwkt_reltoken(&proc_token);
984263bc
MD
483 return error;
484 }
485 }
c7e98b2f 486 LWPRELE(lp);
984263bc
MD
487
488 if (req == PT_DETACH) {
489 /* reset process parent */
490 if (p->p_oppid != p->p_pptr->p_pid) {
491 struct proc *pp;
492
493 pp = pfind(p->p_oppid);
494 proc_reparent(p, pp ? pp : initproc);
495 }
496
497 p->p_flag &= ~(P_TRACED | P_WAITED);
498 p->p_oppid = 0;
499
500 /* should we send SIGCHLD? */
501 }
502
503 sendsig:
344ad853
MD
504 /*
505 * Deliver or queue signal. If the process is stopped
9a379a4a 506 * force it to be SACTIVE again.
344ad853 507 */
e43a034f 508 crit_enter();
164b8401 509 if (p->p_stat == SSTOP) {
984263bc 510 p->p_xstat = data;
9a379a4a
SS
511 lp->lwp_flag |= LWP_BREAKTSLEEP;
512 proc_unstop(p);
984263bc 513 } else if (data) {
84204577 514 ksignal(p, data);
984263bc 515 }
e43a034f 516 crit_exit();
5686ec5a 517 lwkt_reltoken(&p->p_token);
24c69223 518 lwkt_reltoken(&proc_token);
984263bc
MD
519 return 0;
520
521 case PT_WRITE_I:
522 case PT_WRITE_D:
523 write = 1;
524 /* fallthrough */
525 case PT_READ_I:
526 case PT_READ_D:
1301b4d8
MD
527 /*
528 * NOTE! uio_offset represents the offset in the target
529 * process. The iov is in the current process (the guy
530 * making the ptrace call) so uio_td must be the current
531 * process (though for a SYSSPACE transfer it doesn't
532 * really matter).
533 */
984263bc
MD
534 tmp = 0;
535 /* write = 0 set above */
536 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
537 iov.iov_len = sizeof(int);
538 uio.uio_iov = &iov;
539 uio.uio_iovcnt = 1;
540 uio.uio_offset = (off_t)(uintptr_t)addr;
541 uio.uio_resid = sizeof(int);
542 uio.uio_segflg = UIO_SYSSPACE;
543 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 544 uio.uio_td = curthread;
c7e98b2f 545 error = procfs_domem(curp, lp, NULL, &uio);
984263bc
MD
546 if (uio.uio_resid != 0) {
547 /*
548 * XXX procfs_domem() doesn't currently return ENOSPC,
549 * so I think write() can bogusly return 0.
550 * XXX what happens for short writes? We don't want
551 * to write partial data.
552 * XXX procfs_domem() returns EPERM for other invalid
553 * addresses. Convert this to EINVAL. Does this
554 * clobber returns of EPERM for other reasons?
555 */
556 if (error == 0 || error == ENOSPC || error == EPERM)
557 error = EINVAL; /* EOF */
558 }
559 if (!write)
90b9818c 560 *res = tmp;
5686ec5a 561 lwkt_reltoken(&p->p_token);
24c69223 562 lwkt_reltoken(&proc_token);
984263bc
MD
563 return (error);
564
565 case PT_IO:
1301b4d8
MD
566 /*
567 * NOTE! uio_offset represents the offset in the target
568 * process. The iov is in the current process (the guy
569 * making the ptrace call) so uio_td must be the current
570 * process.
571 */
984263bc
MD
572 piod = addr;
573 iov.iov_base = piod->piod_addr;
574 iov.iov_len = piod->piod_len;
575 uio.uio_iov = &iov;
576 uio.uio_iovcnt = 1;
577 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
578 uio.uio_resid = piod->piod_len;
579 uio.uio_segflg = UIO_USERSPACE;
08f2f1bb 580 uio.uio_td = curthread;
984263bc
MD
581 switch (piod->piod_op) {
582 case PIOD_READ_D:
583 case PIOD_READ_I:
584 uio.uio_rw = UIO_READ;
585 break;
586 case PIOD_WRITE_D:
587 case PIOD_WRITE_I:
588 uio.uio_rw = UIO_WRITE;
589 break;
590 default:
5686ec5a 591 lwkt_reltoken(&p->p_token);
24c69223 592 lwkt_reltoken(&proc_token);
984263bc
MD
593 return (EINVAL);
594 }
c7e98b2f 595 error = procfs_domem(curp, lp, NULL, &uio);
984263bc 596 piod->piod_len -= uio.uio_resid;
5686ec5a 597 lwkt_reltoken(&p->p_token);
24c69223 598 lwkt_reltoken(&proc_token);
984263bc
MD
599 return (error);
600
601 case PT_KILL:
602 data = SIGKILL;
603 goto sendsig; /* in PT_CONTINUE above */
604
605#ifdef PT_SETREGS
606 case PT_SETREGS:
607 write = 1;
608 /* fallthrough */
609#endif /* PT_SETREGS */
610#ifdef PT_GETREGS
611 case PT_GETREGS:
612 /* write = 0 above */
613#endif /* PT_SETREGS */
614#if defined(PT_SETREGS) || defined(PT_GETREGS)
24c69223 615 if (!procfs_validregs(lp)) { /* no P_SYSTEM procs please */
5686ec5a 616 lwkt_reltoken(&p->p_token);
24c69223 617 lwkt_reltoken(&proc_token);
984263bc 618 return EINVAL;
24c69223 619 } else {
984263bc
MD
620 iov.iov_base = addr;
621 iov.iov_len = sizeof(struct reg);
622 uio.uio_iov = &iov;
623 uio.uio_iovcnt = 1;
624 uio.uio_offset = 0;
625 uio.uio_resid = sizeof(struct reg);
626 uio.uio_segflg = UIO_SYSSPACE;
627 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 628 uio.uio_td = curthread;
24c69223 629 t = procfs_doregs(curp, lp, NULL, &uio);
5686ec5a 630 lwkt_reltoken(&p->p_token);
24c69223
VS
631 lwkt_reltoken(&proc_token);
632 return t;
984263bc
MD
633 }
634#endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
635
636#ifdef PT_SETFPREGS
637 case PT_SETFPREGS:
638 write = 1;
639 /* fallthrough */
640#endif /* PT_SETFPREGS */
641#ifdef PT_GETFPREGS
642 case PT_GETFPREGS:
643 /* write = 0 above */
644#endif /* PT_SETFPREGS */
645#if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
24c69223 646 if (!procfs_validfpregs(lp)) { /* no P_SYSTEM procs please */
5686ec5a 647 lwkt_reltoken(&p->p_token);
24c69223 648 lwkt_reltoken(&proc_token);
984263bc 649 return EINVAL;
24c69223 650 } else {
984263bc
MD
651 iov.iov_base = addr;
652 iov.iov_len = sizeof(struct fpreg);
653 uio.uio_iov = &iov;
654 uio.uio_iovcnt = 1;
655 uio.uio_offset = 0;
656 uio.uio_resid = sizeof(struct fpreg);
657 uio.uio_segflg = UIO_SYSSPACE;
658 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 659 uio.uio_td = curthread;
24c69223 660 t = procfs_dofpregs(curp, lp, NULL, &uio);
5686ec5a 661 lwkt_reltoken(&p->p_token);
24c69223
VS
662 lwkt_reltoken(&proc_token);
663 return t;
984263bc
MD
664 }
665#endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
666
667#ifdef PT_SETDBREGS
668 case PT_SETDBREGS:
669 write = 1;
670 /* fallthrough */
671#endif /* PT_SETDBREGS */
672#ifdef PT_GETDBREGS
673 case PT_GETDBREGS:
674 /* write = 0 above */
675#endif /* PT_SETDBREGS */
676#if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
24c69223 677 if (!procfs_validdbregs(lp)) { /* no P_SYSTEM procs please */
5686ec5a 678 lwkt_reltoken(&p->p_token);
24c69223 679 lwkt_reltoken(&proc_token);
984263bc 680 return EINVAL;
24c69223 681 } else {
984263bc
MD
682 iov.iov_base = addr;
683 iov.iov_len = sizeof(struct dbreg);
684 uio.uio_iov = &iov;
685 uio.uio_iovcnt = 1;
686 uio.uio_offset = 0;
687 uio.uio_resid = sizeof(struct dbreg);
688 uio.uio_segflg = UIO_SYSSPACE;
689 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
08f2f1bb 690 uio.uio_td = curthread;
24c69223 691 t = procfs_dodbregs(curp, lp, NULL, &uio);
5686ec5a 692 lwkt_reltoken(&p->p_token);
24c69223
VS
693 lwkt_reltoken(&proc_token);
694 return t;
984263bc
MD
695 }
696#endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
697
698 default:
699 break;
700 }
701
5686ec5a 702 lwkt_reltoken(&p->p_token);
24c69223 703 lwkt_reltoken(&proc_token);
984263bc
MD
704 return 0;
705}
706
707int
c972a82f 708trace_req(struct proc *p)
984263bc
MD
709{
710 return 1;
711}
712
713/*
714 * stopevent()
270ac911
MD
715 *
716 * Stop a process because of a procfs event. Stay stopped until p->p_step
717 * is cleared (cleared by PIOCCONT in procfs).
718 *
719 * MPSAFE
984263bc 720 */
984263bc 721void
a2a5ad0d
MD
722stopevent(struct proc *p, unsigned int event, unsigned int val)
723{
e193508c
MD
724 /*
725 * Set event info. Recheck p_stops in case we are
726 * racing a close() on procfs.
727 */
287a8577 728 spin_lock(&p->p_spin);
e193508c 729 if ((p->p_stops & event) == 0) {
287a8577 730 spin_unlock(&p->p_spin);
e193508c
MD
731 return;
732 }
733 p->p_xstat = val;
734 p->p_stype = event;
984263bc 735 p->p_step = 1;
e193508c 736 tsleep_interlock(&p->p_step, 0);
287a8577 737 spin_unlock(&p->p_spin);
984263bc 738
e193508c
MD
739 /*
740 * Wakeup any PIOCWAITing procs and wait for p_step to
741 * be cleared.
742 */
743 for (;;) {
744 wakeup(&p->p_stype);
745 tsleep(&p->p_step, PINTERLOCKED, "stopevent", 0);
287a8577 746 spin_lock(&p->p_spin);
e193508c 747 if (p->p_step == 0) {
287a8577 748 spin_unlock(&p->p_spin);
e193508c
MD
749 break;
750 }
751 tsleep_interlock(&p->p_step, 0);
287a8577 752 spin_unlock(&p->p_spin);
e193508c 753 }
984263bc 754}
a2a5ad0d 755