1 /*
  2  * Copyright (c) 2022, 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_VM_RUNTIME_CONTINUATIONENTRY_HPP
 26 #define SHARE_VM_RUNTIME_CONTINUATIONENTRY_HPP
 27 
 28 #include "oops/oop.hpp"
 29 #include "oops/oopsHierarchy.hpp"
 30 #include "runtime/continuation.hpp"
 31 #include "utilities/sizes.hpp"
 32 
 33 #include CPU_HEADER(continuationEntry)
 34 
 35 class JavaThread;
 36 class nmethod;
 37 class OopMap;
 38 class RegisterMap;
 39 
 40 // Metadata stored in the continuation entry frame
 41 class ContinuationEntry {
 42   friend class JVMCIVMStructs;
 43   ContinuationEntryPD _pd;
 44 #ifdef ASSERT
 45 private:
 46   static const int COOKIE_VALUE = 0x1234;
 47   int cookie;
 48 
 49 public:
 50   static int cookie_value() { return COOKIE_VALUE; }
 51   static ByteSize cookie_offset() { return byte_offset_of(ContinuationEntry, cookie); }
 52 
 53   void verify_cookie() {
 54     assert(cookie == COOKIE_VALUE, "Bad cookie: %#x, expected: %#x", cookie, COOKIE_VALUE);
 55   }
 56 #endif
 57 
 58 public:
 59   static int _return_pc_offset; // friend gen_continuation_enter
 60   static int _thaw_call_pc_offset;
 61   static int _cleanup_offset;
 62 
 63   static void set_enter_code(nmethod* nm, int interpreted_entry_offset);
 64   static bool is_interpreted_call(address call_address);
 65 
 66 private:
 67   static address _return_pc;
 68   static address _thaw_call_pc;
 69   static address _cleanup_pc;
 70   static nmethod* _enter_special;
 71   static int _interpreted_entry_offset;
 72 
 73 private:
 74   ContinuationEntry* _parent;
 75   oopDesc* _cont;
 76   oopDesc* _chunk;
 77   int _flags;
 78   // Size in words of the stack arguments of the bottom frame on stack if compiled 0 otherwise.
 79   // The caller (if there is one) is the still frozen top frame in the StackChunk.
 80   int _argsize;
 81   intptr_t* _parent_cont_fastpath;
 82 #ifdef _LP64
 83   int64_t   _parent_held_monitor_count;
 84 #else
 85   int32_t   _parent_held_monitor_count;
 86 #endif
 87   uint32_t _pin_count;
 88 
 89 public:
 90   static ByteSize parent_offset()   { return byte_offset_of(ContinuationEntry, _parent); }
 91   static ByteSize cont_offset()     { return byte_offset_of(ContinuationEntry, _cont); }
 92   static ByteSize chunk_offset()    { return byte_offset_of(ContinuationEntry, _chunk); }
 93   static ByteSize flags_offset()    { return byte_offset_of(ContinuationEntry, _flags); }
 94   static ByteSize argsize_offset()  { return byte_offset_of(ContinuationEntry, _argsize); }
 95   static ByteSize pin_count_offset(){ return byte_offset_of(ContinuationEntry, _pin_count); }
 96   static ByteSize parent_cont_fastpath_offset()      { return byte_offset_of(ContinuationEntry, _parent_cont_fastpath); }
 97   static ByteSize parent_held_monitor_count_offset() { return byte_offset_of(ContinuationEntry, _parent_held_monitor_count); }
 98 
 99 public:
100   static size_t size() { return align_up((int)sizeof(ContinuationEntry), 2*wordSize); }
101 
102   ContinuationEntry* parent() const { return _parent; }
103   int64_t parent_held_monitor_count() const { return (int64_t)_parent_held_monitor_count; }
104 
105   static address entry_pc() { return _return_pc; }
106   intptr_t* entry_sp() const { return (intptr_t*)this; }
107   intptr_t* entry_fp() const;
108 
109   static address thaw_call_pc_address() { return (address)&_thaw_call_pc; }
110   static address cleanup_pc() { return _cleanup_pc; }
111 
112   static address compiled_entry();
113   static address interpreted_entry();
114 
115   int argsize() const { return _argsize; }
116   void set_argsize(int value) { _argsize = value; }
117 
118   bool is_pinned() { return _pin_count > 0; }
119   bool pin() {
120     if (_pin_count == UINT32_MAX) return false;
121     _pin_count++;
122     return true;
123   }
124   bool unpin() {
125     if (_pin_count == 0) return false;
126     _pin_count--;
127     return true;
128   }
129 
130   intptr_t* parent_cont_fastpath() const { return _parent_cont_fastpath; }
131   void set_parent_cont_fastpath(intptr_t* x) { _parent_cont_fastpath = x; }
132 
133   static ContinuationEntry* from_frame(const frame& f);
134   frame to_frame() const;
135   void update_register_map(RegisterMap* map) const;
136   void flush_stack_processing(JavaThread* thread) const;
137 
138   inline intptr_t* bottom_sender_sp() const;
139   inline oop cont_oop(const JavaThread* thread) const;
140   inline oop scope(const JavaThread* thread) const;
141   inline static oop cont_oop_or_null(const ContinuationEntry* ce, const JavaThread* thread);
142 
143   oop* cont_addr() { return (oop*)&_cont; }
144   oop* chunk_addr() { return (oop*)&_chunk; }
145 
146   bool is_virtual_thread() const { return _flags != 0; }
147 
148 #ifndef PRODUCT
149   void describe(FrameValues& values, int frame_no) const;
150 #endif
151 
152 #ifdef ASSERT
153   static bool assert_entry_frame_laid_out(JavaThread* thread);
154 #endif
155 };
156 
157 #endif // SHARE_VM_RUNTIME_CONTINUATIONENTRY_HPP