Skip to content

Commit

Permalink
lib/libc: Add picolibc support (aarch32, aarch64 and RISC-V) [v15]
Browse files Browse the repository at this point in the history
Picolibc is a fork of newlib designed and tested on embedded systems. It
offers a smaller memory footprint (both ROM and RAM), and native TLS
support, which uses the Zephyr TLS support.

By default, the full printf version is included in the executable, which
includes exact floating point and long long input and output. A
configuration option has been added to switch to the integer-only
version (which also omits long long support).

Here are some size comparisons using qemu-cortex-m3 and this application
(parameters passed to printf to avoid GCC optimizing it into puts):

void main(void)
{
    printf("Hello World! %s %d\n", CONFIG_BOARD, 12);
}

                       FLASH    SRAM
    minimal             8696    3952
    picolibc int        7600    3960
    picolibc float     12304    3960
    newlib-nano int    11696    4128
    newlib-nano float  30516    4496
    newlib             34800    6112

---

v2:
	Include picolibc-tls.ld

v3:
	Document usage in guides/c_library.rst and
	getting_started/toolchain_other_x_compilers.rst

v4:
	Lost the lib/libc/picolibc directory somehow!

v5:
	Add PICOLIBC_ALIGNED_HEAP_SIZE configuration option.
	Delete PICOLIBC_SEMIHOST option support code

v6:
	Don't allocate static RAM for TLS values; TLS
	values only need to be allocated for each thread.

v7:
	Use arm coprocessor for TLS pointer storage where supported for
	compatibility with the -mtp=cp15 compiler option (or when the
	target cpu type selects this option)

	Add a bunch of tests

	Round TLS segment up to stack alignment so that overall stack
	remains correctly aligned

	Add aarch64 support

	Rebase to upstream head

v8:
	Share NEWLIB, NEWLIB_NANO and PICOLIBC library configuration
	variables in a single LIBC_PARTITIONS variable instead of
	having separate PICOLIBC_PART and NEWLIB_PART variables.

v9:
	Update docs to reference pending sdk-ng support for picolibc

v10:
	Support memory protection by creating a partition for
	picolibc shared data and any pre-defined picolibc heap.

v11:
	Fix formatting in arch/arm/core/aarch64/switch.S

v12:
	Remove TLS support from this patch now that TLS is upstream
	Require THREAD_LOCAL_STORAGE when using PICOLIBC for architectures
	that support it.

v13:
	Merge errno changes as they're only needed for picolibc.
	Adapt cmake changes suggested by Torsten Tejlmand Rasmussen

v14:
	Update to picolibc 1.7 and newer (new stdin/stdout/stderr ABI)

v15:
	Respond to comments from dcpleung:
	* switch kernel/errno to use CONFIG_LIBC_ERRNO instead of
          CONFIG_PICOLIBC
	* Add comment to test/lib/sprintf as to why the %n test
	  was disabled for picolibc.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Jan 4, 2022
1 parent 8e9000c commit fe48e9c
Show file tree
Hide file tree
Showing 18 changed files with 421 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,9 @@ if(CONFIG_USERSPACE)
if(CONFIG_NEWLIB_LIBC_NANO)
set(NEWLIB_PART -l libc_nano.a z_libc_partition)
endif()
if(CONFIG_PICOLIBC)
set(NEWLIB_PART -l libc.a z_libc_partition)
endif()

add_custom_command(
OUTPUT ${APP_SMEM_UNALIGNED_LD} ${APP_SMEM_PINNED_UNALIGNED_LD}
Expand Down
7 changes: 5 additions & 2 deletions arch/arm/core/aarch32/cortex_a_r/__aeabi_read_tp.S
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ _ASM_FILE_PROLOGUE

GTEXT(__aeabi_read_tp)

GDATA(z_arm_tls_ptr)

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
1 change: 1 addition & 0 deletions cmake/toolchain/gnuarmemb/generic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ set(SYSROOT_TARGET arm-none-eabi)
set(CROSS_COMPILE ${TOOLCHAIN_HOME}/bin/${CROSS_COMPILE_TARGET}-)
set(SYSROOT_DIR ${TOOLCHAIN_HOME}/${SYSROOT_TARGET})
set(TOOLCHAIN_HAS_NEWLIB ON CACHE BOOL "True if toolchain supports newlib")
set(TOOLCHAIN_HAS_PICOLIBC ON CACHE BOOL "True if toolchain supports picolibc")

message(STATUS "Found toolchain: gnuarmemb (${GNUARMEMB_TOOLCHAIN_PATH})")
1 change: 1 addition & 0 deletions cmake/toolchain/xtools/generic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(SYSROOT_TARGET ${CROSS_COMPILE_TARGET})
set(CROSS_COMPILE ${some_toolchain_root}/${CROSS_COMPILE_TARGET}/bin/${CROSS_COMPILE_TARGET}-)
set(SYSROOT_DIR ${some_toolchain_root}/${SYSROOT_TARGET}/${SYSROOT_TARGET})
set(TOOLCHAIN_HAS_NEWLIB ON CACHE BOOL "True if toolchain supports newlib")
set(TOOLCHAIN_HAS_PICOLIBC ON CACHE BOOL "True if toolchain supports picolibc")

unset(some_toolchain_root)
unset(some_toolchain)
Expand Down
20 changes: 17 additions & 3 deletions include/sys/libc-hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* that need to call into the kernel as system calls
*/

#if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_ARCMWDT_LIBC)
#if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_ARCMWDT_LIBC) || defined(CONFIG_PICOLIBC)

/* syscall generation ignores preprocessor, ensure this is defined to ensure
* we don't have compile errors
Expand Down Expand Up @@ -56,7 +56,21 @@ extern struct k_mem_partition z_malloc_partition;
*/
#define Z_MALLOC_PARTITION_EXISTS 1
#endif /* CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE > 0 */
#endif /* CONFIG_MINIMAL_LIBC */
#elif defined(CONFIG_PICOLIBC)
/* If we are using picolibc, the heap arena is in one of two areas:
* - If we have an MPU that requires power of two alignment, the heap bounds
* must be specified in Kconfig via CONFIG_PICOLIBC_ALIGNED_HEAP_SIZE.
* - Otherwise, the heap arena on most arches starts at a suitably
* aligned base addreess after the `_end` linker symbol, through to the end
* of system RAM.
*/
#if (!defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) || \
(defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT) && \
CONFIG_PICOLIBC_ALIGNED_HEAP_SIZE))
#define Z_MALLOC_PARTITION_EXISTS 1
extern struct k_mem_partition z_malloc_partition;
#endif
#endif /* CONFIG_PICOLIBC */

#ifdef Z_MALLOC_PARTITION_EXISTS
/* Memory partition containing the libc malloc arena. Configuration controls
Expand All @@ -66,7 +80,7 @@ extern struct k_mem_partition z_malloc_partition;
#endif

#if defined(CONFIG_NEWLIB_LIBC) || defined(CONFIG_STACK_CANARIES) || \
defined(CONFIG_NEED_LIBC_MEM_PARTITION)
defined(CONFIG_PICOLIBC) || defined(CONFIG_NEED_LIBC_MEM_PARTITION)
/* - All newlib globals will be placed into z_libc_partition.
* - Minimal C library globals, if any, will be placed into
* z_libc_partition.
Expand Down
8 changes: 8 additions & 0 deletions kernel/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,17 @@ config THREAD_USERSPACE_LOCAL_DATA
depends on USERSPACE
default y if ERRNO && !ERRNO_IN_TLS

config LIBC_ERRNO
bool
help
Use external libc errno, not the internal one. This makes sure that
ERRNO is not set for libc configurations that provide their own
implementation.

config ERRNO
bool "Enable errno support"
default y
depends on !LIBC_ERRNO
help
Enable per-thread errno in the kernel. Application and library code must
include errno.h provided by the C library (libc) to use the errno
Expand Down
3 changes: 3 additions & 0 deletions kernel/errno.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ static inline int *z_vrfy_z_errno(void)
#include <syscalls/z_errno_mrsh.c>

#else
#ifndef CONFIG_LIBC_ERRNO
int *z_impl_z_errno(void)
{
return &_current->errno_var;
}
#endif /* CONFIG_PICOLIBC */

#endif /* CONFIG_USERSPACE */

#endif /* CONFIG_ERRNO_IN_TLS */
Expand Down
2 changes: 2 additions & 0 deletions lib/libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ elseif(CONFIG_MINIMAL_LIBC)
add_subdirectory(minimal)
elseif(CONFIG_ARMCLANG_STD_LIBC)
add_subdirectory(armstdc)
elseif(CONFIG_PICOLIBC)
add_subdirectory(picolibc)
endif()
33 changes: 33 additions & 0 deletions lib/libc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions lib/libc/picolibc/CMakeLists.txt
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()
Loading

0 comments on commit fe48e9c

Please sign in to comment.