xref: /illumos-gate/usr/src/cmd/mdb/common/kmdb/kctl/kctl_dmod.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Driver-side functions for loading and unloading dmods.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/kobj.h>
35 #include <sys/kobj_impl.h>
36 #include <sys/modctl.h>
37 #include <sys/systm.h>
38 #include <sys/ctf_api.h>
39 #include <sys/kmdb.h>
40 
41 #include <kmdb/kctl/kctl.h>
42 #include <kmdb/kctl/kctl_wr.h>
43 #include <kmdb/kmdb_wr_impl.h>
44 #include <kmdb/kmdb_kdi.h>
45 #include <mdb/mdb_errno.h>
46 
47 /*
48  * When a load is attempted, a check is first made of the modules on the
49  * kctl_dmods list.  If a module is found, the load will not proceed.
50  * kctl_dmods_lock must be held while traversing kctl_dmods, and while adding
51  * to and subtracting from it.
52  */
53 static struct modctl	kctl_dmods;
54 static kmutex_t		kctl_dmods_lock;
55 
56 static kmdb_wr_path_t	*kctl_dmod_path;
57 
58 /*
59  * Used to track outstanding driver-initiated load notifications.  These
60  * notifications have been allocated by driver, and thus must be freed by the
61  * driver in the event of an emergency unload.  If we don't free them free
62  * them ourselves, they'll leak.  Granted, the world is probably melting down
63  * at that point, but there's no reason why we shouldn't tidy up the deck
64  * chairs before we go.
65  */
66 static kmdb_wr_load_t	*kctl_dmod_loads;
67 static kmutex_t 	kctl_dmod_loads_lock;
68 
69 static int
70 kctl_find_module(char *modname, char *fullname, size_t fullnamelen)
71 {
72 	intptr_t fd;
73 	int i;
74 
75 	/* If they gave us an absolute path, we don't need to search */
76 	if (modname[0] == '/') {
77 		if (strlen(modname) + 1 > fullnamelen) {
78 			cmn_err(CE_WARN, "Can't load dmod %s - name too long\n",
79 			    modname);
80 			return (0);
81 		}
82 
83 		if ((fd = kobj_open(modname)) == -1)
84 			return (0);
85 		kobj_close(fd);
86 
87 		strcpy(fullname, modname);
88 
89 		return (1);
90 	}
91 
92 	for (i = 0; kctl_dmod_path->dpth_path[i] != NULL; i++) {
93 		const char *path = kctl_dmod_path->dpth_path[i];
94 
95 		if (strlen(path) + 1 + strlen(modname) + 1 > fullnamelen) {
96 			kctl_dprintf("Can't load dmod from %s/%s - "
97 			    "name too long", path, modname);
98 			continue;
99 		}
100 
101 		(void) snprintf(fullname, fullnamelen, "%s/%s", path, modname);
102 
103 		if ((fd = kobj_open(fullname)) == -1)
104 			continue;
105 
106 		kobj_close(fd);
107 
108 		kctl_dprintf("kobj_open %s found", fullname);
109 
110 		/* Found it */
111 		return (1);
112 	}
113 
114 	/* No luck */
115 	return (0);
116 }
117 
118 static void
119 kctl_dlr_free(kmdb_wr_load_t *dlr)
120 {
121 	if (dlr->dlr_node.wn_flags & WNFLAGS_NOFREE)
122 		return;
123 
124 	kctl_strfree(dlr->dlr_fname);
125 	kmem_free(dlr, sizeof (kmdb_wr_load_t));
126 }
127 
128 int
129 kctl_dmod_load(kmdb_wr_load_t *dlr)
130 {
131 	struct modctl *modp;
132 	char modpath[MAXPATHLEN];
133 	const char *modname = kctl_basename(dlr->dlr_fname);
134 	int rc;
135 
136 	mutex_enter(&kctl_dmods_lock);
137 
138 	/* Have we already loaded this dmod? */
139 	for (modp = kctl_dmods.mod_next; modp != &kctl_dmods;
140 	    modp = modp->mod_next) {
141 		if (strcmp(modname, modp->mod_modname) == 0) {
142 			mutex_exit(&kctl_dmods_lock);
143 			dlr->dlr_errno = EEXIST;
144 			return (-1);
145 		}
146 	}
147 
148 	/*
149 	 * If we find something that looks like a dmod, create a modctl for it,
150 	 * and add said modctl to our dmods list.  This will allow us to drop
151 	 * the dmods lock, while still preventing duplicate loads.  If we aren't
152 	 * able to actually load the dmod, we can always remove the modctl
153 	 * later.
154 	 */
155 	if (!kctl_find_module(dlr->dlr_fname, modpath, sizeof (modpath))) {
156 		mutex_exit(&kctl_dmods_lock);
157 		dlr->dlr_errno = ENOENT;
158 		return (-1);
159 	}
160 
161 	modp = kobj_zalloc(sizeof (struct modctl), KM_SLEEP);
162 
163 	modp->mod_filename = kctl_strdup(modpath);
164 	modp->mod_modname = kctl_basename(modp->mod_filename);
165 	modp->mod_busy = 1;
166 	modp->mod_loadflags |= MOD_NOAUTOUNLOAD | MOD_NONOTIFY;
167 	modp->mod_next = &kctl_dmods;
168 	modp->mod_prev = kctl_dmods.mod_prev;
169 	modp->mod_prev->mod_next = modp;
170 	kctl_dmods.mod_prev = modp;
171 
172 	mutex_exit(&kctl_dmods_lock);
173 
174 	if (kctl.kctl_boot_ops == NULL)
175 		rc = kobj_load_module(modp, 0);
176 	else
177 		rc = kobj_load_primary_module(modp);
178 
179 	if (rc < 0) {
180 		kctl_warn("failed to load dmod %s", modp->mod_modname);
181 
182 		if (kctl.kctl_boot_ops == NULL)
183 			mod_release_requisites(modp);
184 
185 		mutex_enter(&kctl_dmods_lock);
186 		modp->mod_next->mod_prev = modp->mod_prev;
187 		modp->mod_prev->mod_next = modp->mod_next;
188 		mutex_exit(&kctl_dmods_lock);
189 
190 		kctl_strfree(modp->mod_filename);
191 		kobj_free(modp, sizeof (struct modctl));
192 
193 		dlr->dlr_errno = EMDB_NOMOD;
194 		return (-1);
195 	}
196 
197 	/*
198 	 * It worked!  If the module has any CTF data, decompress it, and make a
199 	 * note of the load.
200 	 */
201 	mutex_enter(&mod_lock);
202 	if ((rc = kctl_mod_decompress(modp)) != 0) {
203 		kctl_warn("failed to decompress CTF data for dmod %s: %s",
204 		    modpath, ctf_errmsg(rc));
205 	}
206 	mutex_exit(&mod_lock);
207 
208 	kctl_dprintf("loaded dmod %s at %p", modpath, modp);
209 
210 	modp->mod_ref = 1;
211 	modp->mod_loaded = 1;
212 
213 	dlr->dlr_modctl = modp;
214 
215 	return (0);
216 }
217 
218 /*
219  * Driver-initiated loads.  Load the module and announce it to the debugger.
220  */
221 void
222 kctl_dmod_autoload(const char *fname)
223 {
224 	kmdb_wr_load_t *dlr;
225 
226 	dlr = kobj_zalloc(sizeof (kmdb_wr_load_t), KM_SLEEP);
227 	dlr->dlr_node.wn_task = WNTASK_DMOD_LOAD;
228 	dlr->dlr_fname = kctl_strdup(fname);
229 
230 	/*
231 	 * If we're loading at boot, the kmdb_wr_load_t will have been
232 	 * "allocated" by krtld, and will thus not be under the control of
233 	 * kmem.  We need to ensure that we don't attempt to free it when
234 	 * we get it back from the debugger.
235 	 */
236 	if (kctl.kctl_boot_ops != NULL)
237 		dlr->dlr_node.wn_flags |= WNFLAGS_NOFREE;
238 
239 	if (kctl_dmod_load(dlr) < 0) {
240 		kctl_dlr_free(dlr);
241 		return;
242 	}
243 
244 	/*
245 	 * Add to the list of open driver-initiated loads.  We need to track
246 	 * these so we can free them (and thus avoid leaks) in the event that
247 	 * the debugger needs to be blown away before it can return them.
248 	 */
249 	mutex_enter(&kctl_dmod_loads_lock);
250 	dlr->dlr_next = kctl_dmod_loads;
251 	if (kctl_dmod_loads != NULL)
252 		kctl_dmod_loads->dlr_prev = dlr;
253 	kctl_dmod_loads = dlr;
254 	mutex_exit(&kctl_dmod_loads_lock);
255 
256 	kmdb_wr_debugger_notify(dlr);
257 }
258 
259 void
260 kctl_dmod_load_all(void)
261 {
262 	/*
263 	 * The standard list of modules isn't populated until the tail end of
264 	 * kobj_init().  Prior to that point, the only available list is that of
265 	 * primaries.  We'll use that if the normal list isn't ready yet.
266 	 */
267 	if (modules.mod_mp == NULL) {
268 		/* modules hasn't been initialized yet -- use primaries */
269 		struct modctl_list *ml;
270 
271 		for (ml = kobj_linkmaps[KOBJ_LM_PRIMARY]; ml != NULL;
272 		    ml = ml->modl_next)
273 			kctl_dmod_autoload(ml->modl_modp->mod_modname);
274 
275 	} else {
276 		struct modctl *modp = &modules;
277 
278 		do {
279 			if (modp->mod_mp != NULL)
280 				kctl_dmod_autoload(modp->mod_modname);
281 		} while ((modp = modp->mod_next) != &modules);
282 	}
283 }
284 
285 void
286 kctl_dmod_load_ack(kmdb_wr_load_t *dlr)
287 {
288 	/* Remove from the list of open driver-initiated requests */
289 	mutex_enter(&kctl_dmod_loads_lock);
290 	if (dlr->dlr_prev == NULL)
291 		kctl_dmod_loads = dlr->dlr_next;
292 	else
293 		dlr->dlr_prev->dlr_next = dlr->dlr_next;
294 
295 	if (dlr->dlr_next != NULL)
296 		dlr->dlr_next->dlr_prev = dlr->dlr_prev;
297 	mutex_exit(&kctl_dmod_loads_lock);
298 
299 	kctl_dlr_free(dlr);
300 }
301 
302 static int
303 kctl_dmod_unload_common(struct modctl *modp)
304 {
305 	struct modctl *m;
306 
307 	kctl_dprintf("unloading dmod %s", modp->mod_modname);
308 
309 	mutex_enter(&kctl_dmods_lock);
310 	for (m = kctl_dmods.mod_next; m != &kctl_dmods; m = m->mod_next) {
311 		if (m == modp)
312 			break;
313 	}
314 	mutex_exit(&kctl_dmods_lock);
315 
316 	if (m != modp)
317 		return (ENOENT);
318 
319 	/* Found it */
320 	modp->mod_ref = 0;
321 	modp->mod_loaded = 0;
322 
323 	kobj_unload_module(modp);
324 
325 	mod_release_requisites(modp);
326 
327 	/* Remove it from our dmods list */
328 	mutex_enter(&kctl_dmods_lock);
329 	modp->mod_next->mod_prev = modp->mod_prev;
330 	modp->mod_prev->mod_next = modp->mod_next;
331 	mutex_exit(&kctl_dmods_lock);
332 
333 	kctl_strfree(modp->mod_filename);
334 	kmem_free(modp, sizeof (struct modctl));
335 
336 	return (0);
337 }
338 
339 void
340 kctl_dmod_unload(kmdb_wr_unload_t *dur)
341 {
342 	int rc;
343 
344 	if ((rc = kctl_dmod_unload_common(dur->dur_modctl)) != 0) {
345 		cmn_err(CE_WARN, "unexpected dmod unload failure: %d\n", rc);
346 		dur->dur_errno = rc;
347 	}
348 }
349 
350 /*
351  * This will be called during shutdown.  The debugger has been stopped, we're
352  * off the module notification list, and we've already processed everything in
353  * the driver's work queue.  We should have received (and processed) unload
354  * requests for each of the dmods we've loaded.  To be safe, however, we'll
355  * double-check.
356  *
357  * If we're doing an emergency shutdown, there may be outstanding
358  * driver-initiated messages that haven't been returned to us.  The debugger is
359  * dead, so it's not going to be returning them.  We'll leak them unless we
360  * find and free them ourselves.
361  */
362 void
363 kctl_dmod_unload_all(void)
364 {
365 	kmdb_wr_load_t *dlr;
366 	struct modctl *modp;
367 
368 	while ((modp = kctl_dmods.mod_next) != &kctl_dmods)
369 		(void) kctl_dmod_unload_common(modp);
370 
371 	while ((dlr = kctl_dmod_loads) != NULL) {
372 		kctl_dmod_loads = dlr->dlr_next;
373 
374 		kctl_dprintf("freed orphan load notification for %s",
375 		    dlr->dlr_fname);
376 		kctl_dlr_free(dlr);
377 	}
378 }
379 
380 kmdb_wr_path_t *
381 kctl_dmod_path_set(kmdb_wr_path_t *pth)
382 {
383 	kmdb_wr_path_t *opth;
384 
385 	if (kctl.kctl_flags & KMDB_F_DRV_DEBUG) {
386 		int i;
387 		kctl_dprintf("changing dmod path to: %p", pth);
388 		for (i = 0; pth->dpth_path[i] != NULL; i++)
389 			kctl_dprintf(" %s", pth->dpth_path[i]);
390 	}
391 
392 	opth = kctl_dmod_path;
393 	kctl_dmod_path = pth;
394 
395 	return (opth);
396 }
397 
398 void
399 kctl_dmod_path_reset(void)
400 {
401 	kmdb_wr_path_t *pth;
402 
403 	if ((pth = kctl_dmod_path_set(NULL)) != NULL) {
404 		WR_ACK(pth);
405 		kmdb_wr_debugger_notify(pth);
406 	}
407 }
408 
409 void
410 kctl_dmod_sync(void)
411 {
412 	struct modctl *modp;
413 
414 	/*
415 	 * kobj_sync() has no visibility into our dmods, so we need to
416 	 * explicitly tell krtld to export the portions of our dmods that were
417 	 * allocated using boot scratch memory.
418 	 */
419 	for (modp = kctl_dmods.mod_next; modp != &kctl_dmods;
420 	    modp = modp->mod_next)
421 		kobj_export_module(modp->mod_mp);
422 }
423 
424 void
425 kctl_dmod_init(void)
426 {
427 	mutex_init(&kctl_dmod_loads_lock, NULL, MUTEX_DRIVER, NULL);
428 	mutex_init(&kctl_dmods_lock, NULL, MUTEX_DRIVER, NULL);
429 
430 	bzero(&kctl_dmods, sizeof (struct modctl));
431 	kctl_dmods.mod_next = kctl_dmods.mod_prev = &kctl_dmods;
432 }
433 
434 void
435 kctl_dmod_fini(void)
436 {
437 	mutex_destroy(&kctl_dmods_lock);
438 	mutex_destroy(&kctl_dmod_loads_lock);
439 }
440