xref: /illumos-gate/usr/src/uts/sparc/os/obpsym_1275.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1991-1995, 1996, by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * This module provides kernel callbacks to IEEE 1275-1994 system.
31*7c478bd9Sstevel@tonic-gate  * such that address and name lookups can work and use kernel symbol names.
32*7c478bd9Sstevel@tonic-gate  * For example "ctrace" will provide symbolic names, if they are available.
33*7c478bd9Sstevel@tonic-gate  * Also, normal firmware name to address lookups should work, though be
34*7c478bd9Sstevel@tonic-gate  * careful with clashing kernel names, such as "startup" and "reset" which
35*7c478bd9Sstevel@tonic-gate  * may be the firmware names and *not* the kernel names.
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  * This file contains the glue that gets control from the firmware and
38*7c478bd9Sstevel@tonic-gate  * transfers data from/to the firmware.
39*7c478bd9Sstevel@tonic-gate  *
40*7c478bd9Sstevel@tonic-gate  * The platform code needs to provide the routines add_vx_handler
41*7c478bd9Sstevel@tonic-gate  * and remove_vx_handler, which add/remove an Open Firmware callback
42*7c478bd9Sstevel@tonic-gate  * handler for a given callback name.
43*7c478bd9Sstevel@tonic-gate  */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/promif.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/prom_isa.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
57*7c478bd9Sstevel@tonic-gate #include <rpc/types.h>
58*7c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #define	DPRINTF(str)		if (obpsym_debug) prom_printf(str)
61*7c478bd9Sstevel@tonic-gate #define	DPRINTF1(str, a)	if (obpsym_debug) prom_printf(str, a);
62*7c478bd9Sstevel@tonic-gate #define	DPRINTF2(str, a, b)	if (obpsym_debug) prom_printf(str, a, b);
63*7c478bd9Sstevel@tonic-gate #define	DXPRINTF		if (obpsym_debug > 1) prom_printf
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate #define	MAX_NAME	128
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate static void
ieee_sym_to_value(cell_t * cif)69*7c478bd9Sstevel@tonic-gate ieee_sym_to_value(cell_t *cif)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	int	error = -1;
72*7c478bd9Sstevel@tonic-gate 	uintptr_t symvalue = 0;
73*7c478bd9Sstevel@tonic-gate 	unsigned int nargs, nresults;
74*7c478bd9Sstevel@tonic-gate 	char	*symname;
75*7c478bd9Sstevel@tonic-gate 	extern	int name_to_value(char *name, uintptr_t *value);
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	nargs = p1275_cell2uint(cif[1]);
78*7c478bd9Sstevel@tonic-gate 	nresults = p1275_cell2uint(cif[2]);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if (nresults == 0)
81*7c478bd9Sstevel@tonic-gate 		return;		/* No room for results. Just return. */
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	/*
84*7c478bd9Sstevel@tonic-gate 	 * If there are no arguments, fall through and return an error.
85*7c478bd9Sstevel@tonic-gate 	 * Otherwise, try to translate the symbol name arg to a value.
86*7c478bd9Sstevel@tonic-gate 	 */
87*7c478bd9Sstevel@tonic-gate 	if (nargs != 0) {
88*7c478bd9Sstevel@tonic-gate 		symname = p1275_cell2ptr(cif[3]);	/* argument 0 */
89*7c478bd9Sstevel@tonic-gate 		error = name_to_value(symname, &symvalue);
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	/*
93*7c478bd9Sstevel@tonic-gate 	 * Stuff the results in the argument array and set the
94*7c478bd9Sstevel@tonic-gate 	 * nresults element to the number of results actually returned
95*7c478bd9Sstevel@tonic-gate 	 * in the argument array. (It's a maximum of 2).
96*7c478bd9Sstevel@tonic-gate 	 *
97*7c478bd9Sstevel@tonic-gate 	 * cif[0]:	service name	( Pointer to service name )
98*7c478bd9Sstevel@tonic-gate 	 * cif[1]:	nargs		( number of argument cells)
99*7c478bd9Sstevel@tonic-gate 	 * cif[2]:	nresults	( number of result cells)
100*7c478bd9Sstevel@tonic-gate 	 * cif[3]:	argument{0}	( First argument cell )
101*7c478bd9Sstevel@tonic-gate 	 * ...
102*7c478bd9Sstevel@tonic-gate 	 * cif[3 + nargs]: result{0}	( First result cell )
103*7c478bd9Sstevel@tonic-gate 	 * ...
104*7c478bd9Sstevel@tonic-gate 	 */
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	cif[3 + nargs] = p1275_int2cell(error);
107*7c478bd9Sstevel@tonic-gate 	if (nresults > 1) {
108*7c478bd9Sstevel@tonic-gate 		cif[3 + nargs + 1] = p1275_uintptr2cell(symvalue);
109*7c478bd9Sstevel@tonic-gate 		cif[2] = p1275_int2cell(2);	/* there are 2 results */
110*7c478bd9Sstevel@tonic-gate 	} else {
111*7c478bd9Sstevel@tonic-gate 		cif[2] = p1275_int2cell(1);	/* there is 1 result */
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate static char symbol[MAX_NAME];
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate static void
ieee_value_to_sym(cell_t * cif)118*7c478bd9Sstevel@tonic-gate ieee_value_to_sym(cell_t *cif)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	u_int	nargs, nresults;
121*7c478bd9Sstevel@tonic-gate 	u_long	value;
122*7c478bd9Sstevel@tonic-gate 	u_int	offset;
123*7c478bd9Sstevel@tonic-gate 	char	*name = symbol;
124*7c478bd9Sstevel@tonic-gate 	extern u_long value_to_name(uintptr_t value, char *symbol);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	nargs = p1275_cell2uint(cif[1]);
127*7c478bd9Sstevel@tonic-gate 	nresults = p1275_cell2uint(cif[2]);
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	if (nresults == 0)
130*7c478bd9Sstevel@tonic-gate 		return;		/* No room for results. Just return. */
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	/*
133*7c478bd9Sstevel@tonic-gate 	 * If there are no arguments, fall through and return "not found".
134*7c478bd9Sstevel@tonic-gate 	 * Otherwise, try to translate the value to a symbol-name/offset.
135*7c478bd9Sstevel@tonic-gate 	 */
136*7c478bd9Sstevel@tonic-gate 	*name = (char)0;
137*7c478bd9Sstevel@tonic-gate 	offset = (u_int)-1;
138*7c478bd9Sstevel@tonic-gate 	if (nargs != 0) {
139*7c478bd9Sstevel@tonic-gate 		value = p1275_cell2uintptr(cif[3]); /* argument 0 */
140*7c478bd9Sstevel@tonic-gate 		offset = value_to_name(value, name);
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	/*
144*7c478bd9Sstevel@tonic-gate 	 * Stuff the results in the argument array and set the
145*7c478bd9Sstevel@tonic-gate 	 * nresults element to the number of results actually returned
146*7c478bd9Sstevel@tonic-gate 	 * in the argument array. (It's a maximum of 2).
147*7c478bd9Sstevel@tonic-gate 	 *
148*7c478bd9Sstevel@tonic-gate 	 * cif[0]:	service name	( Pointer to service name )
149*7c478bd9Sstevel@tonic-gate 	 * cif[1]:	nargs		( number of argument cells)
150*7c478bd9Sstevel@tonic-gate 	 * cif[2]:	nresults	( number of result cells)
151*7c478bd9Sstevel@tonic-gate 	 * cif[3]:	argument{0}	( First argument cell )
152*7c478bd9Sstevel@tonic-gate 	 * ...
153*7c478bd9Sstevel@tonic-gate 	 * cif[3 + nargs]: result{0}	( First result cell )
154*7c478bd9Sstevel@tonic-gate 	 * ...
155*7c478bd9Sstevel@tonic-gate 	 */
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	/*
158*7c478bd9Sstevel@tonic-gate 	 * Treat this as an integer, so we sign-extend -1, offsets
159*7c478bd9Sstevel@tonic-gate 	 * are always postive, -1 indicates not found.
160*7c478bd9Sstevel@tonic-gate 	 */
161*7c478bd9Sstevel@tonic-gate 	cif[3 + nargs] = p1275_int2cell((int)offset);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	if (nresults > 1) {
164*7c478bd9Sstevel@tonic-gate 		cif[3 + nargs + 1] = p1275_ptr2cell(name);
165*7c478bd9Sstevel@tonic-gate 		cif[2] = p1275_int2cell(2);	/* there are 2 results */
166*7c478bd9Sstevel@tonic-gate 	} else {
167*7c478bd9Sstevel@tonic-gate 		cif[2] = p1275_int2cell(1);	/* there is 1 result */
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate void
set_sym_callbacks()172*7c478bd9Sstevel@tonic-gate set_sym_callbacks()
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	extern int callback_handler(cell_t *arg_array);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	/*
177*7c478bd9Sstevel@tonic-gate 	 * This code assumes a wrapper for the callbacks,
178*7c478bd9Sstevel@tonic-gate 	 * though not all implementations will need them,
179*7c478bd9Sstevel@tonic-gate 	 * they should be easy enough to provide. It might
180*7c478bd9Sstevel@tonic-gate 	 * be better to provide these as 2 macros set by each
181*7c478bd9Sstevel@tonic-gate 	 * platform, this assumes there's a single handler.
182*7c478bd9Sstevel@tonic-gate 	 */
183*7c478bd9Sstevel@tonic-gate 	(void) prom_set_symbol_lookup((void *)callback_handler,
184*7c478bd9Sstevel@tonic-gate 	    (void *)callback_handler);
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate int
install_callbacks(void)188*7c478bd9Sstevel@tonic-gate install_callbacks(void)
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate 	void add_vx_handler(char *, int, void (*f)(cell_t *));
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	add_vx_handler("sym-to-value", 0, ieee_sym_to_value);
193*7c478bd9Sstevel@tonic-gate 	add_vx_handler("value-to-sym", 0, ieee_value_to_sym);
194*7c478bd9Sstevel@tonic-gate 	set_sym_callbacks();
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	return (0);
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate void
remove_callbacks(void)200*7c478bd9Sstevel@tonic-gate remove_callbacks(void)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	void remove_vx_handler(char *);
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	(void) prom_set_symbol_lookup((void *)0, (void *)0);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	remove_vx_handler("sym-to-value");
207*7c478bd9Sstevel@tonic-gate 	remove_vx_handler("value-to-sym");
208*7c478bd9Sstevel@tonic-gate }
209