1 /*
   2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2015 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 #include "runtime/synchronizer.hpp"
  30 
  31   //  C frame layout on PPC-64.
  32   //
  33   //  In this figure the stack grows upwards, while memory grows
  34   //  downwards. See "64-bit PowerPC ELF ABI Supplement Version 1.7",
  35   //  IBM Corp. (2003-10-29)
  36   //  (http://math-atlas.sourceforge.net/devel/assembly/PPC-elf64abi-1.7.pdf).
  37   //
  38   //  Square brackets denote stack regions possibly larger
  39   //  than a single 64 bit slot.
  40   //
  41   //  STACK:
  42   //    0       [C_FRAME]               <-- SP after prolog (mod 16 = 0)
  43   //            [C_FRAME]               <-- SP before prolog
  44   //            ...
  45   //            [C_FRAME]
  46   //
  47   //  C_FRAME:
  48   //    0       [ABI_REG_ARGS]
  49   //    112     CARG_9: outgoing arg 9 (arg_1 ... arg_8 via gpr_3 ... gpr_{10})
  50   //            ...
  51   //    40+M*8  CARG_M: outgoing arg M (M is the maximum of outgoing args taken over all call sites in the procedure)
  52   //            local 1
  53   //            ...
  54   //            local N
  55   //            spill slot for vector reg (16 bytes aligned)
  56   //            ...
  57   //            spill slot for vector reg
  58   //            alignment       (4 or 12 bytes)
  59   //    V       SR_VRSAVE
  60   //    V+4     spill slot for GR
  61   //    ...     ...
  62   //            spill slot for GR
  63   //            spill slot for FR
  64   //            ...
  65   //            spill slot for FR
  66   //
  67   //  ABI_48:
  68   //    0       caller's SP
  69   //    8       space for condition register (CR) for next call
  70   //    16      space for link register (LR) for next call
  71   //    24      reserved
  72   //    32      reserved
  73   //    40      space for TOC (=R2) register for next call
  74   //
  75   //  ABI_REG_ARGS:
  76   //    0       [ABI_48]
  77   //    48      CARG_1: spill slot for outgoing arg 1. used by next callee.
  78   //    ...     ...
  79   //    104     CARG_8: spill slot for outgoing arg 8. used by next callee.
  80   //
  81 
  82  public:
  83 
  84   // C frame layout
  85   static const int alignment_in_bytes = 16;
  86 
  87   // ABI_MINFRAME:
  88   struct abi_minframe {
  89     uint64_t callers_sp;
  90     uint64_t cr;                                  //_16
  91     uint64_t lr;
  92 #if !defined(ABI_ELFv2)
  93     uint64_t reserved1;                           //_16
  94     uint64_t reserved2;
  95 #endif
  96     uint64_t toc;                                 //_16
  97     // nothing to add here!
  98     // aligned to frame::alignment_in_bytes (16)
  99   };
 100 
 101   enum {
 102     abi_minframe_size = sizeof(abi_minframe)
 103   };
 104 
 105   struct abi_reg_args : abi_minframe {
 106     uint64_t carg_1;
 107     uint64_t carg_2;                              //_16
 108     uint64_t carg_3;
 109     uint64_t carg_4;                              //_16
 110     uint64_t carg_5;
 111     uint64_t carg_6;                              //_16
 112     uint64_t carg_7;
 113     uint64_t carg_8;                              //_16
 114     // aligned to frame::alignment_in_bytes (16)
 115   };
 116 
 117   enum {
 118     abi_reg_args_size = sizeof(abi_reg_args)
 119   };
 120 
 121   #define _abi(_component) \
 122           (offset_of(frame::abi_reg_args, _component))
 123 
 124   struct abi_reg_args_spill : abi_reg_args {
 125     // additional spill slots
 126     uint64_t spill_ret;
 127     uint64_t spill_fret;                          //_16
 128     // aligned to frame::alignment_in_bytes (16)
 129   };
 130 
 131   enum {
 132     abi_reg_args_spill_size = sizeof(abi_reg_args_spill)
 133   };
 134 
 135   #define _abi_reg_args_spill(_component) \
 136           (offset_of(frame::abi_reg_args_spill, _component))
 137 
 138   // non-volatile GPRs:
 139 
 140   struct spill_nonvolatiles {
 141     uint64_t r14;
 142     uint64_t r15;                                 //_16
 143     uint64_t r16;
 144     uint64_t r17;                                 //_16
 145     uint64_t r18;
 146     uint64_t r19;                                 //_16
 147     uint64_t r20;
 148     uint64_t r21;                                 //_16
 149     uint64_t r22;
 150     uint64_t r23;                                 //_16
 151     uint64_t r24;
 152     uint64_t r25;                                 //_16
 153     uint64_t r26;
 154     uint64_t r27;                                 //_16
 155     uint64_t r28;
 156     uint64_t r29;                                 //_16
 157     uint64_t r30;
 158     uint64_t r31;                                 //_16
 159 
 160     double f14;
 161     double f15;
 162     double f16;
 163     double f17;
 164     double f18;
 165     double f19;
 166     double f20;
 167     double f21;
 168     double f22;
 169     double f23;
 170     double f24;
 171     double f25;
 172     double f26;
 173     double f27;
 174     double f28;
 175     double f29;
 176     double f30;
 177     double f31;
 178 
 179     // aligned to frame::alignment_in_bytes (16)
 180   };
 181 
 182   enum {
 183     spill_nonvolatiles_size = sizeof(spill_nonvolatiles)
 184   };
 185 
 186   #define _spill_nonvolatiles_neg(_component) \
 187      (int)(-frame::spill_nonvolatiles_size + offset_of(frame::spill_nonvolatiles, _component))
 188 
 189   // Frame layout for the Java template interpreter on PPC64.
 190   //
 191   // In these figures the stack grows upwards, while memory grows
 192   // downwards. Square brackets denote regions possibly larger than
 193   // single 64 bit slots.
 194   //
 195   //  STACK (interpreter is active):
 196   //    0       [TOP_IJAVA_FRAME]
 197   //            [PARENT_IJAVA_FRAME]
 198   //            ...
 199   //            [PARENT_IJAVA_FRAME]
 200   //            [ENTRY_FRAME]
 201   //            [C_FRAME]
 202   //            ...
 203   //            [C_FRAME]
 204   //
 205   //  With the following frame layouts:
 206   //  TOP_IJAVA_FRAME:
 207   //    0       [TOP_IJAVA_FRAME_ABI]
 208   //            alignment (optional)
 209   //            [operand stack]
 210   //            [monitors] (optional)
 211   //            [IJAVA_STATE]
 212   //            note: own locals are located in the caller frame.
 213   //
 214   //  PARENT_IJAVA_FRAME:
 215   //    0       [PARENT_IJAVA_FRAME_ABI]
 216   //            alignment (optional)
 217   //            [callee's Java result]
 218   //            [callee's locals w/o arguments]
 219   //            [outgoing arguments]
 220   //            [used part of operand stack w/o arguments]
 221   //            [monitors] (optional)
 222   //            [IJAVA_STATE]
 223   //
 224   //  ENTRY_FRAME:
 225   //    0       [PARENT_IJAVA_FRAME_ABI]
 226   //            alignment (optional)
 227   //            [callee's Java result]
 228   //            [callee's locals w/o arguments]
 229   //            [outgoing arguments]
 230   //            [ENTRY_FRAME_LOCALS]
 231 
 232   struct parent_ijava_frame_abi : abi_minframe {
 233   };
 234 
 235   enum {
 236     parent_ijava_frame_abi_size = sizeof(parent_ijava_frame_abi)
 237   };
 238 
 239 #define _parent_ijava_frame_abi(_component) \
 240         (offset_of(frame::parent_ijava_frame_abi, _component))
 241 
 242   struct top_ijava_frame_abi : abi_reg_args {
 243   };
 244 
 245   enum {
 246     top_ijava_frame_abi_size = sizeof(top_ijava_frame_abi)
 247   };
 248 
 249 #define _top_ijava_frame_abi(_component) \
 250         (offset_of(frame::top_ijava_frame_abi, _component))
 251 
 252   struct ijava_state {
 253     uint64_t method;
 254     uint64_t mirror;
 255     uint64_t locals;
 256     uint64_t monitors;
 257     uint64_t cpoolCache;
 258     uint64_t bcp;
 259     uint64_t esp;
 260     uint64_t mdx;
 261     uint64_t top_frame_sp; // Maybe define parent_frame_abi and move there.
 262     uint64_t sender_sp;
 263     // Slots only needed for native calls. Maybe better to move elsewhere.
 264     uint64_t oop_tmp;
 265     uint64_t lresult;
 266     uint64_t fresult;
 267   };
 268 
 269   enum {
 270     ijava_state_size = sizeof(ijava_state)
 271   };
 272 
 273 #define _ijava_state_neg(_component) \
 274         (int) (-frame::ijava_state_size + offset_of(frame::ijava_state, _component))
 275 
 276   // ENTRY_FRAME
 277 
 278   struct entry_frame_locals {
 279     uint64_t call_wrapper_address;
 280     uint64_t result_address;                      //_16
 281     uint64_t result_type;
 282     uint64_t arguments_tos_address;               //_16
 283     // aligned to frame::alignment_in_bytes (16)
 284     uint64_t r[spill_nonvolatiles_size/sizeof(uint64_t)];
 285   };
 286 
 287   enum {
 288     entry_frame_locals_size = sizeof(entry_frame_locals)
 289   };
 290 
 291   #define _entry_frame_locals_neg(_component) \
 292     (int)(-frame::entry_frame_locals_size + offset_of(frame::entry_frame_locals, _component))
 293 
 294 
 295   //  Frame layout for JIT generated methods
 296   //
 297   //  In these figures the stack grows upwards, while memory grows
 298   //  downwards. Square brackets denote regions possibly larger than single
 299   //  64 bit slots.
 300   //
 301   //  STACK (interpreted Java calls JIT generated Java):
 302   //          [JIT_FRAME]                                <-- SP (mod 16 = 0)
 303   //          [TOP_IJAVA_FRAME]
 304   //         ...
 305   //
 306   //  JIT_FRAME (is a C frame according to PPC-64 ABI):
 307   //          [out_preserve]
 308   //          [out_args]
 309   //          [spills]
 310   //          [pad_1]
 311   //          [monitor] (optional)
 312   //       ...
 313   //          [monitor] (optional)
 314   //          [pad_2]
 315   //          [in_preserve] added / removed by prolog / epilog
 316   //
 317 
 318   // JIT_ABI (TOP and PARENT)
 319 
 320   struct jit_abi {
 321     uint64_t callers_sp;
 322     uint64_t cr;
 323     uint64_t lr;
 324     uint64_t toc;
 325     // Nothing to add here!
 326     // NOT ALIGNED to frame::alignment_in_bytes (16).
 327   };
 328 
 329   struct jit_out_preserve : jit_abi {
 330     // Nothing to add here!
 331   };
 332 
 333   struct jit_in_preserve {
 334     // Nothing to add here!
 335   };
 336 
 337   enum {
 338     jit_out_preserve_size = sizeof(jit_out_preserve),
 339     jit_in_preserve_size  = sizeof(jit_in_preserve)
 340   };
 341 
 342   struct jit_monitor {
 343     uint64_t monitor[1];
 344   };
 345 
 346   enum {
 347     jit_monitor_size = sizeof(jit_monitor),
 348   };
 349 
 350  private:
 351 
 352   //  STACK:
 353   //            ...
 354   //            [THIS_FRAME]             <-- this._sp (stack pointer for this frame)
 355   //            [CALLER_FRAME]           <-- this.fp() (_sp of caller's frame)
 356   //            ...
 357   //
 358 
 359   // The frame's stack pointer before it has been extended by a c2i adapter;
 360   // needed by deoptimization
 361   intptr_t* _unextended_sp;
 362 
 363   // frame pointer for this frame
 364   intptr_t* _fp;
 365 
 366  public:
 367 
 368   // Accessors for fields
 369   intptr_t* fp() const { return _fp; }
 370 
 371   // Accessors for ABIs
 372   inline abi_minframe* own_abi()     const { return (abi_minframe*) _sp; }
 373   inline abi_minframe* callers_abi() const { return (abi_minframe*) _fp; }
 374 
 375  private:
 376 
 377   // Find codeblob and set deopt_state.
 378   inline void find_codeblob_and_set_pc_and_deopt_state(address pc);
 379 
 380  public:
 381 
 382   // Constructors
 383   inline frame(intptr_t* sp);
 384   inline frame(intptr_t* sp, address pc);
 385   inline frame(intptr_t* sp, address pc, intptr_t* unextended_sp);
 386 
 387  private:
 388 
 389   intptr_t* compiled_sender_sp(CodeBlob* cb) const;
 390   address*  compiled_sender_pc_addr(CodeBlob* cb) const;
 391   address*  sender_pc_addr(void) const;
 392 
 393  public:
 394 
 395   inline ijava_state* get_ijava_state() const;
 396   // Some convenient register frame setters/getters for deoptimization.
 397   inline intptr_t* interpreter_frame_esp() const;
 398   inline void interpreter_frame_set_cpcache(ConstantPoolCache* cp);
 399   inline void interpreter_frame_set_esp(intptr_t* esp);
 400   inline void interpreter_frame_set_top_frame_sp(intptr_t* top_frame_sp);
 401   inline void interpreter_frame_set_sender_sp(intptr_t* sender_sp);
 402 
 403   // Size of a monitor in bytes.
 404   static int interpreter_frame_monitor_size_in_bytes();
 405 
 406   // The size of a cInterpreter object.
 407   static inline int interpreter_frame_cinterpreterstate_size_in_bytes();
 408 
 409   // Additional interface for entry frames:
 410   inline entry_frame_locals* get_entry_frame_locals() const {
 411     return (entry_frame_locals*) (((address) fp()) - entry_frame_locals_size);
 412   }
 413 
 414   enum {
 415     // normal return address is 1 bundle past PC
 416     pc_return_offset = 0
 417   };
 418 
 419   static jint interpreter_frame_expression_stack_direction() { return -1; }
 420 
 421 #endif // CPU_PPC_FRAME_PPC_HPP