1 /*
   2  * Copyright (c) 2013, Red Hat Inc.
   3  * Copyright (c) 2004, 2012, Oracle and/or its affiliates.
   4  * All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "asm/macroAssembler.hpp"
  29 #include "memory/metaspaceShared.hpp"
  30 
  31 // Generate the self-patching vtable method:
  32 //
  33 // This method will be called (as any other Klass virtual method) with
  34 // the Klass itself as the first argument.  Example:
  35 //
  36 //      oop obj;
  37 //      int size = obj->klass()->oop_size(this);
  38 //
  39 // for which the virtual method call is Klass::oop_size();
  40 //
  41 // The dummy method is called with the Klass object as the first
  42 // operand, and an object as the second argument.
  43 //
  44 
  45 //=====================================================================
  46 
  47 // All of the dummy methods in the vtable are essentially identical,
  48 // differing only by an ordinal constant, and they bear no relationship
  49 // to the original method which the caller intended. Also, there needs
  50 // to be 'vtbl_list_size' instances of the vtable in order to
  51 // differentiate between the 'vtable_list_size' original Klass objects.
  52 
  53 #define __ masm->
  54 
  55 extern "C" {
  56   void aarch64_prolog(void);
  57 }
  58 
  59 void MetaspaceShared::generate_vtable_methods(void** vtbl_list,
  60                                                    void** vtable,
  61                                                    char** md_top,
  62                                                    char* md_end,
  63                                                    char** mc_top,
  64                                                    char* mc_end) {
  65 
  66   intptr_t vtable_bytes = (num_virtuals * vtbl_list_size) * sizeof(void*);
  67   *(intptr_t *)(*md_top) = vtable_bytes;
  68   *md_top += sizeof(intptr_t);
  69   void** dummy_vtable = (void**)*md_top;
  70   *vtable = dummy_vtable;
  71   *md_top += vtable_bytes;
  72 
  73   // Get ready to generate dummy methods.
  74 
  75   CodeBuffer cb((unsigned char*)*mc_top, mc_end - *mc_top);
  76   MacroAssembler* masm = new MacroAssembler(&cb);
  77 
  78   Label common_code;
  79   for (int i = 0; i < vtbl_list_size; ++i) {
  80     for (int j = 0; j < num_virtuals; ++j) {
  81       dummy_vtable[num_virtuals * i + j] = (void*)masm->pc();
  82 
  83       // We're called directly from C code.
  84       // Load rscratch1 with a value indicating vtable/offset pair.
  85       // -- bits[ 7..0]  (8 bits) which virtual method in table?
  86       // -- bits[12..8]  (5 bits) which virtual method table?
  87       __ mov(rscratch1, (i << 8) + j);
  88       __ b(common_code);
  89     }
  90   }
  91 
  92   __ bind(common_code);
  93 
  94   Register tmp0 = r10, tmp1 = r11;       // AAPCS64 temporary registers
  95   __ enter();
  96   __ lsr(tmp0, rscratch1, 8);            // isolate vtable identifier.
  97   __ mov(tmp1, (address)vtbl_list);      // address of list of vtable pointers.
  98   __ ldr(tmp1, Address(tmp1, tmp0, Address::lsl(LogBytesPerWord))); // get correct vtable pointer.
  99   __ str(tmp1, Address(c_rarg0));        // update vtable pointer in obj.
 100   __ add(rscratch1, tmp1, rscratch1, ext::uxtb, LogBytesPerWord); // address of real method pointer.
 101   __ ldr(rscratch1, Address(rscratch1)); // get real method pointer.
 102   __ blr(rscratch1);                     // jump to the real method.
 103   __ leave();
 104   __ ret(lr);
 105 
 106   *mc_top = (char*)__ pc();
 107 }
 108