StdLib.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/MemMem.h>
  28. #include <AK/String.h>
  29. #include <AK/Types.h>
  30. #include <Kernel/Arch/i386/CPU.h>
  31. #include <Kernel/Heap/kmalloc.h>
  32. #include <Kernel/StdLib.h>
  33. #include <Kernel/VM/MemoryManager.h>
  34. String copy_string_from_user(const char* user_str, size_t user_str_size)
  35. {
  36. Kernel::SmapDisabler disabler;
  37. size_t length = strnlen(user_str, user_str_size);
  38. return String(user_str, length);
  39. }
  40. extern "C" {
  41. void copy_to_user(void* dest_ptr, const void* src_ptr, size_t n)
  42. {
  43. ASSERT(Kernel::is_user_range(VirtualAddress(dest_ptr), n));
  44. ASSERT(!Kernel::is_user_range(VirtualAddress(src_ptr), n));
  45. Kernel::SmapDisabler disabler;
  46. memcpy(dest_ptr, src_ptr, n);
  47. }
  48. void copy_from_user(void* dest_ptr, const void* src_ptr, size_t n)
  49. {
  50. ASSERT(Kernel::is_user_range(VirtualAddress(src_ptr), n));
  51. ASSERT(!Kernel::is_user_range(VirtualAddress(dest_ptr), n));
  52. Kernel::SmapDisabler disabler;
  53. memcpy(dest_ptr, src_ptr, n);
  54. }
  55. void* memcpy(void* dest_ptr, const void* src_ptr, size_t n)
  56. {
  57. size_t dest = (size_t)dest_ptr;
  58. size_t src = (size_t)src_ptr;
  59. // FIXME: Support starting at an unaligned address.
  60. if (!(dest & 0x3) && !(src & 0x3) && n >= 12) {
  61. size_t size_ts = n / sizeof(size_t);
  62. asm volatile(
  63. "rep movsl\n"
  64. : "=S"(src), "=D"(dest)
  65. : "S"(src), "D"(dest), "c"(size_ts)
  66. : "memory");
  67. n -= size_ts * sizeof(size_t);
  68. if (n == 0)
  69. return dest_ptr;
  70. }
  71. asm volatile(
  72. "rep movsb\n" ::"S"(src), "D"(dest), "c"(n)
  73. : "memory");
  74. return dest_ptr;
  75. }
  76. void* memmove(void* dest, const void* src, size_t n)
  77. {
  78. if (dest < src)
  79. return memcpy(dest, src, n);
  80. u8* pd = (u8*)dest;
  81. const u8* ps = (const u8*)src;
  82. for (pd += n, ps += n; n--;)
  83. *--pd = *--ps;
  84. return dest;
  85. }
  86. const void* memmem(const void* haystack, size_t haystack_length, const void* needle, size_t needle_length)
  87. {
  88. return AK::memmem(haystack, haystack_length, needle, needle_length);
  89. }
  90. void memset_user(void* dest_ptr, int c, size_t n)
  91. {
  92. ASSERT(Kernel::is_user_range(VirtualAddress(dest_ptr), n));
  93. Kernel::SmapDisabler disabler;
  94. memset(dest_ptr, c, n);
  95. }
  96. void* memset(void* dest_ptr, int c, size_t n)
  97. {
  98. size_t dest = (size_t)dest_ptr;
  99. // FIXME: Support starting at an unaligned address.
  100. if (!(dest & 0x3) && n >= 12) {
  101. size_t size_ts = n / sizeof(size_t);
  102. size_t expanded_c = (u8)c;
  103. expanded_c |= expanded_c << 8;
  104. expanded_c |= expanded_c << 16;
  105. asm volatile(
  106. "rep stosl\n"
  107. : "=D"(dest)
  108. : "D"(dest), "c"(size_ts), "a"(expanded_c)
  109. : "memory");
  110. n -= size_ts * sizeof(size_t);
  111. if (n == 0)
  112. return dest_ptr;
  113. }
  114. asm volatile(
  115. "rep stosb\n"
  116. : "=D"(dest), "=c"(n)
  117. : "0"(dest), "1"(n), "a"(c)
  118. : "memory");
  119. return dest_ptr;
  120. }
  121. size_t strlen(const char* str)
  122. {
  123. size_t len = 0;
  124. while (*(str++))
  125. ++len;
  126. return len;
  127. }
  128. size_t strnlen(const char* str, size_t maxlen)
  129. {
  130. size_t len = 0;
  131. for (; len < maxlen && *str; str++)
  132. len++;
  133. return len;
  134. }
  135. int strcmp(const char* s1, const char* s2)
  136. {
  137. for (; *s1 == *s2; ++s1, ++s2) {
  138. if (*s1 == 0)
  139. return 0;
  140. }
  141. return *(const u8*)s1 < *(const u8*)s2 ? -1 : 1;
  142. }
  143. int memcmp(const void* v1, const void* v2, size_t n)
  144. {
  145. auto* s1 = (const u8*)v1;
  146. auto* s2 = (const u8*)v2;
  147. while (n-- > 0) {
  148. if (*s1++ != *s2++)
  149. return s1[-1] < s2[-1] ? -1 : 1;
  150. }
  151. return 0;
  152. }
  153. int strncmp(const char* s1, const char* s2, size_t n)
  154. {
  155. if (!n)
  156. return 0;
  157. do {
  158. if (*s1 != *s2++)
  159. return *(const unsigned char*)s1 - *(const unsigned char*)--s2;
  160. if (*s1++ == 0)
  161. break;
  162. } while (--n);
  163. return 0;
  164. }
  165. char* strstr(const char* haystack, const char* needle)
  166. {
  167. char nch;
  168. char hch;
  169. if ((nch = *needle++) != 0) {
  170. size_t len = strlen(needle);
  171. do {
  172. do {
  173. if ((hch = *haystack++) == 0)
  174. return nullptr;
  175. } while (hch != nch);
  176. } while (strncmp(haystack, needle, len) != 0);
  177. --haystack;
  178. }
  179. return const_cast<char*>(haystack);
  180. }
  181. void* realloc(void* p, size_t s)
  182. {
  183. return krealloc(p, s);
  184. }
  185. void free(void* p)
  186. {
  187. return kfree(p);
  188. }
  189. // Functions that are automatically called by the C++ compiler.
  190. // Declare them first, to tell the silly compiler that they are indeed being used.
  191. [[noreturn]] void __stack_chk_fail();
  192. [[noreturn]] void __stack_chk_fail_local();
  193. extern "C" int __cxa_atexit(void (*)(void*), void*, void*);
  194. [[noreturn]] void __cxa_pure_virtual();
  195. [[noreturn]] void __stack_chk_fail()
  196. {
  197. ASSERT_NOT_REACHED();
  198. }
  199. [[noreturn]] void __stack_chk_fail_local()
  200. {
  201. ASSERT_NOT_REACHED();
  202. }
  203. extern "C" int __cxa_atexit(void (*)(void*), void*, void*)
  204. {
  205. ASSERT_NOT_REACHED();
  206. return 0;
  207. }
  208. [[noreturn]] void __cxa_pure_virtual()
  209. {
  210. ASSERT_NOT_REACHED();
  211. }
  212. }