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