1 /*
  2  * Copyright (c) 2021, 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 "utilities/bitMap.hpp"
 32 #include "utilities/macros.hpp"
 33 
 34 class frame;
 35 class MemRegion;
 36 class RegisterMap;
 37 class VMRegImpl;
 38 typedef VMRegImpl* VMReg;
 39 
 40 // A continuation stack-chunk oop.
 41 // See InstanceStackChunkKlass for a description of continuation stack-chunks.
 42 //
 43 // size and sp are in machine words
 44 // max_size is the maximum space a thawed chunk would take up on the stack, *not* including top-most frame's metadata
 45 class stackChunkOopDesc : public instanceOopDesc {
 46 private:
 47   // Chunk flags.
 48   static const uint8_t FLAG_HAS_INTERPRETED_FRAMES = 1;
 49   static const uint8_t FLAG_GC_MODE = 1 << 2; // once this is true, it can never be false; also, once true, FLAG_HAS_INTERPRETED_FRAMES can't change
 50   static const uint8_t FLAG_HAS_BITMAP = 1 << 3; // can only be true if FLAG_GC_MODE is true
 51   
 52 public:
 53   inline stackChunkOopDesc* parent() const;
 54   inline void set_parent(stackChunkOopDesc* value);
 55   template<typename P>
 56   inline void set_parent_raw(oop value);
 57   template<typename P>
 58   inline bool is_parent_null() const;
 59   inline int stack_size() const;
 60   inline int sp() const;
 61   inline void set_sp(int value);
 62   inline address pc() const;
 63   inline void set_pc(address value);
 64   inline int argsize() const;
 65   inline void set_argsize(int value);
 66   inline uint8_t flags() const;
 67   inline void set_flags(uint8_t value);
 68   inline int max_size() const;
 69   inline void set_max_size(int value);
 70   inline int numFrames() const;
 71   inline void set_numFrames(int value);
 72   inline int numOops() const;
 73   inline void set_numOops(int value);
 74   inline int gc_sp() const;
 75   inline void set_gc_sp(int value);
 76   inline uint64_t mark_cycle() const;
 77   inline void set_mark_cycle(uint64_t value);
 78 
 79   inline oop cont() const;
 80   template<typename P> inline oop cont() const;
 81   inline void set_cont(oop value);
 82   template<typename P>
 83   inline void set_cont_raw(oop value);
 84 
 85   inline bool is_empty() const;
 86   inline int bottom() const;
 87   inline intptr_t* start_address() const;
 88   inline intptr_t* end_address() const;
 89   inline intptr_t* bottom_address() const; // = end_address - argsize
 90   inline intptr_t* sp_address() const;
 91   inline int to_offset(intptr_t* p) const;
 92   inline intptr_t* from_offset(int offset) const;
 93   inline bool is_in_chunk(void* p) const;
 94   inline bool is_usable_in_chunk(void* p) const;
 95   inline bool is_flag(uint8_t flag) const;
 96   inline bool is_non_null_and_flag(uint8_t flag) const;
 97   inline void set_flag(uint8_t flag, bool value);
 98   inline void clear_flags();
 99   inline bool has_mixed_frames() const;
100   inline void set_has_mixed_frames(bool value);
101   inline bool is_gc_mode() const;
102   inline void set_gc_mode(bool value);
103   inline bool has_bitmap() const;
104   inline void set_has_bitmap(bool value);
105   template <typename OopT, bool concurrent_gc> inline bool should_fix() const;
106   bool should_fix() const; // non-templatized version
107   inline bool requires_barriers() const;
108   inline void reset_counters();
109 
110   inline BitMapView bitmap() const;
111   inline BitMap::idx_t bit_offset() const;
112   inline BitMap::idx_t bit_index_for(intptr_t* p) const;
113   inline intptr_t* address_for_bit(BitMap::idx_t index) const;
114   template <typename OopT> inline BitMap::idx_t bit_index_for(OopT* p) const;
115   template <typename OopT> inline OopT* address_for_bit(BitMap::idx_t index) const;
116 
117   //
118   bool verify(size_t* out_size = NULL, int* out_oops = NULL, int* out_frames = NULL, int* out_interpreted_frames = NULL) NOT_DEBUG({ return true; });
119 
120   // template <bool mixed, typename RegisterMapT> bool do_frame(const StackChunkFrameStream<mixed>&, const RegisterMapT*);
121   template <class StackChunkFrameClosureType> 
122   inline void iterate_stack(StackChunkFrameClosureType* closure);
123 
124   MemRegion range();
125 
126   // Returns a relative frame (with offset_sp, offset_unextended_sp, and offset_fp) that can be held during safepoints.
127   // This is orthogonal to the relativizing of the actual content of interpreted frames.
128   // To be used, frame objects need to be derelativized with `derelativize`.
129   frame top_frame(RegisterMap* map);
130   frame sender(const frame& fr, RegisterMap* map);
131 
132   inline int relativize_usp_offset(const frame& fr, const int usp_offset_in_bytes) const;
133   inline address usp_offset_to_location(const frame& fr, const int usp_offset_in_bytes) const;
134   inline address reg_to_location(const frame& fr, const RegisterMap* map, VMReg reg) const;
135 
136   // access to relativized interpreter frames (both the frame object and frame content are relativized)
137   inline Method* interpreter_frame_method(const frame& fr);
138   inline address interpreter_frame_bcp(const frame& fr);
139   inline intptr_t* interpreter_frame_expression_stack_at(const frame& fr, int index) const;
140   inline intptr_t* interpreter_frame_local_at(const frame& fr, int index) const;
141 
142   int num_java_frames() const;
143 
144   template <bool dword_aligned> inline void copy_from_stack_to_chunk(intptr_t* from, intptr_t* to, int size);
145   template <bool dword_aligned> inline void copy_from_chunk_to_stack(intptr_t* from, intptr_t* to, int size);
146 
147   using oopDesc::print_on;
148   void print_on(bool verbose, outputStream* st) const;
149 
150   inline frame relativize(frame fr) const;
151   inline frame derelativize(frame fr) const;
152 
153 private:
154   template <bool mixed, class StackChunkFrameClosureType> inline void iterate_stack(StackChunkFrameClosureType* closure);
155   inline intptr_t* relative_base() const;
156 
157   inline void relativize_frame(frame& fr) const;
158   inline void derelativize_frame(frame& fr) const;
159   inline intptr_t* derelativize_address(int offset) const;
160   int relativize_address(intptr_t* p) const;
161 
162   inline void relativize_frame_pd(frame& fr) const;
163   inline void derelativize_frame_pd(frame& fr) const;
164 };
165 
166 #ifndef CHECK_UNHANDLED_OOPS
167 typedef class stackChunkOopDesc* stackChunkOop;
168 #else
169 DEF_OOP(stackChunk);
170 #endif // CHECK_UNHANDLED_OOPS
171 
172 DEF_HANDLE(stackChunk, is_stackChunk_noinline)
173 
174 #endif // SHARE_OOPS_STACKCHUNKOOP_HPP