src/share/vm/c1/c1_Runtime1.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7598
ddce0b7cee93
parent 7535
7ae4e26cb1e0
child 8604
04d83ba48607
permissions
-rw-r--r--

merge

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

mercurial