]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/BaseSynchronizationLib/Ia32/GccInline.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / BaseSynchronizationLib / Ia32 / GccInline.c
... / ...
CommitLineData
1/** @file\r
2 GCC inline implementation of BaseSynchronizationLib processor specific functions.\r
3\r
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
5 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
7\r
8**/\r
9\r
10\r
11\r
12/**\r
13 Performs an atomic increment of an 32-bit unsigned integer.\r
14\r
15 Performs an atomic increment of the 32-bit unsigned integer specified by\r
16 Value and returns the incremented value. The increment operation must be\r
17 performed using MP safe mechanisms.\r
18\r
19 @param Value A pointer to the 32-bit value to increment.\r
20\r
21 @return The incremented value.\r
22\r
23**/\r
24UINT32\r
25EFIAPI\r
26InternalSyncIncrement (\r
27 IN volatile UINT32 *Value\r
28 )\r
29{\r
30 UINT32 Result;\r
31\r
32 __asm__ __volatile__ (\r
33 "movl $1, %%eax \n\t"\r
34 "lock \n\t"\r
35 "xadd %%eax, %1 \n\t"\r
36 "inc %%eax \n\t"\r
37 : "=&a" (Result), // %0\r
38 "+m" (*Value) // %1\r
39 : // no inputs that aren't also outputs\r
40 : "memory",\r
41 "cc"\r
42 );\r
43\r
44 return Result;\r
45}\r
46\r
47\r
48/**\r
49 Performs an atomic decrement of an 32-bit unsigned integer.\r
50\r
51 Performs an atomic decrement of the 32-bit unsigned integer specified by\r
52 Value and returns the decremented value. The decrement operation must be\r
53 performed using MP safe mechanisms.\r
54\r
55 @param Value A pointer to the 32-bit value to decrement.\r
56\r
57 @return The decremented value.\r
58\r
59**/\r
60UINT32\r
61EFIAPI\r
62InternalSyncDecrement (\r
63 IN volatile UINT32 *Value\r
64 )\r
65{\r
66 UINT32 Result;\r
67\r
68 __asm__ __volatile__ (\r
69 "movl $-1, %%eax \n\t"\r
70 "lock \n\t"\r
71 "xadd %%eax, %1 \n\t"\r
72 "dec %%eax \n\t"\r
73 : "=&a" (Result), // %0\r
74 "+m" (*Value) // %1\r
75 : // no inputs that aren't also outputs\r
76 : "memory",\r
77 "cc"\r
78 );\r
79\r
80 return Result;\r
81}\r
82\r
83\r
84/**\r
85 Performs an atomic compare exchange operation on a 16-bit unsigned integer.\r
86\r
87 Performs an atomic compare exchange operation on the 16-bit unsigned integer\r
88 specified by Value. If Value is equal to CompareValue, then Value is set to\r
89 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
90 then Value is returned. The compare exchange operation must be performed using\r
91 MP safe mechanisms.\r
92\r
93\r
94 @param Value A pointer to the 16-bit value for the compare exchange\r
95 operation.\r
96 @param CompareValue 16-bit value used in compare operation.\r
97 @param ExchangeValue 16-bit value used in exchange operation.\r
98\r
99 @return The original *Value before exchange.\r
100\r
101**/\r
102UINT16\r
103EFIAPI\r
104InternalSyncCompareExchange16 (\r
105 IN OUT volatile UINT16 *Value,\r
106 IN UINT16 CompareValue,\r
107 IN UINT16 ExchangeValue\r
108 )\r
109{\r
110 __asm__ __volatile__ (\r
111 "lock \n\t"\r
112 "cmpxchgw %2, %1 \n\t"\r
113 : "+a" (CompareValue), // %0\r
114 "+m" (*Value) // %1\r
115 : "q" (ExchangeValue) // %2\r
116 : "memory",\r
117 "cc"\r
118 );\r
119\r
120 return CompareValue;\r
121}\r
122\r
123\r
124/**\r
125 Performs an atomic compare exchange operation on a 32-bit unsigned integer.\r
126\r
127 Performs an atomic compare exchange operation on the 32-bit unsigned integer\r
128 specified by Value. If Value is equal to CompareValue, then Value is set to\r
129 ExchangeValue and CompareValue is returned. If Value is not equal to CompareValue,\r
130 then Value is returned. The compare exchange operation must be performed using\r
131 MP safe mechanisms.\r
132\r
133\r
134 @param Value A pointer to the 32-bit value for the compare exchange\r
135 operation.\r
136 @param CompareValue 32-bit value used in compare operation.\r
137 @param ExchangeValue 32-bit value used in exchange operation.\r
138\r
139 @return The original *Value before exchange.\r
140\r
141**/\r
142UINT32\r
143EFIAPI\r
144InternalSyncCompareExchange32 (\r
145 IN OUT volatile UINT32 *Value,\r
146 IN UINT32 CompareValue,\r
147 IN UINT32 ExchangeValue\r
148 )\r
149{\r
150 __asm__ __volatile__ (\r
151 "lock \n\t"\r
152 "cmpxchgl %2, %1 \n\t"\r
153 : "+a" (CompareValue), // %0\r
154 "+m" (*Value) // %1\r
155 : "q" (ExchangeValue) // %2\r
156 : "memory",\r
157 "cc"\r
158 );\r
159\r
160 return CompareValue;\r
161}\r
162\r
163\r
164/**\r
165 Performs an atomic compare exchange operation on a 64-bit unsigned integer.\r
166\r
167 Performs an atomic compare exchange operation on the 64-bit unsigned integer specified\r
168 by Value. If Value is equal to CompareValue, then Value is set to ExchangeValue and\r
169 CompareValue is returned. If Value is not equal to CompareValue, then Value is returned.\r
170 The compare exchange operation must be performed using MP safe mechanisms.\r
171\r
172\r
173 @param Value A pointer to the 64-bit value for the compare exchange\r
174 operation.\r
175 @param CompareValue 64-bit value used in compare operation.\r
176 @param ExchangeValue 64-bit value used in exchange operation.\r
177\r
178 @return The original *Value before exchange.\r
179\r
180**/\r
181UINT64\r
182EFIAPI\r
183InternalSyncCompareExchange64 (\r
184 IN OUT volatile UINT64 *Value,\r
185 IN UINT64 CompareValue,\r
186 IN UINT64 ExchangeValue\r
187 )\r
188{\r
189 __asm__ __volatile__ (\r
190 "lock \n\t"\r
191 "cmpxchg8b (%1) \n\t"\r
192 : "+A" (CompareValue) // %0\r
193 : "S" (Value), // %1\r
194 "b" ((UINT32) ExchangeValue), // %2\r
195 "c" ((UINT32) (ExchangeValue >> 32)) // %3\r
196 : "memory",\r
197 "cc"\r
198 );\r
199\r
200 return CompareValue;\r
201}\r