2 * Support for RAM backed by mmaped host memory.
4 * Copyright (c) 2015 Red Hat, Inc.
7 * Michael S. Tsirkin <mst@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * later. See the COPYING file in the top-level directory.
14 #include <linux/mman.h>
15 #else /* !CONFIG_LINUX */
17 #define MAP_SHARED_VALIDATE 0x0
18 #endif /* CONFIG_LINUX */
20 #include "qemu/osdep.h"
21 #include "qemu/mmap-alloc.h"
22 #include "qemu/host-utils.h"
24 #define HUGETLBFS_MAGIC 0x958458f6
30 size_t qemu_fd_getpagesize(int fd
)
38 ret
= fstatfs(fd
, &fs
);
39 } while (ret
!= 0 && errno
== EINTR
);
41 if (ret
== 0 && fs
.f_type
== HUGETLBFS_MAGIC
) {
46 /* SPARC Linux needs greater alignment than the pagesize */
47 return QEMU_VMALLOC_ALIGN
;
51 return qemu_real_host_page_size
;
54 size_t qemu_mempath_getpagesize(const char *mem_path
)
62 ret
= statfs(mem_path
, &fs
);
63 } while (ret
!= 0 && errno
== EINTR
);
66 fprintf(stderr
, "Couldn't statfs() memory path: %s\n",
71 if (fs
.f_type
== HUGETLBFS_MAGIC
) {
72 /* It's hugepage, return the huge page size */
77 /* SPARC Linux needs greater alignment than the pagesize */
78 return QEMU_VMALLOC_ALIGN
;
82 return qemu_real_host_page_size
;
85 void *qemu_ram_mmap(int fd
,
94 int map_sync_flags
= 0;
103 * Note: this always allocates at least one extra page of virtual address
104 * space, even if size is already aligned.
106 total
= size
+ align
;
108 #if defined(__powerpc64__) && defined(__linux__)
109 /* On ppc64 mappings in the same segment (aka slice) must share the same
110 * page size. Since we will be re-allocating part of this segment
111 * from the supplied fd, we should make sure to use the same page size, to
112 * this end we mmap the supplied fd. In this case, set MAP_NORESERVE to
113 * avoid allocating backing store memory.
114 * We do this unless we are using the system page size, in which case
115 * anonymous memory is OK.
118 pagesize
= qemu_fd_getpagesize(fd
);
119 if (fd
== -1 || pagesize
== qemu_real_host_page_size
) {
121 flags
|= MAP_ANONYMOUS
;
124 flags
|= MAP_NORESERVE
;
128 pagesize
= qemu_real_host_page_size
;
129 flags
= MAP_PRIVATE
| MAP_ANONYMOUS
;
132 guardptr
= mmap(0, total
, PROT_NONE
, flags
, guardfd
, 0);
134 if (guardptr
== MAP_FAILED
) {
138 assert(is_power_of_2(align
));
139 /* Always align to host page size */
140 assert(align
>= pagesize
);
143 flags
|= fd
== -1 ? MAP_ANONYMOUS
: 0;
144 flags
|= shared
? MAP_SHARED
: MAP_PRIVATE
;
145 if (shared
&& is_pmem
) {
146 map_sync_flags
= MAP_SYNC
| MAP_SHARED_VALIDATE
;
149 offset
= QEMU_ALIGN_UP((uintptr_t)guardptr
, align
) - (uintptr_t)guardptr
;
151 prot
= PROT_READ
| (readonly
? 0 : PROT_WRITE
);
153 ptr
= mmap(guardptr
+ offset
, size
, prot
, flags
| map_sync_flags
, fd
, 0);
155 if (ptr
== MAP_FAILED
&& map_sync_flags
) {
156 if (errno
== ENOTSUP
) {
157 char *proc_link
, *file_name
;
159 proc_link
= g_strdup_printf("/proc/self/fd/%d", fd
);
160 file_name
= g_malloc0(PATH_MAX
);
161 len
= readlink(proc_link
, file_name
, PATH_MAX
- 1);
165 file_name
[len
] = '\0';
166 fprintf(stderr
, "Warning: requesting persistence across crashes "
167 "for backend file %s failed. Proceeding without "
168 "persistence, data might become corrupted in case of host "
169 "crash.\n", file_name
);
174 * if map failed with MAP_SHARED_VALIDATE | MAP_SYNC,
175 * we will remove these flags to handle compatibility.
177 ptr
= mmap(guardptr
+ offset
, size
, prot
, flags
, fd
, 0);
180 if (ptr
== MAP_FAILED
) {
181 munmap(guardptr
, total
);
186 munmap(guardptr
, offset
);
190 * Leave a single PROT_NONE page allocated after the RAM block, to serve as
191 * a guard page guarding against potential buffer overflows.
194 if (total
> size
+ pagesize
) {
195 munmap(ptr
+ size
+ pagesize
, total
- size
- pagesize
);
201 void qemu_ram_munmap(int fd
, void *ptr
, size_t size
)
206 /* Unmap both the RAM block and the guard page */
207 #if defined(__powerpc64__) && defined(__linux__)
208 pagesize
= qemu_fd_getpagesize(fd
);
210 pagesize
= qemu_real_host_page_size
;
212 munmap(ptr
, size
+ pagesize
);