1 /*
2  * Copyright (c) 2015 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /** @file
8  *
9  * @brief Per-thread errno accessor function
10  *
11  * Allow accessing the errno for the current thread without involving the
12  * context switching.
13  */
14 
15 #include <zephyr/kernel.h>
16 #include <zephyr/internal/syscall_handler.h>
17 
18 /*
19  * Define _k_neg_eagain for use in assembly files as errno.h is
20  * not assembly language safe.
21  * FIXME: wastes 4 bytes
22  */
23 const int _k_neg_eagain = -EAGAIN;
24 
25 #ifdef CONFIG_ERRNO
26 
27 #if defined(CONFIG_LIBC_ERRNO)
28 /* nothing needed here */
29 #elif defined(CONFIG_ERRNO_IN_TLS)
30 __thread int z_errno_var;
31 #else
32 
33 #ifdef CONFIG_USERSPACE
z_impl_z_errno(void)34 int *z_impl_z_errno(void)
35 {
36 	/* Initialized to the lowest address in the stack so the thread can
37 	 * directly read/write it
38 	 */
39 	return &_current->userspace_local_data->errno_var;
40 }
41 
z_vrfy_z_errno(void)42 static inline int *z_vrfy_z_errno(void)
43 {
44 	return z_impl_z_errno();
45 }
46 #include <syscalls/z_errno_mrsh.c>
47 
48 #else
z_impl_z_errno(void)49 int *z_impl_z_errno(void)
50 {
51 	return &_current->errno_var;
52 }
53 #endif /* CONFIG_USERSPACE */
54 
55 #endif /* CONFIG_ERRNO_IN_TLS */
56 
57 #endif /* CONFIG_ERRNO */
58