serenity.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/API/POSIX/fcntl.h>
  7. #include <Kernel/API/Syscall.h>
  8. #include <arpa/inet.h>
  9. #include <errno.h>
  10. #include <serenity.h>
  11. #include <string.h>
  12. #include <syscall.h>
  13. extern "C" {
  14. int disown(pid_t pid)
  15. {
  16. int rc = syscall(SC_disown, pid);
  17. __RETURN_WITH_ERRNO(rc, rc, -1);
  18. }
  19. int profiling_enable(pid_t pid, uint64_t event_mask)
  20. {
  21. int rc = syscall(SC_profiling_enable, pid, &event_mask);
  22. __RETURN_WITH_ERRNO(rc, rc, -1);
  23. }
  24. int profiling_disable(pid_t pid)
  25. {
  26. int rc = syscall(SC_profiling_disable, pid);
  27. __RETURN_WITH_ERRNO(rc, rc, -1);
  28. }
  29. int profiling_free_buffer(pid_t pid)
  30. {
  31. int rc = syscall(SC_profiling_free_buffer, pid);
  32. __RETURN_WITH_ERRNO(rc, rc, -1);
  33. }
  34. int futex(uint32_t* userspace_address, int futex_op, uint32_t value, const struct timespec* timeout, uint32_t* userspace_address2, uint32_t value3)
  35. {
  36. int rc;
  37. switch (futex_op & FUTEX_CMD_MASK) {
  38. case FUTEX_WAKE_OP: {
  39. // These interpret timeout as a u32 value for val2
  40. Syscall::SC_futex_params params {
  41. .userspace_address = userspace_address,
  42. .futex_op = futex_op,
  43. .val = value,
  44. .val2 = (FlatPtr)timeout,
  45. .userspace_address2 = userspace_address2,
  46. .val3 = value3
  47. };
  48. rc = syscall(SC_futex, &params);
  49. break;
  50. }
  51. default: {
  52. Syscall::SC_futex_params params {
  53. .userspace_address = userspace_address,
  54. .futex_op = futex_op,
  55. .val = value,
  56. .timeout = timeout,
  57. .userspace_address2 = userspace_address2,
  58. .val3 = value3
  59. };
  60. rc = syscall(SC_futex, &params);
  61. break;
  62. }
  63. }
  64. __RETURN_WITH_ERRNO(rc, rc, -1);
  65. }
  66. int purge(int mode)
  67. {
  68. int rc = syscall(SC_purge, mode);
  69. __RETURN_WITH_ERRNO(rc, rc, -1);
  70. }
  71. int perf_event(int type, uintptr_t arg1, FlatPtr arg2)
  72. {
  73. int rc = syscall(SC_perf_event, type, arg1, arg2);
  74. __RETURN_WITH_ERRNO(rc, rc, -1);
  75. }
  76. int perf_register_string(char const* string, size_t string_length)
  77. {
  78. int rc = syscall(SC_perf_register_string, string, string_length);
  79. __RETURN_WITH_ERRNO(rc, rc, -1);
  80. }
  81. int get_stack_bounds(uintptr_t* user_stack_base, size_t* user_stack_size)
  82. {
  83. int rc = syscall(SC_get_stack_bounds, user_stack_base, user_stack_size);
  84. __RETURN_WITH_ERRNO(rc, rc, -1);
  85. }
  86. int anon_create(size_t size, int options)
  87. {
  88. int rc = syscall(SC_anon_create, size, options);
  89. __RETURN_WITH_ERRNO(rc, rc, -1);
  90. }
  91. int serenity_readlink(char const* path, size_t path_length, char* buffer, size_t buffer_size)
  92. {
  93. Syscall::SC_readlink_params small_params {
  94. { path, path_length },
  95. { buffer, buffer_size },
  96. AT_FDCWD
  97. };
  98. int rc = syscall(SC_readlink, &small_params);
  99. __RETURN_WITH_ERRNO(rc, rc, -1);
  100. }
  101. int setkeymap(char const* name, u32 const* map, u32* const shift_map, u32 const* alt_map, u32 const* altgr_map, u32 const* shift_altgr_map)
  102. {
  103. Syscall::SC_setkeymap_params params { map, shift_map, alt_map, altgr_map, shift_altgr_map, { name, strlen(name) } };
  104. return syscall(SC_setkeymap, &params);
  105. }
  106. 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)
  107. {
  108. Syscall::SC_getkeymap_params params {
  109. map,
  110. shift_map,
  111. alt_map,
  112. altgr_map,
  113. shift_altgr_map,
  114. { name_buffer, name_buffer_size }
  115. };
  116. int rc = syscall(SC_getkeymap, &params);
  117. __RETURN_WITH_ERRNO(rc, rc, -1);
  118. }
  119. u16 internet_checksum(void const* ptr, size_t count)
  120. {
  121. u32 checksum = 0;
  122. auto* w = (u16 const*)ptr;
  123. while (count > 1) {
  124. checksum += ntohs(*w++);
  125. if (checksum & 0x80000000)
  126. checksum = (checksum & 0xffff) | (checksum >> 16);
  127. count -= 2;
  128. }
  129. while (checksum >> 16)
  130. checksum = (checksum & 0xffff) + (checksum >> 16);
  131. return htons(~checksum);
  132. }
  133. int emuctl(uintptr_t command, uintptr_t arg0, uintptr_t arg1)
  134. {
  135. return syscall(SC_emuctl, command, arg0, arg1);
  136. }
  137. int serenity_open(char const* path, size_t path_length, int options, ...)
  138. {
  139. if (!path) {
  140. errno = EFAULT;
  141. return -1;
  142. }
  143. if (path_length > INT32_MAX) {
  144. errno = EINVAL;
  145. return -1;
  146. }
  147. va_list ap;
  148. va_start(ap, options);
  149. auto mode = (mode_t)va_arg(ap, unsigned);
  150. va_end(ap);
  151. Syscall::SC_open_params params { AT_FDCWD, { path, path_length }, options, mode };
  152. int rc = syscall(SC_open, &params);
  153. __RETURN_WITH_ERRNO(rc, rc, -1);
  154. }
  155. }