1 /*
   2  * Copyright (c) 1997, 2013, 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 "asm/macroAssembler.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "asm/codeBuffer.hpp"
  29 #include "runtime/atomic.inline.hpp"
  30 #include "runtime/icache.hpp"
  31 #include "runtime/os.hpp"
  32 
  33 
  34 // Implementation of AbstractAssembler
  35 //
  36 // The AbstractAssembler is generating code into a CodeBuffer. To make code generation faster,
  37 // the assembler keeps a copy of the code buffers boundaries & modifies them when
  38 // emitting bytes rather than using the code buffers accessor functions all the time.
  39 // The code buffer is updated via set_code_end(...) after emitting a whole instruction.
  40 
  41 AbstractAssembler::AbstractAssembler(CodeBuffer* code) {
  42   if (code == NULL)  return;
  43   CodeSection* cs = code->insts();
  44   cs->clear_mark();   // new assembler kills old mark
  45   if (cs->start() == NULL)  {
  46     vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "CodeCache: no room for %s", code->name());
  47   }
  48   _code_section = cs;
  49   _oop_recorder= code->oop_recorder();
  50   DEBUG_ONLY( _short_branch_delta = 0; )
  51 }
  52 
  53 void AbstractAssembler::set_code_section(CodeSection* cs) {
  54   assert(cs->outer() == code_section()->outer(), "sanity");
  55   assert(cs->is_allocated(), "need to pre-allocate this section");
  56   cs->clear_mark();  // new assembly into this section kills old mark
  57   _code_section = cs;
  58 }
  59 
  60 // Inform CodeBuffer that incoming code and relocation will be for stubs
  61 address AbstractAssembler::start_a_stub(int required_space) {
  62   CodeBuffer*  cb = code();
  63   CodeSection* cs = cb->stubs();
  64   assert(_code_section == cb->insts(), "not in insts?");
  65   if (cs->maybe_expand_to_ensure_remaining(required_space)
  66       && cb->blob() == NULL) {
  67     return NULL;
  68   }
  69   set_code_section(cs);
  70   return pc();
  71 }
  72 
  73 // Inform CodeBuffer that incoming code and relocation will be code
  74 // Should not be called if start_a_stub() returned NULL
  75 void AbstractAssembler::end_a_stub() {
  76   assert(_code_section == code()->stubs(), "not in stubs?");
  77   set_code_section(code()->insts());
  78 }
  79 
  80 // Inform CodeBuffer that incoming code and relocation will be for stubs
  81 address AbstractAssembler::start_a_const(int required_space, int required_align) {
  82   CodeBuffer*  cb = code();
  83   CodeSection* cs = cb->consts();
  84   assert(_code_section == cb->insts() || _code_section == cb->stubs(), "not in insts/stubs?");
  85   address end = cs->end();
  86   int pad = -(intptr_t)end & (required_align-1);
  87   if (cs->maybe_expand_to_ensure_remaining(pad + required_space)) {
  88     if (cb->blob() == NULL)  return NULL;
  89     end = cs->end();  // refresh pointer
  90   }
  91   if (pad > 0) {
  92     while (--pad >= 0) { *end++ = 0; }
  93     cs->set_end(end);
  94   }
  95   set_code_section(cs);
  96   return end;
  97 }
  98 
  99 // Inform CodeBuffer that incoming code and relocation will be code
 100 // in section cs (insts or stubs).
 101 void AbstractAssembler::end_a_const(CodeSection* cs) {
 102   assert(_code_section == code()->consts(), "not in consts?");
 103   set_code_section(cs);
 104 }
 105 
 106 void AbstractAssembler::flush() {
 107   ICache::invalidate_range(addr_at(0), offset());
 108 }
 109 
 110 void AbstractAssembler::bind(Label& L) {
 111   if (L.is_bound()) {
 112     // Assembler can bind a label more than once to the same place.
 113     guarantee(L.loc() == locator(), "attempt to redefine label");
 114     return;
 115   }
 116   L.bind_loc(locator());
 117   L.patch_instructions((MacroAssembler*)this);
 118 }
 119 
 120 void AbstractAssembler::generate_stack_overflow_check(int frame_size_in_bytes) {
 121   if (UseStackBanging) {
 122     // Each code entry causes one stack bang n pages down the stack where n
 123     // is configurable by StackShadowPages.  The setting depends on the maximum
 124     // depth of VM call stack or native before going back into java code,
 125     // since only java code can raise a stack overflow exception using the
 126     // stack banging mechanism.  The VM and native code does not detect stack
 127     // overflow.
 128     // The code in JavaCalls::call() checks that there is at least n pages
 129     // available, so all entry code needs to do is bang once for the end of
 130     // this shadow zone.
 131     // The entry code may need to bang additional pages if the framesize
 132     // is greater than a page.
 133 
 134     const int page_size = os::vm_page_size();
 135     int bang_end = StackShadowPages * page_size;
 136 
 137     // This is how far the previous frame's stack banging extended.
 138     const int bang_end_safe = bang_end;
 139 
 140     if (frame_size_in_bytes > page_size) {
 141       bang_end += frame_size_in_bytes;
 142     }
 143 
 144     int bang_offset = bang_end_safe;
 145     while (bang_offset <= bang_end) {
 146       // Need at least one stack bang at end of shadow zone.
 147       bang_stack_with_offset(bang_offset);
 148       bang_offset += page_size;
 149     }
 150   } // end (UseStackBanging)
 151 }
 152 
 153 void Label::add_patch_at(CodeBuffer* cb, int branch_loc) {
 154   assert(_loc == -1, "Label is unbound");
 155   if (_patch_index < PatchCacheSize) {
 156     _patches[_patch_index] = branch_loc;
 157   } else {
 158     if (_patch_overflow == NULL) {
 159       _patch_overflow = cb->create_patch_overflow();
 160     }
 161     _patch_overflow->push(branch_loc);
 162   }
 163   ++_patch_index;
 164 }
 165 
 166 void Label::patch_instructions(MacroAssembler* masm) {
 167   assert(is_bound(), "Label is bound");
 168   CodeBuffer* cb = masm->code();
 169   int target_sect = CodeBuffer::locator_sect(loc());
 170   address target = cb->locator_address(loc());
 171   while (_patch_index > 0) {
 172     --_patch_index;
 173     int branch_loc;
 174     if (_patch_index >= PatchCacheSize) {
 175       branch_loc = _patch_overflow->pop();
 176     } else {
 177       branch_loc = _patches[_patch_index];
 178     }
 179     int branch_sect = CodeBuffer::locator_sect(branch_loc);
 180     address branch = cb->locator_address(branch_loc);
 181     if (branch_sect == CodeBuffer::SECT_CONSTS) {
 182       // The thing to patch is a constant word.
 183       *(address*)branch = target;
 184       continue;
 185     }
 186 
 187 #ifdef ASSERT
 188     // Cross-section branches only work if the
 189     // intermediate section boundaries are frozen.
 190     if (target_sect != branch_sect) {
 191       for (int n = MIN2(target_sect, branch_sect),
 192                nlimit = (target_sect + branch_sect) - n;
 193            n < nlimit; n++) {
 194         CodeSection* cs = cb->code_section(n);
 195         assert(cs->is_frozen(), "cross-section branch needs stable offsets");
 196       }
 197     }
 198 #endif //ASSERT
 199 
 200     // Push the target offset into the branch instruction.
 201     masm->pd_patch_instruction(branch, target);
 202   }
 203 }
 204 
 205 struct DelayedConstant {
 206   typedef void (*value_fn_t)();
 207   BasicType type;
 208   intptr_t value;
 209   value_fn_t value_fn;
 210   // This limit of 20 is generous for initial uses.
 211   // The limit needs to be large enough to store the field offsets
 212   // into classes which do not have statically fixed layouts.
 213   // (Initial use is for method handle object offsets.)
 214   // Look for uses of "delayed_value" in the source code
 215   // and make sure this number is generous enough to handle all of them.
 216   enum { DC_LIMIT = 20 };
 217   static DelayedConstant delayed_constants[DC_LIMIT];
 218   static DelayedConstant* add(BasicType type, value_fn_t value_fn);
 219   bool match(BasicType t, value_fn_t cfn) {
 220     return type == t && value_fn == cfn;
 221   }
 222   static void update_all();
 223 };
 224 
 225 DelayedConstant DelayedConstant::delayed_constants[DC_LIMIT];
 226 // Default C structure initialization rules have the following effect here:
 227 // = { { (BasicType)0, (intptr_t)NULL }, ... };
 228 
 229 DelayedConstant* DelayedConstant::add(BasicType type,
 230                                       DelayedConstant::value_fn_t cfn) {
 231   for (int i = 0; i < DC_LIMIT; i++) {
 232     DelayedConstant* dcon = &delayed_constants[i];
 233     if (dcon->match(type, cfn))
 234       return dcon;
 235     if (dcon->value_fn == NULL) {
 236       // (cmpxchg not because this is multi-threaded but because I'm paranoid)
 237       if (Atomic::cmpxchg_ptr(CAST_FROM_FN_PTR(void*, cfn), &dcon->value_fn, NULL) == NULL) {
 238         dcon->type = type;
 239         return dcon;
 240       }
 241     }
 242   }
 243   // If this assert is hit (in pre-integration testing!) then re-evaluate
 244   // the comment on the definition of DC_LIMIT.
 245   guarantee(false, "too many delayed constants");
 246   return NULL;
 247 }
 248 
 249 void DelayedConstant::update_all() {
 250   for (int i = 0; i < DC_LIMIT; i++) {
 251     DelayedConstant* dcon = &delayed_constants[i];
 252     if (dcon->value_fn != NULL && dcon->value == 0) {
 253       typedef int     (*int_fn_t)();
 254       typedef address (*address_fn_t)();
 255       switch (dcon->type) {
 256       case T_INT:     dcon->value = (intptr_t) ((int_fn_t)    dcon->value_fn)(); break;
 257       case T_ADDRESS: dcon->value = (intptr_t) ((address_fn_t)dcon->value_fn)(); break;
 258       }
 259     }
 260   }
 261 }
 262 
 263 RegisterOrConstant AbstractAssembler::delayed_value(int(*value_fn)(), Register tmp, int offset) {
 264   intptr_t val = (intptr_t) (*value_fn)();
 265   if (val != 0)  return val + offset;
 266   return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset);
 267 }
 268 RegisterOrConstant AbstractAssembler::delayed_value(address(*value_fn)(), Register tmp, int offset) {
 269   intptr_t val = (intptr_t) (*value_fn)();
 270   if (val != 0)  return val + offset;
 271   return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset);
 272 }
 273 intptr_t* AbstractAssembler::delayed_value_addr(int(*value_fn)()) {
 274   DelayedConstant* dcon = DelayedConstant::add(T_INT, (DelayedConstant::value_fn_t) value_fn);
 275   return &dcon->value;
 276 }
 277 intptr_t* AbstractAssembler::delayed_value_addr(address(*value_fn)()) {
 278   DelayedConstant* dcon = DelayedConstant::add(T_ADDRESS, (DelayedConstant::value_fn_t) value_fn);
 279   return &dcon->value;
 280 }
 281 void AbstractAssembler::update_delayed_values() {
 282   DelayedConstant::update_all();
 283 }
 284 
 285 void AbstractAssembler::block_comment(const char* comment) {
 286   if (sect() == CodeBuffer::SECT_INSTS) {
 287     code_section()->outer()->block_comment(offset(), comment);
 288   }
 289 }
 290 
 291 const char* AbstractAssembler::code_string(const char* str) {
 292   if (sect() == CodeBuffer::SECT_INSTS || sect() == CodeBuffer::SECT_STUBS) {
 293     return code_section()->outer()->code_string(str);
 294   }
 295   return NULL;
 296 }
 297 
 298 bool MacroAssembler::needs_explicit_null_check(intptr_t offset) {
 299   // Exception handler checks the nmethod's implicit null checks table
 300   // only when this method returns false.
 301 #ifdef _LP64
 302   if (UseCompressedOops && Universe::narrow_oop_base() != NULL) {
 303     assert (Universe::heap() != NULL, "java heap should be initialized");
 304     // The first page after heap_base is unmapped and
 305     // the 'offset' is equal to [heap_base + offset] for
 306     // narrow oop implicit null checks.
 307     uintptr_t base = (uintptr_t)Universe::narrow_oop_base();
 308     if ((uintptr_t)offset >= base) {
 309       // Normalize offset for the next check.
 310       offset = (intptr_t)(pointer_delta((void*)offset, (void*)base, 1));
 311     }
 312   }
 313 #endif
 314   return offset < 0 || os::vm_page_size() <= offset;
 315 }