From 2bec0562ebd7026e68bfeaf3aee88111a1759cd7 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 7 May 2025 17:01:45 -0700 Subject: [PATCH 1/2] Factor out register saves This fixes RISC-V ESP32 because it ignored registers on that architecture. --- main.c | 11 +- ports/analog/Makefile | 5 +- ports/analog/supervisor/cpu.s | 34 ------ ports/atmel-samd/Makefile | 4 +- ports/atmel-samd/supervisor/samd21_cpu.s | 35 ------ ports/atmel-samd/supervisor/samd51_cpu.s | 27 ----- ports/atmel-samd/supervisor/same51_cpu.s | 27 ----- ports/atmel-samd/supervisor/same54_cpu.s | 27 ----- ports/cxd56/Makefile | 4 +- ports/cxd56/supervisor/cpu.s | 27 ----- ports/espressif/Makefile | 3 + ports/espressif/mphalport.c | 6 +- ports/litex/Makefile | 7 +- ports/litex/mphalport.c | 6 -- ports/mimxrt10xx/Makefile | 5 +- ports/mimxrt10xx/supervisor/cpu.S | 27 ----- ports/nordic/Makefile | 4 +- ports/nordic/supervisor/cpu.s | 27 ----- ports/raspberrypi/Makefile | 2 +- ports/raspberrypi/supervisor/rp2_cpu.s | 35 ------ ports/renode/Makefile | 2 +- ports/renode/supervisor/cortex-m0+_cpu.s | 35 ------ ports/stm/Makefile | 3 +- ports/stm/supervisor/cpu.s | 27 ----- .../zephyr-cp/cptools/build_circuitpython.py | 2 +- supervisor/shared/cpu_regs.S | 102 ++++++++++++++++++ supervisor/shared/cpu_regs.h | 31 ++++++ 27 files changed, 166 insertions(+), 359 deletions(-) delete mode 100644 ports/analog/supervisor/cpu.s delete mode 100755 ports/atmel-samd/supervisor/samd21_cpu.s delete mode 100755 ports/atmel-samd/supervisor/samd51_cpu.s delete mode 100755 ports/atmel-samd/supervisor/same51_cpu.s delete mode 100755 ports/atmel-samd/supervisor/same54_cpu.s delete mode 100755 ports/cxd56/supervisor/cpu.s delete mode 100755 ports/mimxrt10xx/supervisor/cpu.S delete mode 100755 ports/nordic/supervisor/cpu.s delete mode 100755 ports/raspberrypi/supervisor/rp2_cpu.s delete mode 100755 ports/renode/supervisor/cortex-m0+_cpu.s delete mode 100755 ports/stm/supervisor/cpu.s create mode 100644 supervisor/shared/cpu_regs.S create mode 100644 supervisor/shared/cpu_regs.h diff --git a/main.c b/main.c index eefc081f33cc8..45b7af2725435 100644 --- a/main.c +++ b/main.c @@ -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" @@ -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)); @@ -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(); } diff --git a/ports/analog/Makefile b/ports/analog/Makefile index 8e4cf3bc91728..08b245c5897e3 100644 --- a/ports/analog/Makefile +++ b/ports/analog/Makefile @@ -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) \ @@ -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 diff --git a/ports/analog/supervisor/cpu.s b/ports/analog/supervisor/cpu.s deleted file mode 100644 index 7cb8291045f14..0000000000000 --- a/ports/analog/supervisor/cpu.s +++ /dev/null @@ -1,34 +0,0 @@ -// This file is part of the CircuitPython project: https://circuitpython.org -// -// SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries -// SPDX-FileCopyrightText: Copyright (c) 2024 Brandon Hurst, Analog Devices, Inc -// -// SPDX-License-Identifier: MIT - -.syntax unified -.cpu cortex-m4 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0], #4 -str r5, [r0], #4 -str r6, [r0], #4 -str r7, [r0], #4 -str r8, [r0], #4 -str r9, [r0], #4 -str r10, [r0], #4 -str r11, [r0], #4 -str r12, [r0], #4 -str r13, [r0], #4 - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index fa9bd923fdc4d..888729c7b7f3d 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -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)) @@ -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 diff --git a/ports/atmel-samd/supervisor/samd21_cpu.s b/ports/atmel-samd/supervisor/samd21_cpu.s deleted file mode 100755 index 741bb21358ad2..0000000000000 --- a/ports/atmel-samd/supervisor/samd21_cpu.s +++ /dev/null @@ -1,35 +0,0 @@ -.syntax unified -.cpu cortex-m0 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0, #0] -str r5, [r0, #4] -str r6, [r0, #8] -str r7, [r0, #12] -push {r1} -mov r1, r8 -str r1, [r0, #16] -mov r1, r9 -str r1, [r0, #20] -mov r1, r10 -str r1, [r0, #24] -mov r1, r11 -str r1, [r0, #28] -mov r1, r12 -str r1, [r0, #32] -mov r1, r13 -str r1, [r0, #36] -pop {r1} - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/atmel-samd/supervisor/samd51_cpu.s b/ports/atmel-samd/supervisor/samd51_cpu.s deleted file mode 100755 index 9e6807a5e2e99..0000000000000 --- a/ports/atmel-samd/supervisor/samd51_cpu.s +++ /dev/null @@ -1,27 +0,0 @@ -.syntax unified -.cpu cortex-m4 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0], #4 -str r5, [r0], #4 -str r6, [r0], #4 -str r7, [r0], #4 -str r8, [r0], #4 -str r9, [r0], #4 -str r10, [r0], #4 -str r11, [r0], #4 -str r12, [r0], #4 -str r13, [r0], #4 - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/atmel-samd/supervisor/same51_cpu.s b/ports/atmel-samd/supervisor/same51_cpu.s deleted file mode 100755 index 9e6807a5e2e99..0000000000000 --- a/ports/atmel-samd/supervisor/same51_cpu.s +++ /dev/null @@ -1,27 +0,0 @@ -.syntax unified -.cpu cortex-m4 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0], #4 -str r5, [r0], #4 -str r6, [r0], #4 -str r7, [r0], #4 -str r8, [r0], #4 -str r9, [r0], #4 -str r10, [r0], #4 -str r11, [r0], #4 -str r12, [r0], #4 -str r13, [r0], #4 - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/atmel-samd/supervisor/same54_cpu.s b/ports/atmel-samd/supervisor/same54_cpu.s deleted file mode 100755 index 9e6807a5e2e99..0000000000000 --- a/ports/atmel-samd/supervisor/same54_cpu.s +++ /dev/null @@ -1,27 +0,0 @@ -.syntax unified -.cpu cortex-m4 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0], #4 -str r5, [r0], #4 -str r6, [r0], #4 -str r7, [r0], #4 -str r8, [r0], #4 -str r9, [r0], #4 -str r10, [r0], #4 -str r11, [r0], #4 -str r12, [r0], #4 -str r13, [r0], #4 - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile index ca97c33ca70ea..fdf3646c80d27 100644 --- a/ports/cxd56/Makefile +++ b/ports/cxd56/Makefile @@ -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 \ @@ -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)) diff --git a/ports/cxd56/supervisor/cpu.s b/ports/cxd56/supervisor/cpu.s deleted file mode 100755 index 9e6807a5e2e99..0000000000000 --- a/ports/cxd56/supervisor/cpu.s +++ /dev/null @@ -1,27 +0,0 @@ -.syntax unified -.cpu cortex-m4 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0], #4 -str r5, [r0], #4 -str r6, [r0], #4 -str r7, [r0], #4 -str r8, [r0], #4 -str r9, [r0], #4 -str r10, [r0], #4 -str r11, [r0], #4 -str r12, [r0], #4 -str r13, [r0], #4 - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/espressif/Makefile b/ports/espressif/Makefile index bdf0fd53f6876..8152fc78ee031 100644 --- a/ports/espressif/Makefile +++ b/ports/espressif/Makefile @@ -486,6 +486,8 @@ 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) @@ -493,6 +495,7 @@ 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 diff --git a/ports/espressif/mphalport.c b/ports/espressif/mphalport.c index d6a7ef1bfce41..9046bbf7b102f 100644 --- a/ports/espressif/mphalport.c +++ b/ports/espressif/mphalport.c @@ -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 @@ -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 diff --git a/ports/litex/Makefile b/ports/litex/Makefile index 688a1887959c8..bb1d88e755201 100644 --- a/ports/litex/Makefile +++ b/ports/litex/Makefile @@ -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 @@ -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 diff --git a/ports/litex/mphalport.c b/ports/litex/mphalport.c index cc74762a14f14..b94699e81e705 100644 --- a/ports/litex/mphalport.c +++ b/ports/litex/mphalport.c @@ -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; -} diff --git a/ports/mimxrt10xx/Makefile b/ports/mimxrt10xx/Makefile index bbbb2479721ae..57a8beb069129 100644 --- a/ports/mimxrt10xx/Makefile +++ b/ports/mimxrt10xx/Makefile @@ -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)) @@ -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) diff --git a/ports/mimxrt10xx/supervisor/cpu.S b/ports/mimxrt10xx/supervisor/cpu.S deleted file mode 100755 index 9e6807a5e2e99..0000000000000 --- a/ports/mimxrt10xx/supervisor/cpu.S +++ /dev/null @@ -1,27 +0,0 @@ -.syntax unified -.cpu cortex-m4 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0], #4 -str r5, [r0], #4 -str r6, [r0], #4 -str r7, [r0], #4 -str r8, [r0], #4 -str r9, [r0], #4 -str r10, [r0], #4 -str r11, [r0], #4 -str r12, [r0], #4 -str r13, [r0], #4 - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/nordic/Makefile b/ports/nordic/Makefile index 9633dfdd3a824..e60b4072a8d48 100755 --- a/ports/nordic/Makefile +++ b/ports/nordic/Makefile @@ -146,7 +146,7 @@ SRC_C += $(SRC_DCD) $(patsubst %.c,$(BUILD)/%.o,$(SRC_DCD)): CFLAGS += -Wno-missing-prototypes endif # CIRCUITPY_USB_DEVICE -SRC_S = supervisor/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_NRFX:.c=.o)) @@ -155,7 +155,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)) # nrfx uses undefined preprocessor variables quite casually, so we can't do diff --git a/ports/nordic/supervisor/cpu.s b/ports/nordic/supervisor/cpu.s deleted file mode 100755 index 9e6807a5e2e99..0000000000000 --- a/ports/nordic/supervisor/cpu.s +++ /dev/null @@ -1,27 +0,0 @@ -.syntax unified -.cpu cortex-m4 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0], #4 -str r5, [r0], #4 -str r6, [r0], #4 -str r7, [r0], #4 -str r8, [r0], #4 -str r9, [r0], #4 -str r10, [r0], #4 -str r11, [r0], #4 -str r12, [r0], #4 -str r13, [r0], #4 - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index e9090f5fbe24e..582c04282734c 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -660,13 +660,13 @@ else OBJ_MBEDTLS := endif -SRC_S = supervisor/$(CHIP_FAMILY)_cpu.s BOOT2_S_CFLAGS ?= -DPICO_FLASH_SPI_CLKDIV=4 SRC_S_UPPER = sdk/src/rp2_common/hardware_irq/irq_handler_chain.S \ sdk/src/rp2_common/pico_bit_ops/bit_ops_aeabi.S \ sdk/src/rp2_common/pico_double/double_aeabi_$(DOUBLE_EABI).S \ sdk/src/rp2_common/pico_int64_ops/pico_int64_ops_aeabi.S \ sdk/src/rp2_common/pico_crt0/crt0.S \ + supervisor/shared/cpu_regs.S \ $(SRC_S_UPPER_CHIP_VARIANT) ifeq ($(CIRCUITPY_PICODVI),1) diff --git a/ports/raspberrypi/supervisor/rp2_cpu.s b/ports/raspberrypi/supervisor/rp2_cpu.s deleted file mode 100755 index 741bb21358ad2..0000000000000 --- a/ports/raspberrypi/supervisor/rp2_cpu.s +++ /dev/null @@ -1,35 +0,0 @@ -.syntax unified -.cpu cortex-m0 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0, #0] -str r5, [r0, #4] -str r6, [r0, #8] -str r7, [r0, #12] -push {r1} -mov r1, r8 -str r1, [r0, #16] -mov r1, r9 -str r1, [r0, #20] -mov r1, r10 -str r1, [r0, #24] -mov r1, r11 -str r1, [r0, #28] -mov r1, r12 -str r1, [r0, #32] -mov r1, r13 -str r1, [r0, #36] -pop {r1} - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/renode/Makefile b/ports/renode/Makefile index 170198455fbf5..22e32f9d2fb6e 100644 --- a/ports/renode/Makefile +++ b/ports/renode/Makefile @@ -45,7 +45,7 @@ SRC_C += \ background.c \ mphalport.c \ -SRC_S = supervisor/$(CPU)_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_COMMON_HAL_SHARED_MODULE_EXPANDED:.c=.o)) diff --git a/ports/renode/supervisor/cortex-m0+_cpu.s b/ports/renode/supervisor/cortex-m0+_cpu.s deleted file mode 100755 index 741bb21358ad2..0000000000000 --- a/ports/renode/supervisor/cortex-m0+_cpu.s +++ /dev/null @@ -1,35 +0,0 @@ -.syntax unified -.cpu cortex-m0 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0, #0] -str r5, [r0, #4] -str r6, [r0, #8] -str r7, [r0, #12] -push {r1} -mov r1, r8 -str r1, [r0, #16] -mov r1, r9 -str r1, [r0, #20] -mov r1, r10 -str r1, [r0, #24] -mov r1, r11 -str r1, [r0, #28] -mov r1, r12 -str r1, [r0, #32] -mov r1, r13 -str r1, [r0, #36] -pop {r1} - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/stm/Makefile b/ports/stm/Makefile index 4bacecf60688a..baea6893a33d9 100755 --- a/ports/stm/Makefile +++ b/ports/stm/Makefile @@ -212,8 +212,8 @@ ifneq ($(CIRCUITPY_USB),0) endif endif +SRC_S_UPPER = supervisor/shared/cpu_regs.S SRC_S = \ - supervisor/cpu.s \ st_driver/cmsis_device_$(MCU_SERIES_LOWER)/Source/Templates/gcc/startup_$(MCU_VARIANT_LOWER).s ifneq ($(FROZEN_MPY_DIR),) @@ -229,6 +229,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)) $(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os diff --git a/ports/stm/supervisor/cpu.s b/ports/stm/supervisor/cpu.s deleted file mode 100755 index 9e6807a5e2e99..0000000000000 --- a/ports/stm/supervisor/cpu.s +++ /dev/null @@ -1,27 +0,0 @@ -.syntax unified -.cpu cortex-m4 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0], #4 -str r5, [r0], #4 -str r6, [r0], #4 -str r7, [r0], #4 -str r8, [r0], #4 -str r9, [r0], #4 -str r10, [r0], #4 -str r11, [r0], #4 -str r12, [r0], #4 -str r13, [r0], #4 - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/zephyr-cp/cptools/build_circuitpython.py b/ports/zephyr-cp/cptools/build_circuitpython.py index 905b281f7ea96..15ed46b794e22 100644 --- a/ports/zephyr-cp/cptools/build_circuitpython.py +++ b/ports/zephyr-cp/cptools/build_circuitpython.py @@ -575,7 +575,7 @@ async def build_circuitpython(): source_files.append(portdir / "common-hal/zephyr_kernel/__init__.c") # source_files.append(srcdir / "ports" / port / "peripherals" / "nrf" / "nrf52840" / "pins.c") - assembly_files.append(srcdir / "ports/nordic/supervisor/cpu.s") + assembly_files.append(srcdir / "supervisor/shared/cpu_regs.S") source_files.extend(assembly_files) diff --git a/supervisor/shared/cpu_regs.S b/supervisor/shared/cpu_regs.S new file mode 100644 index 0000000000000..90e5367ed5808 --- /dev/null +++ b/supervisor/shared/cpu_regs.S @@ -0,0 +1,102 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "supervisor/shared/cpu_regs.h" + +#ifdef __arm__ +.syntax unified +.thumb +.text +.align 2 + +@ uint cpu_get_regs_and_sp(r0=uint regs[SAVED_REGISTER_COUNT]) +.global cpu_get_regs_and_sp +.thumb +.thumb_func +.type cpu_get_regs_and_sp, %function +cpu_get_regs_and_sp: +#if __ARM_ARCH_ISA_THUMB == 2 +@ store registers into given array +#ifdef __arm__ +stmia r0!, {r4-r11} +#endif +#if defined(__aarch64__) && __aarch64__ == 1 +#error "aarch64 not supported" +stmia r0!, {x19-x28} +#endif +#ifdef __ARM_FP +#ifdef __arm__ +vstmia r0!, {s16-s31} +#endif +#if defined(__aarch64__) && __aarch64__ == 1 +vst1.64 {d8-d15}, [r0], #16 +#endif +#endif +#endif +// Thumb 1 can only store directly from R0-R7. This is M0 and M23 mostly. +#if __ARM_ARCH_ISA_THUMB == 1 +str r4, [r0, #0] +str r5, [r0, #4] +str r6, [r0, #8] +str r7, [r0, #12] +push {r1} +mov r1, r8 +str r1, [r0, #16] +mov r1, r9 +str r1, [r0, #20] +mov r1, r10 +str r1, [r0, #24] +mov r1, r11 +str r1, [r0, #28] +mov r1, r12 +str r1, [r0, #32] +mov r1, r13 +str r1, [r0, #36] +pop {r1} +#endif + +@ return the sp +mov r0, sp +bx lr +#endif + +#ifdef __riscv +#if __riscv_xlen == 32 +.global cpu_get_regs_and_sp +.type cpu_get_regs_and_sp, %function +cpu_get_regs_and_sp: +sw s0, 0(a0) +sw s1, 4(a0) +sw s2, 8(a0) +sw s3, 12(a0) +sw s4, 16(a0) +sw s5, 20(a0) +sw s6, 24(a0) +sw s7, 28(a0) +sw s8, 32(a0) +sw s9, 36(a0) +sw s10, 40(a0) +sw s11, 44(a0) +#ifdef __riscv_vector +sw fs0, 48(a0) +sw fs1, 52(a0) +sw fs2, 56(a0) +sw fs3, 60(a0) +sw fs4, 64(a0) +sw fs5, 68(a0) +sw fs6, 72(a0) +sw fs7, 76(a0) +sw fs8, 80(a0) +sw fs9, 84(a0) +sw fs10, 88(a0) +sw fs11, 92(a0) +#endif +move a0, sp +ret +#else +#error "Unsupported RISC-V bit length" +#endif +#endif diff --git a/supervisor/shared/cpu_regs.h b/supervisor/shared/cpu_regs.h new file mode 100644 index 0000000000000..aff0b69ebab0c --- /dev/null +++ b/supervisor/shared/cpu_regs.h @@ -0,0 +1,31 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2025 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#ifdef __arm__ +#define INTEGER_REGS 8 +#ifdef __ARM_FP +#define FLOATING_POINT_REGS 16 +#endif +#endif + +#ifdef __aarch64__ +#define INTEGER_REGS 10 +#ifdef __ARM_FP +#define FLOATING_POINT_REGS 8 +#endif +#endif + +#ifndef INTEGER_REGS +#define INTEGER_REGS 0 +#endif + +#ifndef FLOATING_POINT_REGS +#define FLOATING_POINT_REGS 0 +#endif + +#define SAVED_REGISTER_COUNT (INTEGER_REGS + FLOATING_POINT_REGS) From e6271b2d7760e539e0d57493daaf4d9853348279 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 9 May 2025 11:23:26 -0700 Subject: [PATCH 2/2] Shrink lolin_c3_mini --- ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk b/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk index 36dc38883ff39..2a8d4c97b69a7 100644 --- a/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk +++ b/ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk @@ -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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy