MdePkg/SynchronizationLib: fix Interlocked[De|In]crement return value

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1197

Today's InterlockedIncrement()/InterlockedDecrement() guarantees to
perform atomic increment/decrement but doesn't guarantee the return
value equals to the new value.

The patch fixes the behavior to use "XADD" instruction to guarantee
the return value equals to the new value.

The patch calls intrinsic functions for MSVC tool chain, calls the
NASM implementation for INTEL tool chain and calls GCC inline
assembly implementation (GccInline.c) for GCC tool chain.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
This commit is contained in:
Ruiyu Ni
2018-09-07 17:26:14 +08:00
parent ca3e4f8ab8
commit 17634d026f
16 changed files with 56 additions and 152 deletions

View File

@ -144,8 +144,7 @@ ReleaseSpinLock (
Performs an atomic increment of the 32-bit unsigned integer specified by Performs an atomic increment of the 32-bit unsigned integer specified by
Value and returns the incremented value. The increment operation must be Value and returns the incremented value. The increment operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
If Value is NULL, then ASSERT(). If Value is NULL, then ASSERT().
@ -166,8 +165,7 @@ InterlockedIncrement (
Performs an atomic decrement of the 32-bit unsigned integer specified by Performs an atomic decrement of the 32-bit unsigned integer specified by
Value and returns the decremented value. The decrement operation must be Value and returns the decremented value. The decrement operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
If Value is NULL, then ASSERT(). If Value is NULL, then ASSERT().

View File

@ -34,8 +34,8 @@
Ia32/InterlockedCompareExchange64.c | MSFT Ia32/InterlockedCompareExchange64.c | MSFT
Ia32/InterlockedCompareExchange32.c | MSFT Ia32/InterlockedCompareExchange32.c | MSFT
Ia32/InterlockedCompareExchange16.c | MSFT Ia32/InterlockedCompareExchange16.c | MSFT
Ia32/InterlockedDecrement.c | MSFT InterlockedIncrementMsc.c | MSFT
Ia32/InterlockedIncrement.c | MSFT InterlockedDecrementMsc.c | MSFT
SynchronizationMsc.c | MSFT SynchronizationMsc.c | MSFT
Ia32/InterlockedCompareExchange64.nasm| INTEL Ia32/InterlockedCompareExchange64.nasm| INTEL
@ -54,15 +54,13 @@
X64/InterlockedCompareExchange64.c | MSFT X64/InterlockedCompareExchange64.c | MSFT
X64/InterlockedCompareExchange32.c | MSFT X64/InterlockedCompareExchange32.c | MSFT
X64/InterlockedCompareExchange16.c | MSFT X64/InterlockedCompareExchange16.c | MSFT
InterlockedIncrementMsc.c | MSFT
InterlockedDecrementMsc.c | MSFT
SynchronizationMsc.c | MSFT
X64/InterlockedCompareExchange64.nasm| INTEL X64/InterlockedCompareExchange64.nasm| INTEL
X64/InterlockedCompareExchange32.nasm| INTEL X64/InterlockedCompareExchange32.nasm| INTEL
X64/InterlockedCompareExchange16.nasm| INTEL X64/InterlockedCompareExchange16.nasm| INTEL
X64/InterlockedDecrement.c | MSFT
X64/InterlockedIncrement.c | MSFT
SynchronizationMsc.c | MSFT
X64/InterlockedDecrement.nasm | INTEL X64/InterlockedDecrement.nasm | INTEL
X64/InterlockedIncrement.nasm | INTEL X64/InterlockedIncrement.nasm | INTEL
Synchronization.c | INTEL Synchronization.c | INTEL

View File

@ -27,8 +27,7 @@
Performs an atomic increment of the 32-bit unsigned integer specified by Performs an atomic increment of the 32-bit unsigned integer specified by
Value and returns the incremented value. The increment operation must be Value and returns the incremented value. The increment operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
@param Value A pointer to the 32-bit value to increment. @param Value A pointer to the 32-bit value to increment.
@ -47,8 +46,7 @@ InternalSyncIncrement (
Performs an atomic decrement of the 32-bit unsigned integer specified by Performs an atomic decrement of the 32-bit unsigned integer specified by
Value and returns the decrement value. The decrement operation must be Value and returns the decrement value. The decrement operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
@param Value A pointer to the 32-bit value to decrement. @param Value A pointer to the 32-bit value to decrement.

View File

@ -20,8 +20,7 @@
Performs an atomic increment of the 32-bit unsigned integer specified by Performs an atomic increment of the 32-bit unsigned integer specified by
Value and returns the incremented value. The increment operation must be Value and returns the incremented value. The increment operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
@param Value A pointer to the 32-bit value to increment. @param Value A pointer to the 32-bit value to increment.
@ -37,9 +36,10 @@ InternalSyncIncrement (
UINT32 Result; UINT32 Result;
__asm__ __volatile__ ( __asm__ __volatile__ (
"movl $1, %%eax \n\t"
"lock \n\t" "lock \n\t"
"incl %2 \n\t" "xadd %%eax, %2 \n\t"
"movl %2, %%eax " "inc %%eax "
: "=a" (Result), // %0 : "=a" (Result), // %0
"=m" (*Value) // %1 "=m" (*Value) // %1
: "m" (*Value) // %2 : "m" (*Value) // %2
@ -57,8 +57,7 @@ InternalSyncIncrement (
Performs an atomic decrement of the 32-bit unsigned integer specified by Performs an atomic decrement of the 32-bit unsigned integer specified by
Value and returns the decremented value. The decrement operation must be Value and returns the decremented value. The decrement operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
@param Value A pointer to the 32-bit value to decrement. @param Value A pointer to the 32-bit value to decrement.
@ -74,9 +73,10 @@ InternalSyncDecrement (
UINT32 Result; UINT32 Result;
__asm__ __volatile__ ( __asm__ __volatile__ (
"movl $-1, %%eax \n\t"
"lock \n\t" "lock \n\t"
"decl %2 \n\t" "xadd %%eax, %2 \n\t"
"movl %2, %%eax " "dec %%eax "
: "=a" (Result), // %0 : "=a" (Result), // %0
"=m" (*Value) // %1 "=m" (*Value) // %1
: "m" (*Value) // %2 : "m" (*Value) // %2

View File

@ -1,42 +0,0 @@
/** @file
InterlockedDecrement function
Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
/**
Performs an atomic decrement of an 32-bit unsigned integer.
Performs an atomic decrement of the 32-bit unsigned integer specified by
Value and returns the decrement value. The decrement operation must be
performed using MP safe mechanisms. The state of the return value is not
guaranteed to be MP safe.
@param Value A pointer to the 32-bit value to decrement.
@return The decrement value.
**/
UINT32
EFIAPI
InternalSyncDecrement (
IN volatile UINT32 *Value
)
{
_asm {
mov eax, Value
lock dec dword ptr [eax]
mov eax, [eax]
}
}

View File

@ -1,6 +1,6 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; ;
; Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR> ; Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
; This program and the accompanying materials ; This program and the accompanying materials
; are licensed and made available under the terms and conditions of the BSD License ; are licensed and made available under the terms and conditions of the BSD License
; which accompanies this distribution. The full text of the license may be found at ; which accompanies this distribution. The full text of the license may be found at
@ -32,8 +32,8 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(InternalSyncDecrement) global ASM_PFX(InternalSyncDecrement)
ASM_PFX(InternalSyncDecrement): ASM_PFX(InternalSyncDecrement):
mov eax, [esp + 4] mov ecx, [esp + 4]
lock dec dword [eax] mov eax, 0FFFFFFFFh
mov eax, [eax] lock xadd dword [ecx], eax
dec eax
ret ret

View File

@ -1,43 +0,0 @@
/** @file
InterLockedIncrement function
Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
/**
Performs an atomic increment of an 32-bit unsigned integer.
Performs an atomic increment of the 32-bit unsigned integer specified by
Value and returns the incremented value. The increment operation must be
performed using MP safe mechanisms. The state of the return value is not
guaranteed to be MP safe.
@param Value A pointer to the 32-bit value to increment.
@return The incremented value.
**/
UINT32
EFIAPI
InternalSyncIncrement (
IN volatile UINT32 *Value
)
{
_asm {
mov eax, Value
lock inc dword ptr [eax]
mov eax, [eax]
}
}

View File

@ -32,8 +32,9 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(InternalSyncIncrement) global ASM_PFX(InternalSyncIncrement)
ASM_PFX(InternalSyncIncrement): ASM_PFX(InternalSyncIncrement):
mov eax, [esp + 4] mov ecx, [esp + 4]
lock inc dword [eax] mov eax, 1
mov eax, [eax] lock xadd dword [ecx], eax
inc eax
ret ret

View File

@ -1,7 +1,7 @@
/** @file /** @file
InterlockedDecrement function InterlockedDecrement function
Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR> Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at which accompanies this distribution. The full text of the license may be found at
@ -41,6 +41,6 @@ InternalSyncDecrement (
IN volatile UINT32 *Value IN volatile UINT32 *Value
) )
{ {
return _InterlockedDecrement ((long *)(UINTN)(Value)); return _InterlockedDecrement ((long *)(Value));
} }

View File

@ -1,7 +1,7 @@
/** @file /** @file
InterLockedIncrement function InterLockedIncrement function
Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR> Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at which accompanies this distribution. The full text of the license may be found at
@ -41,6 +41,6 @@ InternalSyncIncrement (
IN volatile UINT32 *Value IN volatile UINT32 *Value
) )
{ {
return _InterlockedIncrement ((long *)(UINTN)(Value)); return _InterlockedIncrement ((long *)(Value));
} }

View File

@ -231,8 +231,7 @@ ReleaseSpinLock (
Performs an atomic increment of the 32-bit unsigned integer specified by Performs an atomic increment of the 32-bit unsigned integer specified by
Value and returns the incremented value. The increment operation must be Value and returns the incremented value. The increment operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
If Value is NULL, then ASSERT(). If Value is NULL, then ASSERT().
@ -256,8 +255,7 @@ InterlockedIncrement (
Performs an atomic decrement of the 32-bit unsigned integer specified by Performs an atomic decrement of the 32-bit unsigned integer specified by
Value and returns the decremented value. The decrement operation must be Value and returns the decremented value. The decrement operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
If Value is NULL, then ASSERT(). If Value is NULL, then ASSERT().

View File

@ -247,8 +247,7 @@ ReleaseSpinLock (
Performs an atomic increment of the 32-bit unsigned integer specified by Performs an atomic increment of the 32-bit unsigned integer specified by
Value and returns the incremented value. The increment operation must be Value and returns the incremented value. The increment operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
If Value is NULL, then ASSERT(). If Value is NULL, then ASSERT().
@ -272,8 +271,7 @@ InterlockedIncrement (
Performs an atomic decrement of the 32-bit unsigned integer specified by Performs an atomic decrement of the 32-bit unsigned integer specified by
Value and returns the decremented value. The decrement operation must be Value and returns the decremented value. The decrement operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
If Value is NULL, then ASSERT(). If Value is NULL, then ASSERT().

View File

@ -249,8 +249,7 @@ ReleaseSpinLock (
Performs an atomic increment of the 32-bit unsigned integer specified by Performs an atomic increment of the 32-bit unsigned integer specified by
Value and returns the incremented value. The increment operation must be Value and returns the incremented value. The increment operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
If Value is NULL, then ASSERT(). If Value is NULL, then ASSERT().
@ -274,8 +273,7 @@ InterlockedIncrement (
Performs an atomic decrement of the 32-bit unsigned integer specified by Performs an atomic decrement of the 32-bit unsigned integer specified by
Value and returns the decremented value. The decrement operation must be Value and returns the decremented value. The decrement operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
If Value is NULL, then ASSERT(). If Value is NULL, then ASSERT().

View File

@ -15,14 +15,12 @@
/** /**
Performs an atomic increment of an 32-bit unsigned integer. Performs an atomic increment of an 32-bit unsigned integer.
Performs an atomic increment of the 32-bit unsigned integer specified by Performs an atomic increment of the 32-bit unsigned integer specified by
Value and returns the incremented value. The increment operation must be Value and returns the incremented value. The increment operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
@param Value A pointer to the 32-bit value to increment. @param Value A pointer to the 32-bit value to increment.
@ -38,9 +36,10 @@ InternalSyncIncrement (
UINT32 Result; UINT32 Result;
__asm__ __volatile__ ( __asm__ __volatile__ (
"movl $1, %%eax \n\t"
"lock \n\t" "lock \n\t"
"incl %2 \n\t" "xadd %%eax, %2 \n\t"
"mov %2, %%eax " "inc %%eax "
: "=a" (Result), // %0 : "=a" (Result), // %0
"=m" (*Value) // %1 "=m" (*Value) // %1
: "m" (*Value) // %2 : "m" (*Value) // %2
@ -57,8 +56,7 @@ InternalSyncIncrement (
Performs an atomic decrement of the 32-bit unsigned integer specified by Performs an atomic decrement of the 32-bit unsigned integer specified by
Value and returns the decremented value. The decrement operation must be Value and returns the decremented value. The decrement operation must be
performed using MP safe mechanisms. The state of the return value is not performed using MP safe mechanisms.
guaranteed to be MP safe.
@param Value A pointer to the 32-bit value to decrement. @param Value A pointer to the 32-bit value to decrement.
@ -74,9 +72,10 @@ InternalSyncDecrement (
UINT32 Result; UINT32 Result;
__asm__ __volatile__ ( __asm__ __volatile__ (
"movl $-1, %%eax \n\t"
"lock \n\t" "lock \n\t"
"decl %2 \n\t" "xadd %%eax, %2 \n\t"
"mov %2, %%eax " "dec %%eax "
: "=a" (Result), // %0 : "=a" (Result), // %0
"=m" (*Value) // %1 "=m" (*Value) // %1
: "m" (*Value) // %2 : "m" (*Value) // %2

View File

@ -1,6 +1,6 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
; ;
; Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR> ; Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
; This program and the accompanying materials ; This program and the accompanying materials
; are licensed and made available under the terms and conditions of the BSD License ; are licensed and made available under the terms and conditions of the BSD License
; which accompanies this distribution. The full text of the license may be found at ; which accompanies this distribution. The full text of the license may be found at
@ -33,7 +33,7 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(InternalSyncDecrement) global ASM_PFX(InternalSyncDecrement)
ASM_PFX(InternalSyncDecrement): ASM_PFX(InternalSyncDecrement):
lock dec dword [rcx] mov eax, 0FFFFFFFFh
mov eax, [rcx] lock xadd dword [rcx], eax
dec eax
ret ret

View File

@ -33,7 +33,8 @@
;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------
global ASM_PFX(InternalSyncIncrement) global ASM_PFX(InternalSyncIncrement)
ASM_PFX(InternalSyncIncrement): ASM_PFX(InternalSyncIncrement):
lock inc dword [rcx] mov eax, 1
mov eax, [rcx] lock xadd dword [rcx], eax
inc eax
ret ret