xref: /titanic_51/usr/src/uts/sparc/os/obpsym.c (revision f500b19684bd0346ac05bec02a50af07f369da1a)
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
5cee4d2b4Smb158278  * Common Development and Distribution License (the "License").
6cee4d2b4Smb158278  * 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  */
2175521904Sraf 
227c478bd9Sstevel@tonic-gate /*
23*f500b196SRichard Bean  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This module supports callbacks from the firmware
297c478bd9Sstevel@tonic-gate  * such that address and name lookups can work and use kernel symbol names.
307c478bd9Sstevel@tonic-gate  * For example "ctrace" will provide symbolic names, if they are available.
317c478bd9Sstevel@tonic-gate  * Also, normal firmware name to address lookups should work, though be
327c478bd9Sstevel@tonic-gate  * careful with clashing kernel names, such as "startup" and "reset" which
337c478bd9Sstevel@tonic-gate  * may be the firmware names and *not* the kernel names.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * The module locks the symbol tables in memory, when it's installed,
36cee4d2b4Smb158278  * and unlocks them when it is removed.  The module is loaded automatically
37cee4d2b4Smb158278  * on cobp systems and is replaced by forthdebug on all other systems.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * This file contains the actual code the does the lookups, and interfaces
407c478bd9Sstevel@tonic-gate  * with the kernel kobj stuff.  The transfer of data and control to/from
417c478bd9Sstevel@tonic-gate  * the firmware is handled in prom-dependent code.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/systm.h>
477c478bd9Sstevel@tonic-gate #include <sys/debug.h>
487c478bd9Sstevel@tonic-gate #include <sys/errno.h>
497c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
507c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
517c478bd9Sstevel@tonic-gate #include <sys/promif.h>
527c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
547c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
557c478bd9Sstevel@tonic-gate #include <sys/callb.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate int obpsym_debug = 0;
587c478bd9Sstevel@tonic-gate #define	DPRINTF(str)		if (obpsym_debug) prom_printf(str)
597c478bd9Sstevel@tonic-gate #define	DPRINTF1(str, a)	if (obpsym_debug) prom_printf(str, a);
607c478bd9Sstevel@tonic-gate #define	DPRINTF2(str, a, b)	if (obpsym_debug) prom_printf(str, a, b);
617c478bd9Sstevel@tonic-gate #define	DXPRINTF		if (obpsym_debug > 1) prom_printf
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate int obpheld = 1;		/* Prevents unloading when set */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * name_to_value - translate a string into an address
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * The string may be one of two forms - 'symname' or 'modname:symname'.
697c478bd9Sstevel@tonic-gate  * (We also accept 'unix:symname' as a synonymn for 'symname'.)
707c478bd9Sstevel@tonic-gate  * The latter form causes a kobj_lookup() to occur only in the given module,
717c478bd9Sstevel@tonic-gate  * the former causes a kobj_getsymvalue() on the entire kernel symbol table.
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * mod_lock locking is not needed for the &modules list because
747c478bd9Sstevel@tonic-gate  * modctl structures are never unlinked from the &modules list.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate int
777c478bd9Sstevel@tonic-gate name_to_value(char *name, uintptr_t *value)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	register char *symname = name;
807c478bd9Sstevel@tonic-gate 	register char *modname = "";
817c478bd9Sstevel@tonic-gate 	char *p;
827c478bd9Sstevel@tonic-gate 	int retval = 0;
837c478bd9Sstevel@tonic-gate 	uintptr_t symvalue = 0;
847c478bd9Sstevel@tonic-gate 	char c = (char)0;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * we take names of the form: "modname:symbol", "unix:symbol", "symbol"
887c478bd9Sstevel@tonic-gate 	 */
897c478bd9Sstevel@tonic-gate 	if ((p = strchr(name, ':')) != NULL && p[1] != (char)0) {
907c478bd9Sstevel@tonic-gate 		symname = p + 1;
917c478bd9Sstevel@tonic-gate 		modname = name;
927c478bd9Sstevel@tonic-gate 		c = *p;
937c478bd9Sstevel@tonic-gate 		*p = (char)0;
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (*modname == (char)0) {
977c478bd9Sstevel@tonic-gate 		symvalue = kobj_getsymvalue(symname, 0);
987c478bd9Sstevel@tonic-gate 	} else  {
997c478bd9Sstevel@tonic-gate 		struct modctl *mp = &modules;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		do {
1027c478bd9Sstevel@tonic-gate 			if (strcmp(modname, mp->mod_modname) == 0) {
1037c478bd9Sstevel@tonic-gate 				symvalue = kobj_lookup(mp->mod_mp, symname);
1047c478bd9Sstevel@tonic-gate 				break;
1057c478bd9Sstevel@tonic-gate 			}
1067c478bd9Sstevel@tonic-gate 		} while ((mp = mp->mod_next) != &modules);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if (symvalue == 0)
1107c478bd9Sstevel@tonic-gate 		retval = -1;
1117c478bd9Sstevel@tonic-gate 	if (c != (char)0)		/* Restore incoming cstr */
1127c478bd9Sstevel@tonic-gate 		*p = c;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	*value = symvalue;
1157c478bd9Sstevel@tonic-gate 	return (retval);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * value_to_name - translate an address into a string + offset
1207c478bd9Sstevel@tonic-gate  *
1217c478bd9Sstevel@tonic-gate  * mod_lock locking is not needed fro the modules list because
1227c478bd9Sstevel@tonic-gate  * modctl structures are never unlinked from the &modules list.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate ulong_t
1257c478bd9Sstevel@tonic-gate value_to_name(uintptr_t value, char *symbol)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	struct modctl *modp = &modules;
1287c478bd9Sstevel@tonic-gate 	ulong_t offset;
1297c478bd9Sstevel@tonic-gate 	char *name;
1307c478bd9Sstevel@tonic-gate 
13175521904Sraf 	DPRINTF1("value_to_name: Looking for %p\n", (void *)value);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	do {
1347c478bd9Sstevel@tonic-gate 		if (modp->mod_mp &&
1357c478bd9Sstevel@tonic-gate 		    (name = kobj_searchsym(modp->mod_mp, value, &offset))) {
1367c478bd9Sstevel@tonic-gate 			(void) strcpy(symbol, modp->mod_modname);
1377c478bd9Sstevel@tonic-gate 			(void) strcat(symbol, ":");
1387c478bd9Sstevel@tonic-gate 			(void) strcat(symbol, name);
1397c478bd9Sstevel@tonic-gate 			return (offset);
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 	} while ((modp = modp->mod_next) != &modules);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	*symbol = (char)0;
1447c478bd9Sstevel@tonic-gate 	return ((ulong_t)-1l);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * loadable module wrapper
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
151*f500b196SRichard Bean 	&mod_miscops, "OBP symbol callbacks"
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1557c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
1567c478bd9Sstevel@tonic-gate };
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1597c478bd9Sstevel@tonic-gate static boolean_t
1607c478bd9Sstevel@tonic-gate reset_callbacks(void *arg, int code)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	extern void set_sym_callbacks();
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (code == CB_CODE_CPR_RESUME)
1657c478bd9Sstevel@tonic-gate 		set_sym_callbacks();
1667c478bd9Sstevel@tonic-gate 	return (B_TRUE);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate int
1707c478bd9Sstevel@tonic-gate _init(void)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	int retval;
1737c478bd9Sstevel@tonic-gate 	extern int install_callbacks(void);
1747c478bd9Sstevel@tonic-gate 	extern void remove_callbacks(void);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (install_callbacks() != 0)
1777c478bd9Sstevel@tonic-gate 		return (ENXIO);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (boothowto & RB_HALT)
1807c478bd9Sstevel@tonic-gate 		debug_enter("obpsym: halt flag (-h) is set.\n");
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	retval = mod_install(&modlinkage);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * if load fails remove callback and unlock symbols
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 	if (retval) {
1887c478bd9Sstevel@tonic-gate 		printf("obpsym: Error %d installing OBP syms module\n", retval);
1897c478bd9Sstevel@tonic-gate 		remove_callbacks();
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	else
1927c478bd9Sstevel@tonic-gate 		(void) callb_add(reset_callbacks, 0, CB_CL_CPR_OBP, "obpsym");
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	return (retval);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate int
1987c478bd9Sstevel@tonic-gate _fini(void)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	int retval;
2017c478bd9Sstevel@tonic-gate 	extern void remove_callbacks(void);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (obpheld != 0)
2047c478bd9Sstevel@tonic-gate 		return (EBUSY);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	retval = mod_remove(&modlinkage);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * if unload succeeds remove callback and unlock symbols
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	if (retval == 0) {
2127c478bd9Sstevel@tonic-gate 		remove_callbacks();
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 	return (retval);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate int
2187c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2217c478bd9Sstevel@tonic-gate }
222