serenity.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2018-2021, 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 <arpa/inet.h>
  27. #include <errno.h>
  28. #include <serenity.h>
  29. #include <string.h>
  30. #include <syscall.h>
  31. extern "C" {
  32. int disown(pid_t pid)
  33. {
  34. int rc = syscall(SC_disown, pid);
  35. __RETURN_WITH_ERRNO(rc, rc, -1);
  36. }
  37. int module_load(const char* path, size_t path_length)
  38. {
  39. int rc = syscall(SC_module_load, path, path_length);
  40. __RETURN_WITH_ERRNO(rc, rc, -1);
  41. }
  42. int module_unload(const char* name, size_t name_length)
  43. {
  44. int rc = syscall(SC_module_unload, name, name_length);
  45. __RETURN_WITH_ERRNO(rc, rc, -1);
  46. }
  47. int profiling_enable(pid_t pid)
  48. {
  49. int rc = syscall(SC_profiling_enable, pid);
  50. __RETURN_WITH_ERRNO(rc, rc, -1);
  51. }
  52. int profiling_disable(pid_t pid)
  53. {
  54. int rc = syscall(SC_profiling_disable, pid);
  55. __RETURN_WITH_ERRNO(rc, rc, -1);
  56. }
  57. int profiling_free_buffer(pid_t pid)
  58. {
  59. int rc = syscall(SC_profiling_free_buffer, pid);
  60. __RETURN_WITH_ERRNO(rc, rc, -1);
  61. }
  62. int futex(uint32_t* userspace_address, int futex_op, uint32_t value, const struct timespec* timeout, uint32_t* userspace_address2, uint32_t value3)
  63. {
  64. int rc;
  65. switch (futex_op & FUTEX_CMD_MASK) {
  66. //case FUTEX_CMP_REQUEUE:
  67. // FUTEX_CMP_REQUEUE_PI:
  68. case FUTEX_WAKE_OP: {
  69. // These interpret timeout as a u32 value for val2
  70. Syscall::SC_futex_params params {
  71. .userspace_address = userspace_address,
  72. .futex_op = futex_op,
  73. .val = value,
  74. .val2 = (FlatPtr)timeout,
  75. .userspace_address2 = userspace_address2,
  76. .val3 = value3
  77. };
  78. rc = syscall(SC_futex, &params);
  79. break;
  80. }
  81. default: {
  82. Syscall::SC_futex_params params {
  83. .userspace_address = userspace_address,
  84. .futex_op = futex_op,
  85. .val = value,
  86. .timeout = timeout,
  87. .userspace_address2 = userspace_address2,
  88. .val3 = value3
  89. };
  90. rc = syscall(SC_futex, &params);
  91. break;
  92. }
  93. }
  94. __RETURN_WITH_ERRNO(rc, rc, -1);
  95. }
  96. int purge(int mode)
  97. {
  98. int rc = syscall(SC_purge, mode);
  99. __RETURN_WITH_ERRNO(rc, rc, -1);
  100. }
  101. int perf_event(int type, uintptr_t arg1, FlatPtr arg2)
  102. {
  103. int rc = syscall(SC_perf_event, type, arg1, arg2);
  104. __RETURN_WITH_ERRNO(rc, rc, -1);
  105. }
  106. int get_stack_bounds(uintptr_t* user_stack_base, size_t* user_stack_size)
  107. {
  108. int rc = syscall(SC_get_stack_bounds, user_stack_base, user_stack_size);
  109. __RETURN_WITH_ERRNO(rc, rc, -1);
  110. }
  111. int anon_create(size_t size, int options)
  112. {
  113. int rc = syscall(SC_anon_create, size, options);
  114. __RETURN_WITH_ERRNO(rc, rc, -1);
  115. }
  116. int serenity_readlink(const char* path, size_t path_length, char* buffer, size_t buffer_size)
  117. {
  118. Syscall::SC_readlink_params small_params {
  119. { path, path_length },
  120. { buffer, buffer_size }
  121. };
  122. int rc = syscall(SC_readlink, &small_params);
  123. __RETURN_WITH_ERRNO(rc, rc, -1);
  124. }
  125. int setkeymap(const char* name, const u32* map, u32* const shift_map, const u32* alt_map, const u32* altgr_map, const u32* shift_altgr_map)
  126. {
  127. Syscall::SC_setkeymap_params params { map, shift_map, alt_map, altgr_map, shift_altgr_map, { name, strlen(name) } };
  128. return syscall(SC_setkeymap, &params);
  129. }
  130. int getkeymap(char* name_buffer, size_t name_buffer_size, u32* map, u32* shift_map, u32* alt_map, u32* altgr_map, u32* shift_altgr_map)
  131. {
  132. Syscall::SC_getkeymap_params params {
  133. map,
  134. shift_map,
  135. alt_map,
  136. altgr_map,
  137. shift_altgr_map,
  138. { name_buffer, name_buffer_size }
  139. };
  140. int rc = syscall(SC_getkeymap, &params);
  141. __RETURN_WITH_ERRNO(rc, rc, -1);
  142. }
  143. u16 internet_checksum(const void* ptr, size_t count)
  144. {
  145. u32 checksum = 0;
  146. auto* w = (const u16*)ptr;
  147. while (count > 1) {
  148. checksum += ntohs(*w++);
  149. if (checksum & 0x80000000)
  150. checksum = (checksum & 0xffff) | (checksum >> 16);
  151. count -= 2;
  152. }
  153. while (checksum >> 16)
  154. checksum = (checksum & 0xffff) + (checksum >> 16);
  155. return htons(~checksum);
  156. }
  157. }