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