bitops.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. static __always_inline bool clear_bit_unlock_is_negative_byte(long nr, volatile unsigned long *addr)
  128. {
  129. bool negative;
  130. asm volatile(LOCK_PREFIX "andb %2,%1\n\t"
  131. CC_SET(s)
  132. : CC_OUT(s) (negative), ADDR
  133. : "ir" ((char) ~(1 << nr)) : "memory");
  134. return negative;
  135. }
  136. // Let everybody know we have it
  137. #define clear_bit_unlock_is_negative_byte clear_bit_unlock_is_negative_byte
  138. /*
  139. * __clear_bit_unlock - Clears a bit in memory
  140. * @nr: Bit to clear
  141. * @addr: Address to start counting from
  142. *
  143. * __clear_bit() is non-atomic and implies release semantics before the memory
  144. * operation. It can be used for an unlock if no other CPUs can concurrently
  145. * modify other bits in the word.
  146. *
  147. * No memory barrier is required here, because x86 cannot reorder stores past
  148. * older loads. Same principle as spin_unlock.
  149. */
  150. static __always_inline void __clear_bit_unlock(long nr, volatile unsigned long *addr)
  151. {
  152. barrier();
  153. __clear_bit(nr, addr);
  154. }
  155. /**
  156. * __change_bit - Toggle a bit in memory
  157. * @nr: the bit to change
  158. * @addr: the address to start counting from
  159. *
  160. * Unlike change_bit(), this function is non-atomic and may be reordered.
  161. * If it's called on the same region of memory simultaneously, the effect
  162. * may be that only one operation succeeds.
  163. */
  164. static __always_inline void __change_bit(long nr, volatile unsigned long *addr)
  165. {
  166. asm volatile("btc %1,%0" : ADDR : "Ir" (nr));
  167. }
  168. /**
  169. * change_bit - Toggle a bit in memory
  170. * @nr: Bit to change
  171. * @addr: Address to start counting from
  172. *
  173. * change_bit() is atomic and may not be reordered.
  174. * Note that @nr may be almost arbitrarily large; this function is not
  175. * restricted to acting on a single-word quantity.
  176. */
  177. static __always_inline void change_bit(long nr, volatile unsigned long *addr)
  178. {
  179. if (IS_IMMEDIATE(nr)) {
  180. asm volatile(LOCK_PREFIX "xorb %1,%0"
  181. : CONST_MASK_ADDR(nr, addr)
  182. : "iq" ((u8)CONST_MASK(nr)));
  183. } else {
  184. asm volatile(LOCK_PREFIX "btc %1,%0"
  185. : BITOP_ADDR(addr)
  186. : "Ir" (nr));
  187. }
  188. }
  189. /**
  190. * test_and_set_bit - Set a bit and return its old value
  191. * @nr: Bit to set
  192. * @addr: Address to count from
  193. *
  194. * This operation is atomic and cannot be reordered.
  195. * It also implies a memory barrier.
  196. */
  197. static __always_inline bool test_and_set_bit(long nr, volatile unsigned long *addr)
  198. {
  199. GEN_BINARY_RMWcc(LOCK_PREFIX "bts", *addr, "Ir", nr, "%0", c);
  200. }
  201. /**
  202. * test_and_set_bit_lock - Set a bit and return its old value for lock
  203. * @nr: Bit to set
  204. * @addr: Address to count from
  205. *
  206. * This is the same as test_and_set_bit on x86.
  207. */
  208. static __always_inline bool
  209. test_and_set_bit_lock(long nr, volatile unsigned long *addr)
  210. {
  211. return test_and_set_bit(nr, addr);
  212. }
  213. /**
  214. * __test_and_set_bit - Set a bit and return its old value
  215. * @nr: Bit to set
  216. * @addr: Address to count from
  217. *
  218. * This operation is non-atomic and can be reordered.
  219. * If two examples of this operation race, one can appear to succeed
  220. * but actually fail. You must protect multiple accesses with a lock.
  221. */
  222. static __always_inline bool __test_and_set_bit(long nr, volatile unsigned long *addr)
  223. {
  224. bool oldbit;
  225. asm("bts %2,%1\n\t"
  226. CC_SET(c)
  227. : CC_OUT(c) (oldbit), ADDR
  228. : "Ir" (nr));
  229. return oldbit;
  230. }
  231. /**
  232. * test_and_clear_bit - Clear a bit and return its old value
  233. * @nr: Bit to clear
  234. * @addr: Address to count from
  235. *
  236. * This operation is atomic and cannot be reordered.
  237. * It also implies a memory barrier.
  238. */
  239. static __always_inline bool test_and_clear_bit(long nr, volatile unsigned long *addr)
  240. {
  241. GEN_BINARY_RMWcc(LOCK_PREFIX "btr", *addr, "Ir", nr, "%0", c);
  242. }
  243. /**
  244. * __test_and_clear_bit - Clear a bit and return its old value
  245. * @nr: Bit to clear
  246. * @addr: Address to count from
  247. *
  248. * This operation is non-atomic and can be reordered.
  249. * If two examples of this operation race, one can appear to succeed
  250. * but actually fail. You must protect multiple accesses with a lock.
  251. *
  252. * Note: the operation is performed atomically with respect to
  253. * the local CPU, but not other CPUs. Portable code should not
  254. * rely on this behaviour.
  255. * KVM relies on this behaviour on x86 for modifying memory that is also
  256. * accessed from a hypervisor on the same CPU if running in a VM: don't change
  257. * this without also updating arch/x86/kernel/kvm.c
  258. */
  259. static __always_inline bool __test_and_clear_bit(long nr, volatile unsigned long *addr)
  260. {
  261. bool oldbit;
  262. asm volatile("btr %2,%1\n\t"
  263. CC_SET(c)
  264. : CC_OUT(c) (oldbit), ADDR
  265. : "Ir" (nr));
  266. return oldbit;
  267. }
  268. /* WARNING: non atomic and it can be reordered! */
  269. static __always_inline bool __test_and_change_bit(long nr, volatile unsigned long *addr)
  270. {
  271. bool oldbit;
  272. asm volatile("btc %2,%1\n\t"
  273. CC_SET(c)
  274. : CC_OUT(c) (oldbit), ADDR
  275. : "Ir" (nr) : "memory");
  276. return oldbit;
  277. }
  278. /**
  279. * test_and_change_bit - Change a bit and return its old value
  280. * @nr: Bit to change
  281. * @addr: Address to count from
  282. *
  283. * This operation is atomic and cannot be reordered.
  284. * It also implies a memory barrier.
  285. */
  286. static __always_inline bool test_and_change_bit(long nr, volatile unsigned long *addr)
  287. {
  288. GEN_BINARY_RMWcc(LOCK_PREFIX "btc", *addr, "Ir", nr, "%0", c);
  289. }
  290. static __always_inline bool constant_test_bit(long nr, const volatile unsigned long *addr)
  291. {
  292. return ((1UL << (nr & (BITS_PER_LONG-1))) &
  293. (addr[nr >> _BITOPS_LONG_SHIFT])) != 0;
  294. }
  295. static __always_inline bool variable_test_bit(long nr, volatile const unsigned long *addr)
  296. {
  297. bool oldbit;
  298. asm volatile("bt %2,%1\n\t"
  299. CC_SET(c)
  300. : CC_OUT(c) (oldbit)
  301. : "m" (*(unsigned long *)addr), "Ir" (nr));
  302. return oldbit;
  303. }
  304. #if 0 /* Fool kernel-doc since it doesn't do macros yet */
  305. /**
  306. * test_bit - Determine whether a bit is set
  307. * @nr: bit number to test
  308. * @addr: Address to start counting from
  309. */
  310. static bool test_bit(int nr, const volatile unsigned long *addr);
  311. #endif
  312. #define test_bit(nr, addr) \
  313. (__builtin_constant_p((nr)) \
  314. ? constant_test_bit((nr), (addr)) \
  315. : variable_test_bit((nr), (addr)))
  316. /**
  317. * __ffs - find first set bit in word
  318. * @word: The word to search
  319. *
  320. * Undefined if no bit exists, so code should check against 0 first.
  321. */
  322. static __always_inline unsigned long __ffs(unsigned long word)
  323. {
  324. asm("rep; bsf %1,%0"
  325. : "=r" (word)
  326. : "rm" (word));
  327. return word;
  328. }
  329. /**
  330. * ffz - find first zero bit in word
  331. * @word: The word to search
  332. *
  333. * Undefined if no zero exists, so code should check against ~0UL first.
  334. */
  335. static __always_inline unsigned long ffz(unsigned long word)
  336. {
  337. asm("rep; bsf %1,%0"
  338. : "=r" (word)
  339. : "r" (~word));
  340. return word;
  341. }
  342. /*
  343. * __fls: find last set bit in word
  344. * @word: The word to search
  345. *
  346. * Undefined if no set bit exists, so code should check against 0 first.
  347. */
  348. static __always_inline unsigned long __fls(unsigned long word)
  349. {
  350. asm("bsr %1,%0"
  351. : "=r" (word)
  352. : "rm" (word));
  353. return word;
  354. }
  355. #undef ADDR
  356. #ifdef __KERNEL__
  357. /**
  358. * ffs - find first set bit in word
  359. * @x: the word to search
  360. *
  361. * This is defined the same way as the libc and compiler builtin ffs
  362. * routines, therefore differs in spirit from the other bitops.
  363. *
  364. * ffs(value) returns 0 if value is 0 or the position of the first
  365. * set bit if value is nonzero. The first (least significant) bit
  366. * is at position 1.
  367. */
  368. static __always_inline int ffs(int x)
  369. {
  370. int r;
  371. #ifdef CONFIG_X86_64
  372. /*
  373. * AMD64 says BSFL won't clobber the dest reg if x==0; Intel64 says the
  374. * dest reg is undefined if x==0, but their CPU architect says its
  375. * value is written to set it to the same as before, except that the
  376. * top 32 bits will be cleared.
  377. *
  378. * We cannot do this on 32 bits because at the very least some
  379. * 486 CPUs did not behave this way.
  380. */
  381. asm("bsfl %1,%0"
  382. : "=r" (r)
  383. : "rm" (x), "0" (-1));
  384. #elif defined(CONFIG_X86_CMOV)
  385. asm("bsfl %1,%0\n\t"
  386. "cmovzl %2,%0"
  387. : "=&r" (r) : "rm" (x), "r" (-1));
  388. #else
  389. asm("bsfl %1,%0\n\t"
  390. "jnz 1f\n\t"
  391. "movl $-1,%0\n"
  392. "1:" : "=r" (r) : "rm" (x));
  393. #endif
  394. return r + 1;
  395. }
  396. /**
  397. * fls - find last set bit in word
  398. * @x: the word to search
  399. *
  400. * This is defined in a similar way as the libc and compiler builtin
  401. * ffs, but returns the position of the most significant set bit.
  402. *
  403. * fls(value) returns 0 if value is 0 or the position of the last
  404. * set bit if value is nonzero. The last (most significant) bit is
  405. * at position 32.
  406. */
  407. static __always_inline int fls(int x)
  408. {
  409. int r;
  410. #ifdef CONFIG_X86_64
  411. /*
  412. * AMD64 says BSRL won't clobber the dest reg if x==0; Intel64 says the
  413. * dest reg is undefined if x==0, but their CPU architect says its
  414. * value is written to set it to the same as before, except that the
  415. * top 32 bits will be cleared.
  416. *
  417. * We cannot do this on 32 bits because at the very least some
  418. * 486 CPUs did not behave this way.
  419. */
  420. asm("bsrl %1,%0"
  421. : "=r" (r)
  422. : "rm" (x), "0" (-1));
  423. #elif defined(CONFIG_X86_CMOV)
  424. asm("bsrl %1,%0\n\t"
  425. "cmovzl %2,%0"
  426. : "=&r" (r) : "rm" (x), "rm" (-1));
  427. #else
  428. asm("bsrl %1,%0\n\t"
  429. "jnz 1f\n\t"
  430. "movl $-1,%0\n"
  431. "1:" : "=r" (r) : "rm" (x));
  432. #endif
  433. return r + 1;
  434. }
  435. /**
  436. * fls64 - find last set bit in a 64-bit word
  437. * @x: the word to search
  438. *
  439. * This is defined in a similar way as the libc and compiler builtin
  440. * ffsll, but returns the position of the most significant set bit.
  441. *
  442. * fls64(value) returns 0 if value is 0 or the position of the last
  443. * set bit if value is nonzero. The last (most significant) bit is
  444. * at position 64.
  445. */
  446. #ifdef CONFIG_X86_64
  447. static __always_inline int fls64(__u64 x)
  448. {
  449. int bitpos = -1;
  450. /*
  451. * AMD64 says BSRQ won't clobber the dest reg if x==0; Intel64 says the
  452. * dest reg is undefined if x==0, but their CPU architect says its
  453. * value is written to set it to the same as before.
  454. */
  455. asm("bsrq %1,%q0"
  456. : "+r" (bitpos)
  457. : "rm" (x));
  458. return bitpos + 1;
  459. }
  460. #else
  461. #include <asm-generic/bitops/fls64.h>
  462. #endif
  463. #include <asm-generic/bitops/find.h>
  464. #include <asm-generic/bitops/sched.h>
  465. #include <asm/arch_hweight.h>
  466. #include <asm-generic/bitops/const_hweight.h>
  467. #include <asm-generic/bitops/le.h>
  468. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  469. #endif /* __KERNEL__ */
  470. #endif /* _ASM_X86_BITOPS_H */