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