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