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