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