1 /*
  2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2020, 2022, Huawei Technologies Co., Ltd. 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_RISCV_FRAME_RISCV_HPP
 27 #define CPU_RISCV_FRAME_RISCV_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     ]
 57 //    [return pc             ]
 58 
 59 //    [last sp               ]   <- fp           = link()
 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 // ------------------------------ C Frame ------------------------------------------------
 68 // Stack: gcc with -fno-omit-frame-pointer
 69 //                    .
 70 //                    .
 71 //       +->          .
 72 //       |   +-----------------+   |
 73 //       |   | return address  |   |
 74 //       |   |   previous fp ------+
 75 //       |   | saved registers |
 76 //       |   | local variables |
 77 //       |   |       ...       | <-+
 78 //       |   +-----------------+   |
 79 //       |   | return address  |   |
 80 //       +------ previous fp   |   |
 81 //           | saved registers |   |
 82 //           | local variables |   |
 83 //       +-> |       ...       |   |
 84 //       |   +-----------------+   |
 85 //       |   | return address  |   |
 86 //       |   |   previous fp ------+
 87 //       |   | saved registers |
 88 //       |   | local variables |
 89 //       |   |       ...       | <-+
 90 //       |   +-----------------+   |
 91 //       |   | return address  |   |
 92 //       +------ previous fp   |   |
 93 //           | saved registers |   |
 94 //           | local variables |   |
 95 //   $fp --> |       ...       |   |
 96 //           +-----------------+   |
 97 //           | return address  |   |
 98 //           |   previous fp ------+
 99 //           | saved registers |
100 //   $sp --> | local variables |
101 //           +-----------------+
102 // ------------------------------ C Frame ------------------------------------------------
103 
104  public:
105   enum {
106     pc_return_offset                                 =  0,
107 
108     // All frames
109     link_offset                                      = -2,
110     return_addr_offset                               = -1,
111     sender_sp_offset                                 =  0,
112 
113     // Interpreter frames
114     interpreter_frame_oop_temp_offset                =  1, // for native calls only
115 
116     interpreter_frame_sender_sp_offset               = -3,
117     // outgoing sp before a call to an invoked method
118     interpreter_frame_last_sp_offset                 = interpreter_frame_sender_sp_offset - 1,
119     interpreter_frame_method_offset                  = interpreter_frame_last_sp_offset - 1,
120     interpreter_frame_mdp_offset                     = interpreter_frame_method_offset - 1,
121     interpreter_frame_extended_sp_offset             = interpreter_frame_mdp_offset - 1,
122     interpreter_frame_mirror_offset                  = interpreter_frame_extended_sp_offset - 1,
123     interpreter_frame_cache_offset                   = interpreter_frame_mirror_offset - 1,
124     interpreter_frame_locals_offset                  = interpreter_frame_cache_offset - 1,
125     interpreter_frame_bcp_offset                     = interpreter_frame_locals_offset - 1,
126     interpreter_frame_initial_sp_offset              = interpreter_frame_bcp_offset - 1,
127 
128     interpreter_frame_monitor_block_top_offset       = interpreter_frame_initial_sp_offset,
129     interpreter_frame_monitor_block_bottom_offset    = interpreter_frame_initial_sp_offset,
130 
131     // Entry frames
132     // n.b. these values are determined by the layout defined in
133     // stubGenerator for the Java call stub
134     entry_frame_after_call_words                     =  35,
135     entry_frame_call_wrapper_offset                  = -10,
136 
137     // we don't need a save area
138     arg_reg_save_area_bytes                          =  0,
139 
140     // size, in words, of frame metadata (e.g. pc and link)
141     metadata_words                                   =  2,
142     // size, in words, of metadata at frame bottom, i.e. it is not part of the
143     // caller/callee overlap
144     metadata_words_at_bottom                         = metadata_words,
145     // size, in words, of frame metadata at the frame top, i.e. it is located
146     // between a callee frame and its stack arguments, where it is part
147     // of the caller/callee overlap
148     metadata_words_at_top                            = 0,
149     // in bytes
150     frame_alignment                                  = 16,
151     // size, in words, of maximum shift in frame position due to alignment
152     align_wiggle                                     =  1
153   };
154 
155   intptr_t ptr_at(int offset) const {
156     return *ptr_at_addr(offset);
157   }
158 
159   void ptr_at_put(int offset, intptr_t value) {
160     *ptr_at_addr(offset) = value;
161   }
162 
163  private:
164   // an additional field beyond _sp and _pc:
165   union {
166     intptr_t*  _fp; // frame pointer
167     int _offset_fp; // relative frame pointer for use in stack-chunk frames
168   };
169   // The interpreter and adapters will extend the frame of the caller.
170   // Since oopMaps are based on the sp of the caller before extension
171   // we need to know that value. However in order to compute the address
172   // of the return address we need the real "raw" sp. Since sparc already
173   // uses sp() to mean "raw" sp and unextended_sp() to mean the caller's
174   // original sp we use that convention.
175 
176   union {
177     intptr_t* _unextended_sp;
178     int _offset_unextended_sp; // for use in stack-chunk frames
179   };
180 
181   void adjust_unextended_sp() NOT_DEBUG_RETURN;
182 
183   intptr_t* ptr_at_addr(int offset) const {
184     return (intptr_t*) addr_at(offset);
185   }
186 
187 #ifdef ASSERT
188   // Used in frame::sender_for_{interpreter,compiled}_frame
189   static void verify_deopt_original_pc(nmethod* nm, intptr_t* unextended_sp);
190 #endif
191 
192  public:
193   // Constructors
194 
195   frame(intptr_t* ptr_sp, intptr_t* ptr_fp, address pc);
196 
197   frame(intptr_t* ptr_sp, intptr_t* unextended_sp, intptr_t* ptr_fp, address pc);
198 
199   frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb);
200   // used for fast frame construction by continuations
201   frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map, bool on_heap);
202 
203   frame(intptr_t* ptr_sp, intptr_t* ptr_fp);
204 
205   void init(intptr_t* ptr_sp, intptr_t* ptr_fp, address pc);
206   void setup(address pc);
207 
208   // accessors for the instance variables
209   // Note: not necessarily the real 'frame pointer' (see real_fp)
210 
211   intptr_t* fp() const          { assert_absolute(); return _fp; }
212   void set_fp(intptr_t* newfp)  { _fp = newfp; }
213   int offset_fp() const         { assert_offset(); return _offset_fp; }
214   void set_offset_fp(int value) { assert_on_heap(); _offset_fp = value; }
215 
216   inline address* sender_pc_addr() const;
217 
218   // expression stack tos if we are nested in a java call
219   intptr_t* interpreter_frame_last_sp() const;
220 
221   void interpreter_frame_set_extended_sp(intptr_t* sp);
222 
223   template <typename RegisterMapT>
224   static void update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr);
225 
226   // deoptimization support
227   void interpreter_frame_set_last_sp(intptr_t* last_sp);
228 
229   static jint interpreter_frame_expression_stack_direction() { return -1; }
230 
231   // returns the sending frame, without applying any barriers
232   inline frame sender_raw(RegisterMap* map) const;
233 
234 #endif // CPU_RISCV_FRAME_RISCV_HPP