src/share/vm/c1/c1_Runtime1.cpp

Wed, 02 Feb 2011 11:35:26 -0500

author
bobv
date
Wed, 02 Feb 2011 11:35:26 -0500
changeset 2508
b92c45f2bc75
parent 2488
e4fee0bdaa85
child 2603
1b4e6a5d98e0
permissions
-rw-r--r--

7016023: Enable building ARM and PPC from src/closed repository
Reviewed-by: dholmes, bdelsart

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

mercurial