bitops.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. #if BITS_PER_LONG == 32
  15. # define _BITOPS_LONG_SHIFT 5
  16. #elif BITS_PER_LONG == 64
  17. # define _BITOPS_LONG_SHIFT 6
  18. #else
  19. # error "Unexpected BITS_PER_LONG"
  20. #endif
  21. #define BIT_64(n) (U64_C(1) << (n))
  22. /*
  23. * These have to be done with inline assembly: that way the bit-setting
  24. * is guaranteed to be atomic. All bit operations return 0 if the bit
  25. * was cleared before the operation and != 0 if it was not.
  26. *
  27. * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
  28. */
  29. #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
  30. /* Technically wrong, but this avoids compilation errors on some gcc
  31. versions. */
  32. #define BITOP_ADDR(x) "=m" (*(volatile long *) (x))
  33. #else
  34. #define BITOP_ADDR(x) "+m" (*(volatile long *) (x))
  35. #endif
  36. #define ADDR BITOP_ADDR(addr)
  37. /*
  38. * We do the locked ops that don't return the old value as
  39. * a mask operation on a byte.
  40. */
  41. #define IS_IMMEDIATE(nr) (__builtin_constant_p(nr))
  42. #define CONST_MASK_ADDR(nr, addr) BITOP_ADDR((void *)(addr) + ((nr)>>3))
  43. #define CONST_MASK(nr) (1 << ((nr) & 7))
  44. /**
  45. * set_bit - Atomically set a bit in memory
  46. * @nr: the bit to set
  47. * @addr: the address to start counting from
  48. *
  49. * This function is atomic and may not be reordered. See __set_bit()
  50. * if you do not require the atomic guarantees.
  51. *
  52. * Note: there are no guarantees that this function will not be reordered
  53. * on non x86 architectures, so if you are writing portable code,
  54. * make sure not to rely on its reordering guarantees.
  55. *
  56. * Note that @nr may be almost arbitrarily large; this function is not
  57. * restricted to acting on a single-word quantity.
  58. */
  59. static __always_inline void
  60. set_bit(long nr, volatile unsigned long *addr)
  61. {
  62. if (IS_IMMEDIATE(nr)) {
  63. asm volatile(LOCK_PREFIX "orb %1,%0"
  64. : CONST_MASK_ADDR(nr, addr)
  65. : "iq" ((u8)CONST_MASK(nr))
  66. : "memory");
  67. } else {
  68. asm volatile(LOCK_PREFIX "bts %1,%0"
  69. : BITOP_ADDR(addr) : "Ir" (nr) : "memory");
  70. }
  71. }
  72. /**
  73. * __set_bit - Set a bit in memory
  74. * @nr: the bit to set
  75. * @addr: the address to start counting from
  76. *
  77. * Unlike set_bit(), this function is non-atomic and may be reordered.
  78. * If it's called on the same region of memory simultaneously, the effect
  79. * may be that only one operation succeeds.
  80. */
  81. static inline void __set_bit(long nr, volatile unsigned long *addr)
  82. {
  83. asm volatile("bts %1,%0" : ADDR : "Ir" (nr) : "memory");
  84. }
  85. /**
  86. * clear_bit - Clears a bit in memory
  87. * @nr: Bit to clear
  88. * @addr: Address to start counting from
  89. *
  90. * clear_bit() is atomic and may not be reordered. However, it does
  91. * not contain a memory barrier, so if it is used for locking purposes,
  92. * you should call smp_mb__before_clear_bit() and/or smp_mb__after_clear_bit()
  93. * in order to ensure changes are visible on other processors.
  94. */
  95. static __always_inline void
  96. clear_bit(long nr, volatile unsigned long *addr)
  97. {
  98. if (IS_IMMEDIATE(nr)) {
  99. asm volatile(LOCK_PREFIX "andb %1,%0"
  100. : CONST_MASK_ADDR(nr, addr)
  101. : "iq" ((u8)~CONST_MASK(nr)));
  102. } else {
  103. asm volatile(LOCK_PREFIX "btr %1,%0"
  104. : BITOP_ADDR(addr)
  105. : "Ir" (nr));
  106. }
  107. }
  108. /*
  109. * clear_bit_unlock - Clears a bit in memory
  110. * @nr: Bit to clear
  111. * @addr: Address to start counting from
  112. *
  113. * clear_bit() is atomic and implies release semantics before the memory
  114. * operation. It can be used for an unlock.
  115. */
  116. static inline void clear_bit_unlock(long nr, volatile unsigned long *addr)
  117. {
  118. barrier();
  119. clear_bit(nr, addr);
  120. }
  121. static inline void __clear_bit(long nr, volatile unsigned long *addr)
  122. {
  123. asm volatile("btr %1,%0" : ADDR : "Ir" (nr));
  124. }
  125. /*
  126. * __clear_bit_unlock - Clears a bit in memory
  127. * @nr: Bit to clear
  128. * @addr: Address to start counting from
  129. *
  130. * __clear_bit() is non-atomic and implies release semantics before the memory
  131. * operation. It can be used for an unlock if no other CPUs can concurrently
  132. * modify other bits in the word.
  133. *
  134. * No memory barrier is required here, because x86 cannot reorder stores past
  135. * older loads. Same principle as spin_unlock.
  136. */
  137. static inline void __clear_bit_unlock(long nr, volatile unsigned long *addr)
  138. {
  139. barrier();
  140. __clear_bit(nr, addr);
  141. }
  142. #define smp_mb__before_clear_bit() barrier()
  143. #define smp_mb__after_clear_bit() barrier()
  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 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 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 inline int test_and_set_bit(long nr, volatile unsigned long *addr)
  187. {
  188. int oldbit;
  189. asm volatile(LOCK_PREFIX "bts %2,%1\n\t"
  190. "sbb %0,%0" : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
  191. return oldbit;
  192. }
  193. /**
  194. * test_and_set_bit_lock - Set a bit and return its old value for lock
  195. * @nr: Bit to set
  196. * @addr: Address to count from
  197. *
  198. * This is the same as test_and_set_bit on x86.
  199. */
  200. static __always_inline int
  201. test_and_set_bit_lock(long nr, volatile unsigned long *addr)
  202. {
  203. return test_and_set_bit(nr, addr);
  204. }
  205. /**
  206. * __test_and_set_bit - Set a bit and return its old value
  207. * @nr: Bit to set
  208. * @addr: Address to count from
  209. *
  210. * This operation is non-atomic and can be reordered.
  211. * If two examples of this operation race, one can appear to succeed
  212. * but actually fail. You must protect multiple accesses with a lock.
  213. */
  214. static inline int __test_and_set_bit(long nr, volatile unsigned long *addr)
  215. {
  216. int oldbit;
  217. asm("bts %2,%1\n\t"
  218. "sbb %0,%0"
  219. : "=r" (oldbit), ADDR
  220. : "Ir" (nr));
  221. return oldbit;
  222. }
  223. /**
  224. * test_and_clear_bit - Clear a bit and return its old value
  225. * @nr: Bit to clear
  226. * @addr: Address to count from
  227. *
  228. * This operation is atomic and cannot be reordered.
  229. * It also implies a memory barrier.
  230. */
  231. static inline int test_and_clear_bit(long nr, volatile unsigned long *addr)
  232. {
  233. int oldbit;
  234. asm volatile(LOCK_PREFIX "btr %2,%1\n\t"
  235. "sbb %0,%0"
  236. : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
  237. return oldbit;
  238. }
  239. /**
  240. * __test_and_clear_bit - Clear a bit and return its old value
  241. * @nr: Bit to clear
  242. * @addr: Address to count from
  243. *
  244. * This operation is non-atomic and can be reordered.
  245. * If two examples of this operation race, one can appear to succeed
  246. * but actually fail. You must protect multiple accesses with a lock.
  247. *
  248. * Note: the operation is performed atomically with respect to
  249. * the local CPU, but not other CPUs. Portable code should not
  250. * rely on this behaviour.
  251. * KVM relies on this behaviour on x86 for modifying memory that is also
  252. * accessed from a hypervisor on the same CPU if running in a VM: don't change
  253. * this without also updating arch/x86/kernel/kvm.c
  254. */
  255. static inline int __test_and_clear_bit(long nr, volatile unsigned long *addr)
  256. {
  257. int oldbit;
  258. asm volatile("btr %2,%1\n\t"
  259. "sbb %0,%0"
  260. : "=r" (oldbit), ADDR
  261. : "Ir" (nr));
  262. return oldbit;
  263. }
  264. /* WARNING: non atomic and it can be reordered! */
  265. static inline int __test_and_change_bit(long nr, volatile unsigned long *addr)
  266. {
  267. int oldbit;
  268. asm volatile("btc %2,%1\n\t"
  269. "sbb %0,%0"
  270. : "=r" (oldbit), ADDR
  271. : "Ir" (nr) : "memory");
  272. return oldbit;
  273. }
  274. /**
  275. * test_and_change_bit - Change a bit and return its old value
  276. * @nr: Bit to change
  277. * @addr: Address to count from
  278. *
  279. * This operation is atomic and cannot be reordered.
  280. * It also implies a memory barrier.
  281. */
  282. static inline int test_and_change_bit(long nr, volatile unsigned long *addr)
  283. {
  284. int oldbit;
  285. asm volatile(LOCK_PREFIX "btc %2,%1\n\t"
  286. "sbb %0,%0"
  287. : "=r" (oldbit), ADDR : "Ir" (nr) : "memory");
  288. return oldbit;
  289. }
  290. static __always_inline int 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 inline int variable_test_bit(long nr, volatile const unsigned long *addr)
  296. {
  297. int oldbit;
  298. asm volatile("bt %2,%1\n\t"
  299. "sbb %0,%0"
  300. : "=r" (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 int 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 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 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 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 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 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. #define ARCH_HAS_FAST_MULTIPLIER 1
  466. #include <asm/arch_hweight.h>
  467. #include <asm-generic/bitops/const_hweight.h>
  468. #include <asm-generic/bitops/le.h>
  469. #include <asm-generic/bitops/ext2-atomic-setbit.h>
  470. #endif /* __KERNEL__ */
  471. #endif /* _ASM_X86_BITOPS_H */