1 /*
   2  * Copyright (c) 2004, 2010, 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 "memory/resourceArea.hpp"
  28 #include "prims/jniFastGetField.hpp"
  29 #include "prims/jvm_misc.hpp"
  30 #include "runtime/jfieldIDWorkaround.hpp"
  31 #include "runtime/safepoint.hpp"
  32 
  33 #define __ masm->
  34 
  35 #define BUFFER_SIZE 30*wordSize
  36 
  37 // Instead of issuing lfence for LoadLoad barrier, we create data dependency
  38 // between loads, which is more efficient than lfence.
  39 
  40 // Common register usage:
  41 // rax/xmm0: result
  42 // c_rarg0:    jni env
  43 // c_rarg1:    obj
  44 // c_rarg2:    jfield id
  45 
  46 static const Register robj          = r9;
  47 static const Register rcounter      = r10;
  48 static const Register roffset       = r11;
  49 static const Register rcounter_addr = r11;
  50 
  51 // Warning: do not use rip relative addressing after the first counter load
  52 // since that may scratch r10!
  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     default:        ShouldNotReachHere();
  64   }
  65   ResourceMark rm;
  66   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
  67   CodeBuffer cbuf(blob);
  68   MacroAssembler* masm = new MacroAssembler(&cbuf);
  69   address fast_entry = __ pc();
  70 
  71   Label slow;
  72 
  73   ExternalAddress counter(SafepointSynchronize::safepoint_counter_addr());
  74   __ mov32 (rcounter, counter);
  75   __ mov   (robj, c_rarg1);
  76   __ testb (rcounter, 1);
  77   __ jcc (Assembler::notZero, slow);
  78   if (os::is_MP()) {
  79     __ xorptr(robj, rcounter);
  80     __ xorptr(robj, rcounter);                   // obj, since
  81                                                 // robj ^ rcounter ^ rcounter == robj
  82                                                 // robj is data dependent on rcounter.
  83   }
  84   __ movptr(robj, Address(robj, 0));             // *obj
  85   __ mov   (roffset, c_rarg2);
  86   __ shrptr(roffset, jfieldIDWorkaround::offset_shift); // offset
  87 
  88   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
  89   speculative_load_pclist[count] = __ pc();
  90   switch (type) {
  91     case T_BOOLEAN: __ movzbl (rax, Address(robj, roffset, Address::times_1)); break;
  92     case T_BYTE:    __ movsbl (rax, Address(robj, roffset, Address::times_1)); break;
  93     case T_CHAR:    __ movzwl (rax, Address(robj, roffset, Address::times_1)); break;
  94     case T_SHORT:   __ movswl (rax, Address(robj, roffset, Address::times_1)); break;
  95     case T_INT:     __ movl   (rax, Address(robj, roffset, Address::times_1)); break;
  96     case T_LONG:    __ movq   (rax, Address(robj, roffset, Address::times_1)); break;
  97     default:        ShouldNotReachHere();
  98   }
  99 
 100   if (os::is_MP()) {
 101     __ lea(rcounter_addr, counter);
 102     // ca is data dependent on rax.
 103     __ xorptr(rcounter_addr, rax);
 104     __ xorptr(rcounter_addr, rax);
 105     __ cmpl (rcounter, Address(rcounter_addr, 0));
 106   } else {
 107     __ cmp32 (rcounter, counter);
 108   }
 109   __ jcc (Assembler::notEqual, slow);
 110 
 111   __ ret (0);
 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();
 123   }
 124   // tail call
 125   __ jump (ExternalAddress(slow_case_addr));
 126 
 127   __ flush ();
 128 
 129   return fast_entry;
 130 }
 131 
 132 address JNI_FastGetField::generate_fast_get_boolean_field() {
 133   return generate_fast_get_int_field0(T_BOOLEAN);
 134 }
 135 
 136 address JNI_FastGetField::generate_fast_get_byte_field() {
 137   return generate_fast_get_int_field0(T_BYTE);
 138 }
 139 
 140 address JNI_FastGetField::generate_fast_get_char_field() {
 141   return generate_fast_get_int_field0(T_CHAR);
 142 }
 143 
 144 address JNI_FastGetField::generate_fast_get_short_field() {
 145   return generate_fast_get_int_field0(T_SHORT);
 146 }
 147 
 148 address JNI_FastGetField::generate_fast_get_int_field() {
 149   return generate_fast_get_int_field0(T_INT);
 150 }
 151 
 152 address JNI_FastGetField::generate_fast_get_long_field() {
 153   return generate_fast_get_int_field0(T_LONG);
 154 }
 155 
 156 address JNI_FastGetField::generate_fast_get_float_field0(BasicType type) {
 157   const char *name;
 158   switch (type) {
 159     case T_FLOAT:     name = "jni_fast_GetFloatField";     break;
 160     case T_DOUBLE:    name = "jni_fast_GetDoubleField";    break;
 161     default:          ShouldNotReachHere();
 162   }
 163   ResourceMark rm;
 164   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE);
 165   CodeBuffer cbuf(blob);
 166   MacroAssembler* masm = new MacroAssembler(&cbuf);
 167   address fast_entry = __ pc();
 168 
 169   Label slow;
 170 
 171   ExternalAddress counter(SafepointSynchronize::safepoint_counter_addr());
 172   __ mov32 (rcounter, counter);
 173   __ mov   (robj, c_rarg1);
 174   __ testb (rcounter, 1);
 175   __ jcc (Assembler::notZero, slow);
 176   if (os::is_MP()) {
 177     __ xorptr(robj, rcounter);
 178     __ xorptr(robj, rcounter);                   // obj, since
 179                                                 // robj ^ rcounter ^ rcounter == robj
 180                                                 // robj is data dependent on rcounter.
 181   }
 182   __ movptr(robj, Address(robj, 0));             // *obj
 183   __ mov   (roffset, c_rarg2);
 184   __ shrptr(roffset, jfieldIDWorkaround::offset_shift); // offset
 185 
 186   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
 187   speculative_load_pclist[count] = __ pc();
 188   switch (type) {
 189     case T_FLOAT:  __ movflt (xmm0, Address(robj, roffset, Address::times_1)); break;
 190     case T_DOUBLE: __ movdbl (xmm0, Address(robj, roffset, Address::times_1)); break;
 191     default:        ShouldNotReachHere();
 192   }
 193 
 194   if (os::is_MP()) {
 195     __ lea(rcounter_addr, counter);
 196     __ movdq (rax, xmm0);
 197     // counter address is data dependent on xmm0.
 198     __ xorptr(rcounter_addr, rax);
 199     __ xorptr(rcounter_addr, rax);
 200     __ cmpl (rcounter, Address(rcounter_addr, 0));
 201   } else {
 202     __ cmp32 (rcounter, counter);
 203   }
 204   __ jcc (Assembler::notEqual, slow);
 205 
 206   __ ret (0);
 207 
 208   slowcase_entry_pclist[count++] = __ pc();
 209   __ bind (slow);
 210   address slow_case_addr;
 211   switch (type) {
 212     case T_FLOAT:     slow_case_addr = jni_GetFloatField_addr();  break;
 213     case T_DOUBLE:    slow_case_addr = jni_GetDoubleField_addr();
 214   }
 215   // tail call
 216   __ jump (ExternalAddress(slow_case_addr));
 217 
 218   __ flush ();
 219 
 220   return fast_entry;
 221 }
 222 
 223 address JNI_FastGetField::generate_fast_get_float_field() {
 224   return generate_fast_get_float_field0(T_FLOAT);
 225 }
 226 
 227 address JNI_FastGetField::generate_fast_get_double_field() {
 228   return generate_fast_get_float_field0(T_DOUBLE);
 229 }