Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/Format.h>
8#include <bits/pthread_cancel.h>
9#include <errno.h>
10#include <stdio.h>
11#include <string.h>
12#include <sys/mman.h>
13#include <syscall.h>
14
15extern "C" {
16
17void* serenity_mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset, size_t alignment, char const* name)
18{
19 Syscall::SC_mmap_params params { addr, size, alignment, prot, flags, fd, offset, { name, name ? strlen(name) : 0 } };
20 ptrdiff_t rc = syscall(SC_mmap, ¶ms);
21 if (rc < 0 && rc > -EMAXERRNO) {
22 errno = -rc;
23 return MAP_FAILED;
24 }
25 return (void*)rc;
26}
27
28// https://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html
29void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset)
30{
31 return serenity_mmap(addr, size, prot, flags, fd, offset, PAGE_SIZE, nullptr);
32}
33
34void* mmap_with_name(void* addr, size_t size, int prot, int flags, int fd, off_t offset, char const* name)
35{
36 return serenity_mmap(addr, size, prot, flags, fd, offset, PAGE_SIZE, name);
37}
38
39void* mremap(void* old_address, size_t old_size, size_t new_size, int flags)
40{
41 Syscall::SC_mremap_params params { old_address, old_size, new_size, flags };
42 ptrdiff_t rc = syscall(SC_mremap, ¶ms);
43 if (rc < 0 && rc > -EMAXERRNO) {
44 errno = -rc;
45 return MAP_FAILED;
46 }
47 return (void*)rc;
48}
49
50// https://pubs.opengroup.org/onlinepubs/9699919799/functions/munmap.html
51int munmap(void* addr, size_t size)
52{
53 int rc = syscall(SC_munmap, addr, size);
54 __RETURN_WITH_ERRNO(rc, rc, -1);
55}
56
57// https://pubs.opengroup.org/onlinepubs/9699919799/functions/mprotect.html
58int mprotect(void* addr, size_t size, int prot)
59{
60 int rc = syscall(SC_mprotect, addr, size, prot);
61 __RETURN_WITH_ERRNO(rc, rc, -1);
62}
63
64int set_mmap_name(void* addr, size_t size, char const* name)
65{
66 if (!name) {
67 errno = EFAULT;
68 return -1;
69 }
70 Syscall::SC_set_mmap_name_params params { addr, size, { name, strlen(name) } };
71 int rc = syscall(SC_set_mmap_name, ¶ms);
72 __RETURN_WITH_ERRNO(rc, rc, -1);
73}
74
75int madvise(void* address, size_t size, int advice)
76{
77 int rc = syscall(SC_madvise, address, size, advice);
78 __RETURN_WITH_ERRNO(rc, rc, -1);
79}
80
81// https://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_madvise.html
82int posix_madvise(void* address, size_t len, int advice)
83{
84 return madvise(address, len, advice);
85}
86
87void* allocate_tls(char const* initial_data, size_t size)
88{
89 ptrdiff_t rc = syscall(SC_allocate_tls, initial_data, size);
90 if (rc < 0 && rc > -EMAXERRNO) {
91 errno = -rc;
92 return MAP_FAILED;
93 }
94 return (void*)rc;
95}
96
97// https://pubs.opengroup.org/onlinepubs/9699919799/functions/mlock.html
98int mlock(void const*, size_t)
99{
100 dbgln("FIXME: Implement mlock()");
101 return 0;
102}
103
104// https://pubs.opengroup.org/onlinepubs/9699919799/functions/munlock.html
105int munlock(void const*, size_t)
106{
107 dbgln("FIXME: Implement munlock()");
108 return 0;
109}
110
111// https://pubs.opengroup.org/onlinepubs/9699919799/functions/msync.html
112int msync(void* address, size_t size, int flags)
113{
114 __pthread_maybe_cancel();
115
116 int rc = syscall(SC_msync, address, size, flags);
117 __RETURN_WITH_ERRNO(rc, rc, -1);
118}
119}