1 /*
  2  * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2012, 2025 SAP SE. 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_PPC_FRAME_PPC_HPP
 27 #define CPU_PPC_FRAME_PPC_HPP
 28 
 29   //  C frame layout on PPC-64.
 30   //
 31   //  In this figure the stack grows upwards, while memory grows
 32   //  downwards. See "64-bit PowerPC ELF ABI Supplement Version 1.7",
 33   //  IBM Corp. (2003-10-29)
 34   //  (http://math-atlas.sourceforge.net/devel/assembly/PPC-elf64abi-1.7.pdf).
 35   //
 36   //  Square brackets denote stack regions possibly larger
 37   //  than a single 64 bit slot.
 38   //
 39   //  STACK:
 40   //    0       [C_FRAME]               <-- SP after prolog (mod 16 = 0)
 41   //            [C_FRAME]               <-- SP before prolog
 42   //            ...
 43   //            [C_FRAME]
 44   //
 45   //  C_FRAME:
 46   //    0       [ABI_REG_ARGS]
 47   //    112     CARG_9: outgoing arg 9 (arg_1 ... arg_8 via gpr_3 ... gpr_{10})
 48   //            ...
 49   //    40+M*8  CARG_M: outgoing arg M (M is the maximum of outgoing args taken over all call sites in the procedure)
 50   //            local 1
 51   //            ...
 52   //            local N
 53   //            spill slot for vector reg (16 bytes aligned)
 54   //            ...
 55   //            spill slot for vector reg
 56   //            alignment       (4 or 12 bytes)
 57   //    V       SR_VRSAVE
 58   //    V+4     spill slot for GR
 59   //    ...     ...
 60   //            spill slot for GR
 61   //            spill slot for FR
 62   //            ...
 63   //            spill slot for FR
 64   //
 65   //  ABI_MINFRAME:
 66   //    0       caller's SP
 67   //    8       space for condition register (CR) for next call
 68   //    16      space for link register (LR) for next call
 69   //    24      reserved (ABI_ELFv2 only)
 70   //    32      reserved (ABI_ELFv2 only)
 71   //    40      space for TOC (=R2) register for next call
 72   //
 73   //  ABI_REG_ARGS:
 74   //    0       [ABI_MINFRAME]
 75   //    48      CARG_1: spill slot for outgoing arg 1. used by next callee.
 76   //    ...     ...
 77   //    104     CARG_8: spill slot for outgoing arg 8. used by next callee.
 78   //
 79 
 80  public:
 81 
 82   // C frame layout
 83   static const int alignment_in_bytes = 16;
 84 
 85   // Common ABI. On top of all frames, C and Java
 86   struct common_abi {
 87     uint64_t callers_sp;
 88     uint64_t cr;
 89     uint64_t lr;
 90   };
 91 
 92   // ABI_MINFRAME. Used for native C frames.
 93   struct native_abi_minframe : common_abi {
 94 #if !defined(ABI_ELFv2)
 95     uint64_t reserved1;                           //_16
 96     uint64_t reserved2;
 97 #endif
 98     uint64_t toc;                                 //_16
 99     // nothing to add here!
100     // aligned to frame::alignment_in_bytes (16)
101   };
102 
103   struct native_abi_reg_args : native_abi_minframe {
104     uint64_t carg_1;
105     uint64_t carg_2;                              //_16
106     uint64_t carg_3;
107     uint64_t carg_4;                              //_16
108     uint64_t carg_5;
109     uint64_t carg_6;                              //_16
110     uint64_t carg_7;
111     uint64_t carg_8;                              //_16
112     // aligned to frame::alignment_in_bytes (16)
113   };
114 
115   enum {
116     native_abi_minframe_size = sizeof(native_abi_minframe),
117     native_abi_reg_args_size = sizeof(native_abi_reg_args)
118   };
119 
120   #define _abi0(_component) \
121           (offset_of(frame::native_abi_reg_args, _component))
122 
123   struct native_abi_reg_args_spill : native_abi_reg_args {
124     // additional spill slots
125     uint64_t spill_ret;
126     uint64_t spill_fret;                          //_16
127     // aligned to frame::alignment_in_bytes (16)
128   };
129 
130   enum {
131     native_abi_reg_args_spill_size = sizeof(native_abi_reg_args_spill)
132   };
133 
134   #define _native_abi_reg_args_spill(_component) \
135           (offset_of(frame::native_abi_reg_args_spill, _component))
136 
137 
138   // Frame layout for the Java template interpreter on PPC64.
139   //
140   // We differnetiate between TOP and PARENT frames.
141   // TOP frames allow for calling native C code.
142   // A TOP frame is trimmed to a PARENT frame when calling a Java method.
143   //
144   // In these figures the stack grows upwards, while memory grows
145   // downwards. Square brackets denote regions possibly larger than
146   // single 64 bit slots.
147   //
148   //  STACK (interpreter is active):
149   //    0       [TOP_IJAVA_FRAME]
150   //            [PARENT_IJAVA_FRAME]
151   //            ...
152   //            [PARENT_IJAVA_FRAME]
153   //            [ENTRY_FRAME]
154   //            [C_FRAME]
155   //            ...
156   //            [C_FRAME]
157   //
158   //  With the following frame layouts:
159   //  TOP_IJAVA_FRAME:
160   //    0       [TOP_IJAVA_FRAME_ABI]
161   //            alignment (optional)
162   //            [operand stack]
163   //            [monitors] (optional)
164   //            [IJAVA_STATE]
165   //            note: own locals are located in the caller frame.
166   //
167   //  PARENT_IJAVA_FRAME:
168   //    0       [PARENT_IJAVA_FRAME_ABI]
169   //            alignment (optional)
170   //            [callee's Java result]
171   //            [callee's locals w/o arguments]
172   //            [outgoing arguments]
173   //            [used part of operand stack w/o arguments]
174   //            [monitors] (optional)
175   //            [IJAVA_STATE]
176   //
177   //  ENTRY_FRAME:
178   //    0       [PARENT_IJAVA_FRAME_ABI]
179   //            alignment (optional)
180   //            [callee's Java result]
181   //            [callee's locals w/o arguments]
182   //            [outgoing arguments]
183   //            [non-volatiles]
184   //            [ENTRY_FRAME_LOCALS]
185 
186   // ABI for every Java frame, compiled and interpreted
187   struct java_abi : common_abi {
188     uint64_t toc;
189   };
190 
191   struct parent_ijava_frame_abi : java_abi {
192   };
193 
194 #define _parent_ijava_frame_abi(_component) \
195         (offset_of(frame::parent_ijava_frame_abi, _component))
196 
197   struct top_ijava_frame_abi : native_abi_reg_args {
198   };
199 
200   enum {
201     java_abi_size = sizeof(java_abi),
202     parent_ijava_frame_abi_size = sizeof(parent_ijava_frame_abi),
203     top_ijava_frame_abi_size = sizeof(top_ijava_frame_abi)
204   };
205 
206 #define _top_ijava_frame_abi(_component) \
207         (offset_of(frame::top_ijava_frame_abi, _component))
208 
209   struct ijava_state {
210     uint64_t method;
211     uint64_t mirror;
212     uint64_t locals;
213     uint64_t monitors;
214     uint64_t cpoolCache;
215     uint64_t bcp;
216     uint64_t esp;
217     uint64_t mdx;
218     uint64_t top_frame_sp; // Original sp to be restored when returning from an i2i call
219     uint64_t sender_sp;
220     // Slots only needed for native calls. Maybe better to move elsewhere.
221     uint64_t oop_tmp;
222     uint64_t lresult;
223     uint64_t fresult;
224   };
225 
226   enum {
227     ijava_state_size = sizeof(ijava_state)
228   };
229 
230 // Byte offset relative to fp
231 #define _ijava_state_neg(_component) \
232         (int) (-frame::ijava_state_size + offset_of(frame::ijava_state, _component))
233 
234 // Frame slot index relative to fp
235 #define ijava_idx(_component) \
236         (_ijava_state_neg(_component) >> LogBytesPerWord)
237 
238   // ENTRY_FRAME
239 
240   struct entry_frame_locals {
241     uint64_t call_wrapper_address;
242     uint64_t result_address;                      //_16
243     uint64_t result_type;
244     uint64_t arguments_tos_address;               //_16
245     // aligned to frame::alignment_in_bytes (16)
246   };
247 
248   enum {
249     entry_frame_locals_size = sizeof(entry_frame_locals)
250   };
251 
252   #define _entry_frame_locals_neg(_component) \
253     (int)(-frame::entry_frame_locals_size + offset_of(frame::entry_frame_locals, _component))
254 
255 
256   //  Frame layout for JIT generated methods
257   //
258   //  In these figures the stack grows upwards, while memory grows
259   //  downwards. Square brackets denote regions possibly larger than single
260   //  64 bit slots.
261   //
262   //  STACK (interpreted Java calls JIT generated Java):
263   //          [JIT_FRAME]                                <-- SP (mod 16 = 0)
264   //          [TOP_IJAVA_FRAME]
265   //         ...
266   //
267   //  JIT_FRAME (is a C frame according to PPC-64 ABI):
268   //          [out_preserve]
269   //          [out_args]
270   //          [spills]
271   //          [pad_1]
272   //          [monitor] (optional)
273   //       ...
274   //          [monitor] (optional)
275   //          [pad_2]
276   //          [in_preserve] added / removed by prolog / epilog
277   //
278 
279   // For JIT frames we don't differentiate between TOP and PARENT frames.
280   // Runtime calls go through stubs which push a new frame.
281 
282   struct jit_out_preserve : java_abi {
283     // Nothing to add here!
284   };
285 
286   struct jit_in_preserve {
287     // Nothing to add here!
288   };
289 
290   enum {
291     jit_out_preserve_size = sizeof(jit_out_preserve),
292     jit_in_preserve_size  = sizeof(jit_in_preserve)
293   };
294 
295   struct jit_monitor {
296     uint64_t monitor[1];
297   };
298 
299   enum {
300     jit_monitor_size = sizeof(jit_monitor),
301   };
302 
303  private:
304 
305 #ifdef ASSERT
306   enum special_backlink_values : uint64_t {
307     NOT_FULLY_INITIALIZED = 0xBBAADDF9
308   };
309   bool is_fully_initialized()       const { return (uint64_t)_fp != NOT_FULLY_INITIALIZED; }
310 #endif // ASSERT
311 
312   //  STACK:
313   //            ...
314   //            [THIS_FRAME]             <-- this._sp (stack pointer for this frame)
315   //            [CALLER_FRAME]           <-- this.fp() (_sp of caller's frame)
316   //            ...
317   //
318 
319   // The frame's stack pointer before it has been extended by a c2i adapter;
320   // needed by deoptimization
321   union {
322     intptr_t* _unextended_sp;
323     int _offset_unextended_sp; // for use in stack-chunk frames
324   };
325 
326   union {
327     intptr_t* _fp;  // frame pointer
328     int _offset_fp; // relative frame pointer for use in stack-chunk frames
329   };
330 
331  public:
332 
333   // Accessors for fields
334   intptr_t* fp() const { assert_absolute(); return _fp; }
335   void set_fp(intptr_t* newfp)  { _fp = newfp; }
336   int offset_fp() const         { assert_offset();  return _offset_fp; }
337   void set_offset_fp(int value) { assert_on_heap(); _offset_fp = value; }
338 
339   // Mark a frame as not fully initialized. Must not be used for frames in the valid back chain.
340   void mark_not_fully_initialized() const { DEBUG_ONLY(own_abi()->callers_sp = NOT_FULLY_INITIALIZED;)  }
341 
342   // Accessors for ABIs
343   inline common_abi* own_abi()     const { return (common_abi*) _sp; }
344   inline common_abi* callers_abi() const { return (common_abi*) _fp; }
345 
346   enum class kind {
347     unknown,          // The frame's pc is not necessarily in the CodeCache.
348                       // CodeCache::find_blob_fast(void* pc) can yield wrong results in this case and must not be used.
349     code_blob,        // The frame's pc is known to be in the CodeCache but it is likely not in an nmethod.
350                       // CodeCache::find_blob_fast() will be correct but not faster in this case.
351     nmethod           // This is likely the frame of a nmethod.
352                       // The code cache lookup is optimized based on NativePostCallNops.
353   };
354 
355  private:
356 
357   // Initialize frame members (_pc and _sp must be given)
358   inline void setup(kind knd);
359 
360  public:
361 
362   // Constructors
363   inline frame(intptr_t* sp, intptr_t* fp, address pc);
364   inline frame(intptr_t* sp, address pc, kind knd = kind::nmethod);
365   inline frame(intptr_t* sp, address pc, intptr_t* unextended_sp, intptr_t* fp = nullptr, CodeBlob* cb = nullptr);
366   inline frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map = nullptr);
367   inline frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map, bool on_heap);
368 
369  private:
370   address*  sender_pc_addr(void) const;
371 
372  public:
373 
374   inline ijava_state* get_ijava_state() const;
375   // Some convenient register frame setters/getters for deoptimization.
376   inline intptr_t* interpreter_frame_esp() const;
377   inline void interpreter_frame_set_cpcache(ConstantPoolCache* cp);
378   inline void interpreter_frame_set_esp(intptr_t* esp);
379   inline void interpreter_frame_set_top_frame_sp(intptr_t* top_frame_sp);
380   inline void interpreter_frame_set_sender_sp(intptr_t* sender_sp);
381 
382   template <typename RegisterMapT>
383   static void update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr);
384 
385   // The size of a cInterpreter object.
386   static inline int interpreter_frame_cinterpreterstate_size_in_bytes();
387 
388   // Additional interface for entry frames:
389   inline entry_frame_locals* get_entry_frame_locals() const {
390     return (entry_frame_locals*) (((address) fp()) - entry_frame_locals_size);
391   }
392 
393   enum {
394     // normal return address is 1 bundle past PC
395     pc_return_offset                       = 0,
396     // size, in words, of frame metadata (e.g. pc and link)
397     metadata_words                         = sizeof(java_abi) >> LogBytesPerWord,
398     // size, in words, of metadata at frame bottom, i.e. it is not part of the
399     // caller/callee overlap
400     metadata_words_at_bottom               = 0,
401     // size, in words, of frame metadata at the frame top, i.e. it is located
402     // between a callee frame and its stack arguments, where it is part
403     // of the caller/callee overlap
404     metadata_words_at_top                  = sizeof(java_abi) >> LogBytesPerWord,
405     // size, in words, of frame metadata at the frame top that needs
406     // to be reserved for callee functions in the runtime
407     frame_alignment                        = 16,
408     frame_alignment_in_words               = frame_alignment >> LogBytesPerWord,
409     // size, in words, of maximum shift in frame position due to alignment
410     align_wiggle                           =  1
411   };
412 
413   static jint interpreter_frame_expression_stack_direction() { return -1; }
414 
415   // returns the sending frame, without applying any barriers
416   inline frame sender_raw(RegisterMap* map) const;
417 
418 #endif // CPU_PPC_FRAME_PPC_HPP