1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2014, Red Hat Inc. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #ifndef CPU_AARCH64_FRAME_AARCH64_INLINE_HPP
27 #define CPU_AARCH64_FRAME_AARCH64_INLINE_HPP
28
29 #include "code/codeBlob.inline.hpp"
30 #include "code/codeCache.inline.hpp"
31 #include "code/vmreg.inline.hpp"
32 #include "interpreter/interpreter.hpp"
33 #include "runtime/sharedRuntime.hpp"
34 #include "pauth_aarch64.hpp"
35 #ifdef COMPILER1
36 #include "c1/c1_Runtime1.hpp"
37 #endif
38
39 // Inline functions for AArch64 frames:
40
41 #if INCLUDE_JFR
42
43 // Static helper routines
44
45 inline address frame::interpreter_bcp(const intptr_t* fp) {
46 assert(fp != nullptr, "invariant");
47 return reinterpret_cast<address>(fp[frame::interpreter_frame_bcp_offset]);
48 }
49
50 inline address frame::interpreter_return_address(const intptr_t* fp) {
51 assert(fp != nullptr, "invariant");
52 return reinterpret_cast<address>(fp[frame::return_addr_offset]);
53 }
54
55 inline intptr_t* frame::interpreter_sender_sp(const intptr_t* fp) {
56 assert(fp != nullptr, "invariant");
57 return reinterpret_cast<intptr_t*>(fp[frame::interpreter_frame_sender_sp_offset]);
58 }
59
60 inline bool frame::is_interpreter_frame_setup_at(const intptr_t* fp, const void* sp) {
61 assert(fp != nullptr, "invariant");
62 assert(sp != nullptr, "invariant");
63 return sp <= fp + frame::interpreter_frame_initial_sp_offset;
64 }
65
66 inline intptr_t* frame::sender_sp(intptr_t* fp) {
67 assert(fp != nullptr, "invariant");
68 return fp + frame::sender_sp_offset;
69 }
70
71 inline intptr_t* frame::link(const intptr_t* fp) {
72 assert(fp != nullptr, "invariant");
73 return reinterpret_cast<intptr_t*>(fp[frame::link_offset]);
74 }
75
76 inline address frame::return_address(const intptr_t* sp) {
77 assert(sp != nullptr, "invariant");
78 return reinterpret_cast<address>(sp[-1]);
79 }
80
81 inline intptr_t* frame::fp(const intptr_t* sp) {
82 assert(sp != nullptr, "invariant");
83 return reinterpret_cast<intptr_t*>(sp[-2]);
84 }
85
86 #endif // INCLUDE_JFR
87
88 // Constructors:
89
90 inline frame::frame() {
91 _pc = nullptr;
92 _sp = nullptr;
93 _unextended_sp = nullptr;
94 _fp = nullptr;
95 _cb = nullptr;
96 _deopt_state = unknown;
97 _sp_is_trusted = false;
98 _on_heap = false;
99 DEBUG_ONLY(_frame_index = -1;)
100 }
101
102 static int spin;
103
104 inline void frame::init(intptr_t* sp, intptr_t* fp, address pc) {
105 assert(pauth_ptr_is_raw(pc), "cannot be signed");
106 intptr_t a = intptr_t(sp);
107 intptr_t b = intptr_t(fp);
108 _sp = sp;
109 _unextended_sp = sp;
110 _fp = fp;
111 _pc = pc;
112 _oop_map = nullptr;
113 _on_heap = false;
114 DEBUG_ONLY(_frame_index = -1;)
115
116 assert(pc != nullptr, "no pc?");
117 _cb = CodeCache::find_blob(pc);
118 setup(pc);
119 }
120
121 inline void frame::setup(address pc) {
122 adjust_unextended_sp();
123
124 address original_pc = get_deopt_original_pc();
125 if (original_pc != nullptr) {
126 _pc = original_pc;
127 _deopt_state = is_deoptimized;
128 assert(_cb == nullptr || _cb->as_nmethod()->insts_contains_inclusive(_pc),
129 "original PC must be in the main code section of the compiled method (or must be immediately following it)");
130 } else {
131 if (_cb == SharedRuntime::deopt_blob()) {
132 _deopt_state = is_deoptimized;
133 } else {
134 _deopt_state = not_deoptimized;
135 }
136 }
137 _sp_is_trusted = false;
138 }
139
140 inline frame::frame(intptr_t* sp, intptr_t* fp, address pc) {
141 init(sp, fp, pc);
142 }
143
144 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, bool allow_cb_null) {
145 assert(pauth_ptr_is_raw(pc), "cannot be signed");
146 intptr_t a = intptr_t(sp);
147 intptr_t b = intptr_t(fp);
148 _sp = sp;
149 _unextended_sp = unextended_sp;
150 _fp = fp;
151 _pc = pc;
152 assert(pc != nullptr, "no pc?");
153 _cb = cb;
154 _oop_map = nullptr;
155 assert(_cb != nullptr || allow_cb_null, "pc: " INTPTR_FORMAT, p2i(pc));
156 _on_heap = false;
157 DEBUG_ONLY(_frame_index = -1;)
158
159 setup(pc);
160 }
161
162 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map, bool on_heap) {
163 _sp = sp;
164 _unextended_sp = unextended_sp;
165 _fp = fp;
166 _pc = pc;
167 _cb = cb;
168 _oop_map = oop_map;
169 _deopt_state = not_deoptimized;
170 _sp_is_trusted = false;
171 _on_heap = on_heap;
172 DEBUG_ONLY(_frame_index = -1;)
173
174 // In thaw, non-heap frames use this constructor to pass oop_map. I don't know why.
175 assert(_on_heap || _cb != nullptr, "these frames are always heap frames");
176 if (cb != nullptr) {
177 setup(pc);
178 }
179 #ifdef ASSERT
180 // The following assertion has been disabled because it would sometime trap for Continuation.run,
181 // which is not *in* a continuation and therefore does not clear the _cont_fastpath flag, but this
182 // is benign even in fast mode (see Freeze::setup_jump)
183 // We might freeze deoptimized frame in slow mode
184 // assert(_pc == pc && _deopt_state == not_deoptimized, "");
185 #endif
186 }
187
188 inline frame::frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc) {
189 intptr_t a = intptr_t(sp);
190 intptr_t b = intptr_t(fp);
191 _sp = sp;
192 _unextended_sp = unextended_sp;
193 _fp = fp;
194 _pc = pc;
195 _cb = CodeCache::find_blob_fast(pc);
196 _oop_map = nullptr;
197 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));
198 _on_heap = false;
199 DEBUG_ONLY(_frame_index = -1;)
200
201 setup(pc);
202 }
203
204 inline frame::frame(intptr_t* sp)
205 : frame(sp, sp,
206 *(intptr_t**)(sp - frame::sender_sp_offset),
207 pauth_strip_verifiable(*(address*)(sp - 1))) {}
208
209 inline frame::frame(intptr_t* sp, intptr_t* fp) {
210 intptr_t a = intptr_t(sp);
211 intptr_t b = intptr_t(fp);
212 _sp = sp;
213 _unextended_sp = sp;
214 _fp = fp;
215 _pc = (address)(sp[-1]);
216 _on_heap = false;
217 DEBUG_ONLY(_frame_index = -1;)
218
219 // Here's a sticky one. This constructor can be called via AsyncGetCallTrace
220 // when last_Java_sp is non-null but the pc fetched is junk.
221 // AsyncGetCallTrace -> pd_get_top_frame_for_signal_handler
222 // -> pd_last_frame should use a specialized version of pd_last_frame which could
223 // call a specilaized frame constructor instead of this one.
224 // Then we could use the assert below. However this assert is of somewhat dubious
225 // value.
226 // assert(_pc != nullptr, "no pc?");
227
228 _cb = CodeCache::find_blob(_pc);
229 adjust_unextended_sp();
230
231 address original_pc = get_deopt_original_pc();
232 if (original_pc != nullptr) {
233 _pc = original_pc;
234 _deopt_state = is_deoptimized;
235 } else {
236 _deopt_state = not_deoptimized;
237 }
238 _sp_is_trusted = false;
239 }
240
241 // Accessors
242
243 inline bool frame::equal(frame other) const {
244 bool ret = sp() == other.sp()
245 && unextended_sp() == other.unextended_sp()
246 && fp() == other.fp()
247 && pc() == other.pc();
248 assert(!ret || (cb() == other.cb() && _deopt_state == other._deopt_state), "inconsistent construction");
249 return ret;
250 }
251
252 // Return unique id for this frame. The id must have a value where we can distinguish
253 // identity and younger/older relationship. null represents an invalid (incomparable)
254 // frame.
255 inline intptr_t* frame::id(void) const { return unextended_sp(); }
256
257 // Return true if the frame is older (less recent activation) than the frame represented by id
258 inline bool frame::is_older(intptr_t* id) const { assert(this->id() != nullptr && id != nullptr, "null frame id");
259 return this->id() > id ; }
260
261 inline intptr_t* frame::link() const { return (intptr_t*) *(intptr_t **)addr_at(link_offset); }
262
263 inline intptr_t* frame::link_or_null() const {
264 intptr_t** ptr = (intptr_t **)addr_at(link_offset);
265 return os::is_readable_pointer(ptr) ? *ptr : nullptr;
266 }
267
268 inline intptr_t* frame::unextended_sp() const { assert_absolute(); return _unextended_sp; }
269 inline void frame::set_unextended_sp(intptr_t* value) { _unextended_sp = value; }
270 inline int frame::offset_unextended_sp() const { assert_offset(); return _offset_unextended_sp; }
271 inline void frame::set_offset_unextended_sp(int value) { assert_on_heap(); _offset_unextended_sp = value; }
272
273 inline intptr_t* frame::real_fp() const {
274 if (_cb != nullptr) {
275 // use the frame size if valid
276 int size = _cb->frame_size();
277 if (size > 0) {
278 return unextended_sp() + size;
279 }
280 }
281 // else rely on fp()
282 assert(! is_compiled_frame(), "unknown compiled frame size");
283 return fp();
284 }
285
286 inline int frame::frame_size() const {
287 return is_interpreted_frame()
288 ? pointer_delta_as_int(sender_sp(), sp())
289 : cb()->frame_size();
290 }
291
292 inline int frame::compiled_frame_stack_argsize() const {
293 assert(cb()->is_nmethod(), "");
294 return (cb()->as_nmethod()->num_stack_arg_slots() * VMRegImpl::stack_slot_size) >> LogBytesPerWord;
295 }
296
297 inline void frame::interpreted_frame_oop_map(InterpreterOopMap* mask) const {
298 assert(mask != nullptr, "");
299 Method* m = interpreter_frame_method();
300 int bci = interpreter_frame_bci();
301 m->mask_for(bci, mask); // OopMapCache::compute_one_oop_map(m, bci, mask);
302 }
303
304 // Return address:
305
306 inline address* frame::sender_pc_addr() const { return (address*) addr_at( return_addr_offset); }
307 inline address frame::sender_pc_maybe_signed() const { return *sender_pc_addr(); }
308 inline address frame::sender_pc() const { return pauth_strip_pointer(sender_pc_maybe_signed()); }
309
310 inline intptr_t* frame::sender_sp() const { return addr_at( sender_sp_offset); }
311
312 inline intptr_t* frame::interpreter_frame_locals() const {
313 intptr_t n = *addr_at(interpreter_frame_locals_offset);
314 return &fp()[n]; // return relativized locals
315 }
316
317 inline intptr_t* frame::interpreter_frame_last_sp() const {
318 intptr_t n = *addr_at(interpreter_frame_last_sp_offset);
319 assert(n <= 0, "n: " INTPTR_FORMAT, n);
320 return n != 0 ? &fp()[n] : nullptr;
321 }
322
323 inline intptr_t* frame::interpreter_frame_bcp_addr() const {
324 return (intptr_t*)addr_at(interpreter_frame_bcp_offset);
325 }
326
327 inline intptr_t* frame::interpreter_frame_mdp_addr() const {
328 return (intptr_t*)addr_at(interpreter_frame_mdp_offset);
329 }
330
331
332 // Constant pool cache
333
334 inline ConstantPoolCache** frame::interpreter_frame_cache_addr() const {
335 return (ConstantPoolCache**)addr_at(interpreter_frame_cache_offset);
336 }
337
338 // Method
339
340 inline Method** frame::interpreter_frame_method_addr() const {
341 return (Method**)addr_at(interpreter_frame_method_offset);
342 }
343
344 // Mirror
345
346 inline oop* frame::interpreter_frame_mirror_addr() const {
347 return (oop*)addr_at(interpreter_frame_mirror_offset);
348 }
349
350 // top of expression stack
351 inline intptr_t* frame::interpreter_frame_tos_address() const {
352 intptr_t* last_sp = interpreter_frame_last_sp();
353 if (last_sp == nullptr) {
354 return sp();
355 } else {
356 // sp() may have been extended or shrunk by an adapter. At least
357 // check that we don't fall behind the legal region.
358 // For top deoptimized frame last_sp == interpreter_frame_monitor_end.
359 assert(last_sp <= (intptr_t*) interpreter_frame_monitor_end(), "bad tos");
360 return last_sp;
361 }
362 }
363
364 inline oop* frame::interpreter_frame_temp_oop_addr() const {
365 return (oop *)(fp() + interpreter_frame_oop_temp_offset);
366 }
367
368 inline int frame::interpreter_frame_monitor_size() {
369 return BasicObjectLock::size();
370 }
371
372
373 // expression stack
374 // (the max_stack arguments are used by the GC; see class FrameClosure)
375
376 inline intptr_t* frame::interpreter_frame_expression_stack() const {
377 intptr_t* monitor_end = (intptr_t*) interpreter_frame_monitor_end();
378 return monitor_end-1;
379 }
380
381
382 // Entry frames
383
384 inline JavaCallWrapper** frame::entry_frame_call_wrapper_addr() const {
385 return (JavaCallWrapper**)addr_at(entry_frame_call_wrapper_offset);
386 }
387
388
389 // Compiled frames
390
391 inline oop frame::saved_oop_result(RegisterMap* map) const {
392 oop* result_adr = (oop *)map->location(r0->as_VMReg(), sp());
393 guarantee(result_adr != nullptr, "bad register save location");
394 return *result_adr;
395 }
396
397 inline void frame::set_saved_oop_result(RegisterMap* map, oop obj) {
398 oop* result_adr = (oop *)map->location(r0->as_VMReg(), sp());
399 guarantee(result_adr != nullptr, "bad register save location");
400
401 *result_adr = obj;
402 }
403
404 inline bool frame::is_interpreted_frame() const {
405 return Interpreter::contains(pc());
406 }
407
408 inline int frame::sender_sp_ret_address_offset() {
409 return frame::sender_sp_offset - frame::return_addr_offset;
410 }
411
412 //------------------------------------------------------------------------------
413 // frame::sender
414 inline frame frame::sender(RegisterMap* map) const {
415 frame result = sender_raw(map);
416
417 if (map->process_frames() && !map->in_cont()) {
418 StackWatermarkSet::on_iteration(map->thread(), result);
419 }
420
421 return result;
422 }
423
424 inline frame frame::sender_raw(RegisterMap* map) const {
425 // Default is we done have to follow them. The sender_for_xxx will
426 // update it accordingly
427 map->set_include_argument_oops(false);
428
429 if (map->in_cont()) { // already in an h-stack
430 return map->stack_chunk()->sender(*this, map);
431 }
432
433 if (is_entry_frame()) return sender_for_entry_frame(map);
434 if (is_upcall_stub_frame()) return sender_for_upcall_stub_frame(map);
435 if (is_interpreted_frame()) return sender_for_interpreter_frame(map);
436
437 assert(_cb == CodeCache::find_blob(pc()), "Must be the same");
438 if (_cb != nullptr) return sender_for_compiled_frame(map);
439
440 // Must be native-compiled frame, i.e. the marshaling code for native
441 // methods that exists in the core system.
442
443 // Native code may or may not have signed the return address, we have no way to be sure or what
444 // signing methods they used. Instead, just ensure the stripped value is used.
445
446 return frame(sender_sp(), link(), sender_pc());
447 }
448
449 inline frame frame::sender_for_compiled_frame(RegisterMap* map) const {
450 // we cannot rely upon the last fp having been saved to the thread
451 // in C2 code but it will have been pushed onto the stack. so we
452 // have to find it relative to the unextended sp
453
454 assert(_cb->frame_size() > 0, "must have non-zero frame size");
455 intptr_t* l_sender_sp = (!PreserveFramePointer || _sp_is_trusted) ? unextended_sp() + _cb->frame_size()
456 : sender_sp();
457 #ifdef ASSERT
458 address sender_pc_copy = pauth_strip_verifiable((address) *(l_sender_sp-1));
459 #endif
460
461 assert(!_sp_is_trusted || l_sender_sp == real_fp(), "");
462
463 intptr_t** saved_fp_addr = (intptr_t**) (l_sender_sp - frame::sender_sp_offset);
464
465 // Repair the sender sp if the frame has been extended
466 l_sender_sp = repair_sender_sp(l_sender_sp, saved_fp_addr);
467
468 // The return_address is always the word on the stack.
469 // For ROP protection, C1/C2 will have signed the sender_pc,
470 // but there is no requirement to authenticate it here.
471 address sender_pc = pauth_strip_verifiable((address) *(l_sender_sp - 1));
472
473 #ifdef ASSERT
474 if (sender_pc != sender_pc_copy) {
475 // When extending the stack in the callee method entry to make room for unpacking of value
476 // type args, we keep a copy of the sender pc at the expected location in the callee frame.
477 // If the sender pc is patched due to deoptimization, the copy is not consistent anymore.
478 nmethod* nm = CodeCache::find_blob(sender_pc)->as_nmethod();
479 assert(sender_pc == nm->deopt_mh_handler_begin() || sender_pc == nm->deopt_handler_begin(), "unexpected sender pc");
480 }
481 #endif
482
483 if (map->update_map()) {
484 // Tell GC to use argument oopmaps for some runtime stubs that need it.
485 // For C1, the runtime stub might not have oop maps, so set this flag
486 // outside of update_register_map.
487 bool c1_buffering = false;
488 #ifdef COMPILER1
489 nmethod* nm = _cb->as_nmethod_or_null();
490 if (nm != nullptr && nm->is_compiled_by_c1() && nm->method()->has_scalarized_args() &&
491 pc() < nm->verified_inline_entry_point()) {
492 // TODO 8284443 Can't we do that by not passing 'dont_gc_arguments' in case 'StubId::c1_buffer_inline_args_id' in 'Runtime1::generate_code_for'?
493 // The VEP and VIEP(RO) of C1-compiled methods call buffer_inline_args_xxx
494 // before doing any argument shuffling, so we need to scan the oops
495 // as the caller passes them.
496 c1_buffering = true;
497 }
498 #endif
499 if (!_cb->is_nmethod() || c1_buffering) { // compiled frames do not use callee-saved registers
500 bool caller_args = _cb->caller_must_gc_arguments(map->thread()) || c1_buffering;
501 map->set_include_argument_oops(caller_args);
502 if (oop_map() != nullptr) {
503 _oop_map->update_register_map(this, map);
504 }
505 } else {
506 assert(!_cb->caller_must_gc_arguments(map->thread()), "");
507 assert(!map->include_argument_oops(), "");
508 assert(oop_map() == nullptr || !oop_map()->has_any(OopMapValue::callee_saved_value), "callee-saved value in compiled frame");
509 }
510
511 // Since the prolog does the save and restore of FP there is no oopmap
512 // for it so we must fill in its location as if there was an oopmap entry
513 // since if our caller was compiled code there could be live jvm state in it.
514 update_map_with_saved_link(map, saved_fp_addr);
515 }
516
517 if (Continuation::is_return_barrier_entry(sender_pc)) {
518 if (map->walk_cont()) { // about to walk into an h-stack
519 return Continuation::top_frame(*this, map);
520 } else {
521 return Continuation::continuation_bottom_sender(map->thread(), *this, l_sender_sp);
522 }
523 }
524
525 intptr_t* unextended_sp = l_sender_sp;
526 return frame(l_sender_sp, unextended_sp, *saved_fp_addr, sender_pc);
527 }
528
529 template <typename RegisterMapT>
530 void frame::update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr) {
531 // The interpreter and compiler(s) always save FP in a known
532 // location on entry. C2-compiled code uses FP as an allocatable
533 // callee-saved register. We must record where that location is so
534 // that if FP was live on callout from c2 we can find the saved copy.
535
536 map->set_location(rfp->as_VMReg(), (address) link_addr);
537 // this is weird "H" ought to be at a higher address however the
538 // oopMaps seems to have the "H" regs at the same address and the
539 // vanilla register.
540 // XXXX make this go away
541 if (true) {
542 map->set_location(rfp->as_VMReg()->next(), (address) link_addr);
543 }
544 }
545 #endif // CPU_AARCH64_FRAME_AARCH64_INLINE_HPP