1 /*
  2  * Copyright (c) 2021, 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 #ifndef SHARE_OOPS_STACKCHUNKOOP_HPP
 26 #define SHARE_OOPS_STACKCHUNKOOP_HPP
 27 
 28 
 29 #include "oops/instanceOop.hpp"
 30 #include "runtime/handles.hpp"
 31 #include "runtime/stackChunkFrameStream.hpp"
 32 #include "utilities/bitMap.hpp"
 33 #include "utilities/macros.hpp"
 34 
 35 class frame;
 36 class MemRegion;
 37 class RegisterMap;
 38 class VMRegImpl;
 39 class ObjectMonitor;
 40 class ObjectWaiter;
 41 typedef VMRegImpl* VMReg;
 42 
 43 // A continuation stack-chunk oop.
 44 // See InstanceStackChunkKlass for a description of continuation stack-chunks.
 45 //
 46 // size and sp are in machine words
 47 // max_size is the maximum space a thawed chunk would take up on the stack, *not* including top-most frame's metadata
 48 class stackChunkOopDesc : public instanceOopDesc {
 49 public:
 50   enum class BarrierType { Load, Store };
 51 
 52 private:
 53   template <BarrierType barrier> friend class DoBarriersStackClosure;
 54 
 55   // Chunk flags.
 56   // FLAG_HAS_INTERPRETED_FRAMES actually means "thaw slow because of some content-based chunk condition"
 57   // It is set whenever we freeze slow, but generally signifies there might be interpreted/deoptimized/stub frames
 58   static const uint8_t FLAG_HAS_INTERPRETED_FRAMES = 1 << 0;
 59   static const uint8_t FLAG_CLAIM_RELATIVIZE = 1 << 1; // Only one thread claims relativization of derived pointers
 60   static const uint8_t FLAG_NOTIFY_RELATIVIZE = 1 << 2; // Someone is waiting for relativization to complete
 61   static const uint8_t FLAG_GC_MODE = 1 << 3; // Once true it and FLAG_HAS_INTERPRETED_FRAMES can't change
 62   static const uint8_t FLAG_HAS_BITMAP = 1 << 4; // Can only be true if FLAG_GC_MODE is true
 63   static const uint8_t FLAG_HAS_LOCKSTACK = 1 << 5; // LockStack was copied into stackChunk
 64   static const uint8_t FLAG_PREEMPTED = 1 << 6; // Continuation was unmounted from inside VM
 65 
 66   bool try_acquire_relativization();
 67   void release_relativization();
 68 
 69 public:
 70   static inline stackChunkOop cast(oop obj);
 71 
 72   inline stackChunkOop parent() const;
 73   inline void set_parent(stackChunkOop value);
 74   template<typename P>
 75   inline void set_parent_raw(oop value);
 76   template<DecoratorSet decorators>
 77   inline void set_parent_access(oop value);
 78 
 79   inline int stack_size() const;
 80 
 81   inline int sp() const;
 82   inline void set_sp(int value);
 83 
 84   inline address pc() const;
 85   inline void set_pc(address value);
 86 
 87   inline int argsize() const;
 88   inline void set_argsize(int value);
 89 
 90   inline uint8_t flags() const;
 91   inline uint8_t flags_acquire() const;
 92   inline void set_flags(uint8_t value);
 93   inline void release_set_flags(uint8_t value);
 94 
 95   inline int max_thawing_size() const;
 96   inline void set_max_thawing_size(int value);
 97 
 98   inline uint8_t lockstack_size() const;
 99   inline void set_lockstack_size(uint8_t value);
100 
101   inline oop cont() const;
102   template<typename P>
103   inline oop cont() const;
104   inline void set_cont(oop value);
105   template<typename P>
106   inline void set_cont_raw(oop value);
107   template<DecoratorSet decorators>
108   inline void set_cont_access(oop value);
109 
110   inline int bottom() const;
111   inline void set_bottom(int value);
112 
113   inline HeapWord* start_of_stack() const;
114 
115   inline intptr_t* start_address() const;
116   inline intptr_t* end_address() const;
117   inline intptr_t* bottom_address() const; // = end_address - argsize
118   inline intptr_t* sp_address() const;
119 
120 
121   inline int to_offset(intptr_t* p) const;
122   inline intptr_t* from_offset(int offset) const;
123 
124   inline bool is_empty() const;
125   inline bool is_in_chunk(void* p) const;
126   inline bool is_usable_in_chunk(void* p) const;
127 
128   inline bool is_flag(uint8_t flag) const;
129   inline bool is_flag_acquire(uint8_t flag) const;
130   inline void set_flag(uint8_t flag, bool value);
131   inline bool try_set_flags(uint8_t prev_flags, uint8_t new_flags);
132   inline void clear_flags();
133 
134   inline bool has_mixed_frames() const;
135   inline void set_has_mixed_frames(bool value);
136 
137   inline bool preempted() const;
138   inline void set_preempted(bool value);
139 
140   inline bool has_lockstack() const;
141   inline void set_has_lockstack(bool value);
142 
143   inline bool is_gc_mode() const;
144   inline bool is_gc_mode_acquire() const;
145   inline void set_gc_mode(bool value);
146 
147   inline bool has_bitmap() const;
148   inline void set_has_bitmap(bool value);
149 
150   inline bool has_thaw_slowpath_condition() const;
151 
152   inline bool requires_barriers();
153 
154   template <BarrierType>
155   void do_barriers();
156 
157   template <BarrierType, ChunkFrames frames, typename RegisterMapT>
158   inline void do_barriers(const StackChunkFrameStream<frames>& f, const RegisterMapT* map);
159 
160   template <typename RegisterMapT>
161   void fix_thawed_frame(const frame& f, const RegisterMapT* map);
162 
163   void transfer_lockstack(oop* start, bool requires_barriers);
164 
165   template <typename OopT, class StackChunkLockStackClosureType>
166   inline void iterate_lockstack(StackChunkLockStackClosureType* closure);
167 
168   template <class StackChunkFrameClosureType>
169   inline void iterate_stack(StackChunkFrameClosureType* closure);
170 
171   // Derived pointers are relativized, with respect to their oop base.
172   void relativize_derived_pointers_concurrently();
173   void transform();
174 
175   inline void* gc_data() const;
176   inline BitMapView bitmap() const;
177   inline BitMap::idx_t bit_index_for(address p) const;
178   inline intptr_t* address_for_bit(BitMap::idx_t index) const;
179   template <typename OopT> inline BitMap::idx_t bit_index_for(OopT* p) const;
180   template <typename OopT> inline OopT* address_for_bit(BitMap::idx_t index) const;
181 
182   MemRegion range();
183 
184   // Returns a relative frame (with offset_sp, offset_unextended_sp, and offset_fp) that can be held
185   // during safepoints.  This is orthogonal to the relativizing of the actual content of interpreted frames.
186   // To be used, frame objects need to be derelativized with `derelativize`.
187   inline frame relativize(frame fr) const;
188   inline frame derelativize(frame fr) const;
189 
190   frame top_frame(RegisterMap* map);
191   frame sender(const frame& fr, RegisterMap* map);
192 
193   inline int relativize_usp_offset(const frame& fr, const int usp_offset_in_bytes) const;
194   inline address usp_offset_to_location(const frame& fr, const int usp_offset_in_bytes) const;
195   inline address reg_to_location(const frame& fr, const RegisterMap* map, VMReg reg) const;
196 
197   // Access to relativized interpreter frames (both the frame object and frame content are relativized)
198   inline Method* interpreter_frame_method(const frame& fr);
199   inline address interpreter_frame_bcp(const frame& fr);
200   inline intptr_t* interpreter_frame_expression_stack_at(const frame& fr, int index) const;
201   inline intptr_t* interpreter_frame_local_at(const frame& fr, int index) const;
202 
203   int num_java_frames() const;
204 
205   inline void copy_from_stack_to_chunk(intptr_t* from, intptr_t* to, int size);
206   inline void copy_from_chunk_to_stack(intptr_t* from, intptr_t* to, int size);
207 
208   template <typename OopT>
209   inline oop load_oop(OopT* addr);
210 
211   using oopDesc::print_on;
212   void print_on(bool verbose, outputStream* st) const;
213 
214   // Verifies the consistency of the chunk's data
215   bool verify(size_t* out_size = nullptr, int* out_oops = nullptr,
216               int* out_frames = nullptr, int* out_interpreted_frames = nullptr) NOT_DEBUG({ return true; });
217 
218 private:
219   template <BarrierType barrier, ChunkFrames frames = ChunkFrames::Mixed, typename RegisterMapT>
220   void do_barriers0(const StackChunkFrameStream<frames>& f, const RegisterMapT* map);
221 
222   template <ChunkFrames frames, class StackChunkFrameClosureType>
223   inline void iterate_stack(StackChunkFrameClosureType* closure);
224 
225   inline intptr_t* relative_base() const;
226 
227   inline intptr_t* derelativize_address(int offset) const;
228   int relativize_address(intptr_t* p) const;
229 
230   inline void relativize_frame(frame& fr) const;
231   inline void derelativize_frame(frame& fr) const;
232 
233   inline void relativize_frame_pd(frame& fr) const;
234   inline void derelativize_frame_pd(frame& fr) const;
235 };
236 
237 #endif // SHARE_OOPS_STACKCHUNKOOP_HPP