48 // copy the spilled rbp from the heap to the stack
49 *(frame_sp - frame::sender_sp_offset) = *(heap_sp - frame::sender_sp_offset);
50 }
51
52 // Slow path
53
54 template<typename FKind>
55 inline frame FreezeBase::sender(const frame& f) {
56 assert(FKind::is_instance(f), "");
57 if (FKind::interpreted) {
58 return frame(f.sender_sp(), f.interpreter_frame_sender_sp(), f.link(), f.sender_pc());
59 }
60 intptr_t** link_addr = link_address<FKind>(f);
61
62 intptr_t* sender_sp = (intptr_t*)(link_addr + frame::sender_sp_offset); // f.unextended_sp() + (fsize/wordSize); //
63 address sender_pc = (address) *(sender_sp-1);
64 assert(sender_sp != f.sp(), "must have changed");
65
66 int slot = 0;
67 CodeBlob* sender_cb = CodeCache::find_blob_and_oopmap(sender_pc, slot);
68 return sender_cb != nullptr
69 ? frame(sender_sp, sender_sp, *link_addr, sender_pc, sender_cb,
70 slot == -1 ? nullptr : sender_cb->oop_map_for_slot(slot, sender_pc), false)
71 : frame(sender_sp, sender_sp, *link_addr, sender_pc);
72 }
73
74 template<typename FKind>
75 frame FreezeBase::new_heap_frame(frame& f, frame& caller) {
76 assert(FKind::is_instance(f), "");
77 assert(!caller.is_interpreted_frame()
78 || caller.unextended_sp() == (intptr_t*)caller.at(frame::interpreter_frame_last_sp_offset), "");
79
80 intptr_t *sp, *fp; // sp is really our unextended_sp
81 if (FKind::interpreted) {
82 assert((intptr_t*)f.at_relative_or_null(frame::interpreter_frame_last_sp_offset) == nullptr
83 || f.unextended_sp() == (intptr_t*)f.at_relative(frame::interpreter_frame_last_sp_offset), "");
84 intptr_t locals_offset = *f.addr_at(frame::interpreter_frame_locals_offset);
85 // If the caller.is_empty(), i.e. we're freezing into an empty chunk, then we set
86 // the chunk's argsize in finalize_freeze and make room for it above the unextended_sp
87 bool overlap_caller = caller.is_interpreted_frame() || caller.is_empty();
88 fp = caller.unextended_sp() - 1 - locals_offset + (overlap_caller ? ContinuationHelper::InterpretedFrame::stack_argsize(f) : 0);
89 sp = fp - (f.fp() - f.unextended_sp());
90 assert(sp <= fp, "");
91 assert(fp <= caller.unextended_sp(), "");
92 caller.set_sp(fp + frame::sender_sp_offset);
93
94 assert(_cont.tail()->is_in_chunk(sp), "");
95
96 frame hf(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */);
97 // copy relativized locals from the stack frame
98 *hf.addr_at(frame::interpreter_frame_locals_offset) = locals_offset;
99 return hf;
100 } else {
101 // We need to re-read fp out of the frame because it may be an oop and we might have
102 // had a safepoint in finalize_freeze, after constructing f.
103 fp = *(intptr_t**)(f.sp() - frame::sender_sp_offset);
104
105 int fsize = FKind::size(f);
106 sp = caller.unextended_sp() - fsize;
107 if (caller.is_interpreted_frame()) {
108 // If the caller is interpreted, our stackargs are not supposed to overlap with it
109 // so we make more room by moving sp down by argsize
110 int argsize = FKind::stack_argsize(f);
111 sp -= argsize;
112 }
113 caller.set_sp(sp + fsize);
114
115 assert(_cont.tail()->is_in_chunk(sp), "");
116
117 return frame(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */);
118 }
119 }
120
121 void FreezeBase::adjust_interpreted_frame_unextended_sp(frame& f) {
122 assert((f.at(frame::interpreter_frame_last_sp_offset) != 0) || (f.unextended_sp() == f.sp()), "");
123 intptr_t* real_unextended_sp = (intptr_t*)f.at_relative_or_null(frame::interpreter_frame_last_sp_offset);
124 if (real_unextended_sp != nullptr) {
125 f.set_unextended_sp(real_unextended_sp); // can be null at a safepoint
126 }
127 }
154 assert(hf.unextended_sp() <= (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), "");
155 assert(hf.fp() > (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), "");
156 assert(hf.fp() <= (intptr_t*)hf.at(frame::interpreter_frame_locals_offset), "");
157 }
158
159 inline void FreezeBase::set_top_frame_metadata_pd(const frame& hf) {
160 stackChunkOop chunk = _cont.tail();
161 assert(chunk->is_in_chunk(hf.sp() - 1), "");
162 assert(chunk->is_in_chunk(hf.sp() - frame::sender_sp_offset), "");
163
164 address frame_pc = hf.pc();
165
166 *(hf.sp() - 1) = (intptr_t)hf.pc();
167
168 intptr_t* fp_addr = hf.sp() - frame::sender_sp_offset;
169 *fp_addr = hf.is_interpreted_frame() ? (intptr_t)(hf.fp() - fp_addr)
170 : (intptr_t)hf.fp();
171 assert(frame_pc == ContinuationHelper::Frame::real_pc(hf), "");
172 }
173
174 inline void FreezeBase::patch_pd(frame& hf, const frame& caller) {
175 if (caller.is_interpreted_frame()) {
176 assert(!caller.is_empty(), "");
177 patch_callee_link_relative(caller, caller.fp());
178 } else {
179 // If we're the bottom-most frame frozen in this freeze, the caller might have stayed frozen in the chunk,
180 // and its oop-containing fp fixed. We've now just overwritten it, so we must patch it back to its value
181 // as read from the chunk.
182 patch_callee_link(caller, caller.fp());
183 }
184 }
185
186 //////// Thaw
187
188 // Fast path
189
190 inline void ThawBase::prefetch_chunk_pd(void* start, int size) {
191 size <<= LogBytesPerWord;
192 Prefetch::read(start, size);
193 Prefetch::read(start, size - 64);
194 }
195
196 template <typename ConfigT>
197 inline void Thaw<ConfigT>::patch_caller_links(intptr_t* sp, intptr_t* bottom) {
198 // Fast path depends on !PreserveFramePointer. See can_thaw_fast().
199 assert(!PreserveFramePointer, "Frame pointers need to be fixed");
200 }
201
202 // Slow path
203
204 inline frame ThawBase::new_entry_frame() {
205 intptr_t* sp = _cont.entrySP();
206 return frame(sp, sp, _cont.entryFP(), _cont.entryPC()); // TODO PERF: This finds code blob and computes deopt state
207 }
208
209 template<typename FKind> frame ThawBase::new_stack_frame(const frame& hf, frame& caller, bool bottom) {
210 assert(FKind::is_instance(hf), "");
211 // The values in the returned frame object will be written into the callee's stack in patch.
212
213 if (FKind::interpreted) {
214 intptr_t* heap_sp = hf.unextended_sp();
215 // If caller is interpreted it already made room for the callee arguments
216 int overlap = caller.is_interpreted_frame() ? ContinuationHelper::InterpretedFrame::stack_argsize(hf) : 0;
217 const int fsize = (int)(ContinuationHelper::InterpretedFrame::frame_bottom(hf) - hf.unextended_sp() - overlap);
218 intptr_t* frame_sp = caller.unextended_sp() - fsize;
219 intptr_t* fp = frame_sp + (hf.fp() - heap_sp);
220 DEBUG_ONLY(intptr_t* unextended_sp = fp + *hf.addr_at(frame::interpreter_frame_last_sp_offset);)
221 assert(frame_sp == unextended_sp, "");
222 caller.set_sp(fp + frame::sender_sp_offset);
223 frame f(frame_sp, frame_sp, fp, hf.pc());
224 // we need to set the locals so that the caller of new_stack_frame() can call
225 // ContinuationHelper::InterpretedFrame::frame_bottom
226 intptr_t locals_offset = *hf.addr_at(frame::interpreter_frame_locals_offset);
227 DEBUG_ONLY(Method* m = hf.interpreter_frame_method();)
228 // Frames for native methods have 2 extra words (temp oop/result handler) before fixed part of frame.
229 DEBUG_ONLY(const int max_locals = !m->is_native() ? m->max_locals() : m->size_of_parameters() + 2;)
230 assert((int)locals_offset == frame::sender_sp_offset + max_locals - 1, "");
231 // copy relativized locals from the heap frame
232 *f.addr_at(frame::interpreter_frame_locals_offset) = locals_offset;
233 return f;
234 } else {
235 int fsize = FKind::size(hf);
236 intptr_t* frame_sp = caller.unextended_sp() - fsize;
237 if (bottom || caller.is_interpreted_frame()) {
238 int argsize = FKind::stack_argsize(hf);
239
240 fsize += argsize;
241 frame_sp -= argsize;
242 caller.set_sp(caller.sp() - argsize);
243 assert(caller.sp() == frame_sp + (fsize-argsize), "");
244
245 frame_sp = align(hf, frame_sp, caller, bottom);
246 }
247
248 assert(hf.cb() != nullptr, "");
249 assert(hf.oop_map() != nullptr, "");
250 intptr_t* fp;
251 if (PreserveFramePointer) {
252 // we need to recreate a "real" frame pointer, pointing into the stack
253 fp = frame_sp + FKind::size(hf) - frame::sender_sp_offset;
254 } else {
255 fp = FKind::stub || FKind::native
256 ? frame_sp + fsize - frame::sender_sp_offset // fp always points to the address below the pushed return pc. We need correct address.
257 : *(intptr_t**)(hf.sp() - frame::sender_sp_offset); // we need to re-read fp because it may be an oop and we might have fixed the frame.
258 }
259 return frame(frame_sp, frame_sp, fp, hf.pc(), hf.cb(), hf.oop_map(), false); // TODO PERF : this computes deopt state; is it necessary?
260 }
261 }
262
263 inline intptr_t* ThawBase::align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom) {
264 if (((intptr_t)frame_sp & 0xf) != 0) {
265 assert(caller.is_interpreted_frame() || (bottom && hf.compiled_frame_stack_argsize() % 2 != 0), "");
266 frame_sp--;
267 caller.set_sp(caller.sp() - 1);
268 }
269 assert(is_aligned(frame_sp, frame::frame_alignment), "");
270 return frame_sp;
271 }
272
273 inline void ThawBase::patch_pd(frame& f, const frame& caller) {
274 patch_callee_link(caller, caller.fp());
275 }
276
277 inline void ThawBase::patch_pd(frame& f, intptr_t* caller_sp) {
278 intptr_t* fp = caller_sp - frame::sender_sp_offset;
279 patch_callee_link(f, fp);
280 }
281
282 inline intptr_t* ThawBase::push_cleanup_continuation() {
283 frame enterSpecial = new_entry_frame();
284 intptr_t* sp = enterSpecial.sp();
285
286 sp[-1] = (intptr_t)ContinuationEntry::cleanup_pc();
287 sp[-2] = (intptr_t)enterSpecial.fp();
288
289 log_develop_trace(continuations, preempt)("push_cleanup_continuation initial sp: " INTPTR_FORMAT " final sp: " INTPTR_FORMAT, p2i(sp + 2 * frame::metadata_words), p2i(sp));
290 return sp;
291 }
292
293 inline void ThawBase::derelativize_interpreted_frame_metadata(const frame& hf, const frame& f) {
294 // Make sure that last_sp is kept relativized.
|
48 // copy the spilled rbp from the heap to the stack
49 *(frame_sp - frame::sender_sp_offset) = *(heap_sp - frame::sender_sp_offset);
50 }
51
52 // Slow path
53
54 template<typename FKind>
55 inline frame FreezeBase::sender(const frame& f) {
56 assert(FKind::is_instance(f), "");
57 if (FKind::interpreted) {
58 return frame(f.sender_sp(), f.interpreter_frame_sender_sp(), f.link(), f.sender_pc());
59 }
60 intptr_t** link_addr = link_address<FKind>(f);
61
62 intptr_t* sender_sp = (intptr_t*)(link_addr + frame::sender_sp_offset); // f.unextended_sp() + (fsize/wordSize); //
63 address sender_pc = (address) *(sender_sp-1);
64 assert(sender_sp != f.sp(), "must have changed");
65
66 int slot = 0;
67 CodeBlob* sender_cb = CodeCache::find_blob_and_oopmap(sender_pc, slot);
68
69 // Repair the sender sp if the frame has been extended
70 if (sender_cb->is_nmethod()) {
71 sender_sp = f.repair_sender_sp(sender_sp, link_addr);
72 }
73
74 return sender_cb != nullptr
75 ? frame(sender_sp, sender_sp, *link_addr, sender_pc, sender_cb,
76 slot == -1 ? nullptr : sender_cb->oop_map_for_slot(slot, sender_pc), false)
77 : frame(sender_sp, sender_sp, *link_addr, sender_pc);
78 }
79
80 template<typename FKind>
81 frame FreezeBase::new_heap_frame(frame& f, frame& caller, int size_adjust) {
82 assert(FKind::is_instance(f), "");
83 assert(!caller.is_interpreted_frame()
84 || caller.unextended_sp() == (intptr_t*)caller.at(frame::interpreter_frame_last_sp_offset), "");
85
86 intptr_t *sp, *fp; // sp is really our unextended_sp
87 if (FKind::interpreted) {
88 assert((intptr_t*)f.at_relative_or_null(frame::interpreter_frame_last_sp_offset) == nullptr
89 || f.unextended_sp() == (intptr_t*)f.at_relative(frame::interpreter_frame_last_sp_offset), "");
90 intptr_t locals_offset = *f.addr_at(frame::interpreter_frame_locals_offset);
91 // If the caller.is_empty(), i.e. we're freezing into an empty chunk, then we set
92 // the chunk's argsize in finalize_freeze and make room for it above the unextended_sp
93 bool overlap_caller = caller.is_interpreted_frame() || caller.is_empty();
94 fp = caller.unextended_sp() - 1 - locals_offset + (overlap_caller ? ContinuationHelper::InterpretedFrame::stack_argsize(f) : 0);
95 sp = fp - (f.fp() - f.unextended_sp());
96 assert(sp <= fp, "");
97 assert(fp <= caller.unextended_sp(), "");
98 caller.set_sp(fp + frame::sender_sp_offset);
99
100 assert(_cont.tail()->is_in_chunk(sp), "");
101
102 frame hf(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */);
103 // copy relativized locals from the stack frame
104 *hf.addr_at(frame::interpreter_frame_locals_offset) = locals_offset;
105 return hf;
106 } else {
107 // We need to re-read fp out of the frame because it may be an oop and we might have
108 // had a safepoint in finalize_freeze, after constructing f.
109 fp = *(intptr_t**)(f.sp() - frame::sender_sp_offset);
110
111 int fsize = FKind::size(f);
112 sp = caller.unextended_sp() - fsize - size_adjust;
113 if (caller.is_interpreted_frame() && size_adjust == 0) {
114 // If the caller is interpreted, our stackargs are not supposed to overlap with it
115 // so we make more room by moving sp down by argsize
116 int argsize = FKind::stack_argsize(f);
117 sp -= argsize;
118 }
119 caller.set_sp(sp + fsize);
120
121 assert(_cont.tail()->is_in_chunk(sp), "");
122
123 return frame(sp, sp, fp, f.pc(), nullptr, nullptr, true /* on_heap */);
124 }
125 }
126
127 void FreezeBase::adjust_interpreted_frame_unextended_sp(frame& f) {
128 assert((f.at(frame::interpreter_frame_last_sp_offset) != 0) || (f.unextended_sp() == f.sp()), "");
129 intptr_t* real_unextended_sp = (intptr_t*)f.at_relative_or_null(frame::interpreter_frame_last_sp_offset);
130 if (real_unextended_sp != nullptr) {
131 f.set_unextended_sp(real_unextended_sp); // can be null at a safepoint
132 }
133 }
160 assert(hf.unextended_sp() <= (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), "");
161 assert(hf.fp() > (intptr_t*)hf.at(frame::interpreter_frame_initial_sp_offset), "");
162 assert(hf.fp() <= (intptr_t*)hf.at(frame::interpreter_frame_locals_offset), "");
163 }
164
165 inline void FreezeBase::set_top_frame_metadata_pd(const frame& hf) {
166 stackChunkOop chunk = _cont.tail();
167 assert(chunk->is_in_chunk(hf.sp() - 1), "");
168 assert(chunk->is_in_chunk(hf.sp() - frame::sender_sp_offset), "");
169
170 address frame_pc = hf.pc();
171
172 *(hf.sp() - 1) = (intptr_t)hf.pc();
173
174 intptr_t* fp_addr = hf.sp() - frame::sender_sp_offset;
175 *fp_addr = hf.is_interpreted_frame() ? (intptr_t)(hf.fp() - fp_addr)
176 : (intptr_t)hf.fp();
177 assert(frame_pc == ContinuationHelper::Frame::real_pc(hf), "");
178 }
179
180 inline void FreezeBase::patch_pd(frame& hf, const frame& caller, bool is_bottom_frame) {
181 if (caller.is_interpreted_frame()) {
182 assert(!caller.is_empty(), "");
183 patch_callee_link_relative(caller, caller.fp());
184 } else if (is_bottom_frame && caller.pc() != nullptr) {
185 assert(caller.is_compiled_frame(), "");
186 // If we're the bottom-most frame frozen in this freeze, the caller might have stayed frozen in the chunk,
187 // and its oop-containing fp fixed. We've now just overwritten it, so we must patch it back to its value
188 // as read from the chunk.
189 patch_callee_link(caller, caller.fp());
190 }
191 }
192
193 //////// Thaw
194
195 // Fast path
196
197 inline void ThawBase::prefetch_chunk_pd(void* start, int size) {
198 size <<= LogBytesPerWord;
199 Prefetch::read(start, size);
200 Prefetch::read(start, size - 64);
201 }
202
203 template <typename ConfigT>
204 inline void Thaw<ConfigT>::patch_caller_links(intptr_t* sp, intptr_t* bottom) {
205 // Fast path depends on !PreserveFramePointer. See can_thaw_fast().
206 assert(!PreserveFramePointer, "Frame pointers need to be fixed");
207 }
208
209 // Slow path
210
211 inline frame ThawBase::new_entry_frame() {
212 intptr_t* sp = _cont.entrySP();
213 return frame(sp, sp, _cont.entryFP(), _cont.entryPC()); // TODO PERF: This finds code blob and computes deopt state
214 }
215
216 template<typename FKind> frame ThawBase::new_stack_frame(const frame& hf, frame& caller, bool bottom, int size_adjust) {
217 assert(FKind::is_instance(hf), "");
218 // The values in the returned frame object will be written into the callee's stack in patch.
219
220 if (FKind::interpreted) {
221 intptr_t* heap_sp = hf.unextended_sp();
222 // If caller is interpreted it already made room for the callee arguments
223 int overlap = caller.is_interpreted_frame() ? ContinuationHelper::InterpretedFrame::stack_argsize(hf) : 0;
224 const int fsize = (int)(ContinuationHelper::InterpretedFrame::frame_bottom(hf) - hf.unextended_sp() - overlap);
225 intptr_t* frame_sp = caller.unextended_sp() - fsize;
226 intptr_t* fp = frame_sp + (hf.fp() - heap_sp);
227 DEBUG_ONLY(intptr_t* unextended_sp = fp + *hf.addr_at(frame::interpreter_frame_last_sp_offset);)
228 assert(frame_sp == unextended_sp, "");
229 caller.set_sp(fp + frame::sender_sp_offset);
230 frame f(frame_sp, frame_sp, fp, hf.pc());
231 // we need to set the locals so that the caller of new_stack_frame() can call
232 // ContinuationHelper::InterpretedFrame::frame_bottom
233 intptr_t locals_offset = *hf.addr_at(frame::interpreter_frame_locals_offset);
234 DEBUG_ONLY(Method* m = hf.interpreter_frame_method();)
235 // Frames for native methods have 2 extra words (temp oop/result handler) before fixed part of frame.
236 DEBUG_ONLY(const int max_locals = !m->is_native() ? m->max_locals() : m->size_of_parameters() + 2;)
237 assert((int)locals_offset == frame::sender_sp_offset + max_locals - 1, "");
238 // copy relativized locals from the heap frame
239 *f.addr_at(frame::interpreter_frame_locals_offset) = locals_offset;
240 return f;
241 } else {
242 int fsize = FKind::size(hf);
243 intptr_t* frame_sp = caller.unextended_sp() - fsize - size_adjust;
244 if (bottom || caller.is_interpreted_frame()) {
245 if (size_adjust == 0) {
246 int argsize = FKind::stack_argsize(hf);
247 frame_sp -= argsize;
248 }
249 frame_sp = align(hf, frame_sp, caller, bottom);
250 }
251 caller.set_sp(frame_sp + fsize);
252 assert(is_aligned(frame_sp, frame::frame_alignment), "");
253
254 assert(hf.cb() != nullptr, "");
255 assert(hf.oop_map() != nullptr, "");
256 intptr_t* fp;
257 if (PreserveFramePointer) {
258 // we need to recreate a "real" frame pointer, pointing into the stack
259 fp = frame_sp + fsize - frame::sender_sp_offset;
260 } else {
261 fp = FKind::stub || FKind::native
262 ? frame_sp + fsize - frame::sender_sp_offset // fp always points to the address below the pushed return pc. We need correct address.
263 : *(intptr_t**)(hf.sp() - frame::sender_sp_offset); // we need to re-read fp because it may be an oop and we might have fixed the frame.
264 }
265 return frame(frame_sp, frame_sp, fp, hf.pc(), hf.cb(), hf.oop_map(), false); // TODO PERF : this computes deopt state; is it necessary?
266 }
267 }
268
269 inline intptr_t* ThawBase::align(const frame& hf, intptr_t* frame_sp, frame& caller, bool bottom) {
270 if (((intptr_t)frame_sp & 0xf) != 0) {
271 assert(caller.is_interpreted_frame() || (bottom && hf.compiled_frame_stack_argsize() % 2 != 0), "");
272 frame_sp--;
273 }
274 assert(is_aligned(frame_sp, frame::frame_alignment), "");
275 return frame_sp;
276 }
277
278 inline void ThawBase::patch_pd(frame& f, const frame& caller) {
279 if (caller.is_interpreted_frame() || PreserveFramePointer) {
280 patch_callee_link(caller, caller.fp());
281 }
282 }
283
284 inline void ThawBase::patch_pd(frame& f, intptr_t* caller_sp) {
285 intptr_t* fp = caller_sp - frame::sender_sp_offset;
286 patch_callee_link(f, fp);
287 }
288
289 inline intptr_t* ThawBase::push_cleanup_continuation() {
290 frame enterSpecial = new_entry_frame();
291 intptr_t* sp = enterSpecial.sp();
292
293 sp[-1] = (intptr_t)ContinuationEntry::cleanup_pc();
294 sp[-2] = (intptr_t)enterSpecial.fp();
295
296 log_develop_trace(continuations, preempt)("push_cleanup_continuation initial sp: " INTPTR_FORMAT " final sp: " INTPTR_FORMAT, p2i(sp + 2 * frame::metadata_words), p2i(sp));
297 return sp;
298 }
299
300 inline void ThawBase::derelativize_interpreted_frame_metadata(const frame& hf, const frame& f) {
301 // Make sure that last_sp is kept relativized.
|