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 "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_content_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 _content_offset = align_code_offset(header_size + _relocation_size); 68 _code_offset = _content_offset; 69 _data_offset = size; 70 _frame_size = 0; 71 set_oop_maps(NULL); 72 } 73 74 75 // Creates a CodeBlob from a CodeBuffer. Sets up the size of the different regions, 76 // and copy code and relocation info. 77 CodeBlob::CodeBlob( 78 const char* name, 79 CodeBuffer* cb, 80 int header_size, 81 int size, 82 int frame_complete, 83 int frame_size, 84 OopMapSet* oop_maps 85 ) { 86 assert(size == round_to(size, oopSize), "unaligned size"); 87 assert(header_size == round_to(header_size, oopSize), "unaligned size"); 88 89 _name = name; 90 _size = size; 91 _frame_complete_offset = frame_complete; 92 _header_size = header_size; 93 _relocation_size = round_to(cb->total_relocation_size(), oopSize); 94 _content_offset = align_code_offset(header_size + _relocation_size); 95 _code_offset = _content_offset + cb->total_offset_of(cb->insts()); 96 _data_offset = _content_offset + round_to(cb->total_content_size(), oopSize); 97 assert(_data_offset <= size, "codeBlob is too small"); 98 99 cb->copy_code_and_locs_to(this); 100 set_oop_maps(oop_maps); 101 _frame_size = frame_size; 102 #ifdef COMPILER1 103 // probably wrong for tiered 104 assert(_frame_size >= -1, "must use frame size or -1 for runtime stubs"); 105 #endif // COMPILER1 106 } 107 108 109 void CodeBlob::set_oop_maps(OopMapSet* p) { 110 // Danger Will Robinson! This method allocates a big 111 // chunk of memory, its your job to free it. 112 if (p != NULL) { 113 // We need to allocate a chunk big enough to hold the OopMapSet and all of its OopMaps 114 _oop_maps = (OopMapSet* )NEW_C_HEAP_ARRAY(unsigned char, p->heap_size()); 115 p->copy_to((address)_oop_maps); 116 } else { 117 _oop_maps = NULL; 118 } 119 } 120 121 122 void CodeBlob::flush() { 123 if (_oop_maps) { 124 FREE_C_HEAP_ARRAY(unsigned char, _oop_maps); 125 _oop_maps = NULL; 126 } 127 _comments.free(); 128 } 129 130 131 OopMap* CodeBlob::oop_map_for_return_address(address return_address) { 132 assert(oop_maps() != NULL, "nope"); 133 return oop_maps()->find_map_at_offset((intptr_t) return_address - (intptr_t) code_begin()); 134 } 135 136 137 //---------------------------------------------------------------------------------------------------- 138 // Implementation of BufferBlob 139 140 141 BufferBlob::BufferBlob(const char* name, int size) 142 : CodeBlob(name, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, /*locs_size:*/ 0) 143 {} 144 145 BufferBlob* BufferBlob::create(const char* name, int buffer_size) { 146 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 147 148 BufferBlob* blob = NULL; 149 unsigned int size = sizeof(BufferBlob); 150 // align the size to CodeEntryAlignment 151 size = align_code_offset(size); 152 size += round_to(buffer_size, oopSize); 153 assert(name != NULL, "must provide a name"); 154 { 155 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 156 blob = new (size) BufferBlob(name, size); 157 } 158 // Track memory usage statistic after releasing CodeCache_lock 159 MemoryService::track_code_cache_memory_usage(); 160 161 return blob; 162 } 163 164 165 BufferBlob::BufferBlob(const char* name, int size, CodeBuffer* cb) 166 : CodeBlob(name, cb, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, 0, NULL) 167 {} 168 169 BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) { 170 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 171 172 BufferBlob* blob = NULL; 173 unsigned int size = allocation_size(cb, sizeof(BufferBlob)); 174 assert(name != NULL, "must provide a name"); 175 { 176 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 177 blob = new (size) BufferBlob(name, size, cb); 178 } 179 // Track memory usage statistic after releasing CodeCache_lock 180 MemoryService::track_code_cache_memory_usage(); 181 182 return blob; 183 } 184 185 186 void* BufferBlob::operator new(size_t s, unsigned size) { 187 void* p = CodeCache::allocate(size); 188 return p; 189 } 190 191 192 void BufferBlob::free( BufferBlob *blob ) { 193 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 194 { 195 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 196 CodeCache::free((CodeBlob*)blob); 197 } 198 // Track memory usage statistic after releasing CodeCache_lock 199 MemoryService::track_code_cache_memory_usage(); 200 } 201 202 203 //---------------------------------------------------------------------------------------------------- 204 // Implementation of AdapterBlob 205 206 AdapterBlob::AdapterBlob(int size, CodeBuffer* cb) : 207 BufferBlob("I2C/C2I adapters", size, cb) { 208 CodeCache::commit(this); 209 } 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->code_begin(), stub->code_end()); 289 } 290 Forte::register_stub(stub_id, stub->code_begin(), stub->code_end()); 291 292 if (JvmtiExport::should_post_dynamic_code_generated()) { 293 JvmtiExport::post_dynamic_code_generated(stub_name, stub->code_begin(), stub->code_end()); 294 } 295 } 296 297 // Track memory usage statistic after releasing CodeCache_lock 298 MemoryService::track_code_cache_memory_usage(); 299 300 return stub; 301 } 302 303 304 void* RuntimeStub::operator new(size_t s, unsigned size) { 305 void* p = CodeCache::allocate(size); 306 if (!p) fatal("Initial size of CodeCache is too small"); 307 return p; 308 } 309 310 311 //---------------------------------------------------------------------------------------------------- 312 // Implementation of DeoptimizationBlob 313 314 DeoptimizationBlob::DeoptimizationBlob( 315 CodeBuffer* cb, 316 int size, 317 OopMapSet* oop_maps, 318 int unpack_offset, 319 int unpack_with_exception_offset, 320 int unpack_with_reexecution_offset, 321 int frame_size 322 ) 323 : SingletonBlob("DeoptimizationBlob", cb, sizeof(DeoptimizationBlob), size, frame_size, oop_maps) 324 { 325 _unpack_offset = unpack_offset; 326 _unpack_with_exception = unpack_with_exception_offset; 327 _unpack_with_reexecution = unpack_with_reexecution_offset; 328 #ifdef COMPILER1 329 _unpack_with_exception_in_tls = -1; 330 #endif 331 } 332 333 334 DeoptimizationBlob* DeoptimizationBlob::create( 335 CodeBuffer* cb, 336 OopMapSet* oop_maps, 337 int unpack_offset, 338 int unpack_with_exception_offset, 339 int unpack_with_reexecution_offset, 340 int frame_size) 341 { 342 DeoptimizationBlob* blob = NULL; 343 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 344 { 345 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 346 unsigned int size = allocation_size(cb, sizeof(DeoptimizationBlob)); 347 blob = new (size) DeoptimizationBlob(cb, 348 size, 349 oop_maps, 350 unpack_offset, 351 unpack_with_exception_offset, 352 unpack_with_reexecution_offset, 353 frame_size); 354 } 355 356 // Do not hold the CodeCache lock during name formatting. 357 if (blob != NULL) { 358 char blob_id[256]; 359 jio_snprintf(blob_id, sizeof(blob_id), "DeoptimizationBlob@" PTR_FORMAT, blob->code_begin()); 360 if (PrintStubCode) { 361 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 362 Disassembler::decode(blob->code_begin(), blob->code_end()); 363 } 364 Forte::register_stub(blob_id, blob->code_begin(), blob->code_end()); 365 366 if (JvmtiExport::should_post_dynamic_code_generated()) { 367 JvmtiExport::post_dynamic_code_generated("DeoptimizationBlob", blob->code_begin(), blob->code_end()); 368 } 369 } 370 371 // Track memory usage statistic after releasing CodeCache_lock 372 MemoryService::track_code_cache_memory_usage(); 373 374 return blob; 375 } 376 377 378 void* DeoptimizationBlob::operator new(size_t s, unsigned size) { 379 void* p = CodeCache::allocate(size); 380 if (!p) fatal("Initial size of CodeCache is too small"); 381 return p; 382 } 383 384 //---------------------------------------------------------------------------------------------------- 385 // Implementation of UncommonTrapBlob 386 387 #ifdef COMPILER2 388 UncommonTrapBlob::UncommonTrapBlob( 389 CodeBuffer* cb, 390 int size, 391 OopMapSet* oop_maps, 392 int frame_size 393 ) 394 : SingletonBlob("UncommonTrapBlob", cb, sizeof(UncommonTrapBlob), size, frame_size, oop_maps) 395 {} 396 397 398 UncommonTrapBlob* UncommonTrapBlob::create( 399 CodeBuffer* cb, 400 OopMapSet* oop_maps, 401 int frame_size) 402 { 403 UncommonTrapBlob* blob = NULL; 404 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 405 { 406 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 407 unsigned int size = allocation_size(cb, sizeof(UncommonTrapBlob)); 408 blob = new (size) UncommonTrapBlob(cb, size, oop_maps, frame_size); 409 } 410 411 // Do not hold the CodeCache lock during name formatting. 412 if (blob != NULL) { 413 char blob_id[256]; 414 jio_snprintf(blob_id, sizeof(blob_id), "UncommonTrapBlob@" PTR_FORMAT, blob->code_begin()); 415 if (PrintStubCode) { 416 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 417 Disassembler::decode(blob->code_begin(), blob->code_end()); 418 } 419 Forte::register_stub(blob_id, blob->code_begin(), blob->code_end()); 420 421 if (JvmtiExport::should_post_dynamic_code_generated()) { 422 JvmtiExport::post_dynamic_code_generated("UncommonTrapBlob", blob->code_begin(), blob->code_end()); 423 } 424 } 425 426 // Track memory usage statistic after releasing CodeCache_lock 427 MemoryService::track_code_cache_memory_usage(); 428 429 return blob; 430 } 431 432 433 void* UncommonTrapBlob::operator new(size_t s, unsigned size) { 434 void* p = CodeCache::allocate(size); 435 if (!p) fatal("Initial size of CodeCache is too small"); 436 return p; 437 } 438 #endif // COMPILER2 439 440 441 //---------------------------------------------------------------------------------------------------- 442 // Implementation of ExceptionBlob 443 444 #ifdef COMPILER2 445 ExceptionBlob::ExceptionBlob( 446 CodeBuffer* cb, 447 int size, 448 OopMapSet* oop_maps, 449 int frame_size 450 ) 451 : SingletonBlob("ExceptionBlob", cb, sizeof(ExceptionBlob), size, frame_size, oop_maps) 452 {} 453 454 455 ExceptionBlob* ExceptionBlob::create( 456 CodeBuffer* cb, 457 OopMapSet* oop_maps, 458 int frame_size) 459 { 460 ExceptionBlob* blob = NULL; 461 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 462 { 463 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 464 unsigned int size = allocation_size(cb, sizeof(ExceptionBlob)); 465 blob = new (size) ExceptionBlob(cb, size, oop_maps, frame_size); 466 } 467 468 // We do not need to hold the CodeCache lock during name formatting 469 if (blob != NULL) { 470 char blob_id[256]; 471 jio_snprintf(blob_id, sizeof(blob_id), "ExceptionBlob@" PTR_FORMAT, blob->code_begin()); 472 if (PrintStubCode) { 473 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 474 Disassembler::decode(blob->code_begin(), blob->code_end()); 475 } 476 Forte::register_stub(blob_id, blob->code_begin(), blob->code_end()); 477 478 if (JvmtiExport::should_post_dynamic_code_generated()) { 479 JvmtiExport::post_dynamic_code_generated("ExceptionBlob", blob->code_begin(), blob->code_end()); 480 } 481 } 482 483 // Track memory usage statistic after releasing CodeCache_lock 484 MemoryService::track_code_cache_memory_usage(); 485 486 return blob; 487 } 488 489 490 void* ExceptionBlob::operator new(size_t s, unsigned size) { 491 void* p = CodeCache::allocate(size); 492 if (!p) fatal("Initial size of CodeCache is too small"); 493 return p; 494 } 495 #endif // COMPILER2 496 497 498 //---------------------------------------------------------------------------------------------------- 499 // Implementation of SafepointBlob 500 501 SafepointBlob::SafepointBlob( 502 CodeBuffer* cb, 503 int size, 504 OopMapSet* oop_maps, 505 int frame_size 506 ) 507 : SingletonBlob("SafepointBlob", cb, sizeof(SafepointBlob), size, frame_size, oop_maps) 508 {} 509 510 511 SafepointBlob* SafepointBlob::create( 512 CodeBuffer* cb, 513 OopMapSet* oop_maps, 514 int frame_size) 515 { 516 SafepointBlob* blob = NULL; 517 ThreadInVMfromUnknown __tiv; // get to VM state in case we block on CodeCache_lock 518 { 519 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 520 unsigned int size = allocation_size(cb, sizeof(SafepointBlob)); 521 blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size); 522 } 523 524 // We do not need to hold the CodeCache lock during name formatting. 525 if (blob != NULL) { 526 char blob_id[256]; 527 jio_snprintf(blob_id, sizeof(blob_id), "SafepointBlob@" PTR_FORMAT, blob->code_begin()); 528 if (PrintStubCode) { 529 tty->print_cr("Decoding %s " INTPTR_FORMAT, blob_id, blob); 530 Disassembler::decode(blob->code_begin(), blob->code_end()); 531 } 532 Forte::register_stub(blob_id, blob->code_begin(), blob->code_end()); 533 534 if (JvmtiExport::should_post_dynamic_code_generated()) { 535 JvmtiExport::post_dynamic_code_generated("SafepointBlob", blob->code_begin(), blob->code_end()); 536 } 537 } 538 539 // Track memory usage statistic after releasing CodeCache_lock 540 MemoryService::track_code_cache_memory_usage(); 541 542 return blob; 543 } 544 545 546 void* SafepointBlob::operator new(size_t s, unsigned size) { 547 void* p = CodeCache::allocate(size); 548 if (!p) fatal("Initial size of CodeCache is too small"); 549 return p; 550 } 551 552 553 //---------------------------------------------------------------------------------------------------- 554 // Verification and printing 555 556 void CodeBlob::verify() { 557 ShouldNotReachHere(); 558 } 559 560 void CodeBlob::print_on(outputStream* st) const { 561 st->print_cr("[CodeBlob (" INTPTR_FORMAT ")]", this); 562 st->print_cr("Framesize: %d", _frame_size); 563 } 564 565 void CodeBlob::print_value_on(outputStream* st) const { 566 st->print_cr("[CodeBlob]"); 567 } 568 569 void BufferBlob::verify() { 570 // unimplemented 571 } 572 573 void BufferBlob::print_on(outputStream* st) const { 574 CodeBlob::print_on(st); 575 print_value_on(st); 576 } 577 578 void BufferBlob::print_value_on(outputStream* st) const { 579 st->print_cr("BufferBlob (" INTPTR_FORMAT ") used for %s", this, name()); 580 } 581 582 void RuntimeStub::verify() { 583 // unimplemented 584 } 585 586 void RuntimeStub::print_on(outputStream* st) const { 587 CodeBlob::print_on(st); 588 st->print("Runtime Stub (" INTPTR_FORMAT "): ", this); 589 st->print_cr(name()); 590 Disassembler::decode((CodeBlob*)this, st); 591 } 592 593 void RuntimeStub::print_value_on(outputStream* st) const { 594 st->print("RuntimeStub (" INTPTR_FORMAT "): ", this); st->print(name()); 595 } 596 597 void SingletonBlob::verify() { 598 // unimplemented 599 } 600 601 void SingletonBlob::print_on(outputStream* st) const { 602 CodeBlob::print_on(st); 603 st->print_cr(name()); 604 Disassembler::decode((CodeBlob*)this, st); 605 } 606 607 void SingletonBlob::print_value_on(outputStream* st) const { 608 st->print_cr(name()); 609 } 610 611 void DeoptimizationBlob::print_value_on(outputStream* st) const { 612 st->print_cr("Deoptimization (frame not available)"); 613 }