mman.cpp 3.2 KB

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