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