]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseSynchronizationLib/X64/GccInline.c
4c4d6e3fc7128f189faa9297e992c463ea416eb5
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / X64 / GccInline.c
1 /** @file
2 GCC inline implementation of BaseSynchronizationLib processor specific functions.
3
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6 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.
24
25 @param Value A pointer to the 32-bit value to increment.
26
27 @return The incremented value.
28
29 **/
30 UINT32
31 EFIAPI
32 InternalSyncIncrement (
33 IN volatile UINT32 *Value
34 )
35 {
36 UINT32 Result;
37
38 __asm__ __volatile__ (
39 "movl $1, %%eax \n\t"
40 "lock \n\t"
41 "xadd %%eax, %2 \n\t"
42 "inc %%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 Performs an atomic decrement of an 32-bit unsigned integer.
56
57 Performs an atomic decrement of the 32-bit unsigned integer specified by
58 Value and returns the decremented value. The decrement operation must be
59 performed using MP safe mechanisms.
60
61 @param Value A pointer to the 32-bit value to decrement.
62
63 @return The decremented value.
64
65 **/
66 UINT32
67 EFIAPI
68 InternalSyncDecrement (
69 IN volatile UINT32 *Value
70 )
71 {
72 UINT32 Result;
73
74 __asm__ __volatile__ (
75 "movl $-1, %%eax \n\t"
76 "lock \n\t"
77 "xadd %%eax, %2 \n\t"
78 "dec %%eax "
79 : "=a" (Result), // %0
80 "=m" (*Value) // %1
81 : "m" (*Value) // %2
82 : "memory",
83 "cc"
84 );
85
86 return Result;
87 }
88
89
90 /**
91 Performs an atomic compare exchange operation on a 16-bit unsigned integer.
92
93 Performs an atomic compare exchange operation on the 16-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 16-bit value for the compare exchange
101 operation.
102 @param CompareValue 16-bit value used in compare operation.
103 @param ExchangeValue 16-bit value used in exchange operation.
104
105 @return The original *Value before exchange.
106
107 **/
108 UINT16
109 EFIAPI
110 InternalSyncCompareExchange16 (
111 IN OUT volatile UINT16 *Value,
112 IN UINT16 CompareValue,
113 IN UINT16 ExchangeValue
114 )
115 {
116
117
118 __asm__ __volatile__ (
119 "lock \n\t"
120 "cmpxchgw %3, %1 "
121 : "=a" (CompareValue),
122 "=m" (*Value)
123 : "a" (CompareValue),
124 "r" (ExchangeValue),
125 "m" (*Value)
126 : "memory",
127 "cc"
128 );
129
130 return CompareValue;
131 }
132
133
134 /**
135 Performs an atomic compare exchange operation on a 32-bit unsigned integer.
136
137 Performs an atomic compare exchange operation on the 32-bit unsigned integer
138 specified by Value. If Value is equal to CompareValue, then Value is set to
139 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,
140 then Value is returned. The compare exchange operation must be performed using
141 MP safe mechanisms.
142
143
144 @param Value A pointer to the 32-bit value for the compare exchange
145 operation.
146 @param CompareValue 32-bit value used in compare operation.
147 @param ExchangeValue 32-bit value used in exchange operation.
148
149 @return The original *Value before exchange.
150
151 **/
152 UINT32
153 EFIAPI
154 InternalSyncCompareExchange32 (
155 IN OUT volatile UINT32 *Value,
156 IN UINT32 CompareValue,
157 IN UINT32 ExchangeValue
158 )
159 {
160
161
162 __asm__ __volatile__ (
163 "lock \n\t"
164 "cmpxchgl %3, %1 "
165 : "=a" (CompareValue), // %0
166 "=m" (*Value) // %1
167 : "a" (CompareValue), // %2
168 "r" (ExchangeValue), // %3
169 "m" (*Value)
170 : "memory",
171 "cc"
172 );
173
174 return CompareValue;
175 }
176
177
178 /**
179 Performs an atomic compare exchange operation on a 64-bit unsigned integer.
180
181 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified
182 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and
183 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.
184 The compare exchange operation must be performed using MP safe mechanisms.
185
186
187 @param Value A pointer to the 64-bit value for the compare exchange
188 operation.
189 @param CompareValue 64-bit value used in compare operation.
190 @param ExchangeValue 64-bit value used in exchange operation.
191
192 @return The original *Value before exchange.
193
194 **/
195 UINT64
196 EFIAPI
197 InternalSyncCompareExchange64 (
198 IN OUT volatile UINT64 *Value,
199 IN UINT64 CompareValue,
200 IN UINT64 ExchangeValue
201 )
202 {
203
204 __asm__ __volatile__ (
205 "lock \n\t"
206 "cmpxchgq %3, %1 "
207 : "=a" (CompareValue), // %0
208 "=m" (*Value) // %1
209 : "a" (CompareValue), // %2
210 "r" (ExchangeValue), // %3
211 "m" (*Value)
212 : "memory",
213 "cc"
214 );
215
216 return CompareValue;
217 }
218
219