1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2014 SAP AG. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/assembler.inline.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "compiler/disassembler.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "runtime/java.hpp"
  32 #include "runtime/os.hpp"
  33 #include "runtime/stubCodeGenerator.hpp"
  34 #include "utilities/defaultStream.hpp"
  35 #include "vm_version_ppc.hpp"
  36 
  37 # include <sys/sysinfo.h>
  38 
  39 int VM_Version::_features = VM_Version::unknown_m;
  40 int VM_Version::_measured_cache_line_size = 128; // default value
  41 const char* VM_Version::_features_str = "";
  42 bool VM_Version::_is_determine_features_test_running = false;
  43 
  44 
  45 #define MSG(flag)   \
  46   if (flag && !FLAG_IS_DEFAULT(flag))                                  \
  47       jio_fprintf(defaultStream::error_stream(),                       \
  48                   "warning: -XX:+" #flag " requires -XX:+UseSIGTRAP\n" \
  49                   "         -XX:+" #flag " will be disabled!\n");
  50 
  51 void VM_Version::initialize() {
  52 
  53   // Test which instructions are supported and measure cache line size.
  54   determine_features();
  55 
  56   // If PowerArchitecturePPC64 hasn't been specified explicitly determine from features.
  57   if (FLAG_IS_DEFAULT(PowerArchitecturePPC64)) {
  58     if (VM_Version::has_popcntw()) {
  59       FLAG_SET_ERGO(uintx, PowerArchitecturePPC64, 7);
  60     } else if (VM_Version::has_cmpb()) {
  61       FLAG_SET_ERGO(uintx, PowerArchitecturePPC64, 6);
  62     } else if (VM_Version::has_popcntb()) {
  63       FLAG_SET_ERGO(uintx, PowerArchitecturePPC64, 5);
  64     } else {
  65       FLAG_SET_ERGO(uintx, PowerArchitecturePPC64, 0);
  66     }
  67   }
  68   guarantee(PowerArchitecturePPC64 == 0 || PowerArchitecturePPC64 == 5 ||
  69             PowerArchitecturePPC64 == 6 || PowerArchitecturePPC64 == 7,
  70             "PowerArchitecturePPC64 should be 0, 5, 6 or 7");
  71 
  72   if (!UseSIGTRAP) {
  73     MSG(TrapBasedICMissChecks);
  74     MSG(TrapBasedNotEntrantChecks);
  75     MSG(TrapBasedNullChecks);
  76     FLAG_SET_ERGO(bool, TrapBasedNotEntrantChecks, false);
  77     FLAG_SET_ERGO(bool, TrapBasedNullChecks,       false);
  78     FLAG_SET_ERGO(bool, TrapBasedICMissChecks,     false);
  79   }
  80 
  81 #ifdef COMPILER2
  82   if (!UseSIGTRAP) {
  83     MSG(TrapBasedRangeChecks);
  84     FLAG_SET_ERGO(bool, TrapBasedRangeChecks, false);
  85   }
  86 
  87   // On Power6 test for section size.
  88   if (PowerArchitecturePPC64 == 6) {
  89     determine_section_size();
  90   // TODO: PPC port } else {
  91   // TODO: PPC port PdScheduling::power6SectorSize = 0x20;
  92   }
  93 
  94   MaxVectorSize = 8;
  95 #endif
  96 
  97   // Create and print feature-string.
  98   char buf[(num_features+1) * 16]; // Max 16 chars per feature.
  99   jio_snprintf(buf, sizeof(buf),
 100                "ppc64%s%s%s%s%s%s%s%s",
 101                (has_fsqrt()   ? " fsqrt"   : ""),
 102                (has_isel()    ? " isel"    : ""),
 103                (has_lxarxeh() ? " lxarxeh" : ""),
 104                (has_cmpb()    ? " cmpb"    : ""),
 105                //(has_mftgpr()? " mftgpr"  : ""),
 106                (has_popcntb() ? " popcntb" : ""),
 107                (has_popcntw() ? " popcntw" : ""),
 108                (has_fcfids()  ? " fcfids"  : ""),
 109                (has_vand()    ? " vand"    : "")
 110                // Make sure number of %s matches num_features!
 111               );
 112   _features_str = os::strdup(buf);
 113   NOT_PRODUCT(if (Verbose) print_features(););
 114 
 115   // PPC64 supports 8-byte compare-exchange operations (see
 116   // Atomic::cmpxchg and StubGenerator::generate_atomic_cmpxchg_ptr)
 117   // and 'atomic long memory ops' (see Unsafe_GetLongVolatile).
 118   _supports_cx8 = true;
 119 
 120   UseSSE = 0; // Only on x86 and x64
 121 
 122   intx cache_line_size = _measured_cache_line_size;
 123 
 124   if (FLAG_IS_DEFAULT(AllocatePrefetchStyle)) AllocatePrefetchStyle = 1;
 125 
 126   if (AllocatePrefetchStyle == 4) {
 127     AllocatePrefetchStepSize = cache_line_size; // Need exact value.
 128     if (FLAG_IS_DEFAULT(AllocatePrefetchLines)) AllocatePrefetchLines = 12; // Use larger blocks by default.
 129     if (AllocatePrefetchDistance < 0) AllocatePrefetchDistance = 2*cache_line_size; // Default is not defined?
 130   } else {
 131     if (cache_line_size > AllocatePrefetchStepSize) AllocatePrefetchStepSize = cache_line_size;
 132     if (FLAG_IS_DEFAULT(AllocatePrefetchLines)) AllocatePrefetchLines = 3; // Optimistic value.
 133     if (AllocatePrefetchDistance < 0) AllocatePrefetchDistance = 3*cache_line_size; // Default is not defined?
 134   }
 135 
 136   assert(AllocatePrefetchLines > 0, "invalid value");
 137   if (AllocatePrefetchLines < 1) // Set valid value in product VM.
 138     AllocatePrefetchLines = 1; // Conservative value.
 139 
 140   if (AllocatePrefetchStyle == 3 && AllocatePrefetchDistance < cache_line_size)
 141     AllocatePrefetchStyle = 1; // Fall back if inappropriate.
 142 
 143   assert(AllocatePrefetchStyle >= 0, "AllocatePrefetchStyle should be positive");
 144 }
 145 
 146 void VM_Version::print_features() {
 147   tty->print_cr("Version: %s cache_line_size = %d", cpu_features(), (int) get_cache_line_size());
 148 }
 149 
 150 #ifdef COMPILER2
 151 // Determine section size on power6: If section size is 8 instructions,
 152 // there should be a difference between the two testloops of ~15 %. If
 153 // no difference is detected the section is assumed to be 32 instructions.
 154 void VM_Version::determine_section_size() {
 155 
 156   int unroll = 80;
 157 
 158   const int code_size = (2* unroll * 32 + 100)*BytesPerInstWord;
 159 
 160   // Allocate space for the code.
 161   ResourceMark rm;
 162   CodeBuffer cb("detect_section_size", code_size, 0);
 163   MacroAssembler* a = new MacroAssembler(&cb);
 164 
 165   uint32_t *code = (uint32_t *)a->pc();
 166   // Emit code.
 167   void (*test1)() = (void(*)())(void *)a->function_entry();
 168 
 169   Label l1;
 170 
 171   a->li(R4, 1);
 172   a->sldi(R4, R4, 28);
 173   a->b(l1);
 174   a->align(CodeEntryAlignment);
 175 
 176   a->bind(l1);
 177 
 178   for (int i = 0; i < unroll; i++) {
 179     // Schleife 1
 180     // ------- sector 0 ------------
 181     // ;; 0
 182     a->nop();                   // 1
 183     a->fpnop0();                // 2
 184     a->fpnop1();                // 3
 185     a->addi(R4,R4, -1); // 4
 186 
 187     // ;;  1
 188     a->nop();                   // 5
 189     a->fmr(F6, F6);             // 6
 190     a->fmr(F7, F7);             // 7
 191     a->endgroup();              // 8
 192     // ------- sector 8 ------------
 193 
 194     // ;;  2
 195     a->nop();                   // 9
 196     a->nop();                   // 10
 197     a->fmr(F8, F8);             // 11
 198     a->fmr(F9, F9);             // 12
 199 
 200     // ;;  3
 201     a->nop();                   // 13
 202     a->fmr(F10, F10);           // 14
 203     a->fmr(F11, F11);           // 15
 204     a->endgroup();              // 16
 205     // -------- sector 16 -------------
 206 
 207     // ;;  4
 208     a->nop();                   // 17
 209     a->nop();                   // 18
 210     a->fmr(F15, F15);           // 19
 211     a->fmr(F16, F16);           // 20
 212 
 213     // ;;  5
 214     a->nop();                   // 21
 215     a->fmr(F17, F17);           // 22
 216     a->fmr(F18, F18);           // 23
 217     a->endgroup();              // 24
 218     // ------- sector 24  ------------
 219 
 220     // ;;  6
 221     a->nop();                   // 25
 222     a->nop();                   // 26
 223     a->fmr(F19, F19);           // 27
 224     a->fmr(F20, F20);           // 28
 225 
 226     // ;;  7
 227     a->nop();                   // 29
 228     a->fmr(F21, F21);           // 30
 229     a->fmr(F22, F22);           // 31
 230     a->brnop0();                // 32
 231 
 232     // ------- sector 32 ------------
 233   }
 234 
 235   // ;; 8
 236   a->cmpdi(CCR0, R4, unroll);   // 33
 237   a->bge(CCR0, l1);             // 34
 238   a->blr();
 239 
 240   // Emit code.
 241   void (*test2)() = (void(*)())(void *)a->function_entry();
 242   // uint32_t *code = (uint32_t *)a->pc();
 243 
 244   Label l2;
 245 
 246   a->li(R4, 1);
 247   a->sldi(R4, R4, 28);
 248   a->b(l2);
 249   a->align(CodeEntryAlignment);
 250 
 251   a->bind(l2);
 252 
 253   for (int i = 0; i < unroll; i++) {
 254     // Schleife 2
 255     // ------- sector 0 ------------
 256     // ;; 0
 257     a->brnop0();                  // 1
 258     a->nop();                     // 2
 259     //a->cmpdi(CCR0, R4, unroll);
 260     a->fpnop0();                  // 3
 261     a->fpnop1();                  // 4
 262     a->addi(R4,R4, -1);           // 5
 263 
 264     // ;; 1
 265 
 266     a->nop();                     // 6
 267     a->fmr(F6, F6);               // 7
 268     a->fmr(F7, F7);               // 8
 269     // ------- sector 8 ---------------
 270 
 271     // ;; 2
 272     a->endgroup();                // 9
 273 
 274     // ;; 3
 275     a->nop();                     // 10
 276     a->nop();                     // 11
 277     a->fmr(F8, F8);               // 12
 278 
 279     // ;; 4
 280     a->fmr(F9, F9);               // 13
 281     a->nop();                     // 14
 282     a->fmr(F10, F10);             // 15
 283 
 284     // ;; 5
 285     a->fmr(F11, F11);             // 16
 286     // -------- sector 16 -------------
 287 
 288     // ;; 6
 289     a->endgroup();                // 17
 290 
 291     // ;; 7
 292     a->nop();                     // 18
 293     a->nop();                     // 19
 294     a->fmr(F15, F15);             // 20
 295 
 296     // ;; 8
 297     a->fmr(F16, F16);             // 21
 298     a->nop();                     // 22
 299     a->fmr(F17, F17);             // 23
 300 
 301     // ;; 9
 302     a->fmr(F18, F18);             // 24
 303     // -------- sector 24 -------------
 304 
 305     // ;; 10
 306     a->endgroup();                // 25
 307 
 308     // ;; 11
 309     a->nop();                     // 26
 310     a->nop();                     // 27
 311     a->fmr(F19, F19);             // 28
 312 
 313     // ;; 12
 314     a->fmr(F20, F20);             // 29
 315     a->nop();                     // 30
 316     a->fmr(F21, F21);             // 31
 317 
 318     // ;; 13
 319     a->fmr(F22, F22);             // 32
 320   }
 321 
 322   // -------- sector 32 -------------
 323   // ;; 14
 324   a->cmpdi(CCR0, R4, unroll); // 33
 325   a->bge(CCR0, l2);           // 34
 326 
 327   a->blr();
 328   uint32_t *code_end = (uint32_t *)a->pc();
 329   a->flush();
 330 
 331   double loop1_seconds,loop2_seconds, rel_diff;
 332   uint64_t start1, stop1;
 333 
 334   start1 = os::current_thread_cpu_time(false);
 335   (*test1)();
 336   stop1 = os::current_thread_cpu_time(false);
 337   loop1_seconds = (stop1- start1) / (1000 *1000 *1000.0);
 338 
 339 
 340   start1 = os::current_thread_cpu_time(false);
 341   (*test2)();
 342   stop1 = os::current_thread_cpu_time(false);
 343 
 344   loop2_seconds = (stop1 - start1) / (1000 *1000 *1000.0);
 345 
 346   rel_diff = (loop2_seconds - loop1_seconds) / loop1_seconds *100;
 347 
 348   if (PrintAssembly) {
 349     ttyLocker ttyl;
 350     tty->print_cr("Decoding section size detection stub at " INTPTR_FORMAT " before execution:", p2i(code));
 351     Disassembler::decode((u_char*)code, (u_char*)code_end, tty);
 352     tty->print_cr("Time loop1 :%f", loop1_seconds);
 353     tty->print_cr("Time loop2 :%f", loop2_seconds);
 354     tty->print_cr("(time2 - time1) / time1 = %f %%", rel_diff);
 355 
 356     if (rel_diff > 12.0) {
 357       tty->print_cr("Section Size 8 Instructions");
 358     } else{
 359       tty->print_cr("Section Size 32 Instructions or Power5");
 360     }
 361   }
 362 
 363 #if 0 // TODO: PPC port
 364   // Set sector size (if not set explicitly).
 365   if (FLAG_IS_DEFAULT(Power6SectorSize128PPC64)) {
 366     if (rel_diff > 12.0) {
 367       PdScheduling::power6SectorSize = 0x20;
 368     } else {
 369       PdScheduling::power6SectorSize = 0x80;
 370     }
 371   } else if (Power6SectorSize128PPC64) {
 372     PdScheduling::power6SectorSize = 0x80;
 373   } else {
 374     PdScheduling::power6SectorSize = 0x20;
 375   }
 376 #endif
 377   if (UsePower6SchedulerPPC64) Unimplemented();
 378 }
 379 #endif // COMPILER2
 380 
 381 void VM_Version::determine_features() {
 382 #if defined(ABI_ELFv2)
 383   const int code_size = (num_features+1+2*7)*BytesPerInstWord; // TODO(asmundak): calculation is incorrect.
 384 #else
 385   // 7 InstWords for each call (function descriptor + blr instruction).
 386   const int code_size = (num_features+1+2*7)*BytesPerInstWord;
 387 #endif
 388   int features = 0;
 389 
 390   // create test area
 391   enum { BUFFER_SIZE = 2*4*K }; // Needs to be >=2* max cache line size (cache line size can't exceed min page size).
 392   char test_area[BUFFER_SIZE];
 393   char *mid_of_test_area = &test_area[BUFFER_SIZE>>1];
 394 
 395   // Allocate space for the code.
 396   ResourceMark rm;
 397   CodeBuffer cb("detect_cpu_features", code_size, 0);
 398   MacroAssembler* a = new MacroAssembler(&cb);
 399 
 400   // Must be set to true so we can generate the test code.
 401   _features = VM_Version::all_features_m;
 402 
 403   // Emit code.
 404   void (*test)(address addr, uint64_t offset)=(void(*)(address addr, uint64_t offset))(void *)a->function_entry();
 405   uint32_t *code = (uint32_t *)a->pc();
 406   // Don't use R0 in ldarx.
 407   // Keep R3_ARG1 unmodified, it contains &field (see below).
 408   // Keep R4_ARG2 unmodified, it contains offset = 0 (see below).
 409   a->fsqrt(F3, F4);                            // code[0] -> fsqrt_m
 410   a->fsqrts(F3, F4);                           // code[1] -> fsqrts_m
 411   a->isel(R7, R5, R6, 0);                      // code[2] -> isel_m
 412   a->ldarx_unchecked(R7, R3_ARG1, R4_ARG2, 1); // code[3] -> lxarx_m
 413   a->cmpb(R7, R5, R6);                         // code[4] -> bcmp
 414   //a->mftgpr(R7, F3);                         // code[5] -> mftgpr
 415   a->popcntb(R7, R5);                          // code[6] -> popcntb
 416   a->popcntw(R7, R5);                          // code[7] -> popcntw
 417   a->fcfids(F3, F4);                           // code[8] -> fcfids
 418   a->vand(VR0, VR0, VR0);                      // code[9] -> vand
 419   a->blr();
 420 
 421   // Emit function to set one cache line to zero. Emit function descriptor and get pointer to it.
 422   void (*zero_cacheline_func_ptr)(char*) = (void(*)(char*))(void *)a->function_entry();
 423   a->dcbz(R3_ARG1); // R3_ARG1 = addr
 424   a->blr();
 425 
 426   uint32_t *code_end = (uint32_t *)a->pc();
 427   a->flush();
 428   _features = VM_Version::unknown_m;
 429 
 430   // Print the detection code.
 431   if (PrintAssembly) {
 432     ttyLocker ttyl;
 433     tty->print_cr("Decoding cpu-feature detection stub at " INTPTR_FORMAT " before execution:", p2i(code));
 434     Disassembler::decode((u_char*)code, (u_char*)code_end, tty);
 435   }
 436 
 437   // Measure cache line size.
 438   memset(test_area, 0xFF, BUFFER_SIZE); // Fill test area with 0xFF.
 439   (*zero_cacheline_func_ptr)(mid_of_test_area); // Call function which executes dcbz to the middle.
 440   int count = 0; // count zeroed bytes
 441   for (int i = 0; i < BUFFER_SIZE; i++) if (test_area[i] == 0) count++;
 442   guarantee(is_power_of_2(count), "cache line size needs to be a power of 2");
 443   _measured_cache_line_size = count;
 444 
 445   // Execute code. Illegal instructions will be replaced by 0 in the signal handler.
 446   VM_Version::_is_determine_features_test_running = true;
 447   (*test)((address)mid_of_test_area, (uint64_t)0);
 448   VM_Version::_is_determine_features_test_running = false;
 449 
 450   // determine which instructions are legal.
 451   int feature_cntr = 0;
 452   if (code[feature_cntr++]) features |= fsqrt_m;
 453   if (code[feature_cntr++]) features |= fsqrts_m;
 454   if (code[feature_cntr++]) features |= isel_m;
 455   if (code[feature_cntr++]) features |= lxarxeh_m;
 456   if (code[feature_cntr++]) features |= cmpb_m;
 457   //if(code[feature_cntr++])features |= mftgpr_m;
 458   if (code[feature_cntr++]) features |= popcntb_m;
 459   if (code[feature_cntr++]) features |= popcntw_m;
 460   if (code[feature_cntr++]) features |= fcfids_m;
 461   if (code[feature_cntr++]) features |= vand_m;
 462 
 463   // Print the detection code.
 464   if (PrintAssembly) {
 465     ttyLocker ttyl;
 466     tty->print_cr("Decoding cpu-feature detection stub at " INTPTR_FORMAT " after execution:", p2i(code));
 467     Disassembler::decode((u_char*)code, (u_char*)code_end, tty);
 468   }
 469 
 470   _features = features;
 471 }
 472 
 473 
 474 static int saved_features = 0;
 475 
 476 void VM_Version::allow_all() {
 477   saved_features = _features;
 478   _features      = all_features_m;
 479 }
 480 
 481 void VM_Version::revert() {
 482   _features = saved_features;
 483 }