mman.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <Kernel/Syscall.h>
  2. #include <errno.h>
  3. #include <mman.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. extern "C" {
  7. void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset)
  8. {
  9. return mmap_with_name(addr, size, prot, flags, fd, offset, nullptr);
  10. }
  11. void* mmap_with_name(void* addr, size_t size, int prot, int flags, int fd, off_t offset, const char* name)
  12. {
  13. Syscall::SC_mmap_params params { (u32)addr, size, prot, flags, fd, offset, { name, name ? strlen(name) : 0 } };
  14. int rc = syscall(SC_mmap, &params);
  15. if (rc < 0 && -rc < EMAXERRNO) {
  16. errno = -rc;
  17. return MAP_FAILED;
  18. }
  19. return (void*)rc;
  20. }
  21. int munmap(void* addr, size_t size)
  22. {
  23. int rc = syscall(SC_munmap, addr, size);
  24. __RETURN_WITH_ERRNO(rc, rc, -1);
  25. }
  26. int mprotect(void* addr, size_t size, int prot)
  27. {
  28. int rc = syscall(SC_mprotect, addr, size, prot);
  29. __RETURN_WITH_ERRNO(rc, rc, -1);
  30. }
  31. int set_mmap_name(void* addr, size_t size, const char* name)
  32. {
  33. if (!name) {
  34. errno = EFAULT;
  35. return -1;
  36. }
  37. Syscall::SC_set_mmap_name_params params { addr, size, { name, strlen(name) } };
  38. int rc = syscall(SC_set_mmap_name, &params);
  39. __RETURN_WITH_ERRNO(rc, rc, -1);
  40. }
  41. int madvise(void* address, size_t size, int advice)
  42. {
  43. int rc = syscall(SC_madvise, address, size, advice);
  44. __RETURN_WITH_ERRNO(rc, rc, -1);
  45. }
  46. }