xref: /titanic_41/usr/src/common/elfcap/elfcap.c (revision 2e1a94749a6ae1ee25de9f4492d4b90a3b408c4c)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* LINTLIBRARY */
28 
29 /*
30  * String conversion routine for hardware capabilities types.
31  */
32 #include	<strings.h>
33 #include	<stdio.h>
34 #include	<ctype.h>
35 #include	<sys/machelf.h>
36 #include	<sys/elf.h>
37 #include	<sys/auxv_SPARC.h>
38 #include	<sys/auxv_386.h>
39 #include	<elfcap.h>
40 
41 /*
42  * Given a literal string, generate an initialization for an
43  * elfcap_str_t value.
44  */
45 #define	STRDESC(_str) { _str, sizeof (_str) - 1 }
46 
47 /*
48  * The items in the elfcap_desc_t arrays are required to be
49  * ordered so that the array index is related to the
50  * c_val field as:
51  *
52  *	array[ndx].c_val = 2^ndx
53  *
54  * meaning that
55  *
56  *	array[0].c_val = 2^0 = 1
57  *	array[1].c_val = 2^1 = 2
58  *	array[2].c_val = 2^2 = 4
59  *	.
60  *	.
61  *	.
62  *
63  * Since 0 is not a valid value for the c_val field, we use it to
64  * mark an array entry that is a placeholder. This can happen if there
65  * is a hole in the assigned bits.
66  *
67  * The RESERVED_ELFCAP_DESC macro is used to reserve such holes.
68  */
69 #define	RESERVED_ELFCAP_DESC { 0, { NULL, 0 }, { NULL, 0 }, { NULL, 0 } }
70 
71 /*
72  * Define separators for output string processing. This must be kept in
73  * sync with the elfcap_fmt_t values in elfcap.h.
74  */
75 static const elfcap_str_t format[] = {
76 	STRDESC(" "),			/* ELFCAP_FMT_SNGSPACE */
77 	STRDESC("  "),			/* ELFCAP_FMT_DBLSPACE */
78 	STRDESC(" | ")			/* ELFCAP_FMT_PIPSPACE */
79 };
80 #define	FORMAT_NELTS	(sizeof (format) / sizeof (format[0]))
81 
82 
83 
84 /*
85  * Define all known software capabilities in all the supported styles.
86  * Order the capabilities by their numeric value. See SF1_SUNW_
87  * values in sys/elf.h.
88  */
89 static const elfcap_desc_t sf1[ELFCAP_NUM_SF1] = {
90 	{						/* 0x00000001 */
91 		SF1_SUNW_FPKNWN, STRDESC("SF1_SUNW_FPKNWN"),
92 		STRDESC("FPKNWN"), STRDESC("fpknwn")
93 	},
94 	{						/* 0x00000002 */
95 		SF1_SUNW_FPUSED, STRDESC("SF1_SUNW_FPUSED"),
96 		STRDESC("FPUSED"), STRDESC("fpused"),
97 	}
98 };
99 
100 
101 
102 /*
103  * Order the SPARC hardware capabilities to match their numeric value.  See
104  * AV_SPARC_ values in sys/auxv_SPARC.h.
105  */
106 static const elfcap_desc_t hw1_sparc[ELFCAP_NUM_HW1_SPARC] = {
107 	{						/* 0x00000001 */
108 		AV_SPARC_MUL32, STRDESC("AV_SPARC_MUL32"),
109 		STRDESC("MUL32"), STRDESC("mul32"),
110 	},
111 	{						/* 0x00000002 */
112 		AV_SPARC_DIV32, STRDESC("AV_SPARC_DIV32"),
113 		STRDESC("DIV32"), STRDESC("div32"),
114 	},
115 	{						/* 0x00000004 */
116 		AV_SPARC_FSMULD, STRDESC("AV_SPARC_FSMULD"),
117 		STRDESC("FSMULD"), STRDESC("fsmuld"),
118 	},
119 	{						/* 0x00000008 */
120 		AV_SPARC_V8PLUS, STRDESC("AV_SPARC_V8PLUS"),
121 		STRDESC("V8PLUS"), STRDESC("v8plus"),
122 	},
123 	{						/* 0x00000010 */
124 		AV_SPARC_POPC, STRDESC("AV_SPARC_POPC"),
125 		STRDESC("POPC"), STRDESC("popc"),
126 	},
127 	{						/* 0x00000020 */
128 		AV_SPARC_VIS, STRDESC("AV_SPARC_VIS"),
129 		STRDESC("VIS"), STRDESC("vis"),
130 	},
131 	{						/* 0x00000040 */
132 		AV_SPARC_VIS2, STRDESC("AV_SPARC_VIS2"),
133 		STRDESC("VIS2"), STRDESC("vis2"),
134 	},
135 	{						/* 0x00000080 */
136 		AV_SPARC_ASI_BLK_INIT, STRDESC("AV_SPARC_ASI_BLK_INIT"),
137 		STRDESC("ASI_BLK_INIT"), STRDESC("asi_blk_init"),
138 	},
139 	{						/* 0x00000100 */
140 		AV_SPARC_FMAF, STRDESC("AV_SPARC_FMAF"),
141 		STRDESC("FMAF"), STRDESC("fmaf"),
142 	},
143 	{						/* 0x00000200 */
144 		AV_SPARC_FMAU, STRDESC("AV_SPARC_FMAU"),
145 		STRDESC("FMAU"), STRDESC("fmau"),
146 	},
147 	{						/* 0x00000400 */
148 		AV_SPARC_VIS3, STRDESC("AV_SPARC_VIS3"),
149 		STRDESC("VIS3"), STRDESC("vis3"),
150 	},
151 	{						/* 0x00000800 */
152 		AV_SPARC_HPC, STRDESC("AV_SPARC_HPC"),
153 		STRDESC("HPC"), STRDESC("hpc"),
154 	},
155 	{						/* 0x00001000 */
156 		AV_SPARC_RANDOM, STRDESC("AV_SPARC_RANDOM"),
157 		STRDESC("RANDOM"), STRDESC("random"),
158 	},
159 	{						/* 0x00002000 */
160 		AV_SPARC_TRANS, STRDESC("AV_SPARC_TRANS"),
161 		STRDESC("TRANS"), STRDESC("trans"),
162 	},
163 	{						/* 0x00004000 */
164 		AV_SPARC_FJFMAU, STRDESC("AV_SPARC_FJFMAU"),
165 		STRDESC("FJFMAU"), STRDESC("fjfmau"),
166 	},
167 	{						/* 0x00008000 */
168 		AV_SPARC_IMA, STRDESC("AV_SPARC_IMA"),
169 		STRDESC("IMA"), STRDESC("ima"),
170 	}
171 };
172 
173 
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 elfcap_desc_t hw1_386[ELFCAP_NUM_HW1_386] = {
180 	{						/* 0x00000001 */
181 		AV_386_FPU, STRDESC("AV_386_FPU"),
182 		STRDESC("FPU"), STRDESC("fpu"),
183 	},
184 	{						/* 0x00000002 */
185 		AV_386_TSC, STRDESC("AV_386_TSC"),
186 		STRDESC("TSC"), STRDESC("tsc"),
187 	},
188 	{						/* 0x00000004 */
189 		AV_386_CX8, STRDESC("AV_386_CX8"),
190 		STRDESC("CX8"), STRDESC("cx8"),
191 	},
192 	{						/* 0x00000008 */
193 		AV_386_SEP, STRDESC("AV_386_SEP"),
194 		STRDESC("SEP"), STRDESC("sep"),
195 	},
196 	{						/* 0x00000010 */
197 		AV_386_AMD_SYSC, STRDESC("AV_386_AMD_SYSC"),
198 		STRDESC("AMD_SYSC"), STRDESC("amd_sysc"),
199 	},
200 	{						/* 0x00000020 */
201 		AV_386_CMOV, STRDESC("AV_386_CMOV"),
202 		STRDESC("CMOV"), STRDESC("cmov"),
203 	},
204 	{						/* 0x00000040 */
205 		AV_386_MMX, STRDESC("AV_386_MMX"),
206 		STRDESC("MMX"), STRDESC("mmx"),
207 	},
208 	{						/* 0x00000080 */
209 		AV_386_AMD_MMX, STRDESC("AV_386_AMD_MMX"),
210 		STRDESC("AMD_MMX"), STRDESC("amd_mmx"),
211 	},
212 	{						/* 0x00000100 */
213 		AV_386_AMD_3DNow, STRDESC("AV_386_AMD_3DNow"),
214 		STRDESC("AMD_3DNow"), STRDESC("amd_3dnow"),
215 	},
216 	{						/* 0x00000200 */
217 		AV_386_AMD_3DNowx, STRDESC("AV_386_AMD_3DNowx"),
218 		STRDESC("AMD_3DNowx"), STRDESC("amd_3dnowx"),
219 	},
220 	{						/* 0x00000400 */
221 		AV_386_FXSR, STRDESC("AV_386_FXSR"),
222 		STRDESC("FXSR"), STRDESC("fxsr"),
223 	},
224 	{						/* 0x00000800 */
225 		AV_386_SSE, STRDESC("AV_386_SSE"),
226 		STRDESC("SSE"), STRDESC("sse"),
227 	},
228 	{						/* 0x00001000 */
229 		AV_386_SSE2, STRDESC("AV_386_SSE2"),
230 		STRDESC("SSE2"), STRDESC("sse2"),
231 	},
232 	{						/* 0x00002000 */
233 		AV_386_PAUSE, STRDESC("AV_386_PAUSE"),
234 		STRDESC("PAUSE"), STRDESC("pause"),
235 	},
236 	{						/* 0x00004000 */
237 		AV_386_SSE3, STRDESC("AV_386_SSE3"),
238 		STRDESC("SSE3"), STRDESC("sse3"),
239 	},
240 	{						/* 0x00008000 */
241 		AV_386_MON, STRDESC("AV_386_MON"),
242 		STRDESC("MON"), STRDESC("mon"),
243 	},
244 	{						/* 0x00010000 */
245 		AV_386_CX16, STRDESC("AV_386_CX16"),
246 		STRDESC("CX16"), STRDESC("cx16"),
247 	},
248 	{						/* 0x00020000 */
249 		AV_386_AHF, STRDESC("AV_386_AHF"),
250 		STRDESC("AHF"), STRDESC("ahf"),
251 	},
252 	{						/* 0x00040000 */
253 		AV_386_TSCP, STRDESC("AV_386_TSCP"),
254 		STRDESC("TSCP"), STRDESC("tscp"),
255 	},
256 	{						/* 0x00080000 */
257 		AV_386_AMD_SSE4A, STRDESC("AV_386_AMD_SSE4A"),
258 		STRDESC("AMD_SSE4A"), STRDESC("amd_sse4a"),
259 	},
260 	{						/* 0x00100000 */
261 		AV_386_POPCNT, STRDESC("AV_386_POPCNT"),
262 		STRDESC("POPCNT"), STRDESC("popcnt"),
263 	},
264 	{						/* 0x00200000 */
265 		AV_386_AMD_LZCNT, STRDESC("AV_386_AMD_LZCNT"),
266 		STRDESC("AMD_LZCNT"), STRDESC("amd_lzcnt"),
267 	},
268 	{						/* 0x00400000 */
269 		AV_386_SSSE3, STRDESC("AV_386_SSSE3"),
270 		STRDESC("SSSE3"), STRDESC("ssse3"),
271 	},
272 	{						/* 0x00800000 */
273 		AV_386_SSE4_1, STRDESC("AV_386_SSE4_1"),
274 		STRDESC("SSE4.1"), STRDESC("sse4.1"),
275 	},
276 	{						/* 0x01000000 */
277 		AV_386_SSE4_2, STRDESC("AV_386_SSE4_2"),
278 		STRDESC("SSE4.2"), STRDESC("sse4.2"),
279 	}
280 };
281 
282 /*
283  * Concatenate a token to the string buffer.  This can be a capabilities token
284  * or a separator token.
285  */
286 static elfcap_err_t
287 token(char **ostr, size_t *olen, const elfcap_str_t *nstr)
288 {
289 	if (*olen < nstr->s_len)
290 		return (ELFCAP_ERR_BUFOVFL);
291 
292 	(void) strcat(*ostr, nstr->s_str);
293 	*ostr += nstr->s_len;
294 	*olen -= nstr->s_len;
295 
296 	return (ELFCAP_ERR_NONE);
297 }
298 
299 static elfcap_err_t
300 get_str_desc(elfcap_style_t style, const elfcap_desc_t *cdp,
301     const elfcap_str_t **ret_str)
302 {
303 	switch (style) {
304 	case ELFCAP_STYLE_FULL:
305 		*ret_str = &cdp->c_full;
306 		break;
307 	case ELFCAP_STYLE_UC:
308 		*ret_str = &cdp->c_uc;
309 		break;
310 	case ELFCAP_STYLE_LC:
311 		*ret_str = &cdp->c_lc;
312 		break;
313 	default:
314 		return (ELFCAP_ERR_INVSTYLE);
315 	}
316 
317 	return (ELFCAP_ERR_NONE);
318 }
319 
320 
321 /*
322  * Expand a capabilities value into the strings defined in the associated
323  * capabilities descriptor.
324  */
325 static elfcap_err_t
326 expand(elfcap_style_t style, uint64_t val, const elfcap_desc_t *cdp,
327     uint_t cnum, char *str, size_t slen, elfcap_fmt_t fmt)
328 {
329 	uint_t			cnt;
330 	int			follow = 0, err;
331 	const elfcap_str_t	*nstr;
332 
333 	if (val == 0)
334 		return (ELFCAP_ERR_NONE);
335 
336 	for (cnt = cnum; cnt > 0; cnt--) {
337 		uint_t mask = cdp[cnt - 1].c_val;
338 
339 		if ((val & mask) != 0) {
340 			if (follow++ && ((err = token(&str, &slen,
341 			    &format[fmt])) != ELFCAP_ERR_NONE))
342 				return (err);
343 
344 			err = get_str_desc(style, &cdp[cnt - 1], &nstr);
345 			if (err != ELFCAP_ERR_NONE)
346 				return (err);
347 			if ((err = token(&str, &slen, nstr)) != ELFCAP_ERR_NONE)
348 				return (err);
349 
350 			val = val & ~mask;
351 		}
352 	}
353 
354 	/*
355 	 * If there are any unknown bits remaining display the numeric value.
356 	 */
357 	if (val) {
358 		if (follow && ((err = token(&str, &slen, &format[fmt])) !=
359 		    ELFCAP_ERR_NONE))
360 			return (err);
361 
362 		(void) snprintf(str, slen, "0x%llx", val);
363 	}
364 	return (ELFCAP_ERR_NONE);
365 }
366 
367 /*
368  * Expand a CA_SUNW_HW_1 value.
369  */
370 elfcap_err_t
371 elfcap_hw1_to_str(elfcap_style_t style, uint64_t val, char *str,
372     size_t len, elfcap_fmt_t fmt, ushort_t mach)
373 {
374 	/*
375 	 * Initialize the string buffer, and validate the format request.
376 	 */
377 	*str = '\0';
378 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
379 		return (ELFCAP_ERR_INVFMT);
380 
381 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
382 		return (expand(style, val, &hw1_386[0], ELFCAP_NUM_HW1_386,
383 		    str, len, fmt));
384 
385 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
386 	    (mach == EM_SPARCV9))
387 		return (expand(style, val, hw1_sparc, ELFCAP_NUM_HW1_SPARC,
388 		    str, len, fmt));
389 
390 	return (ELFCAP_ERR_UNKMACH);
391 }
392 
393 /*
394  * Expand a CA_SUNW_SF_1 value.  Note, that at present these capabilities are
395  * common across all platforms.  The use of "mach" is therefore redundant, but
396  * is retained for compatibility with the interface of elfcap_hw1_to_str(), and
397  * possible future expansion.
398  */
399 elfcap_err_t
400 /* ARGSUSED4 */
401 elfcap_sf1_to_str(elfcap_style_t style, uint64_t val, char *str,
402     size_t len, elfcap_fmt_t fmt, ushort_t mach)
403 {
404 	/*
405 	 * Initialize the string buffer, and validate the format request.
406 	 */
407 	*str = '\0';
408 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
409 		return (ELFCAP_ERR_INVFMT);
410 
411 	return (expand(style, val, &sf1[0], ELFCAP_NUM_SF1, str, len, fmt));
412 }
413 
414 /*
415  * Given a capability tag type and value, map it to a string representation.
416  */
417 elfcap_err_t
418 elfcap_tag_to_str(elfcap_style_t style, uint64_t tag, uint64_t val,
419     char *str, size_t len, elfcap_fmt_t fmt, ushort_t mach)
420 {
421 	if (tag == CA_SUNW_HW_1)
422 		return (elfcap_hw1_to_str(style, val, str, len, fmt, mach));
423 	if (tag == CA_SUNW_SF_1)
424 		return (elfcap_sf1_to_str(style, val, str, len, fmt, mach));
425 
426 	return (ELFCAP_ERR_UNKTAG);
427 }
428 
429 /*
430  * Determine a capabilities value from a capabilities string.
431  */
432 static uint64_t
433 value(elfcap_style_t style, const char *str, const elfcap_desc_t *cdp,
434     uint_t cnum)
435 {
436 	const elfcap_str_t	*nstr;
437 	uint_t	num;
438 	int	err;
439 
440 	for (num = 0; num < cnum; num++) {
441 		/*
442 		 * Skip "reserved" bits. These are unassigned bits in the
443 		 * middle of the assigned range.
444 		 */
445 		if (cdp[num].c_val == 0)
446 			continue;
447 
448 		if ((err = get_str_desc(style, &cdp[num], &nstr)) != 0)
449 			return (err);
450 		if (strcmp(str, nstr->s_str) == 0)
451 			return (cdp[num].c_val);
452 	}
453 	return (0);
454 }
455 
456 uint64_t
457 elfcap_sf1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
458 {
459 	return (value(style, str, &sf1[0], ELFCAP_NUM_SF1));
460 }
461 
462 uint64_t
463 elfcap_hw1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
464 {
465 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
466 		return (value(style, str, &hw1_386[0], ELFCAP_NUM_HW1_386));
467 
468 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
469 	    (mach == EM_SPARCV9))
470 		return (value(style, str, hw1_sparc, ELFCAP_NUM_HW1_SPARC));
471 
472 	return (0);
473 }
474 
475 /*
476  * These functions allow the caller to get direct access to the
477  * cap descriptors.
478  */
479 const elfcap_desc_t *
480 elfcap_getdesc_hw1_sparc(void)
481 {
482 	return (hw1_sparc);
483 }
484 
485 const elfcap_desc_t *
486 elfcap_getdesc_hw1_386(void)
487 {
488 	return (hw1_386);
489 }
490 
491 const elfcap_desc_t *
492 elfcap_getdesc_sf1(void)
493 {
494 	return (sf1);
495 }
496