1 /*
  2  * Copyright (c) 1997, 2023, 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 "asm/macroAssembler.inline.hpp"
 28 #include "memory/resourceArea.hpp"
 29 #include "oops/access.inline.hpp"
 30 #include "oops/oop.inline.hpp"
 31 #include "prims/vectorSupport.hpp"
 32 #include "runtime/continuation.hpp"
 33 #include "runtime/interfaceSupport.inline.hpp"
 34 #include "runtime/timerTrace.hpp"
 35 #include "runtime/sharedRuntime.hpp"
 36 #include "runtime/stubRoutines.hpp"
 37 #include "utilities/align.hpp"
 38 #include "utilities/copy.hpp"
 39 #ifdef COMPILER2
 40 #include "opto/runtime.hpp"
 41 #endif
 42 
 43 UnsafeCopyMemory* UnsafeCopyMemory::_table                      = nullptr;
 44 int UnsafeCopyMemory::_table_length                             = 0;
 45 int UnsafeCopyMemory::_table_max_length                         = 0;
 46 address UnsafeCopyMemory::_common_exit_stub_pc                  = nullptr;
 47 
 48 // Implementation of StubRoutines - for a description
 49 // of how to extend it, see the header file.
 50 
 51 // Class Variables
 52 
 53 BufferBlob* StubRoutines::_initial_stubs_code                   = nullptr;
 54 BufferBlob* StubRoutines::_final_stubs_code                     = nullptr;
 55 BufferBlob* StubRoutines::_compiler_stubs_code                  = nullptr;
 56 BufferBlob* StubRoutines::_continuation_stubs_code              = nullptr;
 57 
 58 address StubRoutines::_call_stub_return_address                 = nullptr;
 59 address StubRoutines::_call_stub_entry                          = nullptr;
 60 
 61 address StubRoutines::_catch_exception_entry                    = nullptr;
 62 address StubRoutines::_forward_exception_entry                  = nullptr;
 63 address StubRoutines::_throw_AbstractMethodError_entry          = nullptr;
 64 address StubRoutines::_throw_IncompatibleClassChangeError_entry = nullptr;
 65 address StubRoutines::_throw_NullPointerException_at_call_entry = nullptr;
 66 address StubRoutines::_throw_StackOverflowError_entry           = nullptr;
 67 address StubRoutines::_throw_delayed_StackOverflowError_entry   = nullptr;
 68 jint    StubRoutines::_verify_oop_count                         = 0;
 69 address StubRoutines::_verify_oop_subroutine_entry              = nullptr;
 70 address StubRoutines::_atomic_xchg_entry                        = nullptr;
 71 address StubRoutines::_atomic_cmpxchg_entry                     = nullptr;
 72 address StubRoutines::_atomic_cmpxchg_long_entry                = nullptr;
 73 address StubRoutines::_atomic_add_entry                         = nullptr;
 74 address StubRoutines::_fence_entry                              = nullptr;
 75 
 76 // Compiled code entry points default values
 77 // The default functions don't have separate disjoint versions.
 78 address StubRoutines::_jbyte_arraycopy          = CAST_FROM_FN_PTR(address, StubRoutines::jbyte_copy);
 79 address StubRoutines::_jshort_arraycopy         = CAST_FROM_FN_PTR(address, StubRoutines::jshort_copy);
 80 address StubRoutines::_jint_arraycopy           = CAST_FROM_FN_PTR(address, StubRoutines::jint_copy);
 81 address StubRoutines::_jlong_arraycopy          = CAST_FROM_FN_PTR(address, StubRoutines::jlong_copy);
 82 address StubRoutines::_oop_arraycopy            = CAST_FROM_FN_PTR(address, StubRoutines::oop_copy);
 83 address StubRoutines::_oop_arraycopy_uninit     = CAST_FROM_FN_PTR(address, StubRoutines::oop_copy_uninit);
 84 address StubRoutines::_jbyte_disjoint_arraycopy          = CAST_FROM_FN_PTR(address, StubRoutines::jbyte_copy);
 85 address StubRoutines::_jshort_disjoint_arraycopy         = CAST_FROM_FN_PTR(address, StubRoutines::jshort_copy);
 86 address StubRoutines::_jint_disjoint_arraycopy           = CAST_FROM_FN_PTR(address, StubRoutines::jint_copy);
 87 address StubRoutines::_jlong_disjoint_arraycopy          = CAST_FROM_FN_PTR(address, StubRoutines::jlong_copy);
 88 address StubRoutines::_oop_disjoint_arraycopy            = CAST_FROM_FN_PTR(address, StubRoutines::oop_copy);
 89 address StubRoutines::_oop_disjoint_arraycopy_uninit     = CAST_FROM_FN_PTR(address, StubRoutines::oop_copy_uninit);
 90 
 91 address StubRoutines::_arrayof_jbyte_arraycopy  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jbyte_copy);
 92 address StubRoutines::_arrayof_jshort_arraycopy = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jshort_copy);
 93 address StubRoutines::_arrayof_jint_arraycopy   = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jint_copy);
 94 address StubRoutines::_arrayof_jlong_arraycopy  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jlong_copy);
 95 address StubRoutines::_arrayof_oop_arraycopy    = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_oop_copy);
 96 address StubRoutines::_arrayof_oop_arraycopy_uninit      = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_oop_copy_uninit);
 97 address StubRoutines::_arrayof_jbyte_disjoint_arraycopy  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jbyte_copy);
 98 address StubRoutines::_arrayof_jshort_disjoint_arraycopy = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jshort_copy);
 99 address StubRoutines::_arrayof_jint_disjoint_arraycopy   = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jint_copy);
100 address StubRoutines::_arrayof_jlong_disjoint_arraycopy  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_jlong_copy);
101 address StubRoutines::_arrayof_oop_disjoint_arraycopy    = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_oop_copy);
102 address StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit  = CAST_FROM_FN_PTR(address, StubRoutines::arrayof_oop_copy_uninit);
103 
104 address StubRoutines::_data_cache_writeback              = nullptr;
105 address StubRoutines::_data_cache_writeback_sync         = nullptr;
106 
107 address StubRoutines::_checkcast_arraycopy               = nullptr;
108 address StubRoutines::_checkcast_arraycopy_uninit        = nullptr;
109 address StubRoutines::_unsafe_arraycopy                  = nullptr;
110 address StubRoutines::_generic_arraycopy                 = nullptr;
111 
112 address StubRoutines::_jbyte_fill;
113 address StubRoutines::_jshort_fill;
114 address StubRoutines::_jint_fill;
115 address StubRoutines::_arrayof_jbyte_fill;
116 address StubRoutines::_arrayof_jshort_fill;
117 address StubRoutines::_arrayof_jint_fill;
118 
119 address StubRoutines::_aescrypt_encryptBlock               = nullptr;
120 address StubRoutines::_aescrypt_decryptBlock               = nullptr;
121 address StubRoutines::_cipherBlockChaining_encryptAESCrypt = nullptr;
122 address StubRoutines::_cipherBlockChaining_decryptAESCrypt = nullptr;
123 address StubRoutines::_electronicCodeBook_encryptAESCrypt  = nullptr;
124 address StubRoutines::_electronicCodeBook_decryptAESCrypt  = nullptr;
125 address StubRoutines::_counterMode_AESCrypt                = nullptr;
126 address StubRoutines::_galoisCounterMode_AESCrypt          = nullptr;
127 address StubRoutines::_ghash_processBlocks                 = nullptr;
128 address StubRoutines::_chacha20Block                       = nullptr;
129 address StubRoutines::_base64_encodeBlock                  = nullptr;
130 address StubRoutines::_base64_decodeBlock                  = nullptr;
131 address StubRoutines::_poly1305_processBlocks              = nullptr;
132 
133 address StubRoutines::_md5_implCompress      = nullptr;
134 address StubRoutines::_md5_implCompressMB    = nullptr;
135 address StubRoutines::_sha1_implCompress     = nullptr;
136 address StubRoutines::_sha1_implCompressMB   = nullptr;
137 address StubRoutines::_sha256_implCompress   = nullptr;
138 address StubRoutines::_sha256_implCompressMB = nullptr;
139 address StubRoutines::_sha512_implCompress   = nullptr;
140 address StubRoutines::_sha512_implCompressMB = nullptr;
141 address StubRoutines::_sha3_implCompress     = nullptr;
142 address StubRoutines::_sha3_implCompressMB   = nullptr;
143 
144 address StubRoutines::_updateBytesCRC32 = nullptr;
145 address StubRoutines::_crc_table_adr =    nullptr;
146 
147 address StubRoutines::_crc32c_table_addr = nullptr;
148 address StubRoutines::_updateBytesCRC32C = nullptr;
149 address StubRoutines::_updateBytesAdler32 = nullptr;
150 
151 address StubRoutines::_multiplyToLen = nullptr;
152 address StubRoutines::_squareToLen = nullptr;
153 address StubRoutines::_mulAdd = nullptr;
154 address StubRoutines::_montgomeryMultiply = nullptr;
155 address StubRoutines::_montgomerySquare = nullptr;
156 address StubRoutines::_bigIntegerRightShiftWorker = nullptr;
157 address StubRoutines::_bigIntegerLeftShiftWorker = nullptr;
158 
159 address StubRoutines::_vectorizedMismatch = nullptr;
160 
161 address StubRoutines::_dexp = nullptr;
162 address StubRoutines::_dlog = nullptr;
163 address StubRoutines::_dlog10 = nullptr;
164 address StubRoutines::_dpow = nullptr;
165 address StubRoutines::_dsin = nullptr;
166 address StubRoutines::_dcos = nullptr;
167 address StubRoutines::_dlibm_sin_cos_huge = nullptr;
168 address StubRoutines::_dlibm_reduce_pi04l = nullptr;
169 address StubRoutines::_dlibm_tan_cot_huge = nullptr;
170 address StubRoutines::_dtan = nullptr;
171 
172 address StubRoutines::_load_inline_type_fields_in_regs = nullptr;
173 address StubRoutines::_store_inline_type_fields_to_buf = nullptr;
174 
175 address StubRoutines::_f2hf = nullptr;
176 address StubRoutines::_hf2f = nullptr;
177 
178 address StubRoutines::_vector_f_math[VectorSupport::NUM_VEC_SIZES][VectorSupport::NUM_SVML_OP] = {{nullptr}, {nullptr}};
179 address StubRoutines::_vector_d_math[VectorSupport::NUM_VEC_SIZES][VectorSupport::NUM_SVML_OP] = {{nullptr}, {nullptr}};
180 
181 address StubRoutines::_cont_thaw          = nullptr;
182 address StubRoutines::_cont_returnBarrier = nullptr;
183 address StubRoutines::_cont_returnBarrierExc = nullptr;
184 
185 JFR_ONLY(RuntimeStub* StubRoutines::_jfr_write_checkpoint_stub = nullptr;)
186 JFR_ONLY(address StubRoutines::_jfr_write_checkpoint = nullptr;)
187 
188 // Initialization
189 //
190 // Note: to break cycle with universe initialization, stubs are generated in two phases.
191 // The first one generates stubs needed during universe init (e.g., _handle_must_compile_first_entry).
192 // The second phase includes all other stubs (which may depend on universe being initialized.)
193 
194 extern void StubGenerator_generate(CodeBuffer* code, StubCodeGenerator::StubsKind kind); // only interface to generators
195 
196 void UnsafeCopyMemory::create_table(int max_size) {
197   UnsafeCopyMemory::_table = new UnsafeCopyMemory[max_size];
198   UnsafeCopyMemory::_table_max_length = max_size;
199 }
200 
201 bool UnsafeCopyMemory::contains_pc(address pc) {
202   for (int i = 0; i < UnsafeCopyMemory::_table_length; i++) {
203     UnsafeCopyMemory* entry = &UnsafeCopyMemory::_table[i];
204     if (pc >= entry->start_pc() && pc < entry->end_pc()) {
205       return true;
206     }
207   }
208   return false;
209 }
210 
211 address UnsafeCopyMemory::page_error_continue_pc(address pc) {
212   for (int i = 0; i < UnsafeCopyMemory::_table_length; i++) {
213     UnsafeCopyMemory* entry = &UnsafeCopyMemory::_table[i];
214     if (pc >= entry->start_pc() && pc < entry->end_pc()) {
215       return entry->error_exit_pc();
216     }
217   }
218   return nullptr;
219 }
220 
221 
222 static BufferBlob* initialize_stubs(StubCodeGenerator::StubsKind kind,
223                                     int code_size, int max_aligned_stubs,
224                                     const char* timer_msg,
225                                     const char* buffer_name,
226                                     const char* assert_msg) {
227   ResourceMark rm;
228   TraceTime timer(timer_msg, TRACETIME_LOG(Info, startuptime));
229   // Add extra space for large CodeEntryAlignment
230   int size = code_size + CodeEntryAlignment * max_aligned_stubs;
231   BufferBlob* stubs_code = BufferBlob::create(buffer_name, size);
232   if (stubs_code == nullptr) {
233     vm_exit_out_of_memory(code_size, OOM_MALLOC_ERROR, "CodeCache: no room for %s", buffer_name);
234   }
235   CodeBuffer buffer(stubs_code);
236   StubGenerator_generate(&buffer, kind);
237   // When new stubs added we need to make sure there is some space left
238   // to catch situation when we should increase size again.
239   assert(code_size == 0 || buffer.insts_remaining() > 200, "increase %s", assert_msg);
240 
241   LogTarget(Info, stubs) lt;
242   if (lt.is_enabled()) {
243     LogStream ls(lt);
244     ls.print_cr("%s\t [" INTPTR_FORMAT ", " INTPTR_FORMAT "] used: %d, free: %d",
245                 buffer_name, p2i(stubs_code->content_begin()), p2i(stubs_code->content_end()),
246                 buffer.total_content_size(), buffer.insts_remaining());
247   }
248   return stubs_code;
249 }
250 
251 void StubRoutines::initialize_initial_stubs() {
252   if (_initial_stubs_code == nullptr) {
253     _initial_stubs_code = initialize_stubs(StubCodeGenerator::Initial_stubs,
254                                            _initial_stubs_code_size, 10,
255                                            "StubRoutines generation initial stubs",
256                                            "StubRoutines (initial stubs)",
257                                            "_initial_stubs_code_size");
258   }
259 }
260 
261 void StubRoutines::initialize_continuation_stubs() {
262   if (_continuation_stubs_code == nullptr) {
263     _continuation_stubs_code = initialize_stubs(StubCodeGenerator::Continuation_stubs,
264                                            _continuation_stubs_code_size, 10,
265                                            "StubRoutines generation continuation stubs",
266                                            "StubRoutines (continuation stubs)",
267                                            "_continuation_stubs_code_size");
268   }
269 }
270 
271 void StubRoutines::initialize_compiler_stubs() {
272   if (_compiler_stubs_code == nullptr) {
273     _compiler_stubs_code = initialize_stubs(StubCodeGenerator::Compiler_stubs,
274                                            _compiler_stubs_code_size, 100,
275                                            "StubRoutines generation compiler stubs",
276                                            "StubRoutines (compiler stubs)",
277                                            "_compiler_stubs_code_size");
278   }
279 }
280 
281 #ifdef ASSERT
282 typedef void (*arraycopy_fn)(address src, address dst, int count);
283 
284 // simple tests of generated arraycopy functions
285 static void test_arraycopy_func(address func, int alignment) {
286   int v = 0xcc;
287   int v2 = 0x11;
288   jlong lbuffer[8];
289   jlong lbuffer2[8];
290   address fbuffer  = (address) lbuffer;
291   address fbuffer2 = (address) lbuffer2;
292   unsigned int i;
293   for (i = 0; i < sizeof(lbuffer); i++) {
294     fbuffer[i] = v; fbuffer2[i] = v2;
295   }
296   // C++ does not guarantee jlong[] array alignment to 8 bytes.
297   // Use middle of array to check that memory before it is not modified.
298   address buffer  = align_up((address)&lbuffer[4], BytesPerLong);
299   address buffer2 = align_up((address)&lbuffer2[4], BytesPerLong);
300   // do an aligned copy
301   ((arraycopy_fn)func)(buffer, buffer2, 0);
302   for (i = 0; i < sizeof(lbuffer); i++) {
303     assert(fbuffer[i] == v && fbuffer2[i] == v2, "shouldn't have copied anything");
304   }
305   // adjust destination alignment
306   ((arraycopy_fn)func)(buffer, buffer2 + alignment, 0);
307   for (i = 0; i < sizeof(lbuffer); i++) {
308     assert(fbuffer[i] == v && fbuffer2[i] == v2, "shouldn't have copied anything");
309   }
310   // adjust source alignment
311   ((arraycopy_fn)func)(buffer + alignment, buffer2, 0);
312   for (i = 0; i < sizeof(lbuffer); i++) {
313     assert(fbuffer[i] == v && fbuffer2[i] == v2, "shouldn't have copied anything");
314   }
315 }
316 #endif // ASSERT
317 
318 void StubRoutines::initialize_final_stubs() {
319   if (_final_stubs_code == nullptr) {
320     _final_stubs_code = initialize_stubs(StubCodeGenerator::Final_stubs,
321                                          _final_stubs_code_size, 10,
322                                          "StubRoutines generation final stubs",
323                                          "StubRoutines (final stubs)",
324                                          "_final_stubs_code_size");
325   }
326 
327 #ifdef ASSERT
328 
329   MACOS_AARCH64_ONLY(os::current_thread_enable_wx(WXExec));
330 
331 #define TEST_ARRAYCOPY(type)                                                    \
332   test_arraycopy_func(          type##_arraycopy(),          sizeof(type));     \
333   test_arraycopy_func(          type##_disjoint_arraycopy(), sizeof(type));     \
334   test_arraycopy_func(arrayof_##type##_arraycopy(),          sizeof(HeapWord)); \
335   test_arraycopy_func(arrayof_##type##_disjoint_arraycopy(), sizeof(HeapWord))
336 
337   // Make sure all the arraycopy stubs properly handle zero count
338   TEST_ARRAYCOPY(jbyte);
339   TEST_ARRAYCOPY(jshort);
340   TEST_ARRAYCOPY(jint);
341   TEST_ARRAYCOPY(jlong);
342 
343 #undef TEST_ARRAYCOPY
344 
345 #define TEST_FILL(type)                                                                      \
346   if (_##type##_fill != nullptr) {                                                              \
347     union {                                                                                  \
348       double d;                                                                              \
349       type body[96];                                                                         \
350     } s;                                                                                     \
351                                                                                              \
352     int v = 32;                                                                              \
353     for (int offset = -2; offset <= 2; offset++) {                                           \
354       for (int i = 0; i < 96; i++) {                                                         \
355         s.body[i] = 1;                                                                       \
356       }                                                                                      \
357       type* start = s.body + 8 + offset;                                                     \
358       for (int aligned = 0; aligned < 2; aligned++) {                                        \
359         if (aligned) {                                                                       \
360           if (((intptr_t)start) % HeapWordSize == 0) {                                       \
361             ((void (*)(type*, int, int))StubRoutines::_arrayof_##type##_fill)(start, v, 80); \
362           } else {                                                                           \
363             continue;                                                                        \
364           }                                                                                  \
365         } else {                                                                             \
366           ((void (*)(type*, int, int))StubRoutines::_##type##_fill)(start, v, 80);           \
367         }                                                                                    \
368         for (int i = 0; i < 96; i++) {                                                       \
369           if (i < (8 + offset) || i >= (88 + offset)) {                                      \
370             assert(s.body[i] == 1, "what?");                                                 \
371           } else {                                                                           \
372             assert(s.body[i] == 32, "what?");                                                \
373           }                                                                                  \
374         }                                                                                    \
375       }                                                                                      \
376     }                                                                                        \
377   }                                                                                          \
378 
379   TEST_FILL(jbyte);
380   TEST_FILL(jshort);
381   TEST_FILL(jint);
382 
383 #undef TEST_FILL
384 
385 #define TEST_COPYRTN(type) \
386   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::conjoint_##type##s_atomic),  sizeof(type)); \
387   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::arrayof_conjoint_##type##s), (int)MAX2(sizeof(HeapWord), sizeof(type)))
388 
389   // Make sure all the copy runtime routines properly handle zero count
390   TEST_COPYRTN(jbyte);
391   TEST_COPYRTN(jshort);
392   TEST_COPYRTN(jint);
393   TEST_COPYRTN(jlong);
394 
395 #undef TEST_COPYRTN
396 
397   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::conjoint_words), sizeof(HeapWord));
398   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::disjoint_words), sizeof(HeapWord));
399   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::disjoint_words_atomic), sizeof(HeapWord));
400   // Aligned to BytesPerLong
401   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::aligned_conjoint_words), sizeof(jlong));
402   test_arraycopy_func(CAST_FROM_FN_PTR(address, Copy::aligned_disjoint_words), sizeof(jlong));
403 
404   MACOS_AARCH64_ONLY(os::current_thread_enable_wx(WXWrite));
405 
406 #endif
407 }
408 
409 
410 void initial_stubs_init()      { StubRoutines::initialize_initial_stubs(); }
411 void continuation_stubs_init() { StubRoutines::initialize_continuation_stubs(); }
412 void final_stubs_init()        { StubRoutines::initialize_final_stubs(); }
413 
414 void compiler_stubs_init(bool in_compiler_thread) {
415   if (in_compiler_thread && DelayCompilerStubsGeneration) {
416     // Temporarily revert state of stubs generation because
417     // it is called after final_stubs_init() finished
418     // during compiler runtime initialization.
419     // It is fine because these stubs are only used by
420     // compiled code and compiler is not running yet.
421     StubCodeDesc::unfreeze();
422     StubRoutines::initialize_compiler_stubs();
423     StubCodeDesc::freeze();
424   } else if (!in_compiler_thread && !DelayCompilerStubsGeneration) {
425     StubRoutines::initialize_compiler_stubs();
426   }
427 }
428 
429 //
430 // Default versions of arraycopy functions
431 //
432 
433 JRT_LEAF(void, StubRoutines::jbyte_copy(jbyte* src, jbyte* dest, size_t count))
434 #ifndef PRODUCT
435   SharedRuntime::_jbyte_array_copy_ctr++;      // Slow-path byte array copy
436 #endif // !PRODUCT
437   Copy::conjoint_jbytes_atomic(src, dest, count);
438 JRT_END
439 
440 JRT_LEAF(void, StubRoutines::jshort_copy(jshort* src, jshort* dest, size_t count))
441 #ifndef PRODUCT
442   SharedRuntime::_jshort_array_copy_ctr++;     // Slow-path short/char array copy
443 #endif // !PRODUCT
444   Copy::conjoint_jshorts_atomic(src, dest, count);
445 JRT_END
446 
447 JRT_LEAF(void, StubRoutines::jint_copy(jint* src, jint* dest, size_t count))
448 #ifndef PRODUCT
449   SharedRuntime::_jint_array_copy_ctr++;       // Slow-path int/float array copy
450 #endif // !PRODUCT
451   Copy::conjoint_jints_atomic(src, dest, count);
452 JRT_END
453 
454 JRT_LEAF(void, StubRoutines::jlong_copy(jlong* src, jlong* dest, size_t count))
455 #ifndef PRODUCT
456   SharedRuntime::_jlong_array_copy_ctr++;      // Slow-path long/double array copy
457 #endif // !PRODUCT
458   Copy::conjoint_jlongs_atomic(src, dest, count);
459 JRT_END
460 
461 JRT_LEAF(void, StubRoutines::oop_copy(oop* src, oop* dest, size_t count))
462 #ifndef PRODUCT
463   SharedRuntime::_oop_array_copy_ctr++;        // Slow-path oop array copy
464 #endif // !PRODUCT
465   assert(count != 0, "count should be non-zero");
466   ArrayAccess<>::oop_arraycopy_raw((HeapWord*)src, (HeapWord*)dest, count);
467 JRT_END
468 
469 JRT_LEAF(void, StubRoutines::oop_copy_uninit(oop* src, oop* dest, size_t count))
470 #ifndef PRODUCT
471   SharedRuntime::_oop_array_copy_ctr++;        // Slow-path oop array copy
472 #endif // !PRODUCT
473   assert(count != 0, "count should be non-zero");
474   ArrayAccess<IS_DEST_UNINITIALIZED>::oop_arraycopy_raw((HeapWord*)src, (HeapWord*)dest, count);
475 JRT_END
476 
477 JRT_LEAF(void, StubRoutines::arrayof_jbyte_copy(HeapWord* src, HeapWord* dest, size_t count))
478 #ifndef PRODUCT
479   SharedRuntime::_jbyte_array_copy_ctr++;      // Slow-path byte array copy
480 #endif // !PRODUCT
481   Copy::arrayof_conjoint_jbytes(src, dest, count);
482 JRT_END
483 
484 JRT_LEAF(void, StubRoutines::arrayof_jshort_copy(HeapWord* src, HeapWord* dest, size_t count))
485 #ifndef PRODUCT
486   SharedRuntime::_jshort_array_copy_ctr++;     // Slow-path short/char array copy
487 #endif // !PRODUCT
488   Copy::arrayof_conjoint_jshorts(src, dest, count);
489 JRT_END
490 
491 JRT_LEAF(void, StubRoutines::arrayof_jint_copy(HeapWord* src, HeapWord* dest, size_t count))
492 #ifndef PRODUCT
493   SharedRuntime::_jint_array_copy_ctr++;       // Slow-path int/float array copy
494 #endif // !PRODUCT
495   Copy::arrayof_conjoint_jints(src, dest, count);
496 JRT_END
497 
498 JRT_LEAF(void, StubRoutines::arrayof_jlong_copy(HeapWord* src, HeapWord* dest, size_t count))
499 #ifndef PRODUCT
500   SharedRuntime::_jlong_array_copy_ctr++;       // Slow-path int/float array copy
501 #endif // !PRODUCT
502   Copy::arrayof_conjoint_jlongs(src, dest, count);
503 JRT_END
504 
505 JRT_LEAF(void, StubRoutines::arrayof_oop_copy(HeapWord* src, HeapWord* dest, size_t count))
506 #ifndef PRODUCT
507   SharedRuntime::_oop_array_copy_ctr++;        // Slow-path oop array copy
508 #endif // !PRODUCT
509   assert(count != 0, "count should be non-zero");
510   ArrayAccess<ARRAYCOPY_ARRAYOF>::oop_arraycopy_raw(src, dest, count);
511 JRT_END
512 
513 JRT_LEAF(void, StubRoutines::arrayof_oop_copy_uninit(HeapWord* src, HeapWord* dest, size_t count))
514 #ifndef PRODUCT
515   SharedRuntime::_oop_array_copy_ctr++;        // Slow-path oop array copy
516 #endif // !PRODUCT
517   assert(count != 0, "count should be non-zero");
518   ArrayAccess<ARRAYCOPY_ARRAYOF | IS_DEST_UNINITIALIZED>::oop_arraycopy_raw(src, dest, count);
519 JRT_END
520 
521 address StubRoutines::select_fill_function(BasicType t, bool aligned, const char* &name) {
522 #define RETURN_STUB(xxx_fill) { \
523   name = #xxx_fill; \
524   return StubRoutines::xxx_fill(); }
525 
526   switch (t) {
527   case T_BYTE:
528   case T_BOOLEAN:
529     if (!aligned) RETURN_STUB(jbyte_fill);
530     RETURN_STUB(arrayof_jbyte_fill);
531   case T_CHAR:
532   case T_SHORT:
533     if (!aligned) RETURN_STUB(jshort_fill);
534     RETURN_STUB(arrayof_jshort_fill);
535   case T_INT:
536   case T_FLOAT:
537     if (!aligned) RETURN_STUB(jint_fill);
538     RETURN_STUB(arrayof_jint_fill);
539   case T_DOUBLE:
540   case T_LONG:
541   case T_ARRAY:
542   case T_PRIMITIVE_OBJECT:
543   case T_OBJECT:
544   case T_NARROWOOP:
545   case T_NARROWKLASS:
546   case T_ADDRESS:
547   case T_VOID:
548     // Currently unsupported
549     return nullptr;
550 
551   default:
552     ShouldNotReachHere();
553     return nullptr;
554   }
555 
556 #undef RETURN_STUB
557 }
558 
559 // constants for computing the copy function
560 enum {
561   COPYFUNC_UNALIGNED = 0,
562   COPYFUNC_ALIGNED = 1,                 // src, dest aligned to HeapWordSize
563   COPYFUNC_CONJOINT = 0,
564   COPYFUNC_DISJOINT = 2                 // src != dest, or transfer can descend
565 };
566 
567 // Note:  The condition "disjoint" applies also for overlapping copies
568 // where an descending copy is permitted (i.e., dest_offset <= src_offset).
569 address
570 StubRoutines::select_arraycopy_function(BasicType t, bool aligned, bool disjoint, const char* &name, bool dest_uninitialized) {
571   int selector =
572     (aligned  ? COPYFUNC_ALIGNED  : COPYFUNC_UNALIGNED) +
573     (disjoint ? COPYFUNC_DISJOINT : COPYFUNC_CONJOINT);
574 
575 #define RETURN_STUB(xxx_arraycopy) { \
576   name = #xxx_arraycopy; \
577   return StubRoutines::xxx_arraycopy(); }
578 
579 #define RETURN_STUB_PARM(xxx_arraycopy, parm) { \
580   name = parm ? #xxx_arraycopy "_uninit": #xxx_arraycopy; \
581   return StubRoutines::xxx_arraycopy(parm); }
582 
583   switch (t) {
584   case T_BYTE:
585   case T_BOOLEAN:
586     switch (selector) {
587     case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB(jbyte_arraycopy);
588     case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED:    RETURN_STUB(arrayof_jbyte_arraycopy);
589     case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB(jbyte_disjoint_arraycopy);
590     case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED:    RETURN_STUB(arrayof_jbyte_disjoint_arraycopy);
591     }
592   case T_CHAR:
593   case T_SHORT:
594     switch (selector) {
595     case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB(jshort_arraycopy);
596     case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED:    RETURN_STUB(arrayof_jshort_arraycopy);
597     case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB(jshort_disjoint_arraycopy);
598     case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED:    RETURN_STUB(arrayof_jshort_disjoint_arraycopy);
599     }
600   case T_INT:
601   case T_FLOAT:
602     switch (selector) {
603     case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB(jint_arraycopy);
604     case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED:    RETURN_STUB(arrayof_jint_arraycopy);
605     case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB(jint_disjoint_arraycopy);
606     case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED:    RETURN_STUB(arrayof_jint_disjoint_arraycopy);
607     }
608   case T_DOUBLE:
609   case T_LONG:
610     switch (selector) {
611     case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB(jlong_arraycopy);
612     case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED:    RETURN_STUB(arrayof_jlong_arraycopy);
613     case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB(jlong_disjoint_arraycopy);
614     case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED:    RETURN_STUB(arrayof_jlong_disjoint_arraycopy);
615     }
616   case T_ARRAY:
617   case T_OBJECT:
618     switch (selector) {
619     case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB_PARM(oop_arraycopy, dest_uninitialized);
620     case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED:    RETURN_STUB_PARM(arrayof_oop_arraycopy, dest_uninitialized);
621     case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED:  RETURN_STUB_PARM(oop_disjoint_arraycopy, dest_uninitialized);
622     case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED:    RETURN_STUB_PARM(arrayof_oop_disjoint_arraycopy, dest_uninitialized);
623     }
624   default:
625     ShouldNotReachHere();
626     return nullptr;
627   }
628 
629 #undef RETURN_STUB
630 #undef RETURN_STUB_PARM
631 }
632 
633 UnsafeCopyMemoryMark::UnsafeCopyMemoryMark(StubCodeGenerator* cgen, bool add_entry, bool continue_at_scope_end, address error_exit_pc) {
634   _cgen = cgen;
635   _ucm_entry = nullptr;
636   if (add_entry) {
637     address err_exit_pc = nullptr;
638     if (!continue_at_scope_end) {
639       err_exit_pc = error_exit_pc != nullptr ? error_exit_pc : UnsafeCopyMemory::common_exit_stub_pc();
640     }
641     assert(err_exit_pc != nullptr || continue_at_scope_end, "error exit not set");
642     _ucm_entry = UnsafeCopyMemory::add_to_table(_cgen->assembler()->pc(), nullptr, err_exit_pc);
643   }
644 }
645 
646 UnsafeCopyMemoryMark::~UnsafeCopyMemoryMark() {
647   if (_ucm_entry != nullptr) {
648     _ucm_entry->set_end_pc(_cgen->assembler()->pc());
649     if (_ucm_entry->error_exit_pc() == nullptr) {
650       _ucm_entry->set_error_exit_pc(_cgen->assembler()->pc());
651     }
652   }
653 }