1 /*
   2  * Copyright (c) 1997, 2010, 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 "asm/codeBuffer.hpp"
  27 #include "memory/resourceArea.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "runtime/interfaceSupport.hpp"
  30 #include "runtime/sharedRuntime.hpp"
  31 #include "runtime/stubRoutines.hpp"
  32 #include "runtime/timer.hpp"
  33 #include "utilities/copy.hpp"
  34 #ifdef COMPILER2
  35 #include "opto/runtime.hpp"
  36 #endif
  37 
  38 
  39 // Implementation of StubRoutines - for a description
  40 // of how to extend it, see the header file.
  41 
  42 // Class Variables
  43 
  44 BufferBlob* StubRoutines::_code1                                = NULL;
  45 BufferBlob* StubRoutines::_code2                                = NULL;
  46 
  47 address StubRoutines::_call_stub_return_address                 = NULL;
  48 address StubRoutines::_call_stub_entry                          = NULL;
  49 
  50 address StubRoutines::_catch_exception_entry                    = NULL;
  51 address StubRoutines::_forward_exception_entry                  = NULL;
  52 address StubRoutines::_throw_AbstractMethodError_entry          = NULL;
  53 address StubRoutines::_throw_IncompatibleClassChangeError_entry = NULL;
  54 address StubRoutines::_throw_ArithmeticException_entry          = NULL;
  55 address StubRoutines::_throw_NullPointerException_entry         = NULL;
  56 address StubRoutines::_throw_NullPointerException_at_call_entry = NULL;
  57 address StubRoutines::_throw_StackOverflowError_entry           = NULL;
  58 address StubRoutines::_handler_for_unsafe_access_entry          = NULL;
  59 jint    StubRoutines::_verify_oop_count                         = 0;
  60 address StubRoutines::_verify_oop_subroutine_entry              = NULL;
  61 address StubRoutines::_atomic_xchg_entry                        = NULL;
  62 address StubRoutines::_atomic_xchg_ptr_entry                    = NULL;
  63 address StubRoutines::_atomic_store_entry                       = NULL;
  64 address StubRoutines::_atomic_store_ptr_entry                   = NULL;
  65 address StubRoutines::_atomic_cmpxchg_entry                     = NULL;
  66 address StubRoutines::_atomic_cmpxchg_ptr_entry                 = NULL;
  67 address StubRoutines::_atomic_cmpxchg_long_entry                = NULL;
  68 address StubRoutines::_atomic_add_entry                         = NULL;
  69 address StubRoutines::_atomic_add_ptr_entry                     = NULL;
  70 address StubRoutines::_fence_entry                              = NULL;
  71 address StubRoutines::_d2i_wrapper                              = NULL;
  72 address StubRoutines::_d2l_wrapper                              = NULL;
  73 
  74 jint    StubRoutines::_fpu_cntrl_wrd_std                        = 0;
  75 jint    StubRoutines::_fpu_cntrl_wrd_24                         = 0;
  76 jint    StubRoutines::_fpu_cntrl_wrd_64                         = 0;
  77 jint    StubRoutines::_fpu_cntrl_wrd_trunc                      = 0;
  78 jint    StubRoutines::_mxcsr_std                                = 0;
  79 jint    StubRoutines::_fpu_subnormal_bias1[3]                   = { 0, 0, 0 };
  80 jint    StubRoutines::_fpu_subnormal_bias2[3]                   = { 0, 0, 0 };
  81 
  82 // Compiled code entry points default values
  83 // The dafault functions don't have separate disjoint versions.
  84 address StubRoutines::_jbyte_arraycopy          = CAST_FROM_FN_PTR(address, StubRoutines::jbyte_copy);
  85 address StubRoutines::_jshort_arraycopy         = CAST_FROM_FN_PTR(address, StubRoutines::jshort_copy);
  86 address StubRoutines::_jint_arraycopy           = CAST_FROM_FN_PTR(address, StubRoutines::jint_copy);
  87 address StubRoutines::_jlong_arraycopy          = CAST_FROM_FN_PTR(address, StubRoutines::jlong_copy);
  88 address StubRoutines::_oop_arraycopy            = CAST_FROM_FN_PTR(address, StubRoutines::oop_copy);
  89 address StubRoutines::_jbyte_disjoint_arraycopy          = CAST_FROM_FN_PTR(address, StubRoutines::jbyte_copy);
  90 address StubRoutines::_jshort_disjoint_arraycopy         = CAST_FROM_FN_PTR(address, StubRoutines::jshort_copy);
  91 address StubRoutines::_jint_disjoint_arraycopy           = CAST_FROM_FN_PTR(address, StubRoutines::jint_copy);
  92 address StubRoutines::_jlong_disjoint_arraycopy          = CAST_FROM_FN_PTR(address, StubRoutines::jlong_copy);
  93 address StubRoutines::_oop_disjoint_arraycopy            = CAST_FROM_FN_PTR(address, StubRoutines::oop_copy);
  94 
  95 address StubRoutines::_arrayof_jbyte_arraycopy  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jbyte_copy);
  96 address StubRoutines::_arrayof_jshort_arraycopy = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jshort_copy);
  97 address StubRoutines::_arrayof_jint_arraycopy   = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jint_copy);
  98 address StubRoutines::_arrayof_jlong_arraycopy  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jlong_copy);
  99 address StubRoutines::_arrayof_oop_arraycopy    = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_oop_copy);
 100 address StubRoutines::_arrayof_jbyte_disjoint_arraycopy  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jbyte_copy);
 101 address StubRoutines::_arrayof_jshort_disjoint_arraycopy = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jshort_copy);
 102 address StubRoutines::_arrayof_jint_disjoint_arraycopy   = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jint_copy);
 103 address StubRoutines::_arrayof_jlong_disjoint_arraycopy  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jlong_copy);
 104 address StubRoutines::_arrayof_oop_disjoint_arraycopy  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_oop_copy);
 105 
 106 address StubRoutines::_checkcast_arraycopy               = NULL;
 107 address StubRoutines::_unsafe_arraycopy                  = NULL;
 108 address StubRoutines::_generic_arraycopy                 = NULL;
 109 
 110 
 111 address StubRoutines::_jbyte_fill;
 112 address StubRoutines::_jshort_fill;
 113 address StubRoutines::_jint_fill;
 114 address StubRoutines::_arrayof_jbyte_fill;
 115 address StubRoutines::_arrayof_jshort_fill;
 116 address StubRoutines::_arrayof_jint_fill;
 117 
 118 
 119 double (* StubRoutines::_intrinsic_log   )(double) = NULL;
 120 double (* StubRoutines::_intrinsic_log10 )(double) = NULL;
 121 double (* StubRoutines::_intrinsic_exp   )(double) = NULL;
 122 double (* StubRoutines::_intrinsic_pow   )(double, double) = NULL;
 123 double (* StubRoutines::_intrinsic_sin   )(double) = NULL;
 124 double (* StubRoutines::_intrinsic_cos   )(double) = NULL;
 125 double (* StubRoutines::_intrinsic_tan   )(double) = NULL;
 126 
 127 // Initialization
 128 //
 129 // Note: to break cycle with universe initialization, stubs are generated in two phases.
 130 // The first one generates stubs needed during universe init (e.g., _handle_must_compile_first_entry).
 131 // The second phase includes all other stubs (which may depend on universe being initialized.)
 132 
 133 extern void StubGenerator_generate(CodeBuffer* code, bool all); // only interface to generators
 134 
 135 void StubRoutines::initialize1() {
 136   if (_code1 == NULL) {
 137     ResourceMark rm;
 138     TraceTime timer("StubRoutines generation 1", TraceStartupTime);
 139     _code1 = BufferBlob::create("StubRoutines (1)", code_size1);
 140     if (_code1 == NULL) {
 141       vm_exit_out_of_memory(code_size1, "CodeCache: no room for StubRoutines (1)");
 142     }
 143     CodeBuffer buffer(_code1);
 144     StubGenerator_generate(&buffer, false);
 145   }
 146 }
 147 
 148 
 149 #ifdef ASSERT
 150 typedef void (*arraycopy_fn)(address src, address dst, int count);
 151 
 152 // simple tests of generated arraycopy functions
 153 static void test_arraycopy_func(address func, int alignment) {
 154   int v = 0xcc;
 155   int v2 = 0x11;
 156   jlong lbuffer[8];
 157   jlong lbuffer2[8];
 158   address fbuffer  = (address) lbuffer;
 159   address fbuffer2 = (address) lbuffer2;
 160   unsigned int i;
 161   for (i = 0; i < sizeof(lbuffer); i++) {
 162     fbuffer[i] = v; fbuffer2[i] = v2;
 163   }
 164   // C++ does not guarantee jlong[] array alignment to 8 bytes.
 165   // Use middle of array to check that memory before it is not modified.
 166   address buffer  = (address) round_to((intptr_t)&lbuffer[4], BytesPerLong);
 167   address buffer2 = (address) round_to((intptr_t)&lbuffer2[4], BytesPerLong);
 168   // do an aligned copy
 169   ((arraycopy_fn)func)(buffer, buffer2, 0);
 170   for (i = 0; i < sizeof(lbuffer); i++) {
 171     assert(fbuffer[i] == v && fbuffer2[i] == v2, "shouldn't have copied anything");
 172   }
 173   // adjust destination alignment
 174   ((arraycopy_fn)func)(buffer, buffer2 + alignment, 0);
 175   for (i = 0; i < sizeof(lbuffer); i++) {
 176     assert(fbuffer[i] == v && fbuffer2[i] == v2, "shouldn't have copied anything");
 177   }
 178   // adjust source alignment
 179   ((arraycopy_fn)func)(buffer + alignment, buffer2, 0);
 180   for (i = 0; i < sizeof(lbuffer); i++) {
 181     assert(fbuffer[i] == v && fbuffer2[i] == v2, "shouldn't have copied anything");
 182   }
 183 }
 184 #endif
 185 
 186 
 187 void StubRoutines::initialize2() {
 188   if (_code2 == NULL) {
 189     ResourceMark rm;
 190     TraceTime timer("StubRoutines generation 2", TraceStartupTime);
 191     _code2 = BufferBlob::create("StubRoutines (2)", code_size2);
 192     if (_code2 == NULL) {
 193       vm_exit_out_of_memory(code_size2, "CodeCache: no room for StubRoutines (2)");
 194     }
 195     CodeBuffer buffer(_code2);
 196     StubGenerator_generate(&buffer, true);
 197   }
 198 
 199 #ifdef ASSERT
 200 
 201 #define TEST_ARRAYCOPY(type)                                                    \
 202   test_arraycopy_func(          type##_arraycopy(),          sizeof(type));     \
 203   test_arraycopy_func(          type##_disjoint_arraycopy(), sizeof(type));     \
 204   test_arraycopy_func(arrayof_##type##_arraycopy(),          sizeof(HeapWord)); \
 205   test_arraycopy_func(arrayof_##type##_disjoint_arraycopy(), sizeof(HeapWord))
 206 
 207   // Make sure all the arraycopy stubs properly handle zero count
 208   TEST_ARRAYCOPY(jbyte);
 209   TEST_ARRAYCOPY(jshort);
 210   TEST_ARRAYCOPY(jint);
 211   TEST_ARRAYCOPY(jlong);
 212 
 213 #undef TEST_ARRAYCOPY
 214 
 215 #define TEST_FILL(type)                                                                      \
 216   if (_##type##_fill != NULL) {                                                              \
 217     union {                                                                                  \
 218       double d;                                                                              \
 219       type body[96];                                                                         \
 220     } s;                                                                                     \
 221                                                                                              \
 222     int v = 32;                                                                              \
 223     for (int offset = -2; offset <= 2; offset++) {                                           \
 224       for (int i = 0; i < 96; i++) {                                                         \
 225         s.body[i] = 1;                                                                       \
 226       }                                                                                      \
 227       type* start = s.body + 8 + offset;                                                     \
 228       for (int aligned = 0; aligned < 2; aligned++) {                                        \
 229         if (aligned) {                                                                       \
 230           if (((intptr_t)start) % HeapWordSize == 0) {                                       \
 231             ((void (*)(type*, int, int))StubRoutines::_arrayof_##type##_fill)(start, v, 80); \
 232           } else {                                                                           \
 233             continue;                                                                        \
 234           }                                                                                  \
 235         } else {                                                                             \
 236           ((void (*)(type*, int, int))StubRoutines::_##type##_fill)(start, v, 80);           \
 237         }                                                                                    \
 238         for (int i = 0; i < 96; i++) {                                                       \
 239           if (i < (8 + offset) || i >= (88 + offset)) {                                      \
 240             assert(s.body[i] == 1, "what?");                                                 \
 241           } else {                                                                           \
 242             assert(s.body[i] == 32, "what?");                                                \
 243           }                                                                                  \
 244         }                                                                                    \
 245       }                                                                                      \
 246     }                                                                                        \
 247   }                                                                                          \
 248 
 249   TEST_FILL(jbyte);
 250   TEST_FILL(jshort);
 251   TEST_FILL(jint);
 252 
 253 #undef TEST_FILL
 254 
 255 #define TEST_COPYRTN(type) \
 256   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::conjoint_##type##s_atomic),  sizeof(type)); \
 257   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::arrayof_conjoint_##type##s), (int)MAX2(sizeof(HeapWord), sizeof(type)))
 258 
 259   // Make sure all the copy runtime routines properly handle zero count
 260   TEST_COPYRTN(jbyte);
 261   TEST_COPYRTN(jshort);
 262   TEST_COPYRTN(jint);
 263   TEST_COPYRTN(jlong);
 264 
 265 #undef TEST_COPYRTN
 266 
 267   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::conjoint_words), sizeof(HeapWord));
 268   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::disjoint_words), sizeof(HeapWord));
 269   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::disjoint_words_atomic), sizeof(HeapWord));
 270   // Aligned to BytesPerLong
 271   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::aligned_conjoint_words), sizeof(jlong));
 272   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::aligned_disjoint_words), sizeof(jlong));
 273 
 274 #endif
 275 }
 276 
 277 
 278 void stubRoutines_init1() { StubRoutines::initialize1(); }
 279 void stubRoutines_init2() { StubRoutines::initialize2(); }
 280 
 281 //
 282 // Default versions of arraycopy functions
 283 //
 284 
 285 static void gen_arraycopy_barrier_pre(oop* dest, size_t count) {
 286     assert(count != 0, "count should be non-zero");
 287     assert(count <= (size_t)max_intx, "count too large");
 288     BarrierSet* bs = Universe::heap()->barrier_set();
 289     assert(bs->has_write_ref_array_pre_opt(), "Must have pre-barrier opt");
 290     bs->write_ref_array_pre(dest, (int)count);
 291 }
 292 
 293 static void gen_arraycopy_barrier(oop* dest, size_t count) {
 294     assert(count != 0, "count should be non-zero");
 295     BarrierSet* bs = Universe::heap()->barrier_set();
 296     assert(bs->has_write_ref_array_opt(), "Barrier set must have ref array opt");
 297     bs->write_ref_array((HeapWord*)dest, count);
 298 }
 299 
 300 JRT_LEAF(void, StubRoutines::jbyte_copy(jbyte* src, jbyte* dest, size_t count))
 301 #ifndef PRODUCT
 302   SharedRuntime::_jbyte_array_copy_ctr++;      // Slow-path byte array copy
 303 #endif // !PRODUCT
 304   Copy::conjoint_jbytes_atomic(src, dest, count);
 305 JRT_END
 306 
 307 JRT_LEAF(void, StubRoutines::jshort_copy(jshort* src, jshort* dest, size_t count))
 308 #ifndef PRODUCT
 309   SharedRuntime::_jshort_array_copy_ctr++;     // Slow-path short/char array copy
 310 #endif // !PRODUCT
 311   Copy::conjoint_jshorts_atomic(src, dest, count);
 312 JRT_END
 313 
 314 JRT_LEAF(void, StubRoutines::jint_copy(jint* src, jint* dest, size_t count))
 315 #ifndef PRODUCT
 316   SharedRuntime::_jint_array_copy_ctr++;       // Slow-path int/float array copy
 317 #endif // !PRODUCT
 318   Copy::conjoint_jints_atomic(src, dest, count);
 319 JRT_END
 320 
 321 JRT_LEAF(void, StubRoutines::jlong_copy(jlong* src, jlong* dest, size_t count))
 322 #ifndef PRODUCT
 323   SharedRuntime::_jlong_array_copy_ctr++;      // Slow-path long/double array copy
 324 #endif // !PRODUCT
 325   Copy::conjoint_jlongs_atomic(src, dest, count);
 326 JRT_END
 327 
 328 JRT_LEAF(void, StubRoutines::oop_copy(oop* src, oop* dest, size_t count))
 329 #ifndef PRODUCT
 330   SharedRuntime::_oop_array_copy_ctr++;        // Slow-path oop array copy
 331 #endif // !PRODUCT
 332   assert(count != 0, "count should be non-zero");
 333   gen_arraycopy_barrier_pre(dest, count);
 334   Copy::conjoint_oops_atomic(src, dest, count);
 335   gen_arraycopy_barrier(dest, count);
 336 JRT_END
 337 
 338 JRT_LEAF(void, StubRoutines::arrayof_jbyte_copy(HeapWord* src, HeapWord* dest, size_t count))
 339 #ifndef PRODUCT
 340   SharedRuntime::_jbyte_array_copy_ctr++;      // Slow-path byte array copy
 341 #endif // !PRODUCT
 342   Copy::arrayof_conjoint_jbytes(src, dest, count);
 343 JRT_END
 344 
 345 JRT_LEAF(void, StubRoutines::arrayof_jshort_copy(HeapWord* src, HeapWord* dest, size_t count))
 346 #ifndef PRODUCT
 347   SharedRuntime::_jshort_array_copy_ctr++;     // Slow-path short/char array copy
 348 #endif // !PRODUCT
 349   Copy::arrayof_conjoint_jshorts(src, dest, count);
 350 JRT_END
 351 
 352 JRT_LEAF(void, StubRoutines::arrayof_jint_copy(HeapWord* src, HeapWord* dest, size_t count))
 353 #ifndef PRODUCT
 354   SharedRuntime::_jint_array_copy_ctr++;       // Slow-path int/float array copy
 355 #endif // !PRODUCT
 356   Copy::arrayof_conjoint_jints(src, dest, count);
 357 JRT_END
 358 
 359 JRT_LEAF(void, StubRoutines::arrayof_jlong_copy(HeapWord* src, HeapWord* dest, size_t count))
 360 #ifndef PRODUCT
 361   SharedRuntime::_jlong_array_copy_ctr++;       // Slow-path int/float array copy
 362 #endif // !PRODUCT
 363   Copy::arrayof_conjoint_jlongs(src, dest, count);
 364 JRT_END
 365 
 366 JRT_LEAF(void, StubRoutines::arrayof_oop_copy(HeapWord* src, HeapWord* dest, size_t count))
 367 #ifndef PRODUCT
 368   SharedRuntime::_oop_array_copy_ctr++;        // Slow-path oop array copy
 369 #endif // !PRODUCT
 370   assert(count != 0, "count should be non-zero");
 371   gen_arraycopy_barrier_pre((oop *) dest, count);
 372   Copy::arrayof_conjoint_oops(src, dest, count);
 373   gen_arraycopy_barrier((oop *) dest, count);
 374 JRT_END
 375 
 376 
 377 address StubRoutines::select_fill_function(BasicType t, bool aligned, const char* &name) {
 378 #define RETURN_STUB(xxx_fill) { \
 379   name = #xxx_fill; \
 380   return StubRoutines::xxx_fill(); }
 381 
 382   switch (t) {
 383   case T_BYTE:
 384   case T_BOOLEAN:
 385     if (!aligned) RETURN_STUB(jbyte_fill);
 386     RETURN_STUB(arrayof_jbyte_fill);
 387   case T_CHAR:
 388   case T_SHORT:
 389     if (!aligned) RETURN_STUB(jshort_fill);
 390     RETURN_STUB(arrayof_jshort_fill);
 391   case T_INT:
 392   case T_FLOAT:
 393     if (!aligned) RETURN_STUB(jint_fill);
 394     RETURN_STUB(arrayof_jint_fill);
 395   case T_DOUBLE:
 396   case T_LONG:
 397   case T_ARRAY:
 398   case T_OBJECT:
 399   case T_NARROWOOP:
 400   case T_ADDRESS:
 401     // Currently unsupported
 402     return NULL;
 403 
 404   default:
 405     ShouldNotReachHere();
 406     return NULL;
 407   }
 408 
 409 #undef RETURN_STUB
 410 }