1 /*
  2  * Copyright (c) 1997, 2022, 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_HPP
 27 #define CPU_AARCH64_FRAME_AARCH64_HPP
 28 
 29 // A frame represents a physical stack frame (an activation).  Frames can be
 30 // C or Java frames, and the Java frames can be interpreted or compiled.
 31 // In contrast, vframes represent source-level activations, so that one physical frame
 32 // can correspond to multiple source level frames because of inlining.
 33 // A frame is comprised of {pc, fp, sp}
 34 // ------------------------------ Asm interpreter ----------------------------------------
 35 // Layout of asm interpreter frame:
 36 //    [expression stack      ] * <- sp
 37 
 38 //    [monitors[0]           ]   \
 39 //     ...                        | monitor block size = k
 40 //    [monitors[k-1]         ]   /
 41 //    [frame initial esp     ] ( == &monitors[0], initially here)       initial_sp_offset
 42 //    [byte code index/pointr]                   = bcx()                bcx_offset
 43 
 44 //    [pointer to locals     ]                   = locals()             locals_offset
 45 //    [constant pool cache   ]                   = cache()              cache_offset
 46 
 47 //    [klass of method       ]                   = mirror()             mirror_offset
 48 //    [extended SP           ]                                          extended_sp offset
 49 
 50 //    [methodData            ]                   = mdp()                mdx_offset
 51 //    [Method                ]                   = method()             method_offset
 52 
 53 //    [last esp              ]                   = last_sp()            last_sp_offset
 54 //    [sender's SP           ]                     (sender_sp)          sender_sp_offset
 55 
 56 //    [old frame pointer     ]   <- fp           = link()
 57 //    [return pc             ]
 58 
 59 //    [last sp               ]
 60 //    [oop temp              ]                     (only for native calls)
 61 
 62 //    [padding               ]                     (to preserve machine SP alignment)
 63 //    [locals and parameters ]
 64 //                               <- sender sp
 65 // ------------------------------ Asm interpreter ----------------------------------------
 66 
 67  public:
 68   enum {
 69     pc_return_offset                                 =  0,
 70     // All frames
 71     link_offset                                      =  0,
 72     return_addr_offset                               =  1,
 73     sender_sp_offset                                 =  2,
 74 
 75     // Interpreter frames
 76     interpreter_frame_oop_temp_offset                =  3, // for native calls only
 77 
 78     interpreter_frame_sender_sp_offset               = -1,
 79     // outgoing sp before a call to an invoked method
 80     interpreter_frame_last_sp_offset                 = interpreter_frame_sender_sp_offset - 1,
 81     interpreter_frame_method_offset                  = interpreter_frame_last_sp_offset - 1,
 82     interpreter_frame_mdp_offset                     = interpreter_frame_method_offset - 1,
 83     interpreter_frame_extended_sp_offset             = interpreter_frame_mdp_offset - 1,
 84     interpreter_frame_mirror_offset                  = interpreter_frame_extended_sp_offset - 1,
 85     interpreter_frame_cache_offset                   = interpreter_frame_mirror_offset - 1,
 86     interpreter_frame_locals_offset                  = interpreter_frame_cache_offset - 1,
 87     interpreter_frame_bcp_offset                     = interpreter_frame_locals_offset - 1,
 88     interpreter_frame_initial_sp_offset              = interpreter_frame_bcp_offset - 1,
 89 
 90     interpreter_frame_monitor_block_top_offset       = interpreter_frame_initial_sp_offset,
 91     interpreter_frame_monitor_block_bottom_offset    = interpreter_frame_initial_sp_offset,
 92 
 93     // Entry frames
 94     // n.b. these values are determined by the layout defined in
 95     // stubGenerator for the Java call stub
 96     entry_frame_after_call_words                     = 27,
 97     entry_frame_call_wrapper_offset                  = -8,
 98 
 99     // we don't need a save area
100     arg_reg_save_area_bytes                          =  0,
101 
102     // size, in words, of frame metadata (e.g. pc and link)
103     metadata_words                                   = sender_sp_offset,
104     // in bytes
105     frame_alignment                                  = 16,
106     // size, in words, of maximum shift in frame position due to alignment
107     align_wiggle                                     =  1
108   };
109 
110   intptr_t ptr_at(int offset) const {
111     return *ptr_at_addr(offset);
112   }
113 
114   void ptr_at_put(int offset, intptr_t value) {
115     *ptr_at_addr(offset) = value;
116   }
117 
118  private:
119   // an additional field beyond _sp and _pc:
120   union {
121     intptr_t*  _fp; // frame pointer
122     int _offset_fp; // relative frame pointer for use in stack-chunk frames
123   };
124   // The interpreter and adapters will extend the frame of the caller.
125   // Since oopMaps are based on the sp of the caller before extension
126   // we need to know that value. However in order to compute the address
127   // of the return address we need the real "raw" sp. Since sparc already
128   // uses sp() to mean "raw" sp and unextended_sp() to mean the caller's
129   // original sp we use that convention.
130 
131   union {
132     intptr_t* _unextended_sp;
133     int _offset_unextended_sp; // for use in stack-chunk frames
134   };
135 
136   void adjust_unextended_sp() NOT_DEBUG_RETURN;
137 
138   // true means _sp value is correct and we can use it to get the sender's sp
139   // of the compiled frame, otherwise, _sp value may be invalid and we can use
140   // _fp to get the sender's sp if PreserveFramePointer is enabled.
141   bool _sp_is_trusted;
142 
143   intptr_t* ptr_at_addr(int offset) const {
144     return (intptr_t*) addr_at(offset);
145   }
146 
147   // Support for scalarized inline type calling convention
148   intptr_t* repair_sender_sp(intptr_t* sender_sp, intptr_t** saved_fp_addr) const;
149 
150 #ifdef ASSERT
151   // Used in frame::sender_for_{interpreter,compiled}_frame
152   static void verify_deopt_original_pc(   CompiledMethod* nm, intptr_t* unextended_sp);
153 #endif
154 
155   const ImmutableOopMap* get_oop_map() const;
156 
157  public:
158   // Constructors
159 
160   frame(intptr_t* sp, intptr_t* fp, address pc);
161 
162   frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc);
163 
164   frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb);
165   // used for fast frame construction by continuations
166   frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map, bool on_heap);
167 
168   frame(intptr_t* sp, intptr_t* fp);
169 
170   void init(intptr_t* sp, intptr_t* fp, address pc);
171   void setup(address pc);
172 
173   // accessors for the instance variables
174   // Note: not necessarily the real 'frame pointer' (see real_fp)
175   intptr_t*   fp() const        { assert_absolute(); return _fp; }
176   void set_fp(intptr_t* newfp)  { _fp = newfp; }
177   int offset_fp() const         { assert_offset();  return _offset_fp; }
178   void set_offset_fp(int value) { assert_on_heap(); _offset_fp = value; }
179 
180   inline address* sender_pc_addr() const;
181   inline address  sender_pc_maybe_signed() const;
182 
183   // expression stack tos if we are nested in a java call
184   intptr_t* interpreter_frame_last_sp() const;
185 
186   void interpreter_frame_set_extended_sp(intptr_t* sp);
187 
188   template <typename RegisterMapT>
189   static void update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr);
190 
191   // deoptimization support
192   void interpreter_frame_set_last_sp(intptr_t* sp);
193 
194   static jint interpreter_frame_expression_stack_direction() { return -1; }
195 
196   // returns the sending frame, without applying any barriers
197   inline frame sender_raw(RegisterMap* map) const;
198 
199   void set_sp_is_trusted() { _sp_is_trusted = true; }
200 
201 #endif // CPU_AARCH64_FRAME_AARCH64_HPP