VirtualBox

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

Last change on this file since 31979 was 29278, checked in by vboxsync, 15 years ago

Runtime/asm-fake.cpp: Non-atomic implementation of the DECLASM bits in iprt/asm.h. Handy for putting off learning the assembly language of a new architecture IPRT is being ported to.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
Line 
1/* $Id: asm-fake.cpp 29278 2010-05-09 23:28:27Z 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(uint32_t) ASMAtomicIncU32(uint32_t volatile *pu32)
130{
131 return *pu32 += 1;
132}
133
134RTDECL(uint32_t) ASMAtomicDecU32(uint32_t volatile *pu32)
135{
136 return *pu32 -= 1;
137}
138
139RTDECL(void) ASMAtomicOrU32(uint32_t volatile *pu32, uint32_t u32)
140{
141 *pu32 |= u32;
142}
143
144RTDECL(void) ASMAtomicAndU32(uint32_t volatile *pu32, uint32_t u32)
145{
146 *pu32 &= u32;
147}
148
149RTDECL(void) ASMSerializeInstruction(void)
150{
151
152}
153
154RTDECL(uint64_t) ASMAtomicReadU64(volatile uint64_t *pu64)
155{
156 return *pu64;
157}
158
159RTDECL(uint64_t) ASMAtomicUoReadU64(volatile uint64_t *pu64)
160{
161 return *pu64;
162}
163
164RTDECL(void) ASMMemZeroPage(volatile void *pv)
165{
166 uintptr_t volatile *puPtr = (uintptr_t volatile *)pv;
167 uint32_t cbLeft = PAGE_SIZE / sizeof(uintptr_t);
168 while (cbLeft-- > 0)
169 *puPtr++ = 0;
170}
171
172RTDECL(void) ASMMemZero32(volatile void *pv, size_t cb)
173{
174 uint32_t volatile *pu32 = (uint32_t volatile *)pv;
175 uint32_t cbLeft = cb / sizeof(uint32_t);
176 while (cbLeft-- > 0)
177 *pu32++ = 0;
178}
179
180RTDECL(void) ASMMemFill32(volatile void *pv, size_t cb, uint32_t u32)
181{
182 uint32_t volatile *pu32 = (uint32_t volatile *)pv;
183 while (cb > 0)
184 {
185 *pu32 = u32;
186 cb -= sizeof(uint32_t);
187 pu32++;
188 }
189}
190
191RTDECL(uint8_t) ASMProbeReadByte(const void *pvByte)
192{
193 return *(volatile uint8_t *)pvByte;
194}
195
196#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
197RTDECL(void) ASMNopPause(void)
198{
199}
200#endif
201
202RTDECL(void) ASMBitSet(volatile void *pvBitmap, int32_t iBit)
203{
204 uint32_t volatile *pau32Bitmap = (uint32_t volatile *)pvBitmap;
205 pau32Bitmap[iBit / 32] |= RT_BIT_32(iBit & 31);
206}
207
208RTDECL(void) ASMAtomicBitSet(volatile void *pvBitmap, int32_t iBit)
209{
210 ASMBitSet(pvBitmap, iBit);
211}
212
213RTDECL(void) ASMBitClear(volatile void *pvBitmap, int32_t iBit)
214{
215 uint32_t volatile *pau32Bitmap = (uint32_t volatile *)pvBitmap;
216 pau32Bitmap[iBit / 32] &= ~RT_BIT_32(iBit & 31);
217}
218
219RTDECL(void) ASMAtomicBitClear(volatile void *pvBitmap, int32_t iBit)
220{
221 ASMBitClear(pvBitmap, iBit);
222}
223
224RTDECL(void) ASMBitToggle(volatile void *pvBitmap, int32_t iBit)
225{
226 uint32_t volatile *pau32Bitmap = (uint32_t volatile *)pvBitmap;
227 pau32Bitmap[iBit / 32] ^= RT_BIT_32(iBit & 31);
228}
229
230RTDECL(void) ASMAtomicBitToggle(volatile void *pvBitmap, int32_t iBit)
231{
232 ASMBitToggle(pvBitmap, iBit);
233}
234
235RTDECL(bool) ASMBitTestAndSet(volatile void *pvBitmap, int32_t iBit)
236{
237 if (ASMBitTest(pvBitmap, iBit))
238 return true;
239 ASMBitSet(pvBitmap, iBit);
240 return false;
241}
242
243RTDECL(bool) ASMAtomicBitTestAndSet(volatile void *pvBitmap, int32_t iBit)
244{
245 return ASMBitTestAndSet(pvBitmap, iBit);
246}
247
248RTDECL(bool) ASMBitTestAndClear(volatile void *pvBitmap, int32_t iBit)
249{
250 if (!ASMBitTest(pvBitmap, iBit))
251 return false;
252 ASMBitClear(pvBitmap, iBit);
253 return true;
254}
255
256RTDECL(bool) ASMAtomicBitTestAndClear(volatile void *pvBitmap, int32_t iBit)
257{
258 return ASMBitTestAndClear(pvBitmap, iBit);
259}
260
261RTDECL(bool) ASMBitTestAndToggle(volatile void *pvBitmap, int32_t iBit)
262{
263 bool fRet = ASMBitTest(pvBitmap, iBit);
264 ASMBitToggle(pvBitmap, iBit);
265 return fRet;
266}
267
268RTDECL(bool) ASMAtomicBitTestAndToggle(volatile void *pvBitmap, int32_t iBit)
269{
270 return ASMBitTestAndToggle(pvBitmap, iBit);
271}
272
273RTDECL(bool) ASMBitTest(const volatile void *pvBitmap, int32_t iBit)
274{
275 uint32_t volatile *pau32Bitmap = (uint32_t volatile *)pvBitmap;
276 return pau32Bitmap[iBit / 32] & RT_BIT_32(iBit & 31) ? true : false;
277}
278
279RTDECL(int) ASMBitFirstClear(const volatile void *pvBitmap, uint32_t cBits)
280{
281 uint32_t iBit = 0;
282 uint32_t volatile *pu32 = (uint32_t volatile *)pvBitmap;
283 while (iBit < cBits)
284 {
285 uint32_t u32 = *pu32;
286 if (u32 != UINT32_MAX)
287 {
288 while (u32 & 1)
289 {
290 u32 >>= 1;
291 iBit++;
292 }
293 if (iBit >= cBits)
294 return -1;
295 return iBit;
296 }
297
298 iBit += 32;
299 pu32++;
300 }
301 return -1;
302}
303
304RTDECL(int) ASMBitNextClear(const volatile void *pvBitmap, uint32_t cBits, uint32_t iBitPrev)
305{
306 const volatile uint32_t *pau32Bitmap = (const volatile uint32_t *)pvBitmap;
307 int iBit = ++iBitPrev & 31;
308 if (iBit)
309 {
310 /*
311 * Inspect the 32-bit word containing the unaligned bit.
312 */
313 uint32_t u32 = ~pau32Bitmap[iBitPrev / 32] >> iBit;
314 if (u32)
315 {
316 iBit = 0;
317 while (!(u32 & 1))
318 {
319 u32 >>= 1;
320 iBit++;
321 }
322 return iBitPrev + iBit;
323 }
324
325 /*
326 * Skip ahead and see if there is anything left to search.
327 */
328 iBitPrev |= 31;
329 iBitPrev++;
330 if (cBits <= (uint32_t)iBitPrev)
331 return -1;
332 }
333
334 /*
335 * 32-bit aligned search, let ASMBitFirstClear do the dirty work.
336 */
337 iBit = ASMBitFirstClear(&pau32Bitmap[iBitPrev / 32], cBits - iBitPrev);
338 if (iBit >= 0)
339 iBit += iBitPrev;
340 return iBit;
341}
342
343RTDECL(int) ASMBitFirstSet(const volatile void *pvBitmap, uint32_t cBits)
344{
345 uint32_t iBit = 0;
346 uint32_t volatile *pu32 = (uint32_t volatile *)pvBitmap;
347 while (iBit < cBits)
348 {
349 uint32_t u32 = *pu32;
350 if (u32 != 0)
351 {
352 while (!(u32 & 1))
353 {
354 u32 >>= 1;
355 iBit++;
356 }
357 if (iBit >= cBits)
358 return -1;
359 return iBit;
360 }
361
362 iBit += 32;
363 pu32++;
364 }
365 return -1;
366}
367
368RTDECL(int) ASMBitNextSet(const volatile void *pvBitmap, uint32_t cBits, uint32_t iBitPrev)
369{
370 const volatile uint32_t *pau32Bitmap = (const volatile uint32_t *)pvBitmap;
371 int iBit = ++iBitPrev & 31;
372 if (iBit)
373 {
374 /*
375 * Inspect the 32-bit word containing the unaligned bit.
376 */
377 uint32_t u32 = pau32Bitmap[iBitPrev / 32] >> iBit;
378 if (u32)
379 {
380 iBit = 0;
381 while (!(u32 & 1))
382 {
383 u32 >>= 1;
384 iBit++;
385 }
386 return iBitPrev + iBit;
387 }
388
389 /*
390 * Skip ahead and see if there is anything left to search.
391 */
392 iBitPrev |= 31;
393 iBitPrev++;
394 if (cBits <= (uint32_t)iBitPrev)
395 return -1;
396 }
397
398 /*
399 * 32-bit aligned search, let ASMBitFirstSet do the dirty work.
400 */
401 iBit = ASMBitFirstSet(&pau32Bitmap[iBitPrev / 32], cBits - iBitPrev);
402 if (iBit >= 0)
403 iBit += iBitPrev;
404 return iBit;
405}
406
407RTDECL(unsigned) ASMBitFirstSetU32(uint32_t u32)
408{
409 uint32_t iBit;
410 for (iBit = 0; iBit < 32; iBit++)
411 if (u32 & RT_BIT_32(iBit))
412 return iBit + 1;
413 return 0;
414}
415
416RTDECL(unsigned) ASMBitLastSetU32(uint32_t u32)
417{
418 int32_t iBit = 32;
419 while (iBit-- > 0)
420 if (u32 & RT_BIT_32(iBit))
421 return iBit + 1;
422 return 0;
423}
424
425RTDECL(uint16_t) ASMByteSwapU16(uint16_t u16)
426{
427 return RT_MAKE_U16(RT_HIBYTE(u16), RT_LOBYTE(u16));
428}
429
430RTDECL(uint32_t) ASMByteSwapU32(uint32_t u32)
431{
432 return RT_MAKE_U32_FROM_U8(RT_BYTE4(u32), RT_BYTE3(u32), RT_BYTE2(u32), RT_BYTE1(u32));
433}
434
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