1 /* 2 * Copyright (c) 2021, 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 #ifndef SHARE_OOPS_STACKCHUNKOOP_INLINE_HPP 26 #define SHARE_OOPS_STACKCHUNKOOP_INLINE_HPP 27 28 #include "oops/stackChunkOop.hpp" 29 30 #include "gc/shared/collectedHeap.hpp" 31 #include "gc/shared/barrierSet.hpp" 32 #include "gc/shared/barrierSetStackChunk.hpp" 33 #include "gc/shared/gc_globals.hpp" 34 #include "memory/memRegion.hpp" 35 #include "memory/universe.hpp" 36 #include "oops/access.inline.hpp" 37 #include "oops/instanceStackChunkKlass.inline.hpp" 38 #include "runtime/continuationJavaClasses.inline.hpp" 39 #include "runtime/frame.inline.hpp" 40 #include "runtime/globals.hpp" 41 #include "runtime/handles.inline.hpp" 42 #include "runtime/registerMap.hpp" 43 #include "runtime/smallRegisterMap.inline.hpp" 44 #include "utilities/macros.hpp" 45 #include CPU_HEADER_INLINE(stackChunkOop) 46 47 DEF_HANDLE_CONSTR(stackChunk, is_stackChunk_noinline) 48 49 inline stackChunkOop stackChunkOopDesc::cast(oop obj) { 50 assert(obj == nullptr || obj->is_stackChunk(), "Wrong type"); 51 return stackChunkOop(obj); 52 } 53 54 inline stackChunkOop stackChunkOopDesc::parent() const { return stackChunkOopDesc::cast(jdk_internal_vm_StackChunk::parent(as_oop())); } 55 inline void stackChunkOopDesc::set_parent(stackChunkOop value) { jdk_internal_vm_StackChunk::set_parent(this, value); } 56 template<typename P> 57 inline void stackChunkOopDesc::set_parent_raw(oop value) { jdk_internal_vm_StackChunk::set_parent_raw<P>(this, value); } 58 template<DecoratorSet decorators> 59 inline void stackChunkOopDesc::set_parent_access(oop value) { jdk_internal_vm_StackChunk::set_parent_access<decorators>(this, value); } 60 61 inline int stackChunkOopDesc::stack_size() const { return jdk_internal_vm_StackChunk::size(as_oop()); } 62 63 inline int stackChunkOopDesc::sp() const { return jdk_internal_vm_StackChunk::sp(as_oop()); } 64 inline void stackChunkOopDesc::set_sp(int value) { jdk_internal_vm_StackChunk::set_sp(this, value); } 65 66 inline address stackChunkOopDesc::pc() const { return jdk_internal_vm_StackChunk::pc(as_oop()); } 67 inline void stackChunkOopDesc::set_pc(address value) { jdk_internal_vm_StackChunk::set_pc(this, value); } 68 69 inline int stackChunkOopDesc::argsize() const { return jdk_internal_vm_StackChunk::argsize(as_oop()); } 70 inline void stackChunkOopDesc::set_argsize(int value) { jdk_internal_vm_StackChunk::set_argsize(as_oop(), value); } 71 72 inline uint8_t stackChunkOopDesc::flags() const { return jdk_internal_vm_StackChunk::flags(as_oop()); } 73 inline void stackChunkOopDesc::set_flags(uint8_t value) { jdk_internal_vm_StackChunk::set_flags(this, value); } 74 75 inline uint8_t stackChunkOopDesc::flags_acquire() const { return jdk_internal_vm_StackChunk::flags_acquire(as_oop()); } 76 77 inline void stackChunkOopDesc::release_set_flags(uint8_t value) { 78 jdk_internal_vm_StackChunk::release_set_flags(this, value); 79 } 80 81 inline bool stackChunkOopDesc::try_set_flags(uint8_t prev_flags, uint8_t new_flags) { 82 return jdk_internal_vm_StackChunk::try_set_flags(this, prev_flags, new_flags); 83 } 84 85 inline int stackChunkOopDesc::max_thawing_size() const { return jdk_internal_vm_StackChunk::maxThawingSize(as_oop()); } 86 inline void stackChunkOopDesc::set_max_thawing_size(int value) { 87 assert(value >= 0, "size must be >= 0"); 88 jdk_internal_vm_StackChunk::set_maxThawingSize(this, (jint)value); 89 } 90 91 inline uint8_t stackChunkOopDesc::lockStackSize() const { return jdk_internal_vm_StackChunk::lockStackSize(as_oop()); } 92 inline void stackChunkOopDesc::set_lockStackSize(uint8_t value) { jdk_internal_vm_StackChunk::set_lockStackSize(this, value); } 93 94 inline ObjectMonitor* stackChunkOopDesc::objectMonitor() const { return (ObjectMonitor*)jdk_internal_vm_StackChunk::objectMonitor(as_oop()); } 95 inline void stackChunkOopDesc::set_objectMonitor(ObjectMonitor* mon) { jdk_internal_vm_StackChunk::set_objectMonitor(this, (address)mon); } 96 97 inline oop stackChunkOopDesc::cont() const { 98 if (UseZGC && !ZGenerational) { 99 assert(!UseCompressedOops, "Non-generational ZGC does not support compressed oops"); 100 // The state of the cont oop is used by XCollectedHeap::requires_barriers, 101 // to determine the age of the stackChunkOopDesc. For that to work, it is 102 // only the GC that is allowed to perform a load barrier on the oop. 103 // This function is used by non-GC code and therfore create a stack-local 104 // copy on the oop and perform the load barrier on that copy instead. 105 oop obj = jdk_internal_vm_StackChunk::cont_raw<oop>(as_oop()); 106 obj = (oop)NativeAccess<>::oop_load(&obj); 107 return obj; 108 } 109 return jdk_internal_vm_StackChunk::cont(as_oop()); 110 } 111 inline void stackChunkOopDesc::set_cont(oop value) { jdk_internal_vm_StackChunk::set_cont(this, value); } 112 template<typename P> 113 inline void stackChunkOopDesc::set_cont_raw(oop value) { jdk_internal_vm_StackChunk::set_cont_raw<P>(this, value); } 114 template<DecoratorSet decorators> 115 inline void stackChunkOopDesc::set_cont_access(oop value) { jdk_internal_vm_StackChunk::set_cont_access<decorators>(this, value); } 116 117 inline int stackChunkOopDesc::bottom() const { return stack_size() - argsize() - frame::metadata_words_at_top; } 118 119 inline HeapWord* stackChunkOopDesc::start_of_stack() const { 120 return (HeapWord*)(cast_from_oop<intptr_t>(as_oop()) + InstanceStackChunkKlass::offset_of_stack()); 121 } 122 123 inline intptr_t* stackChunkOopDesc::start_address() const { return (intptr_t*)start_of_stack(); } 124 inline intptr_t* stackChunkOopDesc::end_address() const { return start_address() + stack_size(); } 125 inline intptr_t* stackChunkOopDesc::bottom_address() const { return start_address() + bottom(); } 126 inline intptr_t* stackChunkOopDesc::sp_address() const { return start_address() + sp(); } 127 128 inline int stackChunkOopDesc::to_offset(intptr_t* p) const { 129 assert(is_in_chunk(p) 130 || (p >= start_address() && (p - start_address()) <= stack_size() + frame::metadata_words), 131 "p: " PTR_FORMAT " start: " PTR_FORMAT " end: " PTR_FORMAT, p2i(p), p2i(start_address()), p2i(bottom_address())); 132 return (int)(p - start_address()); 133 } 134 135 inline intptr_t* stackChunkOopDesc::from_offset(int offset) const { 136 assert(offset <= stack_size(), ""); 137 return start_address() + offset; 138 } 139 140 inline bool stackChunkOopDesc::is_empty() const { 141 assert(sp() <= stack_size(), ""); 142 assert((sp() == stack_size()) == (sp() >= stack_size() - argsize() - frame::metadata_words_at_top), 143 "sp: %d size: %d argsize: %d", sp(), stack_size(), argsize()); 144 return sp() == stack_size(); 145 } 146 147 inline bool stackChunkOopDesc::is_in_chunk(void* p) const { 148 HeapWord* start = (HeapWord*)start_address(); 149 HeapWord* end = start + stack_size(); 150 return (HeapWord*)p >= start && (HeapWord*)p < end; 151 } 152 153 bool stackChunkOopDesc::is_usable_in_chunk(void* p) const { 154 HeapWord* start = (HeapWord*)start_address() + sp() - frame::metadata_words_at_bottom; 155 HeapWord* end = start + stack_size(); 156 return (HeapWord*)p >= start && (HeapWord*)p < end; 157 } 158 159 inline bool stackChunkOopDesc::is_flag(uint8_t flag) const { 160 return (flags() & flag) != 0; 161 } 162 inline bool stackChunkOopDesc::is_flag_acquire(uint8_t flag) const { 163 return (flags_acquire() & flag) != 0; 164 } 165 inline void stackChunkOopDesc::set_flag(uint8_t flag, bool value) { 166 uint32_t flags = this->flags(); 167 set_flags((uint8_t)(value ? flags |= flag : flags &= ~flag)); 168 } 169 inline void stackChunkOopDesc::clear_flags() { 170 set_flags(0); 171 } 172 173 inline bool stackChunkOopDesc::has_mixed_frames() const { return is_flag(FLAG_HAS_INTERPRETED_FRAMES); } 174 inline void stackChunkOopDesc::set_has_mixed_frames(bool value) { 175 assert((flags() & ~(FLAG_HAS_INTERPRETED_FRAMES | FLAG_PREEMPTED)) == 0, "other flags should not be set"); 176 set_flag(FLAG_HAS_INTERPRETED_FRAMES, value); 177 } 178 179 inline bool stackChunkOopDesc::is_preempted() const { return is_flag(FLAG_PREEMPTED); } 180 inline void stackChunkOopDesc::set_is_preempted(bool value) { set_flag(FLAG_PREEMPTED, value); } 181 inline bool stackChunkOopDesc::preempted_on_monitorenter() const { return objectMonitor() != nullptr; } 182 183 inline bool stackChunkOopDesc::has_lockStack() const { return is_flag(FLAG_HAS_LOCKSTACK); } 184 inline void stackChunkOopDesc::set_has_lockStack(bool value) { set_flag(FLAG_HAS_LOCKSTACK, value); } 185 186 inline bool stackChunkOopDesc::is_gc_mode() const { return is_flag(FLAG_GC_MODE); } 187 inline bool stackChunkOopDesc::is_gc_mode_acquire() const { return is_flag_acquire(FLAG_GC_MODE); } 188 inline void stackChunkOopDesc::set_gc_mode(bool value) { set_flag(FLAG_GC_MODE, value); } 189 190 inline bool stackChunkOopDesc::has_bitmap() const { return is_flag(FLAG_HAS_BITMAP); } 191 inline void stackChunkOopDesc::set_has_bitmap(bool value) { set_flag(FLAG_HAS_BITMAP, value); } 192 193 inline bool stackChunkOopDesc::has_thaw_slowpath_condition() const { return flags() != 0; } 194 195 inline bool stackChunkOopDesc::requires_barriers() { 196 return Universe::heap()->requires_barriers(this); 197 } 198 199 template <stackChunkOopDesc::BarrierType barrier, ChunkFrames frame_kind, typename RegisterMapT> 200 void stackChunkOopDesc::do_barriers(const StackChunkFrameStream<frame_kind>& f, const RegisterMapT* map) { 201 if (frame_kind == ChunkFrames::Mixed) { 202 // we could freeze deopted frames in slow mode. 203 f.handle_deopted(); 204 } 205 do_barriers0<barrier>(f, map); 206 } 207 208 template <typename OopT, class StackChunkLockStackClosureType> 209 inline void stackChunkOopDesc::iterate_lockstack(StackChunkLockStackClosureType* closure) { 210 if (LockingMode != LM_LIGHTWEIGHT) { 211 return; 212 } 213 int cnt = lockStackSize(); 214 intptr_t* lockstart_addr = start_address(); 215 for (int i = 0; i < cnt; i++) { 216 closure->do_oop((OopT*)&lockstart_addr[i]); 217 } 218 } 219 220 template <class StackChunkFrameClosureType> 221 inline void stackChunkOopDesc::iterate_stack(StackChunkFrameClosureType* closure) { 222 has_mixed_frames() ? iterate_stack<ChunkFrames::Mixed>(closure) 223 : iterate_stack<ChunkFrames::CompiledOnly>(closure); 224 } 225 226 template <ChunkFrames frame_kind, class StackChunkFrameClosureType> 227 inline void stackChunkOopDesc::iterate_stack(StackChunkFrameClosureType* closure) { 228 const SmallRegisterMap* map = SmallRegisterMap::instance; 229 assert(!map->in_cont(), ""); 230 231 StackChunkFrameStream<frame_kind> f(this); 232 bool should_continue = true; 233 234 if (f.is_stub()) { 235 RegisterMap full_map(nullptr, 236 RegisterMap::UpdateMap::include, 237 RegisterMap::ProcessFrames::skip, 238 RegisterMap::WalkContinuation::include); 239 full_map.set_include_argument_oops(false); 240 closure->do_frame(f, map); 241 242 f.next(&full_map); 243 if (f.is_done()) return; 244 245 should_continue = closure->do_frame(f, &full_map); 246 f.next(&map); 247 } 248 assert(!f.is_stub(), ""); 249 250 for(; should_continue && !f.is_done(); f.next(map)) { 251 if (frame_kind == ChunkFrames::Mixed) { 252 // in slow mode we might freeze deoptimized frames 253 f.handle_deopted(); 254 } 255 should_continue = closure->do_frame(f, map); 256 } 257 } 258 259 inline frame stackChunkOopDesc::relativize(frame fr) const { relativize_frame(fr); return fr; } 260 inline frame stackChunkOopDesc::derelativize(frame fr) const { derelativize_frame(fr); return fr; } 261 262 inline void* stackChunkOopDesc::gc_data() const { 263 int stack_sz = stack_size(); 264 assert(stack_sz != 0, "stack should not be empty"); 265 266 // The gc data is located after the stack. 267 return start_of_stack() + stack_sz; 268 } 269 270 inline BitMapView stackChunkOopDesc::bitmap() const { 271 HeapWord* bitmap_addr = static_cast<HeapWord*>(gc_data()); 272 int stack_sz = stack_size(); 273 size_t bitmap_size_in_bits = InstanceStackChunkKlass::bitmap_size_in_bits(stack_sz); 274 275 BitMapView bitmap((BitMap::bm_word_t*)bitmap_addr, bitmap_size_in_bits); 276 277 DEBUG_ONLY(bitmap.verify_range(bit_index_for(start_address()), bit_index_for(end_address()));) 278 279 return bitmap; 280 } 281 282 inline BitMap::idx_t stackChunkOopDesc::bit_index_for(address p) const { 283 return UseCompressedOops ? bit_index_for((narrowOop*)p) : bit_index_for((oop*)p); 284 } 285 286 template <typename OopT> 287 inline BitMap::idx_t stackChunkOopDesc::bit_index_for(OopT* p) const { 288 assert(is_aligned(p, alignof(OopT)), "should be aligned: " PTR_FORMAT, p2i(p)); 289 assert(p >= (OopT*)start_address(), "Address not in chunk"); 290 return p - (OopT*)start_address(); 291 } 292 293 inline intptr_t* stackChunkOopDesc::address_for_bit(BitMap::idx_t index) const { 294 return UseCompressedOops ? (intptr_t*)address_for_bit<narrowOop>(index) : (intptr_t*)address_for_bit<oop>(index); 295 } 296 297 template <typename OopT> 298 inline OopT* stackChunkOopDesc::address_for_bit(BitMap::idx_t index) const { 299 return (OopT*)start_address() + index; 300 } 301 302 inline MemRegion stackChunkOopDesc::range() { 303 return MemRegion((HeapWord*)this, size()); 304 } 305 306 inline int stackChunkOopDesc::relativize_usp_offset(const frame& fr, const int usp_offset_in_bytes) const { 307 assert(fr.is_compiled_frame() || fr.cb()->is_runtime_stub(), ""); 308 assert(is_in_chunk(fr.unextended_sp()), ""); 309 310 intptr_t* base = fr.real_fp(); // equal to the caller's sp 311 intptr_t* loc = (intptr_t*)((address)fr.unextended_sp() + usp_offset_in_bytes); 312 assert(base > loc, ""); 313 return (int)(base - loc); 314 } 315 316 inline address stackChunkOopDesc::usp_offset_to_location(const frame& fr, const int usp_offset_in_bytes) const { 317 assert(fr.is_compiled_frame(), ""); 318 return (address)derelativize_address(fr.offset_unextended_sp()) + usp_offset_in_bytes; 319 } 320 321 inline address stackChunkOopDesc::reg_to_location(const frame& fr, const RegisterMap* map, VMReg reg) const { 322 assert(fr.is_compiled_frame(), ""); 323 assert(map != nullptr, ""); 324 assert(map->stack_chunk() == as_oop(), ""); 325 326 // the offsets are saved in the map after going through relativize_usp_offset, so they are sp - loc, in words 327 intptr_t offset = (intptr_t)map->location(reg, nullptr); // see usp_offset_to_index for the chunk case 328 intptr_t* base = derelativize_address(fr.offset_sp()); 329 return (address)(base - offset); 330 } 331 332 inline Method* stackChunkOopDesc::interpreter_frame_method(const frame& fr) { 333 return derelativize(fr).interpreter_frame_method(); 334 } 335 336 inline address stackChunkOopDesc::interpreter_frame_bcp(const frame& fr) { 337 return derelativize(fr).interpreter_frame_bcp(); 338 } 339 340 inline intptr_t* stackChunkOopDesc::interpreter_frame_expression_stack_at(const frame& fr, int index) const { 341 frame heap_frame = derelativize(fr); 342 assert(heap_frame.is_heap_frame(), "must be"); 343 return heap_frame.interpreter_frame_expression_stack_at(index); 344 } 345 346 inline intptr_t* stackChunkOopDesc::interpreter_frame_local_at(const frame& fr, int index) const { 347 frame heap_frame = derelativize(fr); 348 assert(heap_frame.is_heap_frame(), "must be"); 349 return heap_frame.interpreter_frame_local_at(index); 350 } 351 352 inline void stackChunkOopDesc::copy_from_stack_to_chunk(intptr_t* from, intptr_t* to, int size) { 353 log_develop_trace(continuations)("Copying from v: " PTR_FORMAT " - " PTR_FORMAT " (%d words, %d bytes)", 354 p2i(from), p2i(from + size), size, size << LogBytesPerWord); 355 log_develop_trace(continuations)("Copying to h: " PTR_FORMAT "(" INTPTR_FORMAT "," INTPTR_FORMAT ") - " PTR_FORMAT "(" INTPTR_FORMAT "," INTPTR_FORMAT ") (%d words, %d bytes)", 356 p2i(to), to - start_address(), relative_base() - to, p2i(to + size), to + size - start_address(), 357 relative_base() - (to + size), size, size << LogBytesPerWord); 358 359 assert(to >= start_address(), "Chunk underflow"); 360 assert(to + size <= end_address(), "Chunk overflow"); 361 362 #if !(defined(AMD64) || defined(AARCH64) || defined(RISCV64) || defined(PPC64)) || defined(ZERO) 363 // Suppress compilation warning-as-error on unimplemented architectures 364 // that stub out arch-specific methods. Some compilers are smart enough 365 // to figure out the argument is always null and then warn about it. 366 if (to != nullptr) 367 #endif 368 memcpy(to, from, size << LogBytesPerWord); 369 } 370 371 inline void stackChunkOopDesc::copy_from_chunk_to_stack(intptr_t* from, intptr_t* to, int size) { 372 log_develop_trace(continuations)("Copying from h: " PTR_FORMAT "(" INTPTR_FORMAT "," INTPTR_FORMAT ") - " PTR_FORMAT "(" INTPTR_FORMAT "," INTPTR_FORMAT ") (%d words, %d bytes)", 373 p2i(from), from - start_address(), relative_base() - from, p2i(from + size), from + size - start_address(), 374 relative_base() - (from + size), size, size << LogBytesPerWord); 375 log_develop_trace(continuations)("Copying to v: " PTR_FORMAT " - " PTR_FORMAT " (%d words, %d bytes)", p2i(to), 376 p2i(to + size), size, size << LogBytesPerWord); 377 378 assert(from >= start_address(), ""); 379 assert(from + size <= end_address(), ""); 380 381 #if !(defined(AMD64) || defined(AARCH64) || defined(RISCV64) || defined(PPC64)) || defined(ZERO) 382 // Suppress compilation warning-as-error on unimplemented architectures 383 // that stub out arch-specific methods. Some compilers are smart enough 384 // to figure out the argument is always null and then warn about it. 385 if (to != nullptr) 386 #endif 387 memcpy(to, from, size << LogBytesPerWord); 388 } 389 390 template <typename OopT> 391 inline oop stackChunkOopDesc::load_oop(OopT* addr) { 392 return BarrierSet::barrier_set()->barrier_set_stack_chunk()->load_oop(this, addr); 393 } 394 395 inline intptr_t* stackChunkOopDesc::relative_base() const { 396 // we relativize with respect to end rather than start because GC might compact the chunk 397 return end_address() + frame::metadata_words; 398 } 399 400 inline intptr_t* stackChunkOopDesc::derelativize_address(int offset) const { 401 intptr_t* base = relative_base(); 402 intptr_t* p = base - offset; 403 assert(start_address() <= p && p <= base, "start_address: " PTR_FORMAT " p: " PTR_FORMAT " base: " PTR_FORMAT, 404 p2i(start_address()), p2i(p), p2i(base)); 405 return p; 406 } 407 408 inline int stackChunkOopDesc::relativize_address(intptr_t* p) const { 409 intptr_t* base = relative_base(); 410 intptr_t offset = base - p; 411 assert(start_address() <= p && p <= base, "start_address: " PTR_FORMAT " p: " PTR_FORMAT " base: " PTR_FORMAT, 412 p2i(start_address()), p2i(p), p2i(base)); 413 assert(0 <= offset && offset <= std::numeric_limits<int>::max(), "offset: " PTR_FORMAT, offset); 414 return (int)offset; 415 } 416 417 inline void stackChunkOopDesc::relativize_frame(frame& fr) const { 418 fr.set_offset_sp(relativize_address(fr.sp())); 419 fr.set_offset_unextended_sp(relativize_address(fr.unextended_sp())); 420 relativize_frame_pd(fr); 421 } 422 423 inline void stackChunkOopDesc::derelativize_frame(frame& fr) const { 424 fr.set_sp(derelativize_address(fr.offset_sp())); 425 fr.set_unextended_sp(derelativize_address(fr.offset_unextended_sp())); 426 derelativize_frame_pd(fr); 427 fr.set_frame_index(-1); // for the sake of assertions in frame 428 } 429 430 #endif // SHARE_OOPS_STACKCHUNKOOP_INLINE_HPP