From 551738192935cd3daeaa5de8fc02f57d571e883a Mon Sep 17 00:00:00 2001 From: Samy Al Bahra Date: Mon, 26 Feb 2018 00:00:45 -0500 Subject: [PATCH] freebsd/x86: Allow and override fence instructions to match kernel environment. This is mostly based off of Olivier's (cognet@) work. I was considering importing in __mbk but realistically, doesn't warrant the refactor. --- include/freebsd/ck_md.h.in | 15 +++------ include/gcc/x86/ck_pr.h | 64 ++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/include/freebsd/ck_md.h.in b/include/freebsd/ck_md.h.in index 026911a..8965e05 100644 --- a/include/freebsd/ck_md.h.in +++ b/include/freebsd/ck_md.h.in @@ -100,19 +100,12 @@ */ #if defined(__i386__) && !defined(__x86__) #define __x86__ -#define CK_MD_TSO - /* - * On i386, we resort to building the fence instructions on top of - * the kernel-provided facilities. + * If x86 becomes more relevant, we may want to consider importing in + * __mbk() to avoid potential issues around false sharing. */ -#include -#include - -#define CK_MD_X86_MFENCE() mb() -#define CK_MD_X86_SFENCE() wmb() -#define CK_MD_X86_LFENCE() rmb() - +#define CK_MD_TSO +#define CK_MD_SSE_DISABLE 1 #elif defined(__amd64__) #define CK_MD_TSO #elif defined(__sparc64__) && !defined(__sparcv9__) diff --git a/include/gcc/x86/ck_pr.h b/include/gcc/x86/ck_pr.h index 9025d21..3e36376 100644 --- a/include/gcc/x86/ck_pr.h +++ b/include/gcc/x86/ck_pr.h @@ -45,15 +45,9 @@ /* Minimum requirements for the CK_PR interface are met. */ #define CK_F_PR -#ifdef CK_MD_UMP -#define CK_PR_LOCK_PREFIX -#else -#define CK_PR_LOCK_PREFIX "lock " -#endif /* CK_MD_UMP */ - /* - * Prevent speculative execution in busy-wait loops (P4 <=) - * or "predefined delay". + * Prevent speculative execution in busy-wait loops (P4 <=) or "predefined + * delay". */ CK_CC_INLINE static void ck_pr_stall(void) @@ -62,28 +56,52 @@ ck_pr_stall(void) return; } +#ifdef CK_MD_UMP +#define CK_PR_LOCK_PREFIX +#define CK_PR_FENCE(T, I) \ + CK_CC_INLINE static void \ + ck_pr_fence_strict_##T(void) \ + { \ + __asm__ __volatile__("" ::: "memory"); \ + return; \ + } +#else +#define CK_PR_LOCK_PREFIX "lock " #define CK_PR_FENCE(T, I) \ CK_CC_INLINE static void \ ck_pr_fence_strict_##T(void) \ { \ __asm__ __volatile__(I ::: "memory"); \ + return; \ } +#endif /* CK_MD_UMP */ -CK_PR_FENCE(atomic, "sfence") -CK_PR_FENCE(atomic_store, "sfence") -CK_PR_FENCE(atomic_load, "mfence") -CK_PR_FENCE(store_atomic, "sfence") -CK_PR_FENCE(load_atomic, "mfence") -CK_PR_FENCE(load, "lfence") -CK_PR_FENCE(load_store, "mfence") -CK_PR_FENCE(store, "sfence") -CK_PR_FENCE(store_load, "mfence") -CK_PR_FENCE(memory, "mfence") -CK_PR_FENCE(release, "mfence") -CK_PR_FENCE(acquire, "mfence") -CK_PR_FENCE(acqrel, "mfence") -CK_PR_FENCE(lock, "mfence") -CK_PR_FENCE(unlock, "mfence") +#if defined(CK_MD_SSE_DISABLE) +/* If SSE is disabled, then use atomic operations for serialization. */ +#define CK_MD_X86_MFENCE "lock addl $0, (%%esp)" +#define CK_MD_X86_SFENCE CK_MD_X86_MFENCE +#define CK_MD_X86_LFENCE CK_MD_X86_MFENCE +#else +#define CK_MD_X86_SFENCE "sfence" +#define CK_MD_X86_LFENCE "lfence" +#define CK_MD_X86_MFENCE "mfence" +#endif /* !CK_MD_SSE_DISABLE */ + +CK_PR_FENCE(atomic, "") +CK_PR_FENCE(atomic_store, "") +CK_PR_FENCE(atomic_load, "") +CK_PR_FENCE(store_atomic, "") +CK_PR_FENCE(load_atomic, "") +CK_PR_FENCE(load, CK_MD_X86_LFENCE) +CK_PR_FENCE(load_store, CK_MD_X86_MFENCE) +CK_PR_FENCE(store, CK_MD_X86_SFENCE) +CK_PR_FENCE(store_load, CK_MD_X86_MFENCE) +CK_PR_FENCE(memory, CK_MD_X86_MFENCE) +CK_PR_FENCE(release, CK_MD_X86_MFENCE) +CK_PR_FENCE(acquire, CK_MD_X86_MFENCE) +CK_PR_FENCE(acqrel, CK_MD_X86_MFENCE) +CK_PR_FENCE(lock, CK_MD_X86_MFENCE) +CK_PR_FENCE(unlock, CK_MD_X86_MFENCE) #undef CK_PR_FENCE