From 53e9b9758e470950141b609526c22d6de405e9b1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 23 Nov 2021 11:47:32 +0100 Subject: [PATCH] LibCore: Add syscall wrapper for mmap() For convenience on SerenityOS, this also takes a custom alignment request, and a memory region name. These are non-POSIX extensions. --- Userland/Libraries/LibCore/System.cpp | 19 +++++++++++++++++++ Userland/Libraries/LibCore/System.h | 1 + 2 files changed, 20 insertions(+) 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 = {}); }