VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asm/asm-fake.cpp@ 67979

Last change on this file since 67979 was 62477, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1/* $Id: asm-fake.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - Fake asm.h routines for use early in a new port.
4 */
5
6/*
7 * Copyright (C) 2010-2016 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
38RTDECL(uint8_t) ASMAtomicXchgU8(volatile uint8_t *pu8, uint8_t u8)
39{
40 uint8_t u8Ret = *pu8;
41 *pu8 = u8;
42 return u8Ret;
43}
44
45RTDECL(uint16_t) ASMAtomicXchgU16(volatile uint16_t *pu16, uint16_t u16)
46{
47 uint16_t u16Ret = *pu16;
48 *pu16 = u16;
49 return u16Ret;
50}
51
52RTDECL(uint32_t) ASMAtomicXchgU32(volatile uint32_t *pu32, uint32_t u32)
53{
54 uint32_t u32Ret = *pu32;
55 *pu32 = u32;
56 return u32Ret;
57}
58
59RTDECL(uint64_t) ASMAtomicXchgU64(volatile uint64_t *pu64, uint64_t u64)
60{
61 uint64_t u64Ret = *pu64;
62 *pu64 = u64;
63 return u64Ret;
64}
65
66RTDECL(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
76RTDECL(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
86RTDECL(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
96RTDECL(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
109RTDECL(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
122RTDECL(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
129RTDECL(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
136RTDECL(uint32_t) ASMAtomicIncU32(uint32_t volatile *pu32)
137{
138 return *pu32 += 1;
139}
140
141RTDECL(uint32_t) ASMAtomicDecU32(uint32_t volatile *pu32)
142{
143 return *pu32 -= 1;
144}
145
146RTDECL(uint64_t) ASMAtomicIncU64(uint64_t volatile *pu64)
147{
148 return *pu64 += 1;
149}
150
151RTDECL(uint64_t) ASMAtomicDecU64(uint64_t volatile *pu64)
152{
153 return *pu64 -= 1;
154}
155
156RTDECL(void) ASMAtomicOrU32(uint32_t volatile *pu32, uint32_t u32)
157{
158 *pu32 |= u32;
159}
160
161RTDECL(void) ASMAtomicAndU32(uint32_t volatile *pu32, uint32_t u32)
162{
163 *pu32 &= u32;
164}
165
166RTDECL(void) ASMAtomicOrU64(uint64_t volatile *pu64, uint64_t u64)
167{
168 *pu64 |= u64;
169}
170
171RTDECL(void) ASMAtomicAndU64(uint64_t volatile *pu64, uint64_t u64)
172{
173 *pu64 &= u64;
174}
175
176RTDECL(void) ASMSerializeInstruction(void)
177{
178
179}
180
181RTDECL(uint64_t) ASMAtomicReadU64(volatile uint64_t *pu64)
182{
183 return *pu64;
184}
185
186RTDECL(uint64_t) ASMAtomicUoReadU64(volatile uint64_t *pu64)
187{
188 return *pu64;
189}
190
191RTDECL(void) ASMMemZeroPage(volatile void *pv)
192{
193 uintptr_t volatile *puPtr = (uintptr_t volatile *)pv;
194 uint32_t cbLeft = PAGE_SIZE / sizeof(uintptr_t);
195 while (cbLeft-- > 0)
196 *puPtr++ = 0;
197}
198
199RTDECL(void) ASMMemZero32(volatile void *pv, size_t cb)
200{
201 uint32_t volatile *pu32 = (uint32_t volatile *)pv;
202 uint32_t cbLeft = cb / sizeof(uint32_t);
203 while (cbLeft-- > 0)
204 *pu32++ = 0;
205}
206
207RTDECL(void) ASMMemFill32(volatile void *pv, size_t cb, uint32_t u32)
208{
209 uint32_t volatile *pu32 = (uint32_t volatile *)pv;
210 while (cb > 0)
211 {
212 *pu32 = u32;
213 cb -= sizeof(uint32_t);
214 pu32++;
215 }
216}
217
218RTDECL(uint8_t) ASMProbeReadByte(const void *pvByte)
219{
220 return *(volatile uint8_t *)pvByte;
221}
222
223#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
224RTDECL(void) ASMNopPause(void)
225{
226}
227#endif
228
229RTDECL(void) ASMBitSet(volatile void *pvBitmap, int32_t iBit)
230{
231 uint8_t volatile *pau8Bitmap = (uint8_t volatile *)pvBitmap;
232 pau8Bitmap[iBit / 8] |= (uint8_t)RT_BIT_32(iBit & 7);
233}
234
235RTDECL(void) ASMAtomicBitSet(volatile void *pvBitmap, int32_t iBit)
236{
237 ASMBitSet(pvBitmap, iBit);
238}
239
240RTDECL(void) ASMBitClear(volatile void *pvBitmap, int32_t iBit)
241{
242 uint8_t volatile *pau8Bitmap = (uint8_t volatile *)pvBitmap;
243 pau8Bitmap[iBit / 8] &= ~((uint8_t)RT_BIT_32(iBit & 7));
244}
245
246RTDECL(void) ASMAtomicBitClear(volatile void *pvBitmap, int32_t iBit)
247{
248 ASMBitClear(pvBitmap, iBit);
249}
250
251RTDECL(void) ASMBitToggle(volatile void *pvBitmap, int32_t iBit)
252{
253 uint8_t volatile *pau8Bitmap = (uint8_t volatile *)pvBitmap;
254 pau8Bitmap[iBit / 8] ^= (uint8_t)RT_BIT_32(iBit & 7);
255}
256
257RTDECL(void) ASMAtomicBitToggle(volatile void *pvBitmap, int32_t iBit)
258{
259 ASMBitToggle(pvBitmap, iBit);
260}
261
262RTDECL(bool) ASMBitTestAndSet(volatile void *pvBitmap, int32_t iBit)
263{
264 if (ASMBitTest(pvBitmap, iBit))
265 return true;
266 ASMBitSet(pvBitmap, iBit);
267 return false;
268}
269
270RTDECL(bool) ASMAtomicBitTestAndSet(volatile void *pvBitmap, int32_t iBit)
271{
272 return ASMBitTestAndSet(pvBitmap, iBit);
273}
274
275RTDECL(bool) ASMBitTestAndClear(volatile void *pvBitmap, int32_t iBit)
276{
277 if (!ASMBitTest(pvBitmap, iBit))
278 return false;
279 ASMBitClear(pvBitmap, iBit);
280 return true;
281}
282
283RTDECL(bool) ASMAtomicBitTestAndClear(volatile void *pvBitmap, int32_t iBit)
284{
285 return ASMBitTestAndClear(pvBitmap, iBit);
286}
287
288RTDECL(bool) ASMBitTestAndToggle(volatile void *pvBitmap, int32_t iBit)
289{
290 bool fRet = ASMBitTest(pvBitmap, iBit);
291 ASMBitToggle(pvBitmap, iBit);
292 return fRet;
293}
294
295RTDECL(bool) ASMAtomicBitTestAndToggle(volatile void *pvBitmap, int32_t iBit)
296{
297 return ASMBitTestAndToggle(pvBitmap, iBit);
298}
299
300RTDECL(bool) ASMBitTest(const volatile void *pvBitmap, int32_t iBit)
301{
302 uint8_t volatile *pau8Bitmap = (uint8_t volatile *)pvBitmap;
303 return pau8Bitmap[iBit / 8] & (uint8_t)RT_BIT_32(iBit & 7) ? true : false;
304}
305
306RTDECL(int) ASMBitFirstClear(const volatile void *pvBitmap, uint32_t cBits)
307{
308 uint32_t iBit = 0;
309 uint8_t volatile *pu8 = (uint8_t volatile *)pvBitmap;
310
311 while (iBit < cBits)
312 {
313 uint8_t u8 = *pu8;
314 if (u8 != UINT8_MAX)
315 {
316 while (u8 & 1)
317 {
318 u8 >>= 1;
319 iBit++;
320 }
321 if (iBit >= cBits)
322 return -1;
323 return iBit;
324 }
325
326 iBit += 8;
327 pu8++;
328 }
329 return -1;
330}
331
332RTDECL(int) ASMBitNextClear(const volatile void *pvBitmap, uint32_t cBits, uint32_t iBitPrev)
333{
334 const volatile uint8_t *pau8Bitmap = (const volatile uint8_t *)pvBitmap;
335 int iBit = ++iBitPrev & 7;
336 if (iBit)
337 {
338 /*
339 * Inspect the byte containing the unaligned bit.
340 */
341 uint8_t u8 = ~pau8Bitmap[iBitPrev / 8] >> iBit;
342 if (u8)
343 {
344 iBit = 0;
345 while (!(u8 & 1))
346 {
347 u8 >>= 1;
348 iBit++;
349 }
350 return iBitPrev + iBit;
351 }
352
353 /*
354 * Skip ahead and see if there is anything left to search.
355 */
356 iBitPrev |= 7;
357 iBitPrev++;
358 if (cBits <= iBitPrev)
359 return -1;
360 }
361
362 /*
363 * Byte search, let ASMBitFirstClear do the dirty work.
364 */
365 iBit = ASMBitFirstClear(&pau8Bitmap[iBitPrev / 8], cBits - iBitPrev);
366 if (iBit >= 0)
367 iBit += iBitPrev;
368 return iBit;
369}
370
371RTDECL(int) ASMBitFirstSet(const volatile void *pvBitmap, uint32_t cBits)
372{
373 uint32_t iBit = 0;
374 uint8_t volatile *pu8 = (uint8_t volatile *)pvBitmap;
375 while (iBit < cBits)
376 {
377 uint8_t u8 = *pu8;
378 if (u8 != 0)
379 {
380 while (!(u8 & 1))
381 {
382 u8 >>= 1;
383 iBit++;
384 }
385 if (iBit >= cBits)
386 return -1;
387 return iBit;
388 }
389
390 iBit += 8;
391 pu8++;
392 }
393 return -1;
394}
395
396RTDECL(int) ASMBitNextSet(const volatile void *pvBitmap, uint32_t cBits, uint32_t iBitPrev)
397{
398 const volatile uint8_t *pau8Bitmap = (const volatile uint8_t *)pvBitmap;
399 int iBit = ++iBitPrev & 7;
400 if (iBit)
401 {
402 /*
403 * Inspect the byte containing the unaligned bit.
404 */
405 uint8_t u8 = pau8Bitmap[iBitPrev / 8] >> iBit;
406 if (u8)
407 {
408 iBit = 0;
409 while (!(u8 & 1))
410 {
411 u8 >>= 1;
412 iBit++;
413 }
414 return iBitPrev + iBit;
415 }
416
417 /*
418 * Skip ahead and see if there is anything left to search.
419 */
420 iBitPrev |= 7;
421 iBitPrev++;
422 if (cBits <= iBitPrev)
423 return -1;
424 }
425
426 /*
427 * Byte search, let ASMBitFirstSet do the dirty work.
428 */
429 iBit = ASMBitFirstSet(&pau8Bitmap[iBitPrev / 8], cBits - iBitPrev);
430 if (iBit >= 0)
431 iBit += iBitPrev;
432 return iBit;
433}
434
435RTDECL(unsigned) ASMBitFirstSetU32(uint32_t u32)
436{
437 uint32_t iBit;
438 for (iBit = 0; iBit < 32; iBit++)
439 if (u32 & RT_BIT_32(iBit))
440 return iBit + 1;
441 return 0;
442}
443
444RTDECL(unsigned) ASMBitLastSetU32(uint32_t u32)
445{
446 int32_t iBit = 32;
447 while (iBit-- > 0)
448 if (u32 & RT_BIT_32(iBit))
449 return iBit + 1;
450 return 0;
451}
452
453RTDECL(uint16_t) ASMByteSwapU16(uint16_t u16)
454{
455 return RT_MAKE_U16(RT_HIBYTE(u16), RT_LOBYTE(u16));
456}
457
458RTDECL(uint32_t) ASMByteSwapU32(uint32_t u32)
459{
460 return RT_MAKE_U32_FROM_U8(RT_BYTE4(u32), RT_BYTE3(u32), RT_BYTE2(u32), RT_BYTE1(u32));
461}
462
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette