* roottypes.cs: Rename from tree.cs.
[mono.git] / mono / metadata / domain.c
index 58c57daf8baf4f48621f17b128e7eb5b51eca247..59098db4706daabf1952e0f5685c4ac0aa6ebe9c 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * domain.c: MonoDomain functions
  *
@@ -16,6 +15,8 @@
 
 #include <mono/os/gc_wrapper.h>
 
+#include <mono/utils/mono-compiler.h>
+#include <mono/utils/mono-logger.h>
 #include <mono/metadata/object.h>
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/domain-internals.h>
 #include <mono/metadata/cil-coff.h>
 #include <mono/metadata/rawbuffer.h>
 #include <mono/metadata/metadata-internals.h>
+#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/appdomain.h>
 #include <mono/metadata/mono-debug-debugger.h>
+#include <metadata/threads.h>
 
 /* #define DEBUG_DOMAIN_UNLOAD */
 
 /* we need to use both the Tls* functions and __thread because
- * the gc needs to see all the appcontext
+ * some archs may generate faster jit code with one meachanism
+ * or the other (we used to do it because tls slots were GC-tracked,
+ * but we can't depend on this).
  */
-static guint32 context_thread_id = -1;
 static guint32 appdomain_thread_id = -1;
  
 #ifdef HAVE_KW_THREAD
-static __thread MonoDomain * tls_appdomain;
-static __thread MonoAppContext * tls_appcontext;
+static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
 #define GET_APPDOMAIN() tls_appdomain
 #define SET_APPDOMAIN(x) do { \
        tls_appdomain = x; \
        TlsSetValue (appdomain_thread_id, x); \
 } while (FALSE)
 
-#define GET_APPCONTEXT() tls_appcontext
-#define SET_APPCONTEXT(x) do { \
-       tls_appcontext = x; \
-       TlsSetValue (context_thread_id, x); \
-} while (FALSE)
-
 #else
+
 #define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
 #define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
 
-#define GET_APPCONTEXT() ((MonoAppContext *)TlsGetValue (context_thread_id))
-#define SET_APPCONTEXT(x) TlsSetValue (context_thread_id, x);
 #endif
 
-static gint32 appdomain_id_counter = 0;
+#define GET_APPCONTEXT() (mono_thread_current ()->current_appcontext)
+#define SET_APPCONTEXT(x) MONO_OBJECT_SETREF (mono_thread_current (), current_appcontext, (x))
 
-static MonoGHashTable * appdomains_list = NULL;
+static guint16 appdomain_list_size = 0;
+static guint16 appdomain_next = 0;
+static MonoDomain **appdomains_list = NULL;
 
+#define mono_appdomains_lock() EnterCriticalSection (&appdomains_mutex)
+#define mono_appdomains_unlock() LeaveCriticalSection (&appdomains_mutex)
 static CRITICAL_SECTION appdomains_mutex;
 
 static MonoDomain *mono_root_domain = NULL;
@@ -76,31 +78,61 @@ 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[] = {
-       {"v1.0.3705", "1.0", 1,0,5000,0},
-       {"v1.1.4322", "1.0", 1,0,5000,0},
-       {"v2.0.40607","2.0", 2,0,3600,0} 
+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.50215","2.0", { {2,0,0,0},    {8,0,0,0} }        },
+       {"v2.0.50727","2.0", { {2,0,0,0},    {8,0,0,0} }        }
 };
 
+
 /* The stable runtime version */
 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
 
-static MonoRuntimeInfo*        
-get_runtime_from_exe (const char *exe_file);
+static void
+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 +171,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 +193,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 ((guint8*)addr);
+               if (image)
+                       ji = jit_info_find_in_aot_func (domain, image, addr);
+       }
+       
+       return ji;
 }
 
 void
@@ -191,13 +230,115 @@ 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 gboolean
+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;
+
+       mono_appdomains_lock ();
+
+       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);
+
+       mono_appdomains_unlock ();
+}
+
+static MonoImage*
+mono_jit_info_find_aot_module (guint8* addr)
+{
+       guint left = 0, right;
+
+       if (!aot_modules)
+               return NULL;
+
+       mono_appdomains_lock ();
+
+       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 {
+                       mono_appdomains_unlock ();
+                       return ai->image;
+               }
+       }
+
+       mono_appdomains_unlock ();
+
+       return NULL;
+}
+
+void
+mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func)
+{
+       jit_info_find_in_aot_func = func;
+}
+
+gpointer
+mono_jit_info_get_code_start (MonoJitInfo* ji)
+{
+       return ji->code_start;
+}
+
+int
+mono_jit_info_get_code_size (MonoJitInfo* ji)
+{
+       return ji->code_size;
+}
+
+MonoMethod*
+mono_jit_info_get_method (MonoJitInfo* ji)
+{
+       return ji->method;
+}
+
+gboolean
 mono_string_equal (MonoString *s1, MonoString *s2)
 {
        int l1 = mono_string_length (s1);
@@ -211,7 +352,7 @@ mono_string_equal (MonoString *s1, MonoString *s2)
        return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1 * 2) == 0; 
 }
 
-static guint
+guint
 mono_string_hash (MonoString *s)
 {
        const guint16 *p = mono_string_chars (s);
@@ -226,24 +367,99 @@ mono_string_hash (MonoString *s)
        return h;       
 }
 
-#if HAVE_BOEHM_GC
-static void
-domain_finalizer (void *obj, void *data) {
-       /*g_print ("domain finalized\n");*/
+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; 
 }
-#endif
+
+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.
+ * We try to assign low numbers to the domain, so it can be used
+ * as an index in data tables to lookup domain-specific info
+ * with minimal memory overhead. We also try not to reuse the
+ * same id too quickly (to help debugging).
+ */
+static int
+domain_id_alloc (MonoDomain *domain)
+{
+       int id = -1, i;
+       if (!appdomains_list) {
+               appdomain_list_size = 2;
+               appdomains_list = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
+       }
+       for (i = appdomain_next; i < appdomain_list_size; ++i) {
+               if (!appdomains_list [i]) {
+                       id = i;
+                       break;
+               }
+       }
+       if (id == -1) {
+               for (i = 0; i < appdomain_next; ++i) {
+                       if (!appdomains_list [i]) {
+                               id = i;
+                               break;
+                       }
+               }
+       }
+       if (id == -1) {
+               MonoDomain **new_list;
+               int new_size = appdomain_list_size * 2;
+               if (new_size >= (1 << 16))
+                       g_assert_not_reached ();
+               id = appdomain_list_size;
+               new_list = mono_gc_alloc_fixed (new_size * sizeof (void*), NULL);
+               memcpy (new_list, appdomains_list, appdomain_list_size * sizeof (void*));
+               mono_gc_free_fixed (appdomains_list);
+               appdomains_list = new_list;
+               appdomain_list_size = new_size;
+       }
+       domain->domain_id = id;
+       appdomains_list [id] = domain;
+       appdomain_next++;
+       if (appdomain_next > appdomain_list_size)
+               appdomain_next = 0;
+       return id;
+}
+
+static guint32 domain_gc_bitmap [sizeof(MonoDomain)/4/32 + 1];
+static gpointer domain_gc_desc = NULL;
 
 MonoDomain *
 mono_domain_create (void)
 {
        MonoDomain *domain;
 
-#if HAVE_BOEHM_GC
-       domain = GC_MALLOC (sizeof (MonoDomain));
-       GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
-#else
-       domain = g_new0 (MonoDomain, 1);
-#endif
+       mono_appdomains_lock ();
+       if (!domain_gc_desc) {
+               unsigned int i, bit = 0;
+               for (i = G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_OBJECT); i < G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_GC_TRACKED); i += sizeof (gpointer)) {
+                       bit = i / sizeof (gpointer);
+                       domain_gc_bitmap [bit / 32] |= 1 << (bit % 32);
+               }
+               domain_gc_desc = mono_gc_make_descr_from_bitmap (domain_gc_bitmap, bit + 1);
+       }
+       mono_appdomains_unlock ();
+
+       domain = mono_gc_alloc_fixed (sizeof (MonoDomain), domain_gc_desc);
        domain->domain = NULL;
        domain->setup = NULL;
        domain->friendly_name = NULL;
@@ -251,24 +467,26 @@ mono_domain_create (void)
 
        domain->mp = mono_mempool_new ();
        domain->code_mp = mono_code_manager_new ();
-       domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
+       domain->env = mono_g_hash_table_new_type ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal, MONO_HASH_KEY_VALUE_GC);
        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->static_data_hash = mono_g_hash_table_new (mono_aligned_addr_hash, NULL);
+       domain->proxy_vtable_hash = g_hash_table_new ((GHashFunc)mono_ptrarray_hash, (GCompareFunc)mono_ptrarray_equal);
+       domain->static_data_array = 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);
        domain->jit_info_table = mono_jit_info_table_new ();
        domain->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
        domain->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
        domain->finalizable_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
+       domain->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+       domain->delegate_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
 
        InitializeCriticalSection (&domain->lock);
+       InitializeCriticalSection (&domain->assemblies_lock);
 
-       EnterCriticalSection (&appdomains_mutex);
-       mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
-       LeaveCriticalSection (&appdomains_mutex);
+       mono_appdomains_lock ();
+       domain_id_alloc (domain);
+       mono_appdomains_unlock ();
 
        return domain;
 }
@@ -290,9 +508,10 @@ static MonoDomain *
 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
 {
        static MonoDomain *domain = NULL;
-       MonoAssembly *ass;
+       MonoAssembly *ass = NULL;
        MonoImageOpenStatus status = MONO_IMAGE_OK;
-       MonoAssemblyName corlib_aname;
+       const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
+       int n;
 
        if (domain)
                g_assert_not_reached ();
@@ -300,7 +519,6 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        MONO_GC_PRE_INIT ();
 
        appdomain_thread_id = TlsAlloc ();
-       context_thread_id = TlsAlloc ();
 
        InitializeCriticalSection (&appdomains_mutex);
 
@@ -308,37 +526,41 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        mono_raw_buffer_init ();
        mono_images_init ();
        mono_assemblies_init ();
+       mono_classes_init ();
        mono_loader_init ();
 
        /* FIXME: When should we release this memory? */
        MONO_GC_REGISTER_ROOT (appdomains_list);
-       appdomains_list = mono_g_hash_table_new (mono_aligned_addr_hash, g_direct_equal);
 
        domain = mono_domain_create ();
        mono_root_domain = domain;
 
        SET_APPDOMAIN (domain);
        
+       /* Get a list of runtimes supported by the exe */
        if (exe_filename != NULL) {
-               current_runtime = get_runtime_from_exe (exe_filename);
+               get_runtimes_from_exe (exe_filename, runtimes);
        } else if (runtime_version != NULL) {
-               current_runtime = get_runtime_by_version (runtime_version);
+               runtimes [0] = get_runtime_by_version (runtime_version);
+               runtimes [1] = NULL;
        }
 
-       if (current_runtime == NULL) {
+       if (runtimes [0] == NULL) {
+               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");
-               current_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
-               g_print ("Using default runtime: %s\n", current_runtime->runtime_version);
+               g_print ("Using default runtime: %s\n", default_runtime->runtime_version);
+       }
+
+       /* The selected runtime will be the first one for which there is a mscrolib.dll */
+       for (n = 0; runtimes [n] != NULL && ass == NULL; n++) {
+               current_runtime = runtimes [n];
+               ass = mono_assembly_load_corlib (current_runtime, &status);
+               if (status != MONO_IMAGE_OK && status != MONO_IMAGE_ERROR_ERRNO)
+                       break;
        }
 
-       /* find the corlib */
-       corlib_aname.name = "mscorlib";
-       corlib_aname.major = current_runtime->assembly_major;
-       corlib_aname.minor = current_runtime->assembly_minor;
-       corlib_aname.build = current_runtime->assembly_build;
-       corlib_aname.revision = current_runtime->assembly_revision;
-       
-       ass = mono_assembly_load (&corlib_aname, NULL, &status);
        if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
                switch (status){
                case MONO_IMAGE_ERROR_ERRNO: {
@@ -470,6 +692,10 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                 mono_defaults.corlib, "System", "RuntimeFieldHandle");
        g_assert (mono_defaults.fieldhandle_class != 0);
 
+       mono_defaults.systemtype_class = mono_class_from_name (
+                mono_defaults.corlib, "System", "Type");
+       g_assert (mono_defaults.systemtype_class != 0);
+
        mono_defaults.monotype_class = mono_class_from_name (
                 mono_defaults.corlib, "System", "MonoType");
        g_assert (mono_defaults.monotype_class != 0);
@@ -564,7 +790,28 @@ 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");
+
+       mono_defaults.internals_visible_class = mono_class_from_name (
+               mono_defaults.corlib, "System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
+
+       mono_defaults.variant_class = mono_class_from_name (
+               mono_defaults.corlib, "System", "Variant");
+
+       mono_defaults.com_object_class = mono_class_from_name (
+               mono_defaults.corlib, "System", "__ComObject");
+
+       /*
+        * Note that mono_defaults.generic_*_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");
+       mono_defaults.generic_nullable_class = mono_class_from_name (
+               mono_defaults.corlib, "System", "Nullable`1");
 
        domain->friendly_name = g_path_get_basename (filename);
 
@@ -621,6 +868,30 @@ mono_init_version (const char *domain_name, const char *version)
        return mono_init_internal (domain_name, NULL, version);
 }
 
+/**
+ * mono_cleanup:
+ *
+ * Cleans up all metadata modules. 
+ */
+void
+mono_cleanup (void)
+{
+       mono_loader_cleanup ();
+       mono_classes_cleanup ();
+       mono_assemblies_cleanup ();
+       mono_images_cleanup ();
+       mono_raw_buffer_cleanup ();
+       mono_metadata_cleanup ();
+
+       TlsFree (appdomain_thread_id);
+       DeleteCriticalSection (&appdomains_mutex);
+}
+
+/**
+ * mono_get_root_domain:
+ *
+ * Returns: the root appdomain.
+ */
 MonoDomain*
 mono_get_root_domain (void)
 {
@@ -632,7 +903,7 @@ mono_get_root_domain (void)
  *
  * Returns: the current domain.
  */
-inline MonoDomain *
+MonoDomain *
 mono_domain_get ()
 {
        return GET_APPDOMAIN ();
@@ -644,55 +915,36 @@ mono_domain_get ()
  *
  * Sets the current domain to @domain.
  */
-inline void
+void
 mono_domain_set_internal (MonoDomain *domain)
 {
        SET_APPDOMAIN (domain);
        SET_APPCONTEXT (domain->default_context);
 }
 
-typedef struct {
-       MonoDomainFunc func;
-       gpointer user_data;
-} DomainInfo;
-
-static void
-copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
-{
-       MonoGHashTable *dest = (MonoGHashTable*)user_data;
-
-       mono_g_hash_table_insert (dest, key, data);
-}
-
-static void
-foreach_domain (gpointer key, gpointer data, gpointer user_data)
-{
-       DomainInfo *dom_info = user_data;
-
-       dom_info->func ((MonoDomain*)data, dom_info->user_data);
-}
-
 void
 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
 {
-       DomainInfo dom_info;
-       MonoGHashTable *copy;
+       int i, size;
+       MonoDomain **copy;
 
        /*
-        * Create a copy of the hashtable to avoid calling the user callback
+        * Create a copy of the data to avoid calling the user callback
         * inside the lock because that could lead to deadlocks.
         * We can do this because this function is not perf. critical.
         */
-       copy = mono_g_hash_table_new (mono_aligned_addr_hash, NULL);
-       EnterCriticalSection (&appdomains_mutex);
-       mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
-       LeaveCriticalSection (&appdomains_mutex);
-
-       dom_info.func = func;
-       dom_info.user_data = user_data;
-       mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
+       mono_appdomains_lock ();
+       size = appdomain_list_size;
+       copy = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
+       memcpy (copy, appdomains_list, appdomain_list_size * sizeof (void*));
+       mono_appdomains_unlock ();
+
+       for (i = 0; i < size; ++i) {
+               if (copy [i])
+                       func (copy [i], user_data);
+       }
 
-       mono_g_hash_table_destroy (copy);
+       mono_gc_free_fixed (copy);
 }
 
 /**
@@ -708,15 +960,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;
@@ -747,9 +999,9 @@ mono_domain_free (MonoDomain *domain, gboolean force)
                return;
        }
 
-       EnterCriticalSection (&appdomains_mutex);
-       mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
-       LeaveCriticalSection (&appdomains_mutex);
+       mono_appdomains_lock ();
+       appdomains_list [domain->domain_id] = NULL;
+       mono_appdomains_unlock ();
 
        /* FIXME: free delegate_hash_table when it's used */
        if (domain->search_path) {
@@ -763,24 +1015,26 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        domain->null_reference_ex = NULL;
        domain->stack_overflow_ex = NULL;
        domain->entry_assembly = NULL;
-       g_free (domain->friendly_name);
-       domain->friendly_name = NULL;
        for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
                MonoAssembly *ass = tmp->data;
-               /*g_print ("Unloading domain %p, assembly %s, refcount: %d\n", domain, ass->aname.name, ass->ref_count);*/
+               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading domain %s %p, assembly %s %p, refcount=%d\n", domain->friendly_name, domain, ass->aname.name, ass, ass->ref_count);
                mono_assembly_close (ass);
        }
        g_slist_free (domain->domain_assemblies);
        domain->domain_assemblies = NULL;
 
+       g_free (domain->friendly_name);
+       domain->friendly_name = NULL;
        mono_g_hash_table_destroy (domain->env);
        domain->env = NULL;
        g_hash_table_destroy (domain->class_vtable_hash);
        domain->class_vtable_hash = NULL;
-       mono_g_hash_table_destroy (domain->proxy_vtable_hash);
+       g_hash_table_destroy (domain->proxy_vtable_hash);
        domain->proxy_vtable_hash = NULL;
-       mono_g_hash_table_destroy (domain->static_data_hash);
-       domain->static_data_hash = NULL;
+       if (domain->static_data_array) {
+               mono_gc_free_fixed (domain->static_data_array);
+               domain->static_data_array = NULL;
+       }
        g_hash_table_destroy (domain->jit_code_hash);
        domain->jit_code_hash = NULL;
        if (domain->dynamic_code_hash) {
@@ -820,19 +1074,21 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        domain->jump_trampoline_hash = NULL;
        g_hash_table_destroy (domain->finalizable_objects_hash);
        domain->finalizable_objects_hash = NULL;
+       g_hash_table_destroy (domain->jit_trampoline_hash);
+       domain->jit_trampoline_hash = NULL;
+       g_hash_table_destroy (domain->delegate_trampoline_hash);
+       domain->delegate_trampoline_hash = NULL;
        if (domain->special_static_fields) {
                g_hash_table_destroy (domain->special_static_fields);
                domain->special_static_fields = NULL;
        }
+       DeleteCriticalSection (&domain->assemblies_lock);
        DeleteCriticalSection (&domain->lock);
        domain->setup = NULL;
 
        /* FIXME: anything else required ? */
 
-#if HAVE_BOEHM_GC
-#else
-       g_free (domain);
-#endif
+       mono_gc_free_fixed (domain);
 
        if ((domain == mono_root_domain))
                mono_root_domain = NULL;
@@ -848,9 +1104,12 @@ mono_domain_get_by_id (gint32 domainid)
 {
        MonoDomain * domain;
 
-       EnterCriticalSection (&appdomains_mutex);
-       domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
-       LeaveCriticalSection (&appdomains_mutex);
+       mono_appdomains_lock ();
+       if (domainid < appdomain_list_size)
+               domain = appdomains_list [domainid];
+       else
+               domain = NULL;
+       mono_appdomains_unlock ();
 
        return domain;
 }
@@ -873,6 +1132,37 @@ mono_context_get (void)
        return GET_APPCONTEXT ();
 }
 
+/* LOCKING: the caller holds the lock for this domain */
+void
+mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap)
+{
+       /* The first entry in the array is the index of the next free slot
+        * and the total size of the array
+        */
+       int next;
+       if (domain->static_data_array) {
+               int size = GPOINTER_TO_INT (domain->static_data_array [1]);
+               next = GPOINTER_TO_INT (domain->static_data_array [0]);
+               if (next >= size) {
+                       gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * (size * 2), NULL);
+                       memcpy (new_array, domain->static_data_array, sizeof (gpointer) * size);
+                       size *= 2;
+                       new_array [1] = GINT_TO_POINTER (size);
+                       mono_gc_free_fixed (domain->static_data_array);
+                       domain->static_data_array = new_array;
+               }
+       } else {
+               int size = 32;
+               gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * size, NULL);
+               next = 2;
+               new_array [0] = GINT_TO_POINTER (next);
+               new_array [1] = GINT_TO_POINTER (size);
+               domain->static_data_array = new_array;
+       }
+       domain->static_data_array [next++] = data;
+       domain->static_data_array [0] = GINT_TO_POINTER (next);
+}
+
 MonoImage*
 mono_get_corlib (void)
 {
@@ -1112,7 +1402,7 @@ app_config_free (AppConfigInfo* app_config)
 }
 
 
-static MonoRuntimeInfo*
+static const MonoRuntimeInfo*
 get_runtime_by_version (const char *version)
 {
        int n;
@@ -1125,13 +1415,13 @@ get_runtime_by_version (const char *version)
        return NULL;
 }
 
-static MonoRuntimeInfo*        
-get_runtime_from_exe (const char *exe_file)
+static void
+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);
@@ -1143,21 +1433,26 @@ get_runtime_from_exe (const char *exe_file)
                 * If there are no such elements, look for a requiredRuntime element.
                 */
                if (app_config->supported_runtimes != NULL) {
+                       int n = 0;
                        GSList *list = app_config->supported_runtimes;
-                       while (list != NULL && runtime == NULL) {
+                       while (list != NULL) {
                                version = (char*) list->data;
                                runtime = get_runtime_by_version (version);
+                               if (runtime != NULL)
+                                       runtimes [n++] = runtime;
                                list = g_slist_next (list);
                        }
+                       runtimes [n] = NULL;
                        app_config_free (app_config);
-                       return runtime;
+                       return;
                }
                
                /* Check the requiredRuntime element. This is for 1.0 apps only. */
                if (app_config->required_runtime != NULL) {
-                       runtime = get_runtime_by_version (app_config->required_runtime);
+                       runtimes [0] = get_runtime_by_version (app_config->required_runtime);
+                       runtimes [1] = NULL;
                        app_config_free (app_config);
-                       return runtime;
+                       return;
                }
                app_config_free (app_config);
        }
@@ -1169,20 +1464,28 @@ get_runtime_from_exe (const char *exe_file)
                 * a default runtime and leave to the initialization method the work of
                 * reporting the error.
                 */
-               return get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
+               runtimes [0] = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
+               runtimes [1] = NULL;
+               return;
        }
 
-       runtime = get_runtime_by_version (image->version);
-       
-       return runtime;
+       /* 
+        * FIXME: This would cause us to unload the image, and it will be loaded again later.
+        * Disabling it will mean the initial exe will not be unloaded on shutdown.
+        */
+       //mono_image_close (image);
+
+       runtimes [0] = get_runtime_by_version (image->version);
+       runtimes [1] = NULL;
 }
 
+
 /**
- * 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;
@@ -1191,9 +1494,12 @@ mono_get_runtime_info (void)
 gchar *
 mono_debugger_check_runtime_version (const char *filename)
 {
-       MonoRuntimeInfo *rinfo;
+       const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
+       const MonoRuntimeInfo *rinfo;
+
+       get_runtimes_from_exe (filename, runtimes);
+       rinfo = runtimes [0];
 
-       rinfo = get_runtime_from_exe (filename);
        if (!rinfo)
                return g_strdup_printf ("Cannot get runtime version from assembly `%s'", filename);