string_32.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #ifdef CONFIG_X86_USE_3DNOW
  134. #include <asm/mmx.h>
  135. /*
  136. * This CPU favours 3DNow strongly (eg AMD Athlon)
  137. */
  138. static inline void *__constant_memcpy3d(void *to, const void *from, size_t len)
  139. {
  140. if (len < 512)
  141. return __constant_memcpy(to, from, len);
  142. return _mmx_memcpy(to, from, len);
  143. }
  144. static inline void *__memcpy3d(void *to, const void *from, size_t len)
  145. {
  146. if (len < 512)
  147. return __memcpy(to, from, len);
  148. return _mmx_memcpy(to, from, len);
  149. }
  150. #define memcpy(t, f, n) \
  151. (__builtin_constant_p((n)) \
  152. ? __constant_memcpy3d((t), (f), (n)) \
  153. : __memcpy3d((t), (f), (n)))
  154. #else
  155. /*
  156. * No 3D Now!
  157. */
  158. #if (__GNUC__ >= 4)
  159. #define memcpy(t, f, n) __builtin_memcpy(t, f, n)
  160. #else
  161. #define memcpy(t, f, n) \
  162. (__builtin_constant_p((n)) \
  163. ? __constant_memcpy((t), (f), (n)) \
  164. : __memcpy((t), (f), (n)))
  165. #endif
  166. #endif
  167. #endif /* !CONFIG_FORTIFY_SOURCE */
  168. #define __HAVE_ARCH_MEMMOVE
  169. void *memmove(void *dest, const void *src, size_t n);
  170. extern int memcmp(const void *, const void *, size_t);
  171. #ifndef CONFIG_FORTIFY_SOURCE
  172. #define memcmp __builtin_memcmp
  173. #endif
  174. #define __HAVE_ARCH_MEMCHR
  175. extern void *memchr(const void *cs, int c, size_t count);
  176. static inline void *__memset_generic(void *s, char c, size_t count)
  177. {
  178. int d0, d1;
  179. asm volatile("rep\n\t"
  180. "stosb"
  181. : "=&c" (d0), "=&D" (d1)
  182. : "a" (c), "1" (s), "0" (count)
  183. : "memory");
  184. return s;
  185. }
  186. /* we might want to write optimized versions of these later */
  187. #define __constant_count_memset(s, c, count) __memset_generic((s), (c), (count))
  188. /*
  189. * memset(x, 0, y) is a reasonably common thing to do, so we want to fill
  190. * things 32 bits at a time even when we don't know the size of the
  191. * area at compile-time..
  192. */
  193. static __always_inline
  194. void *__constant_c_memset(void *s, unsigned long c, size_t count)
  195. {
  196. int d0, d1;
  197. asm volatile("rep ; stosl\n\t"
  198. "testb $2,%b3\n\t"
  199. "je 1f\n\t"
  200. "stosw\n"
  201. "1:\ttestb $1,%b3\n\t"
  202. "je 2f\n\t"
  203. "stosb\n"
  204. "2:"
  205. : "=&c" (d0), "=&D" (d1)
  206. : "a" (c), "q" (count), "0" (count/4), "1" ((long)s)
  207. : "memory");
  208. return s;
  209. }
  210. /* Added by Gertjan van Wingerde to make minix and sysv module work */
  211. #define __HAVE_ARCH_STRNLEN
  212. extern size_t strnlen(const char *s, size_t count);
  213. /* end of additional stuff */
  214. #define __HAVE_ARCH_STRSTR
  215. extern char *strstr(const char *cs, const char *ct);
  216. /*
  217. * This looks horribly ugly, but the compiler can optimize it totally,
  218. * as we by now know that both pattern and count is constant..
  219. */
  220. static __always_inline
  221. void *__constant_c_and_count_memset(void *s, unsigned long pattern,
  222. size_t count)
  223. {
  224. switch (count) {
  225. case 0:
  226. return s;
  227. case 1:
  228. *(unsigned char *)s = pattern & 0xff;
  229. return s;
  230. case 2:
  231. *(unsigned short *)s = pattern & 0xffff;
  232. return s;
  233. case 3:
  234. *(unsigned short *)s = pattern & 0xffff;
  235. *((unsigned char *)s + 2) = pattern & 0xff;
  236. return s;
  237. case 4:
  238. *(unsigned long *)s = pattern;
  239. return s;
  240. }
  241. #define COMMON(x) \
  242. asm volatile("rep ; stosl" \
  243. x \
  244. : "=&c" (d0), "=&D" (d1) \
  245. : "a" (eax), "0" (count/4), "1" ((long)s) \
  246. : "memory")
  247. {
  248. int d0, d1;
  249. #if __GNUC__ == 4 && __GNUC_MINOR__ == 0
  250. /* Workaround for broken gcc 4.0 */
  251. register unsigned long eax asm("%eax") = pattern;
  252. #else
  253. unsigned long eax = pattern;
  254. #endif
  255. switch (count % 4) {
  256. case 0:
  257. COMMON("");
  258. return s;
  259. case 1:
  260. COMMON("\n\tstosb");
  261. return s;
  262. case 2:
  263. COMMON("\n\tstosw");
  264. return s;
  265. default:
  266. COMMON("\n\tstosw\n\tstosb");
  267. return s;
  268. }
  269. }
  270. #undef COMMON
  271. }
  272. #define __constant_c_x_memset(s, c, count) \
  273. (__builtin_constant_p(count) \
  274. ? __constant_c_and_count_memset((s), (c), (count)) \
  275. : __constant_c_memset((s), (c), (count)))
  276. #define __memset(s, c, count) \
  277. (__builtin_constant_p(count) \
  278. ? __constant_count_memset((s), (c), (count)) \
  279. : __memset_generic((s), (c), (count)))
  280. #define __HAVE_ARCH_MEMSET
  281. extern void *memset(void *, int, size_t);
  282. #ifndef CONFIG_FORTIFY_SOURCE
  283. #if (__GNUC__ >= 4)
  284. #define memset(s, c, count) __builtin_memset(s, c, count)
  285. #else
  286. #define memset(s, c, count) \
  287. (__builtin_constant_p(c) \
  288. ? __constant_c_x_memset((s), (0x01010101UL * (unsigned char)(c)), \
  289. (count)) \
  290. : __memset((s), (c), (count)))
  291. #endif
  292. #endif /* !CONFIG_FORTIFY_SOURCE */
  293. #define __HAVE_ARCH_MEMSET16
  294. static inline void *memset16(uint16_t *s, uint16_t v, size_t n)
  295. {
  296. int d0, d1;
  297. asm volatile("rep\n\t"
  298. "stosw"
  299. : "=&c" (d0), "=&D" (d1)
  300. : "a" (v), "1" (s), "0" (n)
  301. : "memory");
  302. return s;
  303. }
  304. #define __HAVE_ARCH_MEMSET32
  305. static inline void *memset32(uint32_t *s, uint32_t v, size_t n)
  306. {
  307. int d0, d1;
  308. asm volatile("rep\n\t"
  309. "stosl"
  310. : "=&c" (d0), "=&D" (d1)
  311. : "a" (v), "1" (s), "0" (n)
  312. : "memory");
  313. return s;
  314. }
  315. /*
  316. * find the first occurrence of byte 'c', or 1 past the area if none
  317. */
  318. #define __HAVE_ARCH_MEMSCAN
  319. extern void *memscan(void *addr, int c, size_t size);
  320. #endif /* __KERNEL__ */
  321. #endif /* _ASM_X86_STRING_32_H */