xref: /illumos-gate/usr/src/cmd/mdb/common/kmdb/kmdb_module_load.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 #include <sys/param.h>
30 #include <sys/modctl.h>
31 #include <sys/kobj.h>
32 #include <sys/kobj_impl.h>
33 #include <unistd.h>
34 #include <strings.h>
35 #include <dlfcn.h>
36 #include <link.h>
37 
38 #include <kmdb/kmdb_module.h>
39 #include <kmdb/kmdb_wr_impl.h>
40 #include <kmdb/kmdb_kdi.h>
41 #include <mdb/mdb_modapi.h>
42 #include <mdb/mdb_debug.h>
43 #include <mdb/mdb_string.h>
44 #include <mdb/mdb_err.h>
45 #include <mdb/mdb_io.h>
46 #include <mdb/mdb_frame.h>
47 #include <mdb/mdb.h>
48 
49 static void kmdb_module_request_unload(kmdb_modctl_t *, const char *, int);
50 
51 static void
52 kmc_free(kmdb_modctl_t *kmc)
53 {
54 	if (kmc->kmc_flags & KMDB_MC_FL_CTFCOPIED)
55 		mdb_free(kmc->kmc_ctfdata, kmc->kmc_ctfsize);
56 
57 	if (kmc->kmc_modname != NULL)
58 		strfree(kmc->kmc_modname);
59 	mdb_free(kmc, sizeof (kmdb_modctl_t));
60 }
61 
62 /*
63  * Sends a request to the driver to load the module.  If/when the load has
64  * completed successfully, kmdb_module_loaded is called.
65  */
66 int
67 mdb_module_load(const char *fname, int mode)
68 {
69 	const char *modname = strbasename(fname);
70 	kmdb_wr_load_t *dlr;
71 	kmdb_modctl_t *kmc = NULL;
72 	const char *wformat = NULL;
73 	mdb_var_t *v;
74 
75 	if (!mdb_module_validate_name(modname, &wformat))
76 		goto module_load_err;
77 
78 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, modname)) != NULL) {
79 		kmc = MDB_NV_COOKIE(v);
80 
81 		if (kmc->kmc_state == KMDB_MC_STATE_LOADING)
82 			wformat = "module %s is already being loaded\n";
83 		else
84 			wformat = "module %s is being unloaded\n";
85 		goto module_load_err;
86 	}
87 
88 	kmc = mdb_zalloc(sizeof (kmdb_modctl_t), UM_SLEEP);
89 	kmc->kmc_loadmode = mode;
90 	kmc->kmc_modname = strdup(modname);
91 	kmc->kmc_state = KMDB_MC_STATE_LOADING;
92 
93 	if (mdb_nv_insert(&mdb.m_dmodctl, modname, NULL, (uintptr_t)kmc, 0) ==
94 	    NULL) {
95 		wformat = "module %s can't be registered for load\n";
96 		kmc_free(kmc);
97 		goto module_load_err;
98 	}
99 
100 	dlr = mdb_zalloc(sizeof (kmdb_wr_load_t), UM_SLEEP);
101 	dlr->dlr_node.wn_task = WNTASK_DMOD_LOAD;
102 	dlr->dlr_fname = strdup(fname);
103 
104 	kmdb_wr_driver_notify(dlr);
105 
106 	if (!(mode & MDB_MOD_DEFER) &&
107 	    mdb_tgt_continue(mdb.m_target, NULL) == 0)
108 		return (0);
109 
110 	if (!(mode & MDB_MOD_SILENT))
111 		mdb_printf("%s load pending (:c to complete)\n", modname);
112 
113 	return (0);
114 
115 module_load_err:
116 	if (!(mode & MDB_MOD_SILENT))
117 		warn(wformat, modname);
118 
119 	return (-1);
120 }
121 
122 int
123 kmdb_module_loaded(kmdb_wr_load_t *dlr)
124 {
125 	struct modctl *modp = dlr->dlr_modctl;
126 	const char *modname = strbasename(dlr->dlr_fname);
127 	struct module *mp;
128 	kmdb_modctl_t *kmc;
129 	mdb_var_t *v;
130 
131 	v = mdb_nv_lookup(&mdb.m_dmodctl, modname);
132 
133 	if (dlr->dlr_errno != 0) {
134 		/*
135 		 * We're somewhat limited in the diagnostics that we can
136 		 * provide in the event of a failed load.  In most load-failure
137 		 * cases, the driver can only send up a generic errno.  We use
138 		 * EMDB_ENOMOD to signal generic errors, and supply our own
139 		 * message.  This twists the meaning of EMDB_NOMOD somewhat, but
140 		 * it's better than defining a new one.
141 		 */
142 		if (dlr->dlr_errno == EMDB_NOMOD) {
143 			mdb_warn("%s does not appear to be a kmdb dmod\n",
144 			    modname);
145 		} else {
146 			(void) set_errno(dlr->dlr_errno);
147 			mdb_warn("dmod %s failed to load", modname);
148 		}
149 
150 		if (v != NULL)
151 			mdb_nv_remove(&mdb.m_dmodctl, v);
152 		return (0);
153 	}
154 
155 	if ((mp = modp->mod_mp) == NULL || mp->symhdr == NULL ||
156 	    mp->strhdr == NULL || mp->symtbl == NULL || mp->strings == NULL) {
157 		mdb_warn("dmod %s did not load properly\n");
158 		goto module_loaded_err;
159 	}
160 
161 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, modname)) == NULL) {
162 		kmc = mdb_zalloc(sizeof (kmdb_modctl_t), UM_SLEEP);
163 		kmc->kmc_loadmode = MDB_MOD_LOCAL;
164 		kmc->kmc_modname = strdup(modname);
165 		kmc->kmc_state = KMDB_MC_STATE_LOADING;
166 
167 		(void) mdb_nv_insert(&mdb.m_dmodctl, modname, NULL,
168 		    (uintptr_t)kmc, 0);
169 	} else {
170 		kmc = MDB_NV_COOKIE(v);
171 		ASSERT(kmc->kmc_symtab == NULL);
172 	}
173 
174 	kmc->kmc_modctl = modp;
175 	kmc->kmc_exported = (mp->flags & KOBJ_EXPORTED) != 0;
176 	mdb_gelf_ehdr_to_gehdr(&mp->hdr, &kmc->kmc_ehdr);
177 
178 	kmc->kmc_symtab = mdb_gelf_symtab_create_raw(&kmc->kmc_ehdr, mp->symhdr,
179 	    mp->symtbl, mp->strhdr, mp->strings,
180 	    MDB_TGT_SYMTAB);
181 
182 	if (mp->flags & KOBJ_PRIM) {
183 		kmc->kmc_flags |= KMDB_MC_FL_NOUNLOAD;
184 
185 		/*
186 		 * Prior to the unmapping of boot scratch memory, CTF and symbol
187 		 * data for primary kernel modules is exported (copied) to vmem.
188 		 * If we start using the pre-export CTF data, we'll be in for a
189 		 * rude surprise when it moves.  Unfortunately, we can't simply
190 		 * adapt to the moved data, as we may have already passed out
191 		 * pointers (via libstandctf) into the data at the old location.
192 		 * To avoid this problem, we'll copy the CTF data into Oz if
193 		 * we're seeing the module before it has been exported.  This
194 		 * issue only arises when we enter boot-loaded kmdb before the
195 		 * completion of kdi_dvec_modavail().
196 		 *
197 		 * Note that this still leaves us with a migratory symbol table.
198 		 * Unlike with CTF, we have the ability to deal with symbol
199 		 * tables whose addresses change, so we'll just rebuild the kmc
200 		 * symtab when we detect the export.
201 		 */
202 		if (!(mp->flags & KOBJ_EXPORTED) && mp->ctfdata != NULL) {
203 			kmc->kmc_ctfdata = mdb_alloc(mp->ctfsize, UM_SLEEP);
204 			kmc->kmc_ctfsize = mp->ctfsize;
205 			bcopy(mp->ctfdata, kmc->kmc_ctfdata, kmc->kmc_ctfsize);
206 			kmc->kmc_flags |= KMDB_MC_FL_CTFCOPIED;
207 		}
208 	} else {
209 		kmc->kmc_ctfdata = mp->ctfdata;
210 		kmc->kmc_ctfsize = mp->ctfsize;
211 	}
212 
213 	if (mdb_module_create(modname, modp->mod_filename,
214 	    kmc->kmc_loadmode, &kmc->kmc_mod) < 0)
215 		goto module_loaded_err;
216 
217 	kmc->kmc_state = KMDB_MC_STATE_LOADED;
218 
219 	return (1);
220 
221 module_loaded_err:
222 	if (kmc->kmc_symtab != NULL)
223 		mdb_gelf_symtab_destroy(kmc->kmc_symtab);
224 
225 	kmdb_module_request_unload(kmc, kmc->kmc_modname, MDB_MOD_DEFER);
226 	return (0);
227 }
228 
229 void
230 kmdb_module_load_ack(kmdb_wr_load_t *dlr)
231 {
232 	strfree(dlr->dlr_fname);
233 	mdb_free(dlr, sizeof (kmdb_wr_load_t));
234 }
235 
236 void
237 mdb_module_load_all(int mode)
238 {
239 	kmdb_wr_t *wn;
240 
241 	ASSERT(mode & MDB_MOD_DEFER);
242 
243 	wn = mdb_zalloc(sizeof (kmdb_wr_t), UM_SLEEP);
244 	wn->wn_task = WNTASK_DMOD_LOAD_ALL;
245 
246 	kmdb_wr_driver_notify(wn);
247 }
248 
249 void
250 kmdb_module_load_all_ack(kmdb_wr_t *wn)
251 {
252 	mdb_free(wn, sizeof (kmdb_wr_t));
253 }
254 
255 static void
256 kmdb_module_request_unload(kmdb_modctl_t *kmc, const char *modname, int mode)
257 {
258 	kmdb_wr_unload_t *dur = mdb_zalloc(sizeof (kmdb_wr_unload_t), UM_SLEEP);
259 	dur->dur_node.wn_task = WNTASK_DMOD_UNLOAD;
260 	dur->dur_modname = strdup(modname);
261 	dur->dur_modctl = kmc->kmc_modctl;
262 
263 	kmdb_wr_driver_notify(dur);
264 
265 	kmc->kmc_state = KMDB_MC_STATE_UNLOADING;
266 
267 	if (!(mode & MDB_MOD_DEFER) &&
268 	    mdb_tgt_continue(mdb.m_target, NULL) == 0)
269 		return;
270 
271 	if (!(mode & MDB_MOD_SILENT))
272 		mdb_printf("%s unload pending (:c to complete)\n", modname);
273 }
274 
275 /*ARGSUSED*/
276 int
277 mdb_module_unload(const char *name, int mode)
278 {
279 	kmdb_modctl_t *kmc = NULL;
280 	const char *basename;
281 	mdb_var_t *v;
282 
283 	/*
284 	 * We may have been called with the name from the module itself
285 	 * if the caller is iterating through the module list, so we need
286 	 * to make a copy of the name.  If we don't, we can't use it after
287 	 * the call to unload_common(), which frees the module.
288 	 */
289 	name = strdup(name);
290 	basename = strbasename(name);
291 
292 	/*
293 	 * Make sure the module is in the proper state for unloading.  Modules
294 	 * may only be unloaded if they have properly completed loading.
295 	 */
296 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, basename)) != NULL) {
297 		kmc = MDB_NV_COOKIE(v);
298 		switch (kmc->kmc_state) {
299 		case KMDB_MC_STATE_LOADING:
300 			warn("%s is in the process of loading\n", basename);
301 			return (set_errno(EMDB_NOMOD));
302 		case KMDB_MC_STATE_UNLOADING:
303 			warn("%s is already being unloaded\n", basename);
304 			return (set_errno(EMDB_NOMOD));
305 		default:
306 			ASSERT(kmc->kmc_state == KMDB_MC_STATE_LOADED);
307 		}
308 
309 		if (kmc->kmc_flags & KMDB_MC_FL_NOUNLOAD)
310 			return (set_errno(EMDB_KMODNOUNLOAD));
311 	}
312 
313 	if (mdb_module_unload_common(name) < 0) {
314 		if (!(mode & MDB_MOD_SILENT)) {
315 			mdb_dprintf(MDB_DBG_MODULE, "unload of %s failed\n",
316 			    name);
317 		}
318 		return (-1); /* errno is set for us */
319 	}
320 
321 	/*
322 	 * Any modules legitimately not listed in dmodctl (builtins, for
323 	 * example) will be handled by mdb_module_unload_common.  If any of
324 	 * them get here, we've got a problem.
325 	 */
326 	if (v == NULL) {
327 		warn("unload of unregistered module %s\n", basename);
328 		return (set_errno(EMDB_NOMOD));
329 	}
330 
331 	ASSERT(kmc->kmc_dlrefcnt == 0);
332 
333 	mdb_gelf_symtab_destroy(kmc->kmc_symtab);
334 
335 	kmdb_module_request_unload(kmc, basename, mode);
336 	return (0);
337 }
338 
339 int
340 kmdb_module_unloaded(kmdb_wr_unload_t *dur)
341 {
342 	mdb_var_t *v;
343 
344 	if ((v = mdb_nv_lookup(&mdb.m_dmodctl, dur->dur_modname)) == NULL) {
345 		mdb_warn("unload for unrequested module %s\n",
346 		    dur->dur_modname);
347 		return (0);
348 	}
349 
350 	if (dur->dur_errno != 0) {
351 		mdb_warn("dmod %s failed to unload", dur->dur_modname);
352 		return (0);
353 	}
354 
355 	kmc_free(MDB_NV_COOKIE(v));
356 	mdb_nv_remove(&mdb.m_dmodctl, v);
357 
358 	return (1);
359 }
360 
361 void
362 kmdb_module_unload_ack(kmdb_wr_unload_t *dur)
363 {
364 	if (dur->dur_modname != NULL)
365 		strfree(dur->dur_modname);
366 	mdb_free(dur, sizeof (kmdb_wr_unload_t));
367 }
368 
369 /*
370  * Called by the kmdb_kvm target upon debugger reentry, this routine checks
371  * to see if the loaded dmods have changed.  Of particular interest is the
372  * exportation of dmod symbol tables, which will happen during the boot
373  * process for dmods that were loaded prior to kernel startup.  If this
374  * has occurred, we'll need to reconstruct our view of the symbol tables for
375  * the affected dmods.
376  */
377 void
378 kmdb_module_sync(void)
379 {
380 	mdb_var_t *v;
381 
382 	mdb_nv_rewind(&mdb.m_dmodctl);
383 	while ((v = mdb_nv_advance(&mdb.m_dmodctl)) != NULL) {
384 		kmdb_modctl_t *kmc = MDB_NV_COOKIE(v);
385 		struct module *mp;
386 
387 		if (kmc->kmc_state != KMDB_MC_STATE_LOADED)
388 			continue;
389 
390 		mp = kmc->kmc_modctl->mod_mp;
391 
392 		if ((mp->flags & (KOBJ_PRIM | KOBJ_EXPORTED)) &&
393 		    !kmc->kmc_exported) {
394 			/*
395 			 * The exporting process moves the symtab from boot
396 			 * scratch memory to vmem.
397 			 */
398 			if (kmc->kmc_symtab != NULL)
399 				mdb_gelf_symtab_destroy(kmc->kmc_symtab);
400 
401 			kmc->kmc_symtab = mdb_gelf_symtab_create_raw(
402 			    &kmc->kmc_ehdr, mp->symhdr, mp->symtbl, mp->strhdr,
403 			    mp->strings, MDB_TGT_SYMTAB);
404 
405 			kmc->kmc_exported = TRUE;
406 		}
407 	}
408 }
409