xref: /onnv-gate/usr/src/uts/sparc/os/obpsym.c (revision 7799:05fc7b266484)
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
55493Smb158278  * Common Development and Distribution License (the "License").
65493Smb158278  * 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  */
211048Sraf 
220Sstevel@tonic-gate /*
23*7799SRichard.Bean@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * This module supports callbacks from the firmware
290Sstevel@tonic-gate  * such that address and name lookups can work and use kernel symbol names.
300Sstevel@tonic-gate  * For example "ctrace" will provide symbolic names, if they are available.
310Sstevel@tonic-gate  * Also, normal firmware name to address lookups should work, though be
320Sstevel@tonic-gate  * careful with clashing kernel names, such as "startup" and "reset" which
330Sstevel@tonic-gate  * may be the firmware names and *not* the kernel names.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * The module locks the symbol tables in memory, when it's installed,
365493Smb158278  * and unlocks them when it is removed.  The module is loaded automatically
375493Smb158278  * on cobp systems and is replaced by forthdebug on all other systems.
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * This file contains the actual code the does the lookups, and interfaces
400Sstevel@tonic-gate  * with the kernel kobj stuff.  The transfer of data and control to/from
410Sstevel@tonic-gate  * the firmware is handled in prom-dependent code.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <sys/param.h>
460Sstevel@tonic-gate #include <sys/systm.h>
470Sstevel@tonic-gate #include <sys/debug.h>
480Sstevel@tonic-gate #include <sys/errno.h>
490Sstevel@tonic-gate #include <sys/modctl.h>
500Sstevel@tonic-gate #include <sys/kobj.h>
510Sstevel@tonic-gate #include <sys/promif.h>
520Sstevel@tonic-gate #include <sys/ddi.h>
530Sstevel@tonic-gate #include <sys/sunddi.h>
540Sstevel@tonic-gate #include <sys/reboot.h>
550Sstevel@tonic-gate #include <sys/callb.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate int obpsym_debug = 0;
580Sstevel@tonic-gate #define	DPRINTF(str)		if (obpsym_debug) prom_printf(str)
590Sstevel@tonic-gate #define	DPRINTF1(str, a)	if (obpsym_debug) prom_printf(str, a);
600Sstevel@tonic-gate #define	DPRINTF2(str, a, b)	if (obpsym_debug) prom_printf(str, a, b);
610Sstevel@tonic-gate #define	DXPRINTF		if (obpsym_debug > 1) prom_printf
620Sstevel@tonic-gate 
630Sstevel@tonic-gate int obpheld = 1;		/* Prevents unloading when set */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * name_to_value - translate a string into an address
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * The string may be one of two forms - 'symname' or 'modname:symname'.
690Sstevel@tonic-gate  * (We also accept 'unix:symname' as a synonymn for 'symname'.)
700Sstevel@tonic-gate  * The latter form causes a kobj_lookup() to occur only in the given module,
710Sstevel@tonic-gate  * the former causes a kobj_getsymvalue() on the entire kernel symbol table.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * mod_lock locking is not needed for the &modules list because
740Sstevel@tonic-gate  * modctl structures are never unlinked from the &modules list.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate int
name_to_value(char * name,uintptr_t * value)770Sstevel@tonic-gate name_to_value(char *name, uintptr_t *value)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	register char *symname = name;
800Sstevel@tonic-gate 	register char *modname = "";
810Sstevel@tonic-gate 	char *p;
820Sstevel@tonic-gate 	int retval = 0;
830Sstevel@tonic-gate 	uintptr_t symvalue = 0;
840Sstevel@tonic-gate 	char c = (char)0;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	/*
870Sstevel@tonic-gate 	 * we take names of the form: "modname:symbol", "unix:symbol", "symbol"
880Sstevel@tonic-gate 	 */
890Sstevel@tonic-gate 	if ((p = strchr(name, ':')) != NULL && p[1] != (char)0) {
900Sstevel@tonic-gate 		symname = p + 1;
910Sstevel@tonic-gate 		modname = name;
920Sstevel@tonic-gate 		c = *p;
930Sstevel@tonic-gate 		*p = (char)0;
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (*modname == (char)0) {
970Sstevel@tonic-gate 		symvalue = kobj_getsymvalue(symname, 0);
980Sstevel@tonic-gate 	} else  {
990Sstevel@tonic-gate 		struct modctl *mp = &modules;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 		do {
1020Sstevel@tonic-gate 			if (strcmp(modname, mp->mod_modname) == 0) {
1030Sstevel@tonic-gate 				symvalue = kobj_lookup(mp->mod_mp, symname);
1040Sstevel@tonic-gate 				break;
1050Sstevel@tonic-gate 			}
1060Sstevel@tonic-gate 		} while ((mp = mp->mod_next) != &modules);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (symvalue == 0)
1100Sstevel@tonic-gate 		retval = -1;
1110Sstevel@tonic-gate 	if (c != (char)0)		/* Restore incoming cstr */
1120Sstevel@tonic-gate 		*p = c;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	*value = symvalue;
1150Sstevel@tonic-gate 	return (retval);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * value_to_name - translate an address into a string + offset
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * mod_lock locking is not needed fro the modules list because
1220Sstevel@tonic-gate  * modctl structures are never unlinked from the &modules list.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate ulong_t
value_to_name(uintptr_t value,char * symbol)1250Sstevel@tonic-gate value_to_name(uintptr_t value, char *symbol)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	struct modctl *modp = &modules;
1280Sstevel@tonic-gate 	ulong_t offset;
1290Sstevel@tonic-gate 	char *name;
1300Sstevel@tonic-gate 
1311048Sraf 	DPRINTF1("value_to_name: Looking for %p\n", (void *)value);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	do {
1340Sstevel@tonic-gate 		if (modp->mod_mp &&
1350Sstevel@tonic-gate 		    (name = kobj_searchsym(modp->mod_mp, value, &offset))) {
1360Sstevel@tonic-gate 			(void) strcpy(symbol, modp->mod_modname);
1370Sstevel@tonic-gate 			(void) strcat(symbol, ":");
1380Sstevel@tonic-gate 			(void) strcat(symbol, name);
1390Sstevel@tonic-gate 			return (offset);
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 	} while ((modp = modp->mod_next) != &modules);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	*symbol = (char)0;
1440Sstevel@tonic-gate 	return ((ulong_t)-1l);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * loadable module wrapper
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate static struct modlmisc modlmisc = {
151*7799SRichard.Bean@Sun.COM 	&mod_miscops, "OBP symbol callbacks"
1520Sstevel@tonic-gate };
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static struct modlinkage modlinkage = {
1550Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
1560Sstevel@tonic-gate };
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*ARGSUSED*/
1590Sstevel@tonic-gate static boolean_t
reset_callbacks(void * arg,int code)1600Sstevel@tonic-gate reset_callbacks(void *arg, int code)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	extern void set_sym_callbacks();
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (code == CB_CODE_CPR_RESUME)
1650Sstevel@tonic-gate 		set_sym_callbacks();
1660Sstevel@tonic-gate 	return (B_TRUE);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate int
_init(void)1700Sstevel@tonic-gate _init(void)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	int retval;
1730Sstevel@tonic-gate 	extern int install_callbacks(void);
1740Sstevel@tonic-gate 	extern void remove_callbacks(void);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if (install_callbacks() != 0)
1770Sstevel@tonic-gate 		return (ENXIO);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (boothowto & RB_HALT)
1800Sstevel@tonic-gate 		debug_enter("obpsym: halt flag (-h) is set.\n");
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	retval = mod_install(&modlinkage);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/*
1850Sstevel@tonic-gate 	 * if load fails remove callback and unlock symbols
1860Sstevel@tonic-gate 	 */
1870Sstevel@tonic-gate 	if (retval) {
1880Sstevel@tonic-gate 		printf("obpsym: Error %d installing OBP syms module\n", retval);
1890Sstevel@tonic-gate 		remove_callbacks();
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 	else
1920Sstevel@tonic-gate 		(void) callb_add(reset_callbacks, 0, CB_CL_CPR_OBP, "obpsym");
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	return (retval);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate int
_fini(void)1980Sstevel@tonic-gate _fini(void)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	int retval;
2010Sstevel@tonic-gate 	extern void remove_callbacks(void);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (obpheld != 0)
2040Sstevel@tonic-gate 		return (EBUSY);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	retval = mod_remove(&modlinkage);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * if unload succeeds remove callback and unlock symbols
2100Sstevel@tonic-gate 	 */
2110Sstevel@tonic-gate 	if (retval == 0) {
2120Sstevel@tonic-gate 		remove_callbacks();
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	return (retval);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2180Sstevel@tonic-gate _info(struct modinfo *modinfop)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2210Sstevel@tonic-gate }
222