diff options
| author | Jim Mussared | 2020-04-23 16:12:55 +1000 |
|---|---|---|
| committer | Damien George | 2020-04-29 23:45:19 +1000 |
| commit | 710426024aece6d248c26bef6627bc3fac13f612 (patch) | |
| tree | 4aeb686aceb7940ddb377383789d6edfc321e7ae /ports/unix | |
| parent | 2e3c42775a402749e790474643cb9f776561264d (diff) | |
all: Factor gchelper code to one place and use it for unix & ARM ports.
No functionality change is intended with this commit, it just consolidates
the separate implementations of GC helper code to the lib/utils/ directory
as a general set of helper functions useful for any port. This reduces
duplication of code, and makes it easier for future ports or embedders to
get the GC implementation correct.
Ports should now link against gchelper_native.c and either gchelper_m0.s or
gchelper_m3.s (currently only Cortex-M is supported but other architectures
can follow), or use the fallback gchelper_generic.c which will work on
x86/x64/ARM.
The gc_helper_get_sp function from gchelper_m3.s is not really GC related
and was only used by cc3200, so it has been moved to that port and renamed
to cortex_m3_get_sp.
Diffstat (limited to 'ports/unix')
| -rw-r--r-- | ports/unix/Makefile | 1 | ||||
| -rw-r--r-- | ports/unix/gccollect.c | 137 | ||||
| -rw-r--r-- | ports/unix/mpconfigport.h | 11 | ||||
| -rw-r--r-- | ports/unix/mpthreadport.c | 5 | ||||
| -rw-r--r-- | ports/unix/variants/minimal/mpconfigvariant.h | 11 |
5 files changed, 13 insertions, 152 deletions
diff --git a/ports/unix/Makefile b/ports/unix/Makefile index d9e77c26c..ec1416614 100644 --- a/ports/unix/Makefile +++ b/ports/unix/Makefile @@ -205,6 +205,7 @@ SRC_C = \ LIB_SRC_C += $(addprefix lib/,\ $(LIB_SRC_C_EXTRA) \ timeutils/timeutils.c \ + utils/gchelper_generic.c \ ) OBJ = $(PY_O) diff --git a/ports/unix/gccollect.c b/ports/unix/gccollect.c index 231f016c2..f0441e4ea 100644 --- a/ports/unix/gccollect.c +++ b/ports/unix/gccollect.c @@ -29,146 +29,15 @@ #include "py/mpstate.h" #include "py/gc.h" -#if MICROPY_ENABLE_GC - -// Even if we have specific support for an architecture, it is -// possible to force use of setjmp-based implementation. -#if !MICROPY_GCREGS_SETJMP - -// We capture here callee-save registers, i.e. ones which may contain -// interesting values held there by our callers. It doesn't make sense -// to capture caller-saved registers, because they, well, put on the -// stack already by the caller. -#if defined(__x86_64__) -typedef mp_uint_t regs_t[6]; - -STATIC void gc_helper_get_regs(regs_t arr) { - register long rbx asm ("rbx"); - register long rbp asm ("rbp"); - register long r12 asm ("r12"); - register long r13 asm ("r13"); - register long r14 asm ("r14"); - register long r15 asm ("r15"); - #ifdef __clang__ - // TODO: - // This is dirty workaround for Clang. It tries to get around - // uncompliant (wrt to GCC) behavior of handling register variables. - // Application of this patch here is random, and done only to unbreak - // MacOS build. Better, cross-arch ways to deal with Clang issues should - // be found. - asm ("" : "=r" (rbx)); - asm ("" : "=r" (rbp)); - asm ("" : "=r" (r12)); - asm ("" : "=r" (r13)); - asm ("" : "=r" (r14)); - asm ("" : "=r" (r15)); - #endif - arr[0] = rbx; - arr[1] = rbp; - arr[2] = r12; - arr[3] = r13; - arr[4] = r14; - arr[5] = r15; -} - -#elif defined(__i386__) - -typedef mp_uint_t regs_t[4]; - -STATIC void gc_helper_get_regs(regs_t arr) { - register long ebx asm ("ebx"); - register long esi asm ("esi"); - register long edi asm ("edi"); - register long ebp asm ("ebp"); - #ifdef __clang__ - // TODO: - // This is dirty workaround for Clang. It tries to get around - // uncompliant (wrt to GCC) behavior of handling register variables. - // Application of this patch here is random, and done only to unbreak - // MacOS build. Better, cross-arch ways to deal with Clang issues should - // be found. - asm ("" : "=r" (ebx)); - asm ("" : "=r" (esi)); - asm ("" : "=r" (edi)); - asm ("" : "=r" (ebp)); - #endif - arr[0] = ebx; - arr[1] = esi; - arr[2] = edi; - arr[3] = ebp; -} - -#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__) - -typedef mp_uint_t regs_t[10]; +#include "lib/utils/gchelper.h" -STATIC void gc_helper_get_regs(regs_t arr) { - register long r4 asm ("r4"); - register long r5 asm ("r5"); - register long r6 asm ("r6"); - register long r7 asm ("r7"); - register long r8 asm ("r8"); - register long r9 asm ("r9"); - register long r10 asm ("r10"); - register long r11 asm ("r11"); - register long r12 asm ("r12"); - register long r13 asm ("r13"); - arr[0] = r4; - arr[1] = r5; - arr[2] = r6; - arr[3] = r7; - arr[4] = r8; - arr[5] = r9; - arr[6] = r10; - arr[7] = r11; - arr[8] = r12; - arr[9] = r13; -} - -#else - -// If we don't have architecture-specific optimized support, -// just fall back to setjmp-based implementation. -#undef MICROPY_GCREGS_SETJMP -#define MICROPY_GCREGS_SETJMP (1) - -#endif // Arch-specific selection -#endif // !MICROPY_GCREGS_SETJMP - -// If MICROPY_GCREGS_SETJMP was requested explicitly, or if -// we enabled it as a fallback above. -#if MICROPY_GCREGS_SETJMP -#include <setjmp.h> - -typedef jmp_buf regs_t; - -STATIC void gc_helper_get_regs(regs_t arr) { - setjmp(arr); -} - -#endif // MICROPY_GCREGS_SETJMP - -// this function is used by mpthreadport.c -MP_NOINLINE void gc_collect_regs_and_stack(void); - -// Explicitly mark this as noinline to make sure the regs variable -// is effectively at the top of the stack: otherwise, in builds where -// LTO is enabled and a lot of inlining takes place we risk a stack -// layout where regs is lower on the stack than pointers which have -// just been allocated but not yet marked, and get incorrectly sweeped. -MP_NOINLINE void gc_collect_regs_and_stack(void) { - regs_t regs; - gc_helper_get_regs(regs); - // GC stack (and regs because we captured them) - void **regs_ptr = (void **)(void *)®s; - gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)®s) / sizeof(uintptr_t)); -} +#if MICROPY_ENABLE_GC void gc_collect(void) { // gc_dump_info(); gc_collect_start(); - gc_collect_regs_and_stack(); + gc_helper_collect_regs_and_stack(); #if MICROPY_PY_THREAD mp_thread_gc_others(); #endif diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 5708ceab6..0565c0d76 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -178,14 +178,9 @@ extern const struct _mp_print_t mp_stderr_print; -// Define to 1 to use undertested inefficient GC helper implementation -// (if more efficient arch-specific one is not available). -#ifndef MICROPY_GCREGS_SETJMP - #ifdef __mips__ - #define MICROPY_GCREGS_SETJMP (1) - #else - #define MICROPY_GCREGS_SETJMP (0) - #endif +#if !defined(__x86_64__) || !defined(__i386__) || !defined(__thumb2__) || !defined(__thumb__) || !defined(__arm__) +// Fall back to setjmp() implementation for discovery of GC pointers in registers. +#define MICROPY_GCREGS_SETJMP (1) #endif #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) diff --git a/ports/unix/mpthreadport.c b/ports/unix/mpthreadport.c index 66c428940..711cf2a6b 100644 --- a/ports/unix/mpthreadport.c +++ b/ports/unix/mpthreadport.c @@ -39,6 +39,8 @@ #include <sched.h> #include <semaphore.h> +#include "lib/utils/gchelper.h" + // Some platforms don't have SIGRTMIN but if we do have it, use it to avoid // potential conflict with other uses of the more commonly used SIGUSR1. #ifdef SIGRTMIN @@ -88,8 +90,7 @@ STATIC void mp_thread_gc(int signo, siginfo_t *info, void *context) { (void)info; // unused (void)context; // unused if (signo == MP_THREAD_GC_SIGNAL) { - void gc_collect_regs_and_stack(void); - gc_collect_regs_and_stack(); + gc_helper_collect_regs_and_stack(); // We have access to the context (regs, stack) of the thread but it seems // that we don't need the extra information, enough is captured by the // gc_collect_regs_and_stack function above diff --git a/ports/unix/variants/minimal/mpconfigvariant.h b/ports/unix/variants/minimal/mpconfigvariant.h index e961ff4b2..25e1d20d5 100644 --- a/ports/unix/variants/minimal/mpconfigvariant.h +++ b/ports/unix/variants/minimal/mpconfigvariant.h @@ -110,14 +110,9 @@ extern const struct _mp_obj_module_t mp_module_os; // Do not change anything beyond this line ////////////////////////////////////////// -// Define to 1 to use undertested inefficient GC helper implementation -// (if more efficient arch-specific one is not available). -#ifndef MICROPY_GCREGS_SETJMP - #ifdef __mips__ - #define MICROPY_GCREGS_SETJMP (1) - #else - #define MICROPY_GCREGS_SETJMP (0) - #endif +#if !defined(__x86_64__) || !defined(__i386__) || !defined(__thumb2__) || !defined(__thumb__) || !defined(__arm__) +// Fall back to setjmp() implementation for discovery of GC pointers in registers. +#define MICROPY_GCREGS_SETJMP (1) #endif // type definitions for the specific machine |
