ladybird/Libraries/LibC/mman.cpp
Andreas Kling 24c736b0e7 Kernel: Use the Syscall string and buffer types more
While I was updating syscalls to stop passing null-terminated strings,
I added some helpful struct types:

    - StringArgument { const char*; size_t; }
    - ImmutableBuffer<Data, Size> { const Data*; Size; }
    - MutableBuffer<Data, Size> { Data*; Size; }

The Process class has some convenience functions for validating and
optionally extracting the contents from these structs:

    - get_syscall_path_argument(StringArgument)
    - validate_and_copy_string_from_user(StringArgument)
    - validate(ImmutableBuffer)
    - validate(MutableBuffer)

There's still so much code around this and I'm wondering if we should
generate most of it instead. Possible nice little project.
2020-01-11 12:47:47 +01:00

53 lines
1.3 KiB
C++

#include <Kernel/Syscall.h>
#include <errno.h>
#include <mman.h>
#include <stdio.h>
#include <string.h>
extern "C" {
void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset)
{
return mmap_with_name(addr, size, prot, flags, fd, offset, nullptr);
}
void* mmap_with_name(void* addr, size_t size, int prot, int flags, int fd, off_t offset, const char* name)
{
Syscall::SC_mmap_params params { (u32)addr, size, prot, flags, fd, offset, { name, name ? strlen(name) : 0 } };
int rc = syscall(SC_mmap, &params);
if (rc < 0 && -rc < EMAXERRNO) {
errno = -rc;
return MAP_FAILED;
}
return (void*)rc;
}
int munmap(void* addr, size_t size)
{
int rc = syscall(SC_munmap, addr, size);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int mprotect(void* addr, size_t size, int prot)
{
int rc = syscall(SC_mprotect, addr, size, prot);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int set_mmap_name(void* addr, size_t size, const char* name)
{
if (!name) {
errno = EFAULT;
return -1;
}
Syscall::SC_set_mmap_name_params params { addr, size, { name, strlen(name) } };
int rc = syscall(SC_set_mmap_name, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int madvise(void* address, size_t size, int advice)
{
int rc = syscall(SC_madvise, address, size, advice);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
}