word-at-a-time.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2013 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __ASM_WORD_AT_A_TIME_H
  17. #define __ASM_WORD_AT_A_TIME_H
  18. #ifndef __AARCH64EB__
  19. #include <linux/kernel.h>
  20. struct word_at_a_time {
  21. const unsigned long one_bits, high_bits;
  22. };
  23. #define WORD_AT_A_TIME_CONSTANTS { REPEAT_BYTE(0x01), REPEAT_BYTE(0x80) }
  24. static inline unsigned long has_zero(unsigned long a, unsigned long *bits,
  25. const struct word_at_a_time *c)
  26. {
  27. unsigned long mask = ((a - c->one_bits) & ~a) & c->high_bits;
  28. *bits = mask;
  29. return mask;
  30. }
  31. #define prep_zero_mask(a, bits, c) (bits)
  32. static inline unsigned long create_zero_mask(unsigned long bits)
  33. {
  34. bits = (bits - 1) & ~bits;
  35. return bits >> 7;
  36. }
  37. static inline unsigned long find_zero(unsigned long mask)
  38. {
  39. return fls64(mask) >> 3;
  40. }
  41. #define zero_bytemask(mask) (mask)
  42. #else /* __AARCH64EB__ */
  43. #include <asm-generic/word-at-a-time.h>
  44. #endif
  45. /*
  46. * Load an unaligned word from kernel space.
  47. *
  48. * In the (very unlikely) case of the word being a page-crosser
  49. * and the next page not being mapped, take the exception and
  50. * return zeroes in the non-existing part.
  51. */
  52. static inline unsigned long load_unaligned_zeropad(const void *addr)
  53. {
  54. unsigned long ret, offset;
  55. /* Load word from unaligned pointer addr */
  56. asm(
  57. "1: ldr %0, %3\n"
  58. "2:\n"
  59. " .pushsection .fixup,\"ax\"\n"
  60. " .align 2\n"
  61. "3: and %1, %2, #0x7\n"
  62. " bic %2, %2, #0x7\n"
  63. " ldr %0, [%2]\n"
  64. " lsl %1, %1, #0x3\n"
  65. #ifndef __AARCH64EB__
  66. " lsr %0, %0, %1\n"
  67. #else
  68. " lsl %0, %0, %1\n"
  69. #endif
  70. " b 2b\n"
  71. " .popsection\n"
  72. " .pushsection __ex_table,\"a\"\n"
  73. " .align 3\n"
  74. " .quad 1b, 3b\n"
  75. " .popsection"
  76. : "=&r" (ret), "=&r" (offset)
  77. : "r" (addr), "Q" (*(unsigned long *)addr));
  78. return ret;
  79. }
  80. #endif /* __ASM_WORD_AT_A_TIME_H */