string.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 ([email protected])
  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. /*
  17. * Helper functions to find the end of a string
  18. */
  19. static inline char *__strend(const char *s)
  20. {
  21. unsigned long e = 0;
  22. asm volatile(
  23. " lghi 0,0\n"
  24. "0: srst %[e],%[s]\n"
  25. " jo 0b\n"
  26. : [e] "+&a" (e), [s] "+&a" (s)
  27. :
  28. : "cc", "memory", "0");
  29. return (char *)e;
  30. }
  31. static inline char *__strnend(const char *s, size_t n)
  32. {
  33. const char *p = s + n;
  34. asm volatile(
  35. " lghi 0,0\n"
  36. "0: srst %[p],%[s]\n"
  37. " jo 0b\n"
  38. : [p] "+&d" (p), [s] "+&a" (s)
  39. :
  40. : "cc", "memory", "0");
  41. return (char *)p;
  42. }
  43. /**
  44. * strlen - Find the length of a string
  45. * @s: The string to be sized
  46. *
  47. * returns the length of @s
  48. */
  49. #ifdef __HAVE_ARCH_STRLEN
  50. size_t strlen(const char *s)
  51. {
  52. return __strend(s) - s;
  53. }
  54. EXPORT_SYMBOL(strlen);
  55. #endif
  56. /**
  57. * strnlen - Find the length of a length-limited string
  58. * @s: The string to be sized
  59. * @n: The maximum number of bytes to search
  60. *
  61. * returns the minimum of the length of @s and @n
  62. */
  63. #ifdef __HAVE_ARCH_STRNLEN
  64. size_t strnlen(const char *s, size_t n)
  65. {
  66. return __strnend(s, n) - s;
  67. }
  68. EXPORT_SYMBOL(strnlen);
  69. #endif
  70. /**
  71. * strcpy - Copy a %NUL terminated string
  72. * @dest: Where to copy the string to
  73. * @src: Where to copy the string from
  74. *
  75. * returns a pointer to @dest
  76. */
  77. #ifdef __HAVE_ARCH_STRCPY
  78. char *strcpy(char *dest, const char *src)
  79. {
  80. char *ret = dest;
  81. asm volatile(
  82. " lghi 0,0\n"
  83. "0: mvst %[dest],%[src]\n"
  84. " jo 0b\n"
  85. : [dest] "+&a" (dest), [src] "+&a" (src)
  86. :
  87. : "cc", "memory", "0");
  88. return ret;
  89. }
  90. EXPORT_SYMBOL(strcpy);
  91. #endif
  92. /**
  93. * strncpy - Copy a length-limited, %NUL-terminated string
  94. * @dest: Where to copy the string to
  95. * @src: Where to copy the string from
  96. * @n: The maximum number of bytes to copy
  97. *
  98. * The result is not %NUL-terminated if the source exceeds
  99. * @n bytes.
  100. */
  101. #ifdef __HAVE_ARCH_STRNCPY
  102. char *strncpy(char *dest, const char *src, size_t n)
  103. {
  104. size_t len = __strnend(src, n) - src;
  105. memset(dest + len, 0, n - len);
  106. memcpy(dest, src, len);
  107. return dest;
  108. }
  109. EXPORT_SYMBOL(strncpy);
  110. #endif
  111. /**
  112. * strcat - Append one %NUL-terminated string to another
  113. * @dest: The string to be appended to
  114. * @src: The string to append to it
  115. *
  116. * returns a pointer to @dest
  117. */
  118. #ifdef __HAVE_ARCH_STRCAT
  119. char *strcat(char *dest, const char *src)
  120. {
  121. unsigned long dummy = 0;
  122. char *ret = dest;
  123. asm volatile(
  124. " lghi 0,0\n"
  125. "0: srst %[dummy],%[dest]\n"
  126. " jo 0b\n"
  127. "1: mvst %[dummy],%[src]\n"
  128. " jo 1b\n"
  129. : [dummy] "+&a" (dummy), [dest] "+&a" (dest), [src] "+&a" (src)
  130. :
  131. : "cc", "memory", "0");
  132. return ret;
  133. }
  134. EXPORT_SYMBOL(strcat);
  135. #endif
  136. /**
  137. * strlcat - Append a length-limited, %NUL-terminated string to another
  138. * @dest: The string to be appended to
  139. * @src: The string to append to it
  140. * @n: The size of the destination buffer.
  141. */
  142. #ifdef __HAVE_ARCH_STRLCAT
  143. size_t strlcat(char *dest, const char *src, size_t n)
  144. {
  145. size_t dsize = __strend(dest) - dest;
  146. size_t len = __strend(src) - src;
  147. size_t res = dsize + len;
  148. if (dsize < n) {
  149. dest += dsize;
  150. n -= dsize;
  151. if (len >= n)
  152. len = n - 1;
  153. dest[len] = '\0';
  154. memcpy(dest, src, len);
  155. }
  156. return res;
  157. }
  158. EXPORT_SYMBOL(strlcat);
  159. #endif
  160. /**
  161. * strncat - Append a length-limited, %NUL-terminated string to another
  162. * @dest: The string to be appended to
  163. * @src: The string to append to it
  164. * @n: The maximum numbers of bytes to copy
  165. *
  166. * returns a pointer to @dest
  167. *
  168. * Note that in contrast to strncpy, strncat ensures the result is
  169. * terminated.
  170. */
  171. #ifdef __HAVE_ARCH_STRNCAT
  172. char *strncat(char *dest, const char *src, size_t n)
  173. {
  174. size_t len = __strnend(src, n) - src;
  175. char *p = __strend(dest);
  176. p[len] = '\0';
  177. memcpy(p, src, len);
  178. return dest;
  179. }
  180. EXPORT_SYMBOL(strncat);
  181. #endif
  182. /**
  183. * strcmp - Compare two strings
  184. * @s1: One string
  185. * @s2: Another string
  186. *
  187. * returns 0 if @s1 and @s2 are equal,
  188. * < 0 if @s1 is less than @s2
  189. * > 0 if @s1 is greater than @s2
  190. */
  191. #ifdef __HAVE_ARCH_STRCMP
  192. int strcmp(const char *s1, const char *s2)
  193. {
  194. int ret = 0;
  195. asm volatile(
  196. " lghi 0,0\n"
  197. "0: clst %[s1],%[s2]\n"
  198. " jo 0b\n"
  199. " je 1f\n"
  200. " ic %[ret],0(%[s1])\n"
  201. " ic 0,0(%[s2])\n"
  202. " sr %[ret],0\n"
  203. "1:"
  204. : [ret] "+&d" (ret), [s1] "+&a" (s1), [s2] "+&a" (s2)
  205. :
  206. : "cc", "memory", "0");
  207. return ret;
  208. }
  209. EXPORT_SYMBOL(strcmp);
  210. #endif
  211. static inline int clcle(const char *s1, unsigned long l1,
  212. const char *s2, unsigned long l2)
  213. {
  214. union register_pair r1 = { .even = (unsigned long)s1, .odd = l1, };
  215. union register_pair r3 = { .even = (unsigned long)s2, .odd = l2, };
  216. int cc;
  217. asm volatile(
  218. "0: clcle %[r1],%[r3],0\n"
  219. " jo 0b\n"
  220. " ipm %[cc]\n"
  221. " srl %[cc],28\n"
  222. : [cc] "=&d" (cc), [r1] "+&d" (r1.pair), [r3] "+&d" (r3.pair)
  223. :
  224. : "cc", "memory");
  225. return cc;
  226. }
  227. /**
  228. * strstr - Find the first substring in a %NUL terminated string
  229. * @s1: The string to be searched
  230. * @s2: The string to search for
  231. */
  232. #ifdef __HAVE_ARCH_STRSTR
  233. char *strstr(const char *s1, const char *s2)
  234. {
  235. int l1, l2;
  236. l2 = __strend(s2) - s2;
  237. if (!l2)
  238. return (char *) s1;
  239. l1 = __strend(s1) - s1;
  240. while (l1-- >= l2) {
  241. int cc;
  242. cc = clcle(s1, l2, s2, l2);
  243. if (!cc)
  244. return (char *) s1;
  245. s1++;
  246. }
  247. return NULL;
  248. }
  249. EXPORT_SYMBOL(strstr);
  250. #endif
  251. /**
  252. * memchr - 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 %NULL
  258. * if @c is not found
  259. */
  260. #ifdef __HAVE_ARCH_MEMCHR
  261. void *memchr(const 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\n"
  268. " jl 1f\n"
  269. " la %[ret],0\n"
  270. "1:"
  271. : [ret] "+&a" (ret), [s] "+&a" (s)
  272. : [c] "d" (c)
  273. : "cc", "memory", "0");
  274. return (void *) ret;
  275. }
  276. EXPORT_SYMBOL(memchr);
  277. #endif
  278. /**
  279. * memcmp - Compare two areas of memory
  280. * @s1: One area of memory
  281. * @s2: Another area of memory
  282. * @n: The size of the area.
  283. */
  284. #ifdef __HAVE_ARCH_MEMCMP
  285. int memcmp(const void *s1, const void *s2, size_t n)
  286. {
  287. int ret;
  288. ret = clcle(s1, n, s2, n);
  289. if (ret)
  290. ret = ret == 1 ? -1 : 1;
  291. return ret;
  292. }
  293. EXPORT_SYMBOL(memcmp);
  294. #endif
  295. /**
  296. * memscan - Find a character in an area of memory.
  297. * @s: The memory area
  298. * @c: The byte to search for
  299. * @n: The size of the area.
  300. *
  301. * returns the address of the first occurrence of @c, or 1 byte past
  302. * the area if @c is not found
  303. */
  304. #ifdef __HAVE_ARCH_MEMSCAN
  305. void *memscan(void *s, int c, size_t n)
  306. {
  307. const void *ret = s + n;
  308. asm volatile(
  309. " lgr 0,%[c]\n"
  310. "0: srst %[ret],%[s]\n"
  311. " jo 0b\n"
  312. : [ret] "+&a" (ret), [s] "+&a" (s)
  313. : [c] "d" (c)
  314. : "cc", "memory", "0");
  315. return (void *)ret;
  316. }
  317. EXPORT_SYMBOL(memscan);
  318. #endif