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]
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>
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>
47 #define KCTL_EXECNAME "/kernel/drv/kmdb"
50 #define KCTL_MEM_GOALSZ (20 * 1024 * 1024)
52 #define KCTL_MEM_GOALSZ (10 * 1024 * 1024)
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.
64 #define KCTL_PROM_LOCK promif_preprom()
65 #define KCTL_PROM_UNLOCK promif_postprom()
67 #define KCTL_PROM_LOCK
68 #define KCTL_PROM_UNLOCK
74 if (kobj_kdi
.kdi_version
!= KDI_VERSION
) {
75 kctl_warn("kmdb/kernel version mismatch (expected %d, "
76 "found %d)", KDI_VERSION
, kobj_kdi
.kdi_version
);
80 sema_init(&kctl
.kctl_wr_avail_sem
, 0, NULL
, SEMA_DRIVER
, NULL
);
81 mutex_init(&kctl
.kctl_wr_lock
, NULL
, MUTEX_DRIVER
, NULL
);
82 cv_init(&kctl
.kctl_wr_cv
, NULL
, CV_DRIVER
, NULL
);
83 mutex_init(&kctl
.kctl_lock
, NULL
, MUTEX_DRIVER
, NULL
);
85 kctl
.kctl_execname
= KCTL_EXECNAME
; /* XXX get from modctl? */
87 kctl
.kctl_state
= KCTL_ST_INACTIVE
;
89 kctl
.kctl_dseg
= kctl
.kctl_mrbase
= NULL
;
90 kctl
.kctl_dseg_size
= kctl
.kctl_mrsize
= 0;
102 mutex_destroy(&kctl
.kctl_lock
);
103 cv_destroy(&kctl
.kctl_wr_cv
);
104 mutex_destroy(&kctl
.kctl_wr_lock
);
105 sema_destroy(&kctl
.kctl_wr_avail_sem
);
109 kctl_set_state(uint_t state
)
111 uint_t ostate
= kctl
.kctl_state
;
113 /* forward progess only, please */
114 if (state
> ostate
) {
115 kctl_dprintf("new kctl state: %d", state
);
116 kctl
.kctl_state
= state
;
123 kctl_boot_dseg_alloc(caddr_t dsegaddr
, size_t dsegsz
)
126 * The Intel boot memory allocator will cleverly map us onto a 4M
127 * page if we request the whole 4M Intel segment at once. This
128 * will break physical memory r/w, so we break the request into
129 * chunks. The allocator isn't smart enough to combine requests,
130 * so it'll give us a bunch of 4k pages.
132 while (dsegsz
>= 1024*1024) {
133 size_t sz
= MIN(dsegsz
, 1024*1024);
135 if (BOP_ALLOC(kctl
.kctl_boot_ops
, dsegaddr
, sz
, BO_NO_ALIGN
) !=
147 kctl_dseg_alloc(caddr_t addr
, size_t sz
)
149 ASSERT(((uintptr_t)addr
& PAGEOFFSET
) == 0);
151 /* make sure there isn't something there already (like kadb) */
152 if (hat_getpfnum(kas
.a_hat
, addr
) != PFN_INVALID
)
155 if (segkmem_xalloc(NULL
, addr
, sz
, VM_NOSLEEP
, 0, segkmem_page_create
,
163 kctl_dseg_free(caddr_t addr
, size_t sz
)
165 ASSERT(((uintptr_t)addr
& PAGEOFFSET
) == 0);
167 segkmem_free(NULL
, addr
, sz
);
177 * We're now free to allocate the non-fixed portion of the debugger's
181 needed
= P2ROUNDUP(kctl
.kctl_memgoalsz
<= kctl
.kctl_dseg_size
? 0 :
182 kctl
.kctl_memgoalsz
- kctl
.kctl_dseg_size
, PAGESIZE
);
187 if ((base
= kmem_zalloc(needed
, KM_NOSLEEP
)) == NULL
) {
189 * If we're going to wedge the machine during debugger startup,
190 * at least let them know why it's going to wedge.
192 cmn_err(CE_WARN
, "retrying of kmdb allocation of 0x%lx bytes",
195 base
= kmem_zalloc(needed
, KM_SLEEP
);
198 kdi_dvec
->dv_memavail(base
, needed
);
199 kctl
.kctl_mrbase
= base
;
200 kctl
.kctl_mrsize
= needed
;
206 uint_t state
= kctl_set_state(KCTL_ST_DEACTIVATING
);
208 kctl_dprintf("cleaning up from state %d", state
);
210 ASSERT(kctl
.kctl_boot_loaded
== 0);
214 boothowto
&= ~RB_DEBUG
;
215 /* XXX there's a race here */
219 case KCTL_ST_DBG_ACTIVATED
:
225 case KCTL_ST_THREAD_STARTED
:
226 if (curthread
!= kctl
.kctl_wr_thr
) {
232 case KCTL_ST_MOD_NOTIFIERS
:
233 kctl_mod_notify_unreg();
236 case KCTL_ST_KCTL_PREACTIVATED
:
237 kctl_depreactivate_isadep();
240 case KCTL_ST_INITIALIZED
:
241 /* There's no kmdb_fini */
242 case KCTL_ST_DSEG_ALLOCED
:
243 kctl_dseg_free(kctl
.kctl_dseg
, kctl
.kctl_dseg_size
);
245 if (kctl
.kctl_mrbase
!= NULL
)
246 kmem_free(kctl
.kctl_mrbase
, kctl
.kctl_mrsize
);
250 kctl
.kctl_state
= KCTL_ST_INACTIVE
;
254 kctl_startup_modules(void)
259 * Normal module load and unload is now available. Prior to this point,
260 * we could only load modules, and that only when the debugger was being
263 * We'll need to prepare the modules we've already loaded (if any) for
264 * the brave new world in which boot is unmapped.
269 * Process any outstanding loads or unloads and prepare for automatic
270 * module loading and unloading.
272 (void) kctl_wr_process();
274 kctl_mod_notify_reg();
276 (void) kctl_set_state(KCTL_ST_MOD_NOTIFIERS
);
280 kctl_mod_loaded(modp
);
281 } while ((modp
= modp
->mod_next
) != &modules
);
285 kctl_startup_thread(void)
288 * Create the worker thread, which will handle future requests from the
293 (void) kctl_set_state(KCTL_ST_THREAD_STARTED
);
297 kctl_startup_boot(void)
299 struct modctl_list
*lp
, **lpp
;
302 if (kctl_wr_process() < 0) {
303 kctl_warn("kmdb: failed to load modules");
307 mutex_enter(&mod_lock
);
309 for (lpp
= kobj_linkmaps
; *lpp
!= NULL
; lpp
++) {
310 for (lp
= *lpp
; lp
!= NULL
; lp
= lp
->modl_next
) {
311 if ((rc
= kctl_mod_decompress(lp
->modl_modp
)) != 0) {
312 kctl_warn("kmdb: failed to decompress CTF data "
313 "for %s: %s", lp
->modl_modp
->mod_modname
,
319 mutex_exit(&mod_lock
);
325 kctl_startup_preactivate(void *romp
, const char *cfg
, const char **argv
)
330 kctl_auxv_init(&kav
, cfg
, argv
, romp
);
332 rc
= kmdb_init(kctl
.kctl_execname
, &kav
);
334 kctl_auxv_fini(&kav
);
337 return (EMDB_KNOLOAD
);
339 (void) kctl_set_state(KCTL_ST_INITIALIZED
);
341 if (kctl_preactivate_isadep() != 0)
344 (void) kctl_set_state(KCTL_ST_KCTL_PREACTIVATED
);
350 kctl_startup_activate(uint_t flags
)
352 kdi_debugvec_t
*dvec
;
355 kmdb_activate(&dvec
, flags
);
358 (void) kctl_set_state(KCTL_ST_DBG_ACTIVATED
);
361 * fill in a few remaining debugvec entries.
363 dvec
->dv_kctl_modavail
= kctl_startup_modules
;
364 dvec
->dv_kctl_thravail
= kctl_startup_thread
;
365 dvec
->dv_kctl_memavail
= kctl_memavail
;
367 kctl_activate_isadep(dvec
);
372 boothowto
|= RB_DEBUG
;
374 (void) kctl_set_state(KCTL_ST_ACTIVE
);
380 kctl_state_check(uint_t state
, uint_t ok_state
)
382 if (state
== ok_state
)
385 if (state
== KCTL_ST_INACTIVE
)
386 return (EMDB_KINACTIVE
);
387 else if (kctl
.kctl_state
> KCTL_ST_INACTIVE
&&
388 kctl
.kctl_state
< KCTL_ST_ACTIVE
)
389 return (EMDB_KACTIVATING
);
390 else if (kctl
.kctl_state
== KCTL_ST_ACTIVE
)
391 return (EMDB_KACTIVE
);
392 else if (kctl
.kctl_state
== KCTL_ST_DEACTIVATING
)
393 return (EMDB_KDEACTIVATING
);
399 kctl_deactivate(void)
403 mutex_enter(&kctl
.kctl_lock
);
405 if (kctl
.kctl_boot_loaded
) {
407 goto deactivate_done
;
410 if ((rc
= kctl_state_check(kctl
.kctl_state
, KCTL_ST_ACTIVE
)) != 0)
411 goto deactivate_done
;
413 kmdb_kdi_set_unload_request();
414 kmdb_kdi_kmdb_enter();
417 * The debugger will pass the request to the work thread, which will
423 mutex_exit(&kctl
.kctl_lock
);
429 * Called from krtld, this indicates that the user loaded kmdb at boot. We
430 * track activation states, but we don't attempt to clean up if activation
431 * fails, because boot debugger load failures are fatal.
433 * Further complicating matters, various kernel routines, such as bcopy and
434 * mutex_enter, assume the presence of some basic state. On SPARC, it's the
435 * presence of a valid curthread pointer. On AMD64, it's a valid curcpu
436 * pointer in GSBASE. We set up temporary versions of these before beginning
437 * activation, and tear them down when we're done.
440 kctl_boot_activate(struct bootops
*ops
, void *romp
, size_t memsz
,
448 * krtld does a name-based symbol lookup to find this routine. It then
449 * casts the address it gets, calling the result. We want to make sure
450 * that the call in krtld stays in sync with the prototype for this
451 * function, so we define a type (kctl_boot_activate_f) that matches the
452 * current prototype. The following assignment ensures that the type
453 * still matches the declaration, with lint as the enforcer.
455 kctl_boot_activate_f
*kba
= kctl_boot_activate
;
456 if (kba
== NULL
) /* Make lint think kba is actually used */
461 old
= kctl_boot_tmpinit(); /* Set up temporary state */
464 kctl
.kctl_boot_ops
= ops
; /* must be set before kctl_init */
469 kctl
.kctl_boot_loaded
= 1;
471 kctl_dprintf("beginning kmdb initialization");
474 memsz
= KCTL_MEM_GOALSZ
;
476 kctl
.kctl_dseg
= kdi_segdebugbase
;
477 kctl
.kctl_dseg_size
=
478 memsz
> kdi_segdebugsize
? kdi_segdebugsize
: memsz
;
479 kctl
.kctl_memgoalsz
= memsz
;
481 if (kctl_boot_dseg_alloc(kctl
.kctl_dseg
, kctl
.kctl_dseg_size
) < 0) {
482 kctl_warn("kmdb: failed to allocate %lu-byte debugger area at "
483 "%p", kctl
.kctl_dseg_size
, (void *)kctl
.kctl_dseg
);
487 (void) kctl_set_state(KCTL_ST_DSEG_ALLOCED
);
489 if (kctl_startup_preactivate(romp
, NULL
, argv
) != 0 ||
490 kctl_startup_activate(KMDB_ACT_F_BOOT
)) {
491 kctl_warn("kmdb: failed to activate");
495 if (kctl_startup_boot() < 0)
498 kctl_dprintf("finished with kmdb initialization");
500 kctl_boot_tmpfini(old
);
502 kctl
.kctl_boot_ops
= NULL
;
508 kctl_modload_activate(size_t memsz
, const char *cfg
, uint_t flags
)
512 mutex_enter(&kctl
.kctl_lock
);
514 if ((rc
= kctl_state_check(kctl
.kctl_state
, KCTL_ST_INACTIVE
)) != 0) {
515 if ((flags
& KMDB_F_AUTO_ENTRY
) && rc
== EMDB_KACTIVE
) {
516 kmdb_kdi_kmdb_enter();
520 mutex_exit(&kctl
.kctl_lock
);
524 kctl
.kctl_flags
= flags
;
527 memsz
= KCTL_MEM_GOALSZ
;
529 kctl
.kctl_dseg
= kdi_segdebugbase
;
530 kctl
.kctl_dseg_size
=
531 memsz
> kdi_segdebugsize
? kdi_segdebugsize
: memsz
;
532 kctl
.kctl_memgoalsz
= memsz
;
534 if ((rc
= kctl_dseg_alloc(kctl
.kctl_dseg
, kctl
.kctl_dseg_size
)) != 0)
537 (void) kctl_set_state(KCTL_ST_DSEG_ALLOCED
);
539 if ((rc
= kctl_startup_preactivate(NULL
, cfg
, NULL
)) != 0)
542 kctl_startup_modules();
543 kctl_startup_thread();
545 if ((rc
= kctl_startup_activate(0)) != 0)
548 kctl_memavail(); /* Must be after kdi_dvec is set */
550 if (kctl
.kctl_flags
& KMDB_F_AUTO_ENTRY
)
551 kmdb_kdi_kmdb_enter();
553 mutex_exit(&kctl
.kctl_lock
);
558 mutex_exit(&kctl
.kctl_lock
);
563 * This interface will be called when drv/kmdb loads. When we get the call, one
564 * of two things will have happened:
566 * 1. The debugger was loaded at boot. We've progressed far enough into boot
567 * as to allow drv/kmdb to be loaded as a non-primary. Invocation of this
568 * interface is the signal to the debugger that it can start allowing things
569 * like dmod loading and automatic CTF decompression - things which require
570 * the system services that have now been started.
572 * 2. The debugger was loaded after boot. mdb opened /dev/kmdb, causing
573 * drv/kmdb to load, followed by misc/kmdb. Nothing has been set up yet,
574 * so we need to initialize. Activation will occur separately, so we don't
575 * have to worry about that.
578 kctl_attach(dev_info_t
*dip
)
580 kctl
.kctl_drv_dip
= dip
;
588 return (kctl
.kctl_state
== KCTL_ST_INACTIVE
? 0 : EBUSY
);
591 static struct modlmisc modlmisc
= {
596 static struct modlinkage modlinkage
= {
603 * Invoked only when debugger is loaded via modload - not invoked when debugger
604 * is loaded at boot. kctl_boot_activate needs to call anything (aside from
605 * mod_install) this function does.
613 return (mod_install(&modlinkage
));
617 _info(struct modinfo
*modinfop
)
619 return (mod_info(&modlinkage
, modinfop
));
627 return (mod_remove(&modlinkage
));