1 /*
  2  * Copyright (c) 2022, 2023, 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_CONTINUATIONHELPER_X86_INLINE_HPP
 26 #define CPU_X86_CONTINUATIONHELPER_X86_INLINE_HPP
 27 
 28 #include "runtime/continuationHelper.hpp"
 29 
 30 #include "runtime/continuationEntry.inline.hpp"
 31 #include "runtime/frame.inline.hpp"
 32 #include "runtime/registerMap.hpp"
 33 #include "utilities/macros.hpp"
 34 
 35 template<typename FKind>
 36 static inline intptr_t** link_address(const frame& f) {
 37   assert(FKind::is_instance(f), "");
 38   return FKind::interpreted
 39     ? (intptr_t**)(f.fp() + frame::link_offset)
 40     : (intptr_t**)(f.unextended_sp() + f.cb()->frame_size() - frame::sender_sp_offset);
 41 }
 42 
 43 static inline void patch_return_pc_with_preempt_stub(frame& f) {
 44   // Patch the pc of the now old last Java frame (we already set the anchor to enterSpecial)
 45   // so that when target goes back to Java it will actually return to the preempt cleanup stub.
 46   intptr_t* sp = f.sp();
 47   sp[-1] = (intptr_t)StubRoutines::cont_preempt_stub();
 48 }
 49 
 50 inline int ContinuationHelper::frame_align_words(int size) {
 51 #ifdef _LP64
 52   return size & 1;
 53 #else
 54   return 0;
 55 #endif
 56 }
 57 
 58 inline intptr_t* ContinuationHelper::frame_align_pointer(intptr_t* sp) {
 59 #ifdef _LP64
 60   sp = align_down(sp, frame::frame_alignment);
 61 #endif
 62   return sp;
 63 }
 64 
 65 template<typename FKind>
 66 inline void ContinuationHelper::update_register_map(const frame& f, RegisterMap* map) {
 67   frame::update_map_with_saved_link(map, link_address<FKind>(f));
 68 }
 69 
 70 inline void ContinuationHelper::update_register_map_with_callee(const frame& f, RegisterMap* map) {
 71   frame::update_map_with_saved_link(map, ContinuationHelper::Frame::callee_link_address(f));
 72 }
 73 
 74 inline void ContinuationHelper::push_pd(const frame& f) {
 75   *(intptr_t**)(f.sp() - frame::sender_sp_offset) = f.fp();
 76 }
 77 
 78 inline void ContinuationHelper::set_anchor_to_entry_pd(JavaFrameAnchor* anchor, ContinuationEntry* entry) {
 79   anchor->set_last_Java_fp(entry->entry_fp());
 80 }
 81 
 82 inline void ContinuationHelper::set_anchor_pd(JavaFrameAnchor* anchor, intptr_t* sp) {
 83   intptr_t* fp = *(intptr_t**)(sp - frame::sender_sp_offset);
 84   anchor->set_last_Java_fp(fp);
 85 }
 86 
 87 #ifdef ASSERT
 88 inline bool ContinuationHelper::Frame::assert_frame_laid_out(frame f) {
 89   intptr_t* sp = f.sp();
 90   address pc = *(address*)(sp - frame::sender_sp_ret_address_offset());
 91   intptr_t* fp = *(intptr_t**)(sp - frame::sender_sp_offset);
 92   assert(f.raw_pc() == pc, "f.ra_pc: " INTPTR_FORMAT " actual: " INTPTR_FORMAT, p2i(f.raw_pc()), p2i(pc));
 93   assert(f.fp() == fp, "f.fp: " INTPTR_FORMAT " actual: " INTPTR_FORMAT, p2i(f.fp()), p2i(fp));
 94   return f.raw_pc() == pc && f.fp() == fp;
 95 }
 96 #endif
 97 
 98 inline intptr_t** ContinuationHelper::Frame::callee_link_address(const frame& f) {
 99   return (intptr_t**)(f.sp() - frame::sender_sp_offset);
100 }
101 
102 inline address* ContinuationHelper::Frame::return_pc_address(const frame& f) {
103   return (address*)(f.real_fp() - 1);
104 }
105 
106 inline address* ContinuationHelper::InterpretedFrame::return_pc_address(const frame& f) {
107   return (address*)(f.fp() + frame::return_addr_offset);
108 }
109 
110 inline void ContinuationHelper::InterpretedFrame::patch_sender_sp(frame& f, const frame& caller) {
111   intptr_t* sp = caller.unextended_sp();
112   assert(f.is_interpreted_frame(), "");
113   intptr_t* la = f.addr_at(frame::interpreter_frame_sender_sp_offset);
114   *la = f.is_heap_frame() ? (intptr_t)(sp - f.fp()) : (intptr_t)sp;
115 }
116 
117 inline address ContinuationHelper::Frame::real_pc(const frame& f) {
118   address* pc_addr = &(((address*) f.sp())[-1]);
119   return *pc_addr;
120 }
121 
122 inline void ContinuationHelper::Frame::patch_pc(const frame& f, address pc) {
123   address* pc_addr = &(((address*) f.sp())[-1]);
124   *pc_addr = pc;
125 }
126 
127 inline intptr_t* ContinuationHelper::InterpretedFrame::frame_top(const frame& f, InterpreterOopMap* mask) { // inclusive; this will be copied with the frame
128   // interpreter_frame_last_sp_offset, points to unextended_sp includes arguments in the frame
129   // interpreter_frame_initial_sp_offset excludes expression stack slots
130   int expression_stack_sz = expression_stack_size(f, mask);
131   intptr_t* res = (intptr_t*)f.at_relative(frame::interpreter_frame_initial_sp_offset) - expression_stack_sz;
132   assert(res == (intptr_t*)f.interpreter_frame_monitor_end() - expression_stack_sz, "");
133   assert(res >= f.unextended_sp(),
134     "res: " INTPTR_FORMAT " initial_sp: " INTPTR_FORMAT " last_sp: " INTPTR_FORMAT " unextended_sp: " INTPTR_FORMAT " expression_stack_size: %d",
135     p2i(res), p2i(f.addr_at(frame::interpreter_frame_initial_sp_offset)), f.at_relative_or_null(frame::interpreter_frame_last_sp_offset),
136     p2i(f.unextended_sp()), expression_stack_sz);
137   return res;
138 }
139 
140 inline intptr_t* ContinuationHelper::InterpretedFrame::frame_bottom(const frame& f) { // exclusive; this will not be copied with the frame
141   return (intptr_t*)f.at_relative(frame::interpreter_frame_locals_offset) + 1; // exclusive, so we add 1 word
142 }
143 
144 inline intptr_t* ContinuationHelper::InterpretedFrame::frame_top(const frame& f, int callee_argsize, bool callee_interpreted) {
145   return f.unextended_sp() + (callee_interpreted ? callee_argsize : 0);
146 }
147 
148 inline intptr_t* ContinuationHelper::InterpretedFrame::callers_sp(const frame& f) {
149   return f.fp() + frame::metadata_words;
150 }
151 
152 #endif // CPU_X86_CONTINUATIONHELPER_X86_INLINE_HPP