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