The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | */ |
Elliott Hughes | ff26a16 | 2017-08-17 22:34:21 +0000 | [diff] [blame] | 28 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 29 | #pragma once |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | |
| 31 | #include <sys/cdefs.h> |
| 32 | #include <sys/types.h> |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 33 | #include <linux/memfd.h> |
Christopher Ferris | 24f97eb | 2019-05-20 12:58:13 -0700 | [diff] [blame] | 34 | #include <linux/mman.h> |
Edgar Arriaga | 128ef8e | 2020-12-23 14:59:56 -0800 | [diff] [blame] | 35 | #include <linux/uio.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | |
| 37 | __BEGIN_DECLS |
| 38 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 39 | /** Alternative spelling of the `MAP_ANONYMOUS` flag for mmap(). */ |
| 40 | #define MAP_ANON MAP_ANONYMOUS |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 42 | /** Return value for mmap(). */ |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 43 | #define MAP_FAILED __BIONIC_CAST(reinterpret_cast, void*, -1) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 44 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 45 | /** |
| 46 | * [mmap(2)](http://man7.org/linux/man-pages/man2/mmap.2.html) |
| 47 | * creates a memory mapping for the given range. |
Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 48 | * |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 49 | * Returns the address of the mapping on success, |
| 50 | * and returns `MAP_FAILED` and sets `errno` on failure. |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 51 | */ |
Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 52 | #if defined(__USE_FILE_OFFSET64) |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 53 | void* _Nonnull mmap(void* _Nullable __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset) __RENAME(mmap64); |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 54 | #else |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 55 | void* _Nonnull mmap(void* _Nullable __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset); |
Elliott Hughes | a348174 | 2017-11-28 14:47:17 -0800 | [diff] [blame] | 56 | #endif |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 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 | * |
Elliott Hughes | 19ed204 | 2023-02-24 00:41:30 +0000 | [diff] [blame] | 63 | * Available since API level 21. |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 64 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 65 | void* _Nonnull mmap64(void* _Nullable __addr, size_t __size, int __prot, int __flags, int __fd, off64_t __offset) __INTRODUCED_IN(21); |
Elliott Hughes | 68dc20d | 2015-02-06 22:28:49 -0800 | [diff] [blame] | 66 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 67 | /** |
| 68 | * [munmap(2)](http://man7.org/linux/man-pages/man2/munmap.2.html) |
| 69 | * deletes a memory mapping for the given range. |
| 70 | * |
| 71 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 72 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 73 | int munmap(void* _Nonnull __addr, size_t __size); |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * [msync(2)](http://man7.org/linux/man-pages/man2/msync.2.html) |
| 77 | * flushes changes to a memory-mapped file to disk. |
| 78 | * |
| 79 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 80 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 81 | int msync(void* _Nonnull __addr, size_t __size, int __flags); |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * [mprotect(2)](http://man7.org/linux/man-pages/man2/mprotect.2.html) |
| 85 | * sets the protection on a memory region. |
| 86 | * |
| 87 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 88 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 89 | int mprotect(void* _Nonnull __addr, size_t __size, int __prot); |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 90 | |
| 91 | /** Flag for mremap(). */ |
| 92 | #define MREMAP_MAYMOVE 1 |
| 93 | |
| 94 | /** Flag for mremap(). */ |
| 95 | #define MREMAP_FIXED 2 |
| 96 | |
| 97 | /** |
| 98 | * [mremap(2)](http://man7.org/linux/man-pages/man2/mremap.2.html) |
| 99 | * expands or shrinks an existing memory mapping. |
| 100 | * |
| 101 | * Returns the address of the mapping on success, |
| 102 | * and returns `MAP_FAILED` and sets `errno` on failure. |
| 103 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 104 | void* _Nonnull mremap(void* _Nonnull __old_addr, size_t __old_size, size_t __new_size, int __flags, ...); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 105 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 106 | /** |
| 107 | * [mlockall(2)](http://man7.org/linux/man-pages/man2/mlockall.2.html) |
| 108 | * locks pages (preventing swapping). |
| 109 | * |
Elliott Hughes | d4fd67c | 2022-11-30 19:53:33 +0000 | [diff] [blame] | 110 | * Available since API level 17. |
| 111 | * |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 112 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 113 | */ |
Elliott Hughes | ff26a16 | 2017-08-17 22:34:21 +0000 | [diff] [blame] | 114 | int mlockall(int __flags) __INTRODUCED_IN(17); |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * [munlockall(2)](http://man7.org/linux/man-pages/man2/munlockall.2.html) |
| 118 | * unlocks pages (allowing swapping). |
| 119 | * |
Elliott Hughes | d4fd67c | 2022-11-30 19:53:33 +0000 | [diff] [blame] | 120 | * Available since API level 17. |
| 121 | * |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 122 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 123 | */ |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 124 | int munlockall(void) __INTRODUCED_IN(17); |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 125 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 126 | /** |
| 127 | * [mlock(2)](http://man7.org/linux/man-pages/man2/mlock.2.html) |
| 128 | * locks pages (preventing swapping). |
| 129 | * |
| 130 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 131 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 132 | int mlock(const void* _Nonnull __addr, size_t __size); |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * [mlock2(2)](http://man7.org/linux/man-pages/man2/mlock.2.html) |
| 136 | * locks pages (preventing swapping), with optional flags. |
| 137 | * |
Elliott Hughes | d4fd67c | 2022-11-30 19:53:33 +0000 | [diff] [blame] | 138 | * Available since API level 30. |
| 139 | * |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 140 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 141 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 142 | int mlock2(const void* _Nonnull __addr, size_t __size, int __flags) __INTRODUCED_IN(30); |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * [munlock(2)](http://man7.org/linux/man-pages/man2/munlock.2.html) |
| 146 | * unlocks pages (allowing swapping). |
| 147 | * |
| 148 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 149 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 150 | int munlock(const void* _Nonnull __addr, size_t __size); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 151 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 152 | /** |
| 153 | * [mincore(2)](http://man7.org/linux/man-pages/man2/mincore.2.html) |
| 154 | * tests whether pages are resident in memory. |
| 155 | * |
| 156 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 157 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 158 | int mincore(void* _Nonnull __addr, size_t __size, unsigned char* _Nonnull __vector); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 159 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 160 | /** |
| 161 | * [madvise(2)](http://man7.org/linux/man-pages/man2/madvise.2.html) |
| 162 | * gives the kernel advice about future usage patterns. |
| 163 | * |
| 164 | * Returns 0 on success, and returns -1 and sets `errno` on failure. |
| 165 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 166 | int madvise(void* _Nonnull __addr, size_t __size, int __advice); |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 167 | |
Edgar Arriaga | 128ef8e | 2020-12-23 14:59:56 -0800 | [diff] [blame] | 168 | /** |
| 169 | * [process_madvise(2)](http://man7.org/linux/man-pages/man2/process_madvise.2.html) |
| 170 | * works just like madvise(2) but applies to the process specified by the given |
| 171 | * PID file descriptor. |
| 172 | * |
Elliott Hughes | ee41a35 | 2022-09-27 19:27:10 +0000 | [diff] [blame] | 173 | * Available since API level 31. Its sibling process_mrelease() does not have a |
| 174 | * libc wrapper and should be called using syscall() instead. Given the lack of |
| 175 | * widespread applicability of this system call and the absence of wrappers in |
| 176 | * other libcs, it was probably a mistake to have added this wrapper to bionic. |
Elliott Hughes | d4fd67c | 2022-11-30 19:53:33 +0000 | [diff] [blame] | 177 | * |
| 178 | * Returns the number of bytes advised on success, and returns -1 and sets `errno` on failure. |
Edgar Arriaga | 128ef8e | 2020-12-23 14:59:56 -0800 | [diff] [blame] | 179 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 180 | ssize_t process_madvise(int __pid_fd, const struct iovec* _Nonnull __iov, size_t __count, int __advice, unsigned __flags) __INTRODUCED_IN(31); |
Edgar Arriaga | 128ef8e | 2020-12-23 14:59:56 -0800 | [diff] [blame] | 181 | |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 182 | #if defined(__USE_GNU) |
| 183 | |
| 184 | /** |
| 185 | * [memfd_create(2)](http://man7.org/linux/man-pages/man2/memfd_create.2.html) |
| 186 | * creates an anonymous file. |
| 187 | * |
Elliott Hughes | d4fd67c | 2022-11-30 19:53:33 +0000 | [diff] [blame] | 188 | * Available since API level 30. |
| 189 | * |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 190 | * Returns an fd on success, and returns -1 and sets `errno` on failure. |
| 191 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 192 | int memfd_create(const char* _Nonnull __name, unsigned __flags) __INTRODUCED_IN(30); |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 193 | |
| 194 | #endif |
| 195 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 196 | #if __ANDROID_API__ >= 23 |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 197 | |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 198 | /* |
| 199 | * Some third-party code uses the existence of POSIX_MADV_NORMAL to detect the |
| 200 | * availability of posix_madvise. This is not correct, since having up-to-date |
| 201 | * UAPI headers says nothing about the C library, but for the time being we |
Elliott Hughes | 00fedf5 | 2017-07-05 15:23:50 -0700 | [diff] [blame] | 202 | * don't want to harm adoption of the unified headers. |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 203 | * |
| 204 | * https://github.com/android-ndk/ndk/issues/395 |
| 205 | */ |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 206 | |
| 207 | /** Flag for posix_madvise(). */ |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 208 | #define POSIX_MADV_NORMAL MADV_NORMAL |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 209 | /** Flag for posix_madvise(). */ |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 210 | #define POSIX_MADV_RANDOM MADV_RANDOM |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 211 | /** Flag for posix_madvise(). */ |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 212 | #define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 213 | /** Flag for posix_madvise(). */ |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 214 | #define POSIX_MADV_WILLNEED MADV_WILLNEED |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 215 | /** Flag for posix_madvise(). */ |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 216 | #define POSIX_MADV_DONTNEED MADV_DONTNEED |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 217 | |
Elliott Hughes | 2eab77e | 2017-06-01 14:08:58 -0700 | [diff] [blame] | 218 | #endif |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * [posix_madvise(3)](http://man7.org/linux/man-pages/man3/posix_madvise.3.html) |
| 222 | * gives the kernel advice about future usage patterns. |
| 223 | * |
Elliott Hughes | d4fd67c | 2022-11-30 19:53:33 +0000 | [diff] [blame] | 224 | * Available since API level 23. |
| 225 | * See also madvise() which is available at all API levels. |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 226 | * |
Elliott Hughes | d4fd67c | 2022-11-30 19:53:33 +0000 | [diff] [blame] | 227 | * Returns 0 on success, and returns a positive error number on failure. |
Elliott Hughes | 3d24d2b | 2019-08-05 13:53:01 -0700 | [diff] [blame] | 228 | */ |
zijunzhao | 2572571 | 2023-05-04 23:29:00 +0000 | [diff] [blame^] | 229 | int posix_madvise(void* _Nonnull __addr, size_t __size, int __advice) __INTRODUCED_IN(23); |
Yabin Cui | 5afae64 | 2014-11-25 20:17:27 -0800 | [diff] [blame] | 230 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 231 | __END_DECLS |