From 4fc00cae7ac8c1698e464c93e00daa77e5b45cc1 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 6 Apr 2022 15:53:39 -0700 Subject: [PATCH] kernel: Allow Zephyr to use libc's internal errno For a library which already provides a multi-thread aware errno, use that instead of creating our own internal value. Signed-off-by: Keith Packard --- include/zephyr/kernel/thread.h | 4 ++-- include/zephyr/sys/errno_private.h | 12 ++++++++++-- kernel/Kconfig | 8 +++++++- kernel/errno.c | 4 +++- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/include/zephyr/kernel/thread.h b/include/zephyr/kernel/thread.h index 05a27ea6e28..71435b68680 100644 --- a/include/zephyr/kernel/thread.h +++ b/include/zephyr/kernel/thread.h @@ -169,7 +169,7 @@ struct _mem_domain_info { #ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA struct _thread_userspace_local_data { -#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS) +#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS) && !defined(CONFIG_LIBC_ERRNO) int errno_var; #endif }; @@ -274,7 +274,7 @@ struct k_thread { struct _thread_userspace_local_data *userspace_local_data; #endif -#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS) +#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS) && !defined(CONFIG_LIBC_ERRNO) #ifndef CONFIG_USERSPACE /** per-thread errno variable */ int errno_var; diff --git a/include/zephyr/sys/errno_private.h b/include/zephyr/sys/errno_private.h index e1c89349d16..60df9622bb8 100644 --- a/include/zephyr/sys/errno_private.h +++ b/include/zephyr/sys/errno_private.h @@ -17,7 +17,15 @@ extern "C" { * and kernel.h */ -#ifdef CONFIG_ERRNO_IN_TLS +#ifdef CONFIG_LIBC_ERRNO +#include + +static inline int *z_errno(void) +{ + return &errno; +} + +#elif defined(CONFIG_ERRNO_IN_TLS) extern __thread int z_errno_var; static inline int *z_errno(void) @@ -41,7 +49,7 @@ __syscall int *z_errno(void); } #endif -#ifndef CONFIG_ERRNO_IN_TLS +#if !defined(CONFIG_ERRNO_IN_TLS) && !defined(CONFIG_LIBC_ERRNO) #include #endif /* CONFIG_ERRNO_IN_TLS */ diff --git a/kernel/Kconfig b/kernel/Kconfig index a44a850f6f3..267b0adb6ce 100644 --- a/kernel/Kconfig +++ b/kernel/Kconfig @@ -204,6 +204,12 @@ 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 eliminates any + locally allocated errno storage and usage. + config ERRNO bool "Errno support" default y @@ -215,7 +221,7 @@ config ERRNO config ERRNO_IN_TLS bool "Store errno in thread local storage (TLS)" - depends on ERRNO && THREAD_LOCAL_STORAGE + depends on ERRNO && THREAD_LOCAL_STORAGE && !LIBC_ERRNO default y help Use thread local storage to store errno instead of storing it in diff --git a/kernel/errno.c b/kernel/errno.c index 41667362def..37866819bf5 100644 --- a/kernel/errno.c +++ b/kernel/errno.c @@ -24,7 +24,9 @@ const int _k_neg_eagain = -EAGAIN; #ifdef CONFIG_ERRNO -#ifdef CONFIG_ERRNO_IN_TLS +#if defined(CONFIG_LIBC_ERRNO) +/* nothing needed here */ +#elif defined(CONFIG_ERRNO_IN_TLS) __thread int z_errno_var; #else