0N/A/*
1564N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "code/vtableStubs.hpp"
1879N/A#include "compiler/disassembler.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "oops/instanceKlass.hpp"
1879N/A#include "oops/klassVtable.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "prims/forte.hpp"
1879N/A#include "prims/jvmtiExport.hpp"
1879N/A#include "runtime/handles.inline.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "runtime/sharedRuntime.hpp"
1879N/A#ifdef COMPILER2
1879N/A#include "opto/matcher.hpp"
1879N/A#endif
0N/A
0N/A// -----------------------------------------------------------------------------------------
0N/A// Implementation of VtableStub
0N/A
0N/Aaddress VtableStub::_chunk = NULL;
0N/Aaddress VtableStub::_chunk_end = NULL;
0N/AVMReg VtableStub::_receiver_location = VMRegImpl::Bad();
0N/A
0N/Astatic int num_vtable_chunks = 0;
0N/A
0N/A
0N/Avoid* VtableStub::operator new(size_t size, int code_size) {
0N/A assert(size == sizeof(VtableStub), "mismatched size");
0N/A num_vtable_chunks++;
0N/A // compute real VtableStub size (rounded to nearest word)
0N/A const int real_size = round_to(code_size + sizeof(VtableStub), wordSize);
0N/A // malloc them in chunks to minimize header overhead
0N/A const int chunk_factor = 32;
0N/A if (_chunk == NULL || _chunk + real_size > _chunk_end) {
0N/A const int bytes = chunk_factor * real_size + pd_code_alignment();
0N/A BufferBlob* blob = BufferBlob::create("vtable chunks", bytes);
1410N/A if (blob == NULL) {
1410N/A vm_exit_out_of_memory(bytes, "CodeCache: no room for vtable chunks");
1410N/A }
1668N/A _chunk = blob->content_begin();
0N/A _chunk_end = _chunk + bytes;
0N/A Forte::register_stub("vtable stub", _chunk, _chunk_end);
0N/A // Notify JVMTI about this stub. The event will be recorded by the enclosing
0N/A // JvmtiDynamicCodeEventCollector and posted when this thread has released
0N/A // all locks.
0N/A if (JvmtiExport::should_post_dynamic_code_generated()) {
0N/A JvmtiExport::post_dynamic_code_generated_while_holding_locks("vtable stub", _chunk, _chunk_end);
0N/A }
0N/A align_chunk();
0N/A }
0N/A assert(_chunk + real_size <= _chunk_end, "bad allocation");
0N/A void* res = _chunk;
0N/A _chunk += real_size;
0N/A align_chunk();
0N/A return res;
0N/A}
0N/A
0N/A
1601N/Avoid VtableStub::print_on(outputStream* st) const {
1601N/A st->print("vtable stub (index = %d, receiver_location = %d, code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)",
0N/A index(), receiver_location(), code_begin(), code_end());
0N/A}
0N/A
0N/A
0N/A// -----------------------------------------------------------------------------------------
0N/A// Implementation of VtableStubs
0N/A//
0N/A// For each hash value there's a linked list of vtable stubs (with that
0N/A// hash value). Each list is anchored in a little hash _table, indexed
0N/A// by that hash value.
0N/A
0N/AVtableStub* VtableStubs::_table[VtableStubs::N];
0N/Aint VtableStubs::_number_of_vtable_stubs = 0;
0N/A
0N/A
0N/Avoid VtableStubs::initialize() {
0N/A VtableStub::_receiver_location = SharedRuntime::name_for_receiver();
0N/A {
0N/A MutexLocker ml(VtableStubs_lock);
0N/A assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once");
0N/A assert(is_power_of_2(N), "N must be a power of 2");
0N/A for (int i = 0; i < N; i++) {
0N/A _table[i] = NULL;
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Aaddress VtableStubs::create_stub(bool is_vtable_stub, int vtable_index, methodOop method) {
0N/A assert(vtable_index >= 0, "must be positive");
0N/A
0N/A VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL;
0N/A if (s == NULL) {
0N/A if (is_vtable_stub) {
0N/A s = create_vtable_stub(vtable_index);
0N/A } else {
0N/A s = create_itable_stub(vtable_index);
0N/A }
0N/A enter(is_vtable_stub, vtable_index, s);
0N/A if (PrintAdapterHandlers) {
0N/A tty->print_cr("Decoding VtableStub %s[%d]@%d",
0N/A is_vtable_stub? "vtbl": "itbl", vtable_index, VtableStub::receiver_location());
0N/A Disassembler::decode(s->code_begin(), s->code_end());
0N/A }
0N/A }
0N/A return s->entry_point();
0N/A}
0N/A
0N/A
0N/Ainline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){
0N/A // Assumption: receiver_location < 4 in most cases.
0N/A int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index;
0N/A return (is_vtable_stub ? ~hash : hash) & mask;
0N/A}
0N/A
0N/A
0N/AVtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) {
0N/A MutexLocker ml(VtableStubs_lock);
0N/A unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index);
0N/A VtableStub* s = _table[hash];
0N/A while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next();
0N/A return s;
0N/A}
0N/A
0N/A
0N/Avoid VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) {
0N/A MutexLocker ml(VtableStubs_lock);
0N/A assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub");
0N/A unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index);
0N/A // enter s at the beginning of the corresponding list
0N/A s->set_next(_table[h]);
0N/A _table[h] = s;
0N/A _number_of_vtable_stubs++;
0N/A}
0N/A
0N/A
0N/Abool VtableStubs::is_entry_point(address pc) {
0N/A MutexLocker ml(VtableStubs_lock);
0N/A VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset());
0N/A uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index());
0N/A VtableStub* s;
0N/A for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {}
0N/A return s == stub;
0N/A}
0N/A
0N/A
0N/Abool VtableStubs::contains(address pc) {
0N/A // simple solution for now - we may want to use
0N/A // a faster way if this function is called often
0N/A return stub_containing(pc) != NULL;
0N/A}
0N/A
0N/A
0N/AVtableStub* VtableStubs::stub_containing(address pc) {
0N/A // Note: No locking needed since any change to the data structure
0N/A // happens with an atomic store into it (we don't care about
0N/A // consistency with the _number_of_vtable_stubs counter).
0N/A for (int i = 0; i < N; i++) {
0N/A for (VtableStub* s = _table[i]; s != NULL; s = s->next()) {
0N/A if (s->contains(pc)) return s;
0N/A }
0N/A }
0N/A return NULL;
0N/A}
0N/A
0N/Avoid vtableStubs_init() {
0N/A VtableStubs::initialize();
0N/A}
0N/A
0N/A
0N/A//-----------------------------------------------------------------------------------------------------
0N/A// Non-product code
0N/A#ifndef PRODUCT
0N/A
0N/Aextern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) {
0N/A ResourceMark rm;
0N/A HandleMark hm;
0N/A klassOop klass = receiver->klass();
0N/A instanceKlass* ik = instanceKlass::cast(klass);
0N/A klassVtable* vt = ik->vtable();
0N/A klass->print();
1410N/A fatal(err_msg("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", "
1410N/A "index %d (vtable length %d)",
1410N/A (address)receiver, index, vt->length()));
0N/A}
0N/A
0N/A#endif // Product