Skip to content

Factor out register saves #10325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "supervisor/cpu.h"
#include "supervisor/filesystem.h"
#include "supervisor/port.h"
#include "supervisor/shared/cpu_regs.h"
#include "supervisor/shared/reload.h"
#include "supervisor/shared/safe_mode.h"
#include "supervisor/shared/serial.h"
Expand Down Expand Up @@ -1135,9 +1136,14 @@ int __attribute__((used)) main(void) {
void gc_collect(void) {
gc_collect_start();

mp_uint_t regs[10];
// Load register values onto the stack. They get collected below with the rest of the stack.
size_t regs[SAVED_REGISTER_COUNT];
mp_uint_t sp = cpu_get_regs_and_sp(regs);

// This naively collects all object references from an approximate stack
// range.
gc_collect_root((void **)sp, ((mp_uint_t)port_stack_get_top() - sp) / sizeof(mp_uint_t));

// This collects root pointers from the VFS mount table. Some of them may
// have lost their references in the VM even though they are mounted.
gc_collect_root((void **)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
Expand Down Expand Up @@ -1170,9 +1176,6 @@ void gc_collect(void) {
common_hal_wifi_gc_collect();
#endif

// This naively collects all object references from an approximate stack
// range.
gc_collect_root((void **)sp, ((mp_uint_t)port_stack_get_top() - sp) / sizeof(mp_uint_t));
gc_collect_end();
}

Expand Down
5 changes: 3 additions & 2 deletions ports/analog/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ LINKERFILE = linking/$(MCU_VARIANT_LOWER)_cktpy.ld
LDFLAGS += -nostartfiles -specs=nano.specs
endif

SRC_S += supervisor/cpu.s \
$(STARTUPFILE)
SRC_S_UPPER = supervisor/shared/cpu_regs.S
SRC_S += $(STARTUPFILE)

# Needed to compile some MAX32 headers
CFLAGS += -D$(MCU_VARIANT_UPPER) \
Expand Down Expand Up @@ -236,6 +236,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
endif
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))

# List of sources for qstr extraction
Expand Down
34 changes: 0 additions & 34 deletions ports/analog/supervisor/cpu.s

This file was deleted.

4 changes: 2 additions & 2 deletions ports/atmel-samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ ifeq ($(CIRCUITPY_AUDIOBUSIO),1)
SRC_C += peripherals/samd/i2s.c peripherals/samd/$(PERIPHERALS_CHIP_FAMILY)/i2s.c
endif

SRC_S = supervisor/$(CHIP_FAMILY)_cpu.s
SRC_S_UPPER = supervisor/shared/cpu_regs.S

OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o))
Expand All @@ -317,7 +317,7 @@ ifeq ($(INTERNAL_LIBM),1)
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
endif
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))

QSTR_GLOBAL_REQUIREMENTS += $(HEADER_BUILD)/sdiodata.h
Expand Down
35 changes: 0 additions & 35 deletions ports/atmel-samd/supervisor/samd21_cpu.s

This file was deleted.

27 changes: 0 additions & 27 deletions ports/atmel-samd/supervisor/samd51_cpu.s

This file was deleted.

27 changes: 0 additions & 27 deletions ports/atmel-samd/supervisor/same51_cpu.s

This file was deleted.

27 changes: 0 additions & 27 deletions ports/atmel-samd/supervisor/same54_cpu.s

This file was deleted.

4 changes: 2 additions & 2 deletions ports/cxd56/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ LDFLAGS = \

CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_CXD56 -DCFG_TUD_MIDI_RX_BUFSIZE=512 -DCFG_TUD_CDC_RX_BUFSIZE=1024 -DCFG_TUD_MIDI_TX_BUFSIZE=512 -DCFG_TUD_CDC_TX_BUFSIZE=1024 -DCFG_TUD_MSC_BUFSIZE=512 $(CFLAGS_MOD)

SRC_S = supervisor/cpu.s
SRC_S_UPPER = supervisor/shared/cpu_regs.S

SRC_C += \
background.c \
Expand All @@ -120,7 +120,7 @@ SRC_C += \
lib/tinyusb/src/portable/sony/cxd56/dcd_cxd56.c \

OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED:.c=.o))
ifeq ($(INTERNAL_LIBM),1)
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
Expand Down
27 changes: 0 additions & 27 deletions ports/cxd56/supervisor/cpu.s

This file was deleted.

3 changes: 3 additions & 0 deletions ports/espressif/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,16 @@ FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py')
FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
endif

SRC_S_UPPER = supervisor/shared/cpu_regs.S

OBJ += $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED:.c=.o))
ifeq ($(INTERNAL_LIBM),1)
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
endif
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))

$(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os
$(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os
Expand Down
3 changes: 3 additions & 0 deletions ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ CIRCUITPY_ESP_FLASH_SIZE=4MB

CIRCUITPY_LEGACY_4MB_FLASH_LAYOUT = 1

# Not enough flash
CIRCUITPY_SOCKETPOOL_IPV6 = 0

CIRCUITPY_ESP_USB_SERIAL_JTAG = 1
6 changes: 2 additions & 4 deletions ports/espressif/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ void IRAM_ATTR mp_hal_delay_us(mp_uint_t delay) {
// This is provided by the esp-idf/components/xtensa/esp32s2/libhal.a binary blob.
#ifndef CONFIG_IDF_TARGET_ARCH_RISCV
extern void xthal_window_spill(void);
#endif

mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs) {
// xtensa has more registers than an instruction can address. The 16 that
Expand All @@ -37,8 +36,7 @@ mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs) {
// there is a HAL call to do it. There is a bit of a race condition here
// because the register value could change after it's been restored but that
// is unlikely to happen with a heap pointer while we do a GC.
#ifndef CONFIG_IDF_TARGET_ARCH_RISCV
xthal_window_spill();
#endif
return (mp_uint_t)__builtin_frame_address(0);
return (mp_uint_t)__builtin_stack_address();
}
#endif
7 changes: 4 additions & 3 deletions ports/litex/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ ifneq ($(CIRCUITPY_USB),0)
SRC_C += lib/tinyusb/src/portable/valentyusb/eptri/dcd_eptri.c
endif

SRC_S = \
crt0-vexriscv.S
SRC_S_UPPER = \
crt0-vexriscv.S \
supervisor/shared/cpu_regs.S

$(BUILD)/lib/tlsf/tlsf.o: CFLAGS += -Wno-cast-align

Expand All @@ -93,7 +94,7 @@ ifeq ($(INTERNAL_LIBM),1)
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
endif
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.S=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))

$(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os
Expand Down
6 changes: 0 additions & 6 deletions ports/litex/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,3 @@ void isr(void) {
// Decrease the "nesting count". Note: This should be going from 1 -> 0.
nesting_count -= 1;
}

mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs) {
unsigned long __tmp;
asm volatile ("mv %0, x2" : "=r" (__tmp));
return __tmp;
}
5 changes: 3 additions & 2 deletions ports/mimxrt10xx/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ SRC_C += \

endif

SRC_S = \
SRC_S_UPPER = \
sdk/devices/$(CHIP_FAMILY)/gcc/startup_$(CHIP_CORE).S \
supervisor/cpu.S
supervisor/shared/cpu_regs.S

OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_SDK:.c=.o))
Expand All @@ -173,6 +173,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
endif
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.S=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))

SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED) $(SRC_CIRCUITPY_COMMON)
Expand Down
27 changes: 0 additions & 27 deletions ports/mimxrt10xx/supervisor/cpu.S

This file was deleted.

Loading