]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseSynchronizationLib/SynchronizationGcc.c
MdePkg/X64/ProcessorBind.h: Fix EmulatorPkg X64 XCODE5
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / SynchronizationGcc.c
CommitLineData
720d3c5f 1/** @file\r
2 Implementation of synchronization functions.\r
3\r
9095d37b 4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
e2d9bfb2 5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
9344f092 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
720d3c5f 7\r
8**/\r
9\r
10#include "BaseSynchronizationLibInternals.h"\r
11\r
12//\r
9095d37b 13// GCC inline assembly for Read Write Barrier\r
720d3c5f 14//\r
ebd04fc2 15#define _ReadWriteBarrier() do { __asm__ __volatile__ ("": : : "memory"); } while(0)\r
720d3c5f 16\r
17#define SPIN_LOCK_RELEASED ((UINTN) 1)\r
18#define SPIN_LOCK_ACQUIRED ((UINTN) 2)\r
19\r
20/**\r
21 Retrieves the architecture specific spin lock alignment requirements for\r
22 optimal spin lock performance.\r
23\r
24 This function retrieves the spin lock alignment requirements for optimal\r
9095d37b 25 performance on a given CPU architecture. The spin lock alignment is byte alignment.\r
744265eb 26 It must be a power of two and is returned by this function. If there are no alignment\r
720d3c5f 27 requirements, then 1 must be returned. The spin lock synchronization\r
28 functions must function correctly if the spin lock size and alignment values\r
29 returned by this function are not used at all. These values are hints to the\r
30 consumers of the spin lock synchronization functions to obtain optimal spin\r
31 lock performance.\r
32\r
33 @return The architecture specific spin lock alignment.\r
34\r
35**/\r
36UINTN\r
37EFIAPI\r
38GetSpinLockProperties (\r
39 VOID\r
40 )\r
41{\r
5f0a17d8 42 return InternalGetSpinLockProperties ();\r
720d3c5f 43}\r
44\r
45/**\r
46 Initializes a spin lock to the released state and returns the spin lock.\r
47\r
48 This function initializes the spin lock specified by SpinLock to the released\r
49 state, and returns SpinLock. Optimal performance can be achieved by calling\r
50 GetSpinLockProperties() to determine the size and alignment requirements for\r
51 SpinLock.\r
52\r
53 If SpinLock is NULL, then ASSERT().\r
54\r
55 @param SpinLock A pointer to the spin lock to initialize to the released\r
56 state.\r
57\r
2fc59a00 58 @return SpinLock is in release state.\r
720d3c5f 59\r
60**/\r
61SPIN_LOCK *\r
62EFIAPI\r
63InitializeSpinLock (\r
64 OUT SPIN_LOCK *SpinLock\r
65 )\r
66{\r
67 ASSERT (SpinLock != NULL);\r
68\r
69 _ReadWriteBarrier();\r
70 *SpinLock = SPIN_LOCK_RELEASED;\r
71 _ReadWriteBarrier();\r
72\r
73 return SpinLock;\r
74}\r
75\r
76/**\r
77 Waits until a spin lock can be placed in the acquired state.\r
78\r
79 This function checks the state of the spin lock specified by SpinLock. If\r
80 SpinLock is in the released state, then this function places SpinLock in the\r
81 acquired state and returns SpinLock. Otherwise, this function waits\r
82 indefinitely for the spin lock to be released, and then places it in the\r
83 acquired state and returns SpinLock. All state transitions of SpinLock must\r
84 be performed using MP safe mechanisms.\r
85\r
86 If SpinLock is NULL, then ASSERT().\r
87 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
88 If PcdSpinLockTimeout is not zero, and SpinLock is can not be acquired in\r
89 PcdSpinLockTimeout microseconds, then ASSERT().\r
90\r
91 @param SpinLock A pointer to the spin lock to place in the acquired state.\r
92\r
2fc59a00 93 @return SpinLock acquired the lock.\r
720d3c5f 94\r
95**/\r
96SPIN_LOCK *\r
97EFIAPI\r
98AcquireSpinLock (\r
99 IN OUT SPIN_LOCK *SpinLock\r
100 )\r
101{\r
102 UINT64 Current;\r
103 UINT64 Previous;\r
104 UINT64 Total;\r
105 UINT64 Start;\r
106 UINT64 End;\r
107 UINT64 Timeout;\r
108 INT64 Cycle;\r
109 INT64 Delta;\r
110\r
0f18e1ed
JF
111 if (PcdGet32 (PcdSpinLockTimeout) == 0) {\r
112 while (!AcquireSpinLockOrFail (SpinLock)) {\r
113 CpuPause ();\r
114 }\r
115 } else if (!AcquireSpinLockOrFail (SpinLock)) {\r
720d3c5f 116 //\r
117 // Get the current timer value\r
118 //\r
119 Current = GetPerformanceCounter();\r
120\r
121 //\r
122 // Initialize local variables\r
123 //\r
124 Start = 0;\r
125 End = 0;\r
126 Total = 0;\r
127\r
128 //\r
129 // Retrieve the performance counter properties and compute the number of performance\r
130 // counter ticks required to reach the timeout\r
131 //\r
132 Timeout = DivU64x32 (\r
133 MultU64x32 (\r
134 GetPerformanceCounterProperties (&Start, &End),\r
135 PcdGet32 (PcdSpinLockTimeout)\r
136 ),\r
137 1000000\r
138 );\r
139 Cycle = End - Start;\r
140 if (Cycle < 0) {\r
141 Cycle = -Cycle;\r
142 }\r
143 Cycle++;\r
144\r
145 while (!AcquireSpinLockOrFail (SpinLock)) {\r
146 CpuPause ();\r
147 Previous = Current;\r
148 Current = GetPerformanceCounter();\r
149 Delta = (INT64) (Current - Previous);\r
150 if (Start > End) {\r
151 Delta = -Delta;\r
152 }\r
153 if (Delta < 0) {\r
154 Delta += Cycle;\r
155 }\r
156 Total += Delta;\r
157 ASSERT (Total < Timeout);\r
158 }\r
720d3c5f 159 }\r
160 return SpinLock;\r
161}\r
162\r
163/**\r
164 Attempts to place a spin lock in the acquired state.\r
165\r
166 This function checks the state of the spin lock specified by SpinLock. If\r
167 SpinLock is in the released state, then this function places SpinLock in the\r
168 acquired state and returns TRUE. Otherwise, FALSE is returned. All state\r
169 transitions of SpinLock must be performed using MP safe mechanisms.\r
170\r
171 If SpinLock is NULL, then ASSERT().\r
172 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
173\r
174 @param SpinLock A pointer to the spin lock to place in the acquired state.\r
175\r
176 @retval TRUE SpinLock was placed in the acquired state.\r
177 @retval FALSE SpinLock could not be acquired.\r
178\r
179**/\r
180BOOLEAN\r
181EFIAPI\r
182AcquireSpinLockOrFail (\r
183 IN OUT SPIN_LOCK *SpinLock\r
184 )\r
185{\r
186 SPIN_LOCK LockValue;\r
187 VOID *Result;\r
9095d37b 188\r
720d3c5f 189 ASSERT (SpinLock != NULL);\r
190\r
191 LockValue = *SpinLock;\r
192 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r
193\r
194 _ReadWriteBarrier ();\r
195 Result = InterlockedCompareExchangePointer (\r
196 (VOID**)SpinLock,\r
197 (VOID*)SPIN_LOCK_RELEASED,\r
198 (VOID*)SPIN_LOCK_ACQUIRED\r
199 );\r
200\r
201 _ReadWriteBarrier ();\r
202 return (BOOLEAN) (Result == (VOID*) SPIN_LOCK_RELEASED);\r
203}\r
204\r
205/**\r
206 Releases a spin lock.\r
207\r
208 This function places the spin lock specified by SpinLock in the release state\r
209 and returns SpinLock.\r
210\r
211 If SpinLock is NULL, then ASSERT().\r
212 If SpinLock was not initialized with InitializeSpinLock(), then ASSERT().\r
213\r
214 @param SpinLock A pointer to the spin lock to release.\r
215\r
2fc59a00 216 @return SpinLock released the lock.\r
720d3c5f 217\r
218**/\r
219SPIN_LOCK *\r
220EFIAPI\r
221ReleaseSpinLock (\r
222 IN OUT SPIN_LOCK *SpinLock\r
223 )\r
224{\r
225 SPIN_LOCK LockValue;\r
226\r
227 ASSERT (SpinLock != NULL);\r
228\r
229 LockValue = *SpinLock;\r
230 ASSERT (LockValue == SPIN_LOCK_ACQUIRED || LockValue == SPIN_LOCK_RELEASED);\r
231\r
232 _ReadWriteBarrier ();\r
233 *SpinLock = SPIN_LOCK_RELEASED;\r
234 _ReadWriteBarrier ();\r
235\r
236 return SpinLock;\r
237}\r
238\r
239/**\r
240 Performs an atomic increment of an 32-bit unsigned integer.\r
241\r
242 Performs an atomic increment of the 32-bit unsigned integer specified by\r
243 Value and returns the incremented value. The increment operation must be\r
17634d02 244 performed using MP safe mechanisms.\r
720d3c5f 245\r
246 If Value is NULL, then ASSERT().\r
247\r
248 @param Value A pointer to the 32-bit value to increment.\r
249\r
250 @return The incremented value.\r
251\r
252**/\r
253UINT32\r
254EFIAPI\r
255InterlockedIncrement (\r
4cee954e 256 IN volatile UINT32 *Value\r
720d3c5f 257 )\r
258{\r
259 ASSERT (Value != NULL);\r
260 return InternalSyncIncrement (Value);\r
261}\r
262\r
263/**\r
264 Performs an atomic decrement of an 32-bit unsigned integer.\r
265\r
266 Performs an atomic decrement of the 32-bit unsigned integer specified by\r
267 Value and returns the decremented value. The decrement operation must be\r
17634d02 268 performed using MP safe mechanisms.\r
720d3c5f 269\r
270 If Value is NULL, then ASSERT().\r
271\r
272 @param Value A pointer to the 32-bit value to decrement.\r
273\r
274 @return The decremented value.\r
275\r
276**/\r
277UINT32\r
278EFIAPI\r
279InterlockedDecrement (\r
4cee954e 280 IN volatile UINT32 *Value\r
720d3c5f 281 )\r
282{\r
283 ASSERT (Value != NULL);\r
284 return InternalSyncDecrement (Value);\r
285}\r
286\r
9b89163e
AB
287/**\r
288 Performs an atomic compare exchange operation on a 16-bit unsigned integer.\r
289\r
290 Performs an atomic compare exchange operation on the 16-bit unsigned integer\r
291 specified by Value. If Value is equal to CompareValue, then Value is set to\r
292 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
293 then Value is returned. The compare exchange operation must be performed using\r
294 MP safe mechanisms.\r
295\r
296 If Value is NULL, then ASSERT().\r
297\r
298 @param Value A pointer to the 16-bit value for the compare exchange\r
299 operation.\r
300 @param CompareValue A 16-bit value used in compare operation.\r
301 @param ExchangeValue A 16-bit value used in exchange operation.\r
302\r
303 @return The original *Value before exchange.\r
304\r
305**/\r
306UINT16\r
307EFIAPI\r
308InterlockedCompareExchange16 (\r
4cee954e 309 IN OUT volatile UINT16 *Value,\r
9b89163e
AB
310 IN UINT16 CompareValue,\r
311 IN UINT16 ExchangeValue\r
312 )\r
313{\r
314 ASSERT (Value != NULL);\r
315 return InternalSyncCompareExchange16 (Value, CompareValue, ExchangeValue);\r
316}\r
317\r
720d3c5f 318/**\r
319 Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
320\r
321 Performs an atomic compare exchange operation on the 32-bit unsigned integer\r
322 specified by Value. If Value is equal to CompareValue, then Value is set to\r
323 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
324 then Value is returned. The compare exchange operation must be performed using\r
325 MP safe mechanisms.\r
326\r
327 If Value is NULL, then ASSERT().\r
328\r
329 @param Value A pointer to the 32-bit value for the compare exchange\r
330 operation.\r
2fc59a00 331 @param CompareValue A 32-bit value used in compare operation.\r
332 @param ExchangeValue A 32-bit value used in exchange operation.\r
720d3c5f 333\r
334 @return The original *Value before exchange.\r
335\r
336**/\r
337UINT32\r
338EFIAPI\r
339InterlockedCompareExchange32 (\r
4cee954e 340 IN OUT volatile UINT32 *Value,\r
720d3c5f 341 IN UINT32 CompareValue,\r
342 IN UINT32 ExchangeValue\r
343 )\r
344{\r
345 ASSERT (Value != NULL);\r
346 return InternalSyncCompareExchange32 (Value, CompareValue, ExchangeValue);\r
347}\r
348\r
349/**\r
350 Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r
351\r
352 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified\r
353 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and\r
354 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.\r
355 The compare exchange operation must be performed using MP safe mechanisms.\r
356\r
357 If Value is NULL, then ASSERT().\r
358\r
359 @param Value A pointer to the 64-bit value for the compare exchange\r
360 operation.\r
2fc59a00 361 @param CompareValue A 64-bit value used in a compare operation.\r
362 @param ExchangeValue A 64-bit value used in an exchange operation.\r
720d3c5f 363\r
364 @return The original *Value before exchange.\r
365\r
366**/\r
367UINT64\r
368EFIAPI\r
369InterlockedCompareExchange64 (\r
4cee954e 370 IN OUT volatile UINT64 *Value,\r
720d3c5f 371 IN UINT64 CompareValue,\r
372 IN UINT64 ExchangeValue\r
373 )\r
374{\r
375 ASSERT (Value != NULL);\r
376 return InternalSyncCompareExchange64 (Value, CompareValue, ExchangeValue);\r
377}\r
378\r
379/**\r
380 Performs an atomic compare exchange operation on a pointer value.\r
381\r
382 Performs an atomic compare exchange operation on the pointer value specified\r
383 by Value. If Value is equal to CompareValue, then Value is set to\r
384 ExchangeValue and CompareValue is returned. If Value is not equal to\r
385 CompareValue, then Value is returned. The compare exchange operation must be\r
386 performed using MP safe mechanisms.\r
387\r
388 If Value is NULL, then ASSERT().\r
389\r
390 @param Value A pointer to the pointer value for the compare exchange\r
391 operation.\r
2fc59a00 392 @param CompareValue A pointer value used in a compare operation.\r
393 @param ExchangeValue A pointer value used in an exchange operation.\r
720d3c5f 394\r
395 @return The original *Value before exchange.\r
396**/\r
397VOID *\r
398EFIAPI\r
399InterlockedCompareExchangePointer (\r
4cee954e 400 IN OUT VOID * volatile *Value,\r
720d3c5f 401 IN VOID *CompareValue,\r
402 IN VOID *ExchangeValue\r
403 )\r
404{\r
405 UINT8 SizeOfValue;\r
406\r
407 SizeOfValue = sizeof (*Value);\r
408\r
409 switch (SizeOfValue) {\r
410 case sizeof (UINT32):\r
411 return (VOID*)(UINTN)InterlockedCompareExchange32 (\r
4cee954e 412 (volatile UINT32 *)Value,\r
720d3c5f 413 (UINT32)(UINTN)CompareValue,\r
414 (UINT32)(UINTN)ExchangeValue\r
415 );\r
416 case sizeof (UINT64):\r
417 return (VOID*)(UINTN)InterlockedCompareExchange64 (\r
4cee954e 418 (volatile UINT64 *)Value,\r
720d3c5f 419 (UINT64)(UINTN)CompareValue,\r
420 (UINT64)(UINTN)ExchangeValue\r
421 );\r
422 default:\r
423 ASSERT (FALSE);\r
424 return NULL;\r
425 }\r
426}\r