1 /*
2 * Copyright (c) 1997, 2025, 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 "asm/codeBuffer.hpp"
26 #include "asm/macroAssembler.inline.hpp"
27 #include "memory/resourceArea.hpp"
28 #include "oops/access.inline.hpp"
29 #include "oops/klass.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/sharedRuntime.hpp"
35 #include "runtime/stubRoutines.hpp"
36 #include "runtime/timerTrace.hpp"
37 #include "utilities/align.hpp"
38 #include "utilities/copy.hpp"
39 #ifdef COMPILER2
40 #include "opto/runtime.hpp"
41 #endif
42
43 UnsafeMemoryAccess* UnsafeMemoryAccess::_table = nullptr;
44 int UnsafeMemoryAccess::_table_length = 0;
45 int UnsafeMemoryAccess::_table_max_length = 0;
46 address UnsafeMemoryAccess::_common_exit_stub_pc = nullptr;
47
48 // Implementation of StubRoutines - for a description of how to
49 // declare new blobs, stubs and entries , see stubDefinitions.hpp.
50
51 // Define fields used to store blobs
52
53 #define DEFINE_STUBGEN_BLOB_FIELD(blob_name) \
54 BufferBlob* StubRoutines:: STUBGEN_BLOB_FIELD_NAME(blob_name) = nullptr;
55
56 STUBGEN_BLOBS_DO(DEFINE_STUBGEN_BLOB_FIELD)
57
58 #undef DEFINE_STUBGEN_BLOB_FIELD
59
60 // Define fields used to store stubgen stub entries
61
62 #define DEFINE_STUBGEN_ENTRY_FIELD(blob_name, stub_name, field_name, getter_name) \
63 address StubRoutines:: STUB_FIELD_NAME(field_name) = nullptr;
64
65 #define DEFINE_STUBGEN_ENTRY_FIELD_INIT(blob_name, stub_name, field_name, getter_name, init_function) \
66 address StubRoutines:: STUB_FIELD_NAME(field_name) = CAST_FROM_FN_PTR(address, init_function);
67
68 #define DEFINE_STUBGEN_ENTRY_FIELD_ARRAY(blob_name, stub_name, field_name, getter_name, count) \
69 address StubRoutines:: STUB_FIELD_NAME(field_name)[count] = { nullptr };
70
71 STUBGEN_ENTRIES_DO(DEFINE_STUBGEN_ENTRY_FIELD, DEFINE_STUBGEN_ENTRY_FIELD_INIT, DEFINE_STUBGEN_ENTRY_FIELD_ARRAY)
72
73 #undef DEFINE_STUBGEN_ENTRY_FIELD_ARRAY
74 #undef DEFINE_STUBGEN_ENTRY_FIELD_INIT
75 #undef DEFINE_STUBGEN_ENTRY_FIELD
76
77 jint StubRoutines::_verify_oop_count = 0;
78
79
80 address StubRoutines::_string_indexof_array[4] = { nullptr };
81
82 const char* StubRoutines::get_blob_name(BlobId id) {
83 assert(StubInfo::is_stubgen(id), "not a stubgen blob %s", StubInfo::name(id));
84 return StubInfo::name(id);
85 }
86
87 const char* StubRoutines::get_stub_name(StubId id) {
88 assert(StubInfo::is_stubgen(id), "not a stubgen stub %s", StubInfo::name(id));
89 return StubInfo::name(id);
90 }
91
92 #ifdef ASSERT
93 // translate a stub id to an associated blob id while checking that it
94 // is a stubgen stub
95
96 BlobId StubRoutines::stub_to_blob(StubId id) {
97 assert(StubInfo::is_stubgen(id), "not a stubgen stub %s", StubInfo::name(id));
98 return StubInfo::blob(id);
99 }
100
101 #endif // ASSERT
102
103 // Initialization
104
105 extern void StubGenerator_generate(CodeBuffer* code, BlobId blob_id); // only interface to generators
106
107 void UnsafeMemoryAccess::create_table(int max_size) {
108 UnsafeMemoryAccess::_table = new UnsafeMemoryAccess[max_size];
109 UnsafeMemoryAccess::_table_max_length = max_size;
110 }
111
112 bool UnsafeMemoryAccess::contains_pc(address pc) {
113 assert(UnsafeMemoryAccess::_table != nullptr, "");
114 for (int i = 0; i < UnsafeMemoryAccess::_table_length; i++) {
115 UnsafeMemoryAccess* entry = &UnsafeMemoryAccess::_table[i];
116 if (pc >= entry->start_pc() && pc < entry->end_pc()) {
117 return true;
118 }
119 }
120 return false;
121 }
122
123 address UnsafeMemoryAccess::page_error_continue_pc(address pc) {
124 assert(UnsafeMemoryAccess::_table != nullptr, "");
125 for (int i = 0; i < UnsafeMemoryAccess::_table_length; i++) {
126 UnsafeMemoryAccess* entry = &UnsafeMemoryAccess::_table[i];
127 if (pc >= entry->start_pc() && pc < entry->end_pc()) {
128 return entry->error_exit_pc();
129 }
130 }
131 return nullptr;
132 }
133
134 // Used to retrieve mark regions that lie within a generated stub so
135 // they can be saved along with the stub and used to reinit the table
136 // when the stub is reloaded.
137
138 void UnsafeMemoryAccess::collect_entries(address range_start, address range_end, GrowableArray<address>& entries)
139 {
140 for (int i = 0; i < _table_length; i++) {
141 UnsafeMemoryAccess& e = _table[i];
142 assert((e._start_pc != nullptr &&
143 e._end_pc != nullptr &&
144 e._error_exit_pc != nullptr),
145 "search for entries found incomplete table entry");
146 if (e._start_pc >= range_start && e._end_pc <= range_end) {
147 assert(((e._error_exit_pc >= range_start &&
148 e._error_exit_pc <= range_end) ||
149 e._error_exit_pc == _common_exit_stub_pc),
150 "unexpected error exit pc");
151 entries.append(e._start_pc);
152 entries.append(e._end_pc);
153 // only return an exit pc when it is within the range of the stub
154 if (e._error_exit_pc != _common_exit_stub_pc) {
155 entries.append(e._error_exit_pc);
156 } else {
157 // an address outside the stub must be the common exit stub address
158 entries.append(nullptr);
159 }
160 }
161 }
162 }
163
164 static BufferBlob* initialize_stubs(BlobId blob_id,
165 int code_size, int max_aligned_stubs,
166 const char* timer_msg,
167 const char* buffer_name,
168 const char* assert_msg) {
169 assert(StubInfo::is_stubgen(blob_id), "not a stubgen blob %s", StubInfo::name(blob_id));
170 ResourceMark rm;
171 TraceTime timer(timer_msg, TRACETIME_LOG(Info, startuptime));
172 // Add extra space for large CodeEntryAlignment
173 int size = code_size + CodeEntryAlignment * max_aligned_stubs;
174 BufferBlob* stubs_code = BufferBlob::create(buffer_name, size);
175 if (stubs_code == nullptr) {
176 // The compiler blob may be created late by a C2 compiler thread
177 // rather than during normal initialization by the initial thread.
178 // In that case we can tolerate an allocation failure because the
179 // compiler will have been shut down and we have no need of the
180 // blob.
181 if (Thread::current()->is_Compiler_thread()) {
182 assert(blob_id == BlobId::stubgen_compiler_id, "sanity");
183 assert(DelayCompilerStubsGeneration, "sanity");
184 log_warning(stubs)("%s\t not generated:\t no space left in CodeCache", buffer_name);
185 return nullptr;
186 }
187 vm_exit_out_of_memory(code_size, OOM_MALLOC_ERROR, "CodeCache: no room for %s", buffer_name);
188 }
189 CodeBuffer buffer(stubs_code);
190 StubGenerator_generate(&buffer, blob_id);
191 if (code_size == 0) {
192 assert(buffer.insts_size() == 0, "should not write into buffer when bob size declared as 0");
193 LogTarget(Info, stubs) lt;
194 if (lt.is_enabled()) {
195 LogStream ls(lt);
196 ls.print_cr("%s\t not generated", buffer_name);
197 }
198 return nullptr;
199 }
200 // When new stubs added we need to make sure there is some space left
201 // to catch situation when we should increase size again.
202 assert(buffer.insts_remaining() > 200,
203 "increase %s, code_size: %d, used: %d, free: %d",
204 assert_msg, code_size, buffer.total_content_size(), buffer.insts_remaining());
205
206 LogTarget(Info, stubs) lt;
207 if (lt.is_enabled()) {
208 LogStream ls(lt);
209 ls.print_cr("%s\t [" INTPTR_FORMAT ", " INTPTR_FORMAT "] used: %d, free: %d",
210 buffer_name, p2i(stubs_code->content_begin()), p2i(stubs_code->content_end()),
211 buffer.total_content_size(), buffer.insts_remaining());
212 }
213
214 return stubs_code;
215 }
216
217 #define DEFINE_BLOB_INIT_METHOD(blob_name) \
218 void StubRoutines::initialize_ ## blob_name ## _stubs() { \
219 if (STUBGEN_BLOB_FIELD_NAME(blob_name) == nullptr) { \
220 BlobId blob_id = BlobId:: JOIN3(stubgen, blob_name, id); \
221 int size = _ ## blob_name ## _code_size; \
222 int max_aligned_size = 10; \
223 const char* timer_msg = "StubRoutines generation " # blob_name " stubs"; \
224 const char* name = "StubRoutines (" # blob_name " stubs)"; \
225 const char* assert_msg = "_" # blob_name "_code_size"; \
226 STUBGEN_BLOB_FIELD_NAME(blob_name) = \
227 initialize_stubs(blob_id, size, max_aligned_size, timer_msg, \
228 name, assert_msg); \
229 } \
230 }
231
232
233 STUBGEN_BLOBS_DO(DEFINE_BLOB_INIT_METHOD)
234
235 #undef DEFINE_BLOB_INIT_METHOD
236
237
238 #define DEFINE_BLOB_INIT_FUNCTION(blob_name) \
239 void blob_name ## _stubs_init() { \
240 StubRoutines::initialize_ ## blob_name ## _stubs(); \
241 }
242
243 STUBGEN_BLOBS_DO(DEFINE_BLOB_INIT_FUNCTION)
244
245 #undef DEFINE_BLOB_INIT_FUNCTION
246
247 /*
248 * we generate the underlying driver method but this wrapper is needed
249 * to perform special handling depending on where the compiler init
250 * gets called from. it ought to be possible to remove this at some
251 * point and have a determinate ordered init.
252 */
253
254 void compiler_stubs_init(bool in_compiler_thread) {
255 if (in_compiler_thread && DelayCompilerStubsGeneration) {
256 // Temporarily revert state of stubs generation because
257 // it is called after final_stubs_init() finished
258 // during compiler runtime initialization.
259 // It is fine because these stubs are only used by
260 // compiled code and compiler is not running yet.
261 StubCodeDesc::unfreeze();
262 StubRoutines::initialize_compiler_stubs();
263 StubCodeDesc::freeze();
264 } else if (!in_compiler_thread && !DelayCompilerStubsGeneration) {
265 StubRoutines::initialize_compiler_stubs();
266 }
267 }
268
269 //
270 // Default versions of arraycopy functions
271 //
272
273 JRT_LEAF(void, StubRoutines::jbyte_copy(jbyte* src, jbyte* dest, size_t count))
274 #ifndef PRODUCT
275 SharedRuntime::_jbyte_array_copy_ctr++; // Slow-path byte array copy
276 #endif // !PRODUCT
277 Copy::conjoint_jbytes_atomic(src, dest, count);
278 JRT_END
279
280 JRT_LEAF(void, StubRoutines::jshort_copy(jshort* src, jshort* dest, size_t count))
281 #ifndef PRODUCT
282 SharedRuntime::_jshort_array_copy_ctr++; // Slow-path short/char array copy
283 #endif // !PRODUCT
284 Copy::conjoint_jshorts_atomic(src, dest, count);
285 JRT_END
286
287 JRT_LEAF(void, StubRoutines::jint_copy(jint* src, jint* dest, size_t count))
288 #ifndef PRODUCT
289 SharedRuntime::_jint_array_copy_ctr++; // Slow-path int/float array copy
290 #endif // !PRODUCT
291 Copy::conjoint_jints_atomic(src, dest, count);
292 JRT_END
293
294 JRT_LEAF(void, StubRoutines::jlong_copy(jlong* src, jlong* dest, size_t count))
295 #ifndef PRODUCT
296 SharedRuntime::_jlong_array_copy_ctr++; // Slow-path long/double array copy
297 #endif // !PRODUCT
298 Copy::conjoint_jlongs_atomic(src, dest, count);
299 JRT_END
300
301 JRT_LEAF(void, StubRoutines::oop_copy(oop* src, oop* dest, size_t count))
302 #ifndef PRODUCT
303 SharedRuntime::_oop_array_copy_ctr++; // Slow-path oop array copy
304 #endif // !PRODUCT
305 assert(count != 0, "count should be non-zero");
306 ArrayAccess<>::oop_arraycopy_raw((HeapWord*)src, (HeapWord*)dest, count);
307 JRT_END
308
309 JRT_LEAF(void, StubRoutines::oop_copy_uninit(oop* src, oop* dest, size_t count))
310 #ifndef PRODUCT
311 SharedRuntime::_oop_array_copy_ctr++; // Slow-path oop array copy
312 #endif // !PRODUCT
313 assert(count != 0, "count should be non-zero");
314 ArrayAccess<IS_DEST_UNINITIALIZED>::oop_arraycopy_raw((HeapWord*)src, (HeapWord*)dest, count);
315 JRT_END
316
317 JRT_LEAF(void, StubRoutines::arrayof_jbyte_copy(HeapWord* src, HeapWord* dest, size_t count))
318 #ifndef PRODUCT
319 SharedRuntime::_jbyte_array_copy_ctr++; // Slow-path byte array copy
320 #endif // !PRODUCT
321 Copy::arrayof_conjoint_jbytes(src, dest, count);
322 JRT_END
323
324 JRT_LEAF(void, StubRoutines::arrayof_jshort_copy(HeapWord* src, HeapWord* dest, size_t count))
325 #ifndef PRODUCT
326 SharedRuntime::_jshort_array_copy_ctr++; // Slow-path short/char array copy
327 #endif // !PRODUCT
328 Copy::arrayof_conjoint_jshorts(src, dest, count);
329 JRT_END
330
331 JRT_LEAF(void, StubRoutines::arrayof_jint_copy(HeapWord* src, HeapWord* dest, size_t count))
332 #ifndef PRODUCT
333 SharedRuntime::_jint_array_copy_ctr++; // Slow-path int/float array copy
334 #endif // !PRODUCT
335 Copy::arrayof_conjoint_jints(src, dest, count);
336 JRT_END
337
338 JRT_LEAF(void, StubRoutines::arrayof_jlong_copy(HeapWord* src, HeapWord* dest, size_t count))
339 #ifndef PRODUCT
340 SharedRuntime::_jlong_array_copy_ctr++; // Slow-path int/float array copy
341 #endif // !PRODUCT
342 Copy::arrayof_conjoint_jlongs(src, dest, count);
343 JRT_END
344
345 JRT_LEAF(void, StubRoutines::arrayof_oop_copy(HeapWord* src, HeapWord* dest, size_t count))
346 #ifndef PRODUCT
347 SharedRuntime::_oop_array_copy_ctr++; // Slow-path oop array copy
348 #endif // !PRODUCT
349 assert(count != 0, "count should be non-zero");
350 ArrayAccess<ARRAYCOPY_ARRAYOF>::oop_arraycopy_raw(src, dest, count);
351 JRT_END
352
353 JRT_LEAF(void, StubRoutines::arrayof_oop_copy_uninit(HeapWord* src, HeapWord* dest, size_t count))
354 #ifndef PRODUCT
355 SharedRuntime::_oop_array_copy_ctr++; // Slow-path oop array copy
356 #endif // !PRODUCT
357 assert(count != 0, "count should be non-zero");
358 ArrayAccess<ARRAYCOPY_ARRAYOF | IS_DEST_UNINITIALIZED>::oop_arraycopy_raw(src, dest, count);
359 JRT_END
360
361 address StubRoutines::select_fill_function(BasicType t, bool aligned, const char* &name) {
362 #define RETURN_STUB(xxx_fill) { \
363 name = #xxx_fill; \
364 return StubRoutines::xxx_fill(); }
365
366 switch (t) {
367 case T_BYTE:
368 case T_BOOLEAN:
369 if (!aligned) RETURN_STUB(jbyte_fill);
370 RETURN_STUB(arrayof_jbyte_fill);
371 case T_CHAR:
372 case T_SHORT:
373 if (!aligned) RETURN_STUB(jshort_fill);
374 RETURN_STUB(arrayof_jshort_fill);
375 case T_INT:
376 case T_FLOAT:
377 if (!aligned) RETURN_STUB(jint_fill);
378 RETURN_STUB(arrayof_jint_fill);
379 case T_DOUBLE:
380 case T_LONG:
381 case T_ARRAY:
382 case T_OBJECT:
383 case T_NARROWOOP:
384 case T_NARROWKLASS:
385 case T_ADDRESS:
386 case T_VOID:
387 // Currently unsupported
388 return nullptr;
389
390 default:
391 ShouldNotReachHere();
392 return nullptr;
393 }
394
395 #undef RETURN_STUB
396 }
397
398 // constants for computing the copy function
399 enum {
400 COPYFUNC_UNALIGNED = 0,
401 COPYFUNC_ALIGNED = 1, // src, dest aligned to HeapWordSize
402 COPYFUNC_CONJOINT = 0,
403 COPYFUNC_DISJOINT = 2 // src != dest, or transfer can descend
404 };
405
406 // Note: The condition "disjoint" applies also for overlapping copies
407 // where an descending copy is permitted (i.e., dest_offset <= src_offset).
408 address
409 StubRoutines::select_arraycopy_function(BasicType t, bool aligned, bool disjoint, const char* &name, bool dest_uninitialized) {
410 int selector =
411 (aligned ? COPYFUNC_ALIGNED : COPYFUNC_UNALIGNED) +
412 (disjoint ? COPYFUNC_DISJOINT : COPYFUNC_CONJOINT);
413
414 #define RETURN_STUB(xxx_arraycopy) { \
415 name = #xxx_arraycopy; \
416 return StubRoutines::xxx_arraycopy(); }
417
418 #define RETURN_STUB_PARM(xxx_arraycopy, parm) { \
419 name = parm ? #xxx_arraycopy "_uninit": #xxx_arraycopy; \
420 return StubRoutines::xxx_arraycopy(parm); }
421
422 switch (t) {
423 case T_BYTE:
424 case T_BOOLEAN:
425 switch (selector) {
426 case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED: RETURN_STUB(jbyte_arraycopy);
427 case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED: RETURN_STUB(arrayof_jbyte_arraycopy);
428 case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED: RETURN_STUB(jbyte_disjoint_arraycopy);
429 case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED: RETURN_STUB(arrayof_jbyte_disjoint_arraycopy);
430 }
431 case T_CHAR:
432 case T_SHORT:
433 switch (selector) {
434 case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED: RETURN_STUB(jshort_arraycopy);
435 case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED: RETURN_STUB(arrayof_jshort_arraycopy);
436 case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED: RETURN_STUB(jshort_disjoint_arraycopy);
437 case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED: RETURN_STUB(arrayof_jshort_disjoint_arraycopy);
438 }
439 case T_INT:
440 case T_FLOAT:
441 switch (selector) {
442 case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED: RETURN_STUB(jint_arraycopy);
443 case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED: RETURN_STUB(arrayof_jint_arraycopy);
444 case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED: RETURN_STUB(jint_disjoint_arraycopy);
445 case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED: RETURN_STUB(arrayof_jint_disjoint_arraycopy);
446 }
447 case T_DOUBLE:
448 case T_LONG:
449 switch (selector) {
450 case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED: RETURN_STUB(jlong_arraycopy);
451 case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED: RETURN_STUB(arrayof_jlong_arraycopy);
452 case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED: RETURN_STUB(jlong_disjoint_arraycopy);
453 case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED: RETURN_STUB(arrayof_jlong_disjoint_arraycopy);
454 }
455 case T_ARRAY:
456 case T_OBJECT:
457 switch (selector) {
458 case COPYFUNC_CONJOINT | COPYFUNC_UNALIGNED: RETURN_STUB_PARM(oop_arraycopy, dest_uninitialized);
459 case COPYFUNC_CONJOINT | COPYFUNC_ALIGNED: RETURN_STUB_PARM(arrayof_oop_arraycopy, dest_uninitialized);
460 case COPYFUNC_DISJOINT | COPYFUNC_UNALIGNED: RETURN_STUB_PARM(oop_disjoint_arraycopy, dest_uninitialized);
461 case COPYFUNC_DISJOINT | COPYFUNC_ALIGNED: RETURN_STUB_PARM(arrayof_oop_disjoint_arraycopy, dest_uninitialized);
462 }
463 default:
464 ShouldNotReachHere();
465 return nullptr;
466 }
467
468 #undef RETURN_STUB
469 #undef RETURN_STUB_PARM
470 }
471
472 UnsafeMemoryAccessMark::UnsafeMemoryAccessMark(StubCodeGenerator* cgen, bool add_entry, bool continue_at_scope_end, address error_exit_pc) {
473 _cgen = cgen;
474 _ucm_entry = nullptr;
475 if (add_entry) {
476 address err_exit_pc = nullptr;
477 if (!continue_at_scope_end) {
478 err_exit_pc = error_exit_pc != nullptr ? error_exit_pc : UnsafeMemoryAccess::common_exit_stub_pc();
479 }
480 assert(err_exit_pc != nullptr || continue_at_scope_end, "error exit not set");
481 _ucm_entry = UnsafeMemoryAccess::add_to_table(_cgen->assembler()->pc(), nullptr, err_exit_pc);
482 }
483 }
484
485 UnsafeMemoryAccessMark::~UnsafeMemoryAccessMark() {
486 if (_ucm_entry != nullptr) {
487 _ucm_entry->set_end_pc(_cgen->assembler()->pc());
488 if (_ucm_entry->error_exit_pc() == nullptr) {
489 _ucm_entry->set_error_exit_pc(_cgen->assembler()->pc());
490 }
491 }
492 }