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 #include <linux/uio.h> 36 37 __BEGIN_DECLS 38 39 /** Alternative spelling of the `MAP_ANONYMOUS` flag for mmap(). */ 40 #define MAP_ANON MAP_ANONYMOUS 41 42 /** Return value for mmap(). */ 43 #define MAP_FAILED __BIONIC_CAST(reinterpret_cast, void*, -1) 44 45 /** 46 * [mmap(2)](https://man7.org/linux/man-pages/man2/mmap.2.html) 47 * creates a memory mapping for the given range. 48 * 49 * Returns the address of the mapping on success, 50 * and returns `MAP_FAILED` and sets `errno` on failure. 51 */ 52 #if defined(__USE_FILE_OFFSET64) 53 void* _Nonnull mmap(void* _Nullable __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset) __RENAME(mmap64); 54 #else 55 void* _Nonnull mmap(void* _Nullable __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset); 56 #endif 57 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/+/main/docs/32-bit-abi.md 62 */ 63 void* _Nonnull mmap64(void* _Nullable __addr, size_t __size, int __prot, int __flags, int __fd, off64_t __offset); 64 65 /** 66 * [munmap(2)](https://man7.org/linux/man-pages/man2/munmap.2.html) 67 * deletes a memory mapping for the given range. 68 * 69 * Returns 0 on success, and returns -1 and sets `errno` on failure. 70 */ 71 int munmap(void* _Nonnull __addr, size_t __size); 72 73 /** 74 * [msync(2)](https://man7.org/linux/man-pages/man2/msync.2.html) 75 * flushes changes to a memory-mapped file to disk. 76 * 77 * Returns 0 on success, and returns -1 and sets `errno` on failure. 78 */ 79 int msync(void* _Nonnull __addr, size_t __size, int __flags); 80 81 /** 82 * [mprotect(2)](https://man7.org/linux/man-pages/man2/mprotect.2.html) 83 * sets the protection on a memory region. 84 * 85 * Returns 0 on success, and returns -1 and sets `errno` on failure. 86 */ 87 int mprotect(void* _Nonnull __addr, size_t __size, int __prot); 88 89 /** Flag for mremap(). */ 90 #define MREMAP_MAYMOVE 1 91 92 /** Flag for mremap(). */ 93 #define MREMAP_FIXED 2 94 95 /** 96 * [mremap(2)](https://man7.org/linux/man-pages/man2/mremap.2.html) 97 * expands or shrinks an existing memory mapping. 98 * 99 * Returns the address of the mapping on success, 100 * and returns `MAP_FAILED` and sets `errno` on failure. 101 */ 102 void* _Nonnull mremap(void* _Nonnull __old_addr, size_t __old_size, size_t __new_size, int __flags, ...); 103 104 /** 105 * [mlockall(2)](https://man7.org/linux/man-pages/man2/mlockall.2.html) 106 * locks pages (preventing swapping). 107 * 108 * Returns 0 on success, and returns -1 and sets `errno` on failure. 109 */ 110 int mlockall(int __flags); 111 112 /** 113 * [munlockall(2)](https://man7.org/linux/man-pages/man2/munlockall.2.html) 114 * unlocks pages (allowing swapping). 115 * 116 * Returns 0 on success, and returns -1 and sets `errno` on failure. 117 */ 118 int munlockall(void); 119 120 /** 121 * [mlock(2)](https://man7.org/linux/man-pages/man2/mlock.2.html) 122 * locks pages (preventing swapping). 123 * 124 * Returns 0 on success, and returns -1 and sets `errno` on failure. 125 */ 126 int mlock(const void* _Nonnull __addr, size_t __size); 127 128 /** 129 * [mlock2(2)](https://man7.org/linux/man-pages/man2/mlock.2.html) 130 * locks pages (preventing swapping), with optional flags. 131 * 132 * Available since API level 30. 133 * 134 * Returns 0 on success, and returns -1 and sets `errno` on failure. 135 */ 136 137 #if __BIONIC_AVAILABILITY_GUARD(30) 138 int mlock2(const void* _Nonnull __addr, size_t __size, int __flags) __INTRODUCED_IN(30); 139 #endif /* __BIONIC_AVAILABILITY_GUARD(30) */ 140 141 142 /** 143 * [munlock(2)](https://man7.org/linux/man-pages/man2/munlock.2.html) 144 * unlocks pages (allowing swapping). 145 * 146 * Returns 0 on success, and returns -1 and sets `errno` on failure. 147 */ 148 int munlock(const void* _Nonnull __addr, size_t __size); 149 150 /** 151 * [mincore(2)](https://man7.org/linux/man-pages/man2/mincore.2.html) 152 * tests whether pages are resident in memory. 153 * 154 * Returns 0 on success, and returns -1 and sets `errno` on failure. 155 */ 156 int mincore(void* _Nonnull __addr, size_t __size, unsigned char* _Nonnull __vector); 157 158 /** 159 * [madvise(2)](https://man7.org/linux/man-pages/man2/madvise.2.html) 160 * gives the kernel advice about future usage patterns. 161 * 162 * Returns 0 on success, and returns -1 and sets `errno` on failure. 163 */ 164 int madvise(void* _Nonnull __addr, size_t __size, int __advice); 165 166 /** 167 * [process_madvise(2)](https://man7.org/linux/man-pages/man2/process_madvise.2.html) 168 * works just like madvise(2) but applies to the process specified by the given 169 * PID file descriptor. 170 * 171 * Available since API level 31. Its sibling process_mrelease() does not have a 172 * libc wrapper and should be called using syscall() instead. Given the lack of 173 * widespread applicability of this system call and the absence of wrappers in 174 * other libcs, it was probably a mistake to have added this wrapper to bionic. 175 * 176 * Returns the number of bytes advised on success, and returns -1 and sets `errno` on failure. 177 */ 178 179 #if __BIONIC_AVAILABILITY_GUARD(31) 180 ssize_t process_madvise(int __pid_fd, const struct iovec* _Nonnull __iov, size_t __count, int __advice, unsigned __flags) __INTRODUCED_IN(31); 181 #endif /* __BIONIC_AVAILABILITY_GUARD(31) */ 182 183 184 #if defined(__USE_GNU) 185 186 /** 187 * [memfd_create(2)](https://man7.org/linux/man-pages/man2/memfd_create.2.html) 188 * creates an anonymous file. 189 * 190 * Available since API level 30. 191 * 192 * Returns an fd on success, and returns -1 and sets `errno` on failure. 193 */ 194 195 #if __BIONIC_AVAILABILITY_GUARD(30) 196 int memfd_create(const char* _Nonnull __name, unsigned __flags) __INTRODUCED_IN(30); 197 #endif /* __BIONIC_AVAILABILITY_GUARD(30) */ 198 199 200 #endif 201 202 #if __ANDROID_API__ >= 23 203 204 /* 205 * Some third-party code uses the existence of POSIX_MADV_NORMAL to detect the 206 * availability of posix_madvise. This is not correct, since having up-to-date 207 * UAPI headers says nothing about the C library, but for the time being we 208 * don't want to harm adoption of the unified headers. 209 * 210 * https://github.com/android-ndk/ndk/issues/395 211 */ 212 213 /** Flag for posix_madvise(). */ 214 #define POSIX_MADV_NORMAL MADV_NORMAL 215 /** Flag for posix_madvise(). */ 216 #define POSIX_MADV_RANDOM MADV_RANDOM 217 /** Flag for posix_madvise(). */ 218 #define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL 219 /** Flag for posix_madvise(). */ 220 #define POSIX_MADV_WILLNEED MADV_WILLNEED 221 /** Flag for posix_madvise(). */ 222 #define POSIX_MADV_DONTNEED MADV_DONTNEED 223 224 #endif 225 226 /** 227 * [posix_madvise(3)](https://man7.org/linux/man-pages/man3/posix_madvise.3.html) 228 * gives the kernel advice about future usage patterns. 229 * 230 * Available since API level 23. 231 * See also madvise() which is available at all API levels. 232 * 233 * Returns 0 on success, and returns a positive error number on failure. 234 */ 235 236 #if __BIONIC_AVAILABILITY_GUARD(23) 237 int posix_madvise(void* _Nonnull __addr, size_t __size, int __advice) __INTRODUCED_IN(23); 238 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */ 239 240 241 /** 242 * [mseal(2)](https://man7.org/linux/man-pages/man2/mseal.2.html) 243 * seals the given range to prevent modifications such as mprotect() calls. 244 * 245 * Available since API level 36. 246 * Requires a Linux 6.10 or newer kernel. 247 * Always fails for 32-bit processes. 248 * 249 * Returns 0 on success, and returns -1 and sets `errno` on failure. 250 */ 251 252 #if __BIONIC_AVAILABILITY_GUARD(36) 253 int mseal(void* _Nonnull __addr, size_t __size, unsigned long __flags) __INTRODUCED_IN(36); 254 #endif /* __BIONIC_AVAILABILITY_GUARD(36) */ 255 256 257 __END_DECLS 258