1 | /* $Id: asm-fake.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Fake asm.h routines for use early in a new port.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/asm.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/string.h>
|
---|
35 | #include <iprt/param.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | RTDECL(uint8_t) ASMAtomicXchgU8(volatile uint8_t *pu8, uint8_t u8)
|
---|
39 | {
|
---|
40 | uint8_t u8Ret = *pu8;
|
---|
41 | *pu8 = u8;
|
---|
42 | return u8Ret;
|
---|
43 | }
|
---|
44 |
|
---|
45 | RTDECL(uint16_t) ASMAtomicXchgU16(volatile uint16_t *pu16, uint16_t u16)
|
---|
46 | {
|
---|
47 | uint16_t u16Ret = *pu16;
|
---|
48 | *pu16 = u16;
|
---|
49 | return u16Ret;
|
---|
50 | }
|
---|
51 |
|
---|
52 | RTDECL(uint32_t) ASMAtomicXchgU32(volatile uint32_t *pu32, uint32_t u32)
|
---|
53 | {
|
---|
54 | uint32_t u32Ret = *pu32;
|
---|
55 | *pu32 = u32;
|
---|
56 | return u32Ret;
|
---|
57 | }
|
---|
58 |
|
---|
59 | RTDECL(uint64_t) ASMAtomicXchgU64(volatile uint64_t *pu64, uint64_t u64)
|
---|
60 | {
|
---|
61 | uint64_t u64Ret = *pu64;
|
---|
62 | *pu64 = u64;
|
---|
63 | return u64Ret;
|
---|
64 | }
|
---|
65 |
|
---|
66 | RTDECL(bool) ASMAtomicCmpXchgU8(volatile uint8_t *pu8, const uint8_t u8New, const uint8_t u8Old)
|
---|
67 | {
|
---|
68 | if (*pu8 == u8Old)
|
---|
69 | {
|
---|
70 | *pu8 = u8New;
|
---|
71 | return true;
|
---|
72 | }
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 |
|
---|
76 | RTDECL(bool) ASMAtomicCmpXchgU32(volatile uint32_t *pu32, const uint32_t u32New, const uint32_t u32Old)
|
---|
77 | {
|
---|
78 | if (*pu32 == u32Old)
|
---|
79 | {
|
---|
80 | *pu32 = u32New;
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 |
|
---|
86 | RTDECL(bool) ASMAtomicCmpXchgU64(volatile uint64_t *pu64, const uint64_t u64New, const uint64_t u64Old)
|
---|
87 | {
|
---|
88 | if (*pu64 == u64Old)
|
---|
89 | {
|
---|
90 | *pu64 = u64New;
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | RTDECL(bool) ASMAtomicCmpXchgExU32(volatile uint32_t *pu32, const uint32_t u32New, const uint32_t u32Old, uint32_t *pu32Old)
|
---|
97 | {
|
---|
98 | uint32_t u32Cur = *pu32;
|
---|
99 | if (u32Cur == u32Old)
|
---|
100 | {
|
---|
101 | *pu32 = u32New;
|
---|
102 | *pu32Old = u32Old;
|
---|
103 | return true;
|
---|
104 | }
|
---|
105 | *pu32Old = u32Cur;
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 |
|
---|
109 | RTDECL(bool) ASMAtomicCmpXchgExU64(volatile uint64_t *pu64, const uint64_t u64New, const uint64_t u64Old, uint64_t *pu64Old)
|
---|
110 | {
|
---|
111 | uint64_t u64Cur = *pu64;
|
---|
112 | if (u64Cur == u64Old)
|
---|
113 | {
|
---|
114 | *pu64 = u64New;
|
---|
115 | *pu64Old = u64Old;
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 | *pu64Old = u64Cur;
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | RTDECL(uint32_t) ASMAtomicAddU32(uint32_t volatile *pu32, uint32_t u32)
|
---|
123 | {
|
---|
124 | uint32_t u32Old = *pu32;
|
---|
125 | *pu32 = u32Old + u32;
|
---|
126 | return u32Old;
|
---|
127 | }
|
---|
128 |
|
---|
129 | RTDECL(uint64_t) ASMAtomicAddU64(uint64_t volatile *pu64, uint64_t u64)
|
---|
130 | {
|
---|
131 | uint64_t u64Old = *pu64;
|
---|
132 | *pu64 = u64Old + u64;
|
---|
133 | return u64Old;
|
---|
134 | }
|
---|
135 |
|
---|
136 | RTDECL(uint32_t) ASMAtomicIncU32(uint32_t volatile *pu32)
|
---|
137 | {
|
---|
138 | return *pu32 += 1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | RTDECL(uint32_t) ASMAtomicUoIncU32(uint32_t volatile *pu32)
|
---|
142 | {
|
---|
143 | return *pu32 += 1;
|
---|
144 | }
|
---|
145 |
|
---|
146 | RTDECL(uint32_t) ASMAtomicDecU32(uint32_t volatile *pu32)
|
---|
147 | {
|
---|
148 | return *pu32 -= 1;
|
---|
149 | }
|
---|
150 |
|
---|
151 | RTDECL(uint32_t) ASMAtomicUoDecU32(uint32_t volatile *pu32)
|
---|
152 | {
|
---|
153 | return *pu32 -= 1;
|
---|
154 | }
|
---|
155 |
|
---|
156 | RTDECL(uint64_t) ASMAtomicIncU64(uint64_t volatile *pu64)
|
---|
157 | {
|
---|
158 | return *pu64 += 1;
|
---|
159 | }
|
---|
160 |
|
---|
161 | RTDECL(uint64_t) ASMAtomicDecU64(uint64_t volatile *pu64)
|
---|
162 | {
|
---|
163 | return *pu64 -= 1;
|
---|
164 | }
|
---|
165 |
|
---|
166 | RTDECL(void) ASMAtomicOrU32(uint32_t volatile *pu32, uint32_t u32)
|
---|
167 | {
|
---|
168 | *pu32 |= u32;
|
---|
169 | }
|
---|
170 |
|
---|
171 | RTDECL(void) ASMAtomicUoOrU32(uint32_t volatile *pu32, uint32_t u32)
|
---|
172 | {
|
---|
173 | *pu32 |= u32;
|
---|
174 | }
|
---|
175 |
|
---|
176 | RTDECL(void) ASMAtomicAndU32(uint32_t volatile *pu32, uint32_t u32)
|
---|
177 | {
|
---|
178 | *pu32 &= u32;
|
---|
179 | }
|
---|
180 |
|
---|
181 | RTDECL(void) ASMAtomicUoAndU32(uint32_t volatile *pu32, uint32_t u32)
|
---|
182 | {
|
---|
183 | *pu32 &= u32;
|
---|
184 | }
|
---|
185 |
|
---|
186 | RTDECL(void) ASMAtomicOrU64(uint64_t volatile *pu64, uint64_t u64)
|
---|
187 | {
|
---|
188 | *pu64 |= u64;
|
---|
189 | }
|
---|
190 |
|
---|
191 | RTDECL(void) ASMAtomicAndU64(uint64_t volatile *pu64, uint64_t u64)
|
---|
192 | {
|
---|
193 | *pu64 &= u64;
|
---|
194 | }
|
---|
195 |
|
---|
196 | RTDECL(void) ASMSerializeInstruction(void)
|
---|
197 | {
|
---|
198 |
|
---|
199 | }
|
---|
200 |
|
---|
201 | RTDECL(uint64_t) ASMAtomicReadU64(volatile uint64_t *pu64)
|
---|
202 | {
|
---|
203 | return *pu64;
|
---|
204 | }
|
---|
205 |
|
---|
206 | RTDECL(uint64_t) ASMAtomicUoReadU64(volatile uint64_t *pu64)
|
---|
207 | {
|
---|
208 | return *pu64;
|
---|
209 | }
|
---|
210 |
|
---|
211 | RTDECL(uint8_t) ASMProbeReadByte(const void *pvByte)
|
---|
212 | {
|
---|
213 | return *(volatile uint8_t *)pvByte;
|
---|
214 | }
|
---|
215 |
|
---|
216 | #if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
217 | RTDECL(void) ASMNopPause(void)
|
---|
218 | {
|
---|
219 | }
|
---|
220 | #endif
|
---|
221 |
|
---|
222 | RTDECL(void) ASMBitSet(volatile void *pvBitmap, int32_t iBit)
|
---|
223 | {
|
---|
224 | uint8_t volatile *pau8Bitmap = (uint8_t volatile *)pvBitmap;
|
---|
225 | pau8Bitmap[iBit / 8] |= (uint8_t)RT_BIT_32(iBit & 7);
|
---|
226 | }
|
---|
227 |
|
---|
228 | RTDECL(void) ASMAtomicBitSet(volatile void *pvBitmap, int32_t iBit)
|
---|
229 | {
|
---|
230 | ASMBitSet(pvBitmap, iBit);
|
---|
231 | }
|
---|
232 |
|
---|
233 | RTDECL(void) ASMBitClear(volatile void *pvBitmap, int32_t iBit)
|
---|
234 | {
|
---|
235 | uint8_t volatile *pau8Bitmap = (uint8_t volatile *)pvBitmap;
|
---|
236 | pau8Bitmap[iBit / 8] &= ~((uint8_t)RT_BIT_32(iBit & 7));
|
---|
237 | }
|
---|
238 |
|
---|
239 | RTDECL(void) ASMAtomicBitClear(volatile void *pvBitmap, int32_t iBit)
|
---|
240 | {
|
---|
241 | ASMBitClear(pvBitmap, iBit);
|
---|
242 | }
|
---|
243 |
|
---|
244 | RTDECL(void) ASMBitToggle(volatile void *pvBitmap, int32_t iBit)
|
---|
245 | {
|
---|
246 | uint8_t volatile *pau8Bitmap = (uint8_t volatile *)pvBitmap;
|
---|
247 | pau8Bitmap[iBit / 8] ^= (uint8_t)RT_BIT_32(iBit & 7);
|
---|
248 | }
|
---|
249 |
|
---|
250 | RTDECL(void) ASMAtomicBitToggle(volatile void *pvBitmap, int32_t iBit)
|
---|
251 | {
|
---|
252 | ASMBitToggle(pvBitmap, iBit);
|
---|
253 | }
|
---|
254 |
|
---|
255 | RTDECL(bool) ASMBitTestAndSet(volatile void *pvBitmap, int32_t iBit)
|
---|
256 | {
|
---|
257 | if (ASMBitTest(pvBitmap, iBit))
|
---|
258 | return true;
|
---|
259 | ASMBitSet(pvBitmap, iBit);
|
---|
260 | return false;
|
---|
261 | }
|
---|
262 |
|
---|
263 | RTDECL(bool) ASMAtomicBitTestAndSet(volatile void *pvBitmap, int32_t iBit)
|
---|
264 | {
|
---|
265 | return ASMBitTestAndSet(pvBitmap, iBit);
|
---|
266 | }
|
---|
267 |
|
---|
268 | RTDECL(bool) ASMBitTestAndClear(volatile void *pvBitmap, int32_t iBit)
|
---|
269 | {
|
---|
270 | if (!ASMBitTest(pvBitmap, iBit))
|
---|
271 | return false;
|
---|
272 | ASMBitClear(pvBitmap, iBit);
|
---|
273 | return true;
|
---|
274 | }
|
---|
275 |
|
---|
276 | RTDECL(bool) ASMAtomicBitTestAndClear(volatile void *pvBitmap, int32_t iBit)
|
---|
277 | {
|
---|
278 | return ASMBitTestAndClear(pvBitmap, iBit);
|
---|
279 | }
|
---|
280 |
|
---|
281 | RTDECL(bool) ASMBitTestAndToggle(volatile void *pvBitmap, int32_t iBit)
|
---|
282 | {
|
---|
283 | bool fRet = ASMBitTest(pvBitmap, iBit);
|
---|
284 | ASMBitToggle(pvBitmap, iBit);
|
---|
285 | return fRet;
|
---|
286 | }
|
---|
287 |
|
---|
288 | RTDECL(bool) ASMAtomicBitTestAndToggle(volatile void *pvBitmap, int32_t iBit)
|
---|
289 | {
|
---|
290 | return ASMBitTestAndToggle(pvBitmap, iBit);
|
---|
291 | }
|
---|
292 |
|
---|
293 | RTDECL(bool) ASMBitTest(const volatile void *pvBitmap, int32_t iBit)
|
---|
294 | {
|
---|
295 | uint8_t volatile *pau8Bitmap = (uint8_t volatile *)pvBitmap;
|
---|
296 | return pau8Bitmap[iBit / 8] & (uint8_t)RT_BIT_32(iBit & 7) ? true : false;
|
---|
297 | }
|
---|
298 |
|
---|
299 | RTDECL(unsigned) ASMBitFirstSetU32(uint32_t u32)
|
---|
300 | {
|
---|
301 | uint32_t iBit;
|
---|
302 | for (iBit = 0; iBit < 32; iBit++)
|
---|
303 | if (u32 & RT_BIT_32(iBit))
|
---|
304 | return iBit + 1;
|
---|
305 | return 0;
|
---|
306 | }
|
---|
307 |
|
---|
308 | RTDECL(unsigned) ASMBitLastSetU32(uint32_t u32)
|
---|
309 | {
|
---|
310 | uint32_t iBit = 32;
|
---|
311 | while (iBit-- > 0)
|
---|
312 | if (u32 & RT_BIT_32(iBit))
|
---|
313 | return iBit + 1;
|
---|
314 | return 0;
|
---|
315 | }
|
---|
316 |
|
---|
317 | RTDECL(unsigned) ASMBitFirstSetU64(uint64_t u64)
|
---|
318 | {
|
---|
319 | uint32_t iBit;
|
---|
320 | for (iBit = 0; iBit < 64; iBit++)
|
---|
321 | if (u64 & RT_BIT_64(iBit))
|
---|
322 | return iBit + 1;
|
---|
323 | return 0;
|
---|
324 | }
|
---|
325 |
|
---|
326 | RTDECL(unsigned) ASMBitLastSetU64(uint64_t u64)
|
---|
327 | {
|
---|
328 | uint32_t iBit = 64;
|
---|
329 | while (iBit-- > 0)
|
---|
330 | if (u64 & RT_BIT_64(iBit))
|
---|
331 | return iBit + 1;
|
---|
332 | return 0;
|
---|
333 | }
|
---|
334 |
|
---|
335 | RTDECL(uint16_t) ASMByteSwapU16(uint16_t u16)
|
---|
336 | {
|
---|
337 | return RT_MAKE_U16(RT_HIBYTE(u16), RT_LOBYTE(u16));
|
---|
338 | }
|
---|
339 |
|
---|
340 | RTDECL(uint32_t) ASMByteSwapU32(uint32_t u32)
|
---|
341 | {
|
---|
342 | return RT_MAKE_U32_FROM_U8(RT_BYTE4(u32), RT_BYTE3(u32), RT_BYTE2(u32), RT_BYTE1(u32));
|
---|
343 | }
|
---|
344 |
|
---|