Skip to content

Commit 5ba4f77

Browse files
authored
Merge pull request #10325 from tannewt/collect_rv32_registers
Factor out register saves
2 parents d0776d3 + e6271b2 commit 5ba4f77

File tree

28 files changed

+169
-359
lines changed

28 files changed

+169
-359
lines changed

main.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "supervisor/cpu.h"
3131
#include "supervisor/filesystem.h"
3232
#include "supervisor/port.h"
33+
#include "supervisor/shared/cpu_regs.h"
3334
#include "supervisor/shared/reload.h"
3435
#include "supervisor/shared/safe_mode.h"
3536
#include "supervisor/shared/serial.h"
@@ -1135,9 +1136,14 @@ int __attribute__((used)) main(void) {
11351136
void gc_collect(void) {
11361137
gc_collect_start();
11371138

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

1143+
// This naively collects all object references from an approximate stack
1144+
// range.
1145+
gc_collect_root((void **)sp, ((mp_uint_t)port_stack_get_top() - sp) / sizeof(mp_uint_t));
1146+
11411147
// This collects root pointers from the VFS mount table. Some of them may
11421148
// have lost their references in the VM even though they are mounted.
11431149
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) {
11701176
common_hal_wifi_gc_collect();
11711177
#endif
11721178

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

ports/analog/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ LINKERFILE = linking/$(MCU_VARIANT_LOWER)_cktpy.ld
138138
LDFLAGS += -nostartfiles -specs=nano.specs
139139
endif
140140

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

144144
# Needed to compile some MAX32 headers
145145
CFLAGS += -D$(MCU_VARIANT_UPPER) \
@@ -236,6 +236,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
236236
endif
237237
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
238238
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
239+
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
239240
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
240241

241242
# List of sources for qstr extraction

ports/analog/supervisor/cpu.s

-34
This file was deleted.

ports/atmel-samd/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ ifeq ($(CIRCUITPY_AUDIOBUSIO),1)
308308
SRC_C += peripherals/samd/i2s.c peripherals/samd/$(PERIPHERALS_CHIP_FAMILY)/i2s.c
309309
endif
310310

311-
SRC_S = supervisor/$(CHIP_FAMILY)_cpu.s
311+
SRC_S_UPPER = supervisor/shared/cpu_regs.S
312312

313313
OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
314314
OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o))
@@ -317,7 +317,7 @@ ifeq ($(INTERNAL_LIBM),1)
317317
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
318318
endif
319319
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
320-
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
320+
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
321321
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
322322

323323
QSTR_GLOBAL_REQUIREMENTS += $(HEADER_BUILD)/sdiodata.h

ports/atmel-samd/supervisor/samd21_cpu.s

-35
This file was deleted.

ports/atmel-samd/supervisor/samd51_cpu.s

-27
This file was deleted.

ports/atmel-samd/supervisor/same51_cpu.s

-27
This file was deleted.

ports/atmel-samd/supervisor/same54_cpu.s

-27
This file was deleted.

ports/cxd56/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ LDFLAGS = \
110110

111111
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)
112112

113-
SRC_S = supervisor/cpu.s
113+
SRC_S_UPPER = supervisor/shared/cpu_regs.S
114114

115115
SRC_C += \
116116
background.c \
@@ -120,7 +120,7 @@ SRC_C += \
120120
lib/tinyusb/src/portable/sony/cxd56/dcd_cxd56.c \
121121

122122
OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
123-
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
123+
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
124124
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED:.c=.o))
125125
ifeq ($(INTERNAL_LIBM),1)
126126
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))

ports/cxd56/supervisor/cpu.s

-27
This file was deleted.

ports/espressif/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,16 @@ FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py')
490490
FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
491491
endif
492492

493+
SRC_S_UPPER = supervisor/shared/cpu_regs.S
494+
493495
OBJ += $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
494496
OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED:.c=.o))
495497
ifeq ($(INTERNAL_LIBM),1)
496498
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
497499
endif
498500
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
499501
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
502+
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
500503

501504
$(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os
502505
$(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os

ports/espressif/boards/lolin_c3_mini/mpconfigboard.mk

+3
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ CIRCUITPY_ESP_FLASH_SIZE=4MB
99

1010
CIRCUITPY_LEGACY_4MB_FLASH_LAYOUT = 1
1111

12+
# Not enough flash
13+
CIRCUITPY_SOCKETPOOL_IPV6 = 0
14+
1215
CIRCUITPY_ESP_USB_SERIAL_JTAG = 1
1316
CIRCUITPY_TILEPALETTEMAPPER = 0

ports/espressif/mphalport.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ void IRAM_ATTR mp_hal_delay_us(mp_uint_t delay) {
2121
// This is provided by the esp-idf/components/xtensa/esp32s2/libhal.a binary blob.
2222
#ifndef CONFIG_IDF_TARGET_ARCH_RISCV
2323
extern void xthal_window_spill(void);
24-
#endif
2524

2625
mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs) {
2726
// 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) {
3736
// there is a HAL call to do it. There is a bit of a race condition here
3837
// because the register value could change after it's been restored but that
3938
// is unlikely to happen with a heap pointer while we do a GC.
40-
#ifndef CONFIG_IDF_TARGET_ARCH_RISCV
4139
xthal_window_spill();
42-
#endif
43-
return (mp_uint_t)__builtin_frame_address(0);
40+
return (mp_uint_t)__builtin_stack_address();
4441
}
42+
#endif

ports/litex/Makefile

+4-3
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ ifneq ($(CIRCUITPY_USB),0)
7777
SRC_C += lib/tinyusb/src/portable/valentyusb/eptri/dcd_eptri.c
7878
endif
7979

80-
SRC_S = \
81-
crt0-vexriscv.S
80+
SRC_S_UPPER = \
81+
crt0-vexriscv.S \
82+
supervisor/shared/cpu_regs.S
8283

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

@@ -93,7 +94,7 @@ ifeq ($(INTERNAL_LIBM),1)
9394
OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
9495
endif
9596
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
96-
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.S=.o))
97+
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
9798
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
9899

99100
$(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os

ports/litex/mphalport.c

-6
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,3 @@ void isr(void) {
5454
// Decrease the "nesting count". Note: This should be going from 1 -> 0.
5555
nesting_count -= 1;
5656
}
57-
58-
mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs) {
59-
unsigned long __tmp;
60-
asm volatile ("mv %0, x2" : "=r" (__tmp));
61-
return __tmp;
62-
}

ports/mimxrt10xx/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ SRC_C += \
161161

162162
endif
163163

164-
SRC_S = \
164+
SRC_S_UPPER = \
165165
sdk/devices/$(CHIP_FAMILY)/gcc/startup_$(CHIP_CORE).S \
166-
supervisor/cpu.S
166+
supervisor/shared/cpu_regs.S
167167

168168
OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
169169
OBJ += $(addprefix $(BUILD)/, $(SRC_SDK:.c=.o))
@@ -173,6 +173,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o))
173173
endif
174174
OBJ += $(addprefix $(BUILD)/, $(SRC_CIRCUITPY_COMMON:.c=.o))
175175
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.S=.o))
176+
OBJ += $(addprefix $(BUILD)/, $(SRC_S_UPPER:.S=.o))
176177
OBJ += $(addprefix $(BUILD)/, $(SRC_MOD:.c=.o))
177178

178179
SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED) $(SRC_CIRCUITPY_COMMON)

ports/mimxrt10xx/supervisor/cpu.S

-27
This file was deleted.

0 commit comments

Comments
 (0)