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