Skip to content

Commit

Permalink
arch_atomic : Introduce CONFIG_LIBC_ARCH_NXATOMIC
Browse files Browse the repository at this point in the history
Some armv7-m-based SoCs do not work with atomic instructions,
even though armv7-m supports them.

To avoid using atomic instructions generated by gcc,
CONFIG_LIBC_ARCH_NXATOMIC is newly introduced with which
arch_atomic.c is linked explicitly.

However, the function names need to be changed to avoid
build errors, since the functions described in stdatomic.h
are gcc built-in and inlined when the code is compiled.

Signed-off-by: Takuya Miyasita <[email protected]>
  • Loading branch information
TakuyaMiyasita committed Nov 7, 2024
1 parent 4a6548e commit 2d1b309
Show file tree
Hide file tree
Showing 5 changed files with 490 additions and 0 deletions.
1 change: 1 addition & 0 deletions arch/arm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ config ARCH_CHIP_CXD32XX
bool "Sony CXD32xx"
select ARCH_CORTEXM4
select ARCH_HAVE_FPU
select LIBC_ARCH_NXATOMIC
---help---
Sony CXD32XX (ARM Cortex-M4) architectures

Expand Down
4 changes: 4 additions & 0 deletions include/nuttx/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
* Included Files
****************************************************************************/

#ifdef CONFIG_LIBC_ARCH_NXATOMIC
# include <nuttx/lib/stdatomic.h>
#else
#ifdef __has_include
# if defined(__cplusplus) && __has_include(<atomic>)
extern "C++"
Expand Down Expand Up @@ -92,6 +95,7 @@ extern "C++"
#else
# include <nuttx/lib/stdatomic.h>
#endif
#endif

/****************************************************************************
* Public Function Prototypes
Expand Down
192 changes: 192 additions & 0 deletions include/nuttx/lib/stdatomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,109 @@
#define ATOMIC_FLAG_INIT 0
#define ATOMIC_VAR_INIT(value) (value)

#ifdef CONFIG_LIBC_ARCH_NXATOMIC

#define atomic_store_n(obj, val, type) \
(sizeof(*(obj)) == 1 ? __nx_atomic_store_1(obj, val, type) : \
sizeof(*(obj)) == 2 ? __nx_atomic_store_2(obj, val, type) : \
sizeof(*(obj)) == 4 ? __nx_atomic_store_4(obj, val, type) : \
__nx_atomic_store_8(obj, val, type))

#define atomic_store(obj, val) atomic_store_n(obj, val, __ATOMIC_RELAXED)
#define atomic_store_explicit(obj, val, type) atomic_store_n(obj, val, type)
#define atomic_init(obj, val) atomic_store(obj, val)

#define atomic_load_n(obj, type) \
(sizeof(*(obj)) == 1 ? __nx_atomic_load_1(obj, type) : \
sizeof(*(obj)) == 2 ? __nx_atomic_load_2(obj, type) : \
sizeof(*(obj)) == 4 ? __nx_atomic_load_4(obj, type) : \
__nx_atomic_load_8(obj, type))

#define atomic_load(obj) atomic_load_n(obj, __ATOMIC_RELAXED)
#define atomic_load_explicit(obj, type) atomic_load_n(obj, type)

#define atomic_exchange_n(obj, val, type) \
(sizeof(*(obj)) == 1 ? __nx_atomic_exchange_1(obj, val, type) : \
sizeof(*(obj)) == 2 ? __nx_atomic_exchange_2(obj, val, type) : \
sizeof(*(obj)) == 4 ? __nx_atomic_exchange_4(obj, val, type) : \
__nx_atomic_exchange_8(obj, val, type))

#define atomic_exchange(obj, val) atomic_exchange_n(obj, val, __ATOMIC_RELAXED)
#define atomic_exchange_explicit(obj, val, type) atomic_exchange_n(obj, val, type)

#define atomic_compare_exchange_n(obj, expected, desired, weak, success, failure) \
(sizeof(*(obj)) == 1 ? __nx_atomic_compare_exchange_1(obj, expected, desired, weak, success, failure) : \
sizeof(*(obj)) == 2 ? __nx_atomic_compare_exchange_2(obj, expected, desired, weak, success, failure) : \
sizeof(*(obj)) == 4 ? __nx_atomic_compare_exchange_4(obj, expected, desired, weak, success, failure) : \
__nx_atomic_compare_exchange_8(obj, expected, desired, weak, success, failure))

#define atomic_compare_exchange_strong(obj, expected, desired) \
atomic_compare_exchange_n(obj, expected, desired, false, __ATOMIC_RELAXED, __ATOMIC_RELAXED)
#define atomic_compare_exchange_strong_explicit(obj, expected, desired, success, failure) \
atomic_compare_exchange_n(obj, expected, desired, false, success, failure)
#define atomic_compare_exchange_weak(obj, expected, desired) \
atomic_compare_exchange_n(obj, expected, desired, true, __ATOMIC_RELAXED, __ATOMIC_RELAXED)
#define atomic_compare_exchange_weak_explicit(obj, expected, desired, success, failure) \
atomic_compare_exchange_n(obj, expected, desired, true, success, failure)

#define atomic_flag_test_and_set_n(obj, type) \
(sizeof(*(obj)) == 1 ? __nx_atomic_flag_test_and_set_1(obj, type) : \
sizeof(*(obj)) == 2 ? __nx_atomic_flag_test_and_set_2(obj, type) : \
sizeof(*(obj)) == 4 ? __nx_atomic_flag_test_and_set_4(obj, type) : \
__nx_atomic_flag_test_and_set_8(obj, type))

#define atomic_flag_test_and_set(obj) atomic_flag_test_and_set_n(obj, __ATOMIC_RELAXED)
#define atomic_flag_test_and_set_explicit(obj, type) atomic_flag_test_and_set_n(obj, 1, type)
#define atomic_flag_clear(obj) atomic_store(obj, 0)
#define atomic_flag_clear_explicit(obj, type) atomic_store_explicit(obj, 0, type)

#define atomic_fetch_and_n(obj, val, type) \
(sizeof(*(obj)) == 1 ? __nx_atomic_fetch_and_1(obj, val, type) : \
sizeof(*(obj)) == 2 ? __nx_atomic_fetch_and_2(obj, val, type) : \
sizeof(*(obj)) == 4 ? __nx_atomic_fetch_and_4(obj, val, type) : \
__nx_atomic_fetch_and_8(obj, val, type))

#define atomic_fetch_and(obj, val) atomic_fetch_and_n(obj, val, __ATOMIC_RELAXED)
#define atomic_fetch_and_explicit(obj, val, type) atomic_fetch_and_n(obj, val, type)

#define atomic_fetch_or_n(obj, val, type) \
(sizeof(*(obj)) == 1 ? __nx_atomic_fetch_or_1(obj, val, type) : \
sizeof(*(obj)) == 2 ? __nx_atomic_fetch_or_2(obj, val, type) : \
sizeof(*(obj)) == 4 ? __nx_atomic_fetch_or_4(obj, val, type) : \
__nx_atomic_fetch_or_8(obj, val, type))

#define atomic_fetch_or(obj, val) atomic_fetch_or_n(obj, val, __ATOMIC_RELAXED)
#define atomic_fetch_or_explicit(obj, val, type) atomic_fetch_or_n(obj, val, type)

#define atomic_fetch_xor_n(obj, val, type) \
(sizeof(*(obj)) == 1 ? __nx_atomic_fetch_xor_1(obj, val, type) : \
sizeof(*(obj)) == 2 ? __nx_atomic_fetch_xor_2(obj, val, type) : \
sizeof(*(obj)) == 4 ? __nx_atomic_fetch_xor_4(obj, val, type) : \
__nx_atomic_fetch_xor_8(obj, val, type))

#define atomic_fetch_xor(obj, val) atomic_fetch_xor_n(obj, val, __ATOMIC_RELAXED)
#define atomic_fetch_xor_explicit(obj, val, type) atomic_fetch_xor_n(obj, val, type)

#define atomic_fetch_add_n(obj, val, type) \
(sizeof(*(obj)) == 1 ? __nx_atomic_fetch_add_1(obj, val, type) : \
sizeof(*(obj)) == 2 ? __nx_atomic_fetch_add_2(obj, val, type) : \
sizeof(*(obj)) == 4 ? __nx_atomic_fetch_add_4(obj, val, type) : \
__nx_atomic_fetch_add_8(obj, val, type))

#define atomic_fetch_add(obj, val) atomic_fetch_add_n(obj, val, __ATOMIC_RELAXED)
#define atomic_fetch_add_explicit(obj, val, type) atomic_fetch_add_n(obj, val, type)

#define atomic_fetch_sub_n(obj, val, type) \
(sizeof(*(obj)) == 1 ? __nx_atomic_fetch_sub_1(obj, val, type) : \
sizeof(*(obj)) == 2 ? __nx_atomic_fetch_sub_2(obj, val, type) : \
sizeof(*(obj)) == 4 ? __nx_atomic_fetch_sub_4(obj, val, type) : \
__nx_atomic_fetch_sub_8(obj, val, type))

#define atomic_fetch_sub(obj, val) atomic_fetch_sub_n(obj, val, __ATOMIC_RELAXED)
#define atomic_fetch_sub_explicit(obj, val, type) atomic_fetch_sub_n(obj, val, type)

#else

#define atomic_store_n(obj, val, type) \
(sizeof(*(obj)) == 1 ? __atomic_store_1(obj, val, type) : \
sizeof(*(obj)) == 2 ? __atomic_store_2(obj, val, type) : \
Expand Down Expand Up @@ -161,6 +264,8 @@
#define atomic_fetch_sub(obj, val) atomic_fetch_sub_n(obj, val, __ATOMIC_RELAXED)
#define atomic_fetch_sub_explicit(obj, val, type) atomic_fetch_sub_n(obj, val, type)

#endif

/****************************************************************************
* Public Types
****************************************************************************/
Expand Down Expand Up @@ -194,6 +299,91 @@ typedef volatile wchar_t atomic_wchar_t;
* Public Function Prototypes
****************************************************************************/

#ifdef CONFIG_LIBC_ARCH_NXATOMIC

void __nx_atomic_store_1(FAR volatile void *ptr, uint8_t value,
int memorder);
void __nx_atomic_store_2(FAR volatile void *ptr, uint16_t value,
int memorder);
void __nx_atomic_store_4(FAR volatile void *ptr, uint32_t value,
int memorder);
void __nx_atomic_store_8(FAR volatile void *ptr, uint64_t value,
int memorder);
uint8_t __nx_atomic_load_1(FAR const volatile void *ptr, int memorder);
uint16_t __nx_atomic_load_2(FAR const volatile void *ptr, int memorder);
uint32_t __nx_atomic_load_4(FAR const volatile void *ptr, int memorder);
uint64_t __nx_atomic_load_8(FAR const volatile void *ptr, int memorder);
uint8_t __nx_atomic_exchange_1(FAR volatile void *ptr, uint8_t value,
int memorder);
uint16_t __nx_atomic_exchange_2(FAR volatile void *ptr, uint16_t value,
int memorder);
uint32_t __nx_atomic_exchange_4(FAR volatile void *ptr, uint32_t value,
int memorder);
uint64_t __nx_atomic_exchange_8(FAR volatile void *ptr, uint64_t value,
int memorder);
bool __nx_atomic_compare_exchange_1(FAR volatile void *mem, FAR void *expect,
uint8_t desired, bool weak, int success,
int failure);
bool __nx_atomic_compare_exchange_2(FAR volatile void *mem, FAR void *expect,
uint16_t desired, bool weak, int success,
int failure);
bool __nx_atomic_compare_exchange_4(FAR volatile void *mem, FAR void *expect,
uint32_t desired, bool weak, int success,
int failure);
bool __nx_atomic_compare_exchange_8(FAR volatile void *mem, FAR void *expect,
uint64_t desired, bool weak, int success,
int failure);
uint8_t __nx_atomic_flag_test_and_set_1(FAR const volatile void *ptr,
int memorder);
uint16_t __nx_atomic_flag_test_and_set_2(FAR const volatile void *ptr,
int memorder);
uint32_t __nx_atomic_flag_test_and_set_4(FAR const volatile void *ptr,
int memorder);
uint64_t __nx_atomic_flag_test_and_set_8(FAR const volatile void *ptr,
int memorder);
uint8_t __nx_atomic_fetch_add_1(FAR volatile void *ptr, uint8_t value,
int memorder);
uint16_t __nx_atomic_fetch_add_2(FAR volatile void *ptr, uint16_t value,
int memorder);
uint32_t __nx_atomic_fetch_add_4(FAR volatile void *ptr, uint32_t value,
int memorder);
uint64_t __nx_atomic_fetch_add_8(FAR volatile void *ptr, uint64_t value,
int memorder);
uint8_t __nx_atomic_fetch_sub_1(FAR volatile void *ptr, uint8_t value,
int memorder);
uint16_t __nx_atomic_fetch_sub_2(FAR volatile void *ptr, uint16_t value,
int memorder);
uint32_t __nx_atomic_fetch_sub_4(FAR volatile void *ptr, uint32_t value,
int memorder);
uint64_t __nx_atomic_fetch_sub_8(FAR volatile void *ptr, uint64_t value,
int memorder);
uint8_t __nx_atomic_fetch_and_1(FAR volatile void *ptr, uint8_t value,
int memorder);
uint16_t __nx_atomic_fetch_and_2(FAR volatile void *ptr, uint16_t value,
int memorder);
uint32_t __nx_atomic_fetch_and_4(FAR volatile void *ptr, uint32_t value,
int memorder);
uint64_t __nx_atomic_fetch_and_8(FAR volatile void *ptr, uint64_t value,
int memorder);
uint8_t __nx_atomic_fetch_or_1(FAR volatile void *ptr, uint8_t value,
int memorder);
uint16_t __nx_atomic_fetch_or_2(FAR volatile void *ptr, uint16_t value,
int memorder);
uint32_t __nx_atomic_fetch_or_4(FAR volatile void *ptr, uint32_t value,
int memorder);
uint64_t __nx_atomic_fetch_or_8(FAR volatile void *ptr, uint64_t value,
int memorder);
uint8_t __nx_atomic_fetch_xor_1(FAR volatile void *ptr, uint8_t value,
int memorder);
uint16_t __nx_atomic_fetch_xor_2(FAR volatile void *ptr, uint16_t value,
int memorder);
uint32_t __nx_atomic_fetch_xor_4(FAR volatile void *ptr, uint32_t value,
int memorder);
uint64_t __nx_atomic_fetch_xor_8(FAR volatile void *ptr, uint64_t value,
int memorder);

#else

void __atomic_store_1(FAR volatile void *ptr, uint8_t value, int memorder);
void __atomic_store_2(FAR volatile void *ptr, uint16_t value, int memorder);
void __atomic_store_4(FAR volatile void *ptr, uint32_t value, int memorder);
Expand Down Expand Up @@ -271,4 +461,6 @@ uint32_t __atomic_fetch_xor_4(FAR volatile void *ptr, uint32_t value,
uint64_t __atomic_fetch_xor_8(FAR volatile void *ptr, uint64_t value,
int memorder);

#endif

#endif /* __INCLUDE_NUTTX_LIB_STDATOMIC_H */
8 changes: 8 additions & 0 deletions libs/libc/machine/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

menu "Architecture-Specific Support"

config LIBC_ARCH_NXATOMIC
bool "arch_nxatomic"
default n
---help---
If this configuration is selected and <include/nuttx/atomic.h> is
included, arch_atomic.c will be linked instead of built-in
atomic function.

config ARCH_LOWPUTC
bool "Low-level console output"
default y
Expand Down
Loading

0 comments on commit 2d1b309

Please sign in to comment.