1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include <linux/memfd.h>
34 #include <linux/mman.h>
35 
36 __BEGIN_DECLS
37 
38 /** Alternative spelling of the `MAP_ANONYMOUS` flag for mmap(). */
39 #define MAP_ANON MAP_ANONYMOUS
40 
41 /** Return value for mmap(). */
42 #define MAP_FAILED __BIONIC_CAST(reinterpret_cast, void*, -1)
43 
44 /**
45  * [mmap(2)](http://man7.org/linux/man-pages/man2/mmap.2.html)
46  * creates a memory mapping for the given range.
47  *
48  * Returns the address of the mapping on success,
49  * and returns `MAP_FAILED` and sets `errno` on failure.
50  */
51 #if defined(__USE_FILE_OFFSET64)
52 void* mmap(void* __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset) __RENAME(mmap64);
53 #else
54 void* mmap(void* __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset);
55 #endif
56 
57 #if __ANDROID_API__ >= 21
58 /**
59  * mmap64() is a variant of mmap() that takes a 64-bit offset even on LP32.
60  *
61  * See https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md
62  *
63  * mmap64 wasn't really around until L, but we added an inline for it since it
64  * allows a lot more code to compile with _FILE_OFFSET_BITS=64.
65  */
66 void* mmap64(void* __addr, size_t __size, int __prot, int __flags, int __fd, off64_t __offset) __INTRODUCED_IN(21);
67 #endif
68 
69 /**
70  * [munmap(2)](http://man7.org/linux/man-pages/man2/munmap.2.html)
71  * deletes a memory mapping for the given range.
72  *
73  * Returns 0 on success, and returns -1 and sets `errno` on failure.
74  */
75 int munmap(void* __addr, size_t __size);
76 
77 /**
78  * [msync(2)](http://man7.org/linux/man-pages/man2/msync.2.html)
79  * flushes changes to a memory-mapped file to disk.
80  *
81  * Returns 0 on success, and returns -1 and sets `errno` on failure.
82  */
83 int msync(void* __addr, size_t __size, int __flags);
84 
85 /**
86  * [mprotect(2)](http://man7.org/linux/man-pages/man2/mprotect.2.html)
87  * sets the protection on a memory region.
88  *
89  * Returns 0 on success, and returns -1 and sets `errno` on failure.
90  */
91 int mprotect(void* __addr, size_t __size, int __prot);
92 
93 /** Flag for mremap(). */
94 #define MREMAP_MAYMOVE  1
95 
96 /** Flag for mremap(). */
97 #define MREMAP_FIXED    2
98 
99 /**
100  * [mremap(2)](http://man7.org/linux/man-pages/man2/mremap.2.html)
101  * expands or shrinks an existing memory mapping.
102  *
103  * Returns the address of the mapping on success,
104  * and returns `MAP_FAILED` and sets `errno` on failure.
105  */
106 void* mremap(void* __old_addr, size_t __old_size, size_t __new_size, int __flags, ...);
107 
108 /**
109  * [mlockall(2)](http://man7.org/linux/man-pages/man2/mlockall.2.html)
110  * locks pages (preventing swapping).
111  *
112  * Returns 0 on success, and returns -1 and sets `errno` on failure.
113  */
114 int mlockall(int __flags) __INTRODUCED_IN(17);
115 
116 /**
117  * [munlockall(2)](http://man7.org/linux/man-pages/man2/munlockall.2.html)
118  * unlocks pages (allowing swapping).
119  *
120  * Returns 0 on success, and returns -1 and sets `errno` on failure.
121  */
122 int munlockall(void) __INTRODUCED_IN(17);
123 
124 /**
125  * [mlock(2)](http://man7.org/linux/man-pages/man2/mlock.2.html)
126  * locks pages (preventing swapping).
127  *
128  * Returns 0 on success, and returns -1 and sets `errno` on failure.
129  */
130 int mlock(const void* __addr, size_t __size);
131 
132 /**
133  * [mlock2(2)](http://man7.org/linux/man-pages/man2/mlock.2.html)
134  * locks pages (preventing swapping), with optional flags.
135  *
136  * Returns 0 on success, and returns -1 and sets `errno` on failure.
137  */
138 int mlock2(const void* __addr, size_t __size, int __flags) __INTRODUCED_IN(30);
139 
140 /**
141  * [munlock(2)](http://man7.org/linux/man-pages/man2/munlock.2.html)
142  * unlocks pages (allowing swapping).
143  *
144  * Returns 0 on success, and returns -1 and sets `errno` on failure.
145  */
146 int munlock(const void* __addr, size_t __size);
147 
148 /**
149  * [mincore(2)](http://man7.org/linux/man-pages/man2/mincore.2.html)
150  * tests whether pages are resident in memory.
151  *
152  * Returns 0 on success, and returns -1 and sets `errno` on failure.
153  */
154 int mincore(void* __addr, size_t __size, unsigned char* __vector);
155 
156 /**
157  * [madvise(2)](http://man7.org/linux/man-pages/man2/madvise.2.html)
158  * gives the kernel advice about future usage patterns.
159  *
160  * Returns 0 on success, and returns -1 and sets `errno` on failure.
161  */
162 int madvise(void* __addr, size_t __size, int __advice);
163 
164 #if defined(__USE_GNU)
165 
166 /**
167  * [memfd_create(2)](http://man7.org/linux/man-pages/man2/memfd_create.2.html)
168  * creates an anonymous file.
169  *
170  * Returns an fd on success, and returns -1 and sets `errno` on failure.
171  */
172 int memfd_create(const char* __name, unsigned __flags) __INTRODUCED_IN(30);
173 
174 #endif
175 
176 #if __ANDROID_API__ >= 23
177 
178 /*
179  * Some third-party code uses the existence of POSIX_MADV_NORMAL to detect the
180  * availability of posix_madvise. This is not correct, since having up-to-date
181  * UAPI headers says nothing about the C library, but for the time being we
182  * don't want to harm adoption of the unified headers.
183  *
184  * https://github.com/android-ndk/ndk/issues/395
185  */
186 
187 /** Flag for posix_madvise(). */
188 #define POSIX_MADV_NORMAL     MADV_NORMAL
189 /** Flag for posix_madvise(). */
190 #define POSIX_MADV_RANDOM     MADV_RANDOM
191 /** Flag for posix_madvise(). */
192 #define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL
193 /** Flag for posix_madvise(). */
194 #define POSIX_MADV_WILLNEED   MADV_WILLNEED
195 /** Flag for posix_madvise(). */
196 #define POSIX_MADV_DONTNEED   MADV_DONTNEED
197 
198 #endif
199 
200 /**
201  * [posix_madvise(3)](http://man7.org/linux/man-pages/man3/posix_madvise.3.html)
202  * gives the kernel advice about future usage patterns.
203  *
204  * Returns 0 on success, and returns a positive error number on failure.
205  *
206  * See also madvise() which has been available much longer.
207  */
208 int posix_madvise(void* __addr, size_t __size, int __advice) __INTRODUCED_IN(23);
209 
210 __END_DECLS
211 
212 #include <android/legacy_sys_mman_inlines.h>
213