Skip to content

Commit bd7977a

Browse files
committed
bsdkm: atomics support.
1 parent a188d13 commit bd7977a

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

bsdkm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ WOLFSSL_DIR=../
66
CFLAGS+=-I/usr/include/sys
77
CFLAGS+=-I${WOLFSSL_DIR}
88
CFLAGS+=-DWOLFSSL_IGNORE_FILE_WARN -DHAVE_CONFIG_H
9-
CFLAGS+=-DNO_MAIN_DRIVER -DWOLFSSL_NO_ATOMICS
9+
CFLAGS+=-DNO_MAIN_DRIVER
1010
# debug mode
1111
CFLAGS+=-DWOLFSSL_BSDKM_VERBOSE_DEBUG
1212
CFLAGS+=$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS)

bsdkm/bsdkm_wc_port.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ extern struct malloc_type M_WOLFSSL[1];
110110
typedef volatile int wolfSSL_Atomic_Int;
111111
typedef volatile unsigned int wolfSSL_Atomic_Uint;
112112
#define WOLFSSL_ATOMIC_INITIALIZER(x) (x)
113-
#define WOLFSSL_ATOMIC_LOAD(x) atomic_load_acq_int(&(x))
113+
#define WOLFSSL_ATOMIC_LOAD(x) (int)atomic_load_acq_int(&(x))
114114
#define WOLFSSL_ATOMIC_STORE(x, v) atomic_store_rel_int(&(x), (v))
115115
#define WOLFSSL_ATOMIC_OPS
116116
#endif /* WOLFSSL_HAVE_ATOMIC_H && !WOLFSSL_NO_ATOMICS */

bsdkm/wolfkmod.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,87 @@ static int wolfkmod_cleanup(void);
4444
static int wolfkmod_load(void);
4545
static int wolfkmod_unload(void);
4646

47+
#if defined(WOLFSSL_ATOMIC_OPS)
48+
/* Note: using compiler built-ins like __atomic_fetch_add will technically
49+
* build, but are not commonly used in FreeBSD and may not be safe / portable.
50+
* */
51+
void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, int i)
52+
{
53+
*c = i;
54+
}
55+
56+
void wolfSSL_Atomic_Uint_Init(wolfSSL_Atomic_Uint* c, unsigned int i)
57+
{
58+
*c = i;
59+
}
60+
61+
int wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, int i)
62+
{
63+
return atomic_fetchadd_int(c, i);
64+
}
65+
66+
int wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, int i)
67+
{
68+
return atomic_fetchadd_int(c, -i);
69+
}
70+
71+
int wolfSSL_Atomic_Int_AddFetch(wolfSSL_Atomic_Int* c, int i)
72+
{
73+
int val = atomic_fetchadd_int(c, i);
74+
return val + i;
75+
}
76+
77+
int wolfSSL_Atomic_Int_SubFetch(wolfSSL_Atomic_Int* c, int i)
78+
{
79+
int val = atomic_fetchadd_int(c, -i);
80+
return val - i;
81+
}
82+
83+
unsigned int wolfSSL_Atomic_Uint_FetchAdd(wolfSSL_Atomic_Uint* c,
84+
unsigned int i)
85+
{
86+
return atomic_fetchadd_int(c, i);
87+
}
88+
89+
unsigned int wolfSSL_Atomic_Uint_FetchSub(wolfSSL_Atomic_Uint* c,
90+
unsigned int i)
91+
{
92+
return atomic_fetchadd_int(c, -i);
93+
}
94+
95+
unsigned int wolfSSL_Atomic_Uint_AddFetch(wolfSSL_Atomic_Uint* c,
96+
unsigned int i)
97+
{
98+
unsigned int val = atomic_fetchadd_int(c, i);
99+
return val + i;
100+
}
101+
102+
unsigned int wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c,
103+
unsigned int i)
104+
{
105+
unsigned int val = atomic_fetchadd_int(c, -i);
106+
return val - i;
107+
}
108+
109+
int wolfSSL_Atomic_Int_CompareExchange(wolfSSL_Atomic_Int* c, int *expected_i,
110+
int new_i)
111+
{
112+
u_int exp = (u_int) *expected_i;
113+
int ret = atomic_fcmpset_int(c, &exp, new_i);
114+
*expected_i = (int)exp;
115+
return ret;
116+
}
117+
118+
int wolfSSL_Atomic_Uint_CompareExchange(
119+
wolfSSL_Atomic_Uint* c, unsigned int *expected_i, unsigned int new_i)
120+
{
121+
u_int exp = (u_int) *expected_i;
122+
int ret = atomic_fcmpset_int(c, &exp, new_i);
123+
*expected_i = (unsigned int)exp;
124+
return ret;
125+
}
126+
#endif /* WOLFSSL_ATOMIC_OPS */
127+
47128
static int wolfkmod_init(void)
48129
{
49130
int ret = 0;

wolfcrypt/src/wc_port.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,10 @@ char* wc_strdup_ex(const char *src, int memType) {
12771277

12781278
#elif defined(SINGLE_THREADED)
12791279

1280+
#elif defined(WOLFSSL_BSDKM)
1281+
1282+
/* definitions are in bsdkm/bsdkm_wc_port.h */
1283+
12801284
#elif defined(HAVE_C___ATOMIC) && defined(WOLFSSL_HAVE_ATOMIC_H) && \
12811285
!defined(__cplusplus)
12821286

0 commit comments

Comments
 (0)