1 /*
   2  * Copyright (c) 2004, 2017, 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/safepoint.hpp"
  31 
  32 #define __ masm->
  33 
  34 #define BUFFER_SIZE 30
  35 
  36 #ifdef _WINDOWS
  37 GetBooleanField_t JNI_FastGetField::jni_fast_GetBooleanField_fp;
  38 GetByteField_t    JNI_FastGetField::jni_fast_GetByteField_fp;
  39 GetCharField_t    JNI_FastGetField::jni_fast_GetCharField_fp;
  40 GetShortField_t   JNI_FastGetField::jni_fast_GetShortField_fp;
  41 GetIntField_t     JNI_FastGetField::jni_fast_GetIntField_fp;
  42 GetLongField_t    JNI_FastGetField::jni_fast_GetLongField_fp;
  43 GetFloatField_t   JNI_FastGetField::jni_fast_GetFloatField_fp;
  44 GetDoubleField_t  JNI_FastGetField::jni_fast_GetDoubleField_fp;
  45 #endif
  46 
  47 // Instead of issuing lfence for LoadLoad barrier, we create data dependency
  48 // between loads, which is much more efficient than lfence.
  49 
  50 address JNI_FastGetField::generate_fast_get_int_field0(BasicType type) {
  51   const char *name = NULL;
  52   switch (type) {
  53     case T_BOOLEAN: name = "jni_fast_GetBooleanField"; break;
  54     case T_BYTE:    name = "jni_fast_GetByteField";    break;
  55     case T_CHAR:    name = "jni_fast_GetCharField";    break;
  56     case T_SHORT:   name = "jni_fast_GetShortField";   break;
  57     case T_INT:     name = "jni_fast_GetIntField";     break;
  58     default:        ShouldNotReachHere();
  59   }
  60   ResourceMark rm;
  61   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE*wordSize);
  62   CodeBuffer cbuf(blob);
  63   MacroAssembler* masm = new MacroAssembler(&cbuf);
  64   address fast_entry = __ pc();
  65 
  66   Label slow;
  67 
  68   // stack layout:    offset from rsp (in words):
  69   //  return pc        0
  70   //  jni env          1
  71   //  obj              2
  72   //  jfieldID         3
  73 
  74   ExternalAddress counter(SafepointSynchronize::safepoint_counter_addr());
  75   __ mov32 (rcx, counter);
  76   __ testb (rcx, 1);
  77   __ jcc (Assembler::notZero, slow);
  78   __ mov(rax, rcx);
  79   __ andptr(rax, 1);                         // rax, must end up 0
  80   __ movptr(rdx, Address(rsp, rax, Address::times_1, 2*wordSize));
  81                                             // obj, notice rax, is 0.
  82                                             // rdx is data dependent on rcx.
  83   __ movptr(rax, Address(rsp, 3*wordSize));  // jfieldID
  84 
  85   __ clear_jweak_tag(rdx);
  86 
  87   __ movptr(rdx, Address(rdx, 0));           // *obj
  88   __ shrptr (rax, 2);                         // offset
  89 
  90   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
  91   speculative_load_pclist[count] = __ pc();
  92   switch (type) {
  93     case T_BOOLEAN: __ movzbl (rax, Address(rdx, rax, Address::times_1)); break;
  94     case T_BYTE:    __ movsbl (rax, Address(rdx, rax, Address::times_1)); break;
  95     case T_CHAR:    __ movzwl (rax, Address(rdx, rax, Address::times_1)); break;
  96     case T_SHORT:   __ movswl (rax, Address(rdx, rax, Address::times_1)); break;
  97     case T_INT:     __ movl   (rax, Address(rdx, rax, Address::times_1)); break;
  98     default:        ShouldNotReachHere();
  99   }
 100 
 101   Address ca1;
 102   __ lea(rdx, counter);
 103   __ xorptr(rdx, rax);
 104   __ xorptr(rdx, rax);
 105   __ cmp32(rcx, Address(rdx, 0));
 106   // ca1 is the same as ca because
 107   // rax, ^ counter_addr ^ rax, = address
 108   // ca1 is data dependent on rax,.
 109   __ jcc (Assembler::notEqual, slow);
 110 
 111 #ifndef _WINDOWS
 112   __ ret (0);
 113 #else
 114   // __stdcall calling convention
 115   __ ret (3*wordSize);
 116 #endif
 117 
 118   slowcase_entry_pclist[count++] = __ pc();
 119   __ bind (slow);
 120   address slow_case_addr = NULL;
 121   switch (type) {
 122     case T_BOOLEAN: slow_case_addr = jni_GetBooleanField_addr(); break;
 123     case T_BYTE:    slow_case_addr = jni_GetByteField_addr();    break;
 124     case T_CHAR:    slow_case_addr = jni_GetCharField_addr();    break;
 125     case T_SHORT:   slow_case_addr = jni_GetShortField_addr();   break;
 126     case T_INT:     slow_case_addr = jni_GetIntField_addr();
 127   }
 128   // tail call
 129   __ jump (ExternalAddress(slow_case_addr));
 130 
 131   __ flush ();
 132 
 133 #ifndef _WINDOWS
 134   return fast_entry;
 135 #else
 136   switch (type) {
 137   case T_BOOLEAN: jni_fast_GetBooleanField_fp = (GetBooleanField_t) fast_entry; break;
 138   case T_BYTE:    jni_fast_GetByteField_fp    = (GetByteField_t)    fast_entry; break;
 139   case T_CHAR:    jni_fast_GetCharField_fp    = (GetCharField_t)    fast_entry; break;
 140   case T_SHORT:   jni_fast_GetShortField_fp   = (GetShortField_t)   fast_entry; break;
 141   case T_INT:     jni_fast_GetIntField_fp     = (GetIntField_t)     fast_entry; break;
 142   }
 143   return os::win32::fast_jni_accessor_wrapper(type);
 144 #endif
 145 }
 146 
 147 address JNI_FastGetField::generate_fast_get_boolean_field() {
 148   return generate_fast_get_int_field0(T_BOOLEAN);
 149 }
 150 
 151 address JNI_FastGetField::generate_fast_get_byte_field() {
 152   return generate_fast_get_int_field0(T_BYTE);
 153 }
 154 
 155 address JNI_FastGetField::generate_fast_get_char_field() {
 156   return generate_fast_get_int_field0(T_CHAR);
 157 }
 158 
 159 address JNI_FastGetField::generate_fast_get_short_field() {
 160   return generate_fast_get_int_field0(T_SHORT);
 161 }
 162 
 163 address JNI_FastGetField::generate_fast_get_int_field() {
 164   return generate_fast_get_int_field0(T_INT);
 165 }
 166 
 167 address JNI_FastGetField::generate_fast_get_long_field() {
 168   const char *name = "jni_fast_GetLongField";
 169   ResourceMark rm;
 170   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE*wordSize);
 171   CodeBuffer cbuf(blob);
 172   MacroAssembler* masm = new MacroAssembler(&cbuf);
 173   address fast_entry = __ pc();
 174 
 175   Label slow;
 176 
 177   // stack layout:    offset from rsp (in words):
 178   //  old rsi          0
 179   //  return pc        1
 180   //  jni env          2
 181   //  obj              3
 182   //  jfieldID         4
 183 
 184   ExternalAddress counter(SafepointSynchronize::safepoint_counter_addr());
 185 
 186   __ push  (rsi);
 187   __ mov32 (rcx, counter);
 188   __ testb (rcx, 1);
 189   __ jcc (Assembler::notZero, slow);
 190   __ mov(rax, rcx);
 191   __ andptr(rax, 1);                         // rax, must end up 0
 192   __ movptr(rdx, Address(rsp, rax, Address::times_1, 3*wordSize));
 193                                             // obj, notice rax, is 0.
 194                                             // rdx is data dependent on rcx.
 195   __ movptr(rsi, Address(rsp, 4*wordSize));  // jfieldID
 196 
 197   __ clear_jweak_tag(rdx);
 198 
 199   __ movptr(rdx, Address(rdx, 0));           // *obj
 200   __ shrptr(rsi, 2);                         // offset
 201 
 202   assert(count < LIST_CAPACITY-1, "LIST_CAPACITY too small");
 203   speculative_load_pclist[count++] = __ pc();
 204   __ movptr(rax, Address(rdx, rsi, Address::times_1));
 205 #ifndef _LP64
 206   speculative_load_pclist[count] = __ pc();
 207   __ movl(rdx, Address(rdx, rsi, Address::times_1, 4));
 208 #endif // _LP64
 209 
 210   __ lea(rsi, counter);
 211   __ xorptr(rsi, rdx);
 212   __ xorptr(rsi, rax);
 213   __ xorptr(rsi, rdx);
 214   __ xorptr(rsi, rax);
 215   __ cmp32(rcx, Address(rsi, 0));
 216   // ca1 is the same as ca because
 217   // rax, ^ rdx ^ counter_addr ^ rax, ^ rdx = address
 218   // ca1 is data dependent on both rax, and rdx.
 219   __ jcc (Assembler::notEqual, slow);
 220 
 221   __ pop (rsi);
 222 
 223 #ifndef _WINDOWS
 224   __ ret (0);
 225 #else
 226   // __stdcall calling convention
 227   __ ret (3*wordSize);
 228 #endif
 229 
 230   slowcase_entry_pclist[count-1] = __ pc();
 231   slowcase_entry_pclist[count++] = __ pc();
 232   __ bind (slow);
 233   __ pop  (rsi);
 234   address slow_case_addr = jni_GetLongField_addr();;
 235   // tail call
 236   __ jump (ExternalAddress(slow_case_addr));
 237 
 238   __ flush ();
 239 
 240 #ifndef _WINDOWS
 241   return fast_entry;
 242 #else
 243   jni_fast_GetLongField_fp = (GetLongField_t) fast_entry;
 244   return os::win32::fast_jni_accessor_wrapper(T_LONG);
 245 #endif
 246 }
 247 
 248 address JNI_FastGetField::generate_fast_get_float_field0(BasicType type) {
 249   const char *name = NULL;
 250   switch (type) {
 251     case T_FLOAT:  name = "jni_fast_GetFloatField";  break;
 252     case T_DOUBLE: name = "jni_fast_GetDoubleField"; break;
 253     default:       ShouldNotReachHere();
 254   }
 255   ResourceMark rm;
 256   BufferBlob* blob = BufferBlob::create(name, BUFFER_SIZE*wordSize);
 257   CodeBuffer cbuf(blob);
 258   MacroAssembler* masm = new MacroAssembler(&cbuf);
 259   address fast_entry = __ pc();
 260 
 261   Label slow_with_pop, slow;
 262 
 263   // stack layout:    offset from rsp (in words):
 264   //  return pc        0
 265   //  jni env          1
 266   //  obj              2
 267   //  jfieldID         3
 268 
 269   ExternalAddress counter(SafepointSynchronize::safepoint_counter_addr());
 270 
 271   __ mov32 (rcx, counter);
 272   __ testb (rcx, 1);
 273   __ jcc (Assembler::notZero, slow);
 274   __ mov(rax, rcx);
 275   __ andptr(rax, 1);                         // rax, must end up 0
 276   __ movptr(rdx, Address(rsp, rax, Address::times_1, 2*wordSize));
 277                                             // obj, notice rax, is 0.
 278                                             // rdx is data dependent on rcx.
 279   __ movptr(rax, Address(rsp, 3*wordSize));  // jfieldID
 280 
 281   __ clear_jweak_tag(rdx);
 282 
 283   __ movptr(rdx, Address(rdx, 0));           // *obj
 284   __ shrptr(rax, 2);                         // offset
 285 
 286   assert(count < LIST_CAPACITY, "LIST_CAPACITY too small");
 287   speculative_load_pclist[count] = __ pc();
 288   switch (type) {
 289 #ifndef _LP64
 290     case T_FLOAT:  __ fld_s (Address(rdx, rax, Address::times_1)); break;
 291     case T_DOUBLE: __ fld_d (Address(rdx, rax, Address::times_1)); break;
 292 #else
 293     case T_FLOAT:  __ movflt (xmm0, Address(robj, roffset, Address::times_1)); break;
 294     case T_DOUBLE: __ movdbl (xmm0, Address(robj, roffset, Address::times_1)); break;
 295 #endif // _LP64
 296     default:       ShouldNotReachHere();
 297   }
 298 
 299   Address ca1;
 300   __ fst_s (Address(rsp, -4));
 301   __ lea(rdx, counter);
 302   __ movl (rax, Address(rsp, -4));
 303   // garbage hi-order bits on 64bit are harmless.
 304   __ xorptr(rdx, rax);
 305   __ xorptr(rdx, rax);
 306   __ cmp32(rcx, Address(rdx, 0));
 307   // rax, ^ counter_addr ^ rax, = address
 308   // ca1 is data dependent on the field
 309   // access.
 310   __ jcc (Assembler::notEqual, slow_with_pop);
 311 
 312 #ifndef _WINDOWS
 313   __ ret (0);
 314 #else
 315   // __stdcall calling convention
 316   __ ret (3*wordSize);
 317 #endif
 318 
 319   __ bind (slow_with_pop);
 320   // invalid load. pop FPU stack.
 321   __ fstp_d (0);
 322 
 323   slowcase_entry_pclist[count++] = __ pc();
 324   __ bind (slow);
 325   address slow_case_addr = NULL;
 326   switch (type) {
 327     case T_FLOAT:  slow_case_addr = jni_GetFloatField_addr();  break;
 328     case T_DOUBLE: slow_case_addr = jni_GetDoubleField_addr(); break;
 329     default:       ShouldNotReachHere();
 330   }
 331   // tail call
 332   __ jump (ExternalAddress(slow_case_addr));
 333 
 334   __ flush ();
 335 
 336 #ifndef _WINDOWS
 337   return fast_entry;
 338 #else
 339   switch (type) {
 340   case T_FLOAT:  jni_fast_GetFloatField_fp  = (GetFloatField_t)  fast_entry; break;
 341   case T_DOUBLE: jni_fast_GetDoubleField_fp = (GetDoubleField_t) fast_entry; break;
 342   }
 343   return os::win32::fast_jni_accessor_wrapper(type);
 344 #endif
 345 }
 346 
 347 address JNI_FastGetField::generate_fast_get_float_field() {
 348   return generate_fast_get_float_field0(T_FLOAT);
 349 }
 350 
 351 address JNI_FastGetField::generate_fast_get_double_field() {
 352   return generate_fast_get_float_field0(T_DOUBLE);
 353 }