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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22*75521904Sraf 237c478bd9Sstevel@tonic-gate /* 24*75521904Sraf * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * This module supports callbacks from the firmware 327c478bd9Sstevel@tonic-gate * such that address and name lookups can work and use kernel symbol names. 337c478bd9Sstevel@tonic-gate * For example "ctrace" will provide symbolic names, if they are available. 347c478bd9Sstevel@tonic-gate * Also, normal firmware name to address lookups should work, though be 357c478bd9Sstevel@tonic-gate * careful with clashing kernel names, such as "startup" and "reset" which 367c478bd9Sstevel@tonic-gate * may be the firmware names and *not* the kernel names. 377c478bd9Sstevel@tonic-gate * 387c478bd9Sstevel@tonic-gate * The module locks the symbol tables in memory, when it's installed, 397c478bd9Sstevel@tonic-gate * and unlocks them when it is removed. To install the module, you 407c478bd9Sstevel@tonic-gate * may either add "set obpdebug=1" to /etc/system or just modload it. 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * This file contains the actual code the does the lookups, and interfaces 437c478bd9Sstevel@tonic-gate * with the kernel kobj stuff. The transfer of data and control to/from 447c478bd9Sstevel@tonic-gate * the firmware is handled in prom-dependent code. 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate #include <sys/types.h> 487c478bd9Sstevel@tonic-gate #include <sys/param.h> 497c478bd9Sstevel@tonic-gate #include <sys/systm.h> 507c478bd9Sstevel@tonic-gate #include <sys/debug.h> 517c478bd9Sstevel@tonic-gate #include <sys/errno.h> 527c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 537c478bd9Sstevel@tonic-gate #include <sys/kobj.h> 547c478bd9Sstevel@tonic-gate #include <sys/promif.h> 557c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 567c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 577c478bd9Sstevel@tonic-gate #include <sys/reboot.h> 587c478bd9Sstevel@tonic-gate #include <sys/callb.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate int obpsym_debug = 0; 617c478bd9Sstevel@tonic-gate #define DPRINTF(str) if (obpsym_debug) prom_printf(str) 627c478bd9Sstevel@tonic-gate #define DPRINTF1(str, a) if (obpsym_debug) prom_printf(str, a); 637c478bd9Sstevel@tonic-gate #define DPRINTF2(str, a, b) if (obpsym_debug) prom_printf(str, a, b); 647c478bd9Sstevel@tonic-gate #define DXPRINTF if (obpsym_debug > 1) prom_printf 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate int obpheld = 1; /* Prevents unloading when set */ 677c478bd9Sstevel@tonic-gate extern int obpdebug; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * name_to_value - translate a string into an address 717c478bd9Sstevel@tonic-gate * 727c478bd9Sstevel@tonic-gate * The string may be one of two forms - 'symname' or 'modname:symname'. 737c478bd9Sstevel@tonic-gate * (We also accept 'unix:symname' as a synonymn for 'symname'.) 747c478bd9Sstevel@tonic-gate * The latter form causes a kobj_lookup() to occur only in the given module, 757c478bd9Sstevel@tonic-gate * the former causes a kobj_getsymvalue() on the entire kernel symbol table. 767c478bd9Sstevel@tonic-gate * 777c478bd9Sstevel@tonic-gate * mod_lock locking is not needed for the &modules list because 787c478bd9Sstevel@tonic-gate * modctl structures are never unlinked from the &modules list. 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate int 817c478bd9Sstevel@tonic-gate name_to_value(char *name, uintptr_t *value) 827c478bd9Sstevel@tonic-gate { 837c478bd9Sstevel@tonic-gate register char *symname = name; 847c478bd9Sstevel@tonic-gate register char *modname = ""; 857c478bd9Sstevel@tonic-gate char *p; 867c478bd9Sstevel@tonic-gate int retval = 0; 877c478bd9Sstevel@tonic-gate uintptr_t symvalue = 0; 887c478bd9Sstevel@tonic-gate char c = (char)0; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * we take names of the form: "modname:symbol", "unix:symbol", "symbol" 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate if ((p = strchr(name, ':')) != NULL && p[1] != (char)0) { 947c478bd9Sstevel@tonic-gate symname = p + 1; 957c478bd9Sstevel@tonic-gate modname = name; 967c478bd9Sstevel@tonic-gate c = *p; 977c478bd9Sstevel@tonic-gate *p = (char)0; 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate if (*modname == (char)0) { 1017c478bd9Sstevel@tonic-gate symvalue = kobj_getsymvalue(symname, 0); 1027c478bd9Sstevel@tonic-gate } else { 1037c478bd9Sstevel@tonic-gate struct modctl *mp = &modules; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate do { 1067c478bd9Sstevel@tonic-gate if (strcmp(modname, mp->mod_modname) == 0) { 1077c478bd9Sstevel@tonic-gate symvalue = kobj_lookup(mp->mod_mp, symname); 1087c478bd9Sstevel@tonic-gate break; 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate } while ((mp = mp->mod_next) != &modules); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (symvalue == 0) 1147c478bd9Sstevel@tonic-gate retval = -1; 1157c478bd9Sstevel@tonic-gate if (c != (char)0) /* Restore incoming cstr */ 1167c478bd9Sstevel@tonic-gate *p = c; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate *value = symvalue; 1197c478bd9Sstevel@tonic-gate return (retval); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* 1237c478bd9Sstevel@tonic-gate * value_to_name - translate an address into a string + offset 1247c478bd9Sstevel@tonic-gate * 1257c478bd9Sstevel@tonic-gate * mod_lock locking is not needed fro the modules list because 1267c478bd9Sstevel@tonic-gate * modctl structures are never unlinked from the &modules list. 1277c478bd9Sstevel@tonic-gate */ 1287c478bd9Sstevel@tonic-gate ulong_t 1297c478bd9Sstevel@tonic-gate value_to_name(uintptr_t value, char *symbol) 1307c478bd9Sstevel@tonic-gate { 1317c478bd9Sstevel@tonic-gate struct modctl *modp = &modules; 1327c478bd9Sstevel@tonic-gate ulong_t offset; 1337c478bd9Sstevel@tonic-gate char *name; 1347c478bd9Sstevel@tonic-gate 135*75521904Sraf DPRINTF1("value_to_name: Looking for %p\n", (void *)value); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate do { 1387c478bd9Sstevel@tonic-gate if (modp->mod_mp && 1397c478bd9Sstevel@tonic-gate (name = kobj_searchsym(modp->mod_mp, value, &offset))) { 1407c478bd9Sstevel@tonic-gate (void) strcpy(symbol, modp->mod_modname); 1417c478bd9Sstevel@tonic-gate (void) strcat(symbol, ":"); 1427c478bd9Sstevel@tonic-gate (void) strcat(symbol, name); 1437c478bd9Sstevel@tonic-gate return (offset); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate } while ((modp = modp->mod_next) != &modules); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate *symbol = (char)0; 1487c478bd9Sstevel@tonic-gate return ((ulong_t)-1l); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * loadable module wrapper 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = { 1557c478bd9Sstevel@tonic-gate &mod_miscops, "OBP symbol callbacks %I%" 1567c478bd9Sstevel@tonic-gate }; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 1597c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modlmisc, NULL 1607c478bd9Sstevel@tonic-gate }; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1637c478bd9Sstevel@tonic-gate static boolean_t 1647c478bd9Sstevel@tonic-gate reset_callbacks(void *arg, int code) 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate extern void set_sym_callbacks(); 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (code == CB_CODE_CPR_RESUME) 1697c478bd9Sstevel@tonic-gate set_sym_callbacks(); 1707c478bd9Sstevel@tonic-gate return (B_TRUE); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate int 1747c478bd9Sstevel@tonic-gate _init(void) 1757c478bd9Sstevel@tonic-gate { 1767c478bd9Sstevel@tonic-gate int retval; 1777c478bd9Sstevel@tonic-gate extern int install_callbacks(void); 1787c478bd9Sstevel@tonic-gate extern void remove_callbacks(void); 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate if (install_callbacks() != 0) 1817c478bd9Sstevel@tonic-gate return (ENXIO); 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate obpdebug = 1; 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate if (boothowto & RB_HALT) 1867c478bd9Sstevel@tonic-gate debug_enter("obpsym: halt flag (-h) is set.\n"); 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate retval = mod_install(&modlinkage); 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* 1917c478bd9Sstevel@tonic-gate * if load fails remove callback and unlock symbols 1927c478bd9Sstevel@tonic-gate */ 1937c478bd9Sstevel@tonic-gate if (retval) { 1947c478bd9Sstevel@tonic-gate printf("obpsym: Error %d installing OBP syms module\n", retval); 1957c478bd9Sstevel@tonic-gate remove_callbacks(); 1967c478bd9Sstevel@tonic-gate obpdebug = 0; 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate else 1997c478bd9Sstevel@tonic-gate (void) callb_add(reset_callbacks, 0, CB_CL_CPR_OBP, "obpsym"); 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate return (retval); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate int 2057c478bd9Sstevel@tonic-gate _fini(void) 2067c478bd9Sstevel@tonic-gate { 2077c478bd9Sstevel@tonic-gate int retval; 2087c478bd9Sstevel@tonic-gate extern void remove_callbacks(void); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if (obpheld != 0) 2117c478bd9Sstevel@tonic-gate return (EBUSY); 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate retval = mod_remove(&modlinkage); 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate /* 2167c478bd9Sstevel@tonic-gate * if unload succeeds remove callback and unlock symbols 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate if (retval == 0) { 2197c478bd9Sstevel@tonic-gate remove_callbacks(); 2207c478bd9Sstevel@tonic-gate obpdebug = 0; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate return (retval); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate int 2267c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 2277c478bd9Sstevel@tonic-gate { 2287c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2297c478bd9Sstevel@tonic-gate } 230