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 CodeBlob::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     _oop_maps = ImmutableOopMapSet::build_from(p);
 133   } else {
 134     _oop_maps = NULL;
 135   }
 136 }
 137 
 138 
 139 void CodeBlob::trace_new_stub(CodeBlob* stub, const char* name1, const char* name2) {
 140   // Do not hold the CodeCache lock during name formatting.
 141   assert(!CodeCache_lock->owned_by_self(), "release CodeCache before registering the stub");
 142 
 143   if (stub != NULL) {
 144     char stub_id[256];
 145     assert(strlen(name1) + strlen(name2) < sizeof(stub_id), "");
 146     jio_snprintf(stub_id, sizeof(stub_id), "%s%s", name1, name2);
 147     if (PrintStubCode) {
 148       ttyLocker ttyl;
 149       tty->print_cr("Decoding %s " INTPTR_FORMAT, stub_id, (intptr_t) stub);
 150       Disassembler::decode(stub->code_begin(), stub->code_end());
 151       tty->cr();
 152     }
 153     Forte::register_stub(stub_id, stub->code_begin(), stub->code_end());
 154 
 155     if (JvmtiExport::should_post_dynamic_code_generated()) {
 156       const char* stub_name = name2;
 157       if (name2[0] == '\0')  stub_name = name1;
 158       JvmtiExport::post_dynamic_code_generated(stub_name, stub->code_begin(), stub->code_end());
 159     }
 160   }
 161 
 162   // Track memory usage statistic after releasing CodeCache_lock
 163   MemoryService::track_code_cache_memory_usage();
 164 }
 165 
 166 
 167 void CodeBlob::flush() {
 168   if (_oop_maps) {
 169     FREE_C_HEAP_ARRAY(unsigned char, _oop_maps);
 170     _oop_maps = NULL;
 171   }
 172   _strings.free();
 173 }
 174 
 175 
 176 const ImmutableOopMap* CodeBlob::oop_map_for_return_address(address return_address) {
 177   assert(oop_maps() != NULL, "nope");
 178   return oop_maps()->find_map_at_offset((intptr_t) return_address - (intptr_t) code_begin());
 179 }
 180 
 181 
 182 //----------------------------------------------------------------------------------------------------
 183 // Implementation of BufferBlob
 184 
 185 
 186 BufferBlob::BufferBlob(const char* name, int size)
 187 : CodeBlob(name, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, /*locs_size:*/ 0)
 188 {}
 189 
 190 BufferBlob* BufferBlob::create(const char* name, int buffer_size) {
 191   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
 192 
 193   BufferBlob* blob = NULL;
 194   unsigned int size = sizeof(BufferBlob);
 195   // align the size to CodeEntryAlignment
 196   size = align_code_offset(size);
 197   size += round_to(buffer_size, oopSize);
 198   assert(name != NULL, "must provide a name");
 199   {
 200     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 201     blob = new (size) BufferBlob(name, size);
 202   }
 203   // Track memory usage statistic after releasing CodeCache_lock
 204   MemoryService::track_code_cache_memory_usage();
 205 
 206   return blob;
 207 }
 208 
 209 
 210 BufferBlob::BufferBlob(const char* name, int size, CodeBuffer* cb)
 211   : CodeBlob(name, cb, sizeof(BufferBlob), size, CodeOffsets::frame_never_safe, 0, NULL)
 212 {}
 213 
 214 BufferBlob* BufferBlob::create(const char* name, CodeBuffer* cb) {
 215   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
 216 
 217   BufferBlob* blob = NULL;
 218   unsigned int size = allocation_size(cb, sizeof(BufferBlob));
 219   assert(name != NULL, "must provide a name");
 220   {
 221     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 222     blob = new (size) BufferBlob(name, size, cb);
 223   }
 224   // Track memory usage statistic after releasing CodeCache_lock
 225   MemoryService::track_code_cache_memory_usage();
 226 
 227   return blob;
 228 }
 229 
 230 void* BufferBlob::operator new(size_t s, unsigned size) throw() {
 231   return CodeCache::allocate(size, CodeBlobType::NonNMethod);
 232 }
 233 
 234 void BufferBlob::free(BufferBlob *blob) {
 235   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
 236   blob->flush();
 237   {
 238     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 239     CodeCache::free((CodeBlob*)blob);
 240   }
 241   // Track memory usage statistic after releasing CodeCache_lock
 242   MemoryService::track_code_cache_memory_usage();
 243 }
 244 
 245 
 246 //----------------------------------------------------------------------------------------------------
 247 // Implementation of AdapterBlob
 248 
 249 AdapterBlob::AdapterBlob(int size, CodeBuffer* cb) :
 250   BufferBlob("I2C/C2I adapters", size, cb) {
 251   CodeCache::commit(this);
 252 }
 253 
 254 AdapterBlob* AdapterBlob::create(CodeBuffer* cb) {
 255   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
 256 
 257   AdapterBlob* blob = NULL;
 258   unsigned int size = allocation_size(cb, sizeof(AdapterBlob));
 259   {
 260     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 261     blob = new (size) AdapterBlob(size, cb);
 262   }
 263   // Track memory usage statistic after releasing CodeCache_lock
 264   MemoryService::track_code_cache_memory_usage();
 265 
 266   return blob;
 267 }
 268 
 269 
 270 //----------------------------------------------------------------------------------------------------
 271 // Implementation of MethodHandlesAdapterBlob
 272 
 273 MethodHandlesAdapterBlob* MethodHandlesAdapterBlob::create(int buffer_size) {
 274   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
 275 
 276   MethodHandlesAdapterBlob* blob = NULL;
 277   unsigned int size = sizeof(MethodHandlesAdapterBlob);
 278   // align the size to CodeEntryAlignment
 279   size = align_code_offset(size);
 280   size += round_to(buffer_size, oopSize);
 281   {
 282     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 283     blob = new (size) MethodHandlesAdapterBlob(size);
 284   }
 285   // Track memory usage statistic after releasing CodeCache_lock
 286   MemoryService::track_code_cache_memory_usage();
 287 
 288   return blob;
 289 }
 290 
 291 //----------------------------------------------------------------------------------------------------
 292 // Implementation of RuntimeStub
 293 
 294 RuntimeStub::RuntimeStub(
 295   const char* name,
 296   CodeBuffer* cb,
 297   int         size,
 298   int         frame_complete,
 299   int         frame_size,
 300   OopMapSet*  oop_maps,
 301   bool        caller_must_gc_arguments
 302 )
 303 : CodeBlob(name, cb, sizeof(RuntimeStub), size, frame_complete, frame_size, oop_maps)
 304 {
 305   _caller_must_gc_arguments = caller_must_gc_arguments;
 306 }
 307 
 308 
 309 RuntimeStub* RuntimeStub::new_runtime_stub(const char* stub_name,
 310                                            CodeBuffer* cb,
 311                                            int frame_complete,
 312                                            int frame_size,
 313                                            OopMapSet* oop_maps,
 314                                            bool caller_must_gc_arguments)
 315 {
 316   RuntimeStub* stub = NULL;
 317   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
 318   {
 319     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 320     unsigned int size = allocation_size(cb, sizeof(RuntimeStub));
 321     stub = new (size) RuntimeStub(stub_name, cb, size, frame_complete, frame_size, oop_maps, caller_must_gc_arguments);
 322   }
 323 
 324   trace_new_stub(stub, "RuntimeStub - ", stub_name);
 325 
 326   return stub;
 327 }
 328 
 329 
 330 void* RuntimeStub::operator new(size_t s, unsigned size) throw() {
 331   void* p = CodeCache::allocate(size, CodeBlobType::NonNMethod);
 332   if (!p) fatal("Initial size of CodeCache is too small");
 333   return p;
 334 }
 335 
 336 // operator new shared by all singletons:
 337 void* SingletonBlob::operator new(size_t s, unsigned size) throw() {
 338   void* p = CodeCache::allocate(size, CodeBlobType::NonNMethod);
 339   if (!p) fatal("Initial size of CodeCache is too small");
 340   return p;
 341 }
 342 
 343 
 344 //----------------------------------------------------------------------------------------------------
 345 // Implementation of DeoptimizationBlob
 346 
 347 DeoptimizationBlob::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 
 367 DeoptimizationBlob* 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   trace_new_stub(blob, "DeoptimizationBlob");
 390 
 391   return blob;
 392 }
 393 
 394 
 395 //----------------------------------------------------------------------------------------------------
 396 // Implementation of UncommonTrapBlob
 397 
 398 #ifdef COMPILER2
 399 UncommonTrapBlob::UncommonTrapBlob(
 400   CodeBuffer* cb,
 401   int         size,
 402   OopMapSet*  oop_maps,
 403   int         frame_size
 404 )
 405 : SingletonBlob("UncommonTrapBlob", cb, sizeof(UncommonTrapBlob), size, frame_size, oop_maps)
 406 {}
 407 
 408 
 409 UncommonTrapBlob* UncommonTrapBlob::create(
 410   CodeBuffer* cb,
 411   OopMapSet*  oop_maps,
 412   int        frame_size)
 413 {
 414   UncommonTrapBlob* blob = NULL;
 415   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
 416   {
 417     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 418     unsigned int size = allocation_size(cb, sizeof(UncommonTrapBlob));
 419     blob = new (size) UncommonTrapBlob(cb, size, oop_maps, frame_size);
 420   }
 421 
 422   trace_new_stub(blob, "UncommonTrapBlob");
 423 
 424   return blob;
 425 }
 426 
 427 
 428 #endif // COMPILER2
 429 
 430 
 431 //----------------------------------------------------------------------------------------------------
 432 // Implementation of ExceptionBlob
 433 
 434 #ifdef COMPILER2
 435 ExceptionBlob::ExceptionBlob(
 436   CodeBuffer* cb,
 437   int         size,
 438   OopMapSet*  oop_maps,
 439   int         frame_size
 440 )
 441 : SingletonBlob("ExceptionBlob", cb, sizeof(ExceptionBlob), size, frame_size, oop_maps)
 442 {}
 443 
 444 
 445 ExceptionBlob* ExceptionBlob::create(
 446   CodeBuffer* cb,
 447   OopMapSet*  oop_maps,
 448   int         frame_size)
 449 {
 450   ExceptionBlob* blob = NULL;
 451   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
 452   {
 453     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 454     unsigned int size = allocation_size(cb, sizeof(ExceptionBlob));
 455     blob = new (size) ExceptionBlob(cb, size, oop_maps, frame_size);
 456   }
 457 
 458   trace_new_stub(blob, "ExceptionBlob");
 459 
 460   return blob;
 461 }
 462 
 463 
 464 #endif // COMPILER2
 465 
 466 
 467 //----------------------------------------------------------------------------------------------------
 468 // Implementation of SafepointBlob
 469 
 470 SafepointBlob::SafepointBlob(
 471   CodeBuffer* cb,
 472   int         size,
 473   OopMapSet*  oop_maps,
 474   int         frame_size
 475 )
 476 : SingletonBlob("SafepointBlob", cb, sizeof(SafepointBlob), size, frame_size, oop_maps)
 477 {}
 478 
 479 
 480 SafepointBlob* SafepointBlob::create(
 481   CodeBuffer* cb,
 482   OopMapSet*  oop_maps,
 483   int         frame_size)
 484 {
 485   SafepointBlob* blob = NULL;
 486   ThreadInVMfromUnknown __tiv;  // get to VM state in case we block on CodeCache_lock
 487   {
 488     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 489     unsigned int size = allocation_size(cb, sizeof(SafepointBlob));
 490     blob = new (size) SafepointBlob(cb, size, oop_maps, frame_size);
 491   }
 492 
 493   trace_new_stub(blob, "SafepointBlob");
 494 
 495   return blob;
 496 }
 497 
 498 
 499 //----------------------------------------------------------------------------------------------------
 500 // Verification and printing
 501 
 502 void CodeBlob::verify() {
 503   ShouldNotReachHere();
 504 }
 505 
 506 void CodeBlob::print_on(outputStream* st) const {
 507   st->print_cr("[CodeBlob (" INTPTR_FORMAT ")]", p2i(this));
 508   st->print_cr("Framesize: %d", _frame_size);
 509 }
 510 
 511 void CodeBlob::print_value_on(outputStream* st) const {
 512   st->print_cr("[CodeBlob]");
 513 }
 514 
 515 void BufferBlob::verify() {
 516   // unimplemented
 517 }
 518 
 519 void BufferBlob::print_on(outputStream* st) const {
 520   CodeBlob::print_on(st);
 521   print_value_on(st);
 522 }
 523 
 524 void BufferBlob::print_value_on(outputStream* st) const {
 525   st->print_cr("BufferBlob (" INTPTR_FORMAT  ") used for %s", p2i(this), name());
 526 }
 527 
 528 void RuntimeStub::verify() {
 529   // unimplemented
 530 }
 531 
 532 void RuntimeStub::print_on(outputStream* st) const {
 533   ttyLocker ttyl;
 534   CodeBlob::print_on(st);
 535   st->print("Runtime Stub (" INTPTR_FORMAT "): ", p2i(this));
 536   st->print_cr("%s", name());
 537   Disassembler::decode((CodeBlob*)this, st);
 538 }
 539 
 540 void RuntimeStub::print_value_on(outputStream* st) const {
 541   st->print("RuntimeStub (" INTPTR_FORMAT "): ", p2i(this)); st->print("%s", name());
 542 }
 543 
 544 void SingletonBlob::verify() {
 545   // unimplemented
 546 }
 547 
 548 void SingletonBlob::print_on(outputStream* st) const {
 549   ttyLocker ttyl;
 550   CodeBlob::print_on(st);
 551   st->print_cr("%s", name());
 552   Disassembler::decode((CodeBlob*)this, st);
 553 }
 554 
 555 void SingletonBlob::print_value_on(outputStream* st) const {
 556   st->print_cr("%s", name());
 557 }
 558 
 559 void DeoptimizationBlob::print_value_on(outputStream* st) const {
 560   st->print_cr("Deoptimization (frame not available)");
 561 }