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