xref: /titanic_51/usr/src/common/elfcap/elfcap.c (revision c81d47afd05baeb768e2f032636019b717899efd)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /* LINTLIBRARY */
30 
31 /*
32  * String conversion routine for hardware capabilities types.
33  */
34 #include	<strings.h>
35 #include	<stdio.h>
36 #include	<ctype.h>
37 #include	<limits.h>
38 #include	<sys/machelf.h>
39 #include	<sys/elf.h>
40 #include	<sys/auxv_SPARC.h>
41 #include	<sys/auxv_386.h>
42 #include	<elfcap.h>
43 
44 /*
45  * Define separators for val2str processing.
46  */
47 static const Fmt_desc format[] = {
48 	{" ",	1 },
49 	{"  ",	2 },
50 	{" | ",	3 }
51 };
52 
53 /*
54  * Define all known capabilities as both lower and upper case strings.  This
55  * duplication is necessary, rather than have one string and use something
56  * like toupper(), as a client such as ld.so.1 doesn't need the overhead of
57  * dragging in the internationalization support of toupper().  The Intel 3DNow
58  * flags are a slightly odd convention too.
59  *
60  * Define all known software capabilities.
61  */
62 #ifdef	CAP_UPPERCASE
63 static const char Sf1_fpknwn[] =	"FPKNWN";
64 static const char Sf1_fpused[] =	"FPUSED";
65 #elif	CAP_LOWERCASE
66 static const char Sf1_fpknwn[] =	"fpknwn";
67 static const char Sf1_fpused[] =	"fpused";
68 #else
69 #error	"Software Capabilities - what case do you want?"
70 #endif
71 
72 /*
73  * Order the software capabilities to match their numeric value.  See SF1_SUNW_
74  * values in sys/elf.h.
75  */
76 static const Cap_desc sf1[] = {
77 	{ SF1_SUNW_FPKNWN,	Sf1_fpknwn,	(sizeof (Sf1_fpknwn) - 1) },
78 	{ SF1_SUNW_FPUSED,	Sf1_fpused,	(sizeof (Sf1_fpused) - 1) }
79 };
80 static const uint_t sf1_num = sizeof (sf1) / sizeof (Cap_desc);
81 
82 /*
83  * Define all known SPARC hardware capabilities.
84  */
85 #ifdef	CAP_UPPERCASE
86 static const char Hw1_s_mul32[] =	"MUL32";
87 static const char Hw1_s_div32[] =	"DIV32";
88 static const char Hw1_s_fsmuld[] =	"FSMULD";
89 static const char Hw1_s_v8plus[] =	"V8PLUS";
90 static const char Hw1_s_popc[] =	"POPC";
91 static const char Hw1_s_vis[] =		"VIS";
92 static const char Hw1_s_vis2[] =	"VIS2";
93 static const char Hw1_s_asi_blk_init[] =	"ASI_BLK_INIT";
94 static const char Hw1_s_fmaf[] = 	"FMAF";
95 #elif	CAP_LOWERCASE
96 static const char Hw1_s_mul32[] =	"mul32";
97 static const char Hw1_s_div32[] =	"div32";
98 static const char Hw1_s_fsmuld[] =	"fsmuld";
99 static const char Hw1_s_v8plus[] =	"v8plus";
100 static const char Hw1_s_popc[] =	"popc";
101 static const char Hw1_s_vis[] =		"vis";
102 static const char Hw1_s_vis2[] =	"vis2";
103 static const char Hw1_s_asi_blk_init[] =	"asi_blk_init";
104 static const char Hw1_s_fmaf[] =	"fmaf";
105 
106 #else
107 #error	"Hardware Capabilities (sparc) - what case do you want?"
108 #endif
109 
110 /*
111  * Order the SPARC hardware capabilities to match their numeric value.  See
112  * AV_SPARC_ values in sys/auxv_SPARC.h.
113  */
114 static const Cap_desc hw1_s[] = {
115 	{ AV_SPARC_MUL32,	Hw1_s_mul32,	sizeof (Hw1_s_mul32) - 1 },
116 	{ AV_SPARC_DIV32,	Hw1_s_div32,	sizeof (Hw1_s_div32) - 1 },
117 	{ AV_SPARC_FSMULD,	Hw1_s_fsmuld,	sizeof (Hw1_s_fsmuld) - 1 },
118 	{ AV_SPARC_V8PLUS,	Hw1_s_v8plus,	sizeof (Hw1_s_v8plus) - 1 },
119 	{ AV_SPARC_POPC,	Hw1_s_popc,	sizeof (Hw1_s_popc) - 1 },
120 	{ AV_SPARC_VIS,		Hw1_s_vis,	sizeof (Hw1_s_vis) - 1 },
121 	{ AV_SPARC_VIS2,	Hw1_s_vis2,	sizeof (Hw1_s_vis2) - 1 },
122 	{ AV_SPARC_ASI_BLK_INIT,	Hw1_s_asi_blk_init,
123 		sizeof (Hw1_s_asi_blk_init) - 1 },
124 	{ AV_SPARC_FMAF,	Hw1_s_fmaf,	sizeof (Hw1_s_fmaf) - 1 }
125 };
126 static const uint_t hw1_s_num = sizeof (hw1_s) / sizeof (Cap_desc);
127 
128 /*
129  * Define all known Intel hardware capabilities.
130  */
131 #ifdef	CAP_UPPERCASE
132 static const char Hw1_i_fpu[] =		"FPU";
133 static const char Hw1_i_tsc[] =		"TSC";
134 static const char Hw1_i_cx8[] =		"CX8";
135 static const char Hw1_i_sep[] =		"SEP";
136 static const char Hw1_i_amd_sysc[] =	"AMD_SYSC";
137 static const char Hw1_i_cmov[] =	"CMOV";
138 static const char Hw1_i_mmx[] =		"MMX";
139 static const char Hw1_i_amd_mmx[] =	"AMD_MMX";
140 static const char Hw1_i_amd_3dnow[] =	"AMD_3DNow";
141 static const char Hw1_i_amd_3dnowx[] =	"AMD_3DNowx";
142 static const char Hw1_i_fxsr[] =	"FXSR";
143 static const char Hw1_i_sse[] =		"SSE";
144 static const char Hw1_i_sse2[] =	"SSE2";
145 static const char Hw1_i_pause[] =	"PAUSE";
146 static const char Hw1_i_sse3[] =	"SSE3";
147 static const char Hw1_i_mon[] =		"MON";
148 static const char Hw1_i_cx16[] =	"CX16";
149 static const char Hw1_i_ahf[] =		"AHF";
150 static const char Hw1_i_tscp[] =	"TSCP";
151 #elif	CAP_LOWERCASE
152 static const char Hw1_i_fpu[] =		"fpu";
153 static const char Hw1_i_tsc[] =		"tsc";
154 static const char Hw1_i_cx8[] =		"cx8";
155 static const char Hw1_i_sep[] =		"sep";
156 static const char Hw1_i_amd_sysc[] =	"amd_sysc";
157 static const char Hw1_i_cmov[] =	"cmov";
158 static const char Hw1_i_mmx[] =		"mmx";
159 static const char Hw1_i_amd_mmx[] =	"amd_mmx";
160 static const char Hw1_i_amd_3dnow[] =	"amd_3dnow";
161 static const char Hw1_i_amd_3dnowx[] =	"amd_3dnowx";
162 static const char Hw1_i_fxsr[] =	"fxsr";
163 static const char Hw1_i_sse[] =		"sse";
164 static const char Hw1_i_sse2[] =	"sse2";
165 static const char Hw1_i_pause[] =	"pause";
166 static const char Hw1_i_sse3[] =	"sse3";
167 static const char Hw1_i_mon[] =		"mon";
168 static const char Hw1_i_cx16[] =	"cx16";
169 static const char Hw1_i_ahf[] =		"ahf";
170 static const char Hw1_i_tscp[] = 	"tscp";
171 #else
172 #error	"Hardware Capabilities (intel) - what case do you want?"
173 #endif
174 
175 /*
176  * Order the Intel hardware capabilities to match their numeric value.  See
177  * AV_386_ values in sys/auxv_386.h.
178  */
179 static const Cap_desc hw1_i[] = {
180 	{ AV_386_FPU,		Hw1_i_fpu,	sizeof (Hw1_i_fpu) - 1 },
181 	{ AV_386_TSC,		Hw1_i_tsc,	sizeof (Hw1_i_tsc) - 1 },
182 	{ AV_386_CX8,		Hw1_i_cx8,	sizeof (Hw1_i_cx8) - 1 },
183 	{ AV_386_SEP,		Hw1_i_sep,	sizeof (Hw1_i_sep) - 1 },
184 	{ AV_386_AMD_SYSC,	Hw1_i_amd_sysc,	sizeof (Hw1_i_amd_sysc) - 1 },
185 	{ AV_386_CMOV,		Hw1_i_cmov,	sizeof (Hw1_i_cmov) - 1 },
186 	{ AV_386_MMX,		Hw1_i_mmx,	sizeof (Hw1_i_mmx) - 1 },
187 	{ AV_386_AMD_MMX,	Hw1_i_amd_mmx,	sizeof (Hw1_i_amd_mmx) - 1 },
188 	{ AV_386_AMD_3DNow,	Hw1_i_amd_3dnow,
189 						sizeof (Hw1_i_amd_3dnow) - 1 },
190 	{ AV_386_AMD_3DNowx,	Hw1_i_amd_3dnowx,
191 						sizeof (Hw1_i_amd_3dnowx) - 1 },
192 	{ AV_386_FXSR,		Hw1_i_fxsr,	sizeof (Hw1_i_fxsr) - 1 },
193 	{ AV_386_SSE,		Hw1_i_sse,	sizeof (Hw1_i_sse) - 1 },
194 	{ AV_386_SSE2,		Hw1_i_sse2,	sizeof (Hw1_i_sse2) - 1 },
195 	{ AV_386_PAUSE,		Hw1_i_pause,	sizeof (Hw1_i_pause) - 1 },
196 	{ AV_386_SSE3,		Hw1_i_sse3,	sizeof (Hw1_i_sse3) - 1 },
197 	{ AV_386_MON,		Hw1_i_mon,	sizeof (Hw1_i_mon) - 1 },
198 	{ AV_386_CX16,		Hw1_i_cx16,	sizeof (Hw1_i_cx16) - 1 },
199 	{ AV_386_AHF,		Hw1_i_ahf,	sizeof (Hw1_i_ahf) - 1 },
200 	{ AV_386_TSCP,		Hw1_i_tscp,	sizeof (Hw1_i_tscp) - 1 }
201 };
202 static const uint_t hw1_i_num = sizeof (hw1_i) / sizeof (Cap_desc);
203 
204 /*
205  * Concatenate a token to the string buffer.  This can be a capabilities token
206  * or a separator token.
207  */
208 static int
209 token(char **ostr, size_t *olen, const char *nstr, size_t nlen)
210 {
211 	if (*olen < nlen)
212 		return (CAP_ERR_BUFOVFL);
213 
214 	(void) strcat(*ostr, nstr);
215 	*ostr += nlen;
216 	*olen -= nlen;
217 
218 	return (0);
219 }
220 
221 /*
222  * Expand a capabilities value into the strings defined in the associated
223  * capabilities descriptor.
224  */
225 static int
226 expand(uint64_t val, const Cap_desc *cdp, uint_t cnum, char *str, size_t slen,
227     int fmt)
228 {
229 	uint_t	cnt, mask;
230 	int	follow = 0, err;
231 
232 	if (val == 0)
233 		return (0);
234 
235 	for (cnt = WORD_BIT, mask = 0x80000000; cnt; cnt--,
236 	    (mask = mask >> 1)) {
237 		if ((val & mask) && (cnt <= cnum) && cdp[cnt - 1].c_val) {
238 			if (follow++ && ((err = token(&str, &slen,
239 			    format[fmt].f_str, format[fmt].f_len)) != 0))
240 				return (err);
241 
242 			if ((err = token(&str, &slen, cdp[cnt - 1].c_str,
243 			    cdp[cnt - 1].c_len)) != 0)
244 				return (err);
245 
246 			val = val & ~mask;
247 		}
248 	}
249 
250 	/*
251 	 * If there are any unknown bits remaining display the numeric value.
252 	 */
253 	if (val) {
254 		if (follow && ((err = token(&str, &slen, format[fmt].f_str,
255 		    format[fmt].f_len)) != 0))
256 			return (err);
257 
258 		(void) snprintf(str, slen, "0x%llx", val);
259 	}
260 	return (0);
261 }
262 
263 /*
264  * Expand a CA_SUNW_HW_1 value.
265  */
266 int
267 hwcap_1_val2str(uint64_t val, char *str, size_t len, int fmt, ushort_t mach)
268 {
269 	/*
270 	 * Initialize the string buffer, and validate the format request.
271 	 */
272 	*str = '\0';
273 	if (fmt > CAP_MAX_TYPE)
274 		return (CAP_ERR_INVFMT);
275 
276 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
277 		return (expand(val, &hw1_i[0], hw1_i_num, str, len, fmt));
278 
279 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
280 	    (mach == EM_SPARCV9))
281 		return (expand(val, &hw1_s[0], hw1_s_num, str, len, fmt));
282 
283 	return (CAP_ERR_UNKMACH);
284 }
285 
286 /*
287  * Expand a CA_SUNW_SF_1 value.  Note, that at present these capabilities are
288  * common across all platforms.  The use of "mach" is therefore redundant, but
289  * is retained for compatibility with the interface of hwcap_1_val2str(), and
290  * possible future expansion.
291  */
292 int
293 /* ARGSUSED4 */
294 sfcap_1_val2str(uint64_t val, char *str, size_t len, int fmt, ushort_t mach)
295 {
296 	/*
297 	 * Initialize the string buffer, and validate the format request.
298 	 */
299 	*str = '\0';
300 	if (fmt > CAP_MAX_TYPE)
301 		return (CAP_ERR_INVFMT);
302 
303 	return (expand(val, &sf1[0], sf1_num, str, len, fmt));
304 }
305 
306 /*
307  * Determine capability type from the capability tag.
308  */
309 int
310 cap_val2str(uint64_t tag, uint64_t val, char *str, size_t len, int fmt,
311     ushort_t mach)
312 {
313 	if (tag == CA_SUNW_HW_1)
314 		return (hwcap_1_val2str(val, str, len, fmt, mach));
315 	if (tag == CA_SUNW_SF_1)
316 		return (sfcap_1_val2str(val, str, len, fmt, mach));
317 
318 	return (CAP_ERR_UNKTAG);
319 }
320 
321 /*
322  * Determine a capabilities value from a capabilities string.
323  */
324 static uint64_t
325 value(const char *str, const Cap_desc *cdp, uint_t cnum)
326 {
327 	uint_t	num;
328 
329 	for (num = 0; num < cnum; num++) {
330 		if (strcmp(str, cdp[num].c_str) == 0)
331 			return (cdp[num].c_val);
332 	}
333 	return (0);
334 }
335 
336 uint64_t
337 sfcap_1_str2val(const char *str, ushort_t mach)
338 {
339 	return (value(str, &sf1[0], sf1_num));
340 }
341 
342 uint64_t
343 hwcap_1_str2val(const char *str, ushort_t mach)
344 {
345 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
346 		return (value(str, &hw1_i[0], hw1_i_num));
347 
348 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
349 	    (mach == EM_SPARCV9))
350 		return (value(str, &hw1_s[0], hw1_s_num));
351 
352 	return (0);
353 }
354