1
2
3
4
5
6
7
8
9
10
11
12
13#ifdef CONFIG_LINUX
14#include <linux/mman.h>
15#else
16#define MAP_SYNC 0x0
17#define MAP_SHARED_VALIDATE 0x0
18#endif
19
20#include "qemu/osdep.h"
21#include "qemu/mmap-alloc.h"
22#include "qemu/host-utils.h"
23#include "qemu/cutils.h"
24#include "qemu/error-report.h"
25
26#define HUGETLBFS_MAGIC 0x958458f6
27
28#ifdef CONFIG_LINUX
29#include <sys/vfs.h>
30#include <linux/magic.h>
31#endif
32
33QemuFsType qemu_fd_getfs(int fd)
34{
35#ifdef CONFIG_LINUX
36 struct statfs fs;
37 int ret;
38
39 if (fd < 0) {
40 return QEMU_FS_TYPE_UNKNOWN;
41 }
42
43 do {
44 ret = fstatfs(fd, &fs);
45 } while (ret != 0 && errno == EINTR);
46
47 switch (fs.f_type) {
48 case TMPFS_MAGIC:
49 return QEMU_FS_TYPE_TMPFS;
50 case HUGETLBFS_MAGIC:
51 return QEMU_FS_TYPE_HUGETLBFS;
52 default:
53 return QEMU_FS_TYPE_UNKNOWN;
54 }
55#else
56 return QEMU_FS_TYPE_UNKNOWN;
57#endif
58}
59
60size_t qemu_fd_getpagesize(int fd)
61{
62#ifdef CONFIG_LINUX
63 struct statfs fs;
64 int ret;
65
66 if (fd != -1) {
67 do {
68 ret = fstatfs(fd, &fs);
69 } while (ret != 0 && errno == EINTR);
70
71 if (ret == 0 && fs.f_type == HUGETLBFS_MAGIC) {
72 return fs.f_bsize;
73 }
74 }
75#ifdef __sparc__
76
77 return QEMU_VMALLOC_ALIGN;
78#endif
79#endif
80
81 return qemu_real_host_page_size();
82}
83
84#define OVERCOMMIT_MEMORY_PATH "/proc/sys/vm/overcommit_memory"
85static bool map_noreserve_effective(int fd, uint32_t qemu_map_flags)
86{
87#if defined(__linux__)
88 const bool readonly = qemu_map_flags & QEMU_MAP_READONLY;
89 const bool shared = qemu_map_flags & QEMU_MAP_SHARED;
90 gchar *content = NULL;
91 const char *endptr;
92 unsigned int tmp;
93
94
95
96
97
98
99
100
101 if (qemu_fd_getpagesize(fd) != qemu_real_host_page_size()) {
102 return true;
103 }
104
105
106
107
108
109
110
111
112
113 if (readonly || (shared && fd >= 0)) {
114 return true;
115 }
116
117
118
119
120
121
122
123
124
125 if (g_file_get_contents(OVERCOMMIT_MEMORY_PATH, &content, NULL, NULL) &&
126 !qemu_strtoui(content, &endptr, 0, &tmp) &&
127 (!endptr || *endptr == '\n')) {
128 if (tmp == 2) {
129 error_report("Skipping reservation of swap space is not supported:"
130 " \"" OVERCOMMIT_MEMORY_PATH "\" is \"2\"");
131 return false;
132 }
133 return true;
134 }
135
136 error_report("Skipping reservation of swap space is not supported:"
137 " Could not read: \"" OVERCOMMIT_MEMORY_PATH "\"");
138 return false;
139#endif
140
141
142
143
144 error_report("Skipping reservation of swap space is not supported");
145 return false;
146}
147
148
149
150
151
152static void *mmap_reserve(size_t size, int fd)
153{
154 int flags = MAP_PRIVATE;
155
156#if defined(__powerpc64__) && defined(__linux__)
157
158
159
160
161
162
163
164
165
166 if (fd == -1 || qemu_fd_getpagesize(fd) == qemu_real_host_page_size()) {
167 fd = -1;
168 flags |= MAP_ANONYMOUS;
169 } else {
170 flags |= MAP_NORESERVE;
171 }
172#else
173 fd = -1;
174 flags |= MAP_ANONYMOUS;
175#endif
176
177 return mmap(0, size, PROT_NONE, flags, fd, 0);
178}
179
180
181
182
183
184static void *mmap_activate(void *ptr, size_t size, int fd,
185 uint32_t qemu_map_flags, off_t map_offset)
186{
187 const bool noreserve = qemu_map_flags & QEMU_MAP_NORESERVE;
188 const bool readonly = qemu_map_flags & QEMU_MAP_READONLY;
189 const bool shared = qemu_map_flags & QEMU_MAP_SHARED;
190 const bool sync = qemu_map_flags & QEMU_MAP_SYNC;
191 const int prot = PROT_READ | (readonly ? 0 : PROT_WRITE);
192 int map_sync_flags = 0;
193 int flags = MAP_FIXED;
194 void *activated_ptr;
195
196 if (noreserve && !map_noreserve_effective(fd, qemu_map_flags)) {
197 return MAP_FAILED;
198 }
199
200 flags |= fd == -1 ? MAP_ANONYMOUS : 0;
201 flags |= shared ? MAP_SHARED : MAP_PRIVATE;
202 flags |= noreserve ? MAP_NORESERVE : 0;
203 if (shared && sync) {
204 map_sync_flags = MAP_SYNC | MAP_SHARED_VALIDATE;
205 }
206
207 activated_ptr = mmap(ptr, size, prot, flags | map_sync_flags, fd,
208 map_offset);
209 if (activated_ptr == MAP_FAILED && map_sync_flags) {
210 if (errno == ENOTSUP) {
211 char *proc_link = g_strdup_printf("/proc/self/fd/%d", fd);
212 char *file_name = g_malloc0(PATH_MAX);
213 int len = readlink(proc_link, file_name, PATH_MAX - 1);
214
215 if (len < 0) {
216 len = 0;
217 }
218 file_name[len] = '\0';
219 fprintf(stderr, "Warning: requesting persistence across crashes "
220 "for backend file %s failed. Proceeding without "
221 "persistence, data might become corrupted in case of host "
222 "crash.\n", file_name);
223 g_free(proc_link);
224 g_free(file_name);
225 warn_report("Using non DAX backing file with 'pmem=on' option"
226 " is deprecated");
227 }
228
229
230
231
232 activated_ptr = mmap(ptr, size, prot, flags, fd, map_offset);
233 }
234 return activated_ptr;
235}
236
237static inline size_t mmap_guard_pagesize(int fd)
238{
239#if defined(__powerpc64__) && defined(__linux__)
240
241 return qemu_fd_getpagesize(fd);
242#else
243 return qemu_real_host_page_size();
244#endif
245}
246
247void *qemu_ram_mmap(int fd,
248 size_t size,
249 size_t align,
250 uint32_t qemu_map_flags,
251 off_t map_offset)
252{
253 const size_t guard_pagesize = mmap_guard_pagesize(fd);
254 size_t offset, total;
255 void *ptr, *guardptr;
256
257
258
259
260
261 total = size + align;
262
263 guardptr = mmap_reserve(total, fd);
264 if (guardptr == MAP_FAILED) {
265 return MAP_FAILED;
266 }
267
268 assert(is_power_of_2(align));
269
270 assert(align >= guard_pagesize);
271
272 offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
273
274 ptr = mmap_activate(guardptr + offset, size, fd, qemu_map_flags,
275 map_offset);
276 if (ptr == MAP_FAILED) {
277 munmap(guardptr, total);
278 return MAP_FAILED;
279 }
280
281 if (offset > 0) {
282 munmap(guardptr, offset);
283 }
284
285
286
287
288
289 total -= offset;
290 if (total > size + guard_pagesize) {
291 munmap(ptr + size + guard_pagesize, total - size - guard_pagesize);
292 }
293
294 return ptr;
295}
296
297void qemu_ram_munmap(int fd, void *ptr, size_t size)
298{
299 if (ptr) {
300
301 munmap(ptr, size + mmap_guard_pagesize(fd));
302 }
303}
304