1 /*
  2  * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
  4  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  6  *
  7  * This code is free software; you can redistribute it and/or modify it
  8  * under the terms of the GNU General Public License version 2 only, as
  9  * published by the Free Software Foundation.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 
 27 #ifndef CPU_RISCV_FRAME_RISCV_HPP
 28 #define CPU_RISCV_FRAME_RISCV_HPP
 29 
 30 #include "runtime/synchronizer.hpp"
 31 
 32 // A frame represents a physical stack frame (an activation).  Frames can be
 33 // C or Java frames, and the Java frames can be interpreted or compiled.
 34 // In contrast, vframes represent source-level activations, so that one physical frame
 35 // can correspond to multiple source level frames because of inlining.
 36 // A frame is comprised of {pc, fp, sp}
 37 // ------------------------------ Asm interpreter ----------------------------------------
 38 // Layout of asm interpreter frame:
 39 //    [expression stack      ] * <- sp
 40 
 41 //    [monitors[0]           ]   \
 42 //     ...                        | monitor block size = k
 43 //    [monitors[k-1]         ]   /
 44 //    [frame initial esp     ] ( == &monitors[0], initially here)       initial_sp_offset
 45 //    [byte code index/pointr]                   = bcx()                bcx_offset
 46 
 47 //    [pointer to locals     ]                   = locals()             locals_offset
 48 //    [constant pool cache   ]                   = cache()              cache_offset
 49 
 50 //    [klass of method       ]                   = mirror()             mirror_offset
 51 //    [padding               ]
 52 
 53 //    [methodData            ]                   = mdp()                mdx_offset
 54 //    [Method                ]                   = method()             method_offset
 55 
 56 //    [last esp              ]                   = last_sp()            last_sp_offset
 57 //    [old stack pointer     ]                     (sender_sp)          sender_sp_offset
 58 
 59 //    [old frame pointer     ]   <- fp           = link()
 60 //    [return pc             ]
 61 
 62 //    [last sp               ]
 63 //    [oop temp              ]                     (only for native calls)
 64 
 65 //    [padding               ]                     (to preserve machine SP alignment)
 66 //    [locals and parameters ]
 67 //                               <- sender sp
 68 // ------------------------------ Asm interpreter ----------------------------------------
 69 
 70 // ------------------------------ C Frame ------------------------------------------------
 71 // Stack: gcc with -fno-omit-frame-pointer
 72 //                    .
 73 //                    .
 74 //       +->          .
 75 //       |   +-----------------+   |
 76 //       |   | return address  |   |
 77 //       |   |   previous fp ------+
 78 //       |   | saved registers |
 79 //       |   | local variables |
 80 //       |   |       ...       | <-+
 81 //       |   +-----------------+   |
 82 //       |   | return address  |   |
 83 //       +------ previous fp   |   |
 84 //           | saved registers |   |
 85 //           | local variables |   |
 86 //       +-> |       ...       |   |
 87 //       |   +-----------------+   |
 88 //       |   | return address  |   |
 89 //       |   |   previous fp ------+
 90 //       |   | saved registers |
 91 //       |   | local variables |
 92 //       |   |       ...       | <-+
 93 //       |   +-----------------+   |
 94 //       |   | return address  |   |
 95 //       +------ previous fp   |   |
 96 //           | saved registers |   |
 97 //           | local variables |   |
 98 //   $fp --> |       ...       |   |
 99 //           +-----------------+   |
100 //           | return address  |   |
101 //           |   previous fp ------+
102 //           | saved registers |
103 //   $sp --> | local variables |
104 //           +-----------------+
105 // ------------------------------ C Frame ------------------------------------------------
106 
107  public:
108   enum {
109     pc_return_offset                                 =  0,
110     // C frames
111     c_frame_link_offset                              = -2,
112     c_frame_return_addr_offset                       = -1,
113     c_frame_sender_sp_offset                         =  0,
114     // Java frames
115     link_offset                                      =  0,
116     return_addr_offset                               =  1,
117     sender_sp_offset                                 =  2,
118     // Interpreter frames
119     interpreter_frame_oop_temp_offset                =  3, // for native calls only
120 
121     interpreter_frame_sender_sp_offset               = -1,
122     // outgoing sp before a call to an invoked method
123     interpreter_frame_last_sp_offset                 = interpreter_frame_sender_sp_offset - 1,
124     interpreter_frame_method_offset                  = interpreter_frame_last_sp_offset - 1,
125     interpreter_frame_mdp_offset                     = interpreter_frame_method_offset - 1,
126     interpreter_frame_padding_offset                 = interpreter_frame_mdp_offset - 1,
127     interpreter_frame_mirror_offset                  = interpreter_frame_padding_offset - 1,
128     interpreter_frame_cache_offset                   = interpreter_frame_mirror_offset - 1,
129     interpreter_frame_locals_offset                  = interpreter_frame_cache_offset - 1,
130     interpreter_frame_bcp_offset                     = interpreter_frame_locals_offset - 1,
131     interpreter_frame_initial_sp_offset              = interpreter_frame_bcp_offset - 1,
132 
133     interpreter_frame_monitor_block_top_offset       = interpreter_frame_initial_sp_offset,
134     interpreter_frame_monitor_block_bottom_offset    = interpreter_frame_initial_sp_offset,
135 
136     // Entry frames
137     // n.b. these values are determined by the layout defined in
138     // stubGenerator for the Java call stub
139     entry_frame_after_call_words                     = 21,
140     entry_frame_call_wrapper_offset                  = -8,
141 
142     // we don't need a save area
143     arg_reg_save_area_bytes                          =  0
144   };
145 
146   intptr_t ptr_at(int offset) const {
147     return *ptr_at_addr(offset);
148   }
149 
150   void ptr_at_put(int offset, intptr_t value) {
151     *ptr_at_addr(offset) = value;
152   }
153 
154  private:
155   // an additional field beyond _sp and _pc:
156   intptr_t*   _fp; // frame pointer
157   // The interpreter and adapters will extend the frame of the caller.
158   // Since oopMaps are based on the sp of the caller before extension
159   // we need to know that value. However in order to compute the address
160   // of the return address we need the real "raw" sp. Since sparc already
161   // uses sp() to mean "raw" sp and unextended_sp() to mean the caller's
162   // original sp we use that convention.
163 
164   intptr_t*     _unextended_sp;
165   void adjust_unextended_sp();
166 
167   intptr_t* ptr_at_addr(int offset) const {
168     return (intptr_t*) addr_at(offset);
169   }
170 
171 #ifdef ASSERT
172   // Used in frame::sender_for_{interpreter,compiled}_frame
173   static void verify_deopt_original_pc(   CompiledMethod* nm, intptr_t* unextended_sp);
174 #endif
175 
176  public:
177   // Constructors
178 
179   frame(intptr_t* ptr_sp, intptr_t* ptr_fp, address pc);
180 
181   frame(intptr_t* ptr_sp, intptr_t* unextended_sp, intptr_t* ptr_fp, address pc);
182 
183   frame(intptr_t* ptr_sp, intptr_t* ptr_fp);
184 
185   void init(intptr_t* ptr_sp, intptr_t* ptr_fp, address pc);
186 
187   // accessors for the instance variables
188   // Note: not necessarily the real 'frame pointer' (see real_fp)
189   intptr_t*   fp() const { return _fp; }
190 
191   inline address* sender_pc_addr() const;
192 
193   // C frame methods
194   inline intptr_t* c_frame_link() const;
195   inline address*  c_frame_sender_pc_addr() const;
196   inline address   c_frame_sender_pc() const;
197   inline intptr_t* c_frame_sender_sp() const;
198 
199   // expression stack tos if we are nested in a java call
200   intptr_t* interpreter_frame_last_sp() const;
201 
202   // helper to update a map with callee-saved RBP
203   static void update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr);
204 
205   // deoptimization support
206   void interpreter_frame_set_last_sp(intptr_t* last_sp);
207 
208   static jint interpreter_frame_expression_stack_direction() { return -1; }
209 
210   // returns the sending frame, without applying any barriers
211   frame sender_raw(RegisterMap* map) const;
212 
213 #endif // CPU_RISCV_FRAME_RISCV_HPP