4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
32 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/cmn_err.h>
37 #include <sys/policy.h>
38 #include <sys/debug.h>
39 #include <sys/errno.h>
41 #include <sys/inline.h>
44 #include <sys/brand.h>
45 #include <sys/regset.h>
46 #include <sys/sysmacros.h>
47 #include <sys/systm.h>
49 #include <sys/vnode.h>
50 #include <sys/signal.h>
53 #include <sys/class.h>
54 #include <sys/fault.h>
55 #include <sys/syscall.h>
56 #include <sys/procfs.h>
58 #include <sys/copyops.h>
59 #include <sys/schedctl.h>
62 #include <sys/proc/prdata.h>
63 #include <sys/contract/process_impl.h>
65 static void pr_settrace(proc_t
*, sigset_t
*);
66 static int pr_setfpregs(prnode_t
*, prfpregset_t
*);
67 static int pr_setvaddr(prnode_t
*, caddr_t
);
68 static int pr_clearsig(prnode_t
*);
69 static int pr_clearflt(prnode_t
*);
70 static int pr_watch(prnode_t
*, prwatch_t
*, int *);
71 static int pr_agent(prnode_t
*, prgregset_t
, int *);
72 static int pr_rdwr(proc_t
*, enum uio_rw
, priovec_t
*);
73 static int pr_scred(proc_t
*, prcred_t
*, cred_t
*, boolean_t
);
74 static int pr_spriv(proc_t
*, prpriv_t
*, cred_t
*);
75 static int pr_szoneid(proc_t
*, zoneid_t
, cred_t
*);
76 static void pauselwps(proc_t
*);
77 static void unpauselwps(proc_t
*);
80 long sig
; /* PCKILL, PCUNKILL */
81 long nice
; /* PCNICE */
82 long timeo
; /* PCTWSTOP */
83 ulong_t flags
; /* PCRUN, PCSET, PCUNSET */
84 caddr_t vaddr
; /* PCSVADDR */
85 siginfo_t siginfo
; /* PCSSIG */
86 sigset_t sigset
; /* PCSTRACE, PCSHOLD */
87 fltset_t fltset
; /* PCSFAULT */
88 sysset_t sysset
; /* PCSENTRY, PCSEXIT */
89 prgregset_t prgregset
; /* PCSREG, PCAGENT */
90 prfpregset_t prfpregset
; /* PCSFPREG */
91 prwatch_t prwatch
; /* PCWATCH */
92 priovec_t priovec
; /* PCREAD, PCWRITE */
93 prcred_t prcred
; /* PCSCRED */
94 prpriv_t prpriv
; /* PCSPRIV */
95 long przoneid
; /* PCSZONE */
98 static int pr_control(long, arg_t
*, prnode_t
*, cred_t
*);
101 ctlsize(long cmd
, size_t resid
, arg_t
*argp
)
103 size_t size
= sizeof (long);
116 size
+= sizeof (siginfo_t
);
119 size
+= sizeof (long);
124 size
+= sizeof (long);
129 size
+= sizeof (ulong_t
);
132 size
+= sizeof (caddr_t
);
136 size
+= sizeof (sigset_t
);
139 size
+= sizeof (fltset_t
);
143 size
+= sizeof (sysset_t
);
147 size
+= sizeof (prgregset_t
);
150 size
+= sizeof (prfpregset_t
);
153 size
+= sizeof (prwatch_t
);
157 size
+= sizeof (priovec_t
);
160 size
+= sizeof (prcred_t
);
164 * We cannot derefence the pr_ngroups fields if it
165 * we don't have enough data.
167 if (resid
< size
+ sizeof (prcred_t
) - sizeof (gid_t
))
169 ngrp
= argp
->prcred
.pr_ngroups
;
170 if (ngrp
< 0 || ngrp
> ngroups_max
)
173 /* The result can be smaller than sizeof (prcred_t) */
174 size
+= sizeof (prcred_t
) - sizeof (gid_t
);
175 size
+= ngrp
* sizeof (gid_t
);
178 if (resid
>= size
+ sizeof (prpriv_t
))
179 size
+= priv_prgetprivsize(&argp
->prpriv
);
184 size
+= sizeof (long);
190 /* Round up to a multiple of long, unless exact amount written */
192 rnd
= size
& (sizeof (long) - 1);
195 size
+= sizeof (long) - rnd
;
204 * Control operations (lots).
207 prwritectl(vnode_t
*vp
, uio_t
*uiop
, cred_t
*cr
)
209 #define MY_BUFFER_SIZE \
210 100 > 1 + sizeof (arg_t) / sizeof (long) ? \
211 100 : 1 + sizeof (arg_t) / sizeof (long)
212 long buf
[MY_BUFFER_SIZE
];
216 prnode_t
*pnp
= VTOP(vp
);
220 while (uiop
->uio_resid
) {
222 * Read several commands in one gulp.
225 if (resid
) { /* move incomplete command to front of buffer */
228 if (resid
>= sizeof (buf
))
230 tail
= (long *)((char *)buf
+ sizeof (buf
) - resid
);
233 } while ((resid
-= sizeof (long)) != 0);
235 resid
= sizeof (buf
) - ((char *)bufp
- (char *)buf
);
236 if (resid
> uiop
->uio_resid
)
237 resid
= uiop
->uio_resid
;
238 if (error
= uiomove((caddr_t
)bufp
, resid
, UIO_WRITE
, uiop
))
240 resid
+= (char *)bufp
- (char *)buf
;
243 do { /* loop over commands in buffer */
245 arg_t
*argp
= (arg_t
*)&bufp
[1];
247 size
= ctlsize(cmd
, resid
, argp
);
248 if (size
== 0) /* incomplete or invalid command */
251 * Perform the specified control operation.
254 if ((error
= prlock(pnp
, ZNO
)) != 0)
258 if (error
= pr_control(cmd
, argp
, pnp
, cr
)) {
259 if (error
== -1) /* -1 is timeout */
264 bufp
= (long *)((char *)bufp
+ size
);
265 } while ((resid
-= size
) != 0);
272 return (resid
? EINVAL
: 0);
276 pr_control(long cmd
, arg_t
*argp
, prnode_t
*pnp
, cred_t
*cr
)
286 pcp
= pnp
->pr_common
;
290 /* System processes defy control. */
291 if (p
->p_flag
& SSYS
) {
302 case PCSTOP
: /* direct process or lwp to stop and wait for stop */
303 case PCDSTOP
: /* direct process or lwp to stop, don't wait */
304 case PCWSTOP
: /* wait for process or lwp to stop */
305 case PCTWSTOP
: /* wait for process or lwp to stop, with timeout */
310 * Can't apply to a system process.
312 if (p
->p_as
== &kas
) {
317 if (cmd
== PCSTOP
|| cmd
== PCDSTOP
)
324 * If an lwp is waiting for itself or its process,
325 * don't wait. The stopped lwp would never see the
326 * fact that it is stopped.
328 if ((pcp
->prc_flags
& PRC_LWP
)?
329 (pcp
->prc_thread
== curthread
) : (p
== curproc
)) {
330 if (cmd
== PCWSTOP
|| cmd
== PCTWSTOP
)
335 timeo
= (cmd
== PCTWSTOP
)? (time_t)argp
->timeo
: 0;
336 if ((error
= pr_wait_stop(pnp
, timeo
)) != 0)
342 case PCRUN
: /* make lwp or process runnable */
343 error
= pr_setrun(pnp
, argp
->flags
);
346 case PCSTRACE
: /* set signal trace mask */
347 pr_settrace(p
, &argp
->sigset
);
350 case PCSSIG
: /* set current signal */
351 error
= pr_setsig(pnp
, &argp
->siginfo
);
352 if (argp
->siginfo
.si_signo
== SIGKILL
&& error
== 0) {
359 case PCKILL
: /* send signal */
360 error
= pr_kill(pnp
, (int)argp
->sig
, cr
);
361 if (error
== 0 && argp
->sig
== SIGKILL
) {
368 case PCUNKILL
: /* delete a pending signal */
369 error
= pr_unkill(pnp
, (int)argp
->sig
);
372 case PCNICE
: /* set nice priority */
373 error
= pr_nice(p
, (int)argp
->nice
, cr
);
376 case PCSENTRY
: /* set syscall entry bit mask */
377 case PCSEXIT
: /* set syscall exit bit mask */
378 pr_setentryexit(p
, &argp
->sysset
, cmd
== PCSENTRY
);
381 case PCSET
: /* set process flags */
382 error
= pr_set(p
, argp
->flags
);
385 case PCUNSET
: /* unset process flags */
386 error
= pr_unset(p
, argp
->flags
);
389 case PCSREG
: /* set general registers */
391 kthread_t
*t
= pr_thread(pnp
);
393 if (!ISTOPPED(t
) && !VSTOPPED(t
) && !DSTOPPED(t
)) {
398 mutex_exit(&p
->p_lock
);
399 prsetprregs(ttolwp(t
), argp
->prgregset
, 0);
400 mutex_enter(&p
->p_lock
);
405 case PCSFPREG
: /* set floating-point registers */
406 error
= pr_setfpregs(pnp
, &argp
->prfpregset
);
409 case PCSXREG
: /* set extra registers */
414 case PCSVADDR
: /* set virtual address at which to resume */
415 error
= pr_setvaddr(pnp
, argp
->vaddr
);
418 case PCSHOLD
: /* set signal-hold mask */
419 pr_sethold(pnp
, &argp
->sigset
);
422 case PCSFAULT
: /* set mask of traced faults */
423 pr_setfault(p
, &argp
->fltset
);
426 case PCCSIG
: /* clear current signal */
427 error
= pr_clearsig(pnp
);
430 case PCCFAULT
: /* clear current fault */
431 error
= pr_clearflt(pnp
);
434 case PCWATCH
: /* set or clear watched areas */
435 error
= pr_watch(pnp
, &argp
->prwatch
, &unlocked
);
436 if (error
&& unlocked
)
440 case PCAGENT
: /* create the /proc agent lwp in the target process */
441 error
= pr_agent(pnp
, argp
->prgregset
, &unlocked
);
442 if (error
&& unlocked
)
446 case PCREAD
: /* read from the address space */
447 error
= pr_rdwr(p
, UIO_READ
, &argp
->priovec
);
450 case PCWRITE
: /* write to the address space */
451 error
= pr_rdwr(p
, UIO_WRITE
, &argp
->priovec
);
454 case PCSCRED
: /* set the process credentials */
456 error
= pr_scred(p
, &argp
->prcred
, cr
, cmd
== PCSCREDX
);
459 case PCSPRIV
: /* set the process privileges */
460 error
= pr_spriv(p
, &argp
->prpriv
, cr
);
462 case PCSZONE
: /* set the process's zoneid credentials */
463 error
= pr_szoneid(p
, (zoneid_t
)argp
->przoneid
, cr
);
472 #ifdef _SYSCALL32_IMPL
475 int32_t sig
; /* PCKILL, PCUNKILL */
476 int32_t nice
; /* PCNICE */
477 int32_t timeo
; /* PCTWSTOP */
478 uint32_t flags
; /* PCRUN, PCSET, PCUNSET */
479 caddr32_t vaddr
; /* PCSVADDR */
480 siginfo32_t siginfo
; /* PCSSIG */
481 sigset_t sigset
; /* PCSTRACE, PCSHOLD */
482 fltset_t fltset
; /* PCSFAULT */
483 sysset_t sysset
; /* PCSENTRY, PCSEXIT */
484 prgregset32_t prgregset
; /* PCSREG, PCAGENT */
485 prfpregset32_t prfpregset
; /* PCSFPREG */
487 prxregset_t prxregset
; /* PCSXREG */
489 prwatch32_t prwatch
; /* PCWATCH */
490 priovec32_t priovec
; /* PCREAD, PCWRITE */
491 prcred32_t prcred
; /* PCSCRED */
492 prpriv_t prpriv
; /* PCSPRIV */
493 int32_t przoneid
; /* PCSZONE */
496 static int pr_control32(int32_t, arg32_t
*, prnode_t
*, cred_t
*);
497 static int pr_setfpregs32(prnode_t
*, prfpregset32_t
*);
500 * Note that while ctlsize32() can use argp, it must do so only in a way
501 * that assumes 32-bit rather than 64-bit alignment as argp is a pointer
502 * to an array of 32-bit values and only 32-bit alignment is ensured.
505 ctlsize32(int32_t cmd
, size_t resid
, arg32_t
*argp
)
507 size_t size
= sizeof (int32_t);
520 size
+= sizeof (siginfo32_t
);
523 size
+= sizeof (int32_t);
528 size
+= sizeof (int32_t);
533 size
+= sizeof (uint32_t);
536 size
+= sizeof (caddr32_t
);
540 size
+= sizeof (sigset_t
);
543 size
+= sizeof (fltset_t
);
547 size
+= sizeof (sysset_t
);
551 size
+= sizeof (prgregset32_t
);
554 size
+= sizeof (prfpregset32_t
);
558 size
+= sizeof (prxregset_t
);
562 size
+= sizeof (prwatch32_t
);
566 size
+= sizeof (priovec32_t
);
569 size
+= sizeof (prcred32_t
);
573 * We cannot derefence the pr_ngroups fields if it
574 * we don't have enough data.
576 if (resid
< size
+ sizeof (prcred32_t
) - sizeof (gid32_t
))
578 ngrp
= argp
->prcred
.pr_ngroups
;
579 if (ngrp
< 0 || ngrp
> ngroups_max
)
582 /* The result can be smaller than sizeof (prcred32_t) */
583 size
+= sizeof (prcred32_t
) - sizeof (gid32_t
);
584 size
+= ngrp
* sizeof (gid32_t
);
587 if (resid
>= size
+ sizeof (prpriv_t
))
588 size
+= priv_prgetprivsize(&argp
->prpriv
);
593 size
+= sizeof (int32_t);
599 /* Round up to a multiple of int32_t */
600 rnd
= size
& (sizeof (int32_t) - 1);
603 size
+= sizeof (int32_t) - rnd
;
611 * Control operations (lots).
614 prwritectl32(struct vnode
*vp
, struct uio
*uiop
, cred_t
*cr
)
616 #define MY_BUFFER_SIZE32 \
617 100 > 1 + sizeof (arg32_t) / sizeof (int32_t) ? \
618 100 : 1 + sizeof (arg32_t) / sizeof (int32_t)
619 int32_t buf
[MY_BUFFER_SIZE32
];
624 prnode_t
*pnp
= VTOP(vp
);
628 while (uiop
->uio_resid
) {
630 * Read several commands in one gulp.
633 if (resid
) { /* move incomplete command to front of buffer */
636 if (resid
>= sizeof (buf
))
638 tail
= (int32_t *)((char *)buf
+ sizeof (buf
) - resid
);
641 } while ((resid
-= sizeof (int32_t)) != 0);
643 resid
= sizeof (buf
) - ((char *)bufp
- (char *)buf
);
644 if (resid
> uiop
->uio_resid
)
645 resid
= uiop
->uio_resid
;
646 if (error
= uiomove((caddr_t
)bufp
, resid
, UIO_WRITE
, uiop
))
648 resid
+= (char *)bufp
- (char *)buf
;
651 do { /* loop over commands in buffer */
652 int32_t cmd
= bufp
[0];
653 arg32_t
*argp
= (arg32_t
*)&bufp
[1];
655 size
= ctlsize32(cmd
, resid
, argp
);
656 if (size
== 0) /* incomplete or invalid command */
659 * Perform the specified control operation.
662 if ((error
= prlock(pnp
, ZNO
)) != 0)
668 * Since some members of the arg32_t union contain
669 * 64-bit values (which must be 64-bit aligned), we
670 * can't simply pass a pointer to the structure as
671 * it may be unaligned. Note that we do pass the
672 * potentially unaligned structure to ctlsize32()
673 * above, but that uses it a way that makes no
674 * assumptions about alignment.
676 ASSERT(size
- sizeof (cmd
) <= sizeof (arg
));
677 bcopy(argp
, &arg
, size
- sizeof (cmd
));
679 if (error
= pr_control32(cmd
, &arg
, pnp
, cr
)) {
680 if (error
== -1) /* -1 is timeout */
685 bufp
= (int32_t *)((char *)bufp
+ size
);
686 } while ((resid
-= size
) != 0);
693 return (resid
? EINVAL
: 0);
697 pr_control32(int32_t cmd
, arg32_t
*argp
, prnode_t
*pnp
, cred_t
*cr
)
707 pcp
= pnp
->pr_common
;
711 if (p
->p_flag
& SSYS
) {
722 case PCSTOP
: /* direct process or lwp to stop and wait for stop */
723 case PCDSTOP
: /* direct process or lwp to stop, don't wait */
724 case PCWSTOP
: /* wait for process or lwp to stop */
725 case PCTWSTOP
: /* wait for process or lwp to stop, with timeout */
730 * Can't apply to a system process.
732 if (p
->p_as
== &kas
) {
737 if (cmd
== PCSTOP
|| cmd
== PCDSTOP
)
744 * If an lwp is waiting for itself or its process,
745 * don't wait. The lwp will never see the fact that
748 if ((pcp
->prc_flags
& PRC_LWP
)?
749 (pcp
->prc_thread
== curthread
) : (p
== curproc
)) {
750 if (cmd
== PCWSTOP
|| cmd
== PCTWSTOP
)
755 timeo
= (cmd
== PCTWSTOP
)? (time_t)argp
->timeo
: 0;
756 if ((error
= pr_wait_stop(pnp
, timeo
)) != 0)
762 case PCRUN
: /* make lwp or process runnable */
763 error
= pr_setrun(pnp
, (ulong_t
)argp
->flags
);
766 case PCSTRACE
: /* set signal trace mask */
767 pr_settrace(p
, &argp
->sigset
);
770 case PCSSIG
: /* set current signal */
771 if (PROCESS_NOT_32BIT(p
))
774 int sig
= (int)argp
->siginfo
.si_signo
;
777 bzero(&siginfo
, sizeof (siginfo
));
778 siginfo_32tok(&argp
->siginfo
, (k_siginfo_t
*)&siginfo
);
779 error
= pr_setsig(pnp
, &siginfo
);
780 if (sig
== SIGKILL
&& error
== 0) {
788 case PCKILL
: /* send signal */
789 error
= pr_kill(pnp
, (int)argp
->sig
, cr
);
790 if (error
== 0 && argp
->sig
== SIGKILL
) {
797 case PCUNKILL
: /* delete a pending signal */
798 error
= pr_unkill(pnp
, (int)argp
->sig
);
801 case PCNICE
: /* set nice priority */
802 error
= pr_nice(p
, (int)argp
->nice
, cr
);
805 case PCSENTRY
: /* set syscall entry bit mask */
806 case PCSEXIT
: /* set syscall exit bit mask */
807 pr_setentryexit(p
, &argp
->sysset
, cmd
== PCSENTRY
);
810 case PCSET
: /* set process flags */
811 error
= pr_set(p
, (long)argp
->flags
);
814 case PCUNSET
: /* unset process flags */
815 error
= pr_unset(p
, (long)argp
->flags
);
818 case PCSREG
: /* set general registers */
819 if (PROCESS_NOT_32BIT(p
))
822 kthread_t
*t
= pr_thread(pnp
);
824 if (!ISTOPPED(t
) && !VSTOPPED(t
) && !DSTOPPED(t
)) {
828 prgregset_t prgregset
;
829 klwp_t
*lwp
= ttolwp(t
);
832 mutex_exit(&p
->p_lock
);
833 prgregset_32ton(lwp
, argp
->prgregset
,
835 prsetprregs(lwp
, prgregset
, 0);
836 mutex_enter(&p
->p_lock
);
841 case PCSFPREG
: /* set floating-point registers */
842 if (PROCESS_NOT_32BIT(p
))
845 error
= pr_setfpregs32(pnp
, &argp
->prfpregset
);
848 case PCSXREG
: /* set extra registers */
850 if (PROCESS_NOT_32BIT(p
))
853 error
= pr_setxregs(pnp
, &argp
->prxregset
);
859 case PCSVADDR
: /* set virtual address at which to resume */
860 if (PROCESS_NOT_32BIT(p
))
863 error
= pr_setvaddr(pnp
,
864 (caddr_t
)(uintptr_t)argp
->vaddr
);
867 case PCSHOLD
: /* set signal-hold mask */
868 pr_sethold(pnp
, &argp
->sigset
);
871 case PCSFAULT
: /* set mask of traced faults */
872 pr_setfault(p
, &argp
->fltset
);
875 case PCCSIG
: /* clear current signal */
876 error
= pr_clearsig(pnp
);
879 case PCCFAULT
: /* clear current fault */
880 error
= pr_clearflt(pnp
);
883 case PCWATCH
: /* set or clear watched areas */
884 if (PROCESS_NOT_32BIT(p
))
889 prwatch
.pr_vaddr
= argp
->prwatch
.pr_vaddr
;
890 prwatch
.pr_size
= argp
->prwatch
.pr_size
;
891 prwatch
.pr_wflags
= argp
->prwatch
.pr_wflags
;
892 prwatch
.pr_pad
= argp
->prwatch
.pr_pad
;
893 error
= pr_watch(pnp
, &prwatch
, &unlocked
);
894 if (error
&& unlocked
)
899 case PCAGENT
: /* create the /proc agent lwp in the target process */
900 if (PROCESS_NOT_32BIT(p
))
903 prgregset_t prgregset
;
904 kthread_t
*t
= pr_thread(pnp
);
905 klwp_t
*lwp
= ttolwp(t
);
907 mutex_exit(&p
->p_lock
);
908 prgregset_32ton(lwp
, argp
->prgregset
, prgregset
);
909 mutex_enter(&p
->p_lock
);
910 error
= pr_agent(pnp
, prgregset
, &unlocked
);
911 if (error
&& unlocked
)
916 case PCREAD
: /* read from the address space */
917 case PCWRITE
: /* write to the address space */
918 if (PROCESS_NOT_32BIT(p
) || (pnp
->pr_flags
& PR_OFFMAX
))
921 enum uio_rw rw
= (cmd
== PCREAD
)? UIO_READ
: UIO_WRITE
;
925 (void *)(uintptr_t)argp
->priovec
.pio_base
;
926 priovec
.pio_len
= (size_t)argp
->priovec
.pio_len
;
927 priovec
.pio_offset
= (off_t
)
928 (uint32_t)argp
->priovec
.pio_offset
;
929 error
= pr_rdwr(p
, rw
, &priovec
);
933 case PCSCRED
: /* set the process credentials */
937 * All the fields in these structures are exactly the
938 * same and so the structures are compatible. In case
939 * this ever changes, we catch this with the ASSERT
942 prcred_t
*prcred
= (prcred_t
*)&argp
->prcred
;
944 ASSERT(sizeof (prcred_t
) == sizeof (prcred32_t
));
946 error
= pr_scred(p
, prcred
, cr
, cmd
== PCSCREDX
);
950 case PCSPRIV
: /* set the process privileges */
951 error
= pr_spriv(p
, &argp
->prpriv
, cr
);
954 case PCSZONE
: /* set the process's zoneid */
955 error
= pr_szoneid(p
, (zoneid_t
)argp
->przoneid
, cr
);
964 #endif /* _SYSCALL32_IMPL */
967 * Return the specific or chosen thread/lwp for a control operation.
968 * Returns with the thread locked via thread_lock(t).
971 pr_thread(prnode_t
*pnp
)
973 prcommon_t
*pcp
= pnp
->pr_common
;
976 if (pcp
->prc_flags
& PRC_LWP
) {
981 proc_t
*p
= pcp
->prc_proc
;
982 t
= prchoose(p
); /* returns locked thread */
990 * Direct the process or lwp to stop.
993 pr_stop(prnode_t
*pnp
)
995 prcommon_t
*pcp
= pnp
->pr_common
;
996 proc_t
*p
= pcp
->prc_proc
;
1001 * If already stopped, do nothing; otherwise flag
1002 * it to be stopped the next time it tries to run.
1003 * If sleeping at interruptible priority, set it
1004 * running so it will stop within cv_wait_sig().
1006 * Take care to cooperate with jobcontrol: if an lwp
1007 * is stopped due to the default action of a jobcontrol
1008 * stop signal, flag it to be stopped the next time it
1009 * starts due to a SIGCONT signal.
1011 if (pcp
->prc_flags
& PRC_LWP
)
1012 t
= pcp
->prc_thread
;
1023 t
->t_proc_flag
|= TP_PRSTOP
;
1024 t
->t_sig_check
= 1; /* do ISSIG */
1027 /* Move the thread from wait queue to run queue */
1031 if (ISWAKEABLE(t
)) {
1032 if (t
->t_wchan0
== NULL
)
1034 else if (!VSTOPPED(t
)) {
1036 * Mark it virtually stopped.
1038 t
->t_proc_flag
|= TP_PRVSTOP
;
1043 * force the thread into the kernel
1044 * if it is not already there.
1049 (vp
= p
->p_lwpdir
[t
->t_dslot
].ld_entry
->le_trace
) != NULL
)
1051 if (pcp
->prc_flags
& PRC_LWP
)
1053 } while ((t
= t
->t_forw
) != p
->p_tlist
);
1056 * We do this just in case the thread we asked
1057 * to stop is in holdlwps() (called from cfork()).
1059 cv_broadcast(&p
->p_holdlwps
);
1063 * Sleep until the lwp stops, but cooperate with
1064 * jobcontrol: Don't wake up if the lwp is stopped
1065 * due to the default action of a jobcontrol stop signal.
1066 * If this is the process file descriptor, sleep
1067 * until all of the process's lwps stop.
1070 pr_wait_stop(prnode_t
*pnp
, time_t timeo
)
1072 prcommon_t
*pcp
= pnp
->pr_common
;
1073 proc_t
*p
= pcp
->prc_proc
;
1075 timestruc_t
*rqtp
= NULL
;
1080 if (timeo
> 0) { /* millisecond timeout */
1082 * Determine the precise future time of the requested timeout.
1086 timecheck
= timechanged
;
1089 rqtp
->tv_sec
= timeo
/ MILLISEC
;
1090 rqtp
->tv_nsec
= (timeo
% MILLISEC
) * MICROSEC
;
1091 timespecadd(rqtp
, &now
);
1094 if (pcp
->prc_flags
& PRC_LWP
) { /* lwp file descriptor */
1095 t
= pcp
->prc_thread
;
1098 while (!ISTOPPED(t
) && !VSTOPPED(t
)) {
1100 mutex_enter(&pcp
->prc_mutex
);
1102 error
= pr_wait(pcp
, rqtp
, timecheck
);
1103 if (error
) /* -1 is timeout */
1105 if ((error
= prlock(pnp
, ZNO
)) != 0)
1107 ASSERT(p
== pcp
->prc_proc
);
1108 ASSERT(t
== pcp
->prc_thread
);
1112 } else { /* process file descriptor */
1113 t
= prchoose(p
); /* returns locked thread */
1115 ASSERT(MUTEX_HELD(&p
->p_lock
));
1116 while ((!ISTOPPED(t
) && !VSTOPPED(t
) && !SUSPENDED(t
)) ||
1117 (p
->p_flag
& SEXITLWPS
)) {
1119 mutex_enter(&pcp
->prc_mutex
);
1121 error
= pr_wait(pcp
, rqtp
, timecheck
);
1122 if (error
) /* -1 is timeout */
1124 if ((error
= prlock(pnp
, ZNO
)) != 0)
1126 ASSERT(p
== pcp
->prc_proc
);
1127 t
= prchoose(p
); /* returns locked t */
1133 ASSERT(!(pcp
->prc_flags
& PRC_DESTROY
) && p
->p_stat
!= SZOMB
&&
1134 t
!= NULL
&& t
->t_state
!= TS_ZOMB
);
1140 pr_setrun(prnode_t
*pnp
, ulong_t flags
)
1142 prcommon_t
*pcp
= pnp
->pr_common
;
1143 proc_t
*p
= pcp
->prc_proc
;
1148 * Cannot set an lwp running if it is not stopped.
1149 * Also, no lwp other than the /proc agent lwp can
1150 * be set running so long as the /proc agent lwp exists.
1152 t
= pr_thread(pnp
); /* returns locked thread */
1153 if ((!ISTOPPED(t
) && !VSTOPPED(t
) &&
1154 !(t
->t_proc_flag
& TP_PRSTOP
)) ||
1155 (p
->p_agenttp
!= NULL
&&
1156 (t
!= p
->p_agenttp
|| !(pcp
->prc_flags
& PRC_LWP
)))) {
1161 if (flags
& ~(PRCSIG
|PRCFAULT
|PRSTEP
|PRSTOP
|PRSABORT
))
1164 if ((flags
& PRCSIG
) && lwp
->lwp_cursig
!= SIGKILL
) {
1166 * Discard current siginfo_t, if any.
1168 lwp
->lwp_cursig
= 0;
1169 lwp
->lwp_extsig
= 0;
1170 if (lwp
->lwp_curinfo
) {
1171 siginfofree(lwp
->lwp_curinfo
);
1172 lwp
->lwp_curinfo
= NULL
;
1175 if (flags
& PRCFAULT
)
1176 lwp
->lwp_curflt
= 0;
1178 * We can't hold p->p_lock when we touch the lwp's registers.
1179 * It may be swapped out and we will get a page fault.
1181 if (flags
& PRSTEP
) {
1182 mutex_exit(&p
->p_lock
);
1184 mutex_enter(&p
->p_lock
);
1186 if (flags
& PRSTOP
) {
1187 t
->t_proc_flag
|= TP_PRSTOP
;
1188 t
->t_sig_check
= 1; /* do ISSIG */
1190 if (flags
& PRSABORT
)
1191 lwp
->lwp_sysabort
= 1;
1193 if ((pcp
->prc_flags
& PRC_LWP
) || (flags
& (PRSTEP
|PRSTOP
))) {
1195 * Here, we are dealing with a single lwp.
1198 t
->t_schedflag
|= TS_PSTART
;
1199 t
->t_dtrace_stop
= 0;
1201 } else if (flags
& PRSABORT
) {
1203 ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1205 } else if (!(flags
& PRSTOP
)) {
1207 ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1212 * Here, we are dealing with the whole process.
1216 * The representative lwp is stopped on an event
1217 * of interest. We demote it to PR_REQUESTED and
1218 * choose another representative lwp. If the new
1219 * representative lwp is not stopped on an event of
1220 * interest (other than PR_REQUESTED), we set the
1221 * whole process running, else we leave the process
1222 * stopped showing the next event of interest.
1224 kthread_t
*tx
= NULL
;
1226 if (!(flags
& PRSABORT
) &&
1227 t
->t_whystop
== PR_SYSENTRY
&&
1228 t
->t_whatstop
== SYS_lwp_exit
)
1229 tx
= t
; /* remember the exiting lwp */
1230 t
->t_whystop
= PR_REQUESTED
;
1233 t
= prchoose(p
); /* returns locked t */
1234 ASSERT(ISTOPPED(t
) || VSTOPPED(t
));
1236 t
->t_whystop
== PR_REQUESTED
) {
1242 * As a special case, if the old representative
1243 * lwp was stopped on entry to _lwp_exit()
1244 * (and we are not aborting the system call),
1245 * we set the old representative lwp running.
1246 * We do this so that the next process stop
1247 * will find the exiting lwp gone.
1251 tx
->t_schedflag
|= TS_PSTART
;
1252 t
->t_dtrace_stop
= 0;
1259 * No event of interest; set all of the lwps running.
1261 if (flags
& PRSABORT
) {
1263 ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1274 * Wait until process/lwp stops or until timer expires.
1275 * Return EINTR for an interruption, -1 for timeout, else 0.
1278 pr_wait(prcommon_t
*pcp
, /* prcommon referring to process/lwp */
1279 timestruc_t
*ts
, /* absolute time of timeout, if any */
1284 ASSERT(MUTEX_HELD(&pcp
->prc_mutex
));
1285 rval
= cv_waituntil_sig(&pcp
->prc_wait
, &pcp
->prc_mutex
, ts
, timecheck
);
1286 mutex_exit(&pcp
->prc_mutex
);
1298 * Make all threads in the process runnable.
1301 allsetrun(proc_t
*p
)
1305 ASSERT(MUTEX_HELD(&p
->p_lock
));
1307 if ((t
= p
->p_tlist
) != NULL
) {
1310 ASSERT(!(t
->t_proc_flag
& TP_LWPEXIT
));
1311 t
->t_proc_flag
&= ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1313 t
->t_schedflag
|= TS_PSTART
;
1314 t
->t_dtrace_stop
= 0;
1318 } while ((t
= t
->t_forw
) != p
->p_tlist
);
1323 * Wait for the process to die.
1324 * We do this after sending SIGKILL because we know it will
1325 * die soon and we want subsequent operations to return ENOENT.
1328 pr_wait_die(prnode_t
*pnp
)
1332 mutex_enter(&pidlock
);
1333 while ((p
= pnp
->pr_common
->prc_proc
) != NULL
&& p
->p_stat
!= SZOMB
) {
1334 if (!cv_wait_sig(&p
->p_srwchan_cv
, &pidlock
))
1337 mutex_exit(&pidlock
);
1341 pr_settrace(proc_t
*p
, sigset_t
*sp
)
1343 prdelset(sp
, SIGKILL
);
1344 prassignset(&p
->p_sigmask
, sp
);
1345 if (!sigisempty(&p
->p_sigmask
))
1346 p
->p_proc_flag
|= P_PR_TRACE
;
1347 else if (prisempty(&p
->p_fltmask
)) {
1348 user_t
*up
= PTOU(p
);
1349 if (up
->u_systrap
== 0)
1350 p
->p_proc_flag
&= ~P_PR_TRACE
;
1355 pr_setsig(prnode_t
*pnp
, siginfo_t
*sip
)
1357 int nsig
= PROC_IS_BRANDED(curproc
)? BROP(curproc
)->b_nsig
: NSIG
;
1358 int sig
= sip
->si_signo
;
1359 prcommon_t
*pcp
= pnp
->pr_common
;
1360 proc_t
*p
= pcp
->prc_proc
;
1365 t
= pr_thread(pnp
); /* returns locked thread */
1368 if (sig
< 0 || sig
>= nsig
)
1369 /* Zero allowed here */
1371 else if (lwp
->lwp_cursig
== SIGKILL
)
1372 /* "can't happen", but just in case */
1374 else if ((lwp
->lwp_cursig
= (uchar_t
)sig
) == 0) {
1375 lwp
->lwp_extsig
= 0;
1377 * Discard current siginfo_t, if any.
1379 if (lwp
->lwp_curinfo
) {
1380 siginfofree(lwp
->lwp_curinfo
);
1381 lwp
->lwp_curinfo
= NULL
;
1387 /* drop p_lock to do kmem_alloc(KM_SLEEP) */
1388 mutex_exit(&p
->p_lock
);
1389 sqp
= kmem_zalloc(sizeof (sigqueue_t
), KM_SLEEP
);
1390 mutex_enter(&p
->p_lock
);
1392 if (lwp
->lwp_curinfo
== NULL
)
1393 lwp
->lwp_curinfo
= sqp
;
1395 kmem_free(sqp
, sizeof (sigqueue_t
));
1397 * Copy contents of info to current siginfo_t.
1399 bcopy(sip
, &lwp
->lwp_curinfo
->sq_info
,
1400 sizeof (lwp
->lwp_curinfo
->sq_info
));
1402 * Prevent contents published by si_zoneid-unaware /proc
1403 * consumers from being incorrectly filtered. Because
1404 * an uninitialized si_zoneid is the same as
1405 * GLOBAL_ZONEID, this means that you can't pr_setsig a
1406 * process in a non-global zone with a siginfo which
1407 * appears to come from the global zone.
1409 if (SI_FROMUSER(sip
) && sip
->si_zoneid
== 0)
1410 lwp
->lwp_curinfo
->sq_info
.si_zoneid
=
1413 * Side-effects for SIGKILL and jobcontrol signals.
1415 if (sig
== SIGKILL
) {
1416 p
->p_flag
|= SKILLED
;
1417 p
->p_flag
&= ~SEXTKILLED
;
1418 } else if (sig
== SIGCONT
) {
1419 p
->p_flag
|= SSCONT
;
1420 sigdelq(p
, NULL
, SIGSTOP
);
1421 sigdelq(p
, NULL
, SIGTSTP
);
1422 sigdelq(p
, NULL
, SIGTTOU
);
1423 sigdelq(p
, NULL
, SIGTTIN
);
1424 sigdiffset(&p
->p_sig
, &stopdefault
);
1425 sigdiffset(&p
->p_extsig
, &stopdefault
);
1426 if ((tx
= p
->p_tlist
) != NULL
) {
1428 sigdelq(p
, tx
, SIGSTOP
);
1429 sigdelq(p
, tx
, SIGTSTP
);
1430 sigdelq(p
, tx
, SIGTTOU
);
1431 sigdelq(p
, tx
, SIGTTIN
);
1432 sigdiffset(&tx
->t_sig
, &stopdefault
);
1433 sigdiffset(&tx
->t_extsig
, &stopdefault
);
1434 } while ((tx
= tx
->t_forw
) != p
->p_tlist
);
1436 } else if (sigismember(&stopdefault
, sig
)) {
1437 if (PTOU(p
)->u_signal
[sig
-1] == SIG_DFL
&&
1438 (sig
== SIGSTOP
|| !p
->p_pgidp
->pid_pgorphaned
))
1439 p
->p_flag
&= ~SSCONT
;
1440 sigdelq(p
, NULL
, SIGCONT
);
1441 sigdelset(&p
->p_sig
, SIGCONT
);
1442 sigdelset(&p
->p_extsig
, SIGCONT
);
1443 if ((tx
= p
->p_tlist
) != NULL
) {
1445 sigdelq(p
, tx
, SIGCONT
);
1446 sigdelset(&tx
->t_sig
, SIGCONT
);
1447 sigdelset(&tx
->t_extsig
, SIGCONT
);
1448 } while ((tx
= tx
->t_forw
) != p
->p_tlist
);
1452 if (ISWAKEABLE(t
) || ISWAITING(t
)) {
1453 /* Set signaled sleeping/waiting lwp running */
1455 } else if (t
->t_state
== TS_STOPPED
&& sig
== SIGKILL
) {
1456 /* If SIGKILL, set stopped lwp running */
1458 t
->t_schedflag
|= TS_XSTART
| TS_PSTART
;
1459 t
->t_dtrace_stop
= 0;
1462 t
->t_sig_check
= 1; /* so ISSIG will be done */
1465 * More jobcontrol side-effects.
1467 if (sig
== SIGCONT
&& (tx
= p
->p_tlist
) != NULL
) {
1471 if (tx
->t_state
== TS_STOPPED
&&
1472 tx
->t_whystop
== PR_JOBCONTROL
) {
1473 tx
->t_schedflag
|= TS_XSTART
;
1477 } while ((tx
= tx
->t_forw
) != p
->p_tlist
);
1484 pr_kill(prnode_t
*pnp
, int sig
, cred_t
*cr
)
1486 int nsig
= PROC_IS_BRANDED(curproc
)? BROP(curproc
)->b_nsig
: NSIG
;
1487 prcommon_t
*pcp
= pnp
->pr_common
;
1488 proc_t
*p
= pcp
->prc_proc
;
1491 if (sig
<= 0 || sig
>= nsig
)
1494 bzero(&info
, sizeof (info
));
1495 info
.si_signo
= sig
;
1496 info
.si_code
= SI_USER
;
1497 info
.si_pid
= curproc
->p_pid
;
1498 info
.si_ctid
= PRCTID(curproc
);
1499 info
.si_zoneid
= getzoneid();
1500 info
.si_uid
= crgetruid(cr
);
1501 sigaddq(p
, (pcp
->prc_flags
& PRC_LWP
)?
1502 pcp
->prc_thread
: NULL
, &info
, KM_NOSLEEP
);
1508 pr_unkill(prnode_t
*pnp
, int sig
)
1510 int nsig
= PROC_IS_BRANDED(curproc
)? BROP(curproc
)->b_nsig
: NSIG
;
1511 prcommon_t
*pcp
= pnp
->pr_common
;
1512 proc_t
*p
= pcp
->prc_proc
;
1513 sigqueue_t
*infop
= NULL
;
1515 if (sig
<= 0 || sig
>= nsig
|| sig
== SIGKILL
)
1518 if (pcp
->prc_flags
& PRC_LWP
)
1519 sigdeq(p
, pcp
->prc_thread
, sig
, &infop
);
1521 sigdeq(p
, NULL
, sig
, &infop
);
1530 pr_nice(proc_t
*p
, int nice
, cred_t
*cr
)
1538 ASSERT(!(t
->t_proc_flag
& TP_LWPEXIT
));
1539 err
= CL_DONICE(t
, cr
, nice
, NULL
);
1540 schedctl_set_cidpri(t
);
1543 } while ((t
= t
->t_forw
) != p
->p_tlist
);
1549 pr_setentryexit(proc_t
*p
, sysset_t
*sysset
, int entry
)
1551 user_t
*up
= PTOU(p
);
1554 prassignset(&up
->u_entrymask
, sysset
);
1556 prassignset(&up
->u_exitmask
, sysset
);
1558 if (!prisempty(&up
->u_entrymask
) ||
1559 !prisempty(&up
->u_exitmask
)) {
1561 p
->p_proc_flag
|= P_PR_TRACE
;
1562 set_proc_sys(p
); /* set pre and post-sys flags */
1565 if (sigisempty(&p
->p_sigmask
) &&
1566 prisempty(&p
->p_fltmask
))
1567 p
->p_proc_flag
&= ~P_PR_TRACE
;
1572 (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_MSACCT|PR_MSFORK|\
1576 pr_set(proc_t
*p
, long flags
)
1578 if ((p
->p_flag
& SSYS
) || p
->p_as
== &kas
)
1581 if (flags
& ~ALLFLAGS
)
1584 if (flags
& PR_FORK
)
1585 p
->p_proc_flag
|= P_PR_FORK
;
1587 p
->p_proc_flag
|= P_PR_RUNLCL
;
1589 p
->p_proc_flag
|= P_PR_KILLCL
;
1590 if (flags
& PR_ASYNC
)
1591 p
->p_proc_flag
|= P_PR_ASYNC
;
1592 if (flags
& PR_BPTADJ
)
1593 p
->p_proc_flag
|= P_PR_BPTADJ
;
1594 if (flags
& PR_MSACCT
)
1595 if ((p
->p_flag
& SMSACCT
) == 0)
1596 estimate_msacct(p
->p_tlist
, gethrtime());
1597 if (flags
& PR_MSFORK
)
1598 p
->p_flag
|= SMSFORK
;
1599 if (flags
& PR_PTRACE
) {
1600 p
->p_proc_flag
|= P_PR_PTRACE
;
1601 /* ptraced process must die if parent dead */
1603 sigtoproc(p
, NULL
, SIGKILL
);
1610 pr_unset(proc_t
*p
, long flags
)
1612 if ((p
->p_flag
& SSYS
) || p
->p_as
== &kas
)
1615 if (flags
& ~ALLFLAGS
)
1618 if (flags
& PR_FORK
)
1619 p
->p_proc_flag
&= ~P_PR_FORK
;
1621 p
->p_proc_flag
&= ~P_PR_RUNLCL
;
1623 p
->p_proc_flag
&= ~P_PR_KILLCL
;
1624 if (flags
& PR_ASYNC
)
1625 p
->p_proc_flag
&= ~P_PR_ASYNC
;
1626 if (flags
& PR_BPTADJ
)
1627 p
->p_proc_flag
&= ~P_PR_BPTADJ
;
1628 if (flags
& PR_MSACCT
)
1630 if (flags
& PR_MSFORK
)
1631 p
->p_flag
&= ~SMSFORK
;
1632 if (flags
& PR_PTRACE
)
1633 p
->p_proc_flag
&= ~P_PR_PTRACE
;
1639 pr_setfpregs(prnode_t
*pnp
, prfpregset_t
*prfpregset
)
1641 proc_t
*p
= pnp
->pr_common
->prc_proc
;
1642 kthread_t
*t
= pr_thread(pnp
); /* returns locked thread */
1644 if (!ISTOPPED(t
) && !VSTOPPED(t
) && !DSTOPPED(t
)) {
1650 return (EINVAL
); /* No FP support */
1653 /* drop p_lock while touching the lwp's stack */
1655 mutex_exit(&p
->p_lock
);
1656 prsetprfpregs(ttolwp(t
), prfpregset
);
1657 mutex_enter(&p
->p_lock
);
1662 #ifdef _SYSCALL32_IMPL
1664 pr_setfpregs32(prnode_t
*pnp
, prfpregset32_t
*prfpregset
)
1666 proc_t
*p
= pnp
->pr_common
->prc_proc
;
1667 kthread_t
*t
= pr_thread(pnp
); /* returns locked thread */
1669 if (!ISTOPPED(t
) && !VSTOPPED(t
) && !DSTOPPED(t
)) {
1675 return (EINVAL
); /* No FP support */
1678 /* drop p_lock while touching the lwp's stack */
1680 mutex_exit(&p
->p_lock
);
1681 prsetprfpregs32(ttolwp(t
), prfpregset
);
1682 mutex_enter(&p
->p_lock
);
1686 #endif /* _SYSCALL32_IMPL */
1690 pr_setvaddr(prnode_t
*pnp
, caddr_t vaddr
)
1692 proc_t
*p
= pnp
->pr_common
->prc_proc
;
1693 kthread_t
*t
= pr_thread(pnp
); /* returns locked thread */
1695 if (!ISTOPPED(t
) && !VSTOPPED(t
) && !DSTOPPED(t
)) {
1700 /* drop p_lock while touching the lwp's stack */
1702 mutex_exit(&p
->p_lock
);
1703 prsvaddr(ttolwp(t
), vaddr
);
1704 mutex_enter(&p
->p_lock
);
1710 pr_sethold(prnode_t
*pnp
, sigset_t
*sp
)
1712 proc_t
*p
= pnp
->pr_common
->prc_proc
;
1713 kthread_t
*t
= pr_thread(pnp
); /* returns locked thread */
1715 schedctl_finish_sigblock(t
);
1716 sigutok(sp
, &t
->t_hold
);
1717 if (ISWAKEABLE(t
) &&
1718 (fsig(&p
->p_sig
, t
) || fsig(&t
->t_sig
, t
)))
1720 t
->t_sig_check
= 1; /* so thread will see new holdmask */
1725 pr_setfault(proc_t
*p
, fltset_t
*fltp
)
1727 prassignset(&p
->p_fltmask
, fltp
);
1728 if (!prisempty(&p
->p_fltmask
))
1729 p
->p_proc_flag
|= P_PR_TRACE
;
1730 else if (sigisempty(&p
->p_sigmask
)) {
1731 user_t
*up
= PTOU(p
);
1732 if (up
->u_systrap
== 0)
1733 p
->p_proc_flag
&= ~P_PR_TRACE
;
1738 pr_clearsig(prnode_t
*pnp
)
1740 kthread_t
*t
= pr_thread(pnp
); /* returns locked thread */
1741 klwp_t
*lwp
= ttolwp(t
);
1744 if (lwp
->lwp_cursig
== SIGKILL
)
1748 * Discard current siginfo_t, if any.
1750 lwp
->lwp_cursig
= 0;
1751 lwp
->lwp_extsig
= 0;
1752 if (lwp
->lwp_curinfo
) {
1753 siginfofree(lwp
->lwp_curinfo
);
1754 lwp
->lwp_curinfo
= NULL
;
1761 pr_clearflt(prnode_t
*pnp
)
1763 kthread_t
*t
= pr_thread(pnp
); /* returns locked thread */
1766 ttolwp(t
)->lwp_curflt
= 0;
1772 pr_watch(prnode_t
*pnp
, prwatch_t
*pwp
, int *unlocked
)
1774 proc_t
*p
= pnp
->pr_common
->prc_proc
;
1775 struct as
*as
= p
->p_as
;
1776 uintptr_t vaddr
= pwp
->pr_vaddr
;
1777 size_t size
= pwp
->pr_size
;
1778 int wflags
= pwp
->pr_wflags
;
1779 ulong_t newpage
= 0;
1780 struct watched_area
*pwa
;
1786 * Can't apply to a system process.
1788 if ((p
->p_flag
& SSYS
) || p
->p_as
== &kas
)
1792 * Verify that the address range does not wrap
1793 * and that only the proper flags were specified.
1795 if ((wflags
& ~WA_TRAPAFTER
) == 0)
1797 if (vaddr
+ size
< vaddr
||
1798 (wflags
& ~(WA_READ
|WA_WRITE
|WA_EXEC
|WA_TRAPAFTER
)) != 0 ||
1799 ((wflags
& ~WA_TRAPAFTER
) != 0 && size
== 0))
1803 * Don't let the address range go above as->a_userlimit.
1804 * There is no error here, just a limitation.
1806 if (vaddr
>= (uintptr_t)as
->a_userlimit
)
1808 if (vaddr
+ size
> (uintptr_t)as
->a_userlimit
)
1809 size
= (uintptr_t)as
->a_userlimit
- vaddr
;
1812 * Compute maximum number of pages this will add.
1814 if ((wflags
& ~WA_TRAPAFTER
) != 0) {
1815 ulong_t pagespan
= (vaddr
+ size
) - (vaddr
& PAGEMASK
);
1816 newpage
= btopr(pagespan
);
1817 if (newpage
> 2 * prnwatch
)
1822 * Force the process to be fully stopped.
1826 while (holdwatch() != 0)
1828 if ((error
= prlock(pnp
, ZNO
)) != 0) {
1835 while (pr_allstopped(p
, 0) > 0) {
1837 * This cv/mutex pair is persistent even
1838 * if the process disappears after we
1839 * unmark it and drop p->p_lock.
1841 kcondvar_t
*cv
= &pr_pid_cv
[p
->p_slot
];
1842 kmutex_t
*mp
= &p
->p_lock
;
1845 (void) cv_wait(cv
, mp
);
1847 if ((error
= prlock(pnp
, ZNO
)) != 0) {
1849 * Unpause the process if it exists.
1852 mutex_exit(&pr_pidlock
);
1864 * Drop p->p_lock in order to perform the rest of this.
1865 * The process is still locked with the P_PR_LOCK flag.
1867 mutex_exit(&p
->p_lock
);
1869 pwa
= kmem_alloc(sizeof (struct watched_area
), KM_SLEEP
);
1870 pwa
->wa_vaddr
= (caddr_t
)vaddr
;
1871 pwa
->wa_eaddr
= (caddr_t
)vaddr
+ size
;
1872 pwa
->wa_flags
= (ulong_t
)wflags
;
1874 error
= ((pwa
->wa_flags
& ~WA_TRAPAFTER
) == 0)?
1875 clear_watched_area(p
, pwa
) : set_watched_area(p
, pwa
);
1879 mutex_enter(&p
->p_lock
);
1882 mutex_enter(&p
->p_lock
);
1889 /* jobcontrol stopped, but with a /proc directed stop in effect */
1890 #define JDSTOPPED(t) \
1891 ((t)->t_state == TS_STOPPED && \
1892 (t)->t_whystop == PR_JOBCONTROL && \
1893 ((t)->t_proc_flag & TP_PRSTOP))
1896 * pr_agent() creates the agent lwp. If the process is exiting while
1897 * we are creating an agent lwp, then exitlwps() waits until the
1898 * agent has been created using prbarrier().
1901 pr_agent(prnode_t
*pnp
, prgregset_t prgregset
, int *unlocked
)
1903 proc_t
*p
= pnp
->pr_common
->prc_proc
;
1916 * Cannot create the /proc agent lwp if :-
1917 * - the process is not fully stopped or directed to stop.
1918 * - there is an agent lwp already.
1919 * - the process has been killed.
1920 * - the process is exiting.
1921 * - it's a vfork(2) parent.
1923 t
= prchoose(p
); /* returns locked thread */
1926 if ((!ISTOPPED(t
) && !VSTOPPED(t
) && !SUSPENDED(t
) && !JDSTOPPED(t
)) ||
1927 p
->p_agenttp
!= NULL
||
1928 (p
->p_flag
& (SKILLED
| SEXITING
| SVFWAIT
))) {
1934 mutex_exit(&p
->p_lock
);
1937 sigdiffset(&smask
, &cantmask
);
1938 clwp
= lwp_create(lwp_rtt
, NULL
, 0, p
, TS_STOPPED
,
1939 t
->t_pri
, &smask
, NOCLASS
, 0);
1941 mutex_enter(&p
->p_lock
);
1944 prsetprregs(clwp
, prgregset
, 1);
1947 * Because abandoning the agent inside the target process leads to
1948 * a state that is essentially undebuggable, we record the psinfo of
1949 * the process creating the agent and hang that off of the lwp.
1951 clwp
->lwp_spymaster
= kmem_zalloc(sizeof (psinfo_t
), KM_SLEEP
);
1952 mutex_enter(&curproc
->p_lock
);
1953 prgetpsinfo(curproc
, clwp
->lwp_spymaster
);
1954 mutex_exit(&curproc
->p_lock
);
1957 * We overload pr_time in the spymaster to denote the time at which the
1958 * agent was created.
1960 gethrestime(&clwp
->lwp_spymaster
->pr_time
);
1964 (void) CL_ALLOC(&bufp
, cid
, KM_SLEEP
);
1965 mutex_enter(&p
->p_lock
);
1966 if (cid
!= t
->t_cid
) {
1968 * Someone just changed this thread's scheduling class,
1969 * so try pre-allocating the buffer again. Hopefully we
1970 * don't hit this often.
1972 mutex_exit(&p
->p_lock
);
1977 clwp
->lwp_ap
= clwp
->lwp_arg
;
1978 clwp
->lwp_eosys
= NORMALRETURN
;
1980 ct
->t_clfuncs
= t
->t_clfuncs
;
1981 CL_FORK(t
, ct
, bufp
);
1982 ct
->t_cid
= t
->t_cid
;
1983 ct
->t_proc_flag
|= TP_PRSTOP
;
1985 * Setting t_sysnum to zero causes post_syscall()
1986 * to bypass all syscall checks and go directly to
1987 * if (issig()) psig();
1988 * so that the agent lwp will stop in issig_forreal()
1989 * showing PR_REQUESTED.
1993 ct
->t_sig_check
= 1;
1995 ct
->t_proc_flag
&= ~TP_HOLDLWP
;
1997 pcp
= pnp
->pr_pcommon
;
1998 mutex_enter(&pcp
->prc_mutex
);
2000 lwp_create_done(ct
);
2003 * Don't return until the agent is stopped on PR_REQUESTED.
2011 * Wait for the agent to stop and notify us.
2012 * If we've been interrupted, return that information.
2014 error
= pr_wait(pcp
, NULL
, 0);
2015 if (error
== EINTR
) {
2021 * Confirm that the agent LWP has stopped.
2024 if ((error
= prlock(pnp
, ZNO
)) != 0)
2029 * Since we dropped the lock on the process, the agent
2030 * may have disappeared or changed. Grab the current
2031 * agent and check fail if it has disappeared.
2033 if ((ct
= p
->p_agenttp
) == NULL
) {
2038 mutex_enter(&pcp
->prc_mutex
);
2043 mutex_exit(&pcp
->prc_mutex
);
2050 return (error
? error
: -1);
2054 pr_rdwr(proc_t
*p
, enum uio_rw rw
, priovec_t
*pio
)
2056 caddr_t base
= (caddr_t
)pio
->pio_base
;
2057 size_t cnt
= pio
->pio_len
;
2058 uintptr_t offset
= (uintptr_t)pio
->pio_offset
;
2063 if ((p
->p_flag
& SSYS
) || p
->p_as
== &kas
)
2065 else if ((base
+ cnt
) < base
|| (offset
+ cnt
) < offset
)
2067 else if (cnt
!= 0) {
2068 aiov
.iov_base
= base
;
2071 auio
.uio_loffset
= offset
;
2072 auio
.uio_iov
= &aiov
;
2073 auio
.uio_iovcnt
= 1;
2074 auio
.uio_resid
= cnt
;
2075 auio
.uio_segflg
= UIO_USERSPACE
;
2076 auio
.uio_llimit
= (longlong_t
)MAXOFFSET_T
;
2077 auio
.uio_fmode
= FREAD
|FWRITE
;
2078 auio
.uio_extflg
= UIO_COPY_DEFAULT
;
2080 mutex_exit(&p
->p_lock
);
2081 error
= prusrio(p
, rw
, &auio
, 0);
2082 mutex_enter(&p
->p_lock
);
2085 * We have no way to return the i/o count,
2086 * like read() or write() would do, so we
2087 * return an error if the i/o was truncated.
2089 if (auio
.uio_resid
!= 0 && error
== 0)
2097 pr_scred(proc_t
*p
, prcred_t
*prcred
, cred_t
*cr
, boolean_t dogrps
)
2104 zone_t
*zone
= crgetzone(cr
);
2106 if (!VALID_UID(prcred
->pr_euid
, zone
) ||
2107 !VALID_UID(prcred
->pr_ruid
, zone
) ||
2108 !VALID_UID(prcred
->pr_suid
, zone
) ||
2109 !VALID_GID(prcred
->pr_egid
, zone
) ||
2110 !VALID_GID(prcred
->pr_rgid
, zone
) ||
2111 !VALID_GID(prcred
->pr_sgid
, zone
))
2115 int ngrp
= prcred
->pr_ngroups
;
2118 if (ngrp
< 0 || ngrp
> ngroups_max
)
2121 for (i
= 0; i
< ngrp
; i
++) {
2122 if (!VALID_GID(prcred
->pr_groups
[i
], zone
))
2127 error
= secpolicy_allow_setid(cr
, prcred
->pr_euid
, B_FALSE
);
2129 if (error
== 0 && prcred
->pr_ruid
!= prcred
->pr_euid
)
2130 error
= secpolicy_allow_setid(cr
, prcred
->pr_ruid
, B_FALSE
);
2132 if (error
== 0 && prcred
->pr_suid
!= prcred
->pr_euid
&&
2133 prcred
->pr_suid
!= prcred
->pr_ruid
)
2134 error
= secpolicy_allow_setid(cr
, prcred
->pr_suid
, B_FALSE
);
2139 mutex_exit(&p
->p_lock
);
2141 /* hold old cred so it doesn't disappear while we dup it */
2142 mutex_enter(&p
->p_crlock
);
2143 crhold(oldcred
= p
->p_cred
);
2144 mutex_exit(&p
->p_crlock
);
2145 newcred
= crdup(oldcred
);
2146 oldruid
= crgetruid(oldcred
);
2149 /* Error checking done above */
2150 (void) crsetresuid(newcred
, prcred
->pr_ruid
, prcred
->pr_euid
,
2152 (void) crsetresgid(newcred
, prcred
->pr_rgid
, prcred
->pr_egid
,
2156 (void) crsetgroups(newcred
, prcred
->pr_ngroups
,
2161 mutex_enter(&p
->p_crlock
);
2162 oldcred
= p
->p_cred
;
2163 p
->p_cred
= newcred
;
2164 mutex_exit(&p
->p_crlock
);
2168 * Keep count of processes per uid consistent.
2170 if (oldruid
!= prcred
->pr_ruid
) {
2171 zoneid_t zoneid
= crgetzoneid(newcred
);
2173 mutex_enter(&pidlock
);
2174 upcount_dec(oldruid
, zoneid
);
2175 upcount_inc(prcred
->pr_ruid
, zoneid
);
2176 mutex_exit(&pidlock
);
2180 * Broadcast the cred change to the threads.
2182 mutex_enter(&p
->p_lock
);
2185 t
->t_pre_sys
= 1; /* so syscall will get new cred */
2186 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2192 * Change process credentials to specified zone. Used to temporarily
2193 * set a process to run in the global zone; only transitions between
2194 * the process's actual zone and the global zone are allowed.
2197 pr_szoneid(proc_t
*p
, zoneid_t zoneid
, cred_t
*cr
)
2205 if (secpolicy_zone_config(cr
) != 0)
2207 if (zoneid
!= GLOBAL_ZONEID
&& zoneid
!= p
->p_zone
->zone_id
)
2209 if ((zptr
= zone_find_by_id(zoneid
)) == NULL
)
2211 mutex_exit(&p
->p_lock
);
2212 mutex_enter(&p
->p_crlock
);
2213 oldcred
= p
->p_cred
;
2215 mutex_exit(&p
->p_crlock
);
2216 newcred
= crdup(oldcred
);
2217 oldzoneid
= crgetzoneid(oldcred
);
2220 crsetzone(newcred
, zptr
);
2223 mutex_enter(&p
->p_crlock
);
2224 oldcred
= p
->p_cred
;
2225 p
->p_cred
= newcred
;
2226 mutex_exit(&p
->p_crlock
);
2230 * The target process is changing zones (according to its cred), so
2231 * update the per-zone upcounts, which are based on process creds.
2233 if (oldzoneid
!= zoneid
) {
2234 uid_t ruid
= crgetruid(newcred
);
2236 mutex_enter(&pidlock
);
2237 upcount_dec(ruid
, oldzoneid
);
2238 upcount_inc(ruid
, zoneid
);
2239 mutex_exit(&pidlock
);
2242 * Broadcast the cred change to the threads.
2244 mutex_enter(&p
->p_lock
);
2247 t
->t_pre_sys
= 1; /* so syscall will get new cred */
2248 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2254 pr_spriv(proc_t
*p
, prpriv_t
*prpriv
, cred_t
*cr
)
2259 ASSERT(MUTEX_HELD(&p
->p_lock
));
2261 if ((err
= priv_pr_spriv(p
, prpriv
, cr
)) == 0) {
2263 * Broadcast the cred change to the threads.
2267 t
->t_pre_sys
= 1; /* so syscall will get new cred */
2268 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2275 * Return -1 if the process is the parent of a vfork(1) whose child has yet to
2276 * terminate or perform an exec(2).
2278 * Returns 0 if the process is fully stopped except for the current thread (if
2279 * we are operating on our own process), 1 otherwise.
2281 * If the watchstop flag is set, then we ignore threads with TP_WATCHSTOP set.
2282 * See holdwatch() for details.
2285 pr_allstopped(proc_t
*p
, int watchstop
)
2290 ASSERT(MUTEX_HELD(&p
->p_lock
));
2292 if (p
->p_flag
& SVFWAIT
) /* waiting for vfork'd child to exec */
2295 if ((t
= p
->p_tlist
) != NULL
) {
2297 if (t
== curthread
|| VSTOPPED(t
) ||
2298 (watchstop
&& (t
->t_proc_flag
& TP_WATCHSTOP
)))
2301 switch (t
->t_state
) {
2306 if (!(t
->t_flag
& T_WAKEABLE
) ||
2307 t
->t_wchan0
== NULL
)
2315 } while (rv
== 0 && (t
= t
->t_forw
) != p
->p_tlist
);
2322 * Cause all lwps in the process to pause (for watchpoint operations).
2325 pauselwps(proc_t
*p
)
2329 ASSERT(MUTEX_HELD(&p
->p_lock
));
2330 ASSERT(p
!= curproc
);
2332 if ((t
= p
->p_tlist
) != NULL
) {
2335 t
->t_proc_flag
|= TP_PAUSE
;
2337 if ((ISWAKEABLE(t
) && (t
->t_wchan0
== NULL
)) ||
2343 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2348 * undo the effects of pauselwps()
2351 unpauselwps(proc_t
*p
)
2355 ASSERT(MUTEX_HELD(&p
->p_lock
));
2356 ASSERT(p
!= curproc
);
2358 if ((t
= p
->p_tlist
) != NULL
) {
2361 t
->t_proc_flag
&= ~TP_PAUSE
;
2362 if (t
->t_state
== TS_STOPPED
) {
2363 t
->t_schedflag
|= TS_UNPAUSE
;
2364 t
->t_dtrace_stop
= 0;
2368 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2373 * Cancel all watched areas. Called from prclose().
2376 pr_cancel_watch(prnode_t
*pnp
)
2378 proc_t
*p
= pnp
->pr_pcommon
->prc_proc
;
2382 ASSERT(MUTEX_HELD(&p
->p_lock
) && (p
->p_proc_flag
& P_PR_LOCK
));
2384 if (!pr_watch_active(p
))
2388 * Pause the process before dealing with the watchpoints.
2392 while (holdwatch() != 0)
2395 mutex_exit(&pr_pidlock
);
2396 ASSERT(p
== curproc
);
2399 while (p
!= NULL
&& pr_allstopped(p
, 0) > 0) {
2401 * This cv/mutex pair is persistent even
2402 * if the process disappears after we
2403 * unmark it and drop p->p_lock.
2405 kcondvar_t
*cv
= &pr_pid_cv
[p
->p_slot
];
2406 kmutex_t
*mp
= &p
->p_lock
;
2409 (void) cv_wait(cv
, mp
);
2411 p
= pr_p_lock(pnp
); /* NULL if process disappeared */
2412 mutex_exit(&pr_pidlock
);
2416 if (p
== NULL
) /* the process disappeared */
2419 ASSERT(p
== pnp
->pr_pcommon
->prc_proc
);
2420 ASSERT(MUTEX_HELD(&p
->p_lock
) && (p
->p_proc_flag
& P_PR_LOCK
));
2422 if (pr_watch_active(p
)) {
2423 pr_free_watchpoints(p
);
2424 if ((t
= p
->p_tlist
) != NULL
) {
2428 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2432 if ((as
= p
->p_as
) != NULL
) {
2434 struct watched_page
*pwp
;
2437 * If this is the parent of a vfork, the watched page
2438 * list has been moved temporarily to p->p_wpage.
2440 if (avl_numnodes(&p
->p_wpage
) != 0)
2443 tree
= &as
->a_wpage
;
2445 mutex_exit(&p
->p_lock
);
2446 AS_LOCK_ENTER(as
, RW_WRITER
);
2448 for (pwp
= avl_first(tree
); pwp
!= NULL
;
2449 pwp
= AVL_NEXT(tree
, pwp
)) {
2453 if ((pwp
->wp_flags
& WP_SETPROT
) == 0) {
2454 pwp
->wp_flags
|= WP_SETPROT
;
2455 pwp
->wp_prot
= pwp
->wp_oprot
;
2456 pwp
->wp_list
= p
->p_wprot
;
2462 mutex_enter(&p
->p_lock
);
2466 * Unpause the process now.