string_32.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_STRING_32_H
  3. #define _ASM_X86_STRING_32_H
  4. #ifdef __KERNEL__
  5. /* Let gcc decide whether to inline or use the out of line functions */
  6. #define __HAVE_ARCH_STRCPY
  7. extern char *strcpy(char *dest, const char *src);
  8. #define __HAVE_ARCH_STRNCPY
  9. extern char *strncpy(char *dest, const char *src, size_t count);
  10. #define __HAVE_ARCH_STRCAT
  11. extern char *strcat(char *dest, const char *src);
  12. #define __HAVE_ARCH_STRNCAT
  13. extern char *strncat(char *dest, const char *src, size_t count);
  14. #define __HAVE_ARCH_STRCMP
  15. extern int strcmp(const char *cs, const char *ct);
  16. #define __HAVE_ARCH_STRNCMP
  17. extern int strncmp(const char *cs, const char *ct, size_t count);
  18. #define __HAVE_ARCH_STRCHR
  19. extern char *strchr(const char *s, int c);
  20. #define __HAVE_ARCH_STRLEN
  21. extern size_t strlen(const char *s);
  22. static __always_inline void *__memcpy(void *to, const void *from, size_t n)
  23. {
  24. int d0, d1, d2;
  25. asm volatile("rep ; movsl\n\t"
  26. "movl %4,%%ecx\n\t"
  27. "andl $3,%%ecx\n\t"
  28. "jz 1f\n\t"
  29. "rep ; movsb\n\t"
  30. "1:"
  31. : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  32. : "0" (n / 4), "g" (n), "1" ((long)to), "2" ((long)from)
  33. : "memory");
  34. return to;
  35. }
  36. /*
  37. * This looks ugly, but the compiler can optimize it totally,
  38. * as the count is constant.
  39. */
  40. static __always_inline void *__constant_memcpy(void *to, const void *from,
  41. size_t n)
  42. {
  43. long esi, edi;
  44. if (!n)
  45. return to;
  46. switch (n) {
  47. case 1:
  48. *(char *)to = *(char *)from;
  49. return to;
  50. case 2:
  51. *(short *)to = *(short *)from;
  52. return to;
  53. case 4:
  54. *(int *)to = *(int *)from;
  55. return to;
  56. case 3:
  57. *(short *)to = *(short *)from;
  58. *((char *)to + 2) = *((char *)from + 2);
  59. return to;
  60. case 5:
  61. *(int *)to = *(int *)from;
  62. *((char *)to + 4) = *((char *)from + 4);
  63. return to;
  64. case 6:
  65. *(int *)to = *(int *)from;
  66. *((short *)to + 2) = *((short *)from + 2);
  67. return to;
  68. case 8:
  69. *(int *)to = *(int *)from;
  70. *((int *)to + 1) = *((int *)from + 1);
  71. return to;
  72. }
  73. esi = (long)from;
  74. edi = (long)to;
  75. if (n >= 5 * 4) {
  76. /* large block: use rep prefix */
  77. int ecx;
  78. asm volatile("rep ; movsl"
  79. : "=&c" (ecx), "=&D" (edi), "=&S" (esi)
  80. : "0" (n / 4), "1" (edi), "2" (esi)
  81. : "memory"
  82. );
  83. } else {
  84. /* small block: don't clobber ecx + smaller code */
  85. if (n >= 4 * 4)
  86. asm volatile("movsl"
  87. : "=&D"(edi), "=&S"(esi)
  88. : "0"(edi), "1"(esi)
  89. : "memory");
  90. if (n >= 3 * 4)
  91. asm volatile("movsl"
  92. : "=&D"(edi), "=&S"(esi)
  93. : "0"(edi), "1"(esi)
  94. : "memory");
  95. if (n >= 2 * 4)
  96. asm volatile("movsl"
  97. : "=&D"(edi), "=&S"(esi)
  98. : "0"(edi), "1"(esi)
  99. : "memory");
  100. if (n >= 1 * 4)
  101. asm volatile("movsl"
  102. : "=&D"(edi), "=&S"(esi)
  103. : "0"(edi), "1"(esi)
  104. : "memory");
  105. }
  106. switch (n % 4) {
  107. /* tail */
  108. case 0:
  109. return to;
  110. case 1:
  111. asm volatile("movsb"
  112. : "=&D"(edi), "=&S"(esi)
  113. : "0"(edi), "1"(esi)
  114. : "memory");
  115. return to;
  116. case 2:
  117. asm volatile("movsw"
  118. : "=&D"(edi), "=&S"(esi)
  119. : "0"(edi), "1"(esi)
  120. : "memory");
  121. return to;
  122. default:
  123. asm volatile("movsw\n\tmovsb"
  124. : "=&D"(edi), "=&S"(esi)
  125. : "0"(edi), "1"(esi)
  126. : "memory");
  127. return to;
  128. }
  129. }
  130. #define __HAVE_ARCH_MEMCPY
  131. extern void *memcpy(void *, const void *, size_t);
  132. #ifndef CONFIG_FORTIFY_SOURCE
  133. #define memcpy(t, f, n) __builtin_memcpy(t, f, n)
  134. #endif /* !CONFIG_FORTIFY_SOURCE */
  135. #define __HAVE_ARCH_MEMMOVE
  136. void *memmove(void *dest, const void *src, size_t n);
  137. extern int memcmp(const void *, const void *, size_t);
  138. #ifndef CONFIG_FORTIFY_SOURCE
  139. #define memcmp __builtin_memcmp
  140. #endif
  141. #define __HAVE_ARCH_MEMCHR
  142. extern void *memchr(const void *cs, int c, size_t count);
  143. static inline void *__memset_generic(void *s, char c, size_t count)
  144. {
  145. int d0, d1;
  146. asm volatile("rep\n\t"
  147. "stosb"
  148. : "=&c" (d0), "=&D" (d1)
  149. : "a" (c), "1" (s), "0" (count)
  150. : "memory");
  151. return s;
  152. }
  153. /* we might want to write optimized versions of these later */
  154. #define __constant_count_memset(s, c, count) __memset_generic((s), (c), (count))
  155. /* Added by Gertjan van Wingerde to make minix and sysv module work */
  156. #define __HAVE_ARCH_STRNLEN
  157. extern size_t strnlen(const char *s, size_t count);
  158. /* end of additional stuff */
  159. #define __HAVE_ARCH_STRSTR
  160. extern char *strstr(const char *cs, const char *ct);
  161. #define __memset(s, c, count) \
  162. (__builtin_constant_p(count) \
  163. ? __constant_count_memset((s), (c), (count)) \
  164. : __memset_generic((s), (c), (count)))
  165. #define __HAVE_ARCH_MEMSET
  166. extern void *memset(void *, int, size_t);
  167. #ifndef CONFIG_FORTIFY_SOURCE
  168. #define memset(s, c, count) __builtin_memset(s, c, count)
  169. #endif /* !CONFIG_FORTIFY_SOURCE */
  170. #define __HAVE_ARCH_MEMSET16
  171. static inline void *memset16(uint16_t *s, uint16_t v, size_t n)
  172. {
  173. int d0, d1;
  174. asm volatile("rep\n\t"
  175. "stosw"
  176. : "=&c" (d0), "=&D" (d1)
  177. : "a" (v), "1" (s), "0" (n)
  178. : "memory");
  179. return s;
  180. }
  181. #define __HAVE_ARCH_MEMSET32
  182. static inline void *memset32(uint32_t *s, uint32_t v, size_t n)
  183. {
  184. int d0, d1;
  185. asm volatile("rep\n\t"
  186. "stosl"
  187. : "=&c" (d0), "=&D" (d1)
  188. : "a" (v), "1" (s), "0" (n)
  189. : "memory");
  190. return s;
  191. }
  192. /*
  193. * find the first occurrence of byte 'c', or 1 past the area if none
  194. */
  195. #define __HAVE_ARCH_MEMSCAN
  196. extern void *memscan(void *addr, int c, size_t size);
  197. #endif /* __KERNEL__ */
  198. #endif /* _ASM_X86_STRING_32_H */