diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 611817c135..6f44dffe83 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #define HANDLE_SYSCALL_RETURN_VALUE(syscall_name, rc, success_value) \ if ((rc) < 0) { \ @@ -66,4 +67,22 @@ ErrorOr fcntl(int fd, int command, ...) return rc; } +ErrorOr mmap(void* address, size_t size, int protection, int flags, int fd, off_t offset, [[maybe_unused]] size_t alignment, [[maybe_unused]] StringView name) +{ +#ifdef __serenity__ + Syscall::SC_mmap_params params { (uintptr_t)address, size, alignment, protection, flags, fd, offset, { name.characters_without_null_termination(), name.length() } }; + ptrdiff_t rc = syscall(SC_mmap, ¶ms); + if (rc < 0 && rc > -EMAXERRNO) + return Error::from_syscall("mmap"sv, rc); + return reinterpret_cast(rc); +#else + // NOTE: Regular POSIX mmap() doesn't support custom alignment requests. + VERIFY(!alignment); + auto* ptr = ::mmap(address, size, protection, flags, fd, offset); + if (ptr == MAP_FAILED) + return Error::from_syscall("mmap"sv, -errno); + return ptr; +#endif +} + } diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 9af82b2233..933c3bb71e 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -20,5 +20,6 @@ ErrorOr unveil(StringView path, StringView permissions); ErrorOr sigaction(int signal, struct sigaction const* action, struct sigaction* old_action); ErrorOr fstat(int fd); ErrorOr fcntl(int fd, int command, ...); +ErrorOr mmap(void* address, size_t, int protection, int flags, int fd, off_t, size_t alignment = 0, StringView name = {}); }