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