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