2005-07-21 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / metadata / domain.c
index 0f020a69e9b6a574d01dac1eed6683700c0b8800..a90020ec35d95f7a4fb20f37f476f08ccd65a63a 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <mono/os/gc_wrapper.h>
 
+#include <mono/utils/mono-compiler.h>
 #include <mono/metadata/object.h>
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/domain-internals.h>
@@ -40,7 +41,7 @@
 static guint32 appdomain_thread_id = -1;
  
 #ifdef HAVE_KW_THREAD
-static __thread MonoDomain * tls_appdomain;
+static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
 #define GET_APPDOMAIN() tls_appdomain
 #define SET_APPDOMAIN(x) do { \
        tls_appdomain = x; \
@@ -75,14 +76,29 @@ typedef struct {
        int startup_count;
 } AppConfigInfo;
 
-static MonoRuntimeInfo *current_runtime = NULL;
+/*
+ * AotModuleInfo: Contains information about AOT modules.
+ */
+typedef struct {
+       MonoImage *image;
+       gpointer start, end;
+} AotModuleInfo;
+
+static const MonoRuntimeInfo *current_runtime = NULL;
+
+static MonoJitInfoFindInAot jit_info_find_in_aot_func = NULL;
+
+/*
+ * Contains information about AOT loaded code.
+ */
+static MonoJitInfoTable *aot_modules = NULL;
 
 /* This is the list of runtime versions supported by this JIT.
  */
-static MonoRuntimeInfo supported_runtimes[] = {
+static const MonoRuntimeInfo supported_runtimes[] = {
        {"v1.0.3705", "1.0", { {1,0,5000,0}, {7,0,5000,0} }     },
        {"v1.1.4322", "1.0", { {1,0,5000,0}, {7,0,5000,0} }     },
-       {"v2.0.40607","2.0", { {2,0,3600,0}, {8,0,3600,0} }     }
+       {"v2.0.50215","2.0", { {2,0,3600,0}, {8,0,3600,0} }     }
 };
 
 
@@ -90,17 +106,30 @@ static MonoRuntimeInfo supported_runtimes[] = {
 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
 
 static void
-get_runtimes_from_exe (const char *exe_file, MonoRuntimeInfo** runtimes);
+get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes);
 
-static MonoRuntimeInfo*
+static const MonoRuntimeInfo*
 get_runtime_by_version (const char *version);
 
+static MonoImage*
+mono_jit_info_find_aot_module (guint8* addr);
+
 guint32
 mono_domain_get_tls_key (void)
 {
        return appdomain_thread_id;
 }
 
+gint32
+mono_domain_get_tls_offset (void)
+{
+       int offset = -1;
+       MONO_THREAD_VAR_OFFSET (tls_appdomain, offset);
+/*     __asm ("jmp 1f; .section writetext, \"awx\"; 1: movl $tls_appdomain@ntpoff, %0; jmp 2f; .previous; 2:" 
+               : "=r" (offset));*/
+       return offset;
+}
+
 static MonoJitInfoTable *
 mono_jit_info_table_new (void)
 {
@@ -139,20 +168,19 @@ MonoJitInfo *
 mono_jit_info_table_find (MonoDomain *domain, char *addr)
 {
        MonoJitInfoTable *table = domain->jit_info_table;
-       int left = 0, right;
+       MonoJitInfo *ji;
+       guint left = 0, right;
 
        mono_domain_lock (domain);
 
        right = table->len;
        while (left < right) {
-               int pos = (left + right) / 2;
-               MonoJitInfo *ji = g_array_index (table, gpointer, pos);
-               char *start = ji->code_start;
-               char *end = start + ji->code_size;
+               guint pos = (left + right) / 2;
+               ji = g_array_index (table, gpointer, pos);
 
-               if (addr < start)
+               if (addr < (char*)ji->code_start)
                        right = pos;
-               else if (addr >= end
+               else if (addr >= (char*)ji->code_start + ji->code_size
                        left = pos + 1;
                else {
                        mono_domain_unlock (domain);
@@ -162,10 +190,18 @@ mono_jit_info_table_find (MonoDomain *domain, char *addr)
        mono_domain_unlock (domain);
 
        /* maybe it is shared code, so we also search in the root domain */
+       ji = NULL;
        if (domain != mono_root_domain)
-               return mono_jit_info_table_find (mono_root_domain, addr);
+               ji = mono_jit_info_table_find (mono_root_domain, addr);
 
-       return NULL;
+       if (ji == NULL) {
+               /* Maybe its an AOT module */
+               MonoImage *image = mono_jit_info_find_aot_module (addr);
+               if (image)
+                       ji = jit_info_find_in_aot_func (domain, image, addr);
+       }
+       
+       return ji;
 }
 
 void
@@ -191,12 +227,96 @@ mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
 
        mono_domain_lock (domain);
        pos = mono_jit_info_table_index (table, start);
+       if (g_array_index (table, gpointer, pos) != ji) {
+               MonoJitInfo *ji2 = g_array_index (table, gpointer, pos);
+               g_assert (ji == ji2);
+       }
        g_assert (g_array_index (table, gpointer, pos) == ji);
 
        g_array_remove_index (table, pos);
        mono_domain_unlock (domain);
 }      
 
+static int
+aot_info_table_index (MonoJitInfoTable *table, char *addr)
+{
+       int left = 0, right = table->len;
+
+       while (left < right) {
+               int pos = (left + right) / 2;
+               AotModuleInfo *ainfo = g_array_index (table, gpointer, pos);
+               char *start = ainfo->start;
+               char *end = ainfo->end;
+
+               if (addr < start)
+                       right = pos;
+               else if (addr >= end) 
+                       left = pos + 1;
+               else
+                       return pos;
+       }
+
+       return left;
+}
+
+void
+mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end)
+{
+       AotModuleInfo *ainfo = g_new0 (AotModuleInfo, 1);
+       int pos;
+
+       ainfo->image = image;
+       ainfo->start = start;
+       ainfo->end = end;
+
+       EnterCriticalSection (&appdomains_mutex);
+
+       if (!aot_modules)
+               aot_modules = mono_jit_info_table_new ();
+
+       pos = aot_info_table_index (aot_modules, start);
+
+       g_array_insert_val (aot_modules, pos, ainfo);
+
+       LeaveCriticalSection (&appdomains_mutex);
+}
+
+static MonoImage*
+mono_jit_info_find_aot_module (guint8* addr)
+{
+       guint left = 0, right;
+
+       if (!aot_modules)
+               return NULL;
+
+       EnterCriticalSection (&appdomains_mutex);
+
+       right = aot_modules->len;
+       while (left < right) {
+               guint pos = (left + right) / 2;
+               AotModuleInfo *ai = g_array_index (aot_modules, gpointer, pos);
+
+               if (addr < (guint8*)ai->start)
+                       right = pos;
+               else if (addr >= (guint8*)ai->end)
+                       left = pos + 1;
+               else {
+                       LeaveCriticalSection (&appdomains_mutex);
+                       return ai->image;
+               }
+       }
+
+       LeaveCriticalSection (&appdomains_mutex);
+
+       return NULL;
+}
+
+void
+mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func)
+{
+       jit_info_find_in_aot_func = func;
+}
+
 gboolean
 mono_string_equal (MonoString *s1, MonoString *s2)
 {
@@ -226,6 +346,29 @@ mono_string_hash (MonoString *s)
        return h;       
 }
 
+static gboolean
+mono_ptrarray_equal (gpointer *s1, gpointer *s2)
+{
+       int len = GPOINTER_TO_INT (s1 [0]);
+       if (len != GPOINTER_TO_INT (s2 [0]))
+               return FALSE;
+
+       return memcmp (s1 + 1, s2 + 1, len * sizeof(gpointer)) == 0; 
+}
+
+static guint
+mono_ptrarray_hash (gpointer *s)
+{
+       int i;
+       int len = GPOINTER_TO_INT (s [0]);
+       guint hash = 0;
+       
+       for (i = 1; i < len; i++)
+               hash += GPOINTER_TO_UINT (s [i]);
+
+       return hash;    
+}
+
 /*
  * Allocate an id for domain and set domain->domain_id.
  * LOCKING: must be called while holding appdomains_mutex.
@@ -292,7 +435,7 @@ mono_domain_create (void)
        domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
        domain->domain_assemblies = NULL;
        domain->class_vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
+       domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_ptrarray_hash, (GCompareFunc)mono_ptrarray_equal);
        domain->static_data_hash = mono_g_hash_table_new (mono_aligned_addr_hash, NULL);
        domain->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
        domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
@@ -303,6 +446,7 @@ mono_domain_create (void)
        domain->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
 
        InitializeCriticalSection (&domain->lock);
+       InitializeCriticalSection (&domain->assemblies_lock);
 
        EnterCriticalSection (&appdomains_mutex);
        domain_id_alloc (domain);
@@ -330,7 +474,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        static MonoDomain *domain = NULL;
        MonoAssembly *ass = NULL;
        MonoImageOpenStatus status = MONO_IMAGE_OK;
-       MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
+       const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
        int n;
 
        if (domain)
@@ -365,7 +509,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        }
 
        if (runtimes [0] == NULL) {
-               MonoRuntimeInfo *default_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
+               const MonoRuntimeInfo *default_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
                runtimes [0] = default_runtime;
                runtimes [1] = NULL;
                g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
@@ -605,7 +749,17 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
 
        mono_defaults.runtimesecurityframe_class = mono_class_from_name (
                mono_defaults.corlib, "System.Security", "RuntimeSecurityFrame");
-       g_assert (mono_defaults.runtimesecurityframe_class != 0);
+
+       mono_defaults.executioncontext_class = mono_class_from_name (
+               mono_defaults.corlib, "System.Threading", "ExecutionContext");
+
+       /*
+        * Note that mono_defaults.generic_array_class is only non-NULL if we're
+        * using the 2.0 corlib.
+        */
+       mono_class_init (mono_defaults.array_class);
+       mono_defaults.generic_array_class = mono_class_from_name (
+               mono_defaults.corlib, "System", "Array/InternalArray`1");
 
        domain->friendly_name = g_path_get_basename (filename);
 
@@ -673,7 +827,7 @@ mono_get_root_domain (void)
  *
  * Returns: the current domain.
  */
-inline MonoDomain *
+MonoDomain *
 mono_domain_get ()
 {
        return GET_APPDOMAIN ();
@@ -685,7 +839,7 @@ mono_domain_get ()
  *
  * Sets the current domain to @domain.
  */
-inline void
+void
 mono_domain_set_internal (MonoDomain *domain)
 {
        SET_APPDOMAIN (domain);
@@ -730,15 +884,15 @@ mono_domain_assembly_open (MonoDomain *domain, const char *name)
        MonoAssembly *ass;
        GSList *tmp;
 
-       mono_domain_lock (domain);
+       mono_domain_assemblies_lock (domain);
        for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
                ass = tmp->data;
                if (strcmp (name, ass->aname.name) == 0) {
-                       mono_domain_unlock (domain);
+                       mono_domain_assemblies_unlock (domain);
                        return ass;
                }
        }
-       mono_domain_unlock (domain);
+       mono_domain_assemblies_unlock (domain);
 
        if (!(ass = mono_assembly_open (name, NULL)))
                return NULL;
@@ -848,6 +1002,7 @@ mono_domain_free (MonoDomain *domain, gboolean force)
                g_hash_table_destroy (domain->special_static_fields);
                domain->special_static_fields = NULL;
        }
+       DeleteCriticalSection (&domain->assemblies_lock);
        DeleteCriticalSection (&domain->lock);
        domain->setup = NULL;
 
@@ -1136,7 +1291,7 @@ app_config_free (AppConfigInfo* app_config)
 }
 
 
-static MonoRuntimeInfo*
+static const MonoRuntimeInfo*
 get_runtime_by_version (const char *version)
 {
        int n;
@@ -1150,12 +1305,12 @@ get_runtime_by_version (const char *version)
 }
 
 static void
-get_runtimes_from_exe (const char *exe_file, MonoRuntimeInfo** runtimes)
+get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes)
 {
        AppConfigInfo* app_config;
        char *version;
        char *config_name;
-       MonoRuntimeInfo* runtime = NULL;
+       const MonoRuntimeInfo* runtime = NULL;
        MonoImage *image = NULL;
        
        config_name = g_strconcat (exe_file, ".config", NULL);
@@ -1209,11 +1364,11 @@ get_runtimes_from_exe (const char *exe_file, MonoRuntimeInfo** runtimes)
 
 
 /**
- * mono_get_framework_assembly_version:
+ * mono_get_runtime_info:
  *
  * Returns: the version of the current runtime instance.
  */
-MonoRuntimeInfo*
+const MonoRuntimeInfo*
 mono_get_runtime_info (void)
 {
        return current_runtime;
@@ -1222,8 +1377,8 @@ mono_get_runtime_info (void)
 gchar *
 mono_debugger_check_runtime_version (const char *filename)
 {
-       MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
-       MonoRuntimeInfo *rinfo;
+       const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
+       const MonoRuntimeInfo *rinfo;
 
        get_runtimes_from_exe (filename, runtimes);
        rinfo = runtimes [0];