Skip to content

Commit 799225d

Browse files
Add PICO_CRT0_NEAR_CALLS to indicate that crt0 can call runtimine_init/main/exit etc via near calls (#2452)
--------- Co-authored-by: will-v-pi <108662275+will-v-pi@users.noreply.github.com>
1 parent 7cdb8ec commit 799225d

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/rp2_common/pico_crt0/crt0.S

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
#include "boot/picobin.h"
1616
#include "pico/bootrom.h"
1717

18+
// PICO_CONFIG: PICO_CRT0_NEAR_CALLS, Whether calls from crt0 into the binary are near (<16M away) - ignored for PICO_COPY_TO_RAM, default=0, type=bool, group=pico_crt0
19+
#ifndef PICO_CRT0_NEAR_CALLS
20+
#define PICO_CRT0_NEAR_CALLS 0
21+
#endif
22+
1823
#ifdef NDEBUG
1924
#ifndef COLLAPSE_IRQS
2025
#define COLLAPSE_IRQS
@@ -377,6 +382,12 @@ _entry_point:
377382
sev
378383
1:
379384
#endif
385+
#if !__ARM_ARCH_6M__
386+
// Make sure stack limit is 0 if we came in thru the debugger; we do not know what it should be
387+
movs r0, #0
388+
msr msplim, r0
389+
#endif
390+
380391
ldr r0, =__vectors
381392
// Vector through our own table (SP, VTOR will not have been set up at
382393
// this point). Same path for debugger entry and bootloader entry.
@@ -403,6 +414,9 @@ _enter_vtable_in_r0:
403414
.type _reset_handler,%function
404415
.thumb_func
405416
_reset_handler:
417+
// Note if we entered thru here on core 0, then we should have gone thru bootrom, so SP (and MSPLIM) on Armv8-M
418+
// should already be set
419+
406420
// Only core 0 should run the C runtime startup code; core 1 is normally
407421
// sleeping in the bootrom at this point but check to be sure (e.g. if
408422
// debugger put core 1 at the ELF entry point for some reason)
@@ -465,20 +479,20 @@ bss_fill_test:
465479
bne bss_fill_loop
466480

467481
platform_entry: // symbol for stack traces
482+
#if PICO_CRT0_NEAR_CALLS && !PICO_COPY_TO_RAM
483+
bl runtime_init
484+
bl main
485+
bl exit
486+
#else
468487
// Use 32-bit jumps, in case these symbols are moved out of branch range
469488
// (e.g. if main is in SRAM and crt0 in flash)
470-
#if !__ARM_ARCH_6M__
471-
// Make sure stack limit is 0 - the user can set it themselves
472-
// todo probably worth adding to the EXE_DEF in the future
473-
movs r0, #0
474-
msr msplim, r0
475-
#endif
476489
ldr r1, =runtime_init
477490
blx r1
478491
ldr r1, =main
479492
blx r1
480493
ldr r1, =exit
481494
blx r1
495+
#endif
482496
// exit should not return. If it does, hang the core.
483497
1: // separate label because _exit can be moved out of branch range
484498
bkpt #0

src/rp2_common/pico_runtime/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ endif()
6868
# FPGA_CHECK - checks for FPGA which allows Raspberry Pi to run your binary on FPGA;
6969
# PANIC - default panic impl which brings in stdio;
7070
# AUTO_INIT_MUTEX - auto init mutexes, without this you get no printf mutex either;
71+
# CRT0_FAR_CALLS - use blx not bl for calls from crt0 to user overridable functions;
7172
#
7273
# \param\ INCLUDE The items to include
7374
# \param\ EXCLUDE The items to exclude
@@ -148,4 +149,7 @@ function(pico_minimize_runtime TARGET)
148149
if (NOT RUNTIME_INCLUDE_FPGA_CHECK)
149150
target_compile_definitions(${TARGET} PRIVATE PICO_NO_FPGA_CHECK=1)
150151
endif()
151-
endfunction()
152+
if (NOT RUNTIME_CRT0_FAR_CALLS)
153+
target_compile_definitions(${TARGET} PRIVATE PICO_CRT0_NEAR_CALLS=1)
154+
endif()
155+
endfunction()

0 commit comments

Comments
 (0)