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