string.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Optimized string functions
  4. *
  5. * S390 version
  6. * Copyright IBM Corp. 2004
  7. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  8. */
  9. #define IN_ARCH_STRING_C 1
  10. #ifndef __NO_FORTIFY
  11. # define __NO_FORTIFY
  12. #endif
  13. #include <linux/types.h>
  14. #include <linux/string.h>
  15. #include <linux/export.h>
  16. #include <asm/asm.h>
  17. /*
  18. * Helper functions to find the end of a string
  19. */
  20. static inline char *__strend(const char *s)
  21. {
  22. unsigned long e = 0;
  23. asm volatile(
  24. " lghi 0,0\n"
  25. "0: srst %[e],%[s]\n"
  26. " jo 0b"
  27. : [e] "+&a" (e), [s] "+&a" (s)
  28. :
  29. : "cc", "memory", "0");
  30. return (char *)e;
  31. }
  32. static inline char *__strnend(const char *s, size_t n)
  33. {
  34. const char *p = s + n;
  35. asm volatile(
  36. " lghi 0,0\n"
  37. "0: srst %[p],%[s]\n"
  38. " jo 0b"
  39. : [p] "+&d" (p), [s] "+&a" (s)
  40. :
  41. : "cc", "memory", "0");
  42. return (char *)p;
  43. }
  44. /**
  45. * strlen - Find the length of a string
  46. * @s: The string to be sized
  47. *
  48. * returns the length of @s
  49. */
  50. #ifdef __HAVE_ARCH_STRLEN
  51. size_t strlen(const char *s)
  52. {
  53. return __strend(s) - s;
  54. }
  55. EXPORT_SYMBOL(strlen);
  56. #endif
  57. /**
  58. * strnlen - Find the length of a length-limited string
  59. * @s: The string to be sized
  60. * @n: The maximum number of bytes to search
  61. *
  62. * returns the minimum of the length of @s and @n
  63. */
  64. #ifdef __HAVE_ARCH_STRNLEN
  65. size_t strnlen(const char *s, size_t n)
  66. {
  67. return __strnend(s, n) - s;
  68. }
  69. EXPORT_SYMBOL(strnlen);
  70. #endif
  71. /**
  72. * strcat - Append one %NUL-terminated string to another
  73. * @dest: The string to be appended to
  74. * @src: The string to append to it
  75. *
  76. * returns a pointer to @dest
  77. */
  78. #ifdef __HAVE_ARCH_STRCAT
  79. char *strcat(char *dest, const char *src)
  80. {
  81. unsigned long dummy = 0;
  82. char *ret = dest;
  83. asm volatile(
  84. " lghi 0,0\n"
  85. "0: srst %[dummy],%[dest]\n"
  86. " jo 0b\n"
  87. "1: mvst %[dummy],%[src]\n"
  88. " jo 1b"
  89. : [dummy] "+&a" (dummy), [dest] "+&a" (dest), [src] "+&a" (src)
  90. :
  91. : "cc", "memory", "0");
  92. return ret;
  93. }
  94. EXPORT_SYMBOL(strcat);
  95. #endif
  96. /**
  97. * strlcat - Append a length-limited, %NUL-terminated string to another
  98. * @dest: The string to be appended to
  99. * @src: The string to append to it
  100. * @n: The size of the destination buffer.
  101. */
  102. #ifdef __HAVE_ARCH_STRLCAT
  103. size_t strlcat(char *dest, const char *src, size_t n)
  104. {
  105. size_t dsize = __strend(dest) - dest;
  106. size_t len = __strend(src) - src;
  107. size_t res = dsize + len;
  108. if (dsize < n) {
  109. dest += dsize;
  110. n -= dsize;
  111. if (len >= n)
  112. len = n - 1;
  113. dest[len] = '\0';
  114. memcpy(dest, src, len);
  115. }
  116. return res;
  117. }
  118. EXPORT_SYMBOL(strlcat);
  119. #endif
  120. /**
  121. * strncat - Append a length-limited, %NUL-terminated string to another
  122. * @dest: The string to be appended to
  123. * @src: The string to append to it
  124. * @n: The maximum numbers of bytes to copy
  125. *
  126. * returns a pointer to @dest
  127. */
  128. #ifdef __HAVE_ARCH_STRNCAT
  129. char *strncat(char *dest, const char *src, size_t n)
  130. {
  131. size_t len = __strnend(src, n) - src;
  132. char *p = __strend(dest);
  133. p[len] = '\0';
  134. memcpy(p, src, len);
  135. return dest;
  136. }
  137. EXPORT_SYMBOL(strncat);
  138. #endif
  139. /**
  140. * strcmp - Compare two strings
  141. * @s1: One string
  142. * @s2: Another string
  143. *
  144. * returns 0 if @s1 and @s2 are equal,
  145. * < 0 if @s1 is less than @s2
  146. * > 0 if @s1 is greater than @s2
  147. */
  148. #ifdef __HAVE_ARCH_STRCMP
  149. int strcmp(const char *s1, const char *s2)
  150. {
  151. int ret = 0;
  152. asm volatile(
  153. " lghi 0,0\n"
  154. "0: clst %[s1],%[s2]\n"
  155. " jo 0b\n"
  156. " je 1f\n"
  157. " ic %[ret],0(%[s1])\n"
  158. " ic 0,0(%[s2])\n"
  159. " sr %[ret],0\n"
  160. "1:"
  161. : [ret] "+&d" (ret), [s1] "+&a" (s1), [s2] "+&a" (s2)
  162. :
  163. : "cc", "memory", "0");
  164. return ret;
  165. }
  166. EXPORT_SYMBOL(strcmp);
  167. #endif
  168. static inline int clcle(const char *s1, unsigned long l1,
  169. const char *s2, unsigned long l2)
  170. {
  171. union register_pair r1 = { .even = (unsigned long)s1, .odd = l1, };
  172. union register_pair r3 = { .even = (unsigned long)s2, .odd = l2, };
  173. int cc;
  174. asm volatile(
  175. "0: clcle %[r1],%[r3],0\n"
  176. " jo 0b\n"
  177. CC_IPM(cc)
  178. : CC_OUT(cc, cc), [r1] "+d" (r1.pair), [r3] "+d" (r3.pair)
  179. :
  180. : CC_CLOBBER_LIST("memory"));
  181. return CC_TRANSFORM(cc);
  182. }
  183. /**
  184. * strstr - Find the first substring in a %NUL terminated string
  185. * @s1: The string to be searched
  186. * @s2: The string to search for
  187. */
  188. #ifdef __HAVE_ARCH_STRSTR
  189. char *strstr(const char *s1, const char *s2)
  190. {
  191. int l1, l2;
  192. l2 = __strend(s2) - s2;
  193. if (!l2)
  194. return (char *) s1;
  195. l1 = __strend(s1) - s1;
  196. while (l1-- >= l2) {
  197. int cc;
  198. cc = clcle(s1, l2, s2, l2);
  199. if (!cc)
  200. return (char *) s1;
  201. s1++;
  202. }
  203. return NULL;
  204. }
  205. EXPORT_SYMBOL(strstr);
  206. #endif
  207. /**
  208. * memchr - Find a character in an area of memory.
  209. * @s: The memory area
  210. * @c: The byte to search for
  211. * @n: The size of the area.
  212. *
  213. * returns the address of the first occurrence of @c, or %NULL
  214. * if @c is not found
  215. */
  216. #ifdef __HAVE_ARCH_MEMCHR
  217. void *memchr(const void *s, int c, size_t n)
  218. {
  219. const void *ret = s + n;
  220. asm volatile(
  221. " lgr 0,%[c]\n"
  222. "0: srst %[ret],%[s]\n"
  223. " jo 0b\n"
  224. " jl 1f\n"
  225. " la %[ret],0\n"
  226. "1:"
  227. : [ret] "+&a" (ret), [s] "+&a" (s)
  228. : [c] "d" (c)
  229. : "cc", "memory", "0");
  230. return (void *) ret;
  231. }
  232. EXPORT_SYMBOL(memchr);
  233. #endif
  234. /**
  235. * memcmp - Compare two areas of memory
  236. * @s1: One area of memory
  237. * @s2: Another area of memory
  238. * @n: The size of the area.
  239. */
  240. #ifdef __HAVE_ARCH_MEMCMP
  241. int memcmp(const void *s1, const void *s2, size_t n)
  242. {
  243. int ret;
  244. ret = clcle(s1, n, s2, n);
  245. if (ret)
  246. ret = ret == 1 ? -1 : 1;
  247. return ret;
  248. }
  249. EXPORT_SYMBOL(memcmp);
  250. #endif
  251. /**
  252. * memscan - Find a character in an area of memory.
  253. * @s: The memory area
  254. * @c: The byte to search for
  255. * @n: The size of the area.
  256. *
  257. * returns the address of the first occurrence of @c, or 1 byte past
  258. * the area if @c is not found
  259. */
  260. #ifdef __HAVE_ARCH_MEMSCAN
  261. void *memscan(void *s, int c, size_t n)
  262. {
  263. const void *ret = s + n;
  264. asm volatile(
  265. " lgr 0,%[c]\n"
  266. "0: srst %[ret],%[s]\n"
  267. " jo 0b"
  268. : [ret] "+&a" (ret), [s] "+&a" (s)
  269. : [c] "d" (c)
  270. : "cc", "memory", "0");
  271. return (void *)ret;
  272. }
  273. EXPORT_SYMBOL(memscan);
  274. #endif