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