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