VirtualBox

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

Last change on this file since 42599 was 40959, checked in by vboxsync, 13 years ago

Runtime/asm-fake: Update to make it work on SPARC

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: asm-fake.cpp 40959 2012-04-17 10:45:00Z vboxsync $ */
2/** @file
3 * IPRT - Fake asm.h routines for use early in a new port.
4 */
5
6/*
7 * Copyright (C) 2010 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 uint32_t volatile *pau32Bitmap = (uint32_t volatile *)pvBitmap;
232 pau32Bitmap[iBit / 32] |= RT_BIT_32(iBit & 31);
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 uint32_t volatile *pau32Bitmap = (uint32_t volatile *)pvBitmap;
243 pau32Bitmap[iBit / 32] &= ~RT_BIT_32(iBit & 31);
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 uint32_t volatile *pau32Bitmap = (uint32_t volatile *)pvBitmap;
254 pau32Bitmap[iBit / 32] ^= RT_BIT_32(iBit & 31);
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 uint32_t volatile *pau32Bitmap = (uint32_t volatile *)pvBitmap;
303 return pau32Bitmap[iBit / 32] & RT_BIT_32(iBit & 31) ? true : false;
304}
305
306RTDECL(int) ASMBitFirstClear(const volatile void *pvBitmap, uint32_t cBits)
307{
308 uint32_t iBit = 0;
309 uint32_t volatile *pu32 = (uint32_t volatile *)pvBitmap;
310 while (iBit < cBits)
311 {
312 uint32_t u32 = *pu32;
313 if (u32 != UINT32_MAX)
314 {
315 while (u32 & 1)
316 {
317 u32 >>= 1;
318 iBit++;
319 }
320 if (iBit >= cBits)
321 return -1;
322 return iBit;
323 }
324
325 iBit += 32;
326 pu32++;
327 }
328 return -1;
329}
330
331RTDECL(int) ASMBitNextClear(const volatile void *pvBitmap, uint32_t cBits, uint32_t iBitPrev)
332{
333 const volatile uint32_t *pau32Bitmap = (const volatile uint32_t *)pvBitmap;
334 int iBit = ++iBitPrev & 31;
335 if (iBit)
336 {
337 /*
338 * Inspect the 32-bit word containing the unaligned bit.
339 */
340 uint32_t u32 = ~pau32Bitmap[iBitPrev / 32] >> iBit;
341 if (u32)
342 {
343 iBit = 0;
344 while (!(u32 & 1))
345 {
346 u32 >>= 1;
347 iBit++;
348 }
349 return iBitPrev + iBit;
350 }
351
352 /*
353 * Skip ahead and see if there is anything left to search.
354 */
355 iBitPrev |= 31;
356 iBitPrev++;
357 if (cBits <= (uint32_t)iBitPrev)
358 return -1;
359 }
360
361 /*
362 * 32-bit aligned search, let ASMBitFirstClear do the dirty work.
363 */
364 iBit = ASMBitFirstClear(&pau32Bitmap[iBitPrev / 32], cBits - iBitPrev);
365 if (iBit >= 0)
366 iBit += iBitPrev;
367 return iBit;
368}
369
370RTDECL(int) ASMBitFirstSet(const volatile void *pvBitmap, uint32_t cBits)
371{
372 uint32_t iBit = 0;
373 uint32_t volatile *pu32 = (uint32_t volatile *)pvBitmap;
374 while (iBit < cBits)
375 {
376 uint32_t u32 = *pu32;
377 if (u32 != 0)
378 {
379 while (!(u32 & 1))
380 {
381 u32 >>= 1;
382 iBit++;
383 }
384 if (iBit >= cBits)
385 return -1;
386 return iBit;
387 }
388
389 iBit += 32;
390 pu32++;
391 }
392 return -1;
393}
394
395RTDECL(int) ASMBitNextSet(const volatile void *pvBitmap, uint32_t cBits, uint32_t iBitPrev)
396{
397 const volatile uint32_t *pau32Bitmap = (const volatile uint32_t *)pvBitmap;
398 int iBit = ++iBitPrev & 31;
399 if (iBit)
400 {
401 /*
402 * Inspect the 32-bit word containing the unaligned bit.
403 */
404 uint32_t u32 = pau32Bitmap[iBitPrev / 32] >> iBit;
405 if (u32)
406 {
407 iBit = 0;
408 while (!(u32 & 1))
409 {
410 u32 >>= 1;
411 iBit++;
412 }
413 return iBitPrev + iBit;
414 }
415
416 /*
417 * Skip ahead and see if there is anything left to search.
418 */
419 iBitPrev |= 31;
420 iBitPrev++;
421 if (cBits <= (uint32_t)iBitPrev)
422 return -1;
423 }
424
425 /*
426 * 32-bit aligned search, let ASMBitFirstSet do the dirty work.
427 */
428 iBit = ASMBitFirstSet(&pau32Bitmap[iBitPrev / 32], cBits - iBitPrev);
429 if (iBit >= 0)
430 iBit += iBitPrev;
431 return iBit;
432}
433
434RTDECL(unsigned) ASMBitFirstSetU32(uint32_t u32)
435{
436 uint32_t iBit;
437 for (iBit = 0; iBit < 32; iBit++)
438 if (u32 & RT_BIT_32(iBit))
439 return iBit + 1;
440 return 0;
441}
442
443RTDECL(unsigned) ASMBitLastSetU32(uint32_t u32)
444{
445 int32_t iBit = 32;
446 while (iBit-- > 0)
447 if (u32 & RT_BIT_32(iBit))
448 return iBit + 1;
449 return 0;
450}
451
452RTDECL(uint16_t) ASMByteSwapU16(uint16_t u16)
453{
454 return RT_MAKE_U16(RT_HIBYTE(u16), RT_LOBYTE(u16));
455}
456
457RTDECL(uint32_t) ASMByteSwapU32(uint32_t u32)
458{
459 return RT_MAKE_U32_FROM_U8(RT_BYTE4(u32), RT_BYTE3(u32), RT_BYTE2(u32), RT_BYTE1(u32));
460}
461
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