From 97182297e6c21021091710a29e1a0ad83ba81ac3 Mon Sep 17 00:00:00 2001 From: Jonathan Chambers Date: Sat, 27 Jan 2018 10:24:12 +0300 Subject: [PATCH] Initial support of Nintendo platform (part of commit 9379c66 from Unity-Technologies/bdwgc) Issue #173 (bdwgc). * include/gc_config_macros.h [(!GC_WIN32_THREADS || GC_WIN32_PTHREADS || __CYGWIN32__ || __CYGWIN__) && GC_THREADS] (GC_PTHREADS): Do not define if NN_PLATFORM_CTR or NN_BUILD_TARGET_PLATFORM_NX. * include/private/gc_priv.h [NN_PLATFORM_CTR] (CLOCK_TYPE, GET_TIME, MS_TIME_DIFF): Define macro. * include/private/gc_priv.h [!PCR && (NN_PLATFORM_CTR || NINTENDO_SWITCH)] (STOP_WORLD, START_WORLD): Likewise. * include/private/gcconfig.h [NN_PLATFORM_CTR] (ARM32, mach_type_known): Likewise. * include/private/gcconfig.h [NN_BUILD_TARGET_PLATFORM_NX] (NINTENDO_SWITCH, mach_type_known): Likewise. * include/private/gcconfig.h [NN_PLATFORM_CTR] (DATASTART, DATAEND, STACKBOTTOM): Likewise. * include/private/gcconfig.h [NINTENDO_SWITCH] (NO_HANDLE_FORK, DATASTART, DATAEND, STACKBOTTOM): Likewise. * include/private/gcconfig.h [NN_PLATFORM_CTR || NINTENDO_SWITCH] (THREADS): Likewise. * include/private/gcconfig.h [NINTENDO_SWITCH] (GET_MEM): Likewise. * misc.c [NN_PLATFORM_CTR || NINTENDO_SWITCH] (WRITE): Likewise. * include/private/gc_priv.h [NN_PLATFORM_CTR] (n3ds_get_system_tick n3ds_convert_tick_to_ms): Declare external function. * include/private/gc_priv.h [!PCR && (NN_PLATFORM_CTR || NINTENDO_SWITCH)] (GC_stop_world, GC_start_world): Declare. * include/private/gcconfig.h [NN_PLATFORM_CTR] (Image$$ZI$$ZI$$Base, Image$$ZI$$ZI$$Limit, n3ds_get_stack_bottom): Likewise. * include/private/gcconfig.h [NINTENDO_SWITCH] (__bss_end, switch_get_stack_bottom): Likewise. * include/private/gcconfig.h [NINTENDO_SWITCH] (switch_get_mem): Likewise. * misc.c [NN_PLATFORM_CTR] (n3ds_log_write): Likewise. * misc.c [NINTENDO_SWITCH] (switch_log_write): Likewise. * include/private/gcconfig.h [__aarch64__ && !LINUX && !DARWIN && !FREEBSD] (NOSYS): Do not define if NN_BUILD_TARGET_PLATFORM_NX. * include/private/gcconfig.h [(__arm || __arm__ || __thumb__) && !NACL && !LINUX && !NETBSD && !FREEBSD && !OPENBSD && !DARWIN && !_WIN32 && !__CEGCC__ && !SYMBIAN] (NOSYS): Do not define if NN_PLATFORM_CTR or NN_BUILD_TARGET_PLATFORM_NX. * misc.c [!OS2 && !MACOS && !GC_ANDROID_LOG && !MSWIN32 && !MSWINCE] (GC_stdout, GC_stderr, GC_log, GC_set_log_fd): Do not define if NN_PLATFORM_CTR or NINTENDO_SWITCH. * misc.c [NN_PLATFORM_CTR] (GC_init): Call GC_get_main_stack_base() to set GC_stackbottom. * os_dep.c (SBRK_ARG_T, OPT_MAP_ANON, GC_unix_mmap_get_mem, GC_unix_sbrk_get_mem, GC_unix_get_mem): Do not define if NINTENDO_SWITCH. * os_dep.c [USE_MUNMAP]: Do not include unistd.h, sys/mman.h, sys/stat.h, sys/types.h if NN_PLATFORM_CTR. * os_dep.c [NN_PLATFORM_CTR || NINTENDO_SWITCH] (GC_default_push_other_roots): Redirect to GC_push_all_stacks. --- include/gc_config_macros.h | 3 ++- include/private/gc_priv.h | 11 +++++++++-- include/private/gcconfig.h | 38 +++++++++++++++++++++++++++++++++++--- misc.c | 9 +++++++++ os_dep.c | 11 ++++++----- 5 files changed, 61 insertions(+), 11 deletions(-) diff --git a/include/gc_config_macros.h b/include/gc_config_macros.h index ac130477..16a23f55 100644 --- a/include/gc_config_macros.h +++ b/include/gc_config_macros.h @@ -128,7 +128,8 @@ #undef GC_PTHREADS #if (!defined(GC_WIN32_THREADS) || defined(GC_WIN32_PTHREADS) \ - || defined(__CYGWIN32__) || defined(__CYGWIN__)) && defined(GC_THREADS) + || defined(__CYGWIN32__) || defined(__CYGWIN__)) && defined(GC_THREADS) \ + && !defined(NN_PLATFORM_CTR) && !defined(NN_BUILD_TARGET_PLATFORM_NX) /* Posix threads. */ # define GC_PTHREADS #endif diff --git a/include/private/gc_priv.h b/include/private/gc_priv.h index 42128ffc..c401aa3c 100644 --- a/include/private/gc_priv.h +++ b/include/private/gc_priv.h @@ -434,7 +434,13 @@ typedef char * ptr_t; /* A generic pointer to which we can add */ # define CLOCK_TYPE DWORD # define GET_TIME(x) (void)(x = GetTickCount()) # define MS_TIME_DIFF(a,b) ((long)((a)-(b))) -#else /* !MSWIN32, !MSWINCE, !BSD_TIME */ +#elif defined(NN_PLATFORM_CTR) +# define CLOCK_TYPE long long + CLOCK_TYPE n3ds_get_system_tick(void); + CLOCK_TYPE n3ds_convert_tick_to_ms(CLOCK_TYPE tick); +# define GET_TIME(x) (void)(x = n3ds_get_system_tick()) +# define MS_TIME_DIFF(a,b) ((long)n3ds_convert_tick_to_ms((a)-(b))) +#else /* !BSD_TIME && !NN_PLATFORM_CTR && !MSWIN32 && !MSWINCE */ # include # if defined(FREEBSD) && !defined(CLOCKS_PER_SEC) # include @@ -513,7 +519,8 @@ typedef char * ptr_t; /* A generic pointer to which we can add */ PCR_allSigsBlocked, \ PCR_waitForever) # else -# if defined(GC_WIN32_THREADS) || defined(GC_PTHREADS) +# if defined(NN_PLATFORM_CTR) || defined(NINTENDO_SWITCH) \ + || defined(GC_WIN32_THREADS) || defined(GC_PTHREADS) GC_INNER void GC_stop_world(void); GC_INNER void GC_start_world(void); # define STOP_WORLD() GC_stop_world() diff --git a/include/private/gcconfig.h b/include/private/gcconfig.h index 4fd768b2..709edeba 100644 --- a/include/private/gcconfig.h +++ b/include/private/gcconfig.h @@ -131,7 +131,8 @@ # endif # if defined(__aarch64__) # define AARCH64 -# if !defined(LINUX) && !defined(DARWIN) && !defined(FREEBSD) +# if !defined(LINUX) && !defined(DARWIN) && !defined(FREEBSD) \ + && !defined(NN_BUILD_TARGET_PLATFORM_NX) # define NOSYS # define mach_type_known # endif @@ -141,8 +142,9 @@ # if defined(NACL) # define mach_type_known # elif !defined(LINUX) && !defined(NETBSD) && !defined(FREEBSD) \ - && !defined(OPENBSD) && !defined(DARWIN) \ - && !defined(_WIN32) && !defined(__CEGCC__) && !defined(SYMBIAN) + && !defined(OPENBSD) && !defined(DARWIN) && !defined(_WIN32) \ + && !defined(__CEGCC__) && !defined(NN_PLATFORM_CTR) \ + && !defined(NN_BUILD_TARGET_PLATFORM_NX) && !defined(SYMBIAN) # define NOSYS # define mach_type_known # endif @@ -616,6 +618,16 @@ # define mach_type_known # endif +# if defined(NN_PLATFORM_CTR) +# define ARM32 +# define mach_type_known +# endif + +# if defined(NN_BUILD_TARGET_PLATFORM_NX) +# define NINTENDO_SWITCH +# define mach_type_known +# endif + # if defined(SYMBIAN) # define mach_type_known # endif @@ -2312,6 +2324,22 @@ # define DATAEND ((ptr_t)(&_end)) # define DYNAMIC_LOADING # endif +# ifdef NN_PLATFORM_CTR + extern unsigned char Image$$ZI$$ZI$$Base[]; +# define DATASTART (ptr_t)(Image$$ZI$$ZI$$Base) + extern unsigned char Image$$ZI$$ZI$$Limit[]; +# define DATAEND (ptr_t)(Image$$ZI$$ZI$$Limit) + void *n3ds_get_stack_bottom(void); +# define STACKBOTTOM ((ptr_t)n3ds_get_stack_bottom()) +# endif +# ifdef NINTENDO_SWITCH + extern int __bss_end[]; +# define NO_HANDLE_FORK +# define DATASTART (ptr_t)ALIGNMENT /* cannot be null */ +# define DATAEND (ptr_t)(&__bss_end) + void *switch_get_stack_bottom(void); +# define STACKBOTTOM ((ptr_t)switch_get_stack_bottom()) +# endif # ifdef NOSYS /* __data_start is usually defined in the target linker script. */ extern int __data_start[]; @@ -3036,6 +3064,7 @@ #endif /* !CPPCHECK */ #if defined(PCR) || defined(GC_WIN32_THREADS) || defined(GC_PTHREADS) \ + || defined(NN_PLATFORM_CTR) || defined(NINTENDO_SWITCH) \ || defined(SN_TARGET_PS3) # define THREADS #endif @@ -3358,6 +3387,9 @@ # elif defined(SN_TARGET_PS3) void *ps3_get_mem(size_t bytes); # define GET_MEM(bytes) (struct hblk*)ps3_get_mem(bytes) +# elif defined(NINTENDO_SWITCH) + void *switch_get_mem(size_t bytes); +# define GET_MEM(bytes) (struct hblk*)switch_get_mem(bytes) # elif defined(HAIKU) ptr_t GC_haiku_get_mem(size_t bytes); # define GET_MEM(bytes) (struct hblk*)GC_haiku_get_mem(bytes) diff --git a/misc.c b/misc.c index 70f5e9f0..a69f4484 100644 --- a/misc.c +++ b/misc.c @@ -764,6 +764,7 @@ GC_API int GC_CALL GC_is_init_called(void) #define GC_DEFAULT_STDERR_FD 2 #if !defined(OS2) && !defined(MACOS) && !defined(GC_ANDROID_LOG) \ + && !defined(NN_PLATFORM_CTR) && !defined(NINTENDO_SWITCH) \ && !defined(MSWIN32) && !defined(MSWINCE) STATIC int GC_stdout = GC_DEFAULT_STDOUT_FD; STATIC int GC_stderr = GC_DEFAULT_STDERR_FD; @@ -1147,6 +1148,7 @@ GC_API void GC_CALL GC_init(void) GC_init_netbsd_elf(); # endif # if !defined(THREADS) || defined(GC_PTHREADS) \ + || defined(NN_PLATFORM_CTR) || defined(NINTENDO_SWITCH) \ || defined(GC_WIN32_THREADS) || defined(GC_SOLARIS_THREADS) if (GC_stackbottom == 0) { GC_stackbottom = GC_get_main_stack_base(); @@ -1539,6 +1541,13 @@ GC_API void GC_CALL GC_enable_incremental(void) # define WRITE(level, buf, unused_len) \ __android_log_write(level, GC_ANDROID_LOG_TAG, buf) +# elif defined(NN_PLATFORM_CTR) + int n3ds_log_write(const char* text, int length); +# define WRITE(level, buf, len) n3ds_log_write(buf, len) +# elif defined(NINTENDO_SWITCH) + int switch_log_write(const char* text, int length); +# define WRITE(level, buf, len) switch_log_write(buf, len) + #else # if !defined(AMIGA) && !defined(__CC_ARM) # include diff --git a/os_dep.c b/os_dep.c index 3a54fa6f..d7f16526 100644 --- a/os_dep.c +++ b/os_dep.c @@ -2082,8 +2082,8 @@ void GC_register_data_segments(void) # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) \ && !defined(USE_WINALLOC) && !defined(MACOS) && !defined(DOS4GW) \ - && !defined(NONSTOP) && !defined(SN_TARGET_PS3) && !defined(RTEMS) \ - && !defined(__CC_ARM) + && !defined(NINTENDO_SWITCH) && !defined(NONSTOP) \ + && !defined(SN_TARGET_PS3) && !defined(RTEMS) && !defined(__CC_ARM) # define SBRK_ARG_T ptrdiff_t @@ -2447,7 +2447,7 @@ void * os2_alloc(size_t bytes) /* systems. If you have something else, don't define */ /* USE_MUNMAP. */ -#if !defined(MSWIN32) && !defined(MSWINCE) +#if !defined(NN_PLATFORM_CTR) && !defined(MSWIN32) && !defined(MSWINCE) # include # ifdef SN_TARGET_PS3 # include @@ -2711,12 +2711,13 @@ STATIC void GC_CALLBACK GC_default_push_other_roots(void) # endif /* PCR */ -# if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS) +# if defined(NN_PLATFORM_CTR) || defined(NINTENDO_SWITCH) \ + || defined(GC_PTHREADS) || defined(GC_WIN32_THREADS) STATIC void GC_CALLBACK GC_default_push_other_roots(void) { GC_push_all_stacks(); } -# endif /* GC_WIN32_THREADS || GC_PTHREADS */ +# endif # ifdef SN_TARGET_PS3 STATIC void GC_CALLBACK GC_default_push_other_roots(void) -- 2.50.1