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