mman.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <bits/pthread_cancel.h>
  8. #include <errno.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/mman.h>
  12. #include <syscall.h>
  13. extern "C" {
  14. void* serenity_mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset, size_t alignment, char const* name)
  15. {
  16. Syscall::SC_mmap_params params { addr, size, alignment, prot, flags, fd, offset, { name, name ? strlen(name) : 0 } };
  17. ptrdiff_t rc = syscall(SC_mmap, &params);
  18. if (rc < 0 && rc > -EMAXERRNO) {
  19. errno = -rc;
  20. return MAP_FAILED;
  21. }
  22. return (void*)rc;
  23. }
  24. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html
  25. void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset)
  26. {
  27. return serenity_mmap(addr, size, prot, flags, fd, offset, PAGE_SIZE, nullptr);
  28. }
  29. void* mmap_with_name(void* addr, size_t size, int prot, int flags, int fd, off_t offset, char const* name)
  30. {
  31. return serenity_mmap(addr, size, prot, flags, fd, offset, PAGE_SIZE, name);
  32. }
  33. void* mremap(void* old_address, size_t old_size, size_t new_size, int flags)
  34. {
  35. Syscall::SC_mremap_params params { old_address, old_size, new_size, flags };
  36. ptrdiff_t rc = syscall(SC_mremap, &params);
  37. if (rc < 0 && rc > -EMAXERRNO) {
  38. errno = -rc;
  39. return MAP_FAILED;
  40. }
  41. return (void*)rc;
  42. }
  43. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/munmap.html
  44. int munmap(void* addr, size_t size)
  45. {
  46. int rc = syscall(SC_munmap, addr, size);
  47. __RETURN_WITH_ERRNO(rc, rc, -1);
  48. }
  49. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/mprotect.html
  50. int mprotect(void* addr, size_t size, int prot)
  51. {
  52. int rc = syscall(SC_mprotect, addr, size, prot);
  53. __RETURN_WITH_ERRNO(rc, rc, -1);
  54. }
  55. int set_mmap_name(void* addr, size_t size, char const* name)
  56. {
  57. if (!name) {
  58. errno = EFAULT;
  59. return -1;
  60. }
  61. Syscall::SC_set_mmap_name_params params { addr, size, { name, strlen(name) } };
  62. int rc = syscall(SC_set_mmap_name, &params);
  63. __RETURN_WITH_ERRNO(rc, rc, -1);
  64. }
  65. int madvise(void* address, size_t size, int advice)
  66. {
  67. int rc = syscall(SC_madvise, address, size, advice);
  68. __RETURN_WITH_ERRNO(rc, rc, -1);
  69. }
  70. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_madvise.html
  71. int posix_madvise(void* address, size_t len, int advice)
  72. {
  73. return madvise(address, len, advice);
  74. }
  75. void* allocate_tls(char const* initial_data, size_t size)
  76. {
  77. ptrdiff_t rc = syscall(SC_allocate_tls, initial_data, size);
  78. if (rc < 0 && rc > -EMAXERRNO) {
  79. errno = -rc;
  80. return MAP_FAILED;
  81. }
  82. return (void*)rc;
  83. }
  84. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/mlock.html
  85. int mlock(void const*, size_t)
  86. {
  87. dbgln("FIXME: Implement mlock()");
  88. return 0;
  89. }
  90. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/munlock.html
  91. int munlock(void const*, size_t)
  92. {
  93. dbgln("FIXME: Implement munlock()");
  94. return 0;
  95. }
  96. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/msync.html
  97. int msync(void* address, size_t size, int flags)
  98. {
  99. __pthread_maybe_cancel();
  100. int rc = syscall(SC_msync, address, size, flags);
  101. __RETURN_WITH_ERRNO(rc, rc, -1);
  102. }
  103. }