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_STRLCPY /* arch function */
35 #define __HAVE_ARCH_STRLEN /* inline & arch function */
36 #define __HAVE_ARCH_STRNCAT /* arch function */
37 #define __HAVE_ARCH_STRNCPY /* arch function */
38 #define __HAVE_ARCH_STRNLEN /* inline & arch function */
39 #define __HAVE_ARCH_STRRCHR /* arch function */
40 #define __HAVE_ARCH_STRSTR /* arch function */
41
42 /* Prototypes for non-inlined arch strings functions. */
43 int memcmp(const void *s1, const void *s2, size_t n);
44 int strcmp(const char *s1, const char *s2);
45 size_t strlcat(char *dest, const char *src, size_t n);
46 size_t strlcpy(char *dest, const char *src, size_t size);
47 char *strncat(char *dest, const char *src, size_t n);
48 char *strncpy(char *dest, const char *src, size_t n);
49 char *strrchr(const char *s, int c);
50 char *strstr(const char *s1, const char *s2);
51 #endif /* !CONFIG_KASAN */
52
53 #undef __HAVE_ARCH_STRCHR
54 #undef __HAVE_ARCH_STRNCHR
55 #undef __HAVE_ARCH_STRNCMP
56 #undef __HAVE_ARCH_STRPBRK
57 #undef __HAVE_ARCH_STRSEP
58 #undef __HAVE_ARCH_STRSPN
59
60 #if defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__)
61
62 extern void *__memcpy(void *dest, const void *src, size_t n);
63 extern void *__memset(void *s, int c, size_t n);
64 extern void *__memmove(void *dest, const void *src, size_t n);
65
66 /*
67 * For files that are not instrumented (e.g. mm/slub.c) we
68 * should use not instrumented version of mem* functions.
69 */
70
71 #define memcpy(dst, src, len) __memcpy(dst, src, len)
72 #define memmove(dst, src, len) __memmove(dst, src, len)
73 #define memset(s, c, n) __memset(s, c, n)
74 #define strlen(s) __strlen(s)
75
76 #define __no_sanitize_prefix_strfunc(x) __##x
77
78 #ifndef __NO_FORTIFY
79 #define __NO_FORTIFY /* FORTIFY_SOURCE uses __builtin_memcpy, etc. */
80 #endif
81
82 #else
83 #define __no_sanitize_prefix_strfunc(x) x
84 #endif /* defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__) */
85
86 void *__memset16(uint16_t *s, uint16_t v, size_t count);
87 void *__memset32(uint32_t *s, uint32_t v, size_t count);
88 void *__memset64(uint64_t *s, uint64_t v, size_t count);
89
memset16(uint16_t * s,uint16_t v,size_t count)90 static inline void *memset16(uint16_t *s, uint16_t v, size_t count)
91 {
92 return __memset16(s, v, count * sizeof(v));
93 }
94
memset32(uint32_t * s,uint32_t v,size_t count)95 static inline void *memset32(uint32_t *s, uint32_t v, size_t count)
96 {
97 return __memset32(s, v, count * sizeof(v));
98 }
99
memset64(uint64_t * s,uint64_t v,size_t count)100 static inline void *memset64(uint64_t *s, uint64_t v, size_t count)
101 {
102 return __memset64(s, v, count * sizeof(v));
103 }
104
105 #if !defined(IN_ARCH_STRING_C) && (!defined(CONFIG_FORTIFY_SOURCE) || defined(__NO_FORTIFY))
106
107 #ifdef __HAVE_ARCH_MEMCHR
memchr(const void * s,int c,size_t n)108 static inline void *memchr(const void * s, int c, size_t n)
109 {
110 const void *ret = s + n;
111
112 asm volatile(
113 " lgr 0,%[c]\n"
114 "0: srst %[ret],%[s]\n"
115 " jo 0b\n"
116 " jl 1f\n"
117 " la %[ret],0\n"
118 "1:"
119 : [ret] "+&a" (ret), [s] "+&a" (s)
120 : [c] "d" (c)
121 : "cc", "memory", "0");
122 return (void *) ret;
123 }
124 #endif
125
126 #ifdef __HAVE_ARCH_MEMSCAN
memscan(void * s,int c,size_t n)127 static inline void *memscan(void *s, int c, size_t n)
128 {
129 const void *ret = s + n;
130
131 asm volatile(
132 " lgr 0,%[c]\n"
133 "0: srst %[ret],%[s]\n"
134 " jo 0b\n"
135 : [ret] "+&a" (ret), [s] "+&a" (s)
136 : [c] "d" (c)
137 : "cc", "memory", "0");
138 return (void *) ret;
139 }
140 #endif
141
142 #ifdef __HAVE_ARCH_STRCAT
strcat(char * dst,const char * src)143 static inline char *strcat(char *dst, const char *src)
144 {
145 unsigned long dummy = 0;
146 char *ret = dst;
147
148 asm volatile(
149 " lghi 0,0\n"
150 "0: srst %[dummy],%[dst]\n"
151 " jo 0b\n"
152 "1: mvst %[dummy],%[src]\n"
153 " jo 1b"
154 : [dummy] "+&a" (dummy), [dst] "+&a" (dst), [src] "+&a" (src)
155 :
156 : "cc", "memory", "0");
157 return ret;
158 }
159 #endif
160
161 #ifdef __HAVE_ARCH_STRCPY
strcpy(char * dst,const char * src)162 static inline char *strcpy(char *dst, const char *src)
163 {
164 char *ret = dst;
165
166 asm volatile(
167 " lghi 0,0\n"
168 "0: mvst %[dst],%[src]\n"
169 " jo 0b"
170 : [dst] "+&a" (dst), [src] "+&a" (src)
171 :
172 : "cc", "memory", "0");
173 return ret;
174 }
175 #endif
176
177 #if defined(__HAVE_ARCH_STRLEN) || (defined(CONFIG_KASAN) && !defined(__SANITIZE_ADDRESS__))
__no_sanitize_prefix_strfunc(strlen)178 static inline size_t __no_sanitize_prefix_strfunc(strlen)(const char *s)
179 {
180 unsigned long end = 0;
181 const char *tmp = s;
182
183 asm volatile(
184 " lghi 0,0\n"
185 "0: srst %[end],%[tmp]\n"
186 " jo 0b"
187 : [end] "+&a" (end), [tmp] "+&a" (tmp)
188 :
189 : "cc", "memory", "0");
190 return end - (unsigned long)s;
191 }
192 #endif
193
194 #ifdef __HAVE_ARCH_STRNLEN
strnlen(const char * s,size_t n)195 static inline size_t strnlen(const char * s, size_t n)
196 {
197 const char *tmp = s;
198 const char *end = s + n;
199
200 asm volatile(
201 " lghi 0,0\n"
202 "0: srst %[end],%[tmp]\n"
203 " jo 0b"
204 : [end] "+&a" (end), [tmp] "+&a" (tmp)
205 :
206 : "cc", "memory", "0");
207 return end - s;
208 }
209 #endif
210 #else /* IN_ARCH_STRING_C */
211 void *memchr(const void * s, int c, size_t n);
212 void *memscan(void *s, int c, size_t n);
213 char *strcat(char *dst, const char *src);
214 char *strcpy(char *dst, const char *src);
215 size_t strlen(const char *s);
216 size_t strnlen(const char * s, size_t n);
217 #endif /* !IN_ARCH_STRING_C */
218
219 #endif /* __S390_STRING_H_ */
220