1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * Copyright (c) 2015-2018, Azul Systems, Inc. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "c1/c1_CodeStubs.hpp"
  30 #include "c1/c1_FrameMap.hpp"
  31 #include "c1/c1_LIRAssembler.hpp"
  32 #include "c1/c1_MacroAssembler.hpp"
  33 #include "c1/c1_Runtime1.hpp"
  34 #include "nativeInst_aarch32.hpp"
  35 #include "runtime/sharedRuntime.hpp"
  36 #include "vmreg_aarch32.inline.hpp"
  37 
  38 
  39 #define __ ce->masm()->
  40 
  41 #define should_not_reach_here() should_not_reach_here_line(__FILE__, __LINE__)
  42 
  43 void CounterOverflowStub::emit_code(LIR_Assembler* ce) {
  44   __ bind(_entry);
  45   Metadata *m = _method->as_constant_ptr()->as_metadata();
  46   __ mov_metadata(rscratch1, m);
  47   ce->store_parameter(rscratch1, 1);
  48   ce->store_parameter(_bci, 0);
  49   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::counter_overflow_id)));
  50   ce->add_call_info_here(_info);
  51   ce->verify_oop_map(_info);
  52   __ b(_continuation);
  53 }
  54 
  55 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index, LIR_Opr array)
  56   : _throw_index_out_of_bounds_exception(false), _index(index), _array(array) {
  57   assert(info != NULL, "must have info");
  58   _info = new CodeEmitInfo(info);
  59 }
  60 
  61 RangeCheckStub::RangeCheckStub(CodeEmitInfo* info, LIR_Opr index)
  62   : _throw_index_out_of_bounds_exception(true), _index(index), _array(NULL) {
  63   assert(info != NULL, "must have info");
  64   _info = new CodeEmitInfo(info);
  65 }
  66 
  67 void RangeCheckStub::emit_code(LIR_Assembler* ce) {
  68   __ bind(_entry);
  69   if (_info->deoptimize_on_exception()) {
  70     address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
  71     __ far_call(RuntimeAddress(a));
  72     ce->add_call_info_here(_info);
  73     ce->verify_oop_map(_info);
  74     debug_only(__ should_not_reach_here());
  75     return;
  76   }
  77 
  78   if (_index->is_cpu_register()) {
  79     __ mov(rscratch1, _index->as_register());
  80   } else {
  81     __ mov(rscratch1, _index->as_jint());
  82   }
  83   Runtime1::StubID stub_id;
  84   if (_throw_index_out_of_bounds_exception) {
  85     stub_id = Runtime1::throw_index_exception_id;
  86   } else {
  87     assert(_array != NULL, "sanity");
  88     __ mov(rscratch2, _array->as_pointer_register());
  89     stub_id = Runtime1::throw_range_check_failed_id;
  90   }
  91   __ far_call(RuntimeAddress(Runtime1::entry_for(stub_id)), NULL);
  92   ce->add_call_info_here(_info);
  93   ce->verify_oop_map(_info);
  94   debug_only(__ should_not_reach_here());
  95 }
  96 
  97 PredicateFailedStub::PredicateFailedStub(CodeEmitInfo* info) {
  98   _info = new CodeEmitInfo(info);
  99 }
 100 
 101 void PredicateFailedStub::emit_code(LIR_Assembler* ce) {
 102   __ bind(_entry);
 103   address a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 104   __ far_call(RuntimeAddress(a));
 105   ce->add_call_info_here(_info);
 106   ce->verify_oop_map(_info);
 107   debug_only(__ should_not_reach_here());
 108 }
 109 
 110 void DivByZeroStub::emit_code(LIR_Assembler* ce) {
 111   if (_offset != -1) {
 112     ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
 113   }
 114   __ bind(_entry);
 115   __ far_call(Address(Runtime1::entry_for(Runtime1::throw_div0_exception_id), relocInfo::runtime_call_type));
 116   ce->add_call_info_here(_info);
 117   ce->verify_oop_map(_info);
 118 #ifdef ASSERT
 119   __ should_not_reach_here();
 120 #endif
 121 }
 122 
 123 
 124 
 125 // Implementation of NewInstanceStub
 126 
 127 NewInstanceStub::NewInstanceStub(LIR_Opr klass_reg, LIR_Opr result, ciInstanceKlass* klass, CodeEmitInfo* info, Runtime1::StubID stub_id) {
 128   _result = result;
 129   _klass = klass;
 130   _klass_reg = klass_reg;
 131   _info = new CodeEmitInfo(info);
 132   assert(stub_id == Runtime1::new_instance_id                 ||
 133          stub_id == Runtime1::fast_new_instance_id            ||
 134          stub_id == Runtime1::fast_new_instance_init_check_id,
 135          "need new_instance id");
 136   _stub_id   = stub_id;
 137 }
 138 
 139 
 140 
 141 void NewInstanceStub::emit_code(LIR_Assembler* ce) {
 142   assert(__ rsp_offset() == 0, "frame size should be fixed");
 143   __ bind(_entry);
 144   __ mov(r3, _klass_reg->as_register());
 145   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub_id)));
 146   ce->add_call_info_here(_info);
 147   ce->verify_oop_map(_info);
 148   assert(_result->as_register() == r0, "result must in r0,");
 149   __ b(_continuation);
 150 }
 151 
 152 
 153 // Implementation of NewTypeArrayStub
 154 
 155 // Implementation of NewTypeArrayStub
 156 
 157 NewTypeArrayStub::NewTypeArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
 158   _klass_reg = klass_reg;
 159   _length = length;
 160   _result = result;
 161   _info = new CodeEmitInfo(info);
 162 }
 163 
 164 
 165 void NewTypeArrayStub::emit_code(LIR_Assembler* ce) {
 166   assert(__ rsp_offset() == 0, "frame size should be fixed");
 167   __ bind(_entry);
 168   assert(_length->as_register() == r6, "length must in r6,");
 169   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
 170   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_type_array_id)));
 171   ce->add_call_info_here(_info);
 172   ce->verify_oop_map(_info);
 173   assert(_result->as_register() == r0, "result must in r0");
 174   __ b(_continuation);
 175 }
 176 
 177 
 178 // Implementation of NewObjectArrayStub
 179 
 180 NewObjectArrayStub::NewObjectArrayStub(LIR_Opr klass_reg, LIR_Opr length, LIR_Opr result, CodeEmitInfo* info) {
 181   _klass_reg = klass_reg;
 182   _result = result;
 183   _length = length;
 184   _info = new CodeEmitInfo(info);
 185 }
 186 
 187 
 188 void NewObjectArrayStub::emit_code(LIR_Assembler* ce) {
 189   assert(__ rsp_offset() == 0, "frame size should be fixed");
 190   __ bind(_entry);
 191   assert(_length->as_register() == r6, "length must in r6");
 192   assert(_klass_reg->as_register() == r3, "klass_reg must in r3");
 193   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::new_object_array_id)));
 194   ce->add_call_info_here(_info);
 195   ce->verify_oop_map(_info);
 196   assert(_result->as_register() == r0, "result must in r0");
 197   __ b(_continuation);
 198 }
 199 // Implementation of MonitorAccessStubs
 200 
 201 MonitorEnterStub::MonitorEnterStub(LIR_Opr obj_reg, LIR_Opr lock_reg, CodeEmitInfo* info)
 202 : MonitorAccessStub(obj_reg, lock_reg)
 203 {
 204   _info = new CodeEmitInfo(info);
 205 }
 206 
 207 
 208 void MonitorEnterStub::emit_code(LIR_Assembler* ce) {
 209   assert(__ rsp_offset() == 0, "frame size should be fixed");
 210   __ bind(_entry);
 211   ce->store_parameter(_obj_reg->as_register(),  1);
 212   ce->store_parameter(_lock_reg->as_register(), 0);
 213   Runtime1::StubID enter_id;
 214   if (ce->compilation()->has_fpu_code()) {
 215     enter_id = Runtime1::monitorenter_id;
 216   } else {
 217     enter_id = Runtime1::monitorenter_nofpu_id;
 218   }
 219   __ far_call(RuntimeAddress(Runtime1::entry_for(enter_id)));
 220   ce->add_call_info_here(_info);
 221   ce->verify_oop_map(_info);
 222   __ b(_continuation);
 223 }
 224 
 225 
 226 void MonitorExitStub::emit_code(LIR_Assembler* ce) {
 227   __ bind(_entry);
 228   if (_compute_lock) {
 229     // lock_reg was destroyed by fast unlocking attempt => recompute it
 230     ce->monitor_address(_monitor_ix, _lock_reg);
 231   }
 232   ce->store_parameter(_lock_reg->as_register(), 0);
 233   // note: non-blocking leaf routine => no call info needed
 234   Runtime1::StubID exit_id;
 235   if (ce->compilation()->has_fpu_code()) {
 236     exit_id = Runtime1::monitorexit_id;
 237   } else {
 238     exit_id = Runtime1::monitorexit_nofpu_id;
 239   }
 240   __ adr(lr, _continuation);
 241   __ far_jump(RuntimeAddress(Runtime1::entry_for(exit_id)));
 242 }
 243 
 244 
 245 // Implementation of patching:
 246 // - Copy the code at given offset to an inlined buffer (first the bytes, then the number of bytes)
 247 // - Replace original code with a call to the stub
 248 // At Runtime:
 249 // - call to stub, jump to runtime
 250 // - in runtime: preserve all registers (rspecially objects, i.e., source and destination object)
 251 // - in runtime: after initializing class, restore original code, reexecute instruction
 252 
 253 int PatchingStub::_patch_info_offset = 0;
 254 
 255 void PatchingStub::align_patch_site(MacroAssembler* masm) {
 256 }
 257 
 258 void PatchingStub::emit_code(LIR_Assembler* ce) {
 259   // NativeCall::instruction_size is dynamically calculated based on CPU,
 260   // armv7 -> 3 instructions, armv6 -> 5 instructions. Initialize _patch_info_offset
 261   // here, when CPU is determined already.
 262   if (!_patch_info_offset)
 263     _patch_info_offset = -NativeCall::instruction_size;
 264   assert(_patch_info_offset == -NativeCall::instruction_size, "must not change");
 265   assert(NativeCall::instruction_size <= _bytes_to_copy && _bytes_to_copy <= 0xFF, "not enough room for call");
 266 
 267   Label call_patch;
 268 
 269   // static field accesses have special semantics while the class
 270   // initializer is being run so we emit a test which can be used to
 271   // check that this code is being executed by the initializing
 272   // thread.
 273   address being_initialized_entry = __ pc();
 274   if (CommentedAssembly) {
 275     __ block_comment(" patch template");
 276   }
 277   address start = __ pc();
 278   if (_id == load_klass_id) {
 279     // produce a copy of the load klass instruction for use by the being initialized case
 280     int metadata_index = -1;
 281     CodeSection* cs = __ code_section();
 282     RelocIterator iter(cs, (address)_pc_start, (address)_pc_start+1);
 283     while (iter.next()) {
 284       if (iter.type() == relocInfo::metadata_type) {
 285         metadata_Relocation* r = iter.metadata_reloc();
 286         assert(metadata_index == -1, "uninitalized yet");
 287         metadata_index = r->metadata_index();
 288         break;
 289       }
 290     }
 291     assert(metadata_index != -1, "initialized");
 292     __ relocate(metadata_Relocation::spec(metadata_index));
 293     __ patchable_load(_obj, __ pc());
 294     while ((intx) __ pc() - (intx) start < NativeCall::instruction_size) {
 295       __ nop();
 296     }
 297 #ifdef ASSERT
 298     for (int i = 0; i < _bytes_to_copy; i++) {
 299       assert(*(_pc_start + i) == *(start + i), "should be the same code");
 300     }
 301 #endif
 302   } else if (_id == load_mirror_id || _id == load_appendix_id) {
 303     // produce a copy of the load mirror instruction for use by the being
 304     // initialized case
 305     int oop_index = -1;
 306     CodeSection* cs = __ code_section();
 307     RelocIterator iter(cs, (address)_pc_start, (address)_pc_start+1);
 308     while (iter.next()) {
 309       if (iter.type() == relocInfo::oop_type) {
 310         oop_Relocation* r = iter.oop_reloc();
 311         assert(oop_index == -1, "uninitalized yet");
 312         oop_index = r->oop_index();
 313         break;
 314       }
 315     }
 316     assert(oop_index != -1, "initialized");
 317     __ relocate(oop_Relocation::spec(oop_index));
 318     __ patchable_load(_obj, __ pc());
 319     while ((intx) __ pc() - (intx) start < NativeCall::instruction_size) {
 320       __ nop();
 321     }
 322 #ifdef ASSERT
 323     for (int i = 0; i < _bytes_to_copy; i++) {
 324       assert(*(_pc_start + i) == *(start + i), "should be the same code");
 325     }
 326 #endif
 327   } else if (_id == access_field_id) {
 328     // make a copy the code which is going to be patched.
 329     address const_addr = (address) -1;
 330     CodeSection* cs = __ code_section();
 331     RelocIterator iter(cs, (address)_pc_start, (address)_pc_start+1);
 332     while (iter.next()) {
 333       if (iter.type() == relocInfo::section_word_type) {
 334         section_word_Relocation* r = iter.section_word_reloc();
 335         assert(const_addr == (address) -1, "uninitalized yet");
 336         const_addr = r->target();
 337         break;
 338       }
 339     }
 340     assert(const_addr != (address) -1, "initialized");
 341     __ relocate(section_word_Relocation::spec(const_addr, CodeBuffer::SECT_CONSTS));
 342     __ patchable_load(rscratch1, const_addr);
 343     while ((intx) __ pc() - (intx) start < NativeCall::instruction_size) {
 344       __ nop();
 345     }
 346 #ifdef ASSERT
 347     intptr_t* from = (intptr_t*) start;
 348     intptr_t* to = (intptr_t*) _pc_start;
 349     assert(from[0] == to[0], "should be same (nop)");
 350     assert(from[1] == to[1], "should be same (barrier)");
 351     //TODO: update
 352     //XXX: update nativeInst_aarch32..?
 353     #if 0
 354     assert(NativeFarLdr::from((address) (from + 2))->data_addr()
 355         == NativeFarLdr::from((address) (to + 2))->data_addr(),
 356         "should load from one addr)");
 357 #endif
 358     for (int i = 4 * NativeInstruction::arm_insn_sz; i < _bytes_to_copy; i++) {
 359       assert(*(_pc_start + i) == *(start + i), "should be the same code");
 360     }
 361 #endif
 362   } else {
 363     ShouldNotReachHere();
 364   }
 365 
 366   int bytes_to_skip = _bytes_to_copy;
 367 
 368   if (_id == load_mirror_id) {
 369     int offset = __ offset();
 370     if (CommentedAssembly) {
 371       __ block_comment(" being_initialized check");
 372     }
 373     assert(_obj != noreg, "must be a valid register");
 374     // Load without verification to keep code size small. We need it because
 375     // begin_initialized_entry_offset has to fit in a byte. Also, we know it's not null.
 376     __ ldr(rscratch1, Address(_obj, java_lang_Class::klass_offset_in_bytes()));
 377     __ ldr(rscratch1, Address(rscratch1, InstanceKlass::init_thread_offset()));
 378     __ cmp(rthread, rscratch1);
 379     __ b(call_patch, Assembler::NE);
 380 
 381     // access_field patches may execute the patched code before it's
 382     // copied back into place so we need to jump back into the main
 383     // code of the nmethod to continue execution.
 384     __ b(_patch_site_continuation);
 385     // make sure this extra code gets skipped
 386     bytes_to_skip += __ offset() - offset;
 387   }
 388 
 389   // Now emit the patch record telling the runtime how to find the
 390   // pieces of the patch.  We only need 3 bytes but it has to be
 391   // aligned as an instruction so emit 4 bytes.
 392   int sizeof_patch_record = 4;
 393   bytes_to_skip += sizeof_patch_record;
 394 
 395   // emit the offsets needed to find the code to patch
 396   int being_initialized_entry_offset = __ pc() - being_initialized_entry + sizeof_patch_record;
 397 
 398   __ emit_int8(0);
 399   __ emit_int8(being_initialized_entry_offset);
 400   __ emit_int8(bytes_to_skip);
 401   __ emit_int8(0);
 402 
 403   address patch_info_pc = __ pc();
 404 
 405   address entry = __ pc();
 406   NativeGeneralJump::insert_unconditional((address)_pc_start, entry);
 407   address target = NULL;
 408   relocInfo::relocType reloc_type = relocInfo::none;
 409   switch (_id) {
 410     case access_field_id:  target = Runtime1::entry_for(Runtime1::access_field_patching_id); reloc_type = relocInfo::section_word_type; break;
 411     case load_klass_id:    target = Runtime1::entry_for(Runtime1::load_klass_patching_id); reloc_type = relocInfo::metadata_type; break;
 412     case load_mirror_id:   target = Runtime1::entry_for(Runtime1::load_mirror_patching_id); reloc_type = relocInfo::oop_type; break;
 413     case load_appendix_id: target = Runtime1::entry_for(Runtime1::load_appendix_patching_id); reloc_type = relocInfo::oop_type; break;
 414     default: ShouldNotReachHere();
 415   }
 416   __ bind(call_patch);
 417 
 418   if (CommentedAssembly) {
 419     __ block_comment("patch entry point");
 420   }
 421   __ mov(rscratch1, RuntimeAddress(target));
 422   __ bl(rscratch1);
 423   // pad with nops to globally known upper bound of patch site size
 424   while (patch_info_pc - __ pc() < _patch_info_offset)
 425     __ nop();
 426   assert(_patch_info_offset == (patch_info_pc - __ pc()), "must not change, required by shared code");
 427   ce->add_call_info_here(_info);
 428   int jmp_off = __ offset();
 429   __ b(_patch_site_entry);
 430   // Add enough nops so deoptimization can overwrite the jmp above with a call
 431   // and not destroy the world.
 432   for (int j = __ offset() ; j < jmp_off + NativeCall::instruction_size; j += NativeInstruction::arm_insn_sz) {
 433     __ nop();
 434   }
 435 
 436   CodeSection* cs = __ code_section();
 437   RelocIterator iter(cs, (address)_pc_start, (address)_pc_start+1);
 438   relocInfo::change_reloc_info_for_address(&iter, (address)_pc_start, reloc_type, relocInfo::none);
 439 }
 440 
 441 
 442 void DeoptimizeStub::emit_code(LIR_Assembler* ce) {
 443   __ bind(_entry);
 444   ce->store_parameter(_trap_request, 0);
 445   __ far_call(RuntimeAddress(Runtime1::entry_for(Runtime1::deoptimize_id)));
 446   ce->add_call_info_here(_info);
 447   DEBUG_ONLY(__ should_not_reach_here());
 448 }
 449 
 450 
 451 void ImplicitNullCheckStub::emit_code(LIR_Assembler* ce) {
 452   address a;
 453   if (_info->deoptimize_on_exception()) {
 454     // Deoptimize, do not throw the exception, because it is probably wrong to do it here.
 455     a = Runtime1::entry_for(Runtime1::predicate_failed_trap_id);
 456   } else {
 457     a = Runtime1::entry_for(Runtime1::throw_null_pointer_exception_id);
 458   }
 459 
 460   ce->compilation()->implicit_exception_table()->append(_offset, __ offset());
 461   __ bind(_entry);
 462   __ far_call(RuntimeAddress(a));
 463   ce->add_call_info_here(_info);
 464   ce->verify_oop_map(_info);
 465   debug_only(__ should_not_reach_here());
 466 }
 467 
 468 
 469 void SimpleExceptionStub::emit_code(LIR_Assembler* ce) {
 470   assert(__ rsp_offset() == 0, "frame size should be fixed");
 471 
 472   __ bind(_entry);
 473   // pass the object in a scratch register because all other registers
 474   // must be preserved
 475   if (_obj->is_cpu_register()) {
 476     __ mov(rscratch1, _obj->as_register());
 477   }
 478   __ far_call(RuntimeAddress(Runtime1::entry_for(_stub)), NULL);
 479   ce->add_call_info_here(_info);
 480   debug_only(__ should_not_reach_here());
 481 }
 482 
 483 
 484 void ArrayCopyStub::emit_code(LIR_Assembler* ce) {
 485   //---------------slow case: call to native-----------------
 486   __ bind(_entry);
 487   // Figure out where the args should go
 488   // This should really convert the IntrinsicID to the Method* and signature
 489   // but I don't know how to do that.
 490   //
 491   VMRegPair args[5];
 492   BasicType signature[5] = { T_OBJECT, T_INT, T_OBJECT, T_INT, T_INT};
 493   SharedRuntime::java_calling_convention(signature, args, 5, true);
 494 
 495   // push parameters
 496   // (src, src_pos, dest, destPos, length)
 497   Register r[5];
 498   r[0] = src()->as_register();
 499   r[1] = src_pos()->as_register();
 500   r[2] = dst()->as_register();
 501   r[3] = dst_pos()->as_register();
 502   r[4] = length()->as_register();
 503 
 504   // next registers will get stored on the stack
 505   for (int i = 0; i < 5 ; i++ ) {
 506     VMReg r_1 = args[i].first();
 507     if (r_1->is_stack()) {
 508       int st_off = r_1->reg2stack() * wordSize;
 509       __ str (r[i], Address(sp, st_off));
 510     } else {
 511       assert(r[i] == args[i].first()->as_Register(), "Wrong register for arg ");
 512     }
 513   }
 514 
 515   ce->align_call(lir_static_call);
 516 
 517   ce->emit_static_call_stub();
 518   Address resolve(SharedRuntime::get_resolve_static_call_stub(),
 519                   relocInfo::static_call_type);
 520   __ trampoline_call(resolve);
 521   ce->add_call_info_here(info());
 522 
 523 #ifndef PRODUCT
 524   __ lea(rscratch2, ExternalAddress((address)&Runtime1::_arraycopy_slowcase_cnt));
 525   __ increment(Address(rscratch2));
 526 #endif
 527 
 528   __ b(_continuation);
 529 }
 530 
 531 #undef __