-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
lib/libc: Add picolibc support (aarch32, aarch64 and RISC-V) [v14] #39563
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (c) 2020 Intel Corporation | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <toolchain.h> | ||
|
||
_ASM_FILE_PROLOGUE | ||
|
||
GTEXT(__aeabi_read_tp) | ||
|
||
SECTION_FUNC(text, __aeabi_read_tp) | ||
/* | ||
* Load TLS base address, which is stored in the TPIDRURO register, also | ||
* known as the "Process ID" register. Refer to the code in z_arm_pendsv | ||
* to see where this register is set. | ||
*/ | ||
mrc 15, 0, r0, c13, c0, 3 | ||
bx lr |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,15 @@ config MINIMAL_LIBC | |
help | ||
Build with minimal C library. | ||
|
||
config PICOLIBC | ||
bool "Picolibc library" | ||
depends on !NATIVE_APPLICATION | ||
select THREAD_LOCAL_STORAGE if ARCH_HAS_THREAD_LOCAL_STORAGE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering why TLS is always enabled. Can picolib work without TLS if choose to do so? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TLS must be enabled in zephyr if picolibc is built with TLS support. Otherwise, any TLS variables in picolibc will not work. I'm using 'ARCH_HAS_THREAD_LOCAL_STORAGE' as a proxy for whether picolibc was built with TLS support as I don't know how to check for that at configure time (picolibc has settings in picolibc.h which could be used, is there some way to look at those? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If picolibc TLS is also always enabled when building for those architectures, this is fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, that depends on the toolchain. ARM ships their GCC embedded toolchain with TLS disabled. As there is no impact on code not using TLS in either case, I assume the ARM people just don't realize they've disabled it. Crosstool-ng and debian both ship with toolchains that enable TLS support in the compiler and picolibc. Once the sdk-ng PR for picolibc is merged, then zephyr can rely on that toolchain for picolibc, where TLS is enabled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, might want to add "&& !(arm toolchain)" for the if condition, since TLS is not required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be a lot better to figure out if picolibc has TLS support automatically; anyone know enough cmake to do that? |
||
select LIBC_ERRNO | ||
help | ||
Build with picolibc library. The picolibc library is expected to be | ||
part of the SDK in this case. | ||
|
||
config NEWLIB_LIBC | ||
bool "Newlib C library" | ||
depends on !NATIVE_APPLICATION | ||
|
@@ -53,6 +62,30 @@ endchoice # LIBC_IMPLEMENTATION | |
config HAS_NEWLIB_LIBC_NANO | ||
bool | ||
|
||
if PICOLIBC | ||
|
||
config PICOLIBC_INTEGER_PRINTF | ||
bool "Build with picolibc integer-only printf" | ||
help | ||
Build with floating point printf disabled. This will reduce the size | ||
of the image. | ||
|
||
config PICOLIBC_ALIGNED_HEAP_SIZE | ||
int "Picolibc aligned heap size (bytes)" | ||
depends on MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT | ||
depends on USERSPACE | ||
default 0 | ||
help | ||
If user mode is enabled, and MPU hardware has requirements that | ||
regions be sized to a power of two and aligned to their size, | ||
and user mode threads need to access this heap, then this is necessary | ||
to properly define an MPU region for the heap. | ||
|
||
If this is left at 0, then remaining system RAM will be used for this | ||
area and it may not be possible to program it as an MPU region. | ||
|
||
endif # PICOLIBC | ||
|
||
if NEWLIB_LIBC | ||
|
||
config NEWLIB_LIBC_NANO | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
zephyr_library() | ||
zephyr_library_sources(libc-hooks.c) | ||
|
||
# Zephyr normally uses -ffreestanding, which with current GNU toolchains | ||
# means that the flag macros used by picolibc <inttypes.h> to signal | ||
# support for PRI.64 macros are not present. To make them available we | ||
# need to hook into the include path before the system files and | ||
# explicitly include the picolibc header that provides those macros. | ||
zephyr_include_directories(include) | ||
|
||
# define __LINUX_ERRNO_EXTENSIONS__ so we get errno defines like -ESHUTDOWN | ||
# used by the network stack | ||
zephyr_compile_definitions(__LINUX_ERRNO_EXTENSIONS__) | ||
|
||
zephyr_link_libraries( | ||
m | ||
c | ||
gcc # Lib C depends on libgcc. | ||
) | ||
|
||
# The -T/dev/null avoids pulling in picolibc.ld | ||
zephyr_link_libraries( | ||
--specs=picolibc.specs | ||
-T/dev/null | ||
) | ||
|
||
zephyr_compile_options( | ||
--specs=picolibc.specs | ||
-D_GNU_SOURCE | ||
) | ||
|
||
if(CONFIG_PICOLIBC_INTEGER_PRINTF) | ||
zephyr_compile_options( | ||
-DPICOLIBC_INTEGER_PRINTF_SCANF | ||
) | ||
zephyr_link_libraries( | ||
-DPICOLIBC_INTEGER_PRINTF_SCANF | ||
) | ||
endif() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any chance that you could instead make this as a cmsis function, probably putting it in include/arch/arm/aarch32/cortex_a_r/cmsis.h until it could be moved to the true arm cmsis module? I assume you probably copied this structure from Cortex-M, but since this uses a mrc, it makes sense to abstract this access.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other side of this operation, storing the TLS value into the register, is done in swap_helper.S without any abstraction, so I'm not sure it makes sense to create an abstraction here?
I also don't think we should use this function anywhere; it's a part of EABI, but should only be used in code compiled with -mtp=soft. However, as Zephyr is the code base storing the TLS base address, it makes sense for Zephyr to offer the standard ABI that also fetches it, so I went ahead and added it.