src/share/vm/c1/c1_Runtime1.cpp

Mon, 09 Mar 2009 13:28:46 -0700

author
xdono
date
Mon, 09 Mar 2009 13:28:46 -0700
changeset 1014
0fbdb4381b99
parent 920
ac8fe14c93e4
child 1577
4ce7240d622c
permissions
-rw-r--r--

6814575: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 03/09
Reviewed-by: katleman, tbell, ohair

duke@435 1 /*
xdono@1014 2 * Copyright 1999-2009 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #include "incls/_precompiled.incl"
duke@435 26 #include "incls/_c1_Runtime1.cpp.incl"
duke@435 27
duke@435 28
duke@435 29 // Implementation of StubAssembler
duke@435 30
duke@435 31 StubAssembler::StubAssembler(CodeBuffer* code, const char * name, int stub_id) : C1_MacroAssembler(code) {
duke@435 32 _name = name;
duke@435 33 _must_gc_arguments = false;
duke@435 34 _frame_size = no_frame_size;
duke@435 35 _num_rt_args = 0;
duke@435 36 _stub_id = stub_id;
duke@435 37 }
duke@435 38
duke@435 39
duke@435 40 void StubAssembler::set_info(const char* name, bool must_gc_arguments) {
duke@435 41 _name = name;
duke@435 42 _must_gc_arguments = must_gc_arguments;
duke@435 43 }
duke@435 44
duke@435 45
duke@435 46 void StubAssembler::set_frame_size(int size) {
duke@435 47 if (_frame_size == no_frame_size) {
duke@435 48 _frame_size = size;
duke@435 49 }
duke@435 50 assert(_frame_size == size, "can't change the frame size");
duke@435 51 }
duke@435 52
duke@435 53
duke@435 54 void StubAssembler::set_num_rt_args(int args) {
duke@435 55 if (_num_rt_args == 0) {
duke@435 56 _num_rt_args = args;
duke@435 57 }
duke@435 58 assert(_num_rt_args == args, "can't change the number of args");
duke@435 59 }
duke@435 60
duke@435 61 // Implementation of Runtime1
duke@435 62
duke@435 63 bool Runtime1::_is_initialized = false;
duke@435 64 CodeBlob* Runtime1::_blobs[Runtime1::number_of_ids];
duke@435 65 const char *Runtime1::_blob_names[] = {
duke@435 66 RUNTIME1_STUBS(STUB_NAME, LAST_STUB_NAME)
duke@435 67 };
duke@435 68
duke@435 69 #ifndef PRODUCT
duke@435 70 // statistics
duke@435 71 int Runtime1::_generic_arraycopy_cnt = 0;
duke@435 72 int Runtime1::_primitive_arraycopy_cnt = 0;
duke@435 73 int Runtime1::_oop_arraycopy_cnt = 0;
duke@435 74 int Runtime1::_arraycopy_slowcase_cnt = 0;
duke@435 75 int Runtime1::_new_type_array_slowcase_cnt = 0;
duke@435 76 int Runtime1::_new_object_array_slowcase_cnt = 0;
duke@435 77 int Runtime1::_new_instance_slowcase_cnt = 0;
duke@435 78 int Runtime1::_new_multi_array_slowcase_cnt = 0;
duke@435 79 int Runtime1::_monitorenter_slowcase_cnt = 0;
duke@435 80 int Runtime1::_monitorexit_slowcase_cnt = 0;
duke@435 81 int Runtime1::_patch_code_slowcase_cnt = 0;
duke@435 82 int Runtime1::_throw_range_check_exception_count = 0;
duke@435 83 int Runtime1::_throw_index_exception_count = 0;
duke@435 84 int Runtime1::_throw_div0_exception_count = 0;
duke@435 85 int Runtime1::_throw_null_pointer_exception_count = 0;
duke@435 86 int Runtime1::_throw_class_cast_exception_count = 0;
duke@435 87 int Runtime1::_throw_incompatible_class_change_error_count = 0;
duke@435 88 int Runtime1::_throw_array_store_exception_count = 0;
duke@435 89 int Runtime1::_throw_count = 0;
duke@435 90 #endif
duke@435 91
duke@435 92 BufferBlob* Runtime1::_buffer_blob = NULL;
duke@435 93
duke@435 94 // Simple helper to see if the caller of a runtime stub which
duke@435 95 // entered the VM has been deoptimized
duke@435 96
duke@435 97 static bool caller_is_deopted() {
duke@435 98 JavaThread* thread = JavaThread::current();
duke@435 99 RegisterMap reg_map(thread, false);
duke@435 100 frame runtime_frame = thread->last_frame();
duke@435 101 frame caller_frame = runtime_frame.sender(&reg_map);
duke@435 102 assert(caller_frame.is_compiled_frame(), "must be compiled");
duke@435 103 return caller_frame.is_deoptimized_frame();
duke@435 104 }
duke@435 105
duke@435 106 // Stress deoptimization
duke@435 107 static void deopt_caller() {
duke@435 108 if ( !caller_is_deopted()) {
duke@435 109 JavaThread* thread = JavaThread::current();
duke@435 110 RegisterMap reg_map(thread, false);
duke@435 111 frame runtime_frame = thread->last_frame();
duke@435 112 frame caller_frame = runtime_frame.sender(&reg_map);
duke@435 113 VM_DeoptimizeFrame deopt(thread, caller_frame.id());
duke@435 114 VMThread::execute(&deopt);
duke@435 115 assert(caller_is_deopted(), "Must be deoptimized");
duke@435 116 }
duke@435 117 }
duke@435 118
duke@435 119
duke@435 120 BufferBlob* Runtime1::get_buffer_blob() {
duke@435 121 // Allocate code buffer space only once
duke@435 122 BufferBlob* blob = _buffer_blob;
duke@435 123 if (blob == NULL) {
duke@435 124 // setup CodeBuffer. Preallocate a BufferBlob of size
duke@435 125 // NMethodSizeLimit plus some extra space for constants.
duke@435 126 int code_buffer_size = desired_max_code_buffer_size() + desired_max_constant_size();
duke@435 127 blob = BufferBlob::create("Compiler1 temporary CodeBuffer",
duke@435 128 code_buffer_size);
duke@435 129 guarantee(blob != NULL, "must create initial code buffer");
duke@435 130 _buffer_blob = blob;
duke@435 131 }
duke@435 132 return _buffer_blob;
duke@435 133 }
duke@435 134
duke@435 135 void Runtime1::setup_code_buffer(CodeBuffer* code, int call_stub_estimate) {
duke@435 136 // Preinitialize the consts section to some large size:
duke@435 137 int locs_buffer_size = 20 * (relocInfo::length_limit + sizeof(relocInfo));
duke@435 138 char* locs_buffer = NEW_RESOURCE_ARRAY(char, locs_buffer_size);
duke@435 139 code->insts()->initialize_shared_locs((relocInfo*)locs_buffer,
duke@435 140 locs_buffer_size / sizeof(relocInfo));
duke@435 141 code->initialize_consts_size(desired_max_constant_size());
duke@435 142 // Call stubs + deopt/exception handler
duke@435 143 code->initialize_stubs_size((call_stub_estimate * LIR_Assembler::call_stub_size) +
duke@435 144 LIR_Assembler::exception_handler_size +
duke@435 145 LIR_Assembler::deopt_handler_size);
duke@435 146 }
duke@435 147
duke@435 148
duke@435 149 void Runtime1::generate_blob_for(StubID id) {
duke@435 150 assert(0 <= id && id < number_of_ids, "illegal stub id");
duke@435 151 ResourceMark rm;
duke@435 152 // create code buffer for code storage
duke@435 153 CodeBuffer code(get_buffer_blob()->instructions_begin(),
duke@435 154 get_buffer_blob()->instructions_size());
duke@435 155
duke@435 156 setup_code_buffer(&code, 0);
duke@435 157
duke@435 158 // create assembler for code generation
duke@435 159 StubAssembler* sasm = new StubAssembler(&code, name_for(id), id);
duke@435 160 // generate code for runtime stub
duke@435 161 OopMapSet* oop_maps;
duke@435 162 oop_maps = generate_code_for(id, sasm);
duke@435 163 assert(oop_maps == NULL || sasm->frame_size() != no_frame_size,
duke@435 164 "if stub has an oop map it must have a valid frame size");
duke@435 165
duke@435 166 #ifdef ASSERT
duke@435 167 // Make sure that stubs that need oopmaps have them
duke@435 168 switch (id) {
duke@435 169 // These stubs don't need to have an oopmap
duke@435 170 case dtrace_object_alloc_id:
ysr@777 171 case g1_pre_barrier_slow_id:
ysr@777 172 case g1_post_barrier_slow_id:
duke@435 173 case slow_subtype_check_id:
duke@435 174 case fpu2long_stub_id:
duke@435 175 case unwind_exception_id:
duke@435 176 #ifndef TIERED
duke@435 177 case counter_overflow_id: // Not generated outside the tiered world
duke@435 178 #endif
duke@435 179 #ifdef SPARC
duke@435 180 case handle_exception_nofpu_id: // Unused on sparc
duke@435 181 #endif
duke@435 182 break;
duke@435 183
duke@435 184 // All other stubs should have oopmaps
duke@435 185 default:
duke@435 186 assert(oop_maps != NULL, "must have an oopmap");
duke@435 187 }
duke@435 188 #endif
duke@435 189
duke@435 190 // align so printing shows nop's instead of random code at the end (SimpleStubs are aligned)
duke@435 191 sasm->align(BytesPerWord);
duke@435 192 // make sure all code is in code buffer
duke@435 193 sasm->flush();
duke@435 194 // create blob - distinguish a few special cases
duke@435 195 CodeBlob* blob = RuntimeStub::new_runtime_stub(name_for(id),
duke@435 196 &code,
duke@435 197 CodeOffsets::frame_never_safe,
duke@435 198 sasm->frame_size(),
duke@435 199 oop_maps,
duke@435 200 sasm->must_gc_arguments());
duke@435 201 // install blob
duke@435 202 assert(blob != NULL, "blob must exist");
duke@435 203 _blobs[id] = blob;
duke@435 204 }
duke@435 205
duke@435 206
duke@435 207 void Runtime1::initialize() {
duke@435 208 // Warning: If we have more than one compilation running in parallel, we
duke@435 209 // need a lock here with the current setup (lazy initialization).
duke@435 210 if (!is_initialized()) {
duke@435 211 _is_initialized = true;
duke@435 212
duke@435 213 // platform-dependent initialization
duke@435 214 initialize_pd();
duke@435 215 // generate stubs
duke@435 216 for (int id = 0; id < number_of_ids; id++) generate_blob_for((StubID)id);
duke@435 217 // printing
duke@435 218 #ifndef PRODUCT
duke@435 219 if (PrintSimpleStubs) {
duke@435 220 ResourceMark rm;
duke@435 221 for (int id = 0; id < number_of_ids; id++) {
duke@435 222 _blobs[id]->print();
duke@435 223 if (_blobs[id]->oop_maps() != NULL) {
duke@435 224 _blobs[id]->oop_maps()->print();
duke@435 225 }
duke@435 226 }
duke@435 227 }
duke@435 228 #endif
duke@435 229 }
duke@435 230 }
duke@435 231
duke@435 232
duke@435 233 CodeBlob* Runtime1::blob_for(StubID id) {
duke@435 234 assert(0 <= id && id < number_of_ids, "illegal stub id");
duke@435 235 if (!is_initialized()) initialize();
duke@435 236 return _blobs[id];
duke@435 237 }
duke@435 238
duke@435 239
duke@435 240 const char* Runtime1::name_for(StubID id) {
duke@435 241 assert(0 <= id && id < number_of_ids, "illegal stub id");
duke@435 242 return _blob_names[id];
duke@435 243 }
duke@435 244
duke@435 245 const char* Runtime1::name_for_address(address entry) {
duke@435 246 for (int id = 0; id < number_of_ids; id++) {
duke@435 247 if (entry == entry_for((StubID)id)) return name_for((StubID)id);
duke@435 248 }
duke@435 249
duke@435 250 #define FUNCTION_CASE(a, f) \
duke@435 251 if ((intptr_t)a == CAST_FROM_FN_PTR(intptr_t, f)) return #f
duke@435 252
duke@435 253 FUNCTION_CASE(entry, os::javaTimeMillis);
duke@435 254 FUNCTION_CASE(entry, os::javaTimeNanos);
duke@435 255 FUNCTION_CASE(entry, SharedRuntime::OSR_migration_end);
duke@435 256 FUNCTION_CASE(entry, SharedRuntime::d2f);
duke@435 257 FUNCTION_CASE(entry, SharedRuntime::d2i);
duke@435 258 FUNCTION_CASE(entry, SharedRuntime::d2l);
duke@435 259 FUNCTION_CASE(entry, SharedRuntime::dcos);
duke@435 260 FUNCTION_CASE(entry, SharedRuntime::dexp);
duke@435 261 FUNCTION_CASE(entry, SharedRuntime::dlog);
duke@435 262 FUNCTION_CASE(entry, SharedRuntime::dlog10);
duke@435 263 FUNCTION_CASE(entry, SharedRuntime::dpow);
duke@435 264 FUNCTION_CASE(entry, SharedRuntime::drem);
duke@435 265 FUNCTION_CASE(entry, SharedRuntime::dsin);
duke@435 266 FUNCTION_CASE(entry, SharedRuntime::dtan);
duke@435 267 FUNCTION_CASE(entry, SharedRuntime::f2i);
duke@435 268 FUNCTION_CASE(entry, SharedRuntime::f2l);
duke@435 269 FUNCTION_CASE(entry, SharedRuntime::frem);
duke@435 270 FUNCTION_CASE(entry, SharedRuntime::l2d);
duke@435 271 FUNCTION_CASE(entry, SharedRuntime::l2f);
duke@435 272 FUNCTION_CASE(entry, SharedRuntime::ldiv);
duke@435 273 FUNCTION_CASE(entry, SharedRuntime::lmul);
duke@435 274 FUNCTION_CASE(entry, SharedRuntime::lrem);
duke@435 275 FUNCTION_CASE(entry, SharedRuntime::lrem);
duke@435 276 FUNCTION_CASE(entry, SharedRuntime::dtrace_method_entry);
duke@435 277 FUNCTION_CASE(entry, SharedRuntime::dtrace_method_exit);
duke@435 278 FUNCTION_CASE(entry, trace_block_entry);
duke@435 279
duke@435 280 #undef FUNCTION_CASE
duke@435 281
duke@435 282 return "<unknown function>";
duke@435 283 }
duke@435 284
duke@435 285
duke@435 286 JRT_ENTRY(void, Runtime1::new_instance(JavaThread* thread, klassOopDesc* klass))
duke@435 287 NOT_PRODUCT(_new_instance_slowcase_cnt++;)
duke@435 288
duke@435 289 assert(oop(klass)->is_klass(), "not a class");
duke@435 290 instanceKlassHandle h(thread, klass);
duke@435 291 h->check_valid_for_instantiation(true, CHECK);
duke@435 292 // make sure klass is initialized
duke@435 293 h->initialize(CHECK);
duke@435 294 // allocate instance and return via TLS
duke@435 295 oop obj = h->allocate_instance(CHECK);
duke@435 296 thread->set_vm_result(obj);
duke@435 297 JRT_END
duke@435 298
duke@435 299
duke@435 300 JRT_ENTRY(void, Runtime1::new_type_array(JavaThread* thread, klassOopDesc* klass, jint length))
duke@435 301 NOT_PRODUCT(_new_type_array_slowcase_cnt++;)
duke@435 302 // Note: no handle for klass needed since they are not used
duke@435 303 // anymore after new_typeArray() and no GC can happen before.
duke@435 304 // (This may have to change if this code changes!)
duke@435 305 assert(oop(klass)->is_klass(), "not a class");
duke@435 306 BasicType elt_type = typeArrayKlass::cast(klass)->element_type();
duke@435 307 oop obj = oopFactory::new_typeArray(elt_type, length, CHECK);
duke@435 308 thread->set_vm_result(obj);
duke@435 309 // This is pretty rare but this runtime patch is stressful to deoptimization
duke@435 310 // if we deoptimize here so force a deopt to stress the path.
duke@435 311 if (DeoptimizeALot) {
duke@435 312 deopt_caller();
duke@435 313 }
duke@435 314
duke@435 315 JRT_END
duke@435 316
duke@435 317
duke@435 318 JRT_ENTRY(void, Runtime1::new_object_array(JavaThread* thread, klassOopDesc* array_klass, jint length))
duke@435 319 NOT_PRODUCT(_new_object_array_slowcase_cnt++;)
duke@435 320
duke@435 321 // Note: no handle for klass needed since they are not used
duke@435 322 // anymore after new_objArray() and no GC can happen before.
duke@435 323 // (This may have to change if this code changes!)
duke@435 324 assert(oop(array_klass)->is_klass(), "not a class");
duke@435 325 klassOop elem_klass = objArrayKlass::cast(array_klass)->element_klass();
duke@435 326 objArrayOop obj = oopFactory::new_objArray(elem_klass, length, CHECK);
duke@435 327 thread->set_vm_result(obj);
duke@435 328 // This is pretty rare but this runtime patch is stressful to deoptimization
duke@435 329 // if we deoptimize here so force a deopt to stress the path.
duke@435 330 if (DeoptimizeALot) {
duke@435 331 deopt_caller();
duke@435 332 }
duke@435 333 JRT_END
duke@435 334
duke@435 335
duke@435 336 JRT_ENTRY(void, Runtime1::new_multi_array(JavaThread* thread, klassOopDesc* klass, int rank, jint* dims))
duke@435 337 NOT_PRODUCT(_new_multi_array_slowcase_cnt++;)
duke@435 338
duke@435 339 assert(oop(klass)->is_klass(), "not a class");
duke@435 340 assert(rank >= 1, "rank must be nonzero");
duke@435 341 oop obj = arrayKlass::cast(klass)->multi_allocate(rank, dims, CHECK);
duke@435 342 thread->set_vm_result(obj);
duke@435 343 JRT_END
duke@435 344
duke@435 345
duke@435 346 JRT_ENTRY(void, Runtime1::unimplemented_entry(JavaThread* thread, StubID id))
duke@435 347 tty->print_cr("Runtime1::entry_for(%d) returned unimplemented entry point", id);
duke@435 348 JRT_END
duke@435 349
duke@435 350
duke@435 351 JRT_ENTRY(void, Runtime1::throw_array_store_exception(JavaThread* thread))
duke@435 352 THROW(vmSymbolHandles::java_lang_ArrayStoreException());
duke@435 353 JRT_END
duke@435 354
duke@435 355
duke@435 356 JRT_ENTRY(void, Runtime1::post_jvmti_exception_throw(JavaThread* thread))
duke@435 357 if (JvmtiExport::can_post_exceptions()) {
duke@435 358 vframeStream vfst(thread, true);
duke@435 359 address bcp = vfst.method()->bcp_from(vfst.bci());
duke@435 360 JvmtiExport::post_exception_throw(thread, vfst.method(), bcp, thread->exception_oop());
duke@435 361 }
duke@435 362 JRT_END
duke@435 363
duke@435 364 #ifdef TIERED
duke@435 365 JRT_ENTRY(void, Runtime1::counter_overflow(JavaThread* thread, int bci))
duke@435 366 RegisterMap map(thread, false);
duke@435 367 frame fr = thread->last_frame().sender(&map);
duke@435 368 nmethod* nm = (nmethod*) fr.cb();
duke@435 369 assert(nm!= NULL && nm->is_nmethod(), "what?");
duke@435 370 methodHandle method(thread, nm->method());
duke@435 371 if (bci == 0) {
duke@435 372 // invocation counter overflow
duke@435 373 if (!Tier1CountOnly) {
duke@435 374 CompilationPolicy::policy()->method_invocation_event(method, CHECK);
duke@435 375 } else {
duke@435 376 method()->invocation_counter()->reset();
duke@435 377 }
duke@435 378 } else {
duke@435 379 if (!Tier1CountOnly) {
duke@435 380 // Twe have a bci but not the destination bci and besides a backedge
duke@435 381 // event is more for OSR which we don't want here.
duke@435 382 CompilationPolicy::policy()->method_invocation_event(method, CHECK);
duke@435 383 } else {
duke@435 384 method()->backedge_counter()->reset();
duke@435 385 }
duke@435 386 }
duke@435 387 JRT_END
duke@435 388 #endif // TIERED
duke@435 389
duke@435 390 extern void vm_exit(int code);
duke@435 391
duke@435 392 // Enter this method from compiled code handler below. This is where we transition
duke@435 393 // to VM mode. This is done as a helper routine so that the method called directly
duke@435 394 // from compiled code does not have to transition to VM. This allows the entry
duke@435 395 // method to see if the nmethod that we have just looked up a handler for has
duke@435 396 // been deoptimized while we were in the vm. This simplifies the assembly code
duke@435 397 // cpu directories.
duke@435 398 //
duke@435 399 // We are entering here from exception stub (via the entry method below)
duke@435 400 // If there is a compiled exception handler in this method, we will continue there;
duke@435 401 // otherwise we will unwind the stack and continue at the caller of top frame method
duke@435 402 // Note: we enter in Java using a special JRT wrapper. This wrapper allows us to
duke@435 403 // control the area where we can allow a safepoint. After we exit the safepoint area we can
duke@435 404 // check to see if the handler we are going to return is now in a nmethod that has
duke@435 405 // been deoptimized. If that is the case we return the deopt blob
duke@435 406 // unpack_with_exception entry instead. This makes life for the exception blob easier
duke@435 407 // because making that same check and diverting is painful from assembly language.
duke@435 408 //
duke@435 409
duke@435 410
duke@435 411 JRT_ENTRY_NO_ASYNC(static address, exception_handler_for_pc_helper(JavaThread* thread, oopDesc* ex, address pc, nmethod*& nm))
duke@435 412
duke@435 413 Handle exception(thread, ex);
duke@435 414 nm = CodeCache::find_nmethod(pc);
duke@435 415 assert(nm != NULL, "this is not an nmethod");
duke@435 416 // Adjust the pc as needed/
duke@435 417 if (nm->is_deopt_pc(pc)) {
duke@435 418 RegisterMap map(thread, false);
duke@435 419 frame exception_frame = thread->last_frame().sender(&map);
duke@435 420 // if the frame isn't deopted then pc must not correspond to the caller of last_frame
duke@435 421 assert(exception_frame.is_deoptimized_frame(), "must be deopted");
duke@435 422 pc = exception_frame.pc();
duke@435 423 }
duke@435 424 #ifdef ASSERT
duke@435 425 assert(exception.not_null(), "NULL exceptions should be handled by throw_exception");
duke@435 426 assert(exception->is_oop(), "just checking");
duke@435 427 // Check that exception is a subclass of Throwable, otherwise we have a VerifyError
duke@435 428 if (!(exception->is_a(SystemDictionary::throwable_klass()))) {
duke@435 429 if (ExitVMOnVerifyError) vm_exit(-1);
duke@435 430 ShouldNotReachHere();
duke@435 431 }
duke@435 432 #endif
duke@435 433
duke@435 434 // Check the stack guard pages and reenable them if necessary and there is
duke@435 435 // enough space on the stack to do so. Use fast exceptions only if the guard
duke@435 436 // pages are enabled.
duke@435 437 bool guard_pages_enabled = thread->stack_yellow_zone_enabled();
duke@435 438 if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack();
duke@435 439
duke@435 440 if (JvmtiExport::can_post_exceptions()) {
duke@435 441 // To ensure correct notification of exception catches and throws
duke@435 442 // we have to deoptimize here. If we attempted to notify the
duke@435 443 // catches and throws during this exception lookup it's possible
duke@435 444 // we could deoptimize on the way out of the VM and end back in
duke@435 445 // the interpreter at the throw site. This would result in double
duke@435 446 // notifications since the interpreter would also notify about
duke@435 447 // these same catches and throws as it unwound the frame.
duke@435 448
duke@435 449 RegisterMap reg_map(thread);
duke@435 450 frame stub_frame = thread->last_frame();
duke@435 451 frame caller_frame = stub_frame.sender(&reg_map);
duke@435 452
duke@435 453 // We don't really want to deoptimize the nmethod itself since we
duke@435 454 // can actually continue in the exception handler ourselves but I
duke@435 455 // don't see an easy way to have the desired effect.
duke@435 456 VM_DeoptimizeFrame deopt(thread, caller_frame.id());
duke@435 457 VMThread::execute(&deopt);
duke@435 458
duke@435 459 return SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
duke@435 460 }
duke@435 461
duke@435 462 // ExceptionCache is used only for exceptions at call and not for implicit exceptions
duke@435 463 if (guard_pages_enabled) {
duke@435 464 address fast_continuation = nm->handler_for_exception_and_pc(exception, pc);
duke@435 465 if (fast_continuation != NULL) {
duke@435 466 if (fast_continuation == ExceptionCache::unwind_handler()) fast_continuation = NULL;
duke@435 467 return fast_continuation;
duke@435 468 }
duke@435 469 }
duke@435 470
duke@435 471 // If the stack guard pages are enabled, check whether there is a handler in
duke@435 472 // the current method. Otherwise (guard pages disabled), force an unwind and
duke@435 473 // skip the exception cache update (i.e., just leave continuation==NULL).
duke@435 474 address continuation = NULL;
duke@435 475 if (guard_pages_enabled) {
duke@435 476
duke@435 477 // New exception handling mechanism can support inlined methods
duke@435 478 // with exception handlers since the mappings are from PC to PC
duke@435 479
duke@435 480 // debugging support
duke@435 481 // tracing
duke@435 482 if (TraceExceptions) {
duke@435 483 ttyLocker ttyl;
duke@435 484 ResourceMark rm;
duke@435 485 tty->print_cr("Exception <%s> (0x%x) thrown in compiled method <%s> at PC " PTR_FORMAT " for thread 0x%x",
duke@435 486 exception->print_value_string(), (address)exception(), nm->method()->print_value_string(), pc, thread);
duke@435 487 }
duke@435 488 // for AbortVMOnException flag
duke@435 489 NOT_PRODUCT(Exceptions::debug_check_abort(exception));
duke@435 490
duke@435 491 // Clear out the exception oop and pc since looking up an
duke@435 492 // exception handler can cause class loading, which might throw an
duke@435 493 // exception and those fields are expected to be clear during
duke@435 494 // normal bytecode execution.
duke@435 495 thread->set_exception_oop(NULL);
duke@435 496 thread->set_exception_pc(NULL);
duke@435 497
duke@435 498 continuation = SharedRuntime::compute_compiled_exc_handler(nm, pc, exception, false, false);
duke@435 499 // If an exception was thrown during exception dispatch, the exception oop may have changed
duke@435 500 thread->set_exception_oop(exception());
duke@435 501 thread->set_exception_pc(pc);
duke@435 502
duke@435 503 // the exception cache is used only by non-implicit exceptions
duke@435 504 if (continuation == NULL) {
duke@435 505 nm->add_handler_for_exception_and_pc(exception, pc, ExceptionCache::unwind_handler());
duke@435 506 } else {
duke@435 507 nm->add_handler_for_exception_and_pc(exception, pc, continuation);
duke@435 508 }
duke@435 509 }
duke@435 510
duke@435 511 thread->set_vm_result(exception());
duke@435 512
duke@435 513 if (TraceExceptions) {
duke@435 514 ttyLocker ttyl;
duke@435 515 ResourceMark rm;
duke@435 516 tty->print_cr("Thread " PTR_FORMAT " continuing at PC " PTR_FORMAT " for exception thrown at PC " PTR_FORMAT,
duke@435 517 thread, continuation, pc);
duke@435 518 }
duke@435 519
duke@435 520 return continuation;
duke@435 521 JRT_END
duke@435 522
duke@435 523 // Enter this method from compiled code only if there is a Java exception handler
duke@435 524 // in the method handling the exception
duke@435 525 // We are entering here from exception stub. We don't do a normal VM transition here.
duke@435 526 // We do it in a helper. This is so we can check to see if the nmethod we have just
duke@435 527 // searched for an exception handler has been deoptimized in the meantime.
duke@435 528 address Runtime1::exception_handler_for_pc(JavaThread* thread) {
duke@435 529 oop exception = thread->exception_oop();
duke@435 530 address pc = thread->exception_pc();
duke@435 531 // Still in Java mode
duke@435 532 debug_only(ResetNoHandleMark rnhm);
duke@435 533 nmethod* nm = NULL;
duke@435 534 address continuation = NULL;
duke@435 535 {
duke@435 536 // Enter VM mode by calling the helper
duke@435 537
duke@435 538 ResetNoHandleMark rnhm;
duke@435 539 continuation = exception_handler_for_pc_helper(thread, exception, pc, nm);
duke@435 540 }
duke@435 541 // Back in JAVA, use no oops DON'T safepoint
duke@435 542
duke@435 543 // Now check to see if the nmethod we were called from is now deoptimized.
duke@435 544 // If so we must return to the deopt blob and deoptimize the nmethod
duke@435 545
duke@435 546 if (nm != NULL && caller_is_deopted()) {
duke@435 547 continuation = SharedRuntime::deopt_blob()->unpack_with_exception_in_tls();
duke@435 548 }
duke@435 549
duke@435 550 return continuation;
duke@435 551 }
duke@435 552
duke@435 553
duke@435 554 JRT_ENTRY(void, Runtime1::throw_range_check_exception(JavaThread* thread, int index))
duke@435 555 NOT_PRODUCT(_throw_range_check_exception_count++;)
duke@435 556 Events::log("throw_range_check");
duke@435 557 char message[jintAsStringSize];
duke@435 558 sprintf(message, "%d", index);
duke@435 559 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), message);
duke@435 560 JRT_END
duke@435 561
duke@435 562
duke@435 563 JRT_ENTRY(void, Runtime1::throw_index_exception(JavaThread* thread, int index))
duke@435 564 NOT_PRODUCT(_throw_index_exception_count++;)
duke@435 565 Events::log("throw_index");
duke@435 566 char message[16];
duke@435 567 sprintf(message, "%d", index);
duke@435 568 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IndexOutOfBoundsException(), message);
duke@435 569 JRT_END
duke@435 570
duke@435 571
duke@435 572 JRT_ENTRY(void, Runtime1::throw_div0_exception(JavaThread* thread))
duke@435 573 NOT_PRODUCT(_throw_div0_exception_count++;)
duke@435 574 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_ArithmeticException(), "/ by zero");
duke@435 575 JRT_END
duke@435 576
duke@435 577
duke@435 578 JRT_ENTRY(void, Runtime1::throw_null_pointer_exception(JavaThread* thread))
duke@435 579 NOT_PRODUCT(_throw_null_pointer_exception_count++;)
duke@435 580 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_NullPointerException());
duke@435 581 JRT_END
duke@435 582
duke@435 583
duke@435 584 JRT_ENTRY(void, Runtime1::throw_class_cast_exception(JavaThread* thread, oopDesc* object))
duke@435 585 NOT_PRODUCT(_throw_class_cast_exception_count++;)
duke@435 586 ResourceMark rm(thread);
duke@435 587 char* message = SharedRuntime::generate_class_cast_message(
duke@435 588 thread, Klass::cast(object->klass())->external_name());
duke@435 589 SharedRuntime::throw_and_post_jvmti_exception(
duke@435 590 thread, vmSymbols::java_lang_ClassCastException(), message);
duke@435 591 JRT_END
duke@435 592
duke@435 593
duke@435 594 JRT_ENTRY(void, Runtime1::throw_incompatible_class_change_error(JavaThread* thread))
duke@435 595 NOT_PRODUCT(_throw_incompatible_class_change_error_count++;)
duke@435 596 ResourceMark rm(thread);
duke@435 597 SharedRuntime::throw_and_post_jvmti_exception(thread, vmSymbols::java_lang_IncompatibleClassChangeError());
duke@435 598 JRT_END
duke@435 599
duke@435 600
duke@435 601 JRT_ENTRY_NO_ASYNC(void, Runtime1::monitorenter(JavaThread* thread, oopDesc* obj, BasicObjectLock* lock))
duke@435 602 NOT_PRODUCT(_monitorenter_slowcase_cnt++;)
duke@435 603 if (PrintBiasedLockingStatistics) {
duke@435 604 Atomic::inc(BiasedLocking::slow_path_entry_count_addr());
duke@435 605 }
duke@435 606 Handle h_obj(thread, obj);
duke@435 607 assert(h_obj()->is_oop(), "must be NULL or an object");
duke@435 608 if (UseBiasedLocking) {
duke@435 609 // Retry fast entry if bias is revoked to avoid unnecessary inflation
duke@435 610 ObjectSynchronizer::fast_enter(h_obj, lock->lock(), true, CHECK);
duke@435 611 } else {
duke@435 612 if (UseFastLocking) {
duke@435 613 // When using fast locking, the compiled code has already tried the fast case
duke@435 614 assert(obj == lock->obj(), "must match");
duke@435 615 ObjectSynchronizer::slow_enter(h_obj, lock->lock(), THREAD);
duke@435 616 } else {
duke@435 617 lock->set_obj(obj);
duke@435 618 ObjectSynchronizer::fast_enter(h_obj, lock->lock(), false, THREAD);
duke@435 619 }
duke@435 620 }
duke@435 621 JRT_END
duke@435 622
duke@435 623
duke@435 624 JRT_LEAF(void, Runtime1::monitorexit(JavaThread* thread, BasicObjectLock* lock))
duke@435 625 NOT_PRODUCT(_monitorexit_slowcase_cnt++;)
duke@435 626 assert(thread == JavaThread::current(), "threads must correspond");
duke@435 627 assert(thread->last_Java_sp(), "last_Java_sp must be set");
duke@435 628 // monitorexit is non-blocking (leaf routine) => no exceptions can be thrown
duke@435 629 EXCEPTION_MARK;
duke@435 630
duke@435 631 oop obj = lock->obj();
duke@435 632 assert(obj->is_oop(), "must be NULL or an object");
duke@435 633 if (UseFastLocking) {
duke@435 634 // When using fast locking, the compiled code has already tried the fast case
duke@435 635 ObjectSynchronizer::slow_exit(obj, lock->lock(), THREAD);
duke@435 636 } else {
duke@435 637 ObjectSynchronizer::fast_exit(obj, lock->lock(), THREAD);
duke@435 638 }
duke@435 639 JRT_END
duke@435 640
duke@435 641
duke@435 642 static klassOop resolve_field_return_klass(methodHandle caller, int bci, TRAPS) {
duke@435 643 Bytecode_field* field_access = Bytecode_field_at(caller(), caller->bcp_from(bci));
duke@435 644 // This can be static or non-static field access
duke@435 645 Bytecodes::Code code = field_access->code();
duke@435 646
duke@435 647 // We must load class, initialize class and resolvethe field
duke@435 648 FieldAccessInfo result; // initialize class if needed
duke@435 649 constantPoolHandle constants(THREAD, caller->constants());
duke@435 650 LinkResolver::resolve_field(result, constants, field_access->index(), Bytecodes::java_code(code), false, CHECK_NULL);
duke@435 651 return result.klass()();
duke@435 652 }
duke@435 653
duke@435 654
duke@435 655 //
duke@435 656 // This routine patches sites where a class wasn't loaded or
duke@435 657 // initialized at the time the code was generated. It handles
duke@435 658 // references to classes, fields and forcing of initialization. Most
duke@435 659 // of the cases are straightforward and involving simply forcing
duke@435 660 // resolution of a class, rewriting the instruction stream with the
duke@435 661 // needed constant and replacing the call in this function with the
duke@435 662 // patched code. The case for static field is more complicated since
duke@435 663 // the thread which is in the process of initializing a class can
duke@435 664 // access it's static fields but other threads can't so the code
duke@435 665 // either has to deoptimize when this case is detected or execute a
duke@435 666 // check that the current thread is the initializing thread. The
duke@435 667 // current
duke@435 668 //
duke@435 669 // Patches basically look like this:
duke@435 670 //
duke@435 671 //
duke@435 672 // patch_site: jmp patch stub ;; will be patched
duke@435 673 // continue: ...
duke@435 674 // ...
duke@435 675 // ...
duke@435 676 // ...
duke@435 677 //
duke@435 678 // They have a stub which looks like this:
duke@435 679 //
duke@435 680 // ;; patch body
duke@435 681 // movl <const>, reg (for class constants)
duke@435 682 // <or> movl [reg1 + <const>], reg (for field offsets)
duke@435 683 // <or> movl reg, [reg1 + <const>] (for field offsets)
duke@435 684 // <being_init offset> <bytes to copy> <bytes to skip>
duke@435 685 // patch_stub: call Runtime1::patch_code (through a runtime stub)
duke@435 686 // jmp patch_site
duke@435 687 //
duke@435 688 //
duke@435 689 // A normal patch is done by rewriting the patch body, usually a move,
duke@435 690 // and then copying it into place over top of the jmp instruction
duke@435 691 // being careful to flush caches and doing it in an MP-safe way. The
duke@435 692 // constants following the patch body are used to find various pieces
duke@435 693 // of the patch relative to the call site for Runtime1::patch_code.
duke@435 694 // The case for getstatic and putstatic is more complicated because
duke@435 695 // getstatic and putstatic have special semantics when executing while
duke@435 696 // the class is being initialized. getstatic/putstatic on a class
duke@435 697 // which is being_initialized may be executed by the initializing
duke@435 698 // thread but other threads have to block when they execute it. This
duke@435 699 // is accomplished in compiled code by executing a test of the current
duke@435 700 // thread against the initializing thread of the class. It's emitted
duke@435 701 // as boilerplate in their stub which allows the patched code to be
duke@435 702 // executed before it's copied back into the main body of the nmethod.
duke@435 703 //
duke@435 704 // being_init: get_thread(<tmp reg>
duke@435 705 // cmpl [reg1 + <init_thread_offset>], <tmp reg>
duke@435 706 // jne patch_stub
duke@435 707 // movl [reg1 + <const>], reg (for field offsets) <or>
duke@435 708 // movl reg, [reg1 + <const>] (for field offsets)
duke@435 709 // jmp continue
duke@435 710 // <being_init offset> <bytes to copy> <bytes to skip>
duke@435 711 // patch_stub: jmp Runtim1::patch_code (through a runtime stub)
duke@435 712 // jmp patch_site
duke@435 713 //
duke@435 714 // If the class is being initialized the patch body is rewritten and
duke@435 715 // the patch site is rewritten to jump to being_init, instead of
duke@435 716 // patch_stub. Whenever this code is executed it checks the current
duke@435 717 // thread against the intializing thread so other threads will enter
duke@435 718 // the runtime and end up blocked waiting the class to finish
duke@435 719 // initializing inside the calls to resolve_field below. The
duke@435 720 // initializing class will continue on it's way. Once the class is
duke@435 721 // fully_initialized, the intializing_thread of the class becomes
duke@435 722 // NULL, so the next thread to execute this code will fail the test,
duke@435 723 // call into patch_code and complete the patching process by copying
duke@435 724 // the patch body back into the main part of the nmethod and resume
duke@435 725 // executing.
duke@435 726 //
duke@435 727 //
duke@435 728
duke@435 729 JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, Runtime1::StubID stub_id ))
duke@435 730 NOT_PRODUCT(_patch_code_slowcase_cnt++;)
duke@435 731
duke@435 732 ResourceMark rm(thread);
duke@435 733 RegisterMap reg_map(thread, false);
duke@435 734 frame runtime_frame = thread->last_frame();
duke@435 735 frame caller_frame = runtime_frame.sender(&reg_map);
duke@435 736
duke@435 737 // last java frame on stack
duke@435 738 vframeStream vfst(thread, true);
duke@435 739 assert(!vfst.at_end(), "Java frame must exist");
duke@435 740
duke@435 741 methodHandle caller_method(THREAD, vfst.method());
duke@435 742 // Note that caller_method->code() may not be same as caller_code because of OSR's
duke@435 743 // Note also that in the presence of inlining it is not guaranteed
duke@435 744 // that caller_method() == caller_code->method()
duke@435 745
duke@435 746
duke@435 747 int bci = vfst.bci();
duke@435 748
duke@435 749 Events::log("patch_code @ " INTPTR_FORMAT , caller_frame.pc());
duke@435 750
duke@435 751 Bytecodes::Code code = Bytecode_at(caller_method->bcp_from(bci))->java_code();
duke@435 752
duke@435 753 #ifndef PRODUCT
duke@435 754 // this is used by assertions in the access_field_patching_id
duke@435 755 BasicType patch_field_type = T_ILLEGAL;
duke@435 756 #endif // PRODUCT
duke@435 757 bool deoptimize_for_volatile = false;
duke@435 758 int patch_field_offset = -1;
duke@435 759 KlassHandle init_klass(THREAD, klassOop(NULL)); // klass needed by access_field_patching code
duke@435 760 Handle load_klass(THREAD, NULL); // oop needed by load_klass_patching code
duke@435 761 if (stub_id == Runtime1::access_field_patching_id) {
duke@435 762
duke@435 763 Bytecode_field* field_access = Bytecode_field_at(caller_method(), caller_method->bcp_from(bci));
duke@435 764 FieldAccessInfo result; // initialize class if needed
duke@435 765 Bytecodes::Code code = field_access->code();
duke@435 766 constantPoolHandle constants(THREAD, caller_method->constants());
duke@435 767 LinkResolver::resolve_field(result, constants, field_access->index(), Bytecodes::java_code(code), false, CHECK);
duke@435 768 patch_field_offset = result.field_offset();
duke@435 769
duke@435 770 // If we're patching a field which is volatile then at compile it
duke@435 771 // must not have been know to be volatile, so the generated code
duke@435 772 // isn't correct for a volatile reference. The nmethod has to be
duke@435 773 // deoptimized so that the code can be regenerated correctly.
duke@435 774 // This check is only needed for access_field_patching since this
duke@435 775 // is the path for patching field offsets. load_klass is only
duke@435 776 // used for patching references to oops which don't need special
duke@435 777 // handling in the volatile case.
duke@435 778 deoptimize_for_volatile = result.access_flags().is_volatile();
duke@435 779
duke@435 780 #ifndef PRODUCT
duke@435 781 patch_field_type = result.field_type();
duke@435 782 #endif
duke@435 783 } else if (stub_id == Runtime1::load_klass_patching_id) {
duke@435 784 oop k;
duke@435 785 switch (code) {
duke@435 786 case Bytecodes::_putstatic:
duke@435 787 case Bytecodes::_getstatic:
duke@435 788 { klassOop klass = resolve_field_return_klass(caller_method, bci, CHECK);
duke@435 789 // Save a reference to the class that has to be checked for initialization
duke@435 790 init_klass = KlassHandle(THREAD, klass);
duke@435 791 k = klass;
duke@435 792 }
duke@435 793 break;
duke@435 794 case Bytecodes::_new:
duke@435 795 { Bytecode_new* bnew = Bytecode_new_at(caller_method->bcp_from(bci));
duke@435 796 k = caller_method->constants()->klass_at(bnew->index(), CHECK);
duke@435 797 }
duke@435 798 break;
duke@435 799 case Bytecodes::_multianewarray:
duke@435 800 { Bytecode_multianewarray* mna = Bytecode_multianewarray_at(caller_method->bcp_from(bci));
duke@435 801 k = caller_method->constants()->klass_at(mna->index(), CHECK);
duke@435 802 }
duke@435 803 break;
duke@435 804 case Bytecodes::_instanceof:
duke@435 805 { Bytecode_instanceof* io = Bytecode_instanceof_at(caller_method->bcp_from(bci));
duke@435 806 k = caller_method->constants()->klass_at(io->index(), CHECK);
duke@435 807 }
duke@435 808 break;
duke@435 809 case Bytecodes::_checkcast:
duke@435 810 { Bytecode_checkcast* cc = Bytecode_checkcast_at(caller_method->bcp_from(bci));
duke@435 811 k = caller_method->constants()->klass_at(cc->index(), CHECK);
duke@435 812 }
duke@435 813 break;
duke@435 814 case Bytecodes::_anewarray:
duke@435 815 { Bytecode_anewarray* anew = Bytecode_anewarray_at(caller_method->bcp_from(bci));
duke@435 816 klassOop ek = caller_method->constants()->klass_at(anew->index(), CHECK);
duke@435 817 k = Klass::cast(ek)->array_klass(CHECK);
duke@435 818 }
duke@435 819 break;
duke@435 820 case Bytecodes::_ldc:
duke@435 821 case Bytecodes::_ldc_w:
duke@435 822 {
duke@435 823 Bytecode_loadconstant* cc = Bytecode_loadconstant_at(caller_method(),
duke@435 824 caller_method->bcp_from(bci));
duke@435 825 klassOop resolved = caller_method->constants()->klass_at(cc->index(), CHECK);
duke@435 826 // ldc wants the java mirror.
duke@435 827 k = resolved->klass_part()->java_mirror();
duke@435 828 }
duke@435 829 break;
duke@435 830 default: Unimplemented();
duke@435 831 }
duke@435 832 // convert to handle
duke@435 833 load_klass = Handle(THREAD, k);
duke@435 834 } else {
duke@435 835 ShouldNotReachHere();
duke@435 836 }
duke@435 837
duke@435 838 if (deoptimize_for_volatile) {
duke@435 839 // At compile time we assumed the field wasn't volatile but after
duke@435 840 // loading it turns out it was volatile so we have to throw the
duke@435 841 // compiled code out and let it be regenerated.
duke@435 842 if (TracePatching) {
duke@435 843 tty->print_cr("Deoptimizing for patching volatile field reference");
duke@435 844 }
never@920 845 // It's possible the nmethod was invalidated in the last
never@920 846 // safepoint, but if it's still alive then make it not_entrant.
never@920 847 nmethod* nm = CodeCache::find_nmethod(caller_frame.pc());
never@920 848 if (nm != NULL) {
never@920 849 nm->make_not_entrant();
never@920 850 }
never@920 851
duke@435 852 VM_DeoptimizeFrame deopt(thread, caller_frame.id());
duke@435 853 VMThread::execute(&deopt);
duke@435 854
duke@435 855 // Return to the now deoptimized frame.
duke@435 856 }
duke@435 857
duke@435 858
duke@435 859 // Now copy code back
duke@435 860
duke@435 861 {
duke@435 862 MutexLockerEx ml_patch (Patching_lock, Mutex::_no_safepoint_check_flag);
duke@435 863 //
duke@435 864 // Deoptimization may have happened while we waited for the lock.
duke@435 865 // In that case we don't bother to do any patching we just return
duke@435 866 // and let the deopt happen
duke@435 867 if (!caller_is_deopted()) {
duke@435 868 NativeGeneralJump* jump = nativeGeneralJump_at(caller_frame.pc());
duke@435 869 address instr_pc = jump->jump_destination();
duke@435 870 NativeInstruction* ni = nativeInstruction_at(instr_pc);
duke@435 871 if (ni->is_jump() ) {
duke@435 872 // the jump has not been patched yet
duke@435 873 // The jump destination is slow case and therefore not part of the stubs
duke@435 874 // (stubs are only for StaticCalls)
duke@435 875
duke@435 876 // format of buffer
duke@435 877 // ....
duke@435 878 // instr byte 0 <-- copy_buff
duke@435 879 // instr byte 1
duke@435 880 // ..
duke@435 881 // instr byte n-1
duke@435 882 // n
duke@435 883 // .... <-- call destination
duke@435 884
duke@435 885 address stub_location = caller_frame.pc() + PatchingStub::patch_info_offset();
duke@435 886 unsigned char* byte_count = (unsigned char*) (stub_location - 1);
duke@435 887 unsigned char* byte_skip = (unsigned char*) (stub_location - 2);
duke@435 888 unsigned char* being_initialized_entry_offset = (unsigned char*) (stub_location - 3);
duke@435 889 address copy_buff = stub_location - *byte_skip - *byte_count;
duke@435 890 address being_initialized_entry = stub_location - *being_initialized_entry_offset;
duke@435 891 if (TracePatching) {
duke@435 892 tty->print_cr(" Patching %s at bci %d at address 0x%x (%s)", Bytecodes::name(code), bci,
duke@435 893 instr_pc, (stub_id == Runtime1::access_field_patching_id) ? "field" : "klass");
duke@435 894 nmethod* caller_code = CodeCache::find_nmethod(caller_frame.pc());
duke@435 895 assert(caller_code != NULL, "nmethod not found");
duke@435 896
duke@435 897 // NOTE we use pc() not original_pc() because we already know they are
duke@435 898 // identical otherwise we'd have never entered this block of code
duke@435 899
duke@435 900 OopMap* map = caller_code->oop_map_for_return_address(caller_frame.pc());
duke@435 901 assert(map != NULL, "null check");
duke@435 902 map->print();
duke@435 903 tty->cr();
duke@435 904
duke@435 905 Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
duke@435 906 }
duke@435 907 // depending on the code below, do_patch says whether to copy the patch body back into the nmethod
duke@435 908 bool do_patch = true;
duke@435 909 if (stub_id == Runtime1::access_field_patching_id) {
duke@435 910 // The offset may not be correct if the class was not loaded at code generation time.
duke@435 911 // Set it now.
duke@435 912 NativeMovRegMem* n_move = nativeMovRegMem_at(copy_buff);
duke@435 913 assert(n_move->offset() == 0 || (n_move->offset() == 4 && (patch_field_type == T_DOUBLE || patch_field_type == T_LONG)), "illegal offset for type");
duke@435 914 assert(patch_field_offset >= 0, "illegal offset");
duke@435 915 n_move->add_offset_in_bytes(patch_field_offset);
duke@435 916 } else if (stub_id == Runtime1::load_klass_patching_id) {
duke@435 917 // If a getstatic or putstatic is referencing a klass which
duke@435 918 // isn't fully initialized, the patch body isn't copied into
duke@435 919 // place until initialization is complete. In this case the
duke@435 920 // patch site is setup so that any threads besides the
duke@435 921 // initializing thread are forced to come into the VM and
duke@435 922 // block.
duke@435 923 do_patch = (code != Bytecodes::_getstatic && code != Bytecodes::_putstatic) ||
duke@435 924 instanceKlass::cast(init_klass())->is_initialized();
duke@435 925 NativeGeneralJump* jump = nativeGeneralJump_at(instr_pc);
duke@435 926 if (jump->jump_destination() == being_initialized_entry) {
duke@435 927 assert(do_patch == true, "initialization must be complete at this point");
duke@435 928 } else {
duke@435 929 // patch the instruction <move reg, klass>
duke@435 930 NativeMovConstReg* n_copy = nativeMovConstReg_at(copy_buff);
duke@435 931 assert(n_copy->data() == 0, "illegal init value");
duke@435 932 assert(load_klass() != NULL, "klass not set");
duke@435 933 n_copy->set_data((intx) (load_klass()));
duke@435 934
duke@435 935 if (TracePatching) {
duke@435 936 Disassembler::decode(copy_buff, copy_buff + *byte_count, tty);
duke@435 937 }
duke@435 938
duke@435 939 #ifdef SPARC
duke@435 940 // Update the oop location in the nmethod with the proper
duke@435 941 // oop. When the code was generated, a NULL was stuffed
duke@435 942 // in the oop table and that table needs to be update to
duke@435 943 // have the right value. On intel the value is kept
duke@435 944 // directly in the instruction instead of in the oop
duke@435 945 // table, so set_data above effectively updated the value.
duke@435 946 nmethod* nm = CodeCache::find_nmethod(instr_pc);
duke@435 947 assert(nm != NULL, "invalid nmethod_pc");
duke@435 948 RelocIterator oops(nm, copy_buff, copy_buff + 1);
duke@435 949 bool found = false;
duke@435 950 while (oops.next() && !found) {
duke@435 951 if (oops.type() == relocInfo::oop_type) {
duke@435 952 oop_Relocation* r = oops.oop_reloc();
duke@435 953 oop* oop_adr = r->oop_addr();
duke@435 954 *oop_adr = load_klass();
duke@435 955 r->fix_oop_relocation();
duke@435 956 found = true;
duke@435 957 }
duke@435 958 }
duke@435 959 assert(found, "the oop must exist!");
duke@435 960 #endif
duke@435 961
duke@435 962 }
duke@435 963 } else {
duke@435 964 ShouldNotReachHere();
duke@435 965 }
duke@435 966 if (do_patch) {
duke@435 967 // replace instructions
duke@435 968 // first replace the tail, then the call
duke@435 969 for (int i = NativeCall::instruction_size; i < *byte_count; i++) {
duke@435 970 address ptr = copy_buff + i;
duke@435 971 int a_byte = (*ptr) & 0xFF;
duke@435 972 address dst = instr_pc + i;
duke@435 973 *(unsigned char*)dst = (unsigned char) a_byte;
duke@435 974 }
duke@435 975 ICache::invalidate_range(instr_pc, *byte_count);
duke@435 976 NativeGeneralJump::replace_mt_safe(instr_pc, copy_buff);
duke@435 977
duke@435 978 if (stub_id == Runtime1::load_klass_patching_id) {
duke@435 979 // update relocInfo to oop
duke@435 980 nmethod* nm = CodeCache::find_nmethod(instr_pc);
duke@435 981 assert(nm != NULL, "invalid nmethod_pc");
duke@435 982
duke@435 983 // The old patch site is now a move instruction so update
duke@435 984 // the reloc info so that it will get updated during
duke@435 985 // future GCs.
duke@435 986 RelocIterator iter(nm, (address)instr_pc, (address)(instr_pc + 1));
duke@435 987 relocInfo::change_reloc_info_for_address(&iter, (address) instr_pc,
duke@435 988 relocInfo::none, relocInfo::oop_type);
duke@435 989 #ifdef SPARC
duke@435 990 // Sparc takes two relocations for an oop so update the second one.
duke@435 991 address instr_pc2 = instr_pc + NativeMovConstReg::add_offset;
duke@435 992 RelocIterator iter2(nm, instr_pc2, instr_pc2 + 1);
duke@435 993 relocInfo::change_reloc_info_for_address(&iter2, (address) instr_pc2,
duke@435 994 relocInfo::none, relocInfo::oop_type);
duke@435 995 #endif
duke@435 996 }
duke@435 997
duke@435 998 } else {
duke@435 999 ICache::invalidate_range(copy_buff, *byte_count);
duke@435 1000 NativeGeneralJump::insert_unconditional(instr_pc, being_initialized_entry);
duke@435 1001 }
duke@435 1002 }
duke@435 1003 }
duke@435 1004 }
duke@435 1005 JRT_END
duke@435 1006
duke@435 1007 //
duke@435 1008 // Entry point for compiled code. We want to patch a nmethod.
duke@435 1009 // We don't do a normal VM transition here because we want to
duke@435 1010 // know after the patching is complete and any safepoint(s) are taken
duke@435 1011 // if the calling nmethod was deoptimized. We do this by calling a
duke@435 1012 // helper method which does the normal VM transition and when it
duke@435 1013 // completes we can check for deoptimization. This simplifies the
duke@435 1014 // assembly code in the cpu directories.
duke@435 1015 //
duke@435 1016 int Runtime1::move_klass_patching(JavaThread* thread) {
duke@435 1017 //
duke@435 1018 // NOTE: we are still in Java
duke@435 1019 //
duke@435 1020 Thread* THREAD = thread;
duke@435 1021 debug_only(NoHandleMark nhm;)
duke@435 1022 {
duke@435 1023 // Enter VM mode
duke@435 1024
duke@435 1025 ResetNoHandleMark rnhm;
duke@435 1026 patch_code(thread, load_klass_patching_id);
duke@435 1027 }
duke@435 1028 // Back in JAVA, use no oops DON'T safepoint
duke@435 1029
duke@435 1030 // Return true if calling code is deoptimized
duke@435 1031
duke@435 1032 return caller_is_deopted();
duke@435 1033 }
duke@435 1034
duke@435 1035 //
duke@435 1036 // Entry point for compiled code. We want to patch a nmethod.
duke@435 1037 // We don't do a normal VM transition here because we want to
duke@435 1038 // know after the patching is complete and any safepoint(s) are taken
duke@435 1039 // if the calling nmethod was deoptimized. We do this by calling a
duke@435 1040 // helper method which does the normal VM transition and when it
duke@435 1041 // completes we can check for deoptimization. This simplifies the
duke@435 1042 // assembly code in the cpu directories.
duke@435 1043 //
duke@435 1044
duke@435 1045 int Runtime1::access_field_patching(JavaThread* thread) {
duke@435 1046 //
duke@435 1047 // NOTE: we are still in Java
duke@435 1048 //
duke@435 1049 Thread* THREAD = thread;
duke@435 1050 debug_only(NoHandleMark nhm;)
duke@435 1051 {
duke@435 1052 // Enter VM mode
duke@435 1053
duke@435 1054 ResetNoHandleMark rnhm;
duke@435 1055 patch_code(thread, access_field_patching_id);
duke@435 1056 }
duke@435 1057 // Back in JAVA, use no oops DON'T safepoint
duke@435 1058
duke@435 1059 // Return true if calling code is deoptimized
duke@435 1060
duke@435 1061 return caller_is_deopted();
duke@435 1062 JRT_END
duke@435 1063
duke@435 1064
duke@435 1065 JRT_LEAF(void, Runtime1::trace_block_entry(jint block_id))
duke@435 1066 // for now we just print out the block id
duke@435 1067 tty->print("%d ", block_id);
duke@435 1068 JRT_END
duke@435 1069
duke@435 1070
coleenp@548 1071 // Array copy return codes.
coleenp@548 1072 enum {
coleenp@548 1073 ac_failed = -1, // arraycopy failed
coleenp@548 1074 ac_ok = 0 // arraycopy succeeded
coleenp@548 1075 };
coleenp@548 1076
coleenp@548 1077
coleenp@548 1078 template <class T> int obj_arraycopy_work(oopDesc* src, T* src_addr,
coleenp@548 1079 oopDesc* dst, T* dst_addr,
coleenp@548 1080 int length) {
coleenp@548 1081
coleenp@548 1082 // For performance reasons, we assume we are using a card marking write
coleenp@548 1083 // barrier. The assert will fail if this is not the case.
coleenp@548 1084 // Note that we use the non-virtual inlineable variant of write_ref_array.
coleenp@548 1085 BarrierSet* bs = Universe::heap()->barrier_set();
coleenp@548 1086 assert(bs->has_write_ref_array_opt(),
coleenp@548 1087 "Barrier set must have ref array opt");
coleenp@548 1088 if (src == dst) {
coleenp@548 1089 // same object, no check
coleenp@548 1090 Copy::conjoint_oops_atomic(src_addr, dst_addr, length);
coleenp@548 1091 bs->write_ref_array(MemRegion((HeapWord*)dst_addr,
coleenp@548 1092 (HeapWord*)(dst_addr + length)));
coleenp@548 1093 return ac_ok;
coleenp@548 1094 } else {
coleenp@548 1095 klassOop bound = objArrayKlass::cast(dst->klass())->element_klass();
coleenp@548 1096 klassOop stype = objArrayKlass::cast(src->klass())->element_klass();
coleenp@548 1097 if (stype == bound || Klass::cast(stype)->is_subtype_of(bound)) {
coleenp@548 1098 // Elements are guaranteed to be subtypes, so no check necessary
coleenp@548 1099 Copy::conjoint_oops_atomic(src_addr, dst_addr, length);
coleenp@548 1100 bs->write_ref_array(MemRegion((HeapWord*)dst_addr,
coleenp@548 1101 (HeapWord*)(dst_addr + length)));
coleenp@548 1102 return ac_ok;
coleenp@548 1103 }
coleenp@548 1104 }
coleenp@548 1105 return ac_failed;
coleenp@548 1106 }
coleenp@548 1107
duke@435 1108 // fast and direct copy of arrays; returning -1, means that an exception may be thrown
duke@435 1109 // and we did not copy anything
duke@435 1110 JRT_LEAF(int, Runtime1::arraycopy(oopDesc* src, int src_pos, oopDesc* dst, int dst_pos, int length))
duke@435 1111 #ifndef PRODUCT
duke@435 1112 _generic_arraycopy_cnt++; // Slow-path oop array copy
duke@435 1113 #endif
duke@435 1114
duke@435 1115 if (src == NULL || dst == NULL || src_pos < 0 || dst_pos < 0 || length < 0) return ac_failed;
duke@435 1116 if (!dst->is_array() || !src->is_array()) return ac_failed;
duke@435 1117 if ((unsigned int) arrayOop(src)->length() < (unsigned int)src_pos + (unsigned int)length) return ac_failed;
duke@435 1118 if ((unsigned int) arrayOop(dst)->length() < (unsigned int)dst_pos + (unsigned int)length) return ac_failed;
duke@435 1119
duke@435 1120 if (length == 0) return ac_ok;
duke@435 1121 if (src->is_typeArray()) {
duke@435 1122 const klassOop klass_oop = src->klass();
duke@435 1123 if (klass_oop != dst->klass()) return ac_failed;
duke@435 1124 typeArrayKlass* klass = typeArrayKlass::cast(klass_oop);
duke@435 1125 const int l2es = klass->log2_element_size();
duke@435 1126 const int ihs = klass->array_header_in_bytes() / wordSize;
duke@435 1127 char* src_addr = (char*) ((oopDesc**)src + ihs) + (src_pos << l2es);
duke@435 1128 char* dst_addr = (char*) ((oopDesc**)dst + ihs) + (dst_pos << l2es);
duke@435 1129 // Potential problem: memmove is not guaranteed to be word atomic
duke@435 1130 // Revisit in Merlin
duke@435 1131 memmove(dst_addr, src_addr, length << l2es);
duke@435 1132 return ac_ok;
duke@435 1133 } else if (src->is_objArray() && dst->is_objArray()) {
coleenp@548 1134 if (UseCompressedOops) { // will need for tiered
coleenp@548 1135 narrowOop *src_addr = objArrayOop(src)->obj_at_addr<narrowOop>(src_pos);
coleenp@548 1136 narrowOop *dst_addr = objArrayOop(dst)->obj_at_addr<narrowOop>(dst_pos);
coleenp@548 1137 return obj_arraycopy_work(src, src_addr, dst, dst_addr, length);
duke@435 1138 } else {
coleenp@548 1139 oop *src_addr = objArrayOop(src)->obj_at_addr<oop>(src_pos);
coleenp@548 1140 oop *dst_addr = objArrayOop(dst)->obj_at_addr<oop>(dst_pos);
coleenp@548 1141 return obj_arraycopy_work(src, src_addr, dst, dst_addr, length);
duke@435 1142 }
duke@435 1143 }
duke@435 1144 return ac_failed;
duke@435 1145 JRT_END
duke@435 1146
duke@435 1147
duke@435 1148 JRT_LEAF(void, Runtime1::primitive_arraycopy(HeapWord* src, HeapWord* dst, int length))
duke@435 1149 #ifndef PRODUCT
duke@435 1150 _primitive_arraycopy_cnt++;
duke@435 1151 #endif
duke@435 1152
duke@435 1153 if (length == 0) return;
duke@435 1154 // Not guaranteed to be word atomic, but that doesn't matter
duke@435 1155 // for anything but an oop array, which is covered by oop_arraycopy.
duke@435 1156 Copy::conjoint_bytes(src, dst, length);
duke@435 1157 JRT_END
duke@435 1158
duke@435 1159 JRT_LEAF(void, Runtime1::oop_arraycopy(HeapWord* src, HeapWord* dst, int num))
duke@435 1160 #ifndef PRODUCT
duke@435 1161 _oop_arraycopy_cnt++;
duke@435 1162 #endif
duke@435 1163
duke@435 1164 if (num == 0) return;
duke@435 1165 Copy::conjoint_oops_atomic((oop*) src, (oop*) dst, num);
duke@435 1166 BarrierSet* bs = Universe::heap()->barrier_set();
duke@435 1167 bs->write_ref_array(MemRegion(dst, dst + num));
duke@435 1168 JRT_END
duke@435 1169
duke@435 1170
duke@435 1171 #ifndef PRODUCT
duke@435 1172 void Runtime1::print_statistics() {
duke@435 1173 tty->print_cr("C1 Runtime statistics:");
duke@435 1174 tty->print_cr(" _resolve_invoke_virtual_cnt: %d", SharedRuntime::_resolve_virtual_ctr);
duke@435 1175 tty->print_cr(" _resolve_invoke_opt_virtual_cnt: %d", SharedRuntime::_resolve_opt_virtual_ctr);
duke@435 1176 tty->print_cr(" _resolve_invoke_static_cnt: %d", SharedRuntime::_resolve_static_ctr);
duke@435 1177 tty->print_cr(" _handle_wrong_method_cnt: %d", SharedRuntime::_wrong_method_ctr);
duke@435 1178 tty->print_cr(" _ic_miss_cnt: %d", SharedRuntime::_ic_miss_ctr);
duke@435 1179 tty->print_cr(" _generic_arraycopy_cnt: %d", _generic_arraycopy_cnt);
duke@435 1180 tty->print_cr(" _primitive_arraycopy_cnt: %d", _primitive_arraycopy_cnt);
duke@435 1181 tty->print_cr(" _oop_arraycopy_cnt: %d", _oop_arraycopy_cnt);
duke@435 1182 tty->print_cr(" _arraycopy_slowcase_cnt: %d", _arraycopy_slowcase_cnt);
duke@435 1183
duke@435 1184 tty->print_cr(" _new_type_array_slowcase_cnt: %d", _new_type_array_slowcase_cnt);
duke@435 1185 tty->print_cr(" _new_object_array_slowcase_cnt: %d", _new_object_array_slowcase_cnt);
duke@435 1186 tty->print_cr(" _new_instance_slowcase_cnt: %d", _new_instance_slowcase_cnt);
duke@435 1187 tty->print_cr(" _new_multi_array_slowcase_cnt: %d", _new_multi_array_slowcase_cnt);
duke@435 1188 tty->print_cr(" _monitorenter_slowcase_cnt: %d", _monitorenter_slowcase_cnt);
duke@435 1189 tty->print_cr(" _monitorexit_slowcase_cnt: %d", _monitorexit_slowcase_cnt);
duke@435 1190 tty->print_cr(" _patch_code_slowcase_cnt: %d", _patch_code_slowcase_cnt);
duke@435 1191
duke@435 1192 tty->print_cr(" _throw_range_check_exception_count: %d:", _throw_range_check_exception_count);
duke@435 1193 tty->print_cr(" _throw_index_exception_count: %d:", _throw_index_exception_count);
duke@435 1194 tty->print_cr(" _throw_div0_exception_count: %d:", _throw_div0_exception_count);
duke@435 1195 tty->print_cr(" _throw_null_pointer_exception_count: %d:", _throw_null_pointer_exception_count);
duke@435 1196 tty->print_cr(" _throw_class_cast_exception_count: %d:", _throw_class_cast_exception_count);
duke@435 1197 tty->print_cr(" _throw_incompatible_class_change_error_count: %d:", _throw_incompatible_class_change_error_count);
duke@435 1198 tty->print_cr(" _throw_array_store_exception_count: %d:", _throw_array_store_exception_count);
duke@435 1199 tty->print_cr(" _throw_count: %d:", _throw_count);
duke@435 1200
duke@435 1201 SharedRuntime::print_ic_miss_histogram();
duke@435 1202 tty->cr();
duke@435 1203 }
duke@435 1204 #endif // PRODUCT

mercurial