1 /*
   2  * Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.hpp"
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shared/barrierSetAssembler.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "prims/jniFastGetField.hpp"
  32 #include "prims/jvm_misc.hpp"
  33 #include "runtime/safepoint.hpp"
  34 
  35 #define __ masm->
  36 
  37 #define BUFFER_SIZE 30*wordSize
  38 
  39 // Instead of issuing a LoadLoad barrier we create an address
  40 // dependency between loads; this might be more efficient.
  41 
  42 // Common register usage:
  43 // r0/v0:      result
  44 // c_rarg0:    jni env
  45 // c_rarg1:    obj
  46 // c_rarg2:    jfield id
  47 
  48 static const Register robj          = r3;
  49 static const Register rcounter      = r4;
  50 static const Register roffset       = r5;
  51 static const Register rcounter_addr = r6;
  52 static const Register result        = r7;
  53 
  54 address JNI_FastGetField::generate_fast_get_int_field0(BasicType type) {
  55   const char *name;
  56   switch (type) {
  57     case T_BOOLEAN: name = "jni_fast_GetBooleanField"; break;
  58     case T_BYTE:    name = "jni_fast_GetByteField";    break;
  59     case T_CHAR:    name = "jni_fast_GetCharField";    break;
  60     case T_SHORT:   name = "jni_fast_GetShortField";   break;
  61     case T_INT:     name = "jni_fast_GetIntField";     break;
  62     case T_LONG:    name = "jni_fast_GetLongField";    break;
  63     case T_FLOAT:   name = "jni_fast_GetFloatField";   break;
  64     case T_DOUBLE:  name = "jni_fast_GetDoubleField";  break;
  65     default:        ShouldNotReachHere();
  66       name = NULL;  // unreachable
  67   }
  68   ResourceMark rm;
  69   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
  70   CodeBuffer cbuf(blob);
  71   MacroAssembler* masm = new MacroAssembler(&cbuf);
  72   address fast_entry = __ pc();
  73 
  74   Label slow;
  75 
  76   unsigned long offset;
  77   __ adrp(rcounter_addr,
  78           SafepointSynchronize::safepoint_counter_addr(), offset);
  79   Address safepoint_counter_addr(rcounter_addr, offset);
  80   __ ldrw(rcounter, safepoint_counter_addr);
  81   __ tbnz(rcounter, 0, slow);
  82   __ eor(robj, c_rarg1, rcounter);
  83   __ eor(robj, robj, rcounter);               // obj, since
  84                                               // robj ^ rcounter ^ rcounter == robj
  85                                               // robj is address dependent on rcounter.
  86 
  87   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
  88   bs->try_resolve_jobject_in_native(masm, c_rarg0, robj, rscratch1, slow);
  89 
  90   __ lsr(roffset, c_rarg2, 2);                // offset
  91 
  92   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
  93   speculative_load_pclist[count] = __ pc();   // Used by the segfault handler
  94   __ access_load_at(type, IN_HEAP, noreg /* tos: r0/v0 */, Address(robj, roffset), noreg, noreg);
  95 
  96   // Ensure that field has finished loading before loading the safepoint_counter.
  97   switch (type) {
  98     case T_FLOAT:
  99     case T_DOUBLE:
 100       __ membar(MacroAssembler::LoadLoad);
 101       break;
 102     default:
 103       // counter_addr is address dependent on result.
 104       __ eor(rcounter_addr, rcounter_addr, r0);
 105       __ eor(rcounter_addr, rcounter_addr, r0);
 106   }
 107 
 108   __ ldrw(rscratch1, safepoint_counter_addr);
 109   __ cmpw(rcounter, rscratch1);
 110   __ br (Assembler::NE, slow);
 111   __ ret(lr);
 112 
 113   slowcase_entry_pclist[count++] = __ pc();
 114   __ bind(slow);
 115   address slow_case_addr;
 116   switch (type) {
 117     case T_BOOLEAN: slow_case_addr = jni_GetBooleanField_addr(); break;
 118     case T_BYTE:    slow_case_addr = jni_GetByteField_addr();    break;
 119     case T_CHAR:    slow_case_addr = jni_GetCharField_addr();    break;
 120     case T_SHORT:   slow_case_addr = jni_GetShortField_addr();   break;
 121     case T_INT:     slow_case_addr = jni_GetIntField_addr();     break;
 122     case T_LONG:    slow_case_addr = jni_GetLongField_addr();    break;
 123     case T_FLOAT:   slow_case_addr = jni_GetFloatField_addr();   break;
 124     case T_DOUBLE:  slow_case_addr = jni_GetDoubleField_addr();  break;
 125     default:        ShouldNotReachHere();
 126       slow_case_addr = NULL;  // unreachable
 127   }
 128 
 129   {
 130     __ enter();
 131     __ lea(rscratch1, ExternalAddress(slow_case_addr));
 132     __ blr(rscratch1);
 133     __ maybe_isb();
 134     __ leave();
 135     __ ret(lr);
 136   }
 137   __ flush ();
 138 
 139   return fast_entry;
 140 }
 141 
 142 address JNI_FastGetField::generate_fast_get_boolean_field() {
 143   return generate_fast_get_int_field0(T_BOOLEAN);
 144 }
 145 
 146 address JNI_FastGetField::generate_fast_get_byte_field() {
 147   return generate_fast_get_int_field0(T_BYTE);
 148 }
 149 
 150 address JNI_FastGetField::generate_fast_get_char_field() {
 151   return generate_fast_get_int_field0(T_CHAR);
 152 }
 153 
 154 address JNI_FastGetField::generate_fast_get_short_field() {
 155   return generate_fast_get_int_field0(T_SHORT);
 156 }
 157 
 158 address JNI_FastGetField::generate_fast_get_int_field() {
 159   return generate_fast_get_int_field0(T_INT);
 160 }
 161 
 162 address JNI_FastGetField::generate_fast_get_long_field() {
 163   return generate_fast_get_int_field0(T_LONG);
 164 }
 165 
 166 address JNI_FastGetField::generate_fast_get_float_field() {
 167   return generate_fast_get_int_field0(T_FLOAT);
 168 }
 169 
 170 address JNI_FastGetField::generate_fast_get_double_field() {
 171   return generate_fast_get_int_field0(T_DOUBLE);
 172 }