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