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_base_size = sizeof(z_abi_160_base),
134     z_abi_160_size = sizeof(z_abi_160_base)
135   };
136 
137   #define _z_abi(_component) \
138           (offset_of(frame::z_abi_160, _component))
139 
140   struct z_abi_160_spill : z_abi_160 {
141    // Additional spill slots. Use as 'offset_of(z_abi_160_spill, spill[n])'.
142     uint64_t spill[0];
143     // Aligned to frame::alignment_in_bytes (16).
144   };
145 
146 
147   // non-volatile GPRs:
148 
149   struct z_spill_nonvolatiles {
150     uint64_t r6;
151     uint64_t r7;
152     uint64_t r8;
153     uint64_t r9;
154     uint64_t r10;
155     uint64_t r11;
156     uint64_t r12;
157     uint64_t r13;
158   };
159 
160   enum {
161     z_spill_nonvolatiles_size = sizeof(z_spill_nonvolatiles)
162   };
163 
164   #define _z_spill_nonvolatiles_neg(_component) \
165           (-frame::z_spill_nonvolatiles_size + offset_of(frame::z_spill_nonvolatiles, _component))
166 
167   // Frame layout for the Java template interpreter on z/Architecture.
168   //
169   // We differentiate between TOP and PARENT frames.
170   // TOP frames allow for calling native C code.
171   // A TOP frame is trimmed to a PARENT frame when calling a Java method.
172   //
173   // In these figures the stack grows upwards, while memory grows
174   // downwards. Square brackets denote regions possibly larger than
175   // single 64 bit slots.
176   //
177   // STACK (no JNI, no compiled code, no library calls, template interpreter is active):
178   //
179   //   0       [TOP_IJAVA_FRAME]
180   //           [PARENT_IJAVA_FRAME]
181   //           [PARENT_IJAVA_FRAME]
182   //           ...
183   //           [PARENT_IJAVA_FRAME]
184   //           [ENTRY_FRAME]
185   //           [C_FRAME]
186   //           ...
187   //           [C_FRAME]
188   //
189   // TOP_IJAVA_FRAME:
190   //
191   //   0       [TOP_IJAVA_FRAME_ABI]
192   //   16      [operand stack]
193   //           [monitors]      (optional)
194   //           [IJAVA_STATE]
195   //           note: Own locals are located in the caller frame.
196   //
197   // PARENT_IJAVA_FRAME:
198   //
199   //   0       [PARENT_IJAVA_FRAME_ABI]
200   //           [callee's locals w/o arguments]
201   //           [outgoing arguments]
202   //           [used part of operand stack w/o arguments]
203   //           [monitors]      (optional)
204   //           [IJAVA_STATE]
205   //
206   // ENTRY_FRAME:
207   //
208   //   0       [PARENT_IJAVA_FRAME_ABI]
209   //           [callee's locals w/o arguments]
210   //           [outgoing arguments]
211   //           [ENTRY_FRAME_LOCALS]
212   //
213   // TOP_IJAVA_FRAME_ABI:
214   //
215   //   0       [ABI_160]
216   //
217   //
218   // PARENT_IJAVA_FRAME_ABI:
219   //
220   //   0       [ABI_16]
221   //
222   // IJAVA_STATE:
223   //
224   //   0       method
225   //   8       locals
226   //           monitors               : monitor block top (i.e. lowest address)
227   //           cpoolCache
228   //           bcp
229   //           mdx
230   //           esp                    : Points to first slot above operands.
231   //           sender_sp              : See comment in z_ijava_state.
232   //           top_frame_sp           : Own SP before modification by i2c adapter.
233   //           oop_tmp
234   //           lresult
235   //           fresult
236   //
237   // EXAMPLE:
238   // ---------
239   //
240   // 3 monitors, 5 operand stack slots max. / 3 allocated
241   //
242   //    F0      callers_sp               <- Z_SP (callers_sp == Z_fp (own fp))
243   //            return_pc
244   //            [rest of ABI_160]
245   //           /slot 4: free
246   //    oper. | slot 3: free             <- Z_esp points to first free slot
247   //    stack | slot 2: ref val v2                caches IJAVA_STATE.esp
248   //          | slot 1: unused
249   //           \slot 0: long val v1
250   //           /slot 5                   <- IJAVA_STATE.monitors  = monitor block top
251   //          | slot 4
252   //  monitors| slot 3
253   //          | slot 2
254   //          | slot 1
255   //           \slot 0
256   //            [IJAVA_STATE]            <- monitor block bot (points to first byte in IJAVA_STATE)
257   //    F1      [PARENT_IJAVA_FRAME_ABI] <- Z_fp (== *Z_SP, points to slot just below IJAVA_STATE)
258   //            [F0's locals]            <- Z_locals, locals[i] := *(Z_locals - i*BytesPerWord)
259   //            [F1's operand stack]
260   //            [F1's monitors]      (optional)
261   //            [IJAVA_STATE]
262 
263  public:
264 
265   // ABI for every Java frame, compiled and interpreted
266 
267   struct z_java_abi : z_common_abi {
268     // Nothing to add here!
269   };
270 
271   struct z_parent_ijava_frame_abi : z_java_abi {
272     // Nothing to add here!
273   };
274 
275   #define _z_parent_ijava_frame_abi(_component) \
276           (offset_of(frame::z_parent_ijava_frame_abi, _component))
277 
278   // TOP_IJAVA_FRAME_ABI
279 
280   struct z_top_ijava_frame_abi : z_abi_160 {
281   };
282 
283   enum {
284     z_java_abi_size = sizeof(z_java_abi),
285     z_parent_ijava_frame_abi_size = sizeof(z_parent_ijava_frame_abi),
286     z_top_ijava_frame_abi_size = sizeof(z_top_ijava_frame_abi)
287   };
288 
289   #define _z_top_ijava_frame_abi(_component) \
290           (offset_of(frame::z_top_ijava_frame_abi, _component))
291 
292   // IJAVA_STATE
293 
294   struct z_ijava_state{
295     DEBUG_ONLY(uint64_t magic;) // wrong magic -> wrong state!
296     uint64_t method;
297     uint64_t mirror;
298     uint64_t locals;       // Z_locals
299     uint64_t monitors;
300     uint64_t cpoolCache;
301     uint64_t bcp;          // Z_bcp
302     uint64_t mdx;
303     uint64_t esp;          // Z_esp
304     // Caller's original SP before modification by c2i adapter (if caller is compiled)
305     // and before top -> parent frame conversion by the interpreter entry.
306     // Note: for i2i calls a correct sender_sp is required, too, because there
307     // we cannot use the caller's top_frame_sp as sp when removing the callee
308     // frame (caller could be compiled or entry frame). Therefore the sender_sp
309     // has to be the interpreted caller's sp as TOP_IJAVA_FRAME. See also
310     // AbstractInterpreter::layout_activation() used by deoptimization.
311     uint64_t sender_sp;
312     // Own SP before modification by i2c adapter and top-2-parent-resize
313     // by interpreted callee.
314     uint64_t top_frame_sp;
315     // Slots only needed for native calls. Maybe better to move elsewhere.
316     uint64_t oop_tmp;
317     uint64_t lresult;
318     uint64_t fresult;
319   };
320 
321   enum  {
322     z_ijava_state_size = sizeof(z_ijava_state)
323   };
324 
325 #ifdef ASSERT
326   enum  {
327     z_istate_magic_number = 0x900d // ~= good magic
328   };
329 #endif
330 
331 #define _z_ijava_state_neg(_component) \
332          (int) (-frame::z_ijava_state_size + offset_of(frame::z_ijava_state, _component))
333 
334 // Frame slot index relative to fp
335 #define _z_ijava_idx(_component) \
336         (_z_ijava_state_neg(_component) >> LogBytesPerWord)
337 
338   // ENTRY_FRAME
339 
340   struct z_entry_frame_locals {
341     uint64_t call_wrapper_address;
342     uint64_t result_address;
343     uint64_t result_type;
344     uint64_t arguments_tos_address;
345     // Callee saved registers are spilled to caller frame.
346     // Caller must have z_abi_160.
347   };
348 
349   enum {
350     z_entry_frame_locals_size = sizeof(z_entry_frame_locals)
351   };
352 
353   #define _z_entry_frame_locals_neg(_component) \
354           (int) (-frame::z_entry_frame_locals_size + offset_of(frame::z_entry_frame_locals, _component))
355 
356   //  Frame layout for JIT generated methods
357   //
358   //  In these figures the stack grows upwards, while memory grows
359   //  downwards. Square brackets denote regions possibly larger than single
360   //  64 bit slots.
361   //
362   //  STACK (interpreted Java calls JIT generated Java):
363   //
364   //          [JIT_FRAME]                                <-- SP (mod 16 = 0)
365   //          [TOP_IJAVA_FRAME]
366   //         ...
367   //
368   //
369   //  JIT_FRAME (is a C frame according to z/Architecture ABI):
370   //
371   //          [out_preserve]
372   //          [out_args]
373   //          [spills]
374   //          [monitor] (optional)
375   //       ...
376   //          [monitor] (optional)
377   //          [in_preserve] added / removed by prolog / epilog
378 
379   // For JIT frames we don't differentiate between TOP and PARENT frames.
380   // Runtime calls go through stubs which push a new frame.
381 
382   struct jit_monitor {
383         uint64_t monitor[1];
384   };
385 
386   struct jit_in_preserve {
387     // Used to provide a z/Architecture ABI on top of a jit frame.
388     // nothing to add here!
389   };
390 
391   struct jit_out_preserve : z_java_abi {
392     // Nothing to add here!
393   };
394 
395   enum {
396     z_jit_out_preserve_size = sizeof(jit_out_preserve)
397   };
398 
399   typedef enum {
400      jit_monitor_size_in_4_byte_units = sizeof(jit_monitor) / 4,
401 
402      // Stack alignment requirement. Log_2 of alignment size in bits.
403      // log_2(16*8 bits) = 7.
404      jit_log_2_of_stack_alignment_in_bits = 7,
405 
406      jit_out_preserve_size_in_4_byte_units = sizeof(jit_out_preserve) / 4,
407 
408      jit_in_preserve_size_in_4_byte_units = sizeof(jit_in_preserve) / 4
409   } jit_frame_constants;
410 
411 
412   // C2I adapter frames:
413   //
414   // STACK (interpreted called from compiled, on entry to template interpreter):
415   //
416   //       [TOP_C2I_FRAME]
417   //       [JIT_FRAME]
418   //       ...
419   //
420   //
421   // STACK (interpreted called from compiled, after interpreter has been pushed):
422   //
423   //       [TOP_IJAVA_FRAME]
424   //       [PARENT_C2I_FRAME]
425   //       [JIT_FRAME]
426   //       ...
427   //
428   //
429   // TOP_C2I_FRAME:
430   //
431   //       [TOP_IJAVA_FRAME_ABI]
432   //       [outgoing Java arguments]
433   //       alignment (optional)
434   //
435   //
436   // PARENT_C2I_FRAME:
437   //
438   //       [PARENT_IJAVA_FRAME_ABI]
439   //       alignment (optional)
440   //       [callee's locals w/o arguments]
441   //       [outgoing Java arguments]
442   //       alignment (optional)
443 
444  private:
445 
446 
447  #ifdef ASSERT
448   enum special_backlink_values : uint64_t {
449     NOT_FULLY_INITIALIZED = 0xDEADBEEF8
450   };
451   bool is_fully_initialized()       const { return (uint64_t)_fp != NOT_FULLY_INITIALIZED; }
452 #endif // ASSERT
453 
454   //  STACK:
455   //            ...
456   //            [THIS_FRAME]             <-- this._sp (stack pointer for this frame)
457   //            [CALLER_FRAME]           <-- this.fp() (_sp of caller's frame)
458   //            ...
459   //
460 
461   // NOTE: Stack pointer is now held in the base class, so remove it from here.
462 
463   // Needed by deoptimization.
464   union {
465     intptr_t* _unextended_sp;
466     int _offset_unextended_sp; // for use in stack-chunk frames
467   };
468 
469   // Frame pointer for this frame.
470   union {
471     intptr_t* _fp;  // frame pointer
472     int _offset_fp; // relative frame pointer for use in stack-chunk frames
473   };
474 
475  public:
476 
477   // Interface for all frames:
478 
479   // Accessors
480 
481   inline intptr_t* fp() const { assert_absolute(); return _fp; }
482   void set_fp(intptr_t* newfp)  { _fp = newfp; }
483   int offset_fp() const         { assert_offset();  return _offset_fp; }
484   void set_offset_fp(int value) { assert_on_heap(); _offset_fp = value; }
485 
486   // Mark a frame as not fully initialized. Must not be used for frames in the valid back chain.
487   void mark_not_fully_initialized() const { DEBUG_ONLY(own_abi()->callers_sp = NOT_FULLY_INITIALIZED;)  }
488 
489  private:
490 
491   // Initialize frame members (_pc and _sp must be given)
492   inline void setup();
493 
494  public:
495 
496   // Constructors
497   inline frame(intptr_t* sp, intptr_t* fp, address pc);
498   // To be used, if sp was not extended to match callee's calling convention.
499   inline frame(intptr_t* sp, address pc, intptr_t* unextended_sp = nullptr, intptr_t* fp = nullptr, CodeBlob* cb = nullptr);
500   inline frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map, bool on_heap);
501   inline frame(intptr_t* sp, intptr_t* unextended_sp, intptr_t* fp, address pc, CodeBlob* cb, const ImmutableOopMap* oop_map = nullptr);
502 
503   // Access frame via stack pointer.
504   inline intptr_t* sp_addr_at(int index) const  { return &sp()[index]; }
505   inline intptr_t  sp_at(     int index) const  { return *sp_addr_at(index); }
506 
507   // Access ABIs.
508   inline z_common_abi*  own_abi()     const { return (z_common_abi*) sp(); }
509   inline z_abi_160* callers_abi() const { return (z_abi_160*) fp(); }
510 
511  private:
512   address* sender_pc_addr(void) const;
513 
514  public:
515   template <typename RegisterMapT>
516   static void update_map_with_saved_link(RegisterMapT* map, intptr_t** link_addr);
517 
518   // template interpreter state
519   inline z_ijava_state* ijava_state_unchecked() const;
520 
521  public:
522   inline z_ijava_state* ijava_state() const;
523 
524   inline intptr_t* interpreter_frame_esp() const;
525   // Where z_ijava_state.esp is saved.
526   inline void interpreter_frame_set_esp(intptr_t* esp);
527   inline intptr_t* interpreter_frame_top_frame_sp();
528   inline void interpreter_frame_set_top_frame_sp(intptr_t* top_frame_sp);
529   inline void interpreter_frame_set_sender_sp(intptr_t* sender_sp);
530 #ifdef ASSERT
531   inline void interpreter_frame_set_magic();
532 #endif
533 
534   // monitors:
535 
536   // Next two functions read and write z_ijava_state.monitors.
537  private:
538   inline BasicObjectLock* interpreter_frame_monitors() const;
539 
540   // Where z_ijava_state.monitors is saved.
541   inline void interpreter_frame_set_monitors(BasicObjectLock* monitors);
542 
543  public:
544 
545   // Additional interface for entry frames:
546   inline z_entry_frame_locals* entry_frame_locals() const {
547     return (z_entry_frame_locals*) (((address) fp()) - z_entry_frame_locals_size);
548   }
549 
550  public:
551 
552   // Get caller pc from stack slot of gpr14.
553   address native_sender_pc() const;
554   // Get caller pc from stack slot of gpr10.
555   address callstub_sender_pc() const;
556 
557   // Dump all frames starting at a given C stack pointer.
558   // max_frames: Limit number of traced frames.
559   //             <= 0 --> full trace
560   //             > 0  --> trace the #max_frames topmost frames
561   static void back_trace(outputStream* st, intptr_t* start_sp, intptr_t* top_pc,
562                          unsigned long flags, int max_frames = 0);
563 
564   enum {
565     // size, in words, of frame metadata (e.g. pc and link)
566     metadata_words           = sizeof(z_java_abi) >> LogBytesPerWord,
567     metadata_words_at_bottom = 0,
568     metadata_words_at_top    = sizeof(z_java_abi) >> LogBytesPerWord,
569     // in bytes
570     frame_alignment          = 8,
571     // size, in words, of maximum shift in frame position due to alignment
572     align_wiggle             =  0,
573     // This is wrong and unimplemented
574     sender_sp_offset         =  0
575   };
576 
577   // returns the sending frame, without applying any barriers
578   inline frame sender_raw(RegisterMap* map) const;
579 
580   intptr_t* repair_sender_sp(intptr_t* sender_sp, intptr_t** saved_fp_addr) const;
581   static intptr_t* repair_sender_sp(nmethod* nm, intptr_t* sp, intptr_t** saved_fp_addr);
582   bool was_augmented_on_entry(int& real_size) const;
583 
584 #endif // CPU_S390_FRAME_S390_HPP