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  * Copyright (c) 2015, Linaro Ltd. 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 void MetaspaceShared::generate_vtable_methods(void** vtbl_list,
  56                                                    void** vtable,
  57                                                    char** md_top,
  58                                                    char* md_end,
  59                                                    char** mc_top,
  60                                                    char* mc_end) {
  61 
  62   intptr_t vtable_bytes = (num_virtuals * vtbl_list_size) * sizeof(void*);
  63   *(intptr_t *)(*md_top) = vtable_bytes;
  64   *md_top += sizeof(intptr_t);
  65   void** dummy_vtable = (void**)*md_top;
  66   *vtable = dummy_vtable;
  67   *md_top += vtable_bytes;
  68 
  69   // Get ready to generate dummy methods.
  70 
  71   CodeBuffer cb((unsigned char*)*mc_top, mc_end - *mc_top);
  72   MacroAssembler* masm = new MacroAssembler(&cb);
  73 
  74   // r12, lr, r9 registers are used as temporary registers
  75   // callee-saved registers (lr, r9) should be restored
  76   RegSet rset = RegSet::of(r9, lr);
  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 
  85       // Load r12 with a value indicating vtable/offset pair.
  86       // -- bits[ 7..0]  (8 bits) which virtual method in table?
  87       // -- bits[12..8]  (5 bits) which virtual method table?
  88       __ mov(r12, (i << 8) + j);
  89       __ b(common_code);
  90     }
  91   }
  92 
  93   __ bind(common_code);
  94 
  95   __ push(rset, sp);
  96   __ lsr(lr, r12, 8);            // isolate vtable identifier.
  97   __ mov(r9, (address)vtbl_list);      // address of list of vtable pointers.
  98   __ ldr(r9, Address(r9, lr, lsl(LogBytesPerWord))); // get correct vtable pointer.
  99   __ str(r9, Address(c_rarg0));        // update vtable pointer in obj.
 100   __ andr(r12, r12, 0xFF);
 101   __ add(r12, r9, r12, lsl(LogBytesPerWord)); // address of real method pointer.
 102   __ ldr(r12, Address(r12)); // get real method pointer.
 103   __ pop(rset, sp);
 104   __ b(r12);           // jump to the real method.
 105 
 106   *mc_top = (char*)__ pc();
 107 }