]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/Ia32/GccInline.c
Fix file header
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / Ia32 / GccInline.c
1 /** @file
2 GCC inline implementation of BaseSynchronizationLib processor specific functions.
3
4 Copyright (c) 2006 - 2009, Intel Corporation<BR>
5 Portions copyright (c) 2008-2009 Apple Inc. All rights reserved.<BR>
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16
17
18 /**
19 Performs an atomic increment of an 32-bit unsigned integer.
20
21 Performs an atomic increment of the 32-bit unsigned integer specified by
22 Value and returns the incremented value. The increment operation must be
23 performed using MP safe mechanisms. The state of the return value is not
24 guaranteed to be MP safe.
25
26 @param Value A pointer to the 32-bit value to increment.
27
28 @return The incremented value.
29
30 **/
31 UINT32
32 EFIAPI
33 InternalSyncIncrement (
34 IN volatile UINT32 *Value
35 )
36 {
37 UINT32 Result;
38
39 __asm__ __volatile__ (
40 "lock \n\t"
41 "incl %2 \n\t"
42 "movl %2, %%eax "
43 : "=a" (Result), // %0
44 "=m" (*Value) // %1
45 : "m" (*Value) // %2
46 : "memory",
47 "cc"
48 );
49
50 return Result;
51
52 }
53
54
55 /**
56 Performs an atomic decrement of an 32-bit unsigned integer.
57
58 Performs an atomic decrement of the 32-bit unsigned integer specified by
59 Value and returns the decremented value. The decrement operation must be
60 performed using MP safe mechanisms. The state of the return value is not
61 guaranteed to be MP safe.
62
63 @param Value A pointer to the 32-bit value to decrement.
64
65 @return The decremented value.
66
67 **/
68 UINT32
69 EFIAPI
70 InternalSyncDecrement (
71 IN volatile UINT32 *Value
72 )
73 {
74 UINT32 Result;
75
76 __asm__ __volatile__ (
77 "lock \n\t"
78 "decl %2 \n\t"
79 "movl %2, %%eax "
80 : "=a" (Result), // %0
81 "=m" (*Value) // %1
82 : "m" (*Value) // %2
83 : "memory",
84 "cc"
85 );
86
87 return Result;
88 }
89
90 /**
91 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
92
93 Performs an atomic compare exchange operation on the 32-bit unsigned integer
94 specified by Value. If Value is equal to CompareValue, then Value is set to
95 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
96 then Value is returned. The compare exchange operation must be performed using
97 MP safe mechanisms.
98
99
100 @param Value A pointer to the 32-bit value for the compare exchange
101 operation.
102 @param CompareValue 32-bit value used in compare operation.
103 @param ExchangeValue 32-bit value used in exchange operation.
104
105 @return The original *Value before exchange.
106
107 **/
108 UINT32
109 EFIAPI
110 InternalSyncCompareExchange32 (
111 IN OUT volatile UINT32 *Value,
112 IN UINT32 CompareValue,
113 IN UINT32 ExchangeValue
114 )
115 {
116
117 // GCC 4.1 and forward supports atomic builtins
118 #if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 1)))
119
120 return __sync_val_compare_and_swap (Value, CompareValue, ExchangeValue);
121
122 #else
123
124 __asm__ __volatile__ (
125 " \n\t"
126 "lock \n\t"
127 "cmpxchgl %1, %2 \n\t"
128 : "=a" (CompareValue) // %0
129 : "q" (ExchangeValue), // %1
130 "m" (*Value), // %2
131 "0" (CompareValue) // %4
132 : "memory",
133 "cc"
134 );
135
136 return CompareValue;
137
138 #endif
139 }
140
141 /**
142 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
143
144 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
145 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
146 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
147 The compare exchange operation must be performed using MP safe mechanisms.
148
149
150 @param Value A pointer to the 64-bit value for the compare exchange
151 operation.
152 @param CompareValue 64-bit value used in compare operation.
153 @param ExchangeValue 64-bit value used in exchange operation.
154
155 @return The original *Value before exchange.
156
157 **/
158 UINT64
159 EFIAPI
160 InternalSyncCompareExchange64 (
161 IN OUT volatile UINT64 *Value,
162 IN UINT64 CompareValue,
163 IN UINT64 ExchangeValue
164 )
165 {
166 // GCC 4.1 and forward supports atomic builtins
167 #if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 1)))
168
169 return __sync_val_compare_and_swap (Value, CompareValue, ExchangeValue);
170
171 #else
172
173 __asm__ __volatile__ (
174 " \n\t"
175 "push %%ebx \n\t"
176 "movl %2,%%ebx \n\t"
177 "lock \n\t"
178 "cmpxchg8b (%1) \n\t"
179 "pop %%ebx \n\t"
180 : "+A" (CompareValue) // %0
181 : "S" (Value), // %1
182 "r" ((UINT32) ExchangeValue), // %2
183 "c" ((UINT32) (ExchangeValue >> 32)) // %3
184 : "memory",
185 "cc"
186 );
187
188 return CompareValue;
189
190 #endif
191 }
192
193