bitops.h 13 KB

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