blob: fea93329df662e70820d4c2b7edb5c3cba8d654b [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Hughesff26a162017-08-17 22:34:21 +000028
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070029#pragma once
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030
31#include <sys/cdefs.h>
32#include <sys/types.h>
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070033#include <linux/memfd.h>
Christopher Ferris24f97eb2019-05-20 12:58:13 -070034#include <linux/mman.h>
Edgar Arriaga128ef8e2020-12-23 14:59:56 -080035#include <linux/uio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036
37__BEGIN_DECLS
38
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070039/** Alternative spelling of the `MAP_ANONYMOUS` flag for mmap(). */
40#define MAP_ANON MAP_ANONYMOUS
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070042/** Return value for mmap(). */
Elliott Hughes5470c182016-07-22 11:36:17 -070043#define MAP_FAILED __BIONIC_CAST(reinterpret_cast, void*, -1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070045/**
46 * [mmap(2)](http://man7.org/linux/man-pages/man2/mmap.2.html)
47 * creates a memory mapping for the given range.
Elliott Hughesa3481742017-11-28 14:47:17 -080048 *
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070049 * Returns the address of the mapping on success,
50 * and returns `MAP_FAILED` and sets `errno` on failure.
Dan Alberta613d0d2017-10-05 16:39:33 -070051 */
Elliott Hughesa3481742017-11-28 14:47:17 -080052#if defined(__USE_FILE_OFFSET64)
Elliott Hughes0d1a8a52018-07-24 19:36:51 +000053void* mmap(void* __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset) __RENAME(mmap64);
Elliott Hughes68dc20d2015-02-06 22:28:49 -080054#else
Elliott Hughesff26a162017-08-17 22:34:21 +000055void* mmap(void* __addr, size_t __size, int __prot, int __flags, int __fd, off_t __offset);
Elliott Hughesa3481742017-11-28 14:47:17 -080056#endif
Elliott Hughes2eab77e2017-06-01 14:08:58 -070057
Elliott Hughes95c6cd72019-12-20 13:26:14 -080058#if __ANDROID_API__ >= 21
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070059/**
60 * mmap64() is a variant of mmap() that takes a 64-bit offset even on LP32.
61 *
62 * See https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md
63 *
64 * mmap64 wasn't really around until L, but we added an inline for it since it
65 * allows a lot more code to compile with _FILE_OFFSET_BITS=64.
66 */
Elliott Hughesff26a162017-08-17 22:34:21 +000067void* mmap64(void* __addr, size_t __size, int __prot, int __flags, int __fd, off64_t __offset) __INTRODUCED_IN(21);
Dan Alberta613d0d2017-10-05 16:39:33 -070068#endif
Elliott Hughes68dc20d2015-02-06 22:28:49 -080069
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070070/**
71 * [munmap(2)](http://man7.org/linux/man-pages/man2/munmap.2.html)
72 * deletes a memory mapping for the given range.
73 *
74 * Returns 0 on success, and returns -1 and sets `errno` on failure.
75 */
Elliott Hughesff26a162017-08-17 22:34:21 +000076int munmap(void* __addr, size_t __size);
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070077
78/**
79 * [msync(2)](http://man7.org/linux/man-pages/man2/msync.2.html)
80 * flushes changes to a memory-mapped file to disk.
81 *
82 * Returns 0 on success, and returns -1 and sets `errno` on failure.
83 */
Elliott Hughesff26a162017-08-17 22:34:21 +000084int msync(void* __addr, size_t __size, int __flags);
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070085
86/**
87 * [mprotect(2)](http://man7.org/linux/man-pages/man2/mprotect.2.html)
88 * sets the protection on a memory region.
89 *
90 * Returns 0 on success, and returns -1 and sets `errno` on failure.
91 */
Elliott Hughesff26a162017-08-17 22:34:21 +000092int mprotect(void* __addr, size_t __size, int __prot);
Elliott Hughes3d24d2b2019-08-05 13:53:01 -070093
94/** Flag for mremap(). */
95#define MREMAP_MAYMOVE 1
96
97/** Flag for mremap(). */
98#define MREMAP_FIXED 2
99
100/**
101 * [mremap(2)](http://man7.org/linux/man-pages/man2/mremap.2.html)
102 * expands or shrinks an existing memory mapping.
103 *
104 * Returns the address of the mapping on success,
105 * and returns `MAP_FAILED` and sets `errno` on failure.
106 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000107void* mremap(void* __old_addr, size_t __old_size, size_t __new_size, int __flags, ...);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700109/**
110 * [mlockall(2)](http://man7.org/linux/man-pages/man2/mlockall.2.html)
111 * locks pages (preventing swapping).
112 *
Elliott Hughesd4fd67c2022-11-30 19:53:33 +0000113 * Available since API level 17.
114 *
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700115 * Returns 0 on success, and returns -1 and sets `errno` on failure.
116 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000117int mlockall(int __flags) __INTRODUCED_IN(17);
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700118
119/**
120 * [munlockall(2)](http://man7.org/linux/man-pages/man2/munlockall.2.html)
121 * unlocks pages (allowing swapping).
122 *
Elliott Hughesd4fd67c2022-11-30 19:53:33 +0000123 * Available since API level 17.
124 *
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700125 * Returns 0 on success, and returns -1 and sets `errno` on failure.
126 */
Elliott Hughes5470c182016-07-22 11:36:17 -0700127int munlockall(void) __INTRODUCED_IN(17);
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700128
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700129/**
130 * [mlock(2)](http://man7.org/linux/man-pages/man2/mlock.2.html)
131 * locks pages (preventing swapping).
132 *
133 * Returns 0 on success, and returns -1 and sets `errno` on failure.
134 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000135int mlock(const void* __addr, size_t __size);
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700136
137/**
138 * [mlock2(2)](http://man7.org/linux/man-pages/man2/mlock.2.html)
139 * locks pages (preventing swapping), with optional flags.
140 *
Elliott Hughesd4fd67c2022-11-30 19:53:33 +0000141 * Available since API level 30.
142 *
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700143 * Returns 0 on success, and returns -1 and sets `errno` on failure.
144 */
145int mlock2(const void* __addr, size_t __size, int __flags) __INTRODUCED_IN(30);
146
147/**
148 * [munlock(2)](http://man7.org/linux/man-pages/man2/munlock.2.html)
149 * unlocks pages (allowing swapping).
150 *
151 * Returns 0 on success, and returns -1 and sets `errno` on failure.
152 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000153int munlock(const void* __addr, size_t __size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700155/**
156 * [mincore(2)](http://man7.org/linux/man-pages/man2/mincore.2.html)
157 * tests whether pages are resident in memory.
158 *
159 * Returns 0 on success, and returns -1 and sets `errno` on failure.
160 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000161int mincore(void* __addr, size_t __size, unsigned char* __vector);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700163/**
164 * [madvise(2)](http://man7.org/linux/man-pages/man2/madvise.2.html)
165 * gives the kernel advice about future usage patterns.
166 *
167 * Returns 0 on success, and returns -1 and sets `errno` on failure.
168 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000169int madvise(void* __addr, size_t __size, int __advice);
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700170
Edgar Arriaga128ef8e2020-12-23 14:59:56 -0800171/**
172 * [process_madvise(2)](http://man7.org/linux/man-pages/man2/process_madvise.2.html)
173 * works just like madvise(2) but applies to the process specified by the given
174 * PID file descriptor.
175 *
Elliott Hughesee41a352022-09-27 19:27:10 +0000176 * Available since API level 31. Its sibling process_mrelease() does not have a
177 * libc wrapper and should be called using syscall() instead. Given the lack of
178 * widespread applicability of this system call and the absence of wrappers in
179 * other libcs, it was probably a mistake to have added this wrapper to bionic.
Elliott Hughesd4fd67c2022-11-30 19:53:33 +0000180 *
181 * Returns the number of bytes advised on success, and returns -1 and sets `errno` on failure.
Edgar Arriaga128ef8e2020-12-23 14:59:56 -0800182 */
Elliott Hughesee41a352022-09-27 19:27:10 +0000183ssize_t process_madvise(int __pid_fd, const struct iovec* __iov, size_t __count, int __advice, unsigned __flags) __INTRODUCED_IN(31);
Edgar Arriaga128ef8e2020-12-23 14:59:56 -0800184
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700185#if defined(__USE_GNU)
186
187/**
188 * [memfd_create(2)](http://man7.org/linux/man-pages/man2/memfd_create.2.html)
189 * creates an anonymous file.
190 *
Elliott Hughesd4fd67c2022-11-30 19:53:33 +0000191 * Available since API level 30.
192 *
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700193 * Returns an fd on success, and returns -1 and sets `errno` on failure.
194 */
195int memfd_create(const char* __name, unsigned __flags) __INTRODUCED_IN(30);
196
197#endif
198
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800199#if __ANDROID_API__ >= 23
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700200
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700201/*
202 * Some third-party code uses the existence of POSIX_MADV_NORMAL to detect the
203 * availability of posix_madvise. This is not correct, since having up-to-date
204 * UAPI headers says nothing about the C library, but for the time being we
Elliott Hughes00fedf52017-07-05 15:23:50 -0700205 * don't want to harm adoption of the unified headers.
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700206 *
207 * https://github.com/android-ndk/ndk/issues/395
208 */
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700209
210/** Flag for posix_madvise(). */
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700211#define POSIX_MADV_NORMAL MADV_NORMAL
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700212/** Flag for posix_madvise(). */
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700213#define POSIX_MADV_RANDOM MADV_RANDOM
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700214/** Flag for posix_madvise(). */
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700215#define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700216/** Flag for posix_madvise(). */
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700217#define POSIX_MADV_WILLNEED MADV_WILLNEED
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700218/** Flag for posix_madvise(). */
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700219#define POSIX_MADV_DONTNEED MADV_DONTNEED
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700220
Elliott Hughes2eab77e2017-06-01 14:08:58 -0700221#endif
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700222
223/**
224 * [posix_madvise(3)](http://man7.org/linux/man-pages/man3/posix_madvise.3.html)
225 * gives the kernel advice about future usage patterns.
226 *
Elliott Hughesd4fd67c2022-11-30 19:53:33 +0000227 * Available since API level 23.
228 * See also madvise() which is available at all API levels.
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700229 *
Elliott Hughesd4fd67c2022-11-30 19:53:33 +0000230 * Returns 0 on success, and returns a positive error number on failure.
Elliott Hughes3d24d2b2019-08-05 13:53:01 -0700231 */
Elliott Hughesff26a162017-08-17 22:34:21 +0000232int posix_madvise(void* __addr, size_t __size, int __advice) __INTRODUCED_IN(23);
Yabin Cui5afae642014-11-25 20:17:27 -0800233
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800234__END_DECLS
235
Dan Alberta613d0d2017-10-05 16:39:33 -0700236#include <android/legacy_sys_mman_inlines.h>