xref: /illumos-gate/usr/src/uts/common/syscall/uadmin.c (revision bb65110f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53f2f09c1Sdp  * Common Development and Distribution License (the "License").
63f2f09c1Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2197eda132Sraf 
227c478bd9Sstevel@tonic-gate /*
23753a6d45SSherry Moore  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25a288e5a9SJoshua M. Clulow  * Copyright 2013 Joyent, Inc.  All rights reserved.
26*bb65110fSLuqman Aden  * Copyright 2024 Oxide Computer Company
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
357c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
367c478bd9Sstevel@tonic-gate #include <sys/swap.h>
377c478bd9Sstevel@tonic-gate #include <sys/file.h>
387c478bd9Sstevel@tonic-gate #include <sys/proc.h>
397c478bd9Sstevel@tonic-gate #include <sys/var.h>
407c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
417c478bd9Sstevel@tonic-gate #include <sys/signal.h>
427c478bd9Sstevel@tonic-gate #include <sys/time.h>
437c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
457c478bd9Sstevel@tonic-gate #include <sys/callb.h>
467c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
477c478bd9Sstevel@tonic-gate #include <sys/debug.h>
487c478bd9Sstevel@tonic-gate #include <sys/ftrace.h>
497c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
507c478bd9Sstevel@tonic-gate #include <sys/panic.h>
517c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
52a288e5a9SJoshua M. Clulow #include <sys/ddi_periodic.h>
537c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
547c478bd9Sstevel@tonic-gate #include <sys/policy.h>
557c478bd9Sstevel@tonic-gate #include <sys/zone.h>
56ed0ee1acScg209009 #include <sys/condvar.h>
57ed0ee1acScg209009 #include <sys/thread.h>
5810e6dadfSbrendan #include <sys/sdt.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Administrivia system call.  We provide this in two flavors: one for calling
627c478bd9Sstevel@tonic-gate  * from the system call path (uadmin), and the other for calling from elsewhere
637c478bd9Sstevel@tonic-gate  * within the kernel (kadmin).  Callers must beware that certain uadmin cmd
647c478bd9Sstevel@tonic-gate  * values (specifically A_SWAPCTL) are only supported by uadmin and not kadmin.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate extern ksema_t fsflush_sema;
687c478bd9Sstevel@tonic-gate kmutex_t ualock;
69ed0ee1acScg209009 kcondvar_t uacond;
70ed0ee1acScg209009 kthread_t *ua_shutdown_thread = NULL;
717c478bd9Sstevel@tonic-gate 
72facf4a8dSllai1 int sys_shutdown = 0;
7319397407SSherry Moore volatile int fastreboot_dryrun = 0;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Kill all user processes in said zone.  A special argument of ALL_ZONES is
777c478bd9Sstevel@tonic-gate  * passed in when the system as a whole is shutting down.  The lack of per-zone
787c478bd9Sstevel@tonic-gate  * process lists is likely to make the following a performance bottleneck on a
797c478bd9Sstevel@tonic-gate  * system with many zones.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate void
killall(zoneid_t zoneid)827c478bd9Sstevel@tonic-gate killall(zoneid_t zoneid)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	proc_t *p;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	ASSERT(zoneid != GLOBAL_ZONEID);
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * Kill all processes except kernel daemons and ourself.
897c478bd9Sstevel@tonic-gate 	 * Make a first pass to stop all processes so they won't
907c478bd9Sstevel@tonic-gate 	 * be trying to restart children as we kill them.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
937c478bd9Sstevel@tonic-gate 	for (p = practive; p != NULL; p = p->p_next) {
947c478bd9Sstevel@tonic-gate 		if ((zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid) &&
957c478bd9Sstevel@tonic-gate 		    p->p_exec != NULLVP &&	/* kernel daemons */
967c478bd9Sstevel@tonic-gate 		    p->p_as != &kas &&
977c478bd9Sstevel@tonic-gate 		    p->p_stat != SZOMB) {
987c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
997c478bd9Sstevel@tonic-gate 			p->p_flag |= SNOWAIT;
1007c478bd9Sstevel@tonic-gate 			sigtoproc(p, NULL, SIGSTOP);
1017c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 	p = practive;
1057c478bd9Sstevel@tonic-gate 	while (p != NULL) {
1067c478bd9Sstevel@tonic-gate 		if ((zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid) &&
1077c478bd9Sstevel@tonic-gate 		    p->p_exec != NULLVP &&	/* kernel daemons */
1087c478bd9Sstevel@tonic-gate 		    p->p_as != &kas &&
1097c478bd9Sstevel@tonic-gate 		    p->p_stat != SIDL &&
1107c478bd9Sstevel@tonic-gate 		    p->p_stat != SZOMB) {
1117c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1127c478bd9Sstevel@tonic-gate 			if (sigismember(&p->p_sig, SIGKILL)) {
1137c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
1147c478bd9Sstevel@tonic-gate 				p = p->p_next;
1157c478bd9Sstevel@tonic-gate 			} else {
1167c478bd9Sstevel@tonic-gate 				sigtoproc(p, NULL, SIGKILL);
1177c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
118d3d50737SRafael Vanoni 				(void) cv_reltimedwait(&p->p_srwchan_cv,
119d3d50737SRafael Vanoni 				    &pidlock, hz, TR_CLOCK_TICK);
1207c478bd9Sstevel@tonic-gate 				p = practive;
1217c478bd9Sstevel@tonic-gate 			}
1227c478bd9Sstevel@tonic-gate 		} else {
1237c478bd9Sstevel@tonic-gate 			p = p->p_next;
1247c478bd9Sstevel@tonic-gate 		}
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
129*bb65110fSLuqman Aden /*
130*bb65110fSLuqman Aden  * Emits an SDT probe (sdt:::test) with 7 arguments.  This is used to test
131*bb65110fSLuqman Aden  * arguments are passed properly, whether via registers or on the stack.
132*bb65110fSLuqman Aden  */
133*bb65110fSLuqman Aden static void
sdt_test_args(void)134*bb65110fSLuqman Aden sdt_test_args(void)
135*bb65110fSLuqman Aden {
136*bb65110fSLuqman Aden 	DTRACE_PROBE7(test, int, 1, int, 2, int, 3, int, 4, int, 5,
137*bb65110fSLuqman Aden 	    int, 6, int, 7);
138*bb65110fSLuqman Aden }
139*bb65110fSLuqman Aden 
140*bb65110fSLuqman Aden /*
141*bb65110fSLuqman Aden  * Same as above, but with the probe called as a tail call.
142*bb65110fSLuqman Aden  * Unfortunately, gcc doesn't yet have a [[musttail]] attribute that would
143*bb65110fSLuqman Aden  * either generate a tail call or error at compile time if it can't.  Instead
144*bb65110fSLuqman Aden  * we use a separate function along with a optimize pragma.  On x86, this does
145*bb65110fSLuqman Aden  * indeed generate a tail call as written.
146*bb65110fSLuqman Aden  */
147*bb65110fSLuqman Aden #pragma GCC push_options
148*bb65110fSLuqman Aden #pragma GCC optimize("optimize-sibling-calls")
149*bb65110fSLuqman Aden static void
sdt_test_args_tail_call(int a,int b,int c,int d,int e,int f,int g)150*bb65110fSLuqman Aden sdt_test_args_tail_call(int a, int b, int c, int d, int e, int f, int g)
151*bb65110fSLuqman Aden {
152*bb65110fSLuqman Aden 	DTRACE_PROBE7(test, int, a, int, b, int, c, int, d,
153*bb65110fSLuqman Aden 	    int, e, int, f, int, g);
154*bb65110fSLuqman Aden }
155*bb65110fSLuqman Aden #pragma GCC pop_options
156*bb65110fSLuqman Aden 
1577c478bd9Sstevel@tonic-gate int
kadmin(int cmd,int fcn,void * mdep,cred_t * credp)1587c478bd9Sstevel@tonic-gate kadmin(int cmd, int fcn, void *mdep, cred_t *credp)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	int error = 0;
1617c478bd9Sstevel@tonic-gate 	char *buf;
1627c478bd9Sstevel@tonic-gate 	size_t buflen = 0;
163edc40228Sachartre 	boolean_t invoke_cb = B_FALSE;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * We might be called directly by the kernel's fault-handling code, so
1677c478bd9Sstevel@tonic-gate 	 * we can't assert that the caller is in the global zone.
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Make sure that cmd is one of the valid <sys/uadmin.h> command codes
1727c478bd9Sstevel@tonic-gate 	 * and that we have appropriate privileges for this action.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	switch (cmd) {
1757c478bd9Sstevel@tonic-gate 	case A_FTRACE:
1767c478bd9Sstevel@tonic-gate 	case A_SHUTDOWN:
1777c478bd9Sstevel@tonic-gate 	case A_REBOOT:
1787c478bd9Sstevel@tonic-gate 	case A_REMOUNT:
1797c478bd9Sstevel@tonic-gate 	case A_FREEZE:
1807c478bd9Sstevel@tonic-gate 	case A_DUMP:
18110e6dadfSbrendan 	case A_SDTTEST:
182753a6d45SSherry Moore 	case A_CONFIG:
1837c478bd9Sstevel@tonic-gate 		if (secpolicy_sys_config(credp, B_FALSE) != 0)
1847c478bd9Sstevel@tonic-gate 			return (EPERM);
1857c478bd9Sstevel@tonic-gate 		break;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	default:
1887c478bd9Sstevel@tonic-gate 		return (EINVAL);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/*
192ed0ee1acScg209009 	 * Serialize these operations on ualock.  If it is held, the
193ed0ee1acScg209009 	 * system should shutdown, reboot, or remount shortly, unless there is
194ed0ee1acScg209009 	 * an error.  We need a cv rather than just a mutex because proper
195ed0ee1acScg209009 	 * functioning of A_REBOOT relies on being able to interrupt blocked
196ed0ee1acScg209009 	 * userland callers.
197ed0ee1acScg209009 	 *
198753a6d45SSherry Moore 	 * We only clear ua_shutdown_thread after A_REMOUNT or A_CONFIG.
199753a6d45SSherry Moore 	 * Other commands should never return.
2007c478bd9Sstevel@tonic-gate 	 */
201753a6d45SSherry Moore 	if (cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_REMOUNT ||
202753a6d45SSherry Moore 	    cmd == A_CONFIG) {
203ed0ee1acScg209009 		mutex_enter(&ualock);
204ed0ee1acScg209009 		while (ua_shutdown_thread != NULL) {
205ed0ee1acScg209009 			if (cv_wait_sig(&uacond, &ualock) == 0) {
206ed0ee1acScg209009 				/*
207ed0ee1acScg209009 				 * If we were interrupted, leave, and handle
208ed0ee1acScg209009 				 * the signal (or exit, depending on what
209ed0ee1acScg209009 				 * happened)
210ed0ee1acScg209009 				 */
211ed0ee1acScg209009 				mutex_exit(&ualock);
212ed0ee1acScg209009 				return (EINTR);
213ed0ee1acScg209009 			}
214ed0ee1acScg209009 		}
215ed0ee1acScg209009 		ua_shutdown_thread = curthread;
216ed0ee1acScg209009 		mutex_exit(&ualock);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	switch (cmd) {
2207c478bd9Sstevel@tonic-gate 	case A_SHUTDOWN:
2217c478bd9Sstevel@tonic-gate 	{
2227c478bd9Sstevel@tonic-gate 		proc_t *p = ttoproc(curthread);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		/*
2257c478bd9Sstevel@tonic-gate 		 * Release (almost) all of our own resources if we are called
2267c478bd9Sstevel@tonic-gate 		 * from a user context, however if we are calling kadmin() from
2277c478bd9Sstevel@tonic-gate 		 * a kernel context then we do not release these resources.
2287c478bd9Sstevel@tonic-gate 		 */
22997eda132Sraf 		if (p != &p0) {
23097eda132Sraf 			proc_is_exiting(p);
2318c9fe657Scg209009 			if ((error = exitlwps(0)) != 0) {
232ed0ee1acScg209009 				/*
233ed0ee1acScg209009 				 * Another thread in this process also called
234ed0ee1acScg209009 				 * exitlwps().
235ed0ee1acScg209009 				 */
236ed0ee1acScg209009 				mutex_enter(&ualock);
237ed0ee1acScg209009 				ua_shutdown_thread = NULL;
238ed0ee1acScg209009 				cv_signal(&uacond);
2398c9fe657Scg209009 				mutex_exit(&ualock);
2407c478bd9Sstevel@tonic-gate 				return (error);
2418c9fe657Scg209009 			}
2427c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
2437c478bd9Sstevel@tonic-gate 			p->p_flag |= SNOWAIT;
2447c478bd9Sstevel@tonic-gate 			sigfillset(&p->p_ignore);
2457c478bd9Sstevel@tonic-gate 			curthread->t_lwp->lwp_cursig = 0;
2467c478bd9Sstevel@tonic-gate 			curthread->t_lwp->lwp_extsig = 0;
2477c478bd9Sstevel@tonic-gate 			if (p->p_exec) {
2487c478bd9Sstevel@tonic-gate 				vnode_t *exec_vp = p->p_exec;
2497c478bd9Sstevel@tonic-gate 				p->p_exec = NULLVP;
2507c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
2517c478bd9Sstevel@tonic-gate 				VN_RELE(exec_vp);
2527c478bd9Sstevel@tonic-gate 			} else {
2537c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 			pollcleanup();
2577c478bd9Sstevel@tonic-gate 			closeall(P_FINFO(curproc));
2587c478bd9Sstevel@tonic-gate 			relvm();
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		} else {
2617c478bd9Sstevel@tonic-gate 			/*
2627c478bd9Sstevel@tonic-gate 			 * Reset t_cred if not set because much of the
2637c478bd9Sstevel@tonic-gate 			 * filesystem code depends on CRED() being valid.
2647c478bd9Sstevel@tonic-gate 			 */
2657c478bd9Sstevel@tonic-gate 			if (curthread->t_cred == NULL)
2667c478bd9Sstevel@tonic-gate 				curthread->t_cred = kcred;
2677c478bd9Sstevel@tonic-gate 		}
2687c478bd9Sstevel@tonic-gate 
269facf4a8dSllai1 		/* indicate shutdown in progress */
270facf4a8dSllai1 		sys_shutdown = 1;
271facf4a8dSllai1 
2727c478bd9Sstevel@tonic-gate 		/*
2737c478bd9Sstevel@tonic-gate 		 * Communcate that init shouldn't be restarted.
2747c478bd9Sstevel@tonic-gate 		 */
2757c478bd9Sstevel@tonic-gate 		zone_shutdown_global();
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		killall(ALL_ZONES);
2787c478bd9Sstevel@tonic-gate 		/*
2797c478bd9Sstevel@tonic-gate 		 * If we are calling kadmin() from a kernel context then we
2807c478bd9Sstevel@tonic-gate 		 * do not release these resources.
2817c478bd9Sstevel@tonic-gate 		 */
2827c478bd9Sstevel@tonic-gate 		if (ttoproc(curthread) != &p0) {
283ae115bc7Smrj 			VN_RELE(PTOU(curproc)->u_cdir);
284ae115bc7Smrj 			if (PTOU(curproc)->u_rdir)
285ae115bc7Smrj 				VN_RELE(PTOU(curproc)->u_rdir);
286ae115bc7Smrj 			if (PTOU(curproc)->u_cwd)
287ae115bc7Smrj 				refstr_rele(PTOU(curproc)->u_cwd);
2887c478bd9Sstevel@tonic-gate 
289ae115bc7Smrj 			PTOU(curproc)->u_cdir = rootdir;
290ae115bc7Smrj 			PTOU(curproc)->u_rdir = NULL;
291ae115bc7Smrj 			PTOU(curproc)->u_cwd = NULL;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * Allow the reboot/halt/poweroff code a chance to do
2967c478bd9Sstevel@tonic-gate 		 * anything it needs to whilst we still have filesystems
2977c478bd9Sstevel@tonic-gate 		 * mounted, like loading any modules necessary for later
2987c478bd9Sstevel@tonic-gate 		 * performing the actual poweroff.
2997c478bd9Sstevel@tonic-gate 		 */
3007c478bd9Sstevel@tonic-gate 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
3017c478bd9Sstevel@tonic-gate 			buf = i_convert_boot_device_name(mdep, NULL, &buflen);
3027c478bd9Sstevel@tonic-gate 			mdpreboot(cmd, fcn, buf);
3037c478bd9Sstevel@tonic-gate 		} else
3047c478bd9Sstevel@tonic-gate 			mdpreboot(cmd, fcn, mdep);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 		/*
3077c478bd9Sstevel@tonic-gate 		 * Allow fsflush to finish running and then prevent it
3087c478bd9Sstevel@tonic-gate 		 * from ever running again so that vfs_unmountall() and
3097c478bd9Sstevel@tonic-gate 		 * vfs_syncall() can acquire the vfs locks they need.
3107c478bd9Sstevel@tonic-gate 		 */
3117c478bd9Sstevel@tonic-gate 		sema_p(&fsflush_sema);
3127e12ceb3SToomas Soome 		(void) callb_execute_class(CB_CL_UADMIN_PRE_VFS, 0);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		vfs_unmountall();
3157c478bd9Sstevel@tonic-gate 		(void) VFS_MOUNTROOT(rootvfs, ROOT_UNMOUNT);
3167c478bd9Sstevel@tonic-gate 		vfs_syncall();
3177c478bd9Sstevel@tonic-gate 
318a288e5a9SJoshua M. Clulow 		/*
319a288e5a9SJoshua M. Clulow 		 * Check for (and unregister) any DDI periodic handlers that
320a288e5a9SJoshua M. Clulow 		 * still exist, as they most likely constitute resource leaks:
321a288e5a9SJoshua M. Clulow 		 */
322a288e5a9SJoshua M. Clulow 		ddi_periodic_fini();
323a288e5a9SJoshua M. Clulow 
3247c478bd9Sstevel@tonic-gate 		dump_ereports();
3257c478bd9Sstevel@tonic-gate 		dump_messages();
3267c478bd9Sstevel@tonic-gate 
327edc40228Sachartre 		invoke_cb = B_TRUE;
3287c478bd9Sstevel@tonic-gate 	}
329a9f62b1aSToomas Soome 	/* FALLTHROUGH */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	case A_REBOOT:
3327c478bd9Sstevel@tonic-gate 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
3337c478bd9Sstevel@tonic-gate 			buf = i_convert_boot_device_name(mdep, NULL, &buflen);
334edc40228Sachartre 			mdboot(cmd, fcn, buf, invoke_cb);
3357c478bd9Sstevel@tonic-gate 		} else
336edc40228Sachartre 			mdboot(cmd, fcn, mdep, invoke_cb);
3377c478bd9Sstevel@tonic-gate 		/* no return expected */
3387c478bd9Sstevel@tonic-gate 		break;
3397c478bd9Sstevel@tonic-gate 
340753a6d45SSherry Moore 	case A_CONFIG:
341753a6d45SSherry Moore 		switch (fcn) {
342753a6d45SSherry Moore 		case AD_UPDATE_BOOT_CONFIG:
343753a6d45SSherry Moore #ifndef	__sparc
344753a6d45SSherry Moore 		{
345753a6d45SSherry Moore 			extern void fastboot_update_config(const char *);
346753a6d45SSherry Moore 
347753a6d45SSherry Moore 			fastboot_update_config(mdep);
348753a6d45SSherry Moore 		}
349753a6d45SSherry Moore #endif
350753a6d45SSherry Moore 
351753a6d45SSherry Moore 			break;
352753a6d45SSherry Moore 		}
353753a6d45SSherry Moore 		/* Let other threads enter the shutdown path now */
354753a6d45SSherry Moore 		mutex_enter(&ualock);
355753a6d45SSherry Moore 		ua_shutdown_thread = NULL;
356753a6d45SSherry Moore 		cv_signal(&uacond);
357753a6d45SSherry Moore 		mutex_exit(&ualock);
358753a6d45SSherry Moore 		break;
359753a6d45SSherry Moore 
3607c478bd9Sstevel@tonic-gate 	case A_REMOUNT:
3617c478bd9Sstevel@tonic-gate 		(void) VFS_MOUNTROOT(rootvfs, ROOT_REMOUNT);
362ed0ee1acScg209009 		/* Let other threads enter the shutdown path now */
363ed0ee1acScg209009 		mutex_enter(&ualock);
364ed0ee1acScg209009 		ua_shutdown_thread = NULL;
365ed0ee1acScg209009 		cv_signal(&uacond);
366ed0ee1acScg209009 		mutex_exit(&ualock);
3677c478bd9Sstevel@tonic-gate 		break;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	case A_FREEZE:
3707c478bd9Sstevel@tonic-gate 	{
3712df1fe9cSrandyf 		/*
3722df1fe9cSrandyf 		 * This is the entrypoint for all suspend/resume actions.
3732df1fe9cSrandyf 		 */
3742df1fe9cSrandyf 		extern int cpr(int, void *);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		if (modload("misc", "cpr") == -1)
3777c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
3782df1fe9cSrandyf 		/* Let the CPR module decide what to do with mdep */
3792df1fe9cSrandyf 		error = cpr(fcn, mdep);
3807c478bd9Sstevel@tonic-gate 		break;
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	case A_FTRACE:
3847c478bd9Sstevel@tonic-gate 	{
3857c478bd9Sstevel@tonic-gate 		switch (fcn) {
3867c478bd9Sstevel@tonic-gate 		case AD_FTRACE_START:
3877c478bd9Sstevel@tonic-gate 			(void) FTRACE_START();
3887c478bd9Sstevel@tonic-gate 			break;
3897c478bd9Sstevel@tonic-gate 		case AD_FTRACE_STOP:
3907c478bd9Sstevel@tonic-gate 			(void) FTRACE_STOP();
3917c478bd9Sstevel@tonic-gate 			break;
3927c478bd9Sstevel@tonic-gate 		default:
3937c478bd9Sstevel@tonic-gate 			error = EINVAL;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 		break;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	case A_DUMP:
3997c478bd9Sstevel@tonic-gate 	{
4007c478bd9Sstevel@tonic-gate 		if (fcn == AD_NOSYNC) {
4017c478bd9Sstevel@tonic-gate 			in_sync = 1;
4027c478bd9Sstevel@tonic-gate 			break;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		panic_bootfcn = fcn;
4067c478bd9Sstevel@tonic-gate 		panic_forced = 1;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		if ((mdep != NULL) && (*(char *)mdep == '/')) {
4097c478bd9Sstevel@tonic-gate 			panic_bootstr = i_convert_boot_device_name(mdep,
4107c478bd9Sstevel@tonic-gate 			    NULL, &buflen);
4117c478bd9Sstevel@tonic-gate 		} else
4127c478bd9Sstevel@tonic-gate 			panic_bootstr = mdep;
4137c478bd9Sstevel@tonic-gate 
414753a6d45SSherry Moore #ifndef	__sparc
415c90a5fbeSSherry Moore 		extern void fastboot_update_and_load(int, char *);
416753a6d45SSherry Moore 
417c90a5fbeSSherry Moore 		fastboot_update_and_load(fcn, mdep);
418753a6d45SSherry Moore #endif
419753a6d45SSherry Moore 
4207c478bd9Sstevel@tonic-gate 		panic("forced crash dump initiated at user request");
4217c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
42410e6dadfSbrendan 	case A_SDTTEST:
42510e6dadfSbrendan 	{
426*bb65110fSLuqman Aden 		sdt_test_args();
427*bb65110fSLuqman Aden 		sdt_test_args_tail_call(1, 2, 3, 4, 5, 6, 7);
42810e6dadfSbrendan 		break;
42910e6dadfSbrendan 	}
43010e6dadfSbrendan 
4317c478bd9Sstevel@tonic-gate 	default:
4327c478bd9Sstevel@tonic-gate 		error = EINVAL;
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	return (error);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate int
uadmin(int cmd,int fcn,uintptr_t mdep)4397c478bd9Sstevel@tonic-gate uadmin(int cmd, int fcn, uintptr_t mdep)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	int error = 0, rv = 0;
4427c478bd9Sstevel@tonic-gate 	size_t nbytes = 0;
4437c478bd9Sstevel@tonic-gate 	cred_t *credp = CRED();
4443f2f09c1Sdp 	char *bootargs = NULL;
44519397407SSherry Moore 	int reset_status = 0;
44619397407SSherry Moore 
44719397407SSherry Moore 	if (cmd == A_SHUTDOWN && fcn == AD_FASTREBOOT_DRYRUN) {
44819397407SSherry Moore 		ddi_walk_devs(ddi_root_node(), check_driver_quiesce,
44919397407SSherry Moore 		    &reset_status);
45019397407SSherry Moore 		if (reset_status != 0)
45119397407SSherry Moore 			return (EIO);
45219397407SSherry Moore 		else
45319397407SSherry Moore 			return (0);
45419397407SSherry Moore 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	/*
4577c478bd9Sstevel@tonic-gate 	 * The swapctl system call doesn't have its own entry point: it uses
4587c478bd9Sstevel@tonic-gate 	 * uadmin as a wrapper so we just call it directly from here.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	if (cmd == A_SWAPCTL) {
4617c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE)
4627c478bd9Sstevel@tonic-gate 			error = swapctl(fcn, (void *)mdep, &rv);
4637c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4647c478bd9Sstevel@tonic-gate 		else
4657c478bd9Sstevel@tonic-gate 			error = swapctl32(fcn, (void *)mdep, &rv);
4667c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
4677c478bd9Sstevel@tonic-gate 		return (error ? set_errno(error) : rv);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4713f2f09c1Sdp 	 * Certain subcommands intepret a non-NULL mdep value as a pointer to
4723f2f09c1Sdp 	 * a boot string.  We pull that in as bootargs, if applicable.
4737c478bd9Sstevel@tonic-gate 	 */
4747e12ceb3SToomas Soome 	if (mdep != (uintptr_t)NULL &&
4752df1fe9cSrandyf 	    (cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_DUMP ||
476753a6d45SSherry Moore 	    cmd == A_FREEZE || cmd == A_CONFIG)) {
4773f2f09c1Sdp 		bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP);
4783f2f09c1Sdp 		if ((error = copyinstr((const char *)mdep, bootargs,
4793f2f09c1Sdp 		    BOOTARGS_MAX, &nbytes)) != 0) {
4803f2f09c1Sdp 			kmem_free(bootargs, BOOTARGS_MAX);
4813f2f09c1Sdp 			return (set_errno(error));
4823f2f09c1Sdp 		}
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4863f2f09c1Sdp 	 * Invoke the appropriate kadmin() routine.
4877c478bd9Sstevel@tonic-gate 	 */
4883f2f09c1Sdp 	if (getzoneid() != GLOBAL_ZONEID)
4893f2f09c1Sdp 		error = zone_kadmin(cmd, fcn, bootargs, credp);
4903f2f09c1Sdp 	else
4913f2f09c1Sdp 		error = kadmin(cmd, fcn, bootargs, credp);
4927c478bd9Sstevel@tonic-gate 
4933f2f09c1Sdp 	if (bootargs != NULL)
4943f2f09c1Sdp 		kmem_free(bootargs, BOOTARGS_MAX);
4953f2f09c1Sdp 	return (error ? set_errno(error) : 0);
4967c478bd9Sstevel@tonic-gate }
497