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 OopMap* CodeBlob::oop_map_for_return_address(address return_address) { 130 address pc = return_address ; 131 assert (oop_maps() != NULL, "nope"); 132 return oop_maps()->find_map_at_offset ((intptr_t) pc - (intptr_t) instructions_begin()); 133 } 134 135 136 //---------------------------------------------------------------------------------------------------- 137 // Implementation of BufferBlob 138 139 140 BufferBlob::BufferBlob(const char* name, int size) 141 : CodeBlob(name, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, /*locs_size:*/ 0) 142 {} 143 144 BufferBlob* BufferBlob::create(const char* name, int buffer_size) { 145 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 146 147 BufferBlob* blob = NULL; 148 unsigned int size = sizeof(BufferBlob); 149 // align the size to CodeEntryAlignment 150 size = align_code_offset(size); 151 size += round_to(buffer_size, oopSize); 152 assert(name != NULL, "must provide a name"); 153 { 154 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 155 blob = new (size) BufferBlob(name, size); 156 } 157 // Track memory usage statistic after releasing CodeCache_lock 158 MemoryService::track_code_cache_memory_usage(); 159 160 return blob; 161 } 162 163 164 BufferBlob::BufferBlob(const char* name, int size, CodeBuffer* cb) 165 : CodeBlob(name, cb, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, 0, NULL) 166 {} 167 168 BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) { 169 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 170 171 BufferBlob* blob = NULL; 172 unsigned int size = allocation_size(cb, sizeof(BufferBlob)); 173 assert(name != NULL, "must provide a name"); 174 { 175 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 176 blob = new (size) BufferBlob(name, size, cb); 177 } 178 // Track memory usage statistic after releasing CodeCache_lock 179 MemoryService::track_code_cache_memory_usage(); 180 181 return blob; 182 } 183 184 185 void* BufferBlob::operator new(size_t s, unsigned size) { 186 void* p = CodeCache::allocate(size); 187 return p; 188 } 189 190 191 void BufferBlob::free( BufferBlob *blob ) { 192 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 193 { 194 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 195 CodeCache::free((CodeBlob*)blob); 196 } 197 // Track memory usage statistic after releasing CodeCache_lock 198 MemoryService::track_code_cache_memory_usage(); 199 } 200 201 202 //---------------------------------------------------------------------------------------------------- 203 // Implementation of AdapterBlob 204 205 AdapterBlob* AdapterBlob::create(CodeBuffer* cb) { 206 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 207 208 AdapterBlob* blob = NULL; 209 unsigned int size = allocation_size(cb, sizeof(AdapterBlob)); 210 { 211 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 212 blob = new (size) AdapterBlob(size, cb); 213 } 214 // Track memory usage statistic after releasing CodeCache_lock 215 MemoryService::track_code_cache_memory_usage(); 216 217 return blob; 218 } 219 220 221 //---------------------------------------------------------------------------------------------------- 222 // Implementation of MethodHandlesAdapterBlob 223 224 MethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) { 225 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 226 227 MethodHandlesAdapterBlob* blob = NULL; 228 unsigned int size = sizeof(MethodHandlesAdapterBlob); 229 // align the size to CodeEntryAlignment 230 size = align_code_offset(size); 231 size += round_to(buffer_size, oopSize); 232 { 233 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 234 blob = new (size) MethodHandlesAdapterBlob(size); 235 } 236 // Track memory usage statistic after releasing CodeCache_lock 237 MemoryService::track_code_cache_memory_usage(); 238 239 return blob; 240 } 241 242 243 //---------------------------------------------------------------------------------------------------- 244 // Implementation of RuntimeStub 245 246 RuntimeStub::RuntimeStub( 247 const char* name, 248 CodeBuffer* cb, 249 int size, 250 int frame_complete, 251 int frame_size, 252 OopMapSet* oop_maps, 253 bool caller_must_gc_arguments 254 ) 255 : CodeBlob(name, cb, sizeof(RuntimeStub), size, frame_complete, frame_size, oop_maps) 256 { 257 _caller_must_gc_arguments = caller_must_gc_arguments; 258 } 259 260 261 RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name, 262 CodeBuffer* cb, 263 int frame_complete, 264 int frame_size, 265 OopMapSet* oop_maps, 266 bool caller_must_gc_arguments) 267 { 268 RuntimeStub* stub = NULL; 269 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 270 { 271 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 272 unsigned int size = allocation_size(cb, sizeof(RuntimeStub)); 273 stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments); 274 } 275 276 // Do not hold the CodeCache lock during name formatting. 277 if (stub != NULL) { 278 char stub_id[256]; 279 jio_snprintf(stub_id, sizeof(stub_id), "RuntimeStub - %s", stub_name); 280 if (PrintStubCode) { 281 tty->print_cr("Decoding %s " INTPTR_FORMAT, stub_id, stub); 282 Disassembler::decode(stub->instructions_begin(), stub->instructions_end()); 283 } 284 VTune::register_stub(stub_id, stub->instructions_begin(), stub->instructions_end()); 285 Forte::register_stub(stub_id, stub->instructions_begin(), stub->instructions_end()); 286 287 if (JvmtiExport::should_post_dynamic_code_generated()) { 288 JvmtiExport::post_dynamic_code_generated(stub_name, stub->instructions_begin(), stub->instructions_end()); 289 } 290 } 291 292 // Track memory usage statistic after releasing CodeCache_lock 293 MemoryService::track_code_cache_memory_usage(); 294 295 return stub; 296 } 297 298 299 void* RuntimeStub::operator new(size_t s, unsigned size) { 300 void* p = CodeCache::allocate(size); 301 if (!p) fatal("Initial size of CodeCache is too small"); 302 return p; 303 } 304 305 306 //---------------------------------------------------------------------------------------------------- 307 // Implementation of DeoptimizationBlob 308 309 DeoptimizationBlob::DeoptimizationBlob( 310 CodeBuffer* cb, 311 int size, 312 OopMapSet* oop_maps, 313 int unpack_offset, 314 int unpack_with_exception_offset, 315 int unpack_with_reexecution_offset, 316 int frame_size 317 ) 318 : SingletonBlob("DeoptimizationBlob", cb, sizeof(DeoptimizationBlob), size, frame_size, oop_maps) 319 { 320 _unpack_offset = unpack_offset; 321 _unpack_with_exception = unpack_with_exception_offset; 322 _unpack_with_reexecution = unpack_with_reexecution_offset; 323 #ifdef COMPILER1 324 _unpack_with_exception_in_tls = -1; 325 #endif 326 } 327 328 329 DeoptimizationBlob* DeoptimizationBlob::create( 330 CodeBuffer* cb, 331 OopMapSet* oop_maps, 332 int unpack_offset, 333 int unpack_with_exception_offset, 334 int unpack_with_reexecution_offset, 335 int frame_size) 336 { 337 DeoptimizationBlob* blob = NULL; 338 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 339 { 340 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 341 unsigned int size = allocation_size(cb, sizeof(DeoptimizationBlob)); 342 blob = new (size) DeoptimizationBlob(cb, 343 size, 344 oop_maps, 345 unpack_offset, 346 unpack_with_exception_offset, 347 unpack_with_reexecution_offset, 348 frame_size); 349 } 350 351 // Do not hold the CodeCache lock during name formatting. 352 if (blob != NULL) { 353 char blob_id[256]; 354 jio_snprintf(blob_id, sizeof(blob_id), "DeoptimizationBlob@" PTR_FORMAT, blob->instructions_begin()); 355 if (PrintStubCode) { 356 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 357 Disassembler::decode(blob->instructions_begin(), blob->instructions_end()); 358 } 359 VTune::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end()); 360 Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end()); 361 362 if (JvmtiExport::should_post_dynamic_code_generated()) { 363 JvmtiExport::post_dynamic_code_generated("DeoptimizationBlob", 364 blob->instructions_begin(), 365 blob->instructions_end()); 366 } 367 } 368 369 // Track memory usage statistic after releasing CodeCache_lock 370 MemoryService::track_code_cache_memory_usage(); 371 372 return blob; 373 } 374 375 376 void* DeoptimizationBlob::operator new(size_t s, unsigned size) { 377 void* p = CodeCache::allocate(size); 378 if (!p) fatal("Initial size of CodeCache is too small"); 379 return p; 380 } 381 382 //---------------------------------------------------------------------------------------------------- 383 // Implementation of UncommonTrapBlob 384 385 #ifdef COMPILER2 386 UncommonTrapBlob::UncommonTrapBlob( 387 CodeBuffer* cb, 388 int size, 389 OopMapSet* oop_maps, 390 int frame_size 391 ) 392 : SingletonBlob("UncommonTrapBlob", cb, sizeof(UncommonTrapBlob), size, frame_size, oop_maps) 393 {} 394 395 396 UncommonTrapBlob* UncommonTrapBlob::create( 397 CodeBuffer* cb, 398 OopMapSet* oop_maps, 399 int frame_size) 400 { 401 UncommonTrapBlob* blob = NULL; 402 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 403 { 404 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 405 unsigned int size = allocation_size(cb, sizeof(UncommonTrapBlob)); 406 blob = new (size) UncommonTrapBlob(cb, size, oop_maps, frame_size); 407 } 408 409 // Do not hold the CodeCache lock during name formatting. 410 if (blob != NULL) { 411 char blob_id[256]; 412 jio_snprintf(blob_id, sizeof(blob_id), "UncommonTrapBlob@" PTR_FORMAT, blob->instructions_begin()); 413 if (PrintStubCode) { 414 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 415 Disassembler::decode(blob->instructions_begin(), blob->instructions_end()); 416 } 417 VTune::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end()); 418 Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end()); 419 420 if (JvmtiExport::should_post_dynamic_code_generated()) { 421 JvmtiExport::post_dynamic_code_generated("UncommonTrapBlob", 422 blob->instructions_begin(), 423 blob->instructions_end()); 424 } 425 } 426 427 // Track memory usage statistic after releasing CodeCache_lock 428 MemoryService::track_code_cache_memory_usage(); 429 430 return blob; 431 } 432 433 434 void* UncommonTrapBlob::operator new(size_t s, unsigned size) { 435 void* p = CodeCache::allocate(size); 436 if (!p) fatal("Initial size of CodeCache is too small"); 437 return p; 438 } 439 #endif // COMPILER2 440 441 442 //---------------------------------------------------------------------------------------------------- 443 // Implementation of ExceptionBlob 444 445 #ifdef COMPILER2 446 ExceptionBlob::ExceptionBlob( 447 CodeBuffer* cb, 448 int size, 449 OopMapSet* oop_maps, 450 int frame_size 451 ) 452 : SingletonBlob("ExceptionBlob", cb, sizeof(ExceptionBlob), size, frame_size, oop_maps) 453 {} 454 455 456 ExceptionBlob* ExceptionBlob::create( 457 CodeBuffer* cb, 458 OopMapSet* oop_maps, 459 int frame_size) 460 { 461 ExceptionBlob* blob = NULL; 462 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 463 { 464 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 465 unsigned int size = allocation_size(cb, sizeof(ExceptionBlob)); 466 blob = new (size) ExceptionBlob(cb, size, oop_maps, frame_size); 467 } 468 469 // We do not need to hold the CodeCache lock during name formatting 470 if (blob != NULL) { 471 char blob_id[256]; 472 jio_snprintf(blob_id, sizeof(blob_id), "ExceptionBlob@" PTR_FORMAT, blob->instructions_begin()); 473 if (PrintStubCode) { 474 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 475 Disassembler::decode(blob->instructions_begin(), blob->instructions_end()); 476 } 477 VTune::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end()); 478 Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end()); 479 480 if (JvmtiExport::should_post_dynamic_code_generated()) { 481 JvmtiExport::post_dynamic_code_generated("ExceptionBlob", 482 blob->instructions_begin(), 483 blob->instructions_end()); 484 } 485 } 486 487 // Track memory usage statistic after releasing CodeCache_lock 488 MemoryService::track_code_cache_memory_usage(); 489 490 return blob; 491 } 492 493 494 void* ExceptionBlob::operator new(size_t s, unsigned size) { 495 void* p = CodeCache::allocate(size); 496 if (!p) fatal("Initial size of CodeCache is too small"); 497 return p; 498 } 499 #endif // COMPILER2 500 501 502 //---------------------------------------------------------------------------------------------------- 503 // Implementation of SafepointBlob 504 505 SafepointBlob::SafepointBlob( 506 CodeBuffer* cb, 507 int size, 508 OopMapSet* oop_maps, 509 int frame_size 510 ) 511 : SingletonBlob("SafepointBlob", cb, sizeof(SafepointBlob), size, frame_size, oop_maps) 512 {} 513 514 515 SafepointBlob* SafepointBlob::create( 516 CodeBuffer* cb, 517 OopMapSet* oop_maps, 518 int frame_size) 519 { 520 SafepointBlob* blob = NULL; 521 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 522 { 523 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 524 unsigned int size = allocation_size(cb, sizeof(SafepointBlob)); 525 blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size); 526 } 527 528 // We do not need to hold the CodeCache lock during name formatting. 529 if (blob != NULL) { 530 char blob_id[256]; 531 jio_snprintf(blob_id, sizeof(blob_id), "SafepointBlob@" PTR_FORMAT, blob->instructions_begin()); 532 if (PrintStubCode) { 533 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 534 Disassembler::decode(blob->instructions_begin(), blob->instructions_end()); 535 } 536 VTune::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end()); 537 Forte::register_stub(blob_id, blob->instructions_begin(), blob->instructions_end()); 538 539 if (JvmtiExport::should_post_dynamic_code_generated()) { 540 JvmtiExport::post_dynamic_code_generated("SafepointBlob", 541 blob->instructions_begin(), 542 blob->instructions_end()); 543 } 544 } 545 546 // Track memory usage statistic after releasing CodeCache_lock 547 MemoryService::track_code_cache_memory_usage(); 548 549 return blob; 550 } 551 552 553 void* SafepointBlob::operator new(size_t s, unsigned size) { 554 void* p = CodeCache::allocate(size); 555 if (!p) fatal("Initial size of CodeCache is too small"); 556 return p; 557 } 558 559 560 //---------------------------------------------------------------------------------------------------- 561 // Verification and printing 562 563 void CodeBlob::verify() { 564 ShouldNotReachHere(); 565 } 566 567 #ifndef PRODUCT 568 569 void CodeBlob::print() const { 570 tty->print_cr("[CodeBlob (" INTPTR_FORMAT ")]", this); 571 tty->print_cr("Framesize: %d", _frame_size); 572 } 573 574 575 void CodeBlob::print_value_on(outputStream* st) const { 576 st->print_cr("[CodeBlob]"); 577 } 578 579 #endif 580 581 void BufferBlob::verify() { 582 // unimplemented 583 } 584 585 #ifndef PRODUCT 586 587 void BufferBlob::print() const { 588 CodeBlob::print(); 589 print_value_on(tty); 590 } 591 592 593 void BufferBlob::print_value_on(outputStream* st) const { 594 st->print_cr("BufferBlob (" INTPTR_FORMAT ") used for %s", this, name()); 595 } 596 597 598 #endif 599 600 void RuntimeStub::verify() { 601 // unimplemented 602 } 603 604 #ifndef PRODUCT 605 606 void RuntimeStub::print() const { 607 CodeBlob::print(); 608 tty->print("Runtime Stub (" INTPTR_FORMAT "): ", this); 609 tty->print_cr(name()); 610 Disassembler::decode((CodeBlob*)this); 611 } 612 613 614 void RuntimeStub::print_value_on(outputStream* st) const { 615 st->print("RuntimeStub (" INTPTR_FORMAT "): ", this); st->print(name()); 616 } 617 618 #endif 619 620 void SingletonBlob::verify() { 621 // unimplemented 622 } 623 624 #ifndef PRODUCT 625 626 void SingletonBlob::print() const { 627 CodeBlob::print(); 628 tty->print_cr(name()); 629 Disassembler::decode((CodeBlob*)this); 630 } 631 632 633 void SingletonBlob::print_value_on(outputStream* st) const { 634 st->print_cr(name()); 635 } 636 637 void DeoptimizationBlob::print_value_on(outputStream* st) const { 638 st->print_cr("Deoptimization (frame not available)"); 639 } 640 641 #endif // PRODUCT