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