xref: /onnv-gate/usr/src/uts/common/os/zone.c (revision 766:c521de78a32f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22390Sraf 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Zones
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  *   A zone is a named collection of processes, namespace constraints,
340Sstevel@tonic-gate  *   and other system resources which comprise a secure and manageable
350Sstevel@tonic-gate  *   application containment facility.
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *   Zones (represented by the reference counted zone_t) are tracked in
380Sstevel@tonic-gate  *   the kernel in the zonehash.  Elsewhere in the kernel, Zone IDs
390Sstevel@tonic-gate  *   (zoneid_t) are used to track zone association.  Zone IDs are
400Sstevel@tonic-gate  *   dynamically generated when the zone is created; if a persistent
410Sstevel@tonic-gate  *   identifier is needed (core files, accounting logs, audit trail,
420Sstevel@tonic-gate  *   etc.), the zone name should be used.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  *   Global Zone:
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  *   The global zone (zoneid 0) is automatically associated with all
480Sstevel@tonic-gate  *   system resources that have not been bound to a user-created zone.
490Sstevel@tonic-gate  *   This means that even systems where zones are not in active use
500Sstevel@tonic-gate  *   have a global zone, and all processes, mounts, etc. are
510Sstevel@tonic-gate  *   associated with that zone.  The global zone is generally
520Sstevel@tonic-gate  *   unconstrained in terms of privileges and access, though the usual
530Sstevel@tonic-gate  *   credential and privilege based restrictions apply.
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  *   Zone States:
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  *   The states in which a zone may be in and the transitions are as
590Sstevel@tonic-gate  *   follows:
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  *   ZONE_IS_UNINITIALIZED: primordial state for a zone. The partially
620Sstevel@tonic-gate  *   initialized zone is added to the list of active zones on the system but
630Sstevel@tonic-gate  *   isn't accessible.
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  *   ZONE_IS_READY: zsched (the kernel dummy process for a zone) is
660Sstevel@tonic-gate  *   ready.  The zone is made visible after the ZSD constructor callbacks are
670Sstevel@tonic-gate  *   executed.  A zone remains in this state until it transitions into
680Sstevel@tonic-gate  *   the ZONE_IS_BOOTING state as a result of a call to zone_boot().
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  *   ZONE_IS_BOOTING: in this shortlived-state, zsched attempts to start
710Sstevel@tonic-gate  *   init.  Should that fail, the zone proceeds to the ZONE_IS_SHUTTING_DOWN
720Sstevel@tonic-gate  *   state.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  *   ZONE_IS_RUNNING: The zone is open for business: zsched has
750Sstevel@tonic-gate  *   successfully started init.   A zone remains in this state until
760Sstevel@tonic-gate  *   zone_shutdown() is called.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN: zone_shutdown() has been called, the system is
790Sstevel@tonic-gate  *   killing all processes running in the zone. The zone remains
800Sstevel@tonic-gate  *   in this state until there are no more user processes running in the zone.
810Sstevel@tonic-gate  *   zone_create(), zone_enter(), and zone_destroy() on this zone will fail.
820Sstevel@tonic-gate  *   Since zone_shutdown() is restartable, it may be called successfully
830Sstevel@tonic-gate  *   multiple times for the same zone_t.  Setting of the zone's state to
840Sstevel@tonic-gate  *   ZONE_IS_SHUTTING_DOWN is synchronized with mounts, so VOP_MOUNT() may check
850Sstevel@tonic-gate  *   the zone's status without worrying about it being a moving target.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  *   ZONE_IS_EMPTY: zone_shutdown() has been called, and there
880Sstevel@tonic-gate  *   are no more user processes in the zone.  The zone remains in this
890Sstevel@tonic-gate  *   state until there are no more kernel threads associated with the
900Sstevel@tonic-gate  *   zone.  zone_create(), zone_enter(), and zone_destroy() on this zone will
910Sstevel@tonic-gate  *   fail.
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  *   ZONE_IS_DOWN: All kernel threads doing work on behalf of the zone
940Sstevel@tonic-gate  *   have exited.  zone_shutdown() returns.  Henceforth it is not possible to
950Sstevel@tonic-gate  *   join the zone or create kernel threads therein.
960Sstevel@tonic-gate  *
970Sstevel@tonic-gate  *   ZONE_IS_DYING: zone_destroy() has been called on the zone; zone
980Sstevel@tonic-gate  *   remains in this state until zsched exits.  Calls to zone_find_by_*()
990Sstevel@tonic-gate  *   return NULL from now on.
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  *   ZONE_IS_DEAD: zsched has exited (zone_ntasks == 0).  There are no
1020Sstevel@tonic-gate  *   processes or threads doing work on behalf of the zone.  The zone is
1030Sstevel@tonic-gate  *   removed from the list of active zones.  zone_destroy() returns, and
1040Sstevel@tonic-gate  *   the zone can be recreated.
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  *   ZONE_IS_FREE (internal state): zone_ref goes to 0, ZSD destructor
1070Sstevel@tonic-gate  *   callbacks are executed, and all memory associated with the zone is
1080Sstevel@tonic-gate  *   freed.
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  *   Threads can wait for the zone to enter a requested state by using
1110Sstevel@tonic-gate  *   zone_status_wait() or zone_status_timedwait() with the desired
1120Sstevel@tonic-gate  *   state passed in as an argument.  Zone state transitions are
1130Sstevel@tonic-gate  *   uni-directional; it is not possible to move back to an earlier state.
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  *   Zone-Specific Data:
1170Sstevel@tonic-gate  *
1180Sstevel@tonic-gate  *   Subsystems needing to maintain zone-specific data can store that
1190Sstevel@tonic-gate  *   data using the ZSD mechanism.  This provides a zone-specific data
1200Sstevel@tonic-gate  *   store, similar to thread-specific data (see pthread_getspecific(3C)
1210Sstevel@tonic-gate  *   or the TSD code in uts/common/disp/thread.c.  Also, ZSD can be used
1220Sstevel@tonic-gate  *   to register callbacks to be invoked when a zone is created, shut
1230Sstevel@tonic-gate  *   down, or destroyed.  This can be used to initialize zone-specific
1240Sstevel@tonic-gate  *   data for new zones and to clean up when zones go away.
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  *   Data Structures:
1280Sstevel@tonic-gate  *
1290Sstevel@tonic-gate  *   The per-zone structure (zone_t) is reference counted, and freed
1300Sstevel@tonic-gate  *   when all references are released.  zone_hold and zone_rele can be
1310Sstevel@tonic-gate  *   used to adjust the reference count.  In addition, reference counts
1320Sstevel@tonic-gate  *   associated with the cred_t structure are tracked separately using
1330Sstevel@tonic-gate  *   zone_cred_hold and zone_cred_rele.
1340Sstevel@tonic-gate  *
1350Sstevel@tonic-gate  *   Pointers to active zone_t's are stored in two hash tables; one
1360Sstevel@tonic-gate  *   for searching by id, the other for searching by name.  Lookups
1370Sstevel@tonic-gate  *   can be performed on either basis, using zone_find_by_id and
1380Sstevel@tonic-gate  *   zone_find_by_name.  Both return zone_t pointers with the zone
1390Sstevel@tonic-gate  *   held, so zone_rele should be called when the pointer is no longer
1400Sstevel@tonic-gate  *   needed.  Zones can also be searched by path; zone_find_by_path
1410Sstevel@tonic-gate  *   returns the zone with which a path name is associated (global
1420Sstevel@tonic-gate  *   zone if the path is not within some other zone's file system
1430Sstevel@tonic-gate  *   hierarchy).  This currently requires iterating through each zone,
1440Sstevel@tonic-gate  *   so it is slower than an id or name search via a hash table.
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  *   Locking:
1480Sstevel@tonic-gate  *
1490Sstevel@tonic-gate  *   zonehash_lock: This is a top-level global lock used to protect the
1500Sstevel@tonic-gate  *       zone hash tables and lists.  Zones cannot be created or destroyed
1510Sstevel@tonic-gate  *       while this lock is held.
1520Sstevel@tonic-gate  *   zone_status_lock: This is a global lock protecting zone state.
1530Sstevel@tonic-gate  *       Zones cannot change state while this lock is held.  It also
1540Sstevel@tonic-gate  *       protects the list of kernel threads associated with a zone.
1550Sstevel@tonic-gate  *   zone_lock: This is a per-zone lock used to protect several fields of
1560Sstevel@tonic-gate  *       the zone_t (see <sys/zone.h> for details).  In addition, holding
1570Sstevel@tonic-gate  *       this lock means that the zone cannot go away.
1580Sstevel@tonic-gate  *   zsd_key_lock: This is a global lock protecting the key state for ZSD.
1590Sstevel@tonic-gate  *   zone_deathrow_lock: This is a global lock protecting the "deathrow"
1600Sstevel@tonic-gate  *       list (a list of zones in the ZONE_IS_DEAD state).
1610Sstevel@tonic-gate  *
1620Sstevel@tonic-gate  *   Ordering requirements:
1630Sstevel@tonic-gate  *       pool_lock --> cpu_lock --> zonehash_lock --> zone_status_lock -->
1640Sstevel@tonic-gate  *       	zone_lock --> zsd_key_lock --> pidlock --> p_lock
1650Sstevel@tonic-gate  *
1660Sstevel@tonic-gate  *   Blocking memory allocations are permitted while holding any of the
1670Sstevel@tonic-gate  *   zone locks.
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  *
1700Sstevel@tonic-gate  *   System Call Interface:
1710Sstevel@tonic-gate  *
1720Sstevel@tonic-gate  *   The zone subsystem can be managed and queried from user level with
1730Sstevel@tonic-gate  *   the following system calls (all subcodes of the primary "zone"
1740Sstevel@tonic-gate  *   system call):
1750Sstevel@tonic-gate  *   - zone_create: creates a zone with selected attributes (name,
1760Sstevel@tonic-gate  *     root path, privileges, resource controls)
1770Sstevel@tonic-gate  *   - zone_enter: allows the current process to enter a zone
1780Sstevel@tonic-gate  *   - zone_getattr: reports attributes of a zone
1790Sstevel@tonic-gate  *   - zone_list: lists all zones active in the system
1800Sstevel@tonic-gate  *   - zone_lookup: looks up zone id based on name
1810Sstevel@tonic-gate  *   - zone_shutdown: initiates shutdown process (see states above)
1820Sstevel@tonic-gate  *   - zone_destroy: completes shutdown process (see states above)
1830Sstevel@tonic-gate  *
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate #include <sys/priv_impl.h>
1870Sstevel@tonic-gate #include <sys/cred.h>
1880Sstevel@tonic-gate #include <c2/audit.h>
1890Sstevel@tonic-gate #include <sys/ddi.h>
1900Sstevel@tonic-gate #include <sys/debug.h>
1910Sstevel@tonic-gate #include <sys/file.h>
1920Sstevel@tonic-gate #include <sys/kmem.h>
1930Sstevel@tonic-gate #include <sys/mutex.h>
1940Sstevel@tonic-gate #include <sys/pathname.h>
1950Sstevel@tonic-gate #include <sys/proc.h>
1960Sstevel@tonic-gate #include <sys/project.h>
1970Sstevel@tonic-gate #include <sys/task.h>
1980Sstevel@tonic-gate #include <sys/systm.h>
1990Sstevel@tonic-gate #include <sys/types.h>
2000Sstevel@tonic-gate #include <sys/utsname.h>
2010Sstevel@tonic-gate #include <sys/vnode.h>
2020Sstevel@tonic-gate #include <sys/vfs.h>
2030Sstevel@tonic-gate #include <sys/systeminfo.h>
2040Sstevel@tonic-gate #include <sys/policy.h>
2050Sstevel@tonic-gate #include <sys/cred_impl.h>
2060Sstevel@tonic-gate #include <sys/contract_impl.h>
2070Sstevel@tonic-gate #include <sys/contract/process_impl.h>
2080Sstevel@tonic-gate #include <sys/class.h>
2090Sstevel@tonic-gate #include <sys/pool.h>
2100Sstevel@tonic-gate #include <sys/pool_pset.h>
2110Sstevel@tonic-gate #include <sys/pset.h>
2120Sstevel@tonic-gate #include <sys/log.h>
2130Sstevel@tonic-gate #include <sys/sysmacros.h>
2140Sstevel@tonic-gate #include <sys/callb.h>
2150Sstevel@tonic-gate #include <sys/vmparam.h>
2160Sstevel@tonic-gate #include <sys/corectl.h>
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate #include <sys/door.h>
2190Sstevel@tonic-gate #include <sys/cpuvar.h>
2200Sstevel@tonic-gate #include <sys/fs/snode.h>
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate #include <sys/uadmin.h>
2230Sstevel@tonic-gate #include <sys/session.h>
2240Sstevel@tonic-gate #include <sys/cmn_err.h>
2250Sstevel@tonic-gate #include <sys/modhash.h>
2260Sstevel@tonic-gate #include <sys/nvpair.h>
2270Sstevel@tonic-gate #include <sys/rctl.h>
2280Sstevel@tonic-gate #include <sys/fss.h>
2290Sstevel@tonic-gate #include <sys/zone.h>
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate  * cv used to signal that all references to the zone have been released.  This
2330Sstevel@tonic-gate  * needs to be global since there may be multiple waiters, and the first to
2340Sstevel@tonic-gate  * wake up will free the zone_t, hence we cannot use zone->zone_cv.
2350Sstevel@tonic-gate  */
2360Sstevel@tonic-gate static kcondvar_t zone_destroy_cv;
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  * Lock used to serialize access to zone_cv.  This could have been per-zone,
2390Sstevel@tonic-gate  * but then we'd need another lock for zone_destroy_cv, and why bother?
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate static kmutex_t zone_status_lock;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * ZSD-related global variables.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate static kmutex_t zsd_key_lock;	/* protects the following two */
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * The next caller of zone_key_create() will be assigned a key of ++zsd_keyval.
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate static zone_key_t zsd_keyval = 0;
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * Global list of registered keys.  We use this when a new zone is created.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate static list_t zsd_registered_keys;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate int zone_hash_size = 256;
2570Sstevel@tonic-gate static mod_hash_t *zonehashbyname, *zonehashbyid;
2580Sstevel@tonic-gate static kmutex_t zonehash_lock;
2590Sstevel@tonic-gate static uint_t zonecount;
2600Sstevel@tonic-gate static id_space_t *zoneid_space;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * The global zone (aka zone0) is the all-seeing, all-knowing zone in which the
2640Sstevel@tonic-gate  * kernel proper runs, and which manages all other zones.
2650Sstevel@tonic-gate  *
2660Sstevel@tonic-gate  * Although not declared as static, the variable "zone0" should not be used
2670Sstevel@tonic-gate  * except for by code that needs to reference the global zone early on in boot,
2680Sstevel@tonic-gate  * before it is fully initialized.  All other consumers should use
2690Sstevel@tonic-gate  * 'global_zone'.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate zone_t zone0;
2720Sstevel@tonic-gate zone_t *global_zone = NULL;	/* Set when the global zone is initialized */
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate  * List of active zones, protected by zonehash_lock.
2760Sstevel@tonic-gate  */
2770Sstevel@tonic-gate static list_t zone_active;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  * List of destroyed zones that still have outstanding cred references.
2810Sstevel@tonic-gate  * Used for debugging.  Uses a separate lock to avoid lock ordering
2820Sstevel@tonic-gate  * problems in zone_free.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate static list_t zone_deathrow;
2850Sstevel@tonic-gate static kmutex_t zone_deathrow_lock;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /* number of zones is limited by virtual interface limit in IP */
2880Sstevel@tonic-gate uint_t maxzones = 8192;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate  * This isn't static so lint doesn't complain.
2920Sstevel@tonic-gate  */
2930Sstevel@tonic-gate rctl_hndl_t rc_zone_cpu_shares;
2940Sstevel@tonic-gate rctl_hndl_t rc_zone_nlwps;
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate  * Synchronization primitives used to synchronize between mounts and zone
2970Sstevel@tonic-gate  * creation/destruction.
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate static int mounts_in_progress;
3000Sstevel@tonic-gate static kcondvar_t mount_cv;
3010Sstevel@tonic-gate static kmutex_t mount_lock;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate const char * const zone_initname = "/sbin/init";
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate static int zone_shutdown(zoneid_t zoneid);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * Certain filesystems (such as NFS and autofs) need to know which zone
3090Sstevel@tonic-gate  * the mount is being placed in.  Because of this, we need to be able to
3100Sstevel@tonic-gate  * ensure that a zone isn't in the process of being created such that
3110Sstevel@tonic-gate  * nfs_mount() thinks it is in the global zone, while by the time it
3120Sstevel@tonic-gate  * gets added the list of mounted zones, it ends up on zoneA's mount
3130Sstevel@tonic-gate  * list.
3140Sstevel@tonic-gate  *
3150Sstevel@tonic-gate  * The following functions: block_mounts()/resume_mounts() and
3160Sstevel@tonic-gate  * mount_in_progress()/mount_completed() are used by zones and the VFS
3170Sstevel@tonic-gate  * layer (respectively) to synchronize zone creation and new mounts.
3180Sstevel@tonic-gate  *
3190Sstevel@tonic-gate  * The semantics are like a reader-reader lock such that there may
3200Sstevel@tonic-gate  * either be multiple mounts (or zone creations, if that weren't
3210Sstevel@tonic-gate  * serialized by zonehash_lock) in progress at the same time, but not
3220Sstevel@tonic-gate  * both.
3230Sstevel@tonic-gate  *
3240Sstevel@tonic-gate  * We use cv's so the user can ctrl-C out of the operation if it's
3250Sstevel@tonic-gate  * taking too long.
3260Sstevel@tonic-gate  *
3270Sstevel@tonic-gate  * The semantics are such that there is unfair bias towards the
3280Sstevel@tonic-gate  * "current" operation.  This means that zone creations may starve if
3290Sstevel@tonic-gate  * there is a rapid succession of new mounts coming in to the system, or
3300Sstevel@tonic-gate  * there is a remote possibility that zones will be created at such a
3310Sstevel@tonic-gate  * rate that new mounts will not be able to proceed.
3320Sstevel@tonic-gate  */
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate  * Prevent new mounts from progressing to the point of calling
3350Sstevel@tonic-gate  * VFS_MOUNT().  If there are already mounts in this "region", wait for
3360Sstevel@tonic-gate  * them to complete.
3370Sstevel@tonic-gate  */
3380Sstevel@tonic-gate static int
3390Sstevel@tonic-gate block_mounts(void)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	int retval = 0;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * Since it may block for a long time, block_mounts() shouldn't be
3450Sstevel@tonic-gate 	 * called with zonehash_lock held.
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
3480Sstevel@tonic-gate 	mutex_enter(&mount_lock);
3490Sstevel@tonic-gate 	while (mounts_in_progress > 0) {
3500Sstevel@tonic-gate 		if (cv_wait_sig(&mount_cv, &mount_lock) == 0)
3510Sstevel@tonic-gate 			goto signaled;
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 	/*
3540Sstevel@tonic-gate 	 * A negative value of mounts_in_progress indicates that mounts
3550Sstevel@tonic-gate 	 * have been blocked by (-mounts_in_progress) different callers.
3560Sstevel@tonic-gate 	 */
3570Sstevel@tonic-gate 	mounts_in_progress--;
3580Sstevel@tonic-gate 	retval = 1;
3590Sstevel@tonic-gate signaled:
3600Sstevel@tonic-gate 	mutex_exit(&mount_lock);
3610Sstevel@tonic-gate 	return (retval);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate  * The VFS layer may progress with new mounts as far as we're concerned.
3660Sstevel@tonic-gate  * Allow them to progress if we were the last obstacle.
3670Sstevel@tonic-gate  */
3680Sstevel@tonic-gate static void
3690Sstevel@tonic-gate resume_mounts(void)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	mutex_enter(&mount_lock);
3720Sstevel@tonic-gate 	if (++mounts_in_progress == 0)
3730Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
3740Sstevel@tonic-gate 	mutex_exit(&mount_lock);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * The VFS layer is busy with a mount; zones should wait until all
3790Sstevel@tonic-gate  * mounts are completed to progress.
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate void
3820Sstevel@tonic-gate mount_in_progress(void)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	mutex_enter(&mount_lock);
3850Sstevel@tonic-gate 	while (mounts_in_progress < 0)
3860Sstevel@tonic-gate 		cv_wait(&mount_cv, &mount_lock);
3870Sstevel@tonic-gate 	mounts_in_progress++;
3880Sstevel@tonic-gate 	mutex_exit(&mount_lock);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * VFS is done with one mount; wake up any waiting block_mounts()
3930Sstevel@tonic-gate  * callers if this is the last mount.
3940Sstevel@tonic-gate  */
3950Sstevel@tonic-gate void
3960Sstevel@tonic-gate mount_completed(void)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate 	mutex_enter(&mount_lock);
3990Sstevel@tonic-gate 	if (--mounts_in_progress == 0)
4000Sstevel@tonic-gate 		cv_broadcast(&mount_cv);
4010Sstevel@tonic-gate 	mutex_exit(&mount_lock);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate  * ZSD routines.
4060Sstevel@tonic-gate  *
4070Sstevel@tonic-gate  * Zone Specific Data (ZSD) is modeled after Thread Specific Data as
4080Sstevel@tonic-gate  * defined by the pthread_key_create() and related interfaces.
4090Sstevel@tonic-gate  *
4100Sstevel@tonic-gate  * Kernel subsystems may register one or more data items and/or
4110Sstevel@tonic-gate  * callbacks to be executed when a zone is created, shutdown, or
4120Sstevel@tonic-gate  * destroyed.
4130Sstevel@tonic-gate  *
4140Sstevel@tonic-gate  * Unlike the thread counterpart, destructor callbacks will be executed
4150Sstevel@tonic-gate  * even if the data pointer is NULL and/or there are no constructor
4160Sstevel@tonic-gate  * callbacks, so it is the responsibility of such callbacks to check for
4170Sstevel@tonic-gate  * NULL data values if necessary.
4180Sstevel@tonic-gate  *
4190Sstevel@tonic-gate  * The locking strategy and overall picture is as follows:
4200Sstevel@tonic-gate  *
4210Sstevel@tonic-gate  * When someone calls zone_key_create(), a template ZSD entry is added to the
4220Sstevel@tonic-gate  * global list "zsd_registered_keys", protected by zsd_key_lock.  The
4230Sstevel@tonic-gate  * constructor callback is called immediately on all existing zones, and a
4240Sstevel@tonic-gate  * copy of the ZSD entry added to the per-zone zone_zsd list (protected by
4250Sstevel@tonic-gate  * zone_lock).  As this operation requires the list of zones, the list of
4260Sstevel@tonic-gate  * registered keys, and the per-zone list of ZSD entries to remain constant
4270Sstevel@tonic-gate  * throughout the entire operation, it must grab zonehash_lock, zone_lock for
4280Sstevel@tonic-gate  * all existing zones, and zsd_key_lock, in that order.  Similar locking is
4290Sstevel@tonic-gate  * needed when zone_key_delete() is called.  It is thus sufficient to hold
4300Sstevel@tonic-gate  * zsd_key_lock *or* zone_lock to prevent additions to or removals from the
4310Sstevel@tonic-gate  * per-zone zone_zsd list.
4320Sstevel@tonic-gate  *
4330Sstevel@tonic-gate  * Note that this implementation does not make a copy of the ZSD entry if a
4340Sstevel@tonic-gate  * constructor callback is not provided.  A zone_getspecific() on such an
4350Sstevel@tonic-gate  * uninitialized ZSD entry will return NULL.
4360Sstevel@tonic-gate  *
4370Sstevel@tonic-gate  * When new zones are created constructor callbacks for all registered ZSD
4380Sstevel@tonic-gate  * entries will be called.
4390Sstevel@tonic-gate  *
4400Sstevel@tonic-gate  * The framework does not provide any locking around zone_getspecific() and
4410Sstevel@tonic-gate  * zone_setspecific() apart from that needed for internal consistency, so
4420Sstevel@tonic-gate  * callers interested in atomic "test-and-set" semantics will need to provide
4430Sstevel@tonic-gate  * their own locking.
4440Sstevel@tonic-gate  */
4450Sstevel@tonic-gate void
4460Sstevel@tonic-gate zone_key_create(zone_key_t *keyp, void *(*create)(zoneid_t),
4470Sstevel@tonic-gate     void (*shutdown)(zoneid_t, void *), void (*destroy)(zoneid_t, void *))
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate 	struct zsd_entry *zsdp;
4500Sstevel@tonic-gate 	struct zsd_entry *t;
4510Sstevel@tonic-gate 	struct zone *zone;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	zsdp = kmem_alloc(sizeof (*zsdp), KM_SLEEP);
4540Sstevel@tonic-gate 	zsdp->zsd_data = NULL;
4550Sstevel@tonic-gate 	zsdp->zsd_create = create;
4560Sstevel@tonic-gate 	zsdp->zsd_shutdown = shutdown;
4570Sstevel@tonic-gate 	zsdp->zsd_destroy = destroy;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* stop the world */
4600Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
4610Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
4620Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
4650Sstevel@tonic-gate 	*keyp = zsdp->zsd_key = ++zsd_keyval;
4660Sstevel@tonic-gate 	ASSERT(zsd_keyval != 0);
4670Sstevel@tonic-gate 	list_insert_tail(&zsd_registered_keys, zsdp);
4680Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (create != NULL) {
4710Sstevel@tonic-gate 		for (zone = list_head(&zone_active); zone != NULL;
4720Sstevel@tonic-gate 		    zone = list_next(&zone_active, zone)) {
4730Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
4740Sstevel@tonic-gate 			t->zsd_key = *keyp;
4750Sstevel@tonic-gate 			t->zsd_data = (*create)(zone->zone_id);
4760Sstevel@tonic-gate 			t->zsd_create = create;
4770Sstevel@tonic-gate 			t->zsd_shutdown = shutdown;
4780Sstevel@tonic-gate 			t->zsd_destroy = destroy;
4790Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
4830Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
4840Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
4850Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate  * Helper function to find the zsd_entry associated with the key in the
4900Sstevel@tonic-gate  * given list.
4910Sstevel@tonic-gate  */
4920Sstevel@tonic-gate static struct zsd_entry *
4930Sstevel@tonic-gate zsd_find(list_t *l, zone_key_t key)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	struct zsd_entry *zsd;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	for (zsd = list_head(l); zsd != NULL; zsd = list_next(l, zsd)) {
4980Sstevel@tonic-gate 		if (zsd->zsd_key == key) {
4990Sstevel@tonic-gate 			/*
5000Sstevel@tonic-gate 			 * Move to head of list to keep list in MRU order.
5010Sstevel@tonic-gate 			 */
5020Sstevel@tonic-gate 			if (zsd != list_head(l)) {
5030Sstevel@tonic-gate 				list_remove(l, zsd);
5040Sstevel@tonic-gate 				list_insert_head(l, zsd);
5050Sstevel@tonic-gate 			}
5060Sstevel@tonic-gate 			return (zsd);
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 	return (NULL);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate  * Function called when a module is being unloaded, or otherwise wishes
5140Sstevel@tonic-gate  * to unregister its ZSD key and callbacks.
5150Sstevel@tonic-gate  */
5160Sstevel@tonic-gate int
5170Sstevel@tonic-gate zone_key_delete(zone_key_t key)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
5200Sstevel@tonic-gate 	zone_t *zone;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);	/* Zone create/delete waits for us */
5230Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5240Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5250Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);	/* lock all zones */
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5280Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
5290Sstevel@tonic-gate 	if (zsdp == NULL)
5300Sstevel@tonic-gate 		goto notfound;
5310Sstevel@tonic-gate 	list_remove(&zsd_registered_keys, zsdp);
5320Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5350Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
5360Sstevel@tonic-gate 		struct zsd_entry *del;
5370Sstevel@tonic-gate 		void *data;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		if (!(zone->zone_flags & ZF_DESTROYED)) {
5400Sstevel@tonic-gate 			del = zsd_find(&zone->zone_zsd, key);
5410Sstevel@tonic-gate 			if (del != NULL) {
5420Sstevel@tonic-gate 				data = del->zsd_data;
5430Sstevel@tonic-gate 				ASSERT(del->zsd_shutdown == zsdp->zsd_shutdown);
5440Sstevel@tonic-gate 				ASSERT(del->zsd_destroy == zsdp->zsd_destroy);
5450Sstevel@tonic-gate 				list_remove(&zone->zone_zsd, del);
5460Sstevel@tonic-gate 				kmem_free(del, sizeof (*del));
5470Sstevel@tonic-gate 			} else {
5480Sstevel@tonic-gate 				data = NULL;
5490Sstevel@tonic-gate 			}
5500Sstevel@tonic-gate 			if (zsdp->zsd_shutdown)
5510Sstevel@tonic-gate 				zsdp->zsd_shutdown(zone->zone_id, data);
5520Sstevel@tonic-gate 			if (zsdp->zsd_destroy)
5530Sstevel@tonic-gate 				zsdp->zsd_destroy(zone->zone_id, data);
5540Sstevel@tonic-gate 		}
5550Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
5580Sstevel@tonic-gate 	kmem_free(zsdp, sizeof (*zsdp));
5590Sstevel@tonic-gate 	return (0);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate notfound:
5620Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
5630Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
5640Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone))
5650Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5660Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
5670Sstevel@tonic-gate 	return (-1);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate /*
5710Sstevel@tonic-gate  * ZSD counterpart of pthread_setspecific().
5720Sstevel@tonic-gate  */
5730Sstevel@tonic-gate int
5740Sstevel@tonic-gate zone_setspecific(zone_key_t key, zone_t *zone, const void *data)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	struct zsd_entry *t;
5770Sstevel@tonic-gate 	struct zsd_entry *zsdp = NULL;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
5800Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
5810Sstevel@tonic-gate 	if (t != NULL) {
5820Sstevel@tonic-gate 		/*
5830Sstevel@tonic-gate 		 * Replace old value with new
5840Sstevel@tonic-gate 		 */
5850Sstevel@tonic-gate 		t->zsd_data = (void *)data;
5860Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
5870Sstevel@tonic-gate 		return (0);
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 	/*
5900Sstevel@tonic-gate 	 * If there was no previous value, go through the list of registered
5910Sstevel@tonic-gate 	 * keys.
5920Sstevel@tonic-gate 	 *
5930Sstevel@tonic-gate 	 * We avoid grabbing zsd_key_lock until we are sure we need it; this is
5940Sstevel@tonic-gate 	 * necessary for shutdown callbacks to be able to execute without fear
5950Sstevel@tonic-gate 	 * of deadlock.
5960Sstevel@tonic-gate 	 */
5970Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
5980Sstevel@tonic-gate 	zsdp = zsd_find(&zsd_registered_keys, key);
5990Sstevel@tonic-gate 	if (zsdp == NULL) { 	/* Key was not registered */
6000Sstevel@tonic-gate 		mutex_exit(&zsd_key_lock);
6010Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
6020Sstevel@tonic-gate 		return (-1);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	/*
6060Sstevel@tonic-gate 	 * Add a zsd_entry to this zone, using the template we just retrieved
6070Sstevel@tonic-gate 	 * to initialize the constructor and destructor(s).
6080Sstevel@tonic-gate 	 */
6090Sstevel@tonic-gate 	t = kmem_alloc(sizeof (*t), KM_SLEEP);
6100Sstevel@tonic-gate 	t->zsd_key = key;
6110Sstevel@tonic-gate 	t->zsd_data = (void *)data;
6120Sstevel@tonic-gate 	t->zsd_create = zsdp->zsd_create;
6130Sstevel@tonic-gate 	t->zsd_shutdown = zsdp->zsd_shutdown;
6140Sstevel@tonic-gate 	t->zsd_destroy = zsdp->zsd_destroy;
6150Sstevel@tonic-gate 	list_insert_tail(&zone->zone_zsd, t);
6160Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6170Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
6180Sstevel@tonic-gate 	return (0);
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate /*
6220Sstevel@tonic-gate  * ZSD counterpart of pthread_getspecific().
6230Sstevel@tonic-gate  */
6240Sstevel@tonic-gate void *
6250Sstevel@tonic-gate zone_getspecific(zone_key_t key, zone_t *zone)
6260Sstevel@tonic-gate {
6270Sstevel@tonic-gate 	struct zsd_entry *t;
6280Sstevel@tonic-gate 	void *data;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6310Sstevel@tonic-gate 	t = zsd_find(&zone->zone_zsd, key);
6320Sstevel@tonic-gate 	data = (t == NULL ? NULL : t->zsd_data);
6330Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
6340Sstevel@tonic-gate 	return (data);
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate  * Function used to initialize a zone's list of ZSD callbacks and data
6390Sstevel@tonic-gate  * when the zone is being created.  The callbacks are initialized from
6400Sstevel@tonic-gate  * the template list (zsd_registered_keys), and the constructor
6410Sstevel@tonic-gate  * callback executed (if one exists).
6420Sstevel@tonic-gate  *
6430Sstevel@tonic-gate  * This is called before the zone is made publicly available, hence no
6440Sstevel@tonic-gate  * need to grab zone_lock.
6450Sstevel@tonic-gate  *
6460Sstevel@tonic-gate  * Although we grab and release zsd_key_lock, new entries cannot be
6470Sstevel@tonic-gate  * added to or removed from the zsd_registered_keys list until we
6480Sstevel@tonic-gate  * release zonehash_lock, so there isn't a window for a
6490Sstevel@tonic-gate  * zone_key_create() to come in after we've dropped zsd_key_lock but
6500Sstevel@tonic-gate  * before the zone is added to the zone list, such that the constructor
6510Sstevel@tonic-gate  * callbacks aren't executed for the new zone.
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate static void
6540Sstevel@tonic-gate zone_zsd_configure(zone_t *zone)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate 	struct zsd_entry *zsdp;
6570Sstevel@tonic-gate 	struct zsd_entry *t;
6580Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
6610Sstevel@tonic-gate 	ASSERT(list_head(&zone->zone_zsd) == NULL);
6620Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
6630Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
6640Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
6650Sstevel@tonic-gate 		if (zsdp->zsd_create != NULL) {
6660Sstevel@tonic-gate 			t = kmem_alloc(sizeof (*t), KM_SLEEP);
6670Sstevel@tonic-gate 			t->zsd_key = zsdp->zsd_key;
6680Sstevel@tonic-gate 			t->zsd_create = zsdp->zsd_create;
6690Sstevel@tonic-gate 			t->zsd_data = (*t->zsd_create)(zoneid);
6700Sstevel@tonic-gate 			t->zsd_shutdown = zsdp->zsd_shutdown;
6710Sstevel@tonic-gate 			t->zsd_destroy = zsdp->zsd_destroy;
6720Sstevel@tonic-gate 			list_insert_tail(&zone->zone_zsd, t);
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate enum zsd_callback_type { ZSD_CREATE, ZSD_SHUTDOWN, ZSD_DESTROY };
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate /*
6810Sstevel@tonic-gate  * Helper function to execute shutdown or destructor callbacks.
6820Sstevel@tonic-gate  */
6830Sstevel@tonic-gate static void
6840Sstevel@tonic-gate zone_zsd_callbacks(zone_t *zone, enum zsd_callback_type ct)
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate 	struct zsd_entry *zsdp;
6870Sstevel@tonic-gate 	struct zsd_entry *t;
6880Sstevel@tonic-gate 	zoneid_t zoneid = zone->zone_id;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	ASSERT(ct == ZSD_SHUTDOWN || ct == ZSD_DESTROY);
6910Sstevel@tonic-gate 	ASSERT(ct != ZSD_SHUTDOWN || zone_status_get(zone) >= ZONE_IS_EMPTY);
6920Sstevel@tonic-gate 	ASSERT(ct != ZSD_DESTROY || zone_status_get(zone) >= ZONE_IS_DOWN);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
6950Sstevel@tonic-gate 	if (ct == ZSD_DESTROY) {
6960Sstevel@tonic-gate 		if (zone->zone_flags & ZF_DESTROYED) {
6970Sstevel@tonic-gate 			/*
6980Sstevel@tonic-gate 			 * Make sure destructors are only called once.
6990Sstevel@tonic-gate 			 */
7000Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
7010Sstevel@tonic-gate 			return;
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 		zone->zone_flags |= ZF_DESTROYED;
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	/*
7080Sstevel@tonic-gate 	 * Both zsd_key_lock and zone_lock need to be held in order to add or
7090Sstevel@tonic-gate 	 * remove a ZSD key, (either globally as part of
7100Sstevel@tonic-gate 	 * zone_key_create()/zone_key_delete(), or on a per-zone basis, as is
7110Sstevel@tonic-gate 	 * possible through zone_setspecific()), so it's sufficient to hold
7120Sstevel@tonic-gate 	 * zsd_key_lock here.
7130Sstevel@tonic-gate 	 *
7140Sstevel@tonic-gate 	 * This is a good thing, since we don't want to recursively try to grab
7150Sstevel@tonic-gate 	 * zone_lock if a callback attempts to do something like a crfree() or
7160Sstevel@tonic-gate 	 * zone_rele().
7170Sstevel@tonic-gate 	 */
7180Sstevel@tonic-gate 	mutex_enter(&zsd_key_lock);
7190Sstevel@tonic-gate 	for (zsdp = list_head(&zsd_registered_keys); zsdp != NULL;
7200Sstevel@tonic-gate 	    zsdp = list_next(&zsd_registered_keys, zsdp)) {
7210Sstevel@tonic-gate 		zone_key_t key = zsdp->zsd_key;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		/* Skip if no callbacks registered */
7240Sstevel@tonic-gate 		if (ct == ZSD_SHUTDOWN && zsdp->zsd_shutdown == NULL)
7250Sstevel@tonic-gate 			continue;
7260Sstevel@tonic-gate 		if (ct == ZSD_DESTROY && zsdp->zsd_destroy == NULL)
7270Sstevel@tonic-gate 			continue;
7280Sstevel@tonic-gate 		/*
7290Sstevel@tonic-gate 		 * Call the callback with the zone-specific data if we can find
7300Sstevel@tonic-gate 		 * any, otherwise with NULL.
7310Sstevel@tonic-gate 		 */
7320Sstevel@tonic-gate 		t = zsd_find(&zone->zone_zsd, key);
7330Sstevel@tonic-gate 		if (t != NULL) {
7340Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
7350Sstevel@tonic-gate 				t->zsd_shutdown(zoneid, t->zsd_data);
7360Sstevel@tonic-gate 			} else {
7370Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
7380Sstevel@tonic-gate 				t->zsd_destroy(zoneid, t->zsd_data);
7390Sstevel@tonic-gate 			}
7400Sstevel@tonic-gate 		} else {
7410Sstevel@tonic-gate 			if (ct == ZSD_SHUTDOWN) {
7420Sstevel@tonic-gate 				zsdp->zsd_shutdown(zoneid, NULL);
7430Sstevel@tonic-gate 			} else {
7440Sstevel@tonic-gate 				ASSERT(ct == ZSD_DESTROY);
7450Sstevel@tonic-gate 				zsdp->zsd_destroy(zoneid, NULL);
7460Sstevel@tonic-gate 			}
7470Sstevel@tonic-gate 		}
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 	mutex_exit(&zsd_key_lock);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate /*
7530Sstevel@tonic-gate  * Called when the zone is going away; free ZSD-related memory, and
7540Sstevel@tonic-gate  * destroy the zone_zsd list.
7550Sstevel@tonic-gate  */
7560Sstevel@tonic-gate static void
7570Sstevel@tonic-gate zone_free_zsd(zone_t *zone)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	struct zsd_entry *t, *next;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	/*
7620Sstevel@tonic-gate 	 * Free all the zsd_entry's we had on this zone.
7630Sstevel@tonic-gate 	 */
7640Sstevel@tonic-gate 	for (t = list_head(&zone->zone_zsd); t != NULL; t = next) {
7650Sstevel@tonic-gate 		next = list_next(&zone->zone_zsd, t);
7660Sstevel@tonic-gate 		list_remove(&zone->zone_zsd, t);
7670Sstevel@tonic-gate 		kmem_free(t, sizeof (*t));
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 	list_destroy(&zone->zone_zsd);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate /*
7730Sstevel@tonic-gate  * zone.cpu-shares resource control support.
7740Sstevel@tonic-gate  */
7750Sstevel@tonic-gate /*ARGSUSED*/
7760Sstevel@tonic-gate static rctl_qty_t
7770Sstevel@tonic-gate zone_cpu_shares_usage(rctl_t *rctl, struct proc *p)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
7800Sstevel@tonic-gate 	return (p->p_zone->zone_shares);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate /*ARGSUSED*/
7840Sstevel@tonic-gate static int
7850Sstevel@tonic-gate zone_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
7860Sstevel@tonic-gate     rctl_qty_t nv)
7870Sstevel@tonic-gate {
7880Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
7890Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
7900Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
7910Sstevel@tonic-gate 		return (0);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	e->rcep_p.zone->zone_shares = nv;
7940Sstevel@tonic-gate 	return (0);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate static rctl_ops_t zone_cpu_shares_ops = {
7980Sstevel@tonic-gate 	rcop_no_action,
7990Sstevel@tonic-gate 	zone_cpu_shares_usage,
8000Sstevel@tonic-gate 	zone_cpu_shares_set,
8010Sstevel@tonic-gate 	rcop_no_test
8020Sstevel@tonic-gate };
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate /*ARGSUSED*/
8050Sstevel@tonic-gate static rctl_qty_t
8060Sstevel@tonic-gate zone_lwps_usage(rctl_t *r, proc_t *p)
8070Sstevel@tonic-gate {
8080Sstevel@tonic-gate 	rctl_qty_t nlwps;
8090Sstevel@tonic-gate 	zone_t *zone = p->p_zone;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
8140Sstevel@tonic-gate 	nlwps = zone->zone_nlwps;
8150Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	return (nlwps);
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate /*ARGSUSED*/
8210Sstevel@tonic-gate static int
8220Sstevel@tonic-gate zone_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
8230Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 	rctl_qty_t nlwps;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8280Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
8290Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
8300Sstevel@tonic-gate 		return (0);
8310Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(e->rcep_p.zone->zone_nlwps_lock)));
8320Sstevel@tonic-gate 	nlwps = e->rcep_p.zone->zone_nlwps;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
8350Sstevel@tonic-gate 		return (1);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	return (0);
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate /*ARGSUSED*/
8410Sstevel@tonic-gate static int
8420Sstevel@tonic-gate zone_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e, rctl_qty_t nv) {
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8450Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_ZONE);
8460Sstevel@tonic-gate 	if (e->rcep_p.zone == NULL)
8470Sstevel@tonic-gate 		return (0);
8480Sstevel@tonic-gate 	e->rcep_p.zone->zone_nlwps_ctl = nv;
8490Sstevel@tonic-gate 	return (0);
8500Sstevel@tonic-gate }
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate static rctl_ops_t zone_lwps_ops = {
8530Sstevel@tonic-gate 	rcop_no_action,
8540Sstevel@tonic-gate 	zone_lwps_usage,
8550Sstevel@tonic-gate 	zone_lwps_set,
8560Sstevel@tonic-gate 	zone_lwps_test,
8570Sstevel@tonic-gate };
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate /*
8600Sstevel@tonic-gate  * Helper function to brand the zone with a unique ID.
8610Sstevel@tonic-gate  */
8620Sstevel@tonic-gate static void
8630Sstevel@tonic-gate zone_uniqid(zone_t *zone)
8640Sstevel@tonic-gate {
8650Sstevel@tonic-gate 	static uint64_t uniqid = 0;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
8680Sstevel@tonic-gate 	zone->zone_uniqid = uniqid++;
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate /*
8720Sstevel@tonic-gate  * Returns a held pointer to the "kcred" for the specified zone.
8730Sstevel@tonic-gate  */
8740Sstevel@tonic-gate struct cred *
8750Sstevel@tonic-gate zone_get_kcred(zoneid_t zoneid)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate 	zone_t *zone;
8780Sstevel@tonic-gate 	cred_t *cr;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if ((zone = zone_find_by_id(zoneid)) == NULL)
8810Sstevel@tonic-gate 		return (NULL);
8820Sstevel@tonic-gate 	cr = zone->zone_kcred;
8830Sstevel@tonic-gate 	crhold(cr);
8840Sstevel@tonic-gate 	zone_rele(zone);
8850Sstevel@tonic-gate 	return (cr);
8860Sstevel@tonic-gate }
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate /*
8890Sstevel@tonic-gate  * Called very early on in boot to initialize the ZSD list so that
8900Sstevel@tonic-gate  * zone_key_create() can be called before zone_init().  It also initializes
8910Sstevel@tonic-gate  * portions of zone0 which may be used before zone_init() is called.  The
8920Sstevel@tonic-gate  * variable "global_zone" will be set when zone0 is fully initialized by
8930Sstevel@tonic-gate  * zone_init().
8940Sstevel@tonic-gate  */
8950Sstevel@tonic-gate void
8960Sstevel@tonic-gate zone_zsd_init(void)
8970Sstevel@tonic-gate {
8980Sstevel@tonic-gate 	mutex_init(&zonehash_lock, NULL, MUTEX_DEFAULT, NULL);
8990Sstevel@tonic-gate 	mutex_init(&zsd_key_lock, NULL, MUTEX_DEFAULT, NULL);
9000Sstevel@tonic-gate 	list_create(&zsd_registered_keys, sizeof (struct zsd_entry),
9010Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
9020Sstevel@tonic-gate 	list_create(&zone_active, sizeof (zone_t),
9030Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
9040Sstevel@tonic-gate 	list_create(&zone_deathrow, sizeof (zone_t),
9050Sstevel@tonic-gate 	    offsetof(zone_t, zone_linkage));
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	mutex_init(&zone0.zone_lock, NULL, MUTEX_DEFAULT, NULL);
9080Sstevel@tonic-gate 	mutex_init(&zone0.zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
9090Sstevel@tonic-gate 	zone0.zone_shares = 1;
9100Sstevel@tonic-gate 	zone0.zone_nlwps_ctl = INT_MAX;
9110Sstevel@tonic-gate 	zone0.zone_name = GLOBAL_ZONENAME;
9120Sstevel@tonic-gate 	zone0.zone_nodename = utsname.nodename;
9130Sstevel@tonic-gate 	zone0.zone_domain = srpc_domain;
9140Sstevel@tonic-gate 	zone0.zone_ref = 1;
9150Sstevel@tonic-gate 	zone0.zone_id = GLOBAL_ZONEID;
9160Sstevel@tonic-gate 	zone0.zone_status = ZONE_IS_RUNNING;
9170Sstevel@tonic-gate 	zone0.zone_rootpath = "/";
9180Sstevel@tonic-gate 	zone0.zone_rootpathlen = 2;
9190Sstevel@tonic-gate 	zone0.zone_psetid = ZONE_PS_INVAL;
9200Sstevel@tonic-gate 	zone0.zone_ncpus = 0;
9210Sstevel@tonic-gate 	zone0.zone_ncpus_online = 0;
9220Sstevel@tonic-gate 	zone0.zone_proc_initpid = 1;
9230Sstevel@tonic-gate 	list_create(&zone0.zone_zsd, sizeof (struct zsd_entry),
9240Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
9250Sstevel@tonic-gate 	list_insert_head(&zone_active, &zone0);
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/*
9280Sstevel@tonic-gate 	 * The root filesystem is not mounted yet, so zone_rootvp cannot be set
9290Sstevel@tonic-gate 	 * to anything meaningful.  It is assigned to be 'rootdir' in
9300Sstevel@tonic-gate 	 * vfs_mountroot().
9310Sstevel@tonic-gate 	 */
9320Sstevel@tonic-gate 	zone0.zone_rootvp = NULL;
9330Sstevel@tonic-gate 	zone0.zone_vfslist = NULL;
9340Sstevel@tonic-gate 	zone0.zone_bootargs = NULL;
9350Sstevel@tonic-gate 	zone0.zone_privset = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
9360Sstevel@tonic-gate 	/*
9370Sstevel@tonic-gate 	 * The global zone has all privileges
9380Sstevel@tonic-gate 	 */
9390Sstevel@tonic-gate 	priv_fillset(zone0.zone_privset);
9400Sstevel@tonic-gate 	/*
9410Sstevel@tonic-gate 	 * Add p0 to the global zone
9420Sstevel@tonic-gate 	 */
9430Sstevel@tonic-gate 	zone0.zone_zsched = &p0;
9440Sstevel@tonic-gate 	p0.p_zone = &zone0;
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate /*
9480Sstevel@tonic-gate  * Called by main() to initialize the zones framework.
9490Sstevel@tonic-gate  */
9500Sstevel@tonic-gate void
9510Sstevel@tonic-gate zone_init(void)
9520Sstevel@tonic-gate {
9530Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
9540Sstevel@tonic-gate 	rctl_val_t *dval;
9550Sstevel@tonic-gate 	rctl_set_t *set;
9560Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
9570Sstevel@tonic-gate 	rctl_entity_p_t e;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	ASSERT(curproc == &p0);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	/*
9620Sstevel@tonic-gate 	 * Create ID space for zone IDs.  ID 0 is reserved for the
9630Sstevel@tonic-gate 	 * global zone.
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	zoneid_space = id_space_create("zoneid_space", 1, MAX_ZONEID);
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	/*
9680Sstevel@tonic-gate 	 * Initialize generic zone resource controls, if any.
9690Sstevel@tonic-gate 	 */
9700Sstevel@tonic-gate 	rc_zone_cpu_shares = rctl_register("zone.cpu-shares",
9710Sstevel@tonic-gate 	    RCENTITY_ZONE, RCTL_GLOBAL_SIGNAL_NEVER | RCTL_GLOBAL_DENY_NEVER |
9720Sstevel@tonic-gate 	    RCTL_GLOBAL_NOBASIC |
9730Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, FSS_MAXSHARES, FSS_MAXSHARES,
9740Sstevel@tonic-gate 	    &zone_cpu_shares_ops);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	rc_zone_nlwps = rctl_register("zone.max-lwps", RCENTITY_ZONE,
9770Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
9780Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &zone_lwps_ops);
9790Sstevel@tonic-gate 	/*
9800Sstevel@tonic-gate 	 * Create a rctl_val with PRIVILEGED, NOACTION, value = 1.  Then attach
9810Sstevel@tonic-gate 	 * this at the head of the rctl_dict_entry for ``zone.cpu-shares''.
9820Sstevel@tonic-gate 	 */
9830Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
9840Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
9850Sstevel@tonic-gate 	dval->rcv_value = 1;
9860Sstevel@tonic-gate 	dval->rcv_privilege = RCPRIV_PRIVILEGED;
9870Sstevel@tonic-gate 	dval->rcv_flagaction = RCTL_LOCAL_NOACTION;
9880Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	rde = rctl_dict_lookup("zone.cpu-shares");
9910Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	/*
9940Sstevel@tonic-gate 	 * Initialize the ``global zone''.
9950Sstevel@tonic-gate 	 */
9960Sstevel@tonic-gate 	set = rctl_set_create();
9970Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
9980Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
9990Sstevel@tonic-gate 	e.rcep_p.zone = &zone0;
10000Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
10010Sstevel@tonic-gate 	zone0.zone_rctls = rctl_set_init(RCENTITY_ZONE, &p0, &e, set,
10020Sstevel@tonic-gate 	    gp);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	zone0.zone_nlwps = p0.p_lwpcnt;
10050Sstevel@tonic-gate 	zone0.zone_ntasks = 1;
10060Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
10070Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
10080Sstevel@tonic-gate 	/*
10090Sstevel@tonic-gate 	 * pool_default hasn't been initialized yet, so we let pool_init() take
10100Sstevel@tonic-gate 	 * care of making the global zone is in the default pool.
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
10130Sstevel@tonic-gate 	zone_uniqid(&zone0);
10140Sstevel@tonic-gate 	ASSERT(zone0.zone_uniqid == GLOBAL_ZONEUNIQID);
10150Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
10160Sstevel@tonic-gate 	zonehashbyid = mod_hash_create_idhash("zone_by_id", zone_hash_size,
10170Sstevel@tonic-gate 	    mod_hash_null_valdtor);
10180Sstevel@tonic-gate 	zonehashbyname = mod_hash_create_strhash("zone_by_name",
10190Sstevel@tonic-gate 	    zone_hash_size, mod_hash_null_valdtor);
10200Sstevel@tonic-gate 	zonecount = 1;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid, (mod_hash_key_t)GLOBAL_ZONEID,
10230Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
10240Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)zone0.zone_name,
10250Sstevel@tonic-gate 	    (mod_hash_val_t)&zone0);
10260Sstevel@tonic-gate 	/*
10270Sstevel@tonic-gate 	 * We avoid setting zone_kcred until now, since kcred is initialized
10280Sstevel@tonic-gate 	 * sometime after zone_zsd_init() and before zone_init().
10290Sstevel@tonic-gate 	 */
10300Sstevel@tonic-gate 	zone0.zone_kcred = kcred;
10310Sstevel@tonic-gate 	/*
10320Sstevel@tonic-gate 	 * The global zone is fully initialized (except for zone_rootvp which
10330Sstevel@tonic-gate 	 * will be set when the root filesystem is mounted).
10340Sstevel@tonic-gate 	 */
10350Sstevel@tonic-gate 	global_zone = &zone0;
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate static void
10390Sstevel@tonic-gate zone_free(zone_t *zone)
10400Sstevel@tonic-gate {
10410Sstevel@tonic-gate 	ASSERT(zone != global_zone);
10420Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks == 0);
10430Sstevel@tonic-gate 	ASSERT(zone->zone_nlwps == 0);
10440Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 0);
10450Sstevel@tonic-gate 	ASSERT(zone->zone_kcred == NULL);
10460Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_DEAD ||
10470Sstevel@tonic-gate 	    zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	/* remove from deathrow list */
10500Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_DEAD) {
10510Sstevel@tonic-gate 		ASSERT(zone->zone_ref == 0);
10520Sstevel@tonic-gate 		mutex_enter(&zone_deathrow_lock);
10530Sstevel@tonic-gate 		list_remove(&zone_deathrow, zone);
10540Sstevel@tonic-gate 		mutex_exit(&zone_deathrow_lock);
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	zone_free_zsd(zone);
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	if (zone->zone_rootvp != NULL)
10600Sstevel@tonic-gate 		VN_RELE(zone->zone_rootvp);
10610Sstevel@tonic-gate 	if (zone->zone_rootpath)
10620Sstevel@tonic-gate 		kmem_free(zone->zone_rootpath, zone->zone_rootpathlen);
10630Sstevel@tonic-gate 	if (zone->zone_name != NULL)
10640Sstevel@tonic-gate 		kmem_free(zone->zone_name, ZONENAME_MAX);
10650Sstevel@tonic-gate 	if (zone->zone_nodename != NULL)
10660Sstevel@tonic-gate 		kmem_free(zone->zone_nodename, _SYS_NMLN);
10670Sstevel@tonic-gate 	if (zone->zone_domain != NULL)
10680Sstevel@tonic-gate 		kmem_free(zone->zone_domain, _SYS_NMLN);
10690Sstevel@tonic-gate 	if (zone->zone_privset != NULL)
10700Sstevel@tonic-gate 		kmem_free(zone->zone_privset, sizeof (priv_set_t));
10710Sstevel@tonic-gate 	if (zone->zone_rctls != NULL)
10720Sstevel@tonic-gate 		rctl_set_free(zone->zone_rctls);
10730Sstevel@tonic-gate 	if (zone->zone_bootargs != NULL)
10740Sstevel@tonic-gate 		kmem_free(zone->zone_bootargs, ZONEBOOTARGS_MAX);
10750Sstevel@tonic-gate 	id_free(zoneid_space, zone->zone_id);
10760Sstevel@tonic-gate 	mutex_destroy(&zone->zone_lock);
10770Sstevel@tonic-gate 	cv_destroy(&zone->zone_cv);
10780Sstevel@tonic-gate 	kmem_free(zone, sizeof (zone_t));
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate /*
10820Sstevel@tonic-gate  * See block comment at the top of this file for information about zone
10830Sstevel@tonic-gate  * status values.
10840Sstevel@tonic-gate  */
10850Sstevel@tonic-gate /*
10860Sstevel@tonic-gate  * Convenience function for setting zone status.
10870Sstevel@tonic-gate  */
10880Sstevel@tonic-gate static void
10890Sstevel@tonic-gate zone_status_set(zone_t *zone, zone_status_t status)
10900Sstevel@tonic-gate {
10910Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zone_status_lock));
10920Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE &&
10930Sstevel@tonic-gate 	    status >= zone_status_get(zone));
10940Sstevel@tonic-gate 	zone->zone_status = status;
10950Sstevel@tonic-gate 	cv_broadcast(&zone->zone_cv);
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate /*
10990Sstevel@tonic-gate  * Public function to retrieve the zone status.  The zone status may
11000Sstevel@tonic-gate  * change after it is retrieved.
11010Sstevel@tonic-gate  */
11020Sstevel@tonic-gate zone_status_t
11030Sstevel@tonic-gate zone_status_get(zone_t *zone)
11040Sstevel@tonic-gate {
11050Sstevel@tonic-gate 	return (zone->zone_status);
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate static int
11090Sstevel@tonic-gate zone_set_bootargs(zone_t *zone, const char *zone_bootargs)
11100Sstevel@tonic-gate {
11110Sstevel@tonic-gate 	char *bootargs = kmem_zalloc(ZONEBOOTARGS_MAX, KM_SLEEP);
11120Sstevel@tonic-gate 	size_t len;
11130Sstevel@tonic-gate 	int err;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	err = copyinstr(zone_bootargs, bootargs, ZONEBOOTARGS_MAX - 1, &len);
11160Sstevel@tonic-gate 	if (err != 0) {
11170Sstevel@tonic-gate 		kmem_free(bootargs, ZONEBOOTARGS_MAX);
11180Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 	bootargs[len] = '\0';
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	ASSERT(zone->zone_bootargs == NULL);
11230Sstevel@tonic-gate 	zone->zone_bootargs = bootargs;
11240Sstevel@tonic-gate 	return (0);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate /*
11280Sstevel@tonic-gate  * Block indefinitely waiting for (zone_status >= status)
11290Sstevel@tonic-gate  */
11300Sstevel@tonic-gate void
11310Sstevel@tonic-gate zone_status_wait(zone_t *zone, zone_status_t status)
11320Sstevel@tonic-gate {
11330Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
11360Sstevel@tonic-gate 	while (zone->zone_status < status) {
11370Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
11380Sstevel@tonic-gate 	}
11390Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate /*
11430Sstevel@tonic-gate  * Private CPR-safe version of zone_status_wait().
11440Sstevel@tonic-gate  */
11450Sstevel@tonic-gate static void
11460Sstevel@tonic-gate zone_status_wait_cpr(zone_t *zone, zone_status_t status, char *str)
11470Sstevel@tonic-gate {
11480Sstevel@tonic-gate 	callb_cpr_t cprinfo;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &zone_status_lock, callb_generic_cpr,
11530Sstevel@tonic-gate 	    str);
11540Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
11550Sstevel@tonic-gate 	while (zone->zone_status < status) {
11560Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
11570Sstevel@tonic-gate 		cv_wait(&zone->zone_cv, &zone_status_lock);
11580Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &zone_status_lock);
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 	/*
11610Sstevel@tonic-gate 	 * zone_status_lock is implicitly released by the following.
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cprinfo);
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate /*
11670Sstevel@tonic-gate  * Block until zone enters requested state or signal is received.  Return (0)
11680Sstevel@tonic-gate  * if signaled, non-zero otherwise.
11690Sstevel@tonic-gate  */
11700Sstevel@tonic-gate int
11710Sstevel@tonic-gate zone_status_wait_sig(zone_t *zone, zone_status_t status)
11720Sstevel@tonic-gate {
11730Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
11760Sstevel@tonic-gate 	while (zone->zone_status < status) {
11770Sstevel@tonic-gate 		if (!cv_wait_sig(&zone->zone_cv, &zone_status_lock)) {
11780Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
11790Sstevel@tonic-gate 			return (0);
11800Sstevel@tonic-gate 		}
11810Sstevel@tonic-gate 	}
11820Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
11830Sstevel@tonic-gate 	return (1);
11840Sstevel@tonic-gate }
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate /*
11870Sstevel@tonic-gate  * Block until the zone enters the requested state or the timeout expires,
11880Sstevel@tonic-gate  * whichever happens first.  Return (-1) if operation timed out, time remaining
11890Sstevel@tonic-gate  * otherwise.
11900Sstevel@tonic-gate  */
11910Sstevel@tonic-gate clock_t
11920Sstevel@tonic-gate zone_status_timedwait(zone_t *zone, clock_t tim, zone_status_t status)
11930Sstevel@tonic-gate {
11940Sstevel@tonic-gate 	clock_t timeleft = 0;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
11990Sstevel@tonic-gate 	while (zone->zone_status < status && timeleft != -1) {
12000Sstevel@tonic-gate 		timeleft = cv_timedwait(&zone->zone_cv, &zone_status_lock, tim);
12010Sstevel@tonic-gate 	}
12020Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
12030Sstevel@tonic-gate 	return (timeleft);
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /*
12070Sstevel@tonic-gate  * Block until the zone enters the requested state, the current process is
12080Sstevel@tonic-gate  * signaled,  or the timeout expires, whichever happens first.  Return (-1) if
12090Sstevel@tonic-gate  * operation timed out, 0 if signaled, time remaining otherwise.
12100Sstevel@tonic-gate  */
12110Sstevel@tonic-gate clock_t
12120Sstevel@tonic-gate zone_status_timedwait_sig(zone_t *zone, clock_t tim, zone_status_t status)
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 	clock_t timeleft = tim - lbolt;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	ASSERT(status > ZONE_MIN_STATE && status <= ZONE_MAX_STATE);
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
12190Sstevel@tonic-gate 	while (zone->zone_status < status) {
12200Sstevel@tonic-gate 		timeleft = cv_timedwait_sig(&zone->zone_cv, &zone_status_lock,
12210Sstevel@tonic-gate 		    tim);
12220Sstevel@tonic-gate 		if (timeleft <= 0)
12230Sstevel@tonic-gate 			break;
12240Sstevel@tonic-gate 	}
12250Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
12260Sstevel@tonic-gate 	return (timeleft);
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate /*
12300Sstevel@tonic-gate  * Zones have two reference counts: one for references from credential
12310Sstevel@tonic-gate  * structures (zone_cred_ref), and one (zone_ref) for everything else.
12320Sstevel@tonic-gate  * This is so we can allow a zone to be rebooted while there are still
12330Sstevel@tonic-gate  * outstanding cred references, since certain drivers cache dblks (which
12340Sstevel@tonic-gate  * implicitly results in cached creds).  We wait for zone_ref to drop to
12350Sstevel@tonic-gate  * 0 (actually 1), but not zone_cred_ref.  The zone structure itself is
12360Sstevel@tonic-gate  * later freed when the zone_cred_ref drops to 0, though nothing other
12370Sstevel@tonic-gate  * than the zone id and privilege set should be accessed once the zone
12380Sstevel@tonic-gate  * is "dead".
12390Sstevel@tonic-gate  *
12400Sstevel@tonic-gate  * A debugging flag, zone_wait_for_cred, can be set to a non-zero value
12410Sstevel@tonic-gate  * to force halt/reboot to block waiting for the zone_cred_ref to drop
12420Sstevel@tonic-gate  * to 0.  This can be useful to flush out other sources of cached creds
12430Sstevel@tonic-gate  * that may be less innocuous than the driver case.
12440Sstevel@tonic-gate  */
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate int zone_wait_for_cred = 0;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate static void
12490Sstevel@tonic-gate zone_hold_locked(zone_t *z)
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&z->zone_lock));
12520Sstevel@tonic-gate 	z->zone_ref++;
12530Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate void
12570Sstevel@tonic-gate zone_hold(zone_t *z)
12580Sstevel@tonic-gate {
12590Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
12600Sstevel@tonic-gate 	zone_hold_locked(z);
12610Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
12620Sstevel@tonic-gate }
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate /*
12650Sstevel@tonic-gate  * If the non-cred ref count drops to 1 and either the cred ref count
12660Sstevel@tonic-gate  * is 0 or we aren't waiting for cred references, the zone is ready to
12670Sstevel@tonic-gate  * be destroyed.
12680Sstevel@tonic-gate  */
12690Sstevel@tonic-gate #define	ZONE_IS_UNREF(zone)	((zone)->zone_ref == 1 && \
12700Sstevel@tonic-gate 	    (!zone_wait_for_cred || (zone)->zone_cred_ref == 0))
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate void
12730Sstevel@tonic-gate zone_rele(zone_t *z)
12740Sstevel@tonic-gate {
12750Sstevel@tonic-gate 	boolean_t wakeup;
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
12780Sstevel@tonic-gate 	ASSERT(z->zone_ref != 0);
12790Sstevel@tonic-gate 	z->zone_ref--;
12800Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
12810Sstevel@tonic-gate 		/* no more refs, free the structure */
12820Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
12830Sstevel@tonic-gate 		zone_free(z);
12840Sstevel@tonic-gate 		return;
12850Sstevel@tonic-gate 	}
12860Sstevel@tonic-gate 	/* signal zone_destroy so the zone can finish halting */
12870Sstevel@tonic-gate 	wakeup = (ZONE_IS_UNREF(z) && zone_status_get(z) >= ZONE_IS_DEAD);
12880Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	if (wakeup) {
12910Sstevel@tonic-gate 		/*
12920Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
12930Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
12940Sstevel@tonic-gate 		 */
12950Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
12960Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
12970Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
12980Sstevel@tonic-gate 	}
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate void
13020Sstevel@tonic-gate zone_cred_hold(zone_t *z)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
13050Sstevel@tonic-gate 	z->zone_cred_ref++;
13060Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
13070Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
13080Sstevel@tonic-gate }
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate void
13110Sstevel@tonic-gate zone_cred_rele(zone_t *z)
13120Sstevel@tonic-gate {
13130Sstevel@tonic-gate 	boolean_t wakeup;
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
13160Sstevel@tonic-gate 	ASSERT(z->zone_cred_ref != 0);
13170Sstevel@tonic-gate 	z->zone_cred_ref--;
13180Sstevel@tonic-gate 	if (z->zone_ref == 0 && z->zone_cred_ref == 0) {
13190Sstevel@tonic-gate 		/* no more refs, free the structure */
13200Sstevel@tonic-gate 		mutex_exit(&z->zone_lock);
13210Sstevel@tonic-gate 		zone_free(z);
13220Sstevel@tonic-gate 		return;
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate 	/*
13250Sstevel@tonic-gate 	 * If zone_destroy is waiting for the cred references to drain
13260Sstevel@tonic-gate 	 * out, and they have, signal it.
13270Sstevel@tonic-gate 	 */
13280Sstevel@tonic-gate 	wakeup = (zone_wait_for_cred && ZONE_IS_UNREF(z) &&
13290Sstevel@tonic-gate 	    zone_status_get(z) >= ZONE_IS_DEAD);
13300Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	if (wakeup) {
13330Sstevel@tonic-gate 		/*
13340Sstevel@tonic-gate 		 * Grabbing zonehash_lock here effectively synchronizes with
13350Sstevel@tonic-gate 		 * zone_destroy() to avoid missed signals.
13360Sstevel@tonic-gate 		 */
13370Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
13380Sstevel@tonic-gate 		cv_broadcast(&zone_destroy_cv);
13390Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate void
13440Sstevel@tonic-gate zone_task_hold(zone_t *z)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	mutex_enter(&z->zone_lock);
13470Sstevel@tonic-gate 	z->zone_ntasks++;
13480Sstevel@tonic-gate 	ASSERT(z->zone_ntasks != 0);
13490Sstevel@tonic-gate 	mutex_exit(&z->zone_lock);
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate void
13530Sstevel@tonic-gate zone_task_rele(zone_t *zone)
13540Sstevel@tonic-gate {
13550Sstevel@tonic-gate 	uint_t refcnt;
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	mutex_enter(&zone->zone_lock);
13580Sstevel@tonic-gate 	ASSERT(zone->zone_ntasks != 0);
13590Sstevel@tonic-gate 	refcnt = --zone->zone_ntasks;
13600Sstevel@tonic-gate 	if (refcnt > 1)	{	/* Common case */
13610Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
13620Sstevel@tonic-gate 		return;
13630Sstevel@tonic-gate 	}
13640Sstevel@tonic-gate 	zone_hold_locked(zone);	/* so we can use the zone_t later */
13650Sstevel@tonic-gate 	mutex_exit(&zone->zone_lock);
13660Sstevel@tonic-gate 	if (refcnt == 1) {
13670Sstevel@tonic-gate 		/*
13680Sstevel@tonic-gate 		 * See if the zone is shutting down.
13690Sstevel@tonic-gate 		 */
13700Sstevel@tonic-gate 		mutex_enter(&zone_status_lock);
13710Sstevel@tonic-gate 		if (zone_status_get(zone) != ZONE_IS_SHUTTING_DOWN) {
13720Sstevel@tonic-gate 			goto out;
13730Sstevel@tonic-gate 		}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		/*
13760Sstevel@tonic-gate 		 * Make sure the ntasks didn't change since we
13770Sstevel@tonic-gate 		 * dropped zone_lock.
13780Sstevel@tonic-gate 		 */
13790Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
13800Sstevel@tonic-gate 		if (refcnt != zone->zone_ntasks) {
13810Sstevel@tonic-gate 			mutex_exit(&zone->zone_lock);
13820Sstevel@tonic-gate 			goto out;
13830Sstevel@tonic-gate 		}
13840Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 		/*
13870Sstevel@tonic-gate 		 * No more user processes in the zone.  The zone is empty.
13880Sstevel@tonic-gate 		 */
13890Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_EMPTY);
13900Sstevel@tonic-gate 		goto out;
13910Sstevel@tonic-gate 	}
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	ASSERT(refcnt == 0);
13940Sstevel@tonic-gate 	/*
13950Sstevel@tonic-gate 	 * zsched has exited; the zone is dead.
13960Sstevel@tonic-gate 	 */
13970Sstevel@tonic-gate 	zone->zone_zsched = NULL;		/* paranoia */
13980Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
13990Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_DEAD);
14000Sstevel@tonic-gate out:
14010Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
14020Sstevel@tonic-gate 	zone_rele(zone);
14030Sstevel@tonic-gate }
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate zoneid_t
14060Sstevel@tonic-gate getzoneid(void)
14070Sstevel@tonic-gate {
14080Sstevel@tonic-gate 	return (curproc->p_zone->zone_id);
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate /*
14120Sstevel@tonic-gate  * Internal versions of zone_find_by_*().  These don't zone_hold() or
14130Sstevel@tonic-gate  * check the validity of a zone's state.
14140Sstevel@tonic-gate  */
14150Sstevel@tonic-gate static zone_t *
14160Sstevel@tonic-gate zone_find_all_by_id(zoneid_t zoneid)
14170Sstevel@tonic-gate {
14180Sstevel@tonic-gate 	mod_hash_val_t hv;
14190Sstevel@tonic-gate 	zone_t *zone = NULL;
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyid,
14240Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zoneid, &hv) == 0)
14250Sstevel@tonic-gate 		zone = (zone_t *)hv;
14260Sstevel@tonic-gate 	return (zone);
14270Sstevel@tonic-gate }
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate static zone_t *
14300Sstevel@tonic-gate zone_find_all_by_name(char *name)
14310Sstevel@tonic-gate {
14320Sstevel@tonic-gate 	mod_hash_val_t hv;
14330Sstevel@tonic-gate 	zone_t *zone = NULL;
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	if (mod_hash_find(zonehashbyname, (mod_hash_key_t)name, &hv) == 0)
14380Sstevel@tonic-gate 		zone = (zone_t *)hv;
14390Sstevel@tonic-gate 	return (zone);
14400Sstevel@tonic-gate }
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate /*
14430Sstevel@tonic-gate  * Public interface for looking up a zone by zoneid.  Only returns the zone if
14440Sstevel@tonic-gate  * it is fully initialized, and has not yet begun the zone_destroy() sequence.
14450Sstevel@tonic-gate  * Caller must call zone_rele() once it is done with the zone.
14460Sstevel@tonic-gate  *
14470Sstevel@tonic-gate  * The zone may begin the zone_destroy() sequence immediately after this
14480Sstevel@tonic-gate  * function returns, but may be safely used until zone_rele() is called.
14490Sstevel@tonic-gate  */
14500Sstevel@tonic-gate zone_t *
14510Sstevel@tonic-gate zone_find_by_id(zoneid_t zoneid)
14520Sstevel@tonic-gate {
14530Sstevel@tonic-gate 	zone_t *zone;
14540Sstevel@tonic-gate 	zone_status_t status;
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
14570Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
14580Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14590Sstevel@tonic-gate 		return (NULL);
14600Sstevel@tonic-gate 	}
14610Sstevel@tonic-gate 	status = zone_status_get(zone);
14620Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
14630Sstevel@tonic-gate 		/*
14640Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
14650Sstevel@tonic-gate 		 */
14660Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14670Sstevel@tonic-gate 		return (NULL);
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 	zone_hold(zone);
14700Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
14710Sstevel@tonic-gate 	return (zone);
14720Sstevel@tonic-gate }
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate /*
14750Sstevel@tonic-gate  * Similar to zone_find_by_id, but using zone name as the key.
14760Sstevel@tonic-gate  */
14770Sstevel@tonic-gate zone_t *
14780Sstevel@tonic-gate zone_find_by_name(char *name)
14790Sstevel@tonic-gate {
14800Sstevel@tonic-gate 	zone_t *zone;
14810Sstevel@tonic-gate 	zone_status_t status;
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
14840Sstevel@tonic-gate 	if ((zone = zone_find_all_by_name(name)) == NULL) {
14850Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14860Sstevel@tonic-gate 		return (NULL);
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate 	status = zone_status_get(zone);
14890Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
14900Sstevel@tonic-gate 		/*
14910Sstevel@tonic-gate 		 * For all practical purposes the zone doesn't exist.
14920Sstevel@tonic-gate 		 */
14930Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
14940Sstevel@tonic-gate 		return (NULL);
14950Sstevel@tonic-gate 	}
14960Sstevel@tonic-gate 	zone_hold(zone);
14970Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
14980Sstevel@tonic-gate 	return (zone);
14990Sstevel@tonic-gate }
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate /*
15020Sstevel@tonic-gate  * Similar to zone_find_by_id(), using the path as a key.  For instance,
15030Sstevel@tonic-gate  * if there is a zone "foo" rooted at /foo/root, and the path argument
15040Sstevel@tonic-gate  * is "/foo/root/proc", it will return the held zone_t corresponding to
15050Sstevel@tonic-gate  * zone "foo".
15060Sstevel@tonic-gate  *
15070Sstevel@tonic-gate  * zone_find_by_path() always returns a non-NULL value, since at the
15080Sstevel@tonic-gate  * very least every path will be contained in the global zone.
15090Sstevel@tonic-gate  *
15100Sstevel@tonic-gate  * As with the other zone_find_by_*() functions, the caller is
15110Sstevel@tonic-gate  * responsible for zone_rele()ing the return value of this function.
15120Sstevel@tonic-gate  */
15130Sstevel@tonic-gate zone_t *
15140Sstevel@tonic-gate zone_find_by_path(const char *path)
15150Sstevel@tonic-gate {
15160Sstevel@tonic-gate 	zone_t *zone;
15170Sstevel@tonic-gate 	zone_t *zret = NULL;
15180Sstevel@tonic-gate 	zone_status_t status;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	if (path == NULL) {
15210Sstevel@tonic-gate 		/*
15220Sstevel@tonic-gate 		 * Call from rootconf().
15230Sstevel@tonic-gate 		 */
15240Sstevel@tonic-gate 		zone_hold(global_zone);
15250Sstevel@tonic-gate 		return (global_zone);
15260Sstevel@tonic-gate 	}
15270Sstevel@tonic-gate 	ASSERT(*path == '/');
15280Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
15290Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
15300Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
15310Sstevel@tonic-gate 		if (ZONE_PATH_VISIBLE(path, zone))
15320Sstevel@tonic-gate 			zret = zone;
15330Sstevel@tonic-gate 	}
15340Sstevel@tonic-gate 	ASSERT(zret != NULL);
15350Sstevel@tonic-gate 	status = zone_status_get(zret);
15360Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status > ZONE_IS_DOWN) {
15370Sstevel@tonic-gate 		/*
15380Sstevel@tonic-gate 		 * Zone practically doesn't exist.
15390Sstevel@tonic-gate 		 */
15400Sstevel@tonic-gate 		zret = global_zone;
15410Sstevel@tonic-gate 	}
15420Sstevel@tonic-gate 	zone_hold(zret);
15430Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
15440Sstevel@tonic-gate 	return (zret);
15450Sstevel@tonic-gate }
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate /*
15480Sstevel@tonic-gate  * Get the number of cpus visible to this zone.  The system-wide global
15490Sstevel@tonic-gate  * 'ncpus' is returned if pools are disabled, the caller is in the
15500Sstevel@tonic-gate  * global zone, or a NULL zone argument is passed in.
15510Sstevel@tonic-gate  */
15520Sstevel@tonic-gate int
15530Sstevel@tonic-gate zone_ncpus_get(zone_t *zone)
15540Sstevel@tonic-gate {
15550Sstevel@tonic-gate 	int myncpus = zone == NULL ? 0 : zone->zone_ncpus;
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	return (myncpus != 0 ? myncpus : ncpus);
15580Sstevel@tonic-gate }
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate /*
15610Sstevel@tonic-gate  * Get the number of online cpus visible to this zone.  The system-wide
15620Sstevel@tonic-gate  * global 'ncpus_online' is returned if pools are disabled, the caller
15630Sstevel@tonic-gate  * is in the global zone, or a NULL zone argument is passed in.
15640Sstevel@tonic-gate  */
15650Sstevel@tonic-gate int
15660Sstevel@tonic-gate zone_ncpus_online_get(zone_t *zone)
15670Sstevel@tonic-gate {
15680Sstevel@tonic-gate 	int myncpus_online = zone == NULL ? 0 : zone->zone_ncpus_online;
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	return (myncpus_online != 0 ? myncpus_online : ncpus_online);
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate /*
15740Sstevel@tonic-gate  * Return the pool to which the zone is currently bound.
15750Sstevel@tonic-gate  */
15760Sstevel@tonic-gate pool_t *
15770Sstevel@tonic-gate zone_pool_get(zone_t *zone)
15780Sstevel@tonic-gate {
15790Sstevel@tonic-gate 	ASSERT(pool_lock_held());
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	return (zone->zone_pool);
15820Sstevel@tonic-gate }
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate /*
15850Sstevel@tonic-gate  * Set the zone's pool pointer and update the zone's visibility to match
15860Sstevel@tonic-gate  * the resources in the new pool.
15870Sstevel@tonic-gate  */
15880Sstevel@tonic-gate void
15890Sstevel@tonic-gate zone_pool_set(zone_t *zone, pool_t *pool)
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate 	ASSERT(pool_lock_held());
15920Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	zone->zone_pool = pool;
15950Sstevel@tonic-gate 	zone_pset_set(zone, pool->pool_pset->pset_id);
15960Sstevel@tonic-gate }
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate /*
15990Sstevel@tonic-gate  * Return the cached value of the id of the processor set to which the
16000Sstevel@tonic-gate  * zone is currently bound.  The value will be ZONE_PS_INVAL if the pools
16010Sstevel@tonic-gate  * facility is disabled.
16020Sstevel@tonic-gate  */
16030Sstevel@tonic-gate psetid_t
16040Sstevel@tonic-gate zone_pset_get(zone_t *zone)
16050Sstevel@tonic-gate {
16060Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	return (zone->zone_psetid);
16090Sstevel@tonic-gate }
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate /*
16120Sstevel@tonic-gate  * Set the cached value of the id of the processor set to which the zone
16130Sstevel@tonic-gate  * is currently bound.  Also update the zone's visibility to match the
16140Sstevel@tonic-gate  * resources in the new processor set.
16150Sstevel@tonic-gate  */
16160Sstevel@tonic-gate void
16170Sstevel@tonic-gate zone_pset_set(zone_t *zone, psetid_t newpsetid)
16180Sstevel@tonic-gate {
16190Sstevel@tonic-gate 	psetid_t oldpsetid;
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
16220Sstevel@tonic-gate 	oldpsetid = zone_pset_get(zone);
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	if (oldpsetid == newpsetid)
16250Sstevel@tonic-gate 		return;
16260Sstevel@tonic-gate 	/*
16270Sstevel@tonic-gate 	 * Global zone sees all.
16280Sstevel@tonic-gate 	 */
16290Sstevel@tonic-gate 	if (zone != global_zone) {
16300Sstevel@tonic-gate 		zone->zone_psetid = newpsetid;
16310Sstevel@tonic-gate 		if (newpsetid != ZONE_PS_INVAL)
16320Sstevel@tonic-gate 			pool_pset_visibility_add(newpsetid, zone);
16330Sstevel@tonic-gate 		if (oldpsetid != ZONE_PS_INVAL)
16340Sstevel@tonic-gate 			pool_pset_visibility_remove(oldpsetid, zone);
16350Sstevel@tonic-gate 	}
16360Sstevel@tonic-gate 	/*
16370Sstevel@tonic-gate 	 * Disabling pools, so we should start using the global values
16380Sstevel@tonic-gate 	 * for ncpus and ncpus_online.
16390Sstevel@tonic-gate 	 */
16400Sstevel@tonic-gate 	if (newpsetid == ZONE_PS_INVAL) {
16410Sstevel@tonic-gate 		zone->zone_ncpus = 0;
16420Sstevel@tonic-gate 		zone->zone_ncpus_online = 0;
16430Sstevel@tonic-gate 	}
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate /*
16470Sstevel@tonic-gate  * Walk the list of active zones and issue the provided callback for
16480Sstevel@tonic-gate  * each of them.
16490Sstevel@tonic-gate  *
16500Sstevel@tonic-gate  * Caller must not be holding any locks that may be acquired under
16510Sstevel@tonic-gate  * zonehash_lock.  See comment at the beginning of the file for a list of
16520Sstevel@tonic-gate  * common locks and their interactions with zones.
16530Sstevel@tonic-gate  */
16540Sstevel@tonic-gate int
16550Sstevel@tonic-gate zone_walk(int (*cb)(zone_t *, void *), void *data)
16560Sstevel@tonic-gate {
16570Sstevel@tonic-gate 	zone_t *zone;
16580Sstevel@tonic-gate 	int ret = 0;
16590Sstevel@tonic-gate 	zone_status_t status;
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
16620Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
16630Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
16640Sstevel@tonic-gate 		/*
16650Sstevel@tonic-gate 		 * Skip zones that shouldn't be externally visible.
16660Sstevel@tonic-gate 		 */
16670Sstevel@tonic-gate 		status = zone_status_get(zone);
16680Sstevel@tonic-gate 		if (status < ZONE_IS_READY || status > ZONE_IS_DOWN)
16690Sstevel@tonic-gate 			continue;
16700Sstevel@tonic-gate 		/*
16710Sstevel@tonic-gate 		 * Bail immediately if any callback invocation returns a
16720Sstevel@tonic-gate 		 * non-zero value.
16730Sstevel@tonic-gate 		 */
16740Sstevel@tonic-gate 		ret = (*cb)(zone, data);
16750Sstevel@tonic-gate 		if (ret != 0)
16760Sstevel@tonic-gate 			break;
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
16790Sstevel@tonic-gate 	return (ret);
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate static int
16830Sstevel@tonic-gate zone_set_root(zone_t *zone, const char *upath)
16840Sstevel@tonic-gate {
16850Sstevel@tonic-gate 	vnode_t *vp;
16860Sstevel@tonic-gate 	int trycount;
16870Sstevel@tonic-gate 	int error = 0;
16880Sstevel@tonic-gate 	char *path;
16890Sstevel@tonic-gate 	struct pathname upn, pn;
16900Sstevel@tonic-gate 	size_t pathlen;
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	if ((error = pn_get((char *)upath, UIO_USERSPACE, &upn)) != 0)
16930Sstevel@tonic-gate 		return (error);
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 	pn_alloc(&pn);
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	/* prevent infinite loop */
16980Sstevel@tonic-gate 	trycount = 10;
16990Sstevel@tonic-gate 	for (;;) {
17000Sstevel@tonic-gate 		if (--trycount <= 0) {
17010Sstevel@tonic-gate 			error = ESTALE;
17020Sstevel@tonic-gate 			goto out;
17030Sstevel@tonic-gate 		}
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 		if ((error = lookuppn(&upn, &pn, FOLLOW, NULLVPP, &vp)) == 0) {
17060Sstevel@tonic-gate 			/*
17070Sstevel@tonic-gate 			 * VOP_ACCESS() may cover 'vp' with a new
17080Sstevel@tonic-gate 			 * filesystem, if 'vp' is an autoFS vnode.
17090Sstevel@tonic-gate 			 * Get the new 'vp' if so.
17100Sstevel@tonic-gate 			 */
17110Sstevel@tonic-gate 			if ((error = VOP_ACCESS(vp, VEXEC, 0, CRED())) == 0 &&
17120Sstevel@tonic-gate 			    (vp->v_vfsmountedhere == NULL ||
17130Sstevel@tonic-gate 			    (error = traverse(&vp)) == 0)) {
17140Sstevel@tonic-gate 				pathlen = pn.pn_pathlen + 2;
17150Sstevel@tonic-gate 				path = kmem_alloc(pathlen, KM_SLEEP);
17160Sstevel@tonic-gate 				(void) strncpy(path, pn.pn_path,
17170Sstevel@tonic-gate 				    pn.pn_pathlen + 1);
17180Sstevel@tonic-gate 				path[pathlen - 2] = '/';
17190Sstevel@tonic-gate 				path[pathlen - 1] = '\0';
17200Sstevel@tonic-gate 				pn_free(&pn);
17210Sstevel@tonic-gate 				pn_free(&upn);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 				/* Success! */
17240Sstevel@tonic-gate 				break;
17250Sstevel@tonic-gate 			}
17260Sstevel@tonic-gate 			VN_RELE(vp);
17270Sstevel@tonic-gate 		}
17280Sstevel@tonic-gate 		if (error != ESTALE)
17290Sstevel@tonic-gate 			goto out;
17300Sstevel@tonic-gate 	}
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	ASSERT(error == 0);
17330Sstevel@tonic-gate 	zone->zone_rootvp = vp;		/* we hold a reference to vp */
17340Sstevel@tonic-gate 	zone->zone_rootpath = path;
17350Sstevel@tonic-gate 	zone->zone_rootpathlen = pathlen;
17360Sstevel@tonic-gate 	return (0);
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate out:
17390Sstevel@tonic-gate 	pn_free(&pn);
17400Sstevel@tonic-gate 	pn_free(&upn);
17410Sstevel@tonic-gate 	return (error);
17420Sstevel@tonic-gate }
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate #define	isalnum(c)	(((c) >= '0' && (c) <= '9') || \
17450Sstevel@tonic-gate 			((c) >= 'a' && (c) <= 'z') || \
17460Sstevel@tonic-gate 			((c) >= 'A' && (c) <= 'Z'))
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate static int
17490Sstevel@tonic-gate zone_set_name(zone_t *zone, const char *uname)
17500Sstevel@tonic-gate {
17510Sstevel@tonic-gate 	char *kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
17520Sstevel@tonic-gate 	size_t len;
17530Sstevel@tonic-gate 	int i, err;
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	if ((err = copyinstr(uname, kname, ZONENAME_MAX, &len)) != 0) {
17560Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
17570Sstevel@tonic-gate 		return (err);	/* EFAULT or ENAMETOOLONG */
17580Sstevel@tonic-gate 	}
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	/* must be less than ZONENAME_MAX */
17610Sstevel@tonic-gate 	if (len == ZONENAME_MAX && kname[ZONENAME_MAX - 1] != '\0') {
17620Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
17630Sstevel@tonic-gate 		return (EINVAL);
17640Sstevel@tonic-gate 	}
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	/*
17670Sstevel@tonic-gate 	 * Name must start with an alphanumeric and must contain only
17680Sstevel@tonic-gate 	 * alphanumerics, '-', '_' and '.'.
17690Sstevel@tonic-gate 	 */
17700Sstevel@tonic-gate 	if (!isalnum(kname[0])) {
17710Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
17720Sstevel@tonic-gate 		return (EINVAL);
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate 	for (i = 1; i < len - 1; i++) {
17750Sstevel@tonic-gate 		if (!isalnum(kname[i]) && kname[i] != '-' && kname[i] != '_' &&
17760Sstevel@tonic-gate 		    kname[i] != '.') {
17770Sstevel@tonic-gate 			kmem_free(kname, ZONENAME_MAX);
17780Sstevel@tonic-gate 			return (EINVAL);
17790Sstevel@tonic-gate 		}
17800Sstevel@tonic-gate 	}
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	zone->zone_name = kname;
17830Sstevel@tonic-gate 	return (0);
17840Sstevel@tonic-gate }
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate /*
17870Sstevel@tonic-gate  * Similar to thread_create(), but makes sure the thread is in the appropriate
17880Sstevel@tonic-gate  * zone's zsched process (curproc->p_zone->zone_zsched) before returning.
17890Sstevel@tonic-gate  */
17900Sstevel@tonic-gate /*ARGSUSED*/
17910Sstevel@tonic-gate kthread_t *
17920Sstevel@tonic-gate zthread_create(
17930Sstevel@tonic-gate     caddr_t stk,
17940Sstevel@tonic-gate     size_t stksize,
17950Sstevel@tonic-gate     void (*proc)(),
17960Sstevel@tonic-gate     void *arg,
17970Sstevel@tonic-gate     size_t len,
17980Sstevel@tonic-gate     pri_t pri)
17990Sstevel@tonic-gate {
18000Sstevel@tonic-gate 	kthread_t *t;
18010Sstevel@tonic-gate 	zone_t *zone = curproc->p_zone;
18020Sstevel@tonic-gate 	proc_t *pp = zone->zone_zsched;
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	zone_hold(zone);	/* Reference to be dropped when thread exits */
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	/*
18070Sstevel@tonic-gate 	 * No-one should be trying to create threads if the zone is shutting
18080Sstevel@tonic-gate 	 * down and there aren't any kernel threads around.  See comment
18090Sstevel@tonic-gate 	 * in zthread_exit().
18100Sstevel@tonic-gate 	 */
18110Sstevel@tonic-gate 	ASSERT(!(zone->zone_kthreads == NULL &&
18120Sstevel@tonic-gate 	    zone_status_get(zone) >= ZONE_IS_EMPTY));
18130Sstevel@tonic-gate 	/*
18140Sstevel@tonic-gate 	 * Create a thread, but don't let it run until we've finished setting
18150Sstevel@tonic-gate 	 * things up.
18160Sstevel@tonic-gate 	 */
18170Sstevel@tonic-gate 	t = thread_create(stk, stksize, proc, arg, len, pp, TS_STOPPED, pri);
18180Sstevel@tonic-gate 	ASSERT(t->t_forw == NULL);
18190Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18200Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL) {
18210Sstevel@tonic-gate 		t->t_forw = t->t_back = t;
18220Sstevel@tonic-gate 	} else {
18230Sstevel@tonic-gate 		kthread_t *tx = zone->zone_kthreads;
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 		t->t_forw = tx;
18260Sstevel@tonic-gate 		t->t_back = tx->t_back;
18270Sstevel@tonic-gate 		tx->t_back->t_forw = t;
18280Sstevel@tonic-gate 		tx->t_back = t;
18290Sstevel@tonic-gate 	}
18300Sstevel@tonic-gate 	zone->zone_kthreads = t;
18310Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
18340Sstevel@tonic-gate 	t->t_proc_flag |= TP_ZTHREAD;
18350Sstevel@tonic-gate 	project_rele(t->t_proj);
18360Sstevel@tonic-gate 	t->t_proj = project_hold(pp->p_task->tk_proj);
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	/*
18390Sstevel@tonic-gate 	 * Setup complete, let it run.
18400Sstevel@tonic-gate 	 */
18410Sstevel@tonic-gate 	thread_lock(t);
18420Sstevel@tonic-gate 	t->t_schedflag |= TS_ALLSTART;
18430Sstevel@tonic-gate 	setrun_locked(t);
18440Sstevel@tonic-gate 	thread_unlock(t);
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	return (t);
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate /*
18520Sstevel@tonic-gate  * Similar to thread_exit().  Must be called by threads created via
18530Sstevel@tonic-gate  * zthread_exit().
18540Sstevel@tonic-gate  */
18550Sstevel@tonic-gate void
18560Sstevel@tonic-gate zthread_exit(void)
18570Sstevel@tonic-gate {
18580Sstevel@tonic-gate 	kthread_t *t = curthread;
18590Sstevel@tonic-gate 	proc_t *pp = curproc;
18600Sstevel@tonic-gate 	zone_t *zone = pp->p_zone;
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 	/*
18650Sstevel@tonic-gate 	 * Reparent to p0
18660Sstevel@tonic-gate 	 */
18670Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
18680Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_ZTHREAD;
18690Sstevel@tonic-gate 	t->t_procp = &p0;
18700Sstevel@tonic-gate 	hat_thread_exit(t);
18710Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 	if (t->t_back == t) {
18740Sstevel@tonic-gate 		ASSERT(t->t_forw == t);
18750Sstevel@tonic-gate 		/*
18760Sstevel@tonic-gate 		 * If the zone is empty, once the thread count
18770Sstevel@tonic-gate 		 * goes to zero no further kernel threads can be
18780Sstevel@tonic-gate 		 * created.  This is because if the creator is a process
18790Sstevel@tonic-gate 		 * in the zone, then it must have exited before the zone
18800Sstevel@tonic-gate 		 * state could be set to ZONE_IS_EMPTY.
18810Sstevel@tonic-gate 		 * Otherwise, if the creator is a kernel thread in the
18820Sstevel@tonic-gate 		 * zone, the thread count is non-zero.
18830Sstevel@tonic-gate 		 *
18840Sstevel@tonic-gate 		 * This really means that non-zone kernel threads should
18850Sstevel@tonic-gate 		 * not create zone kernel threads.
18860Sstevel@tonic-gate 		 */
18870Sstevel@tonic-gate 		zone->zone_kthreads = NULL;
18880Sstevel@tonic-gate 		if (zone_status_get(zone) == ZONE_IS_EMPTY) {
18890Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_DOWN);
18900Sstevel@tonic-gate 		}
18910Sstevel@tonic-gate 	} else {
18920Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
18930Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
18940Sstevel@tonic-gate 		if (zone->zone_kthreads == t)
18950Sstevel@tonic-gate 			zone->zone_kthreads = t->t_forw;
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
18980Sstevel@tonic-gate 	zone_rele(zone);
18990Sstevel@tonic-gate 	thread_exit();
19000Sstevel@tonic-gate 	/* NOTREACHED */
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate static void
19040Sstevel@tonic-gate zone_chdir(vnode_t *vp, vnode_t **vpp, proc_t *pp)
19050Sstevel@tonic-gate {
19060Sstevel@tonic-gate 	vnode_t *oldvp;
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	/* we're going to hold a reference here to the directory */
19090Sstevel@tonic-gate 	VN_HOLD(vp);
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate #ifdef C2_AUDIT
19120Sstevel@tonic-gate 	if (audit_active)	/* update abs cwd/root path see c2audit.c */
19130Sstevel@tonic-gate 		audit_chdirec(vp, vpp);
19140Sstevel@tonic-gate #endif
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
19170Sstevel@tonic-gate 	oldvp = *vpp;
19180Sstevel@tonic-gate 	*vpp = vp;
19190Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
19200Sstevel@tonic-gate 	if (oldvp != NULL)
19210Sstevel@tonic-gate 		VN_RELE(oldvp);
19220Sstevel@tonic-gate }
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate /*
19250Sstevel@tonic-gate  * Convert an rctl value represented by an nvlist_t into an rctl_val_t.
19260Sstevel@tonic-gate  */
19270Sstevel@tonic-gate static int
19280Sstevel@tonic-gate nvlist2rctlval(nvlist_t *nvl, rctl_val_t *rv)
19290Sstevel@tonic-gate {
19300Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
19310Sstevel@tonic-gate 	boolean_t priv_set = B_FALSE;
19320Sstevel@tonic-gate 	boolean_t limit_set = B_FALSE;
19330Sstevel@tonic-gate 	boolean_t action_set = B_FALSE;
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
19360Sstevel@tonic-gate 		const char *name;
19370Sstevel@tonic-gate 		uint64_t ui64;
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 		name = nvpair_name(nvp);
19400Sstevel@tonic-gate 		if (nvpair_type(nvp) != DATA_TYPE_UINT64)
19410Sstevel@tonic-gate 			return (EINVAL);
19420Sstevel@tonic-gate 		(void) nvpair_value_uint64(nvp, &ui64);
19430Sstevel@tonic-gate 		if (strcmp(name, "privilege") == 0) {
19440Sstevel@tonic-gate 			/*
19450Sstevel@tonic-gate 			 * Currently only privileged values are allowed, but
19460Sstevel@tonic-gate 			 * this may change in the future.
19470Sstevel@tonic-gate 			 */
19480Sstevel@tonic-gate 			if (ui64 != RCPRIV_PRIVILEGED)
19490Sstevel@tonic-gate 				return (EINVAL);
19500Sstevel@tonic-gate 			rv->rcv_privilege = ui64;
19510Sstevel@tonic-gate 			priv_set = B_TRUE;
19520Sstevel@tonic-gate 		} else if (strcmp(name, "limit") == 0) {
19530Sstevel@tonic-gate 			rv->rcv_value = ui64;
19540Sstevel@tonic-gate 			limit_set = B_TRUE;
19550Sstevel@tonic-gate 		} else if (strcmp(name, "action") == 0) {
19560Sstevel@tonic-gate 			if (ui64 != RCTL_LOCAL_NOACTION &&
19570Sstevel@tonic-gate 			    ui64 != RCTL_LOCAL_DENY)
19580Sstevel@tonic-gate 				return (EINVAL);
19590Sstevel@tonic-gate 			rv->rcv_flagaction = ui64;
19600Sstevel@tonic-gate 			action_set = B_TRUE;
19610Sstevel@tonic-gate 		} else {
19620Sstevel@tonic-gate 			return (EINVAL);
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 	}
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 	if (!(priv_set && limit_set && action_set))
19670Sstevel@tonic-gate 		return (EINVAL);
19680Sstevel@tonic-gate 	rv->rcv_action_signal = 0;
19690Sstevel@tonic-gate 	rv->rcv_action_recipient = NULL;
19700Sstevel@tonic-gate 	rv->rcv_action_recip_pid = -1;
19710Sstevel@tonic-gate 	rv->rcv_firing_time = 0;
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	return (0);
19740Sstevel@tonic-gate }
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate void
19770Sstevel@tonic-gate zone_icode(void)
19780Sstevel@tonic-gate {
19790Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
19800Sstevel@tonic-gate 	struct core_globals	*cg;
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	/*
19830Sstevel@tonic-gate 	 * For all purposes (ZONE_ATTR_INITPID and restart_init),
19840Sstevel@tonic-gate 	 * storing just the pid of init is sufficient.
19850Sstevel@tonic-gate 	 */
19860Sstevel@tonic-gate 	p->p_zone->zone_proc_initpid = p->p_pid;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 	/*
19890Sstevel@tonic-gate 	 * Allocate user address space and stack segment
19900Sstevel@tonic-gate 	 */
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	p->p_cstime = p->p_stime = p->p_cutime = p->p_utime = 0;
19930Sstevel@tonic-gate 	p->p_usrstack = (caddr_t)USRSTACK32;
19940Sstevel@tonic-gate 	p->p_model = DATAMODEL_ILP32;
19950Sstevel@tonic-gate 	p->p_stkprot = PROT_ZFOD & ~PROT_EXEC;
19960Sstevel@tonic-gate 	p->p_datprot = PROT_ZFOD & ~PROT_EXEC;
19970Sstevel@tonic-gate 	p->p_stk_ctl = INT32_MAX;
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	p->p_as = as_alloc();
20000Sstevel@tonic-gate 	p->p_as->a_userlimit = (caddr_t)USERLIMIT32;
20010Sstevel@tonic-gate 	(void) hat_setup(p->p_as->a_hat, HAT_INIT);
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	cg = zone_getspecific(core_zone_key, p->p_zone);
20040Sstevel@tonic-gate 	ASSERT(cg != NULL);
20050Sstevel@tonic-gate 	corectl_path_hold(cg->core_default_path);
20060Sstevel@tonic-gate 	corectl_content_hold(cg->core_default_content);
20070Sstevel@tonic-gate 	p->p_corefile = cg->core_default_path;
20080Sstevel@tonic-gate 	p->p_content = cg->core_default_content;
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 	init_mstate(curthread, LMS_SYSTEM);
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 	p->p_zone->zone_boot_err = exec_init(zone_initname, 0,
20130Sstevel@tonic-gate 	    p->p_zone->zone_bootargs);
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
20160Sstevel@tonic-gate 	if (p->p_zone->zone_boot_err != 0) {
20170Sstevel@tonic-gate 		/*
20180Sstevel@tonic-gate 		 * Make sure we are still in the booting state-- we could have
20190Sstevel@tonic-gate 		 * raced and already be shutting down, or even further along.
20200Sstevel@tonic-gate 		 */
20210Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
20220Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_SHUTTING_DOWN);
20230Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
20240Sstevel@tonic-gate 		/* It's gone bad, dispose of the process */
20250Sstevel@tonic-gate 		if (proc_exit(CLD_EXITED, p->p_zone->zone_boot_err) != 0) {
2026390Sraf 			mutex_enter(&p->p_lock);
2027390Sraf 			ASSERT(p->p_flag & SEXITLWPS);
20280Sstevel@tonic-gate 			lwp_exit();
20290Sstevel@tonic-gate 		}
20300Sstevel@tonic-gate 	} else {
20310Sstevel@tonic-gate 		if (zone_status_get(p->p_zone) == ZONE_IS_BOOTING)
20320Sstevel@tonic-gate 			zone_status_set(p->p_zone, ZONE_IS_RUNNING);
20330Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
20340Sstevel@tonic-gate 		/* cause the process to return to userland. */
20350Sstevel@tonic-gate 		lwp_rtt();
20360Sstevel@tonic-gate 	}
20370Sstevel@tonic-gate }
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate struct zsched_arg {
20400Sstevel@tonic-gate 	zone_t *zone;
20410Sstevel@tonic-gate 	nvlist_t *nvlist;
20420Sstevel@tonic-gate };
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate /*
20450Sstevel@tonic-gate  * Per-zone "sched" workalike.  The similarity to "sched" doesn't have
20460Sstevel@tonic-gate  * anything to do with scheduling, but rather with the fact that
20470Sstevel@tonic-gate  * per-zone kernel threads are parented to zsched, just like regular
20480Sstevel@tonic-gate  * kernel threads are parented to sched (p0).
20490Sstevel@tonic-gate  *
20500Sstevel@tonic-gate  * zsched is also responsible for launching init for the zone.
20510Sstevel@tonic-gate  */
20520Sstevel@tonic-gate static void
20530Sstevel@tonic-gate zsched(void *arg)
20540Sstevel@tonic-gate {
20550Sstevel@tonic-gate 	struct zsched_arg *za = arg;
20560Sstevel@tonic-gate 	proc_t *pp = curproc;
20570Sstevel@tonic-gate 	proc_t *initp = proc_init;
20580Sstevel@tonic-gate 	zone_t *zone = za->zone;
20590Sstevel@tonic-gate 	cred_t *cr, *oldcred;
20600Sstevel@tonic-gate 	rctl_set_t *set;
20610Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
20620Sstevel@tonic-gate 	contract_t *ct = NULL;
20630Sstevel@tonic-gate 	task_t *tk, *oldtk;
20640Sstevel@tonic-gate 	rctl_entity_p_t e;
20650Sstevel@tonic-gate 	kproject_t *pj;
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	nvlist_t *nvl = za->nvlist;
20680Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 	bcopy("zsched", u.u_psargs, sizeof ("zsched"));
20710Sstevel@tonic-gate 	bcopy("zsched", u.u_comm, sizeof ("zsched"));
20720Sstevel@tonic-gate 	u.u_argc = 0;
20730Sstevel@tonic-gate 	u.u_argv = NULL;
20740Sstevel@tonic-gate 	u.u_envp = NULL;
20750Sstevel@tonic-gate 	closeall(P_FINFO(pp));
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	/*
20780Sstevel@tonic-gate 	 * We are this zone's "zsched" process.  As the zone isn't generally
20790Sstevel@tonic-gate 	 * visible yet we don't need to grab any locks before initializing its
20800Sstevel@tonic-gate 	 * zone_proc pointer.
20810Sstevel@tonic-gate 	 */
20820Sstevel@tonic-gate 	zone_hold(zone);  /* this hold is released by zone_destroy() */
20830Sstevel@tonic-gate 	zone->zone_zsched = pp;
20840Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
20850Sstevel@tonic-gate 	pp->p_zone = zone;
20860Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	/*
20890Sstevel@tonic-gate 	 * Disassociate process from its 'parent'; parent ourselves to init
20900Sstevel@tonic-gate 	 * (pid 1) and change other values as needed.
20910Sstevel@tonic-gate 	 */
20920Sstevel@tonic-gate 	sess_create();
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	mutex_enter(&pidlock);
20950Sstevel@tonic-gate 	proc_detach(pp);
20960Sstevel@tonic-gate 	pp->p_ppid = 1;
20970Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
20980Sstevel@tonic-gate 	pp->p_ancpid = 1;
20990Sstevel@tonic-gate 	pp->p_parent = initp;
21000Sstevel@tonic-gate 	pp->p_psibling = NULL;
21010Sstevel@tonic-gate 	if (initp->p_child)
21020Sstevel@tonic-gate 		initp->p_child->p_psibling = pp;
21030Sstevel@tonic-gate 	pp->p_sibling = initp->p_child;
21040Sstevel@tonic-gate 	initp->p_child = pp;
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 	/* Decrement what newproc() incremented. */
21070Sstevel@tonic-gate 	upcount_dec(crgetruid(CRED()), GLOBAL_ZONEID);
21080Sstevel@tonic-gate 	/*
21090Sstevel@tonic-gate 	 * Our credentials are about to become kcred-like, so we don't care
21100Sstevel@tonic-gate 	 * about the caller's ruid.
21110Sstevel@tonic-gate 	 */
21120Sstevel@tonic-gate 	upcount_inc(crgetruid(kcred), zone->zone_id);
21130Sstevel@tonic-gate 	mutex_exit(&pidlock);
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 	/*
21160Sstevel@tonic-gate 	 * getting out of global zone, so decrement lwp counts
21170Sstevel@tonic-gate 	 */
21180Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
21190Sstevel@tonic-gate 	mutex_enter(&global_zone->zone_nlwps_lock);
21200Sstevel@tonic-gate 	pj->kpj_nlwps -= pp->p_lwpcnt;
21210Sstevel@tonic-gate 	global_zone->zone_nlwps -= pp->p_lwpcnt;
21220Sstevel@tonic-gate 	mutex_exit(&global_zone->zone_nlwps_lock);
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 	/*
21250Sstevel@tonic-gate 	 * Create and join a new task in project '0' of this zone.
21260Sstevel@tonic-gate 	 *
21270Sstevel@tonic-gate 	 * We don't need to call holdlwps() since we know we're the only lwp in
21280Sstevel@tonic-gate 	 * this process.
21290Sstevel@tonic-gate 	 *
21300Sstevel@tonic-gate 	 * task_join() returns with p_lock held.
21310Sstevel@tonic-gate 	 */
21320Sstevel@tonic-gate 	tk = task_create(0, zone);
21330Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
21340Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
21350Sstevel@tonic-gate 	mutex_exit(&curproc->p_lock);
21360Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
21370Sstevel@tonic-gate 	task_rele(oldtk);
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	/*
21400Sstevel@tonic-gate 	 * add lwp counts to zsched's zone, and increment project's task count
21410Sstevel@tonic-gate 	 * due to the task created in the above tasksys_settaskid
21420Sstevel@tonic-gate 	 */
21430Sstevel@tonic-gate 	pj = pp->p_task->tk_proj;
21440Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
21450Sstevel@tonic-gate 	pj->kpj_nlwps += pp->p_lwpcnt;
21460Sstevel@tonic-gate 	pj->kpj_ntasks += 1;
21470Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
21480Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 	/*
21510Sstevel@tonic-gate 	 * The process was created by a process in the global zone, hence the
21520Sstevel@tonic-gate 	 * credentials are wrong.  We might as well have kcred-ish credentials.
21530Sstevel@tonic-gate 	 */
21540Sstevel@tonic-gate 	cr = zone->zone_kcred;
21550Sstevel@tonic-gate 	crhold(cr);
21560Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
21570Sstevel@tonic-gate 	oldcred = pp->p_cred;
21580Sstevel@tonic-gate 	pp->p_cred = cr;
21590Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
21600Sstevel@tonic-gate 	crfree(oldcred);
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 	/*
21630Sstevel@tonic-gate 	 * Hold credentials again (for thread)
21640Sstevel@tonic-gate 	 */
21650Sstevel@tonic-gate 	crhold(cr);
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	/*
21680Sstevel@tonic-gate 	 * p_lwpcnt can't change since this is a kernel process.
21690Sstevel@tonic-gate 	 */
21700Sstevel@tonic-gate 	crset(pp, cr);
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 	/*
21730Sstevel@tonic-gate 	 * Chroot
21740Sstevel@tonic-gate 	 */
21750Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_cdir, pp);
21760Sstevel@tonic-gate 	zone_chdir(zone->zone_rootvp, &PTOU(pp)->u_rdir, pp);
21770Sstevel@tonic-gate 
21780Sstevel@tonic-gate 	/*
21790Sstevel@tonic-gate 	 * Initialize zone's rctl set.
21800Sstevel@tonic-gate 	 */
21810Sstevel@tonic-gate 	set = rctl_set_create();
21820Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_ZONE);
21830Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
21840Sstevel@tonic-gate 	e.rcep_p.zone = zone;
21850Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
21860Sstevel@tonic-gate 	zone->zone_rctls = rctl_set_init(RCENTITY_ZONE, pp, &e, set, gp);
21870Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
21880Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	/*
21910Sstevel@tonic-gate 	 * Apply the rctls passed in to zone_create().  This is basically a list
21920Sstevel@tonic-gate 	 * assignment: all of the old values are removed and the new ones
21930Sstevel@tonic-gate 	 * inserted.  That is, if an empty list is passed in, all values are
21940Sstevel@tonic-gate 	 * removed.
21950Sstevel@tonic-gate 	 */
21960Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
21970Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
21980Sstevel@tonic-gate 		rctl_hndl_t hndl;
21990Sstevel@tonic-gate 		char *name;
22000Sstevel@tonic-gate 		nvlist_t **nvlarray;
22010Sstevel@tonic-gate 		uint_t i, nelem;
22020Sstevel@tonic-gate 		int error;	/* For ASSERT()s */
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 		name = nvpair_name(nvp);
22050Sstevel@tonic-gate 		hndl = rctl_hndl_lookup(name);
22060Sstevel@tonic-gate 		ASSERT(hndl != -1);
22070Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
22080Sstevel@tonic-gate 		ASSERT(rde != NULL);
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 		for (; /* ever */; ) {
22110Sstevel@tonic-gate 			rctl_val_t oval;
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
22140Sstevel@tonic-gate 			error = rctl_local_get(hndl, NULL, &oval, pp);
22150Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
22160Sstevel@tonic-gate 			ASSERT(error == 0);	/* Can't fail for RCTL_FIRST */
22170Sstevel@tonic-gate 			ASSERT(oval.rcv_privilege != RCPRIV_BASIC);
22180Sstevel@tonic-gate 			if (oval.rcv_privilege == RCPRIV_SYSTEM)
22190Sstevel@tonic-gate 				break;
22200Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
22210Sstevel@tonic-gate 			error = rctl_local_delete(hndl, &oval, pp);
22220Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
22230Sstevel@tonic-gate 			ASSERT(error == 0);
22240Sstevel@tonic-gate 		}
22250Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
22260Sstevel@tonic-gate 		ASSERT(error == 0);
22270Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
22280Sstevel@tonic-gate 			rctl_val_t *nvalp;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 			nvalp = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
22310Sstevel@tonic-gate 			error = nvlist2rctlval(nvlarray[i], nvalp);
22320Sstevel@tonic-gate 			ASSERT(error == 0);
22330Sstevel@tonic-gate 			/*
22340Sstevel@tonic-gate 			 * rctl_local_insert can fail if the value being
22350Sstevel@tonic-gate 			 * inserted is a duplicate; this is OK.
22360Sstevel@tonic-gate 			 */
22370Sstevel@tonic-gate 			mutex_enter(&pp->p_lock);
22380Sstevel@tonic-gate 			if (rctl_local_insert(hndl, nvalp, pp) != 0)
22390Sstevel@tonic-gate 				kmem_cache_free(rctl_val_cache, nvalp);
22400Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
22410Sstevel@tonic-gate 		}
22420Sstevel@tonic-gate 	}
22430Sstevel@tonic-gate 	/*
22440Sstevel@tonic-gate 	 * Tell the world that we're done setting up.
22450Sstevel@tonic-gate 	 *
22460Sstevel@tonic-gate 	 * At this point we want to set the zone status to ZONE_IS_READY
22470Sstevel@tonic-gate 	 * and atomically set the zone's processor set visibility.  Once
22480Sstevel@tonic-gate 	 * we drop pool_lock() this zone will automatically get updated
22490Sstevel@tonic-gate 	 * to reflect any future changes to the pools configuration.
22500Sstevel@tonic-gate 	 */
22510Sstevel@tonic-gate 	pool_lock();
22520Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
22530Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
22540Sstevel@tonic-gate 	zone_uniqid(zone);
22550Sstevel@tonic-gate 	zone_zsd_configure(zone);
22560Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED)
22570Sstevel@tonic-gate 		zone_pset_set(zone, pool_default->pool_pset->pset_id);
22580Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
22590Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) == ZONE_IS_UNINITIALIZED);
22600Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_READY);
22610Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
22620Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
22630Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
22640Sstevel@tonic-gate 	pool_unlock();
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	/*
22670Sstevel@tonic-gate 	 * Once we see the zone transition to the ZONE_IS_BOOTING state,
22680Sstevel@tonic-gate 	 * we launch init, and set the state to running.
22690Sstevel@tonic-gate 	 */
22700Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_BOOTING, "zsched");
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	if (zone_status_get(zone) == ZONE_IS_BOOTING) {
22730Sstevel@tonic-gate 		id_t cid;
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 		/*
22760Sstevel@tonic-gate 		 * Ok, this is a little complicated.  We need to grab the
22770Sstevel@tonic-gate 		 * zone's pool's scheduling class ID; note that by now, we
22780Sstevel@tonic-gate 		 * are already bound to a pool if we need to be (zoneadmd
22790Sstevel@tonic-gate 		 * will have done that to us while we're in the READY
22800Sstevel@tonic-gate 		 * state).  *But* the scheduling class for the zone's 'init'
22810Sstevel@tonic-gate 		 * must be explicitly passed to newproc, which doesn't
22820Sstevel@tonic-gate 		 * respect pool bindings.
22830Sstevel@tonic-gate 		 *
22840Sstevel@tonic-gate 		 * We hold the pool_lock across the call to newproc() to
22850Sstevel@tonic-gate 		 * close the obvious race: the pool's scheduling class
22860Sstevel@tonic-gate 		 * could change before we manage to create the LWP with
22870Sstevel@tonic-gate 		 * classid 'cid'.
22880Sstevel@tonic-gate 		 */
22890Sstevel@tonic-gate 		pool_lock();
22900Sstevel@tonic-gate 		cid = pool_get_class(zone->zone_pool);
22910Sstevel@tonic-gate 		if (cid == -1)
22920Sstevel@tonic-gate 			cid = defaultcid;
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 		/*
22950Sstevel@tonic-gate 		 * If this fails, zone_boot will ultimately fail.  The
22960Sstevel@tonic-gate 		 * state of the zone will be set to SHUTTING_DOWN-- userland
22970Sstevel@tonic-gate 		 * will have to tear down the zone, and fail, or try again.
22980Sstevel@tonic-gate 		 */
22990Sstevel@tonic-gate 		if ((zone->zone_boot_err = newproc(zone_icode, NULL, cid,
23000Sstevel@tonic-gate 		    minclsyspri - 1, &ct)) != 0) {
23010Sstevel@tonic-gate 			mutex_enter(&zone_status_lock);
23020Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
23030Sstevel@tonic-gate 			mutex_exit(&zone_status_lock);
23040Sstevel@tonic-gate 		}
23050Sstevel@tonic-gate 		pool_unlock();
23060Sstevel@tonic-gate 	}
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 	/*
23090Sstevel@tonic-gate 	 * Wait for zone_destroy() to be called.  This is what we spend
23100Sstevel@tonic-gate 	 * most of our life doing.
23110Sstevel@tonic-gate 	 */
23120Sstevel@tonic-gate 	zone_status_wait_cpr(zone, ZONE_IS_DYING, "zsched");
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 	if (ct)
23150Sstevel@tonic-gate 		/*
23160Sstevel@tonic-gate 		 * At this point the process contract should be empty.
23170Sstevel@tonic-gate 		 * (Though if it isn't, it's not the end of the world.)
23180Sstevel@tonic-gate 		 */
23190Sstevel@tonic-gate 		VERIFY(contract_abandon(ct, curproc, B_TRUE) == 0);
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	/*
23220Sstevel@tonic-gate 	 * Allow kcred to be freed when all referring processes
23230Sstevel@tonic-gate 	 * (including this one) go away.  We can't just do this in
23240Sstevel@tonic-gate 	 * zone_free because we need to wait for the zone_cred_ref to
23250Sstevel@tonic-gate 	 * drop to 0 before calling zone_free, and the existence of
23260Sstevel@tonic-gate 	 * zone_kcred will prevent that.  Thus, we call crfree here to
23270Sstevel@tonic-gate 	 * balance the crdup in zone_create.  The crhold calls earlier
23280Sstevel@tonic-gate 	 * in zsched will be dropped when the thread and process exit.
23290Sstevel@tonic-gate 	 */
23300Sstevel@tonic-gate 	crfree(zone->zone_kcred);
23310Sstevel@tonic-gate 	zone->zone_kcred = NULL;
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
23340Sstevel@tonic-gate }
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate /*
23370Sstevel@tonic-gate  * Helper function to determine if there are any submounts of the
23380Sstevel@tonic-gate  * provided path.  Used to make sure the zone doesn't "inherit" any
23390Sstevel@tonic-gate  * mounts from before it is created.
23400Sstevel@tonic-gate  */
23410Sstevel@tonic-gate static uint_t
23420Sstevel@tonic-gate zone_mount_count(const char *rootpath)
23430Sstevel@tonic-gate {
23440Sstevel@tonic-gate 	vfs_t *vfsp;
23450Sstevel@tonic-gate 	uint_t count = 0;
23460Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	/*
23490Sstevel@tonic-gate 	 * Holding zonehash_lock prevents race conditions with
23500Sstevel@tonic-gate 	 * vfs_list_add()/vfs_list_remove() since we serialize with
23510Sstevel@tonic-gate 	 * zone_find_by_path().
23520Sstevel@tonic-gate 	 */
23530Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
23540Sstevel@tonic-gate 	/*
23550Sstevel@tonic-gate 	 * The rootpath must end with a '/'
23560Sstevel@tonic-gate 	 */
23570Sstevel@tonic-gate 	ASSERT(rootpath[rootpathlen - 1] == '/');
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	/*
23600Sstevel@tonic-gate 	 * This intentionally does not count the rootpath itself if that
23610Sstevel@tonic-gate 	 * happens to be a mount point.
23620Sstevel@tonic-gate 	 */
23630Sstevel@tonic-gate 	vfs_list_read_lock();
23640Sstevel@tonic-gate 	vfsp = rootvfs;
23650Sstevel@tonic-gate 	do {
23660Sstevel@tonic-gate 		if (strncmp(rootpath, refstr_value(vfsp->vfs_mntpt),
23670Sstevel@tonic-gate 		    rootpathlen) == 0)
23680Sstevel@tonic-gate 			count++;
23690Sstevel@tonic-gate 		vfsp = vfsp->vfs_next;
23700Sstevel@tonic-gate 	} while (vfsp != rootvfs);
23710Sstevel@tonic-gate 	vfs_list_unlock();
23720Sstevel@tonic-gate 	return (count);
23730Sstevel@tonic-gate }
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate /*
23760Sstevel@tonic-gate  * Helper function to make sure that a zone created on 'rootpath'
23770Sstevel@tonic-gate  * wouldn't end up containing other zones' rootpaths.
23780Sstevel@tonic-gate  */
23790Sstevel@tonic-gate static boolean_t
23800Sstevel@tonic-gate zone_is_nested(const char *rootpath)
23810Sstevel@tonic-gate {
23820Sstevel@tonic-gate 	zone_t *zone;
23830Sstevel@tonic-gate 	size_t rootpathlen = strlen(rootpath);
23840Sstevel@tonic-gate 	size_t len;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&zonehash_lock));
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	for (zone = list_head(&zone_active); zone != NULL;
23890Sstevel@tonic-gate 	    zone = list_next(&zone_active, zone)) {
23900Sstevel@tonic-gate 		if (zone == global_zone)
23910Sstevel@tonic-gate 			continue;
23920Sstevel@tonic-gate 		len = strlen(zone->zone_rootpath);
23930Sstevel@tonic-gate 		if (strncmp(rootpath, zone->zone_rootpath,
23940Sstevel@tonic-gate 		    MIN(rootpathlen, len)) == 0)
23950Sstevel@tonic-gate 			return (B_TRUE);
23960Sstevel@tonic-gate 	}
23970Sstevel@tonic-gate 	return (B_FALSE);
23980Sstevel@tonic-gate }
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate static int
24010Sstevel@tonic-gate zone_set_privset(zone_t *zone, const priv_set_t *zone_privs)
24020Sstevel@tonic-gate {
24030Sstevel@tonic-gate 	priv_set_t *privs = kmem_alloc(sizeof (priv_set_t), KM_SLEEP);
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	if (copyin(zone_privs, privs, sizeof (priv_set_t))) {
24060Sstevel@tonic-gate 		kmem_free(privs, sizeof (priv_set_t));
24070Sstevel@tonic-gate 		return (EFAULT);
24080Sstevel@tonic-gate 	}
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 	zone->zone_privset = privs;
24110Sstevel@tonic-gate 	return (0);
24120Sstevel@tonic-gate }
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate /*
24150Sstevel@tonic-gate  * We make creative use of nvlists to pass in rctls from userland.  The list is
24160Sstevel@tonic-gate  * a list of the following structures:
24170Sstevel@tonic-gate  *
24180Sstevel@tonic-gate  * (name = rctl_name, value = nvpair_list_array)
24190Sstevel@tonic-gate  *
24200Sstevel@tonic-gate  * Where each element of the nvpair_list_array is of the form:
24210Sstevel@tonic-gate  *
24220Sstevel@tonic-gate  * [(name = "privilege", value = RCPRIV_PRIVILEGED),
24230Sstevel@tonic-gate  * 	(name = "limit", value = uint64_t),
24240Sstevel@tonic-gate  * 	(name = "action", value = (RCTL_LOCAL_NOACTION || RCTL_LOCAL_DENY))]
24250Sstevel@tonic-gate  */
24260Sstevel@tonic-gate static int
24270Sstevel@tonic-gate parse_rctls(caddr_t ubuf, size_t buflen, nvlist_t **nvlp)
24280Sstevel@tonic-gate {
24290Sstevel@tonic-gate 	nvpair_t *nvp = NULL;
24300Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
24310Sstevel@tonic-gate 	char *kbuf;
24320Sstevel@tonic-gate 	int error;
24330Sstevel@tonic-gate 	rctl_val_t rv;
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 	*nvlp = NULL;
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	if (buflen == 0)
24380Sstevel@tonic-gate 		return (0);
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	if ((kbuf = kmem_alloc(buflen, KM_NOSLEEP)) == NULL)
24410Sstevel@tonic-gate 		return (ENOMEM);
24420Sstevel@tonic-gate 	if (copyin(ubuf, kbuf, buflen)) {
24430Sstevel@tonic-gate 		error = EFAULT;
24440Sstevel@tonic-gate 		goto out;
24450Sstevel@tonic-gate 	}
24460Sstevel@tonic-gate 	if (nvlist_unpack(kbuf, buflen, &nvl, KM_SLEEP) != 0) {
24470Sstevel@tonic-gate 		/*
24480Sstevel@tonic-gate 		 * nvl may have been allocated/free'd, but the value set to
24490Sstevel@tonic-gate 		 * non-NULL, so we reset it here.
24500Sstevel@tonic-gate 		 */
24510Sstevel@tonic-gate 		nvl = NULL;
24520Sstevel@tonic-gate 		error = EINVAL;
24530Sstevel@tonic-gate 		goto out;
24540Sstevel@tonic-gate 	}
24550Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
24560Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
24570Sstevel@tonic-gate 		rctl_hndl_t hndl;
24580Sstevel@tonic-gate 		nvlist_t **nvlarray;
24590Sstevel@tonic-gate 		uint_t i, nelem;
24600Sstevel@tonic-gate 		char *name;
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 		error = EINVAL;
24630Sstevel@tonic-gate 		name = nvpair_name(nvp);
24640Sstevel@tonic-gate 		if (strncmp(nvpair_name(nvp), "zone.", sizeof ("zone.") - 1)
24650Sstevel@tonic-gate 		    != 0 || nvpair_type(nvp) != DATA_TYPE_NVLIST_ARRAY) {
24660Sstevel@tonic-gate 			goto out;
24670Sstevel@tonic-gate 		}
24680Sstevel@tonic-gate 		if ((hndl = rctl_hndl_lookup(name)) == -1) {
24690Sstevel@tonic-gate 			goto out;
24700Sstevel@tonic-gate 		}
24710Sstevel@tonic-gate 		rde = rctl_dict_lookup_hndl(hndl);
24720Sstevel@tonic-gate 		error = nvpair_value_nvlist_array(nvp, &nvlarray, &nelem);
24730Sstevel@tonic-gate 		ASSERT(error == 0);
24740Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
24750Sstevel@tonic-gate 			if (error = nvlist2rctlval(nvlarray[i], &rv))
24760Sstevel@tonic-gate 				goto out;
24770Sstevel@tonic-gate 		}
24780Sstevel@tonic-gate 		if (rctl_invalid_value(rde, &rv)) {
24790Sstevel@tonic-gate 			error = EINVAL;
24800Sstevel@tonic-gate 			goto out;
24810Sstevel@tonic-gate 		}
24820Sstevel@tonic-gate 	}
24830Sstevel@tonic-gate 	error = 0;
24840Sstevel@tonic-gate 	*nvlp = nvl;
24850Sstevel@tonic-gate out:
24860Sstevel@tonic-gate 	kmem_free(kbuf, buflen);
24870Sstevel@tonic-gate 	if (error && nvl != NULL)
24880Sstevel@tonic-gate 		nvlist_free(nvl);
24890Sstevel@tonic-gate 	return (error);
24900Sstevel@tonic-gate }
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate int
24930Sstevel@tonic-gate zone_create_error(int er_error, int er_ext, int *er_out) {
24940Sstevel@tonic-gate 	if (er_out != NULL) {
24950Sstevel@tonic-gate 		if (copyout(&er_ext, er_out, sizeof (int))) {
24960Sstevel@tonic-gate 			return (set_errno(EFAULT));
24970Sstevel@tonic-gate 		}
24980Sstevel@tonic-gate 	}
24990Sstevel@tonic-gate 	return (set_errno(er_error));
25000Sstevel@tonic-gate }
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate /*
25030Sstevel@tonic-gate  * System call to create/initialize a new zone named 'zone_name', rooted
25040Sstevel@tonic-gate  * at 'zone_root', with a zone-wide privilege limit set of 'zone_privs',
25050Sstevel@tonic-gate  * and initialized with the zone-wide rctls described in 'rctlbuf'.
25060Sstevel@tonic-gate  *
25070Sstevel@tonic-gate  * If extended error is non-null, we may use it to return more detailed
25080Sstevel@tonic-gate  * error information.
25090Sstevel@tonic-gate  */
25100Sstevel@tonic-gate static zoneid_t
25110Sstevel@tonic-gate zone_create(const char *zone_name, const char *zone_root,
25120Sstevel@tonic-gate     const priv_set_t *zone_privs, caddr_t rctlbuf, size_t rctlbufsz,
25130Sstevel@tonic-gate     int *extended_error)
25140Sstevel@tonic-gate {
25150Sstevel@tonic-gate 	struct zsched_arg zarg;
25160Sstevel@tonic-gate 	nvlist_t *rctls = NULL;
25170Sstevel@tonic-gate 	proc_t *pp = curproc;
25180Sstevel@tonic-gate 	zone_t *zone, *ztmp;
25190Sstevel@tonic-gate 	zoneid_t zoneid;
25200Sstevel@tonic-gate 	int error;
25210Sstevel@tonic-gate 	int error2 = 0;
25220Sstevel@tonic-gate 	char *str;
25230Sstevel@tonic-gate 	cred_t *zkcr;
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
25260Sstevel@tonic-gate 		return (set_errno(EPERM));
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	/* can't boot zone from within chroot environment */
25290Sstevel@tonic-gate 	if (PTOU(pp)->u_rdir != NULL && PTOU(pp)->u_rdir != rootdir)
25300Sstevel@tonic-gate 		return (zone_create_error(ENOTSUP, ZE_CHROOTED,
25310Sstevel@tonic-gate 			extended_error));
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	zone = kmem_zalloc(sizeof (zone_t), KM_SLEEP);
25340Sstevel@tonic-gate 	zoneid = zone->zone_id = id_alloc(zoneid_space);
25350Sstevel@tonic-gate 	zone->zone_status = ZONE_IS_UNINITIALIZED;
25360Sstevel@tonic-gate 	zone->zone_pool = pool_default;
25370Sstevel@tonic-gate 	zone->zone_pool_mod = gethrtime();
25380Sstevel@tonic-gate 	zone->zone_psetid = ZONE_PS_INVAL;
25390Sstevel@tonic-gate 	zone->zone_ncpus = 0;
25400Sstevel@tonic-gate 	zone->zone_ncpus_online = 0;
25410Sstevel@tonic-gate 	mutex_init(&zone->zone_lock, NULL, MUTEX_DEFAULT, NULL);
25420Sstevel@tonic-gate 	mutex_init(&zone->zone_nlwps_lock, NULL, MUTEX_DEFAULT, NULL);
25430Sstevel@tonic-gate 	cv_init(&zone->zone_cv, NULL, CV_DEFAULT, NULL);
25440Sstevel@tonic-gate 	list_create(&zone->zone_zsd, sizeof (struct zsd_entry),
25450Sstevel@tonic-gate 	    offsetof(struct zsd_entry, zsd_linkage));
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 	if ((error = zone_set_name(zone, zone_name)) != 0) {
25480Sstevel@tonic-gate 		zone_free(zone);
25490Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
25500Sstevel@tonic-gate 	}
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	if ((error = zone_set_root(zone, zone_root)) != 0) {
25530Sstevel@tonic-gate 		zone_free(zone);
25540Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
25550Sstevel@tonic-gate 	}
25560Sstevel@tonic-gate 	if ((error = zone_set_privset(zone, zone_privs)) != 0) {
25570Sstevel@tonic-gate 		zone_free(zone);
25580Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
25590Sstevel@tonic-gate 	}
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	/* initialize node name to be the same as zone name */
25620Sstevel@tonic-gate 	zone->zone_nodename = kmem_alloc(_SYS_NMLN, KM_SLEEP);
25630Sstevel@tonic-gate 	(void) strncpy(zone->zone_nodename, zone->zone_name, _SYS_NMLN);
25640Sstevel@tonic-gate 	zone->zone_nodename[_SYS_NMLN - 1] = '\0';
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate 	zone->zone_domain = kmem_alloc(_SYS_NMLN, KM_SLEEP);
25670Sstevel@tonic-gate 	zone->zone_domain[0] = '\0';
25680Sstevel@tonic-gate 	zone->zone_shares = 1;
25690Sstevel@tonic-gate 	zone->zone_bootargs = NULL;
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 	/*
25720Sstevel@tonic-gate 	 * Zsched initializes the rctls.
25730Sstevel@tonic-gate 	 */
25740Sstevel@tonic-gate 	zone->zone_rctls = NULL;
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate 	if ((error = parse_rctls(rctlbuf, rctlbufsz, &rctls)) != 0) {
25770Sstevel@tonic-gate 		zone_free(zone);
25780Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
25790Sstevel@tonic-gate 	}
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	/*
25820Sstevel@tonic-gate 	 * Stop all lwps since that's what normally happens as part of fork().
25830Sstevel@tonic-gate 	 * This needs to happen before we grab any locks to avoid deadlock
25840Sstevel@tonic-gate 	 * (another lwp in the process could be waiting for the held lock).
25850Sstevel@tonic-gate 	 */
25860Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK)) {
25870Sstevel@tonic-gate 		zone_free(zone);
25880Sstevel@tonic-gate 		if (rctls)
25890Sstevel@tonic-gate 			nvlist_free(rctls);
25900Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
25910Sstevel@tonic-gate 	}
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	if (block_mounts() == 0) {
25940Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
25950Sstevel@tonic-gate 		if (curthread != pp->p_agenttp)
25960Sstevel@tonic-gate 			continuelwps(pp);
25970Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
25980Sstevel@tonic-gate 		zone_free(zone);
25990Sstevel@tonic-gate 		if (rctls)
26000Sstevel@tonic-gate 			nvlist_free(rctls);
26010Sstevel@tonic-gate 		return (zone_create_error(error, 0, extended_error));
26020Sstevel@tonic-gate 	}
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	/*
26050Sstevel@tonic-gate 	 * Set up credential for kernel access.  After this, any errors
26060Sstevel@tonic-gate 	 * should go through the dance in errout rather than calling
26070Sstevel@tonic-gate 	 * zone_free directly.
26080Sstevel@tonic-gate 	 */
26090Sstevel@tonic-gate 	zone->zone_kcred = crdup(kcred);
26100Sstevel@tonic-gate 	crsetzone(zone->zone_kcred, zone);
26110Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(zone->zone_kcred));
26120Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(zone->zone_kcred));
26130Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(zone->zone_kcred));
26140Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(zone->zone_kcred));
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
26170Sstevel@tonic-gate 	/*
26180Sstevel@tonic-gate 	 * Make sure zone doesn't already exist.
26190Sstevel@tonic-gate 	 */
26200Sstevel@tonic-gate 	if ((ztmp = zone_find_all_by_name(zone->zone_name)) != NULL) {
26210Sstevel@tonic-gate 		zone_status_t status;
26220Sstevel@tonic-gate 
26230Sstevel@tonic-gate 		status = zone_status_get(ztmp);
26240Sstevel@tonic-gate 		if (status == ZONE_IS_READY || status == ZONE_IS_RUNNING)
26250Sstevel@tonic-gate 			error = EEXIST;
26260Sstevel@tonic-gate 		else
26270Sstevel@tonic-gate 			error = EBUSY;
26280Sstevel@tonic-gate 		goto errout;
26290Sstevel@tonic-gate 	}
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	/*
26320Sstevel@tonic-gate 	 * Don't allow zone creations which would cause one zone's rootpath to
26330Sstevel@tonic-gate 	 * be accessible from that of another (non-global) zone.
26340Sstevel@tonic-gate 	 */
26350Sstevel@tonic-gate 	if (zone_is_nested(zone->zone_rootpath)) {
26360Sstevel@tonic-gate 		error = EBUSY;
26370Sstevel@tonic-gate 		goto errout;
26380Sstevel@tonic-gate 	}
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	ASSERT(zonecount != 0);		/* check for leaks */
26410Sstevel@tonic-gate 	if (zonecount + 1 > maxzones) {
26420Sstevel@tonic-gate 		error = ENOMEM;
26430Sstevel@tonic-gate 		goto errout;
26440Sstevel@tonic-gate 	}
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
26470Sstevel@tonic-gate 		error = EBUSY;
26480Sstevel@tonic-gate 		error2 = ZE_AREMOUNTS;
26490Sstevel@tonic-gate 		goto errout;
26500Sstevel@tonic-gate 	}
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate 	/*
26530Sstevel@tonic-gate 	 * Zone is still incomplete, but we need to drop all locks while
26540Sstevel@tonic-gate 	 * zsched() initializes this zone's kernel process.  We
26550Sstevel@tonic-gate 	 * optimistically add the zone to the hashtable and associated
26560Sstevel@tonic-gate 	 * lists so a parallel zone_create() doesn't try to create the
26570Sstevel@tonic-gate 	 * same zone.
26580Sstevel@tonic-gate 	 */
26590Sstevel@tonic-gate 	zonecount++;
26600Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyid,
26610Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id,
26620Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
26630Sstevel@tonic-gate 	str = kmem_alloc(strlen(zone->zone_name) + 1, KM_SLEEP);
26640Sstevel@tonic-gate 	(void) strcpy(str, zone->zone_name);
26650Sstevel@tonic-gate 	(void) mod_hash_insert(zonehashbyname, (mod_hash_key_t)str,
26660Sstevel@tonic-gate 	    (mod_hash_val_t)(uintptr_t)zone);
26670Sstevel@tonic-gate 	/*
26680Sstevel@tonic-gate 	 * Insert into active list.  At this point there are no 'hold's
26690Sstevel@tonic-gate 	 * on the zone, but everyone else knows not to use it, so we can
26700Sstevel@tonic-gate 	 * continue to use it.  zsched() will do a zone_hold() if the
26710Sstevel@tonic-gate 	 * newproc() is successful.
26720Sstevel@tonic-gate 	 */
26730Sstevel@tonic-gate 	list_insert_tail(&zone_active, zone);
26740Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	zarg.zone = zone;
26770Sstevel@tonic-gate 	zarg.nvlist = rctls;
26780Sstevel@tonic-gate 	/*
26790Sstevel@tonic-gate 	 * The process, task, and project rctls are probably wrong;
26800Sstevel@tonic-gate 	 * we need an interface to get the default values of all rctls,
26810Sstevel@tonic-gate 	 * and initialize zsched appropriately.  I'm not sure that that
26820Sstevel@tonic-gate 	 * makes much of a difference, though.
26830Sstevel@tonic-gate 	 */
26840Sstevel@tonic-gate 	if (error = newproc(zsched, (void *)&zarg, syscid, minclsyspri, NULL)) {
26850Sstevel@tonic-gate 		/*
26860Sstevel@tonic-gate 		 * We need to undo all globally visible state.
26870Sstevel@tonic-gate 		 */
26880Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
26890Sstevel@tonic-gate 		list_remove(&zone_active, zone);
26900Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyname,
26910Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_name);
26920Sstevel@tonic-gate 		(void) mod_hash_destroy(zonehashbyid,
26930Sstevel@tonic-gate 		    (mod_hash_key_t)(uintptr_t)zone->zone_id);
26940Sstevel@tonic-gate 		ASSERT(zonecount > 1);
26950Sstevel@tonic-gate 		zonecount--;
26960Sstevel@tonic-gate 		goto errout;
26970Sstevel@tonic-gate 	}
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate 	/*
27000Sstevel@tonic-gate 	 * Zone creation can't fail from now on.
27010Sstevel@tonic-gate 	 */
27020Sstevel@tonic-gate 
27030Sstevel@tonic-gate 	/*
27040Sstevel@tonic-gate 	 * Let the other lwps continue.
27050Sstevel@tonic-gate 	 */
27060Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
27070Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
27080Sstevel@tonic-gate 		continuelwps(pp);
27090Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	/*
27120Sstevel@tonic-gate 	 * Wait for zsched to finish initializing the zone.
27130Sstevel@tonic-gate 	 */
27140Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_READY);
27150Sstevel@tonic-gate 	/*
27160Sstevel@tonic-gate 	 * The zone is fully visible, so we can let mounts progress.
27170Sstevel@tonic-gate 	 */
27180Sstevel@tonic-gate 	resume_mounts();
27190Sstevel@tonic-gate 	if (rctls)
27200Sstevel@tonic-gate 		nvlist_free(rctls);
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate 	return (zoneid);
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate errout:
27250Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
27260Sstevel@tonic-gate 	/*
27270Sstevel@tonic-gate 	 * Let the other lwps continue.
27280Sstevel@tonic-gate 	 */
27290Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
27300Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
27310Sstevel@tonic-gate 		continuelwps(pp);
27320Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	resume_mounts();
27350Sstevel@tonic-gate 	if (rctls)
27360Sstevel@tonic-gate 		nvlist_free(rctls);
27370Sstevel@tonic-gate 	/*
27380Sstevel@tonic-gate 	 * There is currently one reference to the zone, a cred_ref from
27390Sstevel@tonic-gate 	 * zone_kcred.  To free the zone, we call crfree, which will call
27400Sstevel@tonic-gate 	 * zone_cred_rele, which will call zone_free.
27410Sstevel@tonic-gate 	 */
27420Sstevel@tonic-gate 	ASSERT(zone->zone_cred_ref == 1);	/* for zone_kcred */
27430Sstevel@tonic-gate 	ASSERT(zone->zone_kcred->cr_ref == 1);
27440Sstevel@tonic-gate 	ASSERT(zone->zone_ref == 0);
27450Sstevel@tonic-gate 	zkcr = zone->zone_kcred;
27460Sstevel@tonic-gate 	zone->zone_kcred = NULL;
27470Sstevel@tonic-gate 	crfree(zkcr);				/* triggers call to zone_free */
27480Sstevel@tonic-gate 	return (zone_create_error(error, error2, extended_error));
27490Sstevel@tonic-gate }
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate /*
27520Sstevel@tonic-gate  * Cause the zone to boot.  This is pretty simple, since we let zoneadmd do
27530Sstevel@tonic-gate  * the heavy lifting.
27540Sstevel@tonic-gate  */
27550Sstevel@tonic-gate static int
27560Sstevel@tonic-gate zone_boot(zoneid_t zoneid, const char *bootargs)
27570Sstevel@tonic-gate {
27580Sstevel@tonic-gate 	int err;
27590Sstevel@tonic-gate 	zone_t *zone;
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
27620Sstevel@tonic-gate 		return (set_errno(EPERM));
27630Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
27640Sstevel@tonic-gate 		return (set_errno(EINVAL));
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
27670Sstevel@tonic-gate 	/*
27680Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with calls to
27690Sstevel@tonic-gate 	 * zone_shutdown, zone_destroy, etc.
27700Sstevel@tonic-gate 	 */
27710Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
27720Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
27730Sstevel@tonic-gate 		return (set_errno(EINVAL));
27740Sstevel@tonic-gate 	}
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 	if ((err = zone_set_bootargs(zone, bootargs)) != 0) {
27770Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
27780Sstevel@tonic-gate 		return (set_errno(err));
27790Sstevel@tonic-gate 	}
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
27820Sstevel@tonic-gate 	if (zone_status_get(zone) != ZONE_IS_READY) {
27830Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
27840Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
27850Sstevel@tonic-gate 		return (set_errno(EINVAL));
27860Sstevel@tonic-gate 	}
27870Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_BOOTING);
27880Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
27890Sstevel@tonic-gate 
27900Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
27910Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 	if (zone_status_wait_sig(zone, ZONE_IS_RUNNING) == 0) {
27940Sstevel@tonic-gate 		zone_rele(zone);
27950Sstevel@tonic-gate 		return (set_errno(EINTR));
27960Sstevel@tonic-gate 	}
27970Sstevel@tonic-gate 
27980Sstevel@tonic-gate 	/*
27990Sstevel@tonic-gate 	 * Boot (starting init) might have failed, in which case the zone
28000Sstevel@tonic-gate 	 * will go to the SHUTTING_DOWN state; an appropriate errno will
28010Sstevel@tonic-gate 	 * be placed in zone->zone_boot_err, and so we return that.
28020Sstevel@tonic-gate 	 */
28030Sstevel@tonic-gate 	err = zone->zone_boot_err;
28040Sstevel@tonic-gate 	zone_rele(zone);
28050Sstevel@tonic-gate 	return (err ? set_errno(err) : 0);
28060Sstevel@tonic-gate }
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate /*
28090Sstevel@tonic-gate  * Kills all user processes in the zone, waiting for them all to exit
28100Sstevel@tonic-gate  * before returning.
28110Sstevel@tonic-gate  */
28120Sstevel@tonic-gate static int
28130Sstevel@tonic-gate zone_empty(zone_t *zone)
28140Sstevel@tonic-gate {
28150Sstevel@tonic-gate 	int waitstatus;
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 	/*
28180Sstevel@tonic-gate 	 * We need to drop zonehash_lock before killing all
28190Sstevel@tonic-gate 	 * processes, otherwise we'll deadlock with zone_find_*
28200Sstevel@tonic-gate 	 * which can be called from the exit path.
28210Sstevel@tonic-gate 	 */
28220Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&zonehash_lock));
28230Sstevel@tonic-gate 	while ((waitstatus = zone_status_timedwait_sig(zone, lbolt + hz,
28240Sstevel@tonic-gate 	    ZONE_IS_EMPTY)) == -1) {
28250Sstevel@tonic-gate 		killall(zone->zone_id);
28260Sstevel@tonic-gate 	}
28270Sstevel@tonic-gate 	/*
28280Sstevel@tonic-gate 	 * return EINTR if we were signaled
28290Sstevel@tonic-gate 	 */
28300Sstevel@tonic-gate 	if (waitstatus == 0)
28310Sstevel@tonic-gate 		return (EINTR);
28320Sstevel@tonic-gate 	return (0);
28330Sstevel@tonic-gate }
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate /*
28360Sstevel@tonic-gate  * Systemcall to start the zone's halt sequence.  By the time this
28370Sstevel@tonic-gate  * function successfully returns, all user processes and kernel threads
28380Sstevel@tonic-gate  * executing in it will have exited, ZSD shutdown callbacks executed,
28390Sstevel@tonic-gate  * and the zone status set to ZONE_IS_DOWN.
28400Sstevel@tonic-gate  *
28410Sstevel@tonic-gate  * It is possible that the call will interrupt itself if the caller is the
28420Sstevel@tonic-gate  * parent of any process running in the zone, and doesn't have SIGCHLD blocked.
28430Sstevel@tonic-gate  */
28440Sstevel@tonic-gate static int
28450Sstevel@tonic-gate zone_shutdown(zoneid_t zoneid)
28460Sstevel@tonic-gate {
28470Sstevel@tonic-gate 	int error;
28480Sstevel@tonic-gate 	zone_t *zone;
28490Sstevel@tonic-gate 	zone_status_t status;
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
28520Sstevel@tonic-gate 		return (set_errno(EPERM));
28530Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
28540Sstevel@tonic-gate 		return (set_errno(EINVAL));
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate 	/*
28570Sstevel@tonic-gate 	 * Block mounts so that VFS_MOUNT() can get an accurate view of
28580Sstevel@tonic-gate 	 * the zone's status with regards to ZONE_IS_SHUTTING down.
28590Sstevel@tonic-gate 	 *
28600Sstevel@tonic-gate 	 * e.g. NFS can fail the mount if it determines that the zone
28610Sstevel@tonic-gate 	 * has already begun the shutdown sequence.
28620Sstevel@tonic-gate 	 */
28630Sstevel@tonic-gate 	if (block_mounts() == 0)
28640Sstevel@tonic-gate 		return (set_errno(EINTR));
28650Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
28660Sstevel@tonic-gate 	/*
28670Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
28680Sstevel@tonic-gate 	 * calls to zone_shutdown and zone_destroy.
28690Sstevel@tonic-gate 	 */
28700Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
28710Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28720Sstevel@tonic-gate 		resume_mounts();
28730Sstevel@tonic-gate 		return (set_errno(EINVAL));
28740Sstevel@tonic-gate 	}
28750Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
28760Sstevel@tonic-gate 	status = zone_status_get(zone);
28770Sstevel@tonic-gate 	/*
28780Sstevel@tonic-gate 	 * Fail if the zone isn't fully initialized yet.
28790Sstevel@tonic-gate 	 */
28800Sstevel@tonic-gate 	if (status < ZONE_IS_READY) {
28810Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
28820Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28830Sstevel@tonic-gate 		resume_mounts();
28840Sstevel@tonic-gate 		return (set_errno(EINVAL));
28850Sstevel@tonic-gate 	}
28860Sstevel@tonic-gate 	/*
28870Sstevel@tonic-gate 	 * If conditions required for zone_shutdown() to return have been met,
28880Sstevel@tonic-gate 	 * return success.
28890Sstevel@tonic-gate 	 */
28900Sstevel@tonic-gate 	if (status >= ZONE_IS_DOWN) {
28910Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
28920Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
28930Sstevel@tonic-gate 		resume_mounts();
28940Sstevel@tonic-gate 		return (0);
28950Sstevel@tonic-gate 	}
28960Sstevel@tonic-gate 	/*
28970Sstevel@tonic-gate 	 * If zone_shutdown() hasn't been called before, go through the motions.
28980Sstevel@tonic-gate 	 * If it has, there's nothing to do but wait for the kernel threads to
28990Sstevel@tonic-gate 	 * drain.
29000Sstevel@tonic-gate 	 */
29010Sstevel@tonic-gate 	if (status < ZONE_IS_EMPTY) {
29020Sstevel@tonic-gate 		uint_t ntasks;
29030Sstevel@tonic-gate 
29040Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
29050Sstevel@tonic-gate 		if ((ntasks = zone->zone_ntasks) != 1) {
29060Sstevel@tonic-gate 			/*
29070Sstevel@tonic-gate 			 * There's still stuff running.
29080Sstevel@tonic-gate 			 */
29090Sstevel@tonic-gate 			zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
29100Sstevel@tonic-gate 		}
29110Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
29120Sstevel@tonic-gate 		if (ntasks == 1) {
29130Sstevel@tonic-gate 			/*
29140Sstevel@tonic-gate 			 * The only way to create another task is through
29150Sstevel@tonic-gate 			 * zone_enter(), which will block until we drop
29160Sstevel@tonic-gate 			 * zonehash_lock.  The zone is empty.
29170Sstevel@tonic-gate 			 */
29180Sstevel@tonic-gate 			if (zone->zone_kthreads == NULL) {
29190Sstevel@tonic-gate 				/*
29200Sstevel@tonic-gate 				 * Skip ahead to ZONE_IS_DOWN
29210Sstevel@tonic-gate 				 */
29220Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_DOWN);
29230Sstevel@tonic-gate 			} else {
29240Sstevel@tonic-gate 				zone_status_set(zone, ZONE_IS_EMPTY);
29250Sstevel@tonic-gate 			}
29260Sstevel@tonic-gate 		}
29270Sstevel@tonic-gate 	}
29280Sstevel@tonic-gate 	zone_hold(zone);	/* so we can use the zone_t later */
29290Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
29300Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
29310Sstevel@tonic-gate 	resume_mounts();
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 	if (error = zone_empty(zone)) {
29340Sstevel@tonic-gate 		zone_rele(zone);
29350Sstevel@tonic-gate 		return (set_errno(error));
29360Sstevel@tonic-gate 	}
29370Sstevel@tonic-gate 	/*
29380Sstevel@tonic-gate 	 * After the zone status goes to ZONE_IS_DOWN this zone will no
29390Sstevel@tonic-gate 	 * longer be notified of changes to the pools configuration, so
29400Sstevel@tonic-gate 	 * in order to not end up with a stale pool pointer, we point
29410Sstevel@tonic-gate 	 * ourselves at the default pool and remove all resource
29420Sstevel@tonic-gate 	 * visibility.  This is especially important as the zone_t may
29430Sstevel@tonic-gate 	 * languish on the deathrow for a very long time waiting for
29440Sstevel@tonic-gate 	 * cred's to drain out.
29450Sstevel@tonic-gate 	 *
29460Sstevel@tonic-gate 	 * This rebinding of the zone can happen multiple times
29470Sstevel@tonic-gate 	 * (presumably due to interrupted or parallel systemcalls)
29480Sstevel@tonic-gate 	 * without any adverse effects.
29490Sstevel@tonic-gate 	 */
29500Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
29510Sstevel@tonic-gate 		zone_rele(zone);
29520Sstevel@tonic-gate 		return (set_errno(EINTR));
29530Sstevel@tonic-gate 	}
29540Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED) {
29550Sstevel@tonic-gate 		mutex_enter(&cpu_lock);
29560Sstevel@tonic-gate 		zone_pool_set(zone, pool_default);
29570Sstevel@tonic-gate 		/*
29580Sstevel@tonic-gate 		 * The zone no longer needs to be able to see any cpus.
29590Sstevel@tonic-gate 		 */
29600Sstevel@tonic-gate 		zone_pset_set(zone, ZONE_PS_INVAL);
29610Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
29620Sstevel@tonic-gate 	}
29630Sstevel@tonic-gate 	pool_unlock();
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate 	/*
29660Sstevel@tonic-gate 	 * ZSD shutdown callbacks can be executed multiple times, hence
29670Sstevel@tonic-gate 	 * it is safe to not be holding any locks across this call.
29680Sstevel@tonic-gate 	 */
29690Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_SHUTDOWN);
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
29720Sstevel@tonic-gate 	if (zone->zone_kthreads == NULL && zone_status_get(zone) < ZONE_IS_DOWN)
29730Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DOWN);
29740Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
29750Sstevel@tonic-gate 
29760Sstevel@tonic-gate 	/*
29770Sstevel@tonic-gate 	 * Wait for kernel threads to drain.
29780Sstevel@tonic-gate 	 */
29790Sstevel@tonic-gate 	if (!zone_status_wait_sig(zone, ZONE_IS_DOWN)) {
29800Sstevel@tonic-gate 		zone_rele(zone);
29810Sstevel@tonic-gate 		return (set_errno(EINTR));
29820Sstevel@tonic-gate 	}
29830Sstevel@tonic-gate 	zone_rele(zone);
29840Sstevel@tonic-gate 	return (0);
29850Sstevel@tonic-gate }
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate /*
29880Sstevel@tonic-gate  * Systemcall entry point to finalize the zone halt process.  The caller
29890Sstevel@tonic-gate  * must have already successfully callefd zone_shutdown().
29900Sstevel@tonic-gate  *
29910Sstevel@tonic-gate  * Upon successful completion, the zone will have been fully destroyed:
29920Sstevel@tonic-gate  * zsched will have exited, destructor callbacks executed, and the zone
29930Sstevel@tonic-gate  * removed from the list of active zones.
29940Sstevel@tonic-gate  */
29950Sstevel@tonic-gate static int
29960Sstevel@tonic-gate zone_destroy(zoneid_t zoneid)
29970Sstevel@tonic-gate {
29980Sstevel@tonic-gate 	uint64_t uniqid;
29990Sstevel@tonic-gate 	zone_t *zone;
30000Sstevel@tonic-gate 	zone_status_t status;
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
30030Sstevel@tonic-gate 		return (set_errno(EPERM));
30040Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
30050Sstevel@tonic-gate 		return (set_errno(EINVAL));
30060Sstevel@tonic-gate 
30070Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
30080Sstevel@tonic-gate 	/*
30090Sstevel@tonic-gate 	 * Look for zone under hash lock to prevent races with other
30100Sstevel@tonic-gate 	 * calls to zone_destroy.
30110Sstevel@tonic-gate 	 */
30120Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
30130Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
30140Sstevel@tonic-gate 		return (set_errno(EINVAL));
30150Sstevel@tonic-gate 	}
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 	if (zone_mount_count(zone->zone_rootpath) != 0) {
30180Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
30190Sstevel@tonic-gate 		return (set_errno(EBUSY));
30200Sstevel@tonic-gate 	}
30210Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
30220Sstevel@tonic-gate 	status = zone_status_get(zone);
30230Sstevel@tonic-gate 	if (status < ZONE_IS_DOWN) {
30240Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
30250Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
30260Sstevel@tonic-gate 		return (set_errno(EBUSY));
30270Sstevel@tonic-gate 	} else if (status == ZONE_IS_DOWN) {
30280Sstevel@tonic-gate 		zone_status_set(zone, ZONE_IS_DYING); /* Tell zsched to exit */
30290Sstevel@tonic-gate 	}
30300Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
30310Sstevel@tonic-gate 	zone_hold(zone);
30320Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 	/*
30350Sstevel@tonic-gate 	 * wait for zsched to exit
30360Sstevel@tonic-gate 	 */
30370Sstevel@tonic-gate 	zone_status_wait(zone, ZONE_IS_DEAD);
30380Sstevel@tonic-gate 	zone_zsd_callbacks(zone, ZSD_DESTROY);
30390Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
30400Sstevel@tonic-gate 	zone_rele(zone);
30410Sstevel@tonic-gate 	zone = NULL;	/* potentially free'd */
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
30440Sstevel@tonic-gate 	for (; /* ever */; ) {
30450Sstevel@tonic-gate 		boolean_t unref;
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 		if ((zone = zone_find_all_by_id(zoneid)) == NULL ||
30480Sstevel@tonic-gate 		    zone->zone_uniqid != uniqid) {
30490Sstevel@tonic-gate 			/*
30500Sstevel@tonic-gate 			 * The zone has gone away.  Necessary conditions
30510Sstevel@tonic-gate 			 * are met, so we return success.
30520Sstevel@tonic-gate 			 */
30530Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
30540Sstevel@tonic-gate 			return (0);
30550Sstevel@tonic-gate 		}
30560Sstevel@tonic-gate 		mutex_enter(&zone->zone_lock);
30570Sstevel@tonic-gate 		unref = ZONE_IS_UNREF(zone);
30580Sstevel@tonic-gate 		mutex_exit(&zone->zone_lock);
30590Sstevel@tonic-gate 		if (unref) {
30600Sstevel@tonic-gate 			/*
30610Sstevel@tonic-gate 			 * There is only one reference to the zone -- that
30620Sstevel@tonic-gate 			 * added when the zone was added to the hashtables --
30630Sstevel@tonic-gate 			 * and things will remain this way until we drop
30640Sstevel@tonic-gate 			 * zonehash_lock... we can go ahead and cleanup the
30650Sstevel@tonic-gate 			 * zone.
30660Sstevel@tonic-gate 			 */
30670Sstevel@tonic-gate 			break;
30680Sstevel@tonic-gate 		}
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 		if (cv_wait_sig(&zone_destroy_cv, &zonehash_lock) == 0) {
30710Sstevel@tonic-gate 			/* Signaled */
30720Sstevel@tonic-gate 			mutex_exit(&zonehash_lock);
30730Sstevel@tonic-gate 			return (set_errno(EINTR));
30740Sstevel@tonic-gate 		}
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 	}
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate 	/*
30790Sstevel@tonic-gate 	 * It is now safe to let the zone be recreated; remove it from the
30800Sstevel@tonic-gate 	 * lists.  The memory will not be freed until the last cred
30810Sstevel@tonic-gate 	 * reference goes away.
30820Sstevel@tonic-gate 	 */
30830Sstevel@tonic-gate 	ASSERT(zonecount > 1);	/* must be > 1; can't destroy global zone */
30840Sstevel@tonic-gate 	zonecount--;
30850Sstevel@tonic-gate 	/* remove from active list and hash tables */
30860Sstevel@tonic-gate 	list_remove(&zone_active, zone);
30870Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyname,
30880Sstevel@tonic-gate 	    (mod_hash_key_t)zone->zone_name);
30890Sstevel@tonic-gate 	(void) mod_hash_destroy(zonehashbyid,
30900Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)zone->zone_id);
30910Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
30920Sstevel@tonic-gate 
3093*766Scarlsonj 	/*
3094*766Scarlsonj 	 * Release the root vnode; we're not using it anymore.  Nor should any
3095*766Scarlsonj 	 * other thread that might access it exist.
3096*766Scarlsonj 	 */
3097*766Scarlsonj 	if (zone->zone_rootvp != NULL) {
3098*766Scarlsonj 		VN_RELE(zone->zone_rootvp);
3099*766Scarlsonj 		zone->zone_rootvp = NULL;
3100*766Scarlsonj 	}
3101*766Scarlsonj 
31020Sstevel@tonic-gate 	/* add to deathrow list */
31030Sstevel@tonic-gate 	mutex_enter(&zone_deathrow_lock);
31040Sstevel@tonic-gate 	list_insert_tail(&zone_deathrow, zone);
31050Sstevel@tonic-gate 	mutex_exit(&zone_deathrow_lock);
31060Sstevel@tonic-gate 
31070Sstevel@tonic-gate 	/*
31080Sstevel@tonic-gate 	 * Drop last reference (which was added by zsched()), this will
31090Sstevel@tonic-gate 	 * free the zone unless there are outstanding cred references.
31100Sstevel@tonic-gate 	 */
31110Sstevel@tonic-gate 	zone_rele(zone);
31120Sstevel@tonic-gate 	return (0);
31130Sstevel@tonic-gate }
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate /*
31160Sstevel@tonic-gate  * Systemcall entry point for zone_getattr(2).
31170Sstevel@tonic-gate  */
31180Sstevel@tonic-gate static ssize_t
31190Sstevel@tonic-gate zone_getattr(zoneid_t zoneid, int attr, void *buf, size_t bufsize)
31200Sstevel@tonic-gate {
31210Sstevel@tonic-gate 	size_t size;
31220Sstevel@tonic-gate 	int error = 0, err;
31230Sstevel@tonic-gate 	zone_t *zone;
31240Sstevel@tonic-gate 	char *zonepath;
31250Sstevel@tonic-gate 	zone_status_t zone_status;
31260Sstevel@tonic-gate 	pid_t initpid;
31270Sstevel@tonic-gate 	boolean_t global = (curproc->p_zone == global_zone);
31280Sstevel@tonic-gate 
31290Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
31300Sstevel@tonic-gate 	if ((zone = zone_find_all_by_id(zoneid)) == NULL) {
31310Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31320Sstevel@tonic-gate 		return (set_errno(EINVAL));
31330Sstevel@tonic-gate 	}
31340Sstevel@tonic-gate 	zone_status = zone_status_get(zone);
31350Sstevel@tonic-gate 	if (zone_status < ZONE_IS_READY) {
31360Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
31370Sstevel@tonic-gate 		return (set_errno(EINVAL));
31380Sstevel@tonic-gate 	}
31390Sstevel@tonic-gate 	zone_hold(zone);
31400Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
31410Sstevel@tonic-gate 
31420Sstevel@tonic-gate 	/*
31430Sstevel@tonic-gate 	 * If not in the global zone, don't show information about other zones.
31440Sstevel@tonic-gate 	 */
31450Sstevel@tonic-gate 	if (!global && curproc->p_zone != zone) {
31460Sstevel@tonic-gate 		zone_rele(zone);
31470Sstevel@tonic-gate 		return (set_errno(EINVAL));
31480Sstevel@tonic-gate 	}
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate 	switch (attr) {
31510Sstevel@tonic-gate 	case ZONE_ATTR_ROOT:
31520Sstevel@tonic-gate 		if (global) {
31530Sstevel@tonic-gate 			/*
31540Sstevel@tonic-gate 			 * Copy the path to trim the trailing "/" (except for
31550Sstevel@tonic-gate 			 * the global zone).
31560Sstevel@tonic-gate 			 */
31570Sstevel@tonic-gate 			if (zone != global_zone)
31580Sstevel@tonic-gate 				size = zone->zone_rootpathlen - 1;
31590Sstevel@tonic-gate 			else
31600Sstevel@tonic-gate 				size = zone->zone_rootpathlen;
31610Sstevel@tonic-gate 			zonepath = kmem_alloc(size, KM_SLEEP);
31620Sstevel@tonic-gate 			bcopy(zone->zone_rootpath, zonepath, size);
31630Sstevel@tonic-gate 			zonepath[size - 1] = '\0';
31640Sstevel@tonic-gate 		} else {
31650Sstevel@tonic-gate 			/*
31660Sstevel@tonic-gate 			 * Caller is not in the global zone, just return
31670Sstevel@tonic-gate 			 * faked-up path for current zone.
31680Sstevel@tonic-gate 			 */
31690Sstevel@tonic-gate 			zonepath = "/";
31700Sstevel@tonic-gate 			size = 2;
31710Sstevel@tonic-gate 		}
31720Sstevel@tonic-gate 		if (bufsize > size)
31730Sstevel@tonic-gate 			bufsize = size;
31740Sstevel@tonic-gate 		if (buf != NULL) {
31750Sstevel@tonic-gate 			err = copyoutstr(zonepath, buf, bufsize, NULL);
31760Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
31770Sstevel@tonic-gate 				error = EFAULT;
31780Sstevel@tonic-gate 		}
31790Sstevel@tonic-gate 		if (global)
31800Sstevel@tonic-gate 			kmem_free(zonepath, size);
31810Sstevel@tonic-gate 		break;
31820Sstevel@tonic-gate 
31830Sstevel@tonic-gate 	case ZONE_ATTR_NAME:
31840Sstevel@tonic-gate 		size = strlen(zone->zone_name) + 1;
31850Sstevel@tonic-gate 		if (bufsize > size)
31860Sstevel@tonic-gate 			bufsize = size;
31870Sstevel@tonic-gate 		if (buf != NULL) {
31880Sstevel@tonic-gate 			err = copyoutstr(zone->zone_name, buf, bufsize, NULL);
31890Sstevel@tonic-gate 			if (err != 0 && err != ENAMETOOLONG)
31900Sstevel@tonic-gate 				error = EFAULT;
31910Sstevel@tonic-gate 		}
31920Sstevel@tonic-gate 		break;
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	case ZONE_ATTR_STATUS:
31950Sstevel@tonic-gate 		/*
31960Sstevel@tonic-gate 		 * Since we're not holding zonehash_lock, the zone status
31970Sstevel@tonic-gate 		 * may be anything; leave it up to userland to sort it out.
31980Sstevel@tonic-gate 		 */
31990Sstevel@tonic-gate 		size = sizeof (zone_status);
32000Sstevel@tonic-gate 		if (bufsize > size)
32010Sstevel@tonic-gate 			bufsize = size;
32020Sstevel@tonic-gate 		zone_status = zone_status_get(zone);
32030Sstevel@tonic-gate 		if (buf != NULL &&
32040Sstevel@tonic-gate 		    copyout(&zone_status, buf, bufsize) != 0)
32050Sstevel@tonic-gate 			error = EFAULT;
32060Sstevel@tonic-gate 		break;
32070Sstevel@tonic-gate 	case ZONE_ATTR_PRIVSET:
32080Sstevel@tonic-gate 		size = sizeof (priv_set_t);
32090Sstevel@tonic-gate 		if (bufsize > size)
32100Sstevel@tonic-gate 			bufsize = size;
32110Sstevel@tonic-gate 		if (buf != NULL &&
32120Sstevel@tonic-gate 		    copyout(zone->zone_privset, buf, bufsize) != 0)
32130Sstevel@tonic-gate 			error = EFAULT;
32140Sstevel@tonic-gate 		break;
32150Sstevel@tonic-gate 	case ZONE_ATTR_UNIQID:
32160Sstevel@tonic-gate 		size = sizeof (zone->zone_uniqid);
32170Sstevel@tonic-gate 		if (bufsize > size)
32180Sstevel@tonic-gate 			bufsize = size;
32190Sstevel@tonic-gate 		if (buf != NULL &&
32200Sstevel@tonic-gate 		    copyout(&zone->zone_uniqid, buf, bufsize) != 0)
32210Sstevel@tonic-gate 			error = EFAULT;
32220Sstevel@tonic-gate 		break;
32230Sstevel@tonic-gate 	case ZONE_ATTR_POOLID:
32240Sstevel@tonic-gate 		{
32250Sstevel@tonic-gate 			pool_t *pool;
32260Sstevel@tonic-gate 			poolid_t poolid;
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 			if (pool_lock_intr() != 0) {
32290Sstevel@tonic-gate 				error = EINTR;
32300Sstevel@tonic-gate 				break;
32310Sstevel@tonic-gate 			}
32320Sstevel@tonic-gate 			pool = zone_pool_get(zone);
32330Sstevel@tonic-gate 			poolid = pool->pool_id;
32340Sstevel@tonic-gate 			pool_unlock();
32350Sstevel@tonic-gate 			size = sizeof (poolid);
32360Sstevel@tonic-gate 			if (bufsize > size)
32370Sstevel@tonic-gate 				bufsize = size;
32380Sstevel@tonic-gate 			if (buf != NULL && copyout(&poolid, buf, size) != 0)
32390Sstevel@tonic-gate 				error = EFAULT;
32400Sstevel@tonic-gate 		}
32410Sstevel@tonic-gate 		break;
32420Sstevel@tonic-gate 	case ZONE_ATTR_INITPID:
32430Sstevel@tonic-gate 		size = sizeof (initpid);
32440Sstevel@tonic-gate 		if (bufsize > size)
32450Sstevel@tonic-gate 			bufsize = size;
32460Sstevel@tonic-gate 		initpid = zone->zone_proc_initpid;
32470Sstevel@tonic-gate 		if (initpid == -1) {
32480Sstevel@tonic-gate 			error = ESRCH;
32490Sstevel@tonic-gate 			break;
32500Sstevel@tonic-gate 		}
32510Sstevel@tonic-gate 		if (buf != NULL &&
32520Sstevel@tonic-gate 		    copyout(&initpid, buf, bufsize) != 0)
32530Sstevel@tonic-gate 			error = EFAULT;
32540Sstevel@tonic-gate 		break;
32550Sstevel@tonic-gate 	default:
32560Sstevel@tonic-gate 		error = EINVAL;
32570Sstevel@tonic-gate 	}
32580Sstevel@tonic-gate 	zone_rele(zone);
32590Sstevel@tonic-gate 
32600Sstevel@tonic-gate 	if (error)
32610Sstevel@tonic-gate 		return (set_errno(error));
32620Sstevel@tonic-gate 	return ((ssize_t)size);
32630Sstevel@tonic-gate }
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate /*
32660Sstevel@tonic-gate  * Return zero if the process has at least one vnode mapped in to its
32670Sstevel@tonic-gate  * address space which shouldn't be allowed to change zones.
32680Sstevel@tonic-gate  */
32690Sstevel@tonic-gate static int
32700Sstevel@tonic-gate as_can_change_zones(void)
32710Sstevel@tonic-gate {
32720Sstevel@tonic-gate 	proc_t *pp = curproc;
32730Sstevel@tonic-gate 	struct seg *seg;
32740Sstevel@tonic-gate 	struct as *as = pp->p_as;
32750Sstevel@tonic-gate 	vnode_t *vp;
32760Sstevel@tonic-gate 	int allow = 1;
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 	ASSERT(pp->p_as != &kas);
32790Sstevel@tonic-gate 	AS_LOCK_ENTER(&as, &as->a_lock, RW_READER);
32800Sstevel@tonic-gate 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
32810Sstevel@tonic-gate 		/*
32820Sstevel@tonic-gate 		 * if we can't get a backing vnode for this segment then skip
32830Sstevel@tonic-gate 		 * it.
32840Sstevel@tonic-gate 		 */
32850Sstevel@tonic-gate 		vp = NULL;
32860Sstevel@tonic-gate 		if (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)
32870Sstevel@tonic-gate 			continue;
32880Sstevel@tonic-gate 		if (!vn_can_change_zones(vp)) { /* bail on first match */
32890Sstevel@tonic-gate 			allow = 0;
32900Sstevel@tonic-gate 			break;
32910Sstevel@tonic-gate 		}
32920Sstevel@tonic-gate 	}
32930Sstevel@tonic-gate 	AS_LOCK_EXIT(&as, &as->a_lock);
32940Sstevel@tonic-gate 	return (allow);
32950Sstevel@tonic-gate }
32960Sstevel@tonic-gate 
32970Sstevel@tonic-gate /*
32980Sstevel@tonic-gate  * Systemcall entry point for zone_enter().
32990Sstevel@tonic-gate  *
33000Sstevel@tonic-gate  * The current process is injected into said zone.  In the process
33010Sstevel@tonic-gate  * it will change its project membership, privileges, rootdir/cwd,
33020Sstevel@tonic-gate  * zone-wide rctls, and pool association to match those of the zone.
33030Sstevel@tonic-gate  *
33040Sstevel@tonic-gate  * The first zone_enter() called while the zone is in the ZONE_IS_READY
33050Sstevel@tonic-gate  * state will transition it to ZONE_IS_RUNNING.  Processes may only
33060Sstevel@tonic-gate  * enter a zone that is "ready" or "running".
33070Sstevel@tonic-gate  */
33080Sstevel@tonic-gate static int
33090Sstevel@tonic-gate zone_enter(zoneid_t zoneid)
33100Sstevel@tonic-gate {
33110Sstevel@tonic-gate 	zone_t *zone;
33120Sstevel@tonic-gate 	vnode_t *vp;
33130Sstevel@tonic-gate 	proc_t *pp = curproc;
33140Sstevel@tonic-gate 	contract_t *ct;
33150Sstevel@tonic-gate 	cont_process_t *ctp;
33160Sstevel@tonic-gate 	task_t *tk, *oldtk;
33170Sstevel@tonic-gate 	kproject_t *zone_proj0;
33180Sstevel@tonic-gate 	cred_t *cr, *newcr;
33190Sstevel@tonic-gate 	pool_t *oldpool, *newpool;
33200Sstevel@tonic-gate 	sess_t *sp;
33210Sstevel@tonic-gate 	uid_t uid;
33220Sstevel@tonic-gate 	zone_status_t status;
33230Sstevel@tonic-gate 	int err = 0;
33240Sstevel@tonic-gate 	rctl_entity_p_t e;
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate 	if (secpolicy_zone_config(CRED()) != 0)
33270Sstevel@tonic-gate 		return (set_errno(EPERM));
33280Sstevel@tonic-gate 	if (zoneid < MIN_USERZONEID || zoneid > MAX_ZONEID)
33290Sstevel@tonic-gate 		return (set_errno(EINVAL));
33300Sstevel@tonic-gate 
33310Sstevel@tonic-gate 	/*
33320Sstevel@tonic-gate 	 * Stop all lwps so we don't need to hold a lock to look at
33330Sstevel@tonic-gate 	 * curproc->p_zone.  This needs to happen before we grab any
33340Sstevel@tonic-gate 	 * locks to avoid deadlock (another lwp in the process could
33350Sstevel@tonic-gate 	 * be waiting for the held lock).
33360Sstevel@tonic-gate 	 */
33370Sstevel@tonic-gate 	if (curthread != pp->p_agenttp && !holdlwps(SHOLDFORK))
33380Sstevel@tonic-gate 		return (set_errno(EINTR));
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate 	/*
33410Sstevel@tonic-gate 	 * Make sure we're not changing zones with files open or mapped in
33420Sstevel@tonic-gate 	 * to our address space which shouldn't be changing zones.
33430Sstevel@tonic-gate 	 */
33440Sstevel@tonic-gate 	if (!files_can_change_zones()) {
33450Sstevel@tonic-gate 		err = EBADF;
33460Sstevel@tonic-gate 		goto out;
33470Sstevel@tonic-gate 	}
33480Sstevel@tonic-gate 	if (!as_can_change_zones()) {
33490Sstevel@tonic-gate 		err = EFAULT;
33500Sstevel@tonic-gate 		goto out;
33510Sstevel@tonic-gate 	}
33520Sstevel@tonic-gate 
33530Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
33540Sstevel@tonic-gate 	if (pp->p_zone != global_zone) {
33550Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
33560Sstevel@tonic-gate 		err = EINVAL;
33570Sstevel@tonic-gate 		goto out;
33580Sstevel@tonic-gate 	}
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate 	zone = zone_find_all_by_id(zoneid);
33610Sstevel@tonic-gate 	if (zone == NULL) {
33620Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
33630Sstevel@tonic-gate 		err = EINVAL;
33640Sstevel@tonic-gate 		goto out;
33650Sstevel@tonic-gate 	}
33660Sstevel@tonic-gate 
33670Sstevel@tonic-gate 	/*
33680Sstevel@tonic-gate 	 * To prevent processes in a zone from holding contracts on
33690Sstevel@tonic-gate 	 * extrazonal resources, and to avoid process contract
33700Sstevel@tonic-gate 	 * memberships which span zones, contract holders and processes
33710Sstevel@tonic-gate 	 * which aren't the sole members of their encapsulating process
33720Sstevel@tonic-gate 	 * contracts are not allowed to zone_enter.
33730Sstevel@tonic-gate 	 */
33740Sstevel@tonic-gate 	ctp = pp->p_ct_process;
33750Sstevel@tonic-gate 	ct = &ctp->conp_contract;
33760Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
33770Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
33780Sstevel@tonic-gate 	if ((avl_numnodes(&pp->p_ct_held) != 0) || (ctp->conp_nmembers != 1)) {
33790Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
33800Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
33810Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
33820Sstevel@tonic-gate 		pool_unlock();
33830Sstevel@tonic-gate 		err = EINVAL;
33840Sstevel@tonic-gate 		goto out;
33850Sstevel@tonic-gate 	}
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 	/*
33880Sstevel@tonic-gate 	 * Moreover, we don't allow processes whose encapsulating
33890Sstevel@tonic-gate 	 * process contracts have inherited extrazonal contracts.
33900Sstevel@tonic-gate 	 * While it would be easier to eliminate all process contracts
33910Sstevel@tonic-gate 	 * with inherited contracts, we need to be able to give a
33920Sstevel@tonic-gate 	 * restarted init (or other zone-penetrating process) its
33930Sstevel@tonic-gate 	 * predecessor's contracts.
33940Sstevel@tonic-gate 	 */
33950Sstevel@tonic-gate 	if (ctp->conp_ninherited != 0) {
33960Sstevel@tonic-gate 		contract_t *next;
33970Sstevel@tonic-gate 		for (next = list_head(&ctp->conp_inherited); next;
33980Sstevel@tonic-gate 		    next = list_next(&ctp->conp_inherited, next)) {
33990Sstevel@tonic-gate 			if (contract_getzuniqid(next) != zone->zone_uniqid) {
34000Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
34010Sstevel@tonic-gate 				mutex_exit(&ct->ct_lock);
34020Sstevel@tonic-gate 				mutex_exit(&zonehash_lock);
34030Sstevel@tonic-gate 				pool_unlock();
34040Sstevel@tonic-gate 				err = EINVAL;
34050Sstevel@tonic-gate 				goto out;
34060Sstevel@tonic-gate 			}
34070Sstevel@tonic-gate 		}
34080Sstevel@tonic-gate 	}
34090Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
34100Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
34110Sstevel@tonic-gate 
34120Sstevel@tonic-gate 	status = zone_status_get(zone);
34130Sstevel@tonic-gate 	if (status < ZONE_IS_READY || status >= ZONE_IS_SHUTTING_DOWN) {
34140Sstevel@tonic-gate 		/*
34150Sstevel@tonic-gate 		 * Can't join
34160Sstevel@tonic-gate 		 */
34170Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34180Sstevel@tonic-gate 		err = EINVAL;
34190Sstevel@tonic-gate 		goto out;
34200Sstevel@tonic-gate 	}
34210Sstevel@tonic-gate 
34220Sstevel@tonic-gate 	/*
34230Sstevel@tonic-gate 	 * Make sure new priv set is within the permitted set for caller
34240Sstevel@tonic-gate 	 */
34250Sstevel@tonic-gate 	if (!priv_issubset(zone->zone_privset, &CR_OPPRIV(CRED()))) {
34260Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34270Sstevel@tonic-gate 		err = EPERM;
34280Sstevel@tonic-gate 		goto out;
34290Sstevel@tonic-gate 	}
34300Sstevel@tonic-gate 	/*
34310Sstevel@tonic-gate 	 * We want to momentarily drop zonehash_lock while we optimistically
34320Sstevel@tonic-gate 	 * bind curproc to the pool it should be running in.  This is safe
34330Sstevel@tonic-gate 	 * since the zone can't disappear (we have a hold on it).
34340Sstevel@tonic-gate 	 */
34350Sstevel@tonic-gate 	zone_hold(zone);
34360Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 	/*
34390Sstevel@tonic-gate 	 * Grab pool_lock to keep the pools configuration from changing
34400Sstevel@tonic-gate 	 * and to stop ourselves from getting rebound to another pool
34410Sstevel@tonic-gate 	 * until we join the zone.
34420Sstevel@tonic-gate 	 */
34430Sstevel@tonic-gate 	if (pool_lock_intr() != 0) {
34440Sstevel@tonic-gate 		zone_rele(zone);
34450Sstevel@tonic-gate 		err = EINTR;
34460Sstevel@tonic-gate 		goto out;
34470Sstevel@tonic-gate 	}
34480Sstevel@tonic-gate 	ASSERT(secpolicy_pool(CRED()) == 0);
34490Sstevel@tonic-gate 	/*
34500Sstevel@tonic-gate 	 * Bind ourselves to the pool currently associated with the zone.
34510Sstevel@tonic-gate 	 */
34520Sstevel@tonic-gate 	oldpool = curproc->p_pool;
34530Sstevel@tonic-gate 	newpool = zone_pool_get(zone);
34540Sstevel@tonic-gate 	if (pool_state == POOL_ENABLED && newpool != oldpool &&
34550Sstevel@tonic-gate 	    (err = pool_do_bind(newpool, P_PID, P_MYID,
34560Sstevel@tonic-gate 	    POOL_BIND_ALL)) != 0) {
34570Sstevel@tonic-gate 		pool_unlock();
34580Sstevel@tonic-gate 		zone_rele(zone);
34590Sstevel@tonic-gate 		goto out;
34600Sstevel@tonic-gate 	}
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 	/*
34630Sstevel@tonic-gate 	 * Grab cpu_lock now; we'll need it later when we call
34640Sstevel@tonic-gate 	 * task_join().
34650Sstevel@tonic-gate 	 */
34660Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
34670Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
34680Sstevel@tonic-gate 	/*
34690Sstevel@tonic-gate 	 * Make sure the zone hasn't moved on since we dropped zonehash_lock.
34700Sstevel@tonic-gate 	 */
34710Sstevel@tonic-gate 	if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
34720Sstevel@tonic-gate 		/*
34730Sstevel@tonic-gate 		 * Can't join anymore.
34740Sstevel@tonic-gate 		 */
34750Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
34760Sstevel@tonic-gate 		mutex_exit(&cpu_lock);
34770Sstevel@tonic-gate 		if (pool_state == POOL_ENABLED &&
34780Sstevel@tonic-gate 		    newpool != oldpool)
34790Sstevel@tonic-gate 			(void) pool_do_bind(oldpool, P_PID, P_MYID,
34800Sstevel@tonic-gate 			    POOL_BIND_ALL);
34810Sstevel@tonic-gate 		pool_unlock();
34820Sstevel@tonic-gate 		zone_rele(zone);
34830Sstevel@tonic-gate 		err = EINVAL;
34840Sstevel@tonic-gate 		goto out;
34850Sstevel@tonic-gate 	}
34860Sstevel@tonic-gate 
34870Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
34880Sstevel@tonic-gate 	zone_proj0 = zone->zone_zsched->p_task->tk_proj;
34890Sstevel@tonic-gate 	/* verify that we do not exceed and task or lwp limits */
34900Sstevel@tonic-gate 	mutex_enter(&zone->zone_nlwps_lock);
34910Sstevel@tonic-gate 	/* add new lwps to zone and zone's proj0 */
34920Sstevel@tonic-gate 	zone_proj0->kpj_nlwps += pp->p_lwpcnt;
34930Sstevel@tonic-gate 	zone->zone_nlwps += pp->p_lwpcnt;
34940Sstevel@tonic-gate 	/* add 1 task to zone's proj0 */
34950Sstevel@tonic-gate 	zone_proj0->kpj_ntasks += 1;
34960Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
34970Sstevel@tonic-gate 	mutex_exit(&zone->zone_nlwps_lock);
34980Sstevel@tonic-gate 
34990Sstevel@tonic-gate 	/* remove lwps from proc's old zone and old project */
35000Sstevel@tonic-gate 	mutex_enter(&pp->p_zone->zone_nlwps_lock);
35010Sstevel@tonic-gate 	pp->p_zone->zone_nlwps -= pp->p_lwpcnt;
35020Sstevel@tonic-gate 	pp->p_task->tk_proj->kpj_nlwps -= pp->p_lwpcnt;
35030Sstevel@tonic-gate 	mutex_exit(&pp->p_zone->zone_nlwps_lock);
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate 	/*
35060Sstevel@tonic-gate 	 * Joining the zone cannot fail from now on.
35070Sstevel@tonic-gate 	 *
35080Sstevel@tonic-gate 	 * This means that a lot of the following code can be commonized and
35090Sstevel@tonic-gate 	 * shared with zsched().
35100Sstevel@tonic-gate 	 */
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 	/*
35130Sstevel@tonic-gate 	 * Reset the encapsulating process contract's zone.
35140Sstevel@tonic-gate 	 */
35150Sstevel@tonic-gate 	ASSERT(ct->ct_mzuniqid == GLOBAL_ZONEUNIQID);
35160Sstevel@tonic-gate 	contract_setzuniqid(ct, zone->zone_uniqid);
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 	/*
35190Sstevel@tonic-gate 	 * Create a new task and associate the process with the project keyed
35200Sstevel@tonic-gate 	 * by (projid,zoneid).
35210Sstevel@tonic-gate 	 *
35220Sstevel@tonic-gate 	 * We might as well be in project 0; the global zone's projid doesn't
35230Sstevel@tonic-gate 	 * make much sense in a zone anyhow.
35240Sstevel@tonic-gate 	 *
35250Sstevel@tonic-gate 	 * This also increments zone_ntasks, and returns with p_lock held.
35260Sstevel@tonic-gate 	 */
35270Sstevel@tonic-gate 	tk = task_create(0, zone);
35280Sstevel@tonic-gate 	oldtk = task_join(tk, 0);
35290Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate 	pp->p_flag |= SZONETOP;
35320Sstevel@tonic-gate 	pp->p_zone = zone;
35330Sstevel@tonic-gate 
35340Sstevel@tonic-gate 	/*
35350Sstevel@tonic-gate 	 * call RCTLOP_SET functions on this proc
35360Sstevel@tonic-gate 	 */
35370Sstevel@tonic-gate 	e.rcep_p.zone = zone;
35380Sstevel@tonic-gate 	e.rcep_t = RCENTITY_ZONE;
35390Sstevel@tonic-gate 	(void) rctl_set_dup(NULL, NULL, pp, &e, zone->zone_rctls, NULL,
35400Sstevel@tonic-gate 	    RCD_CALLBACK);
35410Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate 	/*
35440Sstevel@tonic-gate 	 * We don't need to hold any of zsched's locks here; not only do we know
35450Sstevel@tonic-gate 	 * the process and zone aren't going away, we know its session isn't
35460Sstevel@tonic-gate 	 * changing either.
35470Sstevel@tonic-gate 	 *
35480Sstevel@tonic-gate 	 * By joining zsched's session here, we mimic the behavior in the
35490Sstevel@tonic-gate 	 * global zone of init's sid being the pid of sched.  We extend this
35500Sstevel@tonic-gate 	 * to all zlogin-like zone_enter()'ing processes as well.
35510Sstevel@tonic-gate 	 */
35520Sstevel@tonic-gate 	mutex_enter(&pidlock);
35530Sstevel@tonic-gate 	sp = zone->zone_zsched->p_sessp;
35540Sstevel@tonic-gate 	SESS_HOLD(sp);
35550Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
35560Sstevel@tonic-gate 	pgexit(pp);
35570Sstevel@tonic-gate 	SESS_RELE(pp->p_sessp);
35580Sstevel@tonic-gate 	pp->p_sessp = sp;
35590Sstevel@tonic-gate 	pgjoin(pp, zone->zone_zsched->p_pidp);
35600Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
35610Sstevel@tonic-gate 	mutex_exit(&pidlock);
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
35640Sstevel@tonic-gate 	/*
35650Sstevel@tonic-gate 	 * We're firmly in the zone; let pools progress.
35660Sstevel@tonic-gate 	 */
35670Sstevel@tonic-gate 	pool_unlock();
35680Sstevel@tonic-gate 	task_rele(oldtk);
35690Sstevel@tonic-gate 	/*
35700Sstevel@tonic-gate 	 * We don't need to retain a hold on the zone since we already
35710Sstevel@tonic-gate 	 * incremented zone_ntasks, so the zone isn't going anywhere.
35720Sstevel@tonic-gate 	 */
35730Sstevel@tonic-gate 	zone_rele(zone);
35740Sstevel@tonic-gate 
35750Sstevel@tonic-gate 	/*
35760Sstevel@tonic-gate 	 * Chroot
35770Sstevel@tonic-gate 	 */
35780Sstevel@tonic-gate 	vp = zone->zone_rootvp;
35790Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_cdir, pp);
35800Sstevel@tonic-gate 	zone_chdir(vp, &PTOU(pp)->u_rdir, pp);
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 	/*
35830Sstevel@tonic-gate 	 * Change process credentials
35840Sstevel@tonic-gate 	 */
35850Sstevel@tonic-gate 	newcr = cralloc();
35860Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
35870Sstevel@tonic-gate 	cr = pp->p_cred;
35880Sstevel@tonic-gate 	crcopy_to(cr, newcr);
35890Sstevel@tonic-gate 	crsetzone(newcr, zone);
35900Sstevel@tonic-gate 	pp->p_cred = newcr;
35910Sstevel@tonic-gate 
35920Sstevel@tonic-gate 	/*
35930Sstevel@tonic-gate 	 * Restrict all process privilege sets to zone limit
35940Sstevel@tonic-gate 	 */
35950Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_PPRIV(newcr));
35960Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_EPRIV(newcr));
35970Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_IPRIV(newcr));
35980Sstevel@tonic-gate 	priv_intersect(zone->zone_privset, &CR_LPRIV(newcr));
35990Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
36000Sstevel@tonic-gate 	crset(pp, newcr);
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate 	/*
36030Sstevel@tonic-gate 	 * Adjust upcount to reflect zone entry.
36040Sstevel@tonic-gate 	 */
36050Sstevel@tonic-gate 	uid = crgetruid(newcr);
36060Sstevel@tonic-gate 	mutex_enter(&pidlock);
36070Sstevel@tonic-gate 	upcount_dec(uid, GLOBAL_ZONEID);
36080Sstevel@tonic-gate 	upcount_inc(uid, zoneid);
36090Sstevel@tonic-gate 	mutex_exit(&pidlock);
36100Sstevel@tonic-gate 
36110Sstevel@tonic-gate 	/*
36120Sstevel@tonic-gate 	 * Set up core file path and content.
36130Sstevel@tonic-gate 	 */
36140Sstevel@tonic-gate 	set_core_defaults();
36150Sstevel@tonic-gate 
36160Sstevel@tonic-gate out:
36170Sstevel@tonic-gate 	/*
36180Sstevel@tonic-gate 	 * Let the other lwps continue.
36190Sstevel@tonic-gate 	 */
36200Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
36210Sstevel@tonic-gate 	if (curthread != pp->p_agenttp)
36220Sstevel@tonic-gate 		continuelwps(pp);
36230Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
36240Sstevel@tonic-gate 
36250Sstevel@tonic-gate 	return (err != 0 ? set_errno(err) : 0);
36260Sstevel@tonic-gate }
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate /*
36290Sstevel@tonic-gate  * Systemcall entry point for zone_list(2).
36300Sstevel@tonic-gate  *
36310Sstevel@tonic-gate  * Processes running in a (non-global) zone only see themselves.
36320Sstevel@tonic-gate  */
36330Sstevel@tonic-gate static int
36340Sstevel@tonic-gate zone_list(zoneid_t *zoneidlist, uint_t *numzones)
36350Sstevel@tonic-gate {
36360Sstevel@tonic-gate 	zoneid_t *zoneids;
36370Sstevel@tonic-gate 	zone_t *zone;
36380Sstevel@tonic-gate 	uint_t user_nzones, real_nzones;
36390Sstevel@tonic-gate 	int error = 0;
36400Sstevel@tonic-gate 	uint_t i;
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate 	if (copyin(numzones, &user_nzones, sizeof (uint_t)) != 0)
36430Sstevel@tonic-gate 		return (set_errno(EFAULT));
36440Sstevel@tonic-gate 
36450Sstevel@tonic-gate 	if (curproc->p_zone != global_zone) {
36460Sstevel@tonic-gate 		/* just return current zone */
36470Sstevel@tonic-gate 		real_nzones = 1;
36480Sstevel@tonic-gate 		zoneids = kmem_alloc(sizeof (zoneid_t), KM_SLEEP);
36490Sstevel@tonic-gate 		zoneids[0] = curproc->p_zone->zone_id;
36500Sstevel@tonic-gate 	} else {
36510Sstevel@tonic-gate 		mutex_enter(&zonehash_lock);
36520Sstevel@tonic-gate 		real_nzones = zonecount;
36530Sstevel@tonic-gate 		if (real_nzones) {
36540Sstevel@tonic-gate 			zoneids = kmem_alloc(real_nzones * sizeof (zoneid_t),
36550Sstevel@tonic-gate 			    KM_SLEEP);
36560Sstevel@tonic-gate 			i = 0;
36570Sstevel@tonic-gate 			for (zone = list_head(&zone_active); zone != NULL;
36580Sstevel@tonic-gate 			    zone = list_next(&zone_active, zone))
36590Sstevel@tonic-gate 				zoneids[i++] = zone->zone_id;
36600Sstevel@tonic-gate 			ASSERT(i == real_nzones);
36610Sstevel@tonic-gate 		}
36620Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
36630Sstevel@tonic-gate 	}
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 	if (user_nzones > real_nzones)
36660Sstevel@tonic-gate 		user_nzones = real_nzones;
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate 	if (copyout(&real_nzones, numzones, sizeof (uint_t)) != 0)
36690Sstevel@tonic-gate 		error = EFAULT;
36700Sstevel@tonic-gate 	else if (zoneidlist != NULL && user_nzones != 0) {
36710Sstevel@tonic-gate 		if (copyout(zoneids, zoneidlist,
36720Sstevel@tonic-gate 		    user_nzones * sizeof (zoneid_t)) != 0)
36730Sstevel@tonic-gate 			error = EFAULT;
36740Sstevel@tonic-gate 	}
36750Sstevel@tonic-gate 
36760Sstevel@tonic-gate 	if (real_nzones)
36770Sstevel@tonic-gate 		kmem_free(zoneids, real_nzones * sizeof (zoneid_t));
36780Sstevel@tonic-gate 
36790Sstevel@tonic-gate 	if (error)
36800Sstevel@tonic-gate 		return (set_errno(error));
36810Sstevel@tonic-gate 	else
36820Sstevel@tonic-gate 		return (0);
36830Sstevel@tonic-gate }
36840Sstevel@tonic-gate 
36850Sstevel@tonic-gate /*
36860Sstevel@tonic-gate  * Systemcall entry point for zone_lookup(2).
36870Sstevel@tonic-gate  *
36880Sstevel@tonic-gate  * Non-global zones are only able to see themselves.
36890Sstevel@tonic-gate  */
36900Sstevel@tonic-gate static zoneid_t
36910Sstevel@tonic-gate zone_lookup(const char *zone_name)
36920Sstevel@tonic-gate {
36930Sstevel@tonic-gate 	char *kname;
36940Sstevel@tonic-gate 	zone_t *zone;
36950Sstevel@tonic-gate 	zoneid_t zoneid;
36960Sstevel@tonic-gate 	int err;
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 	if (zone_name == NULL) {
36990Sstevel@tonic-gate 		/* return caller's zone id */
37000Sstevel@tonic-gate 		return (getzoneid());
37010Sstevel@tonic-gate 	}
37020Sstevel@tonic-gate 
37030Sstevel@tonic-gate 	kname = kmem_zalloc(ZONENAME_MAX, KM_SLEEP);
37040Sstevel@tonic-gate 	if ((err = copyinstr(zone_name, kname, ZONENAME_MAX, NULL)) != 0) {
37050Sstevel@tonic-gate 		kmem_free(kname, ZONENAME_MAX);
37060Sstevel@tonic-gate 		return (set_errno(err));
37070Sstevel@tonic-gate 	}
37080Sstevel@tonic-gate 
37090Sstevel@tonic-gate 	mutex_enter(&zonehash_lock);
37100Sstevel@tonic-gate 	zone = zone_find_all_by_name(kname);
37110Sstevel@tonic-gate 	kmem_free(kname, ZONENAME_MAX);
37120Sstevel@tonic-gate 	if (zone == NULL || zone_status_get(zone) < ZONE_IS_READY ||
37130Sstevel@tonic-gate 	    (curproc->p_zone != global_zone && curproc->p_zone != zone)) {
37140Sstevel@tonic-gate 		/* in non-global zone, can only lookup own name */
37150Sstevel@tonic-gate 		mutex_exit(&zonehash_lock);
37160Sstevel@tonic-gate 		return (set_errno(EINVAL));
37170Sstevel@tonic-gate 	}
37180Sstevel@tonic-gate 	zoneid = zone->zone_id;
37190Sstevel@tonic-gate 	mutex_exit(&zonehash_lock);
37200Sstevel@tonic-gate 	return (zoneid);
37210Sstevel@tonic-gate }
37220Sstevel@tonic-gate 
37230Sstevel@tonic-gate /* ARGSUSED */
37240Sstevel@tonic-gate long
37250Sstevel@tonic-gate zone(int cmd, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5)
37260Sstevel@tonic-gate {
37270Sstevel@tonic-gate 	zone_def zs;
37280Sstevel@tonic-gate 
37290Sstevel@tonic-gate 	switch (cmd) {
37300Sstevel@tonic-gate 	case ZONE_CREATE:
37310Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
37320Sstevel@tonic-gate 			if (copyin(arg1, &zs, sizeof (zone_def))) {
37330Sstevel@tonic-gate 				return (set_errno(EFAULT));
37340Sstevel@tonic-gate 			}
37350Sstevel@tonic-gate 		} else {
37360Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
37370Sstevel@tonic-gate 			zone_def32 zs32;
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate 			if (copyin(arg1, &zs32, sizeof (zone_def32))) {
37400Sstevel@tonic-gate 				return (set_errno(EFAULT));
37410Sstevel@tonic-gate 			}
37420Sstevel@tonic-gate 			zs.zone_name =
37430Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_name;
37440Sstevel@tonic-gate 			zs.zone_root =
37450Sstevel@tonic-gate 			    (const char *)(unsigned long)zs32.zone_root;
37460Sstevel@tonic-gate 			zs.zone_privs =
37470Sstevel@tonic-gate 			    (const struct priv_set *)
37480Sstevel@tonic-gate 			    (unsigned long)zs32.zone_privs;
37490Sstevel@tonic-gate 			zs.rctlbuf = (caddr_t)(unsigned long)zs32.rctlbuf;
37500Sstevel@tonic-gate 			zs.rctlbufsz = zs32.rctlbufsz;
37510Sstevel@tonic-gate 			zs.extended_error =
37520Sstevel@tonic-gate 			    (int *)(unsigned long)zs32.extended_error;
37530Sstevel@tonic-gate #else
37540Sstevel@tonic-gate 			panic("get_udatamodel() returned bogus result\n");
37550Sstevel@tonic-gate #endif
37560Sstevel@tonic-gate 		}
37570Sstevel@tonic-gate 
37580Sstevel@tonic-gate 		return (zone_create(zs.zone_name, zs.zone_root,
37590Sstevel@tonic-gate 			zs.zone_privs, (caddr_t)zs.rctlbuf, zs.rctlbufsz,
37600Sstevel@tonic-gate 			zs.extended_error));
37610Sstevel@tonic-gate 	case ZONE_BOOT:
37620Sstevel@tonic-gate 		return (zone_boot((zoneid_t)(uintptr_t)arg1,
37630Sstevel@tonic-gate 		    (const char *)arg2));
37640Sstevel@tonic-gate 	case ZONE_DESTROY:
37650Sstevel@tonic-gate 		return (zone_destroy((zoneid_t)(uintptr_t)arg1));
37660Sstevel@tonic-gate 	case ZONE_GETATTR:
37670Sstevel@tonic-gate 		return (zone_getattr((zoneid_t)(uintptr_t)arg1,
37680Sstevel@tonic-gate 		    (int)(uintptr_t)arg2, arg3, (size_t)arg4));
37690Sstevel@tonic-gate 	case ZONE_ENTER:
37700Sstevel@tonic-gate 		return (zone_enter((zoneid_t)(uintptr_t)arg1));
37710Sstevel@tonic-gate 	case ZONE_LIST:
37720Sstevel@tonic-gate 		return (zone_list((zoneid_t *)arg1, (uint_t *)arg2));
37730Sstevel@tonic-gate 	case ZONE_SHUTDOWN:
37740Sstevel@tonic-gate 		return (zone_shutdown((zoneid_t)(uintptr_t)arg1));
37750Sstevel@tonic-gate 	case ZONE_LOOKUP:
37760Sstevel@tonic-gate 		return (zone_lookup((const char *)arg1));
37770Sstevel@tonic-gate 	default:
37780Sstevel@tonic-gate 		return (set_errno(EINVAL));
37790Sstevel@tonic-gate 	}
37800Sstevel@tonic-gate }
37810Sstevel@tonic-gate 
37820Sstevel@tonic-gate struct zarg {
37830Sstevel@tonic-gate 	zone_t *zone;
37840Sstevel@tonic-gate 	zone_cmd_arg_t arg;
37850Sstevel@tonic-gate };
37860Sstevel@tonic-gate 
37870Sstevel@tonic-gate static int
37880Sstevel@tonic-gate zone_lookup_door(const char *zone_name, door_handle_t *doorp)
37890Sstevel@tonic-gate {
37900Sstevel@tonic-gate 	char *buf;
37910Sstevel@tonic-gate 	size_t buflen;
37920Sstevel@tonic-gate 	int error;
37930Sstevel@tonic-gate 
37940Sstevel@tonic-gate 	buflen = sizeof (ZONE_DOOR_PATH) + strlen(zone_name);
37950Sstevel@tonic-gate 	buf = kmem_alloc(buflen, KM_SLEEP);
37960Sstevel@tonic-gate 	(void) snprintf(buf, buflen, ZONE_DOOR_PATH, zone_name);
37970Sstevel@tonic-gate 	error = door_ki_open(buf, doorp);
37980Sstevel@tonic-gate 	kmem_free(buf, buflen);
37990Sstevel@tonic-gate 	return (error);
38000Sstevel@tonic-gate }
38010Sstevel@tonic-gate 
38020Sstevel@tonic-gate static void
38030Sstevel@tonic-gate zone_release_door(door_handle_t *doorp)
38040Sstevel@tonic-gate {
38050Sstevel@tonic-gate 	door_ki_rele(*doorp);
38060Sstevel@tonic-gate 	*doorp = NULL;
38070Sstevel@tonic-gate }
38080Sstevel@tonic-gate 
38090Sstevel@tonic-gate static void
38100Sstevel@tonic-gate zone_ki_call_zoneadmd(struct zarg *zargp)
38110Sstevel@tonic-gate {
38120Sstevel@tonic-gate 	door_handle_t door = NULL;
38130Sstevel@tonic-gate 	door_arg_t darg, save_arg;
38140Sstevel@tonic-gate 	char *zone_name;
38150Sstevel@tonic-gate 	size_t zone_namelen;
38160Sstevel@tonic-gate 	zoneid_t zoneid;
38170Sstevel@tonic-gate 	zone_t *zone;
38180Sstevel@tonic-gate 	zone_cmd_arg_t arg;
38190Sstevel@tonic-gate 	uint64_t uniqid;
38200Sstevel@tonic-gate 	size_t size;
38210Sstevel@tonic-gate 	int error;
38220Sstevel@tonic-gate 	int retry;
38230Sstevel@tonic-gate 
38240Sstevel@tonic-gate 	zone = zargp->zone;
38250Sstevel@tonic-gate 	arg = zargp->arg;
38260Sstevel@tonic-gate 	kmem_free(zargp, sizeof (*zargp));
38270Sstevel@tonic-gate 
38280Sstevel@tonic-gate 	zone_namelen = strlen(zone->zone_name) + 1;
38290Sstevel@tonic-gate 	zone_name = kmem_alloc(zone_namelen, KM_SLEEP);
38300Sstevel@tonic-gate 	bcopy(zone->zone_name, zone_name, zone_namelen);
38310Sstevel@tonic-gate 	zoneid = zone->zone_id;
38320Sstevel@tonic-gate 	uniqid = zone->zone_uniqid;
38330Sstevel@tonic-gate 	/*
38340Sstevel@tonic-gate 	 * zoneadmd may be down, but at least we can empty out the zone.
38350Sstevel@tonic-gate 	 * We can ignore the return value of zone_empty() since we're called
38360Sstevel@tonic-gate 	 * from a kernel thread and know we won't be delivered any signals.
38370Sstevel@tonic-gate 	 */
38380Sstevel@tonic-gate 	ASSERT(curproc == &p0);
38390Sstevel@tonic-gate 	(void) zone_empty(zone);
38400Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) >= ZONE_IS_EMPTY);
38410Sstevel@tonic-gate 	zone_rele(zone);
38420Sstevel@tonic-gate 
38430Sstevel@tonic-gate 	size = sizeof (arg);
38440Sstevel@tonic-gate 	darg.rbuf = (char *)&arg;
38450Sstevel@tonic-gate 	darg.data_ptr = (char *)&arg;
38460Sstevel@tonic-gate 	darg.rsize = size;
38470Sstevel@tonic-gate 	darg.data_size = size;
38480Sstevel@tonic-gate 	darg.desc_ptr = NULL;
38490Sstevel@tonic-gate 	darg.desc_num = 0;
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 	save_arg = darg;
38520Sstevel@tonic-gate 	/*
38530Sstevel@tonic-gate 	 * Since we're not holding a reference to the zone, any number of
38540Sstevel@tonic-gate 	 * things can go wrong, including the zone disappearing before we get a
38550Sstevel@tonic-gate 	 * chance to talk to zoneadmd.
38560Sstevel@tonic-gate 	 */
38570Sstevel@tonic-gate 	for (retry = 0; /* forever */; retry++) {
38580Sstevel@tonic-gate 		if (door == NULL &&
38590Sstevel@tonic-gate 		    (error = zone_lookup_door(zone_name, &door)) != 0) {
38600Sstevel@tonic-gate 			goto next;
38610Sstevel@tonic-gate 		}
38620Sstevel@tonic-gate 		ASSERT(door != NULL);
38630Sstevel@tonic-gate 
38640Sstevel@tonic-gate 		if ((error = door_ki_upcall(door, &darg)) == 0) {
38650Sstevel@tonic-gate 			break;
38660Sstevel@tonic-gate 		}
38670Sstevel@tonic-gate 		switch (error) {
38680Sstevel@tonic-gate 		case EINTR:
38690Sstevel@tonic-gate 			/* FALLTHROUGH */
38700Sstevel@tonic-gate 		case EAGAIN:	/* process may be forking */
38710Sstevel@tonic-gate 			/*
38720Sstevel@tonic-gate 			 * Back off for a bit
38730Sstevel@tonic-gate 			 */
38740Sstevel@tonic-gate 			break;
38750Sstevel@tonic-gate 		case EBADF:
38760Sstevel@tonic-gate 			zone_release_door(&door);
38770Sstevel@tonic-gate 			if (zone_lookup_door(zone_name, &door) != 0) {
38780Sstevel@tonic-gate 				/*
38790Sstevel@tonic-gate 				 * zoneadmd may be dead, but it may come back to
38800Sstevel@tonic-gate 				 * life later.
38810Sstevel@tonic-gate 				 */
38820Sstevel@tonic-gate 				break;
38830Sstevel@tonic-gate 			}
38840Sstevel@tonic-gate 			break;
38850Sstevel@tonic-gate 		default:
38860Sstevel@tonic-gate 			cmn_err(CE_WARN,
38870Sstevel@tonic-gate 			    "zone_ki_call_zoneadmd: door_ki_upcall error %d\n",
38880Sstevel@tonic-gate 			    error);
38890Sstevel@tonic-gate 			goto out;
38900Sstevel@tonic-gate 		}
38910Sstevel@tonic-gate next:
38920Sstevel@tonic-gate 		/*
38930Sstevel@tonic-gate 		 * If this isn't the same zone_t that we originally had in mind,
38940Sstevel@tonic-gate 		 * then this is the same as if two kadmin requests come in at
38950Sstevel@tonic-gate 		 * the same time: the first one wins.  This means we lose, so we
38960Sstevel@tonic-gate 		 * bail.
38970Sstevel@tonic-gate 		 */
38980Sstevel@tonic-gate 		if ((zone = zone_find_by_id(zoneid)) == NULL) {
38990Sstevel@tonic-gate 			/*
39000Sstevel@tonic-gate 			 * Problem is solved.
39010Sstevel@tonic-gate 			 */
39020Sstevel@tonic-gate 			break;
39030Sstevel@tonic-gate 		}
39040Sstevel@tonic-gate 		if (zone->zone_uniqid != uniqid) {
39050Sstevel@tonic-gate 			/*
39060Sstevel@tonic-gate 			 * zoneid recycled
39070Sstevel@tonic-gate 			 */
39080Sstevel@tonic-gate 			zone_rele(zone);
39090Sstevel@tonic-gate 			break;
39100Sstevel@tonic-gate 		}
39110Sstevel@tonic-gate 		/*
39120Sstevel@tonic-gate 		 * We could zone_status_timedwait(), but there doesn't seem to
39130Sstevel@tonic-gate 		 * be much point in doing that (plus, it would mean that
39140Sstevel@tonic-gate 		 * zone_free() isn't called until this thread exits).
39150Sstevel@tonic-gate 		 */
39160Sstevel@tonic-gate 		zone_rele(zone);
39170Sstevel@tonic-gate 		delay(hz);
39180Sstevel@tonic-gate 		darg = save_arg;
39190Sstevel@tonic-gate 	}
39200Sstevel@tonic-gate out:
39210Sstevel@tonic-gate 	if (door != NULL) {
39220Sstevel@tonic-gate 		zone_release_door(&door);
39230Sstevel@tonic-gate 	}
39240Sstevel@tonic-gate 	kmem_free(zone_name, zone_namelen);
39250Sstevel@tonic-gate 	thread_exit();
39260Sstevel@tonic-gate }
39270Sstevel@tonic-gate 
39280Sstevel@tonic-gate /*
39290Sstevel@tonic-gate  * Entry point for uadmin() to tell the zone to go away or reboot.  The caller
39300Sstevel@tonic-gate  * is a process in the zone to be modified.
39310Sstevel@tonic-gate  *
39320Sstevel@tonic-gate  * In order to shutdown the zone, we will hand off control to zoneadmd
39330Sstevel@tonic-gate  * (running in the global zone) via a door.  We do a half-hearted job at
39340Sstevel@tonic-gate  * killing all processes in the zone, create a kernel thread to contact
39350Sstevel@tonic-gate  * zoneadmd, and make note of the "uniqid" of the zone.  The uniqid is
39360Sstevel@tonic-gate  * a form of generation number used to let zoneadmd (as well as
39370Sstevel@tonic-gate  * zone_destroy()) know exactly which zone they're re talking about.
39380Sstevel@tonic-gate  */
39390Sstevel@tonic-gate int
39400Sstevel@tonic-gate zone_uadmin(int cmd, int fcn, cred_t *credp)
39410Sstevel@tonic-gate {
39420Sstevel@tonic-gate 	struct zarg *zargp;
39430Sstevel@tonic-gate 	zone_cmd_t zcmd;
39440Sstevel@tonic-gate 	zone_t *zone;
39450Sstevel@tonic-gate 
39460Sstevel@tonic-gate 	zone = curproc->p_zone;
39470Sstevel@tonic-gate 	ASSERT(getzoneid() != GLOBAL_ZONEID);
39480Sstevel@tonic-gate 
39490Sstevel@tonic-gate 	switch (cmd) {
39500Sstevel@tonic-gate 	case A_SHUTDOWN:
39510Sstevel@tonic-gate 		switch (fcn) {
39520Sstevel@tonic-gate 		case AD_HALT:
39530Sstevel@tonic-gate 		case AD_POWEROFF:
39540Sstevel@tonic-gate 			zcmd = Z_HALT;
39550Sstevel@tonic-gate 			break;
39560Sstevel@tonic-gate 		case AD_BOOT:
39570Sstevel@tonic-gate 			zcmd = Z_REBOOT;
39580Sstevel@tonic-gate 			break;
39590Sstevel@tonic-gate 		case AD_IBOOT:
39600Sstevel@tonic-gate 		case AD_SBOOT:
39610Sstevel@tonic-gate 		case AD_SIBOOT:
39620Sstevel@tonic-gate 		case AD_NOSYNC:
39630Sstevel@tonic-gate 			return (ENOTSUP);
39640Sstevel@tonic-gate 		default:
39650Sstevel@tonic-gate 			return (EINVAL);
39660Sstevel@tonic-gate 		}
39670Sstevel@tonic-gate 		break;
39680Sstevel@tonic-gate 	case A_REBOOT:
39690Sstevel@tonic-gate 		zcmd = Z_REBOOT;
39700Sstevel@tonic-gate 		break;
39710Sstevel@tonic-gate 	case A_FTRACE:
39720Sstevel@tonic-gate 	case A_REMOUNT:
39730Sstevel@tonic-gate 	case A_FREEZE:
39740Sstevel@tonic-gate 	case A_DUMP:
39750Sstevel@tonic-gate 		return (ENOTSUP);
39760Sstevel@tonic-gate 	default:
39770Sstevel@tonic-gate 		ASSERT(cmd != A_SWAPCTL);	/* handled by uadmin() */
39780Sstevel@tonic-gate 		return (EINVAL);
39790Sstevel@tonic-gate 	}
39800Sstevel@tonic-gate 
39810Sstevel@tonic-gate 	if (secpolicy_zone_admin(credp, B_FALSE))
39820Sstevel@tonic-gate 		return (EPERM);
39830Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
39840Sstevel@tonic-gate 	/*
39850Sstevel@tonic-gate 	 * zone_status can't be ZONE_IS_EMPTY or higher since curproc
39860Sstevel@tonic-gate 	 * is in the zone.
39870Sstevel@tonic-gate 	 */
39880Sstevel@tonic-gate 	ASSERT(zone_status_get(zone) < ZONE_IS_EMPTY);
39890Sstevel@tonic-gate 	if (zone_status_get(zone) > ZONE_IS_RUNNING) {
39900Sstevel@tonic-gate 		/*
39910Sstevel@tonic-gate 		 * This zone is already on its way down.
39920Sstevel@tonic-gate 		 */
39930Sstevel@tonic-gate 		mutex_exit(&zone_status_lock);
39940Sstevel@tonic-gate 		return (0);
39950Sstevel@tonic-gate 	}
39960Sstevel@tonic-gate 	/*
39970Sstevel@tonic-gate 	 * Prevent future zone_enter()s
39980Sstevel@tonic-gate 	 */
39990Sstevel@tonic-gate 	zone_status_set(zone, ZONE_IS_SHUTTING_DOWN);
40000Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
40010Sstevel@tonic-gate 
40020Sstevel@tonic-gate 	/*
40030Sstevel@tonic-gate 	 * Kill everyone now and call zoneadmd later.
40040Sstevel@tonic-gate 	 * zone_ki_call_zoneadmd() will do a more thorough job of this
40050Sstevel@tonic-gate 	 * later.
40060Sstevel@tonic-gate 	 */
40070Sstevel@tonic-gate 	killall(zone->zone_id);
40080Sstevel@tonic-gate 	/*
40090Sstevel@tonic-gate 	 * Now, create the thread to contact zoneadmd and do the rest of the
40100Sstevel@tonic-gate 	 * work.  This thread can't be created in our zone otherwise
40110Sstevel@tonic-gate 	 * zone_destroy() would deadlock.
40120Sstevel@tonic-gate 	 */
40130Sstevel@tonic-gate 	zargp = kmem_alloc(sizeof (*zargp), KM_SLEEP);
40140Sstevel@tonic-gate 	zargp->arg.cmd = zcmd;
40150Sstevel@tonic-gate 	zargp->arg.uniqid = zone->zone_uniqid;
40160Sstevel@tonic-gate 	(void) strcpy(zargp->arg.locale, "C");
40170Sstevel@tonic-gate 	zone_hold(zargp->zone = zone);
40180Sstevel@tonic-gate 
40190Sstevel@tonic-gate 	(void) thread_create(NULL, 0, zone_ki_call_zoneadmd, zargp, 0, &p0,
40200Sstevel@tonic-gate 	    TS_RUN, minclsyspri);
40210Sstevel@tonic-gate 	exit(CLD_EXITED, 0);
40220Sstevel@tonic-gate 
40230Sstevel@tonic-gate 	return (EINVAL);
40240Sstevel@tonic-gate }
40250Sstevel@tonic-gate 
40260Sstevel@tonic-gate /*
40270Sstevel@tonic-gate  * Entry point so kadmin(A_SHUTDOWN, ...) can set the global zone's
40280Sstevel@tonic-gate  * status to ZONE_IS_SHUTTING_DOWN.
40290Sstevel@tonic-gate  */
40300Sstevel@tonic-gate void
40310Sstevel@tonic-gate zone_shutdown_global(void)
40320Sstevel@tonic-gate {
40330Sstevel@tonic-gate 	ASSERT(curproc->p_zone == global_zone);
40340Sstevel@tonic-gate 
40350Sstevel@tonic-gate 	mutex_enter(&zone_status_lock);
40360Sstevel@tonic-gate 	ASSERT(zone_status_get(global_zone) == ZONE_IS_RUNNING);
40370Sstevel@tonic-gate 	zone_status_set(global_zone, ZONE_IS_SHUTTING_DOWN);
40380Sstevel@tonic-gate 	mutex_exit(&zone_status_lock);
40390Sstevel@tonic-gate }
4040