From af5651accfc23e472218bf303c04e76b84744a56 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 18 Mar 2021 07:23:39 +0100 Subject: [PATCH] [runtime] Move MonoVM-specific initialization to MonoVM-specific code. (#10899) * [runtime] Download the CoreCLR embedding header file * [runtime] Create VM-specific code and header files and include them in the build * [runtime] Move MonoVM-specific initialization to MonoVM-specific code. --- mk/quiet.mk | 1 + runtime/.gitignore | 1 + runtime/Makefile | 9 +++- runtime/coreclr-bridge.m | 24 +++++++++ runtime/monotouch-main.m | 53 +++----------------- runtime/monovm-bridge.m | 85 ++++++++++++++++++++++++++++++++ runtime/xamarin/coreclr-bridge.h | 19 +++++++ runtime/xamarin/monovm-bridge.h | 26 ++++++++++ runtime/xamarin/runtime.h | 5 ++ 9 files changed, 175 insertions(+), 48 deletions(-) create mode 100644 runtime/coreclr-bridge.m create mode 100644 runtime/monovm-bridge.m create mode 100644 runtime/xamarin/coreclr-bridge.h create mode 100644 runtime/xamarin/monovm-bridge.h diff --git a/mk/quiet.mk b/mk/quiet.mk index 46d938343f..c5d46ad303 100644 --- a/mk/quiet.mk +++ b/mk/quiet.mk @@ -33,6 +33,7 @@ Q_SN= $(if $(V),,@echo "SN $(@F)";) Q_XBUILD=$(if $(V),,@echo "XBUILD $(@F)";) Q_TT= $(if $(V),,@echo "TT $(@F)";) Q_BUILD=$(if $(V),,@echo "BUILD $(@F)";) +Q_CURL=$(if $(V),,@echo "CURL $(@F)";) Q_DOTNET_BUILD=$(if $(V),,@echo "CSC [dotnet] $(@F)";) Q_DOTNET_GEN =$(if $(V),,@echo "GEN [dotnet] $(@F)";) diff --git a/runtime/.gitignore b/runtime/.gitignore index 295b4b6019..858bc7c5a0 100644 --- a/runtime/.gitignore +++ b/runtime/.gitignore @@ -11,3 +11,4 @@ tvextension-main.m bindings-generated.m *.dylib *.o +coreclrhost.h diff --git a/runtime/Makefile b/runtime/Makefile index c79f09b14f..578937c228 100644 --- a/runtime/Makefile +++ b/runtime/Makefile @@ -19,8 +19,10 @@ SHIPPED_HEADERS += \ xamarin/trampolines.h \ xamarin/runtime.h \ xamarin/runtime-generated.h \ + xamarin/monovm-bridge.h \ + xamarin/coreclr-bridge.h \ -SHARED_SOURCES += mono-runtime.m bindings.m bindings-generated.m shared.m runtime.m trampolines.m trampolines-invoke.m xamarin-support.m nsstring-localization.m trampolines-varargs.m +SHARED_SOURCES += mono-runtime.m bindings.m bindings-generated.m shared.m runtime.m trampolines.m trampolines-invoke.m xamarin-support.m nsstring-localization.m trampolines-varargs.m monovm-bridge.m coreclr-bridge.m SHARED_I386_SOURCES += trampolines-i386.m trampolines-i386-asm.s trampolines-i386-objc_msgSend.s trampolines-i386-objc_msgSendSuper.s trampolines-i386-objc_msgSend_stret.s trampolines-i386-objc_msgSendSuper_stret.s SHARED_X86_64_SOURCES += trampolines-x86_64.m trampolines-x86_64-asm.s trampolines-x86_64-objc_msgSend.s trampolines-x86_64-objc_msgSendSuper.s trampolines-x86_64-objc_msgSend_stret.s trampolines-x86_64-objc_msgSendSuper_stret.s SHARED_ARM64_SOURCES += trampolines-arm64.m trampolines-arm64-asm.s @@ -30,6 +32,11 @@ SHARED_FILES = $(SHARED_SOURCES) $(SHARED_HEADERS) $(SHARED_I386_SOURCES) $(SHAR EXTRA_DEPENDENCIES = $(SHARED_HEADERS) $(TOP)/Make.config $(TOP)/mk/mono.mk +coreclrhost.h: Makefile + $(Q_CURL) curl -L --output "$@" https://raw.githubusercontent.com/dotnet/runtime/6c8f9fff6adcf6c661072646dbdafabed5267ec5/src/coreclr/hosts/inc/coreclrhost.h + +coreclr-bridge.m: coreclrhost.h + xamarin/mono-runtime.h: mono-runtime.h.t4 exports.t4 $(Q_GEN) $(TT) $< -o $@ diff --git a/runtime/coreclr-bridge.m b/runtime/coreclr-bridge.m new file mode 100644 index 0000000000..685a5316de --- /dev/null +++ b/runtime/coreclr-bridge.m @@ -0,0 +1,24 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* +* Authors: Rolf Bjarne Kvinge +* +* Copyright (C) 2021 Microsoft Corp. +* +*/ + +#if defined (CORECLR_RUNTIME) + +#include "xamarin/xamarin.h" +#include "xamarin/coreclr-bridge.h" + +void +xamarin_bridge_setup () +{ +} + +void +xamarin_bridge_initialize () +{ +} + +#endif // CORECLR_RUNTIME diff --git a/runtime/monotouch-main.m b/runtime/monotouch-main.m index 8a1514fb63..ca99682bdc 100644 --- a/runtime/monotouch-main.m +++ b/runtime/monotouch-main.m @@ -31,7 +31,7 @@ #include "../tools/mtouch/monotouch-fixes.c" #endif -static unsigned char * +unsigned char * xamarin_load_aot_data (MonoAssembly *assembly, int size, gpointer user_data, void **out_handle) { // COOP: This is a callback called by the AOT runtime, I believe we don't have to change the GC mode here (even though it accesses managed memory). @@ -76,7 +76,7 @@ xamarin_load_aot_data (MonoAssembly *assembly, int size, gpointer user_data, voi return (unsigned char *) ptr; } -static void +void xamarin_free_aot_data (MonoAssembly *assembly, int size, gpointer user_data, void *handle) { // COOP: This is a callback called by the AOT runtime, I belive we don't have to change the GC mode here. @@ -86,8 +86,8 @@ xamarin_free_aot_data (MonoAssembly *assembly, int size, gpointer user_data, voi /* This hook avoids the gazillion of filesystem probes we do as part of assembly loading. */ -static MonoAssembly* -assembly_preload_hook (MonoAssemblyName *aname, char **assemblies_path, void* user_data) +MonoAssembly* +xamarin_assembly_preload_hook (MonoAssemblyName *aname, char **assemblies_path, void* user_data) { // COOP: This is a callback called by the AOT runtime, I belive we don't have to change the GC mode here. char filename [1024]; @@ -261,15 +261,11 @@ xamarin_main (int argc, char *argv[], enum XamarinLaunchMode launch_mode) MonoAssembly *assembly; GCHandle exception_gchandle = NULL; - const char *c_bundle_path = xamarin_get_bundle_path (); chdir (c_bundle_path); - setenv ("MONO_PATH", c_bundle_path, 1); - setenv ("MONO_XMLSERIALIZER_THS", "no", 1); setenv ("DYLD_BIND_AT_LAUNCH", "1", 1); - setenv ("MONO_REFLECTION_SERIALIZER", "yes", 1); #if TARGET_OS_WATCH // watchOS can raise signals just fine... @@ -277,12 +273,7 @@ xamarin_main (int argc, char *argv[], enum XamarinLaunchMode launch_mode) signal (SIGPIPE, SIG_IGN); #endif -#if TARGET_OS_WATCH || TARGET_OS_TV - mini_parse_debug_option ("explicit-null-checks"); -#endif - // see http://bugzilla.xamarin.com/show_bug.cgi?id=820 - // take this line out once the bug is fixed - mini_parse_debug_option ("no-gdb-backtrace"); + xamarin_bridge_setup (); DEBUG_LAUNCH_TIME_PRINT ("Spin-up time"); @@ -407,39 +398,7 @@ xamarin_main (int argc, char *argv[], enum XamarinLaunchMode launch_mode) xamarin_initialize_cocoa_threads (NULL); #endif -#if defined (__arm__) || defined(__aarch64__) - xamarin_register_modules (); -#endif - DEBUG_LAUNCH_TIME_PRINT ("\tAOT register time"); - -#ifdef DEBUG - monotouch_start_debugging (); - DEBUG_LAUNCH_TIME_PRINT ("\tDebug init time"); -#endif - - if (xamarin_init_mono_debug) - mono_debug_init (MONO_DEBUG_FORMAT_MONO); - - mono_install_assembly_preload_hook (assembly_preload_hook, NULL); - mono_install_load_aot_data_hook (xamarin_load_aot_data, xamarin_free_aot_data, NULL); - -#ifdef DEBUG - monotouch_start_profiling (); - DEBUG_LAUNCH_TIME_PRINT ("\tProfiler config time"); -#endif - - mono_set_signal_chaining (TRUE); - mono_set_crash_chaining (TRUE); - mono_install_unhandled_exception_hook (xamarin_unhandled_exception_handler, NULL); - mono_install_ftnptr_eh_callback (xamarin_ftnptr_exception_handler); - - mono_jit_init_version ("MonoTouch", "mobile"); - /* - As part of mono initialization a preload hook is added that overrides ours, so we need to re-instate it here. - This is wasteful, but there's no way to manipulate the preload hook list except by adding to it. - */ - mono_install_assembly_preload_hook (assembly_preload_hook, NULL); - DEBUG_LAUNCH_TIME_PRINT ("\tJIT init time"); + xamarin_bridge_initialize (); xamarin_initialize (); DEBUG_LAUNCH_TIME_PRINT ("\tmonotouch init time"); diff --git a/runtime/monovm-bridge.m b/runtime/monovm-bridge.m new file mode 100644 index 0000000000..32f0fbb4e0 --- /dev/null +++ b/runtime/monovm-bridge.m @@ -0,0 +1,85 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* +* Authors: Rolf Bjarne Kvinge +* +* Copyright (C) 2021 Microsoft Corp. +* +*/ + +/* Support code for using MonoVM */ + +#if !defined (CORECLR_RUNTIME) + +#include + +#if !DOTNET && TARGET_OS_OSX +#define LEGACY_XAMARIN_MAC 1 +#else +#define LEGACY_XAMARIN_MAC 0 +#endif + +#include "monotouch-debug.h" +#include "runtime-internal.h" +#include "xamarin/xamarin.h" +#include "xamarin/monovm-bridge.h" + +#if !LEGACY_XAMARIN_MAC +void +xamarin_bridge_setup () +{ + const char *c_bundle_path = xamarin_get_bundle_path (); + + setenv ("MONO_PATH", c_bundle_path, 1); + + setenv ("MONO_XMLSERIALIZER_THS", "no", 1); + setenv ("MONO_REFLECTION_SERIALIZER", "yes", 1); + +#if TARGET_OS_WATCH || TARGET_OS_TV + mini_parse_debug_option ("explicit-null-checks"); +#endif + // see http://bugzilla.xamarin.com/show_bug.cgi?id=820 + // take this line out once the bug is fixed + mini_parse_debug_option ("no-gdb-backtrace"); +} + +void +xamarin_bridge_initialize () +{ +#if defined (__arm__) || defined(__aarch64__) + xamarin_register_modules (); +#endif + DEBUG_LAUNCH_TIME_PRINT ("\tAOT register time"); + +#ifdef DEBUG + monotouch_start_debugging (); + DEBUG_LAUNCH_TIME_PRINT ("\tDebug init time"); +#endif + + if (xamarin_init_mono_debug) + mono_debug_init (MONO_DEBUG_FORMAT_MONO); + + mono_install_assembly_preload_hook (xamarin_assembly_preload_hook, NULL); + mono_install_load_aot_data_hook (xamarin_load_aot_data, xamarin_free_aot_data, NULL); + +#ifdef DEBUG + monotouch_start_profiling (); + DEBUG_LAUNCH_TIME_PRINT ("\tProfiler config time"); +#endif + + mono_set_signal_chaining (TRUE); + mono_set_crash_chaining (TRUE); + mono_install_unhandled_exception_hook (xamarin_unhandled_exception_handler, NULL); + mono_install_ftnptr_eh_callback (xamarin_ftnptr_exception_handler); + + mono_jit_init_version ("MonoTouch", "mobile"); + /* + As part of mono initialization a preload hook is added that overrides ours, so we need to re-instate it here. + This is wasteful, but there's no way to manipulate the preload hook list except by adding to it. + */ + mono_install_assembly_preload_hook (xamarin_assembly_preload_hook, NULL); + DEBUG_LAUNCH_TIME_PRINT ("\tJIT init time"); +} +#endif // !LEGACY_XAMARIN_MAC + + +#endif // !CORECLR_RUNTIME diff --git a/runtime/xamarin/coreclr-bridge.h b/runtime/xamarin/coreclr-bridge.h new file mode 100644 index 0000000000..6a14d222fa --- /dev/null +++ b/runtime/xamarin/coreclr-bridge.h @@ -0,0 +1,19 @@ + +/* Support for using CoreCLR */ + +#if defined (CORECLR_RUNTIME) + +#ifndef __CORECLR_BRIDGE__ +#define __CORECLR_BRIDGE__ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* __CORECLR_BRIDGE__ */ + +#endif // CORECLR_RUNTIME diff --git a/runtime/xamarin/monovm-bridge.h b/runtime/xamarin/monovm-bridge.h new file mode 100644 index 0000000000..ca9cf613ce --- /dev/null +++ b/runtime/xamarin/monovm-bridge.h @@ -0,0 +1,26 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* +* Authors: Rolf Bjarne Kvinge +* +* Copyright (C) 2021 Microsoft Corp. +* +*/ + +/* Support code for using MonoVM */ + +#if !defined (CORECLR_RUNTIME) + +#ifndef __MONOVM_BRIDGE__ +#define __MONOVM_BRIDGE__ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* __MONOVM_BRIDGE__ */ + +#endif // !CORECLR_RUNTIME diff --git a/runtime/xamarin/runtime.h b/runtime/xamarin/runtime.h index 41a3b58a15..fe892c57a1 100644 --- a/runtime/xamarin/runtime.h +++ b/runtime/xamarin/runtime.h @@ -195,6 +195,11 @@ const char * xamarin_skip_encoding_flags (const char *encoding); void xamarin_add_registration_map (struct MTRegistrationMap *map, bool partial); uint32_t xamarin_find_protocol_wrapper_type (uint32_t token_ref); void xamarin_release_block_on_main_thread (void *obj); +void xamarin_bridge_setup (); // this is called very early, before parsing the command line arguments +void xamarin_bridge_initialize (); // this is called a bit later, after parsing the command line arguments. +unsigned char * xamarin_load_aot_data (MonoAssembly *assembly, int size, gpointer user_data, void **out_handle); +void xamarin_free_aot_data (MonoAssembly *assembly, int size, gpointer user_data, void *handle); +MonoAssembly* xamarin_assembly_preload_hook (MonoAssemblyName *aname, char **assemblies_path, void* user_data); MonoObject * xamarin_new_nsobject (id self, MonoClass *klass, GCHandle *exception_gchandle); bool xamarin_has_managed_ref (id self);