1 /* 2 * Copyright (c) 1997, 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/_codeBuffer.cpp.incl" 27 28 // The structure of a CodeSection: 29 // 30 // _start -> +----------------+ 31 // | machine code...| 32 // _end -> |----------------| 33 // | | 34 // | (empty) | 35 // | | 36 // | | 37 // +----------------+ 38 // _limit -> | | 39 // 40 // _locs_start -> +----------------+ 41 // |reloc records...| 42 // |----------------| 43 // _locs_end -> | | 44 // | | 45 // | (empty) | 46 // | | 47 // | | 48 // +----------------+ 49 // _locs_limit -> | | 50 // The _end (resp. _limit) pointer refers to the first 51 // unused (resp. unallocated) byte. 52 53 // The structure of the CodeBuffer while code is being accumulated: 54 // 55 // _total_start -> \ 56 // _insts._start -> +----------------+ 57 // | | 58 // | Code | 59 // | | 60 // _stubs._start -> |----------------| 61 // | | 62 // | Stubs | (also handlers for deopt/exception) 63 // | | 64 // _consts._start -> |----------------| 65 // | | 66 // | Constants | 67 // | | 68 // +----------------+ 69 // + _total_size -> | | 70 // 71 // When the code and relocations are copied to the code cache, 72 // the empty parts of each section are removed, and everything 73 // is copied into contiguous locations. 74 75 typedef CodeBuffer::csize_t csize_t; // file-local definition 76 77 // External buffer, in a predefined CodeBlob. 78 // Important: The code_start must be taken exactly, and not realigned. 79 CodeBuffer::CodeBuffer(CodeBlob* blob) { 80 initialize_misc("static buffer"); 81 initialize(blob->content_begin(), blob->content_size()); 82 assert(verify_section_allocation(), "initial use of buffer OK"); 83 } 84 85 void CodeBuffer::initialize(csize_t code_size, csize_t locs_size) { 86 // Compute maximal alignment. 87 int align = _insts.alignment(); 88 // Always allow for empty slop around each section. 89 int slop = (int) CodeSection::end_slop(); 90 91 assert(blob() == NULL, "only once"); 92 set_blob(BufferBlob::create(_name, code_size + (align+slop) * (SECT_LIMIT+1))); 93 if (blob() == NULL) { 94 // The assembler constructor will throw a fatal on an empty CodeBuffer. 95 return; // caller must test this 96 } 97 98 // Set up various pointers into the blob. 99 initialize(_total_start, _total_size); 100 101 assert((uintptr_t)insts_begin() % CodeEntryAlignment == 0, "instruction start not code entry aligned"); 102 103 pd_initialize(); 104 105 if (locs_size != 0) { 106 _insts.initialize_locs(locs_size / sizeof(relocInfo)); 107 } 108 109 assert(verify_section_allocation(), "initial use of blob is OK"); 110 } 111 112 113 CodeBuffer::~CodeBuffer() { 114 // If we allocate our code buffer from the CodeCache 115 // via a BufferBlob, and it's not permanent, then 116 // free the BufferBlob. 117 // The rest of the memory will be freed when the ResourceObj 118 // is released. 119 assert(verify_section_allocation(), "final storage configuration still OK"); 120 for (CodeBuffer* cb = this; cb != NULL; cb = cb->before_expand()) { 121 // Previous incarnations of this buffer are held live, so that internal 122 // addresses constructed before expansions will not be confused. 123 cb->free_blob(); 124 } 125 126 // free any overflow storage 127 delete _overflow_arena; 128 129 #ifdef ASSERT 130 // Save allocation type to execute assert in ~ResourceObj() 131 // which is called after this destructor. 132 ResourceObj::allocation_type at = _default_oop_recorder.get_allocation_type(); 133 Copy::fill_to_bytes(this, sizeof(*this), badResourceValue); 134 ResourceObj::set_allocation_type((address)(&_default_oop_recorder), at); 135 #endif 136 } 137 138 void CodeBuffer::initialize_oop_recorder(OopRecorder* r) { 139 assert(_oop_recorder == &_default_oop_recorder && _default_oop_recorder.is_unused(), "do this once"); 140 DEBUG_ONLY(_default_oop_recorder.oop_size()); // force unused OR to be frozen 141 _oop_recorder = r; 142 } 143 144 void CodeBuffer::initialize_section_size(CodeSection* cs, csize_t size) { 145 assert(cs != &_insts, "insts is the memory provider, not the consumer"); 146 csize_t slop = CodeSection::end_slop(); // margin between sections 147 int align = cs->alignment(); 148 assert(is_power_of_2(align), "sanity"); 149 address start = _insts._start; 150 address limit = _insts._limit; 151 address middle = limit - size; 152 middle -= (intptr_t)middle & (align-1); // align the division point downward 153 guarantee(middle - slop > start, "need enough space to divide up"); 154 _insts._limit = middle - slop; // subtract desired space, plus slop 155 cs->initialize(middle, limit - middle); 156 assert(cs->start() == middle, "sanity"); 157 assert(cs->limit() == limit, "sanity"); 158 // give it some relocations to start with, if the main section has them 159 if (_insts.has_locs()) cs->initialize_locs(1); 160 } 161 162 void CodeBuffer::freeze_section(CodeSection* cs) { 163 CodeSection* next_cs = (cs == consts())? NULL: code_section(cs->index()+1); 164 csize_t frozen_size = cs->size(); 165 if (next_cs != NULL) { 166 frozen_size = next_cs->align_at_start(frozen_size); 167 } 168 address old_limit = cs->limit(); 169 address new_limit = cs->start() + frozen_size; 170 relocInfo* old_locs_limit = cs->locs_limit(); 171 relocInfo* new_locs_limit = cs->locs_end(); 172 // Patch the limits. 173 cs->_limit = new_limit; 174 cs->_locs_limit = new_locs_limit; 175 cs->_frozen = true; 176 if (!next_cs->is_allocated() && !next_cs->is_frozen()) { 177 // Give remaining buffer space to the following section. 178 next_cs->initialize(new_limit, old_limit - new_limit); 179 next_cs->initialize_shared_locs(new_locs_limit, 180 old_locs_limit - new_locs_limit); 181 } 182 } 183 184 void CodeBuffer::set_blob(BufferBlob* blob) { 185 _blob = blob; 186 if (blob != NULL) { 187 address start = blob->content_begin(); 188 address end = blob->content_end(); 189 // Round up the starting address. 190 int align = _insts.alignment(); 191 start += (-(intptr_t)start) & (align-1); 192 _total_start = start; 193 _total_size = end - start; 194 } else { 195 #ifdef ASSERT 196 // Clean out dangling pointers. 197 _total_start = badAddress; 198 _consts._start = _consts._end = badAddress; 199 _insts._start = _insts._end = badAddress; 200 _stubs._start = _stubs._end = badAddress; 201 #endif //ASSERT 202 } 203 } 204 205 void CodeBuffer::free_blob() { 206 if (_blob != NULL) { 207 BufferBlob::free(_blob); 208 set_blob(NULL); 209 } 210 } 211 212 const char* CodeBuffer::code_section_name(int n) { 213 #ifdef PRODUCT 214 return NULL; 215 #else //PRODUCT 216 switch (n) { 217 case SECT_CONSTS: return "consts"; 218 case SECT_INSTS: return "insts"; 219 case SECT_STUBS: return "stubs"; 220 default: return NULL; 221 } 222 #endif //PRODUCT 223 } 224 225 int CodeBuffer::section_index_of(address addr) const { 226 for (int n = 0; n < (int)SECT_LIMIT; n++) { 227 const CodeSection* cs = code_section(n); 228 if (cs->allocates(addr)) return n; 229 } 230 return SECT_NONE; 231 } 232 233 int CodeBuffer::locator(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)) { 237 return locator(addr - cs->start(), n); 238 } 239 } 240 return -1; 241 } 242 243 address CodeBuffer::locator_address(int locator) const { 244 if (locator < 0) return NULL; 245 address start = code_section(locator_sect(locator))->start(); 246 return start + locator_pos(locator); 247 } 248 249 address CodeBuffer::decode_begin() { 250 address begin = _insts.start(); 251 if (_decode_begin != NULL && _decode_begin > begin) 252 begin = _decode_begin; 253 return begin; 254 } 255 256 257 GrowableArray<int>* CodeBuffer::create_patch_overflow() { 258 if (_overflow_arena == NULL) { 259 _overflow_arena = new Arena(); 260 } 261 return new (_overflow_arena) GrowableArray<int>(_overflow_arena, 8, 0, 0); 262 } 263 264 265 // Helper function for managing labels and their target addresses. 266 // Returns a sensible address, and if it is not the label's final 267 // address, notes the dependency (at 'branch_pc') on the label. 268 address CodeSection::target(Label& L, address branch_pc) { 269 if (L.is_bound()) { 270 int loc = L.loc(); 271 if (index() == CodeBuffer::locator_sect(loc)) { 272 return start() + CodeBuffer::locator_pos(loc); 273 } else { 274 return outer()->locator_address(loc); 275 } 276 } else { 277 assert(allocates2(branch_pc), "sanity"); 278 address base = start(); 279 int patch_loc = CodeBuffer::locator(branch_pc - base, index()); 280 L.add_patch_at(outer(), patch_loc); 281 282 // Need to return a pc, doesn't matter what it is since it will be 283 // replaced during resolution later. 284 // Don't return NULL or badAddress, since branches shouldn't overflow. 285 // Don't return base either because that could overflow displacements 286 // for shorter branches. It will get checked when bound. 287 return branch_pc; 288 } 289 } 290 291 void CodeSection::relocate(address at, RelocationHolder const& spec, int format) { 292 Relocation* reloc = spec.reloc(); 293 relocInfo::relocType rtype = (relocInfo::relocType) reloc->type(); 294 if (rtype == relocInfo::none) return; 295 296 // The assertion below has been adjusted, to also work for 297 // relocation for fixup. Sometimes we want to put relocation 298 // information for the next instruction, since it will be patched 299 // with a call. 300 assert(start() <= at && at <= end()+1, 301 "cannot relocate data outside code boundaries"); 302 303 if (!has_locs()) { 304 // no space for relocation information provided => code cannot be 305 // relocated. Make sure that relocate is only called with rtypes 306 // that can be ignored for this kind of code. 307 assert(rtype == relocInfo::none || 308 rtype == relocInfo::runtime_call_type || 309 rtype == relocInfo::internal_word_type|| 310 rtype == relocInfo::section_word_type || 311 rtype == relocInfo::external_word_type, 312 "code needs relocation information"); 313 // leave behind an indication that we attempted a relocation 314 DEBUG_ONLY(_locs_start = _locs_limit = (relocInfo*)badAddress); 315 return; 316 } 317 318 // Advance the point, noting the offset we'll have to record. 319 csize_t offset = at - locs_point(); 320 set_locs_point(at); 321 322 // Test for a couple of overflow conditions; maybe expand the buffer. 323 relocInfo* end = locs_end(); 324 relocInfo* req = end + relocInfo::length_limit; 325 // Check for (potential) overflow 326 if (req >= locs_limit() || offset >= relocInfo::offset_limit()) { 327 req += (uint)offset / (uint)relocInfo::offset_limit(); 328 if (req >= locs_limit()) { 329 // Allocate or reallocate. 330 expand_locs(locs_count() + (req - end)); 331 // reload pointer 332 end = locs_end(); 333 } 334 } 335 336 // If the offset is giant, emit filler relocs, of type 'none', but 337 // each carrying the largest possible offset, to advance the locs_point. 338 while (offset >= relocInfo::offset_limit()) { 339 assert(end < locs_limit(), "adjust previous paragraph of code"); 340 *end++ = filler_relocInfo(); 341 offset -= filler_relocInfo().addr_offset(); 342 } 343 344 // If it's a simple reloc with no data, we'll just write (rtype | offset). 345 (*end) = relocInfo(rtype, offset, format); 346 347 // If it has data, insert the prefix, as (data_prefix_tag | data1), data2. 348 end->initialize(this, reloc); 349 } 350 351 void CodeSection::initialize_locs(int locs_capacity) { 352 assert(_locs_start == NULL, "only one locs init step, please"); 353 // Apply a priori lower limits to relocation size: 354 csize_t min_locs = MAX2(size() / 16, (csize_t)4); 355 if (locs_capacity < min_locs) locs_capacity = min_locs; 356 relocInfo* locs_start = NEW_RESOURCE_ARRAY(relocInfo, locs_capacity); 357 _locs_start = locs_start; 358 _locs_end = locs_start; 359 _locs_limit = locs_start + locs_capacity; 360 _locs_own = true; 361 } 362 363 void CodeSection::initialize_shared_locs(relocInfo* buf, int length) { 364 assert(_locs_start == NULL, "do this before locs are allocated"); 365 // Internal invariant: locs buf must be fully aligned. 366 // See copy_relocations_to() below. 367 while ((uintptr_t)buf % HeapWordSize != 0 && length > 0) { 368 ++buf; --length; 369 } 370 if (length > 0) { 371 _locs_start = buf; 372 _locs_end = buf; 373 _locs_limit = buf + length; 374 _locs_own = false; 375 } 376 } 377 378 void CodeSection::initialize_locs_from(const CodeSection* source_cs) { 379 int lcount = source_cs->locs_count(); 380 if (lcount != 0) { 381 initialize_shared_locs(source_cs->locs_start(), lcount); 382 _locs_end = _locs_limit = _locs_start + lcount; 383 assert(is_allocated(), "must have copied code already"); 384 set_locs_point(start() + source_cs->locs_point_off()); 385 } 386 assert(this->locs_count() == source_cs->locs_count(), "sanity"); 387 } 388 389 void CodeSection::expand_locs(int new_capacity) { 390 if (_locs_start == NULL) { 391 initialize_locs(new_capacity); 392 return; 393 } else { 394 int old_count = locs_count(); 395 int old_capacity = locs_capacity(); 396 if (new_capacity < old_capacity * 2) 397 new_capacity = old_capacity * 2; 398 relocInfo* locs_start; 399 if (_locs_own) { 400 locs_start = REALLOC_RESOURCE_ARRAY(relocInfo, _locs_start, old_capacity, new_capacity); 401 } else { 402 locs_start = NEW_RESOURCE_ARRAY(relocInfo, new_capacity); 403 Copy::conjoint_jbytes(_locs_start, locs_start, old_capacity * sizeof(relocInfo)); 404 _locs_own = true; 405 } 406 _locs_start = locs_start; 407 _locs_end = locs_start + old_count; 408 _locs_limit = locs_start + new_capacity; 409 } 410 } 411 412 413 /// Support for emitting the code to its final location. 414 /// The pattern is the same for all functions. 415 /// We iterate over all the sections, padding each to alignment. 416 417 csize_t CodeBuffer::total_content_size() const { 418 csize_t size_so_far = 0; 419 for (int n = 0; n < (int)SECT_LIMIT; n++) { 420 const CodeSection* cs = code_section(n); 421 if (cs->is_empty()) continue; // skip trivial section 422 size_so_far = cs->align_at_start(size_so_far); 423 size_so_far += cs->size(); 424 } 425 return size_so_far; 426 } 427 428 void CodeBuffer::compute_final_layout(CodeBuffer* dest) const { 429 address buf = dest->_total_start; 430 csize_t buf_offset = 0; 431 assert(dest->_total_size >= total_content_size(), "must be big enough"); 432 433 { 434 // not sure why this is here, but why not... 435 int alignSize = MAX2((intx) sizeof(jdouble), CodeEntryAlignment); 436 assert( (dest->_total_start - _insts.start()) % alignSize == 0, "copy must preserve alignment"); 437 } 438 439 const CodeSection* prev_cs = NULL; 440 CodeSection* prev_dest_cs = NULL; 441 442 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 443 // figure compact layout of each section 444 const CodeSection* cs = code_section(n); 445 csize_t csize = cs->size(); 446 447 CodeSection* dest_cs = dest->code_section(n); 448 if (!cs->is_empty()) { 449 // Compute initial padding; assign it to the previous non-empty guy. 450 // Cf. figure_expanded_capacities. 451 csize_t padding = cs->align_at_start(buf_offset) - buf_offset; 452 if (padding != 0) { 453 buf_offset += padding; 454 assert(prev_dest_cs != NULL, "sanity"); 455 prev_dest_cs->_limit += padding; 456 } 457 #ifdef ASSERT 458 if (prev_cs != NULL && prev_cs->is_frozen() && n < (SECT_LIMIT - 1)) { 459 // Make sure the ends still match up. 460 // This is important because a branch in a frozen section 461 // might target code in a following section, via a Label, 462 // and without a relocation record. See Label::patch_instructions. 463 address dest_start = buf+buf_offset; 464 csize_t start2start = cs->start() - prev_cs->start(); 465 csize_t dest_start2start = dest_start - prev_dest_cs->start(); 466 assert(start2start == dest_start2start, "cannot stretch frozen sect"); 467 } 468 #endif //ASSERT 469 prev_dest_cs = dest_cs; 470 prev_cs = cs; 471 } 472 473 debug_only(dest_cs->_start = NULL); // defeat double-initialization assert 474 dest_cs->initialize(buf+buf_offset, csize); 475 dest_cs->set_end(buf+buf_offset+csize); 476 assert(dest_cs->is_allocated(), "must always be allocated"); 477 assert(cs->is_empty() == dest_cs->is_empty(), "sanity"); 478 479 buf_offset += csize; 480 } 481 482 // Done calculating sections; did it come out to the right end? 483 assert(buf_offset == total_content_size(), "sanity"); 484 assert(dest->verify_section_allocation(), "final configuration works"); 485 } 486 487 csize_t CodeBuffer::total_offset_of(CodeSection* cs) const { 488 csize_t size_so_far = 0; 489 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 490 const CodeSection* cur_cs = code_section(n); 491 if (!cur_cs->is_empty()) { 492 size_so_far = cur_cs->align_at_start(size_so_far); 493 } 494 if (cur_cs->index() == cs->index()) { 495 return size_so_far; 496 } 497 size_so_far += cur_cs->size(); 498 } 499 ShouldNotReachHere(); 500 return -1; 501 } 502 503 csize_t CodeBuffer::total_relocation_size() const { 504 csize_t lsize = copy_relocations_to(NULL); // dry run only 505 csize_t csize = total_content_size(); 506 csize_t total = RelocIterator::locs_and_index_size(csize, lsize); 507 return (csize_t) align_size_up(total, HeapWordSize); 508 } 509 510 csize_t CodeBuffer::copy_relocations_to(CodeBlob* dest) const { 511 address buf = NULL; 512 csize_t buf_offset = 0; 513 csize_t buf_limit = 0; 514 if (dest != NULL) { 515 buf = (address)dest->relocation_begin(); 516 buf_limit = (address)dest->relocation_end() - buf; 517 assert((uintptr_t)buf % HeapWordSize == 0, "buf must be fully aligned"); 518 assert(buf_limit % HeapWordSize == 0, "buf must be evenly sized"); 519 } 520 // if dest == NULL, this is just the sizing pass 521 522 csize_t code_end_so_far = 0; 523 csize_t code_point_so_far = 0; 524 for (int n = (int) SECT_FIRST; n < (int)SECT_LIMIT; n++) { 525 // pull relocs out of each section 526 const CodeSection* cs = code_section(n); 527 assert(!(cs->is_empty() && cs->locs_count() > 0), "sanity"); 528 if (cs->is_empty()) continue; // skip trivial section 529 relocInfo* lstart = cs->locs_start(); 530 relocInfo* lend = cs->locs_end(); 531 csize_t lsize = (csize_t)( (address)lend - (address)lstart ); 532 csize_t csize = cs->size(); 533 code_end_so_far = cs->align_at_start(code_end_so_far); 534 535 if (lsize > 0) { 536 // Figure out how to advance the combined relocation point 537 // first to the beginning of this section. 538 // We'll insert one or more filler relocs to span that gap. 539 // (Don't bother to improve this by editing the first reloc's offset.) 540 csize_t new_code_point = code_end_so_far; 541 for (csize_t jump; 542 code_point_so_far < new_code_point; 543 code_point_so_far += jump) { 544 jump = new_code_point - code_point_so_far; 545 relocInfo filler = filler_relocInfo(); 546 if (jump >= filler.addr_offset()) { 547 jump = filler.addr_offset(); 548 } else { // else shrink the filler to fit 549 filler = relocInfo(relocInfo::none, jump); 550 } 551 if (buf != NULL) { 552 assert(buf_offset + (csize_t)sizeof(filler) <= buf_limit, "filler in bounds"); 553 *(relocInfo*)(buf+buf_offset) = filler; 554 } 555 buf_offset += sizeof(filler); 556 } 557 558 // Update code point and end to skip past this section: 559 csize_t last_code_point = code_end_so_far + cs->locs_point_off(); 560 assert(code_point_so_far <= last_code_point, "sanity"); 561 code_point_so_far = last_code_point; // advance past this guy's relocs 562 } 563 code_end_so_far += csize; // advance past this guy's instructions too 564 565 // Done with filler; emit the real relocations: 566 if (buf != NULL && lsize != 0) { 567 assert(buf_offset + lsize <= buf_limit, "target in bounds"); 568 assert((uintptr_t)lstart % HeapWordSize == 0, "sane start"); 569 if (buf_offset % HeapWordSize == 0) { 570 // Use wordwise copies if possible: 571 Copy::disjoint_words((HeapWord*)lstart, 572 (HeapWord*)(buf+buf_offset), 573 (lsize + HeapWordSize-1) / HeapWordSize); 574 } else { 575 Copy::conjoint_jbytes(lstart, buf+buf_offset, lsize); 576 } 577 } 578 buf_offset += lsize; 579 } 580 581 // Align end of relocation info in target. 582 while (buf_offset % HeapWordSize != 0) { 583 if (buf != NULL) { 584 relocInfo padding = relocInfo(relocInfo::none, 0); 585 assert(buf_offset + (csize_t)sizeof(padding) <= buf_limit, "padding in bounds"); 586 *(relocInfo*)(buf+buf_offset) = padding; 587 } 588 buf_offset += sizeof(relocInfo); 589 } 590 591 assert(code_end_so_far == total_content_size(), "sanity"); 592 593 // Account for index: 594 if (buf != NULL) { 595 RelocIterator::create_index(dest->relocation_begin(), 596 buf_offset / sizeof(relocInfo), 597 dest->relocation_end()); 598 } 599 600 return buf_offset; 601 } 602 603 void CodeBuffer::copy_code_to(CodeBlob* dest_blob) { 604 #ifndef PRODUCT 605 if (PrintNMethods && (WizardMode || Verbose)) { 606 tty->print("done with CodeBuffer:"); 607 ((CodeBuffer*)this)->print(); 608 } 609 #endif //PRODUCT 610 611 CodeBuffer dest(dest_blob); 612 assert(dest_blob->content_size() >= total_content_size(), "good sizing"); 613 this->compute_final_layout(&dest); 614 relocate_code_to(&dest); 615 616 // transfer comments from buffer to blob 617 dest_blob->set_comments(_comments); 618 619 // Done moving code bytes; were they the right size? 620 assert(round_to(dest.total_content_size(), oopSize) == dest_blob->content_size(), "sanity"); 621 622 // Flush generated code 623 ICache::invalidate_range(dest_blob->code_begin(), dest_blob->code_size()); 624 } 625 626 // Move all my code into another code buffer. Consult applicable 627 // relocs to repair embedded addresses. The layout in the destination 628 // CodeBuffer is different to the source CodeBuffer: the destination 629 // CodeBuffer gets the final layout (consts, insts, stubs in order of 630 // ascending address). 631 void CodeBuffer::relocate_code_to(CodeBuffer* dest) const { 632 DEBUG_ONLY(address dest_end = dest->_total_start + dest->_total_size); 633 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 634 // pull code out of each section 635 const CodeSection* cs = code_section(n); 636 if (cs->is_empty()) continue; // skip trivial section 637 CodeSection* dest_cs = dest->code_section(n); 638 assert(cs->size() == dest_cs->size(), "sanity"); 639 csize_t usize = dest_cs->size(); 640 csize_t wsize = align_size_up(usize, HeapWordSize); 641 assert(dest_cs->start() + wsize <= dest_end, "no overflow"); 642 // Copy the code as aligned machine words. 643 // This may also include an uninitialized partial word at the end. 644 Copy::disjoint_words((HeapWord*)cs->start(), 645 (HeapWord*)dest_cs->start(), 646 wsize / HeapWordSize); 647 648 if (dest->blob() == NULL) { 649 // Destination is a final resting place, not just another buffer. 650 // Normalize uninitialized bytes in the final padding. 651 Copy::fill_to_bytes(dest_cs->end(), dest_cs->remaining(), 652 Assembler::code_fill_byte()); 653 } 654 655 assert(cs->locs_start() != (relocInfo*)badAddress, 656 "this section carries no reloc storage, but reloc was attempted"); 657 658 // Make the new code copy use the old copy's relocations: 659 dest_cs->initialize_locs_from(cs); 660 661 { // Repair the pc relative information in the code after the move 662 RelocIterator iter(dest_cs); 663 while (iter.next()) { 664 iter.reloc()->fix_relocation_after_move(this, dest); 665 } 666 } 667 } 668 } 669 670 csize_t CodeBuffer::figure_expanded_capacities(CodeSection* which_cs, 671 csize_t amount, 672 csize_t* new_capacity) { 673 csize_t new_total_cap = 0; 674 675 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 676 const CodeSection* sect = code_section(n); 677 678 if (!sect->is_empty()) { 679 // Compute initial padding; assign it to the previous section, 680 // even if it's empty (e.g. consts section can be empty). 681 // Cf. compute_final_layout 682 csize_t padding = sect->align_at_start(new_total_cap) - new_total_cap; 683 if (padding != 0) { 684 new_total_cap += padding; 685 assert(n - 1 >= SECT_FIRST, "sanity"); 686 new_capacity[n - 1] += padding; 687 } 688 } 689 690 csize_t exp = sect->size(); // 100% increase 691 if ((uint)exp < 4*K) exp = 4*K; // minimum initial increase 692 if (sect == which_cs) { 693 if (exp < amount) exp = amount; 694 if (StressCodeBuffers) exp = amount; // expand only slightly 695 } else if (n == SECT_INSTS) { 696 // scale down inst increases to a more modest 25% 697 exp = 4*K + ((exp - 4*K) >> 2); 698 if (StressCodeBuffers) exp = amount / 2; // expand only slightly 699 } else if (sect->is_empty()) { 700 // do not grow an empty secondary section 701 exp = 0; 702 } 703 // Allow for inter-section slop: 704 exp += CodeSection::end_slop(); 705 csize_t new_cap = sect->size() + exp; 706 if (new_cap < sect->capacity()) { 707 // No need to expand after all. 708 new_cap = sect->capacity(); 709 } 710 new_capacity[n] = new_cap; 711 new_total_cap += new_cap; 712 } 713 714 return new_total_cap; 715 } 716 717 void CodeBuffer::expand(CodeSection* which_cs, csize_t amount) { 718 #ifndef PRODUCT 719 if (PrintNMethods && (WizardMode || Verbose)) { 720 tty->print("expanding CodeBuffer:"); 721 this->print(); 722 } 723 724 if (StressCodeBuffers && blob() != NULL) { 725 static int expand_count = 0; 726 if (expand_count >= 0) expand_count += 1; 727 if (expand_count > 100 && is_power_of_2(expand_count)) { 728 tty->print_cr("StressCodeBuffers: have expanded %d times", expand_count); 729 // simulate an occasional allocation failure: 730 free_blob(); 731 } 732 } 733 #endif //PRODUCT 734 735 // Resizing must be allowed 736 { 737 if (blob() == NULL) return; // caller must check for blob == NULL 738 for (int n = 0; n < (int)SECT_LIMIT; n++) { 739 guarantee(!code_section(n)->is_frozen(), "resizing not allowed when frozen"); 740 } 741 } 742 743 // Figure new capacity for each section. 744 csize_t new_capacity[SECT_LIMIT]; 745 csize_t new_total_cap 746 = figure_expanded_capacities(which_cs, amount, new_capacity); 747 748 // Create a new (temporary) code buffer to hold all the new data 749 CodeBuffer cb(name(), new_total_cap, 0); 750 if (cb.blob() == NULL) { 751 // Failed to allocate in code cache. 752 free_blob(); 753 return; 754 } 755 756 // Create an old code buffer to remember which addresses used to go where. 757 // This will be useful when we do final assembly into the code cache, 758 // because we will need to know how to warp any internal address that 759 // has been created at any time in this CodeBuffer's past. 760 CodeBuffer* bxp = new CodeBuffer(_total_start, _total_size); 761 bxp->take_over_code_from(this); // remember the old undersized blob 762 DEBUG_ONLY(this->_blob = NULL); // silence a later assert 763 bxp->_before_expand = this->_before_expand; 764 this->_before_expand = bxp; 765 766 // Give each section its required (expanded) capacity. 767 for (int n = (int)SECT_LIMIT-1; n >= SECT_FIRST; n--) { 768 CodeSection* cb_sect = cb.code_section(n); 769 CodeSection* this_sect = code_section(n); 770 if (new_capacity[n] == 0) continue; // already nulled out 771 if (n != SECT_INSTS) { 772 cb.initialize_section_size(cb_sect, new_capacity[n]); 773 } 774 assert(cb_sect->capacity() >= new_capacity[n], "big enough"); 775 address cb_start = cb_sect->start(); 776 cb_sect->set_end(cb_start + this_sect->size()); 777 if (this_sect->mark() == NULL) { 778 cb_sect->clear_mark(); 779 } else { 780 cb_sect->set_mark(cb_start + this_sect->mark_off()); 781 } 782 } 783 784 // Move all the code and relocations to the new blob: 785 relocate_code_to(&cb); 786 787 // Copy the temporary code buffer into the current code buffer. 788 // Basically, do {*this = cb}, except for some control information. 789 this->take_over_code_from(&cb); 790 cb.set_blob(NULL); 791 792 // Zap the old code buffer contents, to avoid mistakenly using them. 793 debug_only(Copy::fill_to_bytes(bxp->_total_start, bxp->_total_size, 794 badCodeHeapFreeVal)); 795 796 _decode_begin = NULL; // sanity 797 798 // Make certain that the new sections are all snugly inside the new blob. 799 assert(verify_section_allocation(), "expanded allocation is ship-shape"); 800 801 #ifndef PRODUCT 802 if (PrintNMethods && (WizardMode || Verbose)) { 803 tty->print("expanded CodeBuffer:"); 804 this->print(); 805 } 806 #endif //PRODUCT 807 } 808 809 void CodeBuffer::take_over_code_from(CodeBuffer* cb) { 810 // Must already have disposed of the old blob somehow. 811 assert(blob() == NULL, "must be empty"); 812 #ifdef ASSERT 813 814 #endif 815 // Take the new blob away from cb. 816 set_blob(cb->blob()); 817 // Take over all the section pointers. 818 for (int n = 0; n < (int)SECT_LIMIT; n++) { 819 CodeSection* cb_sect = cb->code_section(n); 820 CodeSection* this_sect = code_section(n); 821 this_sect->take_over_code_from(cb_sect); 822 } 823 _overflow_arena = cb->_overflow_arena; 824 // Make sure the old cb won't try to use it or free it. 825 DEBUG_ONLY(cb->_blob = (BufferBlob*)badAddress); 826 } 827 828 #ifdef ASSERT 829 bool CodeBuffer::verify_section_allocation() { 830 address tstart = _total_start; 831 if (tstart == badAddress) return true; // smashed by set_blob(NULL) 832 address tend = tstart + _total_size; 833 if (_blob != NULL) { 834 assert(tstart >= _blob->content_begin(), "sanity"); 835 assert(tend <= _blob->content_end(), "sanity"); 836 } 837 // Verify disjointness. 838 for (int n = (int) SECT_FIRST; n < (int) SECT_LIMIT; n++) { 839 CodeSection* sect = code_section(n); 840 if (!sect->is_allocated() || sect->is_empty()) continue; 841 assert((intptr_t)sect->start() % sect->alignment() == 0 842 || sect->is_empty() || _blob == NULL, 843 "start is aligned"); 844 for (int m = (int) SECT_FIRST; m < (int) SECT_LIMIT; m++) { 845 CodeSection* other = code_section(m); 846 if (!other->is_allocated() || other == sect) continue; 847 assert(!other->contains(sect->start() ), "sanity"); 848 // limit is an exclusive address and can be the start of another 849 // section. 850 assert(!other->contains(sect->limit() - 1), "sanity"); 851 } 852 assert(sect->end() <= tend, "sanity"); 853 } 854 return true; 855 } 856 #endif //ASSERT 857 858 #ifndef PRODUCT 859 860 void CodeSection::dump() { 861 address ptr = start(); 862 for (csize_t step; ptr < end(); ptr += step) { 863 step = end() - ptr; 864 if (step > jintSize * 4) step = jintSize * 4; 865 tty->print(PTR_FORMAT ": ", ptr); 866 while (step > 0) { 867 tty->print(" " PTR32_FORMAT, *(jint*)ptr); 868 ptr += jintSize; 869 } 870 tty->cr(); 871 } 872 } 873 874 875 void CodeSection::decode() { 876 Disassembler::decode(start(), end()); 877 } 878 879 880 void CodeBuffer::block_comment(intptr_t offset, const char * comment) { 881 _comments.add_comment(offset, comment); 882 } 883 884 885 class CodeComment: public CHeapObj { 886 private: 887 friend class CodeComments; 888 intptr_t _offset; 889 const char * _comment; 890 CodeComment* _next; 891 892 ~CodeComment() { 893 assert(_next == NULL, "wrong interface for freeing list"); 894 os::free((void*)_comment); 895 } 896 897 public: 898 CodeComment(intptr_t offset, const char * comment) { 899 _offset = offset; 900 _comment = os::strdup(comment); 901 _next = NULL; 902 } 903 904 intptr_t offset() const { return _offset; } 905 const char * comment() const { return _comment; } 906 CodeComment* next() { return _next; } 907 908 void set_next(CodeComment* next) { _next = next; } 909 910 CodeComment* find(intptr_t offset) { 911 CodeComment* a = this; 912 while (a != NULL && a->_offset != offset) { 913 a = a->_next; 914 } 915 return a; 916 } 917 }; 918 919 920 void CodeComments::add_comment(intptr_t offset, const char * comment) { 921 CodeComment* c = new CodeComment(offset, comment); 922 CodeComment* insert = NULL; 923 if (_comments != NULL) { 924 CodeComment* c = _comments->find(offset); 925 insert = c; 926 while (c && c->offset() == offset) { 927 insert = c; 928 c = c->next(); 929 } 930 } 931 if (insert) { 932 // insert after comments with same offset 933 c->set_next(insert->next()); 934 insert->set_next(c); 935 } else { 936 c->set_next(_comments); 937 _comments = c; 938 } 939 } 940 941 942 void CodeComments::assign(CodeComments& other) { 943 assert(_comments == NULL, "don't overwrite old value"); 944 _comments = other._comments; 945 } 946 947 948 void CodeComments::print_block_comment(outputStream* stream, intptr_t offset) { 949 if (_comments != NULL) { 950 CodeComment* c = _comments->find(offset); 951 while (c && c->offset() == offset) { 952 stream->bol(); 953 stream->print(" ;; "); 954 stream->print_cr(c->comment()); 955 c = c->next(); 956 } 957 } 958 } 959 960 961 void CodeComments::free() { 962 CodeComment* n = _comments; 963 while (n) { 964 // unlink the node from the list saving a pointer to the next 965 CodeComment* p = n->_next; 966 n->_next = NULL; 967 delete n; 968 n = p; 969 } 970 _comments = NULL; 971 } 972 973 974 975 void CodeBuffer::decode() { 976 Disassembler::decode(decode_begin(), insts_end()); 977 _decode_begin = insts_end(); 978 } 979 980 981 void CodeBuffer::skip_decode() { 982 _decode_begin = insts_end(); 983 } 984 985 986 void CodeBuffer::decode_all() { 987 for (int n = 0; n < (int)SECT_LIMIT; n++) { 988 // dump contents of each section 989 CodeSection* cs = code_section(n); 990 tty->print_cr("! %s:", code_section_name(n)); 991 if (cs != consts()) 992 cs->decode(); 993 else 994 cs->dump(); 995 } 996 } 997 998 999 void CodeSection::print(const char* name) { 1000 csize_t locs_size = locs_end() - locs_start(); 1001 tty->print_cr(" %7s.code = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d)%s", 1002 name, start(), end(), limit(), size(), capacity(), 1003 is_frozen()? " [frozen]": ""); 1004 tty->print_cr(" %7s.locs = " PTR_FORMAT " : " PTR_FORMAT " : " PTR_FORMAT " (%d of %d) point=%d", 1005 name, locs_start(), locs_end(), locs_limit(), locs_size, locs_capacity(), locs_point_off()); 1006 if (PrintRelocations) { 1007 RelocIterator iter(this); 1008 iter.print(); 1009 } 1010 } 1011 1012 void CodeBuffer::print() { 1013 if (this == NULL) { 1014 tty->print_cr("NULL CodeBuffer pointer"); 1015 return; 1016 } 1017 1018 tty->print_cr("CodeBuffer:"); 1019 for (int n = 0; n < (int)SECT_LIMIT; n++) { 1020 // print each section 1021 CodeSection* cs = code_section(n); 1022 cs->print(code_section_name(n)); 1023 } 1024 } 1025 1026 #endif // PRODUCT