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