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 gc_sp() const;
 71   inline void set_gc_sp(int value);
 72   inline uint64_t mark_cycle() const;
 73   inline void set_mark_cycle(uint64_t value);
 74 
 75   inline oop cont() const;
 76   template<typename P> inline oop cont() const;
 77   inline void set_cont(oop value);
 78   template<typename P>
 79   inline void set_cont_raw(oop value);
 80 
 81   inline bool is_empty() const;
 82   inline int bottom() const;
 83   inline intptr_t* start_address() const;
 84   inline intptr_t* end_address() const;
 85   inline intptr_t* bottom_address() const; // = end_address - argsize
 86   inline intptr_t* sp_address() const;
 87   inline int to_offset(intptr_t* p) const;
 88   inline intptr_t* from_offset(int offset) const;
 89   inline bool is_in_chunk(void* p) const;
 90   inline bool is_usable_in_chunk(void* p) const;
 91   inline bool is_flag(uint8_t flag) const;
 92   inline bool is_non_null_and_flag(uint8_t flag) const;
 93   inline void set_flag(uint8_t flag, bool value);
 94   inline void clear_flags();
 95   inline bool has_mixed_frames() const;
 96   inline void set_has_mixed_frames(bool value);
 97   inline bool is_gc_mode() const;
 98   inline void set_gc_mode(bool value);
 99   inline bool has_bitmap() const;
100   inline void set_has_bitmap(bool value);
101   template <typename OopT, bool concurrent_gc> inline bool should_fix() const;
102   bool should_fix() const; // non-templatized version
103   inline bool requires_barriers() const;
104 
105   inline BitMapView bitmap() const;
106   inline BitMap::idx_t bit_offset() const;
107   inline BitMap::idx_t bit_index_for(intptr_t* p) const;
108   inline intptr_t* address_for_bit(BitMap::idx_t index) const;
109   template <typename OopT> inline BitMap::idx_t bit_index_for(OopT* p) const;
110   template <typename OopT> inline OopT* address_for_bit(BitMap::idx_t index) const;
111 
112   bool verify(size_t* out_size = NULL, int* out_oops = NULL, int* out_frames = NULL, int* out_interpreted_frames = NULL) NOT_DEBUG({ return true; });
113 
114   template <class StackChunkFrameClosureType> 
115   inline void iterate_stack(StackChunkFrameClosureType* closure);
116 
117   MemRegion range();
118 
119   // Returns a relative frame (with offset_sp, offset_unextended_sp, and offset_fp) that can be held during safepoints.
120   // This is orthogonal to the relativizing of the actual content of interpreted frames.
121   // To be used, frame objects need to be derelativized with `derelativize`.
122   frame top_frame(RegisterMap* map);
123   frame sender(const frame& fr, RegisterMap* map);
124 
125   inline int relativize_usp_offset(const frame& fr, const int usp_offset_in_bytes) const;
126   inline address usp_offset_to_location(const frame& fr, const int usp_offset_in_bytes) const;
127   inline address reg_to_location(const frame& fr, const RegisterMap* map, VMReg reg) const;
128 
129   // access to relativized interpreter frames (both the frame object and frame content are relativized)
130   inline Method* interpreter_frame_method(const frame& fr);
131   inline address interpreter_frame_bcp(const frame& fr);
132   inline intptr_t* interpreter_frame_expression_stack_at(const frame& fr, int index) const;
133   inline intptr_t* interpreter_frame_local_at(const frame& fr, int index) const;
134 
135   int num_java_frames() const;
136 
137   template <bool dword_aligned> inline void copy_from_stack_to_chunk(intptr_t* from, intptr_t* to, int size);
138   template <bool dword_aligned> inline void copy_from_chunk_to_stack(intptr_t* from, intptr_t* to, int size);
139 
140   using oopDesc::print_on;
141   void print_on(bool verbose, outputStream* st) const;
142 
143   inline frame relativize(frame fr) const;
144   inline frame derelativize(frame fr) const;
145 
146 private:
147   template <bool mixed, class StackChunkFrameClosureType> inline void iterate_stack(StackChunkFrameClosureType* closure);
148   inline intptr_t* relative_base() const;
149 
150   inline void relativize_frame(frame& fr) const;
151   inline void derelativize_frame(frame& fr) const;
152   inline intptr_t* derelativize_address(int offset) const;
153   int relativize_address(intptr_t* p) const;
154 
155   inline void relativize_frame_pd(frame& fr) const;
156   inline void derelativize_frame_pd(frame& fr) const;
157 };
158 
159 #ifndef CHECK_UNHANDLED_OOPS
160 typedef class stackChunkOopDesc* stackChunkOop;
161 #else
162 DEF_OOP(stackChunk);
163 #endif // CHECK_UNHANDLED_OOPS
164 
165 DEF_HANDLE(stackChunk, is_stackChunk_noinline)
166 
167 #endif // SHARE_OOPS_STACKCHUNKOOP_HPP