1 /*
2 * Copyright (c) 1997, 2025, 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_result_handler_offset = 1, // for native calls only
115 interpreter_frame_oop_temp_offset = 0, // for native calls only
116
117 interpreter_frame_sender_sp_offset = -3,
118 // outgoing sp before a call to an invoked method
119 interpreter_frame_last_sp_offset = interpreter_frame_sender_sp_offset - 1,
120 interpreter_frame_method_offset = interpreter_frame_last_sp_offset - 1,
121 interpreter_frame_mdp_offset = interpreter_frame_method_offset - 1,
122 interpreter_frame_extended_sp_offset = interpreter_frame_mdp_offset - 1,
123 interpreter_frame_mirror_offset = interpreter_frame_extended_sp_offset - 1,
124 interpreter_frame_cache_offset = interpreter_frame_mirror_offset - 1,
125 interpreter_frame_locals_offset = interpreter_frame_cache_offset - 1,
126 interpreter_frame_bcp_offset = interpreter_frame_locals_offset - 1,
127 interpreter_frame_initial_sp_offset = interpreter_frame_bcp_offset - 1,
128
129 interpreter_frame_monitor_block_top_offset = interpreter_frame_initial_sp_offset,
130 interpreter_frame_monitor_block_bottom_offset = interpreter_frame_initial_sp_offset,
131
132 // Entry frames
133 // n.b. these values are determined by the layout defined in
134 // stubGenerator for the Java call stub
135 entry_frame_after_call_words = 35,
136 entry_frame_call_wrapper_offset = -10,
137
138 // we don't need a save area
139 arg_reg_save_area_bytes = 0,
140
141 // size, in words, of frame metadata (e.g. pc and link)
142 metadata_words = 2,
143 // size, in words, of metadata at frame bottom, i.e. it is not part of the
144 // caller/callee overlap
145 metadata_words_at_bottom = metadata_words,
146 // size, in words, of frame metadata at the frame top, i.e. it is located
147 // between a callee frame and its stack arguments, where it is part
148 // of the caller/callee overlap
149 metadata_words_at_top = 0,
150 // in bytes
151 frame_alignment = 16,
152 // size, in words, of maximum shift in frame position due to alignment
153 align_wiggle = 1
154 };
155
156 intptr_t ptr_at(int offset) const {
157 return *ptr_at_addr(offset);
158 }
159
160 void ptr_at_put(int offset, intptr_t value) {
161 *ptr_at_addr(offset) = value;
162 }
163
164 private:
165 // an additional field beyond _sp and _pc:
166 union {
167 intptr_t* _fp; // frame pointer
168 int _offset_fp; // relative frame pointer for use in stack-chunk frames
169 };
170 // The interpreter and adapters will extend the frame of the caller.
171 // Since oopMaps are based on the sp of the caller before extension
172 // we need to know that value. However in order to compute the address
173 // of the return address we need the real "raw" sp. Since sparc already
174 // uses sp() to mean "raw" sp and unextended_sp() to mean the caller's
175 // original sp we use that convention.
176
177 union {
178 intptr_t* _unextended_sp;
179 int _offset_unextended_sp; // for use in stack-chunk frames
180 };
181
182 intptr_t* ptr_at_addr(int offset) const {
183 return (intptr_t*) addr_at(offset);
184 }
185
186 public:
187 // Constructors
188
189 frame(intptr_t* ptr_sp, intptr_t* ptr_fp, address pc);
190
191 frame(intptr_t* ptr_sp, intptr_t* unextended_sp, intptr_t* ptr_fp, address pc);
192
193 frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb);
194 // used for fast frame construction by continuations
195 frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map, bool on_heap);
196
197 frame(intptr_t* ptr_sp, intptr_t* ptr_fp);
198
199 void init(intptr_t* ptr_sp, intptr_t* ptr_fp, address pc);
200 void setup(address pc);
201
202 // accessors for the instance variables
203 // Note: not necessarily the real 'frame pointer' (see real_fp)
204
205 intptr_t* fp() const { assert_absolute(); return _fp; }
206 void set_fp(intptr_t* newfp) { _fp = newfp; }
207 int offset_fp() const { assert_offset(); return _offset_fp; }
208 void set_offset_fp(int value) { assert_on_heap(); _offset_fp = value; }
209
210 inline address* sender_pc_addr() const;
211
212 // expression stack tos if we are nested in a java call
213 intptr_t* interpreter_frame_last_sp() const;
214
215 void interpreter_frame_set_extended_sp(intptr_t* sp);
216
217 template <typename RegisterMapT>
218 static void update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr);
219
220 // deoptimization support
221 void interpreter_frame_set_last_sp(intptr_t* last_sp);
222
223 static jint interpreter_frame_expression_stack_direction() { return -1; }
224
225 // returns the sending frame, without applying any barriers
226 inline frame sender_raw(RegisterMap* map) const;
227
228 #endif // CPU_RISCV_FRAME_RISCV_HPP