1
0
mirror of https://github.com/wolfSSL/wolfssl.git synced 2026-10-24 04:09:07 +03:00

bsdkm/bsdkm_wc_port.h, wolfcrypt/src/cpuid.c, wolfssl/wolfcrypt/cpuid.h: refactor BSD-kernel WOLFSSL_ATOMIC_LOAD() and WOLFSSL_ATOMIC_STORE() as polymorphic macros, eliminating BSDKM-unique WOLFSSL_ATOMIC_LOAD_UINT().

This commit is contained in:
Daniel Pouzzner
2026-09-20 23:16:56 -05:00
parent 683aa40cff
commit 5d0861bc1a
3 changed files with 27 additions and 11 deletions

View File

@@ -207,9 +207,33 @@ extern struct malloc_type M_WOLFSSL[1];
typedef volatile int wolfSSL_Atomic_Int;
typedef volatile unsigned int wolfSSL_Atomic_Uint;
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
#define WOLFSSL_ATOMIC_LOAD(x) (int)atomic_load_acq_int(&(x))
#define WOLFSSL_ATOMIC_LOAD_UINT(x) atomic_load_acq_int(&(x))
#define WOLFSSL_ATOMIC_STORE(x, v) atomic_store_rel_int(&(x), (v))
/* _Generic selectors dispatch on (x) + 0 so that qualifier handling is
* identical across pre- and post-DR 481 compilers (C17 settled that
* lvalue conversion strips qualifiers from the controlling expression;
* earlier GCC/clang did not) -- the typedefs are volatile-qualified, so
* bare (x) would match no arm on one compiler generation or the other.
* LOAD's result cast uses the same expression to yield the unqualified
* promoted type. The LOAD and STORE tables must stay member-congruent:
* extend both, in pairs, with each new width's own _acq/_rel primitive;
* no default arm -- an unlisted type is a deliberate compile error.
*/
#define WOLFSSL_ATOMIC_LOAD(x) ((__typeof__((x) + 0)) \
_Generic((__typeof__((x) + 0))0, \
int: atomic_load_acq_int((volatile u_int *)&(x)), \
unsigned int: atomic_load_acq_int((volatile u_int *)&(x)), \
long: atomic_load_acq_long((volatile u_long *)&(x)),\
unsigned long: atomic_load_acq_long((volatile u_long *)&(x))))
#define WOLFSSL_ATOMIC_STORE(x, v) _Generic((__typeof__((x) + 0))0, \
int: atomic_store_rel_int((volatile u_int *)&(x), \
(u_int)(v)), \
unsigned int: atomic_store_rel_int((volatile u_int *)&(x), \
(u_int)(v)), \
long: atomic_store_rel_long((volatile u_long *)&(x),\
(u_long)(v)), \
unsigned long: atomic_store_rel_long((volatile u_long *)&(x),\
(u_long)(v)) \
)
#define WOLFSSL_ATOMIC_OPS
#if defined(HAVE_FIPS)

View File

@@ -171,11 +171,7 @@
static WC_INLINE void cpuid_set_flags(void)
{
#ifdef WOLFSSL_BSDKM
if (WOLFSSL_ATOMIC_LOAD_UINT(cpuid_flags) == WC_CPUID_INITIALIZER) {
#else
if (WOLFSSL_ATOMIC_LOAD(cpuid_flags) == WC_CPUID_INITIALIZER) {
#endif
cpuid_flags_t new_cpuid_flags = 0,
old_cpuid_flags = WC_CPUID_INITIALIZER;
int os_avx = cpuid_os_state_enabled(WC_XCR0_AVX);

View File

@@ -233,11 +233,7 @@ typedef word32 cpuid_flags_t;
* accurate.
*/
static WC_INLINE int cpuid_get_flags_atomic(cpuid_flags_atomic_t *flags) {
#ifdef WOLFSSL_BSDKM
if (WOLFSSL_ATOMIC_LOAD_UINT(*flags) == WC_CPUID_INITIALIZER) {
#else
if (WOLFSSL_ATOMIC_LOAD(*flags) == WC_CPUID_INITIALIZER) {
#endif /* WOLFSSL_BSDKM */
cpuid_flags_t old_cpuid_flags = WC_CPUID_INITIALIZER;
return wolfSSL_Atomic_Uint_CompareExchange
(flags, &old_cpuid_flags, cpuid_get_flags());