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