xref: /openbmc/qemu/include/qemu/mmap-alloc.h (revision fa45f8da)
1 #ifndef QEMU_MMAP_ALLOC_H
2 #define QEMU_MMAP_ALLOC_H
3 
4 typedef enum {
5     QEMU_FS_TYPE_UNKNOWN = 0,
6     QEMU_FS_TYPE_TMPFS,
7     QEMU_FS_TYPE_HUGETLBFS,
8     QEMU_FS_TYPE_NUM,
9 } QemuFsType;
10 
11 size_t qemu_fd_getpagesize(int fd);
12 QemuFsType qemu_fd_getfs(int fd);
13 
14 /**
15  * qemu_ram_mmap: mmap anonymous memory, the specified file or device.
16  *
17  * mmap() abstraction to map guest RAM, simplifying flag handling, taking
18  * care of alignment requirements and installing guard pages.
19  *
20  * Parameters:
21  *  @fd: the file or the device to mmap
22  *  @size: the number of bytes to be mmaped
23  *  @align: if not zero, specify the alignment of the starting mapping address;
24  *          otherwise, the alignment in use will be determined by QEMU.
25  *  @qemu_map_flags: QEMU_MAP_* flags
26  *  @map_offset: map starts at offset of map_offset from the start of fd
27  *
28  * Internally, MAP_PRIVATE, MAP_ANONYMOUS and MAP_SHARED_VALIDATE are set
29  * implicitly based on other parameters.
30  *
31  * Return:
32  *  On success, return a pointer to the mapped area.
33  *  On failure, return MAP_FAILED.
34  */
35 void *qemu_ram_mmap(int fd,
36                     size_t size,
37                     size_t align,
38                     uint32_t qemu_map_flags,
39                     off_t map_offset);
40 
41 void qemu_ram_munmap(int fd, void *ptr, size_t size);
42 
43 /*
44  * Abstraction of PROT_ and MAP_ flags as passed to mmap(), for example,
45  * consumed by qemu_ram_mmap().
46  */
47 
48 /* Map PROT_READ instead of PROT_READ | PROT_WRITE. */
49 #define QEMU_MAP_READONLY   (1 << 0)
50 
51 /* Use MAP_SHARED instead of MAP_PRIVATE. */
52 #define QEMU_MAP_SHARED     (1 << 1)
53 
54 /*
55  * Use MAP_SYNC | MAP_SHARED_VALIDATE if supported. Ignored without
56  * QEMU_MAP_SHARED. If mapping fails, warn and fallback to !QEMU_MAP_SYNC.
57  */
58 #define QEMU_MAP_SYNC       (1 << 2)
59 
60 /*
61  * Use MAP_NORESERVE to skip reservation of swap space (or huge pages if
62  * applicable). Bail out if not supported/effective.
63  */
64 #define QEMU_MAP_NORESERVE  (1 << 3)
65 
66 #endif
67