spapr_pci: Get rid of duplicate code for node name creation
[qemu/ar7.git] / util / mmap-alloc.c
blob85658854206c8db8ce5867b076af10dab07cebc1
1 /*
2 * Support for RAM backed by mmaped host memory.
4 * Copyright (c) 2015 Red Hat, Inc.
6 * Authors:
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.
13 #include "qemu/osdep.h"
14 #include "qemu/mmap-alloc.h"
15 #include "qemu/host-utils.h"
17 #define HUGETLBFS_MAGIC 0x958458f6
19 #ifdef CONFIG_LINUX
20 #include <sys/vfs.h>
21 #endif
23 size_t qemu_fd_getpagesize(int fd)
25 #ifdef CONFIG_LINUX
26 struct statfs fs;
27 int ret;
29 if (fd != -1) {
30 do {
31 ret = fstatfs(fd, &fs);
32 } while (ret != 0 && errno == EINTR);
34 if (ret == 0 && fs.f_type == HUGETLBFS_MAGIC) {
35 return fs.f_bsize;
38 #ifdef __sparc__
39 /* SPARC Linux needs greater alignment than the pagesize */
40 return QEMU_VMALLOC_ALIGN;
41 #endif
42 #endif
44 return getpagesize();
47 size_t qemu_mempath_getpagesize(const char *mem_path)
49 #ifdef CONFIG_LINUX
50 struct statfs fs;
51 int ret;
53 if (mem_path) {
54 do {
55 ret = statfs(mem_path, &fs);
56 } while (ret != 0 && errno == EINTR);
58 if (ret != 0) {
59 fprintf(stderr, "Couldn't statfs() memory path: %s\n",
60 strerror(errno));
61 exit(1);
64 if (fs.f_type == HUGETLBFS_MAGIC) {
65 /* It's hugepage, return the huge page size */
66 return fs.f_bsize;
69 #ifdef __sparc__
70 /* SPARC Linux needs greater alignment than the pagesize */
71 return QEMU_VMALLOC_ALIGN;
72 #endif
73 #endif
75 return getpagesize();
78 void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared)
80 int flags;
81 int guardfd;
82 size_t offset;
83 size_t pagesize;
84 size_t total;
85 void *guardptr;
86 void *ptr;
89 * Note: this always allocates at least one extra page of virtual address
90 * space, even if size is already aligned.
92 total = size + align;
94 #if defined(__powerpc64__) && defined(__linux__)
95 /* On ppc64 mappings in the same segment (aka slice) must share the same
96 * page size. Since we will be re-allocating part of this segment
97 * from the supplied fd, we should make sure to use the same page size, to
98 * this end we mmap the supplied fd. In this case, set MAP_NORESERVE to
99 * avoid allocating backing store memory.
100 * We do this unless we are using the system page size, in which case
101 * anonymous memory is OK.
103 flags = MAP_PRIVATE;
104 pagesize = qemu_fd_getpagesize(fd);
105 if (fd == -1 || pagesize == getpagesize()) {
106 guardfd = -1;
107 flags |= MAP_ANONYMOUS;
108 } else {
109 guardfd = fd;
110 flags |= MAP_NORESERVE;
112 #else
113 guardfd = -1;
114 pagesize = getpagesize();
115 flags = MAP_PRIVATE | MAP_ANONYMOUS;
116 #endif
118 guardptr = mmap(0, total, PROT_NONE, flags, guardfd, 0);
120 if (guardptr == MAP_FAILED) {
121 return MAP_FAILED;
124 assert(is_power_of_2(align));
125 /* Always align to host page size */
126 assert(align >= pagesize);
128 flags = MAP_FIXED;
129 flags |= fd == -1 ? MAP_ANONYMOUS : 0;
130 flags |= shared ? MAP_SHARED : MAP_PRIVATE;
131 offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
133 ptr = mmap(guardptr + offset, size, PROT_READ | PROT_WRITE, flags, fd, 0);
135 if (ptr == MAP_FAILED) {
136 munmap(guardptr, total);
137 return MAP_FAILED;
140 if (offset > 0) {
141 munmap(guardptr, offset);
145 * Leave a single PROT_NONE page allocated after the RAM block, to serve as
146 * a guard page guarding against potential buffer overflows.
148 total -= offset;
149 if (total > size + pagesize) {
150 munmap(ptr + size + pagesize, total - size - pagesize);
153 return ptr;
156 void qemu_ram_munmap(int fd, void *ptr, size_t size)
158 size_t pagesize;
160 if (ptr) {
161 /* Unmap both the RAM block and the guard page */
162 #if defined(__powerpc64__) && defined(__linux__)
163 pagesize = qemu_fd_getpagesize(fd);
164 #else
165 pagesize = getpagesize();
166 #endif
167 munmap(ptr, size + pagesize);