1 /*
   2  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // Platform-specific definitions for method handles.
  26 // These definitions are inlined into class MethodHandles.
  27 
  28 public:
  29 
  30 // The stack just after the recursive call from a ricochet frame
  31 // looks something like this.  Offsets are marked in words, not bytes.
  32 // rsi (r13 on LP64) is part of the interpreter calling sequence
  33 // which tells the callee where is my real rsp (for frame walking).
  34 // (...lower memory addresses)
  35 // rsp:     [ return pc                 ]   always the global RicochetBlob::bounce_addr
  36 // rsp+1:   [ recursive arg N           ]
  37 // rsp+2:   [ recursive arg N-1         ]
  38 // ...
  39 // rsp+N:   [ recursive arg 1           ]
  40 // rsp+N+1: [ recursive method handle   ]
  41 // ...
  42 // rbp-6:   [ cleanup continuation pc   ]   <-- (struct RicochetFrame)
  43 // rbp-5:   [ saved target MH           ]   the MH we will call on the saved args
  44 // rbp-4:   [ saved args layout oop     ]   an int[] array which describes argument layout
  45 // rbp-3:   [ saved args pointer        ]   address of transformed adapter arg M (slot 0)
  46 // rbp-2:   [ conversion                ]   information about how the return value is used
  47 // rbp-1:   [ exact sender sp           ]   exact TOS (rsi/r13) of original sender frame
  48 // rbp+0:   [ saved sender fp           ]   (for original sender of AMH)
  49 // rbp+1:   [ saved sender pc           ]   (back to original sender of AMH)
  50 // rbp+2:   [ transformed adapter arg M ]   <-- (extended TOS of original sender)
  51 // rbp+3:   [ transformed adapter arg M-1]
  52 // ...
  53 // rbp+M+1: [ transformed adapter arg 1 ]
  54 // rbp+M+2: [ padding                   ] <-- (rbp + saved args base offset)
  55 // ...      [ optional padding]
  56 // (higher memory addresses...)
  57 //
  58 // The arguments originally passed by the original sender
  59 // are lost, and arbitrary amounts of stack motion might have
  60 // happened due to argument transformation.
  61 // (This is done by C2I/I2C adapters and non-direct method handles.)
  62 // This is why there is an unpredictable amount of memory between
  63 // the extended and exact TOS of the sender.
  64 // The ricochet adapter itself will also (in general) perform
  65 // transformations before the recursive call.
  66 //
  67 // The transformed and saved arguments, immediately above the saved
  68 // return PC, are a well-formed method handle invocation ready to execute.
  69 // When the GC needs to walk the stack, these arguments are described
  70 // via the saved arg types oop, an int[] array with a private format.
  71 // This array is derived from the type of the transformed adapter
  72 // method handle, which also sits at the base of the saved argument
  73 // bundle.  Since the GC may not be able to fish out the int[]
  74 // array, so it is pushed explicitly on the stack.  This may be
  75 // an unnecessary expense.
  76 //
  77 // The following register conventions are significant at this point:
  78 // rsp       the thread stack, as always; preserved by caller
  79 // rsi/r13   exact TOS of recursive frame (contents of [rbp-2])
  80 // rcx       recursive method handle (contents of [rsp+N+1])
  81 // rbp       preserved by caller (not used by caller)
  82 // Unless otherwise specified, all registers can be blown by the call.
  83 //
  84 // If this frame must be walked, the transformed adapter arguments
  85 // will be found with the help of the saved arguments descriptor.
  86 //
  87 // Therefore, the descriptor must match the referenced arguments.
  88 // The arguments must be followed by at least one word of padding,
  89 // which will be necessary to complete the final method handle call.
  90 // That word is not treated as holding an oop.  Neither is the word
  91 //
  92 // The word pointed to by the return argument pointer is not
  93 // treated as an oop, even if points to a saved argument.
  94 // This allows the saved argument list to have a "hole" in it
  95 // to receive an oop from the recursive call.
  96 // (The hole might temporarily contain RETURN_VALUE_PLACEHOLDER.)
  97 //
  98 // When the recursive callee returns, RicochetBlob::bounce_addr will
  99 // immediately jump to the continuation stored in the RF.
 100 // This continuation will merge the recursive return value
 101 // into the saved argument list.  At that point, the original
 102 // rsi, rbp, and rsp will be reloaded, the ricochet frame will
 103 // disappear, and the final target of the adapter method handle
 104 // will be invoked on the transformed argument list.
 105 
 106 class RicochetFrame {
 107   friend class MethodHandles;
 108 
 109  private:
 110   intptr_t* _continuation;          // what to do when control gets back here
 111   oopDesc*  _saved_target;          // target method handle to invoke on saved_args
 112   oopDesc*  _saved_args_layout;     // caching point for MethodTypeForm.vmlayout cookie
 113   intptr_t* _saved_args_base;       // base of pushed arguments (slot 0, arg N) (-3)
 114   intptr_t  _conversion;            // misc. information from original AdapterMethodHandle (-2)
 115   intptr_t* _exact_sender_sp;       // parallel to interpreter_frame_sender_sp (-1)
 116   intptr_t* _sender_link;           // *must* coincide with frame::link_offset (0)
 117   address   _sender_pc;             // *must* coincide with frame::return_addr_offset (1)
 118 
 119  public:
 120   intptr_t* continuation() const        { return _continuation; }
 121   oop       saved_target() const        { return _saved_target; }
 122   oop       saved_args_layout() const   { return _saved_args_layout; }
 123   intptr_t* saved_args_base() const     { return _saved_args_base; }
 124   intptr_t  conversion() const          { return _conversion; }
 125   intptr_t* exact_sender_sp() const     { return _exact_sender_sp; }
 126   intptr_t* sender_link() const         { return _sender_link; }
 127   address   sender_pc() const           { return _sender_pc; }
 128 
 129   intptr_t* extended_sender_sp() const  { return saved_args_base(); }
 130 
 131   intptr_t  return_value_slot_number() const {
 132     return adapter_conversion_vminfo(conversion());
 133   }
 134   BasicType return_value_type() const {
 135     return adapter_conversion_dest_type(conversion());
 136   }
 137   bool has_return_value_slot() const {
 138     return return_value_type() != T_VOID;
 139   }
 140   intptr_t* return_value_slot_addr() const {
 141     assert(has_return_value_slot(), "");
 142     return saved_arg_slot_addr(return_value_slot_number());
 143   }
 144   intptr_t* saved_target_slot_addr() const {
 145     return saved_arg_slot_addr(saved_args_length());
 146   }
 147   intptr_t* saved_arg_slot_addr(int slot) const {
 148     assert(slot >= 0, "");
 149     return (intptr_t*)( (address)saved_args_base() + (slot * Interpreter::stackElementSize) );
 150   }
 151 
 152   jint      saved_args_length() const;
 153   jint      saved_arg_offset(int arg) const;
 154 
 155   // GC interface
 156   oop*  saved_target_addr()                     { return (oop*)&_saved_target; }
 157   oop*  saved_args_layout_addr()                { return (oop*)&_saved_args_layout; }
 158 
 159   oop  compute_saved_args_layout(bool read_cache, bool write_cache);
 160 
 161   // Compiler/assembler interface.
 162   static int continuation_offset_in_bytes()     { return offset_of(RicochetFrame, _continuation); }
 163   static int saved_target_offset_in_bytes()     { return offset_of(RicochetFrame, _saved_target); }
 164   static int saved_args_layout_offset_in_bytes(){ return offset_of(RicochetFrame, _saved_args_layout); }
 165   static int saved_args_base_offset_in_bytes()  { return offset_of(RicochetFrame, _saved_args_base); }
 166   static int conversion_offset_in_bytes()       { return offset_of(RicochetFrame, _conversion); }
 167   static int exact_sender_sp_offset_in_bytes()  { return offset_of(RicochetFrame, _exact_sender_sp); }
 168   static int sender_link_offset_in_bytes()      { return offset_of(RicochetFrame, _sender_link); }
 169   static int sender_pc_offset_in_bytes()        { return offset_of(RicochetFrame, _sender_pc); }
 170 
 171   // This value is not used for much, but it apparently must be nonzero.
 172   static int frame_size_in_bytes()              { return sender_link_offset_in_bytes(); }
 173 
 174 #ifdef ASSERT
 175   // The magic number is supposed to help find ricochet frames within the bytes of stack dumps.
 176   enum { MAGIC_NUMBER_1 = 0xFEED03E, MAGIC_NUMBER_2 = 0xBEEF03E };
 177   static int magic_number_1_offset_in_bytes()   { return -wordSize; }
 178   static int magic_number_2_offset_in_bytes()   { return sizeof(RicochetFrame); }
 179   intptr_t magic_number_1() const               { return *(intptr_t*)((address)this + magic_number_1_offset_in_bytes()); };
 180   intptr_t magic_number_2() const               { return *(intptr_t*)((address)this + magic_number_2_offset_in_bytes()); };
 181 #endif //ASSERT
 182 
 183   enum { RETURN_VALUE_PLACEHOLDER = (NOT_DEBUG(0) DEBUG_ONLY(42)) };
 184 
 185   static void verify_offsets() NOT_DEBUG_RETURN;
 186   void verify() const NOT_DEBUG_RETURN; // check for MAGIC_NUMBER, etc.
 187   void zap_arguments() NOT_DEBUG_RETURN;
 188 
 189   static void generate_ricochet_blob(MacroAssembler* _masm,
 190                                      // output params:
 191                                      int* frame_size_in_words, int* bounce_offset, int* exception_offset);
 192 
 193   static void enter_ricochet_frame(MacroAssembler* _masm,
 194                                    Register rcx_recv,
 195                                    Register rax_argv,
 196                                    address return_handler,
 197                                    Register rbx_temp);
 198   static void leave_ricochet_frame(MacroAssembler* _masm,
 199                                    Register rcx_recv,
 200                                    Register new_sp_reg,
 201                                    Register sender_pc_reg);
 202 
 203   static Address frame_address(int offset = 0) {
 204     // The RicochetFrame is found by subtracting a constant offset from rbp.
 205     return Address(rbp, - sender_link_offset_in_bytes() + offset);
 206   }
 207 
 208   static RicochetFrame* from_frame(const frame& fr) {
 209     address bp = (address) fr.fp();
 210     RicochetFrame* rf = (RicochetFrame*)(bp - sender_link_offset_in_bytes());
 211     rf->verify();
 212     return rf;
 213   }
 214 
 215   static void verify_clean(MacroAssembler* _masm) NOT_DEBUG_RETURN;
 216 };
 217 
 218 // Additional helper methods for MethodHandles code generation:
 219 public:
 220   static void load_klass_from_Class(MacroAssembler* _masm, Register klass_reg);
 221   static void load_conversion_vminfo(MacroAssembler* _masm, Register reg, Address conversion_field_addr);
 222   static void load_conversion_dest_type(MacroAssembler* _masm, Register reg, Address conversion_field_addr);
 223 
 224   static void load_stack_move(MacroAssembler* _masm,
 225                               Register rdi_stack_move,
 226                               Register rcx_amh,
 227                               bool might_be_negative);
 228 
 229   static void insert_arg_slots(MacroAssembler* _masm,
 230                                RegisterOrConstant arg_slots,
 231                                Register rax_argslot,
 232                                Register rbx_temp, Register rdx_temp);
 233 
 234   static void remove_arg_slots(MacroAssembler* _masm,
 235                                RegisterOrConstant arg_slots,
 236                                Register rax_argslot,
 237                                Register rbx_temp, Register rdx_temp);
 238 
 239   static void push_arg_slots(MacroAssembler* _masm,
 240                                    Register rax_argslot,
 241                                    RegisterOrConstant slot_count,
 242                                    int skip_words_count,
 243                                    Register rbx_temp, Register rdx_temp);
 244 
 245   static void move_arg_slots_up(MacroAssembler* _masm,
 246                                 Register rbx_bottom,  // invariant
 247                                 Address  top_addr,    // can use rax_temp
 248                                 RegisterOrConstant positive_distance_in_slots,
 249                                 Register rax_temp, Register rdx_temp);
 250 
 251   static void move_arg_slots_down(MacroAssembler* _masm,
 252                                   Address  bottom_addr,  // can use rax_temp
 253                                   Register rbx_top,      // invariant
 254                                   RegisterOrConstant negative_distance_in_slots,
 255                                   Register rax_temp, Register rdx_temp);
 256 
 257   static void move_typed_arg(MacroAssembler* _masm,
 258                              BasicType type, bool is_element,
 259                              Address slot_dest, Address value_src,
 260                              Register rbx_temp, Register rdx_temp);
 261 
 262   static void move_return_value(MacroAssembler* _masm, BasicType type,
 263                                 Address return_slot);
 264 
 265   static void verify_argslot(MacroAssembler* _masm, Register argslot_reg,
 266                              const char* error_message) NOT_DEBUG_RETURN;
 267 
 268   static void verify_argslots(MacroAssembler* _masm,
 269                               RegisterOrConstant argslot_count,
 270                               Register argslot_reg,
 271                               bool negate_argslot,
 272                               const char* error_message) NOT_DEBUG_RETURN;
 273 
 274   static void verify_stack_move(MacroAssembler* _masm,
 275                                 RegisterOrConstant arg_slots,
 276                                 int direction) NOT_DEBUG_RETURN;
 277 
 278   static void verify_klass(MacroAssembler* _masm,
 279                            Register obj, KlassHandle klass,
 280                            const char* error_message = "wrong klass") NOT_DEBUG_RETURN;
 281 
 282   static void verify_method_handle(MacroAssembler* _masm, Register mh_reg) {
 283     verify_klass(_masm, mh_reg, SystemDictionaryHandles::MethodHandle_klass(),
 284                  "reference is a MH");
 285   }
 286 
 287   static void trace_method_handle(MacroAssembler* _masm, const char* adaptername) PRODUCT_RETURN;
 288 
 289   static Register saved_last_sp_register() {
 290     // Should be in sharedRuntime, not here.
 291     return LP64_ONLY(r13) NOT_LP64(rsi);
 292   }