1 // © 2021 Qualcomm Innovation Center, Inc. All rights reserved.
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 // Local version of the standard-defined string.h
6 //
7 // Only the memory block manipulation functions (mem*()) are declared. The
8 // hypervisor has no need to operate on real strings, so the string
9 // manipulation functions (str*()) are left undefined.
10 //
11 // Note: MISRA Required Rule 21.2 states that reserved identifiers are not to
12 // be declared, and gives a memcpy declaration as a specific non-conforming
13 // example. However, the identifiers declared here (including memcpy) are
14 // _not_ reserved: the hypervisor is built in freestanding mode (as asserted
15 // below), which does not guarantee their presence and therefore must not give
16 // them special behaviour (see C18 clause 4, item 6). The Clang/GCC option
17 // -ffreestanding implies -fno-builtin for this reason.
18 //
19 // Also, we _must_ implement these functions ourselves with their standard
20 // semantics (regardless of MISRA 21.2) because the LLVM and GCC backends
21 // assume they are provided by the environment, and will generate calls to
22 // them even when the frontend is in freestanding mode.
23 
24 #if !defined(HYP_STANDALONE_TEST)
25 _Static_assert(__STDC_HOSTED__ == 0,
26 	       "This file deviates from MISRA rule 21.2 in hosted mode");
27 #endif
28 
29 // Define size_t and NULL
30 #include <stddef.h>
31 
32 #define memscpy(s1, s1_size, s2, s2_size)                                      \
33 	((void)memcpy(s1, s2,                                                  \
34 		      ((s1_size) < (s2_size)) ? (s1_size) : (s2_size)),        \
35 	 ((s1_size) < (s2_size)) ? (s1_size) : (s2_size))
36 
37 extern void *
38 memcpy(void *restrict s1, const void *restrict s2, size_t n);
39 
40 extern void *
41 memmove(void *s1, const void *s2, size_t n);
42 
43 extern void *
44 memset(void *s, int c, size_t n);
45 
46 typedef int    errno_t;
47 typedef size_t rsize_t;
48 
49 // A secure memset, guaranteed not to be optimized out
50 extern errno_t
51 memset_s(void *s, rsize_t smax, int c, rsize_t n);
52 
53 extern size_t
54 strlen(const char *str);
55 
56 extern char *
57 strchr(const char *str, int c);
58