dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mdb / common / kmdb / kctl / kctl_main.c
blobddf31536a0720a90bf1463eb9cba8293e416d267
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <kmdb/kctl/kctl.h>
27 #include <kmdb/kctl/kctl_wr.h>
28 #include <kmdb/kmdb_kctl.h>
29 #include <kmdb/kmdb_kdi.h>
30 #include <kmdb/kmdb_auxv.h>
31 #include <mdb/mdb_errno.h>
33 #include <sys/sysmacros.h>
34 #include <sys/reboot.h>
35 #include <sys/atomic.h>
36 #include <sys/bootconf.h>
37 #include <sys/kmdb.h>
38 #include <sys/kobj.h>
39 #include <sys/kobj_impl.h>
40 #include <sys/promimpl.h>
41 #include <sys/kdi_impl.h>
42 #include <sys/ctf_api.h>
43 #include <vm/seg_kmem.h>
45 kctl_t kctl;
47 #define KCTL_EXECNAME "/kernel/drv/kmdb"
49 #if defined(_LP64)
50 #define KCTL_MEM_GOALSZ (20 * 1024 * 1024)
51 #else
52 #define KCTL_MEM_GOALSZ (10 * 1024 * 1024)
53 #endif
56 * kmdb will call its own copies of the promif routines during
57 * initialization. As these routines are intended to be used when the
58 * world is stopped, they don't attempt to grab the PROM lock. Very
59 * Bad Things could happen if kmdb called a prom routine while someone
60 * else was calling the kernel's copy of another prom routine, so we
61 * grab the PROM lock ourselves before we start initialization.
63 #define KCTL_PROM_LOCK
64 #define KCTL_PROM_UNLOCK
66 static int
67 kctl_init(void)
69 if (kobj_kdi.kdi_version != KDI_VERSION) {
70 kctl_warn("kmdb/kernel version mismatch (expected %d, "
71 "found %d)", KDI_VERSION, kobj_kdi.kdi_version);
72 return (-1);
75 sema_init(&kctl.kctl_wr_avail_sem, 0, NULL, SEMA_DRIVER, NULL);
76 mutex_init(&kctl.kctl_wr_lock, NULL, MUTEX_DRIVER, NULL);
77 cv_init(&kctl.kctl_wr_cv, NULL, CV_DRIVER, NULL);
78 mutex_init(&kctl.kctl_lock, NULL, MUTEX_DRIVER, NULL);
80 kctl.kctl_execname = KCTL_EXECNAME; /* XXX get from modctl? */
82 kctl.kctl_state = KCTL_ST_INACTIVE;
84 kctl.kctl_dseg = kctl.kctl_mrbase = NULL;
85 kctl.kctl_dseg_size = kctl.kctl_mrsize = 0;
87 kctl_dmod_init();
89 return (0);
92 static void
93 kctl_fini(void)
95 kctl_dmod_fini();
97 mutex_destroy(&kctl.kctl_lock);
98 cv_destroy(&kctl.kctl_wr_cv);
99 mutex_destroy(&kctl.kctl_wr_lock);
100 sema_destroy(&kctl.kctl_wr_avail_sem);
103 static uint_t
104 kctl_set_state(uint_t state)
106 uint_t ostate = kctl.kctl_state;
108 /* forward progess only, please */
109 if (state > ostate) {
110 kctl_dprintf("new kctl state: %d", state);
111 kctl.kctl_state = state;
114 return (ostate);
117 static int
118 kctl_boot_dseg_alloc(caddr_t dsegaddr, size_t dsegsz)
121 * The Intel boot memory allocator will cleverly map us onto a 4M
122 * page if we request the whole 4M Intel segment at once. This
123 * will break physical memory r/w, so we break the request into
124 * chunks. The allocator isn't smart enough to combine requests,
125 * so it'll give us a bunch of 4k pages.
127 while (dsegsz >= 1024*1024) {
128 size_t sz = MIN(dsegsz, 1024*1024);
130 if (BOP_ALLOC(kctl.kctl_boot_ops, dsegaddr, sz, BO_NO_ALIGN) !=
131 dsegaddr)
132 return (-1);
134 dsegaddr += sz;
135 dsegsz -= sz;
138 return (0);
141 static int
142 kctl_dseg_alloc(caddr_t addr, size_t sz)
144 ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0);
146 /* make sure there isn't something there already (like kadb) */
147 if (hat_getpfnum(kas.a_hat, addr) != PFN_INVALID)
148 return (EAGAIN);
150 if (segkmem_xalloc(NULL, addr, sz, VM_NOSLEEP, 0, segkmem_page_create,
151 NULL) == NULL)
152 return (ENOMEM);
154 return (0);
157 static void
158 kctl_dseg_free(caddr_t addr, size_t sz)
160 ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0);
162 segkmem_free(NULL, addr, sz);
165 static void
166 kctl_memavail(void)
168 size_t needed;
169 caddr_t base;
172 * We're now free to allocate the non-fixed portion of the debugger's
173 * memory region.
176 needed = P2ROUNDUP(kctl.kctl_memgoalsz <= kctl.kctl_dseg_size ? 0 :
177 kctl.kctl_memgoalsz - kctl.kctl_dseg_size, PAGESIZE);
179 if (needed == 0)
180 return;
182 if ((base = kmem_zalloc(needed, KM_NOSLEEP)) == NULL) {
184 * If we're going to wedge the machine during debugger startup,
185 * at least let them know why it's going to wedge.
187 cmn_err(CE_WARN, "retrying of kmdb allocation of 0x%lx bytes",
188 (ulong_t)needed);
190 base = kmem_zalloc(needed, KM_SLEEP);
193 kdi_dvec->dv_memavail(base, needed);
194 kctl.kctl_mrbase = base;
195 kctl.kctl_mrsize = needed;
198 void
199 kctl_cleanup(void)
201 uint_t state = kctl_set_state(KCTL_ST_DEACTIVATING);
203 kctl_dprintf("cleaning up from state %d", state);
205 ASSERT(kctl.kctl_boot_loaded == 0);
207 switch (state) {
208 case KCTL_ST_ACTIVE:
209 boothowto &= ~RB_DEBUG;
210 /* XXX there's a race here */
211 kdi_dvec = NULL;
212 /*FALLTHROUGH*/
214 case KCTL_ST_DBG_ACTIVATED:
215 KCTL_PROM_LOCK;
216 kmdb_deactivate();
217 KCTL_PROM_UNLOCK;
218 /*FALLTHROUGH*/
220 case KCTL_ST_THREAD_STARTED:
221 if (curthread != kctl.kctl_wr_thr) {
222 kctl_wr_thr_stop();
223 kctl_wr_thr_join();
225 /*FALLTHROUGH*/
227 case KCTL_ST_MOD_NOTIFIERS:
228 kctl_mod_notify_unreg();
229 /*FALLTHROUGH*/
231 case KCTL_ST_KCTL_PREACTIVATED:
232 kctl_depreactivate_isadep();
233 /*FALLTHROUGH*/
235 case KCTL_ST_INITIALIZED:
236 /* There's no kmdb_fini */
237 case KCTL_ST_DSEG_ALLOCED:
238 kctl_dseg_free(kctl.kctl_dseg, kctl.kctl_dseg_size);
240 if (kctl.kctl_mrbase != NULL)
241 kmem_free(kctl.kctl_mrbase, kctl.kctl_mrsize);
242 /*FALLTHROUGH*/
245 kctl.kctl_state = KCTL_ST_INACTIVE;
248 static void
249 kctl_startup_modules(void)
251 struct modctl *modp;
254 * Normal module load and unload is now available. Prior to this point,
255 * we could only load modules, and that only when the debugger was being
256 * initialized.
258 * We'll need to prepare the modules we've already loaded (if any) for
259 * the brave new world in which boot is unmapped.
261 kctl_dmod_sync();
264 * Process any outstanding loads or unloads and prepare for automatic
265 * module loading and unloading.
267 (void) kctl_wr_process();
269 kctl_mod_notify_reg();
271 (void) kctl_set_state(KCTL_ST_MOD_NOTIFIERS);
273 modp = &modules;
274 do {
275 kctl_mod_loaded(modp);
276 } while ((modp = modp->mod_next) != &modules);
279 static void
280 kctl_startup_thread(void)
283 * Create the worker thread, which will handle future requests from the
284 * debugger.
286 kctl_wr_thr_start();
288 (void) kctl_set_state(KCTL_ST_THREAD_STARTED);
291 static int
292 kctl_startup_boot(void)
294 struct modctl_list *lp, **lpp;
295 int rc;
297 if (kctl_wr_process() < 0) {
298 kctl_warn("kmdb: failed to load modules");
299 return (-1);
302 mutex_enter(&mod_lock);
304 for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) {
305 for (lp = *lpp; lp != NULL; lp = lp->modl_next) {
306 if ((rc = kctl_mod_decompress(lp->modl_modp)) != 0) {
307 kctl_warn("kmdb: failed to decompress CTF data "
308 "for %s: %s", lp->modl_modp->mod_modname,
309 ctf_errmsg(rc));
314 mutex_exit(&mod_lock);
316 return (0);
319 static int
320 kctl_startup_preactivate(void *romp, const char *cfg, const char **argv)
322 kmdb_auxv_t kav;
323 int rc;
325 kctl_auxv_init(&kav, cfg, argv, romp);
326 KCTL_PROM_LOCK;
327 rc = kmdb_init(kctl.kctl_execname, &kav);
328 KCTL_PROM_UNLOCK;
329 kctl_auxv_fini(&kav);
331 if (rc < 0)
332 return (EMDB_KNOLOAD);
334 (void) kctl_set_state(KCTL_ST_INITIALIZED);
336 if (kctl_preactivate_isadep() != 0)
337 return (EIO);
339 (void) kctl_set_state(KCTL_ST_KCTL_PREACTIVATED);
341 return (0);
344 static int
345 kctl_startup_activate(uint_t flags)
347 kdi_debugvec_t *dvec;
349 KCTL_PROM_LOCK;
350 kmdb_activate(&dvec, flags);
351 KCTL_PROM_UNLOCK;
353 (void) kctl_set_state(KCTL_ST_DBG_ACTIVATED);
356 * fill in a few remaining debugvec entries.
358 dvec->dv_kctl_modavail = kctl_startup_modules;
359 dvec->dv_kctl_thravail = kctl_startup_thread;
360 dvec->dv_kctl_memavail = kctl_memavail;
362 kctl_activate_isadep(dvec);
364 kdi_dvec = dvec;
365 membar_producer();
367 boothowto |= RB_DEBUG;
369 (void) kctl_set_state(KCTL_ST_ACTIVE);
371 return (0);
374 static int
375 kctl_state_check(uint_t state, uint_t ok_state)
377 if (state == ok_state)
378 return (0);
380 if (state == KCTL_ST_INACTIVE)
381 return (EMDB_KINACTIVE);
382 else if (kctl.kctl_state > KCTL_ST_INACTIVE &&
383 kctl.kctl_state < KCTL_ST_ACTIVE)
384 return (EMDB_KACTIVATING);
385 else if (kctl.kctl_state == KCTL_ST_ACTIVE)
386 return (EMDB_KACTIVE);
387 else if (kctl.kctl_state == KCTL_ST_DEACTIVATING)
388 return (EMDB_KDEACTIVATING);
389 else
390 return (EINVAL);
394 kctl_deactivate(void)
396 int rc;
398 mutex_enter(&kctl.kctl_lock);
400 if (kctl.kctl_boot_loaded) {
401 rc = EMDB_KNOUNLOAD;
402 goto deactivate_done;
405 if ((rc = kctl_state_check(kctl.kctl_state, KCTL_ST_ACTIVE)) != 0)
406 goto deactivate_done;
408 kmdb_kdi_set_unload_request();
409 kmdb_kdi_kmdb_enter();
412 * The debugger will pass the request to the work thread, which will
413 * stop itself.
415 kctl_wr_thr_join();
417 deactivate_done:
418 mutex_exit(&kctl.kctl_lock);
420 return (rc);
424 * Called from krtld, this indicates that the user loaded kmdb at boot. We
425 * track activation states, but we don't attempt to clean up if activation
426 * fails, because boot debugger load failures are fatal.
428 * Further complicating matters, various kernel routines, such as bcopy and
429 * mutex_enter, assume the presence of some basic state. On SPARC, it's the
430 * presence of a valid curthread pointer. On AMD64, it's a valid curcpu
431 * pointer in GSBASE. We set up temporary versions of these before beginning
432 * activation, and tear them down when we're done.
435 kctl_boot_activate(struct bootops *ops, void *romp, size_t memsz,
436 const char **argv)
438 void *old;
440 old = kctl_boot_tmpinit(); /* Set up temporary state */
442 ASSERT(ops != NULL);
443 kctl.kctl_boot_ops = ops; /* must be set before kctl_init */
445 if (kctl_init() < 0)
446 return (-1);
448 kctl.kctl_boot_loaded = 1;
450 kctl_dprintf("beginning kmdb initialization");
452 if (memsz == 0)
453 memsz = KCTL_MEM_GOALSZ;
455 kctl.kctl_dseg = kdi_segdebugbase;
456 kctl.kctl_dseg_size =
457 memsz > kdi_segdebugsize ? kdi_segdebugsize : memsz;
458 kctl.kctl_memgoalsz = memsz;
460 if (kctl_boot_dseg_alloc(kctl.kctl_dseg, kctl.kctl_dseg_size) < 0) {
461 kctl_warn("kmdb: failed to allocate %lu-byte debugger area at "
462 "%p", kctl.kctl_dseg_size, (void *)kctl.kctl_dseg);
463 return (-1);
466 (void) kctl_set_state(KCTL_ST_DSEG_ALLOCED);
468 if (kctl_startup_preactivate(romp, NULL, argv) != 0 ||
469 kctl_startup_activate(KMDB_ACT_F_BOOT)) {
470 kctl_warn("kmdb: failed to activate");
471 return (-1);
474 if (kctl_startup_boot() < 0)
475 return (-1);
477 kctl_dprintf("finished with kmdb initialization");
479 kctl_boot_tmpfini(old);
481 kctl.kctl_boot_ops = NULL;
483 return (0);
487 kctl_modload_activate(size_t memsz, const char *cfg, uint_t flags)
489 int rc;
491 mutex_enter(&kctl.kctl_lock);
493 if ((rc = kctl_state_check(kctl.kctl_state, KCTL_ST_INACTIVE)) != 0) {
494 if ((flags & KMDB_F_AUTO_ENTRY) && rc == EMDB_KACTIVE) {
495 kmdb_kdi_kmdb_enter();
496 rc = 0;
499 mutex_exit(&kctl.kctl_lock);
500 return (rc);
503 kctl.kctl_flags = flags;
505 if (memsz == 0)
506 memsz = KCTL_MEM_GOALSZ;
508 kctl.kctl_dseg = kdi_segdebugbase;
509 kctl.kctl_dseg_size =
510 memsz > kdi_segdebugsize ? kdi_segdebugsize : memsz;
511 kctl.kctl_memgoalsz = memsz;
513 if ((rc = kctl_dseg_alloc(kctl.kctl_dseg, kctl.kctl_dseg_size)) != 0)
514 goto activate_fail;
516 (void) kctl_set_state(KCTL_ST_DSEG_ALLOCED);
518 if ((rc = kctl_startup_preactivate(NULL, cfg, NULL)) != 0)
519 goto activate_fail;
521 kctl_startup_modules();
522 kctl_startup_thread();
524 if ((rc = kctl_startup_activate(0)) != 0)
525 goto activate_fail;
527 kctl_memavail(); /* Must be after kdi_dvec is set */
529 if (kctl.kctl_flags & KMDB_F_AUTO_ENTRY)
530 kmdb_kdi_kmdb_enter();
532 mutex_exit(&kctl.kctl_lock);
533 return (0);
535 activate_fail:
536 kctl_cleanup();
537 mutex_exit(&kctl.kctl_lock);
538 return (rc);
542 * This interface will be called when drv/kmdb loads. When we get the call, one
543 * of two things will have happened:
545 * 1. The debugger was loaded at boot. We've progressed far enough into boot
546 * as to allow drv/kmdb to be loaded as a non-primary. Invocation of this
547 * interface is the signal to the debugger that it can start allowing things
548 * like dmod loading and automatic CTF decompression - things which require
549 * the system services that have now been started.
551 * 2. The debugger was loaded after boot. mdb opened /dev/kmdb, causing
552 * drv/kmdb to load, followed by misc/kmdb. Nothing has been set up yet,
553 * so we need to initialize. Activation will occur separately, so we don't
554 * have to worry about that.
557 kctl_attach(dev_info_t *dip)
559 kctl.kctl_drv_dip = dip;
561 return (0);
565 kctl_detach(void)
567 return (kctl.kctl_state == KCTL_ST_INACTIVE ? 0 : EBUSY);
570 static struct modlmisc modlmisc = {
571 &mod_miscops,
572 KMDB_VERSION
575 static struct modlinkage modlinkage = {
576 MODREV_1,
577 (void *)&modlmisc,
578 NULL
582 * Invoked only when debugger is loaded via modload - not invoked when debugger
583 * is loaded at boot. kctl_boot_activate needs to call anything (aside from
584 * mod_install) this function does.
587 _init(void)
589 if (kctl_init() < 0)
590 return (EINVAL);
592 return (mod_install(&modlinkage));
596 _info(struct modinfo *modinfop)
598 return (mod_info(&modlinkage, modinfop));
602 _fini(void)
604 kctl_fini();
606 return (mod_remove(&modlinkage));