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