xref: /illumos-gate/usr/src/common/elfcap/elfcap.c (revision 90f7985f020eb82d06bd0d75396ff794105f7528)
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 (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2019, Joyent, Inc.
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 	/* 0x02000 withdrawn - do not assign */
239 	{						/* 0x00004000 */
240 		AV_386_SSE3, STRDESC("AV_386_SSE3"),
241 		STRDESC("SSE3"), STRDESC("sse3"),
242 	},
243 	/* 0x08000 withdrawn - do not assign */
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 	{						/* 0x02000000 */
281 		AV_386_MOVBE, STRDESC("AV_386_MOVBE"),
282 		STRDESC("MOVBE"), STRDESC("movbe"),
283 	},
284 	{						/* 0x04000000 */
285 		AV_386_AES, STRDESC("AV_386_AES"),
286 		STRDESC("AES"), STRDESC("aes"),
287 	},
288 	{						/* 0x08000000 */
289 		AV_386_PCLMULQDQ, STRDESC("AV_386_PCLMULQDQ"),
290 		STRDESC("PCLMULQDQ"), STRDESC("pclmulqdq"),
291 	},
292 	{						/* 0x10000000 */
293 		AV_386_XSAVE, STRDESC("AV_386_XSAVE"),
294 		STRDESC("XSAVE"), STRDESC("xsave"),
295 	},
296 	{						/* 0x20000000 */
297 		AV_386_AVX, STRDESC("AV_386_AVX"),
298 		STRDESC("AVX"), STRDESC("avx"),
299 	},
300 	{						/* 0x40000000 */
301 		AV_386_VMX, STRDESC("AV_386_VMX"),
302 		STRDESC("VMX"), STRDESC("vmx"),
303 	},
304 	{						/* 0x80000000 */
305 		AV_386_AMD_SVM, STRDESC("AV_386_AMD_SVM"),
306 		STRDESC("AMD_SVM"), STRDESC("amd_svm"),
307 	}
308 };
309 
310 static const elfcap_desc_t hw2_386[ELFCAP_NUM_HW2_386] = {
311 	{						/* 0x00000001 */
312 		AV_386_2_F16C, STRDESC("AV_386_2_F16C"),
313 		STRDESC("F16C"), STRDESC("f16c"),
314 	},
315 	{						/* 0x00000002 */
316 		AV_386_2_RDRAND, STRDESC("AV_386_2_RDRAND"),
317 		STRDESC("RDRAND"), STRDESC("rdrand"),
318 	},
319 	{						/* 0x00000004 */
320 		AV_386_2_BMI1, STRDESC("AV_386_2_BMI1"),
321 		STRDESC("BMI1"), STRDESC("bmi1"),
322 	},
323 	{						/* 0x00000008 */
324 		AV_386_2_BMI2, STRDESC("AV_386_2_BMI2"),
325 		STRDESC("BMI2"), STRDESC("bmi2"),
326 	},
327 	{						/* 0x00000010 */
328 		AV_386_2_FMA, STRDESC("AV_386_2_FMA"),
329 		STRDESC("FMA"), STRDESC("fma"),
330 	},
331 	{						/* 0x00000020 */
332 		AV_386_2_AVX2, STRDESC("AV_386_2_AVX2"),
333 		STRDESC("AVX2"), STRDESC("avx2"),
334 	},
335 	{						/* 0x00000040 */
336 		AV_386_2_ADX, STRDESC("AV_386_2_ADX"),
337 		STRDESC("ADX"), STRDESC("adx"),
338 	},
339 	{						/* 0x00000080 */
340 		AV_386_2_RDSEED, STRDESC("AV_386_2_RDSEED"),
341 		STRDESC("RDSEED"), STRDESC("rdseed"),
342 	},
343 	{						/* 0x00000100 */
344 		AV_386_2_AVX512F, STRDESC("AV_386_2_AVX512F"),
345 		STRDESC("AVX512F"), STRDESC("avx512f"),
346 	},
347 	{						/* 0x00000200 */
348 		AV_386_2_AVX512DQ, STRDESC("AV_386_2_AVX512DQ"),
349 		STRDESC("AVX512DQ"), STRDESC("avx512dq"),
350 	},
351 	{						/* 0x00000400 */
352 		AV_386_2_AVX512IFMA, STRDESC("AV_386_2_AVX512IFMA"),
353 		STRDESC("AVX512IFMA"), STRDESC("avx512ifma"),
354 	},
355 	{						/* 0x00000800 */
356 		AV_386_2_AVX512PF, STRDESC("AV_386_2_AVX512PF"),
357 		STRDESC("AVX512PF"), STRDESC("avx512pf"),
358 	},
359 	{						/* 0x00001000 */
360 		AV_386_2_AVX512ER, STRDESC("AV_386_2_AVX512ER"),
361 		STRDESC("AVX512ER"), STRDESC("avx512er"),
362 	},
363 	{						/* 0x00002000 */
364 		AV_386_2_AVX512CD, STRDESC("AV_386_2_AVX512CD"),
365 		STRDESC("AVX512CD"), STRDESC("avx512cd"),
366 	},
367 	{						/* 0x00004000 */
368 		AV_386_2_AVX512BW, STRDESC("AV_386_2_AVX512BW"),
369 		STRDESC("AVX512BW"), STRDESC("avx512bw"),
370 	},
371 	{						/* 0x00008000 */
372 		AV_386_2_AVX512VL, STRDESC("AV_386_2_AVX512VL"),
373 		STRDESC("AVX512VL"), STRDESC("avx512vl"),
374 	},
375 	{						/* 0x00010000 */
376 		AV_386_2_AVX512VBMI, STRDESC("AV_386_2_AVX512VBMI"),
377 		STRDESC("AVX512VBMI"), STRDESC("avx512vbmi"),
378 	},
379 	{						/* 0x00020000 */
380 		AV_386_2_AVX512VPOPCDQ, STRDESC("AV_386_2_AVX512_VPOPCDQ"),
381 		STRDESC("AVX512_VPOPCDQ"), STRDESC("avx512_vpopcntdq"),
382 	},
383 	{						/* 0x00040000 */
384 		AV_386_2_AVX512_4NNIW, STRDESC("AV_386_2_AVX512_4NNIW"),
385 		STRDESC("AVX512_4NNIW"), STRDESC("avx512_4nniw"),
386 	},
387 	{						/* 0x00080000 */
388 		AV_386_2_AVX512_4FMAPS, STRDESC("AV_386_2_AVX512_4FMAPS"),
389 		STRDESC("AVX512_4FMAPS"), STRDESC("avx512_4fmaps"),
390 	},
391 	{						/* 0x00100000 */
392 		AV_386_2_SHA, STRDESC("AV_386_2_SHA"),
393 		STRDESC("SHA"), STRDESC("sha"),
394 	},
395 	{						/* 0x00200000 */
396 		AV_386_2_FSGSBASE, STRDESC("AV_386_2_FSGSBASE"),
397 		STRDESC("FSGSBASE"), STRDESC("fsgsbase")
398 	},
399 	{						/* 0x00400000 */
400 		AV_386_2_CLFLUSHOPT, STRDESC("AV_386_2_CLFLUSHOPT"),
401 		STRDESC("CLFLUSHOPT"), STRDESC("clflushopt")
402 	},
403 	{						/* 0x00800000 */
404 		AV_386_2_CLWB, STRDESC("AV_386_2_CLWB"),
405 		STRDESC("CLWB"), STRDESC("clwb")
406 	},
407 	{						/* 0x01000000 */
408 		AV_386_2_MONITORX, STRDESC("AV_386_2_MONITORX"),
409 		STRDESC("MONITORX"), STRDESC("monitorx")
410 	},
411 	{						/* 0x02000000 */
412 		AV_386_2_CLZERO, STRDESC("AV_386_2_CLZERO"),
413 		STRDESC("CLZERO"), STRDESC("clzero")
414 	},
415 	{						/* 0x04000000 */
416 		AV_386_2_AVX512_VNNI, STRDESC("AV_386_2_AVX512_VNNI"),
417 		STRDESC("AVX512_VNNI"), STRDESC("avx512_vnni")
418 	},
419 	{						/* 0x08000000 */
420 		AV_386_2_VPCLMULQDQ, STRDESC("AV_386_2_VPCLMULQDQ"),
421 		STRDESC("VPCLMULQDQ"), STRDESC("vpclmulqdq")
422 	},
423 	{						/* 0x10000000 */
424 		AV_386_2_VAES, STRDESC("AV_386_2_VAES"),
425 		STRDESC("VAES"), STRDESC("vaes")
426 	}
427 };
428 
429 /*
430  * Concatenate a token to the string buffer.  This can be a capabilities token
431  * or a separator token.
432  */
433 static elfcap_err_t
434 token(char **ostr, size_t *olen, const elfcap_str_t *nstr)
435 {
436 	if (*olen < nstr->s_len)
437 		return (ELFCAP_ERR_BUFOVFL);
438 
439 	(void) strcat(*ostr, nstr->s_str);
440 	*ostr += nstr->s_len;
441 	*olen -= nstr->s_len;
442 
443 	return (ELFCAP_ERR_NONE);
444 }
445 
446 static elfcap_err_t
447 get_str_desc(elfcap_style_t style, const elfcap_desc_t *cdp,
448     const elfcap_str_t **ret_str)
449 {
450 	switch (ELFCAP_STYLE_MASK(style)) {
451 	case ELFCAP_STYLE_FULL:
452 		*ret_str = &cdp->c_full;
453 		break;
454 	case ELFCAP_STYLE_UC:
455 		*ret_str = &cdp->c_uc;
456 		break;
457 	case ELFCAP_STYLE_LC:
458 		*ret_str = &cdp->c_lc;
459 		break;
460 	default:
461 		return (ELFCAP_ERR_INVSTYLE);
462 	}
463 
464 	return (ELFCAP_ERR_NONE);
465 }
466 
467 
468 /*
469  * Expand a capabilities value into the strings defined in the associated
470  * capabilities descriptor.
471  */
472 static elfcap_err_t
473 expand(elfcap_style_t style, elfcap_mask_t val, const elfcap_desc_t *cdp,
474     uint_t cnum, char *str, size_t slen, elfcap_fmt_t fmt)
475 {
476 	uint_t			cnt;
477 	int			follow = 0, err;
478 	const elfcap_str_t	*nstr;
479 
480 	if (val == 0)
481 		return (ELFCAP_ERR_NONE);
482 
483 	for (cnt = cnum; cnt > 0; cnt--) {
484 		uint_t mask = cdp[cnt - 1].c_val;
485 
486 		if ((val & mask) != 0) {
487 			if (follow++ && ((err = token(&str, &slen,
488 			    &format[fmt])) != ELFCAP_ERR_NONE))
489 				return (err);
490 
491 			err = get_str_desc(style, &cdp[cnt - 1], &nstr);
492 			if (err != ELFCAP_ERR_NONE)
493 				return (err);
494 			if ((err = token(&str, &slen, nstr)) != ELFCAP_ERR_NONE)
495 				return (err);
496 
497 			val = val & ~mask;
498 		}
499 	}
500 
501 	/*
502 	 * If there are any unknown bits remaining display the numeric value.
503 	 */
504 	if (val) {
505 		if (follow && ((err = token(&str, &slen, &format[fmt])) !=
506 		    ELFCAP_ERR_NONE))
507 			return (err);
508 
509 		(void) snprintf(str, slen, "0x%x", val);
510 	}
511 	return (ELFCAP_ERR_NONE);
512 }
513 
514 /*
515  * Expand a CA_SUNW_HW_1 value.
516  */
517 elfcap_err_t
518 elfcap_hw1_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
519     size_t len, elfcap_fmt_t fmt, ushort_t mach)
520 {
521 	/*
522 	 * Initialize the string buffer, and validate the format request.
523 	 */
524 	*str = '\0';
525 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
526 		return (ELFCAP_ERR_INVFMT);
527 
528 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
529 		return (expand(style, val, &hw1_386[0], ELFCAP_NUM_HW1_386,
530 		    str, len, fmt));
531 
532 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
533 	    (mach == EM_SPARCV9))
534 		return (expand(style, val, hw1_sparc, ELFCAP_NUM_HW1_SPARC,
535 		    str, len, fmt));
536 
537 	return (ELFCAP_ERR_UNKMACH);
538 }
539 
540 /*
541  * Expand a CA_SUNW_HW_2 value.
542  */
543 elfcap_err_t
544 elfcap_hw2_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
545     size_t len, elfcap_fmt_t fmt, ushort_t mach)
546 {
547 	/*
548 	 * Initialize the string buffer, and validate the format request.
549 	 */
550 	*str = '\0';
551 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
552 		return (ELFCAP_ERR_INVFMT);
553 
554 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
555 		return (expand(style, val, &hw2_386[0], ELFCAP_NUM_HW2_386,
556 		    str, len, fmt));
557 
558 	return (expand(style, val, NULL, 0, str, len, fmt));
559 }
560 
561 /*
562  * Expand a CA_SUNW_SF_1 value.  Note, that at present these capabilities are
563  * common across all platforms.  The use of "mach" is therefore redundant, but
564  * is retained for compatibility with the interface of elfcap_hw1_to_str(), and
565  * possible future expansion.
566  */
567 elfcap_err_t
568 /* ARGSUSED4 */
569 elfcap_sf1_to_str(elfcap_style_t style, elfcap_mask_t val, char *str,
570     size_t len, elfcap_fmt_t fmt, ushort_t mach)
571 {
572 	/*
573 	 * Initialize the string buffer, and validate the format request.
574 	 */
575 	*str = '\0';
576 	if ((fmt < 0) || (fmt >= FORMAT_NELTS))
577 		return (ELFCAP_ERR_INVFMT);
578 
579 	return (expand(style, val, &sf1[0], ELFCAP_NUM_SF1, str, len, fmt));
580 }
581 
582 /*
583  * Given a capability tag type and value, map it to a string representation.
584  */
585 elfcap_err_t
586 elfcap_tag_to_str(elfcap_style_t style, uint64_t tag, elfcap_mask_t val,
587     char *str, size_t len, elfcap_fmt_t fmt, ushort_t mach)
588 {
589 	switch (tag) {
590 	case CA_SUNW_HW_1:
591 		return (elfcap_hw1_to_str(style, val, str, len, fmt, mach));
592 
593 	case CA_SUNW_SF_1:
594 		return (elfcap_sf1_to_str(style, val, str, len, fmt, mach));
595 
596 	case CA_SUNW_HW_2:
597 		return (elfcap_hw2_to_str(style, val, str, len, fmt, mach));
598 
599 	}
600 
601 	return (ELFCAP_ERR_UNKTAG);
602 }
603 
604 /*
605  * Determine a capabilities value from a capabilities string.
606  */
607 static elfcap_mask_t
608 value(elfcap_style_t style, const char *str, const elfcap_desc_t *cdp,
609     uint_t cnum)
610 {
611 	const elfcap_str_t	*nstr;
612 	uint_t	num;
613 	int	err;
614 
615 	for (num = 0; num < cnum; num++) {
616 		/*
617 		 * Skip "reserved" bits. These are unassigned bits in the
618 		 * middle of the assigned range.
619 		 */
620 		if (cdp[num].c_val == 0)
621 			continue;
622 
623 		if ((err = get_str_desc(style, &cdp[num], &nstr)) != 0)
624 			return (err);
625 		if (style & ELFCAP_STYLE_F_ICMP) {
626 			if (strcasecmp(str, nstr->s_str) == 0)
627 				return (cdp[num].c_val);
628 		} else {
629 			if (strcmp(str, nstr->s_str) == 0)
630 				return (cdp[num].c_val);
631 		}
632 	}
633 
634 	return (0);
635 }
636 
637 elfcap_mask_t
638 elfcap_sf1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
639 {
640 	return (value(style, str, &sf1[0], ELFCAP_NUM_SF1));
641 }
642 
643 elfcap_mask_t
644 elfcap_hw1_from_str(elfcap_style_t style, const char *str, ushort_t mach)
645 {
646 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
647 		return (value(style, str, &hw1_386[0], ELFCAP_NUM_HW1_386));
648 
649 	if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) ||
650 	    (mach == EM_SPARCV9))
651 		return (value(style, str, hw1_sparc, ELFCAP_NUM_HW1_SPARC));
652 
653 	return (0);
654 }
655 elfcap_mask_t
656 elfcap_hw2_from_str(elfcap_style_t style, const char *str, ushort_t mach)
657 {
658 	if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64))
659 		return (value(style, str, &hw2_386[0], ELFCAP_NUM_HW2_386));
660 
661 	return (0);
662 }
663 
664 /*
665  * Given a capability tag type and value, return the capabilities values
666  * contained in the string.
667  */
668 elfcap_mask_t
669 elfcap_tag_from_str(elfcap_style_t style, uint64_t tag, const char *str,
670     ushort_t mach)
671 {
672 	switch (tag) {
673 	case CA_SUNW_HW_1:
674 		return (elfcap_hw1_from_str(style, str, mach));
675 
676 	case CA_SUNW_SF_1:
677 		return (elfcap_sf1_from_str(style, str, mach));
678 
679 	case CA_SUNW_HW_2:
680 		return (elfcap_hw2_from_str(style, str, mach));
681 	}
682 
683 	return (0);
684 }
685 
686 /*
687  * These functions allow the caller to get direct access to the
688  * cap descriptors.
689  */
690 const elfcap_desc_t *
691 elfcap_getdesc_hw1_sparc(void)
692 {
693 	return (hw1_sparc);
694 }
695 
696 const elfcap_desc_t *
697 elfcap_getdesc_hw1_386(void)
698 {
699 	return (hw1_386);
700 }
701 
702 const elfcap_desc_t *
703 elfcap_getdesc_sf1(void)
704 {
705 	return (sf1);
706 }
707