bitops.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. #ifndef _ASM_X86_BITOPS_H
  2. #define _ASM_X86_BITOPS_H
  3. /*
  4. * Copyright 1992, Linus Torvalds.
  5. *
  6. * Note: inlines with more than a single statement should be marked
  7. * __always_inline to avoid problems with older gcc's inlining heuristics.
  8. */
  9. #ifndef _LINUX_BITOPS_H
  10. #error only <linux/bitops.h> can be included directly
  11. #endif
  12. #include <linux/compiler.h>
  13. #include <asm/alternative.h>
  14. #include <asm/rmwcc.h>
  15. #include <asm/barrier.h>
  16. #if BITS_PER_LONG == 32
  17. # define _BITOPS_LONG_SHIFT 5
  18. #elif BITS_PER_LONG == 64
  19. # define _BITOPS_LONG_SHIFT 6
  20. #else
  21. # error "Unexpected BITS_PER_LONG"
  22. #endif
  23. #define BIT_64(n) (U64_C(1) << (n))
  24. /*
  25. * These have to be done with inline assembly: that way the bit-setting
  26. * is guaranteed to be atomic. All bit operations return 0 if the bit
  27. * was cleared before the operation and != 0 if it was not.
  28. *
  29. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  30. */
  31. #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
  32. /* Technically wrong, but this avoids compilation errors on some gcc
  33. versions. */
  34. #define BITOP_ADDR(x) "=m" (*(volatile long *) (x))
  35. #else
  36. #define BITOP_ADDR(x) "+m" (*(volatile long *) (x))
  37. #endif
  38. #define ADDR BITOP_ADDR(addr)
  39. /*
  40. * We do the locked ops that don't return the old value as
  41. * a mask operation on a byte.
  42. */
  43. #define IS_IMMEDIATE(nr) (__builtin_constant_p(nr))
  44. #define CONST_MASK_ADDR(nr, addr) BITOP_ADDR((void *)(addr) + ((nr)>>3))
  45. #define CONST_MASK(nr) (1 << ((nr) & 7))
  46. /**
  47. * set_bit - Atomically set a bit in memory
  48. * @nr: the bit to set
  49. * @addr: the address to start counting from
  50. *
  51. * This function is atomic and may not be reordered. See __set_bit()
  52. * if you do not require the atomic guarantees.
  53. *
  54. * Note: there are no guarantees that this function will not be reordered
  55. * on non x86 architectures, so if you are writing portable code,
  56. * make sure not to rely on its reordering guarantees.
  57. *
  58. * Note that @nr may be almost arbitrarily large; this function is not
  59. * restricted to acting on a single-word quantity.
  60. */
  61. static __always_inline void
  62. set_bit(long nr, volatile unsigned long *addr)
  63. {
  64. if (IS_IMMEDIATE(nr)) {
  65. asm volatile(LOCK_PREFIX "orb %1,%0"
  66. : CONST_MASK_ADDR(nr, addr)
  67. : "iq" ((u8)CONST_MASK(nr))
  68. : "memory");
  69. } else {
  70. asm volatile(LOCK_PREFIX "bts %1,%0"
  71. : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
  72. }
  73. }
  74. /**
  75. * __set_bit - Set a bit in memory
  76. * @nr: the bit to set
  77. * @addr: the address to start counting from
  78. *
  79. * Unlike set_bit(), this function is non-atomic and may be reordered.
  80. * If it's called on the same region of memory simultaneously, the effect
  81. * may be that only one operation succeeds.
  82. */
  83. static __always_inline void __set_bit(long nr, volatile unsigned long *addr)
  84. {
  85. asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
  86. }
  87. /**
  88. * clear_bit - Clears a bit in memory
  89. * @nr: Bit to clear
  90. * @addr: Address to start counting from
  91. *
  92. * clear_bit() is atomic and may not be reordered. However, it does
  93. * not contain a memory barrier, so if it is used for locking purposes,
  94. * you should call smp_mb__before_atomic() and/or smp_mb__after_atomic()
  95. * in order to ensure changes are visible on other processors.
  96. */
  97. static __always_inline void
  98. clear_bit(long nr, volatile unsigned long *addr)
  99. {
  100. if (IS_IMMEDIATE(nr)) {
  101. asm volatile(LOCK_PREFIX "andb %1,%0"
  102. : CONST_MASK_ADDR(nr, addr)
  103. : "iq" ((u8)~CONST_MASK(nr)));
  104. } else {
  105. asm volatile(LOCK_PREFIX "btr %1,%0"
  106. : BITOP_ADDR(addr)
  107. : "Ir" (nr));
  108. }
  109. }
  110. /*
  111. * clear_bit_unlock - Clears a bit in memory
  112. * @nr: Bit to clear
  113. * @addr: Address to start counting from
  114. *
  115. * clear_bit() is atomic and implies release semantics before the memory
  116. * operation. It can be used for an unlock.
  117. */
  118. static __always_inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
  119. {
  120. barrier();
  121. clear_bit(nr, addr);
  122. }
  123. static __always_inline void __clear_bit(long nr, volatile unsigned long *addr)
  124. {
  125. asm volatile("btr %1,%0" : ADDR : "Ir" (nr));
  126. }
  127. /*
  128. * __clear_bit_unlock - Clears a bit in memory
  129. * @nr: Bit to clear
  130. * @addr: Address to start counting from
  131. *
  132. * __clear_bit() is non-atomic and implies release semantics before the memory
  133. * operation. It can be used for an unlock if no other CPUs can concurrently
  134. * modify other bits in the word.
  135. *
  136. * No memory barrier is required here, because x86 cannot reorder stores past
  137. * older loads. Same principle as spin_unlock.
  138. */
  139. static __always_inline void __clear_bit_unlock(long nr, volatile unsigned long *addr)
  140. {
  141. barrier();
  142. __clear_bit(nr, addr);
  143. }
  144. /**
  145. * __change_bit - Toggle a bit in memory
  146. * @nr: the bit to change
  147. * @addr: the address to start counting from
  148. *
  149. * Unlike change_bit(), this function is non-atomic and may be reordered.
  150. * If it's called on the same region of memory simultaneously, the effect
  151. * may be that only one operation succeeds.
  152. */
  153. static __always_inline void __change_bit(long nr, volatile unsigned long *addr)
  154. {
  155. asm volatile("btc %1,%0" : ADDR : "Ir" (nr));
  156. }
  157. /**
  158. * change_bit - Toggle a bit in memory
  159. * @nr: Bit to change
  160. * @addr: Address to start counting from
  161. *
  162. * change_bit() is atomic and may not be reordered.
  163. * Note that @nr may be almost arbitrarily large; this function is not
  164. * restricted to acting on a single-word quantity.
  165. */
  166. static __always_inline void change_bit(long nr, volatile unsigned long *addr)
  167. {
  168. if (IS_IMMEDIATE(nr)) {
  169. asm volatile(LOCK_PREFIX "xorb %1,%0"
  170. : CONST_MASK_ADDR(nr, addr)
  171. : "iq" ((u8)CONST_MASK(nr)));
  172. } else {
  173. asm volatile(LOCK_PREFIX "btc %1,%0"
  174. : BITOP_ADDR(addr)
  175. : "Ir" (nr));
  176. }
  177. }
  178. /**
  179. * test_and_set_bit - Set a bit and return its old value
  180. * @nr: Bit to set
  181. * @addr: Address to count from
  182. *
  183. * This operation is atomic and cannot be reordered.
  184. * It also implies a memory barrier.
  185. */
  186. static __always_inline bool test_and_set_bit(long nr, volatile unsigned long *addr)
  187. {
  188. GEN_BINARY_RMWcc(LOCK_PREFIX "bts", *addr, "Ir", nr, "%0", c);
  189. }
  190. /**
  191. * test_and_set_bit_lock - Set a bit and return its old value for lock
  192. * @nr: Bit to set
  193. * @addr: Address to count from
  194. *
  195. * This is the same as test_and_set_bit on x86.
  196. */
  197. static __always_inline bool
  198. test_and_set_bit_lock(long nr, volatile unsigned long *addr)
  199. {
  200. return test_and_set_bit(nr, addr);
  201. }
  202. /**
  203. * __test_and_set_bit - Set a bit and return its old value
  204. * @nr: Bit to set
  205. * @addr: Address to count from
  206. *
  207. * This operation is non-atomic and can be reordered.
  208. * If two examples of this operation race, one can appear to succeed
  209. * but actually fail. You must protect multiple accesses with a lock.
  210. */
  211. static __always_inline bool __test_and_set_bit(long nr, volatile unsigned long *addr)
  212. {
  213. bool oldbit;
  214. asm("bts %2,%1\n\t"
  215. CC_SET(c)
  216. : CC_OUT(c) (oldbit), ADDR
  217. : "Ir" (nr));
  218. return oldbit;
  219. }
  220. /**
  221. * test_and_clear_bit - Clear a bit and return its old value
  222. * @nr: Bit to clear
  223. * @addr: Address to count from
  224. *
  225. * This operation is atomic and cannot be reordered.
  226. * It also implies a memory barrier.
  227. */
  228. static __always_inline bool test_and_clear_bit(long nr, volatile unsigned long *addr)
  229. {
  230. GEN_BINARY_RMWcc(LOCK_PREFIX "btr", *addr, "Ir", nr, "%0", c);
  231. }
  232. /**
  233. * __test_and_clear_bit - Clear a bit and return its old value
  234. * @nr: Bit to clear
  235. * @addr: Address to count from
  236. *
  237. * This operation is non-atomic and can be reordered.
  238. * If two examples of this operation race, one can appear to succeed
  239. * but actually fail. You must protect multiple accesses with a lock.
  240. *
  241. * Note: the operation is performed atomically with respect to
  242. * the local CPU, but not other CPUs. Portable code should not
  243. * rely on this behaviour.
  244. * KVM relies on this behaviour on x86 for modifying memory that is also
  245. * accessed from a hypervisor on the same CPU if running in a VM: don't change
  246. * this without also updating arch/x86/kernel/kvm.c
  247. */
  248. static __always_inline bool __test_and_clear_bit(long nr, volatile unsigned long *addr)
  249. {
  250. bool oldbit;
  251. asm volatile("btr %2,%1\n\t"
  252. CC_SET(c)
  253. : CC_OUT(c) (oldbit), ADDR
  254. : "Ir" (nr));
  255. return oldbit;
  256. }
  257. /* WARNING: non atomic and it can be reordered! */
  258. static __always_inline bool __test_and_change_bit(long nr, volatile unsigned long *addr)
  259. {
  260. bool oldbit;
  261. asm volatile("btc %2,%1\n\t"
  262. CC_SET(c)
  263. : CC_OUT(c) (oldbit), ADDR
  264. : "Ir" (nr) : "memory");
  265. return oldbit;
  266. }
  267. /**
  268. * test_and_change_bit - Change a bit and return its old value
  269. * @nr: Bit to change
  270. * @addr: Address to count from
  271. *
  272. * This operation is atomic and cannot be reordered.
  273. * It also implies a memory barrier.
  274. */
  275. static __always_inline bool test_and_change_bit(long nr, volatile unsigned long *addr)
  276. {
  277. GEN_BINARY_RMWcc(LOCK_PREFIX "btc", *addr, "Ir", nr, "%0", c);
  278. }
  279. static __always_inline bool constant_test_bit(long nr, const volatile unsigned long *addr)
  280. {
  281. return ((1UL << (nr & (BITS_PER_LONG-1))) &
  282. (addr[nr >> _BITOPS_LONG_SHIFT])) != 0;
  283. }
  284. static __always_inline bool variable_test_bit(long nr, volatile const unsigned long *addr)
  285. {
  286. bool oldbit;
  287. asm volatile("bt %2,%1\n\t"
  288. CC_SET(c)
  289. : CC_OUT(c) (oldbit)
  290. : "m" (*(unsigned long *)addr), "Ir" (nr));
  291. return oldbit;
  292. }
  293. #if 0 /* Fool kernel-doc since it doesn't do macros yet */
  294. /**
  295. * test_bit - Determine whether a bit is set
  296. * @nr: bit number to test
  297. * @addr: Address to start counting from
  298. */
  299. static bool test_bit(int nr, const volatile unsigned long *addr);
  300. #endif
  301. #define test_bit(nr, addr) \
  302. (__builtin_constant_p((nr)) \
  303. ? constant_test_bit((nr), (addr)) \
  304. : variable_test_bit((nr), (addr)))
  305. /**
  306. * __ffs - find first set bit in word
  307. * @word: The word to search
  308. *
  309. * Undefined if no bit exists, so code should check against 0 first.
  310. */
  311. static __always_inline unsigned long __ffs(unsigned long word)
  312. {
  313. asm("rep; bsf %1,%0"
  314. : "=r" (word)
  315. : "rm" (word));
  316. return word;
  317. }
  318. /**
  319. * ffz - find first zero bit in word
  320. * @word: The word to search
  321. *
  322. * Undefined if no zero exists, so code should check against ~0UL first.
  323. */
  324. static __always_inline unsigned long ffz(unsigned long word)
  325. {
  326. asm("rep; bsf %1,%0"
  327. : "=r" (word)
  328. : "r" (~word));
  329. return word;
  330. }
  331. /*
  332. * __fls: find last set bit in word
  333. * @word: The word to search
  334. *
  335. * Undefined if no set bit exists, so code should check against 0 first.
  336. */
  337. static __always_inline unsigned long __fls(unsigned long word)
  338. {
  339. asm("bsr %1,%0"
  340. : "=r" (word)
  341. : "rm" (word));
  342. return word;
  343. }
  344. #undef ADDR
  345. #ifdef __KERNEL__
  346. /**
  347. * ffs - find first set bit in word
  348. * @x: the word to search
  349. *
  350. * This is defined the same way as the libc and compiler builtin ffs
  351. * routines, therefore differs in spirit from the other bitops.
  352. *
  353. * ffs(value) returns 0 if value is 0 or the position of the first
  354. * set bit if value is nonzero. The first (least significant) bit
  355. * is at position 1.
  356. */
  357. static __always_inline int ffs(int x)
  358. {
  359. int r;
  360. #ifdef CONFIG_X86_64
  361. /*
  362. * AMD64 says BSFL won't clobber the dest reg if x==0; Intel64 says the
  363. * dest reg is undefined if x==0, but their CPU architect says its
  364. * value is written to set it to the same as before, except that the
  365. * top 32 bits will be cleared.
  366. *
  367. * We cannot do this on 32 bits because at the very least some
  368. * 486 CPUs did not behave this way.
  369. */
  370. asm("bsfl %1,%0"
  371. : "=r" (r)
  372. : "rm" (x), "0" (-1));
  373. #elif defined(CONFIG_X86_CMOV)
  374. asm("bsfl %1,%0\n\t"
  375. "cmovzl %2,%0"
  376. : "=&r" (r) : "rm" (x), "r" (-1));
  377. #else
  378. asm("bsfl %1,%0\n\t"
  379. "jnz 1f\n\t"
  380. "movl $-1,%0\n"
  381. "1:" : "=r" (r) : "rm" (x));
  382. #endif
  383. return r + 1;
  384. }
  385. /**
  386. * fls - find last set bit in word
  387. * @x: the word to search
  388. *
  389. * This is defined in a similar way as the libc and compiler builtin
  390. * ffs, but returns the position of the most significant set bit.
  391. *
  392. * fls(value) returns 0 if value is 0 or the position of the last
  393. * set bit if value is nonzero. The last (most significant) bit is
  394. * at position 32.
  395. */
  396. static __always_inline int fls(int x)
  397. {
  398. int r;
  399. #ifdef CONFIG_X86_64
  400. /*
  401. * AMD64 says BSRL won't clobber the dest reg if x==0; Intel64 says the
  402. * dest reg is undefined if x==0, but their CPU architect says its
  403. * value is written to set it to the same as before, except that the
  404. * top 32 bits will be cleared.
  405. *
  406. * We cannot do this on 32 bits because at the very least some
  407. * 486 CPUs did not behave this way.
  408. */
  409. asm("bsrl %1,%0"
  410. : "=r" (r)
  411. : "rm" (x), "0" (-1));
  412. #elif defined(CONFIG_X86_CMOV)
  413. asm("bsrl %1,%0\n\t"
  414. "cmovzl %2,%0"
  415. : "=&r" (r) : "rm" (x), "rm" (-1));
  416. #else
  417. asm("bsrl %1,%0\n\t"
  418. "jnz 1f\n\t"
  419. "movl $-1,%0\n"
  420. "1:" : "=r" (r) : "rm" (x));
  421. #endif
  422. return r + 1;
  423. }
  424. /**
  425. * fls64 - find last set bit in a 64-bit word
  426. * @x: the word to search
  427. *
  428. * This is defined in a similar way as the libc and compiler builtin
  429. * ffsll, but returns the position of the most significant set bit.
  430. *
  431. * fls64(value) returns 0 if value is 0 or the position of the last
  432. * set bit if value is nonzero. The last (most significant) bit is
  433. * at position 64.
  434. */
  435. #ifdef CONFIG_X86_64
  436. static __always_inline int fls64(__u64 x)
  437. {
  438. int bitpos = -1;
  439. /*
  440. * AMD64 says BSRQ won't clobber the dest reg if x==0; Intel64 says the
  441. * dest reg is undefined if x==0, but their CPU architect says its
  442. * value is written to set it to the same as before.
  443. */
  444. asm("bsrq %1,%q0"
  445. : "+r" (bitpos)
  446. : "rm" (x));
  447. return bitpos + 1;
  448. }
  449. #else
  450. #include <asm-generic/bitops/fls64.h>
  451. #endif
  452. #include <asm-generic/bitops/find.h>
  453. #include <asm-generic/bitops/sched.h>
  454. #include <asm/arch_hweight.h>
  455. #include <asm-generic/bitops/const_hweight.h>
  456. #include <asm-generic/bitops/le.h>
  457. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  458. #endif /* __KERNEL__ */
  459. #endif /* _ASM_X86_BITOPS_H */