1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3 * mm definition for NOLIBC
4 * Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
5 */
6
7 /* make sure to include all global symbols */
8 #include "../nolibc.h"
9
10 #ifndef _NOLIBC_SYS_MMAN_H
11 #define _NOLIBC_SYS_MMAN_H
12
13 #include "../arch.h"
14 #include "../sys.h"
15
16 #ifndef sys_mmap
17 static __attribute__((unused))
sys_mmap(void * addr,size_t length,int prot,int flags,int fd,off_t offset)18 void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
19 off_t offset)
20 {
21 int n;
22
23 #if defined(__NR_mmap2)
24 n = __NR_mmap2;
25 offset >>= 12;
26 #else
27 n = __NR_mmap;
28 #endif
29
30 return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset);
31 }
32 #endif
33
34 /* Note that on Linux, MAP_FAILED is -1 so we can use the generic __sysret()
35 * which returns -1 upon error and still satisfy user land that checks for
36 * MAP_FAILED.
37 */
38
39 static __attribute__((unused))
mmap(void * addr,size_t length,int prot,int flags,int fd,off_t offset)40 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
41 {
42 void *ret = sys_mmap(addr, length, prot, flags, fd, offset);
43
44 if ((unsigned long)ret >= -4095UL) {
45 SET_ERRNO(-(long)ret);
46 ret = MAP_FAILED;
47 }
48 return ret;
49 }
50
51 static __attribute__((unused))
sys_mremap(void * old_address,size_t old_size,size_t new_size,int flags,void * new_address)52 void *sys_mremap(void *old_address, size_t old_size, size_t new_size, int flags, void *new_address)
53 {
54 return (void *)my_syscall5(__NR_mremap, old_address, old_size,
55 new_size, flags, new_address);
56 }
57
58 static __attribute__((unused))
mremap(void * old_address,size_t old_size,size_t new_size,int flags,void * new_address)59 void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, void *new_address)
60 {
61 void *ret = sys_mremap(old_address, old_size, new_size, flags, new_address);
62
63 if ((unsigned long)ret >= -4095UL) {
64 SET_ERRNO(-(long)ret);
65 ret = MAP_FAILED;
66 }
67 return ret;
68 }
69
70 static __attribute__((unused))
sys_munmap(void * addr,size_t length)71 int sys_munmap(void *addr, size_t length)
72 {
73 return my_syscall2(__NR_munmap, addr, length);
74 }
75
76 static __attribute__((unused))
munmap(void * addr,size_t length)77 int munmap(void *addr, size_t length)
78 {
79 return __sysret(sys_munmap(addr, length));
80 }
81
82 #endif /* _NOLIBC_SYS_MMAN_H */
83