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