1 /*
  2  * Copyright (c) 2018, 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_CONTINUATION_HPP
 26 #define SHARE_VM_RUNTIME_CONTINUATION_HPP
 27 
 28 #include "jni.h"
 29 #include "memory/allStatic.hpp"
 30 #include "oops/oopsHierarchy.hpp"
 31 
 32 class ContinuationEntry;
 33 class frame;
 34 class FrameValues;
 35 class Handle;
 36 class outputStream;
 37 class RegisterMap;
 38 
 39 class Continuations : public AllStatic {
 40 public:
 41   static void init();
 42   static bool enabled();
 43 };
 44 
 45 void continuations_init();
 46 
 47 class javaVFrame;
 48 class JavaThread;
 49 
 50 // should match Continuation.pinnedReason() in Continuation.java
 51 enum freeze_result {
 52   freeze_ok = 0,
 53   freeze_ok_bottom = 1,
 54   freeze_pinned_cs = 2,
 55   freeze_pinned_native = 3,
 56   freeze_pinned_monitor = 4,
 57   freeze_exception = 5,
 58   freeze_not_mounted = 6,
 59   freeze_unsupported = 7
 60 };
 61 
 62 class Continuation : AllStatic {
 63 public:
 64 
 65   enum preempt_kind {
 66     monitorenter,
 67     object_wait,
 68     object_locker
 69   };
 70 
 71   enum thaw_kind {
 72     thaw_top = 0,
 73     thaw_return_barrier = 1,
 74     thaw_return_barrier_exception = 2,
 75   };
 76 
 77   static bool is_thaw_return_barrier(thaw_kind kind) {
 78     return kind != thaw_top;
 79   }
 80 
 81   static bool is_thaw_return_barrier_exception(thaw_kind kind) {
 82     bool r = (kind == thaw_return_barrier_exception);
 83     assert(!r || is_thaw_return_barrier(kind), "must be");
 84     return r;
 85   }
 86 
 87   static void init();
 88 
 89   static address freeze_entry();
 90   static address freeze_preempt_entry();
 91   static int prepare_thaw(JavaThread* thread, bool return_barrier);
 92   static address thaw_entry();
 93 
 94   static freeze_result try_preempt(JavaThread* target, oop continuation);
 95 
 96   static ContinuationEntry* get_continuation_entry_for_continuation(JavaThread* thread, oop continuation);
 97   static ContinuationEntry* get_continuation_entry_for_sp(JavaThread* thread, intptr_t* const sp);
 98   static ContinuationEntry* get_continuation_entry_for_entry_frame(JavaThread* thread, const frame& f);
 99 
100   static bool is_continuation_mounted(JavaThread* thread, oop continuation);
101 
102   static bool is_cont_barrier_frame(const frame& f);
103   static bool is_return_barrier_entry(const address pc);
104   static bool is_continuation_enterSpecial(const frame& f);
105   static bool is_continuation_entry_frame(const frame& f, const RegisterMap *map);
106 
107   static bool is_frame_in_continuation(const ContinuationEntry* entry, const frame& f);
108   static bool is_frame_in_continuation(JavaThread* thread, const frame& f);
109 
110   static bool has_last_Java_frame(oop continuation, frame* frame, RegisterMap* map);
111   static frame last_frame(oop continuation, RegisterMap *map);
112   static frame top_frame(const frame& callee, RegisterMap* map);
113   static javaVFrame* last_java_vframe(Handle continuation, RegisterMap *map);
114   static frame continuation_parent_frame(RegisterMap* map);
115 
116   static oop continuation_scope(oop continuation);
117   static bool is_scope_bottom(oop cont_scope, const frame& fr, const RegisterMap* map);
118 
119   static bool is_in_usable_stack(address addr, const RegisterMap* map);
120 
121   // pins/unpins the innermost mounted continuation; returns true on success or false if there's no continuation or the operation failed
122   static bool pin(JavaThread* current);
123   static bool unpin(JavaThread* current);
124 
125   static frame continuation_bottom_sender(JavaThread* thread, const frame& callee, intptr_t* sender_sp);
126   static address get_top_return_pc_post_barrier(JavaThread* thread, address pc);
127   static void set_cont_fastpath_thread_state(JavaThread* thread);
128   static void notify_deopt(JavaThread* thread, intptr_t* sp);
129 
130   // access frame data
131 
132 #ifndef PRODUCT
133   static void describe(FrameValues &values);
134 #endif
135 
136 private:
137   friend class InstanceStackChunkKlass;
138 
139 #ifdef ASSERT
140 public:
141   static void debug_verify_continuation(oop continuation);
142   static void print(oop continuation);
143   static void print_on(outputStream* st, oop continuation);
144 #endif
145 };
146 
147 void CONT_RegisterNativeMethods(JNIEnv *env, jclass cls);
148 
149 #endif // SHARE_VM_RUNTIME_CONTINUATION_HPP