1 /* 2 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "memory/allocation.hpp" 27 #include "memory/allocation.inline.hpp" 28 #include "runtime/os.hpp" 29 #include "vm_version_sparc.hpp" 30 31 #include <sys/auxv.h> 32 #include <sys/auxv_SPARC.h> 33 #include <sys/systeminfo.h> 34 #include <kstat.h> 35 #include <picl.h> 36 #include <dlfcn.h> 37 #include <link.h> 38 39 extern "C" static int PICL_visit_cpu_helper(picl_nodehdl_t nodeh, void *result); 40 41 // Functions from the library we need (signatures should match those in picl.h) 42 extern "C" { 43 typedef int (*picl_initialize_func_t)(void); 44 typedef int (*picl_shutdown_func_t)(void); 45 typedef int (*picl_get_root_func_t)(picl_nodehdl_t *nodehandle); 46 typedef int (*picl_walk_tree_by_class_func_t)(picl_nodehdl_t rooth, 47 const char *classname, void *c_args, 48 int (*callback_fn)(picl_nodehdl_t hdl, void *args)); 49 typedef int (*picl_get_prop_by_name_func_t)(picl_nodehdl_t nodeh, const char *nm, 50 picl_prophdl_t *ph); 51 typedef int (*picl_get_propval_func_t)(picl_prophdl_t proph, void *valbuf, size_t sz); 52 typedef int (*picl_get_propinfo_func_t)(picl_prophdl_t proph, picl_propinfo_t *pi); 53 } 54 55 class PICL { 56 // Pointers to functions in the library 57 picl_initialize_func_t _picl_initialize; 58 picl_shutdown_func_t _picl_shutdown; 59 picl_get_root_func_t _picl_get_root; 60 picl_walk_tree_by_class_func_t _picl_walk_tree_by_class; 61 picl_get_prop_by_name_func_t _picl_get_prop_by_name; 62 picl_get_propval_func_t _picl_get_propval; 63 picl_get_propinfo_func_t _picl_get_propinfo; 64 // Handle to the library that is returned by dlopen 65 void *_dl_handle; 66 67 bool open_library(); 68 void close_library(); 69 70 template<typename FuncType> bool bind(FuncType& func, const char* name); 71 bool bind_library_functions(); 72 73 // Get a value of the integer property. The value in the tree can be either 32 or 64 bit 74 // depending on the platform. The result is converted to int. 75 int get_int_property(picl_nodehdl_t nodeh, const char* name, int* result) { 76 picl_propinfo_t pinfo; 77 picl_prophdl_t proph; 78 if (_picl_get_prop_by_name(nodeh, name, &proph) != PICL_SUCCESS || 79 _picl_get_propinfo(proph, &pinfo) != PICL_SUCCESS) { 80 return PICL_FAILURE; 81 } 82 83 if (pinfo.type != PICL_PTYPE_INT && pinfo.type != PICL_PTYPE_UNSIGNED_INT) { 84 assert(false, "Invalid property type"); 85 return PICL_FAILURE; 86 } 87 if (pinfo.size == sizeof(int64_t)) { 88 int64_t val; 89 if (_picl_get_propval(proph, &val, sizeof(int64_t)) != PICL_SUCCESS) { 90 return PICL_FAILURE; 91 } 92 *result = static_cast<int>(val); 93 } else if (pinfo.size == sizeof(int32_t)) { 94 int32_t val; 95 if (_picl_get_propval(proph, &val, sizeof(int32_t)) != PICL_SUCCESS) { 96 return PICL_FAILURE; 97 } 98 *result = static_cast<int>(val); 99 } else { 100 assert(false, "Unexpected integer property size"); 101 return PICL_FAILURE; 102 } 103 return PICL_SUCCESS; 104 } 105 106 // Visitor and a state machine that visits integer properties and verifies that the 107 // values are the same. Stores the unique value observed. 108 class UniqueValueVisitor { 109 PICL *_picl; 110 enum { 111 INITIAL, // Start state, no assignments happened 112 ASSIGNED, // Assigned a value 113 INCONSISTENT // Inconsistent value seen 114 } _state; 115 int _value; 116 public: 117 UniqueValueVisitor(PICL* picl) : _picl(picl), _state(INITIAL) { } 118 int value() { 119 assert(_state == ASSIGNED, "Precondition"); 120 return _value; 121 } 122 void set_value(int value) { 123 assert(_state == INITIAL, "Precondition"); 124 _value = value; 125 _state = ASSIGNED; 126 } 127 bool is_initial() { return _state == INITIAL; } 128 bool is_assigned() { return _state == ASSIGNED; } 129 bool is_inconsistent() { return _state == INCONSISTENT; } 130 void set_inconsistent() { _state = INCONSISTENT; } 131 132 bool visit(picl_nodehdl_t nodeh, const char* name) { 133 assert(!is_inconsistent(), "Precondition"); 134 int curr; 135 if (_picl->get_int_property(nodeh, name, &curr) == PICL_SUCCESS) { 136 if (!is_assigned()) { // first iteration 137 set_value(curr); 138 } else if (curr != value()) { // following iterations 139 set_inconsistent(); 140 } 141 return true; 142 } 143 return false; 144 } 145 }; 146 147 class CPUVisitor { 148 UniqueValueVisitor _l1_visitor; 149 UniqueValueVisitor _l2_visitor; 150 int _limit; // number of times visit() can be run 151 public: 152 CPUVisitor(PICL *picl, int limit) : _l1_visitor(picl), _l2_visitor(picl), _limit(limit) {} 153 static int visit(picl_nodehdl_t nodeh, void *arg) { 154 CPUVisitor *cpu_visitor = static_cast<CPUVisitor*>(arg); 155 UniqueValueVisitor* l1_visitor = cpu_visitor->l1_visitor(); 156 UniqueValueVisitor* l2_visitor = cpu_visitor->l2_visitor(); 157 if (!l1_visitor->is_inconsistent()) { 158 l1_visitor->visit(nodeh, "l1-dcache-line-size"); 159 } 160 static const char* l2_data_cache_line_property_name = NULL; 161 // On the first visit determine the name of the l2 cache line size property and memoize it. 162 if (l2_data_cache_line_property_name == NULL) { 163 assert(!l2_visitor->is_inconsistent(), "First iteration cannot be inconsistent"); 164 l2_data_cache_line_property_name = "l2-cache-line-size"; 165 if (!l2_visitor->visit(nodeh, l2_data_cache_line_property_name)) { 166 l2_data_cache_line_property_name = "l2-dcache-line-size"; 167 l2_visitor->visit(nodeh, l2_data_cache_line_property_name); 168 } 169 } else { 170 if (!l2_visitor->is_inconsistent()) { 171 l2_visitor->visit(nodeh, l2_data_cache_line_property_name); 172 } 173 } 174 175 if (l1_visitor->is_inconsistent() && l2_visitor->is_inconsistent()) { 176 return PICL_WALK_TERMINATE; 177 } 178 cpu_visitor->_limit--; 179 if (cpu_visitor->_limit <= 0) { 180 return PICL_WALK_TERMINATE; 181 } 182 return PICL_WALK_CONTINUE; 183 } 184 UniqueValueVisitor* l1_visitor() { return &_l1_visitor; } 185 UniqueValueVisitor* l2_visitor() { return &_l2_visitor; } 186 }; 187 int _L1_data_cache_line_size; 188 int _L2_data_cache_line_size; 189 public: 190 static int visit_cpu(picl_nodehdl_t nodeh, void *state) { 191 return CPUVisitor::visit(nodeh, state); 192 } 193 194 PICL(bool is_fujitsu, bool is_sun4v) : _L1_data_cache_line_size(0), _L2_data_cache_line_size(0), _dl_handle(NULL) { 195 if (!open_library()) { 196 return; 197 } 198 if (_picl_initialize() == PICL_SUCCESS) { 199 picl_nodehdl_t rooth; 200 if (_picl_get_root(&rooth) == PICL_SUCCESS) { 201 const char* cpu_class = "cpu"; 202 // If it's a Fujitsu machine, it's a "core" 203 if (is_fujitsu) { 204 cpu_class = "core"; 205 } 206 CPUVisitor cpu_visitor(this, (is_sun4v && !is_fujitsu) ? 1 : os::processor_count()); 207 _picl_walk_tree_by_class(rooth, cpu_class, &cpu_visitor, PICL_visit_cpu_helper); 208 if (cpu_visitor.l1_visitor()->is_assigned()) { // Is there a value? 209 _L1_data_cache_line_size = cpu_visitor.l1_visitor()->value(); 210 } 211 if (cpu_visitor.l2_visitor()->is_assigned()) { 212 _L2_data_cache_line_size = cpu_visitor.l2_visitor()->value(); 213 } 214 } 215 _picl_shutdown(); 216 } 217 close_library(); 218 } 219 220 unsigned int L1_data_cache_line_size() const { return _L1_data_cache_line_size; } 221 unsigned int L2_data_cache_line_size() const { return _L2_data_cache_line_size; } 222 }; 223 224 225 extern "C" static int PICL_visit_cpu_helper(picl_nodehdl_t nodeh, void *result) { 226 return PICL::visit_cpu(nodeh, result); 227 } 228 229 template<typename FuncType> 230 bool PICL::bind(FuncType& func, const char* name) { 231 func = reinterpret_cast<FuncType>(dlsym(_dl_handle, name)); 232 return func != NULL; 233 } 234 235 bool PICL::bind_library_functions() { 236 assert(_dl_handle != NULL, "library should be open"); 237 return bind(_picl_initialize, "picl_initialize" ) && 238 bind(_picl_shutdown, "picl_shutdown" ) && 239 bind(_picl_get_root, "picl_get_root" ) && 240 bind(_picl_walk_tree_by_class, "picl_walk_tree_by_class") && 241 bind(_picl_get_prop_by_name, "picl_get_prop_by_name" ) && 242 bind(_picl_get_propval, "picl_get_propval" ) && 243 bind(_picl_get_propinfo, "picl_get_propinfo" ); 244 } 245 246 bool PICL::open_library() { 247 _dl_handle = dlopen("libpicl.so.1", RTLD_LAZY); 248 if (_dl_handle == NULL) { 249 return false; 250 } 251 if (!bind_library_functions()) { 252 assert(false, "unexpected PICL API change"); 253 close_library(); 254 return false; 255 } 256 return true; 257 } 258 259 void PICL::close_library() { 260 assert(_dl_handle != NULL, "library should be open"); 261 dlclose(_dl_handle); 262 _dl_handle = NULL; 263 } 264 265 // We need to keep these here as long as we have to build on Solaris 266 // versions before 10. 267 268 #ifndef SI_ARCHITECTURE_32 269 #define SI_ARCHITECTURE_32 516 /* basic 32-bit SI_ARCHITECTURE */ 270 #endif 271 272 #ifndef SI_ARCHITECTURE_64 273 #define SI_ARCHITECTURE_64 517 /* basic 64-bit SI_ARCHITECTURE */ 274 #endif 275 276 #ifndef SI_CPUBRAND 277 #define SI_CPUBRAND 523 /* return cpu brand string */ 278 #endif 279 280 class Sysinfo { 281 char* _string; 282 public: 283 Sysinfo(int si) : _string(NULL) { 284 char tmp; 285 size_t bufsize = sysinfo(si, &tmp, 1); 286 287 if (bufsize != -1) { 288 char* buf = (char*) os::malloc(bufsize, mtInternal); 289 if (buf != NULL) { 290 if (sysinfo(si, buf, bufsize) == bufsize) { 291 _string = buf; 292 } else { 293 os::free(buf); 294 } 295 } 296 } 297 } 298 299 ~Sysinfo() { 300 if (_string != NULL) { 301 os::free(_string); 302 } 303 } 304 305 const char* value() const { 306 return _string; 307 } 308 309 bool valid() const { 310 return _string != NULL; 311 } 312 313 bool match(const char* s) const { 314 return valid() ? strcmp(_string, s) == 0 : false; 315 } 316 317 bool match_substring(const char* s) const { 318 return valid() ? strstr(_string, s) != NULL : false; 319 } 320 }; 321 322 class Sysconf { 323 int _value; 324 public: 325 Sysconf(int sc) : _value(-1) { 326 _value = sysconf(sc); 327 } 328 bool valid() const { 329 return _value != -1; 330 } 331 int value() const { 332 return _value; 333 } 334 }; 335 336 337 #ifndef _SC_DCACHE_LINESZ 338 #define _SC_DCACHE_LINESZ 508 /* Data cache line size */ 339 #endif 340 341 #ifndef _SC_L2CACHE_LINESZ 342 #define _SC_L2CACHE_LINESZ 527 /* Size of L2 cache line */ 343 #endif 344 345 int VM_Version::platform_features(int features) { 346 assert(os::Solaris::supports_getisax(), "getisax() must be available"); 347 348 // Check 32-bit architecture. 349 if (Sysinfo(SI_ARCHITECTURE_32).match("sparc")) { 350 features |= v8_instructions_m; 351 } 352 353 // Check 64-bit architecture. 354 if (Sysinfo(SI_ARCHITECTURE_64).match("sparcv9")) { 355 features |= generic_v9_m; 356 } 357 358 // Extract valid instruction set extensions. 359 uint_t avs[2]; 360 uint_t avn = os::Solaris::getisax(avs, 2); 361 assert(avn <= 2, "should return two or less av's"); 362 uint_t av = avs[0]; 363 364 #ifndef PRODUCT 365 if (PrintMiscellaneous && Verbose) { 366 tty->print("getisax(2) returned: " PTR32_FORMAT, av); 367 if (avn > 1) { 368 tty->print(", " PTR32_FORMAT, avs[1]); 369 } 370 tty->cr(); 371 } 372 #endif 373 374 if (av & AV_SPARC_MUL32) features |= hardware_mul32_m; 375 if (av & AV_SPARC_DIV32) features |= hardware_div32_m; 376 if (av & AV_SPARC_FSMULD) features |= hardware_fsmuld_m; 377 if (av & AV_SPARC_V8PLUS) features |= v9_instructions_m; 378 if (av & AV_SPARC_POPC) features |= hardware_popc_m; 379 if (av & AV_SPARC_VIS) features |= vis1_instructions_m; 380 if (av & AV_SPARC_VIS2) features |= vis2_instructions_m; 381 if (avn > 1) { 382 uint_t av2 = avs[1]; 383 #ifndef AV2_SPARC_SPARC5 384 #define AV2_SPARC_SPARC5 0x00000008 /* The 29 new fp and sub instructions */ 385 #endif 386 if (av2 & AV2_SPARC_SPARC5) features |= sparc5_instructions_m; 387 } 388 389 // We only build on Solaris 10 and up, but some of the values below 390 // are not defined on all versions of Solaris 10, so we define them, 391 // if necessary. 392 #ifndef AV_SPARC_ASI_BLK_INIT 393 #define AV_SPARC_ASI_BLK_INIT 0x0080 /* ASI_BLK_INIT_xxx ASI */ 394 #endif 395 if (av & AV_SPARC_ASI_BLK_INIT) features |= blk_init_instructions_m; 396 397 #ifndef AV_SPARC_FMAF 398 #define AV_SPARC_FMAF 0x0100 /* Fused Multiply-Add */ 399 #endif 400 if (av & AV_SPARC_FMAF) features |= fmaf_instructions_m; 401 402 #ifndef AV_SPARC_FMAU 403 #define AV_SPARC_FMAU 0x0200 /* Unfused Multiply-Add */ 404 #endif 405 if (av & AV_SPARC_FMAU) features |= fmau_instructions_m; 406 407 #ifndef AV_SPARC_VIS3 408 #define AV_SPARC_VIS3 0x0400 /* VIS3 instruction set extensions */ 409 #endif 410 if (av & AV_SPARC_VIS3) features |= vis3_instructions_m; 411 412 #ifndef AV_SPARC_CBCOND 413 #define AV_SPARC_CBCOND 0x10000000 /* compare and branch instrs supported */ 414 #endif 415 if (av & AV_SPARC_CBCOND) features |= cbcond_instructions_m; 416 417 #ifndef AV_SPARC_CRC32C 418 #define AV_SPARC_CRC32C 0x20000000 /* crc32c instruction supported */ 419 #endif 420 if (av & AV_SPARC_CRC32C) features |= crc32c_instruction_m; 421 422 #ifndef AV_SPARC_AES 423 #define AV_SPARC_AES 0x00020000 /* aes instrs supported */ 424 #endif 425 if (av & AV_SPARC_AES) features |= aes_instructions_m; 426 427 #ifndef AV_SPARC_SHA1 428 #define AV_SPARC_SHA1 0x00400000 /* sha1 instruction supported */ 429 #endif 430 if (av & AV_SPARC_SHA1) features |= sha1_instruction_m; 431 432 #ifndef AV_SPARC_SHA256 433 #define AV_SPARC_SHA256 0x00800000 /* sha256 instruction supported */ 434 #endif 435 if (av & AV_SPARC_SHA256) features |= sha256_instruction_m; 436 437 #ifndef AV_SPARC_SHA512 438 #define AV_SPARC_SHA512 0x01000000 /* sha512 instruction supported */ 439 #endif 440 if (av & AV_SPARC_SHA512) features |= sha512_instruction_m; 441 442 // Determine the machine type. 443 if (Sysinfo(SI_MACHINE).match("sun4v")) { 444 features |= sun4v_m; 445 } 446 447 bool use_solaris_12_api = false; 448 Sysinfo impl(SI_CPUBRAND); 449 if (impl.valid()) { 450 // If SI_CPUBRAND works, that means Solaris 12 API to get the cache line sizes 451 // is available to us as well 452 use_solaris_12_api = true; 453 features |= parse_features(impl.value()); 454 } else { 455 // Otherwise use kstat to determine the machine type. 456 kstat_ctl_t* kc = kstat_open(); 457 kstat_t* ksp = kstat_lookup(kc, (char*)"cpu_info", -1, NULL); 458 const char* implementation; 459 bool has_implementation = false; 460 if (ksp != NULL) { 461 if (kstat_read(kc, ksp, NULL) != -1 && ksp->ks_data != NULL) { 462 kstat_named_t* knm = (kstat_named_t *)ksp->ks_data; 463 for (int i = 0; i < ksp->ks_ndata; i++) { 464 if (strcmp((const char*)&(knm[i].name),"implementation") == 0) { 465 implementation = KSTAT_NAMED_STR_PTR(&knm[i]); 466 has_implementation = true; 467 #ifndef PRODUCT 468 if (PrintMiscellaneous && Verbose) { 469 tty->print_cr("cpu_info.implementation: %s", implementation); 470 } 471 #endif 472 features |= parse_features(implementation); 473 break; 474 } 475 } // for( 476 } 477 } 478 assert(has_implementation, "unknown cpu info (changed kstat interface?)"); 479 kstat_close(kc); 480 } 481 482 bool is_sun4v = (features & sun4v_m) != 0; 483 if (use_solaris_12_api && is_sun4v) { 484 // If Solaris 12 API is supported and it's sun4v use sysconf() to get the cache line sizes 485 Sysconf l1_dcache_line_size(_SC_DCACHE_LINESZ); 486 if (l1_dcache_line_size.valid()) { 487 _L1_data_cache_line_size = l1_dcache_line_size.value(); 488 } 489 490 Sysconf l2_dcache_line_size(_SC_L2CACHE_LINESZ); 491 if (l2_dcache_line_size.valid()) { 492 _L2_data_cache_line_size = l2_dcache_line_size.value(); 493 } 494 } else { 495 // Otherwise figure out the cache line sizes using PICL 496 bool is_fujitsu = (features & sparc64_family_m) != 0; 497 PICL picl(is_fujitsu, is_sun4v); 498 _L1_data_cache_line_size = picl.L1_data_cache_line_size(); 499 _L2_data_cache_line_size = picl.L2_data_cache_line_size(); 500 } 501 return features; 502 }