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
5f18fc278Sjohnlev  * Common Development and Distribution License (the "License").
6f18fc278Sjohnlev  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*80148899SSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Driver-side functions for loading and unloading dmods.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
327c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h>
337c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/ctf_api.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmdb.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <kmdb/kctl/kctl.h>
397c478bd9Sstevel@tonic-gate #include <kmdb/kctl/kctl_wr.h>
407c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_wr_impl.h>
417c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
427c478bd9Sstevel@tonic-gate #include <mdb/mdb_errno.h>
437c478bd9Sstevel@tonic-gate 
4401f19855Scth struct modctl		*kdi_dmods;
4501f19855Scth 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * When a load is attempted, a check is first made of the modules on the
487c478bd9Sstevel@tonic-gate  * kctl_dmods list.  If a module is found, the load will not proceed.
497c478bd9Sstevel@tonic-gate  * kctl_dmods_lock must be held while traversing kctl_dmods, and while adding
507c478bd9Sstevel@tonic-gate  * to and subtracting from it.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate static struct modctl	kctl_dmods;
537c478bd9Sstevel@tonic-gate static kmutex_t		kctl_dmods_lock;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static kmdb_wr_path_t	*kctl_dmod_path;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Used to track outstanding driver-initiated load notifications.  These
597c478bd9Sstevel@tonic-gate  * notifications have been allocated by driver, and thus must be freed by the
607c478bd9Sstevel@tonic-gate  * driver in the event of an emergency unload.  If we don't free them free
617c478bd9Sstevel@tonic-gate  * them ourselves, they'll leak.  Granted, the world is probably melting down
627c478bd9Sstevel@tonic-gate  * at that point, but there's no reason why we shouldn't tidy up the deck
637c478bd9Sstevel@tonic-gate  * chairs before we go.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate static kmdb_wr_load_t	*kctl_dmod_loads;
667c478bd9Sstevel@tonic-gate static kmutex_t 	kctl_dmod_loads_lock;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static int
kctl_find_module(char * modname,char * fullname,size_t fullnamelen)697c478bd9Sstevel@tonic-gate kctl_find_module(char *modname, char *fullname, size_t fullnamelen)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	intptr_t fd;
727c478bd9Sstevel@tonic-gate 	int i;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	/* If they gave us an absolute path, we don't need to search */
757c478bd9Sstevel@tonic-gate 	if (modname[0] == '/') {
767c478bd9Sstevel@tonic-gate 		if (strlen(modname) + 1 > fullnamelen) {
77ae115bc7Smrj 			cmn_err(CE_WARN, "Can't load dmod %s - name too long",
787c478bd9Sstevel@tonic-gate 			    modname);
797c478bd9Sstevel@tonic-gate 			return (0);
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		if ((fd = kobj_open(modname)) == -1)
837c478bd9Sstevel@tonic-gate 			return (0);
847c478bd9Sstevel@tonic-gate 		kobj_close(fd);
857c478bd9Sstevel@tonic-gate 
86*80148899SSurya Prakki 		(void) strcpy(fullname, modname);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 		return (1);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	for (i = 0; kctl_dmod_path->dpth_path[i] != NULL; i++) {
927c478bd9Sstevel@tonic-gate 		const char *path = kctl_dmod_path->dpth_path[i];
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 		if (strlen(path) + 1 + strlen(modname) + 1 > fullnamelen) {
957c478bd9Sstevel@tonic-gate 			kctl_dprintf("Can't load dmod from %s/%s - "
967c478bd9Sstevel@tonic-gate 			    "name too long", path, modname);
977c478bd9Sstevel@tonic-gate 			continue;
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		(void) snprintf(fullname, fullnamelen, "%s/%s", path, modname);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 		if ((fd = kobj_open(fullname)) == -1)
1037c478bd9Sstevel@tonic-gate 			continue;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		kobj_close(fd);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		kctl_dprintf("kobj_open %s found", fullname);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		/* Found it */
1107c478bd9Sstevel@tonic-gate 		return (1);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/* No luck */
1147c478bd9Sstevel@tonic-gate 	return (0);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static void
kctl_dlr_free(kmdb_wr_load_t * dlr)1187c478bd9Sstevel@tonic-gate kctl_dlr_free(kmdb_wr_load_t *dlr)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	if (dlr->dlr_node.wn_flags & WNFLAGS_NOFREE)
1217c478bd9Sstevel@tonic-gate 		return;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	kctl_strfree(dlr->dlr_fname);
1247c478bd9Sstevel@tonic-gate 	kmem_free(dlr, sizeof (kmdb_wr_load_t));
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate int
kctl_dmod_load(kmdb_wr_load_t * dlr)1287c478bd9Sstevel@tonic-gate kctl_dmod_load(kmdb_wr_load_t *dlr)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	struct modctl *modp;
1317c478bd9Sstevel@tonic-gate 	char modpath[MAXPATHLEN];
1327c478bd9Sstevel@tonic-gate 	const char *modname = kctl_basename(dlr->dlr_fname);
1337c478bd9Sstevel@tonic-gate 	int rc;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	mutex_enter(&kctl_dmods_lock);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/* Have we already loaded this dmod? */
1387c478bd9Sstevel@tonic-gate 	for (modp = kctl_dmods.mod_next; modp != &kctl_dmods;
1397c478bd9Sstevel@tonic-gate 	    modp = modp->mod_next) {
1407c478bd9Sstevel@tonic-gate 		if (strcmp(modname, modp->mod_modname) == 0) {
1417c478bd9Sstevel@tonic-gate 			mutex_exit(&kctl_dmods_lock);
1427c478bd9Sstevel@tonic-gate 			dlr->dlr_errno = EEXIST;
1437c478bd9Sstevel@tonic-gate 			return (-1);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * If we find something that looks like a dmod, create a modctl for it,
1497c478bd9Sstevel@tonic-gate 	 * and add said modctl to our dmods list.  This will allow us to drop
1507c478bd9Sstevel@tonic-gate 	 * the dmods lock, while still preventing duplicate loads.  If we aren't
1517c478bd9Sstevel@tonic-gate 	 * able to actually load the dmod, we can always remove the modctl
1527c478bd9Sstevel@tonic-gate 	 * later.
1537c478bd9Sstevel@tonic-gate 	 */
1547c478bd9Sstevel@tonic-gate 	if (!kctl_find_module(dlr->dlr_fname, modpath, sizeof (modpath))) {
1557c478bd9Sstevel@tonic-gate 		mutex_exit(&kctl_dmods_lock);
1567c478bd9Sstevel@tonic-gate 		dlr->dlr_errno = ENOENT;
1577c478bd9Sstevel@tonic-gate 		return (-1);
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	modp = kobj_zalloc(sizeof (struct modctl), KM_SLEEP);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	modp->mod_filename = kctl_strdup(modpath);
1637c478bd9Sstevel@tonic-gate 	modp->mod_modname = kctl_basename(modp->mod_filename);
1647c478bd9Sstevel@tonic-gate 	modp->mod_busy = 1;
1657c478bd9Sstevel@tonic-gate 	modp->mod_loadflags |= MOD_NOAUTOUNLOAD | MOD_NONOTIFY;
1667c478bd9Sstevel@tonic-gate 	modp->mod_next = &kctl_dmods;
1677c478bd9Sstevel@tonic-gate 	modp->mod_prev = kctl_dmods.mod_prev;
1687c478bd9Sstevel@tonic-gate 	modp->mod_prev->mod_next = modp;
1697c478bd9Sstevel@tonic-gate 	kctl_dmods.mod_prev = modp;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	mutex_exit(&kctl_dmods_lock);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (kctl.kctl_boot_ops == NULL)
1747c478bd9Sstevel@tonic-gate 		rc = kobj_load_module(modp, 0);
1757c478bd9Sstevel@tonic-gate 	else
1767c478bd9Sstevel@tonic-gate 		rc = kobj_load_primary_module(modp);
1777c478bd9Sstevel@tonic-gate 
1786c7d2bacSdh155122 	if (rc != 0) {
1797c478bd9Sstevel@tonic-gate 		kctl_warn("failed to load dmod %s", modp->mod_modname);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		if (kctl.kctl_boot_ops == NULL)
1827c478bd9Sstevel@tonic-gate 			mod_release_requisites(modp);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		mutex_enter(&kctl_dmods_lock);
1857c478bd9Sstevel@tonic-gate 		modp->mod_next->mod_prev = modp->mod_prev;
1867c478bd9Sstevel@tonic-gate 		modp->mod_prev->mod_next = modp->mod_next;
1877c478bd9Sstevel@tonic-gate 		mutex_exit(&kctl_dmods_lock);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		kctl_strfree(modp->mod_filename);
1907c478bd9Sstevel@tonic-gate 		kobj_free(modp, sizeof (struct modctl));
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		dlr->dlr_errno = EMDB_NOMOD;
1937c478bd9Sstevel@tonic-gate 		return (-1);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * It worked!  If the module has any CTF data, decompress it, and make a
1987c478bd9Sstevel@tonic-gate 	 * note of the load.
1997c478bd9Sstevel@tonic-gate 	 */
2007c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
2017c478bd9Sstevel@tonic-gate 	if ((rc = kctl_mod_decompress(modp)) != 0) {
2027c478bd9Sstevel@tonic-gate 		kctl_warn("failed to decompress CTF data for dmod %s: %s",
2037c478bd9Sstevel@tonic-gate 		    modpath, ctf_errmsg(rc));
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	kctl_dprintf("loaded dmod %s at %p", modpath, modp);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	modp->mod_ref = 1;
2107c478bd9Sstevel@tonic-gate 	modp->mod_loaded = 1;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	dlr->dlr_modctl = modp;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return (0);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * Driver-initiated loads.  Load the module and announce it to the debugger.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate void
kctl_dmod_autoload(const char * fname)2217c478bd9Sstevel@tonic-gate kctl_dmod_autoload(const char *fname)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	kmdb_wr_load_t *dlr;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	dlr = kobj_zalloc(sizeof (kmdb_wr_load_t), KM_SLEEP);
2267c478bd9Sstevel@tonic-gate 	dlr->dlr_node.wn_task = WNTASK_DMOD_LOAD;
2277c478bd9Sstevel@tonic-gate 	dlr->dlr_fname = kctl_strdup(fname);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * If we're loading at boot, the kmdb_wr_load_t will have been
2317c478bd9Sstevel@tonic-gate 	 * "allocated" by krtld, and will thus not be under the control of
2327c478bd9Sstevel@tonic-gate 	 * kmem.  We need to ensure that we don't attempt to free it when
2337c478bd9Sstevel@tonic-gate 	 * we get it back from the debugger.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	if (kctl.kctl_boot_ops != NULL)
2367c478bd9Sstevel@tonic-gate 		dlr->dlr_node.wn_flags |= WNFLAGS_NOFREE;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (kctl_dmod_load(dlr) < 0) {
2397c478bd9Sstevel@tonic-gate 		kctl_dlr_free(dlr);
2407c478bd9Sstevel@tonic-gate 		return;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * Add to the list of open driver-initiated loads.  We need to track
2457c478bd9Sstevel@tonic-gate 	 * these so we can free them (and thus avoid leaks) in the event that
2467c478bd9Sstevel@tonic-gate 	 * the debugger needs to be blown away before it can return them.
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	mutex_enter(&kctl_dmod_loads_lock);
2497c478bd9Sstevel@tonic-gate 	dlr->dlr_next = kctl_dmod_loads;
2507c478bd9Sstevel@tonic-gate 	if (kctl_dmod_loads != NULL)
2517c478bd9Sstevel@tonic-gate 		kctl_dmod_loads->dlr_prev = dlr;
2527c478bd9Sstevel@tonic-gate 	kctl_dmod_loads = dlr;
2537c478bd9Sstevel@tonic-gate 	mutex_exit(&kctl_dmod_loads_lock);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	kmdb_wr_debugger_notify(dlr);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate void
kctl_dmod_load_all(void)2597c478bd9Sstevel@tonic-gate kctl_dmod_load_all(void)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * The standard list of modules isn't populated until the tail end of
2637c478bd9Sstevel@tonic-gate 	 * kobj_init().  Prior to that point, the only available list is that of
2647c478bd9Sstevel@tonic-gate 	 * primaries.  We'll use that if the normal list isn't ready yet.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	if (modules.mod_mp == NULL) {
2677c478bd9Sstevel@tonic-gate 		/* modules hasn't been initialized yet -- use primaries */
2687c478bd9Sstevel@tonic-gate 		struct modctl_list *ml;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		for (ml = kobj_linkmaps[KOBJ_LM_PRIMARY]; ml != NULL;
2717c478bd9Sstevel@tonic-gate 		    ml = ml->modl_next)
2727c478bd9Sstevel@tonic-gate 			kctl_dmod_autoload(ml->modl_modp->mod_modname);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	} else {
2757c478bd9Sstevel@tonic-gate 		struct modctl *modp = &modules;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		do {
2787c478bd9Sstevel@tonic-gate 			if (modp->mod_mp != NULL)
2797c478bd9Sstevel@tonic-gate 				kctl_dmod_autoload(modp->mod_modname);
2807c478bd9Sstevel@tonic-gate 		} while ((modp = modp->mod_next) != &modules);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate void
kctl_dmod_load_ack(kmdb_wr_load_t * dlr)2857c478bd9Sstevel@tonic-gate kctl_dmod_load_ack(kmdb_wr_load_t *dlr)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	/* Remove from the list of open driver-initiated requests */
2887c478bd9Sstevel@tonic-gate 	mutex_enter(&kctl_dmod_loads_lock);
2897c478bd9Sstevel@tonic-gate 	if (dlr->dlr_prev == NULL)
2907c478bd9Sstevel@tonic-gate 		kctl_dmod_loads = dlr->dlr_next;
2917c478bd9Sstevel@tonic-gate 	else
2927c478bd9Sstevel@tonic-gate 		dlr->dlr_prev->dlr_next = dlr->dlr_next;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (dlr->dlr_next != NULL)
2957c478bd9Sstevel@tonic-gate 		dlr->dlr_next->dlr_prev = dlr->dlr_prev;
2967c478bd9Sstevel@tonic-gate 	mutex_exit(&kctl_dmod_loads_lock);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	kctl_dlr_free(dlr);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate static int
kctl_dmod_unload_common(struct modctl * modp)3027c478bd9Sstevel@tonic-gate kctl_dmod_unload_common(struct modctl *modp)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	struct modctl *m;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	kctl_dprintf("unloading dmod %s", modp->mod_modname);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	mutex_enter(&kctl_dmods_lock);
3097c478bd9Sstevel@tonic-gate 	for (m = kctl_dmods.mod_next; m != &kctl_dmods; m = m->mod_next) {
3107c478bd9Sstevel@tonic-gate 		if (m == modp)
3117c478bd9Sstevel@tonic-gate 			break;
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	mutex_exit(&kctl_dmods_lock);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (m != modp)
3167c478bd9Sstevel@tonic-gate 		return (ENOENT);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/* Found it */
3197c478bd9Sstevel@tonic-gate 	modp->mod_ref = 0;
3207c478bd9Sstevel@tonic-gate 	modp->mod_loaded = 0;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	kobj_unload_module(modp);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	mod_release_requisites(modp);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/* Remove it from our dmods list */
3277c478bd9Sstevel@tonic-gate 	mutex_enter(&kctl_dmods_lock);
3287c478bd9Sstevel@tonic-gate 	modp->mod_next->mod_prev = modp->mod_prev;
3297c478bd9Sstevel@tonic-gate 	modp->mod_prev->mod_next = modp->mod_next;
3307c478bd9Sstevel@tonic-gate 	mutex_exit(&kctl_dmods_lock);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	kctl_strfree(modp->mod_filename);
3337c478bd9Sstevel@tonic-gate 	kmem_free(modp, sizeof (struct modctl));
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	return (0);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate void
kctl_dmod_unload(kmdb_wr_unload_t * dur)3397c478bd9Sstevel@tonic-gate kctl_dmod_unload(kmdb_wr_unload_t *dur)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	int rc;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if ((rc = kctl_dmod_unload_common(dur->dur_modctl)) != 0) {
344ae115bc7Smrj 		cmn_err(CE_WARN, "unexpected dmod unload failure: %d", rc);
3457c478bd9Sstevel@tonic-gate 		dur->dur_errno = rc;
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * This will be called during shutdown.  The debugger has been stopped, we're
3517c478bd9Sstevel@tonic-gate  * off the module notification list, and we've already processed everything in
3527c478bd9Sstevel@tonic-gate  * the driver's work queue.  We should have received (and processed) unload
3537c478bd9Sstevel@tonic-gate  * requests for each of the dmods we've loaded.  To be safe, however, we'll
3547c478bd9Sstevel@tonic-gate  * double-check.
3557c478bd9Sstevel@tonic-gate  *
3567c478bd9Sstevel@tonic-gate  * If we're doing an emergency shutdown, there may be outstanding
3577c478bd9Sstevel@tonic-gate  * driver-initiated messages that haven't been returned to us.  The debugger is
3587c478bd9Sstevel@tonic-gate  * dead, so it's not going to be returning them.  We'll leak them unless we
3597c478bd9Sstevel@tonic-gate  * find and free them ourselves.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate void
kctl_dmod_unload_all(void)3627c478bd9Sstevel@tonic-gate kctl_dmod_unload_all(void)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	kmdb_wr_load_t *dlr;
3657c478bd9Sstevel@tonic-gate 	struct modctl *modp;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	while ((modp = kctl_dmods.mod_next) != &kctl_dmods)
3687c478bd9Sstevel@tonic-gate 		(void) kctl_dmod_unload_common(modp);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	while ((dlr = kctl_dmod_loads) != NULL) {
3717c478bd9Sstevel@tonic-gate 		kctl_dmod_loads = dlr->dlr_next;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		kctl_dprintf("freed orphan load notification for %s",
3747c478bd9Sstevel@tonic-gate 		    dlr->dlr_fname);
3757c478bd9Sstevel@tonic-gate 		kctl_dlr_free(dlr);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate kmdb_wr_path_t *
kctl_dmod_path_set(kmdb_wr_path_t * pth)3807c478bd9Sstevel@tonic-gate kctl_dmod_path_set(kmdb_wr_path_t *pth)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	kmdb_wr_path_t *opth;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	if (kctl.kctl_flags & KMDB_F_DRV_DEBUG) {
385f18fc278Sjohnlev 		if (pth != NULL) {
3867c478bd9Sstevel@tonic-gate 			int i;
3877c478bd9Sstevel@tonic-gate 			kctl_dprintf("changing dmod path to: %p", pth);
3887c478bd9Sstevel@tonic-gate 			for (i = 0; pth->dpth_path[i] != NULL; i++)
3897c478bd9Sstevel@tonic-gate 				kctl_dprintf(" %s", pth->dpth_path[i]);
390f18fc278Sjohnlev 		} else {
391f18fc278Sjohnlev 			kctl_dprintf("changing dmod path to NULL");
392f18fc278Sjohnlev 		}
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	opth = kctl_dmod_path;
3967c478bd9Sstevel@tonic-gate 	kctl_dmod_path = pth;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	return (opth);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate void
kctl_dmod_path_reset(void)4027c478bd9Sstevel@tonic-gate kctl_dmod_path_reset(void)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	kmdb_wr_path_t *pth;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if ((pth = kctl_dmod_path_set(NULL)) != NULL) {
4077c478bd9Sstevel@tonic-gate 		WR_ACK(pth);
4087c478bd9Sstevel@tonic-gate 		kmdb_wr_debugger_notify(pth);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate void
kctl_dmod_sync(void)4137c478bd9Sstevel@tonic-gate kctl_dmod_sync(void)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	struct modctl *modp;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * kobj_sync() has no visibility into our dmods, so we need to
4197c478bd9Sstevel@tonic-gate 	 * explicitly tell krtld to export the portions of our dmods that were
4207c478bd9Sstevel@tonic-gate 	 * allocated using boot scratch memory.
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	for (modp = kctl_dmods.mod_next; modp != &kctl_dmods;
4237c478bd9Sstevel@tonic-gate 	    modp = modp->mod_next)
4247c478bd9Sstevel@tonic-gate 		kobj_export_module(modp->mod_mp);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate void
kctl_dmod_init(void)4287c478bd9Sstevel@tonic-gate kctl_dmod_init(void)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	mutex_init(&kctl_dmod_loads_lock, NULL, MUTEX_DRIVER, NULL);
4317c478bd9Sstevel@tonic-gate 	mutex_init(&kctl_dmods_lock, NULL, MUTEX_DRIVER, NULL);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	bzero(&kctl_dmods, sizeof (struct modctl));
4347c478bd9Sstevel@tonic-gate 	kctl_dmods.mod_next = kctl_dmods.mod_prev = &kctl_dmods;
43501f19855Scth 	kdi_dmods = &kctl_dmods;
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate void
kctl_dmod_fini(void)4397c478bd9Sstevel@tonic-gate kctl_dmod_fini(void)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	mutex_destroy(&kctl_dmods_lock);
4427c478bd9Sstevel@tonic-gate 	mutex_destroy(&kctl_dmod_loads_lock);
44301f19855Scth 	kdi_dmods = NULL;
4447c478bd9Sstevel@tonic-gate }
445