-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_atomic.h
2436 lines (2216 loc) · 64.2 KB
/
vm_atomic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************
* Copyright (C) 1998 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*********************************************************/
/*
* vm_atomic.h --
*
* Atomic power
*
* Note: Only partially tested on ARM processors: Works for View Open
* Client, which shouldn't have threads.
*
* In ARM, GCC intrinsics (__sync*) compile but might not
* work, while MS intrinsics (_Interlocked*) do not compile,
* and ARM has no equivalent to the "lock" instruction prior to
* ARMv6; the current ARM target is ARMv5. According to glibc
* documentation, ARMv5 cannot have atomic code in user space.
* Instead a Linux system call to kernel code referenced in
* entry-armv.S is used to achieve atomic functions. See bug
* 478054 for details.
*/
#ifndef _ATOMIC_H_
#define _ATOMIC_H_
//#define FAKE_ATOMIC /* defined if true atomic not needed */
#define INCLUDE_ALLOW_USERLEVEL
#define INCLUDE_ALLOW_MODULE
#define INCLUDE_ALLOW_VMMON
#define INCLUDE_ALLOW_VMKDRIVERS
#define INCLUDE_ALLOW_VMK_MODULE
#define INCLUDE_ALLOW_VMKERNEL
#define INCLUDE_ALLOW_DISTRIBUTE
#define INCLUDE_ALLOW_VMCORE
#define INCLUDE_ALLOW_VMIROM
#include "includeCheck.h"
#include "vm_basic_types.h"
/* Basic atomic type: 32 bits */
typedef struct Atomic_uint32 {
volatile uint32 value;
} Atomic_uint32;
/* Basic atomic type: 64 bits */
typedef struct Atomic_uint64 {
volatile uint64 value;
} Atomic_uint64 ALIGNED(8);
/*
* Prototypes for msft atomics. These are defined & inlined by the
* compiler so no function definition is needed. The prototypes are
* needed for c++. Since amd64 compiler doesn't support inline asm we
* have to use these. Unfortunately, we still have to use some inline asm
* for the 32 bit code since the and/or/xor implementations didn't show up
* untill xp or 2k3.
*
* The declarations for the intrinsic functions were taken from ntddk.h
* in the DDK. The declarations must match otherwise the 64-bit c++
* compiler will complain about second linkage of the intrinsic functions.
* We define the intrinsic using the basic types corresponding to the
* Windows typedefs. This avoids having to include windows header files
* to get to the windows types.
*/
#if defined(_MSC_VER) && _MSC_VER >= 1310
#ifdef __cplusplus
extern "C" {
#endif
long _InterlockedExchange(long volatile*, long);
long _InterlockedCompareExchange(long volatile*, long, long);
long _InterlockedExchangeAdd(long volatile*, long);
long _InterlockedDecrement(long volatile*);
long _InterlockedIncrement(long volatile*);
#pragma intrinsic(_InterlockedExchange, _InterlockedCompareExchange)
#pragma intrinsic(_InterlockedExchangeAdd, _InterlockedDecrement)
#pragma intrinsic(_InterlockedIncrement)
#if defined(VM_X86_64)
long _InterlockedAnd(long volatile*, long);
__int64 _InterlockedAnd64(__int64 volatile*, __int64);
long _InterlockedOr(long volatile*, long);
__int64 _InterlockedOr64(__int64 volatile*, __int64);
long _InterlockedXor(long volatile*, long);
__int64 _InterlockedXor64(__int64 volatile*, __int64);
__int64 _InterlockedExchangeAdd64(__int64 volatile*, __int64);
__int64 _InterlockedIncrement64(__int64 volatile*);
__int64 _InterlockedDecrement64(__int64 volatile*);
__int64 _InterlockedExchange64(__int64 volatile*, __int64);
__int64 _InterlockedCompareExchange64(__int64 volatile*, __int64, __int64);
#if !defined(_WIN64)
#pragma intrinsic(_InterlockedAnd, _InterlockedAnd64)
#pragma intrinsic(_InterlockedOr, _InterlockedOr64)
#pragma intrinsic(_InterlockedXor, _InterlockedXor64)
#pragma intrinsic(_InterlockedExchangeAdd64, _InterlockedIncrement64)
#pragma intrinsic(_InterlockedDecrement64, _InterlockedExchange64)
#pragma intrinsic(_InterlockedCompareExchange64)
#endif /* !_WIN64 */
#endif /* __x86_64__ */
#ifdef __cplusplus
}
#endif
#endif /* _MSC_VER */
#ifdef __arm__
# if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || \
defined(__ARM_ARCH_7R__)|| defined(__ARM_ARCH_7M__)
# define VM_ARM_V7
# else
# error Only ARMv7 extends the synchronization primitives ldrex/strex. \
For the lower ARM version, please implement the atomic functions \
by kernel APIs.
# endif
#endif
/* Data Memory Barrier */
#ifdef VM_ARM_V7
#define dmb() __asm__ __volatile__("dmb" : : : "memory")
#endif
/* Convert a volatile uint32 to Atomic_uint32. */
static INLINE Atomic_uint32 *
Atomic_VolatileToAtomic(volatile uint32 *var)
{
return (Atomic_uint32 *)var;
}
/* Convert a volatile uint64 to Atomic_uint64. */
static INLINE Atomic_uint64 *
Atomic_VolatileToAtomic64(volatile uint64 *var)
{
return (Atomic_uint64 *)var;
}
/*
*-----------------------------------------------------------------------------
*
* Atomic_Init, Atomic_SetFence, AtomicUseFence --
*
* Determine whether an lfence intruction is executed after
* every locked instruction.
*
* Certain AMD processes have a bug (see bug 107024) that
* requires an lfence after every locked instruction.
*
* The global variable AtomicUseFence controls whether lfence
* is used (see AtomicEpilogue).
*
* Atomic_SetFence sets AtomicUseFence to the given value.
*
* Atomic_Init computes and sets AtomicUseFence for x86.
* It does not take into account the number of processors.
*
* The rationale for all this complexity is that Atomic_Init
* is the easy-to-use interface. It can be called a number
* of times cheaply, and does not depend on other libraries.
* However, because the number of CPUs is difficult to compute,
* it does without it and always assumes there are more than one.
*
* For programs that care or have special requirements,
* Atomic_SetFence can be called directly, in addition to Atomic_Init.
* It overrides the effect of Atomic_Init, and can be called
* before, after, or between calls to Atomic_Init.
*
*-----------------------------------------------------------------------------
*/
// The freebsd assembler doesn't know the lfence instruction
#if defined(__GNUC__) && \
__GNUC__ >= 3 && \
(defined(__VMKERNEL__) || !defined(__FreeBSD__)) && \
(!defined(MODULE) || defined(__VMKERNEL_MODULE__)) && \
!defined(__APPLE__) && \
(defined(__i386__) || defined(__x86_64__)) /* PR136775 */
#define ATOMIC_USE_FENCE
#endif
#if defined(VMATOMIC_IMPORT_DLLDATA)
VMX86_EXTERN_DATA Bool AtomicUseFence;
#else
EXTERN Bool AtomicUseFence;
#endif
EXTERN Bool atomicFenceInitialized;
void AtomicInitFence(void);
static INLINE void
Atomic_Init(void)
{
#ifdef ATOMIC_USE_FENCE
if (!atomicFenceInitialized) {
AtomicInitFence();
}
#endif
}
static INLINE void
Atomic_SetFence(Bool fenceAfterLock) /* IN: TRUE to enable lfence */
/* FALSE to disable. */
{
AtomicUseFence = fenceAfterLock;
#if defined(__VMKERNEL__)
extern void Atomic_SetFenceVMKAPI(Bool fenceAfterLock);
Atomic_SetFenceVMKAPI(fenceAfterLock);
#endif
atomicFenceInitialized = TRUE;
}
/* Conditionally execute fence after interlocked instruction. */
static INLINE void
AtomicEpilogue(void)
{
#ifdef ATOMIC_USE_FENCE
#ifdef VMM
/* The monitor conditionally patches out the lfence when not needed.*/
/* Construct a MonitorPatchTextEntry in the .patchtext section. */
asm volatile ("1:\n\t"
"lfence\n\t"
"2:\n\t"
".pushsection .patchtext\n\t"
".quad 1b\n\t"
".quad 2b\n\t"
".popsection\n\t" ::: "memory");
#else
if (UNLIKELY(AtomicUseFence)) {
asm volatile ("lfence" ::: "memory");
}
#endif
#endif
}
/*
* All the assembly code is tricky and written conservatively.
* For example, to make sure gcc won't introduce copies,
* we force the addressing mode like this:
*
* "xchgl %0, (%1)"
* : "=r" (val)
* : "r" (&var->value),
* "0" (val)
* : "memory"
*
* - edward
*
* Actually - turns out that gcc never generates memory aliases (it
* still does generate register aliases though), so we can be a bit
* more agressive with the memory constraints. The code above can be
* modified like this:
*
* "xchgl %0, %1"
* : "=r" (val),
* "=m" (var->value),
* : "0" (val),
* "1" (var->value)
*
* The advantages are that gcc can use whatever addressing mode it
* likes to access the memory value, and that we dont have to use a
* way-too-generic "memory" clobber as there is now an explicit
* declaration that var->value is modified.
*
* see also /usr/include/asm/atomic.h to convince yourself this is a
* valid optimization.
*
* - walken
*/
/*
*-----------------------------------------------------------------------------
*
* Atomic_Read --
*
* Read
*
* Results:
* The value of the atomic variable.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static INLINE uint32
Atomic_Read(Atomic_uint32 const *var) // IN
{
return var->value;
}
#define Atomic_Read32 Atomic_Read
/*
*-----------------------------------------------------------------------------
*
* Atomic_Write --
*
* Write
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_Write(Atomic_uint32 *var, // IN
uint32 val) // IN
{
var->value = val;
}
#define Atomic_Write32 Atomic_Write
/*
*-----------------------------------------------------------------------------
*
* Atomic_ReadWrite --
*
* Read followed by write
*
* Results:
* The value of the atomic variable before the write.
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE uint32
Atomic_ReadWrite(Atomic_uint32 *var, // IN
uint32 val) // IN
{
#ifdef __GNUC__
#ifdef VM_ARM_V7
register volatile uint32 retVal;
register volatile uint32 res;
dmb();
__asm__ __volatile__(
"1: ldrex %[retVal], [%[var]] \n\t"
"strex %[res], %[val], [%[var]] \n\t"
"teq %[res], #0 \n\t"
"bne 1b"
: [retVal] "=&r" (retVal), [res] "=&r" (res)
: [var] "r" (&var->value), [val] "r" (val)
: "memory", "cc"
);
dmb();
return retVal;
#else // __VM_ARM_V7 (assume x86*)
/* Checked against the Intel manual and GCC --walken */
__asm__ __volatile__(
"xchgl %0, %1"
: "=r" (val),
"+m" (var->value)
: "0" (val)
);
AtomicEpilogue();
return val;
#endif // VM_ARM_V7
#elif defined _MSC_VER
#if _MSC_VER >= 1310
return _InterlockedExchange((long *)&var->value, (long)val);
#else
#pragma warning(push)
#pragma warning(disable : 4035) // disable no-return warning
{
__asm mov eax, val
__asm mov ebx, var
__asm xchg [ebx]Atomic_uint32.value, eax
// eax is the return value, this is documented to work - edward
}
#pragma warning(pop)
#endif // _MSC_VER >= 1310
#else
#error No compiler defined for Atomic_ReadWrite
#endif // __GNUC__
}
#define Atomic_ReadWrite32 Atomic_ReadWrite
/*
*-----------------------------------------------------------------------------
*
* Atomic_ReadIfEqualWrite --
*
* Compare exchange: Read variable, if equal to oldVal, write newVal
*
* Results:
* The value of the atomic variable before the write.
*
* Side effects:
* The variable may be modified.
*
*-----------------------------------------------------------------------------
*/
static INLINE uint32
Atomic_ReadIfEqualWrite(Atomic_uint32 *var, // IN
uint32 oldVal, // IN
uint32 newVal) // IN
{
#ifdef __GNUC__
#ifdef VM_ARM_V7
register uint32 retVal;
register uint32 res;
dmb();
__asm__ __volatile__(
"1: ldrex %[retVal], [%[var]] \n\t"
"mov %[res], #1 \n\t"
"teq %[retVal], %[oldVal] \n\t"
"strexeq %[res], %[newVal], [%[var]] \n\t"
"teq %[res], #0 \n\t"
"bne 1b"
: [retVal] "=&r" (retVal), [res] "=&r" (res)
: [var] "r" (&var->value), [oldVal] "r" (oldVal), [newVal] "r" (newVal)
: "memory", "cc"
);
dmb();
return retVal;
#else // VM_ARM_V7 (assume x86*)
uint32 val;
/* Checked against the Intel manual and GCC --walken */
__asm__ __volatile__(
"lock; cmpxchgl %2, %1"
: "=a" (val),
"+m" (var->value)
: "r" (newVal),
"0" (oldVal)
: "cc"
);
AtomicEpilogue();
return val;
#endif // VM_ARM_V7
#elif defined _MSC_VER
#if _MSC_VER >= 1310
return _InterlockedCompareExchange((long *)&var->value,
(long)newVal,
(long)oldVal);
#else
#pragma warning(push)
#pragma warning(disable : 4035) // disable no-return warning
{
__asm mov eax, oldVal
__asm mov ebx, var
__asm mov ecx, newVal
__asm lock cmpxchg [ebx]Atomic_uint32.value, ecx
// eax is the return value, this is documented to work - edward
}
#pragma warning(pop)
#endif
#else
#error No compiler defined for Atomic_ReadIfEqualWrite
#endif
}
#define Atomic_ReadIfEqualWrite32 Atomic_ReadIfEqualWrite
#if defined(__x86_64__)
/*
*-----------------------------------------------------------------------------
*
* Atomic_ReadIfEqualWrite64 --
*
* Compare exchange: Read variable, if equal to oldVal, write newVal
*
* Results:
* The value of the atomic variable before the write.
*
* Side effects:
* The variable may be modified.
*
*-----------------------------------------------------------------------------
*/
static INLINE uint64
Atomic_ReadIfEqualWrite64(Atomic_uint64 *var, // IN
uint64 oldVal, // IN
uint64 newVal) // IN
{
#if defined(__GNUC__)
uint64 val;
/* Checked against the AMD manual and GCC --hpreg */
__asm__ __volatile__(
"lock; cmpxchgq %2, %1"
: "=a" (val),
"+m" (var->value)
: "r" (newVal),
"0" (oldVal)
: "cc"
);
AtomicEpilogue();
return val;
#elif defined _MSC_VER
return _InterlockedCompareExchange64((__int64 *)&var->value,
(__int64)newVal,
(__int64)oldVal);
#else
#error No compiler defined for Atomic_ReadIfEqualWrite64
#endif
}
#endif
/*
*-----------------------------------------------------------------------------
*
* Atomic_And --
*
* Atomic read, bitwise AND with a value, write.
*
* Results:
* None
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_And(Atomic_uint32 *var, // IN
uint32 val) // IN
{
#ifdef __GNUC__
#ifdef VM_ARM_V7
register volatile uint32 res;
register volatile uint32 tmp;
dmb();
__asm__ __volatile__(
"1: ldrex %[tmp], [%[var]] \n\t"
"and %[tmp], %[val] \n\t"
"strex %[res], %[tmp], [%[var]] \n\t"
"teq %[res], #0 \n\t"
"bne 1b"
: [res] "=&r" (res), [tmp] "=&r" (tmp)
: [var] "r" (&var->value), [val] "r" (val)
: "memory", "cc"
);
dmb();
#else /* VM_ARM_V7 */
/* Checked against the Intel manual and GCC --walken */
__asm__ __volatile__(
"lock; andl %1, %0"
: "+m" (var->value)
: "ri" (val)
: "cc"
);
AtomicEpilogue();
#endif // VM_ARM_V7
#elif defined _MSC_VER
#if defined(__x86_64__)
_InterlockedAnd((long *)&var->value, (long)val);
#else
__asm mov eax, val
__asm mov ebx, var
__asm lock and [ebx]Atomic_uint32.value, eax
#endif
#else
#error No compiler defined for Atomic_And
#endif
}
#define Atomic_And32 Atomic_And
/*
*-----------------------------------------------------------------------------
*
* Atomic_Or --
*
* Atomic read, bitwise OR with a value, write.
*
* Results:
* None
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_Or(Atomic_uint32 *var, // IN
uint32 val) // IN
{
#ifdef __GNUC__
#ifdef VM_ARM_V7
register volatile uint32 res;
register volatile uint32 tmp;
dmb();
__asm__ __volatile__(
"1: ldrex %[tmp], [%[var]] \n\t"
"orr %[tmp], %[val] \n\t"
"strex %[res], %[tmp], [%[var]] \n\t"
"teq %[res], #0 \n\t"
"bne 1b"
: [res] "=&r" (res), [tmp] "=&r" (tmp)
: [var] "r" (&var->value), [val] "r" (val)
: "memory", "cc"
);
dmb();
#else // VM_ARM_V7
/* Checked against the Intel manual and GCC --walken */
__asm__ __volatile__(
"lock; orl %1, %0"
: "+m" (var->value)
: "ri" (val)
: "cc"
);
AtomicEpilogue();
#endif // VM_ARM_V7
#elif defined _MSC_VER
#if defined(__x86_64__)
_InterlockedOr((long *)&var->value, (long)val);
#else
__asm mov eax, val
__asm mov ebx, var
__asm lock or [ebx]Atomic_uint32.value, eax
#endif
#else
#error No compiler defined for Atomic_Or
#endif
}
#define Atomic_Or32 Atomic_Or
/*
*-----------------------------------------------------------------------------
*
* Atomic_Xor --
*
* Atomic read, bitwise XOR with a value, write.
*
* Results:
* None
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_Xor(Atomic_uint32 *var, // IN
uint32 val) // IN
{
#ifdef __GNUC__
#ifdef VM_ARM_V7
register volatile uint32 res;
register volatile uint32 tmp;
dmb();
__asm__ __volatile__(
"1: ldrex %[tmp], [%[var]] \n\t"
"eor %[tmp], %[val] \n\t"
"strex %[res], %[tmp], [%[var]] \n\t"
"teq %[res], #0 \n\t"
"bne 1b"
: [res] "=&r" (res), [tmp] "=&r" (tmp)
: [var] "r" (&var->value), [val] "r" (val)
: "memory", "cc"
);
dmb();
#else // VM_ARM_V7
/* Checked against the Intel manual and GCC --walken */
__asm__ __volatile__(
"lock; xorl %1, %0"
: "+m" (var->value)
: "ri" (val)
: "cc"
);
AtomicEpilogue();
#endif // VM_ARM_V7
#elif defined _MSC_VER
#if defined(__x86_64__)
_InterlockedXor((long *)&var->value, (long)val);
#else
__asm mov eax, val
__asm mov ebx, var
__asm lock xor [ebx]Atomic_uint32.value, eax
#endif
#else
#error No compiler defined for Atomic_Xor
#endif
}
#define Atomic_Xor32 Atomic_Xor
#if defined(__x86_64__)
/*
*-----------------------------------------------------------------------------
*
* Atomic_Xor64 --
*
* Atomic read, bitwise XOR with a value, write.
*
* Results:
* None
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_Xor64(Atomic_uint64 *var, // IN
uint64 val) // IN
{
#if defined(__GNUC__)
/* Checked against the AMD manual and GCC --hpreg */
__asm__ __volatile__(
"lock; xorq %1, %0"
: "+m" (var->value)
: "ri" (val)
: "cc"
);
AtomicEpilogue();
#elif defined _MSC_VER
_InterlockedXor64((__int64 *)&var->value, (__int64)val);
#else
#error No compiler defined for Atomic_Xor64
#endif
}
#endif
/*
*-----------------------------------------------------------------------------
*
* Atomic_Add --
*
* Atomic read, add a value, write.
*
* Results:
* None
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_Add(Atomic_uint32 *var, // IN
uint32 val) // IN
{
#ifdef __GNUC__
#ifdef VM_ARM_V7
register volatile uint32 res;
register volatile uint32 tmp;
dmb();
__asm__ __volatile__(
"1: ldrex %[tmp], [%[var]] \n\t"
"add %[tmp], %[val] \n\t"
"strex %[res], %[tmp], [%[var]] \n\t"
"teq %[res], #0 \n\t"
"bne 1b"
: [res] "=&r" (res), [tmp] "=&r" (tmp)
: [var] "r" (&var->value), [val] "r" (val)
: "memory", "cc"
);
dmb();
#else // VM_ARM_V7
/* Checked against the Intel manual and GCC --walken */
__asm__ __volatile__(
"lock; addl %1, %0"
: "+m" (var->value)
: "ri" (val)
: "cc"
);
AtomicEpilogue();
#endif // VM_ARM_V7
#elif defined _MSC_VER
#if _MSC_VER >= 1310
_InterlockedExchangeAdd((long *)&var->value, (long)val);
#else
__asm mov eax, val
__asm mov ebx, var
__asm lock add [ebx]Atomic_uint32.value, eax
#endif
#else
#error No compiler defined for Atomic_Add
#endif
}
#define Atomic_Add32 Atomic_Add
#if defined(__x86_64__)
/*
*-----------------------------------------------------------------------------
*
* Atomic_Add64 --
*
* Atomic read, add a value, write.
*
* Results:
* None
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_Add64(Atomic_uint64 *var, // IN
uint64 val) // IN
{
#if defined(__GNUC__)
/* Checked against the AMD manual and GCC --hpreg */
__asm__ __volatile__(
"lock; addq %1, %0"
: "+m" (var->value)
: "ri" (val)
: "cc"
);
AtomicEpilogue();
#elif defined _MSC_VER
_InterlockedExchangeAdd64((__int64 *)&var->value, (__int64)val);
#else
#error No compiler defined for Atomic_Add64
#endif
}
#endif
/*
*-----------------------------------------------------------------------------
*
* Atomic_Sub --
*
* Atomic read, subtract a value, write.
*
* Results:
* None
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_Sub(Atomic_uint32 *var, // IN
uint32 val) // IN
{
#ifdef __GNUC__
#ifdef VM_ARM_V7
register volatile uint32 res;
register volatile uint32 tmp;
dmb();
__asm__ __volatile__(
"1: ldrex %[tmp], [%[var]] \n\t"
"sub %[tmp], %[val] \n\t"
"strex %[res], %[tmp], [%[var]] \n\t"
"teq %[res], #0 \n\t"
"bne 1b"
: [res] "=&r" (res), [tmp] "=&r" (tmp)
: [var] "r" (&var->value), [val] "r" (val)
: "memory", "cc"
);
dmb();
#else // VM_ARM_V7
/* Checked against the Intel manual and GCC --walken */
__asm__ __volatile__(
"lock; subl %1, %0"
: "+m" (var->value)
: "ri" (val)
: "cc"
);
AtomicEpilogue();
#endif // VM_ARM_V7
#elif defined _MSC_VER
#if _MSC_VER >= 1310
_InterlockedExchangeAdd((long *)&var->value, (long)-val);
#else
__asm mov eax, val
__asm mov ebx, var
__asm lock sub [ebx]Atomic_uint32.value, eax
#endif
#else
#error No compiler defined for Atomic_Sub
#endif
}
#define Atomic_Sub32 Atomic_Sub
#if defined(__x86_64__)
/*
*-----------------------------------------------------------------------------
*
* Atomic_Sub64 --
*
* Atomic read, subtract a value, write.
*
* Results:
* None
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_Sub64(Atomic_uint64 *var, // IN
uint64 val) // IN
{
#ifdef __GNUC__
/* Checked against the AMD manual and GCC --hpreg */
__asm__ __volatile__(
"lock; subq %1, %0"
: "+m" (var->value)
: "ri" (val)
: "cc"
);
AtomicEpilogue();
#elif defined _MSC_VER
_InterlockedExchangeAdd64((__int64 *)&var->value, (__int64)-val);
#else
#error No compiler defined for Atomic_Sub64
#endif
}
#endif
/*
*-----------------------------------------------------------------------------
*
* Atomic_Inc --
*
* Atomic read, increment, write.
*
* Results:
* None
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static INLINE void
Atomic_Inc(Atomic_uint32 *var) // IN
{
#ifdef __GNUC__
#ifdef VM_ARM_V7
Atomic_Add(var, 1);
#else // VM_ARM_V7