xref: /illumos-gate/usr/src/uts/common/os/zone.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Zones
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  *   A zone is a named collection of processes, namespace constraints,
33*7c478bd9Sstevel@tonic-gate  *   and other system resources which comprise a secure and manageable
34*7c478bd9Sstevel@tonic-gate  *   application containment facility.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  *   Zones (represented by the reference counted zone_t) are tracked in
37*7c478bd9Sstevel@tonic-gate  *   the kernel in the zonehash.  Elsewhere in the kernel, Zone IDs
38*7c478bd9Sstevel@tonic-gate  *   (zoneid_t) are used to track zone association.  Zone IDs are
39*7c478bd9Sstevel@tonic-gate  *   dynamically generated when the zone is created; if a persistent
40*7c478bd9Sstevel@tonic-gate  *   identifier is needed (core files, accounting logs, audit trail,
41*7c478bd9Sstevel@tonic-gate  *   etc.), the zone name should be used.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  *
44*7c478bd9Sstevel@tonic-gate  *   Global Zone:
45*7c478bd9Sstevel@tonic-gate  *
46*7c478bd9Sstevel@tonic-gate  *   The global zone (zoneid 0) is automatically associated with all
47*7c478bd9Sstevel@tonic-gate  *   system resources that have not been bound to a user-created zone.
48*7c478bd9Sstevel@tonic-gate  *   This means that even systems where zones are not in active use
49*7c478bd9Sstevel@tonic-gate  *   have a global zone, and all processes, mounts, etc. are
50*7c478bd9Sstevel@tonic-gate  *   associated with that zone.  The global zone is generally
51*7c478bd9Sstevel@tonic-gate  *   unconstrained in terms of privileges and access, though the usual
52*7c478bd9Sstevel@tonic-gate  *   credential and privilege based restrictions apply.
53*7c478bd9Sstevel@tonic-gate  *
54*7c478bd9Sstevel@tonic-gate  *
55*7c478bd9Sstevel@tonic-gate  *   Zone States:
56*7c478bd9Sstevel@tonic-gate  *
57*7c478bd9Sstevel@tonic-gate  *   The states in which a zone may be in and the transitions are as
58*7c478bd9Sstevel@tonic-gate  *   follows:
59*7c478bd9Sstevel@tonic-gate  *
60*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially
61*7c478bd9Sstevel@tonic-gate  *   initialized zone is added to the list of active zones on the system but
62*7c478bd9Sstevel@tonic-gate  *   isn't accessible.
63*7c478bd9Sstevel@tonic-gate  *
64*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
65*7c478bd9Sstevel@tonic-gate  *   ready.  The zone is made visible after the ZSD constructor callbacks are
66*7c478bd9Sstevel@tonic-gate  *   executed.  A zone remains in this state until it transitions into
67*7c478bd9Sstevel@tonic-gate  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
70*7c478bd9Sstevel@tonic-gate  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
71*7c478bd9Sstevel@tonic-gate  *   state.
72*7c478bd9Sstevel@tonic-gate  *
73*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
74*7c478bd9Sstevel@tonic-gate  *   successfully started init.   A zone remains in this state until
75*7c478bd9Sstevel@tonic-gate  *   zone_shutdown() is called.
76*7c478bd9Sstevel@tonic-gate  *
77*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
78*7c478bd9Sstevel@tonic-gate  *   killing all processes running in the zone. The zone remains
79*7c478bd9Sstevel@tonic-gate  *   in this state until there are no more user processes running in the zone.
80*7c478bd9Sstevel@tonic-gate  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
81*7c478bd9Sstevel@tonic-gate  *   Since zone_shutdown() is restartable, it may be called successfully
82*7c478bd9Sstevel@tonic-gate  *   multiple times for the same zone_t.  Setting of the zone's state to
83*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
84*7c478bd9Sstevel@tonic-gate  *   the zone's status without worrying about it being a moving target.
85*7c478bd9Sstevel@tonic-gate  *
86*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
87*7c478bd9Sstevel@tonic-gate  *   are no more user processes in the zone.  The zone remains in this
88*7c478bd9Sstevel@tonic-gate  *   state until there are no more kernel threads associated with the
89*7c478bd9Sstevel@tonic-gate  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
90*7c478bd9Sstevel@tonic-gate  *   fail.
91*7c478bd9Sstevel@tonic-gate  *
92*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
93*7c478bd9Sstevel@tonic-gate  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
94*7c478bd9Sstevel@tonic-gate  *   join the zone or create kernel threads therein.
95*7c478bd9Sstevel@tonic-gate  *
96*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
97*7c478bd9Sstevel@tonic-gate  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
98*7c478bd9Sstevel@tonic-gate  *   return NULL from now on.
99*7c478bd9Sstevel@tonic-gate  *
100*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
101*7c478bd9Sstevel@tonic-gate  *   processes or threads doing work on behalf of the zone.  The zone is
102*7c478bd9Sstevel@tonic-gate  *   removed from the list of active zones.  zone_destroy() returns, and
103*7c478bd9Sstevel@tonic-gate  *   the zone can be recreated.
104*7c478bd9Sstevel@tonic-gate  *
105*7c478bd9Sstevel@tonic-gate  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
106*7c478bd9Sstevel@tonic-gate  *   callbacks are executed, and all memory associated with the zone is
107*7c478bd9Sstevel@tonic-gate  *   freed.
108*7c478bd9Sstevel@tonic-gate  *
109*7c478bd9Sstevel@tonic-gate  *   Threads can wait for the zone to enter a requested state by using
110*7c478bd9Sstevel@tonic-gate  *   zone_status_wait() or zone_status_timedwait() with the desired
111*7c478bd9Sstevel@tonic-gate  *   state passed in as an argument.  Zone state transitions are
112*7c478bd9Sstevel@tonic-gate  *   uni-directional; it is not possible to move back to an earlier state.
113*7c478bd9Sstevel@tonic-gate  *
114*7c478bd9Sstevel@tonic-gate  *
115*7c478bd9Sstevel@tonic-gate  *   Zone-Specific Data:
116*7c478bd9Sstevel@tonic-gate  *
117*7c478bd9Sstevel@tonic-gate  *   Subsystems needing to maintain zone-specific data can store that
118*7c478bd9Sstevel@tonic-gate  *   data using the ZSD mechanism.  This provides a zone-specific data
119*7c478bd9Sstevel@tonic-gate  *   store, similar to thread-specific data (see pthread_getspecific(3C)
120*7c478bd9Sstevel@tonic-gate  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
121*7c478bd9Sstevel@tonic-gate  *   to register callbacks to be invoked when a zone is created, shut
122*7c478bd9Sstevel@tonic-gate  *   down, or destroyed.  This can be used to initialize zone-specific
123*7c478bd9Sstevel@tonic-gate  *   data for new zones and to clean up when zones go away.
124*7c478bd9Sstevel@tonic-gate  *
125*7c478bd9Sstevel@tonic-gate  *
126*7c478bd9Sstevel@tonic-gate  *   Data Structures:
127*7c478bd9Sstevel@tonic-gate  *
128*7c478bd9Sstevel@tonic-gate  *   The per-zone structure (zone_t) is reference counted, and freed
129*7c478bd9Sstevel@tonic-gate  *   when all references are released.  zone_hold and zone_rele can be
130*7c478bd9Sstevel@tonic-gate  *   used to adjust the reference count.  In addition, reference counts
131*7c478bd9Sstevel@tonic-gate  *   associated with the cred_t structure are tracked separately using
132*7c478bd9Sstevel@tonic-gate  *   zone_cred_hold and zone_cred_rele.
133*7c478bd9Sstevel@tonic-gate  *
134*7c478bd9Sstevel@tonic-gate  *   Pointers to active zone_t's are stored in two hash tables; one
135*7c478bd9Sstevel@tonic-gate  *   for searching by id, the other for searching by name.  Lookups
136*7c478bd9Sstevel@tonic-gate  *   can be performed on either basis, using zone_find_by_id and
137*7c478bd9Sstevel@tonic-gate  *   zone_find_by_name.  Both return zone_t pointers with the zone
138*7c478bd9Sstevel@tonic-gate  *   held, so zone_rele should be called when the pointer is no longer
139*7c478bd9Sstevel@tonic-gate  *   needed.  Zones can also be searched by path; zone_find_by_path
140*7c478bd9Sstevel@tonic-gate  *   returns the zone with which a path name is associated (global
141*7c478bd9Sstevel@tonic-gate  *   zone if the path is not within some other zone's file system
142*7c478bd9Sstevel@tonic-gate  *   hierarchy).  This currently requires iterating through each zone,
143*7c478bd9Sstevel@tonic-gate  *   so it is slower than an id or name search via a hash table.
144*7c478bd9Sstevel@tonic-gate  *
145*7c478bd9Sstevel@tonic-gate  *
146*7c478bd9Sstevel@tonic-gate  *   Locking:
147*7c478bd9Sstevel@tonic-gate  *
148*7c478bd9Sstevel@tonic-gate  *   zonehash_lock: This is a top-level global lock used to protect the
149*7c478bd9Sstevel@tonic-gate  *       zone hash tables and lists.  Zones cannot be created or destroyed
150*7c478bd9Sstevel@tonic-gate  *       while this lock is held.
151*7c478bd9Sstevel@tonic-gate  *   zone_status_lock: This is a global lock protecting zone state.
152*7c478bd9Sstevel@tonic-gate  *       Zones cannot change state while this lock is held.  It also
153*7c478bd9Sstevel@tonic-gate  *       protects the list of kernel threads associated with a zone.
154*7c478bd9Sstevel@tonic-gate  *   zone_lock: This is a per-zone lock used to protect several fields of
155*7c478bd9Sstevel@tonic-gate  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
156*7c478bd9Sstevel@tonic-gate  *       this lock means that the zone cannot go away.
157*7c478bd9Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
158*7c478bd9Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
159*7c478bd9Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
160*7c478bd9Sstevel@tonic-gate  *
161*7c478bd9Sstevel@tonic-gate  *   Ordering requirements:
162*7c478bd9Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
163*7c478bd9Sstevel@tonic-gate  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
164*7c478bd9Sstevel@tonic-gate  *
165*7c478bd9Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
166*7c478bd9Sstevel@tonic-gate  *   zone locks.
167*7c478bd9Sstevel@tonic-gate  *
168*7c478bd9Sstevel@tonic-gate  *
169*7c478bd9Sstevel@tonic-gate  *   System Call Interface:
170*7c478bd9Sstevel@tonic-gate  *
171*7c478bd9Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
172*7c478bd9Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
173*7c478bd9Sstevel@tonic-gate  *   system call):
174*7c478bd9Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
175*7c478bd9Sstevel@tonic-gate  *     root path, privileges, resource controls)
176*7c478bd9Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
177*7c478bd9Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
178*7c478bd9Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
179*7c478bd9Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
180*7c478bd9Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
181*7c478bd9Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
182*7c478bd9Sstevel@tonic-gate  *
183*7c478bd9Sstevel@tonic-gate  */
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h>
186*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
187*7c478bd9Sstevel@tonic-gate #include <c2/audit.h>
188*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
189*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
190*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
191*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
192*7c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
193*7c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
194*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
195*7c478bd9Sstevel@tonic-gate #include <sys/project.h>
196*7c478bd9Sstevel@tonic-gate #include <sys/task.h>
197*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
198*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
199*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
200*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
201*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
202*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
203*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
204*7c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
205*7c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
206*7c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
207*7c478bd9Sstevel@tonic-gate #include <sys/class.h>
208*7c478bd9Sstevel@tonic-gate #include <sys/pool.h>
209*7c478bd9Sstevel@tonic-gate #include <sys/pool_pset.h>
210*7c478bd9Sstevel@tonic-gate #include <sys/pset.h>
211*7c478bd9Sstevel@tonic-gate #include <sys/log.h>
212*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
213*7c478bd9Sstevel@tonic-gate #include <sys/callb.h>
214*7c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
215*7c478bd9Sstevel@tonic-gate #include <sys/corectl.h>
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate #include <sys/door.h>
218*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
219*7c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
222*7c478bd9Sstevel@tonic-gate #include <sys/session.h>
223*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
224*7c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
225*7c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
226*7c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
227*7c478bd9Sstevel@tonic-gate #include <sys/fss.h>
228*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate /*
231*7c478bd9Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
232*7c478bd9Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
233*7c478bd9Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
234*7c478bd9Sstevel@tonic-gate  */
235*7c478bd9Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
238*7c478bd9Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
239*7c478bd9Sstevel@tonic-gate  */
240*7c478bd9Sstevel@tonic-gate static kmutex_t zone_status_lock;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate /*
243*7c478bd9Sstevel@tonic-gate  * ZSD-related global variables.
244*7c478bd9Sstevel@tonic-gate  */
245*7c478bd9Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
246*7c478bd9Sstevel@tonic-gate /*
247*7c478bd9Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
248*7c478bd9Sstevel@tonic-gate  */
249*7c478bd9Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
250*7c478bd9Sstevel@tonic-gate /*
251*7c478bd9Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
252*7c478bd9Sstevel@tonic-gate  */
253*7c478bd9Sstevel@tonic-gate static list_t zsd_registered_keys;
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate int zone_hash_size = 256;
256*7c478bd9Sstevel@tonic-gate static mod_hash_t *zonehashbyname, *zonehashbyid;
257*7c478bd9Sstevel@tonic-gate static kmutex_t zonehash_lock;
258*7c478bd9Sstevel@tonic-gate static uint_t zonecount;
259*7c478bd9Sstevel@tonic-gate static id_space_t *zoneid_space;
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate /*
262*7c478bd9Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
263*7c478bd9Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
264*7c478bd9Sstevel@tonic-gate  *
265*7c478bd9Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
266*7c478bd9Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
267*7c478bd9Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
268*7c478bd9Sstevel@tonic-gate  * 'global_zone'.
269*7c478bd9Sstevel@tonic-gate  */
270*7c478bd9Sstevel@tonic-gate zone_t zone0;
271*7c478bd9Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate /*
274*7c478bd9Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
275*7c478bd9Sstevel@tonic-gate  */
276*7c478bd9Sstevel@tonic-gate static list_t zone_active;
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate /*
279*7c478bd9Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
280*7c478bd9Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
281*7c478bd9Sstevel@tonic-gate  * problems in zone_free.
282*7c478bd9Sstevel@tonic-gate  */
283*7c478bd9Sstevel@tonic-gate static list_t zone_deathrow;
284*7c478bd9Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
287*7c478bd9Sstevel@tonic-gate uint_t maxzones = 8192;
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate /*
290*7c478bd9Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
291*7c478bd9Sstevel@tonic-gate  */
292*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
293*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
294*7c478bd9Sstevel@tonic-gate /*
295*7c478bd9Sstevel@tonic-gate  * Synchronization primitives used to synchronize between mounts and zone
296*7c478bd9Sstevel@tonic-gate  * creation/destruction.
297*7c478bd9Sstevel@tonic-gate  */
298*7c478bd9Sstevel@tonic-gate static int mounts_in_progress;
299*7c478bd9Sstevel@tonic-gate static kcondvar_t mount_cv;
300*7c478bd9Sstevel@tonic-gate static kmutex_t mount_lock;
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate const char * const zone_initname = "/sbin/init";
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate /*
307*7c478bd9Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
308*7c478bd9Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
309*7c478bd9Sstevel@tonic-gate  * ensure that a zone isn't in the process of being created such that
310*7c478bd9Sstevel@tonic-gate  * nfs_mount() thinks it is in the global zone, while by the time it
311*7c478bd9Sstevel@tonic-gate  * gets added the list of mounted zones, it ends up on zoneA's mount
312*7c478bd9Sstevel@tonic-gate  * list.
313*7c478bd9Sstevel@tonic-gate  *
314*7c478bd9Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
315*7c478bd9Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
316*7c478bd9Sstevel@tonic-gate  * layer (respectively) to synchronize zone creation and new mounts.
317*7c478bd9Sstevel@tonic-gate  *
318*7c478bd9Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
319*7c478bd9Sstevel@tonic-gate  * either be multiple mounts (or zone creations, if that weren't
320*7c478bd9Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
321*7c478bd9Sstevel@tonic-gate  * both.
322*7c478bd9Sstevel@tonic-gate  *
323*7c478bd9Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
324*7c478bd9Sstevel@tonic-gate  * taking too long.
325*7c478bd9Sstevel@tonic-gate  *
326*7c478bd9Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
327*7c478bd9Sstevel@tonic-gate  * "current" operation.  This means that zone creations may starve if
328*7c478bd9Sstevel@tonic-gate  * there is a rapid succession of new mounts coming in to the system, or
329*7c478bd9Sstevel@tonic-gate  * there is a remote possibility that zones will be created at such a
330*7c478bd9Sstevel@tonic-gate  * rate that new mounts will not be able to proceed.
331*7c478bd9Sstevel@tonic-gate  */
332*7c478bd9Sstevel@tonic-gate /*
333*7c478bd9Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
334*7c478bd9Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
335*7c478bd9Sstevel@tonic-gate  * them to complete.
336*7c478bd9Sstevel@tonic-gate  */
337*7c478bd9Sstevel@tonic-gate static int
338*7c478bd9Sstevel@tonic-gate block_mounts(void)
339*7c478bd9Sstevel@tonic-gate {
340*7c478bd9Sstevel@tonic-gate 	int retval = 0;
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	/*
343*7c478bd9Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
344*7c478bd9Sstevel@tonic-gate 	 * called with zonehash_lock held.
345*7c478bd9Sstevel@tonic-gate 	 */
346*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
347*7c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
348*7c478bd9Sstevel@tonic-gate 	while (mounts_in_progress > 0) {
349*7c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&mount_cv, &mount_lock) == 0)
350*7c478bd9Sstevel@tonic-gate 			goto signaled;
351*7c478bd9Sstevel@tonic-gate 	}
352*7c478bd9Sstevel@tonic-gate 	/*
353*7c478bd9Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
354*7c478bd9Sstevel@tonic-gate 	 * have been blocked by (-mounts_in_progress) different callers.
355*7c478bd9Sstevel@tonic-gate 	 */
356*7c478bd9Sstevel@tonic-gate 	mounts_in_progress--;
357*7c478bd9Sstevel@tonic-gate 	retval = 1;
358*7c478bd9Sstevel@tonic-gate signaled:
359*7c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
360*7c478bd9Sstevel@tonic-gate 	return (retval);
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate /*
364*7c478bd9Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
365*7c478bd9Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
366*7c478bd9Sstevel@tonic-gate  */
367*7c478bd9Sstevel@tonic-gate static void
368*7c478bd9Sstevel@tonic-gate resume_mounts(void)
369*7c478bd9Sstevel@tonic-gate {
370*7c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
371*7c478bd9Sstevel@tonic-gate 	if (++mounts_in_progress == 0)
372*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
373*7c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
374*7c478bd9Sstevel@tonic-gate }
375*7c478bd9Sstevel@tonic-gate 
376*7c478bd9Sstevel@tonic-gate /*
377*7c478bd9Sstevel@tonic-gate  * The VFS layer is busy with a mount; zones should wait until all
378*7c478bd9Sstevel@tonic-gate  * mounts are completed to progress.
379*7c478bd9Sstevel@tonic-gate  */
380*7c478bd9Sstevel@tonic-gate void
381*7c478bd9Sstevel@tonic-gate mount_in_progress(void)
382*7c478bd9Sstevel@tonic-gate {
383*7c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
384*7c478bd9Sstevel@tonic-gate 	while (mounts_in_progress < 0)
385*7c478bd9Sstevel@tonic-gate 		cv_wait(&mount_cv, &mount_lock);
386*7c478bd9Sstevel@tonic-gate 	mounts_in_progress++;
387*7c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
388*7c478bd9Sstevel@tonic-gate }
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate /*
391*7c478bd9Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
392*7c478bd9Sstevel@tonic-gate  * callers if this is the last mount.
393*7c478bd9Sstevel@tonic-gate  */
394*7c478bd9Sstevel@tonic-gate void
395*7c478bd9Sstevel@tonic-gate mount_completed(void)
396*7c478bd9Sstevel@tonic-gate {
397*7c478bd9Sstevel@tonic-gate 	mutex_enter(&mount_lock);
398*7c478bd9Sstevel@tonic-gate 	if (--mounts_in_progress == 0)
399*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
400*7c478bd9Sstevel@tonic-gate 	mutex_exit(&mount_lock);
401*7c478bd9Sstevel@tonic-gate }
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate /*
404*7c478bd9Sstevel@tonic-gate  * ZSD routines.
405*7c478bd9Sstevel@tonic-gate  *
406*7c478bd9Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
407*7c478bd9Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
408*7c478bd9Sstevel@tonic-gate  *
409*7c478bd9Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
410*7c478bd9Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
411*7c478bd9Sstevel@tonic-gate  * destroyed.
412*7c478bd9Sstevel@tonic-gate  *
413*7c478bd9Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
414*7c478bd9Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
415*7c478bd9Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
416*7c478bd9Sstevel@tonic-gate  * NULL data values if necessary.
417*7c478bd9Sstevel@tonic-gate  *
418*7c478bd9Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
419*7c478bd9Sstevel@tonic-gate  *
420*7c478bd9Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
421*7c478bd9Sstevel@tonic-gate  * global list "zsd_registered_keys", protected by zsd_key_lock.  The
422*7c478bd9Sstevel@tonic-gate  * constructor callback is called immediately on all existing zones, and a
423*7c478bd9Sstevel@tonic-gate  * copy of the ZSD entry added to the per-zone zone_zsd list (protected by
424*7c478bd9Sstevel@tonic-gate  * zone_lock).  As this operation requires the list of zones, the list of
425*7c478bd9Sstevel@tonic-gate  * registered keys, and the per-zone list of ZSD entries to remain constant
426*7c478bd9Sstevel@tonic-gate  * throughout the entire operation, it must grab zonehash_lock, zone_lock for
427*7c478bd9Sstevel@tonic-gate  * all existing zones, and zsd_key_lock, in that order.  Similar locking is
428*7c478bd9Sstevel@tonic-gate  * needed when zone_key_delete() is called.  It is thus sufficient to hold
429*7c478bd9Sstevel@tonic-gate  * zsd_key_lock *or* zone_lock to prevent additions to or removals from the
430*7c478bd9Sstevel@tonic-gate  * per-zone zone_zsd list.
431*7c478bd9Sstevel@tonic-gate  *
432*7c478bd9Sstevel@tonic-gate  * Note that this implementation does not make a copy of the ZSD entry if a
433*7c478bd9Sstevel@tonic-gate  * constructor callback is not provided.  A zone_getspecific() on such an
434*7c478bd9Sstevel@tonic-gate  * uninitialized ZSD entry will return NULL.
435*7c478bd9Sstevel@tonic-gate  *
436*7c478bd9Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
437*7c478bd9Sstevel@tonic-gate  * entries will be called.
438*7c478bd9Sstevel@tonic-gate  *
439*7c478bd9Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
440*7c478bd9Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
441*7c478bd9Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
442*7c478bd9Sstevel@tonic-gate  * their own locking.
443*7c478bd9Sstevel@tonic-gate  */
444*7c478bd9Sstevel@tonic-gate void
445*7c478bd9Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
446*7c478bd9Sstevel@tonic-gate     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
447*7c478bd9Sstevel@tonic-gate {
448*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp;
449*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
450*7c478bd9Sstevel@tonic-gate 	struct zone *zone;
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 	zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP);
453*7c478bd9Sstevel@tonic-gate 	zsdp->zsd_data = NULL;
454*7c478bd9Sstevel@tonic-gate 	zsdp->zsd_create = create;
455*7c478bd9Sstevel@tonic-gate 	zsdp->zsd_shutdown = shutdown;
456*7c478bd9Sstevel@tonic-gate 	zsdp->zsd_destroy = destroy;
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* stop the world */
459*7c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
460*7c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
461*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
464*7c478bd9Sstevel@tonic-gate 	*keyp = zsdp->zsd_key = ++zsd_keyval;
465*7c478bd9Sstevel@tonic-gate 	ASSERT(zsd_keyval != 0);
466*7c478bd9Sstevel@tonic-gate 	list_insert_tail(&zsd_registered_keys, zsdp);
467*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	if (create != NULL) {
470*7c478bd9Sstevel@tonic-gate 		for (zone = list_head(&zone_active); zone != NULL;
471*7c478bd9Sstevel@tonic-gate 		    zone = list_next(&zone_active, zone)) {
472*7c478bd9Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
473*7c478bd9Sstevel@tonic-gate 			t->zsd_key = *keyp;
474*7c478bd9Sstevel@tonic-gate 			t->zsd_data = (*create)(zone->zone_id);
475*7c478bd9Sstevel@tonic-gate 			t->zsd_create = create;
476*7c478bd9Sstevel@tonic-gate 			t->zsd_shutdown = shutdown;
477*7c478bd9Sstevel@tonic-gate 			t->zsd_destroy = destroy;
478*7c478bd9Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
479*7c478bd9Sstevel@tonic-gate 		}
480*7c478bd9Sstevel@tonic-gate 	}
481*7c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
482*7c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
483*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
484*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
485*7c478bd9Sstevel@tonic-gate }
486*7c478bd9Sstevel@tonic-gate 
487*7c478bd9Sstevel@tonic-gate /*
488*7c478bd9Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
489*7c478bd9Sstevel@tonic-gate  * given list.
490*7c478bd9Sstevel@tonic-gate  */
491*7c478bd9Sstevel@tonic-gate static struct zsd_entry *
492*7c478bd9Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key)
493*7c478bd9Sstevel@tonic-gate {
494*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsd;
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
497*7c478bd9Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
498*7c478bd9Sstevel@tonic-gate 			/*
499*7c478bd9Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
500*7c478bd9Sstevel@tonic-gate 			 */
501*7c478bd9Sstevel@tonic-gate 			if (zsd != list_head(l)) {
502*7c478bd9Sstevel@tonic-gate 				list_remove(l, zsd);
503*7c478bd9Sstevel@tonic-gate 				list_insert_head(l, zsd);
504*7c478bd9Sstevel@tonic-gate 			}
505*7c478bd9Sstevel@tonic-gate 			return (zsd);
506*7c478bd9Sstevel@tonic-gate 		}
507*7c478bd9Sstevel@tonic-gate 	}
508*7c478bd9Sstevel@tonic-gate 	return (NULL);
509*7c478bd9Sstevel@tonic-gate }
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate /*
512*7c478bd9Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
513*7c478bd9Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
514*7c478bd9Sstevel@tonic-gate  */
515*7c478bd9Sstevel@tonic-gate int
516*7c478bd9Sstevel@tonic-gate zone_key_delete(zone_key_t key)
517*7c478bd9Sstevel@tonic-gate {
518*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
519*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* Zone create/delete waits for us */
522*7c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
523*7c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
524*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
527*7c478bd9Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
528*7c478bd9Sstevel@tonic-gate 	if (zsdp == NULL)
529*7c478bd9Sstevel@tonic-gate 		goto notfound;
530*7c478bd9Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
531*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
534*7c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
535*7c478bd9Sstevel@tonic-gate 		struct zsd_entry *del;
536*7c478bd9Sstevel@tonic-gate 		void *data;
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate 		if (!(zone->zone_flags & ZF_DESTROYED)) {
539*7c478bd9Sstevel@tonic-gate 			del = zsd_find(&zone->zone_zsd, key);
540*7c478bd9Sstevel@tonic-gate 			if (del != NULL) {
541*7c478bd9Sstevel@tonic-gate 				data = del->zsd_data;
542*7c478bd9Sstevel@tonic-gate 				ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
543*7c478bd9Sstevel@tonic-gate 				ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
544*7c478bd9Sstevel@tonic-gate 				list_remove(&zone->zone_zsd, del);
545*7c478bd9Sstevel@tonic-gate 				kmem_free(del, sizeof (*del));
546*7c478bd9Sstevel@tonic-gate 			} else {
547*7c478bd9Sstevel@tonic-gate 				data = NULL;
548*7c478bd9Sstevel@tonic-gate 			}
549*7c478bd9Sstevel@tonic-gate 			if (zsdp->zsd_shutdown)
550*7c478bd9Sstevel@tonic-gate 				zsdp->zsd_shutdown(zone->zone_id, data);
551*7c478bd9Sstevel@tonic-gate 			if (zsdp->zsd_destroy)
552*7c478bd9Sstevel@tonic-gate 				zsdp->zsd_destroy(zone->zone_id, data);
553*7c478bd9Sstevel@tonic-gate 		}
554*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
555*7c478bd9Sstevel@tonic-gate 	}
556*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
557*7c478bd9Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
558*7c478bd9Sstevel@tonic-gate 	return (0);
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate notfound:
561*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
562*7c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
563*7c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
564*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
565*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
566*7c478bd9Sstevel@tonic-gate 	return (-1);
567*7c478bd9Sstevel@tonic-gate }
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate /*
570*7c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
571*7c478bd9Sstevel@tonic-gate  */
572*7c478bd9Sstevel@tonic-gate int
573*7c478bd9Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
574*7c478bd9Sstevel@tonic-gate {
575*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
576*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
577*7c478bd9Sstevel@tonic-gate 
578*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
579*7c478bd9Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
580*7c478bd9Sstevel@tonic-gate 	if (t != NULL) {
581*7c478bd9Sstevel@tonic-gate 		/*
582*7c478bd9Sstevel@tonic-gate 		 * Replace old value with new
583*7c478bd9Sstevel@tonic-gate 		 */
584*7c478bd9Sstevel@tonic-gate 		t->zsd_data = (void *)data;
585*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
586*7c478bd9Sstevel@tonic-gate 		return (0);
587*7c478bd9Sstevel@tonic-gate 	}
588*7c478bd9Sstevel@tonic-gate 	/*
589*7c478bd9Sstevel@tonic-gate 	 * If there was no previous value, go through the list of registered
590*7c478bd9Sstevel@tonic-gate 	 * keys.
591*7c478bd9Sstevel@tonic-gate 	 *
592*7c478bd9Sstevel@tonic-gate 	 * We avoid grabbing zsd_key_lock until we are sure we need it; this is
593*7c478bd9Sstevel@tonic-gate 	 * necessary for shutdown callbacks to be able to execute without fear
594*7c478bd9Sstevel@tonic-gate 	 * of deadlock.
595*7c478bd9Sstevel@tonic-gate 	 */
596*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
597*7c478bd9Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
598*7c478bd9Sstevel@tonic-gate 	if (zsdp == NULL) { 	/* Key was not registered */
599*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zsd_key_lock);
600*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
601*7c478bd9Sstevel@tonic-gate 		return (-1);
602*7c478bd9Sstevel@tonic-gate 	}
603*7c478bd9Sstevel@tonic-gate 
604*7c478bd9Sstevel@tonic-gate 	/*
605*7c478bd9Sstevel@tonic-gate 	 * Add a zsd_entry to this zone, using the template we just retrieved
606*7c478bd9Sstevel@tonic-gate 	 * to initialize the constructor and destructor(s).
607*7c478bd9Sstevel@tonic-gate 	 */
608*7c478bd9Sstevel@tonic-gate 	t = kmem_alloc(sizeof (*t), KM_SLEEP);
609*7c478bd9Sstevel@tonic-gate 	t->zsd_key = key;
610*7c478bd9Sstevel@tonic-gate 	t->zsd_data = (void *)data;
611*7c478bd9Sstevel@tonic-gate 	t->zsd_create = zsdp->zsd_create;
612*7c478bd9Sstevel@tonic-gate 	t->zsd_shutdown = zsdp->zsd_shutdown;
613*7c478bd9Sstevel@tonic-gate 	t->zsd_destroy = zsdp->zsd_destroy;
614*7c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone->zone_zsd, t);
615*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
616*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
617*7c478bd9Sstevel@tonic-gate 	return (0);
618*7c478bd9Sstevel@tonic-gate }
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate /*
621*7c478bd9Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
622*7c478bd9Sstevel@tonic-gate  */
623*7c478bd9Sstevel@tonic-gate void *
624*7c478bd9Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
625*7c478bd9Sstevel@tonic-gate {
626*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
627*7c478bd9Sstevel@tonic-gate 	void *data;
628*7c478bd9Sstevel@tonic-gate 
629*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
630*7c478bd9Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
631*7c478bd9Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
632*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
633*7c478bd9Sstevel@tonic-gate 	return (data);
634*7c478bd9Sstevel@tonic-gate }
635*7c478bd9Sstevel@tonic-gate 
636*7c478bd9Sstevel@tonic-gate /*
637*7c478bd9Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
638*7c478bd9Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
639*7c478bd9Sstevel@tonic-gate  * the template list (zsd_registered_keys), and the constructor
640*7c478bd9Sstevel@tonic-gate  * callback executed (if one exists).
641*7c478bd9Sstevel@tonic-gate  *
642*7c478bd9Sstevel@tonic-gate  * This is called before the zone is made publicly available, hence no
643*7c478bd9Sstevel@tonic-gate  * need to grab zone_lock.
644*7c478bd9Sstevel@tonic-gate  *
645*7c478bd9Sstevel@tonic-gate  * Although we grab and release zsd_key_lock, new entries cannot be
646*7c478bd9Sstevel@tonic-gate  * added to or removed from the zsd_registered_keys list until we
647*7c478bd9Sstevel@tonic-gate  * release zonehash_lock, so there isn't a window for a
648*7c478bd9Sstevel@tonic-gate  * zone_key_create() to come in after we've dropped zsd_key_lock but
649*7c478bd9Sstevel@tonic-gate  * before the zone is added to the zone list, such that the constructor
650*7c478bd9Sstevel@tonic-gate  * callbacks aren't executed for the new zone.
651*7c478bd9Sstevel@tonic-gate  */
652*7c478bd9Sstevel@tonic-gate static void
653*7c478bd9Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
654*7c478bd9Sstevel@tonic-gate {
655*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp;
656*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
657*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
658*7c478bd9Sstevel@tonic-gate 
659*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
660*7c478bd9Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
661*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
662*7c478bd9Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
663*7c478bd9Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
664*7c478bd9Sstevel@tonic-gate 		if (zsdp->zsd_create != NULL) {
665*7c478bd9Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
666*7c478bd9Sstevel@tonic-gate 			t->zsd_key = zsdp->zsd_key;
667*7c478bd9Sstevel@tonic-gate 			t->zsd_create = zsdp->zsd_create;
668*7c478bd9Sstevel@tonic-gate 			t->zsd_data = (*t->zsd_create)(zoneid);
669*7c478bd9Sstevel@tonic-gate 			t->zsd_shutdown = zsdp->zsd_shutdown;
670*7c478bd9Sstevel@tonic-gate 			t->zsd_destroy = zsdp->zsd_destroy;
671*7c478bd9Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
672*7c478bd9Sstevel@tonic-gate 		}
673*7c478bd9Sstevel@tonic-gate 	}
674*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
675*7c478bd9Sstevel@tonic-gate }
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate /*
680*7c478bd9Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
681*7c478bd9Sstevel@tonic-gate  */
682*7c478bd9Sstevel@tonic-gate static void
683*7c478bd9Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
684*7c478bd9Sstevel@tonic-gate {
685*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *zsdp;
686*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *t;
687*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
688*7c478bd9Sstevel@tonic-gate 
689*7c478bd9Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
690*7c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
691*7c478bd9Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
694*7c478bd9Sstevel@tonic-gate 	if (ct == ZSD_DESTROY) {
695*7c478bd9Sstevel@tonic-gate 		if (zone->zone_flags & ZF_DESTROYED) {
696*7c478bd9Sstevel@tonic-gate 			/*
697*7c478bd9Sstevel@tonic-gate 			 * Make sure destructors are only called once.
698*7c478bd9Sstevel@tonic-gate 			 */
699*7c478bd9Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
700*7c478bd9Sstevel@tonic-gate 			return;
701*7c478bd9Sstevel@tonic-gate 		}
702*7c478bd9Sstevel@tonic-gate 		zone->zone_flags |= ZF_DESTROYED;
703*7c478bd9Sstevel@tonic-gate 	}
704*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
705*7c478bd9Sstevel@tonic-gate 
706*7c478bd9Sstevel@tonic-gate 	/*
707*7c478bd9Sstevel@tonic-gate 	 * Both zsd_key_lock and zone_lock need to be held in order to add or
708*7c478bd9Sstevel@tonic-gate 	 * remove a ZSD key, (either globally as part of
709*7c478bd9Sstevel@tonic-gate 	 * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is
710*7c478bd9Sstevel@tonic-gate 	 * possible through zone_setspecific()), so it's sufficient to hold
711*7c478bd9Sstevel@tonic-gate 	 * zsd_key_lock here.
712*7c478bd9Sstevel@tonic-gate 	 *
713*7c478bd9Sstevel@tonic-gate 	 * This is a good thing, since we don't want to recursively try to grab
714*7c478bd9Sstevel@tonic-gate 	 * zone_lock if a callback attempts to do something like a crfree() or
715*7c478bd9Sstevel@tonic-gate 	 * zone_rele().
716*7c478bd9Sstevel@tonic-gate 	 */
717*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
718*7c478bd9Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
719*7c478bd9Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
720*7c478bd9Sstevel@tonic-gate 		zone_key_t key = zsdp->zsd_key;
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 		/* Skip if no callbacks registered */
723*7c478bd9Sstevel@tonic-gate 		if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL)
724*7c478bd9Sstevel@tonic-gate 			continue;
725*7c478bd9Sstevel@tonic-gate 		if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL)
726*7c478bd9Sstevel@tonic-gate 			continue;
727*7c478bd9Sstevel@tonic-gate 		/*
728*7c478bd9Sstevel@tonic-gate 		 * Call the callback with the zone-specific data if we can find
729*7c478bd9Sstevel@tonic-gate 		 * any, otherwise with NULL.
730*7c478bd9Sstevel@tonic-gate 		 */
731*7c478bd9Sstevel@tonic-gate 		t = zsd_find(&zone->zone_zsd, key);
732*7c478bd9Sstevel@tonic-gate 		if (t != NULL) {
733*7c478bd9Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
734*7c478bd9Sstevel@tonic-gate 				t->zsd_shutdown(zoneid, t->zsd_data);
735*7c478bd9Sstevel@tonic-gate 			} else {
736*7c478bd9Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
737*7c478bd9Sstevel@tonic-gate 				t->zsd_destroy(zoneid, t->zsd_data);
738*7c478bd9Sstevel@tonic-gate 			}
739*7c478bd9Sstevel@tonic-gate 		} else {
740*7c478bd9Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
741*7c478bd9Sstevel@tonic-gate 				zsdp->zsd_shutdown(zoneid, NULL);
742*7c478bd9Sstevel@tonic-gate 			} else {
743*7c478bd9Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
744*7c478bd9Sstevel@tonic-gate 				zsdp->zsd_destroy(zoneid, NULL);
745*7c478bd9Sstevel@tonic-gate 			}
746*7c478bd9Sstevel@tonic-gate 		}
747*7c478bd9Sstevel@tonic-gate 	}
748*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
749*7c478bd9Sstevel@tonic-gate }
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate /*
752*7c478bd9Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
753*7c478bd9Sstevel@tonic-gate  * destroy the zone_zsd list.
754*7c478bd9Sstevel@tonic-gate  */
755*7c478bd9Sstevel@tonic-gate static void
756*7c478bd9Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
757*7c478bd9Sstevel@tonic-gate {
758*7c478bd9Sstevel@tonic-gate 	struct zsd_entry *t, *next;
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate 	/*
761*7c478bd9Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
762*7c478bd9Sstevel@tonic-gate 	 */
763*7c478bd9Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
764*7c478bd9Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
765*7c478bd9Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
766*7c478bd9Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
767*7c478bd9Sstevel@tonic-gate 	}
768*7c478bd9Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
769*7c478bd9Sstevel@tonic-gate }
770*7c478bd9Sstevel@tonic-gate 
771*7c478bd9Sstevel@tonic-gate /*
772*7c478bd9Sstevel@tonic-gate  * zone.cpu-shares resource control support.
773*7c478bd9Sstevel@tonic-gate  */
774*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
775*7c478bd9Sstevel@tonic-gate static rctl_qty_t
776*7c478bd9Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
777*7c478bd9Sstevel@tonic-gate {
778*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
779*7c478bd9Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
780*7c478bd9Sstevel@tonic-gate }
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
783*7c478bd9Sstevel@tonic-gate static int
784*7c478bd9Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
785*7c478bd9Sstevel@tonic-gate     rctl_qty_t nv)
786*7c478bd9Sstevel@tonic-gate {
787*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
788*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
789*7c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
790*7c478bd9Sstevel@tonic-gate 		return (0);
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
793*7c478bd9Sstevel@tonic-gate 	return (0);
794*7c478bd9Sstevel@tonic-gate }
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
797*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
798*7c478bd9Sstevel@tonic-gate 	zone_cpu_shares_usage,
799*7c478bd9Sstevel@tonic-gate 	zone_cpu_shares_set,
800*7c478bd9Sstevel@tonic-gate 	rcop_no_test
801*7c478bd9Sstevel@tonic-gate };
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
804*7c478bd9Sstevel@tonic-gate static rctl_qty_t
805*7c478bd9Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
806*7c478bd9Sstevel@tonic-gate {
807*7c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
808*7c478bd9Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
811*7c478bd9Sstevel@tonic-gate 
812*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
813*7c478bd9Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
814*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
815*7c478bd9Sstevel@tonic-gate 
816*7c478bd9Sstevel@tonic-gate 	return (nlwps);
817*7c478bd9Sstevel@tonic-gate }
818*7c478bd9Sstevel@tonic-gate 
819*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
820*7c478bd9Sstevel@tonic-gate static int
821*7c478bd9Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
822*7c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
823*7c478bd9Sstevel@tonic-gate {
824*7c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
827*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
828*7c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
829*7c478bd9Sstevel@tonic-gate 		return (0);
830*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
831*7c478bd9Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
832*7c478bd9Sstevel@tonic-gate 
833*7c478bd9Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
834*7c478bd9Sstevel@tonic-gate 		return (1);
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate 	return (0);
837*7c478bd9Sstevel@tonic-gate }
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
840*7c478bd9Sstevel@tonic-gate static int
841*7c478bd9Sstevel@tonic-gate zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) {
842*7c478bd9Sstevel@tonic-gate 
843*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
844*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
845*7c478bd9Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
846*7c478bd9Sstevel@tonic-gate 		return (0);
847*7c478bd9Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
848*7c478bd9Sstevel@tonic-gate 	return (0);
849*7c478bd9Sstevel@tonic-gate }
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
852*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
853*7c478bd9Sstevel@tonic-gate 	zone_lwps_usage,
854*7c478bd9Sstevel@tonic-gate 	zone_lwps_set,
855*7c478bd9Sstevel@tonic-gate 	zone_lwps_test,
856*7c478bd9Sstevel@tonic-gate };
857*7c478bd9Sstevel@tonic-gate 
858*7c478bd9Sstevel@tonic-gate /*
859*7c478bd9Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
860*7c478bd9Sstevel@tonic-gate  */
861*7c478bd9Sstevel@tonic-gate static void
862*7c478bd9Sstevel@tonic-gate zone_uniqid(zone_t *zone)
863*7c478bd9Sstevel@tonic-gate {
864*7c478bd9Sstevel@tonic-gate 	static uint64_t uniqid = 0;
865*7c478bd9Sstevel@tonic-gate 
866*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
867*7c478bd9Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
868*7c478bd9Sstevel@tonic-gate }
869*7c478bd9Sstevel@tonic-gate 
870*7c478bd9Sstevel@tonic-gate /*
871*7c478bd9Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
872*7c478bd9Sstevel@tonic-gate  */
873*7c478bd9Sstevel@tonic-gate struct cred *
874*7c478bd9Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
875*7c478bd9Sstevel@tonic-gate {
876*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
877*7c478bd9Sstevel@tonic-gate 	cred_t *cr;
878*7c478bd9Sstevel@tonic-gate 
879*7c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
880*7c478bd9Sstevel@tonic-gate 		return (NULL);
881*7c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
882*7c478bd9Sstevel@tonic-gate 	crhold(cr);
883*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
884*7c478bd9Sstevel@tonic-gate 	return (cr);
885*7c478bd9Sstevel@tonic-gate }
886*7c478bd9Sstevel@tonic-gate 
887*7c478bd9Sstevel@tonic-gate /*
888*7c478bd9Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
889*7c478bd9Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
890*7c478bd9Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
891*7c478bd9Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
892*7c478bd9Sstevel@tonic-gate  * zone_init().
893*7c478bd9Sstevel@tonic-gate  */
894*7c478bd9Sstevel@tonic-gate void
895*7c478bd9Sstevel@tonic-gate zone_zsd_init(void)
896*7c478bd9Sstevel@tonic-gate {
897*7c478bd9Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
898*7c478bd9Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
899*7c478bd9Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
900*7c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
901*7c478bd9Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
902*7c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
903*7c478bd9Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
904*7c478bd9Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
905*7c478bd9Sstevel@tonic-gate 
906*7c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
907*7c478bd9Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
908*7c478bd9Sstevel@tonic-gate 	zone0.zone_shares = 1;
909*7c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
910*7c478bd9Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
911*7c478bd9Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
912*7c478bd9Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
913*7c478bd9Sstevel@tonic-gate 	zone0.zone_ref = 1;
914*7c478bd9Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
915*7c478bd9Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
916*7c478bd9Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
917*7c478bd9Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
918*7c478bd9Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
919*7c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
920*7c478bd9Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
921*7c478bd9Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
922*7c478bd9Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
923*7c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
924*7c478bd9Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
925*7c478bd9Sstevel@tonic-gate 
926*7c478bd9Sstevel@tonic-gate 	/*
927*7c478bd9Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
928*7c478bd9Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
929*7c478bd9Sstevel@tonic-gate 	 * vfs_mountroot().
930*7c478bd9Sstevel@tonic-gate 	 */
931*7c478bd9Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
932*7c478bd9Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
933*7c478bd9Sstevel@tonic-gate 	zone0.zone_bootargs = NULL;
934*7c478bd9Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
935*7c478bd9Sstevel@tonic-gate 	/*
936*7c478bd9Sstevel@tonic-gate 	 * The global zone has all privileges
937*7c478bd9Sstevel@tonic-gate 	 */
938*7c478bd9Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
939*7c478bd9Sstevel@tonic-gate 	/*
940*7c478bd9Sstevel@tonic-gate 	 * Add p0 to the global zone
941*7c478bd9Sstevel@tonic-gate 	 */
942*7c478bd9Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
943*7c478bd9Sstevel@tonic-gate 	p0.p_zone = &zone0;
944*7c478bd9Sstevel@tonic-gate }
945*7c478bd9Sstevel@tonic-gate 
946*7c478bd9Sstevel@tonic-gate /*
947*7c478bd9Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
948*7c478bd9Sstevel@tonic-gate  */
949*7c478bd9Sstevel@tonic-gate void
950*7c478bd9Sstevel@tonic-gate zone_init(void)
951*7c478bd9Sstevel@tonic-gate {
952*7c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
953*7c478bd9Sstevel@tonic-gate 	rctl_val_t *dval;
954*7c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
955*7c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
956*7c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
957*7c478bd9Sstevel@tonic-gate 
958*7c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
959*7c478bd9Sstevel@tonic-gate 
960*7c478bd9Sstevel@tonic-gate 	/*
961*7c478bd9Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
962*7c478bd9Sstevel@tonic-gate 	 * global zone.
963*7c478bd9Sstevel@tonic-gate 	 */
964*7c478bd9Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
965*7c478bd9Sstevel@tonic-gate 
966*7c478bd9Sstevel@tonic-gate 	/*
967*7c478bd9Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
968*7c478bd9Sstevel@tonic-gate 	 */
969*7c478bd9Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
970*7c478bd9Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
971*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOBASIC |
972*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, FSS_MAXSHARES, FSS_MAXSHARES,
973*7c478bd9Sstevel@tonic-gate 	    &zone_cpu_shares_ops);
974*7c478bd9Sstevel@tonic-gate 
975*7c478bd9Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
976*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
977*7c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
978*7c478bd9Sstevel@tonic-gate 	/*
979*7c478bd9Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
980*7c478bd9Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
981*7c478bd9Sstevel@tonic-gate 	 */
982*7c478bd9Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
983*7c478bd9Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
984*7c478bd9Sstevel@tonic-gate 	dval->rcv_value = 1;
985*7c478bd9Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
986*7c478bd9Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
987*7c478bd9Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
988*7c478bd9Sstevel@tonic-gate 
989*7c478bd9Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
990*7c478bd9Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
991*7c478bd9Sstevel@tonic-gate 
992*7c478bd9Sstevel@tonic-gate 	/*
993*7c478bd9Sstevel@tonic-gate 	 * Initialize the ``global zone''.
994*7c478bd9Sstevel@tonic-gate 	 */
995*7c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
996*7c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
997*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
998*7c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
999*7c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
1000*7c478bd9Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
1001*7c478bd9Sstevel@tonic-gate 	    gp);
1002*7c478bd9Sstevel@tonic-gate 
1003*7c478bd9Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
1004*7c478bd9Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
1005*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
1006*7c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
1007*7c478bd9Sstevel@tonic-gate 	/*
1008*7c478bd9Sstevel@tonic-gate 	 * pool_default hasn't been initialized yet, so we let pool_init() take
1009*7c478bd9Sstevel@tonic-gate 	 * care of making the global zone is in the default pool.
1010*7c478bd9Sstevel@tonic-gate 	 */
1011*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
1012*7c478bd9Sstevel@tonic-gate 	zone_uniqid(&zone0);
1013*7c478bd9Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
1014*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
1015*7c478bd9Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
1016*7c478bd9Sstevel@tonic-gate 	    mod_hash_null_valdtor);
1017*7c478bd9Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
1018*7c478bd9Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
1019*7c478bd9Sstevel@tonic-gate 	zonecount = 1;
1020*7c478bd9Sstevel@tonic-gate 
1021*7c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
1022*7c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
1023*7c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
1024*7c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
1025*7c478bd9Sstevel@tonic-gate 	/*
1026*7c478bd9Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
1027*7c478bd9Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
1028*7c478bd9Sstevel@tonic-gate 	 */
1029*7c478bd9Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
1030*7c478bd9Sstevel@tonic-gate 	/*
1031*7c478bd9Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
1032*7c478bd9Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
1033*7c478bd9Sstevel@tonic-gate 	 */
1034*7c478bd9Sstevel@tonic-gate 	global_zone = &zone0;
1035*7c478bd9Sstevel@tonic-gate }
1036*7c478bd9Sstevel@tonic-gate 
1037*7c478bd9Sstevel@tonic-gate static void
1038*7c478bd9Sstevel@tonic-gate zone_free(zone_t *zone)
1039*7c478bd9Sstevel@tonic-gate {
1040*7c478bd9Sstevel@tonic-gate 	ASSERT(zone != global_zone);
1041*7c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
1042*7c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
1043*7c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
1044*7c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
1045*7c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
1046*7c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
1047*7c478bd9Sstevel@tonic-gate 
1048*7c478bd9Sstevel@tonic-gate 	/* remove from deathrow list */
1049*7c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
1050*7c478bd9Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
1051*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
1052*7c478bd9Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
1053*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
1054*7c478bd9Sstevel@tonic-gate 	}
1055*7c478bd9Sstevel@tonic-gate 
1056*7c478bd9Sstevel@tonic-gate 	zone_free_zsd(zone);
1057*7c478bd9Sstevel@tonic-gate 
1058*7c478bd9Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
1059*7c478bd9Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
1060*7c478bd9Sstevel@tonic-gate 	if (zone->zone_rootpath)
1061*7c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
1062*7c478bd9Sstevel@tonic-gate 	if (zone->zone_name != NULL)
1063*7c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
1064*7c478bd9Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
1065*7c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
1066*7c478bd9Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
1067*7c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
1068*7c478bd9Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
1069*7c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
1070*7c478bd9Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
1071*7c478bd9Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
1072*7c478bd9Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
1073*7c478bd9Sstevel@tonic-gate 		kmem_free(zone->zone_bootargs, ZONEBOOTARGS_MAX);
1074*7c478bd9Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
1075*7c478bd9Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
1076*7c478bd9Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
1077*7c478bd9Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
1078*7c478bd9Sstevel@tonic-gate }
1079*7c478bd9Sstevel@tonic-gate 
1080*7c478bd9Sstevel@tonic-gate /*
1081*7c478bd9Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
1082*7c478bd9Sstevel@tonic-gate  * status values.
1083*7c478bd9Sstevel@tonic-gate  */
1084*7c478bd9Sstevel@tonic-gate /*
1085*7c478bd9Sstevel@tonic-gate  * Convenience function for setting zone status.
1086*7c478bd9Sstevel@tonic-gate  */
1087*7c478bd9Sstevel@tonic-gate static void
1088*7c478bd9Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
1089*7c478bd9Sstevel@tonic-gate {
1090*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
1091*7c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
1092*7c478bd9Sstevel@tonic-gate 	    status >= zone_status_get(zone));
1093*7c478bd9Sstevel@tonic-gate 	zone->zone_status = status;
1094*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
1095*7c478bd9Sstevel@tonic-gate }
1096*7c478bd9Sstevel@tonic-gate 
1097*7c478bd9Sstevel@tonic-gate /*
1098*7c478bd9Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
1099*7c478bd9Sstevel@tonic-gate  * change after it is retrieved.
1100*7c478bd9Sstevel@tonic-gate  */
1101*7c478bd9Sstevel@tonic-gate zone_status_t
1102*7c478bd9Sstevel@tonic-gate zone_status_get(zone_t *zone)
1103*7c478bd9Sstevel@tonic-gate {
1104*7c478bd9Sstevel@tonic-gate 	return (zone->zone_status);
1105*7c478bd9Sstevel@tonic-gate }
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate static int
1108*7c478bd9Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
1109*7c478bd9Sstevel@tonic-gate {
1110*7c478bd9Sstevel@tonic-gate 	char *bootargs = kmem_zalloc(ZONEBOOTARGS_MAX, KM_SLEEP);
1111*7c478bd9Sstevel@tonic-gate 	size_t len;
1112*7c478bd9Sstevel@tonic-gate 	int err;
1113*7c478bd9Sstevel@tonic-gate 
1114*7c478bd9Sstevel@tonic-gate 	err = copyinstr(zone_bootargs, bootargs, ZONEBOOTARGS_MAX - 1, &len);
1115*7c478bd9Sstevel@tonic-gate 	if (err != 0) {
1116*7c478bd9Sstevel@tonic-gate 		kmem_free(bootargs, ZONEBOOTARGS_MAX);
1117*7c478bd9Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
1118*7c478bd9Sstevel@tonic-gate 	}
1119*7c478bd9Sstevel@tonic-gate 	bootargs[len] = '\0';
1120*7c478bd9Sstevel@tonic-gate 
1121*7c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_bootargs == NULL);
1122*7c478bd9Sstevel@tonic-gate 	zone->zone_bootargs = bootargs;
1123*7c478bd9Sstevel@tonic-gate 	return (0);
1124*7c478bd9Sstevel@tonic-gate }
1125*7c478bd9Sstevel@tonic-gate 
1126*7c478bd9Sstevel@tonic-gate /*
1127*7c478bd9Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
1128*7c478bd9Sstevel@tonic-gate  */
1129*7c478bd9Sstevel@tonic-gate void
1130*7c478bd9Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
1131*7c478bd9Sstevel@tonic-gate {
1132*7c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
1133*7c478bd9Sstevel@tonic-gate 
1134*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
1135*7c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
1136*7c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
1137*7c478bd9Sstevel@tonic-gate 	}
1138*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
1139*7c478bd9Sstevel@tonic-gate }
1140*7c478bd9Sstevel@tonic-gate 
1141*7c478bd9Sstevel@tonic-gate /*
1142*7c478bd9Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
1143*7c478bd9Sstevel@tonic-gate  */
1144*7c478bd9Sstevel@tonic-gate static void
1145*7c478bd9Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
1146*7c478bd9Sstevel@tonic-gate {
1147*7c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
1148*7c478bd9Sstevel@tonic-gate 
1149*7c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
1150*7c478bd9Sstevel@tonic-gate 
1151*7c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
1152*7c478bd9Sstevel@tonic-gate 	    str);
1153*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
1154*7c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
1155*7c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
1156*7c478bd9Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
1157*7c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
1158*7c478bd9Sstevel@tonic-gate 	}
1159*7c478bd9Sstevel@tonic-gate 	/*
1160*7c478bd9Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
1161*7c478bd9Sstevel@tonic-gate 	 */
1162*7c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
1163*7c478bd9Sstevel@tonic-gate }
1164*7c478bd9Sstevel@tonic-gate 
1165*7c478bd9Sstevel@tonic-gate /*
1166*7c478bd9Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
1167*7c478bd9Sstevel@tonic-gate  * if signaled, non-zero otherwise.
1168*7c478bd9Sstevel@tonic-gate  */
1169*7c478bd9Sstevel@tonic-gate int
1170*7c478bd9Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
1171*7c478bd9Sstevel@tonic-gate {
1172*7c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
1173*7c478bd9Sstevel@tonic-gate 
1174*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
1175*7c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
1176*7c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
1177*7c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
1178*7c478bd9Sstevel@tonic-gate 			return (0);
1179*7c478bd9Sstevel@tonic-gate 		}
1180*7c478bd9Sstevel@tonic-gate 	}
1181*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
1182*7c478bd9Sstevel@tonic-gate 	return (1);
1183*7c478bd9Sstevel@tonic-gate }
1184*7c478bd9Sstevel@tonic-gate 
1185*7c478bd9Sstevel@tonic-gate /*
1186*7c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
1187*7c478bd9Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
1188*7c478bd9Sstevel@tonic-gate  * otherwise.
1189*7c478bd9Sstevel@tonic-gate  */
1190*7c478bd9Sstevel@tonic-gate clock_t
1191*7c478bd9Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
1192*7c478bd9Sstevel@tonic-gate {
1193*7c478bd9Sstevel@tonic-gate 	clock_t timeleft = 0;
1194*7c478bd9Sstevel@tonic-gate 
1195*7c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
1196*7c478bd9Sstevel@tonic-gate 
1197*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
1198*7c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
1199*7c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
1200*7c478bd9Sstevel@tonic-gate 	}
1201*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
1202*7c478bd9Sstevel@tonic-gate 	return (timeleft);
1203*7c478bd9Sstevel@tonic-gate }
1204*7c478bd9Sstevel@tonic-gate 
1205*7c478bd9Sstevel@tonic-gate /*
1206*7c478bd9Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
1207*7c478bd9Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
1208*7c478bd9Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
1209*7c478bd9Sstevel@tonic-gate  */
1210*7c478bd9Sstevel@tonic-gate clock_t
1211*7c478bd9Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
1212*7c478bd9Sstevel@tonic-gate {
1213*7c478bd9Sstevel@tonic-gate 	clock_t timeleft = tim - lbolt;
1214*7c478bd9Sstevel@tonic-gate 
1215*7c478bd9Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
1216*7c478bd9Sstevel@tonic-gate 
1217*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
1218*7c478bd9Sstevel@tonic-gate 	while (zone->zone_status < status) {
1219*7c478bd9Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
1220*7c478bd9Sstevel@tonic-gate 		    tim);
1221*7c478bd9Sstevel@tonic-gate 		if (timeleft <= 0)
1222*7c478bd9Sstevel@tonic-gate 			break;
1223*7c478bd9Sstevel@tonic-gate 	}
1224*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
1225*7c478bd9Sstevel@tonic-gate 	return (timeleft);
1226*7c478bd9Sstevel@tonic-gate }
1227*7c478bd9Sstevel@tonic-gate 
1228*7c478bd9Sstevel@tonic-gate /*
1229*7c478bd9Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
1230*7c478bd9Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
1231*7c478bd9Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
1232*7c478bd9Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
1233*7c478bd9Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
1234*7c478bd9Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
1235*7c478bd9Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
1236*7c478bd9Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
1237*7c478bd9Sstevel@tonic-gate  * is "dead".
1238*7c478bd9Sstevel@tonic-gate  *
1239*7c478bd9Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
1240*7c478bd9Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
1241*7c478bd9Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
1242*7c478bd9Sstevel@tonic-gate  * that may be less innocuous than the driver case.
1243*7c478bd9Sstevel@tonic-gate  */
1244*7c478bd9Sstevel@tonic-gate 
1245*7c478bd9Sstevel@tonic-gate int zone_wait_for_cred = 0;
1246*7c478bd9Sstevel@tonic-gate 
1247*7c478bd9Sstevel@tonic-gate static void
1248*7c478bd9Sstevel@tonic-gate zone_hold_locked(zone_t *z)
1249*7c478bd9Sstevel@tonic-gate {
1250*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
1251*7c478bd9Sstevel@tonic-gate 	z->zone_ref++;
1252*7c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
1253*7c478bd9Sstevel@tonic-gate }
1254*7c478bd9Sstevel@tonic-gate 
1255*7c478bd9Sstevel@tonic-gate void
1256*7c478bd9Sstevel@tonic-gate zone_hold(zone_t *z)
1257*7c478bd9Sstevel@tonic-gate {
1258*7c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
1259*7c478bd9Sstevel@tonic-gate 	zone_hold_locked(z);
1260*7c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
1261*7c478bd9Sstevel@tonic-gate }
1262*7c478bd9Sstevel@tonic-gate 
1263*7c478bd9Sstevel@tonic-gate /*
1264*7c478bd9Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
1265*7c478bd9Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
1266*7c478bd9Sstevel@tonic-gate  * be destroyed.
1267*7c478bd9Sstevel@tonic-gate  */
1268*7c478bd9Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
1269*7c478bd9Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
1270*7c478bd9Sstevel@tonic-gate 
1271*7c478bd9Sstevel@tonic-gate void
1272*7c478bd9Sstevel@tonic-gate zone_rele(zone_t *z)
1273*7c478bd9Sstevel@tonic-gate {
1274*7c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
1275*7c478bd9Sstevel@tonic-gate 
1276*7c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
1277*7c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
1278*7c478bd9Sstevel@tonic-gate 	z->zone_ref--;
1279*7c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
1280*7c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
1281*7c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
1282*7c478bd9Sstevel@tonic-gate 		zone_free(z);
1283*7c478bd9Sstevel@tonic-gate 		return;
1284*7c478bd9Sstevel@tonic-gate 	}
1285*7c478bd9Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
1286*7c478bd9Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
1287*7c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
1288*7c478bd9Sstevel@tonic-gate 
1289*7c478bd9Sstevel@tonic-gate 	if (wakeup) {
1290*7c478bd9Sstevel@tonic-gate 		/*
1291*7c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
1292*7c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
1293*7c478bd9Sstevel@tonic-gate 		 */
1294*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
1295*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
1296*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
1297*7c478bd9Sstevel@tonic-gate 	}
1298*7c478bd9Sstevel@tonic-gate }
1299*7c478bd9Sstevel@tonic-gate 
1300*7c478bd9Sstevel@tonic-gate void
1301*7c478bd9Sstevel@tonic-gate zone_cred_hold(zone_t *z)
1302*7c478bd9Sstevel@tonic-gate {
1303*7c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
1304*7c478bd9Sstevel@tonic-gate 	z->zone_cred_ref++;
1305*7c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
1306*7c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
1307*7c478bd9Sstevel@tonic-gate }
1308*7c478bd9Sstevel@tonic-gate 
1309*7c478bd9Sstevel@tonic-gate void
1310*7c478bd9Sstevel@tonic-gate zone_cred_rele(zone_t *z)
1311*7c478bd9Sstevel@tonic-gate {
1312*7c478bd9Sstevel@tonic-gate 	boolean_t wakeup;
1313*7c478bd9Sstevel@tonic-gate 
1314*7c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
1315*7c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
1316*7c478bd9Sstevel@tonic-gate 	z->zone_cred_ref--;
1317*7c478bd9Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
1318*7c478bd9Sstevel@tonic-gate 		/* no more refs, free the structure */
1319*7c478bd9Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
1320*7c478bd9Sstevel@tonic-gate 		zone_free(z);
1321*7c478bd9Sstevel@tonic-gate 		return;
1322*7c478bd9Sstevel@tonic-gate 	}
1323*7c478bd9Sstevel@tonic-gate 	/*
1324*7c478bd9Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
1325*7c478bd9Sstevel@tonic-gate 	 * out, and they have, signal it.
1326*7c478bd9Sstevel@tonic-gate 	 */
1327*7c478bd9Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
1328*7c478bd9Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
1329*7c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
1330*7c478bd9Sstevel@tonic-gate 
1331*7c478bd9Sstevel@tonic-gate 	if (wakeup) {
1332*7c478bd9Sstevel@tonic-gate 		/*
1333*7c478bd9Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
1334*7c478bd9Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
1335*7c478bd9Sstevel@tonic-gate 		 */
1336*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
1337*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
1338*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
1339*7c478bd9Sstevel@tonic-gate 	}
1340*7c478bd9Sstevel@tonic-gate }
1341*7c478bd9Sstevel@tonic-gate 
1342*7c478bd9Sstevel@tonic-gate void
1343*7c478bd9Sstevel@tonic-gate zone_task_hold(zone_t *z)
1344*7c478bd9Sstevel@tonic-gate {
1345*7c478bd9Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
1346*7c478bd9Sstevel@tonic-gate 	z->zone_ntasks++;
1347*7c478bd9Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
1348*7c478bd9Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
1349*7c478bd9Sstevel@tonic-gate }
1350*7c478bd9Sstevel@tonic-gate 
1351*7c478bd9Sstevel@tonic-gate void
1352*7c478bd9Sstevel@tonic-gate zone_task_rele(zone_t *zone)
1353*7c478bd9Sstevel@tonic-gate {
1354*7c478bd9Sstevel@tonic-gate 	uint_t refcnt;
1355*7c478bd9Sstevel@tonic-gate 
1356*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
1357*7c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
1358*7c478bd9Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
1359*7c478bd9Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
1360*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
1361*7c478bd9Sstevel@tonic-gate 		return;
1362*7c478bd9Sstevel@tonic-gate 	}
1363*7c478bd9Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
1364*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
1365*7c478bd9Sstevel@tonic-gate 	if (refcnt == 1) {
1366*7c478bd9Sstevel@tonic-gate 		/*
1367*7c478bd9Sstevel@tonic-gate 		 * See if the zone is shutting down.
1368*7c478bd9Sstevel@tonic-gate 		 */
1369*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
1370*7c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
1371*7c478bd9Sstevel@tonic-gate 			goto out;
1372*7c478bd9Sstevel@tonic-gate 		}
1373*7c478bd9Sstevel@tonic-gate 
1374*7c478bd9Sstevel@tonic-gate 		/*
1375*7c478bd9Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
1376*7c478bd9Sstevel@tonic-gate 		 * dropped zone_lock.
1377*7c478bd9Sstevel@tonic-gate 		 */
1378*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
1379*7c478bd9Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
1380*7c478bd9Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
1381*7c478bd9Sstevel@tonic-gate 			goto out;
1382*7c478bd9Sstevel@tonic-gate 		}
1383*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
1384*7c478bd9Sstevel@tonic-gate 
1385*7c478bd9Sstevel@tonic-gate 		/*
1386*7c478bd9Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
1387*7c478bd9Sstevel@tonic-gate 		 */
1388*7c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
1389*7c478bd9Sstevel@tonic-gate 		goto out;
1390*7c478bd9Sstevel@tonic-gate 	}
1391*7c478bd9Sstevel@tonic-gate 
1392*7c478bd9Sstevel@tonic-gate 	ASSERT(refcnt == 0);
1393*7c478bd9Sstevel@tonic-gate 	/*
1394*7c478bd9Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
1395*7c478bd9Sstevel@tonic-gate 	 */
1396*7c478bd9Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
1397*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
1398*7c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
1399*7c478bd9Sstevel@tonic-gate out:
1400*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
1401*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
1402*7c478bd9Sstevel@tonic-gate }
1403*7c478bd9Sstevel@tonic-gate 
1404*7c478bd9Sstevel@tonic-gate zoneid_t
1405*7c478bd9Sstevel@tonic-gate getzoneid(void)
1406*7c478bd9Sstevel@tonic-gate {
1407*7c478bd9Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
1408*7c478bd9Sstevel@tonic-gate }
1409*7c478bd9Sstevel@tonic-gate 
1410*7c478bd9Sstevel@tonic-gate /*
1411*7c478bd9Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
1412*7c478bd9Sstevel@tonic-gate  * check the validity of a zone's state.
1413*7c478bd9Sstevel@tonic-gate  */
1414*7c478bd9Sstevel@tonic-gate static zone_t *
1415*7c478bd9Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
1416*7c478bd9Sstevel@tonic-gate {
1417*7c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
1418*7c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
1419*7c478bd9Sstevel@tonic-gate 
1420*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
1421*7c478bd9Sstevel@tonic-gate 
1422*7c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
1423*7c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
1424*7c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
1425*7c478bd9Sstevel@tonic-gate 	return (zone);
1426*7c478bd9Sstevel@tonic-gate }
1427*7c478bd9Sstevel@tonic-gate 
1428*7c478bd9Sstevel@tonic-gate static zone_t *
1429*7c478bd9Sstevel@tonic-gate zone_find_all_by_name(char *name)
1430*7c478bd9Sstevel@tonic-gate {
1431*7c478bd9Sstevel@tonic-gate 	mod_hash_val_t hv;
1432*7c478bd9Sstevel@tonic-gate 	zone_t *zone = NULL;
1433*7c478bd9Sstevel@tonic-gate 
1434*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
1435*7c478bd9Sstevel@tonic-gate 
1436*7c478bd9Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
1437*7c478bd9Sstevel@tonic-gate 		zone = (zone_t *)hv;
1438*7c478bd9Sstevel@tonic-gate 	return (zone);
1439*7c478bd9Sstevel@tonic-gate }
1440*7c478bd9Sstevel@tonic-gate 
1441*7c478bd9Sstevel@tonic-gate /*
1442*7c478bd9Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
1443*7c478bd9Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
1444*7c478bd9Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
1445*7c478bd9Sstevel@tonic-gate  *
1446*7c478bd9Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
1447*7c478bd9Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
1448*7c478bd9Sstevel@tonic-gate  */
1449*7c478bd9Sstevel@tonic-gate zone_t *
1450*7c478bd9Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
1451*7c478bd9Sstevel@tonic-gate {
1452*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
1453*7c478bd9Sstevel@tonic-gate 	zone_status_t status;
1454*7c478bd9Sstevel@tonic-gate 
1455*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
1456*7c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
1457*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
1458*7c478bd9Sstevel@tonic-gate 		return (NULL);
1459*7c478bd9Sstevel@tonic-gate 	}
1460*7c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
1461*7c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
1462*7c478bd9Sstevel@tonic-gate 		/*
1463*7c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
1464*7c478bd9Sstevel@tonic-gate 		 */
1465*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
1466*7c478bd9Sstevel@tonic-gate 		return (NULL);
1467*7c478bd9Sstevel@tonic-gate 	}
1468*7c478bd9Sstevel@tonic-gate 	zone_hold(zone);
1469*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
1470*7c478bd9Sstevel@tonic-gate 	return (zone);
1471*7c478bd9Sstevel@tonic-gate }
1472*7c478bd9Sstevel@tonic-gate 
1473*7c478bd9Sstevel@tonic-gate /*
1474*7c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
1475*7c478bd9Sstevel@tonic-gate  */
1476*7c478bd9Sstevel@tonic-gate zone_t *
1477*7c478bd9Sstevel@tonic-gate zone_find_by_name(char *name)
1478*7c478bd9Sstevel@tonic-gate {
1479*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
1480*7c478bd9Sstevel@tonic-gate 	zone_status_t status;
1481*7c478bd9Sstevel@tonic-gate 
1482*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
1483*7c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
1484*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
1485*7c478bd9Sstevel@tonic-gate 		return (NULL);
1486*7c478bd9Sstevel@tonic-gate 	}
1487*7c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
1488*7c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
1489*7c478bd9Sstevel@tonic-gate 		/*
1490*7c478bd9Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
1491*7c478bd9Sstevel@tonic-gate 		 */
1492*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
1493*7c478bd9Sstevel@tonic-gate 		return (NULL);
1494*7c478bd9Sstevel@tonic-gate 	}
1495*7c478bd9Sstevel@tonic-gate 	zone_hold(zone);
1496*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
1497*7c478bd9Sstevel@tonic-gate 	return (zone);
1498*7c478bd9Sstevel@tonic-gate }
1499*7c478bd9Sstevel@tonic-gate 
1500*7c478bd9Sstevel@tonic-gate /*
1501*7c478bd9Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
1502*7c478bd9Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
1503*7c478bd9Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
1504*7c478bd9Sstevel@tonic-gate  * zone "foo".
1505*7c478bd9Sstevel@tonic-gate  *
1506*7c478bd9Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
1507*7c478bd9Sstevel@tonic-gate  * very least every path will be contained in the global zone.
1508*7c478bd9Sstevel@tonic-gate  *
1509*7c478bd9Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
1510*7c478bd9Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
1511*7c478bd9Sstevel@tonic-gate  */
1512*7c478bd9Sstevel@tonic-gate zone_t *
1513*7c478bd9Sstevel@tonic-gate zone_find_by_path(const char *path)
1514*7c478bd9Sstevel@tonic-gate {
1515*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
1516*7c478bd9Sstevel@tonic-gate 	zone_t *zret = NULL;
1517*7c478bd9Sstevel@tonic-gate 	zone_status_t status;
1518*7c478bd9Sstevel@tonic-gate 
1519*7c478bd9Sstevel@tonic-gate 	if (path == NULL) {
1520*7c478bd9Sstevel@tonic-gate 		/*
1521*7c478bd9Sstevel@tonic-gate 		 * Call from rootconf().
1522*7c478bd9Sstevel@tonic-gate 		 */
1523*7c478bd9Sstevel@tonic-gate 		zone_hold(global_zone);
1524*7c478bd9Sstevel@tonic-gate 		return (global_zone);
1525*7c478bd9Sstevel@tonic-gate 	}
1526*7c478bd9Sstevel@tonic-gate 	ASSERT(*path == '/');
1527*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
1528*7c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
1529*7c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
1530*7c478bd9Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
1531*7c478bd9Sstevel@tonic-gate 			zret = zone;
1532*7c478bd9Sstevel@tonic-gate 	}
1533*7c478bd9Sstevel@tonic-gate 	ASSERT(zret != NULL);
1534*7c478bd9Sstevel@tonic-gate 	status = zone_status_get(zret);
1535*7c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
1536*7c478bd9Sstevel@tonic-gate 		/*
1537*7c478bd9Sstevel@tonic-gate 		 * Zone practically doesn't exist.
1538*7c478bd9Sstevel@tonic-gate 		 */
1539*7c478bd9Sstevel@tonic-gate 		zret = global_zone;
1540*7c478bd9Sstevel@tonic-gate 	}
1541*7c478bd9Sstevel@tonic-gate 	zone_hold(zret);
1542*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
1543*7c478bd9Sstevel@tonic-gate 	return (zret);
1544*7c478bd9Sstevel@tonic-gate }
1545*7c478bd9Sstevel@tonic-gate 
1546*7c478bd9Sstevel@tonic-gate /*
1547*7c478bd9Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
1548*7c478bd9Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
1549*7c478bd9Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
1550*7c478bd9Sstevel@tonic-gate  */
1551*7c478bd9Sstevel@tonic-gate int
1552*7c478bd9Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
1553*7c478bd9Sstevel@tonic-gate {
1554*7c478bd9Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
1555*7c478bd9Sstevel@tonic-gate 
1556*7c478bd9Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
1557*7c478bd9Sstevel@tonic-gate }
1558*7c478bd9Sstevel@tonic-gate 
1559*7c478bd9Sstevel@tonic-gate /*
1560*7c478bd9Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
1561*7c478bd9Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
1562*7c478bd9Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
1563*7c478bd9Sstevel@tonic-gate  */
1564*7c478bd9Sstevel@tonic-gate int
1565*7c478bd9Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
1566*7c478bd9Sstevel@tonic-gate {
1567*7c478bd9Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
1568*7c478bd9Sstevel@tonic-gate 
1569*7c478bd9Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
1570*7c478bd9Sstevel@tonic-gate }
1571*7c478bd9Sstevel@tonic-gate 
1572*7c478bd9Sstevel@tonic-gate /*
1573*7c478bd9Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
1574*7c478bd9Sstevel@tonic-gate  */
1575*7c478bd9Sstevel@tonic-gate pool_t *
1576*7c478bd9Sstevel@tonic-gate zone_pool_get(zone_t *zone)
1577*7c478bd9Sstevel@tonic-gate {
1578*7c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
1579*7c478bd9Sstevel@tonic-gate 
1580*7c478bd9Sstevel@tonic-gate 	return (zone->zone_pool);
1581*7c478bd9Sstevel@tonic-gate }
1582*7c478bd9Sstevel@tonic-gate 
1583*7c478bd9Sstevel@tonic-gate /*
1584*7c478bd9Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
1585*7c478bd9Sstevel@tonic-gate  * the resources in the new pool.
1586*7c478bd9Sstevel@tonic-gate  */
1587*7c478bd9Sstevel@tonic-gate void
1588*7c478bd9Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
1589*7c478bd9Sstevel@tonic-gate {
1590*7c478bd9Sstevel@tonic-gate 	ASSERT(pool_lock_held());
1591*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1592*7c478bd9Sstevel@tonic-gate 
1593*7c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool;
1594*7c478bd9Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
1595*7c478bd9Sstevel@tonic-gate }
1596*7c478bd9Sstevel@tonic-gate 
1597*7c478bd9Sstevel@tonic-gate /*
1598*7c478bd9Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
1599*7c478bd9Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
1600*7c478bd9Sstevel@tonic-gate  * facility is disabled.
1601*7c478bd9Sstevel@tonic-gate  */
1602*7c478bd9Sstevel@tonic-gate psetid_t
1603*7c478bd9Sstevel@tonic-gate zone_pset_get(zone_t *zone)
1604*7c478bd9Sstevel@tonic-gate {
1605*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1606*7c478bd9Sstevel@tonic-gate 
1607*7c478bd9Sstevel@tonic-gate 	return (zone->zone_psetid);
1608*7c478bd9Sstevel@tonic-gate }
1609*7c478bd9Sstevel@tonic-gate 
1610*7c478bd9Sstevel@tonic-gate /*
1611*7c478bd9Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
1612*7c478bd9Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
1613*7c478bd9Sstevel@tonic-gate  * resources in the new processor set.
1614*7c478bd9Sstevel@tonic-gate  */
1615*7c478bd9Sstevel@tonic-gate void
1616*7c478bd9Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
1617*7c478bd9Sstevel@tonic-gate {
1618*7c478bd9Sstevel@tonic-gate 	psetid_t oldpsetid;
1619*7c478bd9Sstevel@tonic-gate 
1620*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1621*7c478bd9Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
1622*7c478bd9Sstevel@tonic-gate 
1623*7c478bd9Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
1624*7c478bd9Sstevel@tonic-gate 		return;
1625*7c478bd9Sstevel@tonic-gate 	/*
1626*7c478bd9Sstevel@tonic-gate 	 * Global zone sees all.
1627*7c478bd9Sstevel@tonic-gate 	 */
1628*7c478bd9Sstevel@tonic-gate 	if (zone != global_zone) {
1629*7c478bd9Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
1630*7c478bd9Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
1631*7c478bd9Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
1632*7c478bd9Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
1633*7c478bd9Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
1634*7c478bd9Sstevel@tonic-gate 	}
1635*7c478bd9Sstevel@tonic-gate 	/*
1636*7c478bd9Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
1637*7c478bd9Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
1638*7c478bd9Sstevel@tonic-gate 	 */
1639*7c478bd9Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
1640*7c478bd9Sstevel@tonic-gate 		zone->zone_ncpus = 0;
1641*7c478bd9Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
1642*7c478bd9Sstevel@tonic-gate 	}
1643*7c478bd9Sstevel@tonic-gate }
1644*7c478bd9Sstevel@tonic-gate 
1645*7c478bd9Sstevel@tonic-gate /*
1646*7c478bd9Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
1647*7c478bd9Sstevel@tonic-gate  * each of them.
1648*7c478bd9Sstevel@tonic-gate  *
1649*7c478bd9Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
1650*7c478bd9Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
1651*7c478bd9Sstevel@tonic-gate  * common locks and their interactions with zones.
1652*7c478bd9Sstevel@tonic-gate  */
1653*7c478bd9Sstevel@tonic-gate int
1654*7c478bd9Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
1655*7c478bd9Sstevel@tonic-gate {
1656*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
1657*7c478bd9Sstevel@tonic-gate 	int ret = 0;
1658*7c478bd9Sstevel@tonic-gate 	zone_status_t status;
1659*7c478bd9Sstevel@tonic-gate 
1660*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
1661*7c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
1662*7c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
1663*7c478bd9Sstevel@tonic-gate 		/*
1664*7c478bd9Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
1665*7c478bd9Sstevel@tonic-gate 		 */
1666*7c478bd9Sstevel@tonic-gate 		status = zone_status_get(zone);
1667*7c478bd9Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
1668*7c478bd9Sstevel@tonic-gate 			continue;
1669*7c478bd9Sstevel@tonic-gate 		/*
1670*7c478bd9Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
1671*7c478bd9Sstevel@tonic-gate 		 * non-zero value.
1672*7c478bd9Sstevel@tonic-gate 		 */
1673*7c478bd9Sstevel@tonic-gate 		ret = (*cb)(zone, data);
1674*7c478bd9Sstevel@tonic-gate 		if (ret != 0)
1675*7c478bd9Sstevel@tonic-gate 			break;
1676*7c478bd9Sstevel@tonic-gate 	}
1677*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
1678*7c478bd9Sstevel@tonic-gate 	return (ret);
1679*7c478bd9Sstevel@tonic-gate }
1680*7c478bd9Sstevel@tonic-gate 
1681*7c478bd9Sstevel@tonic-gate static int
1682*7c478bd9Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
1683*7c478bd9Sstevel@tonic-gate {
1684*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
1685*7c478bd9Sstevel@tonic-gate 	int trycount;
1686*7c478bd9Sstevel@tonic-gate 	int error = 0;
1687*7c478bd9Sstevel@tonic-gate 	char *path;
1688*7c478bd9Sstevel@tonic-gate 	struct pathname upn, pn;
1689*7c478bd9Sstevel@tonic-gate 	size_t pathlen;
1690*7c478bd9Sstevel@tonic-gate 
1691*7c478bd9Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
1692*7c478bd9Sstevel@tonic-gate 		return (error);
1693*7c478bd9Sstevel@tonic-gate 
1694*7c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
1695*7c478bd9Sstevel@tonic-gate 
1696*7c478bd9Sstevel@tonic-gate 	/* prevent infinite loop */
1697*7c478bd9Sstevel@tonic-gate 	trycount = 10;
1698*7c478bd9Sstevel@tonic-gate 	for (;;) {
1699*7c478bd9Sstevel@tonic-gate 		if (--trycount <= 0) {
1700*7c478bd9Sstevel@tonic-gate 			error = ESTALE;
1701*7c478bd9Sstevel@tonic-gate 			goto out;
1702*7c478bd9Sstevel@tonic-gate 		}
1703*7c478bd9Sstevel@tonic-gate 
1704*7c478bd9Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
1705*7c478bd9Sstevel@tonic-gate 			/*
1706*7c478bd9Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
1707*7c478bd9Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
1708*7c478bd9Sstevel@tonic-gate 			 * Get the new 'vp' if so.
1709*7c478bd9Sstevel@tonic-gate 			 */
1710*7c478bd9Sstevel@tonic-gate 			if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 &&
1711*7c478bd9Sstevel@tonic-gate 			    (vp->v_vfsmountedhere == NULL ||
1712*7c478bd9Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
1713*7c478bd9Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
1714*7c478bd9Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
1715*7c478bd9Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
1716*7c478bd9Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
1717*7c478bd9Sstevel@tonic-gate 				path[pathlen - 2] = '/';
1718*7c478bd9Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
1719*7c478bd9Sstevel@tonic-gate 				pn_free(&pn);
1720*7c478bd9Sstevel@tonic-gate 				pn_free(&upn);
1721*7c478bd9Sstevel@tonic-gate 
1722*7c478bd9Sstevel@tonic-gate 				/* Success! */
1723*7c478bd9Sstevel@tonic-gate 				break;
1724*7c478bd9Sstevel@tonic-gate 			}
1725*7c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
1726*7c478bd9Sstevel@tonic-gate 		}
1727*7c478bd9Sstevel@tonic-gate 		if (error != ESTALE)
1728*7c478bd9Sstevel@tonic-gate 			goto out;
1729*7c478bd9Sstevel@tonic-gate 	}
1730*7c478bd9Sstevel@tonic-gate 
1731*7c478bd9Sstevel@tonic-gate 	ASSERT(error == 0);
1732*7c478bd9Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
1733*7c478bd9Sstevel@tonic-gate 	zone->zone_rootpath = path;
1734*7c478bd9Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
1735*7c478bd9Sstevel@tonic-gate 	return (0);
1736*7c478bd9Sstevel@tonic-gate 
1737*7c478bd9Sstevel@tonic-gate out:
1738*7c478bd9Sstevel@tonic-gate 	pn_free(&pn);
1739*7c478bd9Sstevel@tonic-gate 	pn_free(&upn);
1740*7c478bd9Sstevel@tonic-gate 	return (error);
1741*7c478bd9Sstevel@tonic-gate }
1742*7c478bd9Sstevel@tonic-gate 
1743*7c478bd9Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
1744*7c478bd9Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
1745*7c478bd9Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
1746*7c478bd9Sstevel@tonic-gate 
1747*7c478bd9Sstevel@tonic-gate static int
1748*7c478bd9Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
1749*7c478bd9Sstevel@tonic-gate {
1750*7c478bd9Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
1751*7c478bd9Sstevel@tonic-gate 	size_t len;
1752*7c478bd9Sstevel@tonic-gate 	int i, err;
1753*7c478bd9Sstevel@tonic-gate 
1754*7c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
1755*7c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
1756*7c478bd9Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
1757*7c478bd9Sstevel@tonic-gate 	}
1758*7c478bd9Sstevel@tonic-gate 
1759*7c478bd9Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
1760*7c478bd9Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
1761*7c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
1762*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1763*7c478bd9Sstevel@tonic-gate 	}
1764*7c478bd9Sstevel@tonic-gate 
1765*7c478bd9Sstevel@tonic-gate 	/*
1766*7c478bd9Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
1767*7c478bd9Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
1768*7c478bd9Sstevel@tonic-gate 	 */
1769*7c478bd9Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
1770*7c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
1771*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1772*7c478bd9Sstevel@tonic-gate 	}
1773*7c478bd9Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
1774*7c478bd9Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
1775*7c478bd9Sstevel@tonic-gate 		    kname[i] != '.') {
1776*7c478bd9Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
1777*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1778*7c478bd9Sstevel@tonic-gate 		}
1779*7c478bd9Sstevel@tonic-gate 	}
1780*7c478bd9Sstevel@tonic-gate 
1781*7c478bd9Sstevel@tonic-gate 	zone->zone_name = kname;
1782*7c478bd9Sstevel@tonic-gate 	return (0);
1783*7c478bd9Sstevel@tonic-gate }
1784*7c478bd9Sstevel@tonic-gate 
1785*7c478bd9Sstevel@tonic-gate /*
1786*7c478bd9Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
1787*7c478bd9Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
1788*7c478bd9Sstevel@tonic-gate  */
1789*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1790*7c478bd9Sstevel@tonic-gate kthread_t *
1791*7c478bd9Sstevel@tonic-gate zthread_create(
1792*7c478bd9Sstevel@tonic-gate     caddr_t stk,
1793*7c478bd9Sstevel@tonic-gate     size_t stksize,
1794*7c478bd9Sstevel@tonic-gate     void (*proc)(),
1795*7c478bd9Sstevel@tonic-gate     void *arg,
1796*7c478bd9Sstevel@tonic-gate     size_t len,
1797*7c478bd9Sstevel@tonic-gate     pri_t pri)
1798*7c478bd9Sstevel@tonic-gate {
1799*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
1800*7c478bd9Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
1801*7c478bd9Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
1802*7c478bd9Sstevel@tonic-gate 
1803*7c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
1804*7c478bd9Sstevel@tonic-gate 
1805*7c478bd9Sstevel@tonic-gate 	/*
1806*7c478bd9Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
1807*7c478bd9Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
1808*7c478bd9Sstevel@tonic-gate 	 * in zthread_exit().
1809*7c478bd9Sstevel@tonic-gate 	 */
1810*7c478bd9Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
1811*7c478bd9Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
1812*7c478bd9Sstevel@tonic-gate 	/*
1813*7c478bd9Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
1814*7c478bd9Sstevel@tonic-gate 	 * things up.
1815*7c478bd9Sstevel@tonic-gate 	 */
1816*7c478bd9Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
1817*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
1818*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
1819*7c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
1820*7c478bd9Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
1821*7c478bd9Sstevel@tonic-gate 	} else {
1822*7c478bd9Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
1823*7c478bd9Sstevel@tonic-gate 
1824*7c478bd9Sstevel@tonic-gate 		t->t_forw = tx;
1825*7c478bd9Sstevel@tonic-gate 		t->t_back = tx->t_back;
1826*7c478bd9Sstevel@tonic-gate 		tx->t_back->t_forw = t;
1827*7c478bd9Sstevel@tonic-gate 		tx->t_back = t;
1828*7c478bd9Sstevel@tonic-gate 	}
1829*7c478bd9Sstevel@tonic-gate 	zone->zone_kthreads = t;
1830*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
1831*7c478bd9Sstevel@tonic-gate 
1832*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
1833*7c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
1834*7c478bd9Sstevel@tonic-gate 	project_rele(t->t_proj);
1835*7c478bd9Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
1836*7c478bd9Sstevel@tonic-gate 
1837*7c478bd9Sstevel@tonic-gate 	/*
1838*7c478bd9Sstevel@tonic-gate 	 * Setup complete, let it run.
1839*7c478bd9Sstevel@tonic-gate 	 */
1840*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
1841*7c478bd9Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
1842*7c478bd9Sstevel@tonic-gate 	setrun_locked(t);
1843*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
1844*7c478bd9Sstevel@tonic-gate 
1845*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
1846*7c478bd9Sstevel@tonic-gate 
1847*7c478bd9Sstevel@tonic-gate 	return (t);
1848*7c478bd9Sstevel@tonic-gate }
1849*7c478bd9Sstevel@tonic-gate 
1850*7c478bd9Sstevel@tonic-gate /*
1851*7c478bd9Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
1852*7c478bd9Sstevel@tonic-gate  * zthread_exit().
1853*7c478bd9Sstevel@tonic-gate  */
1854*7c478bd9Sstevel@tonic-gate void
1855*7c478bd9Sstevel@tonic-gate zthread_exit(void)
1856*7c478bd9Sstevel@tonic-gate {
1857*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1858*7c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
1859*7c478bd9Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
1860*7c478bd9Sstevel@tonic-gate 
1861*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
1862*7c478bd9Sstevel@tonic-gate 
1863*7c478bd9Sstevel@tonic-gate 	/*
1864*7c478bd9Sstevel@tonic-gate 	 * Reparent to p0
1865*7c478bd9Sstevel@tonic-gate 	 */
1866*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
1867*7c478bd9Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
1868*7c478bd9Sstevel@tonic-gate 	t->t_procp = &p0;
1869*7c478bd9Sstevel@tonic-gate 	hat_thread_exit(t);
1870*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
1871*7c478bd9Sstevel@tonic-gate 
1872*7c478bd9Sstevel@tonic-gate 	if (t->t_back == t) {
1873*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
1874*7c478bd9Sstevel@tonic-gate 		/*
1875*7c478bd9Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
1876*7c478bd9Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
1877*7c478bd9Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
1878*7c478bd9Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
1879*7c478bd9Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
1880*7c478bd9Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
1881*7c478bd9Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
1882*7c478bd9Sstevel@tonic-gate 		 *
1883*7c478bd9Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
1884*7c478bd9Sstevel@tonic-gate 		 * not create zone kernel threads.
1885*7c478bd9Sstevel@tonic-gate 		 */
1886*7c478bd9Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
1887*7c478bd9Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
1888*7c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
1889*7c478bd9Sstevel@tonic-gate 		}
1890*7c478bd9Sstevel@tonic-gate 	} else {
1891*7c478bd9Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
1892*7c478bd9Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
1893*7c478bd9Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
1894*7c478bd9Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
1895*7c478bd9Sstevel@tonic-gate 	}
1896*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
1897*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
1898*7c478bd9Sstevel@tonic-gate 	thread_exit();
1899*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1900*7c478bd9Sstevel@tonic-gate }
1901*7c478bd9Sstevel@tonic-gate 
1902*7c478bd9Sstevel@tonic-gate static void
1903*7c478bd9Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
1904*7c478bd9Sstevel@tonic-gate {
1905*7c478bd9Sstevel@tonic-gate 	vnode_t *oldvp;
1906*7c478bd9Sstevel@tonic-gate 
1907*7c478bd9Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
1908*7c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
1909*7c478bd9Sstevel@tonic-gate 
1910*7c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
1911*7c478bd9Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
1912*7c478bd9Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
1913*7c478bd9Sstevel@tonic-gate #endif
1914*7c478bd9Sstevel@tonic-gate 
1915*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
1916*7c478bd9Sstevel@tonic-gate 	oldvp = *vpp;
1917*7c478bd9Sstevel@tonic-gate 	*vpp = vp;
1918*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
1919*7c478bd9Sstevel@tonic-gate 	if (oldvp != NULL)
1920*7c478bd9Sstevel@tonic-gate 		VN_RELE(oldvp);
1921*7c478bd9Sstevel@tonic-gate }
1922*7c478bd9Sstevel@tonic-gate 
1923*7c478bd9Sstevel@tonic-gate /*
1924*7c478bd9Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
1925*7c478bd9Sstevel@tonic-gate  */
1926*7c478bd9Sstevel@tonic-gate static int
1927*7c478bd9Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
1928*7c478bd9Sstevel@tonic-gate {
1929*7c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
1930*7c478bd9Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
1931*7c478bd9Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
1932*7c478bd9Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
1933*7c478bd9Sstevel@tonic-gate 
1934*7c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
1935*7c478bd9Sstevel@tonic-gate 		const char *name;
1936*7c478bd9Sstevel@tonic-gate 		uint64_t ui64;
1937*7c478bd9Sstevel@tonic-gate 
1938*7c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
1939*7c478bd9Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
1940*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1941*7c478bd9Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
1942*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
1943*7c478bd9Sstevel@tonic-gate 			/*
1944*7c478bd9Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
1945*7c478bd9Sstevel@tonic-gate 			 * this may change in the future.
1946*7c478bd9Sstevel@tonic-gate 			 */
1947*7c478bd9Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
1948*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1949*7c478bd9Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
1950*7c478bd9Sstevel@tonic-gate 			priv_set = B_TRUE;
1951*7c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
1952*7c478bd9Sstevel@tonic-gate 			rv->rcv_value = ui64;
1953*7c478bd9Sstevel@tonic-gate 			limit_set = B_TRUE;
1954*7c478bd9Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
1955*7c478bd9Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
1956*7c478bd9Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
1957*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
1958*7c478bd9Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
1959*7c478bd9Sstevel@tonic-gate 			action_set = B_TRUE;
1960*7c478bd9Sstevel@tonic-gate 		} else {
1961*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1962*7c478bd9Sstevel@tonic-gate 		}
1963*7c478bd9Sstevel@tonic-gate 	}
1964*7c478bd9Sstevel@tonic-gate 
1965*7c478bd9Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
1966*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1967*7c478bd9Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
1968*7c478bd9Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
1969*7c478bd9Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
1970*7c478bd9Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
1971*7c478bd9Sstevel@tonic-gate 
1972*7c478bd9Sstevel@tonic-gate 	return (0);
1973*7c478bd9Sstevel@tonic-gate }
1974*7c478bd9Sstevel@tonic-gate 
1975*7c478bd9Sstevel@tonic-gate void
1976*7c478bd9Sstevel@tonic-gate zone_icode(void)
1977*7c478bd9Sstevel@tonic-gate {
1978*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1979*7c478bd9Sstevel@tonic-gate 	struct core_globals	*cg;
1980*7c478bd9Sstevel@tonic-gate 
1981*7c478bd9Sstevel@tonic-gate 	/*
1982*7c478bd9Sstevel@tonic-gate 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
1983*7c478bd9Sstevel@tonic-gate 	 * storing just the pid of init is sufficient.
1984*7c478bd9Sstevel@tonic-gate 	 */
1985*7c478bd9Sstevel@tonic-gate 	p->p_zone->zone_proc_initpid = p->p_pid;
1986*7c478bd9Sstevel@tonic-gate 
1987*7c478bd9Sstevel@tonic-gate 	/*
1988*7c478bd9Sstevel@tonic-gate 	 * Allocate user address space and stack segment
1989*7c478bd9Sstevel@tonic-gate 	 */
1990*7c478bd9Sstevel@tonic-gate 
1991*7c478bd9Sstevel@tonic-gate 	p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
1992*7c478bd9Sstevel@tonic-gate 	p->p_usrstack = (caddr_t)USRSTACK32;
1993*7c478bd9Sstevel@tonic-gate 	p->p_model = DATAMODEL_ILP32;
1994*7c478bd9Sstevel@tonic-gate 	p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
1995*7c478bd9Sstevel@tonic-gate 	p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
1996*7c478bd9Sstevel@tonic-gate 	p->p_stk_ctl = INT32_MAX;
1997*7c478bd9Sstevel@tonic-gate 
1998*7c478bd9Sstevel@tonic-gate 	p->p_as = as_alloc();
1999*7c478bd9Sstevel@tonic-gate 	p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
2000*7c478bd9Sstevel@tonic-gate 	(void) hat_setup(p->p_as->a_hat, HAT_INIT);
2001*7c478bd9Sstevel@tonic-gate 
2002*7c478bd9Sstevel@tonic-gate 	cg = zone_getspecific(core_zone_key, p->p_zone);
2003*7c478bd9Sstevel@tonic-gate 	ASSERT(cg != NULL);
2004*7c478bd9Sstevel@tonic-gate 	corectl_path_hold(cg->core_default_path);
2005*7c478bd9Sstevel@tonic-gate 	corectl_content_hold(cg->core_default_content);
2006*7c478bd9Sstevel@tonic-gate 	p->p_corefile = cg->core_default_path;
2007*7c478bd9Sstevel@tonic-gate 	p->p_content = cg->core_default_content;
2008*7c478bd9Sstevel@tonic-gate 
2009*7c478bd9Sstevel@tonic-gate 	init_mstate(curthread, LMS_SYSTEM);
2010*7c478bd9Sstevel@tonic-gate 
2011*7c478bd9Sstevel@tonic-gate 	p->p_zone->zone_boot_err = exec_init(zone_initname, 0,
2012*7c478bd9Sstevel@tonic-gate 	    p->p_zone->zone_bootargs);
2013*7c478bd9Sstevel@tonic-gate 
2014*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
2015*7c478bd9Sstevel@tonic-gate 	if (p->p_zone->zone_boot_err != 0) {
2016*7c478bd9Sstevel@tonic-gate 		/*
2017*7c478bd9Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
2018*7c478bd9Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
2019*7c478bd9Sstevel@tonic-gate 		 */
2020*7c478bd9Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
2021*7c478bd9Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_SHUTTING_DOWN);
2022*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
2023*7c478bd9Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
2024*7c478bd9Sstevel@tonic-gate 		if (proc_exit(CLD_EXITED, p->p_zone->zone_boot_err) != 0) {
2025*7c478bd9Sstevel@tonic-gate 			mutex_enter(&curproc->p_lock);
2026*7c478bd9Sstevel@tonic-gate 			ASSERT(curproc->p_flag & SEXITLWPS);
2027*7c478bd9Sstevel@tonic-gate 			lwp_exit();
2028*7c478bd9Sstevel@tonic-gate 		}
2029*7c478bd9Sstevel@tonic-gate 	} else {
2030*7c478bd9Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
2031*7c478bd9Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_RUNNING);
2032*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
2033*7c478bd9Sstevel@tonic-gate 		/* cause the process to return to userland. */
2034*7c478bd9Sstevel@tonic-gate 		lwp_rtt();
2035*7c478bd9Sstevel@tonic-gate 	}
2036*7c478bd9Sstevel@tonic-gate }
2037*7c478bd9Sstevel@tonic-gate 
2038*7c478bd9Sstevel@tonic-gate struct zsched_arg {
2039*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
2040*7c478bd9Sstevel@tonic-gate 	nvlist_t *nvlist;
2041*7c478bd9Sstevel@tonic-gate };
2042*7c478bd9Sstevel@tonic-gate 
2043*7c478bd9Sstevel@tonic-gate /*
2044*7c478bd9Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
2045*7c478bd9Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
2046*7c478bd9Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
2047*7c478bd9Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
2048*7c478bd9Sstevel@tonic-gate  *
2049*7c478bd9Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
2050*7c478bd9Sstevel@tonic-gate  */
2051*7c478bd9Sstevel@tonic-gate static void
2052*7c478bd9Sstevel@tonic-gate zsched(void *arg)
2053*7c478bd9Sstevel@tonic-gate {
2054*7c478bd9Sstevel@tonic-gate 	struct zsched_arg *za = arg;
2055*7c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
2056*7c478bd9Sstevel@tonic-gate 	proc_t *initp = proc_init;
2057*7c478bd9Sstevel@tonic-gate 	zone_t *zone = za->zone;
2058*7c478bd9Sstevel@tonic-gate 	cred_t *cr, *oldcred;
2059*7c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
2060*7c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
2061*7c478bd9Sstevel@tonic-gate 	contract_t *ct = NULL;
2062*7c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
2063*7c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
2064*7c478bd9Sstevel@tonic-gate 	kproject_t *pj;
2065*7c478bd9Sstevel@tonic-gate 
2066*7c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
2067*7c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
2068*7c478bd9Sstevel@tonic-gate 
2069*7c478bd9Sstevel@tonic-gate 	bcopy("zsched", u.u_psargs, sizeof ("zsched"));
2070*7c478bd9Sstevel@tonic-gate 	bcopy("zsched", u.u_comm, sizeof ("zsched"));
2071*7c478bd9Sstevel@tonic-gate 	u.u_argc = 0;
2072*7c478bd9Sstevel@tonic-gate 	u.u_argv = NULL;
2073*7c478bd9Sstevel@tonic-gate 	u.u_envp = NULL;
2074*7c478bd9Sstevel@tonic-gate 	closeall(P_FINFO(pp));
2075*7c478bd9Sstevel@tonic-gate 
2076*7c478bd9Sstevel@tonic-gate 	/*
2077*7c478bd9Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
2078*7c478bd9Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
2079*7c478bd9Sstevel@tonic-gate 	 * zone_proc pointer.
2080*7c478bd9Sstevel@tonic-gate 	 */
2081*7c478bd9Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
2082*7c478bd9Sstevel@tonic-gate 	zone->zone_zsched = pp;
2083*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
2084*7c478bd9Sstevel@tonic-gate 	pp->p_zone = zone;
2085*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
2086*7c478bd9Sstevel@tonic-gate 
2087*7c478bd9Sstevel@tonic-gate 	/*
2088*7c478bd9Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
2089*7c478bd9Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
2090*7c478bd9Sstevel@tonic-gate 	 */
2091*7c478bd9Sstevel@tonic-gate 	sess_create();
2092*7c478bd9Sstevel@tonic-gate 
2093*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
2094*7c478bd9Sstevel@tonic-gate 	proc_detach(pp);
2095*7c478bd9Sstevel@tonic-gate 	pp->p_ppid = 1;
2096*7c478bd9Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
2097*7c478bd9Sstevel@tonic-gate 	pp->p_ancpid = 1;
2098*7c478bd9Sstevel@tonic-gate 	pp->p_parent = initp;
2099*7c478bd9Sstevel@tonic-gate 	pp->p_psibling = NULL;
2100*7c478bd9Sstevel@tonic-gate 	if (initp->p_child)
2101*7c478bd9Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
2102*7c478bd9Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
2103*7c478bd9Sstevel@tonic-gate 	initp->p_child = pp;
2104*7c478bd9Sstevel@tonic-gate 
2105*7c478bd9Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
2106*7c478bd9Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
2107*7c478bd9Sstevel@tonic-gate 	/*
2108*7c478bd9Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
2109*7c478bd9Sstevel@tonic-gate 	 * about the caller's ruid.
2110*7c478bd9Sstevel@tonic-gate 	 */
2111*7c478bd9Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
2112*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
2113*7c478bd9Sstevel@tonic-gate 
2114*7c478bd9Sstevel@tonic-gate 	/*
2115*7c478bd9Sstevel@tonic-gate 	 * getting out of global zone, so decrement lwp counts
2116*7c478bd9Sstevel@tonic-gate 	 */
2117*7c478bd9Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
2118*7c478bd9Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
2119*7c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
2120*7c478bd9Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
2121*7c478bd9Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
2122*7c478bd9Sstevel@tonic-gate 
2123*7c478bd9Sstevel@tonic-gate 	/*
2124*7c478bd9Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
2125*7c478bd9Sstevel@tonic-gate 	 *
2126*7c478bd9Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
2127*7c478bd9Sstevel@tonic-gate 	 * this process.
2128*7c478bd9Sstevel@tonic-gate 	 *
2129*7c478bd9Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
2130*7c478bd9Sstevel@tonic-gate 	 */
2131*7c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
2132*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2133*7c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
2134*7c478bd9Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
2135*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2136*7c478bd9Sstevel@tonic-gate 	task_rele(oldtk);
2137*7c478bd9Sstevel@tonic-gate 
2138*7c478bd9Sstevel@tonic-gate 	/*
2139*7c478bd9Sstevel@tonic-gate 	 * add lwp counts to zsched's zone, and increment project's task count
2140*7c478bd9Sstevel@tonic-gate 	 * due to the task created in the above tasksys_settaskid
2141*7c478bd9Sstevel@tonic-gate 	 */
2142*7c478bd9Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
2143*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
2144*7c478bd9Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
2145*7c478bd9Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
2146*7c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
2147*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
2148*7c478bd9Sstevel@tonic-gate 
2149*7c478bd9Sstevel@tonic-gate 	/*
2150*7c478bd9Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
2151*7c478bd9Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
2152*7c478bd9Sstevel@tonic-gate 	 */
2153*7c478bd9Sstevel@tonic-gate 	cr = zone->zone_kcred;
2154*7c478bd9Sstevel@tonic-gate 	crhold(cr);
2155*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
2156*7c478bd9Sstevel@tonic-gate 	oldcred = pp->p_cred;
2157*7c478bd9Sstevel@tonic-gate 	pp->p_cred = cr;
2158*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
2159*7c478bd9Sstevel@tonic-gate 	crfree(oldcred);
2160*7c478bd9Sstevel@tonic-gate 
2161*7c478bd9Sstevel@tonic-gate 	/*
2162*7c478bd9Sstevel@tonic-gate 	 * Hold credentials again (for thread)
2163*7c478bd9Sstevel@tonic-gate 	 */
2164*7c478bd9Sstevel@tonic-gate 	crhold(cr);
2165*7c478bd9Sstevel@tonic-gate 
2166*7c478bd9Sstevel@tonic-gate 	/*
2167*7c478bd9Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
2168*7c478bd9Sstevel@tonic-gate 	 */
2169*7c478bd9Sstevel@tonic-gate 	crset(pp, cr);
2170*7c478bd9Sstevel@tonic-gate 
2171*7c478bd9Sstevel@tonic-gate 	/*
2172*7c478bd9Sstevel@tonic-gate 	 * Chroot
2173*7c478bd9Sstevel@tonic-gate 	 */
2174*7c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
2175*7c478bd9Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
2176*7c478bd9Sstevel@tonic-gate 
2177*7c478bd9Sstevel@tonic-gate 	/*
2178*7c478bd9Sstevel@tonic-gate 	 * Initialize zone's rctl set.
2179*7c478bd9Sstevel@tonic-gate 	 */
2180*7c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
2181*7c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
2182*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
2183*7c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
2184*7c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
2185*7c478bd9Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
2186*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
2187*7c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
2188*7c478bd9Sstevel@tonic-gate 
2189*7c478bd9Sstevel@tonic-gate 	/*
2190*7c478bd9Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
2191*7c478bd9Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
2192*7c478bd9Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
2193*7c478bd9Sstevel@tonic-gate 	 * removed.
2194*7c478bd9Sstevel@tonic-gate 	 */
2195*7c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2196*7c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
2197*7c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
2198*7c478bd9Sstevel@tonic-gate 		char *name;
2199*7c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
2200*7c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
2201*7c478bd9Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
2202*7c478bd9Sstevel@tonic-gate 
2203*7c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
2204*7c478bd9Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
2205*7c478bd9Sstevel@tonic-gate 		ASSERT(hndl != -1);
2206*7c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
2207*7c478bd9Sstevel@tonic-gate 		ASSERT(rde != NULL);
2208*7c478bd9Sstevel@tonic-gate 
2209*7c478bd9Sstevel@tonic-gate 		for (; /* ever */; ) {
2210*7c478bd9Sstevel@tonic-gate 			rctl_val_t oval;
2211*7c478bd9Sstevel@tonic-gate 
2212*7c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
2213*7c478bd9Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
2214*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
2215*7c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
2216*7c478bd9Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
2217*7c478bd9Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
2218*7c478bd9Sstevel@tonic-gate 				break;
2219*7c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
2220*7c478bd9Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
2221*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
2222*7c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
2223*7c478bd9Sstevel@tonic-gate 		}
2224*7c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
2225*7c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
2226*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
2227*7c478bd9Sstevel@tonic-gate 			rctl_val_t *nvalp;
2228*7c478bd9Sstevel@tonic-gate 
2229*7c478bd9Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
2230*7c478bd9Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
2231*7c478bd9Sstevel@tonic-gate 			ASSERT(error == 0);
2232*7c478bd9Sstevel@tonic-gate 			/*
2233*7c478bd9Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
2234*7c478bd9Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
2235*7c478bd9Sstevel@tonic-gate 			 */
2236*7c478bd9Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
2237*7c478bd9Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
2238*7c478bd9Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
2239*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
2240*7c478bd9Sstevel@tonic-gate 		}
2241*7c478bd9Sstevel@tonic-gate 	}
2242*7c478bd9Sstevel@tonic-gate 	/*
2243*7c478bd9Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
2244*7c478bd9Sstevel@tonic-gate 	 *
2245*7c478bd9Sstevel@tonic-gate 	 * At this point we want to set the zone status to ZONE_IS_READY
2246*7c478bd9Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
2247*7c478bd9Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
2248*7c478bd9Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
2249*7c478bd9Sstevel@tonic-gate 	 */
2250*7c478bd9Sstevel@tonic-gate 	pool_lock();
2251*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2252*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
2253*7c478bd9Sstevel@tonic-gate 	zone_uniqid(zone);
2254*7c478bd9Sstevel@tonic-gate 	zone_zsd_configure(zone);
2255*7c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
2256*7c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
2257*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
2258*7c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
2259*7c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_READY);
2260*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
2261*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
2262*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2263*7c478bd9Sstevel@tonic-gate 	pool_unlock();
2264*7c478bd9Sstevel@tonic-gate 
2265*7c478bd9Sstevel@tonic-gate 	/*
2266*7c478bd9Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
2267*7c478bd9Sstevel@tonic-gate 	 * we launch init, and set the state to running.
2268*7c478bd9Sstevel@tonic-gate 	 */
2269*7c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
2270*7c478bd9Sstevel@tonic-gate 
2271*7c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
2272*7c478bd9Sstevel@tonic-gate 		id_t cid;
2273*7c478bd9Sstevel@tonic-gate 
2274*7c478bd9Sstevel@tonic-gate 		/*
2275*7c478bd9Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
2276*7c478bd9Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
2277*7c478bd9Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
2278*7c478bd9Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
2279*7c478bd9Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
2280*7c478bd9Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
2281*7c478bd9Sstevel@tonic-gate 		 * respect pool bindings.
2282*7c478bd9Sstevel@tonic-gate 		 *
2283*7c478bd9Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
2284*7c478bd9Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
2285*7c478bd9Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
2286*7c478bd9Sstevel@tonic-gate 		 * classid 'cid'.
2287*7c478bd9Sstevel@tonic-gate 		 */
2288*7c478bd9Sstevel@tonic-gate 		pool_lock();
2289*7c478bd9Sstevel@tonic-gate 		cid = pool_get_class(zone->zone_pool);
2290*7c478bd9Sstevel@tonic-gate 		if (cid == -1)
2291*7c478bd9Sstevel@tonic-gate 			cid = defaultcid;
2292*7c478bd9Sstevel@tonic-gate 
2293*7c478bd9Sstevel@tonic-gate 		/*
2294*7c478bd9Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
2295*7c478bd9Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
2296*7c478bd9Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
2297*7c478bd9Sstevel@tonic-gate 		 */
2298*7c478bd9Sstevel@tonic-gate 		if ((zone->zone_boot_err = newproc(zone_icode, NULL, cid,
2299*7c478bd9Sstevel@tonic-gate 		    minclsyspri - 1, &ct)) != 0) {
2300*7c478bd9Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
2301*7c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
2302*7c478bd9Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
2303*7c478bd9Sstevel@tonic-gate 		}
2304*7c478bd9Sstevel@tonic-gate 		pool_unlock();
2305*7c478bd9Sstevel@tonic-gate 	}
2306*7c478bd9Sstevel@tonic-gate 
2307*7c478bd9Sstevel@tonic-gate 	/*
2308*7c478bd9Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
2309*7c478bd9Sstevel@tonic-gate 	 * most of our life doing.
2310*7c478bd9Sstevel@tonic-gate 	 */
2311*7c478bd9Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
2312*7c478bd9Sstevel@tonic-gate 
2313*7c478bd9Sstevel@tonic-gate 	if (ct)
2314*7c478bd9Sstevel@tonic-gate 		/*
2315*7c478bd9Sstevel@tonic-gate 		 * At this point the process contract should be empty.
2316*7c478bd9Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
2317*7c478bd9Sstevel@tonic-gate 		 */
2318*7c478bd9Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
2319*7c478bd9Sstevel@tonic-gate 
2320*7c478bd9Sstevel@tonic-gate 	/*
2321*7c478bd9Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
2322*7c478bd9Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
2323*7c478bd9Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
2324*7c478bd9Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
2325*7c478bd9Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
2326*7c478bd9Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
2327*7c478bd9Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
2328*7c478bd9Sstevel@tonic-gate 	 */
2329*7c478bd9Sstevel@tonic-gate 	crfree(zone->zone_kcred);
2330*7c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
2331*7c478bd9Sstevel@tonic-gate 
2332*7c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
2333*7c478bd9Sstevel@tonic-gate }
2334*7c478bd9Sstevel@tonic-gate 
2335*7c478bd9Sstevel@tonic-gate /*
2336*7c478bd9Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
2337*7c478bd9Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
2338*7c478bd9Sstevel@tonic-gate  * mounts from before it is created.
2339*7c478bd9Sstevel@tonic-gate  */
2340*7c478bd9Sstevel@tonic-gate static uint_t
2341*7c478bd9Sstevel@tonic-gate zone_mount_count(const char *rootpath)
2342*7c478bd9Sstevel@tonic-gate {
2343*7c478bd9Sstevel@tonic-gate 	vfs_t *vfsp;
2344*7c478bd9Sstevel@tonic-gate 	uint_t count = 0;
2345*7c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
2346*7c478bd9Sstevel@tonic-gate 
2347*7c478bd9Sstevel@tonic-gate 	/*
2348*7c478bd9Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
2349*7c478bd9Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
2350*7c478bd9Sstevel@tonic-gate 	 * zone_find_by_path().
2351*7c478bd9Sstevel@tonic-gate 	 */
2352*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
2353*7c478bd9Sstevel@tonic-gate 	/*
2354*7c478bd9Sstevel@tonic-gate 	 * The rootpath must end with a '/'
2355*7c478bd9Sstevel@tonic-gate 	 */
2356*7c478bd9Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
2357*7c478bd9Sstevel@tonic-gate 
2358*7c478bd9Sstevel@tonic-gate 	/*
2359*7c478bd9Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
2360*7c478bd9Sstevel@tonic-gate 	 * happens to be a mount point.
2361*7c478bd9Sstevel@tonic-gate 	 */
2362*7c478bd9Sstevel@tonic-gate 	vfs_list_read_lock();
2363*7c478bd9Sstevel@tonic-gate 	vfsp = rootvfs;
2364*7c478bd9Sstevel@tonic-gate 	do {
2365*7c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
2366*7c478bd9Sstevel@tonic-gate 		    rootpathlen) == 0)
2367*7c478bd9Sstevel@tonic-gate 			count++;
2368*7c478bd9Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
2369*7c478bd9Sstevel@tonic-gate 	} while (vfsp != rootvfs);
2370*7c478bd9Sstevel@tonic-gate 	vfs_list_unlock();
2371*7c478bd9Sstevel@tonic-gate 	return (count);
2372*7c478bd9Sstevel@tonic-gate }
2373*7c478bd9Sstevel@tonic-gate 
2374*7c478bd9Sstevel@tonic-gate /*
2375*7c478bd9Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
2376*7c478bd9Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
2377*7c478bd9Sstevel@tonic-gate  */
2378*7c478bd9Sstevel@tonic-gate static boolean_t
2379*7c478bd9Sstevel@tonic-gate zone_is_nested(const char *rootpath)
2380*7c478bd9Sstevel@tonic-gate {
2381*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
2382*7c478bd9Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
2383*7c478bd9Sstevel@tonic-gate 	size_t len;
2384*7c478bd9Sstevel@tonic-gate 
2385*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
2386*7c478bd9Sstevel@tonic-gate 
2387*7c478bd9Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
2388*7c478bd9Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
2389*7c478bd9Sstevel@tonic-gate 		if (zone == global_zone)
2390*7c478bd9Sstevel@tonic-gate 			continue;
2391*7c478bd9Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
2392*7c478bd9Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
2393*7c478bd9Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
2394*7c478bd9Sstevel@tonic-gate 			return (B_TRUE);
2395*7c478bd9Sstevel@tonic-gate 	}
2396*7c478bd9Sstevel@tonic-gate 	return (B_FALSE);
2397*7c478bd9Sstevel@tonic-gate }
2398*7c478bd9Sstevel@tonic-gate 
2399*7c478bd9Sstevel@tonic-gate static int
2400*7c478bd9Sstevel@tonic-gate zone_set_privset(zone_t *zone, const priv_set_t *zone_privs)
2401*7c478bd9Sstevel@tonic-gate {
2402*7c478bd9Sstevel@tonic-gate 	priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
2403*7c478bd9Sstevel@tonic-gate 
2404*7c478bd9Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
2405*7c478bd9Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
2406*7c478bd9Sstevel@tonic-gate 		return (EFAULT);
2407*7c478bd9Sstevel@tonic-gate 	}
2408*7c478bd9Sstevel@tonic-gate 
2409*7c478bd9Sstevel@tonic-gate 	zone->zone_privset = privs;
2410*7c478bd9Sstevel@tonic-gate 	return (0);
2411*7c478bd9Sstevel@tonic-gate }
2412*7c478bd9Sstevel@tonic-gate 
2413*7c478bd9Sstevel@tonic-gate /*
2414*7c478bd9Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
2415*7c478bd9Sstevel@tonic-gate  * a list of the following structures:
2416*7c478bd9Sstevel@tonic-gate  *
2417*7c478bd9Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
2418*7c478bd9Sstevel@tonic-gate  *
2419*7c478bd9Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
2420*7c478bd9Sstevel@tonic-gate  *
2421*7c478bd9Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
2422*7c478bd9Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
2423*7c478bd9Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
2424*7c478bd9Sstevel@tonic-gate  */
2425*7c478bd9Sstevel@tonic-gate static int
2426*7c478bd9Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
2427*7c478bd9Sstevel@tonic-gate {
2428*7c478bd9Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
2429*7c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
2430*7c478bd9Sstevel@tonic-gate 	char *kbuf;
2431*7c478bd9Sstevel@tonic-gate 	int error;
2432*7c478bd9Sstevel@tonic-gate 	rctl_val_t rv;
2433*7c478bd9Sstevel@tonic-gate 
2434*7c478bd9Sstevel@tonic-gate 	*nvlp = NULL;
2435*7c478bd9Sstevel@tonic-gate 
2436*7c478bd9Sstevel@tonic-gate 	if (buflen == 0)
2437*7c478bd9Sstevel@tonic-gate 		return (0);
2438*7c478bd9Sstevel@tonic-gate 
2439*7c478bd9Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
2440*7c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2441*7c478bd9Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
2442*7c478bd9Sstevel@tonic-gate 		error = EFAULT;
2443*7c478bd9Sstevel@tonic-gate 		goto out;
2444*7c478bd9Sstevel@tonic-gate 	}
2445*7c478bd9Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
2446*7c478bd9Sstevel@tonic-gate 		/*
2447*7c478bd9Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
2448*7c478bd9Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
2449*7c478bd9Sstevel@tonic-gate 		 */
2450*7c478bd9Sstevel@tonic-gate 		nvl = NULL;
2451*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
2452*7c478bd9Sstevel@tonic-gate 		goto out;
2453*7c478bd9Sstevel@tonic-gate 	}
2454*7c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
2455*7c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
2456*7c478bd9Sstevel@tonic-gate 		rctl_hndl_t hndl;
2457*7c478bd9Sstevel@tonic-gate 		nvlist_t **nvlarray;
2458*7c478bd9Sstevel@tonic-gate 		uint_t i, nelem;
2459*7c478bd9Sstevel@tonic-gate 		char *name;
2460*7c478bd9Sstevel@tonic-gate 
2461*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
2462*7c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
2463*7c478bd9Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
2464*7c478bd9Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
2465*7c478bd9Sstevel@tonic-gate 			goto out;
2466*7c478bd9Sstevel@tonic-gate 		}
2467*7c478bd9Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
2468*7c478bd9Sstevel@tonic-gate 			goto out;
2469*7c478bd9Sstevel@tonic-gate 		}
2470*7c478bd9Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
2471*7c478bd9Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
2472*7c478bd9Sstevel@tonic-gate 		ASSERT(error == 0);
2473*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
2474*7c478bd9Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
2475*7c478bd9Sstevel@tonic-gate 				goto out;
2476*7c478bd9Sstevel@tonic-gate 		}
2477*7c478bd9Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
2478*7c478bd9Sstevel@tonic-gate 			error = EINVAL;
2479*7c478bd9Sstevel@tonic-gate 			goto out;
2480*7c478bd9Sstevel@tonic-gate 		}
2481*7c478bd9Sstevel@tonic-gate 	}
2482*7c478bd9Sstevel@tonic-gate 	error = 0;
2483*7c478bd9Sstevel@tonic-gate 	*nvlp = nvl;
2484*7c478bd9Sstevel@tonic-gate out:
2485*7c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
2486*7c478bd9Sstevel@tonic-gate 	if (error && nvl != NULL)
2487*7c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
2488*7c478bd9Sstevel@tonic-gate 	return (error);
2489*7c478bd9Sstevel@tonic-gate }
2490*7c478bd9Sstevel@tonic-gate 
2491*7c478bd9Sstevel@tonic-gate int
2492*7c478bd9Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
2493*7c478bd9Sstevel@tonic-gate 	if (er_out != NULL) {
2494*7c478bd9Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
2495*7c478bd9Sstevel@tonic-gate 			return (set_errno(EFAULT));
2496*7c478bd9Sstevel@tonic-gate 		}
2497*7c478bd9Sstevel@tonic-gate 	}
2498*7c478bd9Sstevel@tonic-gate 	return (set_errno(er_error));
2499*7c478bd9Sstevel@tonic-gate }
2500*7c478bd9Sstevel@tonic-gate 
2501*7c478bd9Sstevel@tonic-gate /*
2502*7c478bd9Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
2503*7c478bd9Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
2504*7c478bd9Sstevel@tonic-gate  * and initialized with the zone-wide rctls described in 'rctlbuf'.
2505*7c478bd9Sstevel@tonic-gate  *
2506*7c478bd9Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
2507*7c478bd9Sstevel@tonic-gate  * error information.
2508*7c478bd9Sstevel@tonic-gate  */
2509*7c478bd9Sstevel@tonic-gate static zoneid_t
2510*7c478bd9Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
2511*7c478bd9Sstevel@tonic-gate     const priv_set_t *zone_privs, caddr_t rctlbuf, size_t rctlbufsz,
2512*7c478bd9Sstevel@tonic-gate     int *extended_error)
2513*7c478bd9Sstevel@tonic-gate {
2514*7c478bd9Sstevel@tonic-gate 	struct zsched_arg zarg;
2515*7c478bd9Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
2516*7c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
2517*7c478bd9Sstevel@tonic-gate 	zone_t *zone, *ztmp;
2518*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
2519*7c478bd9Sstevel@tonic-gate 	int error;
2520*7c478bd9Sstevel@tonic-gate 	int error2 = 0;
2521*7c478bd9Sstevel@tonic-gate 	char *str;
2522*7c478bd9Sstevel@tonic-gate 	cred_t *zkcr;
2523*7c478bd9Sstevel@tonic-gate 
2524*7c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
2525*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
2526*7c478bd9Sstevel@tonic-gate 
2527*7c478bd9Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
2528*7c478bd9Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
2529*7c478bd9Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
2530*7c478bd9Sstevel@tonic-gate 			extended_error));
2531*7c478bd9Sstevel@tonic-gate 
2532*7c478bd9Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
2533*7c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
2534*7c478bd9Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
2535*7c478bd9Sstevel@tonic-gate 	zone->zone_pool = pool_default;
2536*7c478bd9Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
2537*7c478bd9Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
2538*7c478bd9Sstevel@tonic-gate 	zone->zone_ncpus = 0;
2539*7c478bd9Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
2540*7c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
2541*7c478bd9Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
2542*7c478bd9Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
2543*7c478bd9Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
2544*7c478bd9Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
2545*7c478bd9Sstevel@tonic-gate 
2546*7c478bd9Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
2547*7c478bd9Sstevel@tonic-gate 		zone_free(zone);
2548*7c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
2549*7c478bd9Sstevel@tonic-gate 	}
2550*7c478bd9Sstevel@tonic-gate 
2551*7c478bd9Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
2552*7c478bd9Sstevel@tonic-gate 		zone_free(zone);
2553*7c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
2554*7c478bd9Sstevel@tonic-gate 	}
2555*7c478bd9Sstevel@tonic-gate 	if ((error = zone_set_privset(zone, zone_privs)) != 0) {
2556*7c478bd9Sstevel@tonic-gate 		zone_free(zone);
2557*7c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
2558*7c478bd9Sstevel@tonic-gate 	}
2559*7c478bd9Sstevel@tonic-gate 
2560*7c478bd9Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
2561*7c478bd9Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
2562*7c478bd9Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
2563*7c478bd9Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
2564*7c478bd9Sstevel@tonic-gate 
2565*7c478bd9Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
2566*7c478bd9Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
2567*7c478bd9Sstevel@tonic-gate 	zone->zone_shares = 1;
2568*7c478bd9Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
2569*7c478bd9Sstevel@tonic-gate 
2570*7c478bd9Sstevel@tonic-gate 	/*
2571*7c478bd9Sstevel@tonic-gate 	 * Zsched initializes the rctls.
2572*7c478bd9Sstevel@tonic-gate 	 */
2573*7c478bd9Sstevel@tonic-gate 	zone->zone_rctls = NULL;
2574*7c478bd9Sstevel@tonic-gate 
2575*7c478bd9Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
2576*7c478bd9Sstevel@tonic-gate 		zone_free(zone);
2577*7c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
2578*7c478bd9Sstevel@tonic-gate 	}
2579*7c478bd9Sstevel@tonic-gate 
2580*7c478bd9Sstevel@tonic-gate 	/*
2581*7c478bd9Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
2582*7c478bd9Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
2583*7c478bd9Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
2584*7c478bd9Sstevel@tonic-gate 	 */
2585*7c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
2586*7c478bd9Sstevel@tonic-gate 		zone_free(zone);
2587*7c478bd9Sstevel@tonic-gate 		if (rctls)
2588*7c478bd9Sstevel@tonic-gate 			nvlist_free(rctls);
2589*7c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
2590*7c478bd9Sstevel@tonic-gate 	}
2591*7c478bd9Sstevel@tonic-gate 
2592*7c478bd9Sstevel@tonic-gate 	if (block_mounts() == 0) {
2593*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
2594*7c478bd9Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
2595*7c478bd9Sstevel@tonic-gate 			continuelwps(pp);
2596*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
2597*7c478bd9Sstevel@tonic-gate 		zone_free(zone);
2598*7c478bd9Sstevel@tonic-gate 		if (rctls)
2599*7c478bd9Sstevel@tonic-gate 			nvlist_free(rctls);
2600*7c478bd9Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
2601*7c478bd9Sstevel@tonic-gate 	}
2602*7c478bd9Sstevel@tonic-gate 
2603*7c478bd9Sstevel@tonic-gate 	/*
2604*7c478bd9Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
2605*7c478bd9Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
2606*7c478bd9Sstevel@tonic-gate 	 * zone_free directly.
2607*7c478bd9Sstevel@tonic-gate 	 */
2608*7c478bd9Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
2609*7c478bd9Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
2610*7c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
2611*7c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
2612*7c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
2613*7c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
2614*7c478bd9Sstevel@tonic-gate 
2615*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
2616*7c478bd9Sstevel@tonic-gate 	/*
2617*7c478bd9Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
2618*7c478bd9Sstevel@tonic-gate 	 */
2619*7c478bd9Sstevel@tonic-gate 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL) {
2620*7c478bd9Sstevel@tonic-gate 		zone_status_t status;
2621*7c478bd9Sstevel@tonic-gate 
2622*7c478bd9Sstevel@tonic-gate 		status = zone_status_get(ztmp);
2623*7c478bd9Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
2624*7c478bd9Sstevel@tonic-gate 			error = EEXIST;
2625*7c478bd9Sstevel@tonic-gate 		else
2626*7c478bd9Sstevel@tonic-gate 			error = EBUSY;
2627*7c478bd9Sstevel@tonic-gate 		goto errout;
2628*7c478bd9Sstevel@tonic-gate 	}
2629*7c478bd9Sstevel@tonic-gate 
2630*7c478bd9Sstevel@tonic-gate 	/*
2631*7c478bd9Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
2632*7c478bd9Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
2633*7c478bd9Sstevel@tonic-gate 	 */
2634*7c478bd9Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
2635*7c478bd9Sstevel@tonic-gate 		error = EBUSY;
2636*7c478bd9Sstevel@tonic-gate 		goto errout;
2637*7c478bd9Sstevel@tonic-gate 	}
2638*7c478bd9Sstevel@tonic-gate 
2639*7c478bd9Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
2640*7c478bd9Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
2641*7c478bd9Sstevel@tonic-gate 		error = ENOMEM;
2642*7c478bd9Sstevel@tonic-gate 		goto errout;
2643*7c478bd9Sstevel@tonic-gate 	}
2644*7c478bd9Sstevel@tonic-gate 
2645*7c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
2646*7c478bd9Sstevel@tonic-gate 		error = EBUSY;
2647*7c478bd9Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
2648*7c478bd9Sstevel@tonic-gate 		goto errout;
2649*7c478bd9Sstevel@tonic-gate 	}
2650*7c478bd9Sstevel@tonic-gate 
2651*7c478bd9Sstevel@tonic-gate 	/*
2652*7c478bd9Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
2653*7c478bd9Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
2654*7c478bd9Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
2655*7c478bd9Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
2656*7c478bd9Sstevel@tonic-gate 	 * same zone.
2657*7c478bd9Sstevel@tonic-gate 	 */
2658*7c478bd9Sstevel@tonic-gate 	zonecount++;
2659*7c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
2660*7c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
2661*7c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
2662*7c478bd9Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
2663*7c478bd9Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
2664*7c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
2665*7c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
2666*7c478bd9Sstevel@tonic-gate 	/*
2667*7c478bd9Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
2668*7c478bd9Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
2669*7c478bd9Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
2670*7c478bd9Sstevel@tonic-gate 	 * newproc() is successful.
2671*7c478bd9Sstevel@tonic-gate 	 */
2672*7c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
2673*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
2674*7c478bd9Sstevel@tonic-gate 
2675*7c478bd9Sstevel@tonic-gate 	zarg.zone = zone;
2676*7c478bd9Sstevel@tonic-gate 	zarg.nvlist = rctls;
2677*7c478bd9Sstevel@tonic-gate 	/*
2678*7c478bd9Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
2679*7c478bd9Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
2680*7c478bd9Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
2681*7c478bd9Sstevel@tonic-gate 	 * makes much of a difference, though.
2682*7c478bd9Sstevel@tonic-gate 	 */
2683*7c478bd9Sstevel@tonic-gate 	if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) {
2684*7c478bd9Sstevel@tonic-gate 		/*
2685*7c478bd9Sstevel@tonic-gate 		 * We need to undo all globally visible state.
2686*7c478bd9Sstevel@tonic-gate 		 */
2687*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
2688*7c478bd9Sstevel@tonic-gate 		list_remove(&zone_active, zone);
2689*7c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
2690*7c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
2691*7c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
2692*7c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
2693*7c478bd9Sstevel@tonic-gate 		ASSERT(zonecount > 1);
2694*7c478bd9Sstevel@tonic-gate 		zonecount--;
2695*7c478bd9Sstevel@tonic-gate 		goto errout;
2696*7c478bd9Sstevel@tonic-gate 	}
2697*7c478bd9Sstevel@tonic-gate 
2698*7c478bd9Sstevel@tonic-gate 	/*
2699*7c478bd9Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
2700*7c478bd9Sstevel@tonic-gate 	 */
2701*7c478bd9Sstevel@tonic-gate 
2702*7c478bd9Sstevel@tonic-gate 	/*
2703*7c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
2704*7c478bd9Sstevel@tonic-gate 	 */
2705*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
2706*7c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
2707*7c478bd9Sstevel@tonic-gate 		continuelwps(pp);
2708*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
2709*7c478bd9Sstevel@tonic-gate 
2710*7c478bd9Sstevel@tonic-gate 	/*
2711*7c478bd9Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
2712*7c478bd9Sstevel@tonic-gate 	 */
2713*7c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
2714*7c478bd9Sstevel@tonic-gate 	/*
2715*7c478bd9Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
2716*7c478bd9Sstevel@tonic-gate 	 */
2717*7c478bd9Sstevel@tonic-gate 	resume_mounts();
2718*7c478bd9Sstevel@tonic-gate 	if (rctls)
2719*7c478bd9Sstevel@tonic-gate 		nvlist_free(rctls);
2720*7c478bd9Sstevel@tonic-gate 
2721*7c478bd9Sstevel@tonic-gate 	return (zoneid);
2722*7c478bd9Sstevel@tonic-gate 
2723*7c478bd9Sstevel@tonic-gate errout:
2724*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
2725*7c478bd9Sstevel@tonic-gate 	/*
2726*7c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
2727*7c478bd9Sstevel@tonic-gate 	 */
2728*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
2729*7c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
2730*7c478bd9Sstevel@tonic-gate 		continuelwps(pp);
2731*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
2732*7c478bd9Sstevel@tonic-gate 
2733*7c478bd9Sstevel@tonic-gate 	resume_mounts();
2734*7c478bd9Sstevel@tonic-gate 	if (rctls)
2735*7c478bd9Sstevel@tonic-gate 		nvlist_free(rctls);
2736*7c478bd9Sstevel@tonic-gate 	/*
2737*7c478bd9Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
2738*7c478bd9Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
2739*7c478bd9Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
2740*7c478bd9Sstevel@tonic-gate 	 */
2741*7c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 1);	/* for zone_kcred */
2742*7c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
2743*7c478bd9Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
2744*7c478bd9Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
2745*7c478bd9Sstevel@tonic-gate 	zone->zone_kcred = NULL;
2746*7c478bd9Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
2747*7c478bd9Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
2748*7c478bd9Sstevel@tonic-gate }
2749*7c478bd9Sstevel@tonic-gate 
2750*7c478bd9Sstevel@tonic-gate /*
2751*7c478bd9Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
2752*7c478bd9Sstevel@tonic-gate  * the heavy lifting.
2753*7c478bd9Sstevel@tonic-gate  */
2754*7c478bd9Sstevel@tonic-gate static int
2755*7c478bd9Sstevel@tonic-gate zone_boot(zoneid_t zoneid, const char *bootargs)
2756*7c478bd9Sstevel@tonic-gate {
2757*7c478bd9Sstevel@tonic-gate 	int err;
2758*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
2759*7c478bd9Sstevel@tonic-gate 
2760*7c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
2761*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
2762*7c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
2763*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2764*7c478bd9Sstevel@tonic-gate 
2765*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
2766*7c478bd9Sstevel@tonic-gate 	/*
2767*7c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
2768*7c478bd9Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
2769*7c478bd9Sstevel@tonic-gate 	 */
2770*7c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
2771*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
2772*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2773*7c478bd9Sstevel@tonic-gate 	}
2774*7c478bd9Sstevel@tonic-gate 
2775*7c478bd9Sstevel@tonic-gate 	if ((err = zone_set_bootargs(zone, bootargs)) != 0) {
2776*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
2777*7c478bd9Sstevel@tonic-gate 		return (set_errno(err));
2778*7c478bd9Sstevel@tonic-gate 	}
2779*7c478bd9Sstevel@tonic-gate 
2780*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
2781*7c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
2782*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
2783*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
2784*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2785*7c478bd9Sstevel@tonic-gate 	}
2786*7c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
2787*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
2788*7c478bd9Sstevel@tonic-gate 
2789*7c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
2790*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
2791*7c478bd9Sstevel@tonic-gate 
2792*7c478bd9Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
2793*7c478bd9Sstevel@tonic-gate 		zone_rele(zone);
2794*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
2795*7c478bd9Sstevel@tonic-gate 	}
2796*7c478bd9Sstevel@tonic-gate 
2797*7c478bd9Sstevel@tonic-gate 	/*
2798*7c478bd9Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
2799*7c478bd9Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
2800*7c478bd9Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
2801*7c478bd9Sstevel@tonic-gate 	 */
2802*7c478bd9Sstevel@tonic-gate 	err = zone->zone_boot_err;
2803*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
2804*7c478bd9Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
2805*7c478bd9Sstevel@tonic-gate }
2806*7c478bd9Sstevel@tonic-gate 
2807*7c478bd9Sstevel@tonic-gate /*
2808*7c478bd9Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
2809*7c478bd9Sstevel@tonic-gate  * before returning.
2810*7c478bd9Sstevel@tonic-gate  */
2811*7c478bd9Sstevel@tonic-gate static int
2812*7c478bd9Sstevel@tonic-gate zone_empty(zone_t *zone)
2813*7c478bd9Sstevel@tonic-gate {
2814*7c478bd9Sstevel@tonic-gate 	int waitstatus;
2815*7c478bd9Sstevel@tonic-gate 
2816*7c478bd9Sstevel@tonic-gate 	/*
2817*7c478bd9Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
2818*7c478bd9Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
2819*7c478bd9Sstevel@tonic-gate 	 * which can be called from the exit path.
2820*7c478bd9Sstevel@tonic-gate 	 */
2821*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
2822*7c478bd9Sstevel@tonic-gate 	while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz,
2823*7c478bd9Sstevel@tonic-gate 	    ZONE_IS_EMPTY)) == -1) {
2824*7c478bd9Sstevel@tonic-gate 		killall(zone->zone_id);
2825*7c478bd9Sstevel@tonic-gate 	}
2826*7c478bd9Sstevel@tonic-gate 	/*
2827*7c478bd9Sstevel@tonic-gate 	 * return EINTR if we were signaled
2828*7c478bd9Sstevel@tonic-gate 	 */
2829*7c478bd9Sstevel@tonic-gate 	if (waitstatus == 0)
2830*7c478bd9Sstevel@tonic-gate 		return (EINTR);
2831*7c478bd9Sstevel@tonic-gate 	return (0);
2832*7c478bd9Sstevel@tonic-gate }
2833*7c478bd9Sstevel@tonic-gate 
2834*7c478bd9Sstevel@tonic-gate /*
2835*7c478bd9Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
2836*7c478bd9Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
2837*7c478bd9Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
2838*7c478bd9Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
2839*7c478bd9Sstevel@tonic-gate  *
2840*7c478bd9Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
2841*7c478bd9Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
2842*7c478bd9Sstevel@tonic-gate  */
2843*7c478bd9Sstevel@tonic-gate static int
2844*7c478bd9Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
2845*7c478bd9Sstevel@tonic-gate {
2846*7c478bd9Sstevel@tonic-gate 	int error;
2847*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
2848*7c478bd9Sstevel@tonic-gate 	zone_status_t status;
2849*7c478bd9Sstevel@tonic-gate 
2850*7c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
2851*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
2852*7c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
2853*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2854*7c478bd9Sstevel@tonic-gate 
2855*7c478bd9Sstevel@tonic-gate 	/*
2856*7c478bd9Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
2857*7c478bd9Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
2858*7c478bd9Sstevel@tonic-gate 	 *
2859*7c478bd9Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
2860*7c478bd9Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
2861*7c478bd9Sstevel@tonic-gate 	 */
2862*7c478bd9Sstevel@tonic-gate 	if (block_mounts() == 0)
2863*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
2864*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
2865*7c478bd9Sstevel@tonic-gate 	/*
2866*7c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
2867*7c478bd9Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
2868*7c478bd9Sstevel@tonic-gate 	 */
2869*7c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
2870*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
2871*7c478bd9Sstevel@tonic-gate 		resume_mounts();
2872*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2873*7c478bd9Sstevel@tonic-gate 	}
2874*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
2875*7c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
2876*7c478bd9Sstevel@tonic-gate 	/*
2877*7c478bd9Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
2878*7c478bd9Sstevel@tonic-gate 	 */
2879*7c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
2880*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
2881*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
2882*7c478bd9Sstevel@tonic-gate 		resume_mounts();
2883*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
2884*7c478bd9Sstevel@tonic-gate 	}
2885*7c478bd9Sstevel@tonic-gate 	/*
2886*7c478bd9Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
2887*7c478bd9Sstevel@tonic-gate 	 * return success.
2888*7c478bd9Sstevel@tonic-gate 	 */
2889*7c478bd9Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
2890*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
2891*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
2892*7c478bd9Sstevel@tonic-gate 		resume_mounts();
2893*7c478bd9Sstevel@tonic-gate 		return (0);
2894*7c478bd9Sstevel@tonic-gate 	}
2895*7c478bd9Sstevel@tonic-gate 	/*
2896*7c478bd9Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
2897*7c478bd9Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
2898*7c478bd9Sstevel@tonic-gate 	 * drain.
2899*7c478bd9Sstevel@tonic-gate 	 */
2900*7c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
2901*7c478bd9Sstevel@tonic-gate 		uint_t ntasks;
2902*7c478bd9Sstevel@tonic-gate 
2903*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
2904*7c478bd9Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
2905*7c478bd9Sstevel@tonic-gate 			/*
2906*7c478bd9Sstevel@tonic-gate 			 * There's still stuff running.
2907*7c478bd9Sstevel@tonic-gate 			 */
2908*7c478bd9Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
2909*7c478bd9Sstevel@tonic-gate 		}
2910*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
2911*7c478bd9Sstevel@tonic-gate 		if (ntasks == 1) {
2912*7c478bd9Sstevel@tonic-gate 			/*
2913*7c478bd9Sstevel@tonic-gate 			 * The only way to create another task is through
2914*7c478bd9Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
2915*7c478bd9Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
2916*7c478bd9Sstevel@tonic-gate 			 */
2917*7c478bd9Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
2918*7c478bd9Sstevel@tonic-gate 				/*
2919*7c478bd9Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
2920*7c478bd9Sstevel@tonic-gate 				 */
2921*7c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
2922*7c478bd9Sstevel@tonic-gate 			} else {
2923*7c478bd9Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
2924*7c478bd9Sstevel@tonic-gate 			}
2925*7c478bd9Sstevel@tonic-gate 		}
2926*7c478bd9Sstevel@tonic-gate 	}
2927*7c478bd9Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
2928*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
2929*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
2930*7c478bd9Sstevel@tonic-gate 	resume_mounts();
2931*7c478bd9Sstevel@tonic-gate 
2932*7c478bd9Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
2933*7c478bd9Sstevel@tonic-gate 		zone_rele(zone);
2934*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
2935*7c478bd9Sstevel@tonic-gate 	}
2936*7c478bd9Sstevel@tonic-gate 	/*
2937*7c478bd9Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
2938*7c478bd9Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
2939*7c478bd9Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
2940*7c478bd9Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
2941*7c478bd9Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
2942*7c478bd9Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
2943*7c478bd9Sstevel@tonic-gate 	 * cred's to drain out.
2944*7c478bd9Sstevel@tonic-gate 	 *
2945*7c478bd9Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
2946*7c478bd9Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
2947*7c478bd9Sstevel@tonic-gate 	 * without any adverse effects.
2948*7c478bd9Sstevel@tonic-gate 	 */
2949*7c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
2950*7c478bd9Sstevel@tonic-gate 		zone_rele(zone);
2951*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
2952*7c478bd9Sstevel@tonic-gate 	}
2953*7c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
2954*7c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
2955*7c478bd9Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
2956*7c478bd9Sstevel@tonic-gate 		/*
2957*7c478bd9Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
2958*7c478bd9Sstevel@tonic-gate 		 */
2959*7c478bd9Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
2960*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
2961*7c478bd9Sstevel@tonic-gate 	}
2962*7c478bd9Sstevel@tonic-gate 	pool_unlock();
2963*7c478bd9Sstevel@tonic-gate 
2964*7c478bd9Sstevel@tonic-gate 	/*
2965*7c478bd9Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
2966*7c478bd9Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
2967*7c478bd9Sstevel@tonic-gate 	 */
2968*7c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
2969*7c478bd9Sstevel@tonic-gate 
2970*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
2971*7c478bd9Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
2972*7c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
2973*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
2974*7c478bd9Sstevel@tonic-gate 
2975*7c478bd9Sstevel@tonic-gate 	/*
2976*7c478bd9Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
2977*7c478bd9Sstevel@tonic-gate 	 */
2978*7c478bd9Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
2979*7c478bd9Sstevel@tonic-gate 		zone_rele(zone);
2980*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
2981*7c478bd9Sstevel@tonic-gate 	}
2982*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
2983*7c478bd9Sstevel@tonic-gate 	return (0);
2984*7c478bd9Sstevel@tonic-gate }
2985*7c478bd9Sstevel@tonic-gate 
2986*7c478bd9Sstevel@tonic-gate /*
2987*7c478bd9Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
2988*7c478bd9Sstevel@tonic-gate  * must have already successfully callefd zone_shutdown().
2989*7c478bd9Sstevel@tonic-gate  *
2990*7c478bd9Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
2991*7c478bd9Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
2992*7c478bd9Sstevel@tonic-gate  * removed from the list of active zones.
2993*7c478bd9Sstevel@tonic-gate  */
2994*7c478bd9Sstevel@tonic-gate static int
2995*7c478bd9Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
2996*7c478bd9Sstevel@tonic-gate {
2997*7c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
2998*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
2999*7c478bd9Sstevel@tonic-gate 	zone_status_t status;
3000*7c478bd9Sstevel@tonic-gate 
3001*7c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
3002*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
3003*7c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
3004*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3005*7c478bd9Sstevel@tonic-gate 
3006*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
3007*7c478bd9Sstevel@tonic-gate 	/*
3008*7c478bd9Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
3009*7c478bd9Sstevel@tonic-gate 	 * calls to zone_destroy.
3010*7c478bd9Sstevel@tonic-gate 	 */
3011*7c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
3012*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3013*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3014*7c478bd9Sstevel@tonic-gate 	}
3015*7c478bd9Sstevel@tonic-gate 
3016*7c478bd9Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
3017*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3018*7c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
3019*7c478bd9Sstevel@tonic-gate 	}
3020*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
3021*7c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
3022*7c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
3023*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
3024*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3025*7c478bd9Sstevel@tonic-gate 		return (set_errno(EBUSY));
3026*7c478bd9Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
3027*7c478bd9Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
3028*7c478bd9Sstevel@tonic-gate 	}
3029*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
3030*7c478bd9Sstevel@tonic-gate 	zone_hold(zone);
3031*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
3032*7c478bd9Sstevel@tonic-gate 
3033*7c478bd9Sstevel@tonic-gate 	/*
3034*7c478bd9Sstevel@tonic-gate 	 * wait for zsched to exit
3035*7c478bd9Sstevel@tonic-gate 	 */
3036*7c478bd9Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
3037*7c478bd9Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
3038*7c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
3039*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
3040*7c478bd9Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
3041*7c478bd9Sstevel@tonic-gate 
3042*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
3043*7c478bd9Sstevel@tonic-gate 	for (; /* ever */; ) {
3044*7c478bd9Sstevel@tonic-gate 		boolean_t unref;
3045*7c478bd9Sstevel@tonic-gate 
3046*7c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
3047*7c478bd9Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
3048*7c478bd9Sstevel@tonic-gate 			/*
3049*7c478bd9Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
3050*7c478bd9Sstevel@tonic-gate 			 * are met, so we return success.
3051*7c478bd9Sstevel@tonic-gate 			 */
3052*7c478bd9Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
3053*7c478bd9Sstevel@tonic-gate 			return (0);
3054*7c478bd9Sstevel@tonic-gate 		}
3055*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
3056*7c478bd9Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
3057*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
3058*7c478bd9Sstevel@tonic-gate 		if (unref) {
3059*7c478bd9Sstevel@tonic-gate 			/*
3060*7c478bd9Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
3061*7c478bd9Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
3062*7c478bd9Sstevel@tonic-gate 			 * and things will remain this way until we drop
3063*7c478bd9Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
3064*7c478bd9Sstevel@tonic-gate 			 * zone.
3065*7c478bd9Sstevel@tonic-gate 			 */
3066*7c478bd9Sstevel@tonic-gate 			break;
3067*7c478bd9Sstevel@tonic-gate 		}
3068*7c478bd9Sstevel@tonic-gate 
3069*7c478bd9Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
3070*7c478bd9Sstevel@tonic-gate 			/* Signaled */
3071*7c478bd9Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
3072*7c478bd9Sstevel@tonic-gate 			return (set_errno(EINTR));
3073*7c478bd9Sstevel@tonic-gate 		}
3074*7c478bd9Sstevel@tonic-gate 
3075*7c478bd9Sstevel@tonic-gate 	}
3076*7c478bd9Sstevel@tonic-gate 
3077*7c478bd9Sstevel@tonic-gate 	/*
3078*7c478bd9Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
3079*7c478bd9Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
3080*7c478bd9Sstevel@tonic-gate 	 * reference goes away.
3081*7c478bd9Sstevel@tonic-gate 	 */
3082*7c478bd9Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
3083*7c478bd9Sstevel@tonic-gate 	zonecount--;
3084*7c478bd9Sstevel@tonic-gate 	/* remove from active list and hash tables */
3085*7c478bd9Sstevel@tonic-gate 	list_remove(&zone_active, zone);
3086*7c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
3087*7c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
3088*7c478bd9Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
3089*7c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
3090*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
3091*7c478bd9Sstevel@tonic-gate 
3092*7c478bd9Sstevel@tonic-gate 	/* add to deathrow list */
3093*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
3094*7c478bd9Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
3095*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
3096*7c478bd9Sstevel@tonic-gate 
3097*7c478bd9Sstevel@tonic-gate 	/*
3098*7c478bd9Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
3099*7c478bd9Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
3100*7c478bd9Sstevel@tonic-gate 	 */
3101*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
3102*7c478bd9Sstevel@tonic-gate 	return (0);
3103*7c478bd9Sstevel@tonic-gate }
3104*7c478bd9Sstevel@tonic-gate 
3105*7c478bd9Sstevel@tonic-gate /*
3106*7c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
3107*7c478bd9Sstevel@tonic-gate  */
3108*7c478bd9Sstevel@tonic-gate static ssize_t
3109*7c478bd9Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
3110*7c478bd9Sstevel@tonic-gate {
3111*7c478bd9Sstevel@tonic-gate 	size_t size;
3112*7c478bd9Sstevel@tonic-gate 	int error = 0, err;
3113*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
3114*7c478bd9Sstevel@tonic-gate 	char *zonepath;
3115*7c478bd9Sstevel@tonic-gate 	zone_status_t zone_status;
3116*7c478bd9Sstevel@tonic-gate 	pid_t initpid;
3117*7c478bd9Sstevel@tonic-gate 	boolean_t global = (curproc->p_zone == global_zone);
3118*7c478bd9Sstevel@tonic-gate 
3119*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
3120*7c478bd9Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
3121*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3122*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3123*7c478bd9Sstevel@tonic-gate 	}
3124*7c478bd9Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
3125*7c478bd9Sstevel@tonic-gate 	if (zone_status < ZONE_IS_READY) {
3126*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3127*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3128*7c478bd9Sstevel@tonic-gate 	}
3129*7c478bd9Sstevel@tonic-gate 	zone_hold(zone);
3130*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
3131*7c478bd9Sstevel@tonic-gate 
3132*7c478bd9Sstevel@tonic-gate 	/*
3133*7c478bd9Sstevel@tonic-gate 	 * If not in the global zone, don't show information about other zones.
3134*7c478bd9Sstevel@tonic-gate 	 */
3135*7c478bd9Sstevel@tonic-gate 	if (!global && curproc->p_zone != zone) {
3136*7c478bd9Sstevel@tonic-gate 		zone_rele(zone);
3137*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3138*7c478bd9Sstevel@tonic-gate 	}
3139*7c478bd9Sstevel@tonic-gate 
3140*7c478bd9Sstevel@tonic-gate 	switch (attr) {
3141*7c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
3142*7c478bd9Sstevel@tonic-gate 		if (global) {
3143*7c478bd9Sstevel@tonic-gate 			/*
3144*7c478bd9Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
3145*7c478bd9Sstevel@tonic-gate 			 * the global zone).
3146*7c478bd9Sstevel@tonic-gate 			 */
3147*7c478bd9Sstevel@tonic-gate 			if (zone != global_zone)
3148*7c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
3149*7c478bd9Sstevel@tonic-gate 			else
3150*7c478bd9Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
3151*7c478bd9Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
3152*7c478bd9Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
3153*7c478bd9Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
3154*7c478bd9Sstevel@tonic-gate 		} else {
3155*7c478bd9Sstevel@tonic-gate 			/*
3156*7c478bd9Sstevel@tonic-gate 			 * Caller is not in the global zone, just return
3157*7c478bd9Sstevel@tonic-gate 			 * faked-up path for current zone.
3158*7c478bd9Sstevel@tonic-gate 			 */
3159*7c478bd9Sstevel@tonic-gate 			zonepath = "/";
3160*7c478bd9Sstevel@tonic-gate 			size = 2;
3161*7c478bd9Sstevel@tonic-gate 		}
3162*7c478bd9Sstevel@tonic-gate 		if (bufsize > size)
3163*7c478bd9Sstevel@tonic-gate 			bufsize = size;
3164*7c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
3165*7c478bd9Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
3166*7c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
3167*7c478bd9Sstevel@tonic-gate 				error = EFAULT;
3168*7c478bd9Sstevel@tonic-gate 		}
3169*7c478bd9Sstevel@tonic-gate 		if (global)
3170*7c478bd9Sstevel@tonic-gate 			kmem_free(zonepath, size);
3171*7c478bd9Sstevel@tonic-gate 		break;
3172*7c478bd9Sstevel@tonic-gate 
3173*7c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
3174*7c478bd9Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
3175*7c478bd9Sstevel@tonic-gate 		if (bufsize > size)
3176*7c478bd9Sstevel@tonic-gate 			bufsize = size;
3177*7c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
3178*7c478bd9Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
3179*7c478bd9Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
3180*7c478bd9Sstevel@tonic-gate 				error = EFAULT;
3181*7c478bd9Sstevel@tonic-gate 		}
3182*7c478bd9Sstevel@tonic-gate 		break;
3183*7c478bd9Sstevel@tonic-gate 
3184*7c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
3185*7c478bd9Sstevel@tonic-gate 		/*
3186*7c478bd9Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
3187*7c478bd9Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
3188*7c478bd9Sstevel@tonic-gate 		 */
3189*7c478bd9Sstevel@tonic-gate 		size = sizeof (zone_status);
3190*7c478bd9Sstevel@tonic-gate 		if (bufsize > size)
3191*7c478bd9Sstevel@tonic-gate 			bufsize = size;
3192*7c478bd9Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
3193*7c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
3194*7c478bd9Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
3195*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
3196*7c478bd9Sstevel@tonic-gate 		break;
3197*7c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
3198*7c478bd9Sstevel@tonic-gate 		size = sizeof (priv_set_t);
3199*7c478bd9Sstevel@tonic-gate 		if (bufsize > size)
3200*7c478bd9Sstevel@tonic-gate 			bufsize = size;
3201*7c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
3202*7c478bd9Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
3203*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
3204*7c478bd9Sstevel@tonic-gate 		break;
3205*7c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
3206*7c478bd9Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
3207*7c478bd9Sstevel@tonic-gate 		if (bufsize > size)
3208*7c478bd9Sstevel@tonic-gate 			bufsize = size;
3209*7c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
3210*7c478bd9Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
3211*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
3212*7c478bd9Sstevel@tonic-gate 		break;
3213*7c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
3214*7c478bd9Sstevel@tonic-gate 		{
3215*7c478bd9Sstevel@tonic-gate 			pool_t *pool;
3216*7c478bd9Sstevel@tonic-gate 			poolid_t poolid;
3217*7c478bd9Sstevel@tonic-gate 
3218*7c478bd9Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
3219*7c478bd9Sstevel@tonic-gate 				error = EINTR;
3220*7c478bd9Sstevel@tonic-gate 				break;
3221*7c478bd9Sstevel@tonic-gate 			}
3222*7c478bd9Sstevel@tonic-gate 			pool = zone_pool_get(zone);
3223*7c478bd9Sstevel@tonic-gate 			poolid = pool->pool_id;
3224*7c478bd9Sstevel@tonic-gate 			pool_unlock();
3225*7c478bd9Sstevel@tonic-gate 			size = sizeof (poolid);
3226*7c478bd9Sstevel@tonic-gate 			if (bufsize > size)
3227*7c478bd9Sstevel@tonic-gate 				bufsize = size;
3228*7c478bd9Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
3229*7c478bd9Sstevel@tonic-gate 				error = EFAULT;
3230*7c478bd9Sstevel@tonic-gate 		}
3231*7c478bd9Sstevel@tonic-gate 		break;
3232*7c478bd9Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
3233*7c478bd9Sstevel@tonic-gate 		size = sizeof (initpid);
3234*7c478bd9Sstevel@tonic-gate 		if (bufsize > size)
3235*7c478bd9Sstevel@tonic-gate 			bufsize = size;
3236*7c478bd9Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
3237*7c478bd9Sstevel@tonic-gate 		if (initpid == -1) {
3238*7c478bd9Sstevel@tonic-gate 			error = ESRCH;
3239*7c478bd9Sstevel@tonic-gate 			break;
3240*7c478bd9Sstevel@tonic-gate 		}
3241*7c478bd9Sstevel@tonic-gate 		if (buf != NULL &&
3242*7c478bd9Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
3243*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
3244*7c478bd9Sstevel@tonic-gate 		break;
3245*7c478bd9Sstevel@tonic-gate 	default:
3246*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
3247*7c478bd9Sstevel@tonic-gate 	}
3248*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
3249*7c478bd9Sstevel@tonic-gate 
3250*7c478bd9Sstevel@tonic-gate 	if (error)
3251*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3252*7c478bd9Sstevel@tonic-gate 	return ((ssize_t)size);
3253*7c478bd9Sstevel@tonic-gate }
3254*7c478bd9Sstevel@tonic-gate 
3255*7c478bd9Sstevel@tonic-gate /*
3256*7c478bd9Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
3257*7c478bd9Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
3258*7c478bd9Sstevel@tonic-gate  */
3259*7c478bd9Sstevel@tonic-gate static int
3260*7c478bd9Sstevel@tonic-gate as_can_change_zones(void)
3261*7c478bd9Sstevel@tonic-gate {
3262*7c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
3263*7c478bd9Sstevel@tonic-gate 	struct seg *seg;
3264*7c478bd9Sstevel@tonic-gate 	struct as *as = pp->p_as;
3265*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
3266*7c478bd9Sstevel@tonic-gate 	int allow = 1;
3267*7c478bd9Sstevel@tonic-gate 
3268*7c478bd9Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
3269*7c478bd9Sstevel@tonic-gate 	AS_LOCK_ENTER(&as, &as->a_lock, RW_READER);
3270*7c478bd9Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
3271*7c478bd9Sstevel@tonic-gate 		/*
3272*7c478bd9Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
3273*7c478bd9Sstevel@tonic-gate 		 * it.
3274*7c478bd9Sstevel@tonic-gate 		 */
3275*7c478bd9Sstevel@tonic-gate 		vp = NULL;
3276*7c478bd9Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
3277*7c478bd9Sstevel@tonic-gate 			continue;
3278*7c478bd9Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
3279*7c478bd9Sstevel@tonic-gate 			allow = 0;
3280*7c478bd9Sstevel@tonic-gate 			break;
3281*7c478bd9Sstevel@tonic-gate 		}
3282*7c478bd9Sstevel@tonic-gate 	}
3283*7c478bd9Sstevel@tonic-gate 	AS_LOCK_EXIT(&as, &as->a_lock);
3284*7c478bd9Sstevel@tonic-gate 	return (allow);
3285*7c478bd9Sstevel@tonic-gate }
3286*7c478bd9Sstevel@tonic-gate 
3287*7c478bd9Sstevel@tonic-gate /*
3288*7c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
3289*7c478bd9Sstevel@tonic-gate  *
3290*7c478bd9Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
3291*7c478bd9Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
3292*7c478bd9Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
3293*7c478bd9Sstevel@tonic-gate  *
3294*7c478bd9Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
3295*7c478bd9Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
3296*7c478bd9Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
3297*7c478bd9Sstevel@tonic-gate  */
3298*7c478bd9Sstevel@tonic-gate static int
3299*7c478bd9Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
3300*7c478bd9Sstevel@tonic-gate {
3301*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
3302*7c478bd9Sstevel@tonic-gate 	vnode_t *vp;
3303*7c478bd9Sstevel@tonic-gate 	proc_t *pp = curproc;
3304*7c478bd9Sstevel@tonic-gate 	contract_t *ct;
3305*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
3306*7c478bd9Sstevel@tonic-gate 	task_t *tk, *oldtk;
3307*7c478bd9Sstevel@tonic-gate 	kproject_t *zone_proj0;
3308*7c478bd9Sstevel@tonic-gate 	cred_t *cr, *newcr;
3309*7c478bd9Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
3310*7c478bd9Sstevel@tonic-gate 	sess_t *sp;
3311*7c478bd9Sstevel@tonic-gate 	uid_t uid;
3312*7c478bd9Sstevel@tonic-gate 	zone_status_t status;
3313*7c478bd9Sstevel@tonic-gate 	int err = 0;
3314*7c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
3315*7c478bd9Sstevel@tonic-gate 
3316*7c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
3317*7c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
3318*7c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
3319*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3320*7c478bd9Sstevel@tonic-gate 
3321*7c478bd9Sstevel@tonic-gate 	/*
3322*7c478bd9Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
3323*7c478bd9Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
3324*7c478bd9Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
3325*7c478bd9Sstevel@tonic-gate 	 * be waiting for the held lock).
3326*7c478bd9Sstevel@tonic-gate 	 */
3327*7c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
3328*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
3329*7c478bd9Sstevel@tonic-gate 
3330*7c478bd9Sstevel@tonic-gate 	/*
3331*7c478bd9Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
3332*7c478bd9Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
3333*7c478bd9Sstevel@tonic-gate 	 */
3334*7c478bd9Sstevel@tonic-gate 	if (!files_can_change_zones()) {
3335*7c478bd9Sstevel@tonic-gate 		err = EBADF;
3336*7c478bd9Sstevel@tonic-gate 		goto out;
3337*7c478bd9Sstevel@tonic-gate 	}
3338*7c478bd9Sstevel@tonic-gate 	if (!as_can_change_zones()) {
3339*7c478bd9Sstevel@tonic-gate 		err = EFAULT;
3340*7c478bd9Sstevel@tonic-gate 		goto out;
3341*7c478bd9Sstevel@tonic-gate 	}
3342*7c478bd9Sstevel@tonic-gate 
3343*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
3344*7c478bd9Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
3345*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3346*7c478bd9Sstevel@tonic-gate 		err = EINVAL;
3347*7c478bd9Sstevel@tonic-gate 		goto out;
3348*7c478bd9Sstevel@tonic-gate 	}
3349*7c478bd9Sstevel@tonic-gate 
3350*7c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
3351*7c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
3352*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3353*7c478bd9Sstevel@tonic-gate 		err = EINVAL;
3354*7c478bd9Sstevel@tonic-gate 		goto out;
3355*7c478bd9Sstevel@tonic-gate 	}
3356*7c478bd9Sstevel@tonic-gate 
3357*7c478bd9Sstevel@tonic-gate 	/*
3358*7c478bd9Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
3359*7c478bd9Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
3360*7c478bd9Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
3361*7c478bd9Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
3362*7c478bd9Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
3363*7c478bd9Sstevel@tonic-gate 	 */
3364*7c478bd9Sstevel@tonic-gate 	ctp = pp->p_ct_process;
3365*7c478bd9Sstevel@tonic-gate 	ct = &ctp->conp_contract;
3366*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
3367*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
3368*7c478bd9Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
3369*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
3370*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
3371*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3372*7c478bd9Sstevel@tonic-gate 		pool_unlock();
3373*7c478bd9Sstevel@tonic-gate 		err = EINVAL;
3374*7c478bd9Sstevel@tonic-gate 		goto out;
3375*7c478bd9Sstevel@tonic-gate 	}
3376*7c478bd9Sstevel@tonic-gate 
3377*7c478bd9Sstevel@tonic-gate 	/*
3378*7c478bd9Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
3379*7c478bd9Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
3380*7c478bd9Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
3381*7c478bd9Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
3382*7c478bd9Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
3383*7c478bd9Sstevel@tonic-gate 	 * predecessor's contracts.
3384*7c478bd9Sstevel@tonic-gate 	 */
3385*7c478bd9Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
3386*7c478bd9Sstevel@tonic-gate 		contract_t *next;
3387*7c478bd9Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
3388*7c478bd9Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
3389*7c478bd9Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
3390*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
3391*7c478bd9Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
3392*7c478bd9Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
3393*7c478bd9Sstevel@tonic-gate 				pool_unlock();
3394*7c478bd9Sstevel@tonic-gate 				err = EINVAL;
3395*7c478bd9Sstevel@tonic-gate 				goto out;
3396*7c478bd9Sstevel@tonic-gate 			}
3397*7c478bd9Sstevel@tonic-gate 		}
3398*7c478bd9Sstevel@tonic-gate 	}
3399*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3400*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
3401*7c478bd9Sstevel@tonic-gate 
3402*7c478bd9Sstevel@tonic-gate 	status = zone_status_get(zone);
3403*7c478bd9Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
3404*7c478bd9Sstevel@tonic-gate 		/*
3405*7c478bd9Sstevel@tonic-gate 		 * Can't join
3406*7c478bd9Sstevel@tonic-gate 		 */
3407*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3408*7c478bd9Sstevel@tonic-gate 		err = EINVAL;
3409*7c478bd9Sstevel@tonic-gate 		goto out;
3410*7c478bd9Sstevel@tonic-gate 	}
3411*7c478bd9Sstevel@tonic-gate 
3412*7c478bd9Sstevel@tonic-gate 	/*
3413*7c478bd9Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
3414*7c478bd9Sstevel@tonic-gate 	 */
3415*7c478bd9Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
3416*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3417*7c478bd9Sstevel@tonic-gate 		err = EPERM;
3418*7c478bd9Sstevel@tonic-gate 		goto out;
3419*7c478bd9Sstevel@tonic-gate 	}
3420*7c478bd9Sstevel@tonic-gate 	/*
3421*7c478bd9Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
3422*7c478bd9Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
3423*7c478bd9Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
3424*7c478bd9Sstevel@tonic-gate 	 */
3425*7c478bd9Sstevel@tonic-gate 	zone_hold(zone);
3426*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
3427*7c478bd9Sstevel@tonic-gate 
3428*7c478bd9Sstevel@tonic-gate 	/*
3429*7c478bd9Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
3430*7c478bd9Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
3431*7c478bd9Sstevel@tonic-gate 	 * until we join the zone.
3432*7c478bd9Sstevel@tonic-gate 	 */
3433*7c478bd9Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
3434*7c478bd9Sstevel@tonic-gate 		zone_rele(zone);
3435*7c478bd9Sstevel@tonic-gate 		err = EINTR;
3436*7c478bd9Sstevel@tonic-gate 		goto out;
3437*7c478bd9Sstevel@tonic-gate 	}
3438*7c478bd9Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
3439*7c478bd9Sstevel@tonic-gate 	/*
3440*7c478bd9Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
3441*7c478bd9Sstevel@tonic-gate 	 */
3442*7c478bd9Sstevel@tonic-gate 	oldpool = curproc->p_pool;
3443*7c478bd9Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
3444*7c478bd9Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
3445*7c478bd9Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
3446*7c478bd9Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
3447*7c478bd9Sstevel@tonic-gate 		pool_unlock();
3448*7c478bd9Sstevel@tonic-gate 		zone_rele(zone);
3449*7c478bd9Sstevel@tonic-gate 		goto out;
3450*7c478bd9Sstevel@tonic-gate 	}
3451*7c478bd9Sstevel@tonic-gate 
3452*7c478bd9Sstevel@tonic-gate 	/*
3453*7c478bd9Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
3454*7c478bd9Sstevel@tonic-gate 	 * task_join().
3455*7c478bd9Sstevel@tonic-gate 	 */
3456*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
3457*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
3458*7c478bd9Sstevel@tonic-gate 	/*
3459*7c478bd9Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
3460*7c478bd9Sstevel@tonic-gate 	 */
3461*7c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
3462*7c478bd9Sstevel@tonic-gate 		/*
3463*7c478bd9Sstevel@tonic-gate 		 * Can't join anymore.
3464*7c478bd9Sstevel@tonic-gate 		 */
3465*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3466*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
3467*7c478bd9Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
3468*7c478bd9Sstevel@tonic-gate 		    newpool != oldpool)
3469*7c478bd9Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
3470*7c478bd9Sstevel@tonic-gate 			    POOL_BIND_ALL);
3471*7c478bd9Sstevel@tonic-gate 		pool_unlock();
3472*7c478bd9Sstevel@tonic-gate 		zone_rele(zone);
3473*7c478bd9Sstevel@tonic-gate 		err = EINVAL;
3474*7c478bd9Sstevel@tonic-gate 		goto out;
3475*7c478bd9Sstevel@tonic-gate 	}
3476*7c478bd9Sstevel@tonic-gate 
3477*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
3478*7c478bd9Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
3479*7c478bd9Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
3480*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
3481*7c478bd9Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
3482*7c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
3483*7c478bd9Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
3484*7c478bd9Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
3485*7c478bd9Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
3486*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3487*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
3488*7c478bd9Sstevel@tonic-gate 
3489*7c478bd9Sstevel@tonic-gate 	/* remove lwps from proc's old zone and old project */
3490*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
3491*7c478bd9Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
3492*7c478bd9Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
3493*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
3494*7c478bd9Sstevel@tonic-gate 
3495*7c478bd9Sstevel@tonic-gate 	/*
3496*7c478bd9Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
3497*7c478bd9Sstevel@tonic-gate 	 *
3498*7c478bd9Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
3499*7c478bd9Sstevel@tonic-gate 	 * shared with zsched().
3500*7c478bd9Sstevel@tonic-gate 	 */
3501*7c478bd9Sstevel@tonic-gate 
3502*7c478bd9Sstevel@tonic-gate 	/*
3503*7c478bd9Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
3504*7c478bd9Sstevel@tonic-gate 	 */
3505*7c478bd9Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
3506*7c478bd9Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
3507*7c478bd9Sstevel@tonic-gate 
3508*7c478bd9Sstevel@tonic-gate 	/*
3509*7c478bd9Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
3510*7c478bd9Sstevel@tonic-gate 	 * by (projid,zoneid).
3511*7c478bd9Sstevel@tonic-gate 	 *
3512*7c478bd9Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
3513*7c478bd9Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
3514*7c478bd9Sstevel@tonic-gate 	 *
3515*7c478bd9Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
3516*7c478bd9Sstevel@tonic-gate 	 */
3517*7c478bd9Sstevel@tonic-gate 	tk = task_create(0, zone);
3518*7c478bd9Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
3519*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
3520*7c478bd9Sstevel@tonic-gate 
3521*7c478bd9Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
3522*7c478bd9Sstevel@tonic-gate 	pp->p_zone = zone;
3523*7c478bd9Sstevel@tonic-gate 
3524*7c478bd9Sstevel@tonic-gate 	/*
3525*7c478bd9Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
3526*7c478bd9Sstevel@tonic-gate 	 */
3527*7c478bd9Sstevel@tonic-gate 	e.rcep_p.zone = zone;
3528*7c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
3529*7c478bd9Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
3530*7c478bd9Sstevel@tonic-gate 	    RCD_CALLBACK);
3531*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3532*7c478bd9Sstevel@tonic-gate 
3533*7c478bd9Sstevel@tonic-gate 	/*
3534*7c478bd9Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
3535*7c478bd9Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
3536*7c478bd9Sstevel@tonic-gate 	 * changing either.
3537*7c478bd9Sstevel@tonic-gate 	 *
3538*7c478bd9Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
3539*7c478bd9Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
3540*7c478bd9Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
3541*7c478bd9Sstevel@tonic-gate 	 */
3542*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
3543*7c478bd9Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
3544*7c478bd9Sstevel@tonic-gate 	SESS_HOLD(sp);
3545*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
3546*7c478bd9Sstevel@tonic-gate 	pgexit(pp);
3547*7c478bd9Sstevel@tonic-gate 	SESS_RELE(pp->p_sessp);
3548*7c478bd9Sstevel@tonic-gate 	pp->p_sessp = sp;
3549*7c478bd9Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
3550*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3551*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
3552*7c478bd9Sstevel@tonic-gate 
3553*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
3554*7c478bd9Sstevel@tonic-gate 	/*
3555*7c478bd9Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
3556*7c478bd9Sstevel@tonic-gate 	 */
3557*7c478bd9Sstevel@tonic-gate 	pool_unlock();
3558*7c478bd9Sstevel@tonic-gate 	task_rele(oldtk);
3559*7c478bd9Sstevel@tonic-gate 	/*
3560*7c478bd9Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
3561*7c478bd9Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
3562*7c478bd9Sstevel@tonic-gate 	 */
3563*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
3564*7c478bd9Sstevel@tonic-gate 
3565*7c478bd9Sstevel@tonic-gate 	/*
3566*7c478bd9Sstevel@tonic-gate 	 * Chroot
3567*7c478bd9Sstevel@tonic-gate 	 */
3568*7c478bd9Sstevel@tonic-gate 	vp = zone->zone_rootvp;
3569*7c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
3570*7c478bd9Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
3571*7c478bd9Sstevel@tonic-gate 
3572*7c478bd9Sstevel@tonic-gate 	/*
3573*7c478bd9Sstevel@tonic-gate 	 * Change process credentials
3574*7c478bd9Sstevel@tonic-gate 	 */
3575*7c478bd9Sstevel@tonic-gate 	newcr = cralloc();
3576*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
3577*7c478bd9Sstevel@tonic-gate 	cr = pp->p_cred;
3578*7c478bd9Sstevel@tonic-gate 	crcopy_to(cr, newcr);
3579*7c478bd9Sstevel@tonic-gate 	crsetzone(newcr, zone);
3580*7c478bd9Sstevel@tonic-gate 	pp->p_cred = newcr;
3581*7c478bd9Sstevel@tonic-gate 
3582*7c478bd9Sstevel@tonic-gate 	/*
3583*7c478bd9Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
3584*7c478bd9Sstevel@tonic-gate 	 */
3585*7c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
3586*7c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
3587*7c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
3588*7c478bd9Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
3589*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
3590*7c478bd9Sstevel@tonic-gate 	crset(pp, newcr);
3591*7c478bd9Sstevel@tonic-gate 
3592*7c478bd9Sstevel@tonic-gate 	/*
3593*7c478bd9Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
3594*7c478bd9Sstevel@tonic-gate 	 */
3595*7c478bd9Sstevel@tonic-gate 	uid = crgetruid(newcr);
3596*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
3597*7c478bd9Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
3598*7c478bd9Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
3599*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
3600*7c478bd9Sstevel@tonic-gate 
3601*7c478bd9Sstevel@tonic-gate 	/*
3602*7c478bd9Sstevel@tonic-gate 	 * Set up core file path and content.
3603*7c478bd9Sstevel@tonic-gate 	 */
3604*7c478bd9Sstevel@tonic-gate 	set_core_defaults();
3605*7c478bd9Sstevel@tonic-gate 
3606*7c478bd9Sstevel@tonic-gate out:
3607*7c478bd9Sstevel@tonic-gate 	/*
3608*7c478bd9Sstevel@tonic-gate 	 * Let the other lwps continue.
3609*7c478bd9Sstevel@tonic-gate 	 */
3610*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
3611*7c478bd9Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
3612*7c478bd9Sstevel@tonic-gate 		continuelwps(pp);
3613*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
3614*7c478bd9Sstevel@tonic-gate 
3615*7c478bd9Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
3616*7c478bd9Sstevel@tonic-gate }
3617*7c478bd9Sstevel@tonic-gate 
3618*7c478bd9Sstevel@tonic-gate /*
3619*7c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
3620*7c478bd9Sstevel@tonic-gate  *
3621*7c478bd9Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
3622*7c478bd9Sstevel@tonic-gate  */
3623*7c478bd9Sstevel@tonic-gate static int
3624*7c478bd9Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
3625*7c478bd9Sstevel@tonic-gate {
3626*7c478bd9Sstevel@tonic-gate 	zoneid_t *zoneids;
3627*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
3628*7c478bd9Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
3629*7c478bd9Sstevel@tonic-gate 	int error = 0;
3630*7c478bd9Sstevel@tonic-gate 	uint_t i;
3631*7c478bd9Sstevel@tonic-gate 
3632*7c478bd9Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
3633*7c478bd9Sstevel@tonic-gate 		return (set_errno(EFAULT));
3634*7c478bd9Sstevel@tonic-gate 
3635*7c478bd9Sstevel@tonic-gate 	if (curproc->p_zone != global_zone) {
3636*7c478bd9Sstevel@tonic-gate 		/* just return current zone */
3637*7c478bd9Sstevel@tonic-gate 		real_nzones = 1;
3638*7c478bd9Sstevel@tonic-gate 		zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
3639*7c478bd9Sstevel@tonic-gate 		zoneids[0] = curproc->p_zone->zone_id;
3640*7c478bd9Sstevel@tonic-gate 	} else {
3641*7c478bd9Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
3642*7c478bd9Sstevel@tonic-gate 		real_nzones = zonecount;
3643*7c478bd9Sstevel@tonic-gate 		if (real_nzones) {
3644*7c478bd9Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
3645*7c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
3646*7c478bd9Sstevel@tonic-gate 			i = 0;
3647*7c478bd9Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
3648*7c478bd9Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
3649*7c478bd9Sstevel@tonic-gate 				zoneids[i++] = zone->zone_id;
3650*7c478bd9Sstevel@tonic-gate 			ASSERT(i == real_nzones);
3651*7c478bd9Sstevel@tonic-gate 		}
3652*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3653*7c478bd9Sstevel@tonic-gate 	}
3654*7c478bd9Sstevel@tonic-gate 
3655*7c478bd9Sstevel@tonic-gate 	if (user_nzones > real_nzones)
3656*7c478bd9Sstevel@tonic-gate 		user_nzones = real_nzones;
3657*7c478bd9Sstevel@tonic-gate 
3658*7c478bd9Sstevel@tonic-gate 	if (copyout(&real_nzones, numzones, sizeof (uint_t)) != 0)
3659*7c478bd9Sstevel@tonic-gate 		error = EFAULT;
3660*7c478bd9Sstevel@tonic-gate 	else if (zoneidlist != NULL && user_nzones != 0) {
3661*7c478bd9Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
3662*7c478bd9Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
3663*7c478bd9Sstevel@tonic-gate 			error = EFAULT;
3664*7c478bd9Sstevel@tonic-gate 	}
3665*7c478bd9Sstevel@tonic-gate 
3666*7c478bd9Sstevel@tonic-gate 	if (real_nzones)
3667*7c478bd9Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
3668*7c478bd9Sstevel@tonic-gate 
3669*7c478bd9Sstevel@tonic-gate 	if (error)
3670*7c478bd9Sstevel@tonic-gate 		return (set_errno(error));
3671*7c478bd9Sstevel@tonic-gate 	else
3672*7c478bd9Sstevel@tonic-gate 		return (0);
3673*7c478bd9Sstevel@tonic-gate }
3674*7c478bd9Sstevel@tonic-gate 
3675*7c478bd9Sstevel@tonic-gate /*
3676*7c478bd9Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
3677*7c478bd9Sstevel@tonic-gate  *
3678*7c478bd9Sstevel@tonic-gate  * Non-global zones are only able to see themselves.
3679*7c478bd9Sstevel@tonic-gate  */
3680*7c478bd9Sstevel@tonic-gate static zoneid_t
3681*7c478bd9Sstevel@tonic-gate zone_lookup(const char *zone_name)
3682*7c478bd9Sstevel@tonic-gate {
3683*7c478bd9Sstevel@tonic-gate 	char *kname;
3684*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
3685*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
3686*7c478bd9Sstevel@tonic-gate 	int err;
3687*7c478bd9Sstevel@tonic-gate 
3688*7c478bd9Sstevel@tonic-gate 	if (zone_name == NULL) {
3689*7c478bd9Sstevel@tonic-gate 		/* return caller's zone id */
3690*7c478bd9Sstevel@tonic-gate 		return (getzoneid());
3691*7c478bd9Sstevel@tonic-gate 	}
3692*7c478bd9Sstevel@tonic-gate 
3693*7c478bd9Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
3694*7c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
3695*7c478bd9Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
3696*7c478bd9Sstevel@tonic-gate 		return (set_errno(err));
3697*7c478bd9Sstevel@tonic-gate 	}
3698*7c478bd9Sstevel@tonic-gate 
3699*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
3700*7c478bd9Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
3701*7c478bd9Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
3702*7c478bd9Sstevel@tonic-gate 	if (zone == NULL || zone_status_get(zone) < ZONE_IS_READY ||
3703*7c478bd9Sstevel@tonic-gate 	    (curproc->p_zone != global_zone && curproc->p_zone != zone)) {
3704*7c478bd9Sstevel@tonic-gate 		/* in non-global zone, can only lookup own name */
3705*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
3706*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3707*7c478bd9Sstevel@tonic-gate 	}
3708*7c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id;
3709*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
3710*7c478bd9Sstevel@tonic-gate 	return (zoneid);
3711*7c478bd9Sstevel@tonic-gate }
3712*7c478bd9Sstevel@tonic-gate 
3713*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
3714*7c478bd9Sstevel@tonic-gate long
3715*7c478bd9Sstevel@tonic-gate zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5)
3716*7c478bd9Sstevel@tonic-gate {
3717*7c478bd9Sstevel@tonic-gate 	zone_def zs;
3718*7c478bd9Sstevel@tonic-gate 
3719*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
3720*7c478bd9Sstevel@tonic-gate 	case ZONE_CREATE:
3721*7c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
3722*7c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
3723*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
3724*7c478bd9Sstevel@tonic-gate 			}
3725*7c478bd9Sstevel@tonic-gate 		} else {
3726*7c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3727*7c478bd9Sstevel@tonic-gate 			zone_def32 zs32;
3728*7c478bd9Sstevel@tonic-gate 
3729*7c478bd9Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
3730*7c478bd9Sstevel@tonic-gate 				return (set_errno(EFAULT));
3731*7c478bd9Sstevel@tonic-gate 			}
3732*7c478bd9Sstevel@tonic-gate 			zs.zone_name =
3733*7c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
3734*7c478bd9Sstevel@tonic-gate 			zs.zone_root =
3735*7c478bd9Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
3736*7c478bd9Sstevel@tonic-gate 			zs.zone_privs =
3737*7c478bd9Sstevel@tonic-gate 			    (const struct priv_set *)
3738*7c478bd9Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
3739*7c478bd9Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
3740*7c478bd9Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
3741*7c478bd9Sstevel@tonic-gate 			zs.extended_error =
3742*7c478bd9Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
3743*7c478bd9Sstevel@tonic-gate #else
3744*7c478bd9Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
3745*7c478bd9Sstevel@tonic-gate #endif
3746*7c478bd9Sstevel@tonic-gate 		}
3747*7c478bd9Sstevel@tonic-gate 
3748*7c478bd9Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
3749*7c478bd9Sstevel@tonic-gate 			zs.zone_privs, (caddr_t)zs.rctlbuf, zs.rctlbufsz,
3750*7c478bd9Sstevel@tonic-gate 			zs.extended_error));
3751*7c478bd9Sstevel@tonic-gate 	case ZONE_BOOT:
3752*7c478bd9Sstevel@tonic-gate 		return (zone_boot((zoneid_t)(uintptr_t)arg1,
3753*7c478bd9Sstevel@tonic-gate 		    (const char *)arg2));
3754*7c478bd9Sstevel@tonic-gate 	case ZONE_DESTROY:
3755*7c478bd9Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
3756*7c478bd9Sstevel@tonic-gate 	case ZONE_GETATTR:
3757*7c478bd9Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
3758*7c478bd9Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
3759*7c478bd9Sstevel@tonic-gate 	case ZONE_ENTER:
3760*7c478bd9Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
3761*7c478bd9Sstevel@tonic-gate 	case ZONE_LIST:
3762*7c478bd9Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
3763*7c478bd9Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
3764*7c478bd9Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
3765*7c478bd9Sstevel@tonic-gate 	case ZONE_LOOKUP:
3766*7c478bd9Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
3767*7c478bd9Sstevel@tonic-gate 	default:
3768*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
3769*7c478bd9Sstevel@tonic-gate 	}
3770*7c478bd9Sstevel@tonic-gate }
3771*7c478bd9Sstevel@tonic-gate 
3772*7c478bd9Sstevel@tonic-gate struct zarg {
3773*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
3774*7c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
3775*7c478bd9Sstevel@tonic-gate };
3776*7c478bd9Sstevel@tonic-gate 
3777*7c478bd9Sstevel@tonic-gate static int
3778*7c478bd9Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
3779*7c478bd9Sstevel@tonic-gate {
3780*7c478bd9Sstevel@tonic-gate 	char *buf;
3781*7c478bd9Sstevel@tonic-gate 	size_t buflen;
3782*7c478bd9Sstevel@tonic-gate 	int error;
3783*7c478bd9Sstevel@tonic-gate 
3784*7c478bd9Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
3785*7c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
3786*7c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
3787*7c478bd9Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
3788*7c478bd9Sstevel@tonic-gate 	kmem_free(buf, buflen);
3789*7c478bd9Sstevel@tonic-gate 	return (error);
3790*7c478bd9Sstevel@tonic-gate }
3791*7c478bd9Sstevel@tonic-gate 
3792*7c478bd9Sstevel@tonic-gate static void
3793*7c478bd9Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
3794*7c478bd9Sstevel@tonic-gate {
3795*7c478bd9Sstevel@tonic-gate 	door_ki_rele(*doorp);
3796*7c478bd9Sstevel@tonic-gate 	*doorp = NULL;
3797*7c478bd9Sstevel@tonic-gate }
3798*7c478bd9Sstevel@tonic-gate 
3799*7c478bd9Sstevel@tonic-gate static void
3800*7c478bd9Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
3801*7c478bd9Sstevel@tonic-gate {
3802*7c478bd9Sstevel@tonic-gate 	door_handle_t door = NULL;
3803*7c478bd9Sstevel@tonic-gate 	door_arg_t darg, save_arg;
3804*7c478bd9Sstevel@tonic-gate 	char *zone_name;
3805*7c478bd9Sstevel@tonic-gate 	size_t zone_namelen;
3806*7c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
3807*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
3808*7c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t arg;
3809*7c478bd9Sstevel@tonic-gate 	uint64_t uniqid;
3810*7c478bd9Sstevel@tonic-gate 	size_t size;
3811*7c478bd9Sstevel@tonic-gate 	int error;
3812*7c478bd9Sstevel@tonic-gate 	int retry;
3813*7c478bd9Sstevel@tonic-gate 
3814*7c478bd9Sstevel@tonic-gate 	zone = zargp->zone;
3815*7c478bd9Sstevel@tonic-gate 	arg = zargp->arg;
3816*7c478bd9Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
3817*7c478bd9Sstevel@tonic-gate 
3818*7c478bd9Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
3819*7c478bd9Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
3820*7c478bd9Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
3821*7c478bd9Sstevel@tonic-gate 	zoneid = zone->zone_id;
3822*7c478bd9Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
3823*7c478bd9Sstevel@tonic-gate 	/*
3824*7c478bd9Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
3825*7c478bd9Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
3826*7c478bd9Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
3827*7c478bd9Sstevel@tonic-gate 	 */
3828*7c478bd9Sstevel@tonic-gate 	ASSERT(curproc == &p0);
3829*7c478bd9Sstevel@tonic-gate 	(void) zone_empty(zone);
3830*7c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
3831*7c478bd9Sstevel@tonic-gate 	zone_rele(zone);
3832*7c478bd9Sstevel@tonic-gate 
3833*7c478bd9Sstevel@tonic-gate 	size = sizeof (arg);
3834*7c478bd9Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
3835*7c478bd9Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
3836*7c478bd9Sstevel@tonic-gate 	darg.rsize = size;
3837*7c478bd9Sstevel@tonic-gate 	darg.data_size = size;
3838*7c478bd9Sstevel@tonic-gate 	darg.desc_ptr = NULL;
3839*7c478bd9Sstevel@tonic-gate 	darg.desc_num = 0;
3840*7c478bd9Sstevel@tonic-gate 
3841*7c478bd9Sstevel@tonic-gate 	save_arg = darg;
3842*7c478bd9Sstevel@tonic-gate 	/*
3843*7c478bd9Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
3844*7c478bd9Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
3845*7c478bd9Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
3846*7c478bd9Sstevel@tonic-gate 	 */
3847*7c478bd9Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
3848*7c478bd9Sstevel@tonic-gate 		if (door == NULL &&
3849*7c478bd9Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
3850*7c478bd9Sstevel@tonic-gate 			goto next;
3851*7c478bd9Sstevel@tonic-gate 		}
3852*7c478bd9Sstevel@tonic-gate 		ASSERT(door != NULL);
3853*7c478bd9Sstevel@tonic-gate 
3854*7c478bd9Sstevel@tonic-gate 		if ((error = door_ki_upcall(door, &darg)) == 0) {
3855*7c478bd9Sstevel@tonic-gate 			break;
3856*7c478bd9Sstevel@tonic-gate 		}
3857*7c478bd9Sstevel@tonic-gate 		switch (error) {
3858*7c478bd9Sstevel@tonic-gate 		case EINTR:
3859*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
3860*7c478bd9Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
3861*7c478bd9Sstevel@tonic-gate 			/*
3862*7c478bd9Sstevel@tonic-gate 			 * Back off for a bit
3863*7c478bd9Sstevel@tonic-gate 			 */
3864*7c478bd9Sstevel@tonic-gate 			break;
3865*7c478bd9Sstevel@tonic-gate 		case EBADF:
3866*7c478bd9Sstevel@tonic-gate 			zone_release_door(&door);
3867*7c478bd9Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
3868*7c478bd9Sstevel@tonic-gate 				/*
3869*7c478bd9Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
3870*7c478bd9Sstevel@tonic-gate 				 * life later.
3871*7c478bd9Sstevel@tonic-gate 				 */
3872*7c478bd9Sstevel@tonic-gate 				break;
3873*7c478bd9Sstevel@tonic-gate 			}
3874*7c478bd9Sstevel@tonic-gate 			break;
3875*7c478bd9Sstevel@tonic-gate 		default:
3876*7c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
3877*7c478bd9Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
3878*7c478bd9Sstevel@tonic-gate 			    error);
3879*7c478bd9Sstevel@tonic-gate 			goto out;
3880*7c478bd9Sstevel@tonic-gate 		}
3881*7c478bd9Sstevel@tonic-gate next:
3882*7c478bd9Sstevel@tonic-gate 		/*
3883*7c478bd9Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
3884*7c478bd9Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
3885*7c478bd9Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
3886*7c478bd9Sstevel@tonic-gate 		 * bail.
3887*7c478bd9Sstevel@tonic-gate 		 */
3888*7c478bd9Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
3889*7c478bd9Sstevel@tonic-gate 			/*
3890*7c478bd9Sstevel@tonic-gate 			 * Problem is solved.
3891*7c478bd9Sstevel@tonic-gate 			 */
3892*7c478bd9Sstevel@tonic-gate 			break;
3893*7c478bd9Sstevel@tonic-gate 		}
3894*7c478bd9Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
3895*7c478bd9Sstevel@tonic-gate 			/*
3896*7c478bd9Sstevel@tonic-gate 			 * zoneid recycled
3897*7c478bd9Sstevel@tonic-gate 			 */
3898*7c478bd9Sstevel@tonic-gate 			zone_rele(zone);
3899*7c478bd9Sstevel@tonic-gate 			break;
3900*7c478bd9Sstevel@tonic-gate 		}
3901*7c478bd9Sstevel@tonic-gate 		/*
3902*7c478bd9Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
3903*7c478bd9Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
3904*7c478bd9Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
3905*7c478bd9Sstevel@tonic-gate 		 */
3906*7c478bd9Sstevel@tonic-gate 		zone_rele(zone);
3907*7c478bd9Sstevel@tonic-gate 		delay(hz);
3908*7c478bd9Sstevel@tonic-gate 		darg = save_arg;
3909*7c478bd9Sstevel@tonic-gate 	}
3910*7c478bd9Sstevel@tonic-gate out:
3911*7c478bd9Sstevel@tonic-gate 	if (door != NULL) {
3912*7c478bd9Sstevel@tonic-gate 		zone_release_door(&door);
3913*7c478bd9Sstevel@tonic-gate 	}
3914*7c478bd9Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
3915*7c478bd9Sstevel@tonic-gate 	thread_exit();
3916*7c478bd9Sstevel@tonic-gate }
3917*7c478bd9Sstevel@tonic-gate 
3918*7c478bd9Sstevel@tonic-gate /*
3919*7c478bd9Sstevel@tonic-gate  * Entry point for uadmin() to tell the zone to go away or reboot.  The caller
3920*7c478bd9Sstevel@tonic-gate  * is a process in the zone to be modified.
3921*7c478bd9Sstevel@tonic-gate  *
3922*7c478bd9Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
3923*7c478bd9Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
3924*7c478bd9Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
3925*7c478bd9Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
3926*7c478bd9Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
3927*7c478bd9Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
3928*7c478bd9Sstevel@tonic-gate  */
3929*7c478bd9Sstevel@tonic-gate int
3930*7c478bd9Sstevel@tonic-gate zone_uadmin(int cmd, int fcn, cred_t *credp)
3931*7c478bd9Sstevel@tonic-gate {
3932*7c478bd9Sstevel@tonic-gate 	struct zarg *zargp;
3933*7c478bd9Sstevel@tonic-gate 	zone_cmd_t zcmd;
3934*7c478bd9Sstevel@tonic-gate 	zone_t *zone;
3935*7c478bd9Sstevel@tonic-gate 
3936*7c478bd9Sstevel@tonic-gate 	zone = curproc->p_zone;
3937*7c478bd9Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
3938*7c478bd9Sstevel@tonic-gate 
3939*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
3940*7c478bd9Sstevel@tonic-gate 	case A_SHUTDOWN:
3941*7c478bd9Sstevel@tonic-gate 		switch (fcn) {
3942*7c478bd9Sstevel@tonic-gate 		case AD_HALT:
3943*7c478bd9Sstevel@tonic-gate 		case AD_POWEROFF:
3944*7c478bd9Sstevel@tonic-gate 			zcmd = Z_HALT;
3945*7c478bd9Sstevel@tonic-gate 			break;
3946*7c478bd9Sstevel@tonic-gate 		case AD_BOOT:
3947*7c478bd9Sstevel@tonic-gate 			zcmd = Z_REBOOT;
3948*7c478bd9Sstevel@tonic-gate 			break;
3949*7c478bd9Sstevel@tonic-gate 		case AD_IBOOT:
3950*7c478bd9Sstevel@tonic-gate 		case AD_SBOOT:
3951*7c478bd9Sstevel@tonic-gate 		case AD_SIBOOT:
3952*7c478bd9Sstevel@tonic-gate 		case AD_NOSYNC:
3953*7c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
3954*7c478bd9Sstevel@tonic-gate 		default:
3955*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
3956*7c478bd9Sstevel@tonic-gate 		}
3957*7c478bd9Sstevel@tonic-gate 		break;
3958*7c478bd9Sstevel@tonic-gate 	case A_REBOOT:
3959*7c478bd9Sstevel@tonic-gate 		zcmd = Z_REBOOT;
3960*7c478bd9Sstevel@tonic-gate 		break;
3961*7c478bd9Sstevel@tonic-gate 	case A_FTRACE:
3962*7c478bd9Sstevel@tonic-gate 	case A_REMOUNT:
3963*7c478bd9Sstevel@tonic-gate 	case A_FREEZE:
3964*7c478bd9Sstevel@tonic-gate 	case A_DUMP:
3965*7c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
3966*7c478bd9Sstevel@tonic-gate 	default:
3967*7c478bd9Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
3968*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
3969*7c478bd9Sstevel@tonic-gate 	}
3970*7c478bd9Sstevel@tonic-gate 
3971*7c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
3972*7c478bd9Sstevel@tonic-gate 		return (EPERM);
3973*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
3974*7c478bd9Sstevel@tonic-gate 	/*
3975*7c478bd9Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
3976*7c478bd9Sstevel@tonic-gate 	 * is in the zone.
3977*7c478bd9Sstevel@tonic-gate 	 */
3978*7c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
3979*7c478bd9Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
3980*7c478bd9Sstevel@tonic-gate 		/*
3981*7c478bd9Sstevel@tonic-gate 		 * This zone is already on its way down.
3982*7c478bd9Sstevel@tonic-gate 		 */
3983*7c478bd9Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
3984*7c478bd9Sstevel@tonic-gate 		return (0);
3985*7c478bd9Sstevel@tonic-gate 	}
3986*7c478bd9Sstevel@tonic-gate 	/*
3987*7c478bd9Sstevel@tonic-gate 	 * Prevent future zone_enter()s
3988*7c478bd9Sstevel@tonic-gate 	 */
3989*7c478bd9Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
3990*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
3991*7c478bd9Sstevel@tonic-gate 
3992*7c478bd9Sstevel@tonic-gate 	/*
3993*7c478bd9Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
3994*7c478bd9Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
3995*7c478bd9Sstevel@tonic-gate 	 * later.
3996*7c478bd9Sstevel@tonic-gate 	 */
3997*7c478bd9Sstevel@tonic-gate 	killall(zone->zone_id);
3998*7c478bd9Sstevel@tonic-gate 	/*
3999*7c478bd9Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
4000*7c478bd9Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
4001*7c478bd9Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
4002*7c478bd9Sstevel@tonic-gate 	 */
4003*7c478bd9Sstevel@tonic-gate 	zargp = kmem_alloc(sizeof (*zargp), KM_SLEEP);
4004*7c478bd9Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
4005*7c478bd9Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
4006*7c478bd9Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
4007*7c478bd9Sstevel@tonic-gate 	zone_hold(zargp->zone = zone);
4008*7c478bd9Sstevel@tonic-gate 
4009*7c478bd9Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
4010*7c478bd9Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
4011*7c478bd9Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
4012*7c478bd9Sstevel@tonic-gate 
4013*7c478bd9Sstevel@tonic-gate 	return (EINVAL);
4014*7c478bd9Sstevel@tonic-gate }
4015*7c478bd9Sstevel@tonic-gate 
4016*7c478bd9Sstevel@tonic-gate /*
4017*7c478bd9Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
4018*7c478bd9Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
4019*7c478bd9Sstevel@tonic-gate  */
4020*7c478bd9Sstevel@tonic-gate void
4021*7c478bd9Sstevel@tonic-gate zone_shutdown_global(void)
4022*7c478bd9Sstevel@tonic-gate {
4023*7c478bd9Sstevel@tonic-gate 	ASSERT(curproc->p_zone == global_zone);
4024*7c478bd9Sstevel@tonic-gate 
4025*7c478bd9Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
4026*7c478bd9Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
4027*7c478bd9Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
4028*7c478bd9Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
4029*7c478bd9Sstevel@tonic-gate }
4030