1 /*
   2  * Copyright (c) 1997, 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 #ifndef SHARE_VM_RUNTIME_STUBROUTINES_HPP
  26 #define SHARE_VM_RUNTIME_STUBROUTINES_HPP
  27 
  28 #include "code/codeBlob.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "runtime/frame.hpp"
  31 #include "runtime/mutexLocker.hpp"
  32 #include "runtime/stubCodeGenerator.hpp"
  33 #include "utilities/top.hpp"
  34 #ifdef TARGET_ARCH_x86
  35 # include "nativeInst_x86.hpp"
  36 #endif
  37 #ifdef TARGET_ARCH_aarch64
  38 # include "nativeInst_aarch64.hpp"
  39 #endif
  40 #ifdef TARGET_ARCH_sparc
  41 # include "nativeInst_sparc.hpp"
  42 #endif
  43 #ifdef TARGET_ARCH_zero
  44 # include "nativeInst_zero.hpp"
  45 #endif
  46 #ifdef TARGET_ARCH_arm
  47 # include "nativeInst_arm.hpp"
  48 #endif
  49 #ifdef TARGET_ARCH_ppc
  50 # include "nativeInst_ppc.hpp"
  51 #endif
  52 
  53 // StubRoutines provides entry points to assembly routines used by
  54 // compiled code and the run-time system. Platform-specific entry
  55 // points are defined in the platform-specific inner class.
  56 //
  57 // Class scheme:
  58 //
  59 //    platform-independent               platform-dependent
  60 //
  61 //    stubRoutines.hpp  <-- included --  stubRoutines_<arch>.hpp
  62 //           ^                                  ^
  63 //           |                                  |
  64 //       implements                         implements
  65 //           |                                  |
  66 //           |                                  |
  67 //    stubRoutines.cpp                   stubRoutines_<arch>.cpp
  68 //    stubRoutines_<os_family>.cpp       stubGenerator_<arch>.cpp
  69 //    stubRoutines_<os_arch>.cpp
  70 //
  71 // Note 1: The important thing is a clean decoupling between stub
  72 //         entry points (interfacing to the whole vm; i.e., 1-to-n
  73 //         relationship) and stub generators (interfacing only to
  74 //         the entry points implementation; i.e., 1-to-1 relationship).
  75 //         This significantly simplifies changes in the generator
  76 //         structure since the rest of the vm is not affected.
  77 //
  78 // Note 2: stubGenerator_<arch>.cpp contains a minimal portion of
  79 //         machine-independent code; namely the generator calls of
  80 //         the generator functions that are used platform-independently.
  81 //         However, it comes with the advantage of having a 1-file
  82 //         implementation of the generator. It should be fairly easy
  83 //         to change, should it become a problem later.
  84 //
  85 // Scheme for adding a new entry point:
  86 //
  87 // 1. determine if it's a platform-dependent or independent entry point
  88 //    a) if platform independent: make subsequent changes in the independent files
  89 //    b) if platform   dependent: make subsequent changes in the   dependent files
  90 // 2. add a private instance variable holding the entry point address
  91 // 3. add a public accessor function to the instance variable
  92 // 4. implement the corresponding generator function in the platform-dependent
  93 //    stubGenerator_<arch>.cpp file and call the function in generate_all() of that file
  94 
  95 
  96 class StubRoutines: AllStatic {
  97 
  98  public:
  99   enum platform_independent_constants {
 100     max_size_of_parameters = 256                           // max. parameter size supported by megamorphic lookups
 101   };
 102 
 103   // Dependencies
 104   friend class StubGenerator;
 105 #if defined STUBROUTINES_MD_HPP
 106 # include STUBROUTINES_MD_HPP
 107 #elif defined TARGET_ARCH_MODEL_x86_32
 108 # include "stubRoutines_x86_32.hpp"
 109 #elif defined TARGET_ARCH_MODEL_x86_64
 110 # include "stubRoutines_x86_64.hpp"
 111 #elif defined TARGET_ARCH_MODEL_aarch64
 112 # include "stubRoutines_aarch64.hpp"
 113 #elif defined TARGET_ARCH_MODEL_sparc
 114 # include "stubRoutines_sparc.hpp"
 115 #elif defined TARGET_ARCH_MODEL_zero
 116 # include "stubRoutines_zero.hpp"
 117 #elif defined TARGET_ARCH_MODEL_ppc_64
 118 # include "stubRoutines_ppc_64.hpp"
 119 #endif
 120 
 121   static jint    _verify_oop_count;
 122   static address _verify_oop_subroutine_entry;
 123 
 124   static address _call_stub_return_address;                // the return PC, when returning to a call stub
 125   static address _call_stub_entry;
 126   static address _forward_exception_entry;
 127   static address _catch_exception_entry;
 128   static address _throw_AbstractMethodError_entry;
 129   static address _throw_IncompatibleClassChangeError_entry;
 130   static address _throw_NullPointerException_at_call_entry;
 131   static address _throw_StackOverflowError_entry;
 132   static address _handler_for_unsafe_access_entry;
 133 
 134   static address _atomic_xchg_entry;
 135   static address _atomic_xchg_ptr_entry;
 136   static address _atomic_store_entry;
 137   static address _atomic_store_ptr_entry;
 138   static address _atomic_cmpxchg_entry;
 139   static address _atomic_cmpxchg_ptr_entry;
 140   static address _atomic_cmpxchg_long_entry;
 141   static address _atomic_add_entry;
 142   static address _atomic_add_ptr_entry;
 143   static address _fence_entry;
 144   static address _d2i_wrapper;
 145   static address _d2l_wrapper;
 146 
 147   static jint    _fpu_cntrl_wrd_std;
 148   static jint    _fpu_cntrl_wrd_24;
 149   static jint    _fpu_cntrl_wrd_64;
 150   static jint    _fpu_cntrl_wrd_trunc;
 151   static jint    _mxcsr_std;
 152   static jint    _fpu_subnormal_bias1[3];
 153   static jint    _fpu_subnormal_bias2[3];
 154 
 155   static BufferBlob* _code1;                               // code buffer for initial routines
 156   static BufferBlob* _code2;                               // code buffer for all other routines
 157 
 158   // Leaf routines which implement arraycopy and their addresses
 159   // arraycopy operands aligned on element type boundary
 160   static address _jbyte_arraycopy;
 161   static address _jshort_arraycopy;
 162   static address _jint_arraycopy;
 163   static address _jlong_arraycopy;
 164   static address _oop_arraycopy, _oop_arraycopy_uninit;
 165   static address _jbyte_disjoint_arraycopy;
 166   static address _jshort_disjoint_arraycopy;
 167   static address _jint_disjoint_arraycopy;
 168   static address _jlong_disjoint_arraycopy;
 169   static address _oop_disjoint_arraycopy, _oop_disjoint_arraycopy_uninit;
 170 
 171   // arraycopy operands aligned on zero'th element boundary
 172   // These are identical to the ones aligned aligned on an
 173   // element type boundary, except that they assume that both
 174   // source and destination are HeapWord aligned.
 175   static address _arrayof_jbyte_arraycopy;
 176   static address _arrayof_jshort_arraycopy;
 177   static address _arrayof_jint_arraycopy;
 178   static address _arrayof_jlong_arraycopy;
 179   static address _arrayof_oop_arraycopy, _arrayof_oop_arraycopy_uninit;
 180   static address _arrayof_jbyte_disjoint_arraycopy;
 181   static address _arrayof_jshort_disjoint_arraycopy;
 182   static address _arrayof_jint_disjoint_arraycopy;
 183   static address _arrayof_jlong_disjoint_arraycopy;
 184   static address _arrayof_oop_disjoint_arraycopy, _arrayof_oop_disjoint_arraycopy_uninit;
 185 
 186   // these are recommended but optional:
 187   static address _checkcast_arraycopy, _checkcast_arraycopy_uninit;
 188   static address _unsafe_arraycopy;
 189   static address _generic_arraycopy;
 190 
 191   static address _jbyte_fill;
 192   static address _jshort_fill;
 193   static address _jint_fill;
 194   static address _arrayof_jbyte_fill;
 195   static address _arrayof_jshort_fill;
 196   static address _arrayof_jint_fill;
 197 
 198   // zero heap space aligned to jlong (8 bytes)
 199   static address _zero_aligned_words;
 200 
 201   static address _aescrypt_encryptBlock;
 202   static address _aescrypt_decryptBlock;
 203   static address _cipherBlockChaining_encryptAESCrypt;
 204   static address _cipherBlockChaining_decryptAESCrypt;
 205   static address _ghash_processBlocks;
 206 
 207   static address _sha1_implCompress;
 208   static address _sha1_implCompressMB;
 209   static address _sha256_implCompress;
 210   static address _sha256_implCompressMB;
 211   static address _sha512_implCompress;
 212   static address _sha512_implCompressMB;
 213 
 214   static address _updateBytesCRC32;
 215   static address _crc_table_adr;
 216 
 217   static address _multiplyToLen;
 218   static address _squareToLen;
 219   static address _mulAdd;
 220   static address _montgomeryMultiply;
 221   static address _montgomerySquare;
 222 
 223   // These are versions of the java.lang.Math methods which perform
 224   // the same operations as the intrinsic version.  They are used for
 225   // constant folding in the compiler to ensure equivalence.  If the
 226   // intrinsic version returns the same result as the strict version
 227   // then they can be set to the appropriate function from
 228   // SharedRuntime.
 229   static double (*_intrinsic_log)(double);
 230   static double (*_intrinsic_log10)(double);
 231   static double (*_intrinsic_exp)(double);
 232   static double (*_intrinsic_pow)(double, double);
 233   static double (*_intrinsic_sin)(double);
 234   static double (*_intrinsic_cos)(double);
 235   static double (*_intrinsic_tan)(double);
 236 
 237   // Safefetch stubs.
 238   static address _safefetch32_entry;
 239   static address _safefetch32_fault_pc;
 240   static address _safefetch32_continuation_pc;
 241   static address _safefetchN_entry;
 242   static address _safefetchN_fault_pc;
 243   static address _safefetchN_continuation_pc;
 244 
 245  public:
 246   // Initialization/Testing
 247   static void    initialize1();                            // must happen before universe::genesis
 248   static void    initialize2();                            // must happen after  universe::genesis
 249 
 250   static bool is_stub_code(address addr)                   { return contains(addr); }
 251 
 252   static bool contains(address addr) {
 253     return
 254       (_code1 != NULL && _code1->blob_contains(addr)) ||
 255       (_code2 != NULL && _code2->blob_contains(addr)) ;
 256   }
 257 
 258   static CodeBlob* code1() { return _code1; }
 259   static CodeBlob* code2() { return _code2; }
 260 
 261   // Debugging
 262   static jint    verify_oop_count()                        { return _verify_oop_count; }
 263   static jint*   verify_oop_count_addr()                   { return &_verify_oop_count; }
 264   // a subroutine for debugging the GC
 265   static address verify_oop_subroutine_entry_address()    { return (address)&_verify_oop_subroutine_entry; }
 266 
 267   static address catch_exception_entry()                   { return _catch_exception_entry; }
 268 
 269   // Calls to Java
 270   typedef void (*CallStub)(
 271     address   link,
 272     intptr_t* result,
 273     BasicType result_type,
 274     Method* method,
 275     address   entry_point,
 276     intptr_t* parameters,
 277     int       size_of_parameters,
 278     TRAPS
 279   );
 280 
 281   static CallStub call_stub()                              { return CAST_TO_FN_PTR(CallStub, _call_stub_entry); }
 282 
 283   // Exceptions
 284   static address forward_exception_entry()                 { return _forward_exception_entry; }
 285   // Implicit exceptions
 286   static address throw_AbstractMethodError_entry()         { return _throw_AbstractMethodError_entry; }
 287   static address throw_IncompatibleClassChangeError_entry(){ return _throw_IncompatibleClassChangeError_entry; }
 288   static address throw_NullPointerException_at_call_entry(){ return _throw_NullPointerException_at_call_entry; }
 289   static address throw_StackOverflowError_entry()          { return _throw_StackOverflowError_entry; }
 290 
 291   // Exceptions during unsafe access - should throw Java exception rather
 292   // than crash.
 293   static address handler_for_unsafe_access()               { return _handler_for_unsafe_access_entry; }
 294 
 295   static address atomic_xchg_entry()                       { return _atomic_xchg_entry; }
 296   static address atomic_xchg_ptr_entry()                   { return _atomic_xchg_ptr_entry; }
 297   static address atomic_store_entry()                      { return _atomic_store_entry; }
 298   static address atomic_store_ptr_entry()                  { return _atomic_store_ptr_entry; }
 299   static address atomic_cmpxchg_entry()                    { return _atomic_cmpxchg_entry; }
 300   static address atomic_cmpxchg_ptr_entry()                { return _atomic_cmpxchg_ptr_entry; }
 301   static address atomic_cmpxchg_long_entry()               { return _atomic_cmpxchg_long_entry; }
 302   static address atomic_add_entry()                        { return _atomic_add_entry; }
 303   static address atomic_add_ptr_entry()                    { return _atomic_add_ptr_entry; }
 304   static address fence_entry()                             { return _fence_entry; }
 305 
 306   static address d2i_wrapper()                             { return _d2i_wrapper; }
 307   static address d2l_wrapper()                             { return _d2l_wrapper; }
 308   static jint    fpu_cntrl_wrd_std()                       { return _fpu_cntrl_wrd_std;   }
 309   static address addr_fpu_cntrl_wrd_std()                  { return (address)&_fpu_cntrl_wrd_std;   }
 310   static address addr_fpu_cntrl_wrd_24()                   { return (address)&_fpu_cntrl_wrd_24;   }
 311   static address addr_fpu_cntrl_wrd_64()                   { return (address)&_fpu_cntrl_wrd_64;   }
 312   static address addr_fpu_cntrl_wrd_trunc()                { return (address)&_fpu_cntrl_wrd_trunc; }
 313   static address addr_mxcsr_std()                          { return (address)&_mxcsr_std; }
 314   static address addr_fpu_subnormal_bias1()                { return (address)&_fpu_subnormal_bias1; }
 315   static address addr_fpu_subnormal_bias2()                { return (address)&_fpu_subnormal_bias2; }
 316 
 317 
 318   static address select_arraycopy_function(BasicType t, bool aligned, bool disjoint, const char* &name, bool dest_uninitialized);
 319 
 320   static address jbyte_arraycopy()  { return _jbyte_arraycopy; }
 321   static address jshort_arraycopy() { return _jshort_arraycopy; }
 322   static address jint_arraycopy()   { return _jint_arraycopy; }
 323   static address jlong_arraycopy()  { return _jlong_arraycopy; }
 324   static address oop_arraycopy(bool dest_uninitialized = false) {
 325     return dest_uninitialized ? _oop_arraycopy_uninit : _oop_arraycopy;
 326   }
 327   static address jbyte_disjoint_arraycopy()  { return _jbyte_disjoint_arraycopy; }
 328   static address jshort_disjoint_arraycopy() { return _jshort_disjoint_arraycopy; }
 329   static address jint_disjoint_arraycopy()   { return _jint_disjoint_arraycopy; }
 330   static address jlong_disjoint_arraycopy()  { return _jlong_disjoint_arraycopy; }
 331   static address oop_disjoint_arraycopy(bool dest_uninitialized = false) {
 332     return dest_uninitialized ?  _oop_disjoint_arraycopy_uninit : _oop_disjoint_arraycopy;
 333   }
 334 
 335   static address arrayof_jbyte_arraycopy()  { return _arrayof_jbyte_arraycopy; }
 336   static address arrayof_jshort_arraycopy() { return _arrayof_jshort_arraycopy; }
 337   static address arrayof_jint_arraycopy()   { return _arrayof_jint_arraycopy; }
 338   static address arrayof_jlong_arraycopy()  { return _arrayof_jlong_arraycopy; }
 339   static address arrayof_oop_arraycopy(bool dest_uninitialized = false) {
 340     return dest_uninitialized ? _arrayof_oop_arraycopy_uninit : _arrayof_oop_arraycopy;
 341   }
 342 
 343   static address arrayof_jbyte_disjoint_arraycopy()  { return _arrayof_jbyte_disjoint_arraycopy; }
 344   static address arrayof_jshort_disjoint_arraycopy() { return _arrayof_jshort_disjoint_arraycopy; }
 345   static address arrayof_jint_disjoint_arraycopy()   { return _arrayof_jint_disjoint_arraycopy; }
 346   static address arrayof_jlong_disjoint_arraycopy()  { return _arrayof_jlong_disjoint_arraycopy; }
 347   static address arrayof_oop_disjoint_arraycopy(bool dest_uninitialized = false) {
 348     return dest_uninitialized ? _arrayof_oop_disjoint_arraycopy_uninit : _arrayof_oop_disjoint_arraycopy;
 349   }
 350 
 351   static address checkcast_arraycopy(bool dest_uninitialized = false) {
 352     return dest_uninitialized ? _checkcast_arraycopy_uninit : _checkcast_arraycopy;
 353   }
 354   static address unsafe_arraycopy()        { return _unsafe_arraycopy; }
 355   static address generic_arraycopy()       { return _generic_arraycopy; }
 356 
 357   static address jbyte_fill()          { return _jbyte_fill; }
 358   static address jshort_fill()         { return _jshort_fill; }
 359   static address jint_fill()           { return _jint_fill; }
 360   static address arrayof_jbyte_fill()  { return _arrayof_jbyte_fill; }
 361   static address arrayof_jshort_fill() { return _arrayof_jshort_fill; }
 362   static address arrayof_jint_fill()   { return _arrayof_jint_fill; }
 363 
 364   static address aescrypt_encryptBlock()                { return _aescrypt_encryptBlock; }
 365   static address aescrypt_decryptBlock()                { return _aescrypt_decryptBlock; }
 366   static address cipherBlockChaining_encryptAESCrypt()  { return _cipherBlockChaining_encryptAESCrypt; }
 367   static address cipherBlockChaining_decryptAESCrypt()  { return _cipherBlockChaining_decryptAESCrypt; }
 368   static address ghash_processBlocks() { return _ghash_processBlocks; }
 369 
 370   static address sha1_implCompress()     { return _sha1_implCompress; }
 371   static address sha1_implCompressMB()   { return _sha1_implCompressMB; }
 372   static address sha256_implCompress()   { return _sha256_implCompress; }
 373   static address sha256_implCompressMB() { return _sha256_implCompressMB; }
 374   static address sha512_implCompress()   { return _sha512_implCompress; }
 375   static address sha512_implCompressMB() { return _sha512_implCompressMB; }
 376 
 377   static address updateBytesCRC32()    { return _updateBytesCRC32; }
 378   static address crc_table_addr()      { return _crc_table_adr; }
 379 
 380   static address multiplyToLen()       {return _multiplyToLen; }
 381   static address squareToLen()         {return _squareToLen; }
 382   static address mulAdd()              {return _mulAdd; }
 383   static address montgomeryMultiply()  { return _montgomeryMultiply; }
 384   static address montgomerySquare()    { return _montgomerySquare; }
 385 
 386   static address select_fill_function(BasicType t, bool aligned, const char* &name);
 387 
 388   static address zero_aligned_words()   { return _zero_aligned_words; }
 389 
 390   static double  intrinsic_log(double d) {
 391     assert(_intrinsic_log != NULL, "must be defined");
 392     return _intrinsic_log(d);
 393   }
 394   static double  intrinsic_log10(double d) {
 395     assert(_intrinsic_log != NULL, "must be defined");
 396     return _intrinsic_log10(d);
 397   }
 398   static double  intrinsic_exp(double d) {
 399     assert(_intrinsic_exp != NULL, "must be defined");
 400     return _intrinsic_exp(d);
 401   }
 402   static double  intrinsic_pow(double d, double d2) {
 403     assert(_intrinsic_pow != NULL, "must be defined");
 404     return _intrinsic_pow(d, d2);
 405   }
 406   static double  intrinsic_sin(double d) {
 407     assert(_intrinsic_sin != NULL, "must be defined");
 408     return _intrinsic_sin(d);
 409   }
 410   static double  intrinsic_cos(double d) {
 411     assert(_intrinsic_cos != NULL, "must be defined");
 412     return _intrinsic_cos(d);
 413   }
 414   static double  intrinsic_tan(double d) {
 415     assert(_intrinsic_tan != NULL, "must be defined");
 416     return _intrinsic_tan(d);
 417   }
 418 
 419   //
 420   // Safefetch stub support
 421   //
 422 
 423   typedef int      (*SafeFetch32Stub)(int*      adr, int      errValue);
 424   typedef intptr_t (*SafeFetchNStub) (intptr_t* adr, intptr_t errValue);
 425 
 426   static SafeFetch32Stub SafeFetch32_stub() { return CAST_TO_FN_PTR(SafeFetch32Stub, _safefetch32_entry); }
 427   static SafeFetchNStub  SafeFetchN_stub()  { return CAST_TO_FN_PTR(SafeFetchNStub,  _safefetchN_entry); }
 428 
 429   static bool is_safefetch_fault(address pc) {
 430     return pc != NULL &&
 431           (pc == _safefetch32_fault_pc ||
 432            pc == _safefetchN_fault_pc);
 433   }
 434 
 435   static address continuation_for_safefetch_fault(address pc) {
 436     assert(_safefetch32_continuation_pc != NULL &&
 437            _safefetchN_continuation_pc  != NULL,
 438            "not initialized");
 439 
 440     if (pc == _safefetch32_fault_pc) return _safefetch32_continuation_pc;
 441     if (pc == _safefetchN_fault_pc)  return _safefetchN_continuation_pc;
 442 
 443     ShouldNotReachHere();
 444     return NULL;
 445   }
 446 
 447   //
 448   // Default versions of the above arraycopy functions for platforms which do
 449   // not have specialized versions
 450   //
 451   static void jbyte_copy     (jbyte*  src, jbyte*  dest, size_t count);
 452   static void jshort_copy    (jshort* src, jshort* dest, size_t count);
 453   static void jint_copy      (jint*   src, jint*   dest, size_t count);
 454   static void jlong_copy     (jlong*  src, jlong*  dest, size_t count);
 455   static void oop_copy       (oop*    src, oop*    dest, size_t count);
 456   static void oop_copy_uninit(oop*    src, oop*    dest, size_t count);
 457 
 458   static void arrayof_jbyte_copy     (HeapWord* src, HeapWord* dest, size_t count);
 459   static void arrayof_jshort_copy    (HeapWord* src, HeapWord* dest, size_t count);
 460   static void arrayof_jint_copy      (HeapWord* src, HeapWord* dest, size_t count);
 461   static void arrayof_jlong_copy     (HeapWord* src, HeapWord* dest, size_t count);
 462   static void arrayof_oop_copy       (HeapWord* src, HeapWord* dest, size_t count);
 463   static void arrayof_oop_copy_uninit(HeapWord* src, HeapWord* dest, size_t count);
 464 };
 465 
 466 // Safefetch allows to load a value from a location that's not known
 467 // to be valid. If the load causes a fault, the error value is returned.
 468 inline int SafeFetch32(int* adr, int errValue) {
 469   assert(StubRoutines::SafeFetch32_stub(), "stub not yet generated");
 470   return StubRoutines::SafeFetch32_stub()(adr, errValue);
 471 }
 472 inline intptr_t SafeFetchN(intptr_t* adr, intptr_t errValue) {
 473   assert(StubRoutines::SafeFetchN_stub(), "stub not yet generated");
 474   return StubRoutines::SafeFetchN_stub()(adr, errValue);
 475 }
 476 
 477 #endif // SHARE_VM_RUNTIME_STUBROUTINES_HPP