xref: /openbmc/qemu/util/mmap-alloc.c (revision 64c9a921)
1 /*
2  * Support for RAM backed by mmaped host memory.
3  *
4  * Copyright (c) 2015 Red Hat, Inc.
5  *
6  * Authors:
7  *  Michael S. Tsirkin <mst@redhat.com>
8  *
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.
11  */
12 
13 #ifdef CONFIG_LINUX
14 #include <linux/mman.h>
15 #else  /* !CONFIG_LINUX */
16 #define MAP_SYNC              0x0
17 #define MAP_SHARED_VALIDATE   0x0
18 #endif /* CONFIG_LINUX */
19 
20 #include "qemu/osdep.h"
21 #include "qemu/mmap-alloc.h"
22 #include "qemu/host-utils.h"
23 
24 #define HUGETLBFS_MAGIC       0x958458f6
25 
26 #ifdef CONFIG_LINUX
27 #include <sys/vfs.h>
28 #endif
29 
30 size_t qemu_fd_getpagesize(int fd)
31 {
32 #ifdef CONFIG_LINUX
33     struct statfs fs;
34     int ret;
35 
36     if (fd != -1) {
37         do {
38             ret = fstatfs(fd, &fs);
39         } while (ret != 0 && errno == EINTR);
40 
41         if (ret == 0 && fs.f_type == HUGETLBFS_MAGIC) {
42             return fs.f_bsize;
43         }
44     }
45 #ifdef __sparc__
46     /* SPARC Linux needs greater alignment than the pagesize */
47     return QEMU_VMALLOC_ALIGN;
48 #endif
49 #endif
50 
51     return qemu_real_host_page_size;
52 }
53 
54 size_t qemu_mempath_getpagesize(const char *mem_path)
55 {
56 #ifdef CONFIG_LINUX
57     struct statfs fs;
58     int ret;
59 
60     if (mem_path) {
61         do {
62             ret = statfs(mem_path, &fs);
63         } while (ret != 0 && errno == EINTR);
64 
65         if (ret != 0) {
66             fprintf(stderr, "Couldn't statfs() memory path: %s\n",
67                     strerror(errno));
68             exit(1);
69         }
70 
71         if (fs.f_type == HUGETLBFS_MAGIC) {
72             /* It's hugepage, return the huge page size */
73             return fs.f_bsize;
74         }
75     }
76 #ifdef __sparc__
77     /* SPARC Linux needs greater alignment than the pagesize */
78     return QEMU_VMALLOC_ALIGN;
79 #endif
80 #endif
81 
82     return qemu_real_host_page_size;
83 }
84 
85 void *qemu_ram_mmap(int fd,
86                     size_t size,
87                     size_t align,
88                     bool readonly,
89                     bool shared,
90                     bool is_pmem)
91 {
92     int prot;
93     int flags;
94     int map_sync_flags = 0;
95     int guardfd;
96     size_t offset;
97     size_t pagesize;
98     size_t total;
99     void *guardptr;
100     void *ptr;
101 
102     /*
103      * Note: this always allocates at least one extra page of virtual address
104      * space, even if size is already aligned.
105      */
106     total = size + align;
107 
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.
116      */
117     flags = MAP_PRIVATE;
118     pagesize = qemu_fd_getpagesize(fd);
119     if (fd == -1 || pagesize == qemu_real_host_page_size) {
120         guardfd = -1;
121         flags |= MAP_ANONYMOUS;
122     } else {
123         guardfd = fd;
124         flags |= MAP_NORESERVE;
125     }
126 #else
127     guardfd = -1;
128     pagesize = qemu_real_host_page_size;
129     flags = MAP_PRIVATE | MAP_ANONYMOUS;
130 #endif
131 
132     guardptr = mmap(0, total, PROT_NONE, flags, guardfd, 0);
133 
134     if (guardptr == MAP_FAILED) {
135         return MAP_FAILED;
136     }
137 
138     assert(is_power_of_2(align));
139     /* Always align to host page size */
140     assert(align >= pagesize);
141 
142     flags = MAP_FIXED;
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;
147     }
148 
149     offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
150 
151     prot = PROT_READ | (readonly ? 0 : PROT_WRITE);
152 
153     ptr = mmap(guardptr + offset, size, prot, flags | map_sync_flags, fd, 0);
154 
155     if (ptr == MAP_FAILED && map_sync_flags) {
156         if (errno == ENOTSUP) {
157             char *proc_link, *file_name;
158             int len;
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);
162             if (len < 0) {
163                 len = 0;
164             }
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);
170             g_free(proc_link);
171             g_free(file_name);
172         }
173         /*
174          * if map failed with MAP_SHARED_VALIDATE | MAP_SYNC,
175          * we will remove these flags to handle compatibility.
176          */
177         ptr = mmap(guardptr + offset, size, prot, flags, fd, 0);
178     }
179 
180     if (ptr == MAP_FAILED) {
181         munmap(guardptr, total);
182         return MAP_FAILED;
183     }
184 
185     if (offset > 0) {
186         munmap(guardptr, offset);
187     }
188 
189     /*
190      * Leave a single PROT_NONE page allocated after the RAM block, to serve as
191      * a guard page guarding against potential buffer overflows.
192      */
193     total -= offset;
194     if (total > size + pagesize) {
195         munmap(ptr + size + pagesize, total - size - pagesize);
196     }
197 
198     return ptr;
199 }
200 
201 void qemu_ram_munmap(int fd, void *ptr, size_t size)
202 {
203     size_t pagesize;
204 
205     if (ptr) {
206         /* Unmap both the RAM block and the guard page */
207 #if defined(__powerpc64__) && defined(__linux__)
208         pagesize = qemu_fd_getpagesize(fd);
209 #else
210         pagesize = qemu_real_host_page_size;
211 #endif
212         munmap(ptr, size + pagesize);
213     }
214 }
215