1 /*
   2  * Copyright (c) 2006, 2014, 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 
  36 // We need to keep these here as long as we have to build on Solaris
  37 // versions before 10.
  38 #ifndef SI_ARCHITECTURE_32
  39 #define SI_ARCHITECTURE_32      516     /* basic 32-bit SI_ARCHITECTURE */
  40 #endif
  41 
  42 #ifndef SI_ARCHITECTURE_64
  43 #define SI_ARCHITECTURE_64      517     /* basic 64-bit SI_ARCHITECTURE */
  44 #endif
  45 
  46 static void do_sysinfo(int si, const char* string, int* features, int mask) {
  47   char   tmp;
  48   size_t bufsize = sysinfo(si, &tmp, 1);
  49 
  50   // All SI defines used below must be supported.
  51   guarantee(bufsize != -1, "must be supported");
  52 
  53   char* buf = (char*) os::malloc(bufsize, mtInternal);
  54 
  55   if (buf == NULL)
  56     return;
  57 
  58   if (sysinfo(si, buf, bufsize) == bufsize) {
  59     // Compare the string.
  60     if (strcmp(buf, string) == 0) {
  61       *features |= mask;
  62     }
  63   }
  64 
  65   os::free(buf);
  66 }
  67 
  68 int VM_Version::platform_features(int features) {
  69   assert(os::Solaris::supports_getisax(), "getisax() must be available");
  70 
  71   // Check 32-bit architecture.
  72   do_sysinfo(SI_ARCHITECTURE_32, "sparc", &features, v8_instructions_m);
  73 
  74   // Check 64-bit architecture.
  75   do_sysinfo(SI_ARCHITECTURE_64, "sparcv9", &features, generic_v9_m);
  76 
  77   // Extract valid instruction set extensions.
  78   uint_t avs[2];
  79   uint_t avn = os::Solaris::getisax(avs, 2);
  80   assert(avn <= 2, "should return two or less av's");
  81   uint_t av = avs[0];
  82 
  83 #ifndef PRODUCT
  84   if (PrintMiscellaneous && Verbose) {
  85     tty->print("getisax(2) returned: " PTR32_FORMAT, av);
  86     if (avn > 1) {
  87       tty->print(", " PTR32_FORMAT, avs[1]);
  88     }
  89     tty->cr();
  90   }
  91 #endif
  92 
  93   if (av & AV_SPARC_MUL32)  features |= hardware_mul32_m;
  94   if (av & AV_SPARC_DIV32)  features |= hardware_div32_m;
  95   if (av & AV_SPARC_FSMULD) features |= hardware_fsmuld_m;
  96   if (av & AV_SPARC_V8PLUS) features |= v9_instructions_m;
  97   if (av & AV_SPARC_POPC)   features |= hardware_popc_m;
  98   if (av & AV_SPARC_VIS)    features |= vis1_instructions_m;
  99   if (av & AV_SPARC_VIS2)   features |= vis2_instructions_m;
 100   if (avn > 1) {
 101     uint_t av2 = avs[1];
 102 #ifndef AV2_SPARC_SPARC5
 103 #define AV2_SPARC_SPARC5 0x00000008 /* The 29 new fp and sub instructions */
 104 #endif
 105     if (av2 & AV2_SPARC_SPARC5)       features |= sparc5_instructions_m;
 106   }
 107 
 108   // We only build on Solaris 10 and up, but some of the values below
 109   // are not defined on all versions of Solaris 10, so we define them,
 110   // if necessary.
 111 #ifndef AV_SPARC_ASI_BLK_INIT
 112 #define AV_SPARC_ASI_BLK_INIT 0x0080  /* ASI_BLK_INIT_xxx ASI */
 113 #endif
 114   if (av & AV_SPARC_ASI_BLK_INIT) features |= blk_init_instructions_m;
 115 
 116 #ifndef AV_SPARC_FMAF
 117 #define AV_SPARC_FMAF 0x0100        /* Fused Multiply-Add */
 118 #endif
 119   if (av & AV_SPARC_FMAF)         features |= fmaf_instructions_m;
 120 
 121 #ifndef AV_SPARC_FMAU
 122 #define AV_SPARC_FMAU    0x0200  /* Unfused Multiply-Add */
 123 #endif
 124   if (av & AV_SPARC_FMAU)         features |= fmau_instructions_m;
 125 
 126 #ifndef AV_SPARC_VIS3
 127 #define AV_SPARC_VIS3    0x0400  /* VIS3 instruction set extensions */
 128 #endif
 129   if (av & AV_SPARC_VIS3)         features |= vis3_instructions_m;
 130 
 131 #ifndef AV_SPARC_CBCOND
 132 #define AV_SPARC_CBCOND 0x10000000  /* compare and branch instrs supported */
 133 #endif
 134   if (av & AV_SPARC_CBCOND)       features |= cbcond_instructions_m;
 135 
 136 #ifndef AV_SPARC_AES
 137 #define AV_SPARC_AES 0x00020000  /* aes instrs supported */
 138 #endif
 139   if (av & AV_SPARC_AES)       features |= aes_instructions_m;
 140 
 141 #ifndef AV_SPARC_SHA1
 142 #define AV_SPARC_SHA1   0x00400000  /* sha1 instruction supported */
 143 #endif
 144   if (av & AV_SPARC_SHA1)         features |= sha1_instruction_m;
 145 
 146 #ifndef AV_SPARC_SHA256
 147 #define AV_SPARC_SHA256 0x00800000  /* sha256 instruction supported */
 148 #endif
 149   if (av & AV_SPARC_SHA256)       features |= sha256_instruction_m;
 150 
 151 #ifndef AV_SPARC_SHA512
 152 #define AV_SPARC_SHA512 0x01000000  /* sha512 instruction supported */
 153 #endif
 154   if (av & AV_SPARC_SHA512)       features |= sha512_instruction_m;
 155 
 156   // Determine the machine type.
 157   do_sysinfo(SI_MACHINE, "sun4v", &features, sun4v_m);
 158 
 159   {
 160     // Using kstat to determine the machine type.
 161     kstat_ctl_t* kc = kstat_open();
 162     kstat_t* ksp = kstat_lookup(kc, (char*)"cpu_info", -1, NULL);
 163     const char* implementation = "UNKNOWN";
 164     if (ksp != NULL) {
 165       if (kstat_read(kc, ksp, NULL) != -1 && ksp->ks_data != NULL) {
 166         kstat_named_t* knm = (kstat_named_t *)ksp->ks_data;
 167         for (int i = 0; i < ksp->ks_ndata; i++) {
 168           if (strcmp((const char*)&(knm[i].name),"implementation") == 0) {
 169             implementation = KSTAT_NAMED_STR_PTR(&knm[i]);
 170 #ifndef PRODUCT
 171             if (PrintMiscellaneous && Verbose) {
 172               tty->print_cr("cpu_info.implementation: %s", implementation);
 173             }
 174 #endif
 175             // Convert to UPPER case before compare.
 176             char* impl = os::strdup_check_oom(implementation);
 177 
 178             for (int i = 0; impl[i] != 0; i++)
 179               impl[i] = (char)toupper((uint)impl[i]);
 180 
 181             if (strstr(impl, "SPARC64") != NULL) {
 182               features |= sparc64_family_m;
 183             } else if (strstr(impl, "SPARC-M") != NULL) {
 184               // M-series SPARC is based on T-series.
 185               features |= (M_family_m | T_family_m);
 186             } else if (strstr(impl, "SPARC-T") != NULL) {
 187               features |= T_family_m;
 188               if (strstr(impl, "SPARC-T1") != NULL) {
 189                 features |= T1_model_m;
 190               }
 191             } else {
 192               if (strstr(impl, "SPARC") == NULL) {
 193 #ifndef PRODUCT
 194                 // kstat on Solaris 8 virtual machines (branded zones)
 195                 // returns "(unsupported)" implementation. Solaris 8 is not
 196                 // supported anymore, but include this check to be on the
 197                 // safe side.
 198                 warning("kstat cpu_info implementation = '%s', assume generic SPARC", impl);
 199 #endif
 200                 implementation = "SPARC";
 201               }
 202             }
 203             os::free((void*)impl);
 204             break;
 205           }
 206         } // for(
 207       }
 208     }
 209     assert(strcmp(implementation, "UNKNOWN") != 0,
 210            "unknown cpu info (changed kstat interface?)");
 211     kstat_close(kc);
 212   }
 213 
 214   return features;
 215 }