changeset 10475:3489047ffea2

Restructure the handling of HotSpotInstalledCode and their link to nmethods.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Fri, 21 Jun 2013 18:26:52 +0200
parents 55827d611da7
children cd68d6902328
files graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVM.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVMImpl.java graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotNmethod.java src/cpu/x86/vm/c2_globals_x86.hpp src/cpu/x86/vm/sharedRuntime_x86_64.cpp src/cpu/x86/vm/templateInterpreter_x86_64.cpp src/share/vm/classfile/vmSymbols.hpp src/share/vm/code/nmethod.cpp src/share/vm/graal/graalCompilerToVM.cpp src/share/vm/graal/graalJavaAccess.cpp src/share/vm/graal/graalRuntime.cpp src/share/vm/graal/graalVMToCompiler.cpp src/share/vm/graal/graalVMToCompiler.hpp src/share/vm/prims/nativeLookup.cpp
diffstat 14 files changed, 90 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVM.java	Fri Jun 21 17:52:19 2013 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVM.java	Fri Jun 21 18:26:52 2013 +0200
@@ -206,9 +206,9 @@
 
     StackTraceElement getStackTraceElement(long metaspaceMethod, int bci);
 
-    Object executeCompiledMethod(Object arg1, Object arg2, Object arg3, long nmethod) throws InvalidInstalledCodeException;
+    Object executeCompiledMethod(Object arg1, Object arg2, Object arg3, HotSpotInstalledCode hotspotInstalledCode) throws InvalidInstalledCodeException;
 
-    Object executeCompiledMethodVarargs(Object[] args, long nmethod) throws InvalidInstalledCodeException;
+    Object executeCompiledMethodVarargs(Object[] args, HotSpotInstalledCode hotspotInstalledCode) throws InvalidInstalledCodeException;
 
     int getVtableEntryOffset(long metaspaceMethod);
 
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVMImpl.java	Fri Jun 21 17:52:19 2013 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/CompilerToVMImpl.java	Fri Jun 21 18:26:52 2013 +0200
@@ -143,7 +143,7 @@
     public native StackTraceElement getStackTraceElement(long metaspaceMethod, int bci);
 
     @Override
-    public native Object executeCompiledMethodVarargs(Object[] args, long nmethod);
+    public native Object executeCompiledMethodVarargs(Object[] args, HotSpotInstalledCode hotspotInstalledCode);
 
     @Override
     public native int getVtableEntryOffset(long metaspaceMethod);
@@ -176,8 +176,8 @@
     public native boolean isInstalledCodeValid(long nativeMethod);
 
     @Override
-    public Object executeCompiledMethod(Object arg1, Object arg2, Object arg3, long nmethod) throws InvalidInstalledCodeException {
-        return executeCompiledMethodIntrinsic(arg1, arg2, arg3, nmethod);
+    public Object executeCompiledMethod(Object arg1, Object arg2, Object arg3, HotSpotInstalledCode hotspotInstalledCode) throws InvalidInstalledCodeException {
+        return executeCompiledMethodIntrinsic(arg1, arg2, arg3, hotspotInstalledCode);
     }
 
     /**
@@ -187,5 +187,5 @@
      * stub that does the necessary argument shuffling and a tail call via an indirect jump to the
      * verified entry point of the given native method.
      */
-    private static native Object executeCompiledMethodIntrinsic(Object arg1, Object arg2, Object arg3, long nmethod);
+    private static native Object executeCompiledMethodIntrinsic(Object arg1, Object arg2, Object arg3, HotSpotInstalledCode hotspotInstalledCode);
 }
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotNmethod.java	Fri Jun 21 17:52:19 2013 +0200
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/meta/HotSpotNmethod.java	Fri Jun 21 18:26:52 2013 +0200
@@ -68,7 +68,7 @@
 
     @Override
     public boolean isValid() {
-        return graalRuntime().getCompilerToVM().isInstalledCodeValid(codeBlob);
+        return codeBlob != 0;
     }
 
     @Override
@@ -87,7 +87,11 @@
         assert method.getSignature().getParameterKind(0) == Kind.Object;
         assert method.getSignature().getParameterKind(1) == Kind.Object;
         assert !Modifier.isStatic(method.getModifiers()) || method.getSignature().getParameterKind(2) == Kind.Object;
-        return graalRuntime().getCompilerToVM().executeCompiledMethod(arg1, arg2, arg3, codeBlob);
+        Object result = graalRuntime().getCompilerToVM().executeCompiledMethod(arg1, arg2, arg3, this);
+        if (isValid()) {
+            return result;
+        }
+        throw new InvalidInstalledCodeException();
     }
 
     private boolean checkArgs(Object... args) {
@@ -107,7 +111,11 @@
     @Override
     public Object executeVarargs(Object... args) throws InvalidInstalledCodeException {
         assert checkArgs(args);
-        return graalRuntime().getCompilerToVM().executeCompiledMethodVarargs(args, codeBlob);
+        Object result = graalRuntime().getCompilerToVM().executeCompiledMethodVarargs(args, this);
+        if (isValid()) {
+            return result;
+        }
+        throw new InvalidInstalledCodeException();
     }
 
     @Override
--- a/src/cpu/x86/vm/c2_globals_x86.hpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/cpu/x86/vm/c2_globals_x86.hpp	Fri Jun 21 18:26:52 2013 +0200
@@ -86,7 +86,11 @@
 define_pd_global(bool, OptoScheduling,               false);
 define_pd_global(bool, OptoBundling,                 false);
 
+#ifdef GRAAL
+define_pd_global(intx, ReservedCodeCacheSize,        64*M);
+#else
 define_pd_global(intx, ReservedCodeCacheSize,        48*M);
+#endif
 define_pd_global(uintx,CodeCacheMinBlockLength,      4);
 
 // Heap related flags
--- a/src/cpu/x86/vm/sharedRuntime_x86_64.cpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/cpu/x86/vm/sharedRuntime_x86_64.cpp	Fri Jun 21 18:26:52 2013 +0200
@@ -40,6 +40,9 @@
 #ifdef COMPILER2
 #include "opto/runtime.hpp"
 #endif
+#ifdef GRAAL
+#include "graal/graalJavaAccess.hpp"
+#endif
 
 #define __ masm->
 
@@ -1674,9 +1677,22 @@
   if (iid == vmIntrinsics::_CompilerToVMImpl_executeCompiledMethod) {
     // We are called from compiled code here. The three object arguments
     // are already in the correct registers (j_rarg0, jrarg1, jrarg2). The
-    // fourth argument (j_rarg3) is a raw pointer to the nmethod. Make a tail
-    // call to its verified entry point.
+    // fourth argument (j_rarg3) is a pointer to the HotSpotInstalledCode object.
+
+    // Load the nmethod pointer from the HotSpotInstalledCode object
+    __ movq(j_rarg3, Address(j_rarg3, sizeof(oopDesc)));
+
+    // Check whether the nmethod was invalidated
+    __ testq(j_rarg3, j_rarg3);
+    Label invalid_nmethod;
+    __ jcc(Assembler::zero, invalid_nmethod);
+
+    // Perform a tail call to the verified entry point of the nmethod.
     __ jmp(Address(j_rarg3, nmethod::verified_entry_point_offset()));
+
+    __ bind(invalid_nmethod);
+    __ xorq(rax, rax);
+    __ ret(0);
     return;
   }
 #endif
--- a/src/cpu/x86/vm/templateInterpreter_x86_64.cpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/cpu/x86/vm/templateInterpreter_x86_64.cpp	Fri Jun 21 18:26:52 2013 +0200
@@ -46,6 +46,9 @@
 #include "runtime/vframeArray.hpp"
 #include "utilities/debug.hpp"
 #include "utilities/macros.hpp"
+#ifdef GRAAL
+#include "graal/graalJavaAccess.hpp"
+#endif
 
 #define __ _masm->
 
@@ -908,20 +911,32 @@
 
   // Move first object argument from interpreter calling convention to compiled
   // code calling convention.
-  __ movq(j_rarg0, Address(r11, Interpreter::stackElementSize*5));
+  __ movq(j_rarg0, Address(r11, Interpreter::stackElementSize*4));
 
   // Move second object argument.
-  __ movq(j_rarg1, Address(r11, Interpreter::stackElementSize*4));
+  __ movq(j_rarg1, Address(r11, Interpreter::stackElementSize*3));
 
   // Move third object argument.
-  __ movq(j_rarg2, Address(r11, Interpreter::stackElementSize*3));
+  __ movq(j_rarg2, Address(r11, Interpreter::stackElementSize*2));
+
+  // Load the raw pointer to the HotSpotInstalledCode object.
+  __ movq(j_rarg3, Address(r11, Interpreter::stackElementSize));
 
-  // Load the raw pointer to the nmethod.
-  __ movq(j_rarg3, Address(r11, Interpreter::stackElementSize));
+  // Load the nmethod pointer from the HotSpotInstalledCode object
+  __ movq(j_rarg3, Address(j_rarg3, sizeof(oopDesc)));
+
+  // Check whether the nmethod was invalidated
+  __ testq(j_rarg3, j_rarg3);
+  Label invalid_nmethod;
+  __ jcc(Assembler::zero, invalid_nmethod);
 
   // Perform a tail call to the verified entry point of the nmethod.
   __ jmp(Address(j_rarg3, nmethod::verified_entry_point_offset()));
 
+  __ bind(invalid_nmethod);
+  __ xorq(rax, rax);
+  __ ret(0);
+
   return entry_point;
 }
 
--- a/src/share/vm/classfile/vmSymbols.hpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/share/vm/classfile/vmSymbols.hpp	Fri Jun 21 18:26:52 2013 +0200
@@ -348,6 +348,8 @@
   template(com_oracle_graal_api_code_VirtualObject,                  "com/oracle/graal/api/code/VirtualObject")                       \
   template(com_oracle_graal_api_code_RegisterSaveLayout,             "com/oracle/graal/api/code/RegisterSaveLayout")                  \
   template(com_oracle_graal_api_code_InvalidInstalledCodeException,  "com/oracle/graal/api/code/InvalidInstalledCodeException")       \
+  /* graal.truffle */                                                                                                                 \
+  template(com_oracle_graal_truffle_GraalTruffleRuntime,             "com/oracle/graal/truffle/GraalTruffleRuntime")                  \
   template(startCompiler_name,                    "startCompiler")                                                                    \
   template(bootstrap_name,                        "bootstrap")                                                                        \
   template(shutdownCompiler_name,                 "shutdownCompiler")                                                                 \
@@ -382,6 +384,7 @@
   template(getVMToCompiler_name,                  "getVMToCompiler")                                                                  \
   template(getVMToCompiler_signature,             "()Lcom/oracle/graal/hotspot/bridge/VMToCompiler;")                                 \
   template(getInstance_name,                      "getInstance")                                                                      \
+  template(getTruffleRuntimeInstance_signature,   "()Lcom/oracle/graal/truffle/GraalTruffleRuntime;")                                 \
   template(makeInstance_name,                     "makeInstance")                                                                     \
   template(initialize_name,                       "initialize")                                                                       \
   template(getInstance_signature,                 "()Lcom/oracle/graal/hotspot/HotSpotGraalRuntime;")                                 \
@@ -1145,7 +1148,7 @@
    do_name(     Double_valueOf_signature,                        "(D)Ljava/lang/Double;")                               \
                                                                                                                         \
   do_intrinsic(_CompilerToVMImpl_executeCompiledMethod,           com_oracle_graal_hotspot_bridge_CompilerToVMImpl, executeCompiledMethod_name, CompilerToVMImpl_executeCompiledMethod_signature, F_SN)\
-   do_name(     CompilerToVMImpl_executeCompiledMethod_signature, "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;J)Ljava/lang/Object;")                               \
+   do_name(     CompilerToVMImpl_executeCompiledMethod_signature, "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Lcom/oracle/graal/hotspot/meta/HotSpotInstalledCode;)Ljava/lang/Object;")                               \
    do_name(     executeCompiledMethod_name,                       "executeCompiledMethodIntrinsic")                     \
     /*end*/
 
--- a/src/share/vm/code/nmethod.cpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/share/vm/code/nmethod.cpp	Fri Jun 21 18:26:52 2013 +0200
@@ -1397,9 +1397,10 @@
     if (!is_osr_method() && !is_not_entrant()) {
       address stub = SharedRuntime::get_handle_wrong_method_stub();
 #ifdef GRAAL
-      if (_graal_installed_code != NULL && !HotSpotNmethod::isDefault(_graal_installed_code)) {
-        // This was manually installed machine code. Patch entry with stub that throws an exception.
-        stub = SharedRuntime::get_deoptimized_installed_code_stub();
+      if (_graal_installed_code != NULL) {
+        // Break the link between nmethod and HotSpotInstalledCode such that the nmethod can subsequently be flushed safely.
+        HotSpotInstalledCode::set_codeBlob(_graal_installed_code, 0);
+        _graal_installed_code = NULL;
       }
 #endif
       NativeJump::patch_verified_entry(entry_point(), verified_entry_point(), stub);
--- a/src/share/vm/graal/graalCompilerToVM.cpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/share/vm/graal/graalCompilerToVM.cpp	Fri Jun 21 18:26:52 2013 +0200
@@ -1004,10 +1004,11 @@
   return JNIHandles::make_local(element);
 C2V_END
 
-C2V_VMENTRY(jobject, executeCompiledMethodVarargs, (JNIEnv *env, jobject, jobject args, jlong nmethodValue))
+C2V_VMENTRY(jobject, executeCompiledMethodVarargs, (JNIEnv *env, jobject, jobject args, jobject hotspotInstalledCode))
   ResourceMark rm;
   HandleMark hm;
 
+  jlong nmethodValue = HotSpotInstalledCode::codeBlob(hotspotInstalledCode);
   nmethod* nm = (nmethod*) (address) nmethodValue;
   methodHandle mh = nm->method();
   Symbol* signature = mh->signature();
@@ -1203,7 +1204,6 @@
 #define METHOD_DATA           "Lcom/oracle/graal/hotspot/meta/HotSpotMethodData;"
 #define METASPACE_METHOD      "J"
 #define METASPACE_METHOD_DATA "J"
-#define NMETHOD               "J"
 
 JNINativeMethod CompilerToVM_methods[] = {
   {CC"initializeBytecode",            CC"("METASPACE_METHOD"[B)[B",                                     FN_PTR(initializeBytecode)},
@@ -1243,7 +1243,7 @@
   {CC"installCode0",                  CC"("HS_COMPILED_CODE HS_INSTALLED_CODE"[Z)I",                    FN_PTR(installCode0)},
   {CC"getCode",                       CC"(J)[B",                                                        FN_PTR(getCode)},
   {CC"disassembleCodeBlob",           CC"(J)"STRING,                                                    FN_PTR(disassembleCodeBlob)},
-  {CC"executeCompiledMethodVarargs",  CC"(["OBJECT NMETHOD")"OBJECT,                                    FN_PTR(executeCompiledMethodVarargs)},
+  {CC"executeCompiledMethodVarargs",  CC"(["OBJECT HS_INSTALLED_CODE")"OBJECT,                          FN_PTR(executeCompiledMethodVarargs)},
   {CC"getDeoptedLeafGraphIds",        CC"()[J",                                                         FN_PTR(getDeoptedLeafGraphIds)},
   {CC"getLineNumberTable",            CC"("HS_RESOLVED_METHOD")[J",                                     FN_PTR(getLineNumberTable)},
   {CC"getLocalVariableTable",         CC"("HS_RESOLVED_METHOD")["LOCAL,                                 FN_PTR(getLocalVariableTable)},
--- a/src/share/vm/graal/graalJavaAccess.cpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/share/vm/graal/graalJavaAccess.cpp	Fri Jun 21 18:26:52 2013 +0200
@@ -63,6 +63,7 @@
 
 void graal_compute_offsets() {
   COMPILER_CLASSES_DO(START_CLASS, END_CLASS, CHAR_FIELD, INT_FIELD, BOOLEAN_FIELD, LONG_FIELD, FLOAT_FIELD, OOP_FIELD, STATIC_OOP_FIELD)
+  guarantee(HotSpotInstalledCode::_codeBlob_offset == sizeof(oopDesc), "codeBlob must be first field!");
 }
 
 #define EMPTY0
--- a/src/share/vm/graal/graalRuntime.cpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/share/vm/graal/graalRuntime.cpp	Fri Jun 21 18:26:52 2013 +0200
@@ -474,3 +474,8 @@
 JVM_ENTRY(jobject, JVM_InitializeGraalRuntime(JNIEnv *env, jclass graalclass))
   return VMToCompiler::graalRuntimePermObject();
 JVM_END
+
+// JVM_InitializeTruffleRuntime
+JVM_ENTRY(jobject, JVM_InitializeTruffleRuntime(JNIEnv *env, jclass graalclass))
+  return JNIHandles::make_local(VMToCompiler::truffleRuntime()());
+JVM_END
--- a/src/share/vm/graal/graalVMToCompiler.cpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/share/vm/graal/graalVMToCompiler.cpp	Fri Jun 21 18:26:52 2013 +0200
@@ -46,6 +46,16 @@
   return _vmToCompilerPermKlass;
 }
 
+Handle VMToCompiler::truffleRuntime() {
+  Symbol* name = vmSymbols::com_oracle_graal_truffle_GraalTruffleRuntime();
+  KlassHandle klass = loadClass(name);
+
+  JavaValue result(T_OBJECT);
+  JavaCalls::call_static(&result, klass, vmSymbols::makeInstance_name(), vmSymbols::getTruffleRuntimeInstance_signature(), Thread::current());
+  check_pending_exception("Couldn't initialize GraalTruffleRuntime");
+  return Handle((oop) result.get_jobject());
+}
+
 Handle VMToCompiler::graalRuntime() {
   if (JNIHandles::resolve(_graalRuntimePermObject) == NULL) {
 #ifdef AMD64
@@ -292,3 +302,4 @@
 
 }
 
+
--- a/src/share/vm/graal/graalVMToCompiler.hpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/share/vm/graal/graalVMToCompiler.hpp	Fri Jun 21 18:26:52 2013 +0200
@@ -44,6 +44,7 @@
 
 public:
   static Handle graalRuntime();
+  static Handle truffleRuntime();
 
   static jobject graalRuntimePermObject() {
     graalRuntime();
--- a/src/share/vm/prims/nativeLookup.cpp	Fri Jun 21 17:52:19 2013 +0200
+++ b/src/share/vm/prims/nativeLookup.cpp	Fri Jun 21 18:26:52 2013 +0200
@@ -125,6 +125,7 @@
   void JNICALL JVM_RegisterWhiteBoxMethods(JNIEnv *env, jclass wbclass);
 #ifdef GRAAL
   jobject JNICALL JVM_InitializeGraalRuntime(JNIEnv *env, jclass graalclass);
+  jobject JNICALL JVM_InitializeTruffleRuntime(JNIEnv *env, jclass graalclass);
 #endif
 }
 
@@ -142,6 +143,7 @@
   { CC"Java_sun_hotspot_WhiteBox_registerNatives",                 NULL, FN_PTR(JVM_RegisterWhiteBoxMethods)     },
 #ifdef GRAAL
   { CC"Java_com_oracle_graal_api_runtime_Graal_initializeRuntime", NULL, FN_PTR(JVM_InitializeGraalRuntime)      },
+  { CC"Java_com_oracle_truffle_api_Truffle_initializeRuntime",     NULL, FN_PTR(JVM_InitializeTruffleRuntime)    },
 #endif
 };