codeBlob.cpp revision 2073:b92c45f2bc75
1/* 2 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25#include "precompiled.hpp" 26#include "code/codeBlob.hpp" 27#include "code/codeCache.hpp" 28#include "code/relocInfo.hpp" 29#include "compiler/disassembler.hpp" 30#include "interpreter/bytecode.hpp" 31#include "memory/allocation.inline.hpp" 32#include "memory/heap.hpp" 33#include "oops/oop.inline.hpp" 34#include "prims/forte.hpp" 35#include "runtime/handles.inline.hpp" 36#include "runtime/interfaceSupport.hpp" 37#include "runtime/mutexLocker.hpp" 38#include "runtime/safepoint.hpp" 39#include "runtime/sharedRuntime.hpp" 40#include "runtime/vframe.hpp" 41#include "services/memoryService.hpp" 42#ifdef TARGET_ARCH_x86 43# include "nativeInst_x86.hpp" 44#endif 45#ifdef TARGET_ARCH_sparc 46# include "nativeInst_sparc.hpp" 47#endif 48#ifdef TARGET_ARCH_zero 49# include "nativeInst_zero.hpp" 50#endif 51#ifdef TARGET_ARCH_arm 52# include "nativeInst_arm.hpp" 53#endif 54#ifdef TARGET_ARCH_ppc 55# include "nativeInst_ppc.hpp" 56#endif 57#ifdef COMPILER1 58#include "c1/c1_Runtime1.hpp" 59#endif 60 61unsigned int align_code_offset(int offset) { 62 // align the size to CodeEntryAlignment 63 return 64 ((offset + (int)CodeHeap::header_size() + (CodeEntryAlignment-1)) & ~(CodeEntryAlignment-1)) 65 - (int)CodeHeap::header_size(); 66} 67 68 69// This must be consistent with the CodeBlob constructor's layout actions. 70unsigned int CodeBlob::allocation_size(CodeBuffer* cb, int header_size) { 71 unsigned int size = header_size; 72 size += round_to(cb->total_relocation_size(), oopSize); 73 // align the size to CodeEntryAlignment 74 size = align_code_offset(size); 75 size += round_to(cb->total_content_size(), oopSize); 76 size += round_to(cb->total_oop_size(), oopSize); 77 return size; 78} 79 80 81// Creates a simple CodeBlob. Sets up the size of the different regions. 82CodeBlob::CodeBlob(const char* name, int header_size, int size, int frame_complete, int locs_size) { 83 assert(size == round_to(size, oopSize), "unaligned size"); 84 assert(locs_size == round_to(locs_size, oopSize), "unaligned size"); 85 assert(header_size == round_to(header_size, oopSize), "unaligned size"); 86 assert(!UseRelocIndex, "no space allocated for reloc index yet"); 87 88 // Note: If UseRelocIndex is enabled, there needs to be (at least) one 89 // extra word for the relocation information, containing the reloc 90 // index table length. Unfortunately, the reloc index table imple- 91 // mentation is not easily understandable and thus it is not clear 92 // what exactly the format is supposed to be. For now, we just turn 93 // off the use of this table (gri 7/6/2000). 94 95 _name = name; 96 _size = size; 97 _frame_complete_offset = frame_complete; 98 _header_size = header_size; 99 _relocation_size = locs_size; 100 _content_offset = align_code_offset(header_size + _relocation_size); 101 _code_offset = _content_offset; 102 _data_offset = size; 103 _frame_size = 0; 104 set_oop_maps(NULL); 105} 106 107 108// Creates a CodeBlob from a CodeBuffer. Sets up the size of the different regions, 109// and copy code and relocation info. 110CodeBlob::CodeBlob( 111 const char* name, 112 CodeBuffer* cb, 113 int header_size, 114 int size, 115 int frame_complete, 116 int frame_size, 117 OopMapSet* oop_maps 118) { 119 assert(size == round_to(size, oopSize), "unaligned size"); 120 assert(header_size == round_to(header_size, oopSize), "unaligned size"); 121 122 _name = name; 123 _size = size; 124 _frame_complete_offset = frame_complete; 125 _header_size = header_size; 126 _relocation_size = round_to(cb->total_relocation_size(), oopSize); 127 _content_offset = align_code_offset(header_size + _relocation_size); 128 _code_offset = _content_offset + cb->total_offset_of(cb->insts()); 129 _data_offset = _content_offset + round_to(cb->total_content_size(), oopSize); 130 assert(_data_offset <= size, "codeBlob is too small"); 131 132 cb->copy_code_and_locs_to(this); 133 set_oop_maps(oop_maps); 134 _frame_size = frame_size; 135#ifdef COMPILER1 136 // probably wrong for tiered 137 assert(_frame_size >= -1, "must use frame size or -1 for runtime stubs"); 138#endif // COMPILER1 139} 140 141 142void CodeBlob::set_oop_maps(OopMapSet* p) { 143 // Danger Will Robinson! This method allocates a big 144 // chunk of memory, its your job to free it. 145 if (p != NULL) { 146 // We need to allocate a chunk big enough to hold the OopMapSet and all of its OopMaps 147 _oop_maps = (OopMapSet* )NEW_C_HEAP_ARRAY(unsigned char, p->heap_size()); 148 p->copy_to((address)_oop_maps); 149 } else { 150 _oop_maps = NULL; 151 } 152} 153 154 155void CodeBlob::flush() { 156 if (_oop_maps) { 157 FREE_C_HEAP_ARRAY(unsigned char, _oop_maps); 158 _oop_maps = NULL; 159 } 160 _comments.free(); 161} 162 163 164OopMap* CodeBlob::oop_map_for_return_address(address return_address) { 165 assert(oop_maps() != NULL, "nope"); 166 return oop_maps()->find_map_at_offset((intptr_t) return_address - (intptr_t) code_begin()); 167} 168 169 170//---------------------------------------------------------------------------------------------------- 171// Implementation of BufferBlob 172 173 174BufferBlob::BufferBlob(const char* name, int size) 175: CodeBlob(name, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, /*locs_size:*/ 0) 176{} 177 178BufferBlob* BufferBlob::create(const char* name, int buffer_size) { 179 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 180 181 BufferBlob* blob = NULL; 182 unsigned int size = sizeof(BufferBlob); 183 // align the size to CodeEntryAlignment 184 size = align_code_offset(size); 185 size += round_to(buffer_size, oopSize); 186 assert(name != NULL, "must provide a name"); 187 { 188 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 189 blob = new (size) BufferBlob(name, size); 190 } 191 // Track memory usage statistic after releasing CodeCache_lock 192 MemoryService::track_code_cache_memory_usage(); 193 194 return blob; 195} 196 197 198BufferBlob::BufferBlob(const char* name, int size, CodeBuffer* cb) 199 : CodeBlob(name, cb, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, 0, NULL) 200{} 201 202BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) { 203 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 204 205 BufferBlob* blob = NULL; 206 unsigned int size = allocation_size(cb, sizeof(BufferBlob)); 207 assert(name != NULL, "must provide a name"); 208 { 209 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 210 blob = new (size) BufferBlob(name, size, cb); 211 } 212 // Track memory usage statistic after releasing CodeCache_lock 213 MemoryService::track_code_cache_memory_usage(); 214 215 return blob; 216} 217 218 219void* BufferBlob::operator new(size_t s, unsigned size) { 220 void* p = CodeCache::allocate(size); 221 return p; 222} 223 224 225void BufferBlob::free( BufferBlob *blob ) { 226 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 227 { 228 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 229 CodeCache::free((CodeBlob*)blob); 230 } 231 // Track memory usage statistic after releasing CodeCache_lock 232 MemoryService::track_code_cache_memory_usage(); 233} 234 235 236//---------------------------------------------------------------------------------------------------- 237// Implementation of AdapterBlob 238 239AdapterBlob::AdapterBlob(int size, CodeBuffer* cb) : 240 BufferBlob("I2C/C2I adapters", size, cb) { 241 CodeCache::commit(this); 242} 243 244AdapterBlob* AdapterBlob::create(CodeBuffer* cb) { 245 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 246 247 AdapterBlob* blob = NULL; 248 unsigned int size = allocation_size(cb, sizeof(AdapterBlob)); 249 { 250 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 251 blob = new (size) AdapterBlob(size, cb); 252 } 253 // Track memory usage statistic after releasing CodeCache_lock 254 MemoryService::track_code_cache_memory_usage(); 255 256 return blob; 257} 258 259 260//---------------------------------------------------------------------------------------------------- 261// Implementation of MethodHandlesAdapterBlob 262 263MethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) { 264 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 265 266 MethodHandlesAdapterBlob* blob = NULL; 267 unsigned int size = sizeof(MethodHandlesAdapterBlob); 268 // align the size to CodeEntryAlignment 269 size = align_code_offset(size); 270 size += round_to(buffer_size, oopSize); 271 { 272 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 273 blob = new (size) MethodHandlesAdapterBlob(size); 274 } 275 // Track memory usage statistic after releasing CodeCache_lock 276 MemoryService::track_code_cache_memory_usage(); 277 278 return blob; 279} 280 281 282//---------------------------------------------------------------------------------------------------- 283// Implementation of RuntimeStub 284 285RuntimeStub::RuntimeStub( 286 const char* name, 287 CodeBuffer* cb, 288 int size, 289 int frame_complete, 290 int frame_size, 291 OopMapSet* oop_maps, 292 bool caller_must_gc_arguments 293) 294: CodeBlob(name, cb, sizeof(RuntimeStub), size, frame_complete, frame_size, oop_maps) 295{ 296 _caller_must_gc_arguments = caller_must_gc_arguments; 297} 298 299 300RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name, 301 CodeBuffer* cb, 302 int frame_complete, 303 int frame_size, 304 OopMapSet* oop_maps, 305 bool caller_must_gc_arguments) 306{ 307 RuntimeStub* stub = NULL; 308 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 309 { 310 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 311 unsigned int size = allocation_size(cb, sizeof(RuntimeStub)); 312 stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments); 313 } 314 315 // Do not hold the CodeCache lock during name formatting. 316 if (stub != NULL) { 317 char stub_id[256]; 318 jio_snprintf(stub_id, sizeof(stub_id), "RuntimeStub - %s", stub_name); 319 if (PrintStubCode) { 320 tty->print_cr("Decoding %s " INTPTR_FORMAT, stub_id, stub); 321 Disassembler::decode(stub->code_begin(), stub->code_end()); 322 } 323 Forte::register_stub(stub_id, stub->code_begin(), stub->code_end()); 324 325 if (JvmtiExport::should_post_dynamic_code_generated()) { 326 JvmtiExport::post_dynamic_code_generated(stub_name, stub->code_begin(), stub->code_end()); 327 } 328 } 329 330 // Track memory usage statistic after releasing CodeCache_lock 331 MemoryService::track_code_cache_memory_usage(); 332 333 return stub; 334} 335 336 337void* RuntimeStub::operator new(size_t s, unsigned size) { 338 void* p = CodeCache::allocate(size); 339 if (!p) fatal("Initial size of CodeCache is too small"); 340 return p; 341} 342 343 344//---------------------------------------------------------------------------------------------------- 345// Implementation of DeoptimizationBlob 346 347DeoptimizationBlob::DeoptimizationBlob( 348 CodeBuffer* cb, 349 int size, 350 OopMapSet* oop_maps, 351 int unpack_offset, 352 int unpack_with_exception_offset, 353 int unpack_with_reexecution_offset, 354 int frame_size 355) 356: SingletonBlob("DeoptimizationBlob", cb, sizeof(DeoptimizationBlob), size, frame_size, oop_maps) 357{ 358 _unpack_offset = unpack_offset; 359 _unpack_with_exception = unpack_with_exception_offset; 360 _unpack_with_reexecution = unpack_with_reexecution_offset; 361#ifdef COMPILER1 362 _unpack_with_exception_in_tls = -1; 363#endif 364} 365 366 367DeoptimizationBlob* DeoptimizationBlob::create( 368 CodeBuffer* cb, 369 OopMapSet* oop_maps, 370 int unpack_offset, 371 int unpack_with_exception_offset, 372 int unpack_with_reexecution_offset, 373 int frame_size) 374{ 375 DeoptimizationBlob* blob = NULL; 376 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 377 { 378 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 379 unsigned int size = allocation_size(cb, sizeof(DeoptimizationBlob)); 380 blob = new (size) DeoptimizationBlob(cb, 381 size, 382 oop_maps, 383 unpack_offset, 384 unpack_with_exception_offset, 385 unpack_with_reexecution_offset, 386 frame_size); 387 } 388 389 // Do not hold the CodeCache lock during name formatting. 390 if (blob != NULL) { 391 char blob_id[256]; 392 jio_snprintf(blob_id, sizeof(blob_id), "DeoptimizationBlob@" PTR_FORMAT, blob->code_begin()); 393 if (PrintStubCode) { 394 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 395 Disassembler::decode(blob->code_begin(), blob->code_end()); 396 } 397 Forte::register_stub(blob_id, blob->code_begin(), blob->code_end()); 398 399 if (JvmtiExport::should_post_dynamic_code_generated()) { 400 JvmtiExport::post_dynamic_code_generated("DeoptimizationBlob", blob->code_begin(), blob->code_end()); 401 } 402 } 403 404 // Track memory usage statistic after releasing CodeCache_lock 405 MemoryService::track_code_cache_memory_usage(); 406 407 return blob; 408} 409 410 411void* DeoptimizationBlob::operator new(size_t s, unsigned size) { 412 void* p = CodeCache::allocate(size); 413 if (!p) fatal("Initial size of CodeCache is too small"); 414 return p; 415} 416 417//---------------------------------------------------------------------------------------------------- 418// Implementation of UncommonTrapBlob 419 420#ifdef COMPILER2 421UncommonTrapBlob::UncommonTrapBlob( 422 CodeBuffer* cb, 423 int size, 424 OopMapSet* oop_maps, 425 int frame_size 426) 427: SingletonBlob("UncommonTrapBlob", cb, sizeof(UncommonTrapBlob), size, frame_size, oop_maps) 428{} 429 430 431UncommonTrapBlob* UncommonTrapBlob::create( 432 CodeBuffer* cb, 433 OopMapSet* oop_maps, 434 int frame_size) 435{ 436 UncommonTrapBlob* blob = NULL; 437 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 438 { 439 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 440 unsigned int size = allocation_size(cb, sizeof(UncommonTrapBlob)); 441 blob = new (size) UncommonTrapBlob(cb, size, oop_maps, frame_size); 442 } 443 444 // Do not hold the CodeCache lock during name formatting. 445 if (blob != NULL) { 446 char blob_id[256]; 447 jio_snprintf(blob_id, sizeof(blob_id), "UncommonTrapBlob@" PTR_FORMAT, blob->code_begin()); 448 if (PrintStubCode) { 449 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 450 Disassembler::decode(blob->code_begin(), blob->code_end()); 451 } 452 Forte::register_stub(blob_id, blob->code_begin(), blob->code_end()); 453 454 if (JvmtiExport::should_post_dynamic_code_generated()) { 455 JvmtiExport::post_dynamic_code_generated("UncommonTrapBlob", blob->code_begin(), blob->code_end()); 456 } 457 } 458 459 // Track memory usage statistic after releasing CodeCache_lock 460 MemoryService::track_code_cache_memory_usage(); 461 462 return blob; 463} 464 465 466void* UncommonTrapBlob::operator new(size_t s, unsigned size) { 467 void* p = CodeCache::allocate(size); 468 if (!p) fatal("Initial size of CodeCache is too small"); 469 return p; 470} 471#endif // COMPILER2 472 473 474//---------------------------------------------------------------------------------------------------- 475// Implementation of ExceptionBlob 476 477#ifdef COMPILER2 478ExceptionBlob::ExceptionBlob( 479 CodeBuffer* cb, 480 int size, 481 OopMapSet* oop_maps, 482 int frame_size 483) 484: SingletonBlob("ExceptionBlob", cb, sizeof(ExceptionBlob), size, frame_size, oop_maps) 485{} 486 487 488ExceptionBlob* ExceptionBlob::create( 489 CodeBuffer* cb, 490 OopMapSet* oop_maps, 491 int frame_size) 492{ 493 ExceptionBlob* blob = NULL; 494 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 495 { 496 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 497 unsigned int size = allocation_size(cb, sizeof(ExceptionBlob)); 498 blob = new (size) ExceptionBlob(cb, size, oop_maps, frame_size); 499 } 500 501 // We do not need to hold the CodeCache lock during name formatting 502 if (blob != NULL) { 503 char blob_id[256]; 504 jio_snprintf(blob_id, sizeof(blob_id), "ExceptionBlob@" PTR_FORMAT, blob->code_begin()); 505 if (PrintStubCode) { 506 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 507 Disassembler::decode(blob->code_begin(), blob->code_end()); 508 } 509 Forte::register_stub(blob_id, blob->code_begin(), blob->code_end()); 510 511 if (JvmtiExport::should_post_dynamic_code_generated()) { 512 JvmtiExport::post_dynamic_code_generated("ExceptionBlob", blob->code_begin(), blob->code_end()); 513 } 514 } 515 516 // Track memory usage statistic after releasing CodeCache_lock 517 MemoryService::track_code_cache_memory_usage(); 518 519 return blob; 520} 521 522 523void* ExceptionBlob::operator new(size_t s, unsigned size) { 524 void* p = CodeCache::allocate(size); 525 if (!p) fatal("Initial size of CodeCache is too small"); 526 return p; 527} 528#endif // COMPILER2 529 530 531//---------------------------------------------------------------------------------------------------- 532// Implementation of SafepointBlob 533 534SafepointBlob::SafepointBlob( 535 CodeBuffer* cb, 536 int size, 537 OopMapSet* oop_maps, 538 int frame_size 539) 540: SingletonBlob("SafepointBlob", cb, sizeof(SafepointBlob), size, frame_size, oop_maps) 541{} 542 543 544SafepointBlob* SafepointBlob::create( 545 CodeBuffer* cb, 546 OopMapSet* oop_maps, 547 int frame_size) 548{ 549 SafepointBlob* blob = NULL; 550 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 551 { 552 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 553 unsigned int size = allocation_size(cb, sizeof(SafepointBlob)); 554 blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size); 555 } 556 557 // We do not need to hold the CodeCache lock during name formatting. 558 if (blob != NULL) { 559 char blob_id[256]; 560 jio_snprintf(blob_id, sizeof(blob_id), "SafepointBlob@" PTR_FORMAT, blob->code_begin()); 561 if (PrintStubCode) { 562 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 563 Disassembler::decode(blob->code_begin(), blob->code_end()); 564 } 565 Forte::register_stub(blob_id, blob->code_begin(), blob->code_end()); 566 567 if (JvmtiExport::should_post_dynamic_code_generated()) { 568 JvmtiExport::post_dynamic_code_generated("SafepointBlob", blob->code_begin(), blob->code_end()); 569 } 570 } 571 572 // Track memory usage statistic after releasing CodeCache_lock 573 MemoryService::track_code_cache_memory_usage(); 574 575 return blob; 576} 577 578 579void* SafepointBlob::operator new(size_t s, unsigned size) { 580 void* p = CodeCache::allocate(size); 581 if (!p) fatal("Initial size of CodeCache is too small"); 582 return p; 583} 584 585 586//---------------------------------------------------------------------------------------------------- 587// Verification and printing 588 589void CodeBlob::verify() { 590 ShouldNotReachHere(); 591} 592 593void CodeBlob::print_on(outputStream* st) const { 594 st->print_cr("[CodeBlob (" INTPTR_FORMAT ")]", this); 595 st->print_cr("Framesize: %d", _frame_size); 596} 597 598void CodeBlob::print_value_on(outputStream* st) const { 599 st->print_cr("[CodeBlob]"); 600} 601 602void BufferBlob::verify() { 603 // unimplemented 604} 605 606void BufferBlob::print_on(outputStream* st) const { 607 CodeBlob::print_on(st); 608 print_value_on(st); 609} 610 611void BufferBlob::print_value_on(outputStream* st) const { 612 st->print_cr("BufferBlob (" INTPTR_FORMAT ") used for %s", this, name()); 613} 614 615void RuntimeStub::verify() { 616 // unimplemented 617} 618 619void RuntimeStub::print_on(outputStream* st) const { 620 CodeBlob::print_on(st); 621 st->print("Runtime Stub (" INTPTR_FORMAT "): ", this); 622 st->print_cr(name()); 623 Disassembler::decode((CodeBlob*)this, st); 624} 625 626void RuntimeStub::print_value_on(outputStream* st) const { 627 st->print("RuntimeStub (" INTPTR_FORMAT "): ", this); st->print(name()); 628} 629 630void SingletonBlob::verify() { 631 // unimplemented 632} 633 634void SingletonBlob::print_on(outputStream* st) const { 635 CodeBlob::print_on(st); 636 st->print_cr(name()); 637 Disassembler::decode((CodeBlob*)this, st); 638} 639 640void SingletonBlob::print_value_on(outputStream* st) const { 641 st->print_cr(name()); 642} 643 644void DeoptimizationBlob::print_value_on(outputStream* st) const { 645 st->print_cr("Deoptimization (frame not available)"); 646} 647