xref: /illumos-gate/usr/src/cmd/mdb/common/kmdb/kmdb_kdi.c (revision ae115bc7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*ae115bc7Smrj  * Common Development and Distribution License (the "License").
6*ae115bc7Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * The KDI, or kernel/debugger interface, is used to allow the kernel and the
307c478bd9Sstevel@tonic-gate  * debugger to communicate.  These communications take two forms:
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  *  1. kernel to debugger.  Interfaces of this type are used by the kernel to
337c478bd9Sstevel@tonic-gate  *     inform the debugger of changes in the state of the system that need to
347c478bd9Sstevel@tonic-gate  *     be noted by the debugger.  For example, the kernel uses one of these
357c478bd9Sstevel@tonic-gate  *     interfaces to tell debugger that the set of currently-loaded modules
367c478bd9Sstevel@tonic-gate  *     has changed.
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  *  2. debugger to kernel.  Interfaces of this type are used by the debugger
397c478bd9Sstevel@tonic-gate  *     to extract information from the kernel that would otherwise be difficult
407c478bd9Sstevel@tonic-gate  *     to get, or to perform services that are specific to the machine being
417c478bd9Sstevel@tonic-gate  *     used.  An example of the former is the module iterator, which is needed
427c478bd9Sstevel@tonic-gate  *     to allow symbol resolution, but which needs to resolve symbols prior
437c478bd9Sstevel@tonic-gate  *     to the iteration.  The latter class include machine-specific or
447c478bd9Sstevel@tonic-gate  *     cpu-type-specific functions, such as the I-cache flusher.  By directly
457c478bd9Sstevel@tonic-gate  *     using the kernel versions of these functions, we avoid the need to
467c478bd9Sstevel@tonic-gate  *     include multiple versions of each function - one per cpu and/or machine -
477c478bd9Sstevel@tonic-gate  *     in kmdb.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h>
517c478bd9Sstevel@tonic-gate 
52*ae115bc7Smrj #include <kmdb/kmdb_kdi.h>
537c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_dpi.h>
547c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_kvm.h>
557c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_promif.h>
567c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
577c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
587c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static int kdi_unload_request;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate typedef struct mod_interp_data {
637c478bd9Sstevel@tonic-gate 	int	(*mid_usercb)(struct modctl *, void *);
647c478bd9Sstevel@tonic-gate 	void	*mid_userarg;
657c478bd9Sstevel@tonic-gate 	jmp_buf mid_pcb;
667c478bd9Sstevel@tonic-gate 	jmp_buf *mid_oldpcb;
677c478bd9Sstevel@tonic-gate } mod_interp_data_t;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static kmdb_auxv_t *kdi_auxv;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate int
kmdb_kdi_mods_changed(void)727c478bd9Sstevel@tonic-gate kmdb_kdi_mods_changed(void)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	return (mdb.m_kdi->kdi_mods_changed());
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int
kmdb_kdi_mod_interp(struct modctl * mp,void * arg)787c478bd9Sstevel@tonic-gate kmdb_kdi_mod_interp(struct modctl *mp, void *arg)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	mod_interp_data_t *mid = arg;
817c478bd9Sstevel@tonic-gate 	int rc;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	kmdb_dpi_restore_fault_hdlr(mid->mid_oldpcb);
847c478bd9Sstevel@tonic-gate 	rc = mid->mid_usercb(mp, mid->mid_userarg);
857c478bd9Sstevel@tonic-gate 	mid->mid_oldpcb = kmdb_dpi_set_fault_hdlr(&mid->mid_pcb);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	return (rc);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * We need to protect ourselves against any problems that may occur while
927c478bd9Sstevel@tonic-gate  * executing the module iterator, currently located in krtld.  If, for
937c478bd9Sstevel@tonic-gate  * example, one of the next pointers in the module list points to an invalid
947c478bd9Sstevel@tonic-gate  * address, we don't want kmdb to explode.  As such, we protect ourselves
957c478bd9Sstevel@tonic-gate  * with the DPI fault-protection routines.  We don't want our fault-protection
967c478bd9Sstevel@tonic-gate  * callback to protect the callback that the kmdb consumer provided, so we
977c478bd9Sstevel@tonic-gate  * provide our own interposition callback that removes our fault-protector
987c478bd9Sstevel@tonic-gate  * before invoking the user's callback.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate int
kmdb_kdi_mod_iter(int (* cb)(struct modctl *,void *),void * arg)1017c478bd9Sstevel@tonic-gate kmdb_kdi_mod_iter(int (*cb)(struct modctl *, void *), void *arg)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	mod_interp_data_t mid;
1047c478bd9Sstevel@tonic-gate 	int rc;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (setjmp(mid.mid_pcb) != 0) {
1077c478bd9Sstevel@tonic-gate 		/* We took a fault while iterating through the modules */
1087c478bd9Sstevel@tonic-gate 		kmdb_dpi_restore_fault_hdlr(mid.mid_oldpcb);
1097c478bd9Sstevel@tonic-gate 		return (-1);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	mid.mid_usercb = cb;
1137c478bd9Sstevel@tonic-gate 	mid.mid_userarg = arg;
1147c478bd9Sstevel@tonic-gate 	mid.mid_oldpcb = kmdb_dpi_set_fault_hdlr(&mid.mid_pcb);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	rc = mdb.m_kdi->kdi_mod_iter(kmdb_kdi_mod_interp, &mid);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	kmdb_dpi_restore_fault_hdlr(mid.mid_oldpcb);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	return (rc);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate int
kmdb_kdi_mod_isloaded(struct modctl * modp)1247c478bd9Sstevel@tonic-gate kmdb_kdi_mod_isloaded(struct modctl *modp)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	return (mdb.m_kdi->kdi_mod_isloaded(modp));
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate int
kmdb_kdi_mod_haschanged(struct modctl * mc1,struct module * mp1,struct modctl * mc2,struct module * mp2)1307c478bd9Sstevel@tonic-gate kmdb_kdi_mod_haschanged(struct modctl *mc1, struct module *mp1,
1317c478bd9Sstevel@tonic-gate     struct modctl *mc2, struct module *mp2)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	return (mdb.m_kdi->kdi_mod_haschanged(mc1, mp1, mc2, mp2));
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static ssize_t
kdi_prw(void * buf,size_t nbytes,physaddr_t addr,int (* rw)(caddr_t,size_t,physaddr_t,size_t *))1377c478bd9Sstevel@tonic-gate kdi_prw(void *buf, size_t nbytes, physaddr_t addr, int (*rw)(caddr_t, size_t,
1387c478bd9Sstevel@tonic-gate     physaddr_t, size_t *))
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	size_t sz;
1417c478bd9Sstevel@tonic-gate 	int rc;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	kmdb_dpi_flush_slave_caches();
1447c478bd9Sstevel@tonic-gate 	if ((rc = rw(buf, nbytes, addr, &sz)) != 0)
1457c478bd9Sstevel@tonic-gate 		return (set_errno(rc));
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	return (sz);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate ssize_t
kmdb_kdi_pread(void * buf,size_t nbytes,physaddr_t addr)1517c478bd9Sstevel@tonic-gate kmdb_kdi_pread(void *buf, size_t nbytes, physaddr_t addr)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	return (kdi_prw(buf, nbytes, addr, mdb.m_kdi->kdi_pread));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate ssize_t
kmdb_kdi_pwrite(void * buf,size_t nbytes,physaddr_t addr)1577c478bd9Sstevel@tonic-gate kmdb_kdi_pwrite(void *buf, size_t nbytes, physaddr_t addr)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	return (kdi_prw(buf, nbytes, addr, mdb.m_kdi->kdi_pwrite));
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate void
kmdb_kdi_flush_caches(void)1637c478bd9Sstevel@tonic-gate kmdb_kdi_flush_caches(void)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	mdb.m_kdi->kdi_flush_caches();
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate int
kmdb_kdi_get_unload_request(void)1697c478bd9Sstevel@tonic-gate kmdb_kdi_get_unload_request(void)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	return (kdi_unload_request);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate void
kmdb_kdi_set_unload_request(void)1757c478bd9Sstevel@tonic-gate kmdb_kdi_set_unload_request(void)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	kdi_unload_request = 1;
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate int
kmdb_kdi_get_flags(void)1817c478bd9Sstevel@tonic-gate kmdb_kdi_get_flags(void)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	uint_t flags = 0;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (mdb.m_flags & MDB_FL_NOCTF)
1867c478bd9Sstevel@tonic-gate 		flags |= KMDB_KDI_FL_NOCTF;
1877c478bd9Sstevel@tonic-gate 	if (mdb.m_flags & MDB_FL_NOMODS)
1887c478bd9Sstevel@tonic-gate 		flags |= KMDB_KDI_FL_NOMODS;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (flags);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate size_t
kmdb_kdi_range_is_nontoxic(uintptr_t va,size_t sz,int write)1947c478bd9Sstevel@tonic-gate kmdb_kdi_range_is_nontoxic(uintptr_t va, size_t sz, int write)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	return (mdb.m_kdi->kdi_range_is_nontoxic(va, sz, write));
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate void
kmdb_kdi_system_claim(void)2007c478bd9Sstevel@tonic-gate kmdb_kdi_system_claim(void)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	(void) kmdb_dpi_call((uintptr_t)mdb.m_kdi->kdi_system_claim, 0, NULL);
2037c478bd9Sstevel@tonic-gate 	kmdb_prom_debugger_entry();
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate void
kmdb_kdi_system_release(void)2077c478bd9Sstevel@tonic-gate kmdb_kdi_system_release(void)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	kmdb_prom_debugger_exit();
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (mdb.m_kdi->kdi_system_release != NULL) {
2127c478bd9Sstevel@tonic-gate 		(void) kmdb_dpi_call((uintptr_t)mdb.m_kdi->kdi_system_release,
2137c478bd9Sstevel@tonic-gate 		    0, NULL);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate struct cons_polledio *
kmdb_kdi_get_polled_io(void)2187c478bd9Sstevel@tonic-gate kmdb_kdi_get_polled_io(void)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	return (mdb.m_kdi->kdi_get_polled_io());
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
223*ae115bc7Smrj void
kmdb_kdi_kmdb_enter(void)224*ae115bc7Smrj kmdb_kdi_kmdb_enter(void)
225*ae115bc7Smrj {
226*ae115bc7Smrj 	mdb.m_kdi->kdi_kmdb_enter();
227*ae115bc7Smrj }
228*ae115bc7Smrj 
2297c478bd9Sstevel@tonic-gate int
kmdb_kdi_vtop(uintptr_t va,physaddr_t * pap)2307c478bd9Sstevel@tonic-gate kmdb_kdi_vtop(uintptr_t va, physaddr_t *pap)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	jmp_buf pcb, *oldpcb;
2337c478bd9Sstevel@tonic-gate 	int rc = 0;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (setjmp(pcb) == 0) {
2367c478bd9Sstevel@tonic-gate 		int err;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		oldpcb = kmdb_dpi_set_fault_hdlr(&pcb);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		if ((err = mdb.m_kdi->kdi_vtop(va, pap)) != 0)
2417c478bd9Sstevel@tonic-gate 			rc = set_errno(err == ENOENT ? EMDB_NOMAP : err);
2427c478bd9Sstevel@tonic-gate 	} else {
2437c478bd9Sstevel@tonic-gate 		/* We faulted during the translation */
2447c478bd9Sstevel@tonic-gate 		rc = set_errno(EMDB_NOMAP);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	kmdb_dpi_restore_fault_hdlr(oldpcb);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	return (rc);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate kdi_dtrace_state_t
kmdb_kdi_dtrace_get_state(void)2537c478bd9Sstevel@tonic-gate kmdb_kdi_dtrace_get_state(void)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	return (mdb.m_kdi->kdi_dtrace_get_state());
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate int
kmdb_kdi_dtrace_set(int state)2597c478bd9Sstevel@tonic-gate kmdb_kdi_dtrace_set(int state)
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	int err;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if ((err = mdb.m_kdi->kdi_dtrace_set(state)) != 0)
2647c478bd9Sstevel@tonic-gate 		return (set_errno(err));
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	return (0);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * This function is to be called only during kmdb initialization, as it
2717c478bd9Sstevel@tonic-gate  * uses the running kernel for symbol translation facilities.
2727c478bd9Sstevel@tonic-gate  */
2737c478bd9Sstevel@tonic-gate uintptr_t
kmdb_kdi_lookup_by_name(char * modname,char * symname)2747c478bd9Sstevel@tonic-gate kmdb_kdi_lookup_by_name(char *modname, char *symname)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	ASSERT(kmdb_dpi_get_state(NULL) == DPI_STATE_INIT);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	return (kdi_auxv->kav_lookup_by_name(modname, symname));
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate void
kmdb_kdi_init(kdi_t * kdi,kmdb_auxv_t * kav)2827c478bd9Sstevel@tonic-gate kmdb_kdi_init(kdi_t *kdi, kmdb_auxv_t *kav)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	mdb.m_kdi = kdi;
2857c478bd9Sstevel@tonic-gate 	mdb.m_pagesize = kav->kav_pagesize;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	kdi_unload_request = 0;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	kdi_auxv = kav;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	kmdb_kdi_init_isadep(kdi, kav);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate void
kmdb_kdi_end_init(void)2957c478bd9Sstevel@tonic-gate kmdb_kdi_end_init(void)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	kdi_auxv = NULL;
2987c478bd9Sstevel@tonic-gate }
299