xref: /openbmc/linux/arch/s390/include/asm/string.h (revision 745b7908)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  S390 version
4  *    Copyright IBM Corp. 1999
5  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
6  */
7 
8 #ifndef _S390_STRING_H_
9 #define _S390_STRING_H_
10 
11 #ifndef _LINUX_TYPES_H
12 #include <linux/types.h>
13 #endif
14 
15 #define __HAVE_ARCH_MEMCPY	/* gcc builtin & arch function */
16 #define __HAVE_ARCH_MEMMOVE	/* gcc builtin & arch function */
17 #define __HAVE_ARCH_MEMSET	/* gcc builtin & arch function */
18 #define __HAVE_ARCH_MEMSET16	/* arch function */
19 #define __HAVE_ARCH_MEMSET32	/* arch function */
20 #define __HAVE_ARCH_MEMSET64	/* arch function */
21 
22 void *memcpy(void *dest, const void *src, size_t n);
23 void *memset(void *s, int c, size_t n);
24 void *memmove(void *dest, const void *src, size_t n);
25 
26 #ifndef CONFIG_KASAN
27 #define __HAVE_ARCH_MEMCHR	/* inline & arch function */
28 #define __HAVE_ARCH_MEMCMP	/* arch function */
29 #define __HAVE_ARCH_MEMSCAN	/* inline & arch function */
30 #define __HAVE_ARCH_STRCAT	/* inline & arch function */
31 #define __HAVE_ARCH_STRCMP	/* arch function */
32 #define __HAVE_ARCH_STRCPY	/* inline & arch function */
33 #define __HAVE_ARCH_STRLCAT	/* arch function */
34 #define __HAVE_ARCH_STRLEN	/* inline & arch function */
35 #define __HAVE_ARCH_STRNCAT	/* arch function */
36 #define __HAVE_ARCH_STRNCPY	/* arch function */
37 #define __HAVE_ARCH_STRNLEN	/* inline & arch function */
38 #define __HAVE_ARCH_STRSTR	/* arch function */
39 
40 /* Prototypes for non-inlined arch strings functions. */
41 int memcmp(const void *s1, const void *s2, size_t n);
42 int strcmp(const char *s1, const char *s2);
43 size_t strlcat(char *dest, const char *src, size_t n);
44 char *strncat(char *dest, const char *src, size_t n);
45 char *strncpy(char *dest, const char *src, size_t n);
46 char *strstr(const char *s1, const char *s2);
47 #endif /* !CONFIG_KASAN */
48 
49 #undef __HAVE_ARCH_STRCHR
50 #undef __HAVE_ARCH_STRNCHR
51 #undef __HAVE_ARCH_STRNCMP
52 #undef __HAVE_ARCH_STRPBRK
53 #undef __HAVE_ARCH_STRSEP
54 #undef __HAVE_ARCH_STRSPN
55 
56 #if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
57 
58 #define strlen(s) __strlen(s)
59 
60 #define __no_sanitize_prefix_strfunc(x) __##x
61 
62 #ifndef __NO_FORTIFY
63 #define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
64 #endif
65 
66 #else
67 #define __no_sanitize_prefix_strfunc(x) x
68 #endif /* defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__) */
69 
70 void *__memcpy(void *dest, const void *src, size_t n);
71 void *__memset(void *s, int c, size_t n);
72 void *__memmove(void *dest, const void *src, size_t n);
73 void *__memset16(uint16_t *s, uint16_t v, size_t count);
74 void *__memset32(uint32_t *s, uint32_t v, size_t count);
75 void *__memset64(uint64_t *s, uint64_t v, size_t count);
76 
77 static inline void *memset16(uint16_t *s, uint16_t v, size_t count)
78 {
79 	return __memset16(s, v, count * sizeof(v));
80 }
81 
82 static inline void *memset32(uint32_t *s, uint32_t v, size_t count)
83 {
84 	return __memset32(s, v, count * sizeof(v));
85 }
86 
87 static inline void *memset64(uint64_t *s, uint64_t v, size_t count)
88 {
89 	return __memset64(s, v, count * sizeof(v));
90 }
91 
92 #if !defined(IN_ARCH_STRING_C) && (!defined(CONFIG_FORTIFY_SOURCE) || defined(__NO_FORTIFY))
93 
94 #ifdef __HAVE_ARCH_MEMCHR
95 static inline void *memchr(const void * s, int c, size_t n)
96 {
97 	const void *ret = s + n;
98 
99 	asm volatile(
100 		"	lgr	0,%[c]\n"
101 		"0:	srst	%[ret],%[s]\n"
102 		"	jo	0b\n"
103 		"	jl	1f\n"
104 		"	la	%[ret],0\n"
105 		"1:"
106 		: [ret] "+&a" (ret), [s] "+&a" (s)
107 		: [c] "d" (c)
108 		: "cc", "memory", "0");
109 	return (void *) ret;
110 }
111 #endif
112 
113 #ifdef __HAVE_ARCH_MEMSCAN
114 static inline void *memscan(void *s, int c, size_t n)
115 {
116 	const void *ret = s + n;
117 
118 	asm volatile(
119 		"	lgr	0,%[c]\n"
120 		"0:	srst	%[ret],%[s]\n"
121 		"	jo	0b\n"
122 		: [ret] "+&a" (ret), [s] "+&a" (s)
123 		: [c] "d" (c)
124 		: "cc", "memory", "0");
125 	return (void *) ret;
126 }
127 #endif
128 
129 #ifdef __HAVE_ARCH_STRCAT
130 static inline char *strcat(char *dst, const char *src)
131 {
132 	unsigned long dummy = 0;
133 	char *ret = dst;
134 
135 	asm volatile(
136 		"	lghi	0,0\n"
137 		"0:	srst	%[dummy],%[dst]\n"
138 		"	jo	0b\n"
139 		"1:	mvst	%[dummy],%[src]\n"
140 		"	jo	1b"
141 		: [dummy] "+&a" (dummy), [dst] "+&a" (dst), [src] "+&a" (src)
142 		:
143 		: "cc", "memory", "0");
144 	return ret;
145 }
146 #endif
147 
148 #ifdef __HAVE_ARCH_STRCPY
149 static inline char *strcpy(char *dst, const char *src)
150 {
151 	char *ret = dst;
152 
153 	asm volatile(
154 		"	lghi	0,0\n"
155 		"0:	mvst	%[dst],%[src]\n"
156 		"	jo	0b"
157 		: [dst] "+&a" (dst), [src] "+&a" (src)
158 		:
159 		: "cc", "memory", "0");
160 	return ret;
161 }
162 #endif
163 
164 #if defined(__HAVE_ARCH_STRLEN) || (defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__))
165 static inline size_t __no_sanitize_prefix_strfunc(strlen)(const char *s)
166 {
167 	unsigned long end = 0;
168 	const char *tmp = s;
169 
170 	asm volatile(
171 		"	lghi	0,0\n"
172 		"0:	srst	%[end],%[tmp]\n"
173 		"	jo	0b"
174 		: [end] "+&a" (end), [tmp] "+&a" (tmp)
175 		:
176 		: "cc", "memory", "0");
177 	return end - (unsigned long)s;
178 }
179 #endif
180 
181 #ifdef __HAVE_ARCH_STRNLEN
182 static inline size_t strnlen(const char * s, size_t n)
183 {
184 	const char *tmp = s;
185 	const char *end = s + n;
186 
187 	asm volatile(
188 		"	lghi	0,0\n"
189 		"0:	srst	%[end],%[tmp]\n"
190 		"	jo	0b"
191 		: [end] "+&a" (end), [tmp] "+&a" (tmp)
192 		:
193 		: "cc", "memory", "0");
194 	return end - s;
195 }
196 #endif
197 #else /* IN_ARCH_STRING_C */
198 void *memchr(const void * s, int c, size_t n);
199 void *memscan(void *s, int c, size_t n);
200 char *strcat(char *dst, const char *src);
201 char *strcpy(char *dst, const char *src);
202 size_t strlen(const char *s);
203 size_t strnlen(const char * s, size_t n);
204 #endif /* !IN_ARCH_STRING_C */
205 
206 #endif /* __S390_STRING_H_ */
207