10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53446Smrj * Common Development and Distribution License (the "License").
63446Smrj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
223446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * The KDI is used to allow the kernel debugger to directly invoke various
300Sstevel@tonic-gate * kernel functions. In some cases, such as with kdi_mod_iter(), the
310Sstevel@tonic-gate * debugger needs to execute functions that use the kernel's linker bindings.
320Sstevel@tonic-gate * In other cases, the implementation of the KDI functions vary by platform
330Sstevel@tonic-gate * and/or by CPU. By embedding the implementation of these functions in
340Sstevel@tonic-gate * the platmod/cpumod, we can avoid the need for platform-specific knowledge
350Sstevel@tonic-gate * in the debugger, and can thus have a single debugger binary for all
360Sstevel@tonic-gate * platforms.
370Sstevel@tonic-gate *
380Sstevel@tonic-gate * There are three classes of KDI function:
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * 1. Normal - These are functions whose implementations are in the kernel for
410Sstevel@tonic-gate * convenience. An example is the modctl iterator, kdi_mod_iter. Using the
420Sstevel@tonic-gate * modules symbol, this function iterates through the kernel's modctl list,
430Sstevel@tonic-gate * invoking a debugger-provided callback for each one. This function is in
440Sstevel@tonic-gate * the KDI because the debugger needs to be able to execute it in order to
450Sstevel@tonic-gate * enable symbol resolution. Without symbol resolution, the debugger can't
460Sstevel@tonic-gate * locate the modules symbol. A chicken-and-egg problem results. We solve
470Sstevel@tonic-gate * this problem by locating the module iterator in the kernel, where run-time
480Sstevel@tonic-gate * linking solves the problem for us.
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * 2. CPU-specific - Functions in this class have implementations that differ
510Sstevel@tonic-gate * by CPU. For example, the crosscall delivery notification method differs
520Sstevel@tonic-gate * between Cheetah and Jalapeno, necessitating a different implementation for
530Sstevel@tonic-gate * each. By locating the KDI implementation of these functions in the
540Sstevel@tonic-gate * cpumods, we automatically get the correct implementation, as krtld
550Sstevel@tonic-gate * automatically loads the correct cpumod when it starts. The cpumods
560Sstevel@tonic-gate * directly fill in their portion of the kdi_t, using the mandatory
570Sstevel@tonic-gate * cpu_kdi_init cpumod entry point.
580Sstevel@tonic-gate *
590Sstevel@tonic-gate * 3. Platform-specific - Similar to the CPU-specific class, platform-specific
600Sstevel@tonic-gate * KDI functions have implementations that differ from platform to platform.
610Sstevel@tonic-gate * As such, the implementations live in the platmods. Further
620Sstevel@tonic-gate * differentiating the platform-specific KDI functions from their
630Sstevel@tonic-gate * CPU-dependent brethren, many directly invoke PROM functions. This poses
640Sstevel@tonic-gate * a problem, as the platmods use the kernel's promif functions, rather than
650Sstevel@tonic-gate * the lock-free kmdb versions. We provide an interposition layer for these
660Sstevel@tonic-gate * platform-specific calls that disables the pre- and post-processing
670Sstevel@tonic-gate * functions used by the kernel to implement kernel-specific functionality
680Sstevel@tonic-gate * that must not be executed when kmdb has control of the machine. Platmods
690Sstevel@tonic-gate * fill in a kdi_plat_t using their optional plat_kdi_init entry point.
700Sstevel@tonic-gate * krtld provides wrapper functions which suspend the necessary functions in
710Sstevel@tonic-gate * the promif layer before invoking the kdi_plat_t functions (if any).
720Sstevel@tonic-gate */
730Sstevel@tonic-gate
740Sstevel@tonic-gate #include <sys/types.h>
750Sstevel@tonic-gate #include <sys/systm.h>
760Sstevel@tonic-gate #include <sys/reboot.h>
770Sstevel@tonic-gate #include <sys/kdi_impl.h>
780Sstevel@tonic-gate
79*5648Ssetje #include <krtld/kobj_kdi.h>
800Sstevel@tonic-gate
810Sstevel@tonic-gate #define KOBJ_KDI_MOD_IDLE 0
820Sstevel@tonic-gate #define KOBJ_KDI_MOD_CHANGING 1
830Sstevel@tonic-gate #define KOBJ_KDI_MOD_CHANGED 2
840Sstevel@tonic-gate
850Sstevel@tonic-gate static int kobj_kdi_mod_state = KOBJ_KDI_MOD_IDLE;
860Sstevel@tonic-gate
870Sstevel@tonic-gate extern int standalone;
880Sstevel@tonic-gate
890Sstevel@tonic-gate cons_polledio_t *
kobj_kdi_get_polled_io(void)900Sstevel@tonic-gate kobj_kdi_get_polled_io(void)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate cons_polledio_t **polled_io = &cons_polledio;
930Sstevel@tonic-gate
940Sstevel@tonic-gate return (polled_io == NULL ? NULL : *polled_io);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate
970Sstevel@tonic-gate int
kobj_kdi_mod_iter(int (* func)(struct modctl *,void *),void * arg)980Sstevel@tonic-gate kobj_kdi_mod_iter(int (*func)(struct modctl *, void *), void *arg)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate int rc;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if (standalone) {
1030Sstevel@tonic-gate struct modctl_list *lp, **lpp;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) {
1060Sstevel@tonic-gate for (lp = *lpp; lp != NULL; lp = lp->modl_next) {
1070Sstevel@tonic-gate if ((rc = func(lp->modl_modp, arg)) != 0)
1080Sstevel@tonic-gate return (rc);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate } else {
1130Sstevel@tonic-gate struct modctl *modp = &modules;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate do {
1160Sstevel@tonic-gate if ((rc = func(modp, arg)) != 0)
1170Sstevel@tonic-gate return (rc);
1180Sstevel@tonic-gate } while ((modp = modp->mod_next) != &modules);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate return (0);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate int
kobj_kdi_mod_isloaded(struct modctl * modp)1250Sstevel@tonic-gate kobj_kdi_mod_isloaded(struct modctl *modp)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate return (modp->mod_mp != NULL);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate int
kobj_kdi_mods_changed(void)1310Sstevel@tonic-gate kobj_kdi_mods_changed(void)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate int state;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate if ((state = kobj_kdi_mod_state) == KOBJ_KDI_MOD_CHANGED)
1360Sstevel@tonic-gate kobj_kdi_mod_state = KOBJ_KDI_MOD_IDLE;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate return (state != KOBJ_KDI_MOD_IDLE);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate /*ARGSUSED1*/
1420Sstevel@tonic-gate void
kobj_kdi_mod_notify(uint_t why,struct modctl * what)1430Sstevel@tonic-gate kobj_kdi_mod_notify(uint_t why, struct modctl *what)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate switch (why) {
1460Sstevel@tonic-gate case KOBJ_NOTIFY_MODLOADING:
1470Sstevel@tonic-gate kobj_kdi_mod_state = KOBJ_KDI_MOD_CHANGING;
1480Sstevel@tonic-gate break;
1490Sstevel@tonic-gate case KOBJ_NOTIFY_MODLOADED:
1500Sstevel@tonic-gate kobj_kdi_mod_state = KOBJ_KDI_MOD_CHANGED;
1510Sstevel@tonic-gate if (boothowto & RB_DEBUG)
1520Sstevel@tonic-gate kdi_dvec_mod_loaded(what);
1530Sstevel@tonic-gate break;
1540Sstevel@tonic-gate case KOBJ_NOTIFY_MODUNLOADING:
1550Sstevel@tonic-gate kobj_kdi_mod_state = KOBJ_KDI_MOD_CHANGING;
1560Sstevel@tonic-gate if (boothowto & RB_DEBUG)
1570Sstevel@tonic-gate kdi_dvec_mod_unloading(what);
1580Sstevel@tonic-gate break;
1590Sstevel@tonic-gate case KOBJ_NOTIFY_MODUNLOADED:
1600Sstevel@tonic-gate kobj_kdi_mod_state = KOBJ_KDI_MOD_CHANGED;
1610Sstevel@tonic-gate break;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * Compare two modctl and module snapshots, attempting to determine whether
1670Sstevel@tonic-gate * the module to which they both refer has changed between the time of the first
1680Sstevel@tonic-gate * and the time of the second. We can't do a straight bcmp, because there are
1690Sstevel@tonic-gate * fields that change in the normal course of operations. False positives
1700Sstevel@tonic-gate * aren't the end of the world, but it'd be nice to avoid flagging a module
1710Sstevel@tonic-gate * as changed every time someone holds or releases it.
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate int
kobj_kdi_mod_haschanged(struct modctl * mc1,struct module * mp1,struct modctl * mc2,struct module * mp2)1740Sstevel@tonic-gate kobj_kdi_mod_haschanged(struct modctl *mc1, struct module *mp1,
1750Sstevel@tonic-gate struct modctl *mc2, struct module *mp2)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate if (mc1->mod_loadcnt != mc2->mod_loadcnt || mc1->mod_mp != mc2->mod_mp)
1780Sstevel@tonic-gate return (1);
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate if (mc1->mod_mp == NULL)
1810Sstevel@tonic-gate return (0);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /* Take breath here. */
1840Sstevel@tonic-gate return (bcmp(&mp1->hdr, &mp2->hdr, sizeof (mp1->hdr)) != 0 ||
1850Sstevel@tonic-gate mp1->symhdr != mp2->symhdr || mp1->strhdr != mp2->strhdr ||
1860Sstevel@tonic-gate mp1->text != mp2->text || mp1->bss != mp2->bss ||
1870Sstevel@tonic-gate mp1->ctfdata != mp2->ctfdata || mp1->ctfsize != mp2->ctfsize);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate void
kobj_kdi_system_claim(void)1910Sstevel@tonic-gate kobj_kdi_system_claim(void)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate kobj_kdi.kdi_plat_call(kobj_kdi.pkdi_system_claim);
1940Sstevel@tonic-gate kobj_kdi.kdi_plat_call(kobj_kdi.pkdi_console_claim);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate void
kobj_kdi_system_release(void)1980Sstevel@tonic-gate kobj_kdi_system_release(void)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate kobj_kdi.kdi_plat_call(kobj_kdi.pkdi_console_release);
2010Sstevel@tonic-gate kobj_kdi.kdi_plat_call(kobj_kdi.pkdi_system_release);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate void
kobj_kdi_init(void)2050Sstevel@tonic-gate kobj_kdi_init(void)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate static const char *const initializers[] = {
2080Sstevel@tonic-gate "cpu_kdi_init", "mach_kdi_init", "plat_kdi_init", NULL
2090Sstevel@tonic-gate };
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate Sym *sym;
2120Sstevel@tonic-gate int i;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate for (i = 0; initializers[i] != NULL; i++) {
2150Sstevel@tonic-gate if ((sym = kobj_lookup_kernel(initializers[i])) != NULL)
2160Sstevel@tonic-gate ((void (*)(kdi_t *))sym->st_value)(&kobj_kdi);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate kdi_t kobj_kdi = {
2210Sstevel@tonic-gate KDI_VERSION,
2220Sstevel@tonic-gate kobj_kdi_mods_changed,
2230Sstevel@tonic-gate kobj_kdi_mod_iter,
2240Sstevel@tonic-gate kobj_kdi_mod_isloaded,
2250Sstevel@tonic-gate kobj_kdi_mod_haschanged,
2260Sstevel@tonic-gate kobj_kdi_system_claim,
2270Sstevel@tonic-gate kobj_kdi_system_release,
2280Sstevel@tonic-gate kdi_pread,
2290Sstevel@tonic-gate kdi_pwrite,
2300Sstevel@tonic-gate kdi_flush_caches,
2310Sstevel@tonic-gate kdi_range_is_nontoxic,
2320Sstevel@tonic-gate kobj_kdi_get_polled_io,
2330Sstevel@tonic-gate kdi_vtop,
2340Sstevel@tonic-gate kdi_dtrace_get_state,
2350Sstevel@tonic-gate kdi_dtrace_set,
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate * The rest are filled in by cpu_kdi_init, mach_kdi_init, and/or
2380Sstevel@tonic-gate * plat_kdi_init.
2390Sstevel@tonic-gate */
2400Sstevel@tonic-gate NULL, /* kdi_plat_call */
2413446Smrj NULL, /* kdi_kmdb_enter */
2420Sstevel@tonic-gate { NULL }, /* kdi_arch */
2430Sstevel@tonic-gate { NULL } /* kdi_plat */
2440Sstevel@tonic-gate };
245