xref: /titanic_52/usr/src/common/elfcap/elfcap.c (revision 69112edd987c28fa551d4f8d9362a84a45365f17)
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 2010 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 	{						/* 0x00000004 */
99 		SF1_SUNW_ADDR32, STRDESC("SF1_SUNW_ADDR32"),
100 		STRDESC("ADDR32"), STRDESC("addr32"),
101 	}
102 };
103 
104 
105 
106 /*
107  * Order the SPARC hardware capabilities to match their numeric value.  See
108  * AV_SPARC_ values in sys/auxv_SPARC.h.
109  */
110 static const elfcap_desc_t hw1_sparc[ELFCAP_NUM_HW1_SPARC] = {
111 	{						/* 0x00000001 */
112 		AV_SPARC_MUL32, STRDESC("AV_SPARC_MUL32"),
113 		STRDESC("MUL32"), STRDESC("mul32"),
114 	},
115 	{						/* 0x00000002 */
116 		AV_SPARC_DIV32, STRDESC("AV_SPARC_DIV32"),
117 		STRDESC("DIV32"), STRDESC("div32"),
118 	},
119 	{						/* 0x00000004 */
120 		AV_SPARC_FSMULD, STRDESC("AV_SPARC_FSMULD"),
121 		STRDESC("FSMULD"), STRDESC("fsmuld"),
122 	},
123 	{						/* 0x00000008 */
124 		AV_SPARC_V8PLUS, STRDESC("AV_SPARC_V8PLUS"),
125 		STRDESC("V8PLUS"), STRDESC("v8plus"),
126 	},
127 	{						/* 0x00000010 */
128 		AV_SPARC_POPC, STRDESC("AV_SPARC_POPC"),
129 		STRDESC("POPC"), STRDESC("popc"),
130 	},
131 	{						/* 0x00000020 */
132 		AV_SPARC_VIS, STRDESC("AV_SPARC_VIS"),
133 		STRDESC("VIS"), STRDESC("vis"),
134 	},
135 	{						/* 0x00000040 */
136 		AV_SPARC_VIS2, STRDESC("AV_SPARC_VIS2"),
137 		STRDESC("VIS2"), STRDESC("vis2"),
138 	},
139 	{						/* 0x00000080 */
140 		AV_SPARC_ASI_BLK_INIT, STRDESC("AV_SPARC_ASI_BLK_INIT"),
141 		STRDESC("ASI_BLK_INIT"), STRDESC("asi_blk_init"),
142 	},
143 	{						/* 0x00000100 */
144 		AV_SPARC_FMAF, STRDESC("AV_SPARC_FMAF"),
145 		STRDESC("FMAF"), STRDESC("fmaf"),
146 	},
147 	RESERVED_ELFCAP_DESC,				/* 0x00000200 */
148 	{						/* 0x00000400 */
149 		AV_SPARC_VIS3, STRDESC("AV_SPARC_VIS3"),
150 		STRDESC("VIS3"), STRDESC("vis3"),
151 	},
152 	{						/* 0x00000800 */
153 		AV_SPARC_HPC, STRDESC("AV_SPARC_HPC"),
154 		STRDESC("HPC"), STRDESC("hpc"),
155 	},
156 	{						/* 0x00001000 */
157 		AV_SPARC_RANDOM, STRDESC("AV_SPARC_RANDOM"),
158 		STRDESC("RANDOM"), STRDESC("random"),
159 	},
160 	{						/* 0x00002000 */
161 		AV_SPARC_TRANS, STRDESC("AV_SPARC_TRANS"),
162 		STRDESC("TRANS"), STRDESC("trans"),
163 	},
164 	{						/* 0x00004000 */
165 		AV_SPARC_FJFMAU, STRDESC("AV_SPARC_FJFMAU"),
166 		STRDESC("FJFMAU"), STRDESC("fjfmau"),
167 	},
168 	{						/* 0x00008000 */
169 		AV_SPARC_IMA, STRDESC("AV_SPARC_IMA"),
170 		STRDESC("IMA"), STRDESC("ima"),
171 	},
172 	{						/* 0x00010000 */
173 		AV_SPARC_ASI_CACHE_SPARING,
174 		STRDESC("AV_SPARC_ASI_CACHE_SPARING"),
175 		STRDESC("CSPARE"), STRDESC("cspare"),
176 	}
177 };
178 
179 
180 
181 /*
182  * Order the Intel hardware capabilities to match their numeric value.  See
183  * AV_386_ values in sys/auxv_386.h.
184  */
185 static const elfcap_desc_t hw1_386[ELFCAP_NUM_HW1_386] = {
186 	{						/* 0x00000001 */
187 		AV_386_FPU, STRDESC("AV_386_FPU"),
188 		STRDESC("FPU"), STRDESC("fpu"),
189 	},
190 	{						/* 0x00000002 */
191 		AV_386_TSC, STRDESC("AV_386_TSC"),
192 		STRDESC("TSC"), STRDESC("tsc"),
193 	},
194 	{						/* 0x00000004 */
195 		AV_386_CX8, STRDESC("AV_386_CX8"),
196 		STRDESC("CX8"), STRDESC("cx8"),
197 	},
198 	{						/* 0x00000008 */
199 		AV_386_SEP, STRDESC("AV_386_SEP"),
200 		STRDESC("SEP"), STRDESC("sep"),
201 	},
202 	{						/* 0x00000010 */
203 		AV_386_AMD_SYSC, STRDESC("AV_386_AMD_SYSC"),
204 		STRDESC("AMD_SYSC"), STRDESC("amd_sysc"),
205 	},
206 	{						/* 0x00000020 */
207 		AV_386_CMOV, STRDESC("AV_386_CMOV"),
208 		STRDESC("CMOV"), STRDESC("cmov"),
209 	},
210 	{						/* 0x00000040 */
211 		AV_386_MMX, STRDESC("AV_386_MMX"),
212 		STRDESC("MMX"), STRDESC("mmx"),
213 	},
214 	{						/* 0x00000080 */
215 		AV_386_AMD_MMX, STRDESC("AV_386_AMD_MMX"),
216 		STRDESC("AMD_MMX"), STRDESC("amd_mmx"),
217 	},
218 	{						/* 0x00000100 */
219 		AV_386_AMD_3DNow, STRDESC("AV_386_AMD_3DNow"),
220 		STRDESC("AMD_3DNow"), STRDESC("amd_3dnow"),
221 	},
222 	{						/* 0x00000200 */
223 		AV_386_AMD_3DNowx, STRDESC("AV_386_AMD_3DNowx"),
224 		STRDESC("AMD_3DNowx"), STRDESC("amd_3dnowx"),
225 	},
226 	{						/* 0x00000400 */
227 		AV_386_FXSR, STRDESC("AV_386_FXSR"),
228 		STRDESC("FXSR"), STRDESC("fxsr"),
229 	},
230 	{						/* 0x00000800 */
231 		AV_386_SSE, STRDESC("AV_386_SSE"),
232 		STRDESC("SSE"), STRDESC("sse"),
233 	},
234 	{						/* 0x00001000 */
235 		AV_386_SSE2, STRDESC("AV_386_SSE2"),
236 		STRDESC("SSE2"), STRDESC("sse2"),
237 	},
238 	{						/* 0x00002000 */
239 		AV_386_PAUSE, STRDESC("AV_386_PAUSE"),
240 		STRDESC("PAUSE"), STRDESC("pause"),
241 	},
242 	{						/* 0x00004000 */
243 		AV_386_SSE3, STRDESC("AV_386_SSE3"),
244 		STRDESC("SSE3"), STRDESC("sse3"),
245 	},
246 	{						/* 0x00008000 */
247 		AV_386_MON, STRDESC("AV_386_MON"),
248 		STRDESC("MON"), STRDESC("mon"),
249 	},
250 	{						/* 0x00010000 */
251 		AV_386_CX16, STRDESC("AV_386_CX16"),
252 		STRDESC("CX16"), STRDESC("cx16"),
253 	},
254 	{						/* 0x00020000 */
255 		AV_386_AHF, STRDESC("AV_386_AHF"),
256 		STRDESC("AHF"), STRDESC("ahf"),
257 	},
258 	{						/* 0x00040000 */
259 		AV_386_TSCP, STRDESC("AV_386_TSCP"),
260 		STRDESC("TSCP"), STRDESC("tscp"),
261 	},
262 	{						/* 0x00080000 */
263 		AV_386_AMD_SSE4A, STRDESC("AV_386_AMD_SSE4A"),
264 		STRDESC("AMD_SSE4A"), STRDESC("amd_sse4a"),
265 	},
266 	{						/* 0x00100000 */
267 		AV_386_POPCNT, STRDESC("AV_386_POPCNT"),
268 		STRDESC("POPCNT"), STRDESC("popcnt"),
269 	},
270 	{						/* 0x00200000 */
271 		AV_386_AMD_LZCNT, STRDESC("AV_386_AMD_LZCNT"),
272 		STRDESC("AMD_LZCNT"), STRDESC("amd_lzcnt"),
273 	},
274 	{						/* 0x00400000 */
275 		AV_386_SSSE3, STRDESC("AV_386_SSSE3"),
276 		STRDESC("SSSE3"), STRDESC("ssse3"),
277 	},
278 	{						/* 0x00800000 */
279 		AV_386_SSE4_1, STRDESC("AV_386_SSE4_1"),
280 		STRDESC("SSE4.1"), STRDESC("sse4.1"),
281 	},
282 	{						/* 0x01000000 */
283 		AV_386_SSE4_2, STRDESC("AV_386_SSE4_2"),
284 		STRDESC("SSE4.2"), STRDESC("sse4.2"),
285 	},
286 	{						/* 0x02000000 */
287 		AV_386_MOVBE, STRDESC("AV_386_MOVBE"),
288 		STRDESC("MOVBE"), STRDESC("movbe"),
289 	},
290 	{						/* 0x04000000 */
291 		AV_386_AES, STRDESC("AV_386_AES"),
292 		STRDESC("AES"), STRDESC("aes"),
293 	},
294 	{						/* 0x08000000 */
295 		AV_386_PCLMULQDQ, STRDESC("AV_386_PCLMULQDQ"),
296 		STRDESC("PCLMULQDQ"), STRDESC("pclmulqdq"),
297 	}
298 };
299 
300 /*
301  * Concatenate a token to the string buffer.  This can be a capabilities token
302  * or a separator token.
303  */
304 static elfcap_err_t
305 token(char **ostr, size_t *olen, const elfcap_str_t *nstr)
306 {
307 	if (*olen < nstr->s_len)
308 		return (ELFCAP_ERR_BUFOVFL);
309 
310 	(void) strcat(*ostr, nstr->s_str);
311 	*ostr += nstr->s_len;
312 	*olen -= nstr->s_len;
313 
314 	return (ELFCAP_ERR_NONE);
315 }
316 
317 static elfcap_err_t
318 get_str_desc(elfcap_style_t style, const elfcap_desc_t *cdp,
319     const elfcap_str_t **ret_str)
320 {
321 	switch (ELFCAP_STYLE_MASK(style)) {
322 	case ELFCAP_STYLE_FULL:
323 		*ret_str = &cdp->c_full;
324 		break;
325 	case ELFCAP_STYLE_UC:
326 		*ret_str = &cdp->c_uc;
327 		break;
328 	case ELFCAP_STYLE_LC:
329 		*ret_str = &cdp->c_lc;
330 		break;
331 	default:
332 		return (ELFCAP_ERR_INVSTYLE);
333 	}
334 
335 	return (ELFCAP_ERR_NONE);
336 }
337 
338 
339 /*
340  * Expand a capabilities value into the strings defined in the associated
341  * capabilities descriptor.
342  */
343 static elfcap_err_t
344 expand(elfcap_style_t style, elfcap_mask_t val, const elfcap_desc_t *cdp,
345     uint_t cnum, char *str, size_t slen, elfcap_fmt_t fmt)
346 {
347 	uint_t			cnt;
348 	int			follow = 0, err;
349 	const elfcap_str_t	*nstr;
350 
351 	if (val == 0)
352 		return (ELFCAP_ERR_NONE);
353 
354 	for (cnt = cnum; cnt > 0; cnt--) {
355 		uint_t mask = cdp[cnt - 1].c_val;
356 
357 		if ((val & mask) != 0) {
358 			if (follow++ && ((err = token(&str, &slen,
359 			    &format[fmt])) != ELFCAP_ERR_NONE))
360 				return (err);
361 
362 			err = get_str_desc(style, &cdp[cnt - 1], &nstr);
363 			if (err != ELFCAP_ERR_NONE)
364 				return (err);
365 			if ((err = token(&str, &slen, nstr)) != ELFCAP_ERR_NONE)
366 				return (err);
367 
368 			val = val & ~mask;
369 		}
370 	}
371 
372 	/*
373 	 * If there are any unknown bits remaining display the numeric value.
374 	 */
375 	if (val) {
376 		if (follow && ((err = token(&str, &slen, &format[fmt])) !=
377 		    ELFCAP_ERR_NONE))
378 			return (err);
379 
380 		(void) snprintf(str, slen, "0x%x", val);
381 	}
382 	return (ELFCAP_ERR_NONE);
383 }
384 
385 /*
386  * Expand a CA_SUNW_HW_1 value.
387  */
388 elfcap_err_t
389 elfcap_hw1_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
390     size_t len, elfcap_fmt_t fmt, ushort_t mach)
391 {
392 	/*
393 	 * Initialize the string buffer, and validate the format request.
394 	 */
395 	*str = '\0';
396 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
397 		return (ELFCAP_ERR_INVFMT);
398 
399 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
400 		return (expand(style, val, &hw1_386[0], ELFCAP_NUM_HW1_386,
401 		    str, len, fmt));
402 
403 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
404 	    (mach == EM_SPARCV9))
405 		return (expand(style, val, hw1_sparc, ELFCAP_NUM_HW1_SPARC,
406 		    str, len, fmt));
407 
408 	return (ELFCAP_ERR_UNKMACH);
409 }
410 
411 /*
412  * Expand a CA_SUNW_HW_2 value.  Presently, there are no values, this routine
413  * is simply a place holder for future development.
414  */
415 elfcap_err_t
416 /* ARGSUSED0 */
417 elfcap_hw2_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
418     size_t len, elfcap_fmt_t fmt, ushort_t mach)
419 {
420 	/*
421 	 * Initialize the string buffer, and validate the format request.
422 	 */
423 	*str = '\0';
424 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
425 		return (ELFCAP_ERR_INVFMT);
426 
427 	return (expand(style, val, NULL, 0, str, len, fmt));
428 }
429 
430 /*
431  * Expand a CA_SUNW_SF_1 value.  Note, that at present these capabilities are
432  * common across all platforms.  The use of "mach" is therefore redundant, but
433  * is retained for compatibility with the interface of elfcap_hw1_to_str(), and
434  * possible future expansion.
435  */
436 elfcap_err_t
437 /* ARGSUSED4 */
438 elfcap_sf1_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
439     size_t len, elfcap_fmt_t fmt, ushort_t mach)
440 {
441 	/*
442 	 * Initialize the string buffer, and validate the format request.
443 	 */
444 	*str = '\0';
445 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
446 		return (ELFCAP_ERR_INVFMT);
447 
448 	return (expand(style, val, &sf1[0], ELFCAP_NUM_SF1, str, len, fmt));
449 }
450 
451 /*
452  * Given a capability tag type and value, map it to a string representation.
453  */
454 elfcap_err_t
455 elfcap_tag_to_str(elfcap_style_t style, uint64_t tag, elfcap_mask_t val,
456     char *str, size_t len, elfcap_fmt_t fmt, ushort_t mach)
457 {
458 	switch (tag) {
459 	case CA_SUNW_HW_1:
460 		return (elfcap_hw1_to_str(style, val, str, len, fmt, mach));
461 
462 	case CA_SUNW_SF_1:
463 		return (elfcap_sf1_to_str(style, val, str, len, fmt, mach));
464 
465 	case CA_SUNW_HW_2:
466 		return (elfcap_hw2_to_str(style, val, str, len, fmt, mach));
467 
468 	}
469 
470 	return (ELFCAP_ERR_UNKTAG);
471 }
472 
473 /*
474  * Determine a capabilities value from a capabilities string.
475  */
476 static elfcap_mask_t
477 value(elfcap_style_t style, const char *str, const elfcap_desc_t *cdp,
478     uint_t cnum)
479 {
480 	const elfcap_str_t	*nstr;
481 	uint_t	num;
482 	int	err;
483 
484 	for (num = 0; num < cnum; num++) {
485 		/*
486 		 * Skip "reserved" bits. These are unassigned bits in the
487 		 * middle of the assigned range.
488 		 */
489 		if (cdp[num].c_val == 0)
490 			continue;
491 
492 		if ((err = get_str_desc(style, &cdp[num], &nstr)) != 0)
493 			return (err);
494 		if (style & ELFCAP_STYLE_F_ICMP) {
495 			if (strcasecmp(str, nstr->s_str) == 0)
496 				return (cdp[num].c_val);
497 		} else {
498 			if (strcmp(str, nstr->s_str) == 0)
499 				return (cdp[num].c_val);
500 		}
501 	}
502 
503 	return (0);
504 }
505 
506 elfcap_mask_t
507 elfcap_sf1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
508 {
509 	return (value(style, str, &sf1[0], ELFCAP_NUM_SF1));
510 }
511 
512 elfcap_mask_t
513 elfcap_hw1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
514 {
515 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
516 		return (value(style, str, &hw1_386[0], ELFCAP_NUM_HW1_386));
517 
518 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
519 	    (mach == EM_SPARCV9))
520 		return (value(style, str, hw1_sparc, ELFCAP_NUM_HW1_SPARC));
521 
522 	return (0);
523 }
524 elfcap_mask_t
525 /* ARGSUSED0 */
526 elfcap_hw2_from_str(elfcap_style_t style, const char *str, ushort_t mach)
527 {
528 	return (0);
529 }
530 
531 /*
532  * Given a capability tag type and value, return the capabilities values
533  * contained in the string.
534  */
535 elfcap_mask_t
536 elfcap_tag_from_str(elfcap_style_t style, uint64_t tag, const char *str,
537     ushort_t mach)
538 {
539 	switch (tag) {
540 	case CA_SUNW_HW_1:
541 		return (elfcap_hw1_from_str(style, str, mach));
542 
543 	case CA_SUNW_SF_1:
544 		return (elfcap_sf1_from_str(style, str, mach));
545 
546 	case CA_SUNW_HW_2:
547 		return (elfcap_hw2_from_str(style, str, mach));
548 	}
549 
550 	return (0);
551 }
552 
553 /*
554  * These functions allow the caller to get direct access to the
555  * cap descriptors.
556  */
557 const elfcap_desc_t *
558 elfcap_getdesc_hw1_sparc(void)
559 {
560 	return (hw1_sparc);
561 }
562 
563 const elfcap_desc_t *
564 elfcap_getdesc_hw1_386(void)
565 {
566 	return (hw1_386);
567 }
568 
569 const elfcap_desc_t *
570 elfcap_getdesc_sf1(void)
571 {
572 	return (sf1);
573 }
574