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