1 /*
  2  * Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2016, 2024 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 // Major contributions by ML, AHa.
 27 
 28 #ifndef CPU_S390_FRAME_S390_HPP
 29 #define CPU_S390_FRAME_S390_HPP
 30 
 31   //  C frame layout on ZARCH_64.
 32   //
 33   //  In this figure the stack grows upwards, while memory grows
 34   //  downwards. See "Linux for zSeries: ELF Application Binary Interface Supplement",
 35   //  IBM Corp. (LINUX-1107-01)
 36   //
 37   //  Square brackets denote stack regions possibly larger
 38   //  than a single 64 bit slot.
 39   //
 40   //  STACK:
 41   //    0       [C_FRAME]               <-- SP after prolog (mod 8 = 0)
 42   //            [C_FRAME]               <-- SP before prolog
 43   //            ...
 44   //            [C_FRAME]
 45   //
 46   //  C_FRAME:
 47   //    0       [ABI_160]
 48   //
 49   //  ABI_160:
 50   //    0       [Z_COMMON_ABI]
 51   //    16      CARG_1: spill slot for outgoing arg 1. used by next callee.
 52   //    24      CARG_2: spill slot for outgoing arg 2. used by next callee.
 53   //    32      CARG_3: spill slot for outgoing arg 3. used by next callee.
 54   //    40      CARG_4: spill slot for outgoing arg 4. used by next callee.
 55   //    48      GPR_6:  spill slot for GPR_6. used by next callee.
 56   //    ...     ...
 57   //    120     GPR_15:  spill slot for GPR_15. used by next callee.
 58   //    128     CFARG_1: spill slot for outgoing fp arg 1. used by next callee.
 59   //    136     CFARG_2: spill slot for outgoing fp arg 2. used by next callee.
 60   //    144     CFARG_3: spill slot for outgoing fp arg 3. used by next callee.
 61   //    152     CFARG_4: spill slot for outgoing fp arg 4. used by next callee.
 62   //    160     [REMAINING CARGS]
 63   //
 64   //  Z_COMMON_ABI:
 65   //    0       callers_sp
 66   //    8       return_pc
 67 
 68  public:
 69 
 70   // C frame layout
 71 
 72   typedef enum {
 73      // stack alignment
 74      alignment_in_bytes = 8,
 75      // log_2(8*8 bits) = 6.
 76      log_2_of_alignment_in_bits = 6
 77   } frame_constants;
 78 
 79   // Common ABI. On top of all frames, C and Java
 80   struct z_common_abi {
 81     uint64_t callers_sp;
 82     uint64_t return_pc;
 83   };
 84 
 85   enum {
 86     z_common_abi_size = sizeof(z_common_abi)
 87   };
 88 
 89   #define _z_common_abi(_component) \
 90           (offset_of(frame::z_common_abi, _component))
 91 
 92   // Z_NATIVE_ABI for native C frames.
 93   struct z_native_abi: z_common_abi {
 94     // Nothing to add here!
 95   };
 96 
 97   // ABI_160:
 98 
 99   // REMARK: z_abi_160_base structure reflect the "minimal" ABI frame
100   // layout. There is a field in the z_abi_160
101   // structure that marks the area where arguments are passed, when
102   // the argument registers "overflow". Thus, sizeof(z_abi_160)
103   // doesn't yield the expected (and desired) result.
104   // Therefore, please use sizeof(z_abi_160_base) or
105   // the enum value z_abi_160_size to find out the size of the ABI structure.
106   struct z_abi_160_base : z_native_abi {
107     uint64_t carg_1;
108     uint64_t carg_2;
109     uint64_t carg_3;
110     uint64_t carg_4;
111     uint64_t gpr6;
112     uint64_t gpr7;
113     uint64_t gpr8;
114     uint64_t gpr9;
115     uint64_t gpr10;
116     uint64_t gpr11;
117     uint64_t gpr12;
118     uint64_t gpr13;
119     uint64_t gpr14;
120     uint64_t gpr15;
121     uint64_t cfarg_1;
122     uint64_t cfarg_2;
123     uint64_t cfarg_3;
124     uint64_t cfarg_4;
125   };
126 
127   struct z_abi_160: z_abi_160_base {
128     uint64_t remaining_cargs;
129   };
130 
131   enum {
132     z_native_abi_size = sizeof(z_native_abi),
133     z_abi_160_size = sizeof(z_abi_160_base)
134   };
135 
136   #define _z_abi(_component) \
137           (offset_of(frame::z_abi_160, _component))
138 
139   struct z_abi_160_spill : z_abi_160 {
140    // Additional spill slots. Use as 'offset_of(z_abi_160_spill, spill[n])'.
141     uint64_t spill[0];
142     // Aligned to frame::alignment_in_bytes (16).
143   };
144 
145 
146   // non-volatile GPRs:
147 
148   struct z_spill_nonvolatiles {
149     uint64_t r6;
150     uint64_t r7;
151     uint64_t r8;
152     uint64_t r9;
153     uint64_t r10;
154     uint64_t r11;
155     uint64_t r12;
156     uint64_t r13;
157   };
158 
159   enum {
160     z_spill_nonvolatiles_size = sizeof(z_spill_nonvolatiles)
161   };
162 
163   #define _z_spill_nonvolatiles_neg(_component) \
164           (-frame::z_spill_nonvolatiles_size + offset_of(frame::z_spill_nonvolatiles, _component))
165 
166   // Frame layout for the Java template interpreter on z/Architecture.
167   //
168   // We differentiate between TOP and PARENT frames.
169   // TOP frames allow for calling native C code.
170   // A TOP frame is trimmed to a PARENT frame when calling a Java method.
171   //
172   // In these figures the stack grows upwards, while memory grows
173   // downwards. Square brackets denote regions possibly larger than
174   // single 64 bit slots.
175   //
176   // STACK (no JNI, no compiled code, no library calls, template interpreter is active):
177   //
178   //   0       [TOP_IJAVA_FRAME]
179   //           [PARENT_IJAVA_FRAME]
180   //           [PARENT_IJAVA_FRAME]
181   //           ...
182   //           [PARENT_IJAVA_FRAME]
183   //           [ENTRY_FRAME]
184   //           [C_FRAME]
185   //           ...
186   //           [C_FRAME]
187   //
188   // TOP_IJAVA_FRAME:
189   //
190   //   0       [TOP_IJAVA_FRAME_ABI]
191   //   16      [operand stack]
192   //           [monitors]      (optional)
193   //           [IJAVA_STATE]
194   //           note: Own locals are located in the caller frame.
195   //
196   // PARENT_IJAVA_FRAME:
197   //
198   //   0       [PARENT_IJAVA_FRAME_ABI]
199   //           [callee's locals w/o arguments]
200   //           [outgoing arguments]
201   //           [used part of operand stack w/o arguments]
202   //           [monitors]      (optional)
203   //           [IJAVA_STATE]
204   //
205   // ENTRY_FRAME:
206   //
207   //   0       [PARENT_IJAVA_FRAME_ABI]
208   //           [callee's locals w/o arguments]
209   //           [outgoing arguments]
210   //           [ENTRY_FRAME_LOCALS]
211   //
212   // TOP_IJAVA_FRAME_ABI:
213   //
214   //   0       [ABI_160]
215   //
216   //
217   // PARENT_IJAVA_FRAME_ABI:
218   //
219   //   0       [ABI_16]
220   //
221   // IJAVA_STATE:
222   //
223   //   0       method
224   //   8       locals
225   //           monitors               : monitor block top (i.e. lowest address)
226   //           cpoolCache
227   //           bcp
228   //           mdx
229   //           esp                    : Points to first slot above operands.
230   //           sender_sp              : See comment in z_ijava_state.
231   //           top_frame_sp           : Own SP before modification by i2c adapter.
232   //           oop_tmp
233   //           lresult
234   //           fresult
235   //
236   // EXAMPLE:
237   // ---------
238   //
239   // 3 monitors, 5 operand stack slots max. / 3 allocated
240   //
241   //    F0      callers_sp               <- Z_SP (callers_sp == Z_fp (own fp))
242   //            return_pc
243   //            [rest of ABI_160]
244   //           /slot 4: free
245   //    oper. | slot 3: free             <- Z_esp points to first free slot
246   //    stack | slot 2: ref val v2                caches IJAVA_STATE.esp
247   //          | slot 1: unused
248   //           \slot 0: long val v1
249   //           /slot 5                   <- IJAVA_STATE.monitors  = monitor block top
250   //          | slot 4
251   //  monitors| slot 3
252   //          | slot 2
253   //          | slot 1
254   //           \slot 0
255   //            [IJAVA_STATE]            <- monitor block bot (points to first byte in IJAVA_STATE)
256   //    F1      [PARENT_IJAVA_FRAME_ABI] <- Z_fp (== *Z_SP, points to slot just below IJAVA_STATE)
257   //            [F0's locals]            <- Z_locals, locals[i] := *(Z_locals - i*BytesPerWord)
258   //            [F1's operand stack]
259   //            [F1's monitors]      (optional)
260   //            [IJAVA_STATE]
261 
262  public:
263 
264   // ABI for every Java frame, compiled and interpreted
265 
266   struct z_java_abi : z_common_abi {
267     // Nothing to add here!
268   };
269 
270   struct z_parent_ijava_frame_abi : z_java_abi {
271     // Nothing to add here!
272   };
273 
274   #define _z_parent_ijava_frame_abi(_component) \
275           (offset_of(frame::z_parent_ijava_frame_abi, _component))
276 
277   // TOP_IJAVA_FRAME_ABI
278 
279   struct z_top_ijava_frame_abi : z_abi_160 {
280   };
281 
282   enum {
283     z_java_abi_size = sizeof(z_java_abi),
284     z_parent_ijava_frame_abi_size = sizeof(z_parent_ijava_frame_abi),
285     z_top_ijava_frame_abi_size = sizeof(z_top_ijava_frame_abi)
286   };
287 
288   #define _z_top_ijava_frame_abi(_component) \
289           (offset_of(frame::z_top_ijava_frame_abi, _component))
290 
291   // IJAVA_STATE
292 
293   struct z_ijava_state{
294     DEBUG_ONLY(uint64_t magic;) // wrong magic -> wrong state!
295     uint64_t method;
296     uint64_t mirror;
297     uint64_t locals;       // Z_locals
298     uint64_t monitors;
299     uint64_t cpoolCache;
300     uint64_t bcp;          // Z_bcp
301     uint64_t mdx;
302     uint64_t esp;          // Z_esp
303     // Caller's original SP before modification by c2i adapter (if caller is compiled)
304     // and before top -> parent frame conversion by the interpreter entry.
305     // Note: for i2i calls a correct sender_sp is required, too, because there
306     // we cannot use the caller's top_frame_sp as sp when removing the callee
307     // frame (caller could be compiled or entry frame). Therefore the sender_sp
308     // has to be the interpreted caller's sp as TOP_IJAVA_FRAME. See also
309     // AbstractInterpreter::layout_activation() used by deoptimization.
310     uint64_t sender_sp;
311     // Own SP before modification by i2c adapter and top-2-parent-resize
312     // by interpreted callee.
313     uint64_t top_frame_sp;
314     // Slots only needed for native calls. Maybe better to move elsewhere.
315     uint64_t oop_tmp;
316     uint64_t lresult;
317     uint64_t fresult;
318   };
319 
320   enum  {
321     z_ijava_state_size = sizeof(z_ijava_state)
322   };
323 
324 #ifdef ASSERT
325   enum  {
326     z_istate_magic_number = 0x900d // ~= good magic
327   };
328 #endif
329 
330 #define _z_ijava_state_neg(_component) \
331          (int) (-frame::z_ijava_state_size + offset_of(frame::z_ijava_state, _component))
332 
333 // Frame slot index relative to fp
334 #define _z_ijava_idx(_component) \
335         (_z_ijava_state_neg(_component) >> LogBytesPerWord)
336 
337   // ENTRY_FRAME
338 
339   struct z_entry_frame_locals {
340     uint64_t call_wrapper_address;
341     uint64_t result_address;
342     uint64_t result_type;
343     uint64_t arguments_tos_address;
344     // Callee saved registers are spilled to caller frame.
345     // Caller must have z_abi_160.
346   };
347 
348   enum {
349     z_entry_frame_locals_size = sizeof(z_entry_frame_locals)
350   };
351 
352   #define _z_entry_frame_locals_neg(_component) \
353           (int) (-frame::z_entry_frame_locals_size + offset_of(frame::z_entry_frame_locals, _component))
354 
355   //  Frame layout for JIT generated methods
356   //
357   //  In these figures the stack grows upwards, while memory grows
358   //  downwards. Square brackets denote regions possibly larger than single
359   //  64 bit slots.
360   //
361   //  STACK (interpreted Java calls JIT generated Java):
362   //
363   //          [JIT_FRAME]                                <-- SP (mod 16 = 0)
364   //          [TOP_IJAVA_FRAME]
365   //         ...
366   //
367   //
368   //  JIT_FRAME (is a C frame according to z/Architecture ABI):
369   //
370   //          [out_preserve]
371   //          [out_args]
372   //          [spills]
373   //          [monitor] (optional)
374   //       ...
375   //          [monitor] (optional)
376   //          [in_preserve] added / removed by prolog / epilog
377 
378   // For JIT frames we don't differentiate between TOP and PARENT frames.
379   // Runtime calls go through stubs which push a new frame.
380 
381   struct jit_monitor {
382         uint64_t monitor[1];
383   };
384 
385   struct jit_in_preserve {
386     // Used to provide a z/Architecture ABI on top of a jit frame.
387     // nothing to add here!
388   };
389 
390   struct jit_out_preserve : z_java_abi {
391     // Nothing to add here!
392   };
393 
394   enum {
395     z_jit_out_preserve_size = sizeof(jit_out_preserve)
396   };
397 
398   typedef enum {
399      jit_monitor_size_in_4_byte_units = sizeof(jit_monitor) / 4,
400 
401      // Stack alignment requirement. Log_2 of alignment size in bits.
402      // log_2(16*8 bits) = 7.
403      jit_log_2_of_stack_alignment_in_bits = 7,
404 
405      jit_out_preserve_size_in_4_byte_units = sizeof(jit_out_preserve) / 4,
406 
407      jit_in_preserve_size_in_4_byte_units = sizeof(jit_in_preserve) / 4
408   } jit_frame_constants;
409 
410 
411   // C2I adapter frames:
412   //
413   // STACK (interpreted called from compiled, on entry to template interpreter):
414   //
415   //       [TOP_C2I_FRAME]
416   //       [JIT_FRAME]
417   //       ...
418   //
419   //
420   // STACK (interpreted called from compiled, after interpreter has been pushed):
421   //
422   //       [TOP_IJAVA_FRAME]
423   //       [PARENT_C2I_FRAME]
424   //       [JIT_FRAME]
425   //       ...
426   //
427   //
428   // TOP_C2I_FRAME:
429   //
430   //       [TOP_IJAVA_FRAME_ABI]
431   //       [outgoing Java arguments]
432   //       alignment (optional)
433   //
434   //
435   // PARENT_C2I_FRAME:
436   //
437   //       [PARENT_IJAVA_FRAME_ABI]
438   //       alignment (optional)
439   //       [callee's locals w/o arguments]
440   //       [outgoing Java arguments]
441   //       alignment (optional)
442 
443  private:
444 
445   //  STACK:
446   //            ...
447   //            [THIS_FRAME]             <-- this._sp (stack pointer for this frame)
448   //            [CALLER_FRAME]           <-- this.fp() (_sp of caller's frame)
449   //            ...
450   //
451 
452   // NOTE: Stack pointer is now held in the base class, so remove it from here.
453 
454   // Needed by deoptimization.
455   intptr_t* _unextended_sp;
456 
457   // Frame pointer for this frame.
458   intptr_t* _fp;
459 
460  public:
461 
462   // Interface for all frames:
463 
464   // Accessors
465 
466   inline intptr_t* fp() const { assert_absolute(); return _fp; }
467 
468  private:
469 
470   // Initialize frame members (_pc and _sp must be given)
471   inline void setup();
472 
473  // Constructors
474 
475  public:
476   // To be used, if sp was not extended to match callee's calling convention.
477   inline frame(intptr_t* sp, address pc, intptr_t* unextended_sp = nullptr, intptr_t* fp = nullptr, CodeBlob* cb = nullptr);
478   inline frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map = nullptr);
479 
480   // Access frame via stack pointer.
481   inline intptr_t* sp_addr_at(int index) const  { return &sp()[index]; }
482   inline intptr_t  sp_at(     int index) const  { return *sp_addr_at(index); }
483 
484   // Access ABIs.
485   inline z_common_abi*  own_abi()     const { return (z_common_abi*) sp(); }
486   inline z_abi_160* callers_abi() const { return (z_abi_160*) fp(); }
487 
488  private:
489   address* sender_pc_addr(void) const;
490 
491  public:
492   template <typename RegisterMapT>
493   static void update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr);
494 
495   // template interpreter state
496   inline z_ijava_state* ijava_state_unchecked() const;
497 
498  private:
499 
500   inline z_ijava_state* ijava_state() const;
501 
502  public:
503 
504   inline intptr_t* interpreter_frame_esp() const;
505   // Where z_ijava_state.esp is saved.
506   inline void interpreter_frame_set_esp(intptr_t* esp);
507   inline intptr_t* interpreter_frame_top_frame_sp();
508   inline void interpreter_frame_set_top_frame_sp(intptr_t* top_frame_sp);
509   inline void interpreter_frame_set_sender_sp(intptr_t* sender_sp);
510 #ifdef ASSERT
511   inline void interpreter_frame_set_magic();
512 #endif
513 
514   // monitors:
515 
516   // Next two functions read and write z_ijava_state.monitors.
517  private:
518   inline BasicObjectLock* interpreter_frame_monitors() const;
519 
520   // Where z_ijava_state.monitors is saved.
521   inline void interpreter_frame_set_monitors(BasicObjectLock* monitors);
522 
523  public:
524 
525   // Additional interface for entry frames:
526   inline z_entry_frame_locals* entry_frame_locals() const {
527     return (z_entry_frame_locals*) (((address) fp()) - z_entry_frame_locals_size);
528   }
529 
530  public:
531 
532   // Get caller pc from stack slot of gpr14.
533   address native_sender_pc() const;
534   // Get caller pc from stack slot of gpr10.
535   address callstub_sender_pc() const;
536 
537   // Dump all frames starting at a given C stack pointer.
538   // max_frames: Limit number of traced frames.
539   //             <= 0 --> full trace
540   //             > 0  --> trace the #max_frames topmost frames
541   static void back_trace(outputStream* st, intptr_t* start_sp, intptr_t* top_pc,
542                          unsigned long flags, int max_frames = 0);
543 
544   enum {
545     // This enum value specifies the offset from the pc remembered by
546     // call instructions to the location where control returns to
547     // after a normal return. Most architectures remember the return
548     // location directly, i.e. the offset is zero. This is the case
549     // for z/Architecture, too.
550     //
551     // Normal return address is the instruction following the branch.
552     pc_return_offset         = 0,
553     metadata_words           = 0,
554     metadata_words_at_bottom = 0,
555     metadata_words_at_top    = 0,
556     frame_alignment          = 16,
557     // size, in words, of maximum shift in frame position due to alignment
558     align_wiggle             =  1,
559     // This is wrong and unimplemented
560     sender_sp_offset         =  0
561   };
562 
563   static jint interpreter_frame_expression_stack_direction() { return -1; }
564 
565   intptr_t* repair_sender_sp(intptr_t* sender_sp, intptr_t** saved_fp_addr) const;
566   static intptr_t* repair_sender_sp(nmethod* nm, intptr_t* sp, intptr_t** saved_fp_addr);
567   bool was_augmented_on_entry(int& real_size) const;
568 
569 #endif // CPU_S390_FRAME_S390_HPP