1 /*
   2  * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. 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 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.hpp"
  28 #include "memory/metaspaceShared.hpp"
  29 
  30 // Generate the self-patching vtable method:
  31 //
  32 // This method will be called (as any other Klass virtual method) with
  33 // the Klass itself as the first argument.  Example:
  34 //
  35 //      oop obj;
  36 //      int size = obj->klass()->oop_size(this);
  37 //
  38 // for which the virtual method call is Klass::oop_size();
  39 //
  40 // The dummy method is called with the Klass object as the first
  41 // operand, and an object as the second argument.
  42 //
  43 
  44 //=====================================================================
  45 
  46 // All of the dummy methods in the vtable are essentially identical,
  47 // differing only by an ordinal constant, and they bear no relationship
  48 // to the original method which the caller intended. Also, there needs
  49 // to be 'vtbl_list_size' instances of the vtable in order to
  50 // differentiate between the 'vtable_list_size' original Klass objects.
  51 
  52 #define __ masm->
  53 
  54 extern "C" {
  55   void aarch64_prolog(void);
  56 }
  57 
  58 void MetaspaceShared::generate_vtable_methods(void** vtbl_list,
  59                                                    void** vtable,
  60                                                    char** md_top,
  61                                                    char* md_end,
  62                                                    char** mc_top,
  63                                                    char* mc_end) {
  64 
  65 #ifdef BUILTIN_SIM
  66   // Write a dummy word to the writable shared metaspace.
  67   // MetaspaceShared::initialize_shared_spaces will fill it with the
  68   // address of aarch64_prolog().
  69   address *prolog_ptr = (address*)*md_top;
  70   *(intptr_t *)(*md_top) = (intptr_t)0;
  71   (*md_top) += sizeof(intptr_t);
  72 #endif
  73 
  74   intptr_t vtable_bytes = (num_virtuals * vtbl_list_size) * sizeof(void*);
  75   *(intptr_t *)(*md_top) = vtable_bytes;
  76   *md_top += sizeof(intptr_t);
  77   void** dummy_vtable = (void**)*md_top;
  78   *vtable = dummy_vtable;
  79   *md_top += vtable_bytes;
  80 
  81   // Get ready to generate dummy methods.
  82 
  83   CodeBuffer cb((unsigned char*)*mc_top, mc_end - *mc_top);
  84   MacroAssembler* masm = new MacroAssembler(&cb);
  85 
  86   Label common_code;
  87   for (int i = 0; i < vtbl_list_size; ++i) {
  88     for (int j = 0; j < num_virtuals; ++j) {
  89       dummy_vtable[num_virtuals * i + j] = (void*)masm->pc();
  90 
  91       // We're called directly from C code.
  92 #ifdef BUILTIN_SIM
  93       __ c_stub_prolog(8, 0, MacroAssembler::ret_type_integral, prolog_ptr);
  94 #endif
  95       // Load rscratch1 with a value indicating vtable/offset pair.
  96       // -- bits[ 7..0]  (8 bits) which virtual method in table?
  97       // -- bits[12..8]  (5 bits) which virtual method table?
  98       __ mov(rscratch1, (i << 8) + j);
  99       __ b(common_code);
 100     }
 101   }
 102 
 103   __ bind(common_code);
 104 
 105   Register tmp0 = r10, tmp1 = r11;       // AAPCS64 temporary registers
 106   __ enter();
 107   __ lsr(tmp0, rscratch1, 8);            // isolate vtable identifier.
 108   __ mov(tmp1, (address)vtbl_list);      // address of list of vtable pointers.
 109   __ ldr(tmp1, Address(tmp1, tmp0, Address::lsl(LogBytesPerWord))); // get correct vtable pointer.
 110   __ str(tmp1, Address(c_rarg0));        // update vtable pointer in obj.
 111   __ add(rscratch1, tmp1, rscratch1, ext::uxtb, LogBytesPerWord); // address of real method pointer.
 112   __ ldr(rscratch1, Address(rscratch1)); // get real method pointer.
 113   __ blrt(rscratch1, 8, 0, 1);           // jump to the real method.
 114   __ leave();
 115   __ ret(lr);
 116 
 117   *mc_top = (char*)__ pc();
 118 }
 119 
 120 #ifdef BUILTIN_SIM
 121 void MetaspaceShared::relocate_vtbl_list(char **buffer) {
 122   void **sim_entry = (void**)*buffer;
 123   *sim_entry = (void*)aarch64_prolog;
 124   *buffer += sizeof(intptr_t);
 125 }
 126 #endif