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