Skip to content

Commit bfdce3a

Browse files
authored
Merge pull request #8832 from SparkiDev/aarch64_xfence
Aarch64 XFENCE
2 parents 6571f42 + d66863d commit bfdce3a

4 files changed

Lines changed: 62 additions & 9 deletions

File tree

configure.ac

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3228,6 +3228,26 @@ then
32283228
ENABLED_ARMASM_CRYPTO_SM4=yes
32293229
ENABLED_ARMASM_PLUS=yes
32303230
;;
3231+
barrier-sb)
3232+
case $host_cpu in
3233+
*aarch64*)
3234+
;;
3235+
*)
3236+
AC_MSG_ERROR([SB instructions only available on Aarch64 v8.5+ CPU.])
3237+
break;;
3238+
esac
3239+
ENABLED_ARMASM_BARRIER_SB=yes
3240+
;;
3241+
barrier-detect)
3242+
case $host_cpu in
3243+
*aarch64*)
3244+
;;
3245+
*)
3246+
AC_MSG_ERROR([SB instructions only available on Aarch64 v8.5+ CPU.])
3247+
break;;
3248+
esac
3249+
ENABLED_ARMASM_BARRIER_DETECT=yes
3250+
;;
32313251
*)
32323252
AC_MSG_ERROR([Invalid choice of ARM asm inclusions (yes, sha512-crypto, sha3-crypto): $ENABLED_ARMASM.])
32333253
break;;
@@ -3370,6 +3390,12 @@ fi
33703390
if test "$ENABLED_ARMASM_SM4" = "yes"; then
33713391
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_ARMASM_CRYPTO_SM4"
33723392
fi
3393+
if test "$ENABLED_ARMASM_BARRIER_SB" = "yes"; then
3394+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_ARMASM_BARRIER_SB"
3395+
fi
3396+
if test "$ENABLED_ARMASM_BARRIER_DETECT" = "yes"; then
3397+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_ARMASM_BARRIER_DETECT"
3398+
fi
33733399
if test "$ENABLED_ARMASM_CRYPTO" = "unknown"; then
33743400
ENABLED_ARMASM_CRYPTO=no
33753401
fi

wolfcrypt/src/wc_port.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <AvailabilityMacros.h>
2626
#endif
2727

28+
#include <wolfssl/wolfcrypt/cpuid.h>
2829
#ifdef HAVE_ECC
2930
#include <wolfssl/wolfcrypt/ecc.h>
3031
#endif
@@ -145,6 +146,10 @@
145146
/* prevent multiple mutex initializations */
146147
static volatile int initRefCount = 0;
147148

149+
#if defined(__aarch64__) && defined(WOLFSSL_ARMASM_BARRIER_DETECT)
150+
int aarch64_use_sb = 0;
151+
#endif
152+
148153
/* Used to initialize state for wolfcrypt
149154
return 0 on success
150155
*/
@@ -155,6 +160,10 @@ int wolfCrypt_Init(void)
155160
if (initRefCount == 0) {
156161
WOLFSSL_ENTER("wolfCrypt_Init");
157162

163+
#if defined(__aarch64__) && defined(WOLFSSL_ARMASM_BARRIER_DETECT)
164+
aarch64_use_sb = IS_AARCH64_SB(cpuid_get_flags());
165+
#endif
166+
158167
#ifdef WOLFSSL_CHECK_MEM_ZERO
159168
/* Initialize the mutex for access to the list of memory locations that
160169
* must be freed. */

wolfssl/wolfcrypt/cpuid.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@
7070

7171
#elif defined(HAVE_CPUID_AARCH64)
7272

73-
#define CPUID_AES 0x0001
74-
#define CPUID_PMULL 0x0002
75-
#define CPUID_SHA256 0x0004
76-
#define CPUID_SHA512 0x0008
77-
#define CPUID_RDM 0x0010
78-
#define CPUID_SHA3 0x0020
79-
#define CPUID_SM3 0x0040
80-
#define CPUID_SM4 0x0080
73+
#define CPUID_AES 0x0001 /* AES enc/dec */
74+
#define CPUID_PMULL 0x0002 /* Carryless multiplication */
75+
#define CPUID_SHA256 0x0004 /* SHA-256 digest */
76+
#define CPUID_SHA512 0x0008 /* SHA-512 digest */
77+
#define CPUID_RDM 0x0010 /* SQRDMLAH and SQRDMLSH */
78+
#define CPUID_SHA3 0x0020 /* SHA-3 digest */
79+
#define CPUID_SM3 0x0040 /* SM3 digest */
80+
#define CPUID_SM4 0x0080 /* SM4 enc/dec */
81+
#define CPUID_SB 0x0100 /* Speculation barrier */
8182

8283
#define IS_AARCH64_AES(f) ((f) & CPUID_AES)
8384
#define IS_AARCH64_PMULL(f) ((f) & CPUID_PMULL)
@@ -87,6 +88,7 @@
8788
#define IS_AARCH64_SHA3(f) ((f) & CPUID_SHA3)
8889
#define IS_AARCH64_SM3(f) ((f) & CPUID_SM3)
8990
#define IS_AARCH64_SM4(f) ((f) & CPUID_SM4)
91+
#define IS_AARCH64_SB(f) ((f) & CPUID_SB)
9092

9193
#endif
9294

wolfssl/wolfcrypt/wc_port.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,8 +1536,24 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
15361536
#define XFENCE() WC_DO_NOTHING
15371537
#elif defined (__i386__) || defined(__x86_64__)
15381538
#define XFENCE() XASM_VOLATILE("lfence")
1539-
#elif (defined (__arm__) && (__ARM_ARCH > 6)) || defined(__aarch64__)
1539+
#elif defined (__arm__) && (__ARM_ARCH > 6)
15401540
#define XFENCE() XASM_VOLATILE("isb")
1541+
#elif defined(__aarch64__)
1542+
/* Change ".inst 0xd50330ff" to "sb" when compilers support it. */
1543+
#ifdef WOLFSSL_ARMASM_BARRIER_SB
1544+
#define XFENCE() XASM_VOLATILE(".inst 0xd50330ff")
1545+
#elif defined(WOLFSSL_ARMASM_BARRIER_DETECT)
1546+
extern int aarch64_use_sb;
1547+
#define XFENCE() \
1548+
do { \
1549+
if (aarch64_use_sb) \
1550+
XASM_VOLATILE(".inst 0xd50330ff"); \
1551+
else \
1552+
XASM_VOLATILE("isb"); \
1553+
} while (0)
1554+
#else
1555+
#define XFENCE() XASM_VOLATILE("isb")
1556+
#endif
15411557
#elif defined(__riscv)
15421558
#define XFENCE() XASM_VOLATILE("fence")
15431559
#elif defined(__PPC__) || defined(__POWERPC__)

0 commit comments

Comments
 (0)