2009-09-23 23:32:31 +02:00
|
|
|
#ifndef ATOMIC_X86_GCC_INCLUDED
|
|
|
|
#define ATOMIC_X86_GCC_INCLUDED
|
|
|
|
|
2011-06-30 17:46:53 +02:00
|
|
|
/* Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
|
2006-05-31 18:44:09 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-27 02:23:51 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2006-05-31 18:44:09 +02:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-30 17:46:53 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
2006-05-31 18:44:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
XXX 64-bit atomic operations can be implemented using
|
2006-06-17 16:20:39 +02:00
|
|
|
cmpxchg8b, if necessary. Though I've heard that not all 64-bit
|
|
|
|
architectures support double-word (128-bit) cas.
|
2006-05-31 18:44:09 +02:00
|
|
|
*/
|
|
|
|
|
2009-12-15 15:40:08 +01:00
|
|
|
/*
|
|
|
|
No special support of 8 and 16 bit operations are implemented here
|
|
|
|
currently.
|
|
|
|
*/
|
|
|
|
#undef MY_ATOMIC_HAS_8_AND_16
|
|
|
|
|
2009-10-09 14:21:29 +02:00
|
|
|
#ifdef __x86_64__
|
|
|
|
# ifdef MY_ATOMIC_NO_XADD
|
|
|
|
# define MY_ATOMIC_MODE "gcc-amd64" LOCK_prefix "-no-xadd"
|
|
|
|
# else
|
|
|
|
# define MY_ATOMIC_MODE "gcc-amd64" LOCK_prefix
|
|
|
|
# endif
|
2006-06-29 15:39:53 +02:00
|
|
|
#else
|
2009-10-09 14:21:29 +02:00
|
|
|
# ifdef MY_ATOMIC_NO_XADD
|
|
|
|
# define MY_ATOMIC_MODE "gcc-x86" LOCK_prefix "-no-xadd"
|
|
|
|
# else
|
|
|
|
# define MY_ATOMIC_MODE "gcc-x86" LOCK_prefix
|
|
|
|
# endif
|
2006-06-29 15:39:53 +02:00
|
|
|
#endif
|
2006-06-17 16:20:39 +02:00
|
|
|
|
unittest:
rename *.t* to *-t* to be automake-friendly
simplify Makefiles
test_atomic.c:
move to unittest, add GPL comment, fix warnings, convert to tap framework.
configure:
remove custom tests for available types, use AC_CHECK_TYPE instead
x86-gcc.h:
fix gcc -ansi errors while maintaining readability
ignore:
added *-t
unittest/mysys/base64-t.c:
Rename: unittest/mysys/base64.t.c -> unittest/mysys/base64-t.c
unittest/mysys/bitmap-t.c:
Rename: unittest/mysys/bitmap.t.c -> unittest/mysys/bitmap-t.c
unittest/mytap/t/basic-t.c:
Rename: unittest/mytap/t/basic.t.c -> unittest/mytap/t/basic-t.c
unittest/examples/no_plan-t.c:
Rename: unittest/examples/no_plan.t.c -> unittest/examples/no_plan-t.c
unittest/examples/simple-t.c:
Rename: unittest/examples/simple.t.c -> unittest/examples/simple-t.c
unittest/examples/skip-t.c:
Rename: unittest/examples/skip.t.c -> unittest/examples/skip-t.c
unittest/examples/skip_all-t.c:
Rename: unittest/examples/skip_all.t.c -> unittest/examples/skip_all-t.c
unittest/examples/todo-t.c:
Rename: unittest/examples/todo.t.c -> unittest/examples/todo-t.c
BitKeeper/etc/ignore:
added *-t
config/ac-macros/misc.m4:
remove custom AC_TRY_RUN tests for available types, use AC_CHECK_TYPE instead
configure.in:
remove custom tests for available types, use AC_CHECK_TYPE instead
include/atomic/x86-gcc.h:
fix gcc -ansi errors while maintaining readability
include/my_global.h:
remove custom tests for available types, use AC_CHECK_TYPE instead
include/my_sys.h:
add missing declaration
mysys/Makefile.am:
move test_atomic to unittest
unittest/Makefile.am:
simplifications, correct permissions in chmod
unittest/README.txt:
rename *.t* to *-t* to be automake-friendly
unittest/examples/Makefile.am:
rename *.t* to *-t* to be automake-friendly
simplify Makefile
unittest/mysys/Makefile.am:
rename *.t* to *-t* to be automake-friendly
simplify Makefile
unittest/mysys/my_atomic-t.c:
move mysys/test_atomic.c to unittest, add GPL comment, fix warnings,
convert to tap framework.
unittest/mytap/t/Makefile.am:
rename *.t* to *-t* to be automake-friendly
simplify Makefile
unittest/unit.pl:
rename *.t* to *-t* to be automake-friendly
2006-06-01 12:25:47 +02:00
|
|
|
/* fix -ansi errors while maintaining readability */
|
2006-06-17 16:20:39 +02:00
|
|
|
#ifndef asm
|
unittest:
rename *.t* to *-t* to be automake-friendly
simplify Makefiles
test_atomic.c:
move to unittest, add GPL comment, fix warnings, convert to tap framework.
configure:
remove custom tests for available types, use AC_CHECK_TYPE instead
x86-gcc.h:
fix gcc -ansi errors while maintaining readability
ignore:
added *-t
unittest/mysys/base64-t.c:
Rename: unittest/mysys/base64.t.c -> unittest/mysys/base64-t.c
unittest/mysys/bitmap-t.c:
Rename: unittest/mysys/bitmap.t.c -> unittest/mysys/bitmap-t.c
unittest/mytap/t/basic-t.c:
Rename: unittest/mytap/t/basic.t.c -> unittest/mytap/t/basic-t.c
unittest/examples/no_plan-t.c:
Rename: unittest/examples/no_plan.t.c -> unittest/examples/no_plan-t.c
unittest/examples/simple-t.c:
Rename: unittest/examples/simple.t.c -> unittest/examples/simple-t.c
unittest/examples/skip-t.c:
Rename: unittest/examples/skip.t.c -> unittest/examples/skip-t.c
unittest/examples/skip_all-t.c:
Rename: unittest/examples/skip_all.t.c -> unittest/examples/skip_all-t.c
unittest/examples/todo-t.c:
Rename: unittest/examples/todo.t.c -> unittest/examples/todo-t.c
BitKeeper/etc/ignore:
added *-t
config/ac-macros/misc.m4:
remove custom AC_TRY_RUN tests for available types, use AC_CHECK_TYPE instead
configure.in:
remove custom tests for available types, use AC_CHECK_TYPE instead
include/atomic/x86-gcc.h:
fix gcc -ansi errors while maintaining readability
include/my_global.h:
remove custom tests for available types, use AC_CHECK_TYPE instead
include/my_sys.h:
add missing declaration
mysys/Makefile.am:
move test_atomic to unittest
unittest/Makefile.am:
simplifications, correct permissions in chmod
unittest/README.txt:
rename *.t* to *-t* to be automake-friendly
unittest/examples/Makefile.am:
rename *.t* to *-t* to be automake-friendly
simplify Makefile
unittest/mysys/Makefile.am:
rename *.t* to *-t* to be automake-friendly
simplify Makefile
unittest/mysys/my_atomic-t.c:
move mysys/test_atomic.c to unittest, add GPL comment, fix warnings,
convert to tap framework.
unittest/mytap/t/Makefile.am:
rename *.t* to *-t* to be automake-friendly
simplify Makefile
unittest/unit.pl:
rename *.t* to *-t* to be automake-friendly
2006-06-01 12:25:47 +02:00
|
|
|
#define asm __asm__
|
2006-06-17 16:20:39 +02:00
|
|
|
#endif
|
unittest:
rename *.t* to *-t* to be automake-friendly
simplify Makefiles
test_atomic.c:
move to unittest, add GPL comment, fix warnings, convert to tap framework.
configure:
remove custom tests for available types, use AC_CHECK_TYPE instead
x86-gcc.h:
fix gcc -ansi errors while maintaining readability
ignore:
added *-t
unittest/mysys/base64-t.c:
Rename: unittest/mysys/base64.t.c -> unittest/mysys/base64-t.c
unittest/mysys/bitmap-t.c:
Rename: unittest/mysys/bitmap.t.c -> unittest/mysys/bitmap-t.c
unittest/mytap/t/basic-t.c:
Rename: unittest/mytap/t/basic.t.c -> unittest/mytap/t/basic-t.c
unittest/examples/no_plan-t.c:
Rename: unittest/examples/no_plan.t.c -> unittest/examples/no_plan-t.c
unittest/examples/simple-t.c:
Rename: unittest/examples/simple.t.c -> unittest/examples/simple-t.c
unittest/examples/skip-t.c:
Rename: unittest/examples/skip.t.c -> unittest/examples/skip-t.c
unittest/examples/skip_all-t.c:
Rename: unittest/examples/skip_all.t.c -> unittest/examples/skip_all-t.c
unittest/examples/todo-t.c:
Rename: unittest/examples/todo.t.c -> unittest/examples/todo-t.c
BitKeeper/etc/ignore:
added *-t
config/ac-macros/misc.m4:
remove custom AC_TRY_RUN tests for available types, use AC_CHECK_TYPE instead
configure.in:
remove custom tests for available types, use AC_CHECK_TYPE instead
include/atomic/x86-gcc.h:
fix gcc -ansi errors while maintaining readability
include/my_global.h:
remove custom tests for available types, use AC_CHECK_TYPE instead
include/my_sys.h:
add missing declaration
mysys/Makefile.am:
move test_atomic to unittest
unittest/Makefile.am:
simplifications, correct permissions in chmod
unittest/README.txt:
rename *.t* to *-t* to be automake-friendly
unittest/examples/Makefile.am:
rename *.t* to *-t* to be automake-friendly
simplify Makefile
unittest/mysys/Makefile.am:
rename *.t* to *-t* to be automake-friendly
simplify Makefile
unittest/mysys/my_atomic-t.c:
move mysys/test_atomic.c to unittest, add GPL comment, fix warnings,
convert to tap framework.
unittest/mytap/t/Makefile.am:
rename *.t* to *-t* to be automake-friendly
simplify Makefile
unittest/unit.pl:
rename *.t* to *-t* to be automake-friendly
2006-06-01 12:25:47 +02:00
|
|
|
|
2006-06-29 15:39:53 +02:00
|
|
|
#ifndef MY_ATOMIC_NO_XADD
|
2009-10-12 11:00:39 +02:00
|
|
|
#define make_atomic_add_body(S) make_atomic_add_body ## S
|
|
|
|
#define make_atomic_cas_body(S) make_atomic_cas_body ## S
|
2006-06-29 15:39:53 +02:00
|
|
|
#endif
|
2009-10-12 11:00:39 +02:00
|
|
|
|
|
|
|
#define make_atomic_add_body32 \
|
Bug#22320: my_atomic-t unit test fails
Bug#52261: 64 bit atomic operations do not work on Solaris i386
gcc in debug compilation
One of the various problems was that the source operand to
CMPXCHG8b was marked as a input/output operand, causing GCC
to use the EBX register as the destination register for the
CMPXCHG8b instruction. This could lead to crashes as the EBX
register is also implicitly used by the instruction, causing
the value to be potentially garbaged and a protection fault
once the value is used to access a position in memory.
Another problem was the lack of proper clobbers for the atomic
operations and, also, a discrepancy between the implementations
for the Compare and Set operation. The specific problems are
described and fixed by Kristian Nielsen patches:
Patch: 1
Fix bugs in my_atomic_cas*(val,cmp,new) that *cmp is accessed
after CAS succeds.
In the gcc builtin implementation, problem was that *cmp was
read again after atomic CAS to check if old *val == *cmp;
this fails if CAS is successful and another thread modifies
*cmp in-between.
In the x86-gcc implementation, problem was that *cmp was set
also in the case of successful CAS; this means there is a
window where it can clobber a value written by another thread
after successful CAS.
Patch 2:
Add a GCC asm "memory" clobber to primitives that imply a
memory barrier.
This signifies to GCC that any potentially aliased memory
must be flushed before the operation, and re-read after the
operation, so that read or modification in other threads of
such memory values will work as intended.
In effect, it makes these primitives work as memory barriers
for the compiler as well as the CPU. This is better and more
correct than adding "volatile" to variables.
include/atomic/gcc_builtins.h:
Do not read from *cmp after the operation as it might be
already gone if the operation was successful.
include/atomic/nolock.h:
Prefer system provided atomics over the broken x86 asm.
include/atomic/x86-gcc.h:
Do not mark source operands as input/output operands.
Add proper memory clobbers.
include/my_atomic.h:
Add notes about my_atomic_add and my_atomic_cas behaviors.
unittest/mysys/my_atomic-t.c:
Remove work around, if it fails, there is either a problem
with the atomic operations code or the specific compiler
version should be black-listed.
2010-07-23 14:37:10 +02:00
|
|
|
asm volatile (LOCK_prefix "; xadd %0, %1;" \
|
|
|
|
: "+r" (v), "=m" (*a) \
|
|
|
|
: "m" (*a) \
|
|
|
|
: "memory")
|
2009-10-12 11:00:39 +02:00
|
|
|
|
|
|
|
#define make_atomic_cas_body32 \
|
Bug#22320: my_atomic-t unit test fails
Bug#52261: 64 bit atomic operations do not work on Solaris i386
gcc in debug compilation
One of the various problems was that the source operand to
CMPXCHG8b was marked as a input/output operand, causing GCC
to use the EBX register as the destination register for the
CMPXCHG8b instruction. This could lead to crashes as the EBX
register is also implicitly used by the instruction, causing
the value to be potentially garbaged and a protection fault
once the value is used to access a position in memory.
Another problem was the lack of proper clobbers for the atomic
operations and, also, a discrepancy between the implementations
for the Compare and Set operation. The specific problems are
described and fixed by Kristian Nielsen patches:
Patch: 1
Fix bugs in my_atomic_cas*(val,cmp,new) that *cmp is accessed
after CAS succeds.
In the gcc builtin implementation, problem was that *cmp was
read again after atomic CAS to check if old *val == *cmp;
this fails if CAS is successful and another thread modifies
*cmp in-between.
In the x86-gcc implementation, problem was that *cmp was set
also in the case of successful CAS; this means there is a
window where it can clobber a value written by another thread
after successful CAS.
Patch 2:
Add a GCC asm "memory" clobber to primitives that imply a
memory barrier.
This signifies to GCC that any potentially aliased memory
must be flushed before the operation, and re-read after the
operation, so that read or modification in other threads of
such memory values will work as intended.
In effect, it makes these primitives work as memory barriers
for the compiler as well as the CPU. This is better and more
correct than adding "volatile" to variables.
include/atomic/gcc_builtins.h:
Do not read from *cmp after the operation as it might be
already gone if the operation was successful.
include/atomic/nolock.h:
Prefer system provided atomics over the broken x86 asm.
include/atomic/x86-gcc.h:
Do not mark source operands as input/output operands.
Add proper memory clobbers.
include/my_atomic.h:
Add notes about my_atomic_add and my_atomic_cas behaviors.
unittest/mysys/my_atomic-t.c:
Remove work around, if it fails, there is either a problem
with the atomic operations code or the specific compiler
version should be black-listed.
2010-07-23 14:37:10 +02:00
|
|
|
__typeof__(*cmp) sav; \
|
2009-10-09 14:21:29 +02:00
|
|
|
asm volatile (LOCK_prefix "; cmpxchg %3, %0; setz %2;" \
|
Bug#22320: my_atomic-t unit test fails
Bug#52261: 64 bit atomic operations do not work on Solaris i386
gcc in debug compilation
One of the various problems was that the source operand to
CMPXCHG8b was marked as a input/output operand, causing GCC
to use the EBX register as the destination register for the
CMPXCHG8b instruction. This could lead to crashes as the EBX
register is also implicitly used by the instruction, causing
the value to be potentially garbaged and a protection fault
once the value is used to access a position in memory.
Another problem was the lack of proper clobbers for the atomic
operations and, also, a discrepancy between the implementations
for the Compare and Set operation. The specific problems are
described and fixed by Kristian Nielsen patches:
Patch: 1
Fix bugs in my_atomic_cas*(val,cmp,new) that *cmp is accessed
after CAS succeds.
In the gcc builtin implementation, problem was that *cmp was
read again after atomic CAS to check if old *val == *cmp;
this fails if CAS is successful and another thread modifies
*cmp in-between.
In the x86-gcc implementation, problem was that *cmp was set
also in the case of successful CAS; this means there is a
window where it can clobber a value written by another thread
after successful CAS.
Patch 2:
Add a GCC asm "memory" clobber to primitives that imply a
memory barrier.
This signifies to GCC that any potentially aliased memory
must be flushed before the operation, and re-read after the
operation, so that read or modification in other threads of
such memory values will work as intended.
In effect, it makes these primitives work as memory barriers
for the compiler as well as the CPU. This is better and more
correct than adding "volatile" to variables.
include/atomic/gcc_builtins.h:
Do not read from *cmp after the operation as it might be
already gone if the operation was successful.
include/atomic/nolock.h:
Prefer system provided atomics over the broken x86 asm.
include/atomic/x86-gcc.h:
Do not mark source operands as input/output operands.
Add proper memory clobbers.
include/my_atomic.h:
Add notes about my_atomic_add and my_atomic_cas behaviors.
unittest/mysys/my_atomic-t.c:
Remove work around, if it fails, there is either a problem
with the atomic operations code or the specific compiler
version should be black-listed.
2010-07-23 14:37:10 +02:00
|
|
|
: "=m" (*a), "=a" (sav), "=q" (ret) \
|
|
|
|
: "r" (set), "m" (*a), "a" (*cmp) \
|
|
|
|
: "memory"); \
|
|
|
|
if (!ret) \
|
|
|
|
*cmp= sav
|
2006-05-31 18:44:09 +02:00
|
|
|
|
2009-12-15 22:15:48 +01:00
|
|
|
#ifdef __x86_64__
|
2009-10-12 11:00:39 +02:00
|
|
|
#define make_atomic_add_body64 make_atomic_add_body32
|
|
|
|
#define make_atomic_cas_body64 make_atomic_cas_body32
|
2009-12-15 22:15:48 +01:00
|
|
|
|
Bug#22320: my_atomic-t unit test fails
Bug#52261: 64 bit atomic operations do not work on Solaris i386
gcc in debug compilation
One of the various problems was that the source operand to
CMPXCHG8b was marked as a input/output operand, causing GCC
to use the EBX register as the destination register for the
CMPXCHG8b instruction. This could lead to crashes as the EBX
register is also implicitly used by the instruction, causing
the value to be potentially garbaged and a protection fault
once the value is used to access a position in memory.
Another problem was the lack of proper clobbers for the atomic
operations and, also, a discrepancy between the implementations
for the Compare and Set operation. The specific problems are
described and fixed by Kristian Nielsen patches:
Patch: 1
Fix bugs in my_atomic_cas*(val,cmp,new) that *cmp is accessed
after CAS succeds.
In the gcc builtin implementation, problem was that *cmp was
read again after atomic CAS to check if old *val == *cmp;
this fails if CAS is successful and another thread modifies
*cmp in-between.
In the x86-gcc implementation, problem was that *cmp was set
also in the case of successful CAS; this means there is a
window where it can clobber a value written by another thread
after successful CAS.
Patch 2:
Add a GCC asm "memory" clobber to primitives that imply a
memory barrier.
This signifies to GCC that any potentially aliased memory
must be flushed before the operation, and re-read after the
operation, so that read or modification in other threads of
such memory values will work as intended.
In effect, it makes these primitives work as memory barriers
for the compiler as well as the CPU. This is better and more
correct than adding "volatile" to variables.
include/atomic/gcc_builtins.h:
Do not read from *cmp after the operation as it might be
already gone if the operation was successful.
include/atomic/nolock.h:
Prefer system provided atomics over the broken x86 asm.
include/atomic/x86-gcc.h:
Do not mark source operands as input/output operands.
Add proper memory clobbers.
include/my_atomic.h:
Add notes about my_atomic_add and my_atomic_cas behaviors.
unittest/mysys/my_atomic-t.c:
Remove work around, if it fails, there is either a problem
with the atomic operations code or the specific compiler
version should be black-listed.
2010-07-23 14:37:10 +02:00
|
|
|
#define make_atomic_fas_body(S) \
|
|
|
|
asm volatile ("xchg %0, %1;" \
|
|
|
|
: "+r" (v), "=m" (*a) \
|
|
|
|
: "m" (*a) \
|
|
|
|
: "memory")
|
2009-12-15 22:15:48 +01:00
|
|
|
|
|
|
|
/*
|
2010-12-01 00:19:49 +01:00
|
|
|
Actually 32/64-bit reads/writes are always atomic on x86_64,
|
|
|
|
nonetheless issue memory barriers as appropriate.
|
2009-12-15 22:15:48 +01:00
|
|
|
*/
|
|
|
|
#define make_atomic_load_body(S) \
|
2010-12-01 00:19:49 +01:00
|
|
|
/* Serialize prior load and store operations. */ \
|
|
|
|
asm volatile ("mfence" ::: "memory"); \
|
|
|
|
ret= *a; \
|
|
|
|
/* Prevent compiler from reordering instructions. */ \
|
|
|
|
asm volatile ("" ::: "memory")
|
2009-12-15 22:15:48 +01:00
|
|
|
#define make_atomic_store_body(S) \
|
Bug#22320: my_atomic-t unit test fails
Bug#52261: 64 bit atomic operations do not work on Solaris i386
gcc in debug compilation
One of the various problems was that the source operand to
CMPXCHG8b was marked as a input/output operand, causing GCC
to use the EBX register as the destination register for the
CMPXCHG8b instruction. This could lead to crashes as the EBX
register is also implicitly used by the instruction, causing
the value to be potentially garbaged and a protection fault
once the value is used to access a position in memory.
Another problem was the lack of proper clobbers for the atomic
operations and, also, a discrepancy between the implementations
for the Compare and Set operation. The specific problems are
described and fixed by Kristian Nielsen patches:
Patch: 1
Fix bugs in my_atomic_cas*(val,cmp,new) that *cmp is accessed
after CAS succeds.
In the gcc builtin implementation, problem was that *cmp was
read again after atomic CAS to check if old *val == *cmp;
this fails if CAS is successful and another thread modifies
*cmp in-between.
In the x86-gcc implementation, problem was that *cmp was set
also in the case of successful CAS; this means there is a
window where it can clobber a value written by another thread
after successful CAS.
Patch 2:
Add a GCC asm "memory" clobber to primitives that imply a
memory barrier.
This signifies to GCC that any potentially aliased memory
must be flushed before the operation, and re-read after the
operation, so that read or modification in other threads of
such memory values will work as intended.
In effect, it makes these primitives work as memory barriers
for the compiler as well as the CPU. This is better and more
correct than adding "volatile" to variables.
include/atomic/gcc_builtins.h:
Do not read from *cmp after the operation as it might be
already gone if the operation was successful.
include/atomic/nolock.h:
Prefer system provided atomics over the broken x86 asm.
include/atomic/x86-gcc.h:
Do not mark source operands as input/output operands.
Add proper memory clobbers.
include/my_atomic.h:
Add notes about my_atomic_add and my_atomic_cas behaviors.
unittest/mysys/my_atomic-t.c:
Remove work around, if it fails, there is either a problem
with the atomic operations code or the specific compiler
version should be black-listed.
2010-07-23 14:37:10 +02:00
|
|
|
asm volatile ("; xchg %0, %1;" \
|
|
|
|
: "=m" (*a), "+r" (v) \
|
|
|
|
: "m" (*a) \
|
|
|
|
: "memory")
|
2009-12-15 22:15:48 +01:00
|
|
|
|
2009-10-12 11:00:39 +02:00
|
|
|
#else
|
2009-12-15 22:15:48 +01:00
|
|
|
/*
|
|
|
|
Use default implementations of 64-bit operations since we solved
|
|
|
|
the 64-bit problem on 32-bit platforms for CAS, no need to solve it
|
|
|
|
once more for ADD, LOAD, STORE and FAS as well.
|
|
|
|
Since we already added add32 support, we need to define add64
|
|
|
|
here, but we haven't defined fas, load and store at all, so
|
|
|
|
we can fallback on default implementations.
|
|
|
|
*/
|
2009-10-12 11:00:39 +02:00
|
|
|
#define make_atomic_add_body64 \
|
|
|
|
int64 tmp=*a; \
|
2010-02-19 15:20:29 +01:00
|
|
|
while (!my_atomic_cas64(a, &tmp, tmp+v)) ; \
|
2009-10-12 11:00:39 +02:00
|
|
|
v=tmp;
|
2009-12-15 22:15:48 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
On some platforms (e.g. Mac OS X and Solaris) the ebx register
|
|
|
|
is held as a pointer to the global offset table. Thus we're not
|
|
|
|
allowed to use the b-register on those platforms when compiling
|
Bug#52419: x86 assembly based atomic CAS causes test failures
The problem was that the x86 assembly based atomic CAS
(compare and swap) implementation could copy the wrong
value to the ebx register, where the cmpxchg8b expects
to see part of the "comparand" value. Since the original
value in the ebx register is saved in the stack (that is,
the push instruction causes the stack pointer to change),
a wrong offset could be used if the compiler decides to
put the source of the comparand value in the stack.
The solution is to copy the comparand value directly from
memory. Since the comparand value is 64-bits wide, it is
copied in two steps over to the ebx and ecx registers.
include/atomic/x86-gcc.h:
For reference, an excerpt from a faulty binary follows.
It is a disassembly of my_atomic-t, compiled at -O3 with
ICC 11.0. Most of the code deals with preparations for
a atomic cmpxchg8b operation. This instruction compares
the value in edx:eax with the destination operand. If the
values are equal, the value in ecx:ebx is stored in the
destination, otherwise the value in the destination operand
is copied into edx:eax.
In this case, my_atomic_add64 is implemented as a compare
and exchange. The addition is done over temporary storage
and loaded into the destination if the original term value
is still valid.
volatile int64 a64;
int64 b=0x1000200030004000LL;
a64=0;
mov 0xfffffda8(%ebx),%eax
xor %ebp,%ebp
mov %ebp,(%eax)
mov %ebp,0x4(%eax)
my_atomic_add64(&a64, b);
mov 0xfffffda8(%ebx),%ebp # Load address of a64
mov 0x0(%ebp),%edx # Copy value
mov 0x4(%ebp),%ecx
mov %edx,0xc(%esp) # Assign to tmp var in the stack
mov %ecx,0x10(%esp)
add $0x30004000,%edx # Sum values
adc $0x10002000,%ecx
mov %edx,0x8(%esp) # Save part of result for later
mov 0x0(%ebp),%esi # Copy value of a64 again
mov 0x4(%ebp),%edi
mov 0xc(%esp),%eax # Load the value of a64 used
mov 0x10(%esp),%edx # for comparison
mov %esi,(%esp)
mov %edi,0x4(%esp)
push %ebx # Push %ebx into stack. Changes esp.
mov 0x8(%esp),%ebx # Wrong restore of the result.
lock cmpxchg8b 0x0(%ebp)
sete %cl
pop %ebx
2010-09-17 22:34:15 +02:00
|
|
|
PIC code, to avoid this we push ebx and pop ebx. The new value
|
|
|
|
is copied directly from memory to avoid problems with a implicit
|
|
|
|
manipulation of the stack pointer by the push.
|
2009-12-15 22:15:48 +01:00
|
|
|
|
|
|
|
cmpxchg8b works on both 32-bit platforms and 64-bit platforms but
|
|
|
|
the code here is only used on 32-bit platforms, on 64-bit
|
|
|
|
platforms the much simpler make_atomic_cas_body32 will work
|
|
|
|
fine.
|
|
|
|
*/
|
Bug#22320: my_atomic-t unit test fails
Bug#52261: 64 bit atomic operations do not work on Solaris i386
gcc in debug compilation
One of the various problems was that the source operand to
CMPXCHG8b was marked as a input/output operand, causing GCC
to use the EBX register as the destination register for the
CMPXCHG8b instruction. This could lead to crashes as the EBX
register is also implicitly used by the instruction, causing
the value to be potentially garbaged and a protection fault
once the value is used to access a position in memory.
Another problem was the lack of proper clobbers for the atomic
operations and, also, a discrepancy between the implementations
for the Compare and Set operation. The specific problems are
described and fixed by Kristian Nielsen patches:
Patch: 1
Fix bugs in my_atomic_cas*(val,cmp,new) that *cmp is accessed
after CAS succeds.
In the gcc builtin implementation, problem was that *cmp was
read again after atomic CAS to check if old *val == *cmp;
this fails if CAS is successful and another thread modifies
*cmp in-between.
In the x86-gcc implementation, problem was that *cmp was set
also in the case of successful CAS; this means there is a
window where it can clobber a value written by another thread
after successful CAS.
Patch 2:
Add a GCC asm "memory" clobber to primitives that imply a
memory barrier.
This signifies to GCC that any potentially aliased memory
must be flushed before the operation, and re-read after the
operation, so that read or modification in other threads of
such memory values will work as intended.
In effect, it makes these primitives work as memory barriers
for the compiler as well as the CPU. This is better and more
correct than adding "volatile" to variables.
include/atomic/gcc_builtins.h:
Do not read from *cmp after the operation as it might be
already gone if the operation was successful.
include/atomic/nolock.h:
Prefer system provided atomics over the broken x86 asm.
include/atomic/x86-gcc.h:
Do not mark source operands as input/output operands.
Add proper memory clobbers.
include/my_atomic.h:
Add notes about my_atomic_add and my_atomic_cas behaviors.
unittest/mysys/my_atomic-t.c:
Remove work around, if it fails, there is either a problem
with the atomic operations code or the specific compiler
version should be black-listed.
2010-07-23 14:37:10 +02:00
|
|
|
#define make_atomic_cas_body64 \
|
Bug#52419: x86 assembly based atomic CAS causes test failures
The problem was that the x86 assembly based atomic CAS
(compare and swap) implementation could copy the wrong
value to the ebx register, where the cmpxchg8b expects
to see part of the "comparand" value. Since the original
value in the ebx register is saved in the stack (that is,
the push instruction causes the stack pointer to change),
a wrong offset could be used if the compiler decides to
put the source of the comparand value in the stack.
The solution is to copy the comparand value directly from
memory. Since the comparand value is 64-bits wide, it is
copied in two steps over to the ebx and ecx registers.
include/atomic/x86-gcc.h:
For reference, an excerpt from a faulty binary follows.
It is a disassembly of my_atomic-t, compiled at -O3 with
ICC 11.0. Most of the code deals with preparations for
a atomic cmpxchg8b operation. This instruction compares
the value in edx:eax with the destination operand. If the
values are equal, the value in ecx:ebx is stored in the
destination, otherwise the value in the destination operand
is copied into edx:eax.
In this case, my_atomic_add64 is implemented as a compare
and exchange. The addition is done over temporary storage
and loaded into the destination if the original term value
is still valid.
volatile int64 a64;
int64 b=0x1000200030004000LL;
a64=0;
mov 0xfffffda8(%ebx),%eax
xor %ebp,%ebp
mov %ebp,(%eax)
mov %ebp,0x4(%eax)
my_atomic_add64(&a64, b);
mov 0xfffffda8(%ebx),%ebp # Load address of a64
mov 0x0(%ebp),%edx # Copy value
mov 0x4(%ebp),%ecx
mov %edx,0xc(%esp) # Assign to tmp var in the stack
mov %ecx,0x10(%esp)
add $0x30004000,%edx # Sum values
adc $0x10002000,%ecx
mov %edx,0x8(%esp) # Save part of result for later
mov 0x0(%ebp),%esi # Copy value of a64 again
mov 0x4(%ebp),%edi
mov 0xc(%esp),%eax # Load the value of a64 used
mov 0x10(%esp),%edx # for comparison
mov %esi,(%esp)
mov %edi,0x4(%esp)
push %ebx # Push %ebx into stack. Changes esp.
mov 0x8(%esp),%ebx # Wrong restore of the result.
lock cmpxchg8b 0x0(%ebp)
sete %cl
pop %ebx
2010-09-17 22:34:15 +02:00
|
|
|
asm volatile ("push %%ebx;" \
|
|
|
|
"movl (%%ecx), %%ebx;" \
|
|
|
|
"movl 4(%%ecx), %%ecx;" \
|
2013-05-07 18:28:36 +02:00
|
|
|
LOCK_prefix "; cmpxchg8b (%%esi);" \
|
Bug#52419: x86 assembly based atomic CAS causes test failures
The problem was that the x86 assembly based atomic CAS
(compare and swap) implementation could copy the wrong
value to the ebx register, where the cmpxchg8b expects
to see part of the "comparand" value. Since the original
value in the ebx register is saved in the stack (that is,
the push instruction causes the stack pointer to change),
a wrong offset could be used if the compiler decides to
put the source of the comparand value in the stack.
The solution is to copy the comparand value directly from
memory. Since the comparand value is 64-bits wide, it is
copied in two steps over to the ebx and ecx registers.
include/atomic/x86-gcc.h:
For reference, an excerpt from a faulty binary follows.
It is a disassembly of my_atomic-t, compiled at -O3 with
ICC 11.0. Most of the code deals with preparations for
a atomic cmpxchg8b operation. This instruction compares
the value in edx:eax with the destination operand. If the
values are equal, the value in ecx:ebx is stored in the
destination, otherwise the value in the destination operand
is copied into edx:eax.
In this case, my_atomic_add64 is implemented as a compare
and exchange. The addition is done over temporary storage
and loaded into the destination if the original term value
is still valid.
volatile int64 a64;
int64 b=0x1000200030004000LL;
a64=0;
mov 0xfffffda8(%ebx),%eax
xor %ebp,%ebp
mov %ebp,(%eax)
mov %ebp,0x4(%eax)
my_atomic_add64(&a64, b);
mov 0xfffffda8(%ebx),%ebp # Load address of a64
mov 0x0(%ebp),%edx # Copy value
mov 0x4(%ebp),%ecx
mov %edx,0xc(%esp) # Assign to tmp var in the stack
mov %ecx,0x10(%esp)
add $0x30004000,%edx # Sum values
adc $0x10002000,%ecx
mov %edx,0x8(%esp) # Save part of result for later
mov 0x0(%ebp),%esi # Copy value of a64 again
mov 0x4(%ebp),%edi
mov 0xc(%esp),%eax # Load the value of a64 used
mov 0x10(%esp),%edx # for comparison
mov %esi,(%esp)
mov %edi,0x4(%esp)
push %ebx # Push %ebx into stack. Changes esp.
mov 0x8(%esp),%ebx # Wrong restore of the result.
lock cmpxchg8b 0x0(%ebp)
sete %cl
pop %ebx
2010-09-17 22:34:15 +02:00
|
|
|
"setz %2; pop %%ebx" \
|
2013-05-07 18:28:36 +02:00
|
|
|
: "+S" (a), "+A" (*cmp), "=c" (ret) \
|
|
|
|
: "c" (&set) \
|
Bug#22320: my_atomic-t unit test fails
Bug#52261: 64 bit atomic operations do not work on Solaris i386
gcc in debug compilation
One of the various problems was that the source operand to
CMPXCHG8b was marked as a input/output operand, causing GCC
to use the EBX register as the destination register for the
CMPXCHG8b instruction. This could lead to crashes as the EBX
register is also implicitly used by the instruction, causing
the value to be potentially garbaged and a protection fault
once the value is used to access a position in memory.
Another problem was the lack of proper clobbers for the atomic
operations and, also, a discrepancy between the implementations
for the Compare and Set operation. The specific problems are
described and fixed by Kristian Nielsen patches:
Patch: 1
Fix bugs in my_atomic_cas*(val,cmp,new) that *cmp is accessed
after CAS succeds.
In the gcc builtin implementation, problem was that *cmp was
read again after atomic CAS to check if old *val == *cmp;
this fails if CAS is successful and another thread modifies
*cmp in-between.
In the x86-gcc implementation, problem was that *cmp was set
also in the case of successful CAS; this means there is a
window where it can clobber a value written by another thread
after successful CAS.
Patch 2:
Add a GCC asm "memory" clobber to primitives that imply a
memory barrier.
This signifies to GCC that any potentially aliased memory
must be flushed before the operation, and re-read after the
operation, so that read or modification in other threads of
such memory values will work as intended.
In effect, it makes these primitives work as memory barriers
for the compiler as well as the CPU. This is better and more
correct than adding "volatile" to variables.
include/atomic/gcc_builtins.h:
Do not read from *cmp after the operation as it might be
already gone if the operation was successful.
include/atomic/nolock.h:
Prefer system provided atomics over the broken x86 asm.
include/atomic/x86-gcc.h:
Do not mark source operands as input/output operands.
Add proper memory clobbers.
include/my_atomic.h:
Add notes about my_atomic_add and my_atomic_cas behaviors.
unittest/mysys/my_atomic-t.c:
Remove work around, if it fails, there is either a problem
with the atomic operations code or the specific compiler
version should be black-listed.
2010-07-23 14:37:10 +02:00
|
|
|
: "memory", "esp")
|
2009-10-12 11:00:39 +02:00
|
|
|
#endif
|
|
|
|
|
2009-12-15 22:15:48 +01:00
|
|
|
/*
|
|
|
|
The implementation of make_atomic_cas_body32 is adaptable to
|
|
|
|
the OS word size, so on 64-bit platforms it will automatically
|
|
|
|
adapt to 64-bits and so it will work also on 64-bit platforms
|
|
|
|
*/
|
|
|
|
#define make_atomic_cas_bodyptr make_atomic_cas_body32
|
2009-10-12 11:00:39 +02:00
|
|
|
|
2006-05-31 18:44:09 +02:00
|
|
|
#ifdef MY_ATOMIC_MODE_DUMMY
|
2006-06-17 16:20:39 +02:00
|
|
|
#define make_atomic_load_body(S) ret=*a
|
|
|
|
#define make_atomic_store_body(S) *a=v
|
2006-05-31 18:44:09 +02:00
|
|
|
#endif
|
2009-09-23 23:32:31 +02:00
|
|
|
#endif /* ATOMIC_X86_GCC_INCLUDED */
|