kernel: support using thread local storage for errno

This enables storing errno in the thread local storage area.
With this enabled, a syscall to access errno can be avoided
when userspace is also enabled.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2020-10-05 14:54:45 -07:00 committed by Andrew Boie
commit 62cf1960ad
5 changed files with 35 additions and 3 deletions

View file

@ -266,7 +266,9 @@ struct _mem_domain_info {
#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
struct _thread_userspace_local_data {
#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS)
int errno_var;
#endif
};
#endif
@ -329,7 +331,7 @@ struct k_thread {
struct _thread_userspace_local_data *userspace_local_data;
#endif
#ifdef CONFIG_ERRNO
#if defined(CONFIG_ERRNO) && !defined(CONFIG_ERRNO_IN_TLS)
#ifndef CONFIG_USERSPACE
/** per-thread errno variable */
int errno_var;

View file

@ -17,6 +17,14 @@ extern "C" {
* and kernel.h
*/
#ifdef CONFIG_ERRNO_IN_TLS
extern __thread int z_errno_var;
static inline int *z_errno(void)
{
return &z_errno_var;
}
#else
/**
* return a pointer to a memory location containing errno
*
@ -27,10 +35,14 @@ extern "C" {
*/
__syscall int *z_errno(void);
#endif /* CONFIG_ERRNO_IN_TLS */
#ifdef __cplusplus
}
#endif
#ifndef CONFIG_ERRNO_IN_TLS
#include <syscalls/errno_private.h>
#endif /* CONFIG_ERRNO_IN_TLS */
#endif /* ZEPHYR_INCLUDE_SYS_ERRNO_PRIVATE_H_ */

View file

@ -195,16 +195,24 @@ config THREAD_CUSTOM_DATA
config THREAD_USERSPACE_LOCAL_DATA
bool
depends on USERSPACE
default y if ERRNO && !ERRNO_IN_TLS
config ERRNO
bool "Enable errno support"
default y
select THREAD_USERSPACE_LOCAL_DATA if USERSPACE
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
symbol. The C library must access the per-thread errno via the
_get_errno() symbol.
z_errno() symbol.
config ERRNO_IN_TLS
bool "Store errno in thread local storage (TLS)"
depends on ERRNO && THREAD_LOCAL_STORAGE
default y
help
Use thread local storage to store errno instead of storing it in
the kernel thread struct. This avoids a syscall if userspace is enabled.
choice SCHED_ALGORITHM
prompt "Scheduler priority queue algorithm"

View file

@ -23,6 +23,11 @@
const int _k_neg_eagain = -EAGAIN;
#ifdef CONFIG_ERRNO
#ifdef CONFIG_ERRNO_IN_TLS
__thread int z_errno_var;
#else
#ifdef CONFIG_USERSPACE
int *z_impl_z_errno(void)
{
@ -44,4 +49,7 @@ int *z_impl_z_errno(void)
return &_current->errno_var;
}
#endif /* CONFIG_USERSPACE */
#endif /* CONFIG_ERRNO_IN_TLS */
#endif /* CONFIG_ERRNO */

View file

@ -835,8 +835,10 @@ FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
#ifdef CONFIG_USERSPACE
__ASSERT(z_stack_is_user_capable(_current->stack_obj),
"dropping to user mode with kernel-only stack object");
#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
memset(_current->userspace_local_data, 0,
sizeof(struct _thread_userspace_local_data));
#endif
arch_user_mode_enter(entry, p1, p2, p3);
#else
/* XXX In this case we do not reset the stack */