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