xref: /onnv-gate/usr/src/cmd/mdb/common/kmdb/kctl/kctl_dmod.c (revision 11053:f33a1c7f3155)
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
51530Sjohnlev  * Common Development and Distribution License (the "License").
61530Sjohnlev  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11053SSurya.Prakki@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Driver-side functions for loading and unloading dmods.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/kobj.h>
320Sstevel@tonic-gate #include <sys/kobj_impl.h>
330Sstevel@tonic-gate #include <sys/modctl.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/ctf_api.h>
360Sstevel@tonic-gate #include <sys/kmdb.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <kmdb/kctl/kctl.h>
390Sstevel@tonic-gate #include <kmdb/kctl/kctl_wr.h>
400Sstevel@tonic-gate #include <kmdb/kmdb_wr_impl.h>
410Sstevel@tonic-gate #include <kmdb/kmdb_kdi.h>
420Sstevel@tonic-gate #include <mdb/mdb_errno.h>
430Sstevel@tonic-gate 
441226Scth struct modctl		*kdi_dmods;
451226Scth 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * When a load is attempted, a check is first made of the modules on the
480Sstevel@tonic-gate  * kctl_dmods list.  If a module is found, the load will not proceed.
490Sstevel@tonic-gate  * kctl_dmods_lock must be held while traversing kctl_dmods, and while adding
500Sstevel@tonic-gate  * to and subtracting from it.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static struct modctl	kctl_dmods;
530Sstevel@tonic-gate static kmutex_t		kctl_dmods_lock;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static kmdb_wr_path_t	*kctl_dmod_path;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Used to track outstanding driver-initiated load notifications.  These
590Sstevel@tonic-gate  * notifications have been allocated by driver, and thus must be freed by the
600Sstevel@tonic-gate  * driver in the event of an emergency unload.  If we don't free them free
610Sstevel@tonic-gate  * them ourselves, they'll leak.  Granted, the world is probably melting down
620Sstevel@tonic-gate  * at that point, but there's no reason why we shouldn't tidy up the deck
630Sstevel@tonic-gate  * chairs before we go.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static kmdb_wr_load_t	*kctl_dmod_loads;
660Sstevel@tonic-gate static kmutex_t 	kctl_dmod_loads_lock;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static int
kctl_find_module(char * modname,char * fullname,size_t fullnamelen)690Sstevel@tonic-gate kctl_find_module(char *modname, char *fullname, size_t fullnamelen)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	intptr_t fd;
720Sstevel@tonic-gate 	int i;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	/* If they gave us an absolute path, we don't need to search */
750Sstevel@tonic-gate 	if (modname[0] == '/') {
760Sstevel@tonic-gate 		if (strlen(modname) + 1 > fullnamelen) {
773446Smrj 			cmn_err(CE_WARN, "Can't load dmod %s - name too long",
780Sstevel@tonic-gate 			    modname);
790Sstevel@tonic-gate 			return (0);
800Sstevel@tonic-gate 		}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 		if ((fd = kobj_open(modname)) == -1)
830Sstevel@tonic-gate 			return (0);
840Sstevel@tonic-gate 		kobj_close(fd);
850Sstevel@tonic-gate 
86*11053SSurya.Prakki@Sun.COM 		(void) strcpy(fullname, modname);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 		return (1);
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	for (i = 0; kctl_dmod_path->dpth_path[i] != NULL; i++) {
920Sstevel@tonic-gate 		const char *path = kctl_dmod_path->dpth_path[i];
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 		if (strlen(path) + 1 + strlen(modname) + 1 > fullnamelen) {
950Sstevel@tonic-gate 			kctl_dprintf("Can't load dmod from %s/%s - "
960Sstevel@tonic-gate 			    "name too long", path, modname);
970Sstevel@tonic-gate 			continue;
980Sstevel@tonic-gate 		}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 		(void) snprintf(fullname, fullnamelen, "%s/%s", path, modname);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		if ((fd = kobj_open(fullname)) == -1)
1030Sstevel@tonic-gate 			continue;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		kobj_close(fd);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 		kctl_dprintf("kobj_open %s found", fullname);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		/* Found it */
1100Sstevel@tonic-gate 		return (1);
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/* No luck */
1140Sstevel@tonic-gate 	return (0);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static void
kctl_dlr_free(kmdb_wr_load_t * dlr)1180Sstevel@tonic-gate kctl_dlr_free(kmdb_wr_load_t *dlr)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	if (dlr->dlr_node.wn_flags & WNFLAGS_NOFREE)
1210Sstevel@tonic-gate 		return;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	kctl_strfree(dlr->dlr_fname);
1240Sstevel@tonic-gate 	kmem_free(dlr, sizeof (kmdb_wr_load_t));
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate int
kctl_dmod_load(kmdb_wr_load_t * dlr)1280Sstevel@tonic-gate kctl_dmod_load(kmdb_wr_load_t *dlr)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	struct modctl *modp;
1310Sstevel@tonic-gate 	char modpath[MAXPATHLEN];
1320Sstevel@tonic-gate 	const char *modname = kctl_basename(dlr->dlr_fname);
1330Sstevel@tonic-gate 	int rc;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	mutex_enter(&kctl_dmods_lock);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/* Have we already loaded this dmod? */
1380Sstevel@tonic-gate 	for (modp = kctl_dmods.mod_next; modp != &kctl_dmods;
1390Sstevel@tonic-gate 	    modp = modp->mod_next) {
1400Sstevel@tonic-gate 		if (strcmp(modname, modp->mod_modname) == 0) {
1410Sstevel@tonic-gate 			mutex_exit(&kctl_dmods_lock);
1420Sstevel@tonic-gate 			dlr->dlr_errno = EEXIST;
1430Sstevel@tonic-gate 			return (-1);
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/*
1480Sstevel@tonic-gate 	 * If we find something that looks like a dmod, create a modctl for it,
1490Sstevel@tonic-gate 	 * and add said modctl to our dmods list.  This will allow us to drop
1500Sstevel@tonic-gate 	 * the dmods lock, while still preventing duplicate loads.  If we aren't
1510Sstevel@tonic-gate 	 * able to actually load the dmod, we can always remove the modctl
1520Sstevel@tonic-gate 	 * later.
1530Sstevel@tonic-gate 	 */
1540Sstevel@tonic-gate 	if (!kctl_find_module(dlr->dlr_fname, modpath, sizeof (modpath))) {
1550Sstevel@tonic-gate 		mutex_exit(&kctl_dmods_lock);
1560Sstevel@tonic-gate 		dlr->dlr_errno = ENOENT;
1570Sstevel@tonic-gate 		return (-1);
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	modp = kobj_zalloc(sizeof (struct modctl), KM_SLEEP);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	modp->mod_filename = kctl_strdup(modpath);
1630Sstevel@tonic-gate 	modp->mod_modname = kctl_basename(modp->mod_filename);
1640Sstevel@tonic-gate 	modp->mod_busy = 1;
1650Sstevel@tonic-gate 	modp->mod_loadflags |= MOD_NOAUTOUNLOAD | MOD_NONOTIFY;
1660Sstevel@tonic-gate 	modp->mod_next = &kctl_dmods;
1670Sstevel@tonic-gate 	modp->mod_prev = kctl_dmods.mod_prev;
1680Sstevel@tonic-gate 	modp->mod_prev->mod_next = modp;
1690Sstevel@tonic-gate 	kctl_dmods.mod_prev = modp;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	mutex_exit(&kctl_dmods_lock);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	if (kctl.kctl_boot_ops == NULL)
1740Sstevel@tonic-gate 		rc = kobj_load_module(modp, 0);
1750Sstevel@tonic-gate 	else
1760Sstevel@tonic-gate 		rc = kobj_load_primary_module(modp);
1770Sstevel@tonic-gate 
1782740Sdh155122 	if (rc != 0) {
1790Sstevel@tonic-gate 		kctl_warn("failed to load dmod %s", modp->mod_modname);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		if (kctl.kctl_boot_ops == NULL)
1820Sstevel@tonic-gate 			mod_release_requisites(modp);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 		mutex_enter(&kctl_dmods_lock);
1850Sstevel@tonic-gate 		modp->mod_next->mod_prev = modp->mod_prev;
1860Sstevel@tonic-gate 		modp->mod_prev->mod_next = modp->mod_next;
1870Sstevel@tonic-gate 		mutex_exit(&kctl_dmods_lock);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		kctl_strfree(modp->mod_filename);
1900Sstevel@tonic-gate 		kobj_free(modp, sizeof (struct modctl));
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		dlr->dlr_errno = EMDB_NOMOD;
1930Sstevel@tonic-gate 		return (-1);
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * It worked!  If the module has any CTF data, decompress it, and make a
1980Sstevel@tonic-gate 	 * note of the load.
1990Sstevel@tonic-gate 	 */
2000Sstevel@tonic-gate 	mutex_enter(&mod_lock);
2010Sstevel@tonic-gate 	if ((rc = kctl_mod_decompress(modp)) != 0) {
2020Sstevel@tonic-gate 		kctl_warn("failed to decompress CTF data for dmod %s: %s",
2030Sstevel@tonic-gate 		    modpath, ctf_errmsg(rc));
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 	mutex_exit(&mod_lock);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	kctl_dprintf("loaded dmod %s at %p", modpath, modp);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	modp->mod_ref = 1;
2100Sstevel@tonic-gate 	modp->mod_loaded = 1;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	dlr->dlr_modctl = modp;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	return (0);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate  * Driver-initiated loads.  Load the module and announce it to the debugger.
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate void
kctl_dmod_autoload(const char * fname)2210Sstevel@tonic-gate kctl_dmod_autoload(const char *fname)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	kmdb_wr_load_t *dlr;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	dlr = kobj_zalloc(sizeof (kmdb_wr_load_t), KM_SLEEP);
2260Sstevel@tonic-gate 	dlr->dlr_node.wn_task = WNTASK_DMOD_LOAD;
2270Sstevel@tonic-gate 	dlr->dlr_fname = kctl_strdup(fname);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	/*
2300Sstevel@tonic-gate 	 * If we're loading at boot, the kmdb_wr_load_t will have been
2310Sstevel@tonic-gate 	 * "allocated" by krtld, and will thus not be under the control of
2320Sstevel@tonic-gate 	 * kmem.  We need to ensure that we don't attempt to free it when
2330Sstevel@tonic-gate 	 * we get it back from the debugger.
2340Sstevel@tonic-gate 	 */
2350Sstevel@tonic-gate 	if (kctl.kctl_boot_ops != NULL)
2360Sstevel@tonic-gate 		dlr->dlr_node.wn_flags |= WNFLAGS_NOFREE;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (kctl_dmod_load(dlr) < 0) {
2390Sstevel@tonic-gate 		kctl_dlr_free(dlr);
2400Sstevel@tonic-gate 		return;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	/*
2440Sstevel@tonic-gate 	 * Add to the list of open driver-initiated loads.  We need to track
2450Sstevel@tonic-gate 	 * these so we can free them (and thus avoid leaks) in the event that
2460Sstevel@tonic-gate 	 * the debugger needs to be blown away before it can return them.
2470Sstevel@tonic-gate 	 */
2480Sstevel@tonic-gate 	mutex_enter(&kctl_dmod_loads_lock);
2490Sstevel@tonic-gate 	dlr->dlr_next = kctl_dmod_loads;
2500Sstevel@tonic-gate 	if (kctl_dmod_loads != NULL)
2510Sstevel@tonic-gate 		kctl_dmod_loads->dlr_prev = dlr;
2520Sstevel@tonic-gate 	kctl_dmod_loads = dlr;
2530Sstevel@tonic-gate 	mutex_exit(&kctl_dmod_loads_lock);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	kmdb_wr_debugger_notify(dlr);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate void
kctl_dmod_load_all(void)2590Sstevel@tonic-gate kctl_dmod_load_all(void)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * The standard list of modules isn't populated until the tail end of
2630Sstevel@tonic-gate 	 * kobj_init().  Prior to that point, the only available list is that of
2640Sstevel@tonic-gate 	 * primaries.  We'll use that if the normal list isn't ready yet.
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	if (modules.mod_mp == NULL) {
2670Sstevel@tonic-gate 		/* modules hasn't been initialized yet -- use primaries */
2680Sstevel@tonic-gate 		struct modctl_list *ml;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		for (ml = kobj_linkmaps[KOBJ_LM_PRIMARY]; ml != NULL;
2710Sstevel@tonic-gate 		    ml = ml->modl_next)
2720Sstevel@tonic-gate 			kctl_dmod_autoload(ml->modl_modp->mod_modname);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	} else {
2750Sstevel@tonic-gate 		struct modctl *modp = &modules;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		do {
2780Sstevel@tonic-gate 			if (modp->mod_mp != NULL)
2790Sstevel@tonic-gate 				kctl_dmod_autoload(modp->mod_modname);
2800Sstevel@tonic-gate 		} while ((modp = modp->mod_next) != &modules);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate void
kctl_dmod_load_ack(kmdb_wr_load_t * dlr)2850Sstevel@tonic-gate kctl_dmod_load_ack(kmdb_wr_load_t *dlr)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	/* Remove from the list of open driver-initiated requests */
2880Sstevel@tonic-gate 	mutex_enter(&kctl_dmod_loads_lock);
2890Sstevel@tonic-gate 	if (dlr->dlr_prev == NULL)
2900Sstevel@tonic-gate 		kctl_dmod_loads = dlr->dlr_next;
2910Sstevel@tonic-gate 	else
2920Sstevel@tonic-gate 		dlr->dlr_prev->dlr_next = dlr->dlr_next;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if (dlr->dlr_next != NULL)
2950Sstevel@tonic-gate 		dlr->dlr_next->dlr_prev = dlr->dlr_prev;
2960Sstevel@tonic-gate 	mutex_exit(&kctl_dmod_loads_lock);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	kctl_dlr_free(dlr);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static int
kctl_dmod_unload_common(struct modctl * modp)3020Sstevel@tonic-gate kctl_dmod_unload_common(struct modctl *modp)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	struct modctl *m;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	kctl_dprintf("unloading dmod %s", modp->mod_modname);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	mutex_enter(&kctl_dmods_lock);
3090Sstevel@tonic-gate 	for (m = kctl_dmods.mod_next; m != &kctl_dmods; m = m->mod_next) {
3100Sstevel@tonic-gate 		if (m == modp)
3110Sstevel@tonic-gate 			break;
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	mutex_exit(&kctl_dmods_lock);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (m != modp)
3160Sstevel@tonic-gate 		return (ENOENT);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	/* Found it */
3190Sstevel@tonic-gate 	modp->mod_ref = 0;
3200Sstevel@tonic-gate 	modp->mod_loaded = 0;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	kobj_unload_module(modp);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	mod_release_requisites(modp);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/* Remove it from our dmods list */
3270Sstevel@tonic-gate 	mutex_enter(&kctl_dmods_lock);
3280Sstevel@tonic-gate 	modp->mod_next->mod_prev = modp->mod_prev;
3290Sstevel@tonic-gate 	modp->mod_prev->mod_next = modp->mod_next;
3300Sstevel@tonic-gate 	mutex_exit(&kctl_dmods_lock);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	kctl_strfree(modp->mod_filename);
3330Sstevel@tonic-gate 	kmem_free(modp, sizeof (struct modctl));
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	return (0);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate void
kctl_dmod_unload(kmdb_wr_unload_t * dur)3390Sstevel@tonic-gate kctl_dmod_unload(kmdb_wr_unload_t *dur)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate 	int rc;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if ((rc = kctl_dmod_unload_common(dur->dur_modctl)) != 0) {
3443446Smrj 		cmn_err(CE_WARN, "unexpected dmod unload failure: %d", rc);
3450Sstevel@tonic-gate 		dur->dur_errno = rc;
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * This will be called during shutdown.  The debugger has been stopped, we're
3510Sstevel@tonic-gate  * off the module notification list, and we've already processed everything in
3520Sstevel@tonic-gate  * the driver's work queue.  We should have received (and processed) unload
3530Sstevel@tonic-gate  * requests for each of the dmods we've loaded.  To be safe, however, we'll
3540Sstevel@tonic-gate  * double-check.
3550Sstevel@tonic-gate  *
3560Sstevel@tonic-gate  * If we're doing an emergency shutdown, there may be outstanding
3570Sstevel@tonic-gate  * driver-initiated messages that haven't been returned to us.  The debugger is
3580Sstevel@tonic-gate  * dead, so it's not going to be returning them.  We'll leak them unless we
3590Sstevel@tonic-gate  * find and free them ourselves.
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate void
kctl_dmod_unload_all(void)3620Sstevel@tonic-gate kctl_dmod_unload_all(void)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	kmdb_wr_load_t *dlr;
3650Sstevel@tonic-gate 	struct modctl *modp;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	while ((modp = kctl_dmods.mod_next) != &kctl_dmods)
3680Sstevel@tonic-gate 		(void) kctl_dmod_unload_common(modp);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	while ((dlr = kctl_dmod_loads) != NULL) {
3710Sstevel@tonic-gate 		kctl_dmod_loads = dlr->dlr_next;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 		kctl_dprintf("freed orphan load notification for %s",
3740Sstevel@tonic-gate 		    dlr->dlr_fname);
3750Sstevel@tonic-gate 		kctl_dlr_free(dlr);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate kmdb_wr_path_t *
kctl_dmod_path_set(kmdb_wr_path_t * pth)3800Sstevel@tonic-gate kctl_dmod_path_set(kmdb_wr_path_t *pth)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	kmdb_wr_path_t *opth;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if (kctl.kctl_flags & KMDB_F_DRV_DEBUG) {
3851530Sjohnlev 		if (pth != NULL) {
3861530Sjohnlev 			int i;
3871530Sjohnlev 			kctl_dprintf("changing dmod path to: %p", pth);
3881530Sjohnlev 			for (i = 0; pth->dpth_path[i] != NULL; i++)
3891530Sjohnlev 				kctl_dprintf(" %s", pth->dpth_path[i]);
3901530Sjohnlev 		} else {
3911530Sjohnlev 			kctl_dprintf("changing dmod path to NULL");
3921530Sjohnlev 		}
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	opth = kctl_dmod_path;
3960Sstevel@tonic-gate 	kctl_dmod_path = pth;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	return (opth);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate void
kctl_dmod_path_reset(void)4020Sstevel@tonic-gate kctl_dmod_path_reset(void)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	kmdb_wr_path_t *pth;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if ((pth = kctl_dmod_path_set(NULL)) != NULL) {
4070Sstevel@tonic-gate 		WR_ACK(pth);
4080Sstevel@tonic-gate 		kmdb_wr_debugger_notify(pth);
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate void
kctl_dmod_sync(void)4130Sstevel@tonic-gate kctl_dmod_sync(void)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	struct modctl *modp;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * kobj_sync() has no visibility into our dmods, so we need to
4190Sstevel@tonic-gate 	 * explicitly tell krtld to export the portions of our dmods that were
4200Sstevel@tonic-gate 	 * allocated using boot scratch memory.
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	for (modp = kctl_dmods.mod_next; modp != &kctl_dmods;
4230Sstevel@tonic-gate 	    modp = modp->mod_next)
4240Sstevel@tonic-gate 		kobj_export_module(modp->mod_mp);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate void
kctl_dmod_init(void)4280Sstevel@tonic-gate kctl_dmod_init(void)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	mutex_init(&kctl_dmod_loads_lock, NULL, MUTEX_DRIVER, NULL);
4310Sstevel@tonic-gate 	mutex_init(&kctl_dmods_lock, NULL, MUTEX_DRIVER, NULL);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	bzero(&kctl_dmods, sizeof (struct modctl));
4340Sstevel@tonic-gate 	kctl_dmods.mod_next = kctl_dmods.mod_prev = &kctl_dmods;
4351226Scth 	kdi_dmods = &kctl_dmods;
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate void
kctl_dmod_fini(void)4390Sstevel@tonic-gate kctl_dmod_fini(void)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	mutex_destroy(&kctl_dmods_lock);
4420Sstevel@tonic-gate 	mutex_destroy(&kctl_dmod_loads_lock);
4431226Scth 	kdi_dmods = NULL;
4440Sstevel@tonic-gate }
445