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 <limits.h> 38 #include <sys/machelf.h> 39 #include <sys/elf.h> 40 #include <sys/auxv_SPARC.h> 41 #include <sys/auxv_386.h> 42 #include <elfcap.h> 43 44 /* 45 * Define separators for val2str processing. 46 */ 47 static const Fmt_desc format[] = { 48 {" ", 1 }, 49 {" ", 2 }, 50 {" | ", 3 } 51 }; 52 53 /* 54 * Define all known capabilities as both lower and upper case strings. This 55 * duplication is necessary, rather than have one string and use something 56 * like toupper(), as a client such as ld.so.1 doesn't need the overhead of 57 * dragging in the internationalization support of toupper(). The Intel 3DNow 58 * flags are a slightly odd convention too. 59 * 60 * Define all known software capabilities. 61 */ 62 #ifdef CAP_UPPERCASE 63 static const char Sf1_fpknwn[] = "FPKNWN"; 64 static const char Sf1_fpused[] = "FPUSED"; 65 #elif CAP_LOWERCASE 66 static const char Sf1_fpknwn[] = "fpknwn"; 67 static const char Sf1_fpused[] = "fpused"; 68 #else 69 #error "Software Capabilities - what case do you want?" 70 #endif 71 72 /* 73 * Order the software capabilities to match their numeric value. See SF1_SUNW_ 74 * values in sys/elf.h. 75 */ 76 static const Cap_desc sf1[] = { 77 { SF1_SUNW_FPKNWN, Sf1_fpknwn, (sizeof (Sf1_fpknwn) - 1) }, 78 { SF1_SUNW_FPUSED, Sf1_fpused, (sizeof (Sf1_fpused) - 1) } 79 }; 80 static const uint_t sf1_num = sizeof (sf1) / sizeof (Cap_desc); 81 82 /* 83 * Define all known SPARC hardware capabilities. 84 */ 85 #ifdef CAP_UPPERCASE 86 static const char Hw1_s_mul32[] = "MUL32"; 87 static const char Hw1_s_div32[] = "DIV32"; 88 static const char Hw1_s_fsmuld[] = "FSMULD"; 89 static const char Hw1_s_v8plus[] = "V8PLUS"; 90 static const char Hw1_s_popc[] = "POPC"; 91 static const char Hw1_s_vis[] = "VIS"; 92 static const char Hw1_s_vis2[] = "VIS2"; 93 static const char Hw1_s_asi_blk_init[] = "ASI_BLK_INIT"; 94 static const char Hw1_s_fmaf[] = "FMAF"; 95 static const char Hw1_s_reserved1[] = "RESERVED1"; 96 static const char Hw1_s_reserved2[] = "RESERVED2"; 97 static const char Hw1_s_reserved3[] = "RESERVED3"; 98 static const char Hw1_s_reserved4[] = "RESERVED4"; 99 static const char Hw1_s_reserved5[] = "RESERVED5"; 100 static const char Hw1_s_fjfmau[] = "FJFMAU"; 101 #elif CAP_LOWERCASE 102 static const char Hw1_s_mul32[] = "mul32"; 103 static const char Hw1_s_div32[] = "div32"; 104 static const char Hw1_s_fsmuld[] = "fsmuld"; 105 static const char Hw1_s_v8plus[] = "v8plus"; 106 static const char Hw1_s_popc[] = "popc"; 107 static const char Hw1_s_vis[] = "vis"; 108 static const char Hw1_s_vis2[] = "vis2"; 109 static const char Hw1_s_asi_blk_init[] = "asi_blk_init"; 110 static const char Hw1_s_fmaf[] = "fmaf"; 111 static const char Hw1_s_reserved1[] = "reserved1"; 112 static const char Hw1_s_reserved2[] = "reserved2"; 113 static const char Hw1_s_reserved3[] = "reserved3"; 114 static const char Hw1_s_reserved4[] = "reserved4"; 115 static const char Hw1_s_reserved5[] = "reserved5"; 116 static const char Hw1_s_fjfmau[] = "fjfmau"; 117 118 #else 119 #error "Hardware Capabilities (sparc) - what case do you want?" 120 #endif 121 122 /* 123 * Order the SPARC hardware capabilities to match their numeric value. See 124 * AV_SPARC_ values in sys/auxv_SPARC.h. 125 */ 126 static const Cap_desc hw1_s[] = { 127 { AV_SPARC_MUL32, Hw1_s_mul32, sizeof (Hw1_s_mul32) - 1 }, 128 { AV_SPARC_DIV32, Hw1_s_div32, sizeof (Hw1_s_div32) - 1 }, 129 { AV_SPARC_FSMULD, Hw1_s_fsmuld, sizeof (Hw1_s_fsmuld) - 1 }, 130 { AV_SPARC_V8PLUS, Hw1_s_v8plus, sizeof (Hw1_s_v8plus) - 1 }, 131 { AV_SPARC_POPC, Hw1_s_popc, sizeof (Hw1_s_popc) - 1 }, 132 { AV_SPARC_VIS, Hw1_s_vis, sizeof (Hw1_s_vis) - 1 }, 133 { AV_SPARC_VIS2, Hw1_s_vis2, sizeof (Hw1_s_vis2) - 1 }, 134 { AV_SPARC_ASI_BLK_INIT, Hw1_s_asi_blk_init, 135 sizeof (Hw1_s_asi_blk_init) - 1 }, 136 { AV_SPARC_FMAF, Hw1_s_fmaf, sizeof (Hw1_s_fmaf) - 1 }, 137 { 0, Hw1_s_reserved1, sizeof (Hw1_s_reserved1) - 1 }, 138 { 0, Hw1_s_reserved2, sizeof (Hw1_s_reserved2) - 1 }, 139 { 0, Hw1_s_reserved3, sizeof (Hw1_s_reserved3) - 1 }, 140 { 0, Hw1_s_reserved4, sizeof (Hw1_s_reserved4) - 1 }, 141 { 0, Hw1_s_reserved5, sizeof (Hw1_s_reserved5) - 1 }, 142 { AV_SPARC_FJFMAU, Hw1_s_fjfmau, sizeof (Hw1_s_fjfmau) - 1 } 143 }; 144 static const uint_t hw1_s_num = sizeof (hw1_s) / sizeof (Cap_desc); 145 146 /* 147 * Define all known Intel hardware capabilities. 148 */ 149 #ifdef CAP_UPPERCASE 150 static const char Hw1_i_fpu[] = "FPU"; 151 static const char Hw1_i_tsc[] = "TSC"; 152 static const char Hw1_i_cx8[] = "CX8"; 153 static const char Hw1_i_sep[] = "SEP"; 154 static const char Hw1_i_amd_sysc[] = "AMD_SYSC"; 155 static const char Hw1_i_cmov[] = "CMOV"; 156 static const char Hw1_i_mmx[] = "MMX"; 157 static const char Hw1_i_amd_mmx[] = "AMD_MMX"; 158 static const char Hw1_i_amd_3dnow[] = "AMD_3DNow"; 159 static const char Hw1_i_amd_3dnowx[] = "AMD_3DNowx"; 160 static const char Hw1_i_fxsr[] = "FXSR"; 161 static const char Hw1_i_sse[] = "SSE"; 162 static const char Hw1_i_sse2[] = "SSE2"; 163 static const char Hw1_i_pause[] = "PAUSE"; 164 static const char Hw1_i_sse3[] = "SSE3"; 165 static const char Hw1_i_mon[] = "MON"; 166 static const char Hw1_i_cx16[] = "CX16"; 167 static const char Hw1_i_ahf[] = "AHF"; 168 static const char Hw1_i_tscp[] = "TSCP"; 169 #elif CAP_LOWERCASE 170 static const char Hw1_i_fpu[] = "fpu"; 171 static const char Hw1_i_tsc[] = "tsc"; 172 static const char Hw1_i_cx8[] = "cx8"; 173 static const char Hw1_i_sep[] = "sep"; 174 static const char Hw1_i_amd_sysc[] = "amd_sysc"; 175 static const char Hw1_i_cmov[] = "cmov"; 176 static const char Hw1_i_mmx[] = "mmx"; 177 static const char Hw1_i_amd_mmx[] = "amd_mmx"; 178 static const char Hw1_i_amd_3dnow[] = "amd_3dnow"; 179 static const char Hw1_i_amd_3dnowx[] = "amd_3dnowx"; 180 static const char Hw1_i_fxsr[] = "fxsr"; 181 static const char Hw1_i_sse[] = "sse"; 182 static const char Hw1_i_sse2[] = "sse2"; 183 static const char Hw1_i_pause[] = "pause"; 184 static const char Hw1_i_sse3[] = "sse3"; 185 static const char Hw1_i_mon[] = "mon"; 186 static const char Hw1_i_cx16[] = "cx16"; 187 static const char Hw1_i_ahf[] = "ahf"; 188 static const char Hw1_i_tscp[] = "tscp"; 189 #else 190 #error "Hardware Capabilities (intel) - what case do you want?" 191 #endif 192 193 /* 194 * Order the Intel hardware capabilities to match their numeric value. See 195 * AV_386_ values in sys/auxv_386.h. 196 */ 197 static const Cap_desc hw1_i[] = { 198 { AV_386_FPU, Hw1_i_fpu, sizeof (Hw1_i_fpu) - 1 }, 199 { AV_386_TSC, Hw1_i_tsc, sizeof (Hw1_i_tsc) - 1 }, 200 { AV_386_CX8, Hw1_i_cx8, sizeof (Hw1_i_cx8) - 1 }, 201 { AV_386_SEP, Hw1_i_sep, sizeof (Hw1_i_sep) - 1 }, 202 { AV_386_AMD_SYSC, Hw1_i_amd_sysc, sizeof (Hw1_i_amd_sysc) - 1 }, 203 { AV_386_CMOV, Hw1_i_cmov, sizeof (Hw1_i_cmov) - 1 }, 204 { AV_386_MMX, Hw1_i_mmx, sizeof (Hw1_i_mmx) - 1 }, 205 { AV_386_AMD_MMX, Hw1_i_amd_mmx, sizeof (Hw1_i_amd_mmx) - 1 }, 206 { AV_386_AMD_3DNow, Hw1_i_amd_3dnow, 207 sizeof (Hw1_i_amd_3dnow) - 1 }, 208 { AV_386_AMD_3DNowx, Hw1_i_amd_3dnowx, 209 sizeof (Hw1_i_amd_3dnowx) - 1 }, 210 { AV_386_FXSR, Hw1_i_fxsr, sizeof (Hw1_i_fxsr) - 1 }, 211 { AV_386_SSE, Hw1_i_sse, sizeof (Hw1_i_sse) - 1 }, 212 { AV_386_SSE2, Hw1_i_sse2, sizeof (Hw1_i_sse2) - 1 }, 213 { AV_386_PAUSE, Hw1_i_pause, sizeof (Hw1_i_pause) - 1 }, 214 { AV_386_SSE3, Hw1_i_sse3, sizeof (Hw1_i_sse3) - 1 }, 215 { AV_386_MON, Hw1_i_mon, sizeof (Hw1_i_mon) - 1 }, 216 { AV_386_CX16, Hw1_i_cx16, sizeof (Hw1_i_cx16) - 1 }, 217 { AV_386_AHF, Hw1_i_ahf, sizeof (Hw1_i_ahf) - 1 }, 218 { AV_386_TSCP, Hw1_i_tscp, sizeof (Hw1_i_tscp) - 1 } 219 }; 220 static const uint_t hw1_i_num = sizeof (hw1_i) / sizeof (Cap_desc); 221 222 /* 223 * Concatenate a token to the string buffer. This can be a capabilities token 224 * or a separator token. 225 */ 226 static int 227 token(char **ostr, size_t *olen, const char *nstr, size_t nlen) 228 { 229 if (*olen < nlen) 230 return (CAP_ERR_BUFOVFL); 231 232 (void) strcat(*ostr, nstr); 233 *ostr += nlen; 234 *olen -= nlen; 235 236 return (0); 237 } 238 239 /* 240 * Expand a capabilities value into the strings defined in the associated 241 * capabilities descriptor. 242 */ 243 static int 244 expand(uint64_t val, const Cap_desc *cdp, uint_t cnum, char *str, size_t slen, 245 int fmt) 246 { 247 uint_t cnt, mask; 248 int follow = 0, err; 249 250 if (val == 0) 251 return (0); 252 253 for (cnt = WORD_BIT, mask = 0x80000000; cnt; cnt--, 254 (mask = mask >> 1)) { 255 if ((val & mask) && (cnt <= cnum) && cdp[cnt - 1].c_val) { 256 if (follow++ && ((err = token(&str, &slen, 257 format[fmt].f_str, format[fmt].f_len)) != 0)) 258 return (err); 259 260 if ((err = token(&str, &slen, cdp[cnt - 1].c_str, 261 cdp[cnt - 1].c_len)) != 0) 262 return (err); 263 264 val = val & ~mask; 265 } 266 } 267 268 /* 269 * If there are any unknown bits remaining display the numeric value. 270 */ 271 if (val) { 272 if (follow && ((err = token(&str, &slen, format[fmt].f_str, 273 format[fmt].f_len)) != 0)) 274 return (err); 275 276 (void) snprintf(str, slen, "0x%llx", val); 277 } 278 return (0); 279 } 280 281 /* 282 * Expand a CA_SUNW_HW_1 value. 283 */ 284 int 285 hwcap_1_val2str(uint64_t val, char *str, size_t len, int fmt, ushort_t mach) 286 { 287 /* 288 * Initialize the string buffer, and validate the format request. 289 */ 290 *str = '\0'; 291 if (fmt > CAP_MAX_TYPE) 292 return (CAP_ERR_INVFMT); 293 294 if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64)) 295 return (expand(val, &hw1_i[0], hw1_i_num, str, len, fmt)); 296 297 if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) || 298 (mach == EM_SPARCV9)) 299 return (expand(val, &hw1_s[0], hw1_s_num, str, len, fmt)); 300 301 return (CAP_ERR_UNKMACH); 302 } 303 304 /* 305 * Expand a CA_SUNW_SF_1 value. Note, that at present these capabilities are 306 * common across all platforms. The use of "mach" is therefore redundant, but 307 * is retained for compatibility with the interface of hwcap_1_val2str(), and 308 * possible future expansion. 309 */ 310 int 311 /* ARGSUSED4 */ 312 sfcap_1_val2str(uint64_t val, char *str, size_t len, int fmt, ushort_t mach) 313 { 314 /* 315 * Initialize the string buffer, and validate the format request. 316 */ 317 *str = '\0'; 318 if (fmt > CAP_MAX_TYPE) 319 return (CAP_ERR_INVFMT); 320 321 return (expand(val, &sf1[0], sf1_num, str, len, fmt)); 322 } 323 324 /* 325 * Determine capability type from the capability tag. 326 */ 327 int 328 cap_val2str(uint64_t tag, uint64_t val, char *str, size_t len, int fmt, 329 ushort_t mach) 330 { 331 if (tag == CA_SUNW_HW_1) 332 return (hwcap_1_val2str(val, str, len, fmt, mach)); 333 if (tag == CA_SUNW_SF_1) 334 return (sfcap_1_val2str(val, str, len, fmt, mach)); 335 336 return (CAP_ERR_UNKTAG); 337 } 338 339 /* 340 * Determine a capabilities value from a capabilities string. 341 */ 342 static uint64_t 343 value(const char *str, const Cap_desc *cdp, uint_t cnum) 344 { 345 uint_t num; 346 347 for (num = 0; num < cnum; num++) { 348 if (strcmp(str, cdp[num].c_str) == 0) 349 return (cdp[num].c_val); 350 } 351 return (0); 352 } 353 354 uint64_t 355 sfcap_1_str2val(const char *str, ushort_t mach) 356 { 357 return (value(str, &sf1[0], sf1_num)); 358 } 359 360 uint64_t 361 hwcap_1_str2val(const char *str, ushort_t mach) 362 { 363 if ((mach == EM_386) || (mach == EM_IA_64) || (mach == EM_AMD64)) 364 return (value(str, &hw1_i[0], hw1_i_num)); 365 366 if ((mach == EM_SPARC) || (mach == EM_SPARC32PLUS) || 367 (mach == EM_SPARCV9)) 368 return (value(str, &hw1_s[0], hw1_s_num)); 369 370 return (0); 371 } 372