codeBuffer.cpp revision 3602:da91efe96a93
11245Shannesw/* 21245Shannesw * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 31245Shannesw * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41245Shannesw * 51245Shannesw * This code is free software; you can redistribute it and/or modify it 61245Shannesw * under the terms of the GNU General Public License version 2 only, as 71245Shannesw * published by the Free Software Foundation. 81245Shannesw * 91245Shannesw * This code is distributed in the hope that it will be useful, but WITHOUT 101245Shannesw * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 111245Shannesw * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 121245Shannesw * version 2 for more details (a copy is included in the LICENSE file that 131245Shannesw * accompanied this code). 141245Shannesw * 151245Shannesw * You should have received a copy of the GNU General Public License version 161245Shannesw * 2 along with this work; if not, write to the Free Software Foundation, 171245Shannesw * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181245Shannesw * 191245Shannesw * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 201245Shannesw * or visit www.oracle.com if you need additional information or have any 211245Shannesw * questions. 221245Shannesw * 231245Shannesw */ 241245Shannesw 251245Shannesw#include "precompiled.hpp" 261245Shannesw#include "asm/codeBuffer.hpp" 271245Shannesw#include "compiler/disassembler.hpp" 281245Shannesw#include "memory/gcLocker.hpp" 291245Shannesw#include "oops/methodData.hpp" 301643Salanb#include "oops/oop.inline.hpp" 311245Shannesw#include "utilities/copy.hpp" 321245Shannesw#include "utilities/xmlstream.hpp" 331245Shannesw 341245Shannesw// The structure of a CodeSection: 351245Shannesw// 361245Shannesw// _start -> +----------------+ 371245Shannesw// | machine code...| 381245Shannesw// _end -> |----------------| 391245Shannesw// | | 401643Salanb// | (empty) | 411643Salanb// | | 421245Shannesw// | | 431643Salanb// +----------------+ 441643Salanb// _limit -> | | 451643Salanb// 461643Salanb// _locs_start -> +----------------+ 471643Salanb// |reloc records...| 481643Salanb// |----------------| 491643Salanb// _locs_end -> | | 501643Salanb// | | 511643Salanb// | (empty) | 521643Salanb// | | 531643Salanb// | | 541643Salanb// +----------------+ 551643Salanb// _locs_limit -> | | 561643Salanb// The _end (resp. _limit) pointer refers to the first 571643Salanb// unused (resp. unallocated) byte. 581643Salanb 591643Salanb// The structure of the CodeBuffer while code is being accumulated: 601643Salanb// 611245Shannesw// _total_start -> \ 621245Shannesw// _insts._start -> +----------------+ 631245Shannesw// | | 641245Shannesw// | Code | 651245Shannesw// | | 661245Shannesw// _stubs._start -> |----------------| 671245Shannesw// | | 681245Shannesw// | Stubs | (also handlers for deopt/exception) 691245Shannesw// | | 701245Shannesw// _consts._start -> |----------------| 711245Shannesw// | | 721643Salanb// | Constants | 731643Salanb// | | 741245Shannesw// +----------------+ 751245Shannesw// + _total_size -> | | 761643Salanb// 771245Shannesw// When the code and relocations are copied to the code cache, 781245Shannesw// the empty parts of each section are removed, and everything 791245Shannesw// is copied into contiguous locations. 801245Shannesw 811245Shanneswtypedef CodeBuffer::csize_t csize_t; // file-local definition 821245Shannesw 831245Shannesw// External buffer, in a predefined CodeBlob. 841245Shannesw// Important: The code_start must be taken exactly, and not realigned. 851245ShanneswCodeBuffer::CodeBuffer(CodeBlob* blob) { 861571Shannesw initialize_misc("static buffer"); 871643Salanb initialize(blob->content_begin(), blob->content_size()); 881245Shannesw verify_section_allocation(); 891245Shannesw} 901245Shannesw 911245Shanneswvoid CodeBuffer::initialize(csize_t code_size, csize_t locs_size) { 921245Shannesw // Compute maximal alignment. 931245Shannesw int align = _insts.alignment(); 941245Shannesw // Always allow for empty slop around each section. 951245Shannesw int slop = (int) CodeSection::end_slop(); 961245Shannesw 971245Shannesw assert(blob() == NULL, "only once"); 981245Shannesw set_blob(BufferBlob::create(_name, code_size + (align+slop) * (SECT_LIMIT+1))); 991245Shannesw if (blob() == NULL) { 1001245Shannesw // The assembler constructor will throw a fatal on an empty CodeBuffer. 1011245Shannesw return; // caller must test this 1021245Shannesw } 1031245Shannesw 1041245Shannesw // Set up various pointers into the blob. 1051245Shannesw initialize(_total_start, _total_size); 1061245Shannesw 1071245Shannesw assert((uintptr_t)insts_begin() % CodeEntryAlignment == 0, "instruction start not code entry aligned"); 1081245Shannesw 1091245Shannesw pd_initialize(); 1101245Shannesw 1111245Shannesw if (locs_size != 0) { 1121245Shannesw _insts.initialize_locs(locs_size / sizeof(relocInfo)); 1131245Shannesw } 1141245Shannesw 115 verify_section_allocation(); 116} 117 118 119CodeBuffer::~CodeBuffer() { 120 verify_section_allocation(); 121 122 // If we allocate our code buffer from the CodeCache 123 // via a BufferBlob, and it's not permanent, then 124 // free the BufferBlob. 125 // The rest of the memory will be freed when the ResourceObj 126 // is released. 127 for (CodeBuffer* cb = this; cb != NULL; cb = cb->before_expand()) { 128 // Previous incarnations of this buffer are held live, so that internal 129 // addresses constructed before expansions will not be confused. 130 cb->free_blob(); 131 } 132 133 // free any overflow storage 134 delete _overflow_arena; 135 136#ifdef ASSERT 137 // Save allocation type to execute assert in ~ResourceObj() 138 // which is called after this destructor. 139 assert(_default_oop_recorder.allocated_on_stack(), "should be embedded object"); 140 ResourceObj::allocation_type at = _default_oop_recorder.get_allocation_type(); 141 Copy::fill_to_bytes(this, sizeof(*this), badResourceValue); 142 ResourceObj::set_allocation_type((address)(&_default_oop_recorder), at); 143#endif 144} 145 146void CodeBuffer::initialize_oop_recorder(OopRecorder* r) { 147 assert(_oop_recorder == &_default_oop_recorder && _default_oop_recorder.is_unused(), "do this once"); 148 DEBUG_ONLY(_default_oop_recorder.freeze()); // force unused OR to be frozen 149 _oop_recorder = r; 150} 151 152void CodeBuffer::initialize_section_size(CodeSection* cs, csize_t size) { 153 assert(cs != &_insts, "insts is the memory provider, not the consumer"); 154 csize_t slop = CodeSection::end_slop(); // margin between sections 155 int align = cs->alignment(); 156 assert(is_power_of_2(align), "sanity"); 157 address start = _insts._start; 158 address limit = _insts._limit; 159 address middle = limit - size; 160 middle -= (intptr_t)middle & (align-1); // align the division point downward 161 guarantee(middle - slop > start, "need enough space to divide up"); 162 _insts._limit = middle - slop; // subtract desired space, plus slop 163 cs->initialize(middle, limit - middle); 164 assert(cs->start() == middle, "sanity"); 165 assert(cs->limit() == limit, "sanity"); 166 // give it some relocations to start with, if the main section has them 167 if (_insts.has_locs()) cs->initialize_locs(1); 168} 169 170void CodeBuffer::freeze_section(CodeSection* cs) { 171 CodeSection* next_cs = (cs == consts())? NULL: code_section(cs->index()+1); 172 csize_t frozen_size = cs->size(); 173 if (next_cs != NULL) { 174 frozen_size = next_cs->align_at_start(frozen_size); 175 } 176 address old_limit = cs->limit(); 177 address new_limit = cs->start() + frozen_size; 178 relocInfo* old_locs_limit = cs->locs_limit(); 179 relocInfo* new_locs_limit = cs->locs_end(); 180 // Patch the limits. 181 cs->_limit = new_limit; 182 cs->_locs_limit = new_locs_limit; 183 cs->_frozen = true; 184 if (!next_cs->is_allocated() && !next_cs->is_frozen()) { 185 // Give remaining buffer space to the following section. 186 next_cs->initialize(new_limit, old_limit - new_limit); 187 next_cs->initialize_shared_locs(new_locs_limit, 188 old_locs_limit - new_locs_limit); 189 } 190} 191 192void CodeBuffer::set_blob(BufferBlob* blob) { 193 _blob = blob; 194 if (blob != NULL) { 195 address start = blob->content_begin(); 196 address end = blob->content_end(); 197 // Round up the starting address. 198 int align = _insts.alignment(); 199 start += (-(intptr_t)start) & (align-1); 200 _total_start = start; 201 _total_size = end - start; 202 } else { 203#ifdef ASSERT 204 // Clean out dangling pointers. 205 _total_start = badAddress; 206 _consts._start = _consts._end = badAddress; 207 _insts._start = _insts._end = badAddress; 208 _stubs._start = _stubs._end = badAddress; 209#endif //ASSERT 210 } 211} 212 213void CodeBuffer::free_blob() { 214 if (_blob != NULL) { 215 BufferBlob::free(_blob); 216 set_blob(NULL); 217 } 218} 219 220const char* CodeBuffer::code_section_name(int n) { 221#ifdef PRODUCT 222 return NULL; 223#else //PRODUCT 224 switch (n) { 225 case SECT_CONSTS: return "consts"; 226 case SECT_INSTS: return "insts"; 227 case SECT_STUBS: return "stubs"; 228 default: return NULL; 229 } 230#endif //PRODUCT 231} 232 233int CodeBuffer::section_index_of(address addr) const { 234 for (int n = 0; n < (int)SECT_LIMIT; n++) { 235 const CodeSection* cs = code_section(n); 236 if (cs->allocates(addr)) return n; 237 } 238 return SECT_NONE; 239} 240 241int CodeBuffer::locator(address addr) const { 242 for (int n = 0; n < (int)SECT_LIMIT; n++) { 243 const CodeSection* cs = code_section(n); 244 if (cs->allocates(addr)) { 245 return locator(addr - cs->start(), n); 246 } 247 } 248 return -1; 249} 250 251address CodeBuffer::locator_address(int locator) const { 252 if (locator < 0) return NULL; 253 address start = code_section(locator_sect(locator))->start(); 254 return start + locator_pos(locator); 255} 256 257address CodeBuffer::decode_begin() { 258 address begin = _insts.start(); 259 if (_decode_begin != NULL && _decode_begin > begin) 260 begin = _decode_begin; 261 return begin; 262} 263 264 265GrowableArray<int>* CodeBuffer::create_patch_overflow() { 266 if (_overflow_arena == NULL) { 267 _overflow_arena = new (mtCode) Arena(); 268 } 269 return new (_overflow_arena) GrowableArray<int>(_overflow_arena, 8, 0, 0); 270} 271 272 273// Helper function for managing labels and their target addresses. 274// Returns a sensible address, and if it is not the label's final 275// address, notes the dependency (at 'branch_pc') on the label. 276address CodeSection::target(Label& L, address branch_pc) { 277 if (L.is_bound()) { 278 int loc = L.loc(); 279 if (index() == CodeBuffer::locator_sect(loc)) { 280 return start() + CodeBuffer::locator_pos(loc); 281 } else { 282 return outer()->locator_address(loc); 283 } 284 } else { 285 assert(allocates2(branch_pc), "sanity"); 286 address base = start(); 287 int patch_loc = CodeBuffer::locator(branch_pc - base, index()); 288 L.add_patch_at(outer(), patch_loc); 289 290 // Need to return a pc, doesn't matter what it is since it will be 291 // replaced during resolution later. 292 // Don't return NULL or badAddress, since branches shouldn't overflow. 293 // Don't return base either because that could overflow displacements 294 // for shorter branches. It will get checked when bound. 295 return branch_pc; 296 } 297} 298 299void CodeSection::relocate(address at, RelocationHolder const& spec, int format) { 300 Relocation* reloc = spec.reloc(); 301 relocInfo::relocType rtype = (relocInfo::relocType) reloc->type(); 302 if (rtype == relocInfo::none) return; 303 304 // The assertion below has been adjusted, to also work for 305 // relocation for fixup. Sometimes we want to put relocation 306 // information for the next instruction, since it will be patched 307 // with a call. 308 assert(start() <= at && at <= end()+1, 309 "cannot relocate data outside code boundaries"); 310 311 if (!has_locs()) { 312 // no space for relocation information provided => code cannot be 313 // relocated. Make sure that relocate is only called with rtypes 314 // that can be ignored for this kind of code. 315 assert(rtype == relocInfo::none || 316 rtype == relocInfo::runtime_call_type || 317 rtype == relocInfo::internal_word_type|| 318 rtype == relocInfo::section_word_type || 319 rtype == relocInfo::external_word_type, 320 "code needs relocation information"); 321 // leave behind an indication that we attempted a relocation 322 DEBUG_ONLY(_locs_start = _locs_limit = (relocInfo*)badAddress); 323 return; 324 } 325 326 // Advance the point, noting the offset we'll have to record. 327 csize_t offset = at - locs_point(); 328 set_locs_point(at); 329 330 // Test for a couple of overflow conditions; maybe expand the buffer. 331 relocInfo* end = locs_end(); 332 relocInfo* req = end + relocInfo::length_limit; 333 // Check for (potential) overflow 334 if (req >= locs_limit() || offset >= relocInfo::offset_limit()) { 335 req += (uint)offset / (uint)relocInfo::offset_limit(); 336 if (req >= locs_limit()) { 337 // Allocate or reallocate. 338 expand_locs(locs_count() + (req - end)); 339 // reload pointer 340 end = locs_end(); 341 } 342 } 343 344 // If the offset is giant, emit filler relocs, of type 'none', but 345 // each carrying the largest possible offset, to advance the locs_point. 346 while (offset >= relocInfo::offset_limit()) { 347 assert(end < locs_limit(), "adjust previous paragraph of code"); 348 *end++ = filler_relocInfo(); 349 offset -= filler_relocInfo().addr_offset(); 350 } 351 352 // If it's a simple reloc with no data, we'll just write (rtype | offset). 353 (*end) = relocInfo(rtype, offset, format); 354 355 // If it has data, insert the prefix, as (data_prefix_tag | data1), data2. 356 end->initialize(this, reloc); 357} 358 359void CodeSection::initialize_locs(int locs_capacity) { 360 assert(_locs_start == NULL, "only one locs init step, please"); 361 // Apply a priori lower limits to relocation size: 362 csize_t min_locs = MAX2(size() / 16, (csize_t)4); 363 if (locs_capacity < min_locs) locs_capacity = min_locs; 364 relocInfo* locs_start = NEW_RESOURCE_ARRAY(relocInfo, locs_capacity); 365 _locs_start = locs_start; 366 _locs_end = locs_start; 367 _locs_limit = locs_start + locs_capacity; 368 _locs_own = true; 369} 370 371void CodeSection::initialize_shared_locs(relocInfo* buf, int length) { 372 assert(_locs_start == NULL, "do this before locs are allocated"); 373 // Internal invariant: locs buf must be fully aligned. 374 // See copy_relocations_to() below. 375 while ((uintptr_t)buf % HeapWordSize != 0 && length > 0) { 376 ++buf; --length; 377 } 378 if (length > 0) { 379 _locs_start = buf; 380 _locs_end = buf; 381 _locs_limit = buf + length; 382 _locs_own = false; 383 } 384} 385 386void CodeSection::initialize_locs_from(const CodeSection* source_cs) { 387 int lcount = source_cs->locs_count(); 388 if (lcount != 0) { 389 initialize_shared_locs(source_cs->locs_start(), lcount); 390 _locs_end = _locs_limit = _locs_start + lcount; 391 assert(is_allocated(), "must have copied code already"); 392 set_locs_point(start() + source_cs->locs_point_off()); 393 } 394 assert(this->locs_count() == source_cs->locs_count(), "sanity"); 395} 396 397void CodeSection::expand_locs(int new_capacity) { 398 if (_locs_start == NULL) { 399 initialize_locs(new_capacity); 400 return; 401 } else { 402 int old_count = locs_count(); 403 int old_capacity = locs_capacity(); 404 if (new_capacity < old_capacity * 2) 405 new_capacity = old_capacity * 2; 406 relocInfo* locs_start; 407 if (_locs_own) { 408 locs_start = REALLOC_RESOURCE_ARRAY(relocInfo, _locs_start, old_capacity, new_capacity); 409 } else { 410 locs_start = NEW_RESOURCE_ARRAY(relocInfo, new_capacity); 411 Copy::conjoint_jbytes(_locs_start, locs_start, old_capacity * sizeof(relocInfo)); 412 _locs_own = true; 413 } 414 _locs_start = locs_start; 415 _locs_end = locs_start + old_count; 416 _locs_limit = locs_start + new_capacity; 417 } 418} 419 420 421/// Support for emitting the code to its final location. 422/// The pattern is the same for all functions. 423/// We iterate over all the sections, padding each to alignment. 424 425csize_t CodeBuffer::total_content_size() const { 426 csize_t size_so_far = 0; 427 for (int n = 0; n < (int)SECT_LIMIT; n++) { 428 const CodeSection* cs = code_section(n); 429 if (cs->is_empty()) continue; // skip trivial section 430 size_so_far = cs->align_at_start(size_so_far); 431 size_so_far += cs->size(); 432 } 433 return size_so_far; 434} 435 436void CodeBuffer::compute_final_layout(CodeBuffer* dest) const { 437 address buf = dest->_total_start; 438 csize_t buf_offset = 0; 439 assert(dest->_total_size >= total_content_size(), "must be big enough"); 440 441 { 442 // not sure why this is here, but why not... 443 int alignSize = MAX2((intx) sizeof(jdouble), CodeEntryAlignment); 444 assert( (dest->_total_start - _insts.start()) % alignSize == 0, "copy must preserve alignment"); 445 } 446 447 const CodeSection* prev_cs = NULL; 448 CodeSection* prev_dest_cs = NULL; 449 450 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 451 // figure compact layout of each section 452 const CodeSection* cs = code_section(n); 453 csize_t csize = cs->size(); 454 455 CodeSection* dest_cs = dest->code_section(n); 456 if (!cs->is_empty()) { 457 // Compute initial padding; assign it to the previous non-empty guy. 458 // Cf. figure_expanded_capacities. 459 csize_t padding = cs->align_at_start(buf_offset) - buf_offset; 460 if (padding != 0) { 461 buf_offset += padding; 462 assert(prev_dest_cs != NULL, "sanity"); 463 prev_dest_cs->_limit += padding; 464 } 465 #ifdef ASSERT 466 if (prev_cs != NULL && prev_cs->is_frozen() && n < (SECT_LIMIT - 1)) { 467 // Make sure the ends still match up. 468 // This is important because a branch in a frozen section 469 // might target code in a following section, via a Label, 470 // and without a relocation record. See Label::patch_instructions. 471 address dest_start = buf+buf_offset; 472 csize_t start2start = cs->start() - prev_cs->start(); 473 csize_t dest_start2start = dest_start - prev_dest_cs->start(); 474 assert(start2start == dest_start2start, "cannot stretch frozen sect"); 475 } 476 #endif //ASSERT 477 prev_dest_cs = dest_cs; 478 prev_cs = cs; 479 } 480 481 debug_only(dest_cs->_start = NULL); // defeat double-initialization assert 482 dest_cs->initialize(buf+buf_offset, csize); 483 dest_cs->set_end(buf+buf_offset+csize); 484 assert(dest_cs->is_allocated(), "must always be allocated"); 485 assert(cs->is_empty() == dest_cs->is_empty(), "sanity"); 486 487 buf_offset += csize; 488 } 489 490 // Done calculating sections; did it come out to the right end? 491 assert(buf_offset == total_content_size(), "sanity"); 492 dest->verify_section_allocation(); 493} 494 495void CodeBuffer::finalize_oop_references(methodHandle mh) { 496 No_Safepoint_Verifier nsv; 497 498 GrowableArray<oop> oops; 499 500 // Make sure that immediate metadata records something in the OopRecorder 501 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 502 // pull code out of each section 503 CodeSection* cs = code_section(n); 504 if (cs->is_empty()) continue; // skip trivial section 505 RelocIterator iter(cs); 506 while (iter.next()) { 507 if (iter.type() == relocInfo::metadata_type) { 508 metadata_Relocation* md = iter.metadata_reloc(); 509 if (md->metadata_is_immediate()) { 510 Metadata* m = md->metadata_value(); 511 if (oop_recorder()->is_real(m)) { 512 oop o = NULL; 513 if (m->is_methodData()) { 514 m = ((MethodData*)m)->method(); 515 } 516 if (m->is_method()) { 517 m = ((Method*)m)->method_holder(); 518 } 519 if (m->is_klass()) { 520 o = ((Klass*)m)->class_loader(); 521 } else { 522 // XXX This will currently occur for MDO which don't 523 // have a backpointer. This has to be fixed later. 524 m->print(); 525 ShouldNotReachHere(); 526 } 527 if (o != NULL && oops.find(o) == -1) { 528 oops.append(o); 529 } 530 } 531 } 532 } 533 } 534 } 535 536 if (!oop_recorder()->is_unused()) { 537 for (int i = 0; i < oop_recorder()->metadata_count(); i++) { 538 Metadata* m = oop_recorder()->metadata_at(i); 539 if (oop_recorder()->is_real(m)) { 540 oop o = NULL; 541 if (m->is_methodData()) { 542 m = ((MethodData*)m)->method(); 543 } 544 if (m->is_method()) { 545 m = ((Method*)m)->method_holder(); 546 } 547 if (m->is_klass()) { 548 o = ((Klass*)m)->class_loader(); 549 } else { 550 m->print(); 551 ShouldNotReachHere(); 552 } 553 if (o != NULL && oops.find(o) == -1) { 554 oops.append(o); 555 } 556 } 557 } 558 559 } 560 561 // Add the class loader of Method* for the nmethod itself 562 oop cl = mh->method_holder()->class_loader(); 563 if (cl != NULL) { 564 oops.append(cl); 565 } 566 567 // Add any oops that we've found 568 Thread* thread = Thread::current(); 569 for (int i = 0; i < oops.length(); i++) { 570 oop_recorder()->find_index((jobject)thread->handle_area()->allocate_handle(oops.at(i))); 571 } 572} 573 574 575 576csize_t CodeBuffer::total_offset_of(CodeSection* cs) const { 577 csize_t size_so_far = 0; 578 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 579 const CodeSection* cur_cs = code_section(n); 580 if (!cur_cs->is_empty()) { 581 size_so_far = cur_cs->align_at_start(size_so_far); 582 } 583 if (cur_cs->index() == cs->index()) { 584 return size_so_far; 585 } 586 size_so_far += cur_cs->size(); 587 } 588 ShouldNotReachHere(); 589 return -1; 590} 591 592csize_t CodeBuffer::total_relocation_size() const { 593 csize_t lsize = copy_relocations_to(NULL); // dry run only 594 csize_t csize = total_content_size(); 595 csize_t total = RelocIterator::locs_and_index_size(csize, lsize); 596 return (csize_t) align_size_up(total, HeapWordSize); 597} 598 599csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const { 600 address buf = NULL; 601 csize_t buf_offset = 0; 602 csize_t buf_limit = 0; 603 if (dest != NULL) { 604 buf = (address)dest->relocation_begin(); 605 buf_limit = (address)dest->relocation_end() - buf; 606 assert((uintptr_t)buf % HeapWordSize == 0, "buf must be fully aligned"); 607 assert(buf_limit % HeapWordSize == 0, "buf must be evenly sized"); 608 } 609 // if dest == NULL, this is just the sizing pass 610 611 csize_t code_end_so_far = 0; 612 csize_t code_point_so_far = 0; 613 for (int n = (int) SECT_FIRST; n < (int)SECT_LIMIT; n++) { 614 // pull relocs out of each section 615 const CodeSection* cs = code_section(n); 616 assert(!(cs->is_empty() && cs->locs_count() > 0), "sanity"); 617 if (cs->is_empty()) continue; // skip trivial section 618 relocInfo* lstart = cs->locs_start(); 619 relocInfo* lend = cs->locs_end(); 620 csize_t lsize = (csize_t)( (address)lend - (address)lstart ); 621 csize_t csize = cs->size(); 622 code_end_so_far = cs->align_at_start(code_end_so_far); 623 624 if (lsize > 0) { 625 // Figure out how to advance the combined relocation point 626 // first to the beginning of this section. 627 // We'll insert one or more filler relocs to span that gap. 628 // (Don't bother to improve this by editing the first reloc's offset.) 629 csize_t new_code_point = code_end_so_far; 630 for (csize_t jump; 631 code_point_so_far < new_code_point; 632 code_point_so_far += jump) { 633 jump = new_code_point - code_point_so_far; 634 relocInfo filler = filler_relocInfo(); 635 if (jump >= filler.addr_offset()) { 636 jump = filler.addr_offset(); 637 } else { // else shrink the filler to fit 638 filler = relocInfo(relocInfo::none, jump); 639 } 640 if (buf != NULL) { 641 assert(buf_offset + (csize_t)sizeof(filler) <= buf_limit, "filler in bounds"); 642 *(relocInfo*)(buf+buf_offset) = filler; 643 } 644 buf_offset += sizeof(filler); 645 } 646 647 // Update code point and end to skip past this section: 648 csize_t last_code_point = code_end_so_far + cs->locs_point_off(); 649 assert(code_point_so_far <= last_code_point, "sanity"); 650 code_point_so_far = last_code_point; // advance past this guy's relocs 651 } 652 code_end_so_far += csize; // advance past this guy's instructions too 653 654 // Done with filler; emit the real relocations: 655 if (buf != NULL && lsize != 0) { 656 assert(buf_offset + lsize <= buf_limit, "target in bounds"); 657 assert((uintptr_t)lstart % HeapWordSize == 0, "sane start"); 658 if (buf_offset % HeapWordSize == 0) { 659 // Use wordwise copies if possible: 660 Copy::disjoint_words((HeapWord*)lstart, 661 (HeapWord*)(buf+buf_offset), 662 (lsize + HeapWordSize-1) / HeapWordSize); 663 } else { 664 Copy::conjoint_jbytes(lstart, buf+buf_offset, lsize); 665 } 666 } 667 buf_offset += lsize; 668 } 669 670 // Align end of relocation info in target. 671 while (buf_offset % HeapWordSize != 0) { 672 if (buf != NULL) { 673 relocInfo padding = relocInfo(relocInfo::none, 0); 674 assert(buf_offset + (csize_t)sizeof(padding) <= buf_limit, "padding in bounds"); 675 *(relocInfo*)(buf+buf_offset) = padding; 676 } 677 buf_offset += sizeof(relocInfo); 678 } 679 680 assert(code_end_so_far == total_content_size(), "sanity"); 681 682 // Account for index: 683 if (buf != NULL) { 684 RelocIterator::create_index(dest->relocation_begin(), 685 buf_offset / sizeof(relocInfo), 686 dest->relocation_end()); 687 } 688 689 return buf_offset; 690} 691 692void CodeBuffer::copy_code_to(CodeBlob* dest_blob) { 693#ifndef PRODUCT 694 if (PrintNMethods && (WizardMode || Verbose)) { 695 tty->print("done with CodeBuffer:"); 696 ((CodeBuffer*)this)->print(); 697 } 698#endif //PRODUCT 699 700 CodeBuffer dest(dest_blob); 701 assert(dest_blob->content_size() >= total_content_size(), "good sizing"); 702 this->compute_final_layout(&dest); 703 relocate_code_to(&dest); 704 705 // transfer comments from buffer to blob 706 dest_blob->set_comments(_comments); 707 708 // Done moving code bytes; were they the right size? 709 assert(round_to(dest.total_content_size(), oopSize) == dest_blob->content_size(), "sanity"); 710 711 // Flush generated code 712 ICache::invalidate_range(dest_blob->code_begin(), dest_blob->code_size()); 713} 714 715// Move all my code into another code buffer. Consult applicable 716// relocs to repair embedded addresses. The layout in the destination 717// CodeBuffer is different to the source CodeBuffer: the destination 718// CodeBuffer gets the final layout (consts, insts, stubs in order of 719// ascending address). 720void CodeBuffer::relocate_code_to(CodeBuffer* dest) const { 721 address dest_end = dest->_total_start + dest->_total_size; 722 address dest_filled = NULL; 723 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 724 // pull code out of each section 725 const CodeSection* cs = code_section(n); 726 if (cs->is_empty()) continue; // skip trivial section 727 CodeSection* dest_cs = dest->code_section(n); 728 assert(cs->size() == dest_cs->size(), "sanity"); 729 csize_t usize = dest_cs->size(); 730 csize_t wsize = align_size_up(usize, HeapWordSize); 731 assert(dest_cs->start() + wsize <= dest_end, "no overflow"); 732 // Copy the code as aligned machine words. 733 // This may also include an uninitialized partial word at the end. 734 Copy::disjoint_words((HeapWord*)cs->start(), 735 (HeapWord*)dest_cs->start(), 736 wsize / HeapWordSize); 737 738 if (dest->blob() == NULL) { 739 // Destination is a final resting place, not just another buffer. 740 // Normalize uninitialized bytes in the final padding. 741 Copy::fill_to_bytes(dest_cs->end(), dest_cs->remaining(), 742 Assembler::code_fill_byte()); 743 } 744 // Keep track of the highest filled address 745 dest_filled = MAX2(dest_filled, dest_cs->end() + dest_cs->remaining()); 746 747 assert(cs->locs_start() != (relocInfo*)badAddress, 748 "this section carries no reloc storage, but reloc was attempted"); 749 750 // Make the new code copy use the old copy's relocations: 751 dest_cs->initialize_locs_from(cs); 752 753 { // Repair the pc relative information in the code after the move 754 RelocIterator iter(dest_cs); 755 while (iter.next()) { 756 iter.reloc()->fix_relocation_after_move(this, dest); 757 } 758 } 759 } 760 761 if (dest->blob() == NULL) { 762 // Destination is a final resting place, not just another buffer. 763 // Normalize uninitialized bytes in the final padding. 764 Copy::fill_to_bytes(dest_filled, dest_end - dest_filled, 765 Assembler::code_fill_byte()); 766 767 } 768} 769 770csize_t CodeBuffer::figure_expanded_capacities(CodeSection* which_cs, 771 csize_t amount, 772 csize_t* new_capacity) { 773 csize_t new_total_cap = 0; 774 775 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 776 const CodeSection* sect = code_section(n); 777 778 if (!sect->is_empty()) { 779 // Compute initial padding; assign it to the previous section, 780 // even if it's empty (e.g. consts section can be empty). 781 // Cf. compute_final_layout 782 csize_t padding = sect->align_at_start(new_total_cap) - new_total_cap; 783 if (padding != 0) { 784 new_total_cap += padding; 785 assert(n - 1 >= SECT_FIRST, "sanity"); 786 new_capacity[n - 1] += padding; 787 } 788 } 789 790 csize_t exp = sect->size(); // 100% increase 791 if ((uint)exp < 4*K) exp = 4*K; // minimum initial increase 792 if (sect == which_cs) { 793 if (exp < amount) exp = amount; 794 if (StressCodeBuffers) exp = amount; // expand only slightly 795 } else if (n == SECT_INSTS) { 796 // scale down inst increases to a more modest 25% 797 exp = 4*K + ((exp - 4*K) >> 2); 798 if (StressCodeBuffers) exp = amount / 2; // expand only slightly 799 } else if (sect->is_empty()) { 800 // do not grow an empty secondary section 801 exp = 0; 802 } 803 // Allow for inter-section slop: 804 exp += CodeSection::end_slop(); 805 csize_t new_cap = sect->size() + exp; 806 if (new_cap < sect->capacity()) { 807 // No need to expand after all. 808 new_cap = sect->capacity(); 809 } 810 new_capacity[n] = new_cap; 811 new_total_cap += new_cap; 812 } 813 814 return new_total_cap; 815} 816 817void CodeBuffer::expand(CodeSection* which_cs, csize_t amount) { 818#ifndef PRODUCT 819 if (PrintNMethods && (WizardMode || Verbose)) { 820 tty->print("expanding CodeBuffer:"); 821 this->print(); 822 } 823 824 if (StressCodeBuffers && blob() != NULL) { 825 static int expand_count = 0; 826 if (expand_count >= 0) expand_count += 1; 827 if (expand_count > 100 && is_power_of_2(expand_count)) { 828 tty->print_cr("StressCodeBuffers: have expanded %d times", expand_count); 829 // simulate an occasional allocation failure: 830 free_blob(); 831 } 832 } 833#endif //PRODUCT 834 835 // Resizing must be allowed 836 { 837 if (blob() == NULL) return; // caller must check for blob == NULL 838 for (int n = 0; n < (int)SECT_LIMIT; n++) { 839 guarantee(!code_section(n)->is_frozen(), "resizing not allowed when frozen"); 840 } 841 } 842 843 // Figure new capacity for each section. 844 csize_t new_capacity[SECT_LIMIT]; 845 csize_t new_total_cap 846 = figure_expanded_capacities(which_cs, amount, new_capacity); 847 848 // Create a new (temporary) code buffer to hold all the new data 849 CodeBuffer cb(name(), new_total_cap, 0); 850 if (cb.blob() == NULL) { 851 // Failed to allocate in code cache. 852 free_blob(); 853 return; 854 } 855 856 // Create an old code buffer to remember which addresses used to go where. 857 // This will be useful when we do final assembly into the code cache, 858 // because we will need to know how to warp any internal address that 859 // has been created at any time in this CodeBuffer's past. 860 CodeBuffer* bxp = new CodeBuffer(_total_start, _total_size); 861 bxp->take_over_code_from(this); // remember the old undersized blob 862 DEBUG_ONLY(this->_blob = NULL); // silence a later assert 863 bxp->_before_expand = this->_before_expand; 864 this->_before_expand = bxp; 865 866 // Give each section its required (expanded) capacity. 867 for (int n = (int)SECT_LIMIT-1; n >= SECT_FIRST; n--) { 868 CodeSection* cb_sect = cb.code_section(n); 869 CodeSection* this_sect = code_section(n); 870 if (new_capacity[n] == 0) continue; // already nulled out 871 if (n != SECT_INSTS) { 872 cb.initialize_section_size(cb_sect, new_capacity[n]); 873 } 874 assert(cb_sect->capacity() >= new_capacity[n], "big enough"); 875 address cb_start = cb_sect->start(); 876 cb_sect->set_end(cb_start + this_sect->size()); 877 if (this_sect->mark() == NULL) { 878 cb_sect->clear_mark(); 879 } else { 880 cb_sect->set_mark(cb_start + this_sect->mark_off()); 881 } 882 } 883 884 // Move all the code and relocations to the new blob: 885 relocate_code_to(&cb); 886 887 // Copy the temporary code buffer into the current code buffer. 888 // Basically, do {*this = cb}, except for some control information. 889 this->take_over_code_from(&cb); 890 cb.set_blob(NULL); 891 892 // Zap the old code buffer contents, to avoid mistakenly using them. 893 debug_only(Copy::fill_to_bytes(bxp->_total_start, bxp->_total_size, 894 badCodeHeapFreeVal)); 895 896 _decode_begin = NULL; // sanity 897 898 // Make certain that the new sections are all snugly inside the new blob. 899 verify_section_allocation(); 900 901#ifndef PRODUCT 902 if (PrintNMethods && (WizardMode || Verbose)) { 903 tty->print("expanded CodeBuffer:"); 904 this->print(); 905 } 906#endif //PRODUCT 907} 908 909void CodeBuffer::take_over_code_from(CodeBuffer* cb) { 910 // Must already have disposed of the old blob somehow. 911 assert(blob() == NULL, "must be empty"); 912#ifdef ASSERT 913 914#endif 915 // Take the new blob away from cb. 916 set_blob(cb->blob()); 917 // Take over all the section pointers. 918 for (int n = 0; n < (int)SECT_LIMIT; n++) { 919 CodeSection* cb_sect = cb->code_section(n); 920 CodeSection* this_sect = code_section(n); 921 this_sect->take_over_code_from(cb_sect); 922 } 923 _overflow_arena = cb->_overflow_arena; 924 // Make sure the old cb won't try to use it or free it. 925 DEBUG_ONLY(cb->_blob = (BufferBlob*)badAddress); 926} 927 928void CodeBuffer::verify_section_allocation() { 929 address tstart = _total_start; 930 if (tstart == badAddress) return; // smashed by set_blob(NULL) 931 address tend = tstart + _total_size; 932 if (_blob != NULL) { 933 934 guarantee(tstart >= _blob->content_begin(), "sanity"); 935 guarantee(tend <= _blob->content_end(), "sanity"); 936 } 937 // Verify disjointness. 938 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 939 CodeSection* sect = code_section(n); 940 if (!sect->is_allocated() || sect->is_empty()) continue; 941 guarantee((intptr_t)sect->start() % sect->alignment() == 0 942 || sect->is_empty() || _blob == NULL, 943 "start is aligned"); 944 for (int m = (int) SECT_FIRST; m < (int) SECT_LIMIT; m++) { 945 CodeSection* other = code_section(m); 946 if (!other->is_allocated() || other == sect) continue; 947 guarantee(!other->contains(sect->start() ), "sanity"); 948 // limit is an exclusive address and can be the start of another 949 // section. 950 guarantee(!other->contains(sect->limit() - 1), "sanity"); 951 } 952 guarantee(sect->end() <= tend, "sanity"); 953 guarantee(sect->end() <= sect->limit(), "sanity"); 954 } 955} 956 957void CodeBuffer::log_section_sizes(const char* name) { 958 if (xtty != NULL) { 959 // log info about buffer usage 960 xtty->print_cr("<blob name='%s' size='%d'>", name, _total_size); 961 for (int n = (int) CodeBuffer::SECT_FIRST; n < (int) CodeBuffer::SECT_LIMIT; n++) { 962 CodeSection* sect = code_section(n); 963 if (!sect->is_allocated() || sect->is_empty()) continue; 964 xtty->print_cr("<sect index='%d' size='" SIZE_FORMAT "' free='" SIZE_FORMAT "'/>", 965 n, sect->limit() - sect->start(), sect->limit() - sect->end()); 966 } 967 xtty->print_cr("</blob>"); 968 } 969} 970 971#ifndef PRODUCT 972 973void CodeSection::dump() { 974 address ptr = start(); 975 for (csize_t step; ptr < end(); ptr += step) { 976 step = end() - ptr; 977 if (step > jintSize * 4) step = jintSize * 4; 978 tty->print(PTR_FORMAT ": ", ptr); 979 while (step > 0) { 980 tty->print(" " PTR32_FORMAT, *(jint*)ptr); 981 ptr += jintSize; 982 } 983 tty->cr(); 984 } 985} 986 987 988void CodeSection::decode() { 989 Disassembler::decode(start(), end()); 990} 991 992 993void CodeBuffer::block_comment(intptr_t offset, const char * comment) { 994 _comments.add_comment(offset, comment); 995} 996 997class CodeComment: public CHeapObj<mtCode> { 998 private: 999 friend class CodeComments; 1000 intptr_t _offset; 1001 const char * _comment; 1002 CodeComment* _next; 1003 1004 ~CodeComment() { 1005 assert(_next == NULL, "wrong interface for freeing list"); 1006 os::free((void*)_comment, mtCode); 1007 } 1008 1009 public: 1010 CodeComment(intptr_t offset, const char * comment) { 1011 _offset = offset; 1012 _comment = os::strdup(comment, mtCode); 1013 _next = NULL; 1014 } 1015 1016 intptr_t offset() const { return _offset; } 1017 const char * comment() const { return _comment; } 1018 CodeComment* next() { return _next; } 1019 1020 void set_next(CodeComment* next) { _next = next; } 1021 1022 CodeComment* find(intptr_t offset) { 1023 CodeComment* a = this; 1024 while (a != NULL && a->_offset != offset) { 1025 a = a->_next; 1026 } 1027 return a; 1028 } 1029}; 1030 1031 1032void CodeComments::add_comment(intptr_t offset, const char * comment) { 1033 CodeComment* c = new CodeComment(offset, comment); 1034 CodeComment* insert = NULL; 1035 if (_comments != NULL) { 1036 CodeComment* c = _comments->find(offset); 1037 insert = c; 1038 while (c && c->offset() == offset) { 1039 insert = c; 1040 c = c->next(); 1041 } 1042 } 1043 if (insert) { 1044 // insert after comments with same offset 1045 c->set_next(insert->next()); 1046 insert->set_next(c); 1047 } else { 1048 c->set_next(_comments); 1049 _comments = c; 1050 } 1051} 1052 1053 1054void CodeComments::assign(CodeComments& other) { 1055 assert(_comments == NULL, "don't overwrite old value"); 1056 _comments = other._comments; 1057} 1058 1059 1060void CodeComments::print_block_comment(outputStream* stream, intptr_t offset) { 1061 if (_comments != NULL) { 1062 CodeComment* c = _comments->find(offset); 1063 while (c && c->offset() == offset) { 1064 stream->bol(); 1065 stream->print(" ;; "); 1066 stream->print_cr(c->comment()); 1067 c = c->next(); 1068 } 1069 } 1070} 1071 1072 1073void CodeComments::free() { 1074 CodeComment* n = _comments; 1075 while (n) { 1076 // unlink the node from the list saving a pointer to the next 1077 CodeComment* p = n->_next; 1078 n->_next = NULL; 1079 delete n; 1080 n = p; 1081 } 1082 _comments = NULL; 1083} 1084 1085 1086 1087void CodeBuffer::decode() { 1088 Disassembler::decode(decode_begin(), insts_end()); 1089 _decode_begin = insts_end(); 1090} 1091 1092 1093void CodeBuffer::skip_decode() { 1094 _decode_begin = insts_end(); 1095} 1096 1097 1098void CodeBuffer::decode_all() { 1099 for (int n = 0; n < (int)SECT_LIMIT; n++) { 1100 // dump contents of each section 1101 CodeSection* cs = code_section(n); 1102 tty->print_cr("! %s:", code_section_name(n)); 1103 if (cs != consts()) 1104 cs->decode(); 1105 else 1106 cs->dump(); 1107 } 1108} 1109 1110 1111void CodeSection::print(const char* name) { 1112 csize_t locs_size = locs_end() - locs_start(); 1113 tty->print_cr(" %7s.code = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d)%s", 1114 name, start(), end(), limit(), size(), capacity(), 1115 is_frozen()? " [frozen]": ""); 1116 tty->print_cr(" %7s.locs = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d) point=%d", 1117 name, locs_start(), locs_end(), locs_limit(), locs_size, locs_capacity(), locs_point_off()); 1118 if (PrintRelocations) { 1119 RelocIterator iter(this); 1120 iter.print(); 1121 } 1122} 1123 1124void CodeBuffer::print() { 1125 if (this == NULL) { 1126 tty->print_cr("NULL CodeBuffer pointer"); 1127 return; 1128 } 1129 1130 tty->print_cr("CodeBuffer:"); 1131 for (int n = 0; n < (int)SECT_LIMIT; n++) { 1132 // print each section 1133 CodeSection* cs = code_section(n); 1134 cs->print(code_section_name(n)); 1135 } 1136} 1137 1138#endif // PRODUCT 1139