hotspot/src/share/vm/code/vtableStubs.cpp
author ant
Fri, 04 Dec 2009 15:07:15 +0300
changeset 4369 18b883ed2b58
parent 3261 c7d5aae8d3f7
child 5403 6b0dd9c75dde
permissions -rw-r--r--
6903354: deadlock involving Component.show & SunToolkit.getImageFromHash Reviewed-by: art, bae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
3261
c7d5aae8d3f7 6862919: Update copyright year
xdono
parents: 2533
diff changeset
     2
 * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#include "incls/_vtableStubs.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// -----------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// Implementation of VtableStub
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
address VtableStub::_chunk             = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
address VtableStub::_chunk_end         = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
VMReg   VtableStub::_receiver_location = VMRegImpl::Bad();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
static int num_vtable_chunks = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
void* VtableStub::operator new(size_t size, int code_size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  assert(size == sizeof(VtableStub), "mismatched size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  num_vtable_chunks++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  // compute real VtableStub size (rounded to nearest word)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  const int real_size = round_to(code_size + sizeof(VtableStub), wordSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  // malloc them in chunks to minimize header overhead
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  const int chunk_factor = 32;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  if (_chunk == NULL || _chunk + real_size > _chunk_end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    const int bytes = chunk_factor * real_size + pd_code_alignment();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    BufferBlob* blob = BufferBlob::create("vtable chunks", bytes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    if( blob == NULL ) vm_exit_out_of_memory1(bytes, "CodeCache: no room for %s", "vtable chunks");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    _chunk = blob->instructions_begin();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    _chunk_end = _chunk + bytes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    VTune::register_stub("vtable stub", _chunk, _chunk_end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    Forte::register_stub("vtable stub", _chunk, _chunk_end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    // Notify JVMTI about this stub. The event will be recorded by the enclosing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    // JvmtiDynamicCodeEventCollector and posted when this thread has released
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    // all locks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    if (JvmtiExport::should_post_dynamic_code_generated()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
      JvmtiExport::post_dynamic_code_generated_while_holding_locks("vtable stub", _chunk, _chunk_end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    align_chunk();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  assert(_chunk + real_size <= _chunk_end, "bad allocation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  void* res = _chunk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  _chunk += real_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  align_chunk();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
 return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
void VtableStub::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  tty->print("vtable stub (index = %d, receiver_location = %d, code = [" INTPTR_FORMAT ", " INTPTR_FORMAT "[)",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
             index(), receiver_location(), code_begin(), code_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
// -----------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
// Implementation of VtableStubs
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
// For each hash value there's a linked list of vtable stubs (with that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
// hash value). Each list is anchored in a little hash _table, indexed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
// by that hash value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
VtableStub* VtableStubs::_table[VtableStubs::N];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
int VtableStubs::_number_of_vtable_stubs = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
void VtableStubs::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  VtableStub::_receiver_location = SharedRuntime::name_for_receiver();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    MutexLocker ml(VtableStubs_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    assert(_number_of_vtable_stubs == 0, "potential performance bug: VtableStubs initialized more than once");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    assert(is_power_of_2(N), "N must be a power of 2");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    for (int i = 0; i < N; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      _table[i] = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
address VtableStubs::create_stub(bool is_vtable_stub, int vtable_index, methodOop method) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  assert(vtable_index >= 0, "must be positive");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  VtableStub* s = ShareVtableStubs ? lookup(is_vtable_stub, vtable_index) : NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  if (s == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    if (is_vtable_stub) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
      s = create_vtable_stub(vtable_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
      s = create_itable_stub(vtable_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    enter(is_vtable_stub, vtable_index, s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    if (PrintAdapterHandlers) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      tty->print_cr("Decoding VtableStub %s[%d]@%d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
                    is_vtable_stub? "vtbl": "itbl", vtable_index, VtableStub::receiver_location());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      Disassembler::decode(s->code_begin(), s->code_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  return s->entry_point();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
inline uint VtableStubs::hash(bool is_vtable_stub, int vtable_index){
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  // Assumption: receiver_location < 4 in most cases.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  int hash = ((vtable_index << 2) ^ VtableStub::receiver_location()->value()) + vtable_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  return (is_vtable_stub ? ~hash : hash)  & mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
VtableStub* VtableStubs::lookup(bool is_vtable_stub, int vtable_index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  MutexLocker ml(VtableStubs_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  unsigned hash = VtableStubs::hash(is_vtable_stub, vtable_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  VtableStub* s = _table[hash];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  while( s && !s->matches(is_vtable_stub, vtable_index)) s = s->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  return s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
void VtableStubs::enter(bool is_vtable_stub, int vtable_index, VtableStub* s) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  MutexLocker ml(VtableStubs_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  assert(s->matches(is_vtable_stub, vtable_index), "bad vtable stub");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  unsigned int h = VtableStubs::hash(is_vtable_stub, vtable_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  // enter s at the beginning of the corresponding list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  s->set_next(_table[h]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  _table[h] = s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  _number_of_vtable_stubs++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
bool VtableStubs::is_entry_point(address pc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  MutexLocker ml(VtableStubs_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  VtableStub* stub = (VtableStub*)(pc - VtableStub::entry_offset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  uint hash = VtableStubs::hash(stub->is_vtable_stub(), stub->index());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  VtableStub* s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  for (s = _table[hash]; s != NULL && s != stub; s = s->next()) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  return s == stub;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
bool VtableStubs::contains(address pc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  // simple solution for now - we may want to use
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  // a faster way if this function is called often
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  return stub_containing(pc) != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
VtableStub* VtableStubs::stub_containing(address pc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  // Note: No locking needed since any change to the data structure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  //       happens with an atomic store into it (we don't care about
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  //       consistency with the _number_of_vtable_stubs counter).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  for (int i = 0; i < N; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    for (VtableStub* s = _table[i]; s != NULL; s = s->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      if (s->contains(pc)) return s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
void vtableStubs_init() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  VtableStubs::initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
//-----------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  HandleMark hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  klassOop klass = receiver->klass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  instanceKlass* ik = instanceKlass::cast(klass);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  klassVtable* vt = ik->vtable();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  klass->print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  fatal3("bad compiled vtable dispatch: receiver " INTPTR_FORMAT ", index %d (vtable length %d)", (address)receiver, index, vt->length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
#endif // Product