atomic.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef _SHIM_ATOMIC_H_
  2. #define _SHIM_ATOMIC_H_
  3. /* Copyright (C) 2014 Stony Brook University
  4. * Copyright (C) 2017 Fortanix Inc, and University of North Carolina
  5. * at Chapel Hill.
  6. *
  7. * This file defines atomic operations (And barriers) for use in
  8. * Graphene.
  9. *
  10. * The atomic operation assembly code is taken from musl libc, which
  11. * is subject to the MIT license.
  12. *
  13. * At this point, we primarily focus on x86_64; there are some vestigial
  14. * 32-bit definitions here, but a more portable version would need to
  15. * move and reimplement portions of this for 32-bit x86 (or other architectures).
  16. */
  17. /*
  18. /----------------------------------------------------------------------
  19. Copyright (C) 2005-2014 Rich Felker, et al.
  20. Permission is hereby granted, free of charge, to any person obtaining
  21. a copy of this software and associated documentation files (the
  22. "Software"), to deal in the Software without restriction, including
  23. without limitation the rights to use, copy, modify, merge, publish,
  24. distribute, sublicense, and/or sell copies of the Software, and to
  25. permit persons to whom the Software is furnished to do so, subject to
  26. the following conditions:
  27. The above copyright notice and this permission notice shall be
  28. included in all copies or substantial portions of the Software.
  29. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. ----------------------------------------------------------------------
  37. */
  38. /* Optimization barrier */
  39. #define COMPILER_BARRIER() __asm__ __volatile__("": : :"memory")
  40. #define CPU_RELAX() __asm__ __volatile__("rep; nop" ::: "memory")
  41. #ifdef __i386__
  42. # define RMB() __asm__ __volatile__("lock; addl $0,0(%%esp)" ::: "memory")
  43. struct atomic_int {
  44. volatile int32_t counter;
  45. };
  46. #endif
  47. /* The return types below effectively assume we are dealing with a 64-bit
  48. * signed value.
  49. */
  50. #ifdef __x86_64__
  51. /*
  52. * Some non-Intel clones support out of order store. WMB() ceases to be a
  53. * nop for these.
  54. */
  55. # define MB() __asm__ __volatile__ ("mfence" ::: "memory")
  56. # define RMB() __asm__ __volatile__ ("lfence" ::: "memory")
  57. # define WMB() __asm__ __volatile__ ("sfence" ::: "memory")
  58. struct atomic_int {
  59. volatile int64_t counter;
  60. };
  61. #endif
  62. #define LOCK_PREFIX "\n\tlock; "
  63. #define ATOMIC_INIT(i) { (i) }
  64. /* Read the value currently stored in the atomic_int */
  65. static inline int64_t atomic_read (const struct atomic_int * v)
  66. {
  67. // Effectively:
  68. // return v->counter;
  69. int64_t i;
  70. /* Use inline assembly to ensure this is one instruction */
  71. __asm__ __volatile__("mov %1, %0"
  72. : "=r"(i) :
  73. "m"(v->counter));
  74. return i;
  75. }
  76. /* Does a blind write to the atomic variable */
  77. static inline void atomic_set (struct atomic_int * v, int64_t i)
  78. {
  79. // Effectively:
  80. // v->counter = i;
  81. /* Use inline assembly to ensure this is one instruction */
  82. __asm__ __volatile__("mov %2, %0"
  83. : "=m"(v->counter) :
  84. "m"(v->counter), "r"(i));
  85. }
  86. /* Helper function that atomically adds a value to an atomic_int,
  87. * and returns the _new_ value. */
  88. static inline int64_t _atomic_add (int64_t i, struct atomic_int * v)
  89. {
  90. int64_t increment = i;
  91. __asm__ __volatile__(
  92. "lock ; xadd %0, %1"
  93. : "=r"(i), "=m"(v->counter) : "0"(i) : "cc");
  94. return i + increment;
  95. }
  96. /* Atomically adds i to v. Does not return a value. */
  97. static inline void atomic_add (int64_t i, struct atomic_int * v)
  98. {
  99. _atomic_add(i, v);
  100. }
  101. /* Atomically substracts i from v. Does not return a value. */
  102. static inline void atomic_sub (int64_t i, struct atomic_int * v)
  103. {
  104. _atomic_add(-i, v);
  105. }
  106. /* Atomically adds 1 to v. Does not return a value. */
  107. static inline void atomic_inc (struct atomic_int * v)
  108. {
  109. __asm__ __volatile__(
  110. "lock ; incl %0"
  111. : "=m"(v->counter) : "m"(v->counter) : "cc");
  112. }
  113. /* Atomically substracts 1 from v. Does not return a value. */
  114. static inline void atomic_dec (struct atomic_int * v)
  115. {
  116. __asm__ __volatile__(
  117. "lock ; decl %0"
  118. : "=m"(v->counter) : "m"(v->counter) : "cc");
  119. }
  120. /* Atomically substracts 1 from v. Returns 1 if this causes the
  121. value to reach 0; returns 0 otherwise. */
  122. static inline int64_t atomic_dec_and_test (struct atomic_int * v)
  123. {
  124. int64_t i = _atomic_add(-1, v);
  125. return i == 0;
  126. }
  127. /* Helper function to atomically compare-and-swap the value pointed to by p.
  128. * t is the old value, s is the new value. Returns
  129. * the value originally in p. */
  130. static inline int64_t cmpxchg(volatile int64_t *p, int64_t t, int64_t s)
  131. {
  132. __asm__ __volatile__ (
  133. "lock ; cmpxchg %3, %1"
  134. : "=a"(t), "=m"(*p) : "a"(t), "r"(s) : "cc");
  135. return t;
  136. }
  137. #define atomic_add_return(i, v) _atomic_add(i, v)
  138. #define atomic_inc_return(v) _atomic_add(1, v)
  139. /* Helper function to atomically compare-and-swap the value in v.
  140. * If v == old, it sets v = new.
  141. * Returns the value originally in v. */
  142. static inline int64_t atomic_cmpxchg (struct atomic_int * v, int64_t old, int64_t new)
  143. {
  144. return cmpxchg(&v->counter, old, new);
  145. }
  146. #endif /* _ATOMIC_INT_H_ */