1 /*
2 * Copyright (c) 1997, 2025, 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 CPU_X86_FRAME_X86_INLINE_HPP
26 #define CPU_X86_FRAME_X86_INLINE_HPP
27
28 #include "code/codeBlob.inline.hpp"
29 #include "code/codeCache.inline.hpp"
30 #include "code/vmreg.inline.hpp"
31 #include "compiler/oopMap.inline.hpp"
32 #include "interpreter/interpreter.hpp"
33 #include "runtime/sharedRuntime.hpp"
34 #include "runtime/registerMap.hpp"
35
36 // Inline functions for Intel frames:
37
38 #if INCLUDE_JFR
39
40 // Static helper routines
41
42 inline address frame::interpreter_bcp(const intptr_t* fp) {
43 assert(fp != nullptr, "invariant");
44 return reinterpret_cast<address>(fp[frame::interpreter_frame_bcp_offset]);
45 }
46
47 inline address frame::interpreter_return_address(const intptr_t* fp) {
48 assert(fp != nullptr, "invariant");
49 return reinterpret_cast<address>(fp[frame::return_addr_offset]);
50 }
51
52 inline intptr_t* frame::interpreter_sender_sp(const intptr_t* fp) {
53 assert(fp != nullptr, "invariant");
54 return reinterpret_cast<intptr_t*>(fp[frame::interpreter_frame_sender_sp_offset]);
55 }
56
57 inline bool frame::is_interpreter_frame_setup_at(const intptr_t* fp, const void* sp) {
58 assert(fp != nullptr, "invariant");
59 assert(sp != nullptr, "invariant");
60 return sp <= fp + frame::interpreter_frame_initial_sp_offset;
61 }
62
63 inline intptr_t* frame::sender_sp(intptr_t* fp) {
64 assert(fp != nullptr, "invariant");
65 return fp + frame::sender_sp_offset;
66 }
67
68 inline intptr_t* frame::link(const intptr_t* fp) {
69 assert(fp != nullptr, "invariant");
70 return reinterpret_cast<intptr_t*>(fp[frame::link_offset]);
71 }
72
73 inline address frame::return_address(const intptr_t* sp) {
74 assert(sp != nullptr, "invariant");
75 return reinterpret_cast<address>(sp[-1]);
76 }
77
78 inline intptr_t* frame::fp(const intptr_t* sp) {
79 assert(sp != nullptr, "invariant");
80 return reinterpret_cast<intptr_t*>(sp[-2]);
81 }
82
83 #endif // INCLUDE_JFR
84
85 // Constructors:
86
87 inline frame::frame() {
88 _pc = nullptr;
89 _sp = nullptr;
90 _unextended_sp = nullptr;
91 _fp = nullptr;
92 _cb = nullptr;
93 _deopt_state = unknown;
94 _oop_map = nullptr;
95 _on_heap = false;
96 DEBUG_ONLY(_frame_index = -1;)
97 }
98
99 inline void frame::init(intptr_t* sp, intptr_t* fp, address pc) {
100 _sp = sp;
101 _unextended_sp = sp;
102 _fp = fp;
103 _pc = pc;
104 _oop_map = nullptr;
105 _on_heap = false;
106 DEBUG_ONLY(_frame_index = -1;)
107
108 assert(pc != nullptr, "no pc?");
109 _cb = CodeCache::find_blob(pc); // not fast because this constructor can be used on native frames
110 setup(pc);
111 }
112
113 inline void frame::setup(address pc) {
114 address original_pc = get_deopt_original_pc();
115 if (original_pc != nullptr) {
116 _pc = original_pc;
117 _deopt_state = is_deoptimized;
118 assert(_cb == nullptr || _cb->as_nmethod()->insts_contains_inclusive(_pc),
119 "original PC must be in the main code section of the compiled method (or must be immediately following it)");
120 } else {
121 if (_cb == SharedRuntime::deopt_blob()) {
122 _deopt_state = is_deoptimized;
123 } else {
124 _deopt_state = not_deoptimized;
125 }
126 }
127 }
128
129 inline frame::frame(intptr_t* sp, intptr_t* fp, address pc) {
130 init(sp, fp, pc);
131 }
132
133 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb) {
134 _sp = sp;
135 _unextended_sp = unextended_sp;
136 _fp = fp;
137 _pc = pc;
138 assert(pc != nullptr, "no pc?");
139 _cb = cb;
140 _oop_map = nullptr;
141 assert(_cb != nullptr, "pc: " INTPTR_FORMAT, p2i(pc));
142 _on_heap = false;
143 DEBUG_ONLY(_frame_index = -1;)
144
145 setup(pc);
146 }
147
148 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb,
149 const ImmutableOopMap* oop_map, bool on_heap) {
150 _sp = sp;
151 _unextended_sp = unextended_sp;
152 _fp = fp;
153 _pc = pc;
154 _cb = cb;
155 _oop_map = oop_map;
156 _deopt_state = not_deoptimized;
157 _on_heap = on_heap;
158 DEBUG_ONLY(_frame_index = -1;)
159
160 // In thaw, non-heap frames use this constructor to pass oop_map. I don't know why.
161 assert(_on_heap || _cb != nullptr, "these frames are always heap frames");
162 if (cb != nullptr) {
163 setup(pc);
164 }
165 #ifdef ASSERT
166 // The following assertion has been disabled because it would sometime trap for Continuation.run,
167 // which is not *in* a continuation and therefore does not clear the _cont_fastpath flag, but this
168 // is benign even in fast mode (see Freeze::setup_jump)
169 // We might freeze deoptimized frame in slow mode
170 // assert(_pc == pc && _deopt_state == not_deoptimized, "");
171 #endif
172 }
173
174 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc) {
175 _sp = sp;
176 _unextended_sp = unextended_sp;
177 _fp = fp;
178 _pc = pc;
179 assert(pc != nullptr, "no pc?");
180 _cb = CodeCache::find_blob_fast(pc);
181 _oop_map = nullptr;
182 assert(_cb != nullptr, "pc: " INTPTR_FORMAT " sp: " INTPTR_FORMAT " unextended_sp: " INTPTR_FORMAT " fp: " INTPTR_FORMAT, p2i(pc), p2i(sp), p2i(unextended_sp), p2i(fp));
183 _on_heap = false;
184 DEBUG_ONLY(_frame_index = -1;)
185
186 setup(pc);
187 }
188
189 inline frame::frame(intptr_t* sp) : frame(sp, sp, *(intptr_t**)(sp - frame::sender_sp_offset), *(address*)(sp - 1)) {}
190
191 inline frame::frame(intptr_t* sp, intptr_t* fp) {
192 _sp = sp;
193 _unextended_sp = sp;
194 _fp = fp;
195 _pc = (address)(sp[-1]);
196 _on_heap = false;
197 DEBUG_ONLY(_frame_index = -1;)
198
199 // Here's a sticky one. This constructor can be called via AsyncGetCallTrace
200 // when last_Java_sp is non-null but the pc fetched is junk.
201 // AsyncGetCallTrace -> pd_get_top_frame_for_signal_handler
202 // -> pd_last_frame should use a specialized version of pd_last_frame which could
203 // call a specialized frame constructor instead of this one.
204 // Then we could use the assert below. However this assert is of somewhat dubious
205 // value.
206 // UPDATE: this constructor is only used by trace_method_handle_stub() now.
207 // assert(_pc != nullptr, "no pc?");
208
209 _cb = CodeCache::find_blob(_pc);
210
211 address original_pc = get_deopt_original_pc();
212 if (original_pc != nullptr) {
213 _pc = original_pc;
214 _deopt_state = is_deoptimized;
215 } else {
216 _deopt_state = not_deoptimized;
217 }
218 _oop_map = nullptr;
219 }
220
221 // Accessors
222
223 inline bool frame::equal(frame other) const {
224 bool ret = sp() == other.sp()
225 && unextended_sp() == other.unextended_sp()
226 && fp() == other.fp()
227 && pc() == other.pc();
228 assert(!ret || (ret && cb() == other.cb() && _deopt_state == other._deopt_state), "inconsistent construction");
229 return ret;
230 }
231
232 // Return unique id for this frame. The id must have a value where we can distinguish
233 // identity and younger/older relationship. null represents an invalid (incomparable)
234 // frame.
235 inline intptr_t* frame::id(void) const { return unextended_sp(); }
236
237 // Return true if the frame is older (less recent activation) than the frame represented by id
238 inline bool frame::is_older(intptr_t* id) const { assert(this->id() != nullptr && id != nullptr, "null frame id");
239 return this->id() > id ; }
240
241 inline intptr_t* frame::link() const { return *(intptr_t **)addr_at(link_offset); }
242
243 inline intptr_t* frame::link_or_null() const {
244 intptr_t** ptr = (intptr_t **)addr_at(link_offset);
245 return os::is_readable_pointer(ptr) ? *ptr : nullptr;
246 }
247
248 inline intptr_t* frame::unextended_sp() const { assert_absolute(); return _unextended_sp; }
249 inline void frame::set_unextended_sp(intptr_t* value) { _unextended_sp = value; }
250 inline int frame::offset_unextended_sp() const { assert_offset(); return _offset_unextended_sp; }
251 inline void frame::set_offset_unextended_sp(int value) { assert_on_heap(); _offset_unextended_sp = value; }
252
253 inline intptr_t* frame::real_fp() const {
254 if (_cb != nullptr) {
255 // use the frame size if valid
256 int size = _cb->frame_size();
257 if (size > 0) {
258 return unextended_sp() + size;
259 }
260 }
261 // else rely on fp()
262 assert(! is_compiled_frame(), "unknown compiled frame size");
263 return fp();
264 }
265
266 inline int frame::frame_size() const {
267 return is_interpreted_frame()
268 ? pointer_delta_as_int(sender_sp(), sp())
269 : cb()->frame_size();
270 }
271
272 inline int frame::compiled_frame_stack_argsize() const {
273 assert(cb()->is_nmethod(), "");
274 return (cb()->as_nmethod()->num_stack_arg_slots() * VMRegImpl::stack_slot_size) >> LogBytesPerWord;
275 }
276
277 inline void frame::interpreted_frame_oop_map(InterpreterOopMap* mask) const {
278 assert(mask != nullptr, "");
279 Method* m = interpreter_frame_method();
280 int bci = interpreter_frame_bci();
281 m->mask_for(bci, mask); // OopMapCache::compute_one_oop_map(m, bci, mask);
282 }
283
284 // Return address:
285
286 inline address* frame::sender_pc_addr() const { return (address*) addr_at(return_addr_offset); }
287 inline address frame::sender_pc() const { return *sender_pc_addr(); }
288
289 inline intptr_t* frame::sender_sp() const { return addr_at(sender_sp_offset); }
290
291 inline intptr_t* frame::interpreter_frame_locals() const {
292 intptr_t n = *addr_at(interpreter_frame_locals_offset);
293 return &fp()[n]; // return relativized locals
294 }
295
296 inline intptr_t* frame::interpreter_frame_last_sp() const {
297 intptr_t n = *addr_at(interpreter_frame_last_sp_offset);
298 assert(n <= 0, "n: " INTPTR_FORMAT, n);
299 return n != 0 ? &fp()[n] : nullptr;
300 }
301
302 inline intptr_t* frame::interpreter_frame_bcp_addr() const {
303 return (intptr_t*)addr_at(interpreter_frame_bcp_offset);
304 }
305
306 inline intptr_t* frame::interpreter_frame_mdp_addr() const {
307 return (intptr_t*)addr_at(interpreter_frame_mdp_offset);
308 }
309
310
311
312 // Constant pool cache
313
314 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
315 return (ConstantPoolCache**)addr_at(interpreter_frame_cache_offset);
316 }
317
318 // Method
319
320 inline Method** frame::interpreter_frame_method_addr() const {
321 return (Method**)addr_at(interpreter_frame_method_offset);
322 }
323
324 // Mirror
325
326 inline oop* frame::interpreter_frame_mirror_addr() const {
327 return (oop*)addr_at(interpreter_frame_mirror_offset);
328 }
329
330 // top of expression stack
331 inline intptr_t* frame::interpreter_frame_tos_address() const {
332 intptr_t* last_sp = interpreter_frame_last_sp();
333 if (last_sp == nullptr) {
334 return sp();
335 } else {
336 // sp() may have been extended or shrunk by an adapter. At least
337 // check that we don't fall behind the legal region.
338 // For top deoptimized frame last_sp == interpreter_frame_monitor_end.
339 assert(last_sp <= (intptr_t*) interpreter_frame_monitor_end(), "bad tos");
340 return last_sp;
341 }
342 }
343
344 inline oop* frame::interpreter_frame_temp_oop_addr() const {
345 return (oop *)(fp() + interpreter_frame_oop_temp_offset);
346 }
347
348 inline int frame::interpreter_frame_monitor_size() {
349 return BasicObjectLock::size();
350 }
351
352
353 // expression stack
354 // (the max_stack arguments are used by the GC; see class FrameClosure)
355
356 inline intptr_t* frame::interpreter_frame_expression_stack() const {
357 intptr_t* monitor_end = (intptr_t*) interpreter_frame_monitor_end();
358 return monitor_end-1;
359 }
360
361 // Entry frames
362
363 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
364 return (JavaCallWrapper**)addr_at(entry_frame_call_wrapper_offset);
365 }
366
367 // Compiled frames
368
369 inline oop frame::saved_oop_result(RegisterMap* map) const {
370 oop* result_adr = (oop *)map->location(rax->as_VMReg(), sp());
371 guarantee(result_adr != nullptr, "bad register save location");
372 return *result_adr;
373 }
374
375 inline void frame::set_saved_oop_result(RegisterMap* map, oop obj) {
376 oop* result_adr = (oop *)map->location(rax->as_VMReg(), sp());
377 guarantee(result_adr != nullptr, "bad register save location");
378
379 *result_adr = obj;
380 }
381
382 inline bool frame::is_interpreted_frame() const {
383 return Interpreter::contains(pc());
384 }
385
386 inline int frame::sender_sp_ret_address_offset() {
387 return frame::sender_sp_offset - frame::return_addr_offset;
388 }
389
390 //------------------------------------------------------------------------------
391 // frame::sender
392
393 inline frame frame::sender(RegisterMap* map) const {
394 frame result = sender_raw(map);
395
396 if (map->process_frames() && !map->in_cont()) {
397 StackWatermarkSet::on_iteration(map->thread(), result);
398 }
399
400 return result;
401 }
402
403 inline frame frame::sender_raw(RegisterMap* map) const {
404 // Default is we done have to follow them. The sender_for_xxx will
405 // update it accordingly
406 map->set_include_argument_oops(false);
407
408 if (map->in_cont()) { // already in an h-stack
409 return map->stack_chunk()->sender(*this, map);
410 }
411
412 if (is_entry_frame()) return sender_for_entry_frame(map);
413 if (is_upcall_stub_frame()) return sender_for_upcall_stub_frame(map);
414 if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
415
416 assert(_cb == CodeCache::find_blob(pc()), "Must be the same");
417 if (_cb != nullptr) return sender_for_compiled_frame(map);
418
419 // Must be native-compiled frame, i.e. the marshaling code for native
420 // methods that exists in the core system.
421 return frame(sender_sp(), link(), sender_pc());
422 }
423
424 inline frame frame::sender_for_compiled_frame(RegisterMap* map) const {
425 assert(map != nullptr, "map must be set");
426
427 // frame owned by optimizing compiler
428 assert(_cb->frame_size() > 0, "must have non-zero frame size");
429 intptr_t* sender_sp = unextended_sp() + _cb->frame_size();
430 assert(sender_sp == real_fp(), "");
431
432 // On Intel the return_address is always the word on the stack
433 address sender_pc = (address) *(sender_sp-1);
434
435 // This is the saved value of EBP which may or may not really be an FP.
436 // It is only an FP if the sender is an interpreter frame (or C1?).
437 // saved_fp_addr should be correct even for a bottom thawed frame (with a return barrier)
438 intptr_t** saved_fp_addr = (intptr_t**) (sender_sp - frame::sender_sp_offset);
439
440 if (map->update_map()) {
441 // Tell GC to use argument oopmaps for some runtime stubs that need it.
442 // For C1, the runtime stub might not have oop maps, so set this flag
443 // outside of update_register_map.
444 if (!_cb->is_nmethod()) { // compiled frames do not use callee-saved registers
445 map->set_include_argument_oops(_cb->caller_must_gc_arguments(map->thread()));
446 if (oop_map() != nullptr) {
447 _oop_map->update_register_map(this, map);
448 }
449 } else {
450 assert(!_cb->caller_must_gc_arguments(map->thread()), "");
451 assert(!map->include_argument_oops(), "");
452 assert(oop_map() == nullptr || !oop_map()->has_any(OopMapValue::callee_saved_value), "callee-saved value in compiled frame");
453 }
454
455 // Since the prolog does the save and restore of EBP there is no oopmap
456 // for it so we must fill in its location as if there was an oopmap entry
457 // since if our caller was compiled code there could be live jvm state in it.
458 update_map_with_saved_link(map, saved_fp_addr);
459 }
460
461 assert(sender_sp != sp(), "must have changed");
462
463 if (Continuation::is_return_barrier_entry(sender_pc)) {
464 if (map->walk_cont()) { // about to walk into an h-stack
465 return Continuation::top_frame(*this, map);
466 } else {
467 return Continuation::continuation_bottom_sender(map->thread(), *this, sender_sp);
468 }
469 }
470
471 intptr_t* unextended_sp = sender_sp;
472 return frame(sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
473 }
474
475 template <typename RegisterMapT>
476 void frame::update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr) {
477 // The interpreter and compiler(s) always save EBP/RBP in a known
478 // location on entry. We must record where that location is
479 // so this if EBP/RBP was live on callout from c2 we can find
480 // the saved copy no matter what it called.
481
482 // Since the interpreter always saves EBP/RBP if we record where it is then
483 // we don't have to always save EBP/RBP on entry and exit to c2 compiled
484 // code, on entry will be enough.
485 map->set_location(rbp->as_VMReg(), (address) link_addr);
486 #ifdef AMD64
487 // this is weird "H" ought to be at a higher address however the
488 // oopMaps seems to have the "H" regs at the same address and the
489 // vanilla register.
490 // XXXX make this go away
491 if (true) {
492 map->set_location(rbp->as_VMReg()->next(), (address) link_addr);
493 }
494 #endif // AMD64
495 }
496 #endif // CPU_X86_FRAME_X86_INLINE_HPP